<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Sat, Sep 15, 2018 at 12:46 AM Caio Marcelo de Oliveira Filho <<a href="mailto:caio.oliveira@intel.com">caio.oliveira@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Also tests for removal of redundant loads, that we currently handle as<br>
part of the copy propagation.<br>
---<br>
 src/compiler/nir/tests/vars_tests.cpp | 300 ++++++++++++++++++++++++++<br>
 1 file changed, 300 insertions(+)<br>
<br>
diff --git a/src/compiler/nir/tests/vars_tests.cpp b/src/compiler/nir/tests/vars_tests.cpp<br>
index cdd2a17fe92..b1fa04b5cb9 100644<br>
--- a/src/compiler/nir/tests/vars_tests.cpp<br>
+++ b/src/compiler/nir/tests/vars_tests.cpp<br>
@@ -140,11 +140,131 @@ nir_imm_ivec2(nir_builder *build, int x, int y)<br>
 }<br>
<br>
 /* Allow grouping the tests while still sharing the helpers. */<br>
+class nir_redundant_load_vars_test : public nir_vars_test {};<br>
 class nir_copy_prop_vars_test : public nir_vars_test {};<br>
 class nir_dead_write_vars_test : public nir_vars_test {};<br>
<br>
 } // namespace<br>
<br>
+TEST_F(nir_redundant_load_vars_test, duplicated_load)<br>
+{<br>
+   /* Load a variable twice in the same block.  One should be removed. */<br>
+<br>
+   nir_variable *in = create_int(nir_var_shader_in, "in");<br>
+   nir_variable **out = create_many_int(nir_var_shader_out, "out", 2);<br>
+<br>
+   nir_store_var(b, out[0], nir_load_var(b, in), 1);<br>
+   nir_store_var(b, out[1], nir_load_var(b, in), 1);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 2);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   EXPECT_TRUE(progress);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 1);<br>
+}<br>
+<br>
+TEST_F(nir_redundant_load_vars_test, DISABLED_duplicated_load_in_two_blocks)<br>
+{<br>
+   /* Load a variable twice in different blocks.  One should be removed. */<br>
+<br>
+   nir_variable *in = create_int(nir_var_shader_in, "in");<br>
+   nir_variable **out = create_many_int(nir_var_shader_out, "out", 2);<br>
+<br>
+   nir_store_var(b, out[0], nir_load_var(b, in), 1);<br>
+<br>
+   /* Forces the stores to be in different blocks. */<br>
+   nir_pop_if(b, nir_push_if(b, nir_imm_int(b, 0)));<br>
+<br>
+   nir_store_var(b, out[1], nir_load_var(b, in), 1);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 2);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   EXPECT_TRUE(progress);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 1);<br>
+}<br>
+<br>
+TEST_F(nir_redundant_load_vars_test, DISABLED_invalidate_inside_if_block)<br>
+{<br>
+   /* Load variables, then write to some of then in different branches of the<br>
+    * if statement.  They should be invalidated accordingly.<br>
+    */<br>
+<br>
+   nir_variable **g = create_many_int(nir_var_global, "g", 3);<br>
+   nir_variable **out = create_many_int(nir_var_shader_out, "out", 3);<br>
+<br>
+   nir_load_var(b, g[0]);<br>
+   nir_load_var(b, g[1]);<br>
+   nir_load_var(b, g[2]);<br>
+<br>
+   nir_if *if_stmt = nir_push_if(b, nir_imm_int(b, 0));<br>
+   nir_store_var(b, g[0], nir_imm_int(b, 10), 1);<br>
+<br>
+   nir_push_else(b, if_stmt);<br>
+   nir_store_var(b, g[1], nir_imm_int(b, 20), 1);<br>
+<br>
+   nir_pop_if(b, if_stmt);<br>
+<br>
+   nir_store_var(b, out[0], nir_load_var(b, g[0]), 1);<br>
+   nir_store_var(b, out[1], nir_load_var(b, g[1]), 1);<br>
+   nir_store_var(b, out[2], nir_load_var(b, g[2]), 1);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   EXPECT_TRUE(progress);<br>
+<br>
+   /* There are 3 initial loads, plus 2 loads for the values invalidated<br>
+    * inside the if statement.<br>
+    */<br>
+   ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 5);<br>
+<br>
+   /* We only load g[2] once. */<br>
+   unsigned g2_load_count = 0;<br>
+   nir_intrinsic_instr *load = NULL;<br>
+   for (int i = 0; i < 5; i++) {<br>
+      load = find_next_intrinsic(nir_intrinsic_load_deref, load);<br>
+      if (nir_intrinsic_get_var(load, 0) == g[2])<br>
+         g2_load_count++;<br>
+   }<br>
+   EXPECT_EQ(g2_load_count, 1);<br>
+}<br>
+<br>
+TEST_F(nir_redundant_load_vars_test, invalidate_live_load_in_the_end_of_loop)<br>
+{<br>
+   /* Invalidating a load in the end of loop body will apply to the whole loop<br>
+    * body.<br>
+    */<br>
+<br>
+   nir_variable *v = create_int(nir_var_shader_storage, "v");<br>
+<br>
+   nir_load_var(b, v);<br>
+<br>
+   nir_loop *loop = nir_push_loop(b);<br>
+<br>
+   nir_if *if_stmt = nir_push_if(b, nir_imm_int(b, 0));<br>
+   nir_jump(b, nir_jump_break);<br>
+   nir_pop_if(b, if_stmt);<br>
+<br>
+   nir_load_var(b, v);<br>
+   nir_store_var(b, v, nir_imm_int(b, 10), 1);<br>
+<br>
+   nir_pop_loop(b, loop);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   ASSERT_FALSE(progress);<br>
+}<br>
+<br>
 TEST_F(nir_copy_prop_vars_test, simple_copies)<br>
 {<br>
    nir_variable *in   = create_int(nir_var_shader_in,  "in");<br>
@@ -199,6 +319,186 @@ TEST_F(nir_copy_prop_vars_test, simple_store_load)<br>
    }<br>
 }<br>
