nir: Use a switch in nir_inline_function_impl
[mesa.git] / src / compiler / nir / nir_inline_functions.c
index 3cf832790531dd97ffdd2e21dad31f3abb5a365b..03a26e0dbf3e79b21832157d1bcc0bc2c8e0a891 100644 (file)
 #include "nir.h"
 #include "nir_builder.h"
 #include "nir_control_flow.h"
+#include "nir_vla.h"
 
-struct inline_functions_state {
-   struct set *inlined;
-   nir_builder builder;
-   bool progress;
-};
+void nir_inline_function_impl(struct nir_builder *b,
+                              const nir_function_impl *impl,
+                              nir_ssa_def **params)
+{
+   nir_function_impl *copy = nir_function_impl_clone(b->shader, impl);
+
+   /* Insert a nop at the cursor so we can keep track of where things are as
+    * we add/remove stuff from the CFG.
+    */
+   nir_intrinsic_instr *nop =
+      nir_intrinsic_instr_create(b->shader, nir_intrinsic_nop);
+   nir_builder_instr_insert(b, &nop->instr);
+
+   exec_list_append(&b->impl->locals, &copy->locals);
+   exec_list_append(&b->impl->registers, &copy->registers);
+
+   nir_foreach_block(block, copy) {
+      nir_foreach_instr_safe(instr, block) {
+         switch (instr->type) {
+         case nir_instr_type_intrinsic: {
+            nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
+            if (load->intrinsic != nir_intrinsic_load_param)
+               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]));
+
+            /* Remove any left-over load_param intrinsics because they're soon
+             * to be in another function and therefore no longer valid.
+             */
+            nir_instr_remove(&load->instr);
+            break;
+         }
+
+         case nir_instr_type_jump:
+            /* Returns have to be lowered for this to work */
+            assert(nir_instr_as_jump(instr)->type != nir_jump_return);
+            break;
+
+         default:
+            break;
+         }
+      }
+   }
+
+   /* Pluck the body out of the function and place it here */
+   nir_cf_list body;
+   nir_cf_list_extract(&body, &copy->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, void *void_state)
+inline_functions_block(nir_block *block, nir_builder *b,
+                       struct set *inlined)
 {
-   struct inline_functions_state *state = void_state;
-
-   nir_builder *b = &state->builder;
-
+   bool progress = false;
    /* This is tricky.  We're iterating over instructions in a block but, as
     * we go, the block and its instruction list are being split into
     * pieces.  However, this *should* be safe since foreach_safe always
@@ -47,65 +96,36 @@ inline_functions_block(nir_block *block, void *void_state)
     * properly get moved to the next block when it gets split, and we
     * continue iterating there.
     */
-   nir_foreach_instr_safe(block, instr) {
+   nir_foreach_instr_safe(instr, block) {
       if (instr->type != nir_instr_type_call)
          continue;
 
-      state->progress = true;
+      progress = true;
 
       nir_call_instr *call = nir_instr_as_call(instr);
       assert(call->callee->impl);
 
-      inline_function_impl(call->callee->impl, state->inlined);
-
-      nir_function_impl *callee_copy =
-         nir_function_impl_clone(call->callee->impl);
-
-      exec_list_append(&b->impl->locals, &callee_copy->locals);
-      exec_list_append(&b->impl->registers, &callee_copy->registers);
-
-      b->cursor = nir_before_instr(&call->instr);
-
-      /* Add copies of all in parameters */
-      assert(call->num_params == callee_copy->num_params);
-      for (unsigned i = 0; i < callee_copy->num_params; i++) {
-         /* Only in or inout parameters */
-         if (call->callee->params[i].param_type == nir_parameter_out)
-            continue;
-
-         nir_copy_deref_var(b, nir_deref_var_create(b->shader,
-                                                    callee_copy->params[i]),
-                               call->params[i]);
-      }
-
-      /* 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++) {
-         /* Only out or inout parameters */
-         if (call->callee->params[i].param_type == nir_parameter_in)
-            continue;
-
-         nir_copy_deref_var(b, call->params[i],
-                               nir_deref_var_create(b->shader,
-                                                    callee_copy->params[i]));
-      }
-      if (!glsl_type_is_void(call->callee->return_type)) {
-         nir_copy_deref_var(b, call->return_deref,
-                               nir_deref_var_create(b->shader,
-                                                    callee_copy->return_var));
+      /* Make sure that the function we're calling is already inlined */
+      inline_function_impl(call->callee->impl, inlined);
+
+      b->cursor = nir_instr_remove(&call->instr);
+
+      /* 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.
+       */
+      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 true;
+   return progress;
 }
 
 static bool
@@ -114,35 +134,106 @@ inline_function_impl(nir_function_impl *impl, struct set *inlined)
    if (_mesa_set_search(inlined, impl))
       return false; /* Already inlined */
 
-   struct inline_functions_state state;
-
-   state.inlined = inlined;
-   state.progress = false;
-   nir_builder_init(&state.builder, impl);
+   nir_builder b;
+   nir_builder_init(&b, impl);
 
-   nir_foreach_block(impl, inline_functions_block, &state);
+   bool progress = false;
+   nir_foreach_block_safe(block, impl) {
+      progress |= inline_functions_block(block, &b, inlined);
+   }
 
-   if (state.progress) {
+   if (progress) {
       /* SSA and register indices are completely messed up now */
       nir_index_ssa_defs(impl);
       nir_index_local_regs(impl);
 
       nir_metadata_preserve(impl, nir_metadata_none);
+   } else {
+      nir_metadata_preserve(impl, nir_metadata_all);
    }
 
    _mesa_set_add(inlined, impl);
 
-   return state.progress;
+   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_variable_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_variable_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(shader, function) {
+   nir_foreach_function(function, shader) {
       if (function->impl)
          progress = inline_function_impl(function->impl, inlined) || progress;
    }