Debian Codegraph @

test/opt/graphics_robust_access_test.cpp

raw
1 // Copyright (c) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <sstream>
16 #include <string>
17 #include <vector>
18
19 #include "pass_fixture.h"
20 #include "pass_utils.h"
21 #include "source/opt/graphics_robust_access_pass.h"
22
23 namespace {
24
25 using namespace spvtools;
26
27 using opt::GraphicsRobustAccessPass;
28 using GraphicsRobustAccessTest = opt::PassTest<::testing::Test>;
29
30 // Test incompatible module, determined at module-level.
31
32 TEST_F(GraphicsRobustAccessTest, FailNotShader) {
33 const std::string text = R"(
34 ; CHECK: Can only process Shader modules
35 OpCapability Kernel
36 )";
37
38 SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
39 }
40
41 TEST_F(GraphicsRobustAccessTest, FailCantProcessVariablePointers) {
42 const std::string text = R"(
43 ; CHECK: Can't process modules with VariablePointers capability
44 OpCapability VariablePointers
45 )";
46
47 SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
48 }
49
50 TEST_F(GraphicsRobustAccessTest, FailCantProcessVariablePointersStorageBuffer) {
51 const std::string text = R"(
52 ; CHECK: Can't process modules with VariablePointersStorageBuffer capability
53 OpCapability VariablePointersStorageBuffer
54 )";
55
56 SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
57 }
58
59 TEST_F(GraphicsRobustAccessTest, FailCantProcessRuntimeDescriptorArrayEXT) {
60 const std::string text = R"(
61 ; CHECK: Can't process modules with RuntimeDescriptorArrayEXT capability
62 OpCapability RuntimeDescriptorArrayEXT
63 )";
64
65 SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
66 }
67
68 TEST_F(GraphicsRobustAccessTest, FailCantProcessPhysical32AddressingModel) {
69 const std::string text = R"(
70 ; CHECK: Addressing model must be Logical. Found OpMemoryModel Physical32 OpenCL
71 OpCapability Shader
72 OpMemoryModel Physical32 OpenCL
73 )";
74
75 SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
76 }
77
78 TEST_F(GraphicsRobustAccessTest, FailCantProcessPhysical64AddressingModel) {
79 const std::string text = R"(
80 ; CHECK: Addressing model must be Logical. Found OpMemoryModel Physical64 OpenCL
81 OpCapability Shader
82 OpMemoryModel Physical64 OpenCL
83 )";
84
85 SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
86 }
87
88 TEST_F(GraphicsRobustAccessTest,
89 FailCantProcessPhysicalStorageBuffer64EXTAddressingModel) {
90 const std::string text = R"(
91 ; CHECK: Addressing model must be Logical. Found OpMemoryModel PhysicalStorageBuffer64 GLSL450
92 OpCapability Shader
93 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
94 )";
95
96 SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
97 }
98
99 // Test access chains
100
101 // Returns the names of access chain instructions handled by the pass.
102 // For the purposes of this pass, regular and in-bounds access chains are the
103 // same.)
104 std::vector<const char*> AccessChains() {
105 return {"OpAccessChain", "OpInBoundsAccessChain"};
106 }
107
108 std::string ShaderPreamble() {
109 return R"(
110 OpCapability Shader
111 OpMemoryModel Logical Simple
112 OpEntryPoint GLCompute %main "main"
113 OpExecutionMode %main LocalSize 16 1 1
114 )";
115 }
116
117 std::string ShaderPreamble(const std::vector<std::string>& names) {
118 std::ostringstream os;
119 os << ShaderPreamble();
120 for (auto& name : names) {
121 os << " OpName %" << name << " \"" << name << "\"\n";
122 }
123 return os.str();
124 }
125
126 std::string ShaderPreambleAC() {
127 return ShaderPreamble({"ac", "ptr_ty", "var"});
128 }
129
130 std::string ShaderPreambleAC(const std::vector<std::string>& names) {
131 auto names2 = names;
132 names2.push_back("ac");
133 names2.push_back("ptr_ty");
134 names2.push_back("var");
135 return ShaderPreamble(names2);
136 }
137
138 std::string DecoSSBO() {
139 return R"(
140 OpDecorate %ssbo_s BufferBlock
141 OpMemberDecorate %ssbo_s 0 Offset 0
142 OpMemberDecorate %ssbo_s 1 Offset 4
143 OpMemberDecorate %ssbo_s 2 Offset 16
144 OpDecorate %var DescriptorSet 0
145 OpDecorate %var Binding 0
146 )";
147 }
148
149 std::string TypesVoid() {
150 return R"(
151 %void = OpTypeVoid
152 %void_fn = OpTypeFunction %void
153 )";
154 }
155
156 std::string TypesInt() {
157 return R"(
158 %uint = OpTypeInt 32 0
159 %int = OpTypeInt 32 1
160 )";
161 }
162
163 std::string TypesFloat() {
164 return R"(
165 %float = OpTypeFloat 32
166 )";
167 }
168
169 std::string TypesShort() {
170 return R"(
171 %ushort = OpTypeInt 16 0
172 %short = OpTypeInt 16 1
173 )";
174 }
175
176 std::string TypesLong() {
177 return R"(
178 %ulong = OpTypeInt 64 0
179 %long = OpTypeInt 64 1
180 )";
181 }
182
183 std::string MainPrefix() {
184 return R"(
185 %main = OpFunction %void None %void_fn
186 %entry = OpLabel
187 )";
188 }
189
190 std::string MainSuffix() {
191 return R"(
192 OpReturn
193 OpFunctionEnd
194 )";
195 }
196
197 std::string ACCheck(const std::string& access_chain_inst,
198 const std::string& original,
199 const std::string& transformed) {
200 return "\n ; CHECK: %ac = " + access_chain_inst + " %ptr_ty %var" +
201 (transformed.empty() ? "" : " ") + transformed +
202 "\n ; CHECK-NOT: " + access_chain_inst +
203 "\n ; CHECK-NEXT: OpReturn"
204 "\n %ac = " +
205 access_chain_inst + " %ptr_ty %var " + (original.empty() ? "" : " ") +
206 original + "\n";
207 }
208
209 std::string ACCheckFail(const std::string& access_chain_inst,
210 const std::string& original,
211 const std::string& transformed) {
212 return "\n ; CHECK: %ac = " + access_chain_inst + " %ptr_ty %var" +
213 (transformed.empty() ? "" : " ") + transformed +
214 "\n ; CHECK-NOT: " + access_chain_inst +
215 "\n ; CHECK-NOT: OpReturn"
216 "\n %ac = " +
217 access_chain_inst + " %ptr_ty %var " + (original.empty() ? "" : " ") +
218 original + "\n";
219 }
220
221 // Access chain into:
222 // Vector
223 // Vector sizes 2, 3, 4
224 // Matrix
225 // Matrix columns 2, 4
226 // Component is vector 2, 4
227 // Array
228 // Struct
229 // TODO(dneto): RuntimeArray
230
231 TEST_F(GraphicsRobustAccessTest, ACVectorLeastInboundConstantUntouched) {
232 SetTargetEnv(SPV_ENV_VULKAN_1_0);
233 for (auto* ac : AccessChains()) {
234 std::ostringstream shaders;
235 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
236 %uvec2 = OpTypeVector %uint 2
237 %var_ty = OpTypePointer Function %uvec2
238 %ptr_ty = OpTypePointer Function %uint
239 %uint_0 = OpConstant %uint 0
240 )"
241 << MainPrefix() << R"(
242 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%uint_0", "%uint_0")
243 << MainSuffix();
244 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
245 }
246 }
247
248 TEST_F(GraphicsRobustAccessTest, ACVectorMostInboundConstantUntouched) {
249 SetTargetEnv(SPV_ENV_VULKAN_1_0);
250 for (auto* ac : AccessChains()) {
251 std::ostringstream shaders;
252 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
253 %v4uint = OpTypeVector %uint 4
254 %var_ty = OpTypePointer Function %v4uint
255 %ptr_ty = OpTypePointer Function %uint
256 %uint_3 = OpConstant %uint 3
257 )"
258 << MainPrefix() << R"(
259 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%uint_3", "%uint_3")
260 << MainSuffix();
261 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
262 }
263 }
264
265 TEST_F(GraphicsRobustAccessTest, ACVectorExcessConstantClamped) {
266 SetTargetEnv(SPV_ENV_VULKAN_1_0);
267 for (auto* ac : AccessChains()) {
268 std::ostringstream shaders;
269 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
270 %v4uint = OpTypeVector %uint 4
271 %var_ty = OpTypePointer Function %v4uint
272 %ptr_ty = OpTypePointer Function %uint
273 %uint_4 = OpConstant %uint 4
274 )"
275 << MainPrefix() << R"(
276 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%uint_4", "%int_3")
277 << MainSuffix();
278 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
279 }
280 }
281
282 TEST_F(GraphicsRobustAccessTest, ACVectorNegativeConstantClamped) {
283 SetTargetEnv(SPV_ENV_VULKAN_1_0);
284 for (auto* ac : AccessChains()) {
285 std::ostringstream shaders;
286 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
287 %v4uint = OpTypeVector %uint 4
288 %var_ty = OpTypePointer Function %v4uint
289 %ptr_ty = OpTypePointer Function %uint
290 %int_n1 = OpConstant %int -1
291 )"
292 << MainPrefix() << R"(
293 ; CHECK: %int_0 = OpConstant %int 0
294 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%int_n1", "%int_0")
295 << MainSuffix();
296 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
297 }
298 }
299
300 // Like the previous test, but ensures the pass knows how to modify an index
301 // which does not come first in the access chain.
302 TEST_F(GraphicsRobustAccessTest, ACVectorInArrayNegativeConstantClamped) {
303 SetTargetEnv(SPV_ENV_VULKAN_1_0);
304 for (auto* ac : AccessChains()) {
305 std::ostringstream shaders;
306 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
307 %v4uint = OpTypeVector %uint 4
308 %uint_1 = OpConstant %uint 1
309 %uint_2 = OpConstant %uint 2
310 %arr = OpTypeArray %v4uint %uint_2
311 %var_ty = OpTypePointer Function %arr
312 %ptr_ty = OpTypePointer Function %uint
313 %int_n1 = OpConstant %int -1
314 )"
315 << MainPrefix() << R"(
316 ; CHECK: %int_0 = OpConstant %int 0
317 %var = OpVariable %var_ty Function)"
318 << ACCheck(ac, "%uint_1 %int_n1", "%uint_1 %int_0") << MainSuffix();
319 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
320 }
321 }
322
323 TEST_F(GraphicsRobustAccessTest, ACVectorGeneralClamped) {
324 SetTargetEnv(SPV_ENV_VULKAN_1_0);
325 for (auto* ac : AccessChains()) {
326 std::ostringstream shaders;
327 shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt() << R"(
328 %v4uint = OpTypeVector %uint 4
329 %var_ty = OpTypePointer Function %v4uint
330 %ptr_ty = OpTypePointer Function %uint
331 %i = OpUndef %int)"
332 << MainPrefix() << R"(
333 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
334 ; CHECK-DAG: %int_0 = OpConstant %int 0
335 ; CHECK-DAG: %int_3 = OpConstant %int 3
336 ; CHECK: OpLabel
337 ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_3
338 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
339 << MainSuffix();
340 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
341 }
342 }
343
344 TEST_F(GraphicsRobustAccessTest, ACVectorGeneralShortClamped) {
345 SetTargetEnv(SPV_ENV_VULKAN_1_0);
346 // Show that signed 16 bit integers are clamped as well.
347 for (auto* ac : AccessChains()) {
348 std::ostringstream shaders;
349 shaders << "OpCapability Int16\n"
350 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesShort() <<
351 R"(
352 %v4short = OpTypeVector %short 4
353 %var_ty = OpTypePointer Function %v4short
354 %ptr_ty = OpTypePointer Function %short
355 %i = OpUndef %short)"
356 << MainPrefix() << R"(
357 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
358 ; CHECK-NOT: = OpTypeInt 32
359 ; CHECK-DAG: %short_0 = OpConstant %short 0
360 ; CHECK-DAG: %short_3 = OpConstant %short 3
361 ; CHECK-NOT: = OpTypeInt 32
362 ; CHECK: OpLabel
363 ; CHECK: %[[clamp:\w+]] = OpExtInst %short %[[GLSLSTD450]] SClamp %i %short_0 %short_3
364 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
365 << MainSuffix();
366 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
367 }
368 }
369
370 TEST_F(GraphicsRobustAccessTest, ACVectorGeneralUShortClamped) {
371 SetTargetEnv(SPV_ENV_VULKAN_1_0);
372 // Show that unsigned 16 bit integers are clamped as well.
373 for (auto* ac : AccessChains()) {
374 std::ostringstream shaders;
375 shaders << "OpCapability Int16\n"
376 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesShort() <<
377 R"(
378 %v4ushort = OpTypeVector %ushort 4
379 %var_ty = OpTypePointer Function %v4ushort
380 %ptr_ty = OpTypePointer Function %ushort
381 %i = OpUndef %ushort)"
382 << MainPrefix() << R"(
383 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
384 ; CHECK-NOT: = OpTypeInt 32
385 ; CHECK-DAG: %short_0 = OpConstant %short 0
386 ; CHECK-DAG: %short_3 = OpConstant %short 3
387 ; CHECK-NOT: = OpTypeInt 32
388 ; CHECK: OpLabel
389 ; CHECK: %[[clamp:\w+]] = OpExtInst %ushort %[[GLSLSTD450]] SClamp %i %short_0 %short_3
390 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
391 << MainSuffix();
392 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
393 }
394 }
395
396 TEST_F(GraphicsRobustAccessTest, ACVectorGeneralLongClamped) {
397 SetTargetEnv(SPV_ENV_VULKAN_1_0);
398 // Show that signed 64 bit integers are clamped as well.
399 for (auto* ac : AccessChains()) {
400 std::ostringstream shaders;
401 shaders << "OpCapability Int64\n"
402 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesLong() <<
403 R"(
404 %v4long = OpTypeVector %long 4
405 %var_ty = OpTypePointer Function %v4long
406 %ptr_ty = OpTypePointer Function %long
407 %i = OpUndef %long)"
408 << MainPrefix() << R"(
409 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
410 ; CHECK-NOT: = OpTypeInt 32
411 ; CHECK-DAG: %long_0 = OpConstant %long 0
412 ; CHECK-DAG: %long_3 = OpConstant %long 3
413 ; CHECK-NOT: = OpTypeInt 32
414 ; CHECK: OpLabel
415 ; CHECK: %[[clamp:\w+]] = OpExtInst %long %[[GLSLSTD450]] SClamp %i %long_0 %long_3
416 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
417 << MainSuffix();
418 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
419 }
420 }
421
422 TEST_F(GraphicsRobustAccessTest, ACVectorGeneralULongClamped) {
423 SetTargetEnv(SPV_ENV_VULKAN_1_0);
424 // Show that unsigned 64 bit integers are clamped as well.
425 for (auto* ac : AccessChains()) {
426 std::ostringstream shaders;
427 shaders << "OpCapability Int64\n"
428 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesLong() <<
429 R"(
430 %v4ulong = OpTypeVector %ulong 4
431 %var_ty = OpTypePointer Function %v4ulong
432 %ptr_ty = OpTypePointer Function %ulong
433 %i = OpUndef %ulong)"
434 << MainPrefix() << R"(
435 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
436 ; CHECK-NOT: = OpTypeInt 32
437 ; CHECK-DAG: %long_0 = OpConstant %long 0
438 ; CHECK-DAG: %long_3 = OpConstant %long 3
439 ; CHECK-NOT: = OpTypeInt 32
440 ; CHECK: OpLabel
441 ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %long_0 %long_3
442 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
443 << MainSuffix();
444 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
445 }
446 }
447
448 TEST_F(GraphicsRobustAccessTest, ACMatrixLeastInboundConstantUntouched) {
449 SetTargetEnv(SPV_ENV_VULKAN_1_0);
450 for (auto* ac : AccessChains()) {
451 std::ostringstream shaders;
452 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
453 << TypesFloat() << R"(
454 %v2float = OpTypeVector %float 2
455 %mat4v2float = OpTypeMatrix %v2float 4
456 %var_ty = OpTypePointer Function %mat4v2float
457 %ptr_ty = OpTypePointer Function %float
458 %uint_0 = OpConstant %uint 0
459 %uint_1 = OpConstant %uint 1
460 )" << MainPrefix() << R"(
461 %var = OpVariable %var_ty Function)"
462 << ACCheck(ac, "%uint_0 %uint_1", "%uint_0 %uint_1")
463 << MainSuffix();
464 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
465 }
466 }
467
468 TEST_F(GraphicsRobustAccessTest, ACMatrixMostInboundConstantUntouched) {
469 SetTargetEnv(SPV_ENV_VULKAN_1_0);
470 for (auto* ac : AccessChains()) {
471 std::ostringstream shaders;
472 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
473 << TypesFloat() << R"(
474 %v2float = OpTypeVector %float 2
475 %mat4v2float = OpTypeMatrix %v2float 4
476 %var_ty = OpTypePointer Function %mat4v2float
477 %ptr_ty = OpTypePointer Function %float
478 %uint_1 = OpConstant %uint 1
479 %uint_3 = OpConstant %uint 3
480 )" << MainPrefix() << R"(
481 %var = OpVariable %var_ty Function)"
482 << ACCheck(ac, "%uint_3 %uint_1", "%uint_3 %uint_1")
483 << MainSuffix();
484 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
485 }
486 }
487
488 TEST_F(GraphicsRobustAccessTest, ACMatrixExcessConstantClamped) {
489 SetTargetEnv(SPV_ENV_VULKAN_1_0);
490 for (auto* ac : AccessChains()) {
491 std::ostringstream shaders;
492 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
493 << TypesFloat() << R"(
494 %v2float = OpTypeVector %float 2
495 %mat4v2float = OpTypeMatrix %v2float 4
496 %var_ty = OpTypePointer Function %mat4v2float
497 %ptr_ty = OpTypePointer Function %float
498 %uint_1 = OpConstant %uint 1
499 %uint_4 = OpConstant %uint 4
500 )" << MainPrefix() << R"(
501 ; CHECK: %int_3 = OpConstant %int 3
502 %var = OpVariable %var_ty Function)"
503 << ACCheck(ac, "%uint_4 %uint_1", "%int_3 %uint_1") << MainSuffix();
504 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
505 }
506 }
507
508 TEST_F(GraphicsRobustAccessTest, ACMatrixNegativeConstantClamped) {
509 SetTargetEnv(SPV_ENV_VULKAN_1_0);
510 for (auto* ac : AccessChains()) {
511 std::ostringstream shaders;
512 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
513 << TypesFloat() << R"(
514 %v2float = OpTypeVector %float 2
515 %mat4v2float = OpTypeMatrix %v2float 4
516 %var_ty = OpTypePointer Function %mat4v2float
517 %ptr_ty = OpTypePointer Function %float
518 %uint_1 = OpConstant %uint 1
519 %int_n1 = OpConstant %int -1
520 )" << MainPrefix() << R"(
521 ; CHECK: %int_0 = OpConstant %int 0
522 %var = OpVariable %var_ty Function)"
523 << ACCheck(ac, "%int_n1 %uint_1", "%int_0 %uint_1") << MainSuffix();
524 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
525 }
526 }
527
528 TEST_F(GraphicsRobustAccessTest, ACMatrixGeneralClamped) {
529 SetTargetEnv(SPV_ENV_VULKAN_1_0);
530 for (auto* ac : AccessChains()) {
531 std::ostringstream shaders;
532 shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
533 << TypesFloat() << R"(
534 %v2float = OpTypeVector %float 2
535 %mat4v2float = OpTypeMatrix %v2float 4
536 %var_ty = OpTypePointer Function %mat4v2float
537 %ptr_ty = OpTypePointer Function %float
538 %uint_1 = OpConstant %uint 1
539 %i = OpUndef %int
540 )" << MainPrefix() << R"(
541 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
542 ; CHECK-DAG: %int_0 = OpConstant %int 0
543 ; CHECK-DAG: %int_3 = OpConstant %int 3
544 ; CHECK: OpLabel
545 ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_3
546 %var = OpVariable %var_ty Function)"
547 << ACCheck(ac, "%i %uint_1", "%[[clamp]] %uint_1") << MainSuffix();
548 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
549 }
550 }
551
552 TEST_F(GraphicsRobustAccessTest, ACArrayLeastInboundConstantUntouched) {
553 SetTargetEnv(SPV_ENV_VULKAN_1_0);
554 for (auto* ac : AccessChains()) {
555 std::ostringstream shaders;
556 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
557 << TypesFloat() << R"(
558 %uint_200 = OpConstant %uint 200
559 %arr = OpTypeArray %float %uint_200
560 %var_ty = OpTypePointer Function %arr
561 %ptr_ty = OpTypePointer Function %float
562 %int_0 = OpConstant %int 0
563 )" << MainPrefix() << R"(
564 %var = OpVariable %var_ty Function)"
565 << ACCheck(ac, "%int_0", "%int_0") << MainSuffix();
566 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
567 }
568 }
569
570 TEST_F(GraphicsRobustAccessTest, ACArrayMostInboundConstantUntouched) {
571 SetTargetEnv(SPV_ENV_VULKAN_1_0);
572 for (auto* ac : AccessChains()) {
573 std::ostringstream shaders;
574 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
575 << TypesFloat() << R"(
576 %uint_200 = OpConstant %uint 200
577 %arr = OpTypeArray %float %uint_200
578 %var_ty = OpTypePointer Function %arr
579 %ptr_ty = OpTypePointer Function %float
580 %int_199 = OpConstant %int 199
581 )" << MainPrefix() << R"(
582 %var = OpVariable %var_ty Function)"
583 << ACCheck(ac, "%int_199", "%int_199") << MainSuffix();
584 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
585 }
586 }
587
588 TEST_F(GraphicsRobustAccessTest, ACArrayGeneralClamped) {
589 SetTargetEnv(SPV_ENV_VULKAN_1_0);
590 for (auto* ac : AccessChains()) {
591 std::ostringstream shaders;
592 shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
593 << TypesFloat() << R"(
594 %uint_200 = OpConstant %uint 200
595 %arr = OpTypeArray %float %uint_200
596 %var_ty = OpTypePointer Function %arr
597 %ptr_ty = OpTypePointer Function %float
598 %i = OpUndef %int
599 )" << MainPrefix() << R"(
600 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
601 ; CHECK-DAG: %int_0 = OpConstant %int 0
602 ; CHECK-DAG: %int_199 = OpConstant %int 199
603 ; CHECK: OpLabel
604 ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_199
605 %var = OpVariable %var_ty Function)"
606 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
607 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
608 }
609 }
610
611 TEST_F(GraphicsRobustAccessTest, ACArrayGeneralShortIndexUIntBoundsClamped) {
612 SetTargetEnv(SPV_ENV_VULKAN_1_0);
613 // Index is signed short, array bounds overflows the index type.
614 for (auto* ac : AccessChains()) {
615 std::ostringstream shaders;
616 shaders << "OpCapability Int16\n"
617 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
618 << TypesShort() << TypesFloat() << R"(
619 %uint_70000 = OpConstant %uint 70000 ; overflows 16bits
620 %arr = OpTypeArray %float %uint_70000
621 %var_ty = OpTypePointer Function %arr
622 %ptr_ty = OpTypePointer Function %float
623 %i = OpUndef %short
624 )" << MainPrefix() << R"(
625 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
626 ; CHECK-DAG: %int_0 = OpConstant %int 0
627 ; CHECK-DAG: %int_69999 = OpConstant %int 69999
628 ; CHECK: OpLabel
629 ; CHECK: %[[i_ext:\w+]] = OpSConvert %uint %i
630 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %int_0 %int_69999
631 %var = OpVariable %var_ty Function)"
632 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
633 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
634 }
635 }
636
637 TEST_F(GraphicsRobustAccessTest, ACArrayGeneralUShortIndexIntBoundsClamped) {
638 SetTargetEnv(SPV_ENV_VULKAN_1_0);
639 // Index is unsigned short, array bounds overflows the index type.
640 for (auto* ac : AccessChains()) {
641 std::ostringstream shaders;
642 shaders << "OpCapability Int16\n"
643 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
644 << TypesShort() << TypesFloat() << R"(
645 %int_70000 = OpConstant %int 70000 ; overflows 16bits
646 %arr = OpTypeArray %float %int_70000
647 %var_ty = OpTypePointer Function %arr
648 %ptr_ty = OpTypePointer Function %float
649 %i = OpUndef %ushort
650 )" << MainPrefix() << R"(
651 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
652 ; CHECK-DAG: %int_0 = OpConstant %int 0
653 ; CHECK-DAG: %int_69999 = OpConstant %int 69999
654 ; CHECK: OpLabel
655 ; CHECK: %[[i_ext:\w+]] = OpUConvert %uint %i
656 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %int_0 %int_69999
657 %var = OpVariable %var_ty Function)"
658 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
659 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
660 }
661 }
662
663 TEST_F(GraphicsRobustAccessTest, ACArrayGeneralUIntIndexShortBoundsClamped) {
664 SetTargetEnv(SPV_ENV_VULKAN_1_0);
665 // Signed int index i is wider than the array bounds type.
666 for (auto* ac : AccessChains()) {
667 std::ostringstream shaders;
668 shaders << "OpCapability Int16\n"
669 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
670 << TypesShort() << TypesFloat() << R"(
671 %short_200 = OpConstant %short 200
672 %arr = OpTypeArray %float %short_200
673 %var_ty = OpTypePointer Function %arr
674 %ptr_ty = OpTypePointer Function %float
675 %i = OpUndef %uint
676 )" << MainPrefix() << R"(
677 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
678 ; CHECK-DAG: %int_0 = OpConstant %int 0
679 ; CHECK-DAG: %int_199 = OpConstant %int 199
680 ; CHECK: OpLabel
681 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %i %int_0 %int_199
682 %var = OpVariable %var_ty Function)"
683 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
684 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
685 }
686 }
687
688 TEST_F(GraphicsRobustAccessTest, ACArrayGeneralIntIndexUShortBoundsClamped) {
689 SetTargetEnv(SPV_ENV_VULKAN_1_0);
690 // Unsigned int index i is wider than the array bounds type.
691 for (auto* ac : AccessChains()) {
692 std::ostringstream shaders;
693 shaders << "OpCapability Int16\n"
694 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
695 << TypesShort() << TypesFloat() << R"(
696 %ushort_200 = OpConstant %ushort 200
697 %arr = OpTypeArray %float %ushort_200
698 %var_ty = OpTypePointer Function %arr
699 %ptr_ty = OpTypePointer Function %float
700 %i = OpUndef %int
701 )" << MainPrefix() << R"(
702 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
703 ; CHECK-DAG: %int_0 = OpConstant %int 0
704 ; CHECK-DAG: %int_199 = OpConstant %int 199
705 ; CHECK: OpLabel
706 ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_199
707 %var = OpVariable %var_ty Function)"
708 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
709 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
710 }
711 }
712
713 TEST_F(GraphicsRobustAccessTest, ACArrayGeneralLongIndexUIntBoundsClamped) {
714 SetTargetEnv(SPV_ENV_VULKAN_1_0);
715 // Signed long index i is wider than the array bounds type.
716 for (auto* ac : AccessChains()) {
717 std::ostringstream shaders;
718 shaders << "OpCapability Int64\n"
719 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
720 << TypesLong() << TypesFloat() << R"(
721 %uint_200 = OpConstant %uint 200
722 %arr = OpTypeArray %float %uint_200
723 %var_ty = OpTypePointer Function %arr
724 %ptr_ty = OpTypePointer Function %float
725 %i = OpUndef %long
726 )" << MainPrefix() << R"(
727 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
728 ; CHECK-DAG: %long_0 = OpConstant %long 0
729 ; CHECK-DAG: %long_199 = OpConstant %long 199
730 ; CHECK: OpLabel
731 ; CHECK: %[[clamp:\w+]] = OpExtInst %long %[[GLSLSTD450]] SClamp %i %long_0 %long_199
732 %var = OpVariable %var_ty Function)"
733 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
734 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
735 }
736 }
737
738 TEST_F(GraphicsRobustAccessTest, ACArrayGeneralULongIndexIntBoundsClamped) {
739 SetTargetEnv(SPV_ENV_VULKAN_1_0);
740 // Unsigned long index i is wider than the array bounds type.
741 for (auto* ac : AccessChains()) {
742 std::ostringstream shaders;
743 shaders << "OpCapability Int64\n"
744 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
745 << TypesLong() << TypesFloat() << R"(
746 %int_200 = OpConstant %int 200
747 %arr = OpTypeArray %float %int_200
748 %var_ty = OpTypePointer Function %arr
749 %ptr_ty = OpTypePointer Function %float
750 %i = OpUndef %ulong
751 )" << MainPrefix() << R"(
752 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
753 ; CHECK-DAG: %long_0 = OpConstant %long 0
754 ; CHECK-DAG: %long_199 = OpConstant %long 199
755 ; CHECK: OpLabel
756 ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %long_0 %long_199
757 %var = OpVariable %var_ty Function)"
758 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
759 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
760 }
761 }
762
763 TEST_F(GraphicsRobustAccessTest,
764 ACArrayGeneralShortIndeArrayBiggerThanShortMaxClipsToShortIntMax) {
765 SetTargetEnv(SPV_ENV_VULKAN_1_0);
766 for (auto* ac : AccessChains()) {
767 std::ostringstream shaders;
768 shaders << "OpCapability Int16\n"
769 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesShort()
770 << TypesInt() << TypesFloat() << R"(
771 %uint_50000 = OpConstant %uint 50000
772 %arr = OpTypeArray %float %uint_50000
773 %var_ty = OpTypePointer Function %arr
774 %ptr_ty = OpTypePointer Function %float
775 %i = OpUndef %ushort
776 )" << MainPrefix() << R"(
777 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
778 ; CHECK-DAG: %short_0 = OpConstant %short 0
779 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %short 32767
780 ; CHECK: OpLabel
781 ; CHECK: %[[clamp:\w+]] = OpExtInst %ushort %[[GLSLSTD450]] SClamp %i %short_0 %[[intmax]]
782 %var = OpVariable %var_ty Function)"
783 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
784 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
785 }
786 }
787
788 TEST_F(GraphicsRobustAccessTest,
789 ACArrayGeneralIntIndexArrayBiggerThanIntMaxClipsToSignedIntMax) {
790 SetTargetEnv(SPV_ENV_VULKAN_1_0);
791 for (auto* ac : AccessChains()) {
792 std::ostringstream shaders;
793 shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
794 << TypesFloat() << R"(
795 %uint_3000000000 = OpConstant %uint 3000000000
796 %arr = OpTypeArray %float %uint_3000000000
797 %var_ty = OpTypePointer Function %arr
798 %ptr_ty = OpTypePointer Function %float
799 %i = OpUndef %uint
800 )" << MainPrefix() << R"(
801 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
802 ; CHECK-DAG: %int_0 = OpConstant %int 0
803 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
804 ; CHECK: OpLabel
805 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %i %int_0 %[[intmax]]
806 %var = OpVariable %var_ty Function)"
807 << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
808 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
809 }
810 }
811
812 TEST_F(GraphicsRobustAccessTest,
813 ACArrayGeneralLongIndexArrayBiggerThanLongMaxClipsToSignedLongMax) {
814 SetTargetEnv(SPV_ENV_VULKAN_1_0);
815 for (auto* ac : AccessChains()) {
816 std::ostringstream shaders;
817 shaders << "OpCapability Int64\n"
818 << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
819 << TypesLong()
820 << TypesFloat()
821 // 2^63 == 9,223,372,036,854,775,807
822 << R"(
823 %ulong_9223372036854775999 = OpConstant %ulong 9223372036854775999
824 %arr = OpTypeArray %float %ulong_9223372036854775999
825 %var_ty = OpTypePointer Function %arr
826 %ptr_ty = OpTypePointer Function %float
827 %i = OpUndef %ulong
828 )"
829 << MainPrefix() << R"(
830 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
831 ; CHECK-DAG: %long_0 = OpConstant %long 0
832 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %long 9223372036854775807
833 ; CHECK: OpLabel
834 ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %long_0 %[[intmax]]
835 %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
836 << MainSuffix();
837 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
838 }
839 }
840
841 TEST_F(GraphicsRobustAccessTest, ACArraySpecIdSizedAlwaysClamped) {
842 SetTargetEnv(SPV_ENV_VULKAN_1_0);
843 for (auto* ac : AccessChains()) {
844 std::ostringstream shaders;
845 shaders << ShaderPreambleAC({"spec200"}) << R"(
846 OpDecorate %spec200 SpecId 0 )" << TypesVoid() << TypesInt()
847 << TypesFloat() << R"(
848 %spec200 = OpSpecConstant %int 200
849 %arr = OpTypeArray %float %spec200
850 %var_ty = OpTypePointer Function %arr
851 %ptr_ty = OpTypePointer Function %float
852 %uint_5 = OpConstant %uint 5
853 )" << MainPrefix() << R"(
854 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
855 ; CHECK-DAG: %uint_0 = OpConstant %uint 0
856 ; CHECK-DAG: %uint_1 = OpConstant %uint 1
857 ; CHECK-DAG: %[[uint_intmax:\w+]] = OpConstant %uint 2147483647
858 ; CHECK: OpLabel
859 ; CHECK: %[[max:\w+]] = OpISub %uint %spec200 %uint_1
860 ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[uint_intmax]]
861 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %uint_5 %uint_0 %[[smin]]
862 %var = OpVariable %var_ty Function)"
863 << ACCheck(ac, "%uint_5", "%[[clamp]]") << MainSuffix();
864 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
865 }
866 }
867
868 TEST_F(GraphicsRobustAccessTest, ACStructLeastUntouched) {
869 SetTargetEnv(SPV_ENV_VULKAN_1_0);
870 for (auto* ac : AccessChains()) {
871 std::ostringstream shaders;
872 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
873 << TypesFloat() << R"(
874 %struct = OpTypeStruct %float %float %float
875 %var_ty = OpTypePointer Function %struct
876 %ptr_ty = OpTypePointer Function %float
877 %int_0 = OpConstant %int 0
878 )" << MainPrefix() << R"(
879 %var = OpVariable %var_ty Function)"
880 << ACCheck(ac, "%int_0", "%int_0") << MainSuffix();
881 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
882 }
883 }
884
885 TEST_F(GraphicsRobustAccessTest, ACStructMostUntouched) {
886 SetTargetEnv(SPV_ENV_VULKAN_1_0);
887 for (auto* ac : AccessChains()) {
888 std::ostringstream shaders;
889 shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
890 << TypesFloat() << R"(
891 %struct = OpTypeStruct %float %float %float
892 %var_ty = OpTypePointer Function %struct
893 %ptr_ty = OpTypePointer Function %float
894 %int_2 = OpConstant %int 2
895 )" << MainPrefix() << R"(
896 %var = OpVariable %var_ty Function)"
897 << ACCheck(ac, "%int_2", "%int_2") << MainSuffix();
898 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
899 }
900 }
901
902 TEST_F(GraphicsRobustAccessTest, ACStructSpecConstantFail) {
903 SetTargetEnv(SPV_ENV_VULKAN_1_0);
904 for (auto* ac : AccessChains()) {
905 std::ostringstream shaders;
906 shaders << ShaderPreambleAC({"struct", "spec200"})
907 << "OpDecorate %spec200 SpecId 0\n"
908 <<
909
910 TypesVoid() << TypesInt() << TypesFloat() << R"(
911 %spec200 = OpSpecConstant %int 200
912 %struct = OpTypeStruct %float %float %float
913 %var_ty = OpTypePointer Function %struct
914 %ptr_ty = OpTypePointer Function %float
915 )" << MainPrefix() << R"(
916 %var = OpVariable %var_ty Function
917 ; CHECK: Member index into struct is not a constant integer
918 ; CHECK-SAME: %spec200 = OpSpecConstant %int 200
919 )"
920 << ACCheckFail(ac, "%spec200", "%spec200") << MainSuffix();
921 SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
922 }
923 }
924
925 TEST_F(GraphicsRobustAccessTest, ACStructFloatConstantFail) {
926 SetTargetEnv(SPV_ENV_VULKAN_1_0);
927 for (auto* ac : AccessChains()) {
928 std::ostringstream shaders;
929 shaders << ShaderPreambleAC({"struct"}) <<
930
931 TypesVoid() << TypesInt() << TypesFloat() << R"(
932 %float_2 = OpConstant %float 2
933 %struct = OpTypeStruct %float %float %float
934 %var_ty = OpTypePointer Function %struct
935 %ptr_ty = OpTypePointer Function %float
936 )" << MainPrefix() << R"(
937 %var = OpVariable %var_ty Function
938 ; CHECK: Member index into struct is not a constant integer
939 ; CHECK-SAME: %float_2 = OpConstant %float 2
940 )"
941 << ACCheckFail(ac, "%float_2", "%float_2") << MainSuffix();
942 SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
943 }
944 }
945
946 TEST_F(GraphicsRobustAccessTest, ACStructNonConstantFail) {
947 SetTargetEnv(SPV_ENV_VULKAN_1_0);
948 for (auto* ac : AccessChains()) {
949 std::ostringstream shaders;
950 shaders << ShaderPreambleAC({"struct", "i"}) <<
951
952 TypesVoid() << TypesInt() << TypesFloat() << R"(
953 %float_2 = OpConstant %float 2
954 %struct = OpTypeStruct %float %float %float
955 %var_ty = OpTypePointer Function %struct
956 %ptr_ty = OpTypePointer Function %float
957 %i = OpUndef %int
958 )" << MainPrefix() << R"(
959 %var = OpVariable %var_ty Function
960 ; CHECK: Member index into struct is not a constant integer
961 ; CHECK-SAME: %i = OpUndef %int
962 )"
963 << ACCheckFail(ac, "%i", "%i") << MainSuffix();
964 SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
965 }
966 }
967
968 TEST_F(GraphicsRobustAccessTest, ACStructExcessFail) {
969 SetTargetEnv(SPV_ENV_VULKAN_1_0);
970 for (auto* ac : AccessChains()) {
971 std::ostringstream shaders;
972 shaders << ShaderPreambleAC({"struct", "i"}) << TypesVoid() << TypesInt()
973 << TypesFloat() << R"(
974 %struct = OpTypeStruct %float %float %float
975 %var_ty = OpTypePointer Function %struct
976 %ptr_ty = OpTypePointer Function %float
977 %i = OpConstant %int 4
978 )" << MainPrefix() << R"(
979 %var = OpVariable %var_ty Function
980 ; CHECK: Member index 4 is out of bounds for struct type:
981 ; CHECK-SAME: %struct = OpTypeStruct %float %float %float
982 )"
983 << ACCheckFail(ac, "%i", "%i") << MainSuffix();
984 SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
985 }
986 }
987
988 TEST_F(GraphicsRobustAccessTest, ACStructNegativeFail) {
989 SetTargetEnv(SPV_ENV_VULKAN_1_0);
990 for (auto* ac : AccessChains()) {
991 std::ostringstream shaders;
992 shaders << ShaderPreambleAC({"struct", "i"}) << TypesVoid() << TypesInt()
993 << TypesFloat() << R"(
994 %struct = OpTypeStruct %float %float %float
995 %var_ty = OpTypePointer Function %struct
996 %ptr_ty = OpTypePointer Function %float
997 %i = OpConstant %int -1
998 )" << MainPrefix() << R"(
999 %var = OpVariable %var_ty Function
1000 ; CHECK: Member index -1 is out of bounds for struct type:
1001 ; CHECK-SAME: %struct = OpTypeStruct %float %float %float
1002 )"
1003 << ACCheckFail(ac, "%i", "%i") << MainSuffix();
1004 SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
1005 }
1006 }
1007
1008 TEST_F(GraphicsRobustAccessTest, ACRTArrayLeastInboundClamped) {
1009 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1010 for (auto* ac : AccessChains()) {
1011 std::ostringstream shaders;
1012 shaders << ShaderPreambleAC() << "OpDecorate %rtarr ArrayStride 4 "
1013 << DecoSSBO() << TypesVoid() << TypesInt() << TypesFloat() << R"(
1014 %rtarr = OpTypeRuntimeArray %float
1015 %ssbo_s = OpTypeStruct %uint %uint %rtarr
1016 %var_ty = OpTypePointer Uniform %ssbo_s
1017 %ptr_ty = OpTypePointer Uniform %float
1018 %var = OpVariable %var_ty Uniform
1019 %int_0 = OpConstant %int 0
1020 %int_2 = OpConstant %int 2
1021 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1022 ; CHECK-DAG: %int_1 = OpConstant %int 1
1023 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
1024 ; CHECK: OpLabel
1025 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1026 ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
1027 ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1028 ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %int_0 %int_0 %[[smin]]
1029 )"
1030 << MainPrefix() << ACCheck(ac, "%int_2 %int_0", "%int_2 %[[clamp]]")
1031 << MainSuffix();
1032 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1033 }
1034 }
1035
1036 TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralShortIndexClamped) {
1037 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1038 for (auto* ac : AccessChains()) {
1039 std::ostringstream shaders;
1040 shaders << "OpCapability Int16\n"
1041 << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
1042 << DecoSSBO() << TypesVoid() << TypesShort() << TypesFloat() << R"(
1043 %rtarr = OpTypeRuntimeArray %float
1044 %ssbo_s = OpTypeStruct %short %short %rtarr
1045 %var_ty = OpTypePointer Uniform %ssbo_s
1046 %ptr_ty = OpTypePointer Uniform %float
1047 %var = OpVariable %var_ty Uniform
1048 %short_2 = OpConstant %short 2
1049 %i = OpUndef %short
1050 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1051 ; CHECK: %uint = OpTypeInt 32 0
1052 ; CHECK-DAG: %uint_1 = OpConstant %uint 1
1053 ; CHECK-DAG: %uint_0 = OpConstant %uint 0
1054 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %uint 2147483647
1055 ; CHECK: OpLabel
1056 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1057 ; CHECK-DAG: %[[max:\w+]] = OpISub %uint %[[arrlen]] %uint_1
1058 ; CHECK-DAG: %[[i_ext:\w+]] = OpSConvert %uint %i
1059 ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1060 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %uint_0 %[[smin]]
1061 )"
1062 << MainPrefix() << ACCheck(ac, "%short_2 %i", "%short_2 %[[clamp]]")
1063 << MainSuffix();
1064 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1065 }
1066 }
1067
1068 TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralUShortIndexClamped) {
1069 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1070 for (auto* ac : AccessChains()) {
1071 std::ostringstream shaders;
1072 shaders << "OpCapability Int16\n"
1073 << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
1074 << DecoSSBO() << TypesVoid() << TypesShort() << TypesFloat() << R"(
1075 %rtarr = OpTypeRuntimeArray %float
1076 %ssbo_s = OpTypeStruct %short %short %rtarr
1077 %var_ty = OpTypePointer Uniform %ssbo_s
1078 %ptr_ty = OpTypePointer Uniform %float
1079 %var = OpVariable %var_ty Uniform
1080 %short_2 = OpConstant %short 2
1081 %i = OpUndef %ushort
1082 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1083 ; CHECK: %uint = OpTypeInt 32 0
1084 ; CHECK-DAG: %uint_1 = OpConstant %uint 1
1085 ; CHECK-DAG: %uint_0 = OpConstant %uint 0
1086 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %uint 2147483647
1087 ; CHECK: OpLabel
1088 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1089 ; CHECK-DAG: %[[max:\w+]] = OpISub %uint %[[arrlen]] %uint_1
1090 ; CHECK-DAG: %[[i_ext:\w+]] = OpSConvert %uint %i
1091 ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1092 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %uint_0 %[[smin]]
1093 )"
1094 << MainPrefix() << ACCheck(ac, "%short_2 %i", "%short_2 %[[clamp]]")
1095 << MainSuffix();
1096 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1097 }
1098 }
1099
1100 TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralIntIndexClamped) {
1101 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1102 for (auto* ac : AccessChains()) {
1103 std::ostringstream shaders;
1104 shaders << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
1105 << DecoSSBO() << TypesVoid() << TypesInt() << TypesFloat() << R"(
1106 %rtarr = OpTypeRuntimeArray %float
1107 %ssbo_s = OpTypeStruct %int %int %rtarr
1108 %var_ty = OpTypePointer Uniform %ssbo_s
1109 %ptr_ty = OpTypePointer Uniform %float
1110 %var = OpVariable %var_ty Uniform
1111 %int_2 = OpConstant %int 2
1112 %i = OpUndef %int
1113 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1114 ; CHECK-DAG: %int_1 = OpConstant %int 1
1115 ; CHECK-DAG: %int_0 = OpConstant %int 0
1116 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
1117 ; CHECK: OpLabel
1118 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1119 ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
1120 ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1121 ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %[[smin]]
1122 )"
1123 << MainPrefix() << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]")
1124 << MainSuffix();
1125 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1126 }
1127 }
1128
1129 TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralUIntIndexClamped) {
1130 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1131 for (auto* ac : AccessChains()) {
1132 std::ostringstream shaders;
1133 shaders << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
1134 << DecoSSBO() << TypesVoid() << TypesInt() << TypesFloat() << R"(
1135 %rtarr = OpTypeRuntimeArray %float
1136 %ssbo_s = OpTypeStruct %int %int %rtarr
1137 %var_ty = OpTypePointer Uniform %ssbo_s
1138 %ptr_ty = OpTypePointer Uniform %float
1139 %var = OpVariable %var_ty Uniform
1140 %int_2 = OpConstant %int 2
1141 %i = OpUndef %uint
1142 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1143 ; CHECK-DAG: %uint_1 = OpConstant %uint 1
1144 ; CHECK-DAG: %uint_0 = OpConstant %uint 0
1145 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %uint 2147483647
1146 ; CHECK: OpLabel
1147 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1148 ; CHECK: %[[max:\w+]] = OpISub %uint %[[arrlen]] %uint_1
1149 ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1150 ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %i %uint_0 %[[smin]]
1151 )"
1152 << MainPrefix() << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]")
1153 << MainSuffix();
1154 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1155 }
1156 }
1157
1158 TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralLongIndexClamped) {
1159 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1160 for (auto* ac : AccessChains()) {
1161 std::ostringstream shaders;
1162 shaders << "OpCapability Int64" << ShaderPreambleAC({"i"})
1163 << "OpDecorate %rtarr ArrayStride 4 " << DecoSSBO() << TypesVoid()
1164 << TypesInt() << TypesLong() << TypesFloat() << R"(
1165 %rtarr = OpTypeRuntimeArray %float
1166 %ssbo_s = OpTypeStruct %int %int %rtarr
1167 %var_ty = OpTypePointer Uniform %ssbo_s
1168 %ptr_ty = OpTypePointer Uniform %float
1169 %var = OpVariable %var_ty Uniform
1170 %int_2 = OpConstant %int 2
1171 %i = OpUndef %long
1172 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1173 ; CHECK-DAG: %long_0 = OpConstant %long 0
1174 ; CHECK-DAG: %long_1 = OpConstant %long 1
1175 ; CHECK-DAG: %[[longmax:\w+]] = OpConstant %long 9223372036854775807
1176 ; CHECK: OpLabel
1177 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1178 ; CHECK: %[[arrlen_ext:\w+]] = OpUConvert %ulong %[[arrlen]]
1179 ; CHECK: %[[max:\w+]] = OpISub %long %[[arrlen_ext]] %long_1
1180 ; CHECK: %[[smin:\w+]] = OpExtInst %long %[[GLSLSTD450]] UMin %[[max]] %[[longmax]]
1181 ; CHECK: %[[clamp:\w+]] = OpExtInst %long %[[GLSLSTD450]] SClamp %i %long_0 %[[smin]]
1182 )" << MainPrefix()
1183 << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]") << MainSuffix();
1184 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1185 }
1186 }
1187
1188 TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralULongIndexClamped) {
1189 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1190 for (auto* ac : AccessChains()) {
1191 std::ostringstream shaders;
1192 shaders << "OpCapability Int64" << ShaderPreambleAC({"i"})
1193 << "OpDecorate %rtarr ArrayStride 4 " << DecoSSBO() << TypesVoid()
1194 << TypesInt() << TypesLong() << TypesFloat() << R"(
1195 %rtarr = OpTypeRuntimeArray %float
1196 %ssbo_s = OpTypeStruct %int %int %rtarr
1197 %var_ty = OpTypePointer Uniform %ssbo_s
1198 %ptr_ty = OpTypePointer Uniform %float
1199 %var = OpVariable %var_ty Uniform
1200 %int_2 = OpConstant %int 2
1201 %i = OpUndef %ulong
1202 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1203 ; CHECK-DAG: %ulong_0 = OpConstant %ulong 0
1204 ; CHECK-DAG: %ulong_1 = OpConstant %ulong 1
1205 ; CHECK-DAG: %[[longmax:\w+]] = OpConstant %ulong 9223372036854775807
1206 ; CHECK: OpLabel
1207 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1208 ; CHECK: %[[arrlen_ext:\w+]] = OpUConvert %ulong %[[arrlen]]
1209 ; CHECK: %[[max:\w+]] = OpISub %ulong %[[arrlen_ext]] %ulong_1
1210 ; CHECK: %[[smin:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] UMin %[[max]] %[[longmax]]
1211 ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %ulong_0 %[[smin]]
1212 )" << MainPrefix()
1213 << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]") << MainSuffix();
1214 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1215 }
1216 }
1217
1218 TEST_F(GraphicsRobustAccessTest, ACRTArrayStructVectorElem) {
1219 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1220 // The point of this test is that the access chain can have indices past the
1221 // index into the runtime array. For good measure, the index into the final
1222 // struct is out of bounds. We have to clamp that index too.
1223 for (auto* ac : AccessChains()) {
1224 std::ostringstream shaders;
1225 shaders << ShaderPreambleAC({"i", "j"})
1226 << "OpDecorate %rtarr ArrayStride 32\n"
1227 << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
1228 << "OpMemberDecorate %rtelem 1 Offset 16\n"
1229 << TypesVoid() << TypesInt() << TypesFloat() << R"(
1230 %v4float = OpTypeVector %float 4
1231 %rtelem = OpTypeStruct %v4float %v4float
1232 %rtarr = OpTypeRuntimeArray %rtelem
1233 %ssbo_s = OpTypeStruct %int %int %rtarr
1234 %var_ty = OpTypePointer Uniform %ssbo_s
1235 %ptr_ty = OpTypePointer Uniform %float
1236 %var = OpVariable %var_ty Uniform
1237 %int_1 = OpConstant %int 1
1238 %int_2 = OpConstant %int 2
1239 %i = OpUndef %int
1240 %j = OpUndef %int
1241 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1242 ; CHECK-DAG: %int_0 = OpConstant %int 0
1243 ; CHECK-DAG: %int_3 = OpConstant %int 3
1244 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
1245 ; CHECK: OpLabel
1246 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
1247 ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
1248 ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1249 ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %[[smin]]
1250 ; CHECK: %[[clamp_j:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %j %int_0 %int_3
1251 )" << MainPrefix()
1252 << ACCheck(ac, "%int_2 %i %int_1 %j",
1253 "%int_2 %[[clamp_i]] %int_1 %[[clamp_j]]")
1254 << MainSuffix();
1255 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1256 }
1257 }
1258
1259 TEST_F(GraphicsRobustAccessTest, ACArrayRTArrayStructVectorElem) {
1260 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1261 // Now add an additional level of arrays around the Block-decorated struct.
1262 for (auto* ac : AccessChains()) {
1263 std::ostringstream shaders;
1264 shaders << ShaderPreambleAC({"i", "ssbo_s"})
1265 << "OpDecorate %rtarr ArrayStride 32\n"
1266 << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
1267 << "OpMemberDecorate %rtelem 1 Offset 16\n"
1268 << TypesVoid() << TypesInt() << TypesFloat() << R"(
1269 %v4float = OpTypeVector %float 4
1270 %rtelem = OpTypeStruct %v4float %v4float
1271 %rtarr = OpTypeRuntimeArray %rtelem
1272 %ssbo_s = OpTypeStruct %int %int %rtarr
1273 %arr_size = OpConstant %int 10
1274 %arr_ssbo = OpTypeArray %ssbo_s %arr_size
1275 %var_ty = OpTypePointer Uniform %arr_ssbo
1276 %ptr_ty = OpTypePointer Uniform %float
1277 %var = OpVariable %var_ty Uniform
1278 %int_1 = OpConstant %int 1
1279 %int_2 = OpConstant %int 2
1280 %int_17 = OpConstant %int 17
1281 %i = OpUndef %int
1282 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1283 ; CHECK-DAG: %[[ssbo_p:\w+]] = OpTypePointer Uniform %ssbo_s
1284 ; CHECK-DAG: %int_0 = OpConstant %int 0
1285 ; CHECK-DAG: %int_9 = OpConstant %int 9
1286 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
1287 ; CHECK: OpLabel
1288 ; This access chain is manufactured only so we can compute the array length.
1289 ; Note that the %int_9 is already clamped
1290 ; CHECK: %[[ssbo_base:\w+]] = )" << ac
1291 << R"( %[[ssbo_p]] %var %int_9
1292 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %[[ssbo_base]] 2
1293 ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
1294 ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1295 ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %[[smin]]
1296 )" << MainPrefix()
1297 << ACCheck(ac, "%int_17 %int_2 %i %int_1 %int_2",
1298 "%int_9 %int_2 %[[clamp_i]] %int_1 %int_2")
1299 << MainSuffix();
1300 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1301 }
1302 }
1303
1304 TEST_F(GraphicsRobustAccessTest, ACSplitACArrayRTArrayStructVectorElem) {
1305 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1306 // Split the address calculation across two access chains. Force
1307 // the transform to walk up the access chains to find the base variable.
1308 for (auto* ac : AccessChains()) {
1309 std::ostringstream shaders;
1310 shaders << ShaderPreambleAC({"i", "j", "k", "ssbo_s", "ssbo_pty",
1311 "rtarr_pty", "ac_ssbo", "ac_rtarr"})
1312 << "OpDecorate %rtarr ArrayStride 32\n"
1313 << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
1314 << "OpMemberDecorate %rtelem 1 Offset 16\n"
1315 << TypesVoid() << TypesInt() << TypesFloat() << R"(
1316 %v4float = OpTypeVector %float 4
1317 %rtelem = OpTypeStruct %v4float %v4float
1318 %rtarr = OpTypeRuntimeArray %rtelem
1319 %ssbo_s = OpTypeStruct %int %int %rtarr
1320 %arr_size = OpConstant %int 10
1321 %arr_ssbo = OpTypeArray %ssbo_s %arr_size
1322 %var_ty = OpTypePointer Uniform %arr_ssbo
1323 %ssbo_pty = OpTypePointer Uniform %ssbo_s
1324 %rtarr_pty = OpTypePointer Uniform %rtarr
1325 %ptr_ty = OpTypePointer Uniform %float
1326 %var = OpVariable %var_ty Uniform
1327 %int_1 = OpConstant %int 1
1328 %int_2 = OpConstant %int 2
1329 %i = OpUndef %int
1330 %j = OpUndef %int
1331 %k = OpUndef %int
1332 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1333 ; CHECK-DAG: %int_0 = OpConstant %int 0
1334 ; CHECK-DAG: %int_9 = OpConstant %int 9
1335 ; CHECK-DAG: %int_3 = OpConstant %int 3
1336 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
1337 ; CHECK: OpLabel
1338 ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_9
1339 ; CHECK: %ac_ssbo = )" << ac
1340 << R"( %ssbo_pty %var %[[clamp_i]]
1341 ; CHECK: %ac_rtarr = )"
1342 << ac << R"( %rtarr_pty %ac_ssbo %int_2
1343
1344 ; This is the interesting bit. This array length is needed for an OpAccessChain
1345 ; computing %ac, but the algorithm had to track back through %ac_rtarr's
1346 ; definition to find the base pointer %ac_ssbo.
1347 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %ac_ssbo 2
1348 ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
1349 ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1350 ; CHECK: %[[clamp_j:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %j %int_0 %[[smin]]
1351 ; CHECK: %[[clamp_k:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %k %int_0 %int_3
1352 ; CHECK: %ac = )" << ac
1353 << R"( %ptr_ty %ac_rtarr %[[clamp_j]] %int_1 %[[clamp_k]]
1354 ; CHECK-NOT: AccessChain
1355 )" << MainPrefix()
1356 << "%ac_ssbo = " << ac << " %ssbo_pty %var %i\n"
1357 << "%ac_rtarr = " << ac << " %rtarr_pty %ac_ssbo %int_2\n"
1358 << "%ac = " << ac << " %ptr_ty %ac_rtarr %j %int_1 %k\n"
1359
1360 << MainSuffix();
1361 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1362 }
1363 }
1364
1365 TEST_F(GraphicsRobustAccessTest,
1366 ACSplitACArrayRTArrayStructVectorElemAcrossBasicBlocks) {
1367 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1368 // Split the address calculation across two access chains. Force
1369 // the transform to walk up the access chains to find the base variable.
1370 // This time, put the different access chains in different basic blocks.
1371 // This is an integrity check to ensure that we keep the instruction-to-block
1372 // mapping consistent.
1373 for (auto* ac : AccessChains()) {
1374 std::ostringstream shaders;
1375 shaders << ShaderPreambleAC({"i", "j", "k", "bb1", "bb2", "ssbo_s",
1376 "ssbo_pty", "rtarr_pty", "ac_ssbo",
1377 "ac_rtarr"})
1378 << "OpDecorate %rtarr ArrayStride 32\n"
1379 << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
1380 << "OpMemberDecorate %rtelem 1 Offset 16\n"
1381 << TypesVoid() << TypesInt() << TypesFloat() << R"(
1382 %v4float = OpTypeVector %float 4
1383 %rtelem = OpTypeStruct %v4float %v4float
1384 %rtarr = OpTypeRuntimeArray %rtelem
1385 %ssbo_s = OpTypeStruct %int %int %rtarr
1386 %arr_size = OpConstant %int 10
1387 %arr_ssbo = OpTypeArray %ssbo_s %arr_size
1388 %var_ty = OpTypePointer Uniform %arr_ssbo
1389 %ssbo_pty = OpTypePointer Uniform %ssbo_s
1390 %rtarr_pty = OpTypePointer Uniform %rtarr
1391 %ptr_ty = OpTypePointer Uniform %float
1392 %var = OpVariable %var_ty Uniform
1393 %int_1 = OpConstant %int 1
1394 %int_2 = OpConstant %int 2
1395 %i = OpUndef %int
1396 %j = OpUndef %int
1397 %k = OpUndef %int
1398 ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
1399 ; CHECK-DAG: %int_0 = OpConstant %int 0
1400 ; CHECK-DAG: %int_9 = OpConstant %int 9
1401 ; CHECK-DAG: %int_3 = OpConstant %int 3
1402 ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
1403 ; CHECK: OpLabel
1404 ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_9
1405 ; CHECK: %ac_ssbo = )" << ac
1406 << R"( %ssbo_pty %var %[[clamp_i]]
1407 ; CHECK: %bb1 = OpLabel
1408 ; CHECK: %ac_rtarr = )"
1409 << ac << R"( %rtarr_pty %ac_ssbo %int_2
1410 ; CHECK: %bb2 = OpLabel
1411
1412 ; This is the interesting bit. This array length is needed for an OpAccessChain
1413 ; computing %ac, but the algorithm had to track back through %ac_rtarr's
1414 ; definition to find the base pointer %ac_ssbo.
1415 ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %ac_ssbo 2
1416 ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
1417 ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
1418 ; CHECK: %[[clamp_j:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %j %int_0 %[[smin]]
1419 ; CHECK: %[[clamp_k:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %k %int_0 %int_3
1420 ; CHECK: %ac = )" << ac
1421 << R"( %ptr_ty %ac_rtarr %[[clamp_j]] %int_1 %[[clamp_k]]
1422 ; CHECK-NOT: AccessChain
1423 )" << MainPrefix()
1424 << "%ac_ssbo = " << ac << " %ssbo_pty %var %i\n"
1425 << "OpBranch %bb1\n%bb1 = OpLabel\n"
1426 << "%ac_rtarr = " << ac << " %rtarr_pty %ac_ssbo %int_2\n"
1427 << "OpBranch %bb2\n%bb2 = OpLabel\n"
1428 << "%ac = " << ac << " %ptr_ty %ac_rtarr %j %int_1 %k\n"
1429
1430 << MainSuffix();
1431 SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
1432 }
1433 }
1434
1435 TEST_F(GraphicsRobustAccessTest, bug3813) {
1436 // This shader comes from Dawn's
1437 // TextureViewSamplingTest.TextureCubeMapOnWholeTexture, converted from GLSL
1438 // by glslang.
1439 // The pass was inserting a signed 32-bit int type, but not correctly marking
1440 // the shader as changed.
1441 std::string shader = R"(
1442 ; SPIR-V
1443 ; Version: 1.0
1444 ; Generator: Google Shaderc over Glslang; 10
1445 ; Bound: 46
1446 ; Schema: 0
1447 OpCapability Shader
1448 %1 = OpExtInstImport "GLSL.std.450"
1449 OpMemoryModel Logical GLSL450
1450 OpEntryPoint Fragment %4 "main" %12 %29
1451 OpExecutionMode %4 OriginUpperLeft
1452 OpSource GLSL 450
1453 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
1454 OpSourceExtension "GL_GOOGLE_include_directive"
1455 OpName %4 "main"
1456 OpName %8 "sc"
1457 OpName %12 "texCoord"
1458 OpName %21 "tc"
1459 OpName %29 "fragColor"
1460 OpName %32 "texture0"
1461 OpName %36 "sampler0"
1462 OpDecorate %12 Location 0
1463 OpDecorate %29 Location 0
1464 OpDecorate %32 DescriptorSet 0
1465 OpDecorate %32 Binding 1
1466 OpDecorate %36 DescriptorSet 0
1467 OpDecorate %36 Binding 0
1468 %2 = OpTypeVoid
1469 %3 = OpTypeFunction %2
1470 %6 = OpTypeFloat 32
1471 %7 = OpTypePointer Function %6
1472 %9 = OpConstant %6 2
1473 %10 = OpTypeVector %6 2
1474 %11 = OpTypePointer Input %10
1475 %12 = OpVariable %11 Input
1476 %13 = OpTypeInt 32 0
1477 %14 = OpConstant %13 0
1478 %15 = OpTypePointer Input %6
1479 %19 = OpConstant %6 1
1480 %22 = OpConstant %13 1
1481 %27 = OpTypeVector %6 4
1482 %28 = OpTypePointer Output %27
1483 %29 = OpVariable %28 Output
1484 %30 = OpTypeImage %6 Cube 0 0 0 1 Unknown
1485 %31 = OpTypePointer UniformConstant %30
1486 %32 = OpVariable %31 UniformConstant
1487 %34 = OpTypeSampler
1488 %35 = OpTypePointer UniformConstant %34
1489 %36 = OpVariable %35 UniformConstant
1490 %38 = OpTypeSampledImage %30
1491 %43 = OpTypeVector %6 3
1492 %4 = OpFunction %2 None %3
1493 %5 = OpLabel
1494 %8 = OpVariable %7 Function
1495 %21 = OpVariable %7 Function
1496 %16 = OpAccessChain %15 %12 %14
1497 %17 = OpLoad %6 %16
1498 %18 = OpFMul %6 %9 %17
1499 %20 = OpFSub %6 %18 %19
1500 OpStore %8 %20
1501 %23 = OpAccessChain %15 %12 %22
1502 %24 = OpLoad %6 %23
1503 %25 = OpFMul %6 %9 %24
1504 %26 = OpFSub %6 %25 %19
1505 OpStore %21 %26
1506 %33 = OpLoad %30 %32
1507 %37 = OpLoad %34 %36
1508 %39 = OpSampledImage %38 %33 %37
1509 %40 = OpLoad %6 %21
1510 %41 = OpLoad %6 %8
1511 %42 = OpFNegate %6 %41
1512 %44 = OpCompositeConstruct %43 %19 %40 %42
1513 %45 = OpImageSampleImplicitLod %27 %39 %44
1514 OpStore %29 %45
1515 OpReturn
1516 OpFunctionEnd
1517 )";
1518
1519 std::string expected = R"(OpCapability Shader
1520 %1 = OpExtInstImport "GLSL.std.450"
1521 OpMemoryModel Logical GLSL450
1522 OpEntryPoint Fragment %main "main" %texCoord %fragColor
1523 OpExecutionMode %main OriginUpperLeft
1524 OpSource GLSL 450
1525 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
1526 OpSourceExtension "GL_GOOGLE_include_directive"
1527 OpName %main "main"
1528 OpName %sc "sc"
1529 OpName %texCoord "texCoord"
1530 OpName %tc "tc"
1531 OpName %fragColor "fragColor"
1532 OpName %texture0 "texture0"
1533 OpName %sampler0 "sampler0"
1534 OpDecorate %texCoord Location 0
1535 OpDecorate %fragColor Location 0
1536 OpDecorate %texture0 DescriptorSet 0
1537 OpDecorate %texture0 Binding 1
1538 OpDecorate %sampler0 DescriptorSet 0
1539 OpDecorate %sampler0 Binding 0
1540 %void = OpTypeVoid
1541 %10 = OpTypeFunction %void
1542 %float = OpTypeFloat 32
1543 %_ptr_Function_float = OpTypePointer Function %float
1544 %float_2 = OpConstant %float 2
1545 %v2float = OpTypeVector %float 2
1546 %_ptr_Input_v2float = OpTypePointer Input %v2float
1547 %texCoord = OpVariable %_ptr_Input_v2float Input
1548 %uint = OpTypeInt 32 0
1549 %uint_0 = OpConstant %uint 0
1550 %_ptr_Input_float = OpTypePointer Input %float
1551 %float_1 = OpConstant %float 1
1552 %uint_1 = OpConstant %uint 1
1553 %v4float = OpTypeVector %float 4
1554 %_ptr_Output_v4float = OpTypePointer Output %v4float
1555 %fragColor = OpVariable %_ptr_Output_v4float Output
1556 %23 = OpTypeImage %float Cube 0 0 0 1 Unknown
1557 %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
1558 %texture0 = OpVariable %_ptr_UniformConstant_23 UniformConstant
1559 %25 = OpTypeSampler
1560 %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
1561 %sampler0 = OpVariable %_ptr_UniformConstant_25 UniformConstant
1562 %27 = OpTypeSampledImage %23
1563 %v3float = OpTypeVector %float 3
1564 %int = OpTypeInt 32 1
1565 %main = OpFunction %void None %10
1566 %29 = OpLabel
1567 %sc = OpVariable %_ptr_Function_float Function
1568 %tc = OpVariable %_ptr_Function_float Function
1569 %30 = OpAccessChain %_ptr_Input_float %texCoord %uint_0
1570 %31 = OpLoad %float %30
1571 %32 = OpFMul %float %float_2 %31
1572 %33 = OpFSub %float %32 %float_1
1573 OpStore %sc %33
1574 %34 = OpAccessChain %_ptr_Input_float %texCoord %uint_1
1575 %35 = OpLoad %float %34
1576 %36 = OpFMul %float %float_2 %35
1577 %37 = OpFSub %float %36 %float_1
1578 OpStore %tc %37
1579 %38 = OpLoad %23 %texture0
1580 %39 = OpLoad %25 %sampler0
1581 %40 = OpSampledImage %27 %38 %39
1582 %41 = OpLoad %float %tc
1583 %42 = OpLoad %float %sc
1584 %43 = OpFNegate %float %42
1585 %44 = OpCompositeConstruct %v3float %float_1 %41 %43
1586 %45 = OpImageSampleImplicitLod %v4float %40 %44
1587 OpStore %fragColor %45
1588 OpReturn
1589 OpFunctionEnd
1590 )";
1591
1592 SinglePassRunAndCheck<GraphicsRobustAccessPass>(shader, expected, false,
1593 true);
1594 }
1595
1596 TEST_F(GraphicsRobustAccessTest, ReplaceIndexReportsChanged) {
1597 // A ClusterFuzz generated shader that triggered a
1598 // "Binary size unexpectedly changed despite the optimizer saying there was no
1599 // change" assertion.
1600 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/4166.
1601 std::string shader = R"(
1602 ; SPIR-V
1603 ; Version: 1.0
1604 ; Generator: Google Shaderc over Glslang; 245
1605 ; Bound: 41
1606 ; Schema: 0
1607 OpCapability Shader
1608 %1 = OpExtInstImport "GLSL.std.450"
1609 OpMemoryModel Logical GLSL450
1610 OpEntryPoint GLCompute %main "else" %gl_GlobalInvocationID
1611 OpExecutionMode %main LocalSize 1 1 3338665985
1612 OpSource GLSL 450
1613 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
1614 OpSourceExtension "GL_GOOGLE_include_directive"
1615 OpName %main "main"
1616 OpName %index "index"
1617 OpName %gl_GlobalInvocationID "gl_GlobalInvocationID"
1618 OpName %S "S"
1619 OpMemberName %_struct_24 0 ""
1620 OpMemberName %_struct_24 1 ""
1621 OpName %Dst "Dst"
1622 OpMemberName %Dst 0 "s"
1623 OpName %dst "dst"
1624 OpName %Src "Src"
1625 OpMemberName %Src 0 "s"
1626 OpName %src "src"
1627 OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
1628 OpMemberDecorate %_struct_24 0 Offset 64
1629 OpMemberDecorate %_struct_24 1 Offset 8
1630 OpDecorate %_arr__struct_24_uint_1 ArrayStride 16
1631 OpMemberDecorate %Dst 0 Offset 0
1632 OpDecorate %Dst BufferBlock
1633 OpDecorate %dst DescriptorSet 0
1634 OpDecorate %dst Binding 1
1635 OpDecorate %_arr__struct_24_uint_1_0 ArrayStride 16
1636 OpMemberDecorate %Src 0 Offset 0
1637 OpDecorate %Src Block
1638 OpDecorate %src DescriptorSet 0
1639 OpDecorate %src Binding 0
1640 %void = OpTypeVoid
1641 %3 = OpTypeFunction %void
1642 %uint = OpTypeInt 32 0
1643 %_ptr_Function_uint = OpTypePointer Function %uint
1644 %v3uint = OpTypeVector %uint 3
1645 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
1646 %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
1647 %uint_4864 = OpConstant %uint 4864
1648 %_ptr_Input_uint = OpTypePointer Input %uint
1649 %uint_1 = OpConstant %uint 1
1650 %bool = OpTypeBool
1651 %v2uint = OpTypeVector %uint 2
1652 %_struct_24 = OpTypeStruct %_ptr_Input_uint %v2uint
1653 %_arr__struct_24_uint_1 = OpTypeArray %_struct_24 %uint_1
1654 %Dst = OpTypeStruct %_arr__struct_24_uint_1
1655 %_ptr_Uniform_Dst = OpTypePointer Uniform %Dst
1656 %dst = OpVariable %_ptr_Uniform_Dst Uniform
1657 %int = OpTypeInt 32 1
1658 %int_0 = OpConstant %int 0
1659 %_arr__struct_24_uint_1_0 = OpTypeArray %_struct_24 %uint_1
1660 %Src = OpTypeStruct %_arr__struct_24_uint_1_0
1661 %_ptr_Uniform_Src = OpTypePointer Uniform %Src
1662 %src = OpVariable %_ptr_Uniform_Src Uniform
1663 %_ptr_Uniform__struct_24 = OpTypePointer Uniform %_struct_24
1664 %main = OpFunction %void None %3
1665 %5 = OpLabel
1666 %index = OpVariable %_ptr_Function_uint Function
1667 %14 = OpAccessChain %_ptr_Input_uint %gl_GlobalInvocationID %uint_4864
1668 %15 = OpLoad %uint %14
1669 OpStore %index %15
1670 %16 = OpLoad %uint %index
1671 %S = OpUGreaterThanEqual %bool %16 %uint_1
1672 OpSelectionMerge %21 None
1673 OpBranchConditional %S %20 %21
1674 %20 = OpLabel
1675 OpReturn
1676 %21 = OpLabel
1677 %31 = OpLoad %uint %index
1678 %36 = OpLoad %uint %index
1679 %38 = OpAccessChain %_ptr_Uniform__struct_24 %src %int_0 %36
1680 %39 = OpLoad %_struct_24 %38
1681 %40 = OpAccessChain %_ptr_Uniform__struct_24 %dst %int_0 %31
1682 OpStore %40 %39
1683 OpReturn
1684 OpFunctionEnd
1685 )";
1686
1687 std::vector<uint32_t> optimized_bin;
1688 auto status = spvtools::opt::Pass::Status::Failure;
1689 std::tie(optimized_bin, status) =
1690 SinglePassRunToBinary<GraphicsRobustAccessPass>(shader, false);
1691 // Check whether the pass returns the correct modification indication.
1692 EXPECT_EQ(status, spvtools::opt::Pass::Status::SuccessWithChange);
1693 }
1694
1695 // TODO(dneto): Test access chain index wider than 64 bits?
1696 // TODO(dneto): Test struct access chain index wider than 64 bits?
1697 // TODO(dneto): OpImageTexelPointer
1698 // - all Dim types: 1D 2D Cube 3D Rect Buffer
1699 // - all Dim types that can be arrayed: 1D 2D 3D
1700 // - sample index: set to 0 if not multisampled
1701 // - Dim (2D, Cube Rect} with multisampling
1702 // -1 0 max excess
1703 // TODO(dneto): Test OpImageTexelPointer with coordinate component index other
1704 // than 32 bits.
1705
1706 } // namespace