Mesa (master): nir: use Python to autogenerate opcode information

Jason Ekstrand jekstrand at kemper.freedesktop.org
Sun Jan 25 05:55:29 UTC 2015


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

Author: Connor Abbott <cwabbott0 at gmail.com>
Date:   Thu Jan 22 23:32:14 2015 -0500

nir: use Python to autogenerate opcode information

Before, we used a system where a file, nir_opcodes.h, defined some macros that
were included to generate the enum values and the nir_op_infos structure. This
worked pretty well, but for development the error messages were never very
useful, Python tools couldn't understand the opcode list, and it was difficult
to use nir_opcodes.h to do other things like autogenerate a builder API. Now, we
store opcode information in nir_opcodes.py, and we have nir_opcodes_c.py to
generate the old nir_opcodes.c and nir_opcodes_h.py to generate nir_opcodes.h,
which contains all the enum names and gets included into nir.h like before.  In
addition to solving the above problems, using Python and Mako to generate
everything means that it's much easier to add keep information centralized as we
add new things like constant propagation that require per-opcode information.

v2:
 - make Opcode derive from object (Dylan)
 - don't use assert like it's a function (Dylan)
 - style fixes for fnoise, use xrange (Dylan)
 - use iterkeys() in nir_opcodes_h.py (Dylan)
 - use pydoc-style comments (Jason)
 - don't make fmin/fmax commutative and associative yet (Jason)

Signed-off-by: Connor Abbott <cwabbott0 at gmail.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand at intel.com>

v3 Jason Ekstrand <jason.ekstrand at intel.com>
 - Alphabetize source file lists
 - Generate nir_opcodes.h in the builddir instead of the source dir
 - Include $(builddir)/src/glsl/nir in the i965 build
 - Rework nir_opcodes.h generation so it generates a complete header file
   instead of one that has to be embedded inside an enum declaration

---

 src/glsl/Makefile.am                             |   16 +
 src/glsl/Makefile.sources                        |    4 +-
 src/glsl/nir/.gitignore                          |    2 +
 src/glsl/nir/nir.h                               |   16 +-
 src/glsl/nir/nir_opcodes.h                       |  366 ---------------------
 src/glsl/nir/nir_opcodes.py                      |  381 ++++++++++++++++++++++
 src/glsl/nir/nir_opcodes_c.py                    |   55 ++++
 src/glsl/nir/{nir_opcodes.c => nir_opcodes_h.py} |   39 +--
 src/mesa/drivers/dri/i965/Makefile.am            |    1 +
 9 files changed, 479 insertions(+), 401 deletions(-)

diff --git a/src/glsl/Makefile.am b/src/glsl/Makefile.am
index 78ccfee..1691c46 100644
--- a/src/glsl/Makefile.am
+++ b/src/glsl/Makefile.am
@@ -29,6 +29,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/glsl/glcpp \
 	-I$(top_srcdir)/src/glsl/nir \
 	-I$(top_srcdir)/src/gtest/include \
+	-I$(top_builddir)/src/glsl/nir \
 	$(DEFINES)
 AM_CFLAGS = $(VISIBILITY_CFLAGS)
 AM_CXXFLAGS = $(VISIBILITY_CXXFLAGS)
@@ -39,6 +40,9 @@ EXTRA_DIST = tests glcpp/tests README TODO glcpp/README	\
 	glcpp/glcpp-lex.l				\
 	glcpp/glcpp-parse.y				\
 	nir/nir_algebraic.py				\
+	nir/nir_opcodes.py				\
+	nir/nir_opcodes_c.py				\
+	nir/nir_opcodes_h.py				\
 	nir/nir_opt_algebraic.py			\
 	SConscript
 
@@ -211,6 +215,8 @@ BUILT_SOURCES =						\
 	glsl_lexer.cpp					\
 	glcpp/glcpp-parse.c				\
 	glcpp/glcpp-lex.c				\
+	nir/nir_opcodes.c				\
+	nir/nir_opcodes.h				\
 	nir/nir_opt_algebraic.c
 CLEANFILES =						\
 	glcpp/glcpp-parse.h				\
@@ -224,6 +230,16 @@ dist-hook:
 	$(RM) glcpp/tests/*.out
 	$(RM) glcpp/tests/subtest*/*.out
 
