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 27class nir_opt_if_test : public ::testing::Test { 28protected: 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 38nir_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 bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "if test"); 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 51nir_opt_if_test::~nir_opt_if_test() 52{ 53 ralloc_free(bld.shader); 54 glsl_type_singleton_decref(); 55} 56 57TEST_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, nir_opt_if_optimize_phi_true_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 90TEST_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_instr_add_src(phi, then_block, nir_src_for_ssa(one)); 127 128 nir_ssa_dest_init(&phi->instr, &phi->dest, 129 one->num_components, one->bit_size, NULL); 130 131 nir_builder_instr_insert(&bld, &phi->instr); 132 133 ASSERT_TRUE(nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false)); 134 135 nir_validate_shader(bld.shader, NULL); 136 137 ASSERT_TRUE(nir_block_ends_in_jump(nir_if_last_then_block(nif))); 138 ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif)->instr_list))); 139} 140 141TEST_F(nir_opt_if_test, opt_if_alu_of_phi_progress) 142{ 143 nir_ssa_def *two = nir_imm_int(&bld, 2); 144 nir_ssa_def *x = nir_imm_int(&bld, 0); 145 146 nir_phi_instr *phi = nir_phi_instr_create(bld.shader); 147 148 nir_loop *loop = nir_push_loop(&bld); 149 { 150 nir_ssa_dest_init(&phi->instr, &phi->dest, 151 x->num_components, x->bit_size, NULL); 152 153 nir_phi_instr_add_src(phi, x->parent_instr->block, nir_src_for_ssa(x)); 154 155 nir_ssa_def *y = nir_iadd(&bld, &phi->dest.ssa, two); 156 nir_store_var(&bld, out_var, 157 nir_imul(&bld, &phi->dest.ssa, two), 1); 158 159 nir_phi_instr_add_src(phi, nir_cursor_current_block(bld.cursor), nir_src_for_ssa(y)); 160 } 161 nir_pop_loop(&bld, loop); 162 163 bld.cursor = nir_before_block(nir_loop_first_block(loop)); 164 nir_builder_instr_insert(&bld, &phi->instr); 165 166 nir_validate_shader(bld.shader, "input"); 167 168 bool progress; 169 170 int progress_count = 0; 171 for (int i = 0; i < 10; i++) { 172 progress = nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false); 173 if (progress) 174 progress_count++; 175 else 176 break; 177 nir_opt_constant_folding(bld.shader); 178 } 179 180 EXPECT_LE(progress_count, 2); 181 ASSERT_FALSE(progress); 182} 183