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
,
52 MESA_SHADER_VERTEX
, NULL
,
53 false, false /* no_spills */,
54 ST_NONE
, ST_NONE
, ST_NONE
)
59 virtual dst_reg
*make_reg_for_system_value(ir_variable
*ir
)
61 unreachable("Not reached");
64 virtual void setup_payload()
66 unreachable("Not reached");
69 virtual void emit_prolog()
71 unreachable("Not reached");
74 virtual void emit_program_code()
76 unreachable("Not reached");
79 virtual void emit_thread_end()
81 unreachable("Not reached");
84 virtual void emit_urb_write_header(int mrf
)
86 unreachable("Not reached");
89 virtual vec4_instruction
*emit_urb_write_opcode(bool complete
)
91 unreachable("Not reached");
96 void register_coalesce_test::SetUp()
98 brw
= (struct brw_context
*)calloc(1, sizeof(*brw
));
101 vp
= ralloc(NULL
, struct brw_vertex_program
);
103 shader_prog
= ralloc(NULL
, struct gl_shader_program
);
105 v
= new register_coalesce_vec4_visitor(brw
, shader_prog
);
107 _mesa_init_vertex_program(ctx
, &vp
->program
, GL_VERTEX_SHADER
, 0);
113 _register_coalesce(vec4_visitor
*v
, const char *func
)
118 printf("%s: instructions before:\n", func
);
119 v
->dump_instructions();
122 v
->opt_register_coalesce();
125 printf("%s: instructions after:\n", func
);
126 v
->dump_instructions();
130 TEST_F(register_coalesce_test
, test_compute_to_mrf
)
132 src_reg something
= src_reg(v
, glsl_type::float_type
);
133 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
136 dst_reg m0
= dst_reg(MRF
, 0);
137 m0
.writemask
= WRITEMASK_X
;
138 m0
.type
= BRW_REGISTER_TYPE_F
;
140 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, src_reg(1.0f
)));
141 v
->emit(v
->MOV(m0
, src_reg(temp
)));
143 register_coalesce(v
);
145 EXPECT_EQ(mul
->dst
.file
, MRF
);
149 TEST_F(register_coalesce_test
, test_multiple_use
)
151 src_reg something
= src_reg(v
, glsl_type::float_type
);
152 dst_reg temp
= dst_reg(v
, glsl_type::vec4_type
);
155 dst_reg m0
= dst_reg(MRF
, 0);
156 m0
.writemask
= WRITEMASK_X
;
157 m0
.type
= BRW_REGISTER_TYPE_F
;
159 dst_reg m1
= dst_reg(MRF
, 1);
160 m1
.writemask
= WRITEMASK_XYZW
;
161 m1
.type
= BRW_REGISTER_TYPE_F
;
163 src_reg src
= src_reg(temp
);
164 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, src_reg(1.0f
)));
165 src
.swizzle
= BRW_SWIZZLE_XXXX
;
166 v
->emit(v
->MOV(m0
, src
));
167 src
.swizzle
= BRW_SWIZZLE_XYZW
;
168 v
->emit(v
->MOV(m1
, src
));
170 register_coalesce(v
);
172 EXPECT_NE(mul
->dst
.file
, MRF
);
175 TEST_F(register_coalesce_test
, test_dp4_mrf
)
177 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
178 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
181 dst_reg m0
= dst_reg(MRF
, 0);
182 m0
.writemask
= WRITEMASK_Y
;
183 m0
.type
= BRW_REGISTER_TYPE_F
;
185 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
187 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
188 v
->emit(v
->MOV(m0
, src_reg(temp
)));
190 register_coalesce(v
);
192 EXPECT_EQ(dp4
->dst
.file
, MRF
);
193 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
196 TEST_F(register_coalesce_test
, test_dp4_grf
)
198 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
199 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
202 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
203 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
205 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
206 to
.writemask
= WRITEMASK_Y
;
207 v
->emit(v
->MOV(to
, src_reg(temp
)));
209 /* if we don't do something with the result, the automatic dead code
210 * elimination will remove all our instructions.
212 src_reg src
= src_reg(to
);
214 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
216 register_coalesce(v
);
218 EXPECT_EQ(dp4
->dst
.reg
, to
.reg
);
219 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
222 TEST_F(register_coalesce_test
, test_channel_mul_grf
)
224 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
225 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
228 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
229 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
231 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, some_src_1
, some_src_2
));
232 to
.writemask
= WRITEMASK_Y
;
233 v
->emit(v
->MOV(to
, src_reg(temp
)));
235 /* if we don't do something with the result, the automatic dead code
236 * elimination will remove all our instructions.
238 src_reg src
= src_reg(to
);
240 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
242 register_coalesce(v
);
244 /* This path isn't supported yet in the reswizzling code, so we're checking
245 * that we haven't done anything bad to scalar non-DP[234]s.
247 EXPECT_NE(mul
->dst
.reg
, to
.reg
);