#include "nir.h"
#include "nir_builder.h"
#include "nir_control_flow.h"
+#include "nir_vla.h"
-static bool
-deref_apply_constant_initializer(nir_deref_var *deref, void *state)
-{
- struct nir_builder *b = state;
-
- nir_load_const_instr *initializer =
- nir_deref_get_const_initializer_load(b->shader, deref);
- nir_builder_instr_insert(b, &initializer->instr);
-
- nir_store_deref_var(b, deref, &initializer->def, 0xf);
-
- return true;
-}
-
-static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
-
-static void
-convert_deref_to_param_deref(nir_instr *instr, nir_deref_var **deref,
- nir_call_instr *call)
+void nir_inline_function_impl(struct nir_builder *b,
+ const nir_function_impl *impl,
+ nir_ssa_def **params)
{
- /* This isn't a parameter, just return the deref */
- if ((*deref)->var->data.mode != nir_var_param)
- return;
-
- int param_idx = (*deref)->var->data.location;
+ nir_function_impl *copy = nir_function_impl_clone(b->shader, impl);
- nir_deref_var *call_deref;
- if (param_idx >= 0) {
- assert(param_idx < call->callee->num_params);
- call_deref = call->params[param_idx];
- } else {
- call_deref = call->return_deref;
- }
- assert(call_deref);
-
- /* Now we make a new deref by concatenating the deref in the call's
- * parameter with the deref we were given.
+ /* Insert a nop at the cursor so we can keep track of where things are as
+ * we add/remove stuff from the CFG.
*/
- nir_deref_var *new_deref = nir_deref_as_var(nir_copy_deref(instr, &call_deref->deref));
- nir_deref *new_tail = nir_deref_tail(&new_deref->deref);
- new_tail->child = (*deref)->deref.child;
- ralloc_steal(new_tail, new_tail->child);
- *deref = new_deref;
-}
-
-static void
-rewrite_param_derefs(nir_instr *instr, nir_call_instr *call)
-{
- switch (instr->type) {
- case nir_instr_type_intrinsic: {
- nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
-
- for (unsigned i = 0;
- i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) {
- convert_deref_to_param_deref(instr, &intrin->variables[i], call);
- }
- break;
- }
-
- case nir_instr_type_tex: {
- nir_tex_instr *tex = nir_instr_as_tex(instr);
- if (tex->texture)
- convert_deref_to_param_deref(&tex->instr, &tex->texture, call);
- if (tex->sampler)
- convert_deref_to_param_deref(&tex->instr, &tex->sampler, call);
- break;
- }
-
- default:
- break; /* Nothing else has derefs */
- }
-}
-
-static void
-lower_param_to_local(nir_variable *param, nir_function_impl *impl, bool write)
-{
- if (param->data.mode != nir_var_param)
- return;
+ nir_intrinsic_instr *nop =
+ nir_intrinsic_instr_create(b->shader, nir_intrinsic_nop);
+ nir_builder_instr_insert(b, &nop->instr);
- nir_parameter_type param_type;
- if (param->data.location >= 0) {
- assert(param->data.location < impl->num_params);
- param_type = impl->function->params[param->data.location].param_type;
- } else {
- /* Return variable */
- param_type = nir_parameter_out;
- }
-
- if ((write && param_type == nir_parameter_in) ||
- (!write && param_type == nir_parameter_out)) {
- /* In this case, we need a shadow copy. Turn it into a local */
- param->data.mode = nir_var_local;
- exec_list_push_tail(&impl->locals, ¶m->node);
- }
-}
+ exec_list_append(&b->impl->locals, ©->locals);
+ exec_list_append(&b->impl->registers, ©->registers);
-static bool
-lower_params_to_locals_block(nir_block *block, nir_function_impl *impl)
-{
- nir_foreach_instr(instr, block) {
- if (instr->type != nir_instr_type_intrinsic)
- continue;
+ nir_foreach_block(block, copy) {
+ nir_foreach_instr_safe(instr, block) {
+ /* Returns have to be lowered for this to work */
+ assert(instr->type != nir_instr_type_jump ||
+ nir_instr_as_jump(instr)->type != nir_jump_return);
- nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+ if (instr->type != nir_instr_type_intrinsic)
+ continue;
- switch (intrin->intrinsic) {
- case nir_intrinsic_store_var:
- lower_param_to_local(intrin->variables[0]->var, impl, true);
- break;
+ nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
+ if (load->intrinsic != nir_intrinsic_load_param)
+ continue;
- case nir_intrinsic_copy_var:
- lower_param_to_local(intrin->variables[0]->var, impl, true);
- lower_param_to_local(intrin->variables[1]->var, impl, false);
- break;
+ unsigned param_idx = nir_intrinsic_param_idx(load);
+ assert(param_idx < impl->function->num_params);
+ assert(load->dest.is_ssa);
+ nir_ssa_def_rewrite_uses(&load->dest.ssa,
+ nir_src_for_ssa(params[param_idx]));
- case nir_intrinsic_load_var:
- /* All other intrinsics which access variables (image_load_store)
- * do so in a read-only fasion.
+ /* Remove any left-over load_param intrinsics because they're soon
+ * to be in another function and therefore no longer valid.
*/
- for (unsigned i = 0;
- i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) {
- lower_param_to_local(intrin->variables[i]->var, impl, false);
- }
- break;
-
- default:
- continue;
+ nir_instr_remove(&load->instr);
}
}
- return true;
+ /* Pluck the body out of the function and place it here */
+ nir_cf_list body;
+ nir_cf_list_extract(&body, ©->body);
+ nir_cf_reinsert(&body, nir_before_instr(&nop->instr));
+
+ b->cursor = nir_instr_remove(&nop->instr);
}
+static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);
+
static bool
inline_functions_block(nir_block *block, nir_builder *b,
struct set *inlined)
nir_call_instr *call = nir_instr_as_call(instr);
assert(call->callee->impl);
+ /* Make sure that the function we're calling is already inlined */
inline_function_impl(call->callee->impl, inlined);
- nir_function_impl *callee_copy =
- nir_function_impl_clone(call->callee->impl);
- callee_copy->function = call->callee;
-
- /* Add copies of all in parameters */
- assert(call->num_params == callee_copy->num_params);
-
- b->cursor = nir_before_instr(&call->instr);
-
- /* Before we insert the copy of the function, we need to lower away
- * constant initializers on local variables. This is because constant
- * initializers happen (effectively) at the top of the function and,
- * since these are about to become locals of the calling function,
- * initialization will happen at the top of the caller rather than at
- * the top of the callee. This isn't usually a problem, but if we are
- * being inlined inside of a loop, it can result in the variable not
- * getting re-initialized properly for all loop iterations.
- */
- nir_foreach_variable(local, &callee_copy->locals) {
- if (!local->constant_initializer)
- continue;
-
- nir_deref_var deref;
- deref.deref.deref_type = nir_deref_type_var,
- deref.deref.child = NULL;
- deref.deref.type = local->type,
- deref.var = local;
-
- nir_deref_foreach_leaf(&deref, deref_apply_constant_initializer, b);
-
- local->constant_initializer = NULL;
- }
-
- exec_list_append(&b->impl->locals, &callee_copy->locals);
- exec_list_append(&b->impl->registers, &callee_copy->registers);
+ b->cursor = nir_instr_remove(&call->instr);
- /* We now need to tie the two functions together using the
- * parameters. There are two ways we do this: One is to turn the
- * parameter into a local variable and do a shadow-copy. The other
- * is to treat the parameter as a "proxy" and rewrite derefs to use
- * the actual variable that comes from the call instruction. We
- * implement both schemes. The first is needed in the case where we
- * have an in parameter that we write or similar. The second case is
- * needed for handling things such as images and uniforms properly.
+ /* Rewrite all of the uses of the callee's parameters to use the call
+ * instructions sources. In order to ensure that the "load" happens
+ * here and not later (for register sources), we make sure to convert it
+ * to an SSA value first.
*/
-
- /* Figure out when we need to lower to a shadow local */
- nir_foreach_block(block, callee_copy) {
- lower_params_to_locals_block(block, callee_copy);
- }
-
- for (unsigned i = 0; i < callee_copy->num_params; i++) {
- nir_variable *param = callee_copy->params[i];
-
- if (param->data.mode == nir_var_local &&
- call->callee->params[i].param_type != nir_parameter_out) {
- nir_copy_deref_var(b, nir_deref_var_create(b->shader, param),
- call->params[i]);
- }
- }
-
- nir_foreach_block(block, callee_copy) {
- nir_foreach_instr(instr, block)
- rewrite_param_derefs(instr, call);
- }
-
- /* Pluck the body out of the function and place it here */
- nir_cf_list body;
- nir_cf_list_extract(&body, &callee_copy->body);
- nir_cf_reinsert(&body, b->cursor);
-
- b->cursor = nir_before_instr(&call->instr);
-
- /* Add copies of all out parameters and the return */
- assert(call->num_params == callee_copy->num_params);
- for (unsigned i = 0; i < callee_copy->num_params; i++) {
- nir_variable *param = callee_copy->params[i];
-
- if (param->data.mode == nir_var_local &&
- call->callee->params[i].param_type != nir_parameter_in) {
- nir_copy_deref_var(b, call->params[i],
- nir_deref_var_create(b->shader, param));
- }
- }
- if (!glsl_type_is_void(call->callee->return_type) &&
- callee_copy->return_var->data.mode == nir_var_local) {
- nir_copy_deref_var(b, call->return_deref,
- nir_deref_var_create(b->shader,
- callee_copy->return_var));
+ const unsigned num_params = call->num_params;
+ NIR_VLA(nir_ssa_def *, params, num_params);
+ for (unsigned i = 0; i < num_params; i++) {
+ params[i] = nir_ssa_for_src(b, call->params[i],
+ call->callee->params[i].num_components);
}
- nir_instr_remove(&call->instr);
+ nir_inline_function_impl(b, call->callee->impl, params);
}
return progress;
nir_index_local_regs(impl);
nir_metadata_preserve(impl, nir_metadata_none);
+ } else {
+#ifndef NDEBUG
+ impl->valid_metadata &= ~nir_metadata_not_properly_reset;
+#endif
}
_mesa_set_add(inlined, impl);
return progress;
}
+/** A pass to inline all functions in a shader into their callers
+ *
+ * For most use-cases, function inlining is a multi-step process. The general
+ * pattern employed by SPIR-V consumers and others is as follows:
+ *
+ * 1. nir_lower_constant_initializers(shader, nir_var_function_temp)
+ *
+ * This is needed because local variables from the callee are simply added
+ * to the locals list for the caller and the information about where the
+ * constant initializer logically happens is lost. If the callee is
+ * called in a loop, this can cause the variable to go from being
+ * initialized once per loop iteration to being initialized once at the
+ * top of the caller and values to persist from one invocation of the
+ * callee to the next. The simple solution to this problem is to get rid
+ * of constant initializers before function inlining.
+ *
+ * 2. nir_lower_returns(shader)
+ *
+ * nir_inline_functions assumes that all functions end "naturally" by
+ * execution reaching the end of the function without any return
+ * instructions causing instant jumps to the end. Thanks to NIR being
+ * structured, we can't represent arbitrary jumps to various points in the
+ * program which is what an early return in the callee would have to turn
+ * into when we inline it into the caller. Instead, we require returns to
+ * be lowered which lets us just copy+paste the callee directly into the
+ * caller.
+ *
+ * 3. nir_inline_functions(shader)
+ *
+ * This does the actual function inlining and the resulting shader will
+ * contain no call instructions.
+ *
+ * 4. nir_opt_deref(shader)
+ *
+ * Most functions contain pointer parameters where the result of a deref
+ * instruction is passed in as a parameter, loaded via a load_param
+ * intrinsic, and then turned back into a deref via a cast. Function
+ * inlining will get rid of the load_param but we are still left with a
+ * cast. Running nir_opt_deref gets rid of the intermediate cast and
+ * results in a whole deref chain again. This is currently required by a
+ * number of optimizations and lowering passes at least for certain
+ * variable modes.
+ *
+ * 5. Loop over the functions and delete all but the main entrypoint.
+ *
+ * In the Intel Vulkan driver this looks like this:
+ *
+ * foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
+ * if (func != entry_point)
+ * exec_node_remove(&func->node);
+ * }
+ * assert(exec_list_length(&nir->functions) == 1);
+ *
+ * While nir_inline_functions does get rid of all call instructions, it
+ * doesn't get rid of any functions because it doesn't know what the "root
+ * function" is. Instead, it's up to the individual driver to know how to
+ * decide on a root function and delete the rest. With SPIR-V,
+ * spirv_to_nir returns the root function and so we can just use == whereas
+ * with GL, you may have to look for a function named "main".
+ *
+ * 6. nir_lower_constant_initializers(shader, ~nir_var_function_temp)
+ *
+ * Lowering constant initializers on inputs, outputs, global variables,
+ * etc. requires that we know the main entrypoint so that we know where to
+ * initialize them. Otherwise, we would have to assume that anything
+ * could be a main entrypoint and initialize them at the start of every
+ * function but that would clearly be wrong if any of those functions were
+ * ever called within another function. Simply requiring a single-
+ * entrypoint function shader is the best way to make it well-defined.
+ */
bool
nir_inline_functions(nir_shader *shader)
{
- struct set *inlined = _mesa_set_create(NULL, _mesa_hash_pointer,
- _mesa_key_pointer_equal);
+ struct set *inlined = _mesa_pointer_set_create(NULL);
bool progress = false;
nir_foreach_function(function, shader) {