Mesa (master): glsl/standalone: Optimize dead variable declarations

Ian Romanick idr at kemper.freedesktop.org
Thu Nov 10 22:31:05 UTC 2016


Module: Mesa
Branch: master
Commit: 4dc759c8c236e725ff7bbd439e19eada12bf390f
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=4dc759c8c236e725ff7bbd439e19eada12bf390f

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Thu Sep 15 11:18:35 2016 -0700

glsl/standalone: Optimize dead variable declarations

We didn't bother with this in the regular compiler because it doesn't
change the generated code.  In the stand-alone compiler, this can
clutter the output with useless variables.  It's especially bad after
functions are inlined but the foo_retval declarations remain.

v2: Use set_foreach.  Suggested by Tapani.

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
Reviewed-by: Tapani Pälli <tapani.palli at intel.com>
Reviewed-by: Iago Toral Quiroga <itoral at igalia.com>

---

 src/compiler/glsl/standalone.cpp | 61 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/src/compiler/glsl/standalone.cpp b/src/compiler/glsl/standalone.cpp
index efc6da9..7e633a7 100644
--- a/src/compiler/glsl/standalone.cpp
+++ b/src/compiler/glsl/standalone.cpp
@@ -37,8 +37,65 @@
 #include "standalone_scaffolding.h"
 #include "standalone.h"
 #include "util/string_to_uint_map.h"
+#include "util/set.h"
 #include "opt_add_neg_to_sub.h"
 
+class dead_variable_visitor : public ir_hierarchical_visitor {
+public:
+   dead_variable_visitor()
+   {
+      variables = _mesa_set_create(NULL,
+                                   _mesa_hash_pointer,
+                                   _mesa_key_pointer_equal);
+   }
+
+   virtual ~dead_variable_visitor()
+   {
+      _mesa_set_destroy(variables, NULL);
+   }
+
+   virtual ir_visitor_status visit(ir_variable *ir)
+   {
+      /* If the variable is auto or temp, add it to the set of variables that
+       * are candidates for removal.
+       */
+      if (ir->data.mode != ir_var_auto && ir->data.mode != ir_var_temporary)
+         return visit_continue;
+
+      _mesa_set_add(variables, ir);
+
+      return visit_continue;
+   }
+
+   virtual ir_visitor_status visit(ir_dereference_variable *ir)
+   {
+      struct set_entry *entry = _mesa_set_search(variables, ir->var);
+
+      /* If a variable is dereferenced at all, remove it from the set of
+       * variables that are candidates for removal.
+       */
+      if (entry != NULL)
+         _mesa_set_remove(variables, entry);
+
+      return visit_continue;
+   }
+
+   void remove_dead_variables()
+   {
+      struct set_entry *entry;
+
+      set_foreach(variables, entry) {
+         ir_variable *ir = (ir_variable *) entry->key;
+
+         assert(ir->ir_type == ir_type_variable);
+         ir->remove();
+      }
+   }
+
+private:
+   set *variables;
+};
+
 static const struct standalone_options *options;
 
 static void
@@ -445,6 +502,10 @@ standalone_compile_shader(const struct standalone_options *_options,
          add_neg_to_sub_visitor v;
          visit_list_elements(&v, shader->ir);
 
+         dead_variable_visitor dv;
+         visit_list_elements(&dv, shader->ir);
+         dv.remove_dead_variables();
+
          shader->Program = rzalloc(shader, gl_program);
          init_gl_program(shader->Program, shader->Stage);
       }




More information about the mesa-commit mailing list