Merge remote-tracking branch 'origin/master' into vulkan
[mesa.git] / src / mesa / drivers / dri / i965 / test_vec4_copy_propagation.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <gtest/gtest.h>
25 #include "brw_vec4.h"
26 #include "brw_vs.h"
27 #include "program/program.h"
28
29 using namespace brw;
30
31 int ret = 0;
32
33 class copy_propagation_test : public ::testing::Test {
34 virtual void SetUp();
35
36 public:
37 struct brw_compiler *compiler;
38 struct brw_device_info *devinfo;
39 struct gl_context *ctx;
40 struct gl_shader_program *shader_prog;
41 struct brw_vertex_program *vp;
42 struct brw_vue_prog_data *prog_data;
43 vec4_visitor *v;
44 };
45
46 class copy_propagation_vec4_visitor : public vec4_visitor
47 {
48 public:
49 copy_propagation_vec4_visitor(struct brw_compiler *compiler,
50 nir_shader *shader,
51 struct brw_vue_prog_data *prog_data)
52 : vec4_visitor(compiler, NULL, NULL, prog_data, shader, NULL,
53 false /* no_spills */, -1)
54 {
55 prog_data->dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
56 }
57
58 protected:
59 virtual dst_reg *make_reg_for_system_value(int location,
60 const glsl_type *type)
61 {
62 unreachable("Not reached");
63 }
64
65 virtual void setup_payload()
66 {
67 unreachable("Not reached");
68 }
69
70 virtual void emit_prolog()
71 {
72 unreachable("Not reached");
73 }
74
75 virtual void emit_thread_end()
76 {
77 unreachable("Not reached");
78 }
79
80 virtual void emit_urb_write_header(int mrf)
81 {
82 unreachable("Not reached");
83 }
84
85 virtual vec4_instruction *emit_urb_write_opcode(bool complete)
86 {
87 unreachable("Not reached");
88 }
89 };
90
91
92 void copy_propagation_test::SetUp()
93 {
94 ctx = (struct gl_context *)calloc(1, sizeof(*ctx));
95 compiler = (struct brw_compiler *)calloc(1, sizeof(*compiler));
96 devinfo = (struct brw_device_info *)calloc(1, sizeof(*devinfo));
97 prog_data = (struct brw_vue_prog_data *)calloc(1, sizeof(*prog_data));
98 compiler->devinfo = devinfo;
99
100 vp = ralloc(NULL, struct brw_vertex_program);
101
102 nir_shader *shader = nir_shader_create(NULL, MESA_SHADER_VERTEX, NULL);
103
104 v = new copy_propagation_vec4_visitor(compiler, shader, prog_data);
105
106 _mesa_init_gl_program(&vp->program.Base, GL_VERTEX_SHADER, 0);
107
108 devinfo->gen = 4;
109 }
110
111 static void
112 copy_propagation(vec4_visitor *v)
113 {
114 bool print = false;
115
116 if (print) {
117 fprintf(stderr, "instructions before:\n");
118 v->dump_instructions();
119 }
120
121 v->calculate_cfg();
122 v->opt_copy_propagation();
123
124 if (print) {
125 fprintf(stderr, "instructions after:\n");
126 v->dump_instructions();
127 }
128 }
129
130 TEST_F(copy_propagation_test, test_swizzle_swizzle)
131 {
132 dst_reg a = dst_reg(v, glsl_type::vec4_type);
133 dst_reg b = dst_reg(v, glsl_type::vec4_type);
134 dst_reg c = dst_reg(v, glsl_type::vec4_type);
135
136 v->emit(v->ADD(a, src_reg(a), src_reg(a)));
137
138 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_Y,
139 SWIZZLE_Z,
140 SWIZZLE_W,
141 SWIZZLE_X))));
142
143 vec4_instruction *test_mov =
144 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_Y,
145 SWIZZLE_Z,
146 SWIZZLE_W,
147 SWIZZLE_X)));
148 v->emit(test_mov);
149
150 copy_propagation(v);
151
152 EXPECT_EQ(test_mov->src[0].nr, a.nr);
153 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_Z,
154 SWIZZLE_W,
155 SWIZZLE_X,
156 SWIZZLE_Y));
157 }
158
159 TEST_F(copy_propagation_test, test_swizzle_writemask)
160 {
161 dst_reg a = dst_reg(v, glsl_type::vec4_type);
162 dst_reg b = dst_reg(v, glsl_type::vec4_type);
163 dst_reg c = dst_reg(v, glsl_type::vec4_type);
164
165 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_X,
166 SWIZZLE_Y,
167 SWIZZLE_X,
168 SWIZZLE_Z))));
169
170 v->emit(v->MOV(writemask(a, WRITEMASK_XYZ), brw_imm_f(1.0f)));
171
172 vec4_instruction *test_mov =
173 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_W,
174 SWIZZLE_W,
175 SWIZZLE_W,
176 SWIZZLE_W)));
177 v->emit(test_mov);
178
179 copy_propagation(v);
180
181 /* should not copy propagate */
182 EXPECT_EQ(test_mov->src[0].nr, b.nr);
183 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_W,
184 SWIZZLE_W,
185 SWIZZLE_W,
186 SWIZZLE_W));
187 }