1 /*
2 * Copyright © 2020 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <gtest/gtest.h>
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 class nir_opt_dce_test : public ::testing::Test {
28 protected:
29 nir_opt_dce_test();
30 ~nir_opt_dce_test();
31
32 nir_builder bld;
33 };
34
nir_opt_dce_test()35 nir_opt_dce_test::nir_opt_dce_test()
36 {
37 glsl_type_singleton_init_or_ref();
38
39 static const nir_shader_compiler_options options = { };
40 bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "dce test");
41 }
42
~nir_opt_dce_test()43 nir_opt_dce_test::~nir_opt_dce_test()
44 {
45 ralloc_free(bld.shader);
46 glsl_type_singleton_decref();
47 }
48
create_one_source_phi(nir_shader *shader, nir_block *pred, nir_ssa_def *def)49 nir_phi_instr *create_one_source_phi(nir_shader *shader, nir_block *pred,
50 nir_ssa_def *def)
51 {
52 nir_phi_instr *phi = nir_phi_instr_create(shader);
53 nir_phi_instr_add_src(phi, pred, nir_src_for_ssa(def));
54 nir_ssa_dest_init(&phi->instr, &phi->dest,
55 def->num_components, def->bit_size, NULL);
56
57 return phi;
58 }
59
TEST_F(nir_opt_dce_test, return_before_loop)60 TEST_F(nir_opt_dce_test, return_before_loop)
61 {
62 /* Test that nir_opt_dce() works correctly with loops immediately following a jump.
63 * nir_opt_dce() has a fast path for loops without continues, and this test ensures that it
64 * looks at the actual predecessors of the header instead of just counting.
65 *
66 * block block_0:
67 * // preds:
68 * return
69 * // succs: block_3
70 * loop {
71 * block block_1:
72 * // preds: block_1
73 * vec1 32 ssa_1 = phi block_1: ssa_0
74 * vec1 32 ssa_0 = load_const (0x00000001)
75 * vec1 32 ssa_2 = deref_var &out (shader_out int)
76 * intrinsic store_deref (ssa_2, ssa_1) (1, 0)
77 * // succs: block_1
78 * }
79 * block block_2:
80 * // preds:
81 * // succs: block_3
82 * block block_3:
83 *
84 * If the fast path is taken here, ssa_0 will be incorrectly DCE'd.
85 */
86
87 nir_variable *var = nir_variable_create(bld.shader, nir_var_shader_out, glsl_int_type(), "out");
88
89 nir_jump(&bld, nir_jump_return);
90
91 nir_loop *loop = nir_push_loop(&bld);
92
93 nir_ssa_def *one = nir_imm_int(&bld, 1);
94
95 nir_phi_instr *phi = create_one_source_phi(bld.shader, one->parent_instr->block, one);
96 nir_instr_insert_before_block(one->parent_instr->block, &phi->instr);
97
98 nir_store_var(&bld, var, &phi->dest.ssa, 0x1);
99
100 nir_pop_loop(&bld, loop);
101
102 ASSERT_FALSE(nir_opt_dce(bld.shader));
103
104 nir_validate_shader(bld.shader, NULL);
105 }
106