[Mesa-dev] [PATCH 2/3] nir/search: Add support for matching unknown constants

Jason Ekstrand jason at jlekstrand.net
Fri Jan 23 22:04:40 PST 2015


There are some algebraic transformations that we want to do but only if
certain things are constants.  For instance, we may want to replace
a * (b + c) with (a * b) + (a * c) as long as a and either b or c is constant.
While this generates more instructions, some of it will get constant
folded.  This commit allows you to match an arbitrary constant value by
adding a "#" on the front of a variable name.
---
 src/glsl/nir/nir_algebraic.py | 8 ++++++++
 src/glsl/nir/nir_search.c     | 6 ++++++
 src/glsl/nir/nir_search.h     | 7 +++++++
 3 files changed, 21 insertions(+)

diff --git a/src/glsl/nir/nir_algebraic.py b/src/glsl/nir/nir_algebraic.py
index f9b246d..5afd53d 100644
--- a/src/glsl/nir/nir_algebraic.py
+++ b/src/glsl/nir/nir_algebraic.py
@@ -60,6 +60,7 @@ static const ${val.c_type} ${val.name} = {
    { ${hex(val)} /* ${val.value} */ },
 % elif isinstance(val, Variable):
    ${val.index}, /* ${val.var_name} */
+   ${'true' if val.is_constant else 'false'},
 % elif isinstance(val, Expression):
    nir_op_${val.opcode},
    { ${', '.join(src.c_ptr for src in val.sources)} },
@@ -109,6 +110,13 @@ class Constant(Value):
 class Variable(Value):
    def __init__(self, val, name, varset):
       Value.__init__(self, name, "variable")
+
+      if val.startswith('#'):
+         val = val[1:]
+         self.is_constant = True
+      else:
+         self.is_constant = False
+
       self.var_name = val
       self.index = varset[val]
       self.name = name
diff --git a/src/glsl/nir/nir_search.c b/src/glsl/nir/nir_search.c
index 18e0330..ec89817 100644
--- a/src/glsl/nir/nir_search.c
+++ b/src/glsl/nir/nir_search.c
@@ -78,6 +78,10 @@ match_value(const nir_search_value *value, nir_alu_instr *instr, unsigned src,
 
          return true;
       } else {
+         if (var->is_constant &&
+             instr->src[src].src.ssa->parent_instr->type != nir_instr_type_load_const)
+            return false;
+
          state->variables_seen |= (1 << var->variable);
          state->variables[var->variable].src = instr->src[src].src;
          state->variables[var->variable].abs = false;
@@ -236,6 +240,8 @@ construct_value(const nir_search_value *value, nir_alu_type type,
       nir_alu_src val;
       nir_alu_src_copy(&val, &state->variables[var->variable], mem_ctx);
 
+      assert(!var->is_constant);
+
       return val;
    }
 
diff --git a/src/glsl/nir/nir_search.h b/src/glsl/nir/nir_search.h
index 8ec58b0..18aa28d 100644
--- a/src/glsl/nir/nir_search.h
+++ b/src/glsl/nir/nir_search.h
@@ -47,6 +47,13 @@ typedef struct {
 
    /** The variable index;  Must be less than NIR_SEARCH_MAX_VARIABLES */
    unsigned variable;
+
+   /** Indicates that the given variable must be a constant
+    *
+    * This is only alloed in search expressions and indicates that the
+    * given variable is only allowed to match constant values.
+    */
+   bool is_constant;
 } nir_search_variable;
 
 typedef struct {
-- 
2.2.1



More information about the mesa-dev mailing list