nir/tests: add nir_opt_dead_cf_test.jump_before_constant_if
authorRhys Perry <pendingchaos02@gmail.com>
Mon, 24 Jul 2023 11:02:48 +0000 (12:02 +0100)
committerRhys Perry <pendingchaos02@gmail.com>
Mon, 24 Jul 2023 13:06:16 +0000 (14:06 +0100)
Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Konstantin Seurer <konstantin.seurer@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24235>

src/compiler/nir/tests/dce_tests.cpp

index 42afcc0..373bfc1 100644 (file)
@@ -31,6 +31,14 @@ protected:
    }
 };
 
+class nir_opt_dead_cf_test : public nir_test {
+protected:
+   nir_opt_dead_cf_test()
+      : nir_test::nir_test("nir_opt_dead_cf_test")
+   {
+   }
+};
+
 nir_phi_instr *create_one_source_phi(nir_shader *shader, nir_block *pred,
                                      nir_ssa_def *def)
 {
@@ -88,3 +96,44 @@ TEST_F(nir_opt_dce_test, return_before_loop)
 
    nir_validate_shader(b->shader, NULL);
 }
+
+TEST_F(nir_opt_dead_cf_test, jump_before_constant_if)
+{
+   /*
+    * Test that nir_opt_dead_cf removes everything after the jump, instead of trying and failing to
+    * stitch b0 and b3.
+    *
+    * block b0:  // preds:
+    * 1     %0 = load_const (false)
+    *            return
+    *            // succs: b4
+    * if %0 (false) {
+    *     block b1:  // preds:
+    *     32    %1 = load_const (0x00000001)
+    *     32    %2 = deref_var &out (shader_out int)
+    *                @store_deref (%2, %1 (0x1)) (wrmask=x, access=0)
+    *                // succs: b3
+    * } else {
+    *     block b2:  // preds: , succs: b3
+    * }
+    * block b3:  // preds: b1 b2
+    * 32    %3 = load_const (0x00000000)
+    * 32    %4 = deref_var &out (shader_out int)
+    *            @store_deref (%4, %3 (0x0)) (wrmask=x, access=0)
+    *            // succs: b4
+    * block b4:
+    */
+   nir_variable *var = nir_variable_create(b->shader, nir_var_shader_out, glsl_int_type(), "out");
+
+   nir_ssa_def *cond = nir_imm_false(b);
+   nir_jump(b, nir_jump_return);
+   nir_push_if(b, cond);
+   nir_store_var(b, var, nir_imm_int(b, 1), 0x1);
+   nir_pop_if(b, NULL);
+
+   nir_store_var(b, var, nir_imm_int(b, 0), 0x1);
+
+   ASSERT_TRUE(nir_opt_dead_cf(b->shader));
+
+   nir_validate_shader(b->shader, NULL);
+}