1 /* 2 * Copyright 2014 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 DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #include <gtest/gtest.h> 25 #include "brw_vec4.h" 26 #include "program/program.h" 27 28 using namespace brw; 29 30 int ret = 0; 31 32 class copy_propagation_test : public ::testing::Test { 33 virtual void SetUp(); 34 35 public: 36 struct brw_compiler *compiler; 37 struct gen_device_info *devinfo; 38 struct gl_context *ctx; 39 struct gl_shader_program *shader_prog; 40 struct brw_vue_prog_data *prog_data; 41 vec4_visitor *v; 42 }; 43 44 class copy_propagation_vec4_visitor : public vec4_visitor 45 { 46 public: 47 copy_propagation_vec4_visitor(struct brw_compiler *compiler, 48 nir_shader *shader, 49 struct brw_vue_prog_data *prog_data) 50 : vec4_visitor(compiler, NULL, NULL, prog_data, shader, NULL, 51 false /* no_spills */, -1) 52 { 53 prog_data->dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT; 54 } 55 56 protected: 57 virtual dst_reg *make_reg_for_system_value(int location) 58 { 59 unreachable("Not reached"); 60 } 61 62 virtual void setup_payload() 63 { 64 unreachable("Not reached"); 65 } 66 67 virtual void emit_prolog() 68 { 69 unreachable("Not reached"); 70 } 71 72 virtual void emit_thread_end() 73 { 74 unreachable("Not reached"); 75 } 76 77 virtual void emit_urb_write_header(int mrf) 78 { 79 unreachable("Not reached"); 80 } 81 82 virtual vec4_instruction *emit_urb_write_opcode(bool complete) 83 { 84 unreachable("Not reached"); 85 } 86 }; 87 88 89 void copy_propagation_test::SetUp() 90 { 91 ctx = (struct gl_context *)calloc(1, sizeof(*ctx)); 92 compiler = (struct brw_compiler *)calloc(1, sizeof(*compiler)); 93 devinfo = (struct gen_device_info *)calloc(1, sizeof(*devinfo)); 94 prog_data = (struct brw_vue_prog_data *)calloc(1, sizeof(*prog_data)); 95 compiler->devinfo = devinfo; 96 97 nir_shader *shader = 98 nir_shader_create(NULL, MESA_SHADER_VERTEX, NULL, NULL); 99 100 v = new copy_propagation_vec4_visitor(compiler, shader, prog_data); 101 102 devinfo->gen = 4; 103 } 104 105 static void 106 copy_propagation(vec4_visitor *v) 107 { 108 bool print = false; 109 110 if (print) { 111 fprintf(stderr, "instructions before:\n"); 112 v->dump_instructions(); 113 } 114 115 v->calculate_cfg(); 116 v->opt_copy_propagation(); 117 118 if (print) { 119 fprintf(stderr, "instructions after:\n"); 120 v->dump_instructions(); 121 } 122 } 123 124 TEST_F(copy_propagation_test, test_swizzle_swizzle) 125 { 126 dst_reg a = dst_reg(v, glsl_type::vec4_type); 127 dst_reg b = dst_reg(v, glsl_type::vec4_type); 128 dst_reg c = dst_reg(v, glsl_type::vec4_type); 129 130 v->emit(v->ADD(a, src_reg(a), src_reg(a))); 131 132 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_Y, 133 SWIZZLE_Z, 134 SWIZZLE_W, 135 SWIZZLE_X)))); 136 137 vec4_instruction *test_mov = 138 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_Y, 139 SWIZZLE_Z, 140 SWIZZLE_W, 141 SWIZZLE_X))); 142 v->emit(test_mov); 143 144 copy_propagation(v); 145 146 EXPECT_EQ(test_mov->src[0].nr, a.nr); 147 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_Z, 148 SWIZZLE_W, 149 SWIZZLE_X, 150 SWIZZLE_Y)); 151 } 152 153 TEST_F(copy_propagation_test, test_swizzle_writemask) 154 { 155 dst_reg a = dst_reg(v, glsl_type::vec4_type); 156 dst_reg b = dst_reg(v, glsl_type::vec4_type); 157 dst_reg c = dst_reg(v, glsl_type::vec4_type); 158 159 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_X, 160 SWIZZLE_Y, 161 SWIZZLE_X, 162 SWIZZLE_Z)))); 163 164 v->emit(v->MOV(writemask(a, WRITEMASK_XYZ), brw_imm_f(1.0f))); 165 166 vec4_instruction *test_mov = 167 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_W, 168 SWIZZLE_W, 169 SWIZZLE_W, 170 SWIZZLE_W))); 171 v->emit(test_mov); 172 173 copy_propagation(v); 174 175 /* should not copy propagate */ 176 EXPECT_EQ(test_mov->src[0].nr, b.nr); 177 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_W, 178 SWIZZLE_W, 179 SWIZZLE_W, 180 SWIZZLE_W)); 181 } 182