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_if_test
: public ::testing::Test
{
35 nir_variable
*out_var
;
38 nir_opt_if_test::nir_opt_if_test()
40 glsl_type_singleton_init_or_ref();
42 static const nir_shader_compiler_options options
= { };
43 nir_builder_init_simple_shader(&bld
, NULL
, MESA_SHADER_VERTEX
, &options
);
45 nir_variable
*var
= nir_variable_create(bld
.shader
, nir_var_shader_in
, glsl_int_type(), "in");
46 in_def
= nir_load_var(&bld
, var
);
48 out_var
= nir_variable_create(bld
.shader
, nir_var_shader_out
, glsl_int_type(), "out");
51 nir_opt_if_test::~nir_opt_if_test()
53 ralloc_free(bld
.shader
);
54 glsl_type_singleton_decref();
57 TEST_F(nir_opt_if_test
, opt_if_simplification
)
59 /* Tests that opt_if_simplification correctly optimizes a simple case:
61 * vec1 1 ssa_2 = ieq ssa_0, ssa_1
70 nir_ssa_def
*one
= nir_imm_int(&bld
, 1);
72 nir_ssa_def
*cmp_result
= nir_ieq(&bld
, in_def
, one
);
73 nir_if
*nif
= nir_push_if(&bld
, cmp_result
);
75 nir_push_else(&bld
, NULL
);
78 nir_store_var(&bld
, out_var
, one
, 1);
80 nir_pop_if(&bld
, NULL
);
82 ASSERT_TRUE(nir_opt_if(bld
.shader
, false));
84 nir_validate_shader(bld
.shader
, NULL
);
86 ASSERT_TRUE(!exec_list_is_empty((&nir_if_first_then_block(nif
)->instr_list
)));
87 ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif
)->instr_list
)));
90 TEST_F(nir_opt_if_test
, opt_if_simplification_single_source_phi_after_if
)
92 /* Tests that opt_if_simplification correctly handles single-source
95 * vec1 1 ssa_2 = ieq ssa_0, ssa_1
104 * vec1 32 ssa_3 = phi block_2: ssa_0
107 nir_ssa_def
*one
= nir_imm_int(&bld
, 1);
109 nir_ssa_def
*cmp_result
= nir_ieq(&bld
, in_def
, one
);
110 nir_if
*nif
= nir_push_if(&bld
, cmp_result
);
112 nir_push_else(&bld
, NULL
);
115 nir_store_var(&bld
, out_var
, one
, 1);
117 nir_jump_instr
*jump
= nir_jump_instr_create(bld
.shader
, nir_jump_return
);
118 nir_builder_instr_insert(&bld
, &jump
->instr
);
120 nir_pop_if(&bld
, NULL
);
122 nir_block
*then_block
= nir_if_last_then_block(nif
);
124 nir_phi_instr
*const phi
= nir_phi_instr_create(bld
.shader
);
126 nir_phi_src
*phi_src
;
127 phi_src
= ralloc(phi
, nir_phi_src
);
128 phi_src
->pred
= then_block
;
129 phi_src
->src
= nir_src_for_ssa(one
);
130 exec_list_push_tail(&phi
->srcs
, &phi_src
->node
);
132 nir_ssa_dest_init(&phi
->instr
, &phi
->dest
,
133 one
->num_components
, one
->bit_size
, NULL
);
135 nir_builder_instr_insert(&bld
, &phi
->instr
);
137 ASSERT_TRUE(nir_opt_if(bld
.shader
, false));
139 nir_validate_shader(bld
.shader
, NULL
);
141 ASSERT_TRUE(nir_block_ends_in_jump(nir_if_last_then_block(nif
)));
142 ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif
)->instr_list
)));