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