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