2 * Copyright © 2018 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>
26 #include "program/program.h"
30 class dead_code_eliminate_test
: public ::testing::Test
{
34 struct brw_compiler
*compiler
;
35 struct gen_device_info
*devinfo
;
36 struct gl_context
*ctx
;
37 struct gl_shader_program
*shader_prog
;
38 struct brw_vue_prog_data
*prog_data
;
42 class dead_code_eliminate_vec4_visitor
: public vec4_visitor
45 dead_code_eliminate_vec4_visitor(struct brw_compiler
*compiler
,
47 struct brw_vue_prog_data
*prog_data
)
48 : vec4_visitor(compiler
, NULL
, NULL
, prog_data
, shader
, NULL
,
49 false /* no_spills */, -1)
51 prog_data
->dispatch_mode
= DISPATCH_MODE_4X2_DUAL_OBJECT
;
55 virtual dst_reg
*make_reg_for_system_value(int /* location */)
57 unreachable("Not reached");
60 virtual void setup_payload()
62 unreachable("Not reached");
65 virtual void emit_prolog()
67 unreachable("Not reached");
70 virtual void emit_thread_end()
72 unreachable("Not reached");
75 virtual void emit_urb_write_header(int /* mrf */)
77 unreachable("Not reached");
80 virtual vec4_instruction
*emit_urb_write_opcode(bool /* complete */)
82 unreachable("Not reached");
87 void dead_code_eliminate_test::SetUp()
89 ctx
= (struct gl_context
*)calloc(1, sizeof(*ctx
));
90 compiler
= (struct brw_compiler
*)calloc(1, sizeof(*compiler
));
91 devinfo
= (struct gen_device_info
*)calloc(1, sizeof(*devinfo
));
92 prog_data
= (struct brw_vue_prog_data
*)calloc(1, sizeof(*prog_data
));
93 compiler
->devinfo
= devinfo
;
96 nir_shader_create(NULL
, MESA_SHADER_VERTEX
, NULL
, NULL
);
98 v
= new dead_code_eliminate_vec4_visitor(compiler
, shader
, prog_data
);
104 dead_code_eliminate(vec4_visitor
*v
)
109 fprintf(stderr
, "instructions before:\n");
110 v
->dump_instructions();
114 v
->dead_code_eliminate();
117 fprintf(stderr
, "instructions after:\n");
118 v
->dump_instructions();
122 TEST_F(dead_code_eliminate_test
, some_dead_channels_all_flags_used
)
124 const vec4_builder bld
= vec4_builder(v
).at_end();
125 src_reg r1
= src_reg(v
, glsl_type::vec4_type
);
126 src_reg r2
= src_reg(v
, glsl_type::vec4_type
);
127 src_reg r3
= src_reg(v
, glsl_type::vec4_type
);
128 src_reg r4
= src_reg(v
, glsl_type::vec4_type
);
129 src_reg r5
= src_reg(v
, glsl_type::vec4_type
);
130 src_reg r6
= src_reg(v
, glsl_type::vec4_type
);
132 /* Sequence like the following should not be modified by DCE.
134 * cmp.l.f0(8) g4<1>F g2<4,4,1>.wF g1<4,4,1>.xF
135 * mov(8) g5<1>.xF g4<4,4,1>.xF
136 * (+f0.x) sel(8) g6<1>UD g3<4>UD g6<4>UD
138 vec4_instruction
*test_cmp
=
139 bld
.CMP(dst_reg(r4
), r2
, r1
, BRW_CONDITIONAL_L
);
141 test_cmp
->src
[0].swizzle
= BRW_SWIZZLE_WWWW
;
142 test_cmp
->src
[1].swizzle
= BRW_SWIZZLE_XXXX
;
144 vec4_instruction
*test_mov
=
145 bld
.MOV(dst_reg(r5
), r4
);
147 test_mov
->dst
.writemask
= WRITEMASK_X
;
148 test_mov
->src
[0].swizzle
= BRW_SWIZZLE_XXXX
;
150 vec4_instruction
*test_sel
=
151 bld
.SEL(dst_reg(r6
), r3
, r6
);
153 set_predicate(BRW_PREDICATE_NORMAL
, test_sel
);
155 /* The scratch write is here just to make r5 and r6 be live so that the
156 * whole program doesn't get eliminated by DCE.
158 v
->emit(v
->SCRATCH_WRITE(dst_reg(r4
), r6
, r5
));
160 dead_code_eliminate(v
);
162 EXPECT_EQ(test_cmp
->dst
.writemask
, WRITEMASK_XYZW
);