nir: Add a lowering pass to split 64bit phis
[mesa.git] / src / compiler / nir / tests / opt_if_tests.cpp
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_if_test : public ::testing::Test {
28 protected:
29 nir_opt_if_test();
30 ~nir_opt_if_test();
31
32 nir_builder bld;
33
34 nir_ssa_def *in_def;
35 nir_variable *out_var;
36 };
37
38 nir_opt_if_test::nir_opt_if_test()
39 {
40 glsl_type_singleton_init_or_ref();
41
42 static const nir_shader_compiler_options options = { };
43 nir_builder_init_simple_shader(&bld, NULL, MESA_SHADER_VERTEX, &options);
44
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);
47
48 out_var = nir_variable_create(bld.shader, nir_var_shader_out, glsl_int_type(), "out");
49 }
50
51 nir_opt_if_test::~nir_opt_if_test()
52 {
53 ralloc_free(bld.shader);
54 glsl_type_singleton_decref();
55 }
56
57 TEST_F(nir_opt_if_test, opt_if_simplification)
58 {
59 /* Tests that opt_if_simplification correctly optimizes a simple case:
60 *
61 * vec1 1 ssa_2 = ieq ssa_0, ssa_1
62 * if ssa_2 {
63 * block block_2:
64 * } else {
65 * block block_3:
66 * do_work()
67 * }
68 */
69
70 nir_ssa_def *one = nir_imm_int(&bld, 1);
71
72 nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
73 nir_if *nif = nir_push_if(&bld, cmp_result);
74
75 nir_push_else(&bld, NULL);
76
77 // do_work
78 nir_store_var(&bld, out_var, one, 1);
79
80 nir_pop_if(&bld, NULL);
81
82 ASSERT_TRUE(nir_opt_if(bld.shader, false));
83
84 nir_validate_shader(bld.shader, NULL);
85
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)));
88 }
89
90 TEST_F(nir_opt_if_test, opt_if_simplification_single_source_phi_after_if)
91 {
92 /* Tests that opt_if_simplification correctly handles single-source
93 * phis after the if.
94 *
95 * vec1 1 ssa_2 = ieq ssa_0, ssa_1
96 * if ssa_2 {
97 * block block_2:
98 * } else {
99 * block block_3:
100 * do_work()
101 * return
102 * }
103 * block block_4:
104 * vec1 32 ssa_3 = phi block_2: ssa_0
105 */
106
107 nir_ssa_def *one = nir_imm_int(&bld, 1);
108
109 nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
110 nir_if *nif = nir_push_if(&bld, cmp_result);
111
112 nir_push_else(&bld, NULL);
113
114 // do_work
115 nir_store_var(&bld, out_var, one, 1);
116
117 nir_jump_instr *jump = nir_jump_instr_create(bld.shader, nir_jump_return);
118 nir_builder_instr_insert(&bld, &jump->instr);
119
120 nir_pop_if(&bld, NULL);
121
122 nir_block *then_block = nir_if_last_then_block(nif);
123
124 nir_phi_instr *const phi = nir_phi_instr_create(bld.shader);
125
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);
131
132 nir_ssa_dest_init(&phi->instr, &phi->dest,
133 one->num_components, one->bit_size, NULL);
134
135 nir_builder_instr_insert(&bld, &phi->instr);
136
137 ASSERT_TRUE(nir_opt_if(bld.shader, false));
138
139 nir_validate_shader(bld.shader, NULL);
140
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)));
143 }