1
2 #include "sfn_test_shaders.h"
3 #include "../sfn_shader.h"
4 #include "../sfn_optimizer.h"
5 #include "../sfn_ra.h"
6 #include "../sfn_scheduler.h"
7
8 using namespace r600;
9 using std::ostringstream;
10
11 class TestShaderFromNir : public TestShader {
12
13 protected:
14
15 void check(Shader *s, const char *expect_str);
16 void ra_check(Shader *s, const char *expect_str);
17 };
18
19
TEST_F(TestShaderFromNir,SimpleDCE)20 TEST_F(TestShaderFromNir, SimpleDCE)
21 {
22 auto sh = from_string(red_triangle_fs_expect_from_nir);
23 dead_code_elimination(*sh);
24
25 check(sh, red_triangle_fs_expect_from_nir_dce);
26 }
27
28
TEST_F(TestShaderFromNir,CopyPropagationForwardBackward)29 TEST_F(TestShaderFromNir, CopyPropagationForwardBackward)
30 {
31 auto sh = from_string(add_add_1_expect_from_nir);
32 copy_propagation_fwd(*sh);
33 check(sh, add_add_1_expect_from_nir_copy_prop_fwd);
34 }
35
TEST_F(TestShaderFromNir,CopyPropagationForwardDCE)36 TEST_F(TestShaderFromNir, CopyPropagationForwardDCE)
37 {
38 auto sh = from_string(add_add_1_expect_from_nir);
39 copy_propagation_fwd(*sh);
40 dead_code_elimination(*sh);
41 check(sh, add_add_1_expect_from_nir_copy_prop_fwd_dce);
42 }
43
TEST_F(TestShaderFromNir,CopyPropagationBackwardDCE)44 TEST_F(TestShaderFromNir, CopyPropagationBackwardDCE)
45 {
46 auto sh = from_string(add_add_1_expect_from_nir_copy_prop_fwd_dce);
47 copy_propagation_backward(*sh);
48 dead_code_elimination(*sh);
49 check(sh, add_add_1_expect_from_nir_copy_prop_fwd_dce_bwd);
50 }
51
52
TEST_F(TestShaderFromNir,FullOPtimize)53 TEST_F(TestShaderFromNir, FullOPtimize)
54 {
55 auto sh = from_string(basic_interpolation_orig);
56
57 bool progress;
58
59 do {
60 progress = false;
61 progress |= copy_propagation_fwd(*sh);
62 progress |= dead_code_elimination(*sh);
63 progress |= copy_propagation_backward(*sh);
64 progress |= dead_code_elimination(*sh);
65 } while (progress);
66
67 check(sh, basic_interpolation_expect_from_nir_opt);
68 }
69
TEST_F(TestShaderFromNir,CombinePinFlags)70 TEST_F(TestShaderFromNir, CombinePinFlags)
71 {
72 auto sh = from_string(shader_group_chan_pin_to_combine);
73
74 bool progress;
75
76 do {
77 progress = false;
78 progress |= copy_propagation_fwd(*sh);
79 progress |= dead_code_elimination(*sh);
80 progress |= copy_propagation_backward(*sh);
81 progress |= dead_code_elimination(*sh);
82 } while (progress);
83
84 check(sh, shader_group_chan_pin_combined);
85 }
86
87
TEST_F(TestShaderFromNir,FullOPtimizeLoop)88 TEST_F(TestShaderFromNir, FullOPtimizeLoop)
89 {
90 auto sh = from_string(vs_nexted_loop_from_nir_expect);
91
92 optimize(*sh);
93
94 check(sh, vs_nexted_loop_from_nir_expect_opt);
95 }
TEST_F(TestShaderFromNir,OptimizeWithDestArrayValue)96 TEST_F(TestShaderFromNir, OptimizeWithDestArrayValue)
97 {
98 auto sh = from_string(shader_with_dest_array);
99
100 optimize(*sh);
101
102 check(sh, shader_with_dest_array_opt_expect);
103 }
104
TEST_F(TestShaderFromNir,ScheduleOPtimizedWithDestArrayValue)105 TEST_F(TestShaderFromNir, ScheduleOPtimizedWithDestArrayValue)
106 {
107 auto sh = from_string(shader_with_dest_array_opt_expect);
108 check(schedule(sh), shader_with_dest_array_opt_scheduled);
109 }
110
TEST_F(TestShaderFromNir,ScheduleWithArrayWriteAndRead)111 TEST_F(TestShaderFromNir, ScheduleWithArrayWriteAndRead)
112 {
113 auto sh = from_string(shader_with_dest_array2);
114 check(schedule(sh), shader_with_dest_array2_scheduled);
115 }
116
TEST_F(TestShaderFromNir,RA_with_dest_array)117 TEST_F(TestShaderFromNir, RA_with_dest_array)
118 {
119 auto sh = from_string(shader_with_dest_array2_scheduled);
120
121 auto lrm = r600::LiveRangeEvaluator().run(*sh);
122 EXPECT_TRUE(r600::register_allocation(lrm));
123
124
125 ra_check(sh, shader_with_dest_array2_scheduled_ra);
126 }
127
TEST_F(TestShaderFromNir,RA_with_chan_group)128 TEST_F(TestShaderFromNir, RA_with_chan_group)
129 {
130 auto sh = from_string(shader_group_chan_pin_combined_sheduled);
131
132 auto lrm = r600::LiveRangeEvaluator().run(*sh);
133 EXPECT_TRUE(r600::register_allocation(lrm));
134 ra_check(sh, shader_group_chan_pin_combined_sheduled_ra);
135 }
136
137
TEST_F(TestShaderFromNir,TES_opt)138 TEST_F(TestShaderFromNir, TES_opt)
139 {
140 auto sh = from_string(tes_pre_op);
141
142 optimize(*sh);
143
144 check(sh, tes_optimized);
145 }
146
TEST_F(TestShaderFromNir,TES_scheduled)147 TEST_F(TestShaderFromNir, TES_scheduled)
148 {
149 auto sh = from_string(tes_optimized_pre_sched);
150
151 check(schedule(sh), tes_optimized_sched);
152 }
153
154
155 /*
156 TEST_F(TestShaderFromNir, ShaderClone)
157 {
158 auto sh = from_string(red_triangle_fs_expect_from_nir);
159
160 auto sh_cloned = sh->clone();
161
162 MemoryPool::instance().push();
163 dead_code_elimination(*sh);
164
165 check(sh, red_triangle_fs_expect_from_nir_dce);
166
167 check(sh_cloned, red_triangle_fs_expect_from_nir);
168
169 MemoryPool::instance().pop();
170 check(sh, red_triangle_fs_expect_from_nir_dce);
171 }
172 */
173
TEST_F(TestShaderFromNir,ShaderSchedule)174 TEST_F(TestShaderFromNir, ShaderSchedule)
175 {
176 auto sh = from_string(basic_interpolation_orig);
177
178 check(schedule(sh), basic_interpolation_expect_from_nir_sched);
179
180 }
181
TEST_F(TestShaderFromNir,ShaderScheduleCayman)182 TEST_F(TestShaderFromNir, ShaderScheduleCayman)
183 {
184 auto sh = from_string(basic_interpolation_orig_cayman);
185
186 check(schedule(sh), basic_interpolation_expect_from_nir_sched_cayman);
187 }
188
189
TEST_F(TestShaderFromNir,ShaderScheduleOptimizedCayman)190 TEST_F(TestShaderFromNir, ShaderScheduleOptimizedCayman)
191 {
192 auto sh = from_string(basic_interpolation_orig_cayman);
193
194 optimize(*sh);
195
196 check(schedule(sh), basic_interpolation_expect_opt_sched_cayman);
197 }
198
199
TEST_F(TestShaderFromNir,CopyPropLegalConst)200 TEST_F(TestShaderFromNir, CopyPropLegalConst)
201 {
202 auto sh = from_string(dot4_pre);
203
204 copy_propagation_fwd(*sh);
205 dead_code_elimination(*sh);
206
207 check(sh, dot4_copy_prop_dce);
208 }
209
210
TEST_F(TestShaderFromNir,FullOPtimize_glxgears_vs2)211 TEST_F(TestShaderFromNir, FullOPtimize_glxgears_vs2)
212 {
213
214 auto sh = from_string(glxgears_vs2_from_nir_expect);
215 optimize(*sh);
216 check(sh, glxgears_vs2_from_nir_expect_optimized);
217 }
218
TEST_F(TestShaderFromNir,test_schedule_group)219 TEST_F(TestShaderFromNir, test_schedule_group)
220 {
221
222 auto sh = from_string(test_schedule_group);
223 optimize(*sh);
224 check(schedule(sh), test_schedule_group_expect);
225 }
226
TEST_F(TestShaderFromNir,test_dont_kill_dual_use)227 TEST_F(TestShaderFromNir, test_dont_kill_dual_use)
228 {
229 auto sh = from_string(shader_copy_prop_dont_kill_double_use);
230 optimize(*sh);
231 check(schedule(sh), shader_copy_prop_dont_kill_double_use_expect);
232 }
233
234
235
TEST_F(TestShaderFromNir,test_schedule_with_bany)236 TEST_F(TestShaderFromNir, test_schedule_with_bany)
237 {
238
239 auto sh = from_string(shader_with_bany_expect_eg);
240 optimize(*sh);
241 check(schedule(sh), shader_with_bany_expect_opt_sched_eg);
242 }
243
244
TEST_F(TestShaderFromNir,GroupAndChanCombine)245 TEST_F(TestShaderFromNir, GroupAndChanCombine)
246 {
247 auto sh = from_string(shader_group_chan_pin_to_combine_2);
248 optimize(*sh);
249 check(sh, shader_group_chan_pin_to_combine_2_opt);
250 }
251
TEST_F(TestShaderFromNir,RemoveUseAfterSplitgroup)252 TEST_F(TestShaderFromNir, RemoveUseAfterSplitgroup)
253 {
254 auto sh = from_string(fs_with_loop_multislot_reuse);
255 check(schedule(sh), fs_with_loop_multislot_reuse_scheduled);
256 }
257
TEST_F(TestShaderFromNir,OptimizeVSforTCS)258 TEST_F(TestShaderFromNir, OptimizeVSforTCS)
259 {
260 auto sh = from_string(vtx_for_tcs_inp);
261 optimize(*sh);
262 check(sh, vtx_for_tcs_opt);
263 }
264
TEST_F(TestShaderFromNir,ScheduleVSforTCS)265 TEST_F(TestShaderFromNir, ScheduleVSforTCS)
266 {
267 auto sh = from_string(vtx_for_tcs_pre_sched);
268 check(schedule(sh), vtx_for_tcs_sched);
269 }
270
271
check(Shader * s,const char * expect_orig)272 void TestShaderFromNir::check(Shader *s, const char *expect_orig)
273 {
274 ostringstream test_str;
275 s->print(test_str);
276
277 auto expect = from_string(expect_orig);
278
279 ostringstream expect_str;
280 expect->print(expect_str);
281
282 EXPECT_EQ(test_str.str(), expect_str.str());
283 }
284
ra_check(Shader * s,const char * expect_orig)285 void TestShaderFromNir::ra_check(Shader *s, const char *expect_orig)
286 {
287 s->value_factory().clear_pins();
288 ostringstream test_str;
289 s->print(test_str);
290
291 auto expect = from_string(expect_orig);
292 expect->value_factory().clear_pins();
293
294 ostringstream expect_str;
295 expect->print(expect_str);
296
297 EXPECT_EQ(test_str.str(), expect_str.str());
298 }
299
300
301