[Mesa-dev] [PATCH 103/133] nir: Add a helper for getting a constant value from an SSA source

Jason Ekstrand jason at jlekstrand.net
Mon Dec 15 22:12:46 PST 2014


---
 src/glsl/nir/nir.c | 19 +++++++++++++++++++
 src/glsl/nir/nir.h |  1 +
 2 files changed, 20 insertions(+)

diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c
index a4edc79..f5183ce 100644
--- a/src/glsl/nir/nir.c
+++ b/src/glsl/nir/nir.c
@@ -1666,6 +1666,25 @@ nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
    return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
 }
 
+nir_const_value *
+nir_src_as_const_value(nir_src src)
+{
+   if (!src.is_ssa)
+      return NULL;
+
+   if (src.ssa->parent_instr->type != nir_instr_type_load_const)
+      return NULL;
+
+   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
+
+   if (load->array_elems == 0)
+      return &load->value;
+   if (load->array_elems == 1)
+      return load->array;
+   else
+      return NULL;
+}
+
 bool
 nir_srcs_equal(nir_src src1, nir_src src2)
 {
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index 412ceea..4fe5c68 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1326,6 +1326,7 @@ typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
 
+nir_const_value *nir_src_as_const_value(nir_src src);
 bool nir_srcs_equal(nir_src src1, nir_src src2);
 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
 
-- 
2.2.0



More information about the mesa-dev mailing list