[Mesa-dev] [PATCH RFC 05/11] glsl: add loop jump visitor

Connor Abbott cwabbott0 at gmail.com
Wed Jan 22 09:16:52 PST 2014


This visitor will allow us to determine all the loop jumps that
correspond to each loop. In SSA form, each input to a phi node is
associated with a predecessor basic block. In the case of phi nodes
at the beginning and end of loops, these predecessor blocks will
include all blocks that end with a loop_jump (break or continue), and
so in order to insert phi nodes we must know all the loop_jump
instructions that correspond to each loop.
---
 src/glsl/Makefile.sources  |   1 +
 src/glsl/ir_loop_jumps.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++++
 src/glsl/ir_loop_jumps.h   |  71 +++++++++++++++++++++++++
 3 files changed, 201 insertions(+)
 create mode 100644 src/glsl/ir_loop_jumps.cpp
 create mode 100644 src/glsl/ir_loop_jumps.h

diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index a43bfa7..869158a 100644
--- a/src/glsl/Makefile.sources
+++ b/src/glsl/Makefile.sources
@@ -42,6 +42,7 @@ LIBGLSL_FILES = \
 	$(GLSL_SRCDIR)/ir_hierarchical_visitor.cpp \
 	$(GLSL_SRCDIR)/ir_hv_accept.cpp \
 	$(GLSL_SRCDIR)/ir_import_prototypes.cpp \
+	$(GLSL_SRCDIR)/ir_loop_jumps.cpp \
 	$(GLSL_SRCDIR)/ir_print_visitor.cpp \
 	$(GLSL_SRCDIR)/ir_reader.cpp \
 	$(GLSL_SRCDIR)/ir_rvalue_visitor.cpp \
diff --git a/src/glsl/ir_loop_jumps.cpp b/src/glsl/ir_loop_jumps.cpp
new file mode 100644
index 0000000..1386340
--- /dev/null
+++ b/src/glsl/ir_loop_jumps.cpp
@@ -0,0 +1,129 @@
+/*
+ * Copyright © 2013 Connor Abbott (connor at abbott.cx)
+ *
+ * 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 "ir.h"
+#include "ir_visitor.h"
+#include "ir_loop_jumps.h"
+#include "main/hash_table.h"
+
+/**
+ * \file ir_loop_jumps.h
+ *
+ * Provides a visitor that collects all the continue and break statements for
+ * each loop.
+ */
+
+ir_loop_jumps::ir_loop_jumps(ir_loop *loop) : loop(loop)
+{
+   this->mem_ctx = ralloc_context(NULL);
+}
+
+ir_loop_jumps::~ir_loop_jumps()
+{
+   ralloc_free(this->mem_ctx);
+}
+
+void
+ir_loop_jumps::add_continue(ir_loop_jump *ir)
+{
+   ir_loop_jump_entry *entry = new(this->mem_ctx) ir_loop_jump_entry();
+   entry->ir = ir;
+   this->continues.push_tail(entry);
+}
+
+void
+ir_loop_jumps::add_break(ir_loop_jump *ir)
+{
+   ir_loop_jump_entry *entry = new(this->mem_ctx) ir_loop_jump_entry();
+   entry->ir = ir;
+   this->breaks.push_tail(entry);
+}
+
+ir_loop_jumps_visitor::ir_loop_jumps_visitor()
+{
+   this->ht = _mesa_hash_table_create(NULL, _mesa_key_pointer_equal);
+   this->outer_loop = NULL;
+}
+
+static void
+free_entry(struct hash_entry *entry)
+{
+   ir_loop_jumps *loop_jumps = (ir_loop_jumps *) entry->data;
+   delete loop_jumps;
+}
+
+ir_loop_jumps_visitor::~ir_loop_jumps_visitor()
+{
+   _mesa_hash_table_destroy(this->ht, free_entry);
+}
+
+ir_visitor_status
+ir_loop_jumps_visitor::visit_enter(ir_loop *ir)
+{
+   ir_loop_jumps *loop_jumps = new ir_loop_jumps(ir);
+   _mesa_hash_table_insert(this->ht, _mesa_hash_pointer(ir), ir, loop_jumps);
+
+   ir_loop *old_outer_loop = this->outer_loop;
+   this->outer_loop = ir;
+
+   visit_list_elements(this, &ir->body_instructions);
+
+   this->outer_loop = old_outer_loop;
+   return visit_continue_with_parent;
+}
+
+ir_visitor_status
+ir_loop_jumps_visitor::visit(ir_loop_jump *ir)
+{
+   ir_loop_jumps *loop_jumps = this->get_loop_jumps(this->outer_loop);
+   switch (ir->mode) {
+      case ir_loop_jump::jump_break:
+	 loop_jumps->add_break(ir);
+	 break;
+
+      case ir_loop_jump::jump_continue:
+	 loop_jumps->add_continue(ir);
+	 break;
+
+      default:
+	 assert(!"unknown loop jump mode");
+	 break;
+   }
+
+   return visit_continue;
+}
+
+ir_loop_jumps *
+ir_loop_jumps_visitor::get_loop_jumps(ir_loop *ir)
+{
+   assert(ir);
+
+   struct hash_entry *e = _mesa_hash_table_search(this->ht,
+						  _mesa_hash_pointer(ir),
+						  ir);
+   if (e)
+      return (ir_loop_jumps *)e->data;
+
+   assert(0);
+   return NULL;
+}
diff --git a/src/glsl/ir_loop_jumps.h b/src/glsl/ir_loop_jumps.h
new file mode 100644
index 0000000..4d33c97
--- /dev/null
+++ b/src/glsl/ir_loop_jumps.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright © 2013 Connor Abbott (connor at abbott.cx)
+ *
+ * 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.
+ */
+
+/**
+ * \file ir_loop_jumps.h
+ *
+ * Provides a visitor that collects all the continue and break statements for
+ * each loop.
+ */
+
+#include "ir.h"
+#include "ir_visitor.h"
+
+class ir_loop_jump_entry : public exec_node
+{
+public:
+   ir_loop_jump *ir;
+};
+
+class ir_loop_jumps
+{
+public:
+   ir_loop_jumps(ir_loop *loop);
+   ~ir_loop_jumps();
+
+   void add_continue(ir_loop_jump *ir);
+   void add_break(ir_loop_jump *ir);
+
+   ir_loop *loop;
+   exec_list continues, breaks;
+
+private:
+   void *mem_ctx;
+};
+
+class ir_loop_jumps_visitor : public ir_hierarchical_visitor
+{
+public:
+   ir_loop_jumps_visitor();
+   ~ir_loop_jumps_visitor();
+
+   virtual ir_visitor_status visit_enter(ir_loop *);
+   virtual ir_visitor_status visit(ir_loop_jump *);
+
+   ir_loop_jumps *get_loop_jumps(ir_loop *ir);
+
+private:
+   ir_loop *outer_loop;
+
+   struct hash_table *ht;
+};
-- 
1.8.3.1



More information about the mesa-dev mailing list