[Mesa-dev] [PATCH 02/11] glsl: Replace big pile of hand-written code with a generator

Ian Romanick idr at freedesktop.org
Wed Jul 6 00:46:10 UTC 2016


From: Ian Romanick <ian.d.romanick at intel.com>

Right now the generator generates nearly identical code.  There is no
change in the binary size.

   text	   data	    bss	    dec	    hex	filename
7529283	 273096	  28584	7830963	 777db3	/tmp/i965_dri-64bit-before.so
7529283	 273096	  28584	7830963	 777db3	/tmp/i965_dri-64bit-after.so

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/compiler/Makefile.glsl.am          |  11 ++-
 src/compiler/glsl/glsl_to_nir.cpp      | 123 +--------------------------------
 src/compiler/glsl/nir_intrinsic_map.py |  95 +++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 124 deletions(-)
 create mode 100644 src/compiler/glsl/nir_intrinsic_map.py

diff --git a/src/compiler/Makefile.glsl.am b/src/compiler/Makefile.glsl.am
index 1132aae..af80e60 100644
--- a/src/compiler/Makefile.glsl.am
+++ b/src/compiler/Makefile.glsl.am
@@ -27,6 +27,7 @@ EXTRA_DIST += glsl/tests glsl/glcpp/tests glsl/README	\
 	glsl/glsl_parser.yy				\
 	glsl/glcpp/glcpp-lex.l				\
 	glsl/glcpp/glcpp-parse.y			\
+	glsl/nir_intrinsic_map.cpp			\
 	SConscript.glsl
 
 TESTS += glsl/glcpp/tests/glcpp-test			\
@@ -208,6 +209,10 @@ glsl/glcpp/glcpp-lex.c: glsl/glcpp/glcpp-lex.l
 	$(MKDIR_GEN)
 	$(LEX_GEN) -o $@ $(srcdir)/glsl/glcpp/glcpp-lex.l
 
+glsl/nir_intrinsic_map.cpp: glsl/nir_intrinsic_map.py
+	$(MKDIR_GEN)
+	$(PYTHON_GEN) $(srcdir)/glsl/nir_intrinsic_map.py > $@ || ($(RM) $@; false)
+
 # Only the parsers (specifically the header files generated at the same time)
 # need to be in BUILT_SOURCES. Though if we list the parser headers YACC is
 # called for the .c/.cpp file and the .h files. By listing the .c/.cpp files
@@ -218,14 +223,16 @@ BUILT_SOURCES +=					\
 	glsl/glsl_parser.cpp				\
 	glsl/glsl_lexer.cpp				\
 	glsl/glcpp/glcpp-parse.c			\
-	glsl/glcpp/glcpp-lex.c
+	glsl/glcpp/glcpp-lex.c				\
+	glsl/nir_intrinsic_map.cpp
 CLEANFILES +=						\
 	glsl/glcpp/glcpp-parse.h			\
 	glsl/glsl_parser.h				\
 	glsl/glsl_parser.cpp				\
 	glsl/glsl_lexer.cpp				\
 	glsl/glcpp/glcpp-parse.c			\
-	glsl/glcpp/glcpp-lex.c
+	glsl/glcpp/glcpp-lex.c				\
+	glsl/nir_intrinsic_map.cpp
 
 clean-local:
 	$(RM) -r subtest-cr subtest-cr-lf subtest-lf subtest-lf-cr
diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp
index 266f150..3b8424e 100644
--- a/src/compiler/glsl/glsl_to_nir.cpp
+++ b/src/compiler/glsl/glsl_to_nir.cpp
@@ -602,128 +602,7 @@ nir_visitor::visit(ir_return *ir)
    nir_builder_instr_insert(&b, &instr->instr);
 }
 
