2 * Copyright © 2012 Intel Corporation
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:
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
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
24 #include <gtest/gtest.h>
32 #define register_coalesce(v) _register_coalesce(v, __FUNCTION__)
34 class register_coalesce_test
: public ::testing::Test
{
38 struct brw_context
*brw
;
39 struct gl_context
*ctx
;
40 struct gl_shader_program
*shader_prog
;
41 struct brw_vertex_program
*vp
;
46 class register_coalesce_vec4_visitor
: public vec4_visitor
49 register_coalesce_vec4_visitor(struct brw_context
*brw
,
50 struct gl_shader_program
*shader_prog
)
51 : vec4_visitor(brw
, NULL
, NULL
, NULL
, NULL
, shader_prog
, NULL
, NULL
,
52 false, false /* no_spills */,
53 ST_NONE
, ST_NONE
, ST_NONE
)
58 virtual dst_reg
*make_reg_for_system_value(ir_variable
*ir
)
60 assert(!"Not reached");
64 virtual void setup_payload()
66 assert(!"Not reached");
69 virtual void emit_prolog()
71 assert(!"Not reached");
74 virtual void emit_program_code()
76 assert(!"Not reached");
79 virtual void emit_thread_end()
81 assert(!"Not reached");
84 virtual void emit_urb_write_header(int mrf
)
86 assert(!"Not reached");
89 virtual vec4_instruction
*emit_urb_write_opcode(bool complete
)
91 assert(!"Not reached");
97 void register_coalesce_test::SetUp()
99 brw
= (struct brw_context
*)calloc(1, sizeof(*brw
));
102 vp
= ralloc(NULL
, struct brw_vertex_program
);
104 shader_prog
= ralloc(NULL
, struct gl_shader_program
);
106 v
= new register_coalesce_vec4_visitor(brw
, shader_prog
);
108 _mesa_init_vertex_program(ctx
, &vp
->program
, GL_VERTEX_SHADER
, 0);
114 _register_coalesce(vec4_visitor
*v
, const char *func
)
119 printf("%s: instructions before:\n", func
);
120 v
->dump_instructions();
123 v
->opt_register_coalesce();
126 printf("%s: instructions after:\n", func
);
127 v
->dump_instructions();
131 TEST_F(register_coalesce_test
, test_compute_to_mrf
)
133 src_reg something
= src_reg(v
, glsl_type::float_type
);
134 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
137 dst_reg m0
= dst_reg(MRF
, 0);
138 m0
.writemask
= WRITEMASK_X
;
139 m0
.type
= BRW_REGISTER_TYPE_F
;
141 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, src_reg(1.0f
)));
142 v
->emit(v
->MOV(m0
, src_reg(temp
)));
144 register_coalesce(v
);
146 EXPECT_EQ(mul
->dst
.file
, MRF
);
150 TEST_F(register_coalesce_test
, test_multiple_use
)
152 src_reg something
= src_reg(v
, glsl_type::float_type
);
153 dst_reg temp
= dst_reg(v
, glsl_type::vec4_type
);
156 dst_reg m0
= dst_reg(MRF
, 0);
157 m0
.writemask
= WRITEMASK_X
;
158 m0
.type
= BRW_REGISTER_TYPE_F
;
160 dst_reg m1
= dst_reg(MRF
, 1);
161 m1
.writemask
= WRITEMASK_XYZW
;
162 m1
.type
= BRW_REGISTER_TYPE_F
;
164 src_reg src
= src_reg(temp
);
165 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, src_reg(1.0f
)));
166 src
.swizzle
= BRW_SWIZZLE_XXXX
;
167 v
->emit(v
->MOV(m0
, src
));
168 src
.swizzle
= BRW_SWIZZLE_XYZW
;
169 v
->emit(v
->MOV(m1
, src
));
171 register_coalesce(v
);
173 EXPECT_NE(mul
->dst
.file
, MRF
);
176 TEST_F(register_coalesce_test
, test_dp4_mrf
)
178 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
179 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
182 dst_reg m0
= dst_reg(MRF
, 0);
183 m0
.writemask
= WRITEMASK_Y
;
184 m0
.type
= BRW_REGISTER_TYPE_F
;
186 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
188 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
189 v
->emit(v
->MOV(m0
, src_reg(temp
)));
191 register_coalesce(v
);
193 EXPECT_EQ(dp4
->dst
.file
, MRF
);
194 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
197 TEST_F(register_coalesce_test
, test_dp4_grf
)
199 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
200 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
203 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
204 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
206 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
207 to
.writemask
= WRITEMASK_Y
;
208 v
->emit(v
->MOV(to
, src_reg(temp
)));
210 /* if we don't do something with the result, the automatic dead code
211 * elimination will remove all our instructions.
213 src_reg src
= src_reg(to
);
215 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
217 register_coalesce(v
);
219 EXPECT_EQ(dp4
->dst
.reg
, to
.reg
);
220 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
223 TEST_F(register_coalesce_test
, test_channel_mul_grf
)
225 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
226 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
229 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
230 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
232 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, some_src_1
, some_src_2
));
233 to
.writemask
= WRITEMASK_Y
;
234 v
->emit(v
->MOV(to
, src_reg(temp
)));
236 /* if we don't do something with the result, the automatic dead code
237 * elimination will remove all our instructions.
239 src_reg src
= src_reg(to
);
241 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
243 register_coalesce(v
);
245 /* This path isn't supported yet in the reswizzling code, so we're checking
246 * that we haven't done anything bad to scalar non-DP[234]s.
248 EXPECT_NE(mul
->dst
.reg
, to
.reg
);