[Mesa-dev] [PATCH 3/3] nir: Add an ALU op builder kind of like ir_builder.h

Eric Anholt eric at anholt.net
Wed Jan 28 17:08:22 PST 2015


v2: Rebase on the nir_opcodes.h python code generation support.
v3: Use SSA values, and set an appropriate writemask on dot products.
---
 src/glsl/Makefile.am                  |  5 ++
 src/glsl/nir/.gitignore               |  1 +
 src/glsl/nir/nir_builder.h            | 95 +++++++++++++++++++++++++++++++++++
 src/glsl/nir/nir_builder_opcodes_h.py | 38 ++++++++++++++
 4 files changed, 139 insertions(+)
 create mode 100644 src/glsl/nir/nir_builder.h
 create mode 100644 src/glsl/nir/nir_builder_opcodes_h.py

diff --git a/src/glsl/Makefile.am b/src/glsl/Makefile.am
index e89a9ad..42c77ae 100644
--- a/src/glsl/Makefile.am
+++ b/src/glsl/Makefile.am
@@ -216,6 +216,7 @@ BUILT_SOURCES =						\
 	glsl_lexer.cpp					\
 	glcpp/glcpp-parse.c				\
 	glcpp/glcpp-lex.c				\
+	nir/nir_builder_opcodes.h				\
 	nir/nir_constant_expressions.c			\
 	nir/nir_opcodes.c				\
 	nir/nir_opcodes.h				\
@@ -232,6 +233,10 @@ dist-hook:
 	$(RM) glcpp/tests/*.out
 	$(RM) glcpp/tests/subtest*/*.out
 
+nir/nir_builder_opcodes.h: nir/nir_opcodes.py nir/nir_builder_opcodes_h.py
+	$(MKDIR_P) nir;							\
+	$(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_builder_opcodes_h.py > $@
+
 nir/nir_constant_expressions.c: nir/nir_opcodes.py nir/nir_constant_expressions.py nir/nir_constant_expressions.h
 	$(MKDIR_P) nir;							\
 	$(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_constant_expressions.py > $@
diff --git a/src/glsl/nir/.gitignore b/src/glsl/nir/.gitignore
index 261f64f..64828eb 100644
--- a/src/glsl/nir/.gitignore
+++ b/src/glsl/nir/.gitignore
@@ -1,3 +1,4 @@
+nir_builder_opcodes.h
 nir_opt_algebraic.c
 nir_opcodes.c
 nir_opcodes.h
diff --git a/src/glsl/nir/nir_builder.h b/src/glsl/nir/nir_builder.h
new file mode 100644
index 0000000..84158c2
--- /dev/null
+++ b/src/glsl/nir/nir_builder.h
@@ -0,0 +1,95 @@
+/*
+ * Copyright © 2014 Broadcom
+ *
+ * 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.
+ */
+
+#ifndef NIR_BUILDER_H
+#define NIR_BUILDER_H
+
+struct exec_list;
+
+struct nir_builder {
+   struct exec_list *cf_node_list;
+   nir_shader *shader;
+   nir_function_impl *impl;
+};
+
+static inline nir_alu_instr *
+nir_build_alu(struct nir_builder *build, nir_op op, nir_alu_src *src0,
+              nir_alu_src *src1, nir_alu_src *src2, nir_alu_src *src3)
+{
+   nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
+   if (!instr)
+      return NULL;
+
+   instr->src[0] = *src0;
+   if (src1)
+      instr->src[1] = *src1;
+   if (src2)
+      instr->src[2] = *src2;
+   if (src3)
+      instr->src[3] = *src3;
+
+   unsigned num_components = nir_op_infos[op].output_size;
+   if (num_components == 0)
+      num_components = 4;
+
+   nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
+   instr->dest.write_mask = (1 << num_components) - 1;
+
+   nir_instr_insert_after_cf_list(build->cf_node_list, &instr->instr);
+
+   return instr;
+}
+
+#define ALU1(op)                                                         \
+static inline nir_alu_instr *                                            \
+nir_##op(struct nir_builder *build, nir_alu_src src0)                    \
+{                                                                        \
+   return nir_build_alu(build, nir_op_##op, &src0, NULL, NULL, NULL);    \
+}
+
+#define ALU2(op)                                                         \
+static inline nir_alu_instr *                                            \
+nir_##op(struct nir_builder *build, nir_alu_src src0, nir_alu_src src1)  \
+{                                                                        \
+   return nir_build_alu(build, nir_op_##op, &src0, &src1, NULL, NULL);   \
+}
+
+#define ALU3(op)                                                         \
+static inline nir_alu_instr *                                            \
+nir_##op(struct nir_builder *build, nir_alu_src src0, nir_alu_src src1,  \
+         nir_alu_src src2)                                               \
+{                                                                        \
+   return nir_build_alu(build, nir_op_##op, &src0, &src1, &src2, NULL);  \
+}
+
+#define ALU4(op)                                                         \
+static inline nir_alu_instr *                                            \
+nir_##op(struct nir_builder *build, nir_alu_src src0, nir_alu_src src1,  \
+         nir_alu_src src2, nir_alu_src src3)                             \
+{                                                                        \
+   return nir_build_alu(build, nir_op_##op, &src0, &src1, &src2, &src3); \
+}
+
+#include "nir_builder_opcodes.h"
+
+#endif /* NIR_BUILDER_H */
diff --git a/src/glsl/nir/nir_builder_opcodes_h.py b/src/glsl/nir/nir_builder_opcodes_h.py
new file mode 100644
index 0000000..e482339
--- /dev/null
+++ b/src/glsl/nir/nir_builder_opcodes_h.py
@@ -0,0 +1,38 @@
+#! /usr/bin/env python
+
+template = """\
+/* Copyright (C) 2015 Broadcom
+ *
+ * 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.
+ */
+
+#ifndef _NIR_BUILDER_OPCODES_
+#define _NIR_BUILDER_OPCODES_
+
+% for name in sorted(opcodes.iterkeys()):
+ALU${opcodes[name].num_inputs}(${name});
+% endfor
+
+#endif /* _NIR_BUILDER_OPCODES_ */"""
+
+from nir_opcodes import opcodes
+from mako.template import Template
+
+print Template(template).render(opcodes=opcodes)
-- 
2.1.4



More information about the mesa-dev mailing list