1 // Copyright (c) 2018 Google LLC
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 "source/opt/simplification_pass.h"
19 #include "test/opt/pass_fixture.h"
20 
21 namespace spvtools {
22 namespace opt {
23 namespace {
24 
25 using SimplificationTest = PassTest<::testing::Test>;
26 
TEST_F(SimplificationTest,StraightLineTest)27 TEST_F(SimplificationTest, StraightLineTest) {
28   // Testing that folding rules are combined in simple straight line code.
29   const std::string text = R"(OpCapability Shader
30           %1 = OpExtInstImport "GLSL.std.450"
31                OpMemoryModel Logical GLSL450
32                OpEntryPoint Fragment %main "main" %i %o
33                OpExecutionMode %main OriginUpperLeft
34                OpSource GLSL 430
35                OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
36                OpSourceExtension "GL_GOOGLE_include_directive"
37                OpName %main "main"
38                OpName %i "i"
39                OpName %o "o"
40                OpDecorate %i Flat
41                OpDecorate %i Location 0
42                OpDecorate %o Location 0
43        %void = OpTypeVoid
44           %8 = OpTypeFunction %void
45         %int = OpTypeInt 32 1
46       %v4int = OpTypeVector %int 4
47       %int_0 = OpConstant %int 0
48          %13 = OpConstantComposite %v4int %int_0 %int_0 %int_0 %int_0
49       %int_1 = OpConstant %int 1
50 %_ptr_Input_v4int = OpTypePointer Input %v4int
51           %i = OpVariable %_ptr_Input_v4int Input
52 %_ptr_Output_int = OpTypePointer Output %int
53           %o = OpVariable %_ptr_Output_int Output
54        %main = OpFunction %void None %8
55          %21 = OpLabel
56          %31 = OpCompositeInsert %v4int %int_1 %13 0
57 ; CHECK: [[load:%[a-zA-Z_\d]+]] = OpLoad
58          %23 = OpLoad %v4int %i
59          %33 = OpCompositeInsert %v4int %int_0 %23 0
60          %35 = OpCompositeExtract %int %31 0
61 ; CHECK: [[extract:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 1
62          %37 = OpCompositeExtract %int %33 1
63 ; CHECK: [[add:%[a-zA-Z_\d]+]] = OpIAdd %int %int_1 [[extract]]
64          %29 = OpIAdd %int %35 %37
65                OpStore %o %29
66                OpReturn
67                OpFunctionEnd
68 )";
69 
70   SinglePassRunAndMatch<SimplificationPass>(text, false);
71 }
72 
TEST_F(SimplificationTest,NewInstructionTest)73 TEST_F(SimplificationTest, NewInstructionTest) {
74   // Testing that new instructions are simplified. Specifically,
75   // that the new add instruction generated by FactorAddMul is
76   // further simplified by MergeGenericAddSub.
77   const std::string text = R"(OpCapability Shader
78           %1 = OpExtInstImport "GLSL.std.450"
79                OpMemoryModel Logical GLSL450
80                OpEntryPoint Fragment %main "main"
81                OpExecutionMode %main OriginUpperLeft
82                OpSource GLSL 430
83                OpName %main "main"
84        %void = OpTypeVoid
85           %4 = OpTypeFunction %void
86         %int = OpTypeInt 32 1
87    %_ptr_int = OpTypePointer Function %int
88 ; CHECK: [[mul:%[a-zA-Z_\d]+]] = OpIMul %int %13 %11
89        %main = OpFunction %void None %4
90           %7 = OpLabel
91           %8 = OpVariable %_ptr_int Function
92           %9 = OpVariable %_ptr_int Function
93          %10 = OpVariable %_ptr_int Function
94          %11 = OpLoad %int %8
95          %12 = OpLoad %int %9
96          %13 = OpLoad %int %10
97          %14 = OpISub %int %11 %12
98          %15 = OpIMul %int %13 %11
99          %16 = OpIMul %int %13 %12
100          %17 = OpIAdd %int %14 %15
101                OpReturn
102                OpFunctionEnd
103 )";
104 
105   SinglePassRunAndMatch<SimplificationPass>(text, false);
106 }
107 
TEST_F(SimplificationTest,AcrossBasicBlocks)108 TEST_F(SimplificationTest, AcrossBasicBlocks) {
109   // Testing that folding rules are combined across basic blocks.
110   const std::string text = R"(OpCapability Shader
111           %1 = OpExtInstImport "GLSL.std.450"
112                OpMemoryModel Logical GLSL450
113                OpEntryPoint Fragment %main "main" %i %o
114                OpExecutionMode %main OriginUpperLeft
115                OpSource GLSL 430
116                OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
117                OpSourceExtension "GL_GOOGLE_include_directive"
118                OpName %main "main"
119                OpName %i "i"
120                OpName %o "o"
121                OpDecorate %i Flat
122                OpDecorate %i Location 0
123                OpDecorate %o Location 0
124        %void = OpTypeVoid
125           %8 = OpTypeFunction %void
126         %int = OpTypeInt 32 1
127       %v4int = OpTypeVector %int 4
128       %int_0 = OpConstant %int 0
129 %_ptr_Input_v4int = OpTypePointer Input %v4int
130           %i = OpVariable %_ptr_Input_v4int Input
131        %uint = OpTypeInt 32 0
132      %uint_0 = OpConstant %uint 0
133 %_ptr_Input_int = OpTypePointer Input %int
134      %int_10 = OpConstant %int 10
135        %bool = OpTypeBool
136       %int_1 = OpConstant %int 1
137 %_ptr_Output_int = OpTypePointer Output %int
138           %o = OpVariable %_ptr_Output_int Output
139        %main = OpFunction %void None %8
140          %24 = OpLabel
141 ; CHECK: [[load:%[a-zA-Z_\d]+]] = OpLoad %v4int %i
142          %25 = OpLoad %v4int %i
143          %41 = OpCompositeInsert %v4int %int_0 %25 0
144          %27 = OpAccessChain %_ptr_Input_int %i %uint_0
145          %28 = OpLoad %int %27
146          %29 = OpSGreaterThan %bool %28 %int_10
147                OpSelectionMerge %30 None
148                OpBranchConditional %29 %31 %32
149          %31 = OpLabel
150          %43 = OpCopyObject %v4int %25
151                OpBranch %30
152          %32 = OpLabel
153          %45 = OpCopyObject %v4int %25
154                OpBranch %30
155          %30 = OpLabel
156          %50 = OpPhi %v4int %43 %31 %45 %32
157 ; CHECK: [[extract1:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 0
158          %47 = OpCompositeExtract %int %50 0
159 ; CHECK: [[extract2:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 1
160          %49 = OpCompositeExtract %int %41 1
161 ; CHECK: [[add:%[a-zA-Z_\d]+]] = OpIAdd %int [[extract1]] [[extract2]]
162          %39 = OpIAdd %int %47 %49
163                OpStore %o %39
164                OpReturn
165                OpFunctionEnd
166 
167 )";
168 
169   SinglePassRunAndMatch<SimplificationPass>(text, false);
170 }
171 
TEST_F(SimplificationTest,ThroughLoops)172 TEST_F(SimplificationTest, ThroughLoops) {
173   // Testing that folding rules are applied multiple times to instructions
174   // to be able to propagate across loop iterations.
175   const std::string text = R"(
176                OpCapability Shader
177           %1 = OpExtInstImport "GLSL.std.450"
178                OpMemoryModel Logical GLSL450
179                OpEntryPoint Fragment %main "main" %o %i
180                OpExecutionMode %main OriginUpperLeft
181                OpSource GLSL 430
182                OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
183                OpSourceExtension "GL_GOOGLE_include_directive"
184                OpName %main "main"
185                OpName %o "o"
186                OpName %i "i"
187                OpDecorate %o Location 0
188                OpDecorate %i Flat
189                OpDecorate %i Location 0
190        %void = OpTypeVoid
191           %8 = OpTypeFunction %void
192         %int = OpTypeInt 32 1
193       %v4int = OpTypeVector %int 4
194       %int_0 = OpConstant %int 0
195 ; CHECK: [[constant:%[a-zA-Z_\d]+]] = OpConstantComposite %v4int %int_0 %int_0 %int_0 %int_0
196          %13 = OpConstantComposite %v4int %int_0 %int_0 %int_0 %int_0
197        %bool = OpTypeBool
198 %_ptr_Output_int = OpTypePointer Output %int
199           %o = OpVariable %_ptr_Output_int Output
200 %_ptr_Input_v4int = OpTypePointer Input %v4int
201           %i = OpVariable %_ptr_Input_v4int Input
202          %68 = OpUndef %v4int
203        %main = OpFunction %void None %8
204          %23 = OpLabel
205 ; CHECK: [[load:%[a-zA-Z_\d]+]] = OpLoad %v4int %i
206        %load = OpLoad %v4int %i
207                OpBranch %24
208          %24 = OpLabel
209          %67 = OpPhi %v4int %load %23 %64 %26
210 ; CHECK: OpLoopMerge [[merge_lab:%[a-zA-Z_\d]+]]
211                OpLoopMerge %25 %26 None
212                OpBranch %27
213          %27 = OpLabel
214          %48 = OpCompositeExtract %int %67 0
215          %30 = OpIEqual %bool %48 %int_0
216                OpBranchConditional %30 %31 %25
217          %31 = OpLabel
218          %50 = OpCompositeExtract %int %67 0
219          %54 = OpCompositeExtract %int %67 1
220          %58 = OpCompositeExtract %int %67 2
221          %62 = OpCompositeExtract %int %67 3
222 	 %64 = OpCompositeConstruct %v4int %50 %54 %58 %62
223                OpBranch %26
224          %26 = OpLabel
225                OpBranch %24
226          %25 = OpLabel
227 ; CHECK: [[merge_lab]] = OpLabel
228 ; CHECK: [[extract:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 0
229          %66 = OpCompositeExtract %int %67 0
230 ; CHECK-NEXT: OpStore %o [[extract]]
231                OpStore %o %66
232                OpReturn
233                OpFunctionEnd
234 )";
235 
236   SinglePassRunAndMatch<SimplificationPass>(text, false);
237 }
238 
TEST_F(SimplificationTest,CopyObjectWithDecorations1)239 TEST_F(SimplificationTest, CopyObjectWithDecorations1) {
240   // Don't simplify OpCopyObject if the result id has a decoration that the
241   // operand does not.
242   const std::string text = R"(OpCapability Shader
243 OpCapability ShaderNonUniform
244 %1 = OpExtInstImport "GLSL.std.450"
245 OpMemoryModel Logical GLSL450
246 OpEntryPoint Fragment %2 "main"
247 OpExecutionMode %2 OriginUpperLeft
248 OpSource GLSL 430
249 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
250 OpSourceExtension "GL_GOOGLE_include_directive"
251 OpDecorate %3 NonUniform
252 %void = OpTypeVoid
253 %5 = OpTypeFunction %void
254 %int = OpTypeInt 32 1
255 %2 = OpFunction %void None %5
256 %7 = OpLabel
257 %8 = OpUndef %int
258 %3 = OpCopyObject %int %8
259 %9 = OpIAdd %int %3 %3
260 OpReturn
261 OpFunctionEnd
262 )";
263 
264   SinglePassRunAndCheck<SimplificationPass>(text, text, false);
265 }
266 
TEST_F(SimplificationTest,CopyObjectWithDecorations2)267 TEST_F(SimplificationTest, CopyObjectWithDecorations2) {
268   // Simplify OpCopyObject if the result id is a subset of the decorations of
269   // the operand.
270   const std::string before = R"(OpCapability Shader
271 OpCapability ShaderNonUniform
272 %1 = OpExtInstImport "GLSL.std.450"
273 OpMemoryModel Logical GLSL450
274 OpEntryPoint Fragment %2 "main"
275 OpExecutionMode %2 OriginUpperLeft
276 OpSource GLSL 430
277 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
278 OpSourceExtension "GL_GOOGLE_include_directive"
279 OpDecorate %3 NonUniform
280 %void = OpTypeVoid
281 %5 = OpTypeFunction %void
282 %int = OpTypeInt 32 1
283 %2 = OpFunction %void None %5
284 %7 = OpLabel
285 %3 = OpUndef %int
286 %8 = OpCopyObject %int %3
287 %9 = OpIAdd %int %8 %8
288 OpReturn
289 OpFunctionEnd
290 )";
291 
292   const std::string after = R"(OpCapability Shader
293 OpCapability ShaderNonUniform
294 %1 = OpExtInstImport "GLSL.std.450"
295 OpMemoryModel Logical GLSL450
296 OpEntryPoint Fragment %2 "main"
297 OpExecutionMode %2 OriginUpperLeft
298 OpSource GLSL 430
299 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
300 OpSourceExtension "GL_GOOGLE_include_directive"
301 OpDecorate %3 NonUniform
302 %void = OpTypeVoid
303 %5 = OpTypeFunction %void
304 %int = OpTypeInt 32 1
305 %2 = OpFunction %void None %5
306 %7 = OpLabel
307 %3 = OpUndef %int
308 %9 = OpIAdd %int %3 %3
309 OpReturn
310 OpFunctionEnd
311 )";
312 
313   SinglePassRunAndCheck<SimplificationPass>(before, after, false);
314 }
315 
TEST_F(SimplificationTest,DontMoveDecorations)316 TEST_F(SimplificationTest, DontMoveDecorations) {
317   const std::string spirv = R"(
318 ; CHECK-NOT: RelaxedPrecision
319 ; CHECK: [[sub:%\w+]] = OpFSub
320 ; CHECK: OpStore {{.*}} [[sub]]
321 OpCapability Shader
322 OpMemoryModel Logical GLSL450
323 OpEntryPoint GLCompute %main "main"
324 OpExecutionMode %main LocalSize 1 1 1
325 OpDecorate %add RelaxedPrecision
326 OpDecorate %block Block
327 OpMemberDecorate %block 0 Offset 0
328 OpMemberDecorate %block 1 Offset 4
329 OpDecorate %in DescriptorSet 0
330 OpDecorate %in Binding 0
331 OpDecorate %out DescriptorSet 0
332 OpDecorate %out Binding 1
333 %void = OpTypeVoid
334 %float = OpTypeFloat 32
335 %void_fn = OpTypeFunction %void
336 %block = OpTypeStruct %float %float
337 %ptr_ssbo_block = OpTypePointer StorageBuffer %block
338 %in = OpVariable %ptr_ssbo_block StorageBuffer
339 %out = OpVariable %ptr_ssbo_block StorageBuffer
340 %ptr_ssbo_float = OpTypePointer StorageBuffer %float
341 %int = OpTypeInt 32 0
342 %int_0 = OpConstant %int 0
343 %int_1 = OpConstant %int 1
344 %float_0 = OpConstant %float 0
345 %main = OpFunction %void None %void_fn
346 %entry = OpLabel
347 %in_gep_0 = OpAccessChain %ptr_ssbo_float %in %int_0
348 %in_gep_1 = OpAccessChain %ptr_ssbo_float %in %int_1
349 %load_0 = OpLoad %float %in_gep_0
350 %load_1 = OpLoad %float %in_gep_1
351 %sub = OpFSub %float %load_0 %load_1
352 %add = OpFAdd %float %float_0 %sub
353 %out_gep_0 = OpAccessChain %ptr_ssbo_float %out %int_0
354 OpStore %out_gep_0 %add
355 OpReturn
356 OpFunctionEnd
357 )";
358 
359   SinglePassRunAndMatch<SimplificationPass>(spirv, true);
360 }
361 
TEST_F(SimplificationTest,FunctionDeclaration)362 TEST_F(SimplificationTest, FunctionDeclaration) {
363   // Make sure the pass works with a function declaration that is called.
364   const std::string text = R"(OpCapability Addresses
365 OpCapability Linkage
366 OpCapability Kernel
367 OpCapability Int8
368 %1 = OpExtInstImport "OpenCL.std"
369 OpMemoryModel Physical64 OpenCL
370 OpEntryPoint Kernel %2 "_Z23julia__1166_kernel_77094Bool"
371 OpExecutionMode %2 ContractionOff
372 OpSource Unknown 0
373 OpDecorate %3 LinkageAttributes "julia_error_7712" Import
374 %void = OpTypeVoid
375 %5 = OpTypeFunction %void
376 %3 = OpFunction %void None %5
377 OpFunctionEnd
378 %2 = OpFunction %void None %5
379 %6 = OpLabel
380 %7 = OpFunctionCall %void %3
381 OpReturn
382 OpFunctionEnd
383 )";
384 
385   SinglePassRunAndCheck<SimplificationPass>(text, text, false);
386 }
387 }  // namespace
388 }  // namespace opt
389 }  // namespace spvtools
390