<br>
+TEST_F(nir_copy_prop_vars_test, store_store_load)<br>
+{<br>
+   nir_variable **v = create_many_ivec2(nir_var_local, "v", 2);<br>
+   unsigned mask = 1 | 2;<br>
+<br>
+   nir_ssa_def *first_value = nir_imm_ivec2(b, 10, 20);<br>
+   nir_store_var(b, v[0], first_value, mask);<br>
+<br>
+   nir_ssa_def *second_value = nir_imm_ivec2(b, 30, 40);<br>
+   nir_store_var(b, v[0], second_value, mask);<br>
+<br>
+   nir_ssa_def *read_value = nir_load_var(b, v[0]);<br>
+   nir_store_var(b, v[1], read_value, mask);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   EXPECT_TRUE(progress);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   /* Store to v[1] should use second_value directly. */<br>
+   nir_intrinsic_instr *store_to_v1 = NULL;<br>
+   while ((store_to_v1 = find_next_intrinsic(nir_intrinsic_store_deref, store_to_v1)) != NULL) {<br>
+      if (nir_intrinsic_get_var(store_to_v1, 0) == v[1]) {<br>
+         ASSERT_TRUE(store_to_v1->src[1].is_ssa);<br>
+         EXPECT_EQ(store_to_v1->src[1].ssa, second_value);<br>
+         break;<br>
+      }<br>
+   }<br>
+   EXPECT_TRUE(store_to_v1);<br>
+}<br>
+<br>
+TEST_F(nir_copy_prop_vars_test, store_store_load_different_components)<br>
+{<br>
+   nir_variable **v = create_many_ivec2(nir_var_local, "v", 2);<br>
+<br>
+   nir_ssa_def *first_value = nir_imm_ivec2(b, 10, 20);<br>
+   nir_store_var(b, v[0], first_value, 1 << 1);<br>
+<br>
+   nir_ssa_def *second_value = nir_imm_ivec2(b, 30, 40);<br>
+   nir_store_var(b, v[0], second_value, 1 << 0);<br>
+<br>
+   nir_ssa_def *read_value = nir_load_var(b, v[0]);<br>
+   nir_store_var(b, v[1], read_value, 1 << 1);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   EXPECT_TRUE(progress);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   nir_opt_constant_folding(b->shader);<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   /* Store to v[1] should use first_value directly.  The write of<br>
+    * second_value did not overwrite the component it uses.<br>
+    */<br>
+   nir_intrinsic_instr *store_to_v1 = NULL;<br>
+   while ((store_to_v1 = find_next_intrinsic(nir_intrinsic_store_deref, store_to_v1)) != NULL) {<br>
+      if (nir_intrinsic_get_var(store_to_v1, 0) == v[1]) {<br>
+         ASSERT_TRUE(store_to_v1->src[1].is_ssa);<br>
+<br>
+         nir_const_value *const_index = nir_src_as_const_value(store_to_v1->src[1]);<br>
+         ASSERT_TRUE(const_index);<br>
+         ASSERT_EQ(const_index->u32[1], 20);<br>
+         break;<br>
+      }<br>
+   }<br>
+   EXPECT_TRUE(store_to_v1);<br>
+}<br>
+<br>
+TEST_F(nir_copy_prop_vars_test, store_store_load_different_components_in_many_blocks)<br>
+{<br>
+   nir_variable **v = create_many_ivec2(nir_var_local, "v", 2);<br>
+<br>
+   nir_ssa_def *first_value = nir_imm_ivec2(b, 10, 20);<br>
+   nir_store_var(b, v[0], first_value, 1 << 1);<br>
+<br>
+   /* Adding an if statement will cause blocks to be created. */<br>
+   nir_pop_if(b, nir_push_if(b, nir_imm_int(b, 0)));<br>
+<br>
+   nir_ssa_def *second_value = nir_imm_ivec2(b, 30, 40);<br>
+   nir_store_var(b, v[0], second_value, 1 << 0);<br>
+<br>
+   /* Adding an if statement will cause blocks to be created. */<br>
+   nir_pop_if(b, nir_push_if(b, nir_imm_int(b, 0)));<br>
+<br>
+   nir_ssa_def *read_value = nir_load_var(b, v[0]);<br>
+   nir_store_var(b, v[1], read_value, 1 << 1);<br></blockquote><div><br></div><div>Do we pass this test today?  I suspect we don't.  Maybe it should be disabled?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   nir_print_shader(b->shader, stdout);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   EXPECT_TRUE(progress);<br>
+<br>
+   nir_print_shader(b->shader, stdout);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   nir_opt_constant_folding(b->shader);<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   /* Store to v[1] should use first_value directly.  The write of<br>
+    * second_value did not overwrite the component it uses.<br>
+    */<br>
+   nir_intrinsic_instr *store_to_v1 = NULL;<br>
+   while ((store_to_v1 = find_next_intrinsic(nir_intrinsic_store_deref, store_to_v1)) != NULL) {<br>
+      if (nir_intrinsic_get_var(store_to_v1, 0) == v[1]) {<br>
+         ASSERT_TRUE(store_to_v1->src[1].is_ssa);<br>
+<br>
+         nir_const_value *const_index = nir_src_as_const_value(store_to_v1->src[1]);<br>
+         ASSERT_TRUE(const_index);<br>
+         ASSERT_EQ(const_index->u32[1], 20);<br>
+         break;<br>
+      }<br>
+   }<br>
+   EXPECT_TRUE(store_to_v1);<br>
+}<br>
+<br>
+TEST_F(nir_copy_prop_vars_test, memory_barrier_in_two_blocks)<br>
+{<br>
+   nir_variable **v = create_many_int(nir_var_shader_storage, "v", 4);<br>
+<br>
+   nir_store_var(b, v[0], nir_imm_int(b, 1), 1);<br>
+   nir_store_var(b, v[1], nir_imm_int(b, 2), 1);<br>
+<br>
+   /* Split into many blocks. */<br>
+   nir_pop_if(b, nir_push_if(b, nir_imm_int(b, 0)));<br>
+<br>
+   nir_store_var(b, v[2], nir_load_var(b, v[0]), 1);<br>
+<br>
+   nir_builder_instr_insert(b, &nir_intrinsic_instr_create(b->shader, nir_intrinsic_memory_barrier)->instr);<br>
+<br>
+   nir_store_var(b, v[3], nir_load_var(b, v[1]), 1);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   ASSERT_TRUE(progress);<br></blockquote><div><br></div><div>Again, disabled?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+   /* Only the second load will remain after the optimization. */<br>
+   ASSERT_EQ(1, count_intrinsics(nir_intrinsic_load_deref));<br>
+   nir_intrinsic_instr *load = NULL;<br>
+   load = find_next_intrinsic(nir_intrinsic_load_deref, load);<br>
+   ASSERT_EQ(nir_intrinsic_get_var(load, 0), v[1]);<br>
+}<br>
+<br>
+TEST_F(nir_copy_prop_vars_test, simple_store_load_in_two_blocks)<br></blockquote><div><br></div><div>DISABLED</div><div><br></div><div>Thanks for writing all the tests!  They aren't complete tests for correctness but should at least tell us if obvious stuff breaks or if someone does something to the pass to make it suddenly stop copy-propagating.  Assuming all tests are disabled that need to be (I think I found 3),</div><div><br></div><div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+{<br>
+   nir_variable **v = create_many_ivec2(nir_var_local, "v", 2);<br>
+   unsigned mask = 1 | 2;<br>
+<br>
+   nir_ssa_def *stored_value = nir_imm_ivec2(b, 10, 20);<br>
+   nir_store_var(b, v[0], stored_value, mask);<br>
+<br>
+   /* Adding an if statement will cause blocks to be created. */<br>
+   nir_pop_if(b, nir_push_if(b, nir_imm_int(b, 0)));<br>
+<br>
+   nir_ssa_def *read_value = nir_load_var(b, v[0]);<br>
+   nir_store_var(b, v[1], read_value, mask);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   bool progress = nir_opt_copy_prop_vars(b->shader);<br>
+   EXPECT_TRUE(progress);<br>
+<br>
+   nir_validate_shader(b->shader);<br>
+<br>
+   ASSERT_EQ(count_intrinsics(nir_intrinsic_store_deref), 2);<br>
+<br>
+   nir_intrinsic_instr *store = NULL;<br>
+   for (int i = 0; i < 2; i++) {<br>
+      store = find_next_intrinsic(nir_intrinsic_store_deref, store);<br>
+      ASSERT_TRUE(store->src[1].is_ssa);<br>
+      EXPECT_EQ(store->src[1].ssa, stored_value);<br>
+   }<br>
+}<br>
+<br>
 TEST_F(nir_dead_write_vars_test, no_dead_writes_in_block)<br>
 {<br>
    nir_variable **v = create_many_int(nir_var_shader_storage, "v", 2);<br>
-- <br>
2.19.0<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div></div>