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
24 #include "nir_test.h"
25
26 class nir_opt_if_test : public nir_test {
27 protected:
28 nir_opt_if_test();
29
30 nir_builder bld;
31
32 nir_def *in_def;
33 nir_variable *out_var;
34 };
35
nir_opt_if_test()36 nir_opt_if_test::nir_opt_if_test()
37 : nir_test::nir_test("nir_opt_if_test")
38 {
39 nir_variable *var = nir_variable_create(b->shader, nir_var_shader_in, glsl_int_type(), "in");
40 in_def = nir_load_var(b, var);
41
42 out_var = nir_variable_create(b->shader, nir_var_shader_out, glsl_int_type(), "out");
43 }
44
TEST_F(nir_opt_if_test,opt_if_simplification)45 TEST_F(nir_opt_if_test, opt_if_simplification)
46 {
47 /* Tests that opt_if_simplification correctly optimizes a simple case:
48 *
49 * vec1 1 ssa_2 = ieq ssa_0, ssa_1
50 * if ssa_2 {
51 * block block_2:
52 * } else {
53 * block block_3:
54 * do_work()
55 * }
56 */
57
58 nir_def *one = nir_imm_int(b, 1);
59
60 nir_def *cmp_result = nir_ieq(b, in_def, one);
61 nir_if *nif = nir_push_if(b, cmp_result);
62
63 nir_push_else(b, NULL);
64
65 // do_work
66 nir_store_var(b, out_var, one, 1);
67
68 nir_pop_if(b, NULL);
69
70 ASSERT_TRUE(nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false));
71
72 nir_validate_shader(b->shader, NULL);
73
74 ASSERT_TRUE(!exec_list_is_empty((&nir_if_first_then_block(nif)->instr_list)));
75 ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif)->instr_list)));
76 }
77
TEST_F(nir_opt_if_test,opt_if_simplification_single_source_phi_after_if)78 TEST_F(nir_opt_if_test, opt_if_simplification_single_source_phi_after_if)
79 {
80 /* Tests that opt_if_simplification correctly handles single-source
81 * phis after the if.
82 *
83 * vec1 1 ssa_2 = ieq ssa_0, ssa_1
84 * if ssa_2 {
85 * block block_2:
86 * } else {
87 * block block_3:
88 * do_work()
89 * return
90 * }
91 * block block_4:
92 * vec1 32 ssa_3 = phi block_2: ssa_0
93 */
94
95 nir_def *one = nir_imm_int(b, 1);
96
97 nir_def *cmp_result = nir_ieq(b, in_def, one);
98 nir_if *nif = nir_push_if(b, cmp_result);
99
100 nir_push_else(b, NULL);
101
102 // do_work
103 nir_store_var(b, out_var, one, 1);
104
105 nir_jump_instr *jump = nir_jump_instr_create(b->shader, nir_jump_return);
106 nir_builder_instr_insert(b, &jump->instr);
107
108 nir_pop_if(b, NULL);
109
110 nir_block *then_block = nir_if_last_then_block(nif);
111
112 nir_phi_instr *const phi = nir_phi_instr_create(b->shader);
113
114 nir_phi_instr_add_src(phi, then_block, one);
115
116 nir_def_init(&phi->instr, &phi->def,
117 one->num_components, one->bit_size);
118
119 nir_builder_instr_insert(b, &phi->instr);
120
121 ASSERT_TRUE(nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false));
122
123 nir_validate_shader(b->shader, NULL);
124
125 ASSERT_TRUE(nir_block_ends_in_jump(nir_if_last_then_block(nif)));
126 ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif)->instr_list)));
127 }
128
TEST_F(nir_opt_if_test,opt_if_alu_of_phi_progress)129 TEST_F(nir_opt_if_test, opt_if_alu_of_phi_progress)
130 {
131 nir_def *two = nir_imm_int(b, 2);
132 nir_def *x = nir_imm_int(b, 0);
133
134 nir_phi_instr *phi = nir_phi_instr_create(b->shader);
135
136 nir_loop *loop = nir_push_loop(b);
137 {
138 nir_def_init(&phi->instr, &phi->def,
139 x->num_components, x->bit_size);
140
141 nir_phi_instr_add_src(phi, x->parent_instr->block, x);
142
143 nir_def *y = nir_iadd(b, &phi->def, two);
144 nir_store_var(b, out_var,
145 nir_imul(b, &phi->def, two), 1);
146
147 nir_phi_instr_add_src(phi, nir_cursor_current_block(b->cursor), y);
148 }
149 nir_pop_loop(b, loop);
150
151 b->cursor = nir_before_block(nir_loop_first_block(loop));
152 nir_builder_instr_insert(b, &phi->instr);
153
154 nir_validate_shader(b->shader, "input");
155
156 bool progress;
157
158 int progress_count = 0;
159 for (int i = 0; i < 10; i++) {
160 progress = nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false);
161 if (progress)
162 progress_count++;
163 else
164 break;
165 nir_opt_constant_folding(b->shader);
166 }
167
168 EXPECT_LE(progress_count, 2);
169 ASSERT_FALSE(progress);
170 }
171