• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ScalarReplacementTest = PassTest<::testing::Test>;
27 
TEST_F(ScalarReplacementTest,SimpleStruct)28 TEST_F(ScalarReplacementTest, SimpleStruct) {
29   const std::string text = R"(
30 ;
31 ; CHECK: [[struct:%\w+]] = OpTypeStruct [[elem:%\w+]]
32 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
33 ; CHECK: [[elem_ptr:%\w+]] = OpTypePointer Function [[elem]]
34 ; CHECK: OpConstantNull [[struct]]
35 ; CHECK: [[null:%\w+]] = OpConstantNull [[elem]]
36 ; CHECK-NOT: OpVariable [[struct_ptr]]
37 ; CHECK: [[one:%\w+]] = OpVariable [[elem_ptr]] Function [[null]]
38 ; CHECK-NEXT: [[two:%\w+]] = OpVariable [[elem_ptr]] Function [[null]]
39 ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[null]]
40 ; CHECK-NOT: OpVariable [[struct_ptr]]
41 ; CHECK-NOT: OpInBoundsAccessChain
42 ; CHECK: [[l1:%\w+]] = OpLoad [[elem]] [[two]]
43 ; CHECK-NOT: OpAccessChain
44 ; CHECK: [[l2:%\w+]] = OpLoad [[elem]] [[one]]
45 ; CHECK: OpIAdd [[elem]] [[l1]] [[l2]]
46 ;
47 OpCapability Shader
48 OpCapability Linkage
49 OpMemoryModel Logical GLSL450
50 OpName %6 "simple_struct"
51 %1 = OpTypeVoid
52 %2 = OpTypeInt 32 0
53 %3 = OpTypeStruct %2 %2 %2 %2
54 %4 = OpTypePointer Function %3
55 %5 = OpTypePointer Function %2
56 %6 = OpTypeFunction %2
57 %7 = OpConstantNull %3
58 %8 = OpConstant %2 0
59 %9 = OpConstant %2 1
60 %10 = OpConstant %2 2
61 %11 = OpConstant %2 3
62 %12 = OpFunction %2 None %6
63 %13 = OpLabel
64 %14 = OpVariable %4 Function %7
65 %15 = OpInBoundsAccessChain %5 %14 %8
66 %16 = OpLoad %2 %15
67 %17 = OpAccessChain %5 %14 %10
68 %18 = OpLoad %2 %17
69 %19 = OpIAdd %2 %16 %18
70 OpReturnValue %19
71 OpFunctionEnd
72   )";
73 
74   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
75 }
76 
TEST_F(ScalarReplacementTest,StructInitialization)77 TEST_F(ScalarReplacementTest, StructInitialization) {
78   const std::string text = R"(
79 ;
80 ; CHECK: [[elem:%\w+]] = OpTypeInt 32 0
81 ; CHECK: [[struct:%\w+]] = OpTypeStruct [[elem]] [[elem]] [[elem]] [[elem]]
82 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
83 ; CHECK: [[elem_ptr:%\w+]] = OpTypePointer Function [[elem]]
84 ; CHECK: [[zero:%\w+]] = OpConstant [[elem]] 0
85 ; CHECK: [[undef:%\w+]] = OpUndef [[elem]]
86 ; CHECK: [[two:%\w+]] = OpConstant [[elem]] 2
87 ; CHECK: [[null:%\w+]] = OpConstantNull [[elem]]
88 ; CHECK-NOT: OpVariable [[struct_ptr]]
89 ; CHECK: OpVariable [[elem_ptr]] Function [[null]]
90 ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[two]]
91 ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
92 ; CHECK-NEXT: OpVariable [[elem_ptr]] Function
93 ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[zero]]
94 ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
95 ;
96 OpCapability Shader
97 OpCapability Linkage
98 OpMemoryModel Logical GLSL450
99 OpName %6 "struct_init"
100 %1 = OpTypeVoid
101 %2 = OpTypeInt 32 0
102 %3 = OpTypeStruct %2 %2 %2 %2
103 %4 = OpTypePointer Function %3
104 %20 = OpTypePointer Function %2
105 %6 = OpTypeFunction %1
106 %7 = OpConstant %2 0
107 %8 = OpUndef %2
108 %9 = OpConstant %2 2
109 %30 = OpConstant %2 1
110 %31 = OpConstant %2 3
111 %10 = OpConstantNull %2
112 %11 = OpConstantComposite %3 %7 %8 %9 %10
113 %12 = OpFunction %1 None %6
114 %13 = OpLabel
115 %14 = OpVariable %4 Function %11
116 %15 = OpAccessChain %20 %14 %7
117 OpStore %15 %10
118 %16 = OpAccessChain %20 %14 %9
119 OpStore %16 %10
120 %17 = OpAccessChain %20 %14 %30
121 OpStore %17 %10
122 %18 = OpAccessChain %20 %14 %31
123 OpStore %18 %10
124 OpReturn
125 OpFunctionEnd
126   )";
127 
128   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
129 }
130 
TEST_F(ScalarReplacementTest,SpecConstantInitialization)131 TEST_F(ScalarReplacementTest, SpecConstantInitialization) {
132   const std::string text = R"(
133 ;
134 ; CHECK: [[int:%\w+]] = OpTypeInt 32 0
135 ; CHECK: [[struct:%\w+]] = OpTypeStruct [[int]] [[int]]
136 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
137 ; CHECK: [[int_ptr:%\w+]] = OpTypePointer Function [[int]]
138 ; CHECK: [[spec_comp:%\w+]] = OpSpecConstantComposite [[struct]]
139 ; CHECK: [[ex0:%\w+]] = OpSpecConstantOp [[int]] CompositeExtract [[spec_comp]] 0
140 ; CHECK: [[ex1:%\w+]] = OpSpecConstantOp [[int]] CompositeExtract [[spec_comp]] 1
141 ; CHECK-NOT: OpVariable [[struct]]
142 ; CHECK: OpVariable [[int_ptr]] Function [[ex1]]
143 ; CHECK-NEXT: OpVariable [[int_ptr]] Function [[ex0]]
144 ; CHECK-NOT: OpVariable [[struct]]
145 ;
146 OpCapability Shader
147 OpCapability Linkage
148 OpMemoryModel Logical GLSL450
149 OpName %6 "spec_const"
150 %1 = OpTypeVoid
151 %2 = OpTypeInt 32 0
152 %3 = OpTypeStruct %2 %2
153 %4 = OpTypePointer Function %3
154 %20 = OpTypePointer Function %2
155 %5 = OpTypeFunction %1
156 %6 = OpConstant %2 0
157 %30 = OpConstant %2 1
158 %7 = OpSpecConstant %2 0
159 %8 = OpSpecConstantOp %2 IAdd %7 %7
160 %9 = OpSpecConstantComposite %3 %7 %8
161 %10 = OpFunction %1 None %5
162 %11 = OpLabel
163 %12 = OpVariable %4 Function %9
164 %13 = OpAccessChain %20 %12 %6
165 %14 = OpLoad %2 %13
166 %15 = OpAccessChain %20 %12 %30
167 %16 = OpLoad %2 %15
168 OpReturn
169 OpFunctionEnd
170   )";
171 
172   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
173 }
174 
175 // TODO(alanbaker): Re-enable when vector and matrix scalarization is supported.
176 // TEST_F(ScalarReplacementTest, VectorInitialization) {
177 //  const std::string text = R"(
178 // ;
179 // ; CHECK: [[elem:%\w+]] = OpTypeInt 32 0
180 // ; CHECK: [[vector:%\w+]] = OpTypeVector [[elem]] 4
181 // ; CHECK: [[vector_ptr:%\w+]] = OpTypePointer Function [[vector]]
182 // ; CHECK: [[elem_ptr:%\w+]] = OpTypePointer Function [[elem]]
183 // ; CHECK: [[zero:%\w+]] = OpConstant [[elem]] 0
184 // ; CHECK: [[undef:%\w+]] = OpUndef [[elem]]
185 // ; CHECK: [[two:%\w+]] = OpConstant [[elem]] 2
186 // ; CHECK: [[null:%\w+]] = OpConstantNull [[elem]]
187 // ; CHECK-NOT: OpVariable [[vector_ptr]]
188 // ; CHECK: OpVariable [[elem_ptr]] Function [[zero]]
189 // ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
190 // ; CHECK-NEXT: OpVariable [[elem_ptr]] Function
191 // ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[two]]
192 // ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[null]]
193 // ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
194 // ;
195 //  OpCapability Shader
196 //  OpCapability Linkage
197 //  OpMemoryModel Logical GLSL450
198 //  OpName %6 "vector_init"
199 // %1 = OpTypeVoid
200 // %2 = OpTypeInt 32 0
201 // %3 = OpTypeVector %2 4
202 // %4 = OpTypePointer Function %3
203 // %20 = OpTypePointer Function %2
204 // %6 = OpTypeFunction %1
205 // %7 = OpConstant %2 0
206 // %8 = OpUndef %2
207 // %9 = OpConstant %2 2
208 // %30 = OpConstant %2 1
209 // %31 = OpConstant %2 3
210 // %10 = OpConstantNull %2
211 // %11 = OpConstantComposite %3 %10 %9 %8 %7
212 // %12 = OpFunction %1 None %6
213 // %13 = OpLabel
214 // %14 = OpVariable %4 Function %11
215 // %15 = OpAccessChain %20 %14 %7
216 //  OpStore %15 %10
217 // %16 = OpAccessChain %20 %14 %9
218 //  OpStore %16 %10
219 // %17 = OpAccessChain %20 %14 %30
220 //  OpStore %17 %10
221 // %18 = OpAccessChain %20 %14 %31
222 //  OpStore %18 %10
223 //  OpReturn
224 //  OpFunctionEnd
225 //   )";
226 //
227 //   SinglePassRunAndMatch<opt::ScalarReplacementPass>(text, true);
228 // }
229 //
230 //  TEST_F(ScalarReplacementTest, MatrixInitialization) {
231 //   const std::string text = R"(
232 // ;
233 // ; CHECK: [[float:%\w+]] = OpTypeFloat 32
234 // ; CHECK: [[vector:%\w+]] = OpTypeVector [[float]] 2
235 // ; CHECK: [[matrix:%\w+]] = OpTypeMatrix [[vector]] 2
236 // ; CHECK: [[matrix_ptr:%\w+]] = OpTypePointer Function [[matrix]]
237 // ; CHECK: [[float_ptr:%\w+]] = OpTypePointer Function [[float]]
238 // ; CHECK: [[vec_ptr:%\w+]] = OpTypePointer Function [[vector]]
239 // ; CHECK: [[zerof:%\w+]] = OpConstant [[float]] 0
240 // ; CHECK: [[onef:%\w+]] = OpConstant [[float]] 1
241 // ; CHECK: [[one_zero:%\w+]] = OpConstantComposite [[vector]] [[onef]]
242 // [[zerof]] ; CHECK: [[zero_one:%\w+]] = OpConstantComposite [[vector]]
243 // [[zerof]] [[onef]] ; CHECK: [[const_mat:%\w+]] = OpConstantComposite
244 // [[matrix]] [[one_zero]]
245 // [[zero_one]] ; CHECK-NOT: OpVariable [[matrix]] ; CHECK-NOT: OpVariable
246 // [[vector]] Function [[one_zero]] ; CHECK: [[f1:%\w+]] = OpVariable
247 // [[float_ptr]] Function [[zerof]] ; CHECK-NEXT: [[f2:%\w+]] = OpVariable
248 // [[float_ptr]] Function [[onef]] ; CHECK-NEXT: [[vec_var:%\w+]] = OpVariable
249 // [[vec_ptr]] Function [[zero_one]] ; CHECK-NOT: OpVariable [[matrix]] ;
250 //  CHECK-NOT: OpVariable [[vector]] Function [[one_zero]]
251 // ;
252 //  OpCapability Shader
253 //  OpCapability Linkage
254 //  OpMemoryModel Logical GLSL450
255 //  OpName %7 "matrix_init"
256 // %1 = OpTypeVoid
257 // %2 = OpTypeFloat 32
258 // %3 = OpTypeVector %2 2
259 // %4 = OpTypeMatrix %3 2
260 // %5 = OpTypePointer Function %4
261 // %6 = OpTypePointer Function %2
262 // %30 = OpTypePointer Function %3
263 // %10 = OpTypeInt 32 0
264 // %7 = OpTypeFunction %1 %10
265 // %8 = OpConstant %2 0.0
266 // %9 = OpConstant %2 1.0
267 // %11 = OpConstant %10 0
268 // %12 = OpConstant %10 1
269 // %13 = OpConstantComposite %3 %9 %8
270 // %14 = OpConstantComposite %3 %8 %9
271 // %15 = OpConstantComposite %4 %13 %14
272 // %16 = OpFunction %1 None %7
273 // %31 = OpFunctionParameter %10
274 // %17 = OpLabel
275 // %18 = OpVariable %5 Function %15
276 // %19 = OpAccessChain %6 %18 %11 %12
277 //  OpStore %19 %8
278 // %20 = OpAccessChain %6 %18 %11 %11
279 //  OpStore %20 %8
280 // %21 = OpAccessChain %30 %18 %12
281 //  OpStore %21 %14
282 //  OpReturn
283 //  OpFunctionEnd
284 //   )";
285 //
286 //   SinglePassRunAndMatch<opt::ScalarReplacementPass>(text, true);
287 // }
288 
TEST_F(ScalarReplacementTest,ElideAccessChain)289 TEST_F(ScalarReplacementTest, ElideAccessChain) {
290   const std::string text = R"(
291 ;
292 ; CHECK: [[var:%\w+]] = OpVariable
293 ; CHECK-NOT: OpAccessChain
294 ; CHECK: OpStore [[var]]
295 ;
296 OpCapability Shader
297 OpCapability Linkage
298 OpMemoryModel Logical GLSL450
299 OpName %6 "elide_access_chain"
300 %1 = OpTypeVoid
301 %2 = OpTypeInt 32 0
302 %3 = OpTypeStruct %2 %2 %2 %2
303 %4 = OpTypePointer Function %3
304 %20 = OpTypePointer Function %2
305 %6 = OpTypeFunction %1
306 %7 = OpConstant %2 0
307 %8 = OpUndef %2
308 %9 = OpConstant %2 2
309 %10 = OpConstantNull %2
310 %11 = OpConstantComposite %3 %7 %8 %9 %10
311 %12 = OpFunction %1 None %6
312 %13 = OpLabel
313 %14 = OpVariable %4 Function %11
314 %15 = OpAccessChain %20 %14 %7
315 OpStore %15 %10
316 OpReturn
317 OpFunctionEnd
318   )";
319 
320   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
321 }
322 
TEST_F(ScalarReplacementTest,ElideMultipleAccessChains)323 TEST_F(ScalarReplacementTest, ElideMultipleAccessChains) {
324   const std::string text = R"(
325 ;
326 ; CHECK: [[var:%\w+]] = OpVariable
327 ; CHECK-NOT: OpInBoundsAccessChain
328 ; CHECK OpStore [[var]]
329 ;
330 OpCapability Shader
331 OpCapability Linkage
332 OpMemoryModel Logical GLSL450
333 OpName %6 "elide_two_access_chains"
334 %1 = OpTypeVoid
335 %2 = OpTypeFloat 32
336 %3 = OpTypeStruct %2 %2
337 %4 = OpTypeStruct %3 %3
338 %5 = OpTypePointer Function %4
339 %6 = OpTypePointer Function %2
340 %7 = OpTypeFunction %1
341 %8 = OpConstant %2 0.0
342 %9 = OpConstant %2 1.0
343 %10 = OpTypeInt 32 0
344 %11 = OpConstant %10 0
345 %12 = OpConstant %10 1
346 %13 = OpConstantComposite %3 %9 %8
347 %14 = OpConstantComposite %3 %8 %9
348 %15 = OpConstantComposite %4 %13 %14
349 %16 = OpFunction %1 None %7
350 %17 = OpLabel
351 %18 = OpVariable %5 Function %15
352 %19 = OpInBoundsAccessChain %6 %18 %11 %12
353 OpStore %19 %8
354 OpReturn
355 OpFunctionEnd
356   )";
357 
358   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
359 }
360 
TEST_F(ScalarReplacementTest,ReplaceAccessChain)361 TEST_F(ScalarReplacementTest, ReplaceAccessChain) {
362   const std::string text = R"(
363 ;
364 ; CHECK: [[param:%\w+]] = OpFunctionParameter
365 ; CHECK: [[var:%\w+]] = OpVariable
366 ; CHECK: [[access:%\w+]] = OpAccessChain {{%\w+}} [[var]] [[param]]
367 ; CHECK: OpStore [[access]]
368 ;
369 OpCapability Shader
370 OpCapability Linkage
371 OpMemoryModel Logical GLSL450
372 OpName %7 "replace_access_chain"
373 %1 = OpTypeVoid
374 %2 = OpTypeFloat 32
375 %10 = OpTypeInt 32 0
376 %uint_2 = OpConstant %10 2
377 %3 = OpTypeArray %2 %uint_2
378 %4 = OpTypeStruct %3 %3
379 %5 = OpTypePointer Function %4
380 %20 = OpTypePointer Function %3
381 %6 = OpTypePointer Function %2
382 %7 = OpTypeFunction %1 %10
383 %8 = OpConstant %2 0.0
384 %9 = OpConstant %2 1.0
385 %11 = OpConstant %10 0
386 %12 = OpConstant %10 1
387 %13 = OpConstantComposite %3 %9 %8
388 %14 = OpConstantComposite %3 %8 %9
389 %15 = OpConstantComposite %4 %13 %14
390 %16 = OpFunction %1 None %7
391 %32 = OpFunctionParameter %10
392 %17 = OpLabel
393 %18 = OpVariable %5 Function %15
394 %19 = OpAccessChain %6 %18 %11 %32
395 OpStore %19 %8
396 OpReturn
397 OpFunctionEnd
398   )";
399 
400   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
401 }
402 
TEST_F(ScalarReplacementTest,ArrayInitialization)403 TEST_F(ScalarReplacementTest, ArrayInitialization) {
404   const std::string text = R"(
405 ;
406 ; CHECK: [[float:%\w+]] = OpTypeFloat 32
407 ; CHECK: [[array:%\w+]] = OpTypeArray
408 ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
409 ; CHECK: [[float_ptr:%\w+]] = OpTypePointer Function [[float]]
410 ; CHECK: [[float0:%\w+]] = OpConstant [[float]] 0
411 ; CHECK: [[float1:%\w+]] = OpConstant [[float]] 1
412 ; CHECK: [[float2:%\w+]] = OpConstant [[float]] 2
413 ; CHECK-NOT: OpVariable [[array_ptr]]
414 ; CHECK: [[var0:%\w+]] = OpVariable [[float_ptr]] Function [[float0]]
415 ; CHECK-NEXT: [[var1:%\w+]] = OpVariable [[float_ptr]] Function [[float1]]
416 ; CHECK-NEXT: [[var2:%\w+]] = OpVariable [[float_ptr]] Function [[float2]]
417 ; CHECK-NOT: OpVariable [[array_ptr]]
418 ;
419 OpCapability Shader
420 OpCapability Linkage
421 OpMemoryModel Logical GLSL450
422 OpName %func "array_init"
423 %void = OpTypeVoid
424 %uint = OpTypeInt 32 0
425 %float = OpTypeFloat 32
426 %uint_0 = OpConstant %uint 0
427 %uint_1 = OpConstant %uint 1
428 %uint_2 = OpConstant %uint 2
429 %uint_3 = OpConstant %uint 3
430 %float_array = OpTypeArray %float %uint_3
431 %array_ptr = OpTypePointer Function %float_array
432 %float_ptr = OpTypePointer Function %float
433 %float_0 = OpConstant %float 0
434 %float_1 = OpConstant %float 1
435 %float_2 = OpConstant %float 2
436 %const_array = OpConstantComposite %float_array %float_2 %float_1 %float_0
437 %func = OpTypeFunction %void
438 %1 = OpFunction %void None %func
439 %2 = OpLabel
440 %3 = OpVariable %array_ptr Function %const_array
441 %4 = OpInBoundsAccessChain %float_ptr %3 %uint_0
442 OpStore %4 %float_0
443 %5 = OpInBoundsAccessChain %float_ptr %3 %uint_1
444 OpStore %5 %float_0
445 %6 = OpInBoundsAccessChain %float_ptr %3 %uint_2
446 OpStore %6 %float_0
447 OpReturn
448 OpFunctionEnd
449   )";
450 
451   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
452 }
453 
TEST_F(ScalarReplacementTest,NonUniformCompositeInitialization)454 TEST_F(ScalarReplacementTest, NonUniformCompositeInitialization) {
455   const std::string text = R"(
456 ;
457 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
458 ; CHECK: [[long:%\w+]] = OpTypeInt 64 1
459 ; CHECK: [[dvector:%\w+]] = OpTypeVector
460 ; CHECK: [[vector:%\w+]] = OpTypeVector
461 ; CHECK: [[array:%\w+]] = OpTypeArray
462 ; CHECK: [[matrix:%\w+]] = OpTypeMatrix
463 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[vector]]
464 ; CHECK: [[struct2:%\w+]] = OpTypeStruct [[struct1]] [[matrix]] [[array]] [[uint]]
465 ; CHECK: [[struct1_ptr:%\w+]] = OpTypePointer Function [[struct1]]
466 ; CHECK: [[matrix_ptr:%\w+]] = OpTypePointer Function [[matrix]]
467 ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
468 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
469 ; CHECK: [[struct2_ptr:%\w+]] = OpTypePointer Function [[struct2]]
470 ; CHECK: [[const_array:%\w+]] = OpConstantComposite [[array]]
471 ; CHECK: [[const_matrix:%\w+]] = OpConstantNull [[matrix]]
472 ; CHECK: [[const_struct1:%\w+]] = OpConstantComposite [[struct1]]
473 ; CHECK: OpConstantNull [[uint]]
474 ; CHECK: OpConstantNull [[vector]]
475 ; CHECK: OpConstantNull [[long]]
476 ; CHECK: OpFunction
477 ; CHECK-NOT: OpVariable [[struct2_ptr]] Function
478 ; CHECK: OpVariable [[uint_ptr]] Function
479 ; CHECK-NEXT: OpVariable [[matrix_ptr]] Function [[const_matrix]]
480 ; CHECK-NOT: OpVariable [[struct1_ptr]] Function [[const_struct1]]
481 ; CHECK-NOT: OpVariable [[struct2_ptr]] Function
482 ;
483 OpCapability Shader
484 OpCapability Linkage
485 OpCapability Int64
486 OpCapability Float64
487 OpMemoryModel Logical GLSL450
488 OpName %func "non_uniform_composite_init"
489 %void = OpTypeVoid
490 %uint = OpTypeInt 32 0
491 %int64 = OpTypeInt 64 1
492 %float = OpTypeFloat 32
493 %double = OpTypeFloat 64
494 %double2 = OpTypeVector %double 2
495 %float4 = OpTypeVector %float 4
496 %int64_0 = OpConstant %int64 0
497 %int64_1 = OpConstant %int64 1
498 %int64_2 = OpConstant %int64 2
499 %int64_3 = OpConstant %int64 3
500 %int64_array3 = OpTypeArray %int64 %int64_3
501 %matrix_double2 = OpTypeMatrix %double2 2
502 %struct1 = OpTypeStruct %uint %float4
503 %struct2 = OpTypeStruct %struct1 %matrix_double2 %int64_array3 %uint
504 %struct1_ptr = OpTypePointer Function %struct1
505 %matrix_double2_ptr = OpTypePointer Function %matrix_double2
506 %int64_array_ptr = OpTypePointer Function %int64_array3
507 %uint_ptr = OpTypePointer Function %uint
508 %struct2_ptr = OpTypePointer Function %struct2
509 %const_uint = OpConstant %uint 0
510 %const_int64_array = OpConstantComposite %int64_array3 %int64_0 %int64_1 %int64_2
511 %const_double2 = OpConstantNull %double2
512 %const_matrix_double2 = OpConstantNull %matrix_double2
513 %undef_float4 = OpUndef %float4
514 %const_struct1 = OpConstantComposite %struct1 %const_uint %undef_float4
515 %const_struct2 = OpConstantComposite %struct2 %const_struct1 %const_matrix_double2 %const_int64_array %const_uint
516 %func = OpTypeFunction %void
517 %1 = OpFunction %void None %func
518 %2 = OpLabel
519 %var = OpVariable %struct2_ptr Function %const_struct2
520 %3 = OpAccessChain %struct1_ptr %var %int64_0
521 OpStore %3 %const_struct1
522 %4 = OpAccessChain %matrix_double2_ptr %var %int64_1
523 OpStore %4 %const_matrix_double2
524 %5 = OpAccessChain %int64_array_ptr %var %int64_2
525 OpStore %5 %const_int64_array
526 %6 = OpAccessChain %uint_ptr %var %int64_3
527 OpStore %6 %const_uint
528 OpReturn
529 OpFunctionEnd
530   )";
531 
532   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
533 }
534 
TEST_F(ScalarReplacementTest,ElideUncombinedAccessChains)535 TEST_F(ScalarReplacementTest, ElideUncombinedAccessChains) {
536   const std::string text = R"(
537 ;
538 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
539 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
540 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
541 ; CHECK: [[var:%\w+]] = OpVariable [[uint_ptr]] Function
542 ; CHECK-NOT: OpAccessChain
543 ; CHECK: OpStore [[var]] [[const]]
544 ;
545 OpCapability Shader
546 OpCapability Linkage
547 OpMemoryModel Logical GLSL450
548 OpName %func "elide_uncombined_access_chains"
549 %void = OpTypeVoid
550 %uint = OpTypeInt 32 0
551 %struct1 = OpTypeStruct %uint
552 %struct2 = OpTypeStruct %struct1
553 %uint_ptr = OpTypePointer Function %uint
554 %struct1_ptr = OpTypePointer Function %struct1
555 %struct2_ptr = OpTypePointer Function %struct2
556 %uint_0 = OpConstant %uint 0
557 %func = OpTypeFunction %void
558 %1 = OpFunction %void None %func
559 %2 = OpLabel
560 %var = OpVariable %struct2_ptr Function
561 %3 = OpAccessChain %struct1_ptr %var %uint_0
562 %4 = OpAccessChain %uint_ptr %3 %uint_0
563 OpStore %4 %uint_0
564 OpReturn
565 OpFunctionEnd
566   )";
567 
568   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
569 }
570 
TEST_F(ScalarReplacementTest,ElideSingleUncombinedAccessChains)571 TEST_F(ScalarReplacementTest, ElideSingleUncombinedAccessChains) {
572   const std::string text = R"(
573 ;
574 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
575 ; CHECK: [[array:%\w+]] = OpTypeArray [[uint]]
576 ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
577 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
578 ; CHECK: [[param:%\w+]] = OpFunctionParameter [[uint]]
579 ; CHECK: [[var:%\w+]] = OpVariable [[array_ptr]] Function
580 ; CHECK: [[access:%\w+]] = OpAccessChain {{.*}} [[var]] [[param]]
581 ; CHECK: OpStore [[access]] [[const]]
582 ;
583 OpCapability Shader
584 OpCapability Linkage
585 OpMemoryModel Logical GLSL450
586 OpName %func "elide_single_uncombined_access_chains"
587 %void = OpTypeVoid
588 %uint = OpTypeInt 32 0
589 %uint_1 = OpConstant %uint 1
590 %array = OpTypeArray %uint %uint_1
591 %struct2 = OpTypeStruct %array
592 %uint_ptr = OpTypePointer Function %uint
593 %array_ptr = OpTypePointer Function %array
594 %struct2_ptr = OpTypePointer Function %struct2
595 %uint_0 = OpConstant %uint 0
596 %func = OpTypeFunction %void %uint
597 %1 = OpFunction %void None %func
598 %param = OpFunctionParameter %uint
599 %2 = OpLabel
600 %var = OpVariable %struct2_ptr Function
601 %3 = OpAccessChain %array_ptr %var %uint_0
602 %4 = OpAccessChain %uint_ptr %3 %param
603 OpStore %4 %uint_0
604 OpReturn
605 OpFunctionEnd
606   )";
607 
608   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
609 }
610 
TEST_F(ScalarReplacementTest,ReplaceWholeLoad)611 TEST_F(ScalarReplacementTest, ReplaceWholeLoad) {
612   const std::string text = R"(
613 ;
614 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
615 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
616 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
617 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
618 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
619 ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
620 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
621 ; CHECK: [[l0:%\w+]] = OpLoad [[uint]] [[var0]]
622 ; CHECK: OpCompositeConstruct [[struct1]] [[l0]] [[l1]]
623 ;
624 OpCapability Shader
625 OpCapability Linkage
626 OpMemoryModel Logical GLSL450
627 OpName %func "replace_whole_load"
628 %void = OpTypeVoid
629 %uint = OpTypeInt 32 0
630 %struct1 = OpTypeStruct %uint %uint
631 %uint_ptr = OpTypePointer Function %uint
632 %struct1_ptr = OpTypePointer Function %struct1
633 %uint_0 = OpConstant %uint 0
634 %uint_1 = OpConstant %uint 1
635 %func = OpTypeFunction %void
636 %1 = OpFunction %void None %func
637 %2 = OpLabel
638 %var = OpVariable %struct1_ptr Function
639 %load = OpLoad %struct1 %var
640 %3 = OpAccessChain %uint_ptr %var %uint_0
641 OpStore %3 %uint_0
642 %4 = OpAccessChain %uint_ptr %var %uint_1
643 OpStore %4 %uint_0
644 OpReturn
645 OpFunctionEnd
646   )";
647 
648   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
649 }
650 
TEST_F(ScalarReplacementTest,ReplaceWholeLoadCopyMemoryAccess)651 TEST_F(ScalarReplacementTest, ReplaceWholeLoadCopyMemoryAccess) {
652   const std::string text = R"(
653 ;
654 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
655 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
656 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
657 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
658 ; CHECK: [[null:%\w+]] = OpConstantNull [[uint]]
659 ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
660 ; CHECK: [[l0:%\w+]] = OpLoad [[uint]] [[var0]] Nontemporal
661 ; CHECK: OpCompositeConstruct [[struct1]] [[l0]] [[null]]
662 ;
663 OpCapability Shader
664 OpCapability Linkage
665 OpMemoryModel Logical GLSL450
666 OpName %func "replace_whole_load_copy_memory_access"
667 %void = OpTypeVoid
668 %uint = OpTypeInt 32 0
669 %struct1 = OpTypeStruct %uint %uint
670 %uint_ptr = OpTypePointer Function %uint
671 %struct1_ptr = OpTypePointer Function %struct1
672 %uint_0 = OpConstant %uint 0
673 %func = OpTypeFunction %void
674 %1 = OpFunction %void None %func
675 %2 = OpLabel
676 %var = OpVariable %struct1_ptr Function
677 %load = OpLoad %struct1 %var Nontemporal
678 %3 = OpAccessChain %uint_ptr %var %uint_0
679 OpStore %3 %uint_0
680 OpReturn
681 OpFunctionEnd
682   )";
683 
684   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
685 }
686 
TEST_F(ScalarReplacementTest,ReplaceWholeStore)687 TEST_F(ScalarReplacementTest, ReplaceWholeStore) {
688   const std::string text = R"(
689 ;
690 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
691 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
692 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
693 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
694 ; CHECK: [[const_struct:%\w+]] = OpConstantComposite [[struct1]] [[const]] [[const]]
695 ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
696 ; CHECK: [[ex0:%\w+]] = OpCompositeExtract [[uint]] [[const_struct]] 0
697 ; CHECK: OpStore [[var0]] [[ex0]]
698 ;
699 OpCapability Shader
700 OpCapability Linkage
701 OpMemoryModel Logical GLSL450
702 OpName %func "replace_whole_store"
703 %void = OpTypeVoid
704 %uint = OpTypeInt 32 0
705 %struct1 = OpTypeStruct %uint %uint
706 %uint_ptr = OpTypePointer Function %uint
707 %struct1_ptr = OpTypePointer Function %struct1
708 %uint_0 = OpConstant %uint 0
709 %const_struct = OpConstantComposite %struct1 %uint_0 %uint_0
710 %func = OpTypeFunction %void
711 %1 = OpFunction %void None %func
712 %2 = OpLabel
713 %var = OpVariable %struct1_ptr Function
714 OpStore %var %const_struct
715 %3 = OpAccessChain %uint_ptr %var %uint_0
716 %4 = OpLoad %uint %3
717 OpReturn
718 OpFunctionEnd
719   )";
720 
721   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
722 }
723 
TEST_F(ScalarReplacementTest,ReplaceWholeStoreCopyMemoryAccess)724 TEST_F(ScalarReplacementTest, ReplaceWholeStoreCopyMemoryAccess) {
725   const std::string text = R"(
726 ;
727 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
728 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
729 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
730 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
731 ; CHECK: [[const_struct:%\w+]] = OpConstantComposite [[struct1]] [[const]] [[const]]
732 ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
733 ; CHECK-NOT: OpVariable
734 ; CHECK: [[ex0:%\w+]] = OpCompositeExtract [[uint]] [[const_struct]] 0
735 ; CHECK: OpStore [[var0]] [[ex0]] Aligned 4
736 ;
737 OpCapability Shader
738 OpCapability Linkage
739 OpMemoryModel Logical GLSL450
740 OpName %func "replace_whole_store_copy_memory_access"
741 %void = OpTypeVoid
742 %uint = OpTypeInt 32 0
743 %struct1 = OpTypeStruct %uint %uint
744 %uint_ptr = OpTypePointer Function %uint
745 %struct1_ptr = OpTypePointer Function %struct1
746 %uint_0 = OpConstant %uint 0
747 %const_struct = OpConstantComposite %struct1 %uint_0 %uint_0
748 %func = OpTypeFunction %void
749 %1 = OpFunction %void None %func
750 %2 = OpLabel
751 %var = OpVariable %struct1_ptr Function
752 OpStore %var %const_struct Aligned 4
753 %3 = OpAccessChain %uint_ptr %var %uint_0
754 %4 = OpLoad %uint %3
755 OpReturn
756 OpFunctionEnd
757   )";
758 
759   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
760 }
761 
TEST_F(ScalarReplacementTest,DontTouchVolatileLoad)762 TEST_F(ScalarReplacementTest, DontTouchVolatileLoad) {
763   const std::string text = R"(
764 ;
765 ; CHECK: [[struct:%\w+]] = OpTypeStruct
766 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
767 ; CHECK: OpLabel
768 ; CHECK-NEXT: OpVariable [[struct_ptr]]
769 ; CHECK-NOT: OpVariable
770 ;
771 OpCapability Shader
772 OpCapability Linkage
773 OpMemoryModel Logical GLSL450
774 OpName %func "dont_touch_volatile_load"
775 %void = OpTypeVoid
776 %uint = OpTypeInt 32 0
777 %struct1 = OpTypeStruct %uint
778 %uint_ptr = OpTypePointer Function %uint
779 %struct1_ptr = OpTypePointer Function %struct1
780 %uint_0 = OpConstant %uint 0
781 %func = OpTypeFunction %void
782 %1 = OpFunction %void None %func
783 %2 = OpLabel
784 %var = OpVariable %struct1_ptr Function
785 %3 = OpAccessChain %uint_ptr %var %uint_0
786 %4 = OpLoad %uint %3 Volatile
787 OpReturn
788 OpFunctionEnd
789   )";
790 
791   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
792 }
793 
TEST_F(ScalarReplacementTest,DontTouchVolatileStore)794 TEST_F(ScalarReplacementTest, DontTouchVolatileStore) {
795   const std::string text = R"(
796 ;
797 ; CHECK: [[struct:%\w+]] = OpTypeStruct
798 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
799 ; CHECK: OpLabel
800 ; CHECK-NEXT: OpVariable [[struct_ptr]]
801 ; CHECK-NOT: OpVariable
802 ;
803 OpCapability Shader
804 OpCapability Linkage
805 OpMemoryModel Logical GLSL450
806 OpName %func "dont_touch_volatile_store"
807 %void = OpTypeVoid
808 %uint = OpTypeInt 32 0
809 %struct1 = OpTypeStruct %uint
810 %uint_ptr = OpTypePointer Function %uint
811 %struct1_ptr = OpTypePointer Function %struct1
812 %uint_0 = OpConstant %uint 0
813 %func = OpTypeFunction %void
814 %1 = OpFunction %void None %func
815 %2 = OpLabel
816 %var = OpVariable %struct1_ptr Function
817 %3 = OpAccessChain %uint_ptr %var %uint_0
818 OpStore %3 %uint_0 Volatile
819 OpReturn
820 OpFunctionEnd
821   )";
822 
823   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
824 }
825 
TEST_F(ScalarReplacementTest,DontTouchSpecNonFunctionVariable)826 TEST_F(ScalarReplacementTest, DontTouchSpecNonFunctionVariable) {
827   const std::string text = R"(
828 ;
829 ; CHECK: [[struct:%\w+]] = OpTypeStruct
830 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Uniform [[struct]]
831 ; CHECK: OpConstant
832 ; CHECK-NEXT: OpVariable [[struct_ptr]]
833 ; CHECK-NOT: OpVariable
834 ;
835 OpCapability Shader
836 OpCapability Linkage
837 OpMemoryModel Logical GLSL450
838 OpName %func "dont_touch_spec_constant_access_chain"
839 %void = OpTypeVoid
840 %uint = OpTypeInt 32 0
841 %struct1 = OpTypeStruct %uint
842 %uint_ptr = OpTypePointer Uniform %uint
843 %struct1_ptr = OpTypePointer Uniform %struct1
844 %uint_0 = OpConstant %uint 0
845 %var = OpVariable %struct1_ptr Uniform
846 %func = OpTypeFunction %void
847 %1 = OpFunction %void None %func
848 %2 = OpLabel
849 %3 = OpAccessChain %uint_ptr %var %uint_0
850 OpStore %3 %uint_0 Volatile
851 OpReturn
852 OpFunctionEnd
853   )";
854 
855   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
856 }
857 
TEST_F(ScalarReplacementTest,DontTouchSpecConstantAccessChain)858 TEST_F(ScalarReplacementTest, DontTouchSpecConstantAccessChain) {
859   const std::string text = R"(
860 ;
861 ; CHECK: [[array:%\w+]] = OpTypeArray
862 ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
863 ; CHECK: OpLabel
864 ; CHECK-NEXT: OpVariable [[array_ptr]]
865 ; CHECK-NOT: OpVariable
866 ;
867 OpCapability Shader
868 OpCapability Linkage
869 OpMemoryModel Logical GLSL450
870 OpName %func "dont_touch_spec_constant_access_chain"
871 %void = OpTypeVoid
872 %uint = OpTypeInt 32 0
873 %uint_1 = OpConstant %uint 1
874 %array = OpTypeArray %uint %uint_1
875 %uint_ptr = OpTypePointer Function %uint
876 %array_ptr = OpTypePointer Function %array
877 %uint_0 = OpConstant %uint 0
878 %spec_const = OpSpecConstant %uint 0
879 %func = OpTypeFunction %void
880 %1 = OpFunction %void None %func
881 %2 = OpLabel
882 %var = OpVariable %array_ptr Function
883 %3 = OpAccessChain %uint_ptr %var %spec_const
884 OpStore %3 %uint_0 Volatile
885 OpReturn
886 OpFunctionEnd
887   )";
888 
889   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
890 }
891 
TEST_F(ScalarReplacementTest,NoPartialAccesses)892 TEST_F(ScalarReplacementTest, NoPartialAccesses) {
893   const std::string text = R"(
894 ;
895 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
896 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
897 ; CHECK: OpLabel
898 ; CHECK-NOT: OpVariable
899 ;
900 OpCapability Shader
901 OpCapability Linkage
902 OpMemoryModel Logical GLSL450
903 OpName %func "no_partial_accesses"
904 %void = OpTypeVoid
905 %uint = OpTypeInt 32 0
906 %struct1 = OpTypeStruct %uint
907 %uint_ptr = OpTypePointer Function %uint
908 %struct1_ptr = OpTypePointer Function %struct1
909 %const = OpConstantNull %struct1
910 %func = OpTypeFunction %void
911 %1 = OpFunction %void None %func
912 %2 = OpLabel
913 %var = OpVariable %struct1_ptr Function
914 OpStore %var %const
915 OpReturn
916 OpFunctionEnd
917   )";
918 
919   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
920 }
921 
TEST_F(ScalarReplacementTest,DontTouchPtrAccessChain)922 TEST_F(ScalarReplacementTest, DontTouchPtrAccessChain) {
923   const std::string text = R"(
924 ;
925 ; CHECK: [[struct:%\w+]] = OpTypeStruct
926 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
927 ; CHECK: OpLabel
928 ; CHECK-NEXT: OpVariable [[struct_ptr]]
929 ; CHECK-NOT: OpVariable
930 ;
931 OpCapability Shader
932 OpCapability Linkage
933 OpMemoryModel Logical GLSL450
934 OpName %func "dont_touch_ptr_access_chain"
935 %void = OpTypeVoid
936 %uint = OpTypeInt 32 0
937 %struct1 = OpTypeStruct %uint
938 %uint_ptr = OpTypePointer Function %uint
939 %struct1_ptr = OpTypePointer Function %struct1
940 %uint_0 = OpConstant %uint 0
941 %func = OpTypeFunction %void
942 %1 = OpFunction %void None %func
943 %2 = OpLabel
944 %var = OpVariable %struct1_ptr Function
945 %3 = OpPtrAccessChain %uint_ptr %var %uint_0 %uint_0
946 OpStore %3 %uint_0
947 %4 = OpAccessChain %uint_ptr %var %uint_0
948 OpStore %4 %uint_0
949 OpReturn
950 OpFunctionEnd
951   )";
952 
953   SinglePassRunAndMatch<ScalarReplacementPass>(text, false);
954 }
955 
TEST_F(ScalarReplacementTest,DontTouchInBoundsPtrAccessChain)956 TEST_F(ScalarReplacementTest, DontTouchInBoundsPtrAccessChain) {
957   const std::string text = R"(
958 ;
959 ; CHECK: [[struct:%\w+]] = OpTypeStruct
960 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
961 ; CHECK: OpLabel
962 ; CHECK-NEXT: OpVariable [[struct_ptr]]
963 ; CHECK-NOT: OpVariable
964 ;
965 OpCapability Shader
966 OpCapability Linkage
967 OpMemoryModel Logical GLSL450
968 OpName %func "dont_touch_in_bounds_ptr_access_chain"
969 %void = OpTypeVoid
970 %uint = OpTypeInt 32 0
971 %struct1 = OpTypeStruct %uint
972 %uint_ptr = OpTypePointer Function %uint
973 %struct1_ptr = OpTypePointer Function %struct1
974 %uint_0 = OpConstant %uint 0
975 %func = OpTypeFunction %void
976 %1 = OpFunction %void None %func
977 %2 = OpLabel
978 %var = OpVariable %struct1_ptr Function
979 %3 = OpInBoundsPtrAccessChain %uint_ptr %var %uint_0 %uint_0
980 OpStore %3 %uint_0
981 %4 = OpInBoundsAccessChain %uint_ptr %var %uint_0
982 OpStore %4 %uint_0
983 OpReturn
984 OpFunctionEnd
985   )";
986 
987   SinglePassRunAndMatch<ScalarReplacementPass>(text, false);
988 }
989 
TEST_F(ScalarReplacementTest,DonTouchAliasedDecoration)990 TEST_F(ScalarReplacementTest, DonTouchAliasedDecoration) {
991   const std::string text = R"(
992 ;
993 ; CHECK: [[struct:%\w+]] = OpTypeStruct
994 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
995 ; CHECK: OpLabel
996 ; CHECK-NEXT: OpVariable [[struct_ptr]]
997 ; CHECK-NOT: OpVariable
998 ;
999 OpCapability Shader
1000 OpCapability Linkage
1001 OpMemoryModel Logical GLSL450
1002 OpName %func "aliased"
1003 OpDecorate %var Aliased
1004 %void = OpTypeVoid
1005 %uint = OpTypeInt 32 0
1006 %struct1 = OpTypeStruct %uint
1007 %uint_ptr = OpTypePointer Function %uint
1008 %struct1_ptr = OpTypePointer Function %struct1
1009 %uint_0 = OpConstant %uint 0
1010 %func = OpTypeFunction %void
1011 %1 = OpFunction %void None %func
1012 %2 = OpLabel
1013 %var = OpVariable %struct1_ptr Function
1014 %3 = OpAccessChain %uint_ptr %var %uint_0
1015 %4 = OpLoad %uint %3
1016 OpReturn
1017 OpFunctionEnd
1018   )";
1019 
1020   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1021 }
1022 
TEST_F(ScalarReplacementTest,CopyRestrictDecoration)1023 TEST_F(ScalarReplacementTest, CopyRestrictDecoration) {
1024   const std::string text = R"(
1025 ;
1026 ; CHECK: OpName
1027 ; CHECK-NEXT: OpDecorate [[var0:%\w+]] Restrict
1028 ; CHECK-NEXT: OpDecorate [[var1:%\w+]] Restrict
1029 ; CHECK: [[int:%\w+]] = OpTypeInt
1030 ; CHECK: [[struct:%\w+]] = OpTypeStruct
1031 ; CHECK: [[int_ptr:%\w+]] = OpTypePointer Function [[int]]
1032 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
1033 ; CHECK: OpLabel
1034 ; CHECK-NEXT: [[var1]] = OpVariable [[int_ptr]]
1035 ; CHECK-NEXT: [[var0]] = OpVariable [[int_ptr]]
1036 ; CHECK-NOT: OpVariable [[struct_ptr]]
1037 ;
1038 OpCapability Shader
1039 OpCapability Linkage
1040 OpMemoryModel Logical GLSL450
1041 OpName %func "restrict"
1042 OpDecorate %var Restrict
1043 %void = OpTypeVoid
1044 %uint = OpTypeInt 32 0
1045 %struct1 = OpTypeStruct %uint %uint
1046 %uint_ptr = OpTypePointer Function %uint
1047 %struct1_ptr = OpTypePointer Function %struct1
1048 %uint_0 = OpConstant %uint 0
1049 %uint_1 = OpConstant %uint 1
1050 %func = OpTypeFunction %void
1051 %1 = OpFunction %void None %func
1052 %2 = OpLabel
1053 %var = OpVariable %struct1_ptr Function
1054 %3 = OpAccessChain %uint_ptr %var %uint_0
1055 %4 = OpLoad %uint %3
1056 %5 = OpAccessChain %uint_ptr %var %uint_1
1057 %6 = OpLoad %uint %5
1058 OpReturn
1059 OpFunctionEnd
1060   )";
1061 
1062   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1063 }
1064 
TEST_F(ScalarReplacementTest,DontClobberDecoratesOnSubtypes)1065 TEST_F(ScalarReplacementTest, DontClobberDecoratesOnSubtypes) {
1066   const std::string text = R"(
1067 ;
1068 ; CHECK: OpDecorate [[array:%\w+]] ArrayStride 1
1069 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1070 ; CHECK: [[array]] = OpTypeArray [[uint]]
1071 ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
1072 ; CHECK: OpLabel
1073 ; CHECK-NEXT: OpVariable [[array_ptr]] Function
1074 ; CHECK-NOT: OpVariable
1075 ;
1076 OpCapability Shader
1077 OpCapability Linkage
1078 OpMemoryModel Logical GLSL450
1079 OpName %func "array_stride"
1080 OpDecorate %array ArrayStride 1
1081 %void = OpTypeVoid
1082 %uint = OpTypeInt 32 0
1083 %uint_1 = OpConstant %uint 1
1084 %array = OpTypeArray %uint %uint_1
1085 %struct1 = OpTypeStruct %array
1086 %uint_ptr = OpTypePointer Function %uint
1087 %struct1_ptr = OpTypePointer Function %struct1
1088 %uint_0 = OpConstant %uint 0
1089 %func = OpTypeFunction %void %uint
1090 %1 = OpFunction %void None %func
1091 %param = OpFunctionParameter %uint
1092 %2 = OpLabel
1093 %var = OpVariable %struct1_ptr Function
1094 %3 = OpAccessChain %uint_ptr %var %uint_0 %param
1095 %4 = OpLoad %uint %3
1096 OpReturn
1097 OpFunctionEnd
1098   )";
1099 
1100   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1101 }
1102 
TEST_F(ScalarReplacementTest,DontCopyMemberDecorate)1103 TEST_F(ScalarReplacementTest, DontCopyMemberDecorate) {
1104   const std::string text = R"(
1105 ;
1106 ; CHECK-NOT: OpDecorate
1107 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1108 ; CHECK: [[struct:%\w+]] = OpTypeStruct [[uint]]
1109 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1110 ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
1111 ; CHECK: OpLabel
1112 ; CHECK-NEXT: OpVariable [[uint_ptr]] Function
1113 ; CHECK-NOT: OpVariable
1114 ;
1115 OpCapability Shader
1116 OpCapability Linkage
1117 OpMemoryModel Logical GLSL450
1118 OpName %func "member_decorate"
1119 OpMemberDecorate %struct1 0 Offset 1
1120 %void = OpTypeVoid
1121 %uint = OpTypeInt 32 0
1122 %uint_1 = OpConstant %uint 1
1123 %struct1 = OpTypeStruct %uint
1124 %uint_ptr = OpTypePointer Function %uint
1125 %struct1_ptr = OpTypePointer Function %struct1
1126 %uint_0 = OpConstant %uint 0
1127 %func = OpTypeFunction %void %uint
1128 %1 = OpFunction %void None %func
1129 %2 = OpLabel
1130 %var = OpVariable %struct1_ptr Function
1131 %3 = OpAccessChain %uint_ptr %var %uint_0
1132 %4 = OpLoad %uint %3
1133 OpReturn
1134 OpFunctionEnd
1135   )";
1136 
1137   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1138 }
1139 
TEST_F(ScalarReplacementTest,NoPartialAccesses2)1140 TEST_F(ScalarReplacementTest, NoPartialAccesses2) {
1141   const std::string text = R"(
1142 ;
1143 ; CHECK: [[float:%\w+]] = OpTypeFloat 32
1144 ; CHECK: [[float_ptr:%\w+]] = OpTypePointer Function [[float]]
1145 ; CHECK: OpVariable [[float_ptr]] Function
1146 ; CHECK: OpVariable [[float_ptr]] Function
1147 ; CHECK: OpVariable [[float_ptr]] Function
1148 ; CHECK: OpVariable [[float_ptr]] Function
1149 ; CHECK: OpVariable [[float_ptr]] Function
1150 ; CHECK: OpVariable [[float_ptr]] Function
1151 ; CHECK: OpVariable [[float_ptr]] Function
1152 ; CHECK-NOT: OpVariable
1153 ;
1154 OpCapability Shader
1155 %1 = OpExtInstImport "GLSL.std.450"
1156 OpMemoryModel Logical GLSL450
1157 OpEntryPoint Fragment %main "main" %fo
1158 OpExecutionMode %main OriginUpperLeft
1159 OpSource GLSL 430
1160 OpName %main "main"
1161 OpName %S "S"
1162 OpMemberName %S 0 "x"
1163 OpMemberName %S 1 "y"
1164 OpName %ts1 "ts1"
1165 OpName %S_0 "S"
1166 OpMemberName %S_0 0 "x"
1167 OpMemberName %S_0 1 "y"
1168 OpName %U_t "U_t"
1169 OpMemberName %U_t 0 "g_s1"
1170 OpMemberName %U_t 1 "g_s2"
1171 OpMemberName %U_t 2 "g_s3"
1172 OpName %_ ""
1173 OpName %ts2 "ts2"
1174 OpName %_Globals_ "_Globals_"
1175 OpMemberName %_Globals_ 0 "g_b"
1176 OpName %__0 ""
1177 OpName %ts3 "ts3"
1178 OpName %ts4 "ts4"
1179 OpName %fo "fo"
1180 OpMemberDecorate %S_0 0 Offset 0
1181 OpMemberDecorate %S_0 1 Offset 4
1182 OpMemberDecorate %U_t 0 Offset 0
1183 OpMemberDecorate %U_t 1 Offset 8
1184 OpMemberDecorate %U_t 2 Offset 16
1185 OpDecorate %U_t BufferBlock
1186 OpDecorate %_ DescriptorSet 0
1187 OpMemberDecorate %_Globals_ 0 Offset 0
1188 OpDecorate %_Globals_ Block
1189 OpDecorate %__0 DescriptorSet 0
1190 OpDecorate %__0 Binding 0
1191 OpDecorate %fo Location 0
1192 %void = OpTypeVoid
1193 %15 = OpTypeFunction %void
1194 %float = OpTypeFloat 32
1195 %S = OpTypeStruct %float %float
1196 %_ptr_Function_S = OpTypePointer Function %S
1197 %S_0 = OpTypeStruct %float %float
1198 %U_t = OpTypeStruct %S_0 %S_0 %S_0
1199 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
1200 %_ = OpVariable %_ptr_Uniform_U_t Uniform
1201 %int = OpTypeInt 32 1
1202 %int_0 = OpConstant %int 0
1203 %_ptr_Uniform_S_0 = OpTypePointer Uniform %S_0
1204 %_ptr_Function_float = OpTypePointer Function %float
1205 %int_1 = OpConstant %int 1
1206 %uint = OpTypeInt 32 0
1207 %_Globals_ = OpTypeStruct %uint
1208 %_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
1209 %__0 = OpVariable %_ptr_Uniform__Globals_ Uniform
1210 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
1211 %bool = OpTypeBool
1212 %uint_0 = OpConstant %uint 0
1213 %_ptr_Output_float = OpTypePointer Output %float
1214 %fo = OpVariable %_ptr_Output_float Output
1215 %main = OpFunction %void None %15
1216 %30 = OpLabel
1217 %ts1 = OpVariable %_ptr_Function_S Function
1218 %ts2 = OpVariable %_ptr_Function_S Function
1219 %ts3 = OpVariable %_ptr_Function_S Function
1220 %ts4 = OpVariable %_ptr_Function_S Function
1221 %31 = OpAccessChain %_ptr_Uniform_S_0 %_ %int_0
1222 %32 = OpLoad %S_0 %31
1223 %33 = OpCompositeExtract %float %32 0
1224 %34 = OpAccessChain %_ptr_Function_float %ts1 %int_0
1225 OpStore %34 %33
1226 %35 = OpCompositeExtract %float %32 1
1227 %36 = OpAccessChain %_ptr_Function_float %ts1 %int_1
1228 OpStore %36 %35
1229 %37 = OpAccessChain %_ptr_Uniform_S_0 %_ %int_1
1230 %38 = OpLoad %S_0 %37
1231 %39 = OpCompositeExtract %float %38 0
1232 %40 = OpAccessChain %_ptr_Function_float %ts2 %int_0
1233 OpStore %40 %39
1234 %41 = OpCompositeExtract %float %38 1
1235 %42 = OpAccessChain %_ptr_Function_float %ts2 %int_1
1236 OpStore %42 %41
1237 %43 = OpAccessChain %_ptr_Uniform_uint %__0 %int_0
1238 %44 = OpLoad %uint %43
1239 %45 = OpINotEqual %bool %44 %uint_0
1240 OpSelectionMerge %46 None
1241 OpBranchConditional %45 %47 %48
1242 %47 = OpLabel
1243 %49 = OpLoad %S %ts1
1244 OpStore %ts3 %49
1245 OpBranch %46
1246 %48 = OpLabel
1247 %50 = OpLoad %S %ts2
1248 OpStore %ts3 %50
1249 OpBranch %46
1250 %46 = OpLabel
1251 %51 = OpLoad %S %ts3
1252 OpStore %ts4 %51
1253 %52 = OpAccessChain %_ptr_Function_float %ts4 %int_1
1254 %53 = OpLoad %float %52
1255 OpStore %fo %53
1256 OpReturn
1257 OpFunctionEnd
1258   )";
1259 
1260   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1261 }
1262 
TEST_F(ScalarReplacementTest,ReplaceWholeLoadAndStore)1263 TEST_F(ScalarReplacementTest, ReplaceWholeLoadAndStore) {
1264   const std::string text = R"(
1265 ;
1266 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1267 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
1268 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1269 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1270 ; CHECK: [[null:%\w+]] = OpConstantNull [[uint]]
1271 ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
1272 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1273 ; CHECK-NOT: OpVariable
1274 ; CHECK: [[l0:%\w+]] = OpLoad [[uint]] [[var0]]
1275 ; CHECK: [[c0:%\w+]] = OpCompositeConstruct [[struct1]] [[l0]] [[null]]
1276 ; CHECK: [[e0:%\w+]] = OpCompositeExtract [[uint]] [[c0]] 0
1277 ; CHECK: OpStore [[var1]] [[e0]]
1278 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1279 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1280 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1281 ;
1282 OpCapability Shader
1283 OpCapability Linkage
1284 OpMemoryModel Logical GLSL450
1285 OpName %func "replace_whole_load"
1286 %void = OpTypeVoid
1287 %uint = OpTypeInt 32 0
1288 %struct1 = OpTypeStruct %uint %uint
1289 %uint_ptr = OpTypePointer Function %uint
1290 %struct1_ptr = OpTypePointer Function %struct1
1291 %uint_0 = OpConstant %uint 0
1292 %uint_1 = OpConstant %uint 1
1293 %func = OpTypeFunction %void
1294 %1 = OpFunction %void None %func
1295 %2 = OpLabel
1296 %var2 = OpVariable %struct1_ptr Function
1297 %var1 = OpVariable %struct1_ptr Function
1298 %load1 = OpLoad %struct1 %var1
1299 OpStore %var2 %load1
1300 %load2 = OpLoad %struct1 %var2
1301 %3 = OpCompositeExtract %uint %load2 0
1302 OpReturn
1303 OpFunctionEnd
1304   )";
1305 
1306   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1307 }
1308 
TEST_F(ScalarReplacementTest,ReplaceWholeLoadAndStore2)1309 TEST_F(ScalarReplacementTest, ReplaceWholeLoadAndStore2) {
1310   // TODO: We can improve this case by ensuring that |var2| is processed first.
1311   const std::string text = R"(
1312 ;
1313 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1314 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
1315 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1316 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1317 ; CHECK: [[null:%\w+]] = OpConstantNull [[uint]]
1318 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1319 ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
1320 ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
1321 ; CHECK-NOT: OpVariable
1322 ; CHECK: [[l0a:%\w+]] = OpLoad [[uint]] [[var0a]]
1323 ; CHECK: [[l0b:%\w+]] = OpLoad [[uint]] [[var0b]]
1324 ; CHECK: [[c0:%\w+]] = OpCompositeConstruct [[struct1]] [[l0b]] [[l0a]]
1325 ; CHECK: [[e0:%\w+]] = OpCompositeExtract [[uint]] [[c0]] 0
1326 ; CHECK: OpStore [[var1]] [[e0]]
1327 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1328 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1329 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1330 ;
1331 OpCapability Shader
1332 OpCapability Linkage
1333 OpMemoryModel Logical GLSL450
1334 OpName %func "replace_whole_load"
1335 %void = OpTypeVoid
1336 %uint = OpTypeInt 32 0
1337 %struct1 = OpTypeStruct %uint %uint
1338 %uint_ptr = OpTypePointer Function %uint
1339 %struct1_ptr = OpTypePointer Function %struct1
1340 %uint_0 = OpConstant %uint 0
1341 %uint_1 = OpConstant %uint 1
1342 %func = OpTypeFunction %void
1343 %1 = OpFunction %void None %func
1344 %2 = OpLabel
1345 %var1 = OpVariable %struct1_ptr Function
1346 %var2 = OpVariable %struct1_ptr Function
1347 %load1 = OpLoad %struct1 %var1
1348 OpStore %var2 %load1
1349 %load2 = OpLoad %struct1 %var2
1350 %3 = OpCompositeExtract %uint %load2 0
1351 OpReturn
1352 OpFunctionEnd
1353   )";
1354 
1355   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1356 }
1357 
TEST_F(ScalarReplacementTest,CreateAmbiguousNullConstant1)1358 TEST_F(ScalarReplacementTest, CreateAmbiguousNullConstant1) {
1359   const std::string text = R"(
1360 ;
1361 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1362 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[struct_member:%\w+]]
1363 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1364 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1365 ; CHECK: [[null:%\w+]] = OpConstantNull [[struct_member]]
1366 ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
1367 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1368 ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
1369 ; CHECK-NOT: OpVariable
1370 ; CHECK: OpStore [[var1]]
1371 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1372 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1373 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1374 ;
1375 OpCapability Shader
1376 OpCapability Linkage
1377 OpMemoryModel Logical GLSL450
1378 OpName %func "replace_whole_load"
1379 %void = OpTypeVoid
1380 %uint = OpTypeInt 32 0
1381 %struct2 = OpTypeStruct %uint
1382 %struct3 = OpTypeStruct %uint
1383 %struct1 = OpTypeStruct %uint %struct2
1384 %uint_ptr = OpTypePointer Function %uint
1385 %struct1_ptr = OpTypePointer Function %struct1
1386 %uint_0 = OpConstant %uint 0
1387 %uint_1 = OpConstant %uint 1
1388 %func = OpTypeFunction %void
1389 %1 = OpFunction %void None %func
1390 %2 = OpLabel
1391 %var1 = OpVariable %struct1_ptr Function
1392 %var2 = OpVariable %struct1_ptr Function
1393 %load1 = OpLoad %struct1 %var1
1394 OpStore %var2 %load1
1395 %load2 = OpLoad %struct1 %var2
1396 %3 = OpCompositeExtract %uint %load2 0
1397 OpReturn
1398 OpFunctionEnd
1399   )";
1400 
1401   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1402 }
1403 
TEST_F(ScalarReplacementTest,SpecConstantArray)1404 TEST_F(ScalarReplacementTest, SpecConstantArray) {
1405   const std::string text = R"(
1406 ; CHECK: [[int:%\w+]] = OpTypeInt
1407 ; CHECK: [[spec_const:%\w+]] = OpSpecConstant [[int]] 4
1408 ; CHECK: [[spec_op:%\w+]] = OpSpecConstantOp [[int]] IAdd [[spec_const]] [[spec_const]]
1409 ; CHECK: [[array1:%\w+]] = OpTypeArray [[int]] [[spec_const]]
1410 ; CHECK: [[array2:%\w+]] = OpTypeArray [[int]] [[spec_op]]
1411 ; CHECK: [[ptr_array1:%\w+]] = OpTypePointer Function [[array1]]
1412 ; CHECK: [[ptr_array2:%\w+]] = OpTypePointer Function [[array2]]
1413 ; CHECK: OpLabel
1414 ; CHECK-NEXT: OpVariable [[ptr_array1]] Function
1415 ; CHECK-NEXT: OpVariable [[ptr_array2]] Function
1416 ; CHECK-NOT: OpVariable
1417 OpCapability Shader
1418 OpCapability Linkage
1419 OpMemoryModel Logical GLSL450
1420 %void = OpTypeVoid
1421 %void_fn = OpTypeFunction %void
1422 %int = OpTypeInt 32 0
1423 %spec_const = OpSpecConstant %int 4
1424 %spec_op = OpSpecConstantOp %int IAdd %spec_const %spec_const
1425 %array_1 = OpTypeArray %int %spec_const
1426 %array_2 = OpTypeArray %int %spec_op
1427 %ptr_array_1_Function = OpTypePointer Function %array_1
1428 %ptr_array_2_Function = OpTypePointer Function %array_2
1429 %func = OpFunction %void None %void_fn
1430 %1 = OpLabel
1431 %var_1 = OpVariable %ptr_array_1_Function Function
1432 %var_2 = OpVariable %ptr_array_2_Function Function
1433 OpReturn
1434 OpFunctionEnd
1435 )";
1436 
1437   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1438 }
1439 
TEST_F(ScalarReplacementTest,CreateAmbiguousNullConstant2)1440 TEST_F(ScalarReplacementTest, CreateAmbiguousNullConstant2) {
1441   const std::string text = R"(
1442 ;
1443 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1444 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[struct_member:%\w+]]
1445 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1446 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1447 ; CHECK: [[null:%\w+]] = OpConstantNull [[struct_member]]
1448 ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
1449 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1450 ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
1451 ; CHECK: OpStore [[var1]]
1452 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1453 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1454 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1455 ;
1456 OpCapability Shader
1457 OpCapability Linkage
1458 OpMemoryModel Logical GLSL450
1459 OpName %func "replace_whole_load"
1460 %void = OpTypeVoid
1461 %uint = OpTypeInt 32 0
1462 %struct3 = OpTypeStruct %uint
1463 %struct2 = OpTypeStruct %uint
1464 %struct1 = OpTypeStruct %uint %struct2
1465 %uint_ptr = OpTypePointer Function %uint
1466 %struct1_ptr = OpTypePointer Function %struct1
1467 %uint_0 = OpConstant %uint 0
1468 %uint_1 = OpConstant %uint 1
1469 %func = OpTypeFunction %void
1470 %1 = OpFunction %void None %func
1471 %2 = OpLabel
1472 %var1 = OpVariable %struct1_ptr Function
1473 %var2 = OpVariable %struct1_ptr Function
1474 %load1 = OpLoad %struct1 %var1
1475 OpStore %var2 %load1
1476 %load2 = OpLoad %struct1 %var2
1477 %3 = OpCompositeExtract %uint %load2 0
1478 OpReturn
1479 OpFunctionEnd
1480   )";
1481 
1482   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1483 }
1484 
1485 // Test that a struct of size 4 is not replaced when there is a limit of 2.
TEST_F(ScalarReplacementTest,TestLimit)1486 TEST_F(ScalarReplacementTest, TestLimit) {
1487   const std::string text = R"(
1488 OpCapability Shader
1489 OpCapability Linkage
1490 OpMemoryModel Logical GLSL450
1491 OpName %6 "simple_struct"
1492 %1 = OpTypeVoid
1493 %2 = OpTypeInt 32 0
1494 %3 = OpTypeStruct %2 %2 %2 %2
1495 %4 = OpTypePointer Function %3
1496 %5 = OpTypePointer Function %2
1497 %6 = OpTypeFunction %2
1498 %7 = OpConstantNull %3
1499 %8 = OpConstant %2 0
1500 %9 = OpConstant %2 1
1501 %10 = OpConstant %2 2
1502 %11 = OpConstant %2 3
1503 %12 = OpFunction %2 None %6
1504 %13 = OpLabel
1505 %14 = OpVariable %4 Function %7
1506 %15 = OpInBoundsAccessChain %5 %14 %8
1507 %16 = OpLoad %2 %15
1508 %17 = OpAccessChain %5 %14 %10
1509 %18 = OpLoad %2 %17
1510 %19 = OpIAdd %2 %16 %18
1511 OpReturnValue %19
1512 OpFunctionEnd
1513   )";
1514 
1515   auto result =
1516       SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false, 2);
1517   EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
1518 }
1519 
1520 // Test that a struct of size 4 is replaced when there is a limit of 0 (no
1521 // limit).  This is the same spir-v as a test above, so we do not check that it
1522 // is correctly transformed.  We leave that to the test above.
TEST_F(ScalarReplacementTest,TestUnimited)1523 TEST_F(ScalarReplacementTest, TestUnimited) {
1524   const std::string text = R"(
1525 OpCapability Shader
1526 OpCapability Linkage
1527 OpMemoryModel Logical GLSL450
1528 OpName %6 "simple_struct"
1529 %1 = OpTypeVoid
1530 %2 = OpTypeInt 32 0
1531 %3 = OpTypeStruct %2 %2 %2 %2
1532 %4 = OpTypePointer Function %3
1533 %5 = OpTypePointer Function %2
1534 %6 = OpTypeFunction %2
1535 %7 = OpConstantNull %3
1536 %8 = OpConstant %2 0
1537 %9 = OpConstant %2 1
1538 %10 = OpConstant %2 2
1539 %11 = OpConstant %2 3
1540 %12 = OpFunction %2 None %6
1541 %13 = OpLabel
1542 %14 = OpVariable %4 Function %7
1543 %15 = OpInBoundsAccessChain %5 %14 %8
1544 %16 = OpLoad %2 %15
1545 %17 = OpAccessChain %5 %14 %10
1546 %18 = OpLoad %2 %17
1547 %19 = OpIAdd %2 %16 %18
1548 OpReturnValue %19
1549 OpFunctionEnd
1550   )";
1551 
1552   auto result =
1553       SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false, 0);
1554   EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
1555 }
1556 
TEST_F(ScalarReplacementTest,AmbigousPointer)1557 TEST_F(ScalarReplacementTest, AmbigousPointer) {
1558   const std::string text = R"(
1559 ; CHECK: [[s1:%\w+]] = OpTypeStruct %uint
1560 ; CHECK: [[s2:%\w+]] = OpTypeStruct %uint
1561 ; CHECK: [[s3:%\w+]] = OpTypeStruct [[s2]]
1562 ; CHECK: [[s3_const:%\w+]] = OpConstantComposite [[s3]]
1563 ; CHECK: [[s2_ptr:%\w+]] = OpTypePointer Function [[s2]]
1564 ; CHECK: OpCompositeExtract [[s2]] [[s3_const]]
1565 
1566                OpCapability Shader
1567           %1 = OpExtInstImport "GLSL.std.450"
1568                OpMemoryModel Logical GLSL450
1569                OpEntryPoint Fragment %2 "main"
1570                OpExecutionMode %2 OriginUpperLeft
1571                OpSource ESSL 310
1572        %void = OpTypeVoid
1573           %5 = OpTypeFunction %void
1574        %uint = OpTypeInt 32 0
1575   %_struct_7 = OpTypeStruct %uint
1576   %_struct_8 = OpTypeStruct %uint
1577   %_struct_9 = OpTypeStruct %_struct_8
1578      %uint_1 = OpConstant %uint 1
1579          %11 = OpConstantComposite %_struct_8 %uint_1
1580          %12 = OpConstantComposite %_struct_9 %11
1581 %_ptr_Function__struct_9 = OpTypePointer Function %_struct_9
1582 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
1583           %2 = OpFunction %void None %5
1584          %15 = OpLabel
1585         %var = OpVariable %_ptr_Function__struct_9 Function
1586                OpStore %var %12
1587          %ld = OpLoad %_struct_9 %var
1588          %ex = OpCompositeExtract %_struct_8 %ld 0
1589                OpReturn
1590                OpFunctionEnd
1591   )";
1592 
1593   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1594 }
1595 
1596 // Test that scalar replacement does not crash when there is an OpAccessChain
1597 // with no index.  If we choose to handle this case in the future, then the
1598 // result can change.
TEST_F(ScalarReplacementTest,TestAccessChainWithNoIndexes)1599 TEST_F(ScalarReplacementTest, TestAccessChainWithNoIndexes) {
1600   const std::string text = R"(
1601                OpCapability Shader
1602                OpMemoryModel Logical GLSL450
1603                OpEntryPoint Fragment %1 "main"
1604                OpExecutionMode %1 OriginLowerLeft
1605        %void = OpTypeVoid
1606           %3 = OpTypeFunction %void
1607       %float = OpTypeFloat 32
1608   %_struct_5 = OpTypeStruct %float
1609 %_ptr_Function__struct_5 = OpTypePointer Function %_struct_5
1610           %1 = OpFunction %void None %3
1611           %7 = OpLabel
1612           %8 = OpVariable %_ptr_Function__struct_5 Function
1613           %9 = OpAccessChain %_ptr_Function__struct_5 %8
1614                OpReturn
1615                OpFunctionEnd
1616   )";
1617 
1618   auto result =
1619       SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false);
1620   EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
1621 }
1622 
1623 // Test that id overflow is handled gracefully.
TEST_F(ScalarReplacementTest,IdBoundOverflow1)1624 TEST_F(ScalarReplacementTest, IdBoundOverflow1) {
1625   const std::string text = R"(
1626 OpCapability ImageQuery
1627 OpMemoryModel Logical GLSL450
1628 OpEntryPoint Fragment %4 "main"
1629 OpExecutionMode %4 OriginUpperLeft
1630 OpDecorate %4194302 DescriptorSet 1073495039
1631 %2 = OpTypeVoid
1632 %3 = OpTypeFunction %2
1633 %6 = OpTypeFloat 32
1634 %7 = OpTypeStruct %6 %6
1635 %557056 = OpTypeStruct %7
1636 %9 = OpTypePointer Function %7
1637 %18 = OpTypeFunction %7 %9
1638 %4 = OpFunction %2 Pure|Const %3
1639 %1836763 = OpLabel
1640 %4194302 = OpVariable %9 Function
1641 %10 = OpVariable %9 Function
1642 OpKill
1643 %4194301 = OpLabel
1644 %524296 = OpLoad %7 %4194302
1645 OpKill
1646 OpFunctionEnd
1647   )";
1648 
1649   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1650 
1651   std::vector<Message> messages = {
1652       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1653       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
1654   SetMessageConsumer(GetTestMessageConsumer(messages));
1655   auto result = SinglePassRunToBinary<ScalarReplacementPass>(text, true, false);
1656   EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
1657 }
1658 
1659 // Test that id overflow is handled gracefully.
TEST_F(ScalarReplacementTest,IdBoundOverflow2)1660 TEST_F(ScalarReplacementTest, IdBoundOverflow2) {
1661   const std::string text = R"(
1662 OpCapability Shader
1663 OpMemoryModel Logical GLSL450
1664 OpEntryPoint Fragment %4 "main" %17
1665 OpExecutionMode %4 OriginUpperLeft
1666 %2 = OpTypeVoid
1667 %3 = OpTypeFunction %2
1668 %6 = OpTypeFloat 32
1669 %7 = OpTypeVector %6 4
1670 %8 = OpTypeStruct %7
1671 %9 = OpTypePointer Function %8
1672 %16 = OpTypePointer Output %7
1673 %21 = OpTypeInt 32 1
1674 %22 = OpConstant %21 0
1675 %23 = OpTypePointer Function %7
1676 %17 = OpVariable %16 Output
1677 %4 = OpFunction %2 None %3
1678 %5 = OpLabel
1679 %4194300 = OpVariable %23 Function
1680 %10 = OpVariable %9 Function
1681 %4194301 = OpAccessChain %23 %10 %22
1682 %4194302 = OpLoad %7 %4194301
1683 OpStore %4194300 %4194302
1684 %15 = OpLoad %7 %4194300
1685 OpStore %17 %15
1686 OpReturn
1687 OpFunctionEnd
1688   )";
1689 
1690   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1691 
1692   std::vector<Message> messages = {
1693       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
1694   SetMessageConsumer(GetTestMessageConsumer(messages));
1695   auto result = SinglePassRunToBinary<ScalarReplacementPass>(text, true, false);
1696   EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
1697 }
1698 
1699 // Test that id overflow is handled gracefully.
TEST_F(ScalarReplacementTest,IdBoundOverflow3)1700 TEST_F(ScalarReplacementTest, IdBoundOverflow3) {
1701   const std::string text = R"(
1702 OpCapability InterpolationFunction
1703 OpExtension "z"
1704 OpMemoryModel Logical GLSL450
1705 OpEntryPoint Fragment %4 "main"
1706 OpExecutionMode %4 OriginUpperLeft
1707 %2 = OpTypeVoid
1708 %3 = OpTypeFunction %2
1709 %6 = OpTypeFloat 32
1710 %7 = OpTypeStruct %6 %6
1711 %9 = OpTypePointer Function %7
1712 %18 = OpTypeFunction %7 %9
1713 %21 = OpTypeInt 32 0
1714 %22 = OpConstant %21 4293000676
1715 %4194302 = OpConstantNull %6
1716 %4 = OpFunction %2 Inline|Pure %3
1717 %786464 = OpLabel
1718 %4194298 = OpVariable %9 Function
1719 %10 = OpVariable %9 Function
1720 %4194299 = OpUDiv %21 %22 %22
1721 %4194300 = OpLoad %7 %10
1722 %50959 = OpLoad %7 %4194298
1723 OpKill
1724 OpFunctionEnd
1725 %1 = OpFunction %7 None %18
1726 %19 = OpFunctionParameter %9
1727 %147667 = OpLabel
1728 %2044391 = OpUDiv %21 %22 %22
1729 %25 = OpLoad %7 %19
1730 OpReturnValue %25
1731 OpFunctionEnd
1732 %4194295 = OpFunction %2 None %3
1733 %4194296 = OpLabel
1734 OpKill
1735 OpFunctionEnd
1736   )";
1737 
1738   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1739 
1740   std::vector<Message> messages = {
1741       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1742       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1743       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1744       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1745       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1746       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1747       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1748       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
1749       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
1750   SetMessageConsumer(GetTestMessageConsumer(messages));
1751   auto result = SinglePassRunToBinary<ScalarReplacementPass>(text, true, false);
1752   EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
1753 }
1754 
1755 // Test that replacements for OpAccessChain do not go out of bounds.
1756 // https://github.com/KhronosGroup/SPIRV-Tools/issues/2609.
TEST_F(ScalarReplacementTest,OutOfBoundOpAccessChain)1757 TEST_F(ScalarReplacementTest, OutOfBoundOpAccessChain) {
1758   const std::string text = R"(
1759                OpCapability Shader
1760           %1 = OpExtInstImport "GLSL.std.450"
1761                OpMemoryModel Logical GLSL450
1762                OpEntryPoint Fragment %main "main" %_GLF_color
1763                OpExecutionMode %main OriginUpperLeft
1764                OpSource ESSL 310
1765                OpName %main "main"
1766                OpName %a "a"
1767                OpName %_GLF_color "_GLF_color"
1768                OpDecorate %_GLF_color Location 0
1769        %void = OpTypeVoid
1770           %3 = OpTypeFunction %void
1771         %int = OpTypeInt 32 1
1772 %_ptr_Function_int = OpTypePointer Function %int
1773       %int_1 = OpConstant %int 1
1774       %float = OpTypeFloat 32
1775        %uint = OpTypeInt 32 0
1776      %uint_1 = OpConstant %uint 1
1777 %_arr_float_uint_1 = OpTypeArray %float %uint_1
1778 %_ptr_Function__arr_float_uint_1 = OpTypePointer Function %_arr_float_uint_1
1779 %_ptr_Function_float = OpTypePointer Function %float
1780 %_ptr_Output_float = OpTypePointer Output %float
1781  %_GLF_color = OpVariable %_ptr_Output_float Output
1782        %main = OpFunction %void None %3
1783           %5 = OpLabel
1784           %a = OpVariable %_ptr_Function__arr_float_uint_1 Function
1785          %21 = OpAccessChain %_ptr_Function_float %a %int_1
1786          %22 = OpLoad %float %21
1787                OpStore %_GLF_color %22
1788                OpReturn
1789                OpFunctionEnd
1790   )";
1791 
1792   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1793 
1794   auto result =
1795       SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false);
1796   EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
1797 }
1798 
TEST_F(ScalarReplacementTest,CharIndex)1799 TEST_F(ScalarReplacementTest, CharIndex) {
1800   const std::string text = R"(
1801 ; CHECK: [[int:%\w+]] = OpTypeInt 32 0
1802 ; CHECK: [[ptr:%\w+]] = OpTypePointer Function [[int]]
1803 ; CHECK: OpVariable [[ptr]] Function
1804 OpCapability Shader
1805 OpCapability Int8
1806 OpMemoryModel Logical GLSL450
1807 OpEntryPoint GLCompute %main "main"
1808 OpExecutionMode %main LocalSize 1 1 1
1809 %void = OpTypeVoid
1810 %int = OpTypeInt 32 0
1811 %int_1024 = OpConstant %int 1024
1812 %char = OpTypeInt 8 0
1813 %char_1 = OpConstant %char 1
1814 %array = OpTypeArray %int %int_1024
1815 %ptr_func_array = OpTypePointer Function %array
1816 %ptr_func_int = OpTypePointer Function %int
1817 %void_fn = OpTypeFunction %void
1818 %main = OpFunction %void None %void_fn
1819 %entry = OpLabel
1820 %var = OpVariable %ptr_func_array Function
1821 %gep = OpAccessChain %ptr_func_int %var %char_1
1822 OpStore %gep %int_1024
1823 OpReturn
1824 OpFunctionEnd
1825 )";
1826 
1827   SinglePassRunAndMatch<ScalarReplacementPass>(text, true, 0);
1828 }
1829 
TEST_F(ScalarReplacementTest,OutOfBoundsOpAccessChainNegative)1830 TEST_F(ScalarReplacementTest, OutOfBoundsOpAccessChainNegative) {
1831   const std::string text = R"(
1832 OpCapability Shader
1833 OpCapability Int8
1834 OpMemoryModel Logical GLSL450
1835 OpEntryPoint GLCompute %main "main"
1836 OpExecutionMode %main LocalSize 1 1 1
1837 %void = OpTypeVoid
1838 %int = OpTypeInt 32 0
1839 %int_1024 = OpConstant %int 1024
1840 %char = OpTypeInt 8 1
1841 %char_n1 = OpConstant %char -1
1842 %array = OpTypeArray %int %int_1024
1843 %ptr_func_array = OpTypePointer Function %array
1844 %ptr_func_int = OpTypePointer Function %int
1845 %void_fn = OpTypeFunction %void
1846 %main = OpFunction %void None %void_fn
1847 %entry = OpLabel
1848 %var = OpVariable %ptr_func_array Function
1849 %gep = OpAccessChain %ptr_func_int %var %char_n1
1850 OpStore %gep %int_1024
1851 OpReturn
1852 OpFunctionEnd
1853 )";
1854 
1855   auto result =
1856       SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, true, 0);
1857   EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
1858 }
1859 
TEST_F(ScalarReplacementTest,RelaxedPrecisionMemberDecoration)1860 TEST_F(ScalarReplacementTest, RelaxedPrecisionMemberDecoration) {
1861   const std::string text = R"(
1862 ; CHECK: OpDecorate {{%\w+}} RelaxedPrecision
1863 ; CHECK: OpDecorate [[new_var:%\w+]] RelaxedPrecision
1864 ; CHECK: [[new_var]] = OpVariable %_ptr_Function_v3float Function
1865 ; CHECK: OpLoad %v3float [[new_var]]
1866                OpCapability Shader
1867                OpMemoryModel Logical GLSL450
1868                OpEntryPoint Vertex %1 "Draw2DTexCol_VS" %2 %3
1869                OpSource HLSL 600
1870                OpDecorate %2 Location 0
1871                OpDecorate %3 Location 1
1872                OpDecorate %3 RelaxedPrecision
1873                OpMemberDecorate %_struct_4 1 RelaxedPrecision
1874       %float = OpTypeFloat 32
1875         %int = OpTypeInt 32 1
1876       %int_1 = OpConstant %int 1
1877     %v3float = OpTypeVector %float 3
1878 %_ptr_Input_v3float = OpTypePointer Input %v3float
1879        %void = OpTypeVoid
1880          %11 = OpTypeFunction %void
1881   %_struct_4 = OpTypeStruct %v3float %v3float
1882 %_ptr_Function__struct_4 = OpTypePointer Function %_struct_4
1883 %_ptr_Function_v3float = OpTypePointer Function %v3float
1884           %2 = OpVariable %_ptr_Input_v3float Input
1885           %3 = OpVariable %_ptr_Input_v3float Input
1886           %1 = OpFunction %void None %11
1887          %14 = OpLabel
1888          %15 = OpVariable %_ptr_Function__struct_4 Function
1889          %16 = OpLoad %v3float %2
1890          %17 = OpLoad %v3float %3
1891          %18 = OpCompositeConstruct %_struct_4 %16 %17
1892                OpStore %15 %18
1893          %19 = OpAccessChain %_ptr_Function_v3float %15 %int_1
1894          %20 = OpLoad %v3float %19
1895                OpReturn
1896                OpFunctionEnd
1897 )";
1898 
1899   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1900 }
1901 
TEST_F(ScalarReplacementTest,DebugDeclare)1902 TEST_F(ScalarReplacementTest, DebugDeclare) {
1903   const std::string text = R"(
1904 OpCapability Shader
1905 OpCapability Linkage
1906 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
1907 OpMemoryModel Logical GLSL450
1908 %test = OpString "test"
1909 OpName %6 "simple_struct"
1910 %1 = OpTypeVoid
1911 %2 = OpTypeInt 32 0
1912 %uint_32 = OpConstant %2 32
1913 %3 = OpTypeStruct %2 %2 %2 %2
1914 %4 = OpTypePointer Function %3
1915 %5 = OpTypePointer Function %2
1916 %6 = OpTypeFunction %2
1917 %7 = OpConstantNull %3
1918 %8 = OpConstant %2 0
1919 %9 = OpConstant %2 1
1920 %10 = OpConstant %2 2
1921 %11 = OpConstant %2 3
1922 %null_expr = OpExtInst %1 %ext DebugExpression
1923 %src = OpExtInst %1 %ext DebugSource %test
1924 %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
1925 %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
1926 %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
1927 %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
1928 %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
1929 %12 = OpFunction %2 None %6
1930 %13 = OpLabel
1931 %scope = OpExtInst %1 %ext DebugScope %dbg_main
1932 %14 = OpVariable %4 Function %7
1933 
1934 ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
1935 ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
1936 ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
1937 ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
1938 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %int_3
1939 ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
1940 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_2
1941 ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
1942 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1
1943 ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
1944 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
1945 ; CHECK-NOT: DebugDeclare
1946 %decl = OpExtInst %1 %ext DebugDeclare %dbg_foo %14 %null_expr
1947 
1948 %15 = OpInBoundsAccessChain %5 %14 %8
1949 %16 = OpLoad %2 %15
1950 %17 = OpAccessChain %5 %14 %10
1951 %18 = OpLoad %2 %17
1952 %19 = OpIAdd %2 %16 %18
1953 OpReturnValue %19
1954 OpFunctionEnd
1955 )";
1956 
1957   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1958 }
1959 
TEST_F(ScalarReplacementTest,DebugValue)1960 TEST_F(ScalarReplacementTest, DebugValue) {
1961   const std::string text = R"(
1962 OpCapability Shader
1963 OpCapability Linkage
1964 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
1965 OpMemoryModel Logical GLSL450
1966 %test = OpString "test"
1967 OpName %6 "simple_struct"
1968 %1 = OpTypeVoid
1969 %2 = OpTypeInt 32 0
1970 %uint_32 = OpConstant %2 32
1971 %3 = OpTypeStruct %2 %2 %2 %2
1972 %4 = OpTypePointer Function %3
1973 %5 = OpTypePointer Function %2
1974 %6 = OpTypeFunction %2
1975 %7 = OpConstantNull %3
1976 %8 = OpConstant %2 0
1977 %9 = OpConstant %2 1
1978 %10 = OpConstant %2 2
1979 %11 = OpConstant %2 3
1980 %deref = OpExtInst %1 %ext DebugOperation Deref
1981 %deref_expr = OpExtInst %1 %ext DebugExpression %deref
1982 %null_expr = OpExtInst %1 %ext DebugExpression
1983 %src = OpExtInst %1 %ext DebugSource %test
1984 %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
1985 %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
1986 %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
1987 %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
1988 %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
1989 %12 = OpFunction %2 None %6
1990 %13 = OpLabel
1991 %scope = OpExtInst %1 %ext DebugScope %dbg_main
1992 %14 = OpVariable %4 Function %7
1993 
1994 ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
1995 ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
1996 ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
1997 ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
1998 ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
1999 ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
2000 ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
2001 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
2002 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1
2003 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_2
2004 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %int_3
2005 %value = OpExtInst %1 %ext DebugValue %dbg_foo %14 %deref_expr
2006 
2007 %15 = OpInBoundsAccessChain %5 %14 %8
2008 %16 = OpLoad %2 %15
2009 %17 = OpAccessChain %5 %14 %10
2010 %18 = OpLoad %2 %17
2011 %19 = OpIAdd %2 %16 %18
2012 OpReturnValue %19
2013 OpFunctionEnd
2014 )";
2015 
2016   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
2017 }
2018 
TEST_F(ScalarReplacementTest,DebugDeclareRecursive)2019 TEST_F(ScalarReplacementTest, DebugDeclareRecursive) {
2020   const std::string text = R"(
2021 OpCapability Shader
2022 OpCapability Linkage
2023 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2024 OpMemoryModel Logical GLSL450
2025 %test = OpString "test"
2026 OpName %6 "simple_struct"
2027 %1 = OpTypeVoid
2028 %2 = OpTypeInt 32 0
2029 %uint_32 = OpConstant %2 32
2030 %float = OpTypeFloat 32
2031 %float_1 = OpConstant %float 1
2032 %member = OpTypeStruct %2 %float
2033 %3 = OpTypeStruct %2 %member %float
2034 %4 = OpTypePointer Function %3
2035 %5 = OpTypePointer Function %2
2036 %ptr_float_Function = OpTypePointer Function %float
2037 %6 = OpTypeFunction %2
2038 %cmember = OpConstantComposite %member %uint_32 %float_1
2039 %7 = OpConstantComposite %3 %uint_32 %cmember %float_1
2040 %8 = OpConstant %2 0
2041 %9 = OpConstant %2 1
2042 %10 = OpConstant %2 2
2043 %null_expr = OpExtInst %1 %ext DebugExpression
2044 %src = OpExtInst %1 %ext DebugSource %test
2045 %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
2046 %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
2047 %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
2048 %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
2049 %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
2050 %12 = OpFunction %2 None %6
2051 %13 = OpLabel
2052 %scope = OpExtInst %1 %ext DebugScope %dbg_main
2053 %14 = OpVariable %4 Function %7
2054 
2055 ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
2056 ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
2057 ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
2058 ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_float Function %float_1
2059 ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function %uint_32
2060 ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_float Function %float_1
2061 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %int_2
2062 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1 %int_0
2063 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_1 %int_1
2064 ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function %uint_32
2065 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
2066 ; CHECK-NOT: DebugDeclare
2067 %decl = OpExtInst %1 %ext DebugDeclare %dbg_foo %14 %null_expr
2068 
2069 %15 = OpInBoundsAccessChain %5 %14 %8
2070 %16 = OpLoad %2 %15
2071 %17 = OpAccessChain %ptr_float_Function %14 %10
2072 %18 = OpLoad %float %17
2073 %value = OpConvertFToU %2 %18
2074 %19 = OpIAdd %2 %16 %value
2075 OpReturnValue %19
2076 OpFunctionEnd
2077 )";
2078 
2079   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
2080 }
2081 
TEST_F(ScalarReplacementTest,DebugValueWithIndex)2082 TEST_F(ScalarReplacementTest, DebugValueWithIndex) {
2083   const std::string text = R"(
2084 OpCapability Shader
2085 OpCapability Linkage
2086 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2087 OpMemoryModel Logical GLSL450
2088 %test = OpString "test"
2089 OpName %6 "simple_struct"
2090 %1 = OpTypeVoid
2091 %2 = OpTypeInt 32 0
2092 %uint_32 = OpConstant %2 32
2093 %3 = OpTypeStruct %2 %2 %2 %2
2094 %4 = OpTypePointer Function %3
2095 %5 = OpTypePointer Function %2
2096 %6 = OpTypeFunction %2
2097 %7 = OpConstantNull %3
2098 %8 = OpConstant %2 0
2099 %9 = OpConstant %2 1
2100 %10 = OpConstant %2 2
2101 %11 = OpConstant %2 3
2102 %deref = OpExtInst %1 %ext DebugOperation Deref
2103 %deref_expr = OpExtInst %1 %ext DebugExpression %deref
2104 %null_expr = OpExtInst %1 %ext DebugExpression
2105 %src = OpExtInst %1 %ext DebugSource %test
2106 %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
2107 %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
2108 %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
2109 %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
2110 %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
2111 %12 = OpFunction %2 None %6
2112 %13 = OpLabel
2113 %scope = OpExtInst %1 %ext DebugScope %dbg_main
2114 %14 = OpVariable %4 Function %7
2115 
2116 ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
2117 ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
2118 ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
2119 ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
2120 ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
2121 ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
2122 ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
2123 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_0
2124 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_1
2125 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_2
2126 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_3
2127 %value = OpExtInst %1 %ext DebugValue %dbg_foo %14 %deref_expr %8 %9 %10
2128 
2129 %15 = OpInBoundsAccessChain %5 %14 %8
2130 %16 = OpLoad %2 %15
2131 %17 = OpAccessChain %5 %14 %10
2132 %18 = OpLoad %2 %17
2133 %19 = OpIAdd %2 %16 %18
2134 OpReturnValue %19
2135 OpFunctionEnd
2136 )";
2137 
2138   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
2139 }
2140 
TEST_F(ScalarReplacementTest,DebugDeclareForVariableInOtherBB)2141 TEST_F(ScalarReplacementTest, DebugDeclareForVariableInOtherBB) {
2142   const std::string text = R"(
2143 OpCapability Shader
2144 OpCapability Linkage
2145 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2146 OpMemoryModel Logical GLSL450
2147 %test = OpString "test"
2148 OpName %6 "simple_struct"
2149 %1 = OpTypeVoid
2150 %2 = OpTypeInt 32 0
2151 %uint_32 = OpConstant %2 32
2152 %3 = OpTypeStruct %2 %2 %2 %2
2153 %4 = OpTypePointer Function %3
2154 %5 = OpTypePointer Function %2
2155 %6 = OpTypeFunction %2
2156 %7 = OpConstantNull %3
2157 %8 = OpConstant %2 0
2158 %9 = OpConstant %2 1
2159 %10 = OpConstant %2 2
2160 %11 = OpConstant %2 3
2161 %deref = OpExtInst %1 %ext DebugOperation Deref
2162 %deref_expr = OpExtInst %1 %ext DebugExpression %deref
2163 %null_expr = OpExtInst %1 %ext DebugExpression
2164 %src = OpExtInst %1 %ext DebugSource %test
2165 %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
2166 %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
2167 %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
2168 %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
2169 %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
2170 %12 = OpFunction %2 None %6
2171 %13 = OpLabel
2172 %scope = OpExtInst %1 %ext DebugScope %dbg_main
2173 %14 = OpVariable %4 Function %7
2174 
2175 ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugLocalVariable
2176 ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
2177 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr:%\w+]] %int_3
2178 ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
2179 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_2
2180 ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
2181 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1
2182 ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
2183 ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
2184 
2185 OpBranch %20
2186 %20 = OpLabel
2187 %value = OpExtInst %1 %ext DebugDeclare %dbg_foo %14 %null_expr
2188 %15 = OpInBoundsAccessChain %5 %14 %8
2189 %16 = OpLoad %2 %15
2190 %17 = OpAccessChain %5 %14 %10
2191 %18 = OpLoad %2 %17
2192 %19 = OpIAdd %2 %16 %18
2193 OpReturnValue %19
2194 OpFunctionEnd
2195 )";
2196 
2197   SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
2198 }
2199 
TEST_F(ScalarReplacementTest,ImageTexelPointer)2200 TEST_F(ScalarReplacementTest, ImageTexelPointer) {
2201   // Test whether the scalar replacement correctly checks the
2202   // OpImageTexelPointer user of an aggregate with an image type.
2203   const std::string text = R"(
2204 ;
2205 ; CHECK: [[imgTy:%\w+]] = OpTypeImage %uint Buffer 2 0 0 2 R32ui
2206 ; CHECK: [[ptrImgTy:%\w+]] = OpTypePointer Function [[imgTy]]
2207 ; CHECK: [[img:%\w+]] = OpVariable [[ptrImgTy]] Function
2208 ; CHECK: [[imgTexelPtr:%\w+]] = OpImageTexelPointer {{%\w+}} [[img]] %uint_0 %uint_0
2209 ; CHECK: OpAtomicIAdd %uint [[imgTexelPtr]] %uint_1 %uint_0 %uint_1
2210 ;
2211 OpCapability Shader
2212 OpCapability SampledBuffer
2213 OpCapability ImageBuffer
2214 OpMemoryModel Logical GLSL450
2215 OpEntryPoint GLCompute %1 "main"
2216 OpExecutionMode %1 LocalSize 64 1 1
2217 %void = OpTypeVoid
2218 %uint = OpTypeInt 32 0
2219 %uint_0 = OpConstant %uint 0
2220 %uint_1 = OpConstant %uint 1
2221 %_ptr_Image_uint = OpTypePointer Image %uint
2222 %type_buffer_image = OpTypeImage %uint Buffer 2 0 0 2 R32ui
2223 %_ptr_Function_type_buffer_image = OpTypePointer Function %type_buffer_image
2224 %image_struct = OpTypeStruct %type_buffer_image %type_buffer_image
2225 %_ptr_Function_image_struct = OpTypePointer Function %image_struct
2226 %func = OpTypeFunction %void
2227 %1 = OpFunction %void None %func
2228 %2 = OpLabel
2229 %3 = OpVariable %_ptr_Function_image_struct Function
2230 %4 = OpAccessChain %_ptr_Function_type_buffer_image %3 %uint_1
2231 %5 = OpImageTexelPointer %_ptr_Image_uint %4 %uint_0 %uint_0
2232 %6 = OpAtomicIAdd %uint %5 %uint_1 %uint_0 %uint_1
2233 OpReturn
2234 OpFunctionEnd
2235   )";
2236 
2237   SinglePassRunAndMatch<ScalarReplacementPass>(text, false);
2238 }
2239 
2240 }  // namespace
2241 }  // namespace opt
2242 }  // namespace spvtools
2243