nir+drivers: add helpers to get # of src/dest components
authorRob Clark <robdclark@gmail.com>
Wed, 28 Mar 2018 12:32:10 +0000 (08:32 -0400)
committerRob Clark <robdclark@gmail.com>
Tue, 3 Apr 2018 10:08:56 +0000 (06:08 -0400)
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@gmail.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
src/compiler/nir/nir.h
src/compiler/nir/nir_opt_copy_propagate.c
src/compiler/nir/nir_validate.c
src/compiler/spirv/spirv_to_nir.c
src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c
src/intel/compiler/brw_fs_nir.cpp

index cc7c401b40eff5518865ab1195f5b0aa962ca7f2..f33049d7134e4a0690a20f756825e978c5f3cbf1 100644 (file)
@@ -1146,6 +1146,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                                                            \
index c4001fa73f5335d2ff5cf4bb51a3d9b95ba1a8c8..3cd476a1b9728e87a119bc6ffc915a0443ea3fd8 100644 (file)
@@ -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;
index 5566ceb29853b2cd1f95c6169037868a42a9584a..eee737e8069cd8c9f9d90cc8a8377af94fc9ea64 100644 (file)
@@ -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);
 
index 72ab426e8054f4011dfbfa3c231a0c8ed0dce408..b7c07064d9d238766ec4f5bd58e38715321792d7 100644 (file)
@@ -2434,8 +2434,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);
index 3d3c85dd03b702be5719eee0f14cdde57b1e86c5..174141b7fecd80b440f784aa0bbdc6c532f5b835 100644 (file)
@@ -2044,11 +2044,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;
index 197d41062e38256418bf5b82af2f49a594291536..6c4bcd1c11326dfc212d471a15912b445ae79255 100644 (file)
@@ -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;
    }