nir: Add a nir_src_as_intrinsic() helper
authorJason Ekstrand <jason@jlekstrand.net>
Wed, 17 Apr 2019 22:18:19 +0000 (17:18 -0500)
committerJason Ekstrand <jason@jlekstrand.net>
Thu, 18 Apr 2019 17:12:44 +0000 (17:12 +0000)
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Caio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
src/compiler/nir/nir.h
src/compiler/nir/nir_opt_find_array_copies.c
src/compiler/nir/nir_split_vars.c
src/compiler/nir/tests/vars_tests.cpp
src/intel/compiler/brw_fs_nir.cpp

index 91ab40a9482627860de099f1c9f639aa59564568..2a381d28d7cd7f08a1dfee57b49ddd8770ea3bf8 100644 (file)
@@ -2748,12 +2748,6 @@ 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);
 
-static inline struct nir_instr *
-nir_src_instr(const struct nir_src *src)
-{
-   return src->is_ssa ? src->ssa->parent_instr : NULL;
-}
-
 #define NIR_SRC_AS_(name, c_type, type_enum, cast_macro)                \
 static inline c_type *                                                  \
 nir_src_as_ ## name (nir_src src)                                       \
@@ -2763,6 +2757,8 @@ nir_src_as_ ## name (nir_src src)                                       \
 }
 
 NIR_SRC_AS_(alu_instr, nir_alu_instr, nir_instr_type_alu, nir_instr_as_alu)
+NIR_SRC_AS_(intrinsic, nir_intrinsic_instr,
+            nir_instr_type_intrinsic, nir_instr_as_intrinsic)
 
 bool nir_src_is_dynamically_uniform(nir_src src);
 bool nir_srcs_equal(nir_src src1, nir_src src2);
index e50264c590032323c33f0091eda579f856f75c63..1573cce83748652a42691e136cd0883c7957b14a 100644 (file)
@@ -37,14 +37,8 @@ index_ssa_def_cb(nir_ssa_def *def, void *state)
 static nir_deref_instr *
 get_deref_for_load_src(nir_src src, unsigned first_valid_load)
 {
-   if (!src.is_ssa)
-      return NULL;
-
-   if (src.ssa->parent_instr->type != nir_instr_type_intrinsic)
-      return NULL;
-
-   nir_intrinsic_instr *load = nir_instr_as_intrinsic(src.ssa->parent_instr);
-   if (load->intrinsic != nir_intrinsic_load_deref)
+   nir_intrinsic_instr *load = nir_src_as_intrinsic(src);
+   if (load == NULL || load->intrinsic != nir_intrinsic_load_deref)
       return NULL;
 
    if (load->dest.ssa.index < first_valid_load)
index c6fe1b59a65f226dca0dea3945f4d49621b2abf7..5e799d2dc574b764f2010fc8d0a0462c857f87dd 100644 (file)
@@ -1030,14 +1030,8 @@ mark_deref_used(nir_deref_instr *deref,
 static bool
 src_is_load_deref(nir_src src, nir_src deref_src)
 {
-   assert(src.is_ssa);
-   assert(deref_src.is_ssa);
-
-   if (src.ssa->parent_instr->type != nir_instr_type_intrinsic)
-      return false;
-
-   nir_intrinsic_instr *load = nir_instr_as_intrinsic(src.ssa->parent_instr);
-   if (load->intrinsic != nir_intrinsic_load_deref)
+   nir_intrinsic_instr *load = nir_src_as_intrinsic(src);
+   if (load == NULL || load->intrinsic != nir_intrinsic_load_deref)
       return false;
 
    assert(load->src[0].is_ssa);
index d27a9c0fb1d1d7ef254c38ce760e248fbee43c56..06e5dbc8ba7a1198e2d9b233890376fa40ff211c 100644 (file)
@@ -993,8 +993,7 @@ TEST_F(nir_combine_stores_test, non_overlapping_stores)
    nir_alu_instr *vec = nir_src_as_alu_instr(combined->src[1]);
    ASSERT_TRUE(vec);
    for (int i = 0; i < 4; i++) {
-      nir_intrinsic_instr *load =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[i].src));
+      nir_intrinsic_instr *load = nir_src_as_intrinsic(vec->src[i].src);
       ASSERT_EQ(load->intrinsic, nir_intrinsic_load_deref);
       ASSERT_EQ(nir_intrinsic_get_var(load, 0), v[i])
          << "Source value for component " << i << " of store is wrong";
@@ -1034,22 +1033,18 @@ TEST_F(nir_combine_stores_test, overlapping_stores)
    ASSERT_TRUE(vec);
 
    /* Component x comes from v[0]. */
-   nir_intrinsic_instr *load_for_x =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[0].src));
+   nir_intrinsic_instr *load_for_x = nir_src_as_intrinsic(vec->src[0].src);
    ASSERT_EQ(nir_intrinsic_get_var(load_for_x, 0), v[0]);
    ASSERT_EQ(vec->src[0].swizzle[0], 0);
 
    /* Component y comes from v[1]. */
