Re: [Mesa-dev] [PATCH 02/11] nir: Add a pass for propagating invariant decorations

2016-06-17 Thread Kenneth Graunke
On Friday, June 17, 2016 1:53:19 PM PDT Jason Ekstrand wrote:
> This pass is similar to propagate_invariance in the GLSL compiler.  The
> real "output" of this pass is that any algebraic operations which are
> eventually consumed by an invariant variable get marked as "exact".
> 
> Signed-off-by: Jason Ekstrand 
> Cc: "12.0" 
> Cc: Kenneth Graunke 

Patches 2-8 are:
Reviewed-by: Kenneth Graunke 


signature.asc
Description: This is a digitally signed message part.
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


[Mesa-dev] [PATCH 02/11] nir: Add a pass for propagating invariant decorations

2016-06-17 Thread Jason Ekstrand
This pass is similar to propagate_invariance in the GLSL compiler.  The
real "output" of this pass is that any algebraic operations which are
eventually consumed by an invariant variable get marked as "exact".

Signed-off-by: Jason Ekstrand 
Cc: "12.0" 
Cc: Kenneth Graunke 
---
 src/compiler/Makefile.sources  |   1 +
 src/compiler/nir/nir.h |   2 +
 src/compiler/nir/nir_propagate_invariant.c | 196 +
 3 files changed, 199 insertions(+)
 create mode 100644 src/compiler/nir/nir_propagate_invariant.c

diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 5c4ea65..0ff9b23 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -231,6 +231,7 @@ NIR_FILES = \
nir/nir_phi_builder.c \
nir/nir_phi_builder.h \
nir/nir_print.c \
+   nir/nir_propagate_invariant.c \
nir/nir_remove_dead_variables.c \
nir/nir_repair_ssa.c \
nir/nir_search.c \
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index ec7b0c7..1725ee3 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -2290,6 +2290,8 @@ bool nir_lower_returns(nir_shader *shader);
 
 bool nir_inline_functions(nir_shader *shader);
 
+bool nir_propagate_invariant(nir_shader *shader);
+
 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);
 void nir_lower_var_copies(nir_shader *shader);
 
diff --git a/src/compiler/nir/nir_propagate_invariant.c 
b/src/compiler/nir/nir_propagate_invariant.c
new file mode 100644
index 000..7b5bd6c
--- /dev/null
+++ b/src/compiler/nir/nir_propagate_invariant.c
@@ -0,0 +1,196 @@
+/*
+ * 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.
+ */
+
+#include "nir.h"
+
+static void
+add_src(nir_src *src, struct set *invariants)
+{
+   if (src->is_ssa) {
+  _mesa_set_add(invariants, src->ssa);
+   } else {
+  _mesa_set_add(invariants, src->reg.reg);
+   }
+}
+
+static bool
+add_src_cb(nir_src *src, void *state)
+{
+   add_src(src, state);
+   return true;
+}
+
+static bool
+dest_is_invariant(nir_dest *dest, struct set *invariants)
+{
+   if (dest->is_ssa) {
+  return _mesa_set_search(invariants, >ssa);
+   } else {
+  return _mesa_set_search(invariants, dest->reg.reg);
+   }
+}
+
+static void
+add_cf_node(nir_cf_node *cf, struct set *invariants)
+{
+   if (cf->type == nir_cf_node_if) {
+  nir_if *if_stmt = nir_cf_node_as_if(cf);
+  add_src(_stmt->condition, invariants);
+   }
+
+   if (cf->parent)
+  add_cf_node(cf->parent, invariants);
+}
+
+static void
+add_var(nir_variable *var, struct set *invariants)
+{
+   _mesa_set_add(invariants, var);
+}
+
+static bool
+var_is_invariant(nir_variable *var, struct set * invariants)
+{
+   return var->data.invariant || _mesa_set_search(invariants, var);
+}
+
+static void
+propagate_invariant_instr(nir_instr *instr, struct set *invariants)
+{
+   switch (instr->type) {
+   case nir_instr_type_alu: {
+  nir_alu_instr *alu = nir_instr_as_alu(instr);
+  if (!dest_is_invariant(>dest.dest, invariants))
+ break;
+
+  alu->exact = true;
+  nir_foreach_src(instr, add_src_cb, invariants);
+  break;
+   }
+
+   case nir_instr_type_tex: {
+  nir_tex_instr *tex = nir_instr_as_tex(instr);
+  if (dest_is_invariant(>dest, invariants))
+ nir_foreach_src(instr, add_src_cb, invariants);
+  break;
+   }
+
+   case nir_instr_type_intrinsic: {
+  nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+  switch (intrin->intrinsic) {
+  case nir_intrinsic_copy_var:
+ /* If the destination is invariant then so is the source */
+ if (var_is_invariant(intrin->variables[0]->var, invariants))
+add_var(intrin->variables[1]->var, invariants);
+