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