-namespace _glsl_to_nir {
-
-nir_intrinsic_op
-get_intrinsic_opcode(const char *name, const ir_dereference *return_deref)
-{
-   nir_intrinsic_op op;
-
-   if (strcmp(name, "__intrinsic_atomic_read") == 0) {
-      op = nir_intrinsic_atomic_counter_read_var;
-   } else if (strcmp(name, "__intrinsic_atomic_increment") == 0) {
-      op = nir_intrinsic_atomic_counter_inc_var;
-   } else if (strcmp(name, "__intrinsic_atomic_predecrement") == 0) {
-      op = nir_intrinsic_atomic_counter_dec_var;
-   } else if (strcmp(name, "__intrinsic_image_load") == 0) {
-      op = nir_intrinsic_image_load;
-   } else if (strcmp(name, "__intrinsic_image_store") == 0) {
-      op = nir_intrinsic_image_store;
-   } else if (strcmp(name, "__intrinsic_image_atomic_add") == 0) {
-      op = nir_intrinsic_image_atomic_add;
-   } else if (strcmp(name, "__intrinsic_image_atomic_min") == 0) {
-      op = nir_intrinsic_image_atomic_min;
-   } else if (strcmp(name, "__intrinsic_image_atomic_max") == 0) {
-      op = nir_intrinsic_image_atomic_max;
-   } else if (strcmp(name, "__intrinsic_image_atomic_and") == 0) {
-      op = nir_intrinsic_image_atomic_and;
-   } else if (strcmp(name, "__intrinsic_image_atomic_or") == 0) {
-      op = nir_intrinsic_image_atomic_or;
-   } else if (strcmp(name, "__intrinsic_image_atomic_xor") == 0) {
-      op = nir_intrinsic_image_atomic_xor;
-   } else if (strcmp(name, "__intrinsic_image_atomic_exchange") == 0) {
-      op = nir_intrinsic_image_atomic_exchange;
-   } else if (strcmp(name, "__intrinsic_image_atomic_comp_swap") == 0) {
-      op = nir_intrinsic_image_atomic_comp_swap;
-   } else if (strcmp(name, "__intrinsic_memory_barrier") == 0) {
-      op = nir_intrinsic_memory_barrier;
-   } else if (strcmp(name, "__intrinsic_image_size") == 0) {
-      op = nir_intrinsic_image_size;
-   } else if (strcmp(name, "__intrinsic_image_samples") == 0) {
-      op = nir_intrinsic_image_samples;
-   } else if (strcmp(name, "__intrinsic_store_ssbo") == 0) {
-      op = nir_intrinsic_store_ssbo;
-   } else if (strcmp(name, "__intrinsic_load_ssbo") == 0) {
-      op = nir_intrinsic_load_ssbo;
-   } else if (strcmp(name, "__intrinsic_atomic_add_ssbo") == 0) {
-      op = nir_intrinsic_ssbo_atomic_add;
-   } else if (strcmp(name, "__intrinsic_atomic_and_ssbo") == 0) {
-      op = nir_intrinsic_ssbo_atomic_and;
-   } else if (strcmp(name, "__intrinsic_atomic_or_ssbo") == 0) {
-      op = nir_intrinsic_ssbo_atomic_or;
-   } else if (strcmp(name, "__intrinsic_atomic_xor_ssbo") == 0) {
-      op = nir_intrinsic_ssbo_atomic_xor;
-   } else if (strcmp(name, "__intrinsic_atomic_min_ssbo") == 0) {
-      assert(return_deref);
-      if (return_deref->type == glsl_type::int_type)
-         op = nir_intrinsic_ssbo_atomic_imin;
-      else if (return_deref->type == glsl_type::uint_type)
-         op = nir_intrinsic_ssbo_atomic_umin;
-      else
-         unreachable("Invalid type");
-   } else if (strcmp(name, "__intrinsic_atomic_max_ssbo") == 0) {
-      assert(return_deref);
-      if (return_deref->type == glsl_type::int_type)
-         op = nir_intrinsic_ssbo_atomic_imax;
-      else if (return_deref->type == glsl_type::uint_type)
-         op = nir_intrinsic_ssbo_atomic_umax;
-      else
-         unreachable("Invalid type");
-   } else if (strcmp(name, "__intrinsic_atomic_exchange_ssbo") == 0) {
-      op = nir_intrinsic_ssbo_atomic_exchange;
-   } else if (strcmp(name, "__intrinsic_atomic_comp_swap_ssbo") == 0) {
-      op = nir_intrinsic_ssbo_atomic_comp_swap;
-   } else if (strcmp(name, "__intrinsic_shader_clock") == 0) {
-      op = nir_intrinsic_shader_clock;
-   } else if (strcmp(name, "__intrinsic_group_memory_barrier") == 0) {
-      op = nir_intrinsic_group_memory_barrier;
-   } else if (strcmp(name, "__intrinsic_memory_barrier_atomic_counter") == 0) {
-      op = nir_intrinsic_memory_barrier_atomic_counter;
-   } else if (strcmp(name, "__intrinsic_memory_barrier_buffer") == 0) {
-      op = nir_intrinsic_memory_barrier_buffer;
-   } else if (strcmp(name, "__intrinsic_memory_barrier_image") == 0) {
-      op = nir_intrinsic_memory_barrier_image;
-   } else if (strcmp(name, "__intrinsic_memory_barrier_shared") == 0) {
-      op = nir_intrinsic_memory_barrier_shared;
-   } else if (strcmp(name, "__intrinsic_load_shared") == 0) {
-      op = nir_intrinsic_load_shared;
-   } else if (strcmp(name, "__intrinsic_store_shared") == 0) {
-      op = nir_intrinsic_store_shared;
-   } else if (strcmp(name, "__intrinsic_atomic_add_shared") == 0) {
-      op = nir_intrinsic_shared_atomic_add;
-   } else if (strcmp(name, "__intrinsic_atomic_and_shared") == 0) {
-      op = nir_intrinsic_shared_atomic_and;
-   } else if (strcmp(name, "__intrinsic_atomic_or_shared") == 0) {
-      op = nir_intrinsic_shared_atomic_or;
-   } else if (strcmp(name, "__intrinsic_atomic_xor_shared") == 0) {
-      op = nir_intrinsic_shared_atomic_xor;
-   } else if (strcmp(name, "__intrinsic_atomic_min_shared") == 0) {
-      assert(return_deref);
-      if (return_deref->type == glsl_type::int_type)
-         op = nir_intrinsic_shared_atomic_imin;
-      else if (return_deref->type == glsl_type::uint_type)
-         op = nir_intrinsic_shared_atomic_umin;
-      else
-         unreachable("Invalid type");
-   } else if (strcmp(name, "__intrinsic_atomic_max_shared") == 0) {
-      assert(return_deref);
-      if (return_deref->type == glsl_type::int_type)
-         op = nir_intrinsic_shared_atomic_imax;
-      else if (return_deref->type == glsl_type::uint_type)
-         op = nir_intrinsic_shared_atomic_umax;
-      else
-         unreachable("Invalid type");
-   } else if (strcmp(name, "__intrinsic_atomic_exchange_shared") == 0) {
-      op = nir_intrinsic_shared_atomic_exchange;
-   } else if (strcmp(name, "__intrinsic_atomic_comp_swap_shared") == 0) {
-      op = nir_intrinsic_shared_atomic_comp_swap;
-   } else {
-      unreachable("not reached");
-   }
-
-   return op;
-}
-}
+#include "nir_intrinsic_map.cpp"
 
 void
 nir_visitor::visit(ir_call *ir)
