1 /*
2 * Copyright © 2018 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 "elk_vec4.h"
26
27 using namespace elk;
28
29 class dead_code_eliminate_vec4_test : public ::testing::Test {
30 virtual void SetUp();
31 virtual void TearDown();
32
33 public:
34 struct elk_compiler *compiler;
35 struct elk_compile_params params;
36 struct intel_device_info *devinfo;
37 void *ctx;
38 struct gl_shader_program *shader_prog;
39 struct elk_vue_prog_data *prog_data;
40 vec4_visitor *v;
41 };
42
43 class dead_code_eliminate_vec4_visitor : public vec4_visitor
44 {
45 public:
dead_code_eliminate_vec4_visitor(struct elk_compiler * compiler,struct elk_compile_params * params,nir_shader * shader,struct elk_vue_prog_data * prog_data)46 dead_code_eliminate_vec4_visitor(struct elk_compiler *compiler,
47 struct elk_compile_params *params,
48 nir_shader *shader,
49 struct elk_vue_prog_data *prog_data)
50 : vec4_visitor(compiler, params, NULL, prog_data, shader,
51 false /* no_spills */, false)
52 {
53 prog_data->dispatch_mode = INTEL_DISPATCH_MODE_4X2_DUAL_OBJECT;
54 }
55
56 protected:
make_reg_for_system_value(int)57 virtual dst_reg *make_reg_for_system_value(int /* location */)
58 {
59 unreachable("Not reached");
60 }
61
setup_payload()62 virtual void setup_payload()
63 {
64 unreachable("Not reached");
65 }
66
emit_prolog()67 virtual void emit_prolog()
68 {
69 unreachable("Not reached");
70 }
71
emit_thread_end()72 virtual void emit_thread_end()
73 {
74 unreachable("Not reached");
75 }
76
emit_urb_write_header(int)77 virtual void emit_urb_write_header(int /* mrf */)
78 {
79 unreachable("Not reached");
80 }
81
emit_urb_write_opcode(bool)82 virtual vec4_instruction *emit_urb_write_opcode(bool /* complete */)
83 {
84 unreachable("Not reached");
85 }
86 };
87
88
SetUp()89 void dead_code_eliminate_vec4_test::SetUp()
90 {
91 ctx = ralloc_context(NULL);
92 compiler = rzalloc(ctx, struct elk_compiler);
93 devinfo = rzalloc(ctx, struct intel_device_info);
94 compiler->devinfo = devinfo;
95
96 params = {};
97 params.mem_ctx = ctx;
98
99 prog_data = ralloc(ctx, struct elk_vue_prog_data);
100 nir_shader *shader =
101 nir_shader_create(ctx, MESA_SHADER_VERTEX, NULL, NULL);
102
103 v = new dead_code_eliminate_vec4_visitor(compiler, ¶ms, shader, prog_data);
104
105 devinfo->ver = 4;
106 devinfo->verx10 = devinfo->ver * 10;
107 }
108
TearDown()109 void dead_code_eliminate_vec4_test::TearDown()
110 {
111 delete v;
112 v = NULL;
113
114 ralloc_free(ctx);
115 ctx = NULL;
116 }
117
118 static void
dead_code_eliminate(vec4_visitor * v)119 dead_code_eliminate(vec4_visitor *v)
120 {
121 const bool print = getenv("TEST_DEBUG");
122
123 if (print) {
124 fprintf(stderr, "instructions before:\n");
125 v->dump_instructions();
126 }
127
128 v->calculate_cfg();
129 v->dead_code_eliminate();
130
131 if (print) {
132 fprintf(stderr, "instructions after:\n");
133 v->dump_instructions();
134 }
135 }
136
TEST_F(dead_code_eliminate_vec4_test,some_dead_channels_all_flags_used)137 TEST_F(dead_code_eliminate_vec4_test, some_dead_channels_all_flags_used)
138 {
139 const vec4_builder bld = vec4_builder(v).at_end();
140 src_reg r1 = src_reg(v, glsl_vec4_type());
141 src_reg r2 = src_reg(v, glsl_vec4_type());
142 src_reg r3 = src_reg(v, glsl_vec4_type());
143 src_reg r4 = src_reg(v, glsl_vec4_type());
144 src_reg r5 = src_reg(v, glsl_vec4_type());
145 src_reg r6 = src_reg(v, glsl_vec4_type());
146
147 /* Sequence like the following should not be modified by DCE.
148 *
149 * cmp.l.f0(8) g4<1>F g2<4,4,1>.wF g1<4,4,1>.xF
150 * mov(8) g5<1>.xF g4<4,4,1>.xF
151 * (+f0.x) sel(8) g6<1>UD g3<4>UD g6<4>UD
152 */
153 vec4_instruction *test_cmp =
154 bld.CMP(dst_reg(r4), r2, r1, ELK_CONDITIONAL_L);
155
156 test_cmp->src[0].swizzle = ELK_SWIZZLE_WWWW;
157 test_cmp->src[1].swizzle = ELK_SWIZZLE_XXXX;
158
159 vec4_instruction *test_mov =
160 bld.MOV(dst_reg(r5), r4);
161
162 test_mov->dst.writemask = WRITEMASK_X;
163 test_mov->src[0].swizzle = ELK_SWIZZLE_XXXX;
164
165 vec4_instruction *test_sel =
166 bld.SEL(dst_reg(r6), r3, r6);
167
168 set_predicate(ELK_PREDICATE_NORMAL, test_sel);
169
170 /* The scratch write is here just to make r5 and r6 be live so that the
171 * whole program doesn't get eliminated by DCE.
172 */
173 v->emit(v->SCRATCH_WRITE(dst_reg(r4), r6, r5));
174
175 dead_code_eliminate(v);
176
177 EXPECT_EQ(test_cmp->dst.writemask, WRITEMASK_XYZW);
178 }
179