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: OpVariable [[float_ptr]] Function
1153 ; CHECK-NOT: OpVariable
1154 ;
1155 OpCapability Shader
1156 %1 = OpExtInstImport "GLSL.std.450"
1157 OpMemoryModel Logical GLSL450
1158 OpEntryPoint Fragment %main "main" %fo
1159 OpExecutionMode %main OriginUpperLeft
1160 OpSource GLSL 430
1161 OpName %main "main"
1162 OpName %S "S"
1163 OpMemberName %S 0 "x"
1164 OpMemberName %S 1 "y"
1165 OpName %ts1 "ts1"
1166 OpName %S_0 "S"
1167 OpMemberName %S_0 0 "x"
1168 OpMemberName %S_0 1 "y"
1169 OpName %U_t "U_t"
1170 OpMemberName %U_t 0 "g_s1"
1171 OpMemberName %U_t 1 "g_s2"
1172 OpMemberName %U_t 2 "g_s3"
1173 OpName %_ ""
1174 OpName %ts2 "ts2"
1175 OpName %_Globals_ "_Globals_"
1176 OpMemberName %_Globals_ 0 "g_b"
1177 OpName %__0 ""
1178 OpName %ts3 "ts3"
1179 OpName %ts4 "ts4"
1180 OpName %fo "fo"
1181 OpMemberDecorate %S_0 0 Offset 0
1182 OpMemberDecorate %S_0 1 Offset 4
1183 OpMemberDecorate %U_t 0 Offset 0
1184 OpMemberDecorate %U_t 1 Offset 8
1185 OpMemberDecorate %U_t 2 Offset 16
1186 OpDecorate %U_t BufferBlock
1187 OpDecorate %_ DescriptorSet 0
1188 OpMemberDecorate %_Globals_ 0 Offset 0
1189 OpDecorate %_Globals_ Block
1190 OpDecorate %__0 DescriptorSet 0
1191 OpDecorate %__0 Binding 0
1192 OpDecorate %fo Location 0
1193 %void = OpTypeVoid
1194 %15 = OpTypeFunction %void
1195 %float = OpTypeFloat 32
1196 %S = OpTypeStruct %float %float
1197 %_ptr_Function_S = OpTypePointer Function %S
1198 %S_0 = OpTypeStruct %float %float
1199 %U_t = OpTypeStruct %S_0 %S_0 %S_0
1200 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
1201 %_ = OpVariable %_ptr_Uniform_U_t Uniform
1202 %int = OpTypeInt 32 1
1203 %int_0 = OpConstant %int 0
1204 %_ptr_Uniform_S_0 = OpTypePointer Uniform %S_0
1205 %_ptr_Function_float = OpTypePointer Function %float
1206 %int_1 = OpConstant %int 1
1207 %uint = OpTypeInt 32 0
1208 %_Globals_ = OpTypeStruct %uint
1209 %_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
1210 %__0 = OpVariable %_ptr_Uniform__Globals_ Uniform
1211 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
1212 %bool = OpTypeBool
1213 %uint_0 = OpConstant %uint 0
1214 %_ptr_Output_float = OpTypePointer Output %float
1215 %fo = OpVariable %_ptr_Output_float Output
1216 %main = OpFunction %void None %15
1217 %30 = OpLabel
1218 %ts1 = OpVariable %_ptr_Function_S Function
1219 %ts2 = OpVariable %_ptr_Function_S Function
1220 %ts3 = OpVariable %_ptr_Function_S Function
1221 %ts4 = OpVariable %_ptr_Function_S Function
1222 %31 = OpAccessChain %_ptr_Uniform_S_0 %_ %int_0
1223 %32 = OpLoad %S_0 %31
1224 %33 = OpCompositeExtract %float %32 0
1225 %34 = OpAccessChain %_ptr_Function_float %ts1 %int_0
1226 OpStore %34 %33
1227 %35 = OpCompositeExtract %float %32 1
1228 %36 = OpAccessChain %_ptr_Function_float %ts1 %int_1
1229 OpStore %36 %35
1230 %37 = OpAccessChain %_ptr_Uniform_S_0 %_ %int_1
1231 %38 = OpLoad %S_0 %37
1232 %39 = OpCompositeExtract %float %38 0
1233 %40 = OpAccessChain %_ptr_Function_float %ts2 %int_0
1234 OpStore %40 %39
1235 %41 = OpCompositeExtract %float %38 1
1236 %42 = OpAccessChain %_ptr_Function_float %ts2 %int_1
1237 OpStore %42 %41
1238 %43 = OpAccessChain %_ptr_Uniform_uint %__0 %int_0
1239 %44 = OpLoad %uint %43
1240 %45 = OpINotEqual %bool %44 %uint_0
1241 OpSelectionMerge %46 None
1242 OpBranchConditional %45 %47 %48
1243 %47 = OpLabel
1244 %49 = OpLoad %S %ts1
1245 OpStore %ts3 %49
1246 OpBranch %46
1247 %48 = OpLabel
1248 %50 = OpLoad %S %ts2
1249 OpStore %ts3 %50
1250 OpBranch %46
1251 %46 = OpLabel
1252 %51 = OpLoad %S %ts3
1253 OpStore %ts4 %51
1254 %52 = OpAccessChain %_ptr_Function_float %ts4 %int_1
1255 %53 = OpLoad %float %52
1256 OpStore %fo %53
1257 OpReturn
1258 OpFunctionEnd
1259 )";
1260
1261 SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1262 }
1263
TEST_F(ScalarReplacementTest,ReplaceWholeLoadAndStore)1264 TEST_F(ScalarReplacementTest, ReplaceWholeLoadAndStore) {
1265 const std::string text = R"(
1266 ;
1267 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1268 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
1269 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1270 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1271 ; CHECK: [[null:%\w+]] = OpConstantNull [[uint]]
1272 ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
1273 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1274 ; CHECK-NOT: OpVariable
1275 ; CHECK: [[l0:%\w+]] = OpLoad [[uint]] [[var0]]
1276 ; CHECK: [[c0:%\w+]] = OpCompositeConstruct [[struct1]] [[l0]] [[null]]
1277 ; CHECK: [[e0:%\w+]] = OpCompositeExtract [[uint]] [[c0]] 0
1278 ; CHECK: OpStore [[var1]] [[e0]]
1279 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1280 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1281 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1282 ;
1283 OpCapability Shader
1284 OpCapability Linkage
1285 OpMemoryModel Logical GLSL450
1286 OpName %func "replace_whole_load"
1287 %void = OpTypeVoid
1288 %uint = OpTypeInt 32 0
1289 %struct1 = OpTypeStruct %uint %uint
1290 %uint_ptr = OpTypePointer Function %uint
1291 %struct1_ptr = OpTypePointer Function %struct1
1292 %uint_0 = OpConstant %uint 0
1293 %uint_1 = OpConstant %uint 1
1294 %func = OpTypeFunction %void
1295 %1 = OpFunction %void None %func
1296 %2 = OpLabel
1297 %var2 = OpVariable %struct1_ptr Function
1298 %var1 = OpVariable %struct1_ptr Function
1299 %load1 = OpLoad %struct1 %var1
1300 OpStore %var2 %load1
1301 %load2 = OpLoad %struct1 %var2
1302 %3 = OpCompositeExtract %uint %load2 0
1303 OpReturn
1304 OpFunctionEnd
1305 )";
1306
1307 SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1308 }
1309
TEST_F(ScalarReplacementTest,ReplaceWholeLoadAndStore2)1310 TEST_F(ScalarReplacementTest, ReplaceWholeLoadAndStore2) {
1311 // TODO: We can improve this case by ensuring that |var2| is processed first.
1312 const std::string text = R"(
1313 ;
1314 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1315 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
1316 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1317 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1318 ; CHECK: [[null:%\w+]] = OpConstantNull [[uint]]
1319 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1320 ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
1321 ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
1322 ; CHECK-NOT: OpVariable
1323 ; CHECK: [[l0a:%\w+]] = OpLoad [[uint]] [[var0a]]
1324 ; CHECK: [[l0b:%\w+]] = OpLoad [[uint]] [[var0b]]
1325 ; CHECK: [[c0:%\w+]] = OpCompositeConstruct [[struct1]] [[l0b]] [[l0a]]
1326 ; CHECK: [[e0:%\w+]] = OpCompositeExtract [[uint]] [[c0]] 0
1327 ; CHECK: OpStore [[var1]] [[e0]]
1328 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1329 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1330 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1331 ;
1332 OpCapability Shader
1333 OpCapability Linkage
1334 OpMemoryModel Logical GLSL450
1335 OpName %func "replace_whole_load"
1336 %void = OpTypeVoid
1337 %uint = OpTypeInt 32 0
1338 %struct1 = OpTypeStruct %uint %uint
1339 %uint_ptr = OpTypePointer Function %uint
1340 %struct1_ptr = OpTypePointer Function %struct1
1341 %uint_0 = OpConstant %uint 0
1342 %uint_1 = OpConstant %uint 1
1343 %func = OpTypeFunction %void
1344 %1 = OpFunction %void None %func
1345 %2 = OpLabel
1346 %var1 = OpVariable %struct1_ptr Function
1347 %var2 = OpVariable %struct1_ptr Function
1348 %load1 = OpLoad %struct1 %var1
1349 OpStore %var2 %load1
1350 %load2 = OpLoad %struct1 %var2
1351 %3 = OpCompositeExtract %uint %load2 0
1352 OpReturn
1353 OpFunctionEnd
1354 )";
1355
1356 SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1357 }
1358
TEST_F(ScalarReplacementTest,CreateAmbiguousNullConstant1)1359 TEST_F(ScalarReplacementTest, CreateAmbiguousNullConstant1) {
1360 const std::string text = R"(
1361 ;
1362 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1363 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[struct_member:%\w+]]
1364 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1365 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1366 ; CHECK: [[null:%\w+]] = OpConstantNull [[struct_member]]
1367 ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
1368 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1369 ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
1370 ; CHECK-NOT: OpVariable
1371 ; CHECK: OpStore [[var1]]
1372 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1373 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1374 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1375 ;
1376 OpCapability Shader
1377 OpCapability Linkage
1378 OpMemoryModel Logical GLSL450
1379 OpName %func "replace_whole_load"
1380 %void = OpTypeVoid
1381 %uint = OpTypeInt 32 0
1382 %struct2 = OpTypeStruct %uint
1383 %struct3 = OpTypeStruct %uint
1384 %struct1 = OpTypeStruct %uint %struct2
1385 %uint_ptr = OpTypePointer Function %uint
1386 %struct1_ptr = OpTypePointer Function %struct1
1387 %uint_0 = OpConstant %uint 0
1388 %uint_1 = OpConstant %uint 1
1389 %func = OpTypeFunction %void
1390 %1 = OpFunction %void None %func
1391 %2 = OpLabel
1392 %var1 = OpVariable %struct1_ptr Function
1393 %var2 = OpVariable %struct1_ptr Function
1394 %load1 = OpLoad %struct1 %var1
1395 OpStore %var2 %load1
1396 %load2 = OpLoad %struct1 %var2
1397 %3 = OpCompositeExtract %uint %load2 0
1398 OpReturn
1399 OpFunctionEnd
1400 )";
1401
1402 SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1403 }
1404
TEST_F(ScalarReplacementTest,SpecConstantArray)1405 TEST_F(ScalarReplacementTest, SpecConstantArray) {
1406 const std::string text = R"(
1407 ; CHECK: [[int:%\w+]] = OpTypeInt
1408 ; CHECK: [[spec_const:%\w+]] = OpSpecConstant [[int]] 4
1409 ; CHECK: [[spec_op:%\w+]] = OpSpecConstantOp [[int]] IAdd [[spec_const]] [[spec_const]]
1410 ; CHECK: [[array1:%\w+]] = OpTypeArray [[int]] [[spec_const]]
1411 ; CHECK: [[array2:%\w+]] = OpTypeArray [[int]] [[spec_op]]
1412 ; CHECK: [[ptr_array1:%\w+]] = OpTypePointer Function [[array1]]
1413 ; CHECK: [[ptr_array2:%\w+]] = OpTypePointer Function [[array2]]
1414 ; CHECK: OpLabel
1415 ; CHECK-NEXT: OpVariable [[ptr_array1]] Function
1416 ; CHECK-NEXT: OpVariable [[ptr_array2]] Function
1417 ; CHECK-NOT: OpVariable
1418 OpCapability Shader
1419 OpCapability Linkage
1420 OpMemoryModel Logical GLSL450
1421 %void = OpTypeVoid
1422 %void_fn = OpTypeFunction %void
1423 %int = OpTypeInt 32 0
1424 %spec_const = OpSpecConstant %int 4
1425 %spec_op = OpSpecConstantOp %int IAdd %spec_const %spec_const
1426 %array_1 = OpTypeArray %int %spec_const
1427 %array_2 = OpTypeArray %int %spec_op
1428 %ptr_array_1_Function = OpTypePointer Function %array_1
1429 %ptr_array_2_Function = OpTypePointer Function %array_2
1430 %func = OpFunction %void None %void_fn
1431 %1 = OpLabel
1432 %var_1 = OpVariable %ptr_array_1_Function Function
1433 %var_2 = OpVariable %ptr_array_2_Function Function
1434 OpReturn
1435 OpFunctionEnd
1436 )";
1437
1438 SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1439 }
1440
TEST_F(ScalarReplacementTest,CreateAmbiguousNullConstant2)1441 TEST_F(ScalarReplacementTest, CreateAmbiguousNullConstant2) {
1442 const std::string text = R"(
1443 ;
1444 ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
1445 ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[struct_member:%\w+]]
1446 ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
1447 ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
1448 ; CHECK: [[null:%\w+]] = OpConstantNull [[struct_member]]
1449 ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
1450 ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
1451 ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
1452 ; CHECK: OpStore [[var1]]
1453 ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
1454 ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[null]]
1455 ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
1456 ;
1457 OpCapability Shader
1458 OpCapability Linkage
1459 OpMemoryModel Logical GLSL450
1460 OpName %func "replace_whole_load"
1461 %void = OpTypeVoid
1462 %uint = OpTypeInt 32 0
1463 %struct3 = OpTypeStruct %uint
1464 %struct2 = OpTypeStruct %uint
1465 %struct1 = OpTypeStruct %uint %struct2
1466 %uint_ptr = OpTypePointer Function %uint
1467 %struct1_ptr = OpTypePointer Function %struct1
1468 %uint_0 = OpConstant %uint 0
1469 %uint_1 = OpConstant %uint 1
1470 %func = OpTypeFunction %void
1471 %1 = OpFunction %void None %func
1472 %2 = OpLabel
1473 %var1 = OpVariable %struct1_ptr Function
1474 %var2 = OpVariable %struct1_ptr Function
1475 %load1 = OpLoad %struct1 %var1
1476 OpStore %var2 %load1
1477 %load2 = OpLoad %struct1 %var2
1478 %3 = OpCompositeExtract %uint %load2 0
1479 OpReturn
1480 OpFunctionEnd
1481 )";
1482
1483 SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
1484 }
1485
1486 // Test that a struct of size 4 is not replaced when there is a limit of 2.
TEST_F(ScalarReplacementTest,TestLimit)1487 TEST_F(ScalarReplacementTest, TestLimit) {
1488 const std::string text = R"(
1489 OpCapability Shader
1490 OpCapability Linkage
1491 OpMemoryModel Logical GLSL450
1492 OpName %6 "simple_struct"
1493 %1 = OpTypeVoid
1494 %2 = OpTypeInt 32 0
1495 %3 = OpTypeStruct %2 %2 %2 %2
1496 %4 = OpTypePointer Function %3
1497 %5 = OpTypePointer Function %2
1498 %6 = OpTypeFunction %2
1499 %7 = OpConstantNull %3
1500 %8 = OpConstant %2 0
1501 %9 = OpConstant %2 1
1502 %10 = OpConstant %2 2
1503 %11 = OpConstant %2 3
1504 %12 = OpFunction %2 None %6
1505 %13 = OpLabel
1506 %14 = OpVariable %4 Function %7
1507 %15 = OpInBoundsAccessChain %5 %14 %8
1508 %16 = OpLoad %2 %15
1509 %17 = OpAccessChain %5 %14 %10
1510 %18 = OpLoad %2 %17
1511 %19 = OpIAdd %2 %16 %18
1512 OpReturnValue %19
1513 OpFunctionEnd
1514 )";
1515
1516 auto result =
1517 SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false, 2);
1518 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
1519 }
1520
1521 // Test that a struct of size 4 is replaced when there is a limit of 0 (no
1522 // limit). This is the same spir-v as a test above, so we do not check that it
1523 // is correctly transformed. We leave that to the test above.
TEST_F(ScalarReplacementTest,TestUnimited)1524 TEST_F(ScalarReplacementTest, TestUnimited) {
1525 const std::string text = R"(
1526 OpCapability Shader
1527 OpCapability Linkage
1528 OpMemoryModel Logical GLSL450
1529 OpName %6 "simple_struct"
1530 %1 = OpTypeVoid
1531 %2 = OpTypeInt 32 0
1532 %3 = OpTypeStruct %2 %2 %2 %2
1533 %4 = OpTypePointer Function %3
1534 %5 = OpTypePointer Function %2
1535 %6 = OpTypeFunction %2
1536 %7 = OpConstantNull %3
1537 %8 = OpConstant %2 0
1538 %9 = OpConstant %2 1
1539 %10 = OpConstant %2 2
1540 %11 = OpConstant %2 3
1541 %12 = OpFunction %2 None %6
1542 %13 = OpLabel
1543 %14 = OpVariable %4 Function %7
1544 %15 = OpInBoundsAccessChain %5 %14 %8
1545 %16 = OpLoad %2 %15
1546 %17 = OpAccessChain %5 %14 %10
1547 %18 = OpLoad %2 %17
1548 %19 = OpIAdd %2 %16 %18
1549 OpReturnValue %19
1550 OpFunctionEnd
1551 )";
1552
1553 auto result =
1554 SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false, 0);
1555 EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
1556 }
1557
1558 } // namespace
1559 } // namespace opt
1560 } // namespace spvtools
1561