Debian Codegraph @

test/opt/redundancy_elimination_test.cpp

raw
1 // Copyright (c) 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string>
16
17 #include "gmock/gmock.h"
18 #include "test/opt/assembly_builder.h"
19 #include "test/opt/pass_fixture.h"
20 #include "test/opt/pass_utils.h"
21
22 namespace spvtools {
23 namespace opt {
24 namespace {
25
26 using ::testing::HasSubstr;
27 using ::testing::MatchesRegex;
28 using RedundancyEliminationTest = PassTest<::testing::Test>;
29
30 // Test that it can get a simple case of local redundancy elimination.
31 // The rest of the test check for extra functionality.
32 TEST_F(RedundancyEliminationTest, RemoveRedundantLocalAdd) {
33 const std::string text = R"(
34 OpCapability Shader
35 %1 = OpExtInstImport "GLSL.std.450"
36 OpMemoryModel Logical GLSL450
37 OpEntryPoint Fragment %2 "main"
38 OpExecutionMode %2 OriginUpperLeft
39 OpSource GLSL 430
40 %3 = OpTypeVoid
41 %4 = OpTypeFunction %3
42 %5 = OpTypeFloat 32
43 %6 = OpTypePointer Function %5
44 %2 = OpFunction %3 None %4
45 %7 = OpLabel
46 %8 = OpVariable %6 Function
47 %9 = OpLoad %5 %8
48 %10 = OpFAdd %5 %9 %9
49 ; CHECK: OpFAdd
50 ; CHECK-NOT: OpFAdd
51 %11 = OpFAdd %5 %9 %9
52 OpReturn
53 OpFunctionEnd
54 )";
55 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
56 }
57
58 // Remove a redundant add across basic blocks.
59 TEST_F(RedundancyEliminationTest, RemoveRedundantAdd) {
60 const std::string text = R"(
61 OpCapability Shader
62 %1 = OpExtInstImport "GLSL.std.450"
63 OpMemoryModel Logical GLSL450
64 OpEntryPoint Fragment %2 "main"
65 OpExecutionMode %2 OriginUpperLeft
66 OpSource GLSL 430
67 %3 = OpTypeVoid
68 %4 = OpTypeFunction %3
69 %5 = OpTypeFloat 32
70 %6 = OpTypePointer Function %5
71 %2 = OpFunction %3 None %4
72 %7 = OpLabel
73 %8 = OpVariable %6 Function
74 %9 = OpLoad %5 %8
75 %10 = OpFAdd %5 %9 %9
76 OpBranch %11
77 %11 = OpLabel
78 ; CHECK: OpFAdd
79 ; CHECK-NOT: OpFAdd
80 %12 = OpFAdd %5 %9 %9
81 OpReturn
82 OpFunctionEnd
83 )";
84 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
85 }
86
87 // Remove a redundant add with flipped arguments.
88 TEST_F(RedundancyEliminationTest, RemoveRedundantAddFlipped) {
89 const std::string text = R"(
90 OpCapability Shader
91 %1 = OpExtInstImport "GLSL.std.450"
92 OpMemoryModel Logical GLSL450
93 OpEntryPoint Fragment %2 "main"
94 OpExecutionMode %2 OriginUpperLeft
95 OpSource GLSL 430
96 %3 = OpTypeVoid
97 %4 = OpTypeFunction %3
98 %5 = OpTypeFloat 32
99 %6 = OpTypePointer Function %5
100 %2 = OpFunction %3 None %4
101 %7 = OpLabel
102 %8 = OpVariable %6 Function
103 %9 = OpLoad %5 %8
104 %10 = OpFMul %5 %9 %9
105 %11 = OpFAdd %5 %9 %10
106 ; CHECK: OpFAdd
107 ; CHECK-NEXT: OpReturn
108 %12 = OpFAdd %5 %10 %9
109 OpReturn
110 OpFunctionEnd
111 )";
112 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
113 }
114
115 // Remove a redundant add going through a multiple basic blocks.
116 TEST_F(RedundancyEliminationTest, RemoveRedundantAddDiamond) {
117 const std::string text = R"(
118 OpCapability Shader
119 %1 = OpExtInstImport "GLSL.std.450"
120 OpMemoryModel Logical GLSL450
121 OpEntryPoint Fragment %2 "main"
122 OpExecutionMode %2 OriginUpperLeft
123 OpSource GLSL 430
124 %3 = OpTypeVoid
125 %4 = OpTypeFunction %3
126 %5 = OpTypeFloat 32
127 %6 = OpTypePointer Function %5
128 %7 = OpTypeBool
129 %8 = OpConstantTrue %7
130 %2 = OpFunction %3 None %4
131 %9 = OpLabel
132 %10 = OpVariable %6 Function
133 %11 = OpLoad %5 %10
134 %12 = OpFAdd %5 %11 %11
135 ; CHECK: OpFAdd
136 ; CHECK-NOT: OpFAdd
137 OpBranchConditional %8 %13 %14
138 %13 = OpLabel
139 OpBranch %15
140 %14 = OpLabel
141 OpBranch %15
142 %15 = OpLabel
143 %16 = OpFAdd %5 %11 %11
144 OpReturn
145 OpFunctionEnd
146
147 )";
148 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
149 }
150
151 // Remove a redundant add in a side node.
152 TEST_F(RedundancyEliminationTest, RemoveRedundantAddInSideNode) {
153 const std::string text = R"(
154 OpCapability Shader
155 %1 = OpExtInstImport "GLSL.std.450"
156 OpMemoryModel Logical GLSL450
157 OpEntryPoint Fragment %2 "main"
158 OpExecutionMode %2 OriginUpperLeft
159 OpSource GLSL 430
160 %3 = OpTypeVoid
161 %4 = OpTypeFunction %3
162 %5 = OpTypeFloat 32
163 %6 = OpTypePointer Function %5
164 %7 = OpTypeBool
165 %8 = OpConstantTrue %7
166 %2 = OpFunction %3 None %4
167 %9 = OpLabel
168 %10 = OpVariable %6 Function
169 %11 = OpLoad %5 %10
170 %12 = OpFAdd %5 %11 %11
171 ; CHECK: OpFAdd
172 ; CHECK-NOT: OpFAdd
173 OpBranchConditional %8 %13 %14
174 %13 = OpLabel
175 OpBranch %15
176 %14 = OpLabel
177 %16 = OpFAdd %5 %11 %11
178 OpBranch %15
179 %15 = OpLabel
180 OpReturn
181 OpFunctionEnd
182
183 )";
184 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
185 }
186
187 // Remove a redundant add whose value is in the result of a phi node.
188 TEST_F(RedundancyEliminationTest, RemoveRedundantAddWithPhi) {
189 const std::string text = R"(
190 OpCapability Shader
191 %1 = OpExtInstImport "GLSL.std.450"
192 OpMemoryModel Logical GLSL450
193 OpEntryPoint Fragment %2 "main"
194 OpExecutionMode %2 OriginUpperLeft
195 OpSource GLSL 430
196 %3 = OpTypeVoid
197 %4 = OpTypeFunction %3
198 %5 = OpTypeFloat 32
199 %6 = OpTypePointer Function %5
200 %7 = OpTypeBool
201 %8 = OpConstantTrue %7
202 %2 = OpFunction %3 None %4
203 %9 = OpLabel
204 %10 = OpVariable %6 Function
205 %11 = OpLoad %5 %10
206 OpBranchConditional %8 %13 %14
207 %13 = OpLabel
208 %add1 = OpFAdd %5 %11 %11
209 ; CHECK: OpFAdd
210 OpBranch %15
211 %14 = OpLabel
212 %add2 = OpFAdd %5 %11 %11
213 ; CHECK: OpFAdd
214 OpBranch %15
215 %15 = OpLabel
216 ; CHECK: OpPhi
217 %phi = OpPhi %5 %add1 %13 %add2 %14
218 ; CHECK-NOT: OpFAdd
219 %16 = OpFAdd %5 %11 %11
220 OpReturn
221 OpFunctionEnd
222
223 )";
224 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
225 }
226
227 // Keep the add because it is redundant on some paths, but not all paths.
228 TEST_F(RedundancyEliminationTest, KeepPartiallyRedundantAdd) {
229 const std::string text = R"(
230 OpCapability Shader
231 %1 = OpExtInstImport "GLSL.std.450"
232 OpMemoryModel Logical GLSL450
233 OpEntryPoint Fragment %2 "main"
234 OpExecutionMode %2 OriginUpperLeft
235 OpSource GLSL 430
236 %3 = OpTypeVoid
237 %4 = OpTypeFunction %3
238 %5 = OpTypeFloat 32
239 %6 = OpTypePointer Function %5
240 %7 = OpTypeBool
241 %8 = OpConstantTrue %7
242 %2 = OpFunction %3 None %4
243 %9 = OpLabel
244 %10 = OpVariable %6 Function
245 %11 = OpLoad %5 %10
246 OpBranchConditional %8 %13 %14
247 %13 = OpLabel
248 %add = OpFAdd %5 %11 %11
249 OpBranch %15
250 %14 = OpLabel
251 OpBranch %15
252 %15 = OpLabel
253 %16 = OpFAdd %5 %11 %11
254 OpReturn
255 OpFunctionEnd
256
257 )";
258 auto result = SinglePassRunAndDisassemble<RedundancyEliminationPass>(
259 text, /* skip_nop = */ true, /* do_validation = */ false);
260 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
261 }
262
263 // Keep the add. Even if it is redundant on all paths, there is no single id
264 // whose definition dominates the add and contains the same value.
265 TEST_F(RedundancyEliminationTest, KeepRedundantAddWithoutPhi) {
266 const std::string text = R"(
267 OpCapability Shader
268 %1 = OpExtInstImport "GLSL.std.450"
269 OpMemoryModel Logical GLSL450
270 OpEntryPoint Fragment %2 "main"
271 OpExecutionMode %2 OriginUpperLeft
272 OpSource GLSL 430
273 %3 = OpTypeVoid
274 %4 = OpTypeFunction %3
275 %5 = OpTypeFloat 32
276 %6 = OpTypePointer Function %5
277 %7 = OpTypeBool
278 %8 = OpConstantTrue %7
279 %2 = OpFunction %3 None %4
280 %9 = OpLabel
281 %10 = OpVariable %6 Function
282 %11 = OpLoad %5 %10
283 OpBranchConditional %8 %13 %14
284 %13 = OpLabel
285 %add1 = OpFAdd %5 %11 %11
286 OpBranch %15
287 %14 = OpLabel
288 %add2 = OpFAdd %5 %11 %11
289 OpBranch %15
290 %15 = OpLabel
291 %16 = OpFAdd %5 %11 %11
292 OpReturn
293 OpFunctionEnd
294
295 )";
296 auto result = SinglePassRunAndDisassemble<RedundancyEliminationPass>(
297 text, /* skip_nop = */ true, /* do_validation = */ false);
298 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
299 }
300
301 // Test that it can get a simple case of local redundancy elimination
302 // when it has OpenCL.DebugInfo.100 instructions.
303 TEST_F(RedundancyEliminationTest, OpenCLDebugInfo100) {
304 // When three redundant DebugValues exist, only one DebugValue must remain.
305 const std::string text = R"(
306 OpCapability Shader
307 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
308 %2 = OpExtInstImport "GLSL.std.450"
309 OpMemoryModel Logical GLSL450
310 OpEntryPoint Fragment %3 "main"
311 OpExecutionMode %3 OriginUpperLeft
312 OpSource GLSL 430
313 %4 = OpString "ps.hlsl"
314 %5 = OpString "float"
315 %6 = OpString "s0"
316 %7 = OpString "main"
317 %void = OpTypeVoid
318 %9 = OpTypeFunction %void
319 %float = OpTypeFloat 32
320 %uint = OpTypeInt 32 0
321 %uint_0 = OpConstant %uint 0
322 %uint_32 = OpConstant %uint 32
323 %_ptr_Function_float = OpTypePointer Function %float
324 %15 = OpExtInst %void %1 DebugExpression
325 %16 = OpExtInst %void %1 DebugSource %4
326 %17 = OpExtInst %void %1 DebugCompilationUnit 1 4 %16 HLSL
327 %18 = OpExtInst %void %1 DebugTypeBasic %5 %uint_32 Float
328 %19 = OpExtInst %void %1 DebugTypeVector %18 4
329 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %19
330 %21 = OpExtInst %void %1 DebugFunction %7 %20 %16 4 1 %17 %7 FlagIsProtected|FlagIsPrivate 4 %3
331 ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable
332 %22 = OpExtInst %void %1 DebugLocalVariable %6 %19 %16 0 0 %21 FlagIsLocal
333 %14 = OpExtInst %void %1 DebugLocalVariable %6 %19 %16 0 0 %21 FlagIsLocal
334 %3 = OpFunction %void None %9
335 %23 = OpLabel
336 %24 = OpExtInst %void %1 DebugScope %21
337 %25 = OpVariable %_ptr_Function_float Function
338 %26 = OpLoad %float %25
339 OpLine %4 0 0
340 ; Two `OpFAdd %float %26 %26` are the same. One must be removed.
341 ; After removing one `OpFAdd %float %26 %26`, two DebugValues are the same.
342 ; One must be removed.
343 ;
344 ; CHECK: OpLine {{%\w+}} 0 0
345 ; CHECK-NEXT: [[add:%\w+]] = OpFAdd %float [[value:%\w+]]
346 ; CHECK-NEXT: DebugValue [[dbg_local_var]] [[add]]
347 ; CHECK-NEXT: OpLine {{%\w+}} 1 0
348 ; CHECK-NEXT: OpFAdd %float [[add]] [[value]]
349 ; CHECK-NEXT: OpReturn
350 %27 = OpFAdd %float %26 %26
351 %28 = OpExtInst %void %1 DebugValue %22 %27 %15 %uint_0
352 OpLine %4 1 0
353 %29 = OpFAdd %float %26 %26
354 %30 = OpExtInst %void %1 DebugValue %14 %29 %15 %uint_0
355 %31 = OpExtInst %void %1 DebugValue %22 %29 %15 %uint_0
356 %32 = OpFAdd %float %29 %26
357 %33 = OpFAdd %float %27 %26
358 OpReturn
359 OpFunctionEnd
360 )";
361 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
362 }
363
364 TEST_F(RedundancyEliminationTest, FunctionDeclaration) {
365 // Make sure the pass works with a function declaration that is called.
366 const std::string text = R"(OpCapability Addresses
367 OpCapability Linkage
368 OpCapability Kernel
369 OpCapability Int8
370 %1 = OpExtInstImport "OpenCL.std"
371 OpMemoryModel Physical64 OpenCL
372 OpEntryPoint Kernel %2 "_Z23julia__1166_kernel_77094Bool"
373 OpExecutionMode %2 ContractionOff
374 OpSource Unknown 0
375 OpDecorate %3 LinkageAttributes "julia_error_7712" Import
376 %void = OpTypeVoid
377 %5 = OpTypeFunction %void
378 %3 = OpFunction %void None %5
379 OpFunctionEnd
380 %2 = OpFunction %void None %5
381 %6 = OpLabel
382 %7 = OpFunctionCall %void %3
383 OpReturn
384 OpFunctionEnd
385 )";
386
387 SinglePassRunAndCheck<RedundancyEliminationPass>(text, text, false);
388 }
389
390 TEST_F(RedundancyEliminationTest, PreserveLoadYieldingImage) {
391 // This is more strict than needed.
392 // This is sufficient to ensure that the load of an image
393 // occurs in the same basic block as its use.
394 const std::string text = R"(
395 OpCapability Shader
396 OpMemoryModel Logical GLSL450
397 OpEntryPoint Fragment %main "main"
398 OpExecutionMode %main OriginLowerLeft
399 OpName %main "main"
400 OpName %load_ty "load_ty"
401 OpDecorate %var DescriptorSet 0
402 OpDecorate %var Binding 0
403 %void = OpTypeVoid
404 %6 = OpTypeFunction %void
405 %float = OpTypeFloat 32
406 %load_ty = OpTypeImage %float 2D 0 0 0 2 Rgba32f
407 %ptr_load_ty = OpTypePointer UniformConstant %load_ty
408 %var = OpVariable %ptr_load_ty UniformConstant
409 %main = OpFunction %void None %6
410 ; CHECK: OpLoad %load_ty
411 ; CHECK: OpLoad %load_ty
412 %15 = OpLabel
413 %16 = OpLoad %load_ty %var
414 %17 = OpLoad %load_ty %var
415 OpReturn
416 OpFunctionEnd
417 )";
418 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
419 }
420
421 TEST_F(RedundancyEliminationTest, PreserveLoadYieldingSampler) {
422 const std::string text = R"(
423 OpCapability Shader
424 OpMemoryModel Logical GLSL450
425 OpEntryPoint Fragment %main "main"
426 OpExecutionMode %main OriginLowerLeft
427 OpName %main "main"
428 OpName %load_ty "load_ty"
429 OpDecorate %var DescriptorSet 0
430 OpDecorate %var Binding 0
431 %void = OpTypeVoid
432 %6 = OpTypeFunction %void
433 %load_ty = OpTypeSampler
434 %ptr_load_ty = OpTypePointer UniformConstant %load_ty
435 %var = OpVariable %ptr_load_ty UniformConstant
436 %main = OpFunction %void None %6
437 ; CHECK: OpLoad %load_ty
438 ; CHECK: OpLoad %load_ty
439 %15 = OpLabel
440 %16 = OpLoad %load_ty %var
441 %17 = OpLoad %load_ty %var
442 OpReturn
443 OpFunctionEnd
444 )";
445 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
446 }
447
448 TEST_F(RedundancyEliminationTest, PreserveLoadYieldingSampledImage) {
449 const std::string text = R"(
450 OpCapability Shader
451 OpMemoryModel Logical GLSL450
452 OpEntryPoint Fragment %main "main"
453 OpExecutionMode %main OriginLowerLeft
454 OpName %main "main"
455 OpName %load_ty "load_ty"
456 OpDecorate %var DescriptorSet 0
457 OpDecorate %var Binding 0
458 %void = OpTypeVoid
459 %6 = OpTypeFunction %void
460 %float = OpTypeFloat 32
461 %i_ty = OpTypeImage %float 2D 0 0 0 2 Rgba32f
462 %load_ty = OpTypeSampledImage %i_ty
463 %ptr_load_ty = OpTypePointer UniformConstant %load_ty
464 %var = OpVariable %ptr_load_ty UniformConstant
465 %main = OpFunction %void None %6
466 ; CHECK: OpLoad %load_ty
467 ; CHECK: OpLoad %load_ty
468 %15 = OpLabel
469 %16 = OpLoad %load_ty %var
470 %17 = OpLoad %load_ty %var
471 OpReturn
472 OpFunctionEnd
473 )";
474 SinglePassRunAndMatch<RedundancyEliminationPass>(text, false);
475 }
476
477 } // namespace
478 } // namespace opt
479 } // namespace spvtools