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>
27 #include "program/program.h"
33 #define register_coalesce(v) _register_coalesce(v, __func__)
35 class register_coalesce_test
: public ::testing::Test
{
39 struct brw_compiler
*compiler
;
40 struct brw_device_info
*devinfo
;
41 struct gl_context
*ctx
;
42 struct gl_shader_program
*shader_prog
;
43 struct brw_vertex_program
*vp
;
44 struct brw_vue_prog_data
*prog_data
;
49 class register_coalesce_vec4_visitor
: public vec4_visitor
52 register_coalesce_vec4_visitor(struct brw_compiler
*compiler
,
54 struct brw_vue_prog_data
*prog_data
)
55 : vec4_visitor(compiler
, NULL
, NULL
, prog_data
, shader
, NULL
,
56 false /* no_spills */, -1)
58 prog_data
->dispatch_mode
= DISPATCH_MODE_4X2_DUAL_OBJECT
;
62 virtual dst_reg
*make_reg_for_system_value(int location
)
64 unreachable("Not reached");
67 virtual void setup_payload()
69 unreachable("Not reached");
72 virtual void emit_prolog()
74 unreachable("Not reached");
77 virtual void emit_thread_end()
79 unreachable("Not reached");
82 virtual void emit_urb_write_header(int mrf
)
84 unreachable("Not reached");
87 virtual vec4_instruction
*emit_urb_write_opcode(bool complete
)
89 unreachable("Not reached");
94 void register_coalesce_test::SetUp()
96 ctx
= (struct gl_context
*)calloc(1, sizeof(*ctx
));
97 compiler
= (struct brw_compiler
*)calloc(1, sizeof(*compiler
));
98 devinfo
= (struct brw_device_info
*)calloc(1, sizeof(*devinfo
));
99 prog_data
= (struct brw_vue_prog_data
*)calloc(1, sizeof(*prog_data
));
100 compiler
->devinfo
= devinfo
;
102 vp
= ralloc(NULL
, struct brw_vertex_program
);
104 nir_shader
*shader
= nir_shader_create(NULL
, MESA_SHADER_VERTEX
, NULL
);
106 v
= new register_coalesce_vec4_visitor(compiler
, shader
, prog_data
);
108 _mesa_init_gl_program(&vp
->program
.Base
, GL_VERTEX_SHADER
, 0);
114 _register_coalesce(vec4_visitor
*v
, const char *func
)
119 printf("%s: instructions before:\n", func
);
120 v
->dump_instructions();
124 v
->opt_register_coalesce();
127 printf("%s: instructions after:\n", func
);
128 v
->dump_instructions();
132 TEST_F(register_coalesce_test
, test_compute_to_mrf
)
134 src_reg something
= src_reg(v
, glsl_type::float_type
);
135 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
138 dst_reg m0
= dst_reg(MRF
, 0);
139 m0
.writemask
= WRITEMASK_X
;
140 m0
.type
= BRW_REGISTER_TYPE_F
;
142 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, brw_imm_f(1.0f
)));
143 v
->emit(v
->MOV(m0
, src_reg(temp
)));
145 register_coalesce(v
);
147 EXPECT_EQ(mul
->dst
.file
, MRF
);
151 TEST_F(register_coalesce_test
, test_multiple_use
)
153 src_reg something
= src_reg(v
, glsl_type::float_type
);
154 dst_reg temp
= dst_reg(v
, glsl_type::vec4_type
);
157 dst_reg m0
= dst_reg(MRF
, 0);
158 m0
.writemask
= WRITEMASK_X
;
159 m0
.type
= BRW_REGISTER_TYPE_F
;
161 dst_reg m1
= dst_reg(MRF
, 1);
162 m1
.writemask
= WRITEMASK_XYZW
;
163 m1
.type
= BRW_REGISTER_TYPE_F
;
165 src_reg src
= src_reg(temp
);
166 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, something
, brw_imm_f(1.0f
)));
167 src
.swizzle
= BRW_SWIZZLE_XXXX
;
168 v
->emit(v
->MOV(m0
, src
));
169 src
.swizzle
= BRW_SWIZZLE_XYZW
;
170 v
->emit(v
->MOV(m1
, src
));
172 register_coalesce(v
);
174 EXPECT_NE(mul
->dst
.file
, MRF
);
177 TEST_F(register_coalesce_test
, test_dp4_mrf
)
179 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
180 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
183 dst_reg m0
= dst_reg(MRF
, 0);
184 m0
.writemask
= WRITEMASK_Y
;
185 m0
.type
= BRW_REGISTER_TYPE_F
;
187 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
189 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
190 v
->emit(v
->MOV(m0
, src_reg(temp
)));
192 register_coalesce(v
);
194 EXPECT_EQ(dp4
->dst
.file
, MRF
);
195 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
198 TEST_F(register_coalesce_test
, test_dp4_grf
)
200 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
201 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
204 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
205 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
207 vec4_instruction
*dp4
= v
->emit(v
->DP4(temp
, some_src_1
, some_src_2
));
208 to
.writemask
= WRITEMASK_Y
;
209 v
->emit(v
->MOV(to
, src_reg(temp
)));
211 /* if we don't do something with the result, the automatic dead code
212 * elimination will remove all our instructions.
214 src_reg src
= src_reg(to
);
216 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
218 register_coalesce(v
);
220 EXPECT_EQ(dp4
->dst
.nr
, to
.nr
);
221 EXPECT_EQ(dp4
->dst
.writemask
, WRITEMASK_Y
);
224 TEST_F(register_coalesce_test
, test_channel_mul_grf
)
226 src_reg some_src_1
= src_reg(v
, glsl_type::vec4_type
);
227 src_reg some_src_2
= src_reg(v
, glsl_type::vec4_type
);
230 dst_reg to
= dst_reg(v
, glsl_type::vec4_type
);
231 dst_reg temp
= dst_reg(v
, glsl_type::float_type
);
233 vec4_instruction
*mul
= v
->emit(v
->MUL(temp
, some_src_1
, some_src_2
));
234 to
.writemask
= WRITEMASK_Y
;
235 v
->emit(v
->MOV(to
, src_reg(temp
)));
237 /* if we don't do something with the result, the automatic dead code
238 * elimination will remove all our instructions.
240 src_reg src
= src_reg(to
);
242 v
->emit(v
->MOV(dst_reg(MRF
, 0), src
));
244 register_coalesce(v
);
246 EXPECT_EQ(mul
->dst
.nr
, to
.nr
);