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 "elk_vec4.h"
26
27 using namespace elk;
28
29 class copy_propagation_vec4_test : public ::testing::Test {
30 virtual void SetUp();
31 virtual void TearDown();
32
33 public:
34 struct elk_compiler *compiler;
35 struct elk_compile_params params;
36 struct intel_device_info *devinfo;
37 void *ctx;
38 struct gl_shader_program *shader_prog;
39 struct elk_vue_prog_data *prog_data;
40 vec4_visitor *v;
41 };
42
43 class copy_propagation_vec4_visitor : public vec4_visitor
44 {
45 public:
copy_propagation_vec4_visitor(struct elk_compiler * compiler,struct elk_compile_params * params,nir_shader * shader,struct elk_vue_prog_data * prog_data)46 copy_propagation_vec4_visitor(struct elk_compiler *compiler,
47 struct elk_compile_params *params,
48 nir_shader *shader,
49 struct elk_vue_prog_data *prog_data)
50 : vec4_visitor(compiler, params, NULL, prog_data, shader,
51 false /* no_spills */, false)
52 {
53 prog_data->dispatch_mode = INTEL_DISPATCH_MODE_4X2_DUAL_OBJECT;
54 }
55
56 protected:
make_reg_for_system_value(int)57 virtual dst_reg *make_reg_for_system_value(int /* location */)
58 {
59 unreachable("Not reached");
60 }
61
setup_payload()62 virtual void setup_payload()
63 {
64 unreachable("Not reached");
65 }
66
emit_prolog()67 virtual void emit_prolog()
68 {
69 unreachable("Not reached");
70 }
71
emit_thread_end()72 virtual void emit_thread_end()
73 {
74 unreachable("Not reached");
75 }
76
emit_urb_write_header(int)77 virtual void emit_urb_write_header(int /* mrf */)
78 {
79 unreachable("Not reached");
80 }
81
emit_urb_write_opcode(bool)82 virtual vec4_instruction *emit_urb_write_opcode(bool /* complete */)
83 {
84 unreachable("Not reached");
85 }
86 };
87
88
SetUp()89 void copy_propagation_vec4_test::SetUp()
90 {
91 ctx = ralloc_context(NULL);
92 compiler = rzalloc(ctx, struct elk_compiler);
93 devinfo = rzalloc(ctx, struct intel_device_info);
94 compiler->devinfo = devinfo;
95
96 params = {};
97 params.mem_ctx = ctx;
98
99 prog_data = ralloc(ctx, struct elk_vue_prog_data);
100 nir_shader *shader =
101 nir_shader_create(ctx, MESA_SHADER_VERTEX, NULL, NULL);
102
103 v = new copy_propagation_vec4_visitor(compiler, ¶ms, shader, prog_data);
104
105 devinfo->ver = 4;
106 devinfo->verx10 = devinfo->ver * 10;
107 }
108
TearDown()109 void copy_propagation_vec4_test::TearDown()
110 {
111 delete v;
112 v = NULL;
113
114 ralloc_free(ctx);
115 ctx = NULL;
116 }
117
118
119 static void
copy_propagation(vec4_visitor * v)120 copy_propagation(vec4_visitor *v)
121 {
122 const bool print = getenv("TEST_DEBUG");
123
124 if (print) {
125 fprintf(stderr, "instructions before:\n");
126 v->dump_instructions();
127 }
128
129 v->calculate_cfg();
130 v->opt_copy_propagation();
131
132 if (print) {
133 fprintf(stderr, "instructions after:\n");
134 v->dump_instructions();
135 }
136 }
137
TEST_F(copy_propagation_vec4_test,test_swizzle_swizzle)138 TEST_F(copy_propagation_vec4_test, test_swizzle_swizzle)
139 {
140 dst_reg a = dst_reg(v, glsl_vec4_type());
141 dst_reg b = dst_reg(v, glsl_vec4_type());
142 dst_reg c = dst_reg(v, glsl_vec4_type());
143
144 v->emit(v->ADD(a, src_reg(a), src_reg(a)));
145
146 v->emit(v->MOV(b, swizzle(src_reg(a), ELK_SWIZZLE4(ELK_SWIZZLE_Y,
147 ELK_SWIZZLE_Z,
148 ELK_SWIZZLE_W,
149 ELK_SWIZZLE_X))));
150
151 vec4_instruction *test_mov =
152 v->MOV(c, swizzle(src_reg(b), ELK_SWIZZLE4(ELK_SWIZZLE_Y,
153 ELK_SWIZZLE_Z,
154 ELK_SWIZZLE_W,
155 ELK_SWIZZLE_X)));
156 v->emit(test_mov);
157
158 copy_propagation(v);
159
160 EXPECT_EQ(test_mov->src[0].nr, a.nr);
161 EXPECT_EQ(test_mov->src[0].swizzle, ELK_SWIZZLE4(ELK_SWIZZLE_Z,
162 ELK_SWIZZLE_W,
163 ELK_SWIZZLE_X,
164 ELK_SWIZZLE_Y));
165 }
166
TEST_F(copy_propagation_vec4_test,test_swizzle_writemask)167 TEST_F(copy_propagation_vec4_test, test_swizzle_writemask)
168 {
169 dst_reg a = dst_reg(v, glsl_vec4_type());
170 dst_reg b = dst_reg(v, glsl_vec4_type());
171 dst_reg c = dst_reg(v, glsl_vec4_type());
172
173 v->emit(v->MOV(b, swizzle(src_reg(a), ELK_SWIZZLE4(ELK_SWIZZLE_X,
174 ELK_SWIZZLE_Y,
175 ELK_SWIZZLE_X,
176 ELK_SWIZZLE_Z))));
177
178 v->emit(v->MOV(writemask(a, WRITEMASK_XYZ), elk_imm_f(1.0f)));
179
180 vec4_instruction *test_mov =
181 v->MOV(c, swizzle(src_reg(b), ELK_SWIZZLE4(ELK_SWIZZLE_W,
182 ELK_SWIZZLE_W,
183 ELK_SWIZZLE_W,
184 ELK_SWIZZLE_W)));
185 v->emit(test_mov);
186
187 copy_propagation(v);
188
189 /* should not copy propagate */
190 EXPECT_EQ(test_mov->src[0].nr, b.nr);
191 EXPECT_EQ(test_mov->src[0].swizzle, ELK_SWIZZLE4(ELK_SWIZZLE_W,
192 ELK_SWIZZLE_W,
193 ELK_SWIZZLE_W,
194 ELK_SWIZZLE_W));
195 }
196