xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/tests/opt_if_tests.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 
172 static const struct nir_opt_if_merge_test_config {
173    template <class ParamType>
174    std::string
operator ()nir_opt_if_merge_test_config175    operator()(const ::testing::TestParamInfo<ParamType> &info) const
176    {
177       return info.param.name;
178    }
179 
180    const char *name;
181 
182    /* Test parameter: Location of a return statement within the control flow */
183    enum return_location {
184       NO_RETURN,
185       RETURN_IN_1ST_THEN,
186       RETURN_IN_1ST_ELSE,
187       RETURN_IN_2ND_THEN,
188       RETURN_IN_2ND_ELSE,
189    } return_location;
190 } nir_opt_if_merge_test_configs [] = {
191    { "no_return",          nir_opt_if_merge_test_config::NO_RETURN, },
192    { "return_in_1st_then", nir_opt_if_merge_test_config::RETURN_IN_1ST_THEN, },
193    { "return_in_1st_else", nir_opt_if_merge_test_config::RETURN_IN_1ST_ELSE, },
194    { "return_in_2nd_then", nir_opt_if_merge_test_config::RETURN_IN_2ND_THEN, },
195    { "return_in_2nd_else", nir_opt_if_merge_test_config::RETURN_IN_2ND_ELSE, },
196 };
197 
198 class nir_opt_if_merge_test
199    : public nir_opt_if_test
200    , public ::testing::WithParamInterface<struct nir_opt_if_merge_test_config>
201 {
202 protected:
203    virtual void SetUp();
204 
205    enum nir_opt_if_merge_test_config::return_location return_location;
206 };
207 
208 void
SetUp()209 nir_opt_if_merge_test::SetUp()
210 {
211    struct nir_opt_if_merge_test_config config = GetParam();
212    return_location = config.return_location;
213 }
214 
215 INSTANTIATE_TEST_SUITE_P(
216    nir_opt_if, nir_opt_if_merge_test,
217    ::testing::ValuesIn(nir_opt_if_merge_test_configs),
218    nir_opt_if_merge_test_config()
219 );
220 
TEST_P(nir_opt_if_merge_test,opt_if_merge)221 TEST_P(nir_opt_if_merge_test, opt_if_merge)
222 {
223    /* Tests that opt_if_merge correctly merges if statements with the same
224     * condition, parameterized on the location of a return statement added
225     * within the control-flow.
226     *
227     * block b0:   // preds:
228     * 32     %0 = deref_var &in (shader_in int)
229     * 32     %1 = @load_deref (%0) (access=none)
230     * 32     %2 = load_const (0x00000001)
231     * 32     %3 = load_const (0x00000002)
232     * 32     %4 = load_const (0x00000006)
233     * 1      %5 = ige %1, %2 (0x1)
234     *             // succs: b1 b2
235     * if %5 {
236     *     block b1:   // preds: b0
237     *     32     %6 = iadd %1, %2 (0x1)
238     *                 // succs: b3
239     * } else {
240     *     block b2:   // preds: b0
241     *     32     %7 = iadd %1, %3 (0x2)
242     *                 // succs: b3
243     * }
244     * block b3:  // preds: b1 b2, succs: b4 b5
245     * if %5 {
246     *     block b4:   // preds: b3
247     *     32     %8 = imul %6, %3 (0x2)
248     *                 // succs: b6
249     * } else {
250     *     block b5:   // preds: b3
251     *     32     %9 = imul %7, %4 (0x6)
252     *                 // succs: b6
253     * }
254     * block b6:   // preds: b4 b5
255     * 32    %10 = phi b4: %8, b5: %9
256     * 32    %11 = deref_var &out (shader_out int)
257     *             @store_deref (%11, %10) (wrmask=x, access=none)
258     *             // succs: b7
259     * block b7:
260     */
261 
262    int instr_in_1st_then = 1, instr_in_1st_else = 1;
263    int instr_in_2nd_then = 1, instr_in_2nd_else = 1;
264 
265    nir_def *one = nir_imm_int(b, 1);
266    nir_def *two = nir_imm_int(b, 2);
267    nir_def *six = nir_imm_int(b, 6);
268 
269    nir_def *cmp_result = nir_ige(b, in_def, one);
270 
271    nir_if *nif = nir_push_if(b, cmp_result);
272 
273    nir_def *x1 = nir_iadd(b, in_def, one);
274    if (return_location == nir_opt_if_merge_test_config::RETURN_IN_1ST_THEN) {
275       nir_jump(b, nir_jump_return);
276       instr_in_1st_then++;
277    }
278 
279    nir_push_else(b, NULL);
280 
281    nir_def *x2 = nir_iadd(b, in_def, two);
282    if (return_location == nir_opt_if_merge_test_config::RETURN_IN_1ST_ELSE) {
283       nir_jump(b, nir_jump_return);
284       instr_in_1st_else++;
285    }
286 
287    nir_pop_if(b, NULL);
288 
289    nir_phi_instr *phi = nir_phi_instr_create(b->shader);
290    nir_def_init(&phi->instr, &phi->def,
291                 one->num_components, one->bit_size);
292 
293    nir_if *next_if = nir_push_if(b, cmp_result);
294 
295    nir_def *y1 = nir_imul(b, x1, two);
296    if (return_location == nir_opt_if_merge_test_config::RETURN_IN_2ND_THEN) {
297       nir_jump(b, nir_jump_return);
298       instr_in_2nd_then++;
299    } else {
300       nir_phi_instr_add_src(phi, nir_cursor_current_block(b->cursor), y1);
301    }
302 
303    nir_push_else(b, NULL);
304 
305    nir_def *y2 = nir_imul(b, x2, six);
306    if (return_location == nir_opt_if_merge_test_config::RETURN_IN_2ND_ELSE) {
307       nir_jump(b, nir_jump_return);
308       instr_in_2nd_else++;
309    } else {
310       nir_phi_instr_add_src(phi, nir_cursor_current_block(b->cursor), y2);
311    }
312 
313    nir_pop_if(b, NULL);
314 
315    nir_builder_instr_insert(b, &phi->instr);
316 
317    // do_work
318    nir_store_var(b, out_var, &phi->def, 1);
319 
320    if (return_location == nir_opt_if_merge_test_config::RETURN_IN_2ND_THEN ||
321        return_location == nir_opt_if_merge_test_config::RETURN_IN_2ND_ELSE) {
322       ASSERT_FALSE(nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false));
323    } else if (return_location == nir_opt_if_merge_test_config::NO_RETURN) {
324       ASSERT_TRUE(nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false));
325       instr_in_1st_then = 2;
326       instr_in_1st_else = 2;
327       instr_in_2nd_then = 0;
328       instr_in_2nd_else = 0;
329    } else {
330       ASSERT_TRUE(nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false));
331    }
332 
333    nir_validate_shader(b->shader, NULL);
334 
335    ASSERT_EQ(exec_list_length((&nir_if_first_then_block(nif)->instr_list)), instr_in_1st_then);
336    ASSERT_EQ(exec_list_length((&nir_if_first_else_block(nif)->instr_list)), instr_in_1st_else);
337    ASSERT_EQ(exec_list_length((&nir_if_first_then_block(next_if)->instr_list)), instr_in_2nd_then);
338    ASSERT_EQ(exec_list_length((&nir_if_first_else_block(next_if)->instr_list)), instr_in_2nd_else);
339 }
340