1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci#include <gtest/gtest.h>
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci#include "nir_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ciclass nir_opt_if_test : public ::testing::Test {
28bf215546Sopenharmony_ciprotected:
29bf215546Sopenharmony_ci   nir_opt_if_test();
30bf215546Sopenharmony_ci   ~nir_opt_if_test();
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci   nir_builder bld;
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci   nir_ssa_def *in_def;
35bf215546Sopenharmony_ci   nir_variable *out_var;
36bf215546Sopenharmony_ci};
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cinir_opt_if_test::nir_opt_if_test()
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   glsl_type_singleton_init_or_ref();
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   static const nir_shader_compiler_options options = { };
43bf215546Sopenharmony_ci   bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "if test");
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   nir_variable *var = nir_variable_create(bld.shader, nir_var_shader_in, glsl_int_type(), "in");
46bf215546Sopenharmony_ci   in_def = nir_load_var(&bld, var);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   out_var = nir_variable_create(bld.shader, nir_var_shader_out, glsl_int_type(), "out");
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cinir_opt_if_test::~nir_opt_if_test()
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci   ralloc_free(bld.shader);
54bf215546Sopenharmony_ci   glsl_type_singleton_decref();
55bf215546Sopenharmony_ci}
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ciTEST_F(nir_opt_if_test, opt_if_simplification)
58bf215546Sopenharmony_ci{
59bf215546Sopenharmony_ci   /* Tests that opt_if_simplification correctly optimizes a simple case:
60bf215546Sopenharmony_ci    *
61bf215546Sopenharmony_ci    * vec1 1 ssa_2 = ieq ssa_0, ssa_1
62bf215546Sopenharmony_ci    * if ssa_2 {
63bf215546Sopenharmony_ci    *    block block_2:
64bf215546Sopenharmony_ci    * } else {
65bf215546Sopenharmony_ci    *    block block_3:
66bf215546Sopenharmony_ci    *    do_work()
67bf215546Sopenharmony_ci    * }
68bf215546Sopenharmony_ci    */
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   nir_ssa_def *one = nir_imm_int(&bld, 1);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
73bf215546Sopenharmony_ci   nir_if *nif = nir_push_if(&bld, cmp_result);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   nir_push_else(&bld, NULL);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   // do_work
78bf215546Sopenharmony_ci   nir_store_var(&bld, out_var, one, 1);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   nir_pop_if(&bld, NULL);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   ASSERT_TRUE(nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false));
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   nir_validate_shader(bld.shader, NULL);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   ASSERT_TRUE(!exec_list_is_empty((&nir_if_first_then_block(nif)->instr_list)));
87bf215546Sopenharmony_ci   ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif)->instr_list)));
88bf215546Sopenharmony_ci}
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ciTEST_F(nir_opt_if_test, opt_if_simplification_single_source_phi_after_if)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci   /* Tests that opt_if_simplification correctly handles single-source
93bf215546Sopenharmony_ci    * phis after the if.
94bf215546Sopenharmony_ci    *
95bf215546Sopenharmony_ci    * vec1 1 ssa_2 = ieq ssa_0, ssa_1
96bf215546Sopenharmony_ci    * if ssa_2 {
97bf215546Sopenharmony_ci    *    block block_2:
98bf215546Sopenharmony_ci    * } else {
99bf215546Sopenharmony_ci    *    block block_3:
100bf215546Sopenharmony_ci    *    do_work()
101bf215546Sopenharmony_ci    *    return
102bf215546Sopenharmony_ci    * }
103bf215546Sopenharmony_ci    * block block_4:
104bf215546Sopenharmony_ci    * vec1 32 ssa_3 = phi block_2: ssa_0
105bf215546Sopenharmony_ci    */
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   nir_ssa_def *one = nir_imm_int(&bld, 1);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one);
110bf215546Sopenharmony_ci   nir_if *nif = nir_push_if(&bld, cmp_result);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   nir_push_else(&bld, NULL);
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   // do_work
115bf215546Sopenharmony_ci   nir_store_var(&bld, out_var, one, 1);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   nir_jump_instr *jump = nir_jump_instr_create(bld.shader, nir_jump_return);
118bf215546Sopenharmony_ci   nir_builder_instr_insert(&bld, &jump->instr);
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   nir_pop_if(&bld, NULL);
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   nir_block *then_block = nir_if_last_then_block(nif);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   nir_phi_instr *const phi = nir_phi_instr_create(bld.shader);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   nir_phi_instr_add_src(phi, then_block, nir_src_for_ssa(one));
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   nir_ssa_dest_init(&phi->instr, &phi->dest,
129bf215546Sopenharmony_ci                     one->num_components, one->bit_size, NULL);
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   nir_builder_instr_insert(&bld, &phi->instr);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   ASSERT_TRUE(nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false));
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   nir_validate_shader(bld.shader, NULL);
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   ASSERT_TRUE(nir_block_ends_in_jump(nir_if_last_then_block(nif)));
138bf215546Sopenharmony_ci   ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif)->instr_list)));
139bf215546Sopenharmony_ci}
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ciTEST_F(nir_opt_if_test, opt_if_alu_of_phi_progress)
142bf215546Sopenharmony_ci{
143bf215546Sopenharmony_ci   nir_ssa_def *two = nir_imm_int(&bld, 2);
144bf215546Sopenharmony_ci   nir_ssa_def *x = nir_imm_int(&bld, 0);
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci   nir_phi_instr *phi = nir_phi_instr_create(bld.shader);
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   nir_loop *loop = nir_push_loop(&bld);
149bf215546Sopenharmony_ci   {
150bf215546Sopenharmony_ci      nir_ssa_dest_init(&phi->instr, &phi->dest,
151bf215546Sopenharmony_ci                        x->num_components, x->bit_size, NULL);
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci      nir_phi_instr_add_src(phi, x->parent_instr->block, nir_src_for_ssa(x));
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci      nir_ssa_def *y = nir_iadd(&bld, &phi->dest.ssa, two);
156bf215546Sopenharmony_ci      nir_store_var(&bld, out_var,
157bf215546Sopenharmony_ci                    nir_imul(&bld, &phi->dest.ssa, two), 1);
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci      nir_phi_instr_add_src(phi, nir_cursor_current_block(bld.cursor), nir_src_for_ssa(y));
160bf215546Sopenharmony_ci   }
161bf215546Sopenharmony_ci   nir_pop_loop(&bld, loop);
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   bld.cursor = nir_before_block(nir_loop_first_block(loop));
164bf215546Sopenharmony_ci   nir_builder_instr_insert(&bld, &phi->instr);
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci   nir_validate_shader(bld.shader, "input");
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   bool progress;
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci   int progress_count = 0;
171bf215546Sopenharmony_ci   for (int i = 0; i < 10; i++) {
172bf215546Sopenharmony_ci      progress = nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false);
173bf215546Sopenharmony_ci      if (progress)
174bf215546Sopenharmony_ci         progress_count++;
175bf215546Sopenharmony_ci      else
176bf215546Sopenharmony_ci         break;
177bf215546Sopenharmony_ci      nir_opt_constant_folding(bld.shader);
178bf215546Sopenharmony_ci   }
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   EXPECT_LE(progress_count, 2);
181bf215546Sopenharmony_ci   ASSERT_FALSE(progress);
182bf215546Sopenharmony_ci}
183