+nir/nir_opcodes.h: nir/nir_opcodes.py nir/nir_opcodes_h.py
+	$(MKDIR_P) nir;							\
+	$(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_opcodes_h.py > $@
+
+nir/nir.h: $(top_builddir)/src/glsl/nir/nir_opcodes.h
+
+nir/nir_opcodes.c: nir/nir_opcodes.py nir/nir_opcodes_c.py
+	$(MKDIR_P) nir;							\
+	$(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_opcodes_c.py > $@
+
 nir/nir_opt_algebraic.c: nir/nir_opt_algebraic.py nir/nir_algebraic.py
 	$(MKDIR_P) nir;							\
 	$(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_opt_algebraic.py > $@
diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index 96c4ec5..97c637e 100644
--- a/src/glsl/Makefile.sources
+++ b/src/glsl/Makefile.sources
@@ -11,6 +11,8 @@ LIBGLCPP_GENERATED_FILES = \
 	glcpp/glcpp-parse.c
 
 NIR_GENERATED_FILES = \
+	nir/nir_opcodes.c \
+	nir/nir_opcodes.h \
 	nir/nir_opt_algebraic.c
 
 NIR_FILES = \
@@ -35,8 +37,6 @@ NIR_FILES = \
 	nir/nir_lower_var_copies.c \
 	nir/nir_lower_vec_to_movs.c \
 	nir/nir_metadata.c \
-	nir/nir_opcodes.c \
-	nir/nir_opcodes.h \
 	nir/nir_opt_constant_folding.c \
 	nir/nir_opt_copy_propagate.c \
 	nir/nir_opt_cse.c \
diff --git a/src/glsl/nir/.gitignore b/src/glsl/nir/.gitignore
index 6d954fe..4c28193 100644
--- a/src/glsl/nir/.gitignore
+++ b/src/glsl/nir/.gitignore
@@ -1 +1,3 @@
 nir_opt_algebraic.c
+nir_opcodes.c
+nir_opcodes.h
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index 119ca01..0ef83a1 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -37,6 +37,8 @@
 #include "nir_types.h"
 #include <stdio.h>
 
+#include "nir_opcodes.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -573,20 +575,6 @@ void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx);
 void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
                        void *mem_ctx);
 
-#define OPCODE(name, num_inputs, output_size, output_type, \
-               input_sizes, input_types, algebraic_props) \
-   nir_op_##name,
-
-#define LAST_OPCODE(name) nir_last_opcode = nir_op_##name,
-
-typedef enum {
-#include "nir_opcodes.h"
-   nir_num_opcodes = nir_last_opcode + 1
-} nir_op;
-
-#undef OPCODE
-#undef LAST_OPCODE
-
 typedef enum {
    nir_type_float,
    nir_type_int,
diff --git a/src/glsl/nir/nir_opcodes.h b/src/glsl/nir/nir_opcodes.h
deleted file mode 100644
index c16b7fe..0000000
--- a/src/glsl/nir/nir_opcodes.h
+++ /dev/null
@@ -1,366 +0,0 @@
-/*
- * Copyright © 2014 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.
- *
- * Authors:
- *    Connor Abbott (cwabbott0 at gmail.com)
- *
- */
-
-/**
- * This header file defines all the available opcodes in one place. It expands
- * to a list of macros of the form:
- *
- * OPCODE(name, num_inputs, output_size, output_type,
- *        input_sizes, input_types, algebraic_properties)
- *
- * Which should correspond one-to-one with the nir_op_info structure. It is
- * included in both ir.h to create the nir_op enum (with members of the form
- * nir_op_(name)) and and in opcodes.c to create nir_op_infos, which is a
- * const array of nir_op_info structures for each opcode.
- */
-
-#define ARR(...) { __VA_ARGS__ }
-
-#define UNOP(name, type) OPCODE(name, 1, 0, type, ARR(0), ARR(type), 0)
-#define UNOP_CONVERT(name, in_type, out_type) \
-   OPCODE(name, 1, 0, out_type, ARR(0), ARR(in_type), 0)
-#define UNOP_HORIZ(name, output_size, output_type, input_size, input_type) \
-   OPCODE(name, 1, output_size, output_type, ARR(input_size), \
-          ARR(input_type), 0)
-
-#define UNOP_REDUCE(name, output_size, output_type, input_type) \
-   UNOP_HORIZ(name##2, output_size, output_type, 2, input_type) \
-   UNOP_HORIZ(name##3, output_size, output_type, 3, input_type) \
-   UNOP_HORIZ(name##4, output_size, output_type, 4, input_type)
-
-/**
- * These two move instructions differ in what modifiers they support and what
- * the negate modifier means. Otherwise, they are identical.
- */
-UNOP(fmov, nir_type_float)
-UNOP(imov, nir_type_int)
-
-UNOP(ineg, nir_type_int)
-UNOP(fneg, nir_type_float)
-UNOP(inot, nir_type_int) /* invert every bit of the integer */
-UNOP(fnot, nir_type_float) /* (src == 0.0) ? 1.0 : 0.0 */
-UNOP(fsign, nir_type_float)
-UNOP(isign, nir_type_int)
-UNOP(iabs, nir_type_int)
-UNOP(fabs, nir_type_float)
-UNOP(fsat, nir_type_float)
-UNOP(frcp, nir_type_float)
-UNOP(frsq, nir_type_float)
-UNOP(fsqrt, nir_type_float)
-UNOP(fexp, nir_type_float) /* < e^x */
-UNOP(flog, nir_type_float) /* log base e */
-UNOP(fexp2, nir_type_float)
-UNOP(flog2, nir_type_float)
-UNOP_CONVERT(f2i, nir_type_float, nir_type_int)       /**< Float-to-integer conversion. */
-UNOP_CONVERT(f2u, nir_type_float, nir_type_unsigned)  /**< Float-to-unsigned conversion. */
-UNOP_CONVERT(i2f, nir_type_int, nir_type_float)       /**< Integer-to-float conversion. */
-UNOP_CONVERT(f2b, nir_type_float, nir_type_bool)      /**< Float-to-boolean conversion */
-UNOP_CONVERT(b2f, nir_type_bool, nir_type_float)      /**< Boolean-to-float conversion */
-UNOP_CONVERT(i2b, nir_type_int, nir_type_bool)        /**< int-to-boolean conversion */
-UNOP_CONVERT(b2i, nir_type_bool, nir_type_int)        /**< Boolean-to-int conversion */
-UNOP_CONVERT(u2f, nir_type_unsigned, nir_type_float)  /**< Unsigned-to-float conversion. */
-
-UNOP_REDUCE(bany, 1, nir_type_bool, nir_type_bool) /* returns ~0 if any component of src[0] != 0 */
-UNOP_REDUCE(ball, 1, nir_type_bool, nir_type_bool) /* returns ~0 if all components of src[0] != 0 */
-UNOP_REDUCE(fany, 1, nir_type_float, nir_type_float) /* returns 1.0 if any component of src[0] != 0 */
-UNOP_REDUCE(fall, 1, nir_type_float, nir_type_float) /* returns 1.0 if all components of src[0] != 0 */
-
-/**
- * \name Unary floating-point rounding operations.
- */
-/*@{*/
-UNOP(ftrunc, nir_type_float)
-UNOP(fceil, nir_type_float)
-UNOP(ffloor, nir_type_float)
-UNOP(ffract, nir_type_float)
-UNOP(fround_even, nir_type_float)
-/*@}*/
-
-/**
- * \name Trigonometric operations.
- */
-/*@{*/
-UNOP(fsin, nir_type_float)
-UNOP(fcos, nir_type_float)
-UNOP(fsin_reduced, nir_type_float)
-UNOP(fcos_reduced, nir_type_float)
-/*@}*/
-
-/**
- * \name Partial derivatives.
- */
-/*@{*/
-UNOP(fddx, nir_type_float)
-UNOP(fddy, nir_type_float)
-UNOP(fddx_fine, nir_type_float)
-UNOP(fddy_fine, nir_type_float)
-UNOP(fddx_coarse, nir_type_float)
-UNOP(fddy_coarse, nir_type_float)
-/*@}*/
-
-/**
- * \name Floating point pack and unpack operations.
- */
-/*@{*/
-UNOP_HORIZ(pack_snorm_2x16, 1, nir_type_unsigned, 2, nir_type_float)
-UNOP_HORIZ(pack_snorm_4x8, 1, nir_type_unsigned, 4, nir_type_float)
-UNOP_HORIZ(pack_unorm_2x16, 1, nir_type_unsigned, 2, nir_type_float)
-UNOP_HORIZ(pack_unorm_4x8, 1, nir_type_unsigned, 4, nir_type_float)
-UNOP_HORIZ(pack_half_2x16, 1, nir_type_unsigned, 2, nir_type_float)
-UNOP_HORIZ(unpack_snorm_2x16, 2, nir_type_float, 1, nir_type_unsigned)
-UNOP_HORIZ(unpack_snorm_4x8, 4, nir_type_float, 1, nir_type_unsigned)
-UNOP_HORIZ(unpack_unorm_2x16, 2, nir_type_float, 1, nir_type_unsigned)
-UNOP_HORIZ(unpack_unorm_4x8, 4, nir_type_float, 1, nir_type_unsigned)
-UNOP_HORIZ(unpack_half_2x16, 2, nir_type_float, 1, nir_type_unsigned)
-/*@}*/
-
-/**
- * \name Lowered floating point unpacking operations.
- */
-/*@{*/
-UNOP_HORIZ(unpack_half_2x16_split_x, 1, nir_type_float, 1, nir_type_unsigned)
-UNOP_HORIZ(unpack_half_2x16_split_y, 1, nir_type_float, 1, nir_type_unsigned)
-/*@}*/
-
-/**
- * \name Bit operations, part of ARB_gpu_shader5.
- */
-/*@{*/
-UNOP(bitfield_reverse, nir_type_unsigned)
-UNOP(bit_count, nir_type_unsigned)
-UNOP_CONVERT(ufind_msb, nir_type_unsigned, nir_type_int)
-UNOP(ifind_msb, nir_type_int)
-UNOP(find_lsb, nir_type_int)
-/*@}*/
-
-UNOP_HORIZ(fnoise1_1, 1, nir_type_float, 1, nir_type_float)
-UNOP_HORIZ(fnoise1_2, 1, nir_type_float, 2, nir_type_float)
-UNOP_HORIZ(fnoise1_3, 1, nir_type_float, 3, nir_type_float)
-UNOP_HORIZ(fnoise1_4, 1, nir_type_float, 4, nir_type_float)
-UNOP_HORIZ(fnoise2_1, 2, nir_type_float, 1, nir_type_float)
-UNOP_HORIZ(fnoise2_2, 2, nir_type_float, 2, nir_type_float)
-UNOP_HORIZ(fnoise2_3, 2, nir_type_float, 3, nir_type_float)
-UNOP_HORIZ(fnoise2_4, 2, nir_type_float, 4, nir_type_float)
-UNOP_HORIZ(fnoise3_1, 3, nir_type_float, 1, nir_type_float)
-UNOP_HORIZ(fnoise3_2, 3, nir_type_float, 2, nir_type_float)
-UNOP_HORIZ(fnoise3_3, 3, nir_type_float, 3, nir_type_float)
-UNOP_HORIZ(fnoise3_4, 3, nir_type_float, 4, nir_type_float)
-UNOP_HORIZ(fnoise4_1, 4, nir_type_float, 1, nir_type_float)
-UNOP_HORIZ(fnoise4_2, 4, nir_type_float, 2, nir_type_float)
-UNOP_HORIZ(fnoise4_3, 4, nir_type_float, 3, nir_type_float)
-UNOP_HORIZ(fnoise4_4, 4, nir_type_float, 4, nir_type_float)
-
-#define BINOP(name, type, alg_props) \
-   OPCODE(name, 2, 0, type, ARR(0, 0), ARR(type, type), alg_props)
-#define BINOP_CONVERT(name, out_type, in_type, alg_props) \
-   OPCODE(name, 2, 0, out_type, ARR(0, 0), ARR(in_type, in_type), alg_props)
-#define BINOP_COMPARE(name, type, alg_props) \
-   OPCODE(name, 2, 0, nir_type_bool, ARR(0, 0), ARR(type, type), alg_props)
-#define BINOP_HORIZ(name, output_size, output_type, src1_size, src1_type, \
-                    src2_size, src2_type) \
-   OPCODE(name, 2, output_size, output_type, ARR(src1_size, src2_size), \
-          ARR(src1_type, src2_type), 0)
-#define BINOP_REDUCE(name, output_size, output_type, src_type) \
-   OPCODE(name##2, 2, output_size, output_type, \
-          ARR(2, 2), ARR(src_type, src_type), NIR_OP_IS_COMMUTATIVE) \
-   OPCODE(name##3, 2, output_size, output_type, \
-          ARR(3, 3), ARR(src_type, src_type), NIR_OP_IS_COMMUTATIVE) \
-   OPCODE(name##4, 2, output_size, output_type, \
-          ARR(4, 4), ARR(src_type, src_type), NIR_OP_IS_COMMUTATIVE)
-
-BINOP(fadd, nir_type_float, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-BINOP(iadd, nir_type_int, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-BINOP(fsub, nir_type_float, 0)
-BINOP(isub, nir_type_int, 0)
-
-BINOP(fmul, nir_type_float, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-/* low 32-bits of signed/unsigned integer multiply */
-BINOP(imul, nir_type_int, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-/* high 32-bits of signed integer multiply */
-BINOP(imul_high, nir_type_int, NIR_OP_IS_COMMUTATIVE)
-/* high 32-bits of unsigned integer multiply */
-BINOP(umul_high, nir_type_unsigned, NIR_OP_IS_COMMUTATIVE)
-
-BINOP(fdiv, nir_type_float, 0)
-BINOP(idiv, nir_type_int, 0)
-BINOP(udiv, nir_type_unsigned, 0)
-
-/**
- * returns a boolean representing the carry resulting from the addition of
- * the two unsigned arguments.
- */
-BINOP_CONVERT(uadd_carry, nir_type_bool, nir_type_unsigned,
-              NIR_OP_IS_COMMUTATIVE)
-
-/**
- * returns a boolean representing the borrow resulting from the subtraction
- * of the two unsigned arguments.
- */
-BINOP_CONVERT(usub_borrow, nir_type_bool, nir_type_unsigned, 0)
-
-BINOP(fmod, nir_type_float, 0)
-BINOP(umod, nir_type_unsigned, 0)
-
-/**
- * \name comparisons
- */
-/*@{*/
-
-/**
- * these integer-aware comparisons return a boolean (0 or ~0)
- */
-BINOP_COMPARE(flt, nir_type_float, 0)
-BINOP_COMPARE(fge, nir_type_float, 0)
-BINOP_COMPARE(feq, nir_type_float, NIR_OP_IS_COMMUTATIVE)
-BINOP_COMPARE(fne, nir_type_float, NIR_OP_IS_COMMUTATIVE)
-BINOP_COMPARE(ilt, nir_type_int, 0)
-BINOP_COMPARE(ige, nir_type_int, 0)
-BINOP_COMPARE(ieq, nir_type_int, NIR_OP_IS_COMMUTATIVE)
-BINOP_COMPARE(ine, nir_type_int, NIR_OP_IS_COMMUTATIVE)
-BINOP_COMPARE(ult, nir_type_unsigned, 0)
-BINOP_COMPARE(uge, nir_type_unsigned, 0)
-
-/** integer-aware GLSL-style comparisons that compare floats and ints */
-BINOP_REDUCE(ball_fequal,  1, nir_type_bool, nir_type_float)
-BINOP_REDUCE(bany_fnequal, 1, nir_type_bool, nir_type_float)
-BINOP_REDUCE(ball_iequal,  1, nir_type_bool, nir_type_int)
-BINOP_REDUCE(bany_inequal, 1, nir_type_bool, nir_type_int)
-
-/** non-integer-aware GLSL-style comparisons that return 0.0 or 1.0 */
-BINOP_REDUCE(fall_equal,  1, nir_type_float, nir_type_float)
-BINOP_REDUCE(fany_nequal, 1, nir_type_float, nir_type_float)
-
-/**
- * These comparisons for integer-less hardware return 1.0 and 0.0 for true
- * and false respectively
- */
-BINOP(slt, nir_type_float, 0) /* Set on Less Than */
-BINOP(sge, nir_type_float, 0) /* Set on Greater Than or Equal */
-BINOP(seq, nir_type_float, NIR_OP_IS_COMMUTATIVE) /* Set on Equal */
-BINOP(sne, nir_type_float, NIR_OP_IS_COMMUTATIVE) /* Set on Not Equal */
-
-/*@}*/
-
-BINOP(ishl, nir_type_int, 0)
-BINOP(ishr, nir_type_int, 0)
-BINOP(ushr, nir_type_unsigned, 0)
-
-/**
- * \name bitwise logic operators
- *
- * These are also used as boolean and, or, xor for hardware supporting
- * integers.
- */
-/*@{*/
-BINOP(iand, nir_type_unsigned, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-BINOP(ior, nir_type_unsigned, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-BINOP(ixor, nir_type_unsigned, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-/*@{*/
-
-/**
- * \name floating point logic operators
- *
- * These use (src != 0.0) for testing the truth of the input, and output 1.0
- * for true and 0.0 for false
- */
-BINOP(fand, nir_type_float, NIR_OP_IS_COMMUTATIVE)
-BINOP(for, nir_type_float, NIR_OP_IS_COMMUTATIVE)
-BINOP(fxor, nir_type_float, NIR_OP_IS_COMMUTATIVE)
-
-BINOP_REDUCE(fdot, 1, nir_type_float, nir_type_float)
-
-BINOP(fmin, nir_type_float, 0)
-BINOP(imin, nir_type_int, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-BINOP(umin, nir_type_unsigned, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-BINOP(fmax, nir_type_float, 0)
-BINOP(imax, nir_type_int, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-BINOP(umax, nir_type_unsigned, NIR_OP_IS_COMMUTATIVE | NIR_OP_IS_ASSOCIATIVE)
-
-BINOP(fpow, nir_type_float, 0)
-
-BINOP_HORIZ(pack_half_2x16_split, 1, nir_type_unsigned, 1, nir_type_float, 1, nir_type_float)
-
-BINOP(bfm, nir_type_unsigned, 0)
-
-BINOP(ldexp, nir_type_unsigned, 0)
-
-/**
- * Combines the first component of each input to make a 2-component vector.
- */
-BINOP_HORIZ(vec2, 2, nir_type_unsigned, 1, nir_type_unsigned, 1, nir_type_unsigned)
-
-#define TRIOP(name, type) \
-   OPCODE(name, 3, 0, type, ARR(0, 0, 0), ARR(type, type, type), 0)
-#define TRIOP_HORIZ(name, output_size, src1_size, src2_size, src3_size) \
-   OPCODE(name, 3, output_size, nir_type_unsigned, \
-   ARR(src1_size, src2_size, src3_size), \
-   ARR(nir_type_unsigned, nir_type_unsigned, nir_type_unsigned), 0)
-
-/* fma(a, b, c) = (a * b) + c */
-TRIOP(ffma, nir_type_float)
-
-TRIOP(flrp, nir_type_float)
-
-/**
- * \name Conditional Select
- *
- * A vector conditional select instruction (like ?:, but operating per-
- * component on vectors). There are two versions, one for floating point
- * bools (0.0 vs 1.0) and one for integer bools (0 vs ~0).
- */
-
-TRIOP(fcsel, nir_type_float)
-OPCODE(bcsel, 3, 0, nir_type_unsigned, ARR(0, 0, 0),
-       ARR(nir_type_bool, nir_type_unsigned, nir_type_unsigned), 0)
-
-TRIOP(bfi, nir_type_unsigned)
-
-TRIOP(ubitfield_extract, nir_type_unsigned)
-OPCODE(ibitfield_extract, 3, 0, nir_type_int, ARR(0, 0, 0),
-       ARR(nir_type_int, nir_type_unsigned, nir_type_unsigned), 0)
-
-/**
- * Combines the first component of each input to make a 3-component vector.
- */
-TRIOP_HORIZ(vec3, 3, 1, 1, 1)
-
-#define QUADOP(name) \
-   OPCODE(name, 4, 0, nir_type_unsigned, ARR(0, 0, 0, 0), \
-          ARR(nir_type_unsigned, nir_type_unsigned, nir_type_unsigned, nir_type_unsigned), \
-          0)
-#define QUADOP_HORIZ(name, output_size, src1_size, src2_size, src3_size, \
-                     src4_size) \
-   OPCODE(name, 4, output_size, nir_type_unsigned, \
-          ARR(src1_size, src2_size, src3_size, src4_size), \
-          ARR(nir_type_unsigned, nir_type_unsigned, nir_type_unsigned, nir_type_unsigned), \
-          0)
-
-QUADOP(bitfield_insert)
-
-QUADOP_HORIZ(vec4, 4, 1, 1, 1, 1)
-
-LAST_OPCODE(vec4)
diff --git a/src/glsl/nir/nir_opcodes.py b/src/glsl/nir/nir_opcodes.py
new file mode 100644
index 0000000..5bafbb0
--- /dev/null
+++ b/src/glsl/nir/nir_opcodes.py
@@ -0,0 +1,381 @@
+#! /usr/bin/env python
+#
+# Copyright (C) 2014 Connor Abbott
+#
+# 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.
+#
+# Authors:
+#    Connor Abbott (cwabbott0 at gmail.com)
+
+# Class that represents all the information we have about the opcode
+# NOTE: this must be kept in sync with nir_op_info
+
+class Opcode(object):
+   """Class that represents all the information we have about the opcode
+   NOTE: this must be kept in sync with nir_op_info
+   """
+   def __init__(self, name, output_size, output_type, input_sizes,
+                input_types, algebraic_properties):
+      """Parameters:
+
+      - name is the name of the opcode (prepend nir_op_ for the enum name)
+      - all types are strings that get nir_type_ prepended to them
+      - input_types is a list of types
+      - algebraic_properties is a space-seperated string, where nir_op_is_ is
+        prepended before each entry
+      """
+      assert isinstance(name, str)
+      assert isinstance(output_size, int)
+      assert isinstance(output_type, str)
+      assert isinstance(input_sizes, list)
+      assert isinstance(input_sizes[0], int)
+      assert isinstance(input_types, list)
+      assert isinstance(input_types[0], str)
+      assert isinstance(algebraic_properties, str)
+      assert len(input_sizes) == len(input_types)
+      assert 0 <= output_size <= 4
+      for size in input_sizes:
+         assert 0 <= size <= 4
+         if output_size != 0:
+            assert size != 0
+      self.name = name
+      self.num_inputs = len(input_sizes)
+      self.output_size = output_size
+      self.output_type = output_type
+      self.input_sizes = input_sizes
+      self.input_types = input_types
+      self.algebraic_properties = algebraic_properties
+
+# helper variables for strings
+tfloat = "float"
+tint = "int"
+tbool = "bool"
+tunsigned = "unsigned"
+
+commutative = "commutative "
+associative = "associative "
+
+# global dictionary of opcodes
+opcodes = {}
+
+def opcode(name, output_size, output_type, input_sizes, input_types,
+           algebraic_properties):
+   assert name not in opcodes
+   opcodes[name] = Opcode(name, output_size, output_type, input_sizes,
+                          input_types, algebraic_properties)
+
+def unop_convert(name, in_type, out_type):
+   opcode(name, 0, out_type, [0], [in_type], "")
+
+def unop(name, ty):
+   opcode(name, 0, ty, [0], [ty], "")
+
+def unop_horiz(name, output_size, output_type, input_size, input_type):
+   opcode(name, output_size, output_type, [input_size], [input_type], "")
+
+def unop_reduce(name, output_size, output_type, input_type):
+   unop_horiz(name + "2", output_size, output_type, 2, input_type)
+   unop_horiz(name + "3", output_size, output_type, 3, input_type)
+   unop_horiz(name + "4", output_size, output_type, 4, input_type)
+
+
+# These two move instructions differ in what modifiers they support and what
+# the negate modifier means. Otherwise, they are identical.
+unop("fmov", tfloat)
+unop("imov", tint)
+
+unop("ineg", tint)
+unop("fneg", tfloat)
+unop("inot", tint) # invert every bit of the integer
+unop("fnot", tfloat) # (src == 0.0) ? 1.0 : 0.0
+unop("fsign", tfloat)
+unop("isign", tint)
+unop("iabs", tint)
+unop("fabs", tfloat)
+unop("fsat", tfloat)
+unop("frcp", tfloat)
+unop("frsq", tfloat)
+unop("fsqrt", tfloat)
+unop("fexp", tfloat) # < e^x
+unop("flog", tfloat) # log base e
+unop("fexp2", tfloat)
+unop("flog2", tfloat)
+unop_convert("f2i", tfloat, tint) # Float-to-integer conversion.
+unop_convert("f2u", tfloat, tunsigned) # Float-to-unsigned conversion
+unop_convert("i2f", tint, tfloat) # Integer-to-float conversion.
+unop_convert("f2b", tfloat, tbool) # Float-to-boolean conversion
+unop_convert("b2f", tbool, tfloat) # Boolean-to-float conversion
+unop_convert("i2b", tint, tbool) # int-to-boolean conversion
+unop_convert("b2i", tbool, tint) # Boolean-to-int conversion
+unop_convert("u2f", tunsigned, tfloat) #Unsigned-to-float conversion.
+
+unop_reduce("bany", 1, tbool, tbool) # returns ~0 if any component of src[0] != 0
+unop_reduce("ball", 1, tbool, tbool) # returns ~0 if all components of src[0] != 0
+unop_reduce("fany", 1, tfloat, tfloat) # returns 1.0 if any component of src[0] != 0
+unop_reduce("fall", 1, tfloat, tfloat) # returns 1.0 if all components of src[0] != 0
+
+# Unary floating-point rounding operations.
+
+
+unop("ftrunc", tfloat)
+unop("fceil", tfloat)
+unop("ffloor", tfloat)
+unop("ffract", tfloat)
+unop("fround_even", tfloat)
+
+
+# Trigonometric operations.
+
+
+unop("fsin", tfloat)
+unop("fcos", tfloat)
+unop("fsin_reduced", tfloat)
+unop("fcos_reduced", tfloat)
+
+
+# Partial derivatives.
+
+
+unop("fddx", tfloat)
+unop("fddy", tfloat)
+unop("fddx_fine", tfloat)
+unop("fddy_fine", tfloat)
+unop("fddx_coarse", tfloat)
+unop("fddy_coarse", tfloat)
+
+
+# Floating point pack and unpack operations.
+
+
+unop_horiz("pack_snorm_2x16", 1, tunsigned, 2, tfloat)
+unop_horiz("pack_snorm_4x8", 1, tunsigned, 4, tfloat)
+unop_horiz("pack_unorm_2x16", 1, tunsigned, 2, tfloat)
+unop_horiz("pack_unorm_4x8", 1, tunsigned, 4, tfloat)
+unop_horiz("pack_half_2x16", 1, tunsigned, 2, tfloat)
+unop_horiz("unpack_snorm_2x16", 2, tfloat, 1, tunsigned)
+unop_horiz("unpack_snorm_4x8", 4, tfloat, 1, tunsigned)
+unop_horiz("unpack_unorm_2x16", 2, tfloat, 1, tunsigned)
+unop_horiz("unpack_unorm_4x8", 4, tfloat, 1, tunsigned)
+unop_horiz("unpack_half_2x16", 2, tfloat, 1, tunsigned)
+
+
+# Lowered floating point unpacking operations.
+
+
+unop_horiz("unpack_half_2x16_split_x", 1, tfloat, 1, tunsigned)
+unop_horiz("unpack_half_2x16_split_y", 1, tfloat, 1, tunsigned)
+
+
+# Bit operations, part of ARB_gpu_shader5.
+
+
+unop("bitfield_reverse", tunsigned)
+unop("bit_count", tunsigned)
+unop_convert("ufind_msb", tunsigned, tint)
+unop("ifind_msb", tint)
+unop("find_lsb", tint)
+
+
+for i in xrange(1, 5):
+   for j in xrange(1, 5):
+      unop_horiz("fnoise{0}_{1}".format(i, j), i, tfloat, j, tfloat)
+
+def binop_convert(name, out_type, in_type, alg_props):
+   opcode(name, 0, out_type, [0, 0], [in_type, in_type], alg_props)
+
+def binop(name, ty, alg_props):
+   binop_convert(name, ty, ty, alg_props)
+
+def binop_compare(name, ty, alg_props):
+   binop_convert(name, ty, tbool, alg_props)
+
+def binop_horiz(name, out_size, out_type, src1_size, src1_type, src2_size,
+                src2_type):
+   opcode(name, out_size, out_type, [src1_size, src2_size], [src1_type, src2_type], "")
+
+def binop_reduce(name, output_size, output_type, src_type):
+   opcode(name + "2",output_size, output_type,
+          [2, 2], [src_type, src_type], commutative)
+   opcode(name + "3", output_size, output_type,
+          [3, 3], [src_type, src_type], commutative)
+   opcode(name + "4", output_size, output_type,
+          [4, 4], [src_type, src_type], commutative)
+
+binop("fadd", tfloat, commutative + associative)
+binop("iadd", tint, commutative + associative)
+binop("fsub", tfloat, "")
+binop("isub", tint, "")
+
+binop("fmul", tfloat, commutative + associative)
+# low 32-bits of signed/unsigned integer multiply
+binop("imul", tint, commutative + associative)
+# high 32-bits of signed integer multiply
+binop("imul_high", tint, commutative)
+# high 32-bits of unsigned integer multiply
+binop("umul_high", tunsigned, commutative)
+
+binop("fdiv", tfloat, "")
+binop("idiv", tint, "")
+binop("udiv", tunsigned, "")
+
+# returns a boolean representing the carry resulting from the addition of
+# the two unsigned arguments.
+
+binop_convert("uadd_carry", tbool, tunsigned,
+              commutative)
+
+# returns a boolean representing the borrow resulting from the subtraction
+# of the two unsigned arguments.
+
+binop_convert("usub_borrow", tbool, tunsigned, "")
+
+binop("fmod", tfloat, "")
+binop("umod", tunsigned, "")
+
+#
+# Comparisons
+#
+
+
+# these integer-aware comparisons return a boolean (0 or ~0)
+
+binop_compare("flt", tfloat, "")
+binop_compare("fge", tfloat, "")
+binop_compare("feq", tfloat, commutative)
+binop_compare("fne", tfloat, commutative)
+binop_compare("ilt", tint, "")
+binop_compare("ige", tint, "")
+binop_compare("ieq", tint, commutative)
+binop_compare("ine", tint, commutative)
+binop_compare("ult", tunsigned, "")
+binop_compare("uge", tunsigned, "")
+
+# integer-aware GLSL-style comparisons that compare floats and ints
+
+binop_reduce("ball_fequal",  1, tbool, tfloat)
+binop_reduce("bany_fnequal", 1, tbool, tfloat)
+binop_reduce("ball_iequal",  1, tbool, tint)
+binop_reduce("bany_inequal", 1, tbool, tint)
+
+# non-integer-aware GLSL-style comparisons that return 0.0 or 1.0
+
+binop_reduce("fall_equal",  1, tfloat, tfloat)
+binop_reduce("fany_nequal", 1, tfloat, tfloat)
+
+# These comparisons for integer-less hardware return 1.0 and 0.0 for true
+# and false respectively
+
+binop("slt", tfloat, "") # Set on Less Than
+binop("sge", tfloat, "") # Set on Greater Than or Equal
+binop("seq", tfloat, commutative) # Set on Equal
+binop("sne", tfloat, commutative) # Set on Not Equal
+
+
+binop("ishl", tint, "")
+binop("ishr", tint, "")
+binop("ushr", tunsigned, "")
+
+# bitwise logic operators
+#
+# These are also used as boolean and, or, xor for hardware supporting
+# integers.
+
+
+binop("iand", tunsigned, commutative + associative)
+binop("ior", tunsigned, commutative + associative)
+binop("ixor", tunsigned, commutative + associative)
+
+
+# floating point logic operators
+#
+# These use (src != 0.0) for testing the truth of the input, and output 1.0
+# for true and 0.0 for false
+
+binop("fand", tfloat, commutative)
+binop("for", tfloat, commutative)
+binop("fxor", tfloat, commutative)
+
+binop_reduce("fdot", 1, tfloat, tfloat)
+
+binop("fmin", tfloat, "")
+binop("imin", tint, commutative + associative)
+binop("umin", tunsigned, commutative + associative)
+binop("fmax", tfloat, "")
+binop("imax", tint, commutative + associative)
+binop("umax", tunsigned, commutative + associative)
+
+binop("fpow", tfloat, "")
+
+binop_horiz("pack_half_2x16_split", 1, tunsigned, 1, tfloat, 1, tfloat)
+
+binop("bfm", tunsigned, "")
+
+binop("ldexp", tunsigned, "")
+
+# Combines the first component of each input to make a 2-component vector.
+
+binop_horiz("vec2", 2, tunsigned, 1, tunsigned, 1, tunsigned)
+
+def triop(name, ty):
+   opcode(name, 0, ty, [0, 0, 0], [ty, ty, ty], "")
+def triop_horiz(name, output_size, src1_size, src2_size, src3_size):
+   opcode(name, output_size, tunsigned,
+   [src1_size, src2_size, src3_size],
+   [tunsigned, tunsigned, tunsigned], "")
+
+# fma(a, b, c) = (a# b) + c
+triop("ffma", tfloat)
+
+triop("flrp", tfloat)
+
+# Conditional Select
+#
+# A vector conditional select instruction (like ?:, but operating per-
+# component on vectors). There are two versions, one for floating point
+# bools (0.0 vs 1.0) and one for integer bools (0 vs ~0).
+
+
+triop("fcsel", tfloat)
+opcode("bcsel", 0, tunsigned, [0, 0, 0],
+       [tbool, tunsigned, tunsigned], "")
+
+triop("bfi", tunsigned)
+
+triop("ubitfield_extract", tunsigned)
+opcode("ibitfield_extract", 0, tint, [0, 0, 0],
+       [tint, tunsigned, tunsigned], "")
+
+# Combines the first component of each input to make a 3-component vector.
+
+triop_horiz("vec3", 3, 1, 1, 1)
+
+def quadop(name):
+   opcode(name, 0, tunsigned, [0, 0, 0, 0],
+          [tunsigned, tunsigned, tunsigned, tunsigned],
+          "")
+def quadop_horiz(name, output_size, src1_size, src2_size, src3_size, src4_size):
+   opcode(name, output_size, tunsigned,
+          [src1_size, src2_size, src3_size, src4_size],
+          [tunsigned, tunsigned, tunsigned, tunsigned],
+          "")
+
+quadop("bitfield_insert")
+
+quadop_horiz("vec4", 4, 1, 1, 1, 1)
diff --git a/src/glsl/nir/nir_opcodes_c.py b/src/glsl/nir/nir_opcodes_c.py
new file mode 100644
index 0000000..7049c5b
--- /dev/null
+++ b/src/glsl/nir/nir_opcodes_c.py
@@ -0,0 +1,55 @@
+#! /usr/bin/env python
+#
+# Copyright (C) 2014 Connor Abbott
+#
+# 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.
+#
+# Authors:
+#    Connor Abbott (cwabbott0 at gmail.com)
+
+from nir_opcodes import opcodes
+from mako.template import Template
+
+template = Template("""
+#include "nir.h"
+
+const nir_op_info nir_op_infos[nir_num_opcodes] = {
+% for name, opcode in sorted(opcodes.iteritems()):
+{
+   .name = "${name}",
+   .num_inputs = ${opcode.num_inputs},
+   .output_size = ${opcode.output_size},
+   .output_type = ${"nir_type_" + opcode.output_type},
+   .input_sizes = {
+      ${ ", ".join(str(size) for size in opcode.input_sizes) }
+   },
+   .input_types = {
+      ${ ", ".join("nir_type_" + type for type in opcode.input_types) }
+   },
+   .algebraic_properties =
+      ${ "0" if opcode.algebraic_properties == "" else " | ".join(
+            "NIR_OP_IS_" + prop.upper() for prop in
+               opcode.algebraic_properties.strip().split(" ")) }
+},
+% endfor
+};
+""")
+
+print template.render(opcodes=opcodes)
diff --git a/src/glsl/nir/nir_opcodes.c b/src/glsl/nir/nir_opcodes_h.py
similarity index 68%
rename from src/glsl/nir/nir_opcodes.c
rename to src/glsl/nir/nir_opcodes_h.py
index 1e66c55..be15a96 100644
--- a/src/glsl/nir/nir_opcodes.c
+++ b/src/glsl/nir/nir_opcodes_h.py
@@ -1,5 +1,7 @@
-/*
- * Copyright © 2014 Intel Corporation
+#! /usr/bin/env python
+
+template = """\
+/* Copyright (C) 2014 Connor Abbott
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -22,25 +24,24 @@
  *
  * Authors:
  *    Connor Abbott (cwabbott0 at gmail.com)
- *
  */
 
-#include "nir.h"
+#ifndef _NIR_OPCODES_
+#define _NIR_OPCODES_
+
+<% opcode_names = sorted(opcodes.iterkeys()) %>
+
+typedef enum {
+% for name in opcode_names:
+   nir_op_${name},
+% endfor
+   nir_last_opcode = nir_op_${opcode_names[-1]},
+   nir_num_opcodes = nir_last_opcode + 1
+} nir_op;
 
-#define OPCODE(_name, _num_inputs, _output_size, _output_type, \
-               _input_sizes, _input_types, _algebraic_props) \
-{ \
-   .name = #_name, \
-   .num_inputs = _num_inputs, \
-   .output_size = _output_size, \
-   .output_type = _output_type, \
-   .input_sizes = _input_sizes, \
-   .input_types = _input_types, \
-   .algebraic_properties = _algebraic_props, \
-},
+#endif /* _NIR_OPCODES_ */"""
 
-#define LAST_OPCODE(name)
+from nir_opcodes import opcodes
+from mako.template import Template
 
-const nir_op_info nir_op_infos[nir_num_opcodes] = {
-#include "nir_opcodes.h"
-};
+print Template(template).render(opcodes=opcodes)
diff --git a/src/mesa/drivers/dri/i965/Makefile.am b/src/mesa/drivers/dri/i965/Makefile.am
index b74c7d7..07eefce 100644
--- a/src/mesa/drivers/dri/i965/Makefile.am
+++ b/src/mesa/drivers/dri/i965/Makefile.am
@@ -31,6 +31,7 @@ AM_CFLAGS = \
 	-I$(top_srcdir)/src/mesa/drivers/dri/common \
 	-I$(top_srcdir)/src/mesa/drivers/dri/intel/server \
 	-I$(top_srcdir)/src/gtest/include \
+	-I$(top_builddir)/src/glsl/nir \
 	-I$(top_builddir)/src/mesa/drivers/dri/common \
 	$(DEFINES) \
 	$(VISIBILITY_CFLAGS) \




More information about the mesa-commit mailing list