2 * Copyright © 2014 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 class copy_propagation_test
: public ::testing::Test
{
37 struct brw_compiler
*compiler
;
38 struct gen_device_info
*devinfo
;
39 struct gl_context
*ctx
;
40 struct gl_shader_program
*shader_prog
;
41 struct brw_vue_prog_data
*prog_data
;
45 class copy_propagation_vec4_visitor
: public vec4_visitor
48 copy_propagation_vec4_visitor(struct brw_compiler
*compiler
,
50 struct brw_vue_prog_data
*prog_data
)
51 : vec4_visitor(compiler
, NULL
, NULL
, prog_data
, shader
, NULL
,
52 false /* no_spills */, -1)
54 prog_data
->dispatch_mode
= DISPATCH_MODE_4X2_DUAL_OBJECT
;
58 virtual dst_reg
*make_reg_for_system_value(int location
)
60 unreachable("Not reached");
63 virtual void setup_payload()
65 unreachable("Not reached");
68 virtual void emit_prolog()
70 unreachable("Not reached");
73 virtual void emit_thread_end()
75 unreachable("Not reached");
78 virtual void emit_urb_write_header(int mrf
)
80 unreachable("Not reached");
83 virtual vec4_instruction
*emit_urb_write_opcode(bool complete
)
85 unreachable("Not reached");
90 void copy_propagation_test::SetUp()
92 ctx
= (struct gl_context
*)calloc(1, sizeof(*ctx
));
93 compiler
= (struct brw_compiler
*)calloc(1, sizeof(*compiler
));
94 devinfo
= (struct gen_device_info
*)calloc(1, sizeof(*devinfo
));
95 prog_data
= (struct brw_vue_prog_data
*)calloc(1, sizeof(*prog_data
));
96 compiler
->devinfo
= devinfo
;
98 nir_shader
*shader
= nir_shader_create(NULL
, MESA_SHADER_VERTEX
, NULL
);
100 v
= new copy_propagation_vec4_visitor(compiler
, shader
, prog_data
);
106 copy_propagation(vec4_visitor
*v
)
111 fprintf(stderr
, "instructions before:\n");
112 v
->dump_instructions();
116 v
->opt_copy_propagation();
119 fprintf(stderr
, "instructions after:\n");
120 v
->dump_instructions();
124 TEST_F(copy_propagation_test
, test_swizzle_swizzle
)
126 dst_reg a
= dst_reg(v
, glsl_type::vec4_type
);
127 dst_reg b
= dst_reg(v
, glsl_type::vec4_type
);
128 dst_reg c
= dst_reg(v
, glsl_type::vec4_type
);
130 v
->emit(v
->ADD(a
, src_reg(a
), src_reg(a
)));
132 v
->emit(v
->MOV(b
, swizzle(src_reg(a
), BRW_SWIZZLE4(SWIZZLE_Y
,
137 vec4_instruction
*test_mov
=
138 v
->MOV(c
, swizzle(src_reg(b
), BRW_SWIZZLE4(SWIZZLE_Y
,
146 EXPECT_EQ(test_mov
->src
[0].nr
, a
.nr
);
147 EXPECT_EQ(test_mov
->src
[0].swizzle
, BRW_SWIZZLE4(SWIZZLE_Z
,
153 TEST_F(copy_propagation_test
, test_swizzle_writemask
)
155 dst_reg a
= dst_reg(v
, glsl_type::vec4_type
);
156 dst_reg b
= dst_reg(v
, glsl_type::vec4_type
);
157 dst_reg c
= dst_reg(v
, glsl_type::vec4_type
);
159 v
->emit(v
->MOV(b
, swizzle(src_reg(a
), BRW_SWIZZLE4(SWIZZLE_X
,
164 v
->emit(v
->MOV(writemask(a
, WRITEMASK_XYZ
), brw_imm_f(1.0f
)));
166 vec4_instruction
*test_mov
=
167 v
->MOV(c
, swizzle(src_reg(b
), BRW_SWIZZLE4(SWIZZLE_W
,
175 /* should not copy propagate */
176 EXPECT_EQ(test_mov
->src
[0].nr
, b
.nr
);
177 EXPECT_EQ(test_mov
->src
[0].swizzle
, BRW_SWIZZLE4(SWIZZLE_W
,