[Mesa-dev] [PATCH] nir+drivers: add helpers to get # of src/dest components

Rob Clark robdclark at gmail.com
Wed Mar 28 12:42:43 UTC 2018


Add helpers to get the number of src/dest components for an intrinsic,
and update spots that were open-coding this logic to use the helpers
instead.

Signed-off-by: Rob Clark <robdclark at gmail.com>
---
 src/compiler/nir/nir.h                             | 22 ++++++++++++++++++++++
 src/compiler/nir/nir_opt_copy_propagate.c          |  5 +----
 src/compiler/nir/nir_validate.c                    | 10 ++--------
 src/compiler/spirv/spirv_to_nir.c                  |  3 +--
 .../drivers/freedreno/ir3/ir3_compiler_nir.c       |  6 +-----
 src/intel/compiler/brw_fs_nir.cpp                  | 11 +++++------
 6 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 9fff1f4647d..2f4ff193fe6 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -1134,6 +1134,28 @@ typedef struct {
 
 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
 
+static inline unsigned
+nir_intrinsic_src_components(nir_intrinsic_instr *intr, unsigned srcn)
+{
+   const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
+   assert(srcn < info->num_srcs);
+   if (info->src_components[srcn])
+      return info->src_components[srcn];
+   else
+      return intr->num_components;
+}
+
+static inline unsigned
+nir_intrinsic_dest_components(nir_intrinsic_instr *intr)
+{
+   const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
+   if (!info->has_dest)
+      return 0;
+   else if (info->dest_components)
+      return info->dest_components;
+   else
+      return intr->num_components;
+}
 
 #define INTRINSIC_IDX_ACCESSORS(name, flag, type)                             \
 static inline type                                                            \
diff --git a/src/compiler/nir/nir_opt_copy_propagate.c b/src/compiler/nir/nir_opt_copy_propagate.c
index c4001fa73f5..3cd476a1b97 100644
--- a/src/compiler/nir/nir_opt_copy_propagate.c
+++ b/src/compiler/nir/nir_opt_copy_propagate.c
@@ -257,10 +257,7 @@ copy_prop_instr(nir_instr *instr)
       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
       for (unsigned i = 0;
            i < nir_intrinsic_infos[intrin->intrinsic].num_srcs; i++) {
-         unsigned num_components =
-            nir_intrinsic_infos[intrin->intrinsic].src_components[i];
-         if (!num_components)
-            num_components = intrin->num_components;
+         unsigned num_components = nir_intrinsic_src_components(intrin, i);
 
          while (copy_prop_src(&intrin->src[i], instr, NULL, num_components))
             progress = true;
diff --git a/src/compiler/nir/nir_validate.c b/src/compiler/nir/nir_validate.c
index 725ba43152c..81376f98b22 100644
--- a/src/compiler/nir/nir_validate.c
+++ b/src/compiler/nir/nir_validate.c
@@ -483,10 +483,7 @@ validate_intrinsic_instr(nir_intrinsic_instr *instr, validate_state *state)
 
    unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
    for (unsigned i = 0; i < num_srcs; i++) {
-      unsigned components_read =
-         nir_intrinsic_infos[instr->intrinsic].src_components[i];
-      if (components_read == 0)
-         components_read = instr->num_components;
+      unsigned components_read = nir_intrinsic_src_components(instr, i);
 
       validate_assert(state, components_read > 0);
 
@@ -499,10 +496,7 @@ validate_intrinsic_instr(nir_intrinsic_instr *instr, validate_state *state)
    }
 
    if (nir_intrinsic_infos[instr->intrinsic].has_dest) {
-      unsigned components_written =
-         nir_intrinsic_infos[instr->intrinsic].dest_components;
-      if (components_written == 0)
-         components_written = instr->num_components;
+      unsigned components_written = nir_intrinsic_dest_components(instr);
 
       validate_assert(state, components_written > 0);
 
diff --git a/src/compiler/spirv/spirv_to_nir.c b/src/compiler/spirv/spirv_to_nir.c
index 7888e1b7463..da4fac2e577 100644
--- a/src/compiler/spirv/spirv_to_nir.c
+++ b/src/compiler/spirv/spirv_to_nir.c
@@ -2430,8 +2430,7 @@ vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
       struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
       struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
 
-      unsigned dest_components =
-         nir_intrinsic_infos[intrin->intrinsic].dest_components;
+      unsigned dest_components = nir_intrinsic_dest_components(intrin);
       if (intrin->intrinsic == nir_intrinsic_image_var_size) {
          dest_components = intrin->num_components =
             glsl_get_vector_elements(type->type);
diff --git a/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c b/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c
index a3e82ab593b..f42ba7a8c6e 100644
--- a/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c
+++ b/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c
@@ -1985,11 +1985,7 @@ emit_intrinsic(struct ir3_context *ctx, nir_intrinsic_instr *intr)
 	int idx, comp;
 
 	if (info->has_dest) {
-		unsigned n;
-		if (info->dest_components)
-			n = info->dest_components;
-		else
-			n = intr->num_components;
+		unsigned n = nir_intrinsic_dest_components(intr);
 		dst = get_dst(ctx, &intr->dest, n);
 	} else {
 		dst = NULL;
diff --git a/src/intel/compiler/brw_fs_nir.cpp b/src/intel/compiler/brw_fs_nir.cpp
index 197d41062e3..6c4bcd1c113 100644
--- a/src/intel/compiler/brw_fs_nir.cpp
+++ b/src/intel/compiler/brw_fs_nir.cpp
@@ -3821,6 +3821,7 @@ fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr
       const unsigned arr_dims = type->sampler_array ? 1 : 0;
       const unsigned surf_dims = type->coordinate_components() - arr_dims;
       const unsigned format = var->data.image.format;
+      const unsigned dest_components = nir_intrinsic_dest_components(instr);
 
       /* Get the arguments of the image intrinsic. */
       const fs_reg image = get_nir_image_deref(instr->variables[0]);
@@ -3844,15 +3845,13 @@ fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr
 
       else
          tmp = emit_image_atomic(bld, image, addr, src0, src1,
-                                 surf_dims, arr_dims, info->dest_components,
+                                 surf_dims, arr_dims, dest_components,
                                  get_image_atomic_op(instr->intrinsic, type));
 
       /* Assign the result. */
-      if (nir_intrinsic_infos[instr->intrinsic].has_dest) {
-         for (unsigned c = 0; c < info->dest_components; ++c) {
-            bld.MOV(offset(retype(dest, base_type), bld, c),
-                    offset(tmp, bld, c));
-         }
+      for (unsigned c = 0; c < dest_components; ++c) {
+         bld.MOV(offset(retype(dest, base_type), bld, c),
+               offset(tmp, bld, c));
       }
       break;
    }
-- 
2.14.3



More information about the mesa-dev mailing list