nir: Add a stub function inlining pass
authorJason Ekstrand <jason.ekstrand@intel.com>
Fri, 18 Dec 2015 19:16:16 +0000 (11:16 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Wed, 23 Dec 2015 21:49:56 +0000 (13:49 -0800)
All it does is remove the return at the end, but it's good enough for
simple functions.

src/glsl/Makefile.sources
src/glsl/nir/nir.h
src/glsl/nir/nir_lower_returns.c [new file with mode: 0644]

index e64c31e17c6b5f6b6b29a977ac729dcadb63b1af..32fbf6d1b6c31ca4e162be0e29b76bd7c4dd6835 100644 (file)
@@ -44,6 +44,7 @@ NIR_FILES = \
        nir/nir_lower_alu_to_scalar.c \
        nir/nir_lower_atomics.c \
        nir/nir_lower_clip.c \
+       nir/nir_lower_returns.c \
        nir/nir_lower_global_vars_to_local.c \
        nir/nir_lower_gs_intrinsics.c \
        nir/nir_lower_load_const_to_scalar.c \
index 021c4280557c73d9152b450d6d621ab418e6a11a..0cf5f80c591280bfb515babf53067692ce6d49d9 100644 (file)
@@ -1998,6 +1998,9 @@ int nir_gs_count_vertices(const nir_shader *shader);
 
 bool nir_split_var_copies(nir_shader *shader);
 
+bool nir_lower_returns_impl(nir_function_impl *impl);
+bool nir_lower_returns(nir_shader *shader);
+
 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);
 void nir_lower_var_copies(nir_shader *shader);
 
diff --git a/src/glsl/nir/nir_lower_returns.c b/src/glsl/nir/nir_lower_returns.c
new file mode 100644 (file)
index 0000000..a15d033
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright © 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "nir.h"
+
+static bool
+assert_no_returns_block(nir_block *block, void *state)
+{
+   (void)state;
+
+   nir_foreach_instr(block, instr) {
+      if (instr->type != nir_instr_type_jump)
+         continue;
+
+      nir_jump_instr *jump = nir_instr_as_jump(instr);
+      assert(jump->type != nir_jump_return);
+   }
+
+   return true;
+}
+
+bool
+nir_lower_returns_impl(nir_function_impl *impl)
+{
+   bool progress = false;
+
+   assert(impl->end_block->predecessors->entries == 1);
+
+   struct set_entry *entry =
+      _mesa_set_next_entry(impl->end_block->predecessors, NULL);
+
+   nir_block *last_block = (nir_block *)entry->key;
+
+   nir_instr *last_instr = nir_block_last_instr(last_block);
+   if (last_instr && last_instr->type == nir_instr_type_jump) {
+      nir_jump_instr *jump = nir_instr_as_jump(last_instr);
+      assert(jump->type == nir_jump_return);
+      nir_instr_remove(&jump->instr);
+      progress = true;
+   }
+
+   nir_foreach_block(impl, assert_no_returns_block, NULL);
+
+   return progress;
+}
+
+bool
+nir_lower_returns(nir_shader *shader)
+{
+   bool progress = false;
+
+   nir_foreach_overload(shader, overload) {
+      if (overload->impl)
+         progress = nir_lower_returns_impl(overload->impl) || progress;
+   }
+
+   return progress;
+}