diff --git a/src/compiler/glsl/nir_intrinsic_map.py b/src/compiler/glsl/nir_intrinsic_map.py
new file mode 100644
index 0000000..ef13fa3
--- /dev/null
+++ b/src/compiler/glsl/nir_intrinsic_map.py
@@ -0,0 +1,95 @@
+# coding=utf-8
+#
+# Copyright © 2016 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.
+
+from mako.template import Template
+
+intrinsics = [("__intrinsic_atomic_read", ("nir_intrinsic_atomic_counter_read_var", None)),
+              ("__intrinsic_atomic_increment", ("nir_intrinsic_atomic_counter_inc_var", None)),
+              ("__intrinsic_atomic_predecrement", ("nir_intrinsic_atomic_counter_dec_var", None)),
+              ("__intrinsic_image_load", ("nir_intrinsic_image_load", None)),
+              ("__intrinsic_image_store", ("nir_intrinsic_image_store", None)),
+              ("__intrinsic_image_atomic_add", ("nir_intrinsic_image_atomic_add", None)),
+              ("__intrinsic_image_atomic_min", ("nir_intrinsic_image_atomic_min", None)),
+              ("__intrinsic_image_atomic_max", ("nir_intrinsic_image_atomic_max", None)),
+              ("__intrinsic_image_atomic_and", ("nir_intrinsic_image_atomic_and", None)),
+              ("__intrinsic_image_atomic_or", ("nir_intrinsic_image_atomic_or", None)),
+              ("__intrinsic_image_atomic_xor", ("nir_intrinsic_image_atomic_xor", None)),
+              ("__intrinsic_image_atomic_exchange", ("nir_intrinsic_image_atomic_exchange", None)),
+              ("__intrinsic_image_atomic_comp_swap", ("nir_intrinsic_image_atomic_comp_swap", None)),
+              ("__intrinsic_memory_barrier", ("nir_intrinsic_memory_barrier", None)),
+              ("__intrinsic_image_size", ("nir_intrinsic_image_size", None)),
+              ("__intrinsic_image_samples", ("nir_intrinsic_image_samples", None)),
+              ("__intrinsic_store_ssbo", ("nir_intrinsic_store_ssbo", None)),
+              ("__intrinsic_load_ssbo", ("nir_intrinsic_load_ssbo", None)),
+              ("__intrinsic_atomic_add_ssbo", ("nir_intrinsic_ssbo_atomic_add", None)),
+              ("__intrinsic_atomic_and_ssbo", ("nir_intrinsic_ssbo_atomic_and", None)),
+              ("__intrinsic_atomic_or_ssbo", ("nir_intrinsic_ssbo_atomic_or", None)),
+              ("__intrinsic_atomic_xor_ssbo", ("nir_intrinsic_ssbo_atomic_xor", None)),
+              ("__intrinsic_atomic_min_ssbo", ("nir_intrinsic_ssbo_atomic_imin", "nir_intrinsic_ssbo_atomic_umin")),
+              ("__intrinsic_atomic_max_ssbo", ("nir_intrinsic_ssbo_atomic_imax", "nir_intrinsic_ssbo_atomic_umax")),
+              ("__intrinsic_atomic_exchange_ssbo", ("nir_intrinsic_ssbo_atomic_exchange", None)),
+              ("__intrinsic_atomic_comp_swap_ssbo", ("nir_intrinsic_ssbo_atomic_comp_swap", None)),
+              ("__intrinsic_shader_clock", ("nir_intrinsic_shader_clock", None)),
+              ("__intrinsic_group_memory_barrier", ("nir_intrinsic_group_memory_barrier", None)),
+              ("__intrinsic_memory_barrier_atomic_counter", ("nir_intrinsic_memory_barrier_atomic_counter", None)),
+              ("__intrinsic_memory_barrier_buffer", ("nir_intrinsic_memory_barrier_buffer", None)),
+              ("__intrinsic_memory_barrier_image", ("nir_intrinsic_memory_barrier_image", None)),
+              ("__intrinsic_memory_barrier_shared", ("nir_intrinsic_memory_barrier_shared", None)),
+              ("__intrinsic_load_shared", ("nir_intrinsic_load_shared", None)),
+              ("__intrinsic_store_shared", ("nir_intrinsic_store_shared", None)),
+              ("__intrinsic_atomic_add_shared", ("nir_intrinsic_shared_atomic_add", None)),
+              ("__intrinsic_atomic_and_shared", ("nir_intrinsic_shared_atomic_and", None)),
+              ("__intrinsic_atomic_or_shared", ("nir_intrinsic_shared_atomic_or", None)),
+              ("__intrinsic_atomic_xor_shared", ("nir_intrinsic_shared_atomic_xor", None)),
+              ("__intrinsic_atomic_min_shared", ("nir_intrinsic_shared_atomic_imin", "nir_intrinsic_shared_atomic_umin")),
+              ("__intrinsic_atomic_max_shared", ("nir_intrinsic_shared_atomic_imax", "nir_intrinsic_shared_atomic_umax")),
+              ("__intrinsic_atomic_exchange_shared", ("nir_intrinsic_shared_atomic_exchange", None)),
+              ("__intrinsic_atomic_comp_swap_shared", ("nir_intrinsic_shared_atomic_comp_swap", None))]
+
+template = """
+namespace _glsl_to_nir {
+
+nir_intrinsic_op
+get_intrinsic_opcode(const char *name, const ir_dereference *return_deref)
+{
+    % for (name, ops) in intrinsics:
+   if (strcmp(name, "${name}") == 0) {
+        % if ops[1] is None:
+      return ${ops[0]};
+        % else:
+      assert(return_deref);
+      if (return_deref->type == glsl_type::int_type)
+         return ${ops[0]};
+      else if (return_deref->type == glsl_type::uint_type)
+         return ${ops[1]};
+      else
+         unreachable("Invalid type");
+        % endif
+   } else
+    % endfor
+      unreachable("Unknown intrinsic name");
+}
+}
+"""
+
+print(Template(template).render(intrinsics=intrinsics))
-- 
2.5.5



More information about the mesa-dev mailing list