1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2012 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 DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include <gtest/gtest.h> 25bf215546Sopenharmony_ci#include "brw_vec4.h" 26bf215546Sopenharmony_ci#include "program/program.h" 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ciusing namespace brw; 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#define register_coalesce(v) _register_coalesce(v, __func__) 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ciclass register_coalesce_vec4_test : public ::testing::Test { 33bf215546Sopenharmony_ci virtual void SetUp(); 34bf215546Sopenharmony_ci virtual void TearDown(); 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_cipublic: 37bf215546Sopenharmony_ci struct brw_compiler *compiler; 38bf215546Sopenharmony_ci struct intel_device_info *devinfo; 39bf215546Sopenharmony_ci void *ctx; 40bf215546Sopenharmony_ci struct gl_shader_program *shader_prog; 41bf215546Sopenharmony_ci struct brw_vue_prog_data *prog_data; 42bf215546Sopenharmony_ci vec4_visitor *v; 43bf215546Sopenharmony_ci}; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ciclass register_coalesce_vec4_visitor : public vec4_visitor 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_cipublic: 49bf215546Sopenharmony_ci register_coalesce_vec4_visitor(struct brw_compiler *compiler, 50bf215546Sopenharmony_ci void *mem_ctx, 51bf215546Sopenharmony_ci nir_shader *shader, 52bf215546Sopenharmony_ci struct brw_vue_prog_data *prog_data) 53bf215546Sopenharmony_ci : vec4_visitor(compiler, NULL, NULL, prog_data, shader, mem_ctx, 54bf215546Sopenharmony_ci false /* no_spills */, false) 55bf215546Sopenharmony_ci { 56bf215546Sopenharmony_ci prog_data->dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT; 57bf215546Sopenharmony_ci } 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ciprotected: 60bf215546Sopenharmony_ci virtual dst_reg *make_reg_for_system_value(int /* location */) 61bf215546Sopenharmony_ci { 62bf215546Sopenharmony_ci unreachable("Not reached"); 63bf215546Sopenharmony_ci } 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci virtual void setup_payload() 66bf215546Sopenharmony_ci { 67bf215546Sopenharmony_ci unreachable("Not reached"); 68bf215546Sopenharmony_ci } 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci virtual void emit_prolog() 71bf215546Sopenharmony_ci { 72bf215546Sopenharmony_ci unreachable("Not reached"); 73bf215546Sopenharmony_ci } 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci virtual void emit_thread_end() 76bf215546Sopenharmony_ci { 77bf215546Sopenharmony_ci unreachable("Not reached"); 78bf215546Sopenharmony_ci } 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci virtual void emit_urb_write_header(int /* mrf */) 81bf215546Sopenharmony_ci { 82bf215546Sopenharmony_ci unreachable("Not reached"); 83bf215546Sopenharmony_ci } 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci virtual vec4_instruction *emit_urb_write_opcode(bool /* complete */) 86bf215546Sopenharmony_ci { 87bf215546Sopenharmony_ci unreachable("Not reached"); 88bf215546Sopenharmony_ci } 89bf215546Sopenharmony_ci}; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_civoid register_coalesce_vec4_test::SetUp() 93bf215546Sopenharmony_ci{ 94bf215546Sopenharmony_ci ctx = ralloc_context(NULL); 95bf215546Sopenharmony_ci compiler = rzalloc(ctx, struct brw_compiler); 96bf215546Sopenharmony_ci devinfo = rzalloc(ctx, struct intel_device_info); 97bf215546Sopenharmony_ci compiler->devinfo = devinfo; 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci prog_data = ralloc(ctx, struct brw_vue_prog_data); 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci nir_shader *shader = 102bf215546Sopenharmony_ci nir_shader_create(ctx, MESA_SHADER_VERTEX, NULL, NULL); 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci v = new register_coalesce_vec4_visitor(compiler, ctx, shader, prog_data); 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci devinfo->ver = 4; 107bf215546Sopenharmony_ci devinfo->verx10 = devinfo->ver * 10; 108bf215546Sopenharmony_ci} 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_civoid register_coalesce_vec4_test::TearDown() 111bf215546Sopenharmony_ci{ 112bf215546Sopenharmony_ci delete v; 113bf215546Sopenharmony_ci v = NULL; 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci ralloc_free(ctx); 116bf215546Sopenharmony_ci ctx = NULL; 117bf215546Sopenharmony_ci} 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_cistatic void 120bf215546Sopenharmony_ci_register_coalesce(vec4_visitor *v, const char *func) 121bf215546Sopenharmony_ci{ 122bf215546Sopenharmony_ci const bool print = getenv("TEST_DEBUG"); 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci if (print) { 125bf215546Sopenharmony_ci printf("%s: instructions before:\n", func); 126bf215546Sopenharmony_ci v->dump_instructions(); 127bf215546Sopenharmony_ci } 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci v->calculate_cfg(); 130bf215546Sopenharmony_ci v->opt_register_coalesce(); 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci if (print) { 133bf215546Sopenharmony_ci printf("%s: instructions after:\n", func); 134bf215546Sopenharmony_ci v->dump_instructions(); 135bf215546Sopenharmony_ci } 136bf215546Sopenharmony_ci} 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ciTEST_F(register_coalesce_vec4_test, test_compute_to_mrf) 139bf215546Sopenharmony_ci{ 140bf215546Sopenharmony_ci src_reg something = src_reg(v, glsl_type::float_type); 141bf215546Sopenharmony_ci dst_reg temp = dst_reg(v, glsl_type::float_type); 142bf215546Sopenharmony_ci dst_reg init; 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci dst_reg m0 = dst_reg(MRF, 0); 145bf215546Sopenharmony_ci m0.writemask = WRITEMASK_X; 146bf215546Sopenharmony_ci m0.type = BRW_REGISTER_TYPE_F; 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci vec4_instruction *mul = v->emit(v->MUL(temp, something, brw_imm_f(1.0f))); 149bf215546Sopenharmony_ci v->emit(v->MOV(m0, src_reg(temp))); 150bf215546Sopenharmony_ci 151bf215546Sopenharmony_ci register_coalesce(v); 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci EXPECT_EQ(mul->dst.file, MRF); 154bf215546Sopenharmony_ci} 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ciTEST_F(register_coalesce_vec4_test, test_multiple_use) 158bf215546Sopenharmony_ci{ 159bf215546Sopenharmony_ci src_reg something = src_reg(v, glsl_type::float_type); 160bf215546Sopenharmony_ci dst_reg temp = dst_reg(v, glsl_type::vec4_type); 161bf215546Sopenharmony_ci dst_reg init; 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci dst_reg m0 = dst_reg(MRF, 0); 164bf215546Sopenharmony_ci m0.writemask = WRITEMASK_X; 165bf215546Sopenharmony_ci m0.type = BRW_REGISTER_TYPE_F; 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci dst_reg m1 = dst_reg(MRF, 1); 168bf215546Sopenharmony_ci m1.writemask = WRITEMASK_XYZW; 169bf215546Sopenharmony_ci m1.type = BRW_REGISTER_TYPE_F; 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci src_reg src = src_reg(temp); 172bf215546Sopenharmony_ci vec4_instruction *mul = v->emit(v->MUL(temp, something, brw_imm_f(1.0f))); 173bf215546Sopenharmony_ci src.swizzle = BRW_SWIZZLE_XXXX; 174bf215546Sopenharmony_ci v->emit(v->MOV(m0, src)); 175bf215546Sopenharmony_ci src.swizzle = BRW_SWIZZLE_XYZW; 176bf215546Sopenharmony_ci v->emit(v->MOV(m1, src)); 177bf215546Sopenharmony_ci 178bf215546Sopenharmony_ci register_coalesce(v); 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci EXPECT_NE(mul->dst.file, MRF); 181bf215546Sopenharmony_ci} 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_ciTEST_F(register_coalesce_vec4_test, test_dp4_mrf) 184bf215546Sopenharmony_ci{ 185bf215546Sopenharmony_ci src_reg some_src_1 = src_reg(v, glsl_type::vec4_type); 186bf215546Sopenharmony_ci src_reg some_src_2 = src_reg(v, glsl_type::vec4_type); 187bf215546Sopenharmony_ci dst_reg init; 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ci dst_reg m0 = dst_reg(MRF, 0); 190bf215546Sopenharmony_ci m0.writemask = WRITEMASK_Y; 191bf215546Sopenharmony_ci m0.type = BRW_REGISTER_TYPE_F; 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci dst_reg temp = dst_reg(v, glsl_type::float_type); 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci vec4_instruction *dp4 = v->emit(v->DP4(temp, some_src_1, some_src_2)); 196bf215546Sopenharmony_ci v->emit(v->MOV(m0, src_reg(temp))); 197bf215546Sopenharmony_ci 198bf215546Sopenharmony_ci register_coalesce(v); 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci EXPECT_EQ(dp4->dst.file, MRF); 201bf215546Sopenharmony_ci EXPECT_EQ(dp4->dst.writemask, WRITEMASK_Y); 202bf215546Sopenharmony_ci} 203bf215546Sopenharmony_ci 204bf215546Sopenharmony_ciTEST_F(register_coalesce_vec4_test, test_dp4_grf) 205bf215546Sopenharmony_ci{ 206bf215546Sopenharmony_ci src_reg some_src_1 = src_reg(v, glsl_type::vec4_type); 207bf215546Sopenharmony_ci src_reg some_src_2 = src_reg(v, glsl_type::vec4_type); 208bf215546Sopenharmony_ci dst_reg init; 209bf215546Sopenharmony_ci 210bf215546Sopenharmony_ci dst_reg to = dst_reg(v, glsl_type::vec4_type); 211bf215546Sopenharmony_ci dst_reg temp = dst_reg(v, glsl_type::float_type); 212bf215546Sopenharmony_ci 213bf215546Sopenharmony_ci vec4_instruction *dp4 = v->emit(v->DP4(temp, some_src_1, some_src_2)); 214bf215546Sopenharmony_ci to.writemask = WRITEMASK_Y; 215bf215546Sopenharmony_ci v->emit(v->MOV(to, src_reg(temp))); 216bf215546Sopenharmony_ci 217bf215546Sopenharmony_ci /* if we don't do something with the result, the automatic dead code 218bf215546Sopenharmony_ci * elimination will remove all our instructions. 219bf215546Sopenharmony_ci */ 220bf215546Sopenharmony_ci src_reg src = src_reg(to); 221bf215546Sopenharmony_ci src.negate = true; 222bf215546Sopenharmony_ci v->emit(v->MOV(dst_reg(MRF, 0), src)); 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci register_coalesce(v); 225bf215546Sopenharmony_ci 226bf215546Sopenharmony_ci EXPECT_EQ(dp4->dst.nr, to.nr); 227bf215546Sopenharmony_ci EXPECT_EQ(dp4->dst.writemask, WRITEMASK_Y); 228bf215546Sopenharmony_ci} 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ciTEST_F(register_coalesce_vec4_test, test_channel_mul_grf) 231bf215546Sopenharmony_ci{ 232bf215546Sopenharmony_ci src_reg some_src_1 = src_reg(v, glsl_type::vec4_type); 233bf215546Sopenharmony_ci src_reg some_src_2 = src_reg(v, glsl_type::vec4_type); 234bf215546Sopenharmony_ci dst_reg init; 235bf215546Sopenharmony_ci 236bf215546Sopenharmony_ci dst_reg to = dst_reg(v, glsl_type::vec4_type); 237bf215546Sopenharmony_ci dst_reg temp = dst_reg(v, glsl_type::float_type); 238bf215546Sopenharmony_ci 239bf215546Sopenharmony_ci vec4_instruction *mul = v->emit(v->MUL(temp, some_src_1, some_src_2)); 240bf215546Sopenharmony_ci to.writemask = WRITEMASK_Y; 241bf215546Sopenharmony_ci v->emit(v->MOV(to, src_reg(temp))); 242bf215546Sopenharmony_ci 243bf215546Sopenharmony_ci /* if we don't do something with the result, the automatic dead code 244bf215546Sopenharmony_ci * elimination will remove all our instructions. 245bf215546Sopenharmony_ci */ 246bf215546Sopenharmony_ci src_reg src = src_reg(to); 247bf215546Sopenharmony_ci src.negate = true; 248bf215546Sopenharmony_ci v->emit(v->MOV(dst_reg(MRF, 0), src)); 249bf215546Sopenharmony_ci 250bf215546Sopenharmony_ci register_coalesce(v); 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci EXPECT_EQ(mul->dst.nr, to.nr); 253bf215546Sopenharmony_ci} 254