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_lower_returns_test : public ::testing::Test {
28 protected:
29 nir_opt_lower_returns_test();
30 ~nir_opt_lower_returns_test();
31
32 nir_builder bld;
33
34 nir_ssa_def *in_def;
35 };
36
nir_opt_lower_returns_test()37 nir_opt_lower_returns_test::nir_opt_lower_returns_test()
38 {
39 glsl_type_singleton_init_or_ref();
40
41 static const nir_shader_compiler_options options = { };
42 nir_builder_init_simple_shader(&bld, NULL, MESA_SHADER_VERTEX, &options);
43
44 nir_variable *var = nir_variable_create(bld.shader, nir_var_shader_in, glsl_int_type(), "in");
45 in_def = nir_load_var(&bld, var);
46 }
47
~nir_opt_lower_returns_test()48 nir_opt_lower_returns_test::~nir_opt_lower_returns_test()
49 {
50 ralloc_free(bld.shader);
51 glsl_type_singleton_decref();
52 }
53
create_one_source_phi(nir_shader * shader,nir_block * pred,nir_ssa_def * def)54 nir_phi_instr *create_one_source_phi(nir_shader *shader, nir_block *pred,
55 nir_ssa_def *def)
56 {
57 nir_phi_instr *phi = nir_phi_instr_create(shader);
58
59 nir_phi_src *phi_src;
60 phi_src = ralloc(phi, nir_phi_src);
61 phi_src->pred = pred;
62 phi_src->src = nir_src_for_ssa(def);
63 exec_list_push_tail(&phi->srcs, &phi_src->node);
64
65 nir_ssa_dest_init(&phi->instr, &phi->dest,
66 def->num_components, def->bit_size, NULL);
67
68 return phi;
69 }
70
TEST_F(nir_opt_lower_returns_test,phis_after_loop)71 TEST_F(nir_opt_lower_returns_test, phis_after_loop)
72 {
73 /* Test that after lowering of "return" the phis in block_5
74 * have two sources, because block_2 will have block_5
75 * as a successor.
76 *
77 * block block_0:
78 * loop {
79 * block block_1:
80 * if ssa_2 {
81 * block block_2:
82 * return
83 * // succs: block_6
84 * } else {
85 * block block_3:
86 * break;
87 * // succs: block_5
88 * }
89 * block block_4:
90 * }
91 * block block_5:
92 * // preds: block_3
93 * vec1 32 ssa_4 = phi block_3: ssa_1
94 * vec1 32 ssa_5 = phi block_3: ssa_1
95 * // succs: block_6
96 * block block_6:
97 */
98
99 nir_loop *loop = nir_push_loop(&bld);
100
101 nir_ssa_def *one = nir_imm_int(&bld, 1);
102
103 nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
104 nir_if *nif = nir_push_if(&bld, cmp_result);
105
106 nir_jump(&bld, nir_jump_return);
107
108 nir_push_else(&bld, NULL);
109
110 nir_jump(&bld, nir_jump_break);
111
112 nir_pop_if(&bld, NULL);
113
114 nir_block *else_block = nir_if_last_else_block(nif);
115
116 nir_pop_loop(&bld, loop);
117
118 bld.cursor = nir_after_cf_node_and_phis(&loop->cf_node);
119
120 nir_phi_instr *const phi_1 =
121 create_one_source_phi(bld.shader, else_block, one);
122 nir_builder_instr_insert(&bld, &phi_1->instr);
123
124 nir_phi_instr *const phi_2 =
125 create_one_source_phi(bld.shader, else_block, one);
126 nir_builder_instr_insert(&bld, &phi_2->instr);
127
128 ASSERT_TRUE(nir_lower_returns(bld.shader));
129 EXPECT_EQ(phi_1->srcs.length(), 2);
130 EXPECT_EQ(phi_2->srcs.length(), 2);
131
132 nir_validate_shader(bld.shader, NULL);
133 }
134
TEST_F(nir_opt_lower_returns_test,phis_after_outer_loop)135 TEST_F(nir_opt_lower_returns_test, phis_after_outer_loop)
136 {
137 /* Test that after lowering of "return" the phis in block_7
138 * have two sources, because block_6 will have a conditional break
139 * inserted, which will add a new predcessor to block_7.
140 *
141 * block block_0:
142 * loop {
143 * block block_1:
144 * loop {
145 * block block_2:
146 * if ssa_2 {
147 * block block_3:
148 * return
149 * // succs: block_8
150 * } else {
151 * block block_4:
152 * break;
153 * // succs: block_6
154 * }
155 * block block_5:
156 * }
157 * block block_6:
158 * break;
159 * // succs: block_7
160 * }
161 * block block_7:
162 * // preds: block_6
163 * vec1 32 ssa_4 = phi block_6: ssa_1
164 * vec1 32 ssa_5 = phi block_6: ssa_1
165 * // succs: block_8
166 * block block_8:
167 */
168
169 nir_loop *loop_outer = nir_push_loop(&bld);
170
171 bld.cursor = nir_after_cf_list(&loop_outer->body);
172
173 nir_loop *loop_inner = nir_push_loop(&bld);
174
175 bld.cursor = nir_after_cf_list(&loop_inner->body);
176
177 nir_ssa_def *one = nir_imm_int(&bld, 1);
178
179 nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
180 nir_push_if(&bld, cmp_result);
181
182 nir_jump(&bld, nir_jump_return);
183
184 nir_push_else(&bld, NULL);
185
186 nir_jump(&bld, nir_jump_break);
187
188 nir_pop_if(&bld, NULL);
189
190 nir_pop_loop(&bld, loop_inner);
191
192 bld.cursor = nir_after_cf_node_and_phis(&loop_inner->cf_node);
193
194 nir_jump(&bld, nir_jump_break);
195
196 nir_pop_loop(&bld, loop_outer);
197
198 bld.cursor = nir_after_cf_node_and_phis(&loop_outer->cf_node);
199
200 nir_phi_instr *const phi_1 =
201 create_one_source_phi(bld.shader, nir_loop_last_block(loop_outer), one);
202 nir_builder_instr_insert(&bld, &phi_1->instr);
203
204 nir_phi_instr *const phi_2 =
205 create_one_source_phi(bld.shader, nir_loop_last_block(loop_outer), one);
206 nir_builder_instr_insert(&bld, &phi_2->instr);
207
208 ASSERT_TRUE(nir_lower_returns(bld.shader));
209 EXPECT_EQ(phi_1->srcs.length(), 2);
210 EXPECT_EQ(phi_2->srcs.length(), 2);
211
212 nir_validate_shader(bld.shader, NULL);
213 }
214