Module: Mesa
Branch: main
Commit: c20751d61dc237d6786d37a9cf8f4736662d5ee2
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=c20751d61dc237d6786d37a9cf8f4736662d5ee2

Author: Daniel Schürmann <[email protected]>
Date:   Wed Dec  1 17:46:16 2021 +0100

nir: add lowering for Loop Continue Constructs

This pass lowers Loop Continue Constructs to the previous solution
by inserting it at the beginning of the loop:

loop {
   if (i != 0) {
      continue construct
   }
   loop body
}

Reviewed-by: Faith Ekstrand <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13962>

---

 src/compiler/nir/meson.build                     |   1 +
 src/compiler/nir/nir.h                           |   1 +
 src/compiler/nir/nir_lower_continue_constructs.c | 141 +++++++++++++++++++++++
 3 files changed, 143 insertions(+)

diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
index db748c23b0e..1948ba24448 100644
--- a/src/compiler/nir/meson.build
+++ b/src/compiler/nir/meson.build
@@ -145,6 +145,7 @@ files_libnir = files(
   'nir_lower_clip_disable.c',
   'nir_lower_clip_halfz.c',
   'nir_lower_const_arrays_to_uniforms.c',
+  'nir_lower_continue_constructs.c',
   'nir_lower_convert_alu_types.c',
   'nir_lower_variable_initializers.c',
   'nir_lower_discard_if.c',
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 18ede24f8c2..872f5156567 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -5615,6 +5615,7 @@ bool nir_lower_discard_or_demote(nir_shader *shader,
 bool nir_lower_memory_model(nir_shader *shader);
 
 bool nir_lower_goto_ifs(nir_shader *shader);
+bool nir_lower_continue_constructs(nir_shader *shader);
 
 bool nir_shader_uses_view_index(nir_shader *shader);
 bool nir_can_lower_multiview(nir_shader *shader);
diff --git a/src/compiler/nir/nir_lower_continue_constructs.c 
b/src/compiler/nir/nir_lower_continue_constructs.c
new file mode 100644
index 00000000000..96114b06a86
--- /dev/null
+++ b/src/compiler/nir/nir_lower_continue_constructs.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright © 2021 Valve 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"
+#include "nir_builder.h"
+#include "nir_control_flow.h"
+
+static bool
+lower_loop_continue_block(nir_builder *b, nir_loop *loop)
+{
+   if (!nir_loop_has_continue_construct(loop))
+      return false;
+
+   nir_block *header = nir_loop_first_block(loop);
+   nir_block *cont = nir_loop_first_continue_block(loop);
+
+   nir_lower_phis_to_regs_block(header);
+   nir_lower_phis_to_regs_block(cont);
+
+   /* As control flow has to re-converge before executing the continue
+    * construct, we insert it at the beginning of the loop with a flag
+    * to ensure that it doesn't get executed in the first iteration:
+    *
+    *    loop {
+    *       if (i != 0) {
+    *          continue construct
+    *       }
+    *       loop body
+    *    }
+    */
+
+   nir_variable *do_cont =
+      nir_local_variable_create(b->impl, glsl_bool_type(), "cont");
+
+   b->cursor = nir_before_cf_node(&loop->cf_node);
+   nir_store_var(b, do_cont, nir_imm_false(b), 1);
+   b->cursor = nir_before_block(header);
+   nir_if *cont_if = nir_push_if(b, nir_load_var(b, do_cont));
+   {
+      nir_cf_list extracted;
+      nir_cf_list_extract(&extracted, &loop->continue_list);
+      nir_cf_reinsert(&extracted, nir_before_cf_list(&cont_if->then_list));
+   }
+   nir_pop_if(b, cont_if);
+   nir_store_var(b, do_cont, nir_imm_true(b), 1);
+
+   nir_loop_remove_continue_construct(loop);
+   return true;
+}
+
+
+static bool
+visit_cf_list(nir_builder *b, struct exec_list *list)
+{
+   bool progress = false;
+
+   foreach_list_typed(nir_cf_node, node, node, list) {
+      switch (node->type) {
+      case nir_cf_node_block:
+         continue;
+      case nir_cf_node_if: {
+         nir_if *nif = nir_cf_node_as_if(node);
+         progress |= visit_cf_list(b, &nif->then_list);
+         progress |= visit_cf_list(b, &nif->else_list);
+         break;
+      }
+      case nir_cf_node_loop: {
+         nir_loop *loop = nir_cf_node_as_loop(node);
+         progress |= visit_cf_list(b, &loop->body);
+         progress |= visit_cf_list(b, &loop->continue_list);
+         progress |= lower_loop_continue_block(b, loop);
+         break;
+      }
+      case nir_cf_node_function:
+         unreachable("Unsupported cf_node type.");
+      }
+   }
+
+   return progress;
+}
+
+static bool
+lower_continue_constructs_impl(nir_function_impl *impl)
+{
+   nir_builder b;
+   nir_builder_init(&b, impl);
+   bool progress = visit_cf_list(&b, &impl->body);
+
+   if (progress) {
+      nir_metadata_preserve(impl, nir_metadata_none);
+
+      /* Merge the Phis from Header and Continue Target */
+      nir_lower_regs_to_ssa_impl(impl);
+
+      /* Re-inserting the Continue Target at the beginning of the loop
+       * violates the dominance property if instructions in the continue
+       * use SSA defs from the loop body.
+       */
+      nir_repair_ssa_impl(impl);
+   } else {
+      nir_metadata_preserve(impl, nir_metadata_all);
+   }
+
+   return progress;
+}
+
+bool
+nir_lower_continue_constructs(nir_shader *shader)
+{
+   bool progress = false;
+
+   nir_foreach_function(function, shader) {
+      if (function->impl && lower_continue_constructs_impl(function->impl))
+         progress = true;
+   }
+
+   return progress;
+}
+

Reply via email to