-   nir_intrinsic_instr *load_for_y =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[1].src));
+   nir_intrinsic_instr *load_for_y = nir_src_as_intrinsic(vec->src[1].src);
    ASSERT_EQ(nir_intrinsic_get_var(load_for_y, 0), v[1]);
    ASSERT_EQ(vec->src[1].swizzle[0], 1);
 
    /* Components z and w come from v[2]. */
-   nir_intrinsic_instr *load_for_z =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[2].src));
-   nir_intrinsic_instr *load_for_w =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[3].src));
+   nir_intrinsic_instr *load_for_z = nir_src_as_intrinsic(vec->src[2].src);
+   nir_intrinsic_instr *load_for_w = nir_src_as_intrinsic(vec->src[3].src);
    ASSERT_EQ(load_for_z, load_for_w);
    ASSERT_EQ(nir_intrinsic_get_var(load_for_z, 0), v[2]);
    ASSERT_EQ(vec->src[2].swizzle[0], 2);
@@ -1104,26 +1099,22 @@ TEST_F(nir_combine_stores_test, direct_array_derefs)
    ASSERT_TRUE(vec);
 
    /* Component x comes from v[0]. */
-   nir_intrinsic_instr *load_for_x =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[0].src));
+   nir_intrinsic_instr *load_for_x = nir_src_as_intrinsic(vec->src[0].src);
    ASSERT_EQ(nir_intrinsic_get_var(load_for_x, 0), v[0]);
    ASSERT_EQ(vec->src[0].swizzle[0], 0);
 
    /* Component y comes from v[1]. */
-   nir_intrinsic_instr *load_for_y =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[1].src));
+   nir_intrinsic_instr *load_for_y = nir_src_as_intrinsic(vec->src[1].src);
    ASSERT_EQ(nir_intrinsic_get_var(load_for_y, 0), v[1]);
    ASSERT_EQ(vec->src[1].swizzle[0], 1);
 
    /* Components z comes from s[0]. */
-   nir_intrinsic_instr *load_for_z =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[2].src));
+   nir_intrinsic_instr *load_for_z = nir_src_as_intrinsic(vec->src[2].src);
    ASSERT_EQ(nir_intrinsic_get_var(load_for_z, 0), s[0]);
    ASSERT_EQ(vec->src[2].swizzle[0], 0);
 
    /* Component w comes from s[1]. */
-   nir_intrinsic_instr *load_for_w =
-         nir_instr_as_intrinsic(nir_src_instr(&vec->src[3].src));
+   nir_intrinsic_instr *load_for_w = nir_src_as_intrinsic(vec->src[3].src);
    ASSERT_EQ(nir_intrinsic_get_var(load_for_w, 0), s[1]);
    ASSERT_EQ(vec->src[3].swizzle[0], 0);
 }
index ac566be7476d197fdccfe795a2e8e214816710c4..397d1f5980a15795de696203d2100cab5c27d8b5 100644 (file)
@@ -558,14 +558,8 @@ bool
 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
                                          const fs_reg &result)
 {
-   if (!instr->src[0].src.is_ssa ||
-       instr->src[0].src.ssa->parent_instr->type != nir_instr_type_intrinsic)
-      return false;
-
-   nir_intrinsic_instr *src0 =
-      nir_instr_as_intrinsic(instr->src[0].src.ssa->parent_instr);
-
-   if (src0->intrinsic != nir_intrinsic_load_front_face)
+   nir_intrinsic_instr *src0 = nir_src_as_intrinsic(instr->src[0].src);
+   if (src0 == NULL || src0->intrinsic != nir_intrinsic_load_front_face)
       return false;
 
    if (!nir_src_is_const(instr->src[1].src) ||
@@ -2527,9 +2521,8 @@ fs_visitor::nir_emit_tcs_intrinsic(const fs_builder &bld,
                  retype(brw_vec1_grf(1 + (vertex >> 3), vertex & 7),
                         BRW_REGISTER_TYPE_UD));
       } else if (tcs_prog_data->instances == 1 &&
-                 vertex_src.is_ssa &&
-                 vertex_src.ssa->parent_instr->type == nir_instr_type_intrinsic &&
-                 nir_instr_as_intrinsic(vertex_src.ssa->parent_instr)->intrinsic == nir_intrinsic_load_invocation_id) {
+                 nir_src_as_intrinsic(vertex_src) != NULL &&
+                 nir_src_as_intrinsic(vertex_src)->intrinsic == nir_intrinsic_load_invocation_id) {
          /* For the common case of only 1 instance, an array index of
           * gl_InvocationID means reading g1.  Skip all the indirect work.
           */