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 28using namespace brw; 29 30class copy_propagation_test : public ::testing::Test { 31 virtual void SetUp(); 32 virtual void TearDown(); 33 34public: 35 struct brw_compiler *compiler; 36 struct intel_device_info *devinfo; 37 void *ctx; 38 struct gl_shader_program *shader_prog; 39 struct brw_vue_prog_data *prog_data; 40 vec4_visitor *v; 41}; 42 43class copy_propagation_vec4_visitor : public vec4_visitor 44{ 45public: 46 copy_propagation_vec4_visitor(struct brw_compiler *compiler, 47 void *mem_ctx, 48 nir_shader *shader, 49 struct brw_vue_prog_data *prog_data) 50 : vec4_visitor(compiler, NULL, NULL, prog_data, shader, mem_ctx, 51 false /* no_spills */, -1, false) 52 { 53 prog_data->dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT; 54 } 55 56protected: 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 89void copy_propagation_test::SetUp() 90{ 91 ctx = ralloc_context(NULL); 92 compiler = rzalloc(ctx, struct brw_compiler); 93 devinfo = rzalloc(ctx, struct intel_device_info); 94 compiler->devinfo = devinfo; 95 96 prog_data = ralloc(ctx, struct brw_vue_prog_data); 97 nir_shader *shader = 98 nir_shader_create(ctx, MESA_SHADER_VERTEX, NULL, NULL); 99 100 v = new copy_propagation_vec4_visitor(compiler, ctx, shader, prog_data); 101 102 devinfo->ver = 4; 103 devinfo->verx10 = devinfo->ver * 10; 104} 105 106void copy_propagation_test::TearDown() 107{ 108 delete v; 109 v = NULL; 110 111 ralloc_free(ctx); 112 ctx = NULL; 113} 114 115 116static void 117copy_propagation(vec4_visitor *v) 118{ 119 const bool print = getenv("TEST_DEBUG"); 120 121 if (print) { 122 fprintf(stderr, "instructions before:\n"); 123 v->dump_instructions(); 124 } 125 126 v->calculate_cfg(); 127 v->opt_copy_propagation(); 128 129 if (print) { 130 fprintf(stderr, "instructions after:\n"); 131 v->dump_instructions(); 132 } 133} 134 135TEST_F(copy_propagation_test, test_swizzle_swizzle) 136{ 137 dst_reg a = dst_reg(v, glsl_type::vec4_type); 138 dst_reg b = dst_reg(v, glsl_type::vec4_type); 139 dst_reg c = dst_reg(v, glsl_type::vec4_type); 140 141 v->emit(v->ADD(a, src_reg(a), src_reg(a))); 142 143 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_Y, 144 SWIZZLE_Z, 145 SWIZZLE_W, 146 SWIZZLE_X)))); 147 148 vec4_instruction *test_mov = 149 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_Y, 150 SWIZZLE_Z, 151 SWIZZLE_W, 152 SWIZZLE_X))); 153 v->emit(test_mov); 154 155 copy_propagation(v); 156 157 EXPECT_EQ(test_mov->src[0].nr, a.nr); 158 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_Z, 159 SWIZZLE_W, 160 SWIZZLE_X, 161 SWIZZLE_Y)); 162} 163 164TEST_F(copy_propagation_test, test_swizzle_writemask) 165{ 166 dst_reg a = dst_reg(v, glsl_type::vec4_type); 167 dst_reg b = dst_reg(v, glsl_type::vec4_type); 168 dst_reg c = dst_reg(v, glsl_type::vec4_type); 169 170 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_X, 171 SWIZZLE_Y, 172 SWIZZLE_X, 173 SWIZZLE_Z)))); 174 175 v->emit(v->MOV(writemask(a, WRITEMASK_XYZ), brw_imm_f(1.0f))); 176 177 vec4_instruction *test_mov = 178 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_W, 179 SWIZZLE_W, 180 SWIZZLE_W, 181 SWIZZLE_W))); 182 v->emit(test_mov); 183 184 copy_propagation(v); 185 186 /* should not copy propagate */ 187 EXPECT_EQ(test_mov->src[0].nr, b.nr); 188 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_W, 189 SWIZZLE_W, 190 SWIZZLE_W, 191 SWIZZLE_W)); 192} 193