2
* Copyright © 2020 Intel Corporation
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:
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
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.
23
#include <gtest/gtest.h>
25
#include "nir_builder.h"
27
class nir_opt_lower_returns_test : public ::testing::Test {
29
nir_opt_lower_returns_test();
30
~nir_opt_lower_returns_test();
37
nir_opt_lower_returns_test::nir_opt_lower_returns_test()
39
glsl_type_singleton_init_or_ref();
41
static const nir_shader_compiler_options options = { };
42
bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "lower returns test");
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);
48
nir_opt_lower_returns_test::~nir_opt_lower_returns_test()
50
ralloc_free(bld.shader);
51
glsl_type_singleton_decref();
54
nir_phi_instr *create_one_source_phi(nir_shader *shader, nir_block *pred,
57
nir_phi_instr *phi = nir_phi_instr_create(shader);
59
nir_phi_instr_add_src(phi, pred, nir_src_for_ssa(def));
61
nir_ssa_dest_init(&phi->instr, &phi->dest,
62
def->num_components, def->bit_size, NULL);
67
TEST_F(nir_opt_lower_returns_test, phis_after_loop)
69
/* Test that after lowering of "return" the phis in block_5
70
* have two sources, because block_2 will have block_5
89
* vec1 32 ssa_4 = phi block_3: ssa_1
90
* vec1 32 ssa_5 = phi block_3: ssa_1
95
nir_loop *loop = nir_push_loop(&bld);
97
nir_ssa_def *one = nir_imm_int(&bld, 1);
99
nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
100
nir_if *nif = nir_push_if(&bld, cmp_result);
102
nir_jump(&bld, nir_jump_return);
104
nir_push_else(&bld, NULL);
106
nir_jump(&bld, nir_jump_break);
108
nir_pop_if(&bld, NULL);
110
nir_block *else_block = nir_if_last_else_block(nif);
112
nir_pop_loop(&bld, loop);
114
bld.cursor = nir_after_cf_node_and_phis(&loop->cf_node);
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);
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);
124
ASSERT_TRUE(nir_lower_returns(bld.shader));
125
EXPECT_EQ(phi_1->srcs.length(), 2);
126
EXPECT_EQ(phi_2->srcs.length(), 2);
128
nir_validate_shader(bld.shader, NULL);
131
TEST_F(nir_opt_lower_returns_test, phis_after_outer_loop)
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.
159
* vec1 32 ssa_4 = phi block_6: ssa_1
160
* vec1 32 ssa_5 = phi block_6: ssa_1
165
nir_loop *loop_outer = nir_push_loop(&bld);
167
bld.cursor = nir_after_cf_list(&loop_outer->body);
169
nir_loop *loop_inner = nir_push_loop(&bld);
171
bld.cursor = nir_after_cf_list(&loop_inner->body);
173
nir_ssa_def *one = nir_imm_int(&bld, 1);
175
nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
176
nir_push_if(&bld, cmp_result);
178
nir_jump(&bld, nir_jump_return);
180
nir_push_else(&bld, NULL);
182
nir_jump(&bld, nir_jump_break);
184
nir_pop_if(&bld, NULL);
186
nir_pop_loop(&bld, loop_inner);
188
bld.cursor = nir_after_cf_node_and_phis(&loop_inner->cf_node);
190
nir_jump(&bld, nir_jump_break);
192
nir_pop_loop(&bld, loop_outer);
194
bld.cursor = nir_after_cf_node_and_phis(&loop_outer->cf_node);
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);
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);
204
ASSERT_TRUE(nir_lower_returns(bld.shader));
205
EXPECT_EQ(phi_1->srcs.length(), 2);
206
EXPECT_EQ(phi_2->srcs.length(), 2);
208
nir_validate_shader(bld.shader, NULL);