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, __func__)
34 class register_coalesce_test
: public ::testing::Test
{
38 struct brw_compiler
*compiler
;
39 struct brw_device_info
*devinfo
;
40 struct gl_context
*ctx
;
41 struct gl_shader_program
*shader_prog
;
42 struct brw_vertex_program
*vp
;
47 class register_coalesce_vec4_visitor
: public vec4_visitor
50 register_coalesce_vec4_visitor(struct brw_compiler
*compiler
,
51 struct gl_shader_program
*shader_prog
)
52 : vec4_visitor(compiler
, NULL
, NULL
, NULL
, NULL
, shader_prog
,
53 MESA_SHADER_VERTEX
, NULL
,
54 false /* no_spills */, -1)
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 ctx
= (struct gl_context
*)calloc(1, sizeof(*ctx
));
99 compiler
= (struct brw_compiler
*)calloc(1, sizeof(*compiler
));
100 devinfo
= (struct brw_device_info
*)calloc(1, sizeof(*devinfo
));
101 compiler
->devinfo
= devinfo
;
103 vp
= ralloc(NULL
, struct brw_vertex_program
);
105 shader_prog
= ralloc(NULL
, struct gl_shader_program
);
107 v
= new register_coalesce_vec4_visitor(compiler
, shader_prog
);
109 _mesa_init_vertex_program(ctx
, &vp
->program
, GL_VERTEX_SHADER
, 0);
115 _register_coalesce(vec4_visitor
*v
, const char *func
)
120 printf("%s: instructions before:\n", func
);
121 v
->dump_instructions();
125 v
->opt_register_coalesce();
128 printf("%s: instructions after:\n", func
);
129 v
->dump_instructions();
133 TEST_F(register_coalesce_test
, test_compute_to_mrf
)
135 src_reg something
= src_reg(v
, glsl_type::float_type
);
136 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
139 dst_reg m0
= dst_reg(MRF
, 0);
140 m0
.writemask
= WRITEMASK_X
;
141 m0
.type
= BRW_REGISTER_TYPE_F
;
143 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, src_reg(1.0f
)));
144 v
->emit(v
->MOV(m0
, src_reg(temp
)));
146 register_coalesce(v
);
148 EXPECT_EQ(mul
->dst
.file
, MRF
);
152 TEST_F(register_coalesce_test
, test_multiple_use
)
154 src_reg something
= src_reg(v
, glsl_type::float_type
);
155 dst_reg temp
= dst_reg(v
, glsl_type::vec4_type
);
158 dst_reg m0
= dst_reg(MRF
, 0);
159 m0
.writemask
= WRITEMASK_X
;
160 m0
.type
= BRW_REGISTER_TYPE_F
;
162 dst_reg m1
= dst_reg(MRF
, 1);
163 m1
.writemask
= WRITEMASK_XYZW
;
164 m1
.type
= BRW_REGISTER_TYPE_F
;
166 src_reg src
= src_reg(temp
);
167 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, src_reg(1.0f
)));
168 src
.swizzle
= BRW_SWIZZLE_XXXX
;
169 v
->emit(v
->MOV(m0
, src
));
170 src
.swizzle
= BRW_SWIZZLE_XYZW
;
171 v
->emit(v
->MOV(m1
, src
));
173 register_coalesce(v
);
175 EXPECT_NE(mul
->dst
.file
, MRF
);
178 TEST_F(register_coalesce_test
, test_dp4_mrf
)
180 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
181 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
184 dst_reg m0
= dst_reg(MRF
, 0);
185 m0
.writemask
= WRITEMASK_Y
;
186 m0
.type
= BRW_REGISTER_TYPE_F
;
188 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
190 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
191 v
->emit(v
->MOV(m0
, src_reg(temp
)));
193 register_coalesce(v
);
195 EXPECT_EQ(dp4
->dst
.file
, MRF
);
196 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
199 TEST_F(register_coalesce_test
, test_dp4_grf
)
201 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
202 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
205 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
206 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
208 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
209 to
.writemask
= WRITEMASK_Y
;
210 v
->emit(v
->MOV(to
, src_reg(temp
)));
212 /* if we don't do something with the result, the automatic dead code
213 * elimination will remove all our instructions.
215 src_reg src
= src_reg(to
);
217 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
219 register_coalesce(v
);
221 EXPECT_EQ(dp4
->dst
.reg
, to
.reg
);
222 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
225 TEST_F(register_coalesce_test
, test_channel_mul_grf
)
227 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
228 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
231 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
232 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
234 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, some_src_1
, some_src_2
));
235 to
.writemask
= WRITEMASK_Y
;
236 v
->emit(v
->MOV(to
, src_reg(temp
)));
238 /* if we don't do something with the result, the automatic dead code
239 * elimination will remove all our instructions.
241 src_reg src
= src_reg(to
);
243 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
245 register_coalesce(v
);
247 EXPECT_EQ(mul
->dst
.reg
, to
.reg
);