Debian Codegraph @

test/val/val_logical_pointers_test.cpp

raw
1 // Copyright (c) 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string>
16 #include <vector>
17
18 #include "gmock/gmock.h"
19 #include "test/unit_spirv.h"
20 #include "test/val/val_fixtures.h"
21
22 namespace spvtools {
23 namespace val {
24 namespace {
25
26 using ::testing::Combine;
27 using ::testing::Eq;
28 using ::testing::HasSubstr;
29 using ::testing::Values;
30 using ::testing::ValuesIn;
31
32 using ValidateLogicalPointersTest = spvtest::ValidateBase<bool>;
33
34 enum class MatrixTrace : uint32_t {
35 kNotAMatrix,
36 kColumn,
37 kComponent,
38 };
39
40 const MatrixTrace traces[] = {MatrixTrace::kNotAMatrix, MatrixTrace::kColumn,
41 MatrixTrace::kComponent};
42
43 using MatrixTraceTypedTest = spvtest::ValidateBase<MatrixTrace>;
44
45 TEST_P(MatrixTraceTypedTest, PhiLoopOp1) {
46 const auto trace_type = GetParam();
47 std::string gep, type;
48 switch (trace_type) {
49 case MatrixTrace::kColumn:
50 type = "%ptr_wg_v2float";
51 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
52 break;
53 case MatrixTrace::kComponent:
54 type = "%ptr_wg_float";
55 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
56 break;
57 default:
58 type = "%ptr_wg_float";
59 gep = "OpAccessChain %ptr_wg_float %var %int_1";
60 break;
61 }
62
63 const std::string spirv = R"(
64 OpCapability Shader
65 OpCapability VariablePointers
66 OpMemoryModel Logical GLSL450
67 OpEntryPoint GLCompute %main "main"
68 OpExecutionMode %main LocalSize 1 1 1
69 %void = OpTypeVoid
70 %bool = OpTypeBool
71 %bool_cond = OpUndef %bool
72 %int = OpTypeInt 32 0
73 %int_0 = OpConstant %int 0
74 %int_1 = OpConstant %int 1
75 %int_2 = OpConstant %int 2
76 %index = OpUndef %int
77 %float = OpTypeFloat 32
78 %v2float = OpTypeVector %float 2
79 %mat2x2 = OpTypeMatrix %v2float 2
80 %array = OpTypeArray %mat2x2 %int_2
81 %struct = OpTypeStruct %array %float
82 %ptr_wg_struct = OpTypePointer Workgroup %struct
83 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
84 %ptr_wg_float = OpTypePointer Workgroup %float
85 %null = OpConstantNull )" + type +
86 R"(
87 %var = OpVariable %ptr_wg_struct Workgroup
88 %void_fn = OpTypeFunction %void
89 %main = OpFunction %void None %void_fn
90 %entry = OpLabel
91 %gep = )" + gep + R"(
92 OpBranch %loop
93 %loop = OpLabel
94 %phi = OpPhi )" + type + R"( %gep %entry %copy %continue
95 OpLoopMerge %merge %continue None
96 OpBranchConditional %bool_cond %merge %continue
97 %continue = OpLabel
98 %copy = OpCopyObject )" + type +
99 R"( %phi
100 OpBranch %loop
101 %merge = OpLabel
102 OpReturn
103 OpFunctionEnd
104 )";
105
106 const auto expected = trace_type == MatrixTrace::kNotAMatrix
107 ? SPV_SUCCESS
108 : SPV_ERROR_INVALID_DATA;
109 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
110 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
111 if (expected) {
112 EXPECT_THAT(getDiagnosticString(),
113 HasSubstr("Variable pointer must not point to a column or a "
114 "component of a column of a matrix"));
115 }
116 }
117
118 TEST_P(MatrixTraceTypedTest, PhiLoopOp2) {
119 const auto trace_type = GetParam();
120 std::string gep, type;
121 switch (trace_type) {
122 case MatrixTrace::kColumn:
123 type = "%ptr_wg_v2float";
124 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
125 break;
126 case MatrixTrace::kComponent:
127 type = "%ptr_wg_float";
128 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
129 break;
130 default:
131 type = "%ptr_wg_float";
132 gep = "OpAccessChain %ptr_wg_float %var %int_1";
133 break;
134 }
135
136 const std::string spirv = R"(
137 OpCapability Shader
138 OpCapability VariablePointers
139 OpMemoryModel Logical GLSL450
140 OpEntryPoint GLCompute %main "main"
141 OpExecutionMode %main LocalSize 1 1 1
142 %void = OpTypeVoid
143 %bool = OpTypeBool
144 %bool_cond = OpUndef %bool
145 %int = OpTypeInt 32 0
146 %int_0 = OpConstant %int 0
147 %int_1 = OpConstant %int 1
148 %int_2 = OpConstant %int 2
149 %index = OpUndef %int
150 %float = OpTypeFloat 32
151 %v2float = OpTypeVector %float 2
152 %mat2x2 = OpTypeMatrix %v2float 2
153 %array = OpTypeArray %mat2x2 %int_2
154 %struct = OpTypeStruct %array %float
155 %ptr_wg_struct = OpTypePointer Workgroup %struct
156 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
157 %ptr_wg_float = OpTypePointer Workgroup %float
158 %null = OpConstantNull )" + type +
159 R"(
160 %var = OpVariable %ptr_wg_struct Workgroup
161 %void_fn = OpTypeFunction %void
162 %main = OpFunction %void None %void_fn
163 %entry = OpLabel
164 %gep = )" + gep + R"(
165 OpBranch %loop
166 %loop = OpLabel
167 %phi = OpPhi )" + type + R"( %copy %continue %gep %entry
168 OpLoopMerge %merge %continue None
169 OpBranchConditional %bool_cond %merge %continue
170 %continue = OpLabel
171 %copy = OpCopyObject )" + type +
172 R"( %phi
173 OpBranch %loop
174 %merge = OpLabel
175 OpReturn
176 OpFunctionEnd
177 )";
178
179 const auto expected = trace_type == MatrixTrace::kNotAMatrix
180 ? SPV_SUCCESS
181 : SPV_ERROR_INVALID_DATA;
182 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
183 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
184 if (expected) {
185 EXPECT_THAT(getDiagnosticString(),
186 HasSubstr("Variable pointer must not point to a column or a "
187 "component of a column of a matrix"));
188 }
189 }
190
191 TEST_P(MatrixTraceTypedTest, SelectOp1) {
192 const auto trace_type = GetParam();
193 std::string gep, type;
194 switch (trace_type) {
195 case MatrixTrace::kColumn:
196 type = "%ptr_wg_v2float";
197 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
198 break;
199 case MatrixTrace::kComponent:
200 type = "%ptr_wg_float";
201 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
202 break;
203 default:
204 type = "%ptr_wg_float";
205 gep = "OpAccessChain %ptr_wg_float %var %int_1";
206 break;
207 }
208
209 const std::string spirv = R"(
210 OpCapability Shader
211 OpCapability VariablePointers
212 OpMemoryModel Logical GLSL450
213 OpEntryPoint GLCompute %main "main"
214 OpExecutionMode %main LocalSize 1 1 1
215 %void = OpTypeVoid
216 %bool = OpTypeBool
217 %bool_cond = OpUndef %bool
218 %int = OpTypeInt 32 0
219 %int_0 = OpConstant %int 0
220 %int_1 = OpConstant %int 1
221 %int_2 = OpConstant %int 2
222 %index = OpUndef %int
223 %float = OpTypeFloat 32
224 %v2float = OpTypeVector %float 2
225 %mat2x2 = OpTypeMatrix %v2float 2
226 %array = OpTypeArray %mat2x2 %int_2
227 %struct = OpTypeStruct %array %float
228 %ptr_wg_struct = OpTypePointer Workgroup %struct
229 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
230 %ptr_wg_float = OpTypePointer Workgroup %float
231 %null = OpConstantNull )" + type +
232 R"(
233 %var = OpVariable %ptr_wg_struct Workgroup
234 %void_fn = OpTypeFunction %void
235 %main = OpFunction %void None %void_fn
236 %entry = OpLabel
237 %gep = )" + gep + R"(
238 %copy = OpCopyObject )" + type +
239 R"( %gep
240 %sel = OpSelect )" + type + R"( %bool_cond %copy %null
241 OpReturn
242 OpFunctionEnd
243 )";
244
245 const auto expected = trace_type == MatrixTrace::kNotAMatrix
246 ? SPV_SUCCESS
247 : SPV_ERROR_INVALID_DATA;
248 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
249 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
250 if (expected) {
251 EXPECT_THAT(getDiagnosticString(),
252 HasSubstr("Variable pointer must not point to a column or a "
253 "component of a column of a matrix"));
254 }
255 }
256
257 TEST_P(MatrixTraceTypedTest, SelectOp2) {
258 const auto trace_type = GetParam();
259 std::string gep, type;
260 switch (trace_type) {
261 case MatrixTrace::kColumn:
262 type = "%ptr_wg_v2float";
263 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
264 break;
265 case MatrixTrace::kComponent:
266 type = "%ptr_wg_float";
267 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
268 break;
269 default:
270 type = "%ptr_wg_float";
271 gep = "OpAccessChain %ptr_wg_float %var %int_1";
272 break;
273 }
274
275 const std::string spirv = R"(
276 OpCapability Shader
277 OpCapability VariablePointers
278 OpMemoryModel Logical GLSL450
279 OpEntryPoint GLCompute %main "main"
280 OpExecutionMode %main LocalSize 1 1 1
281 %void = OpTypeVoid
282 %bool = OpTypeBool
283 %bool_cond = OpUndef %bool
284 %int = OpTypeInt 32 0
285 %int_0 = OpConstant %int 0
286 %int_1 = OpConstant %int 1
287 %int_2 = OpConstant %int 2
288 %index = OpUndef %int
289 %float = OpTypeFloat 32
290 %v2float = OpTypeVector %float 2
291 %mat2x2 = OpTypeMatrix %v2float 2
292 %array = OpTypeArray %mat2x2 %int_2
293 %struct = OpTypeStruct %array %float
294 %ptr_wg_struct = OpTypePointer Workgroup %struct
295 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
296 %ptr_wg_float = OpTypePointer Workgroup %float
297 %null = OpConstantNull )" + type +
298 R"(
299 %var = OpVariable %ptr_wg_struct Workgroup
300 %void_fn = OpTypeFunction %void
301 %main = OpFunction %void None %void_fn
302 %entry = OpLabel
303 %gep = )" + gep + R"(
304 %copy = OpCopyObject )" + type +
305 R"( %gep
306 %sel = OpSelect )" + type + R"( %bool_cond %null %copy
307 OpReturn
308 OpFunctionEnd
309 )";
310
311 const auto expected = trace_type == MatrixTrace::kNotAMatrix
312 ? SPV_SUCCESS
313 : SPV_ERROR_INVALID_DATA;
314 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
315 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
316 if (expected) {
317 EXPECT_THAT(getDiagnosticString(),
318 HasSubstr("Variable pointer must not point to a column or a "
319 "component of a column of a matrix"));
320 }
321 }
322
323 TEST_P(MatrixTraceTypedTest, FunctionVariable) {
324 const auto trace_type = GetParam();
325 std::string gep, type, ld_type;
326 switch (trace_type) {
327 case MatrixTrace::kColumn:
328 type = "%ptr_func_wg_v2float";
329 ld_type = "%ptr_wg_v2float";
330 gep = "OpAccessChain %ptr_wg_v2float %var_mat2x2 %index";
331 break;
332 case MatrixTrace::kComponent:
333 type = "%ptr_func_wg_float";
334 ld_type = "%ptr_wg_float";
335 gep = "OpAccessChain %ptr_wg_float %var_mat2x2 %index %index";
336 break;
337 default:
338 type = "%ptr_func_wg_float";
339 ld_type = "%ptr_wg_float";
340 gep = "OpAccessChain %ptr_wg_float %var_float";
341 break;
342 }
343
344 const std::string spirv = R"(
345 OpCapability Shader
346 OpCapability VariablePointers
347 OpMemoryModel Logical GLSL450
348 OpEntryPoint GLCompute %main "main"
349 OpExecutionMode %main LocalSize 1 1 1
350 %void = OpTypeVoid
351 %bool = OpTypeBool
352 %bool_cond = OpUndef %bool
353 %int = OpTypeInt 32 0
354 %int_0 = OpConstant %int 0
355 %int_1 = OpConstant %int 1
356 %int_2 = OpConstant %int 2
357 %index = OpUndef %int
358 %float = OpTypeFloat 32
359 %v2float = OpTypeVector %float 2
360 %mat2x2 = OpTypeMatrix %v2float 2
361 %array = OpTypeArray %mat2x2 %int_2
362 %struct = OpTypeStruct %array %float
363 %ptr_wg_float = OpTypePointer Workgroup %float
364 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
365 %ptr_wg_mat2x2 = OpTypePointer Workgroup %mat2x2
366 %ptr_func_wg_float = OpTypePointer Function %ptr_wg_float
367 %ptr_func_wg_v2float = OpTypePointer Function %ptr_wg_v2float
368 %var_mat2x2 = OpVariable %ptr_wg_mat2x2 Workgroup
369 %var_float = OpVariable %ptr_wg_float Workgroup
370 %void_fn = OpTypeFunction %void
371 %main = OpFunction %void None %void_fn
372 %entry = OpLabel
373 %func_var = OpVariable )" + type +
374 R"( Function
375 %gep = )" + gep + R"(
376 OpStore %func_var %gep
377 %ld = OpLoad )" + ld_type + R"( %func_var
378 OpReturn
379 OpFunctionEnd
380 )";
381
382 const auto expected = trace_type == MatrixTrace::kNotAMatrix
383 ? SPV_SUCCESS
384 : SPV_ERROR_INVALID_DATA;
385 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
386 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
387 if (expected) {
388 EXPECT_THAT(getDiagnosticString(),
389 HasSubstr("Variable pointer must not point to a column or a "
390 "component of a column of a matrix"));
391 }
392 }
393
394 TEST_P(MatrixTraceTypedTest, PrivateVariable) {
395 const auto trace_type = GetParam();
396 std::string gep, type, ld_type;
397 switch (trace_type) {
398 case MatrixTrace::kColumn:
399 type = "%ptr_priv_wg_v2float";
400 ld_type = "%ptr_wg_v2float";
401 gep = "OpAccessChain %ptr_wg_v2float %var_mat2x2 %index";
402 break;
403 case MatrixTrace::kComponent:
404 type = "%ptr_priv_wg_float";
405 ld_type = "%ptr_wg_float";
406 gep = "OpAccessChain %ptr_wg_float %var_mat2x2 %index %index";
407 break;
408 default:
409 type = "%ptr_priv_wg_float";
410 ld_type = "%ptr_wg_float";
411 gep = "OpAccessChain %ptr_wg_float %var_float";
412 break;
413 }
414
415 const std::string spirv = R"(
416 OpCapability Shader
417 OpCapability VariablePointers
418 OpMemoryModel Logical GLSL450
419 OpEntryPoint GLCompute %main "main"
420 OpExecutionMode %main LocalSize 1 1 1
421 %void = OpTypeVoid
422 %bool = OpTypeBool
423 %bool_cond = OpUndef %bool
424 %int = OpTypeInt 32 0
425 %int_0 = OpConstant %int 0
426 %int_1 = OpConstant %int 1
427 %int_2 = OpConstant %int 2
428 %index = OpUndef %int
429 %float = OpTypeFloat 32
430 %v2float = OpTypeVector %float 2
431 %mat2x2 = OpTypeMatrix %v2float 2
432 %array = OpTypeArray %mat2x2 %int_2
433 %struct = OpTypeStruct %array %float
434 %ptr_wg_float = OpTypePointer Workgroup %float
435 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
436 %ptr_wg_mat2x2 = OpTypePointer Workgroup %mat2x2
437 %ptr_priv_wg_float = OpTypePointer Private %ptr_wg_float
438 %ptr_priv_wg_v2float = OpTypePointer Private %ptr_wg_v2float
439 %var_mat2x2 = OpVariable %ptr_wg_mat2x2 Workgroup
440 %var_float = OpVariable %ptr_wg_float Workgroup
441 %priv_var = OpVariable )" + type +
442 R"( Private
443 %void_fn = OpTypeFunction %void
444 %main = OpFunction %void None %void_fn
445 %entry = OpLabel
446 %gep = )" + gep + R"(
447 OpStore %priv_var %gep
448 %ld = OpLoad )" + ld_type + R"( %priv_var
449 OpReturn
450 OpFunctionEnd
451 )";
452
453 const auto expected = trace_type == MatrixTrace::kNotAMatrix
454 ? SPV_SUCCESS
455 : SPV_ERROR_INVALID_DATA;
456 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
457 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
458 if (expected) {
459 EXPECT_THAT(getDiagnosticString(),
460 HasSubstr("Variable pointer must not point to a column or a "
461 "component of a column of a matrix"));
462 }
463 }
464
465 TEST_P(MatrixTraceTypedTest, FunctionVariableAggregate) {
466 const auto trace_type = GetParam();
467 std::string gep, var_type, gep_type, ld_type;
468 switch (trace_type) {
469 case MatrixTrace::kColumn:
470 var_type = "%ptr_func_wg_struct_v2float";
471 gep_type = "%ptr_func_wg_v2float";
472 ld_type = "%ptr_wg_v2float";
473 gep = "OpAccessChain %ptr_wg_v2float %var_mat2x2 %index";
474 break;
475 case MatrixTrace::kComponent:
476 var_type = "%ptr_func_wg_struct_float";
477 gep_type = "%ptr_func_wg_float";
478 ld_type = "%ptr_wg_float";
479 gep = "OpAccessChain %ptr_wg_float %var_mat2x2 %index %index";
480 break;
481 default:
482 var_type = "%ptr_func_wg_struct_float";
483 gep_type = "%ptr_func_wg_float";
484 ld_type = "%ptr_wg_float";
485 gep = "OpAccessChain %ptr_wg_float %var_float";
486 break;
487 }
488
489 const std::string spirv = R"(
490 OpCapability Shader
491 OpCapability VariablePointers
492 OpMemoryModel Logical GLSL450
493 OpEntryPoint GLCompute %main "main"
494 OpExecutionMode %main LocalSize 1 1 1
495 %void = OpTypeVoid
496 %bool = OpTypeBool
497 %bool_cond = OpUndef %bool
498 %int = OpTypeInt 32 0
499 %int_0 = OpConstant %int 0
500 %int_1 = OpConstant %int 1
501 %int_2 = OpConstant %int 2
502 %index = OpUndef %int
503 %float = OpTypeFloat 32
504 %v2float = OpTypeVector %float 2
505 %mat2x2 = OpTypeMatrix %v2float 2
506 %array = OpTypeArray %mat2x2 %int_2
507 %struct = OpTypeStruct %array %float
508 %ptr_wg_float = OpTypePointer Workgroup %float
509 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
510 %ptr_wg_mat2x2 = OpTypePointer Workgroup %mat2x2
511 %func_array_float = OpTypeArray %ptr_wg_float %int_1
512 %func_array_v2float = OpTypeArray %ptr_wg_v2float %int_1
513 %func_struct_float = OpTypeStruct %func_array_float
514 %func_struct_v2float = OpTypeStruct %func_array_v2float
515 %ptr_func_wg_struct_float = OpTypePointer Function %func_struct_float
516 %ptr_func_wg_struct_v2float = OpTypePointer Function %func_struct_v2float
517 %ptr_func_wg_float = OpTypePointer Function %ptr_wg_float
518 %ptr_func_wg_v2float = OpTypePointer Function %ptr_wg_v2float
519 %var_mat2x2 = OpVariable %ptr_wg_mat2x2 Workgroup
520 %var_float = OpVariable %ptr_wg_float Workgroup
521 %void_fn = OpTypeFunction %void
522 %main = OpFunction %void None %void_fn
523 %entry = OpLabel
524 %func_var = OpVariable )" + var_type +
525 R"( Function
526 %gep = )" + gep + R"(
527 %store_gep = OpAccessChain )" +
528 gep_type + R"( %func_var %int_0 %index
529 %store_gep_copy = OpCopyObject )" +
530 gep_type + R"( %store_gep
531 OpStore %store_gep_copy %gep
532 %ld_gep = OpAccessChain )" + gep_type +
533 R"( %func_var %int_0 %index
534 %ld = OpLoad )" + ld_type + R"( %ld_gep
535 OpReturn
536 OpFunctionEnd
537 )";
538
539 const auto expected = trace_type == MatrixTrace::kNotAMatrix
540 ? SPV_SUCCESS
541 : SPV_ERROR_INVALID_DATA;
542 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
543 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
544 if (expected) {
545 EXPECT_THAT(getDiagnosticString(),
546 HasSubstr("Variable pointer must not point to a column or a "
547 "component of a column of a matrix"));
548 }
549 }
550
551 TEST_P(MatrixTraceTypedTest, FunctionCallParam1) {
552 const auto trace_type = GetParam();
553 std::string gep, type;
554 switch (trace_type) {
555 case MatrixTrace::kColumn:
556 type = "%ptr_wg_v2float";
557 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
558 break;
559 case MatrixTrace::kComponent:
560 type = "%ptr_wg_float";
561 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
562 break;
563 default:
564 type = "%ptr_wg_float";
565 gep = "OpAccessChain %ptr_wg_float %var %int_1";
566 break;
567 }
568
569 const std::string spirv = R"(
570 OpCapability Shader
571 OpCapability VariablePointers
572 OpMemoryModel Logical GLSL450
573 OpEntryPoint GLCompute %main "main"
574 OpExecutionMode %main LocalSize 1 1 1
575 %void = OpTypeVoid
576 %bool = OpTypeBool
577 %bool_cond = OpUndef %bool
578 %int = OpTypeInt 32 0
579 %int_0 = OpConstant %int 0
580 %int_1 = OpConstant %int 1
581 %int_2 = OpConstant %int 2
582 %index = OpUndef %int
583 %float = OpTypeFloat 32
584 %v2float = OpTypeVector %float 2
585 %mat2x2 = OpTypeMatrix %v2float 2
586 %array = OpTypeArray %mat2x2 %int_2
587 %struct = OpTypeStruct %array %float
588 %ptr_wg_struct = OpTypePointer Workgroup %struct
589 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
590 %ptr_wg_float = OpTypePointer Workgroup %float
591 %null = OpConstantNull )" + type +
592 R"(
593 %var = OpVariable %ptr_wg_struct Workgroup
594 %void_fn = OpTypeFunction %void
595 %foo_fn = OpTypeFunction %void )" +
596 type + R"( %bool
597 %main = OpFunction %void None %void_fn
598 %entry = OpLabel
599 %gep = )" + gep + R"(
600 %copy = OpCopyObject )" + type +
601 R"( %gep
602 %call = OpFunctionCall %void %foo %copy %bool_cond
603 OpReturn
604 OpFunctionEnd
605 %foo = OpFunction %void None %foo_fn
606 %ptr_param = OpFunctionParameter )" +
607 type + R"(
608 %bool_param = OpFunctionParameter %bool
609 %foo_entry = OpLabel
610 %sel = OpSelect )" + type + R"( %bool_param %ptr_param %null
611 OpReturn
612 OpFunctionEnd
613 )";
614
615 const auto expected = trace_type == MatrixTrace::kNotAMatrix
616 ? SPV_SUCCESS
617 : SPV_ERROR_INVALID_DATA;
618 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
619 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
620 if (expected) {
621 EXPECT_THAT(getDiagnosticString(),
622 HasSubstr("Variable pointer must not point to a column or a "
623 "component of a column of a matrix"));
624 }
625 }
626
627 TEST_P(MatrixTraceTypedTest, FunctionCallParam2) {
628 const auto trace_type = GetParam();
629 std::string gep, type;
630 switch (trace_type) {
631 case MatrixTrace::kColumn:
632 type = "%ptr_wg_v2float";
633 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
634 break;
635 case MatrixTrace::kComponent:
636 type = "%ptr_wg_float";
637 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
638 break;
639 default:
640 type = "%ptr_wg_float";
641 gep = "OpAccessChain %ptr_wg_float %var %int_1";
642 break;
643 }
644
645 const std::string spirv = R"(
646 OpCapability Shader
647 OpCapability VariablePointers
648 OpMemoryModel Logical GLSL450
649 OpEntryPoint GLCompute %main "main"
650 OpExecutionMode %main LocalSize 1 1 1
651 %void = OpTypeVoid
652 %bool = OpTypeBool
653 %bool_cond = OpUndef %bool
654 %int = OpTypeInt 32 0
655 %int_0 = OpConstant %int 0
656 %int_1 = OpConstant %int 1
657 %int_2 = OpConstant %int 2
658 %index = OpUndef %int
659 %float = OpTypeFloat 32
660 %v2float = OpTypeVector %float 2
661 %mat2x2 = OpTypeMatrix %v2float 2
662 %array = OpTypeArray %mat2x2 %int_2
663 %struct = OpTypeStruct %array %float
664 %ptr_wg_struct = OpTypePointer Workgroup %struct
665 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
666 %ptr_wg_float = OpTypePointer Workgroup %float
667 %null = OpConstantNull )" + type +
668 R"(
669 %var = OpVariable %ptr_wg_struct Workgroup
670 %void_fn = OpTypeFunction %void
671 %foo_fn = OpTypeFunction %void %bool )" +
672 type + R"(
673 %main = OpFunction %void None %void_fn
674 %entry = OpLabel
675 %gep = )" + gep + R"(
676 %copy = OpCopyObject )" + type +
677 R"( %gep
678 %call = OpFunctionCall %void %foo %bool_cond %copy
679 OpReturn
680 OpFunctionEnd
681 %foo = OpFunction %void None %foo_fn
682 %bool_param = OpFunctionParameter %bool
683 %ptr_param = OpFunctionParameter )" +
684 type + R"(
685 %foo_entry = OpLabel
686 %sel = OpSelect )" + type + R"( %bool_param %ptr_param %null
687 OpReturn
688 OpFunctionEnd
689 )";
690
691 const auto expected = trace_type == MatrixTrace::kNotAMatrix
692 ? SPV_SUCCESS
693 : SPV_ERROR_INVALID_DATA;
694 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
695 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
696 if (expected) {
697 EXPECT_THAT(getDiagnosticString(),
698 HasSubstr("Variable pointer must not point to a column or a "
699 "component of a column of a matrix"));
700 }
701 }
702
703 TEST_P(MatrixTraceTypedTest, FunctionCall) {
704 const auto trace_type = GetParam();
705 std::string gep, type;
706 switch (trace_type) {
707 case MatrixTrace::kColumn:
708 type = "%ptr_wg_v2float";
709 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
710 break;
711 case MatrixTrace::kComponent:
712 type = "%ptr_wg_float";
713 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
714 break;
715 default:
716 type = "%ptr_wg_float";
717 gep = "OpAccessChain %ptr_wg_float %var %int_1";
718 break;
719 }
720
721 const std::string spirv = R"(
722 OpCapability Shader
723 OpCapability VariablePointers
724 OpMemoryModel Logical GLSL450
725 OpEntryPoint GLCompute %main "main"
726 OpExecutionMode %main LocalSize 1 1 1
727 %void = OpTypeVoid
728 %bool = OpTypeBool
729 %bool_cond = OpUndef %bool
730 %int = OpTypeInt 32 0
731 %int_0 = OpConstant %int 0
732 %int_1 = OpConstant %int 1
733 %int_2 = OpConstant %int 2
734 %index = OpUndef %int
735 %float = OpTypeFloat 32
736 %v2float = OpTypeVector %float 2
737 %mat2x2 = OpTypeMatrix %v2float 2
738 %array = OpTypeArray %mat2x2 %int_2
739 %struct = OpTypeStruct %array %float
740 %ptr_wg_struct = OpTypePointer Workgroup %struct
741 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
742 %ptr_wg_float = OpTypePointer Workgroup %float
743 %null = OpConstantNull )" + type +
744 R"(
745 %var = OpVariable %ptr_wg_struct Workgroup
746 %void_fn = OpTypeFunction %void
747 %foo_ty = OpTypeFunction )" +
748 type + R"(
749 %main = OpFunction %void None %void_fn
750 %entry = OpLabel
751 %call = OpFunctionCall )" + type +
752 R"( %foo
753 OpReturn
754 OpFunctionEnd
755 %foo = OpFunction )" + type +
756 R"( None %foo_ty
757 %foo_entry = OpLabel
758 %gep = )" + gep + R"(
759 OpReturnValue %gep
760 OpFunctionEnd
761 )";
762
763 const auto expected = trace_type == MatrixTrace::kNotAMatrix
764 ? SPV_SUCCESS
765 : SPV_ERROR_INVALID_DATA;
766 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
767 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
768 if (expected) {
769 EXPECT_THAT(getDiagnosticString(),
770 HasSubstr("Variable pointer must not point to a column or a "
771 "component of a column of a matrix"));
772 }
773 }
774
775 TEST_P(MatrixTraceTypedTest, FunctionCallMultiReturn1) {
776 const auto trace_type = GetParam();
777 std::string gep, type;
778 switch (trace_type) {
779 case MatrixTrace::kColumn:
780 type = "%ptr_wg_v2float";
781 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
782 break;
783 case MatrixTrace::kComponent:
784 type = "%ptr_wg_float";
785 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
786 break;
787 default:
788 type = "%ptr_wg_float";
789 gep = "OpAccessChain %ptr_wg_float %var %int_1";
790 break;
791 }
792
793 const std::string spirv = R"(
794 OpCapability Shader
795 OpCapability VariablePointers
796 OpMemoryModel Logical GLSL450
797 OpEntryPoint GLCompute %main "main"
798 OpExecutionMode %main LocalSize 1 1 1
799 %void = OpTypeVoid
800 %bool = OpTypeBool
801 %bool_cond = OpUndef %bool
802 %int = OpTypeInt 32 0
803 %int_0 = OpConstant %int 0
804 %int_1 = OpConstant %int 1
805 %int_2 = OpConstant %int 2
806 %index = OpUndef %int
807 %float = OpTypeFloat 32
808 %v2float = OpTypeVector %float 2
809 %mat2x2 = OpTypeMatrix %v2float 2
810 %array = OpTypeArray %mat2x2 %int_2
811 %struct = OpTypeStruct %array %float
812 %ptr_wg_struct = OpTypePointer Workgroup %struct
813 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
814 %ptr_wg_float = OpTypePointer Workgroup %float
815 %null = OpConstantNull )" + type +
816 R"(
817 %var = OpVariable %ptr_wg_struct Workgroup
818 %void_fn = OpTypeFunction %void
819 %foo_ty = OpTypeFunction )" +
820 type + R"(
821 %main = OpFunction %void None %void_fn
822 %entry = OpLabel
823 %call = OpFunctionCall )" + type +
824 R"( %foo
825 OpReturn
826 OpFunctionEnd
827 %foo = OpFunction )" + type +
828 R"( None %foo_ty
829 %foo_entry = OpLabel
830 %gep = )" + gep + R"(
831 OpSelectionMerge %merge None
832 OpBranchConditional %bool_cond %then %merge
833 %then = OpLabel
834 OpReturnValue %gep
835 %merge = OpLabel
836 OpReturnValue %null
837 OpFunctionEnd
838 )";
839
840 const auto expected = trace_type == MatrixTrace::kNotAMatrix
841 ? SPV_SUCCESS
842 : SPV_ERROR_INVALID_DATA;
843 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
844 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
845 if (expected) {
846 EXPECT_THAT(getDiagnosticString(),
847 HasSubstr("Variable pointer must not point to a column or a "
848 "component of a column of a matrix"));
849 }
850 }
851
852 TEST_P(MatrixTraceTypedTest, FunctionCallMultiReturn2) {
853 const auto trace_type = GetParam();
854 std::string gep, type;
855 switch (trace_type) {
856 case MatrixTrace::kColumn:
857 type = "%ptr_wg_v2float";
858 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
859 break;
860 case MatrixTrace::kComponent:
861 type = "%ptr_wg_float";
862 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
863 break;
864 default:
865 type = "%ptr_wg_float";
866 gep = "OpAccessChain %ptr_wg_float %var %int_1";
867 break;
868 }
869
870 const std::string spirv = R"(
871 OpCapability Shader
872 OpCapability VariablePointers
873 OpMemoryModel Logical GLSL450
874 OpEntryPoint GLCompute %main "main"
875 OpExecutionMode %main LocalSize 1 1 1
876 %void = OpTypeVoid
877 %bool = OpTypeBool
878 %bool_cond = OpUndef %bool
879 %int = OpTypeInt 32 0
880 %int_0 = OpConstant %int 0
881 %int_1 = OpConstant %int 1
882 %int_2 = OpConstant %int 2
883 %index = OpUndef %int
884 %float = OpTypeFloat 32
885 %v2float = OpTypeVector %float 2
886 %mat2x2 = OpTypeMatrix %v2float 2
887 %array = OpTypeArray %mat2x2 %int_2
888 %struct = OpTypeStruct %array %float
889 %ptr_wg_struct = OpTypePointer Workgroup %struct
890 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
891 %ptr_wg_float = OpTypePointer Workgroup %float
892 %null = OpConstantNull )" + type +
893 R"(
894 %var = OpVariable %ptr_wg_struct Workgroup
895 %void_fn = OpTypeFunction %void
896 %foo_ty = OpTypeFunction )" +
897 type + R"(
898 %main = OpFunction %void None %void_fn
899 %entry = OpLabel
900 %call = OpFunctionCall )" + type +
901 R"( %foo
902 OpReturn
903 OpFunctionEnd
904 %foo = OpFunction )" + type +
905 R"( None %foo_ty
906 %foo_entry = OpLabel
907 %gep = )" + gep + R"(
908 OpSelectionMerge %merge None
909 OpBranchConditional %bool_cond %then %merge
910 %then = OpLabel
911 OpReturnValue %null
912 %merge = OpLabel
913 OpReturnValue %gep
914 OpFunctionEnd
915 )";
916
917 const auto expected = trace_type == MatrixTrace::kNotAMatrix
918 ? SPV_SUCCESS
919 : SPV_ERROR_INVALID_DATA;
920 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
921 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
922 if (expected) {
923 EXPECT_THAT(getDiagnosticString(),
924 HasSubstr("Variable pointer must not point to a column or a "
925 "component of a column of a matrix"));
926 }
927 }
928
929 INSTANTIATE_TEST_SUITE_P(ValidateLogicalPointersMatrixTraceTyped,
930 MatrixTraceTypedTest, ValuesIn(traces));
931
932 using MatrixTraceUntypedTest = spvtest::ValidateBase<MatrixTrace>;
933
934 TEST_P(MatrixTraceUntypedTest, PhiLoopOp1) {
935 const auto trace_type = GetParam();
936 std::string gep;
937 switch (trace_type) {
938 case MatrixTrace::kColumn:
939 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
940 break;
941 case MatrixTrace::kComponent:
942 gep =
943 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
944 "%index";
945 break;
946 default:
947 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
948 break;
949 }
950
951 const std::string spirv = R"(
952 OpCapability Shader
953 OpCapability VariablePointers
954 OpCapability UntypedPointersKHR
955 OpExtension "SPV_KHR_untyped_pointers"
956 OpMemoryModel Logical GLSL450
957 OpEntryPoint GLCompute %main "main"
958 OpExecutionMode %main LocalSize 1 1 1
959 %void = OpTypeVoid
960 %bool = OpTypeBool
961 %bool_cond = OpUndef %bool
962 %int = OpTypeInt 32 0
963 %int_0 = OpConstant %int 0
964 %int_1 = OpConstant %int 1
965 %int_2 = OpConstant %int 2
966 %index = OpUndef %int
967 %float = OpTypeFloat 32
968 %v2float = OpTypeVector %float 2
969 %mat2x2 = OpTypeMatrix %v2float 2
970 %array = OpTypeArray %mat2x2 %int_2
971 %struct = OpTypeStruct %array %float
972 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
973 %null = OpConstantNull %ptr_wg
974 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
975 %void_fn = OpTypeFunction %void
976 %main = OpFunction %void None %void_fn
977 %entry = OpLabel
978 %gep = )" + gep + R"(
979 OpBranch %loop
980 %loop = OpLabel
981 %phi = OpPhi %ptr_wg %gep %entry %copy %continue
982 OpLoopMerge %merge %continue None
983 OpBranchConditional %bool_cond %merge %continue
984 %continue = OpLabel
985 %copy = OpCopyObject %ptr_wg %phi
986 OpBranch %loop
987 %merge = OpLabel
988 OpReturn
989 OpFunctionEnd
990 )";
991
992 const auto expected = trace_type == MatrixTrace::kNotAMatrix
993 ? SPV_SUCCESS
994 : SPV_ERROR_INVALID_DATA;
995 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
996 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
997 if (expected) {
998 EXPECT_THAT(getDiagnosticString(),
999 HasSubstr("Variable pointer must not point to a column or a "
1000 "component of a column of a matrix"));
1001 }
1002 }
1003 TEST_P(MatrixTraceUntypedTest, PhiLoopOp2) {
1004 const auto trace_type = GetParam();
1005 std::string gep;
1006 switch (trace_type) {
1007 case MatrixTrace::kColumn:
1008 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1009 break;
1010 case MatrixTrace::kComponent:
1011 gep =
1012 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1013 "%index";
1014 break;
1015 default:
1016 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1017 break;
1018 }
1019
1020 const std::string spirv = R"(
1021 OpCapability Shader
1022 OpCapability VariablePointers
1023 OpCapability UntypedPointersKHR
1024 OpExtension "SPV_KHR_untyped_pointers"
1025 OpMemoryModel Logical GLSL450
1026 OpEntryPoint GLCompute %main "main"
1027 OpExecutionMode %main LocalSize 1 1 1
1028 %void = OpTypeVoid
1029 %bool = OpTypeBool
1030 %bool_cond = OpUndef %bool
1031 %int = OpTypeInt 32 0
1032 %int_0 = OpConstant %int 0
1033 %int_1 = OpConstant %int 1
1034 %int_2 = OpConstant %int 2
1035 %index = OpUndef %int
1036 %float = OpTypeFloat 32
1037 %v2float = OpTypeVector %float 2
1038 %mat2x2 = OpTypeMatrix %v2float 2
1039 %array = OpTypeArray %mat2x2 %int_2
1040 %struct = OpTypeStruct %array %float
1041 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1042 %null = OpConstantNull %ptr_wg
1043 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1044 %void_fn = OpTypeFunction %void
1045 %main = OpFunction %void None %void_fn
1046 %entry = OpLabel
1047 %gep = )" + gep + R"(
1048 OpBranch %loop
1049 %loop = OpLabel
1050 %phi = OpPhi %ptr_wg %copy %continue %gep %entry
1051 OpLoopMerge %merge %continue None
1052 OpBranchConditional %bool_cond %merge %continue
1053 %continue = OpLabel
1054 %copy = OpCopyObject %ptr_wg %phi
1055 OpBranch %loop
1056 %merge = OpLabel
1057 OpReturn
1058 OpFunctionEnd
1059 )";
1060
1061 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1062 ? SPV_SUCCESS
1063 : SPV_ERROR_INVALID_DATA;
1064 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1065 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1066 if (expected) {
1067 EXPECT_THAT(getDiagnosticString(),
1068 HasSubstr("Variable pointer must not point to a column or a "
1069 "component of a column of a matrix"));
1070 }
1071 }
1072
1073 TEST_P(MatrixTraceUntypedTest, SelectOp1) {
1074 const auto trace_type = GetParam();
1075 std::string gep;
1076 switch (trace_type) {
1077 case MatrixTrace::kColumn:
1078 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1079 break;
1080 case MatrixTrace::kComponent:
1081 gep =
1082 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1083 "%index";
1084 break;
1085 default:
1086 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1087 break;
1088 }
1089
1090 const std::string spirv = R"(
1091 OpCapability Shader
1092 OpCapability VariablePointers
1093 OpCapability UntypedPointersKHR
1094 OpExtension "SPV_KHR_untyped_pointers"
1095 OpMemoryModel Logical GLSL450
1096 OpEntryPoint GLCompute %main "main"
1097 OpExecutionMode %main LocalSize 1 1 1
1098 %void = OpTypeVoid
1099 %bool = OpTypeBool
1100 %bool_cond = OpUndef %bool
1101 %int = OpTypeInt 32 0
1102 %int_0 = OpConstant %int 0
1103 %int_1 = OpConstant %int 1
1104 %int_2 = OpConstant %int 2
1105 %index = OpUndef %int
1106 %float = OpTypeFloat 32
1107 %v2float = OpTypeVector %float 2
1108 %mat2x2 = OpTypeMatrix %v2float 2
1109 %array = OpTypeArray %mat2x2 %int_2
1110 %struct = OpTypeStruct %array %float
1111 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1112 %null = OpConstantNull %ptr_wg
1113 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1114 %void_fn = OpTypeFunction %void
1115 %main = OpFunction %void None %void_fn
1116 %entry = OpLabel
1117 %gep = )" + gep + R"(
1118 %copy = OpCopyObject %ptr_wg %gep
1119 %sel = OpSelect %ptr_wg %bool_cond %copy %null
1120 OpReturn
1121 OpFunctionEnd
1122 )";
1123
1124 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1125 ? SPV_SUCCESS
1126 : SPV_ERROR_INVALID_DATA;
1127 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1128 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1129 if (expected) {
1130 EXPECT_THAT(getDiagnosticString(),
1131 HasSubstr("Variable pointer must not point to a column or a "
1132 "component of a column of a matrix"));
1133 }
1134 }
1135
1136 TEST_P(MatrixTraceUntypedTest, SelectOp2) {
1137 const auto trace_type = GetParam();
1138 std::string gep;
1139 switch (trace_type) {
1140 case MatrixTrace::kColumn:
1141 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1142 break;
1143 case MatrixTrace::kComponent:
1144 gep =
1145 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1146 "%index";
1147 break;
1148 default:
1149 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1150 break;
1151 }
1152
1153 const std::string spirv = R"(
1154 OpCapability Shader
1155 OpCapability VariablePointers
1156 OpCapability UntypedPointersKHR
1157 OpExtension "SPV_KHR_untyped_pointers"
1158 OpMemoryModel Logical GLSL450
1159 OpEntryPoint GLCompute %main "main"
1160 OpExecutionMode %main LocalSize 1 1 1
1161 %void = OpTypeVoid
1162 %bool = OpTypeBool
1163 %bool_cond = OpUndef %bool
1164 %int = OpTypeInt 32 0
1165 %int_0 = OpConstant %int 0
1166 %int_1 = OpConstant %int 1
1167 %int_2 = OpConstant %int 2
1168 %index = OpUndef %int
1169 %float = OpTypeFloat 32
1170 %v2float = OpTypeVector %float 2
1171 %mat2x2 = OpTypeMatrix %v2float 2
1172 %array = OpTypeArray %mat2x2 %int_2
1173 %struct = OpTypeStruct %array %float
1174 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1175 %null = OpConstantNull %ptr_wg
1176 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1177 %void_fn = OpTypeFunction %void
1178 %main = OpFunction %void None %void_fn
1179 %entry = OpLabel
1180 %gep = )" + gep + R"(
1181 %copy = OpCopyObject %ptr_wg %gep
1182 %sel = OpSelect %ptr_wg %bool_cond %null %copy
1183 OpReturn
1184 OpFunctionEnd
1185 )";
1186
1187 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1188 ? SPV_SUCCESS
1189 : SPV_ERROR_INVALID_DATA;
1190 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1191 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1192 if (expected) {
1193 EXPECT_THAT(getDiagnosticString(),
1194 HasSubstr("Variable pointer must not point to a column or a "
1195 "component of a column of a matrix"));
1196 }
1197 }
1198
1199 TEST_P(MatrixTraceUntypedTest, FunctionVariable) {
1200 const auto trace_type = GetParam();
1201 std::string gep;
1202 switch (trace_type) {
1203 case MatrixTrace::kColumn:
1204 gep = "OpUntypedAccessChainKHR %ptr_wg %mat2x2 %var_mat2x2 %index";
1205 break;
1206 case MatrixTrace::kComponent:
1207 gep = "OpUntypedAccessChainKHR %ptr_wg %mat2x2 %var_mat2x2 %index %index";
1208 break;
1209 default:
1210 gep = "OpUntypedAccessChainKHR %ptr_wg %float %var_float";
1211 break;
1212 }
1213
1214 const std::string spirv = R"(
1215 OpCapability Shader
1216 OpCapability VariablePointers
1217 OpCapability UntypedPointersKHR
1218 OpExtension "SPV_KHR_untyped_pointers"
1219 OpMemoryModel Logical GLSL450
1220 OpEntryPoint GLCompute %main "main"
1221 OpExecutionMode %main LocalSize 1 1 1
1222 %void = OpTypeVoid
1223 %bool = OpTypeBool
1224 %bool_cond = OpUndef %bool
1225 %int = OpTypeInt 32 0
1226 %int_0 = OpConstant %int 0
1227 %int_1 = OpConstant %int 1
1228 %int_2 = OpConstant %int 2
1229 %index = OpUndef %int
1230 %float = OpTypeFloat 32
1231 %v2float = OpTypeVector %float 2
1232 %mat2x2 = OpTypeMatrix %v2float 2
1233 %array = OpTypeArray %mat2x2 %int_2
1234 %struct = OpTypeStruct %array %float
1235 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1236 %ptr_func_wg = OpTypePointer Function %ptr_wg
1237 %var_mat2x2 = OpUntypedVariableKHR %ptr_wg Workgroup %mat2x2
1238 %var_float = OpUntypedVariableKHR %ptr_wg Workgroup %float
1239 %void_fn = OpTypeFunction %void
1240 %main = OpFunction %void None %void_fn
1241 %entry = OpLabel
1242 %func_var = OpVariable %ptr_func_wg Function
1243 %gep = )" + gep + R"(
1244 OpStore %func_var %gep
1245 %ld = OpLoad %ptr_wg %func_var
1246 OpReturn
1247 OpFunctionEnd
1248 )";
1249
1250 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1251 ? SPV_SUCCESS
1252 : SPV_ERROR_INVALID_DATA;
1253 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1254 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1255 if (expected) {
1256 EXPECT_THAT(getDiagnosticString(),
1257 HasSubstr("Variable pointer must not point to a column or a "
1258 "component of a column of a matrix"));
1259 }
1260 }
1261
1262 TEST_P(MatrixTraceUntypedTest, PrivateVariable) {
1263 const auto trace_type = GetParam();
1264 std::string gep;
1265 switch (trace_type) {
1266 case MatrixTrace::kColumn:
1267 gep = "OpUntypedAccessChainKHR %ptr_wg %mat2x2 %var_mat2x2 %index";
1268 break;
1269 case MatrixTrace::kComponent:
1270 gep = "OpUntypedAccessChainKHR %ptr_wg %mat2x2 %var_mat2x2 %index %index";
1271 break;
1272 default:
1273 gep = "OpUntypedAccessChainKHR %ptr_wg %float %var_float";
1274 break;
1275 }
1276
1277 const std::string spirv = R"(
1278 OpCapability Shader
1279 OpCapability VariablePointers
1280 OpCapability UntypedPointersKHR
1281 OpExtension "SPV_KHR_untyped_pointers"
1282 OpMemoryModel Logical GLSL450
1283 OpEntryPoint GLCompute %main "main"
1284 OpExecutionMode %main LocalSize 1 1 1
1285 %void = OpTypeVoid
1286 %bool = OpTypeBool
1287 %bool_cond = OpUndef %bool
1288 %int = OpTypeInt 32 0
1289 %int_0 = OpConstant %int 0
1290 %int_1 = OpConstant %int 1
1291 %int_2 = OpConstant %int 2
1292 %index = OpUndef %int
1293 %float = OpTypeFloat 32
1294 %v2float = OpTypeVector %float 2
1295 %mat2x2 = OpTypeMatrix %v2float 2
1296 %array = OpTypeArray %mat2x2 %int_2
1297 %struct = OpTypeStruct %array %float
1298 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1299 %ptr_priv_wg = OpTypePointer Private %ptr_wg
1300 %var_mat2x2 = OpUntypedVariableKHR %ptr_wg Workgroup %mat2x2
1301 %var_float = OpUntypedVariableKHR %ptr_wg Workgroup %float
1302 %priv_var = OpVariable %ptr_priv_wg Private
1303 %void_fn = OpTypeFunction %void
1304 %main = OpFunction %void None %void_fn
1305 %entry = OpLabel
1306 %gep = )" + gep + R"(
1307 OpStore %priv_var %gep
1308 %ld = OpLoad %ptr_wg %priv_var
1309 OpReturn
1310 OpFunctionEnd
1311 )";
1312
1313 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1314 ? SPV_SUCCESS
1315 : SPV_ERROR_INVALID_DATA;
1316 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1317 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1318 if (expected) {
1319 EXPECT_THAT(getDiagnosticString(),
1320 HasSubstr("Variable pointer must not point to a column or a "
1321 "component of a column of a matrix"));
1322 }
1323 }
1324
1325 TEST_P(MatrixTraceUntypedTest, FunctionVariableAggregate) {
1326 const auto trace_type = GetParam();
1327 std::string gep;
1328 switch (trace_type) {
1329 case MatrixTrace::kColumn:
1330 gep = "OpUntypedAccessChainKHR %ptr_wg %mat2x2 %var_mat2x2 %index";
1331 break;
1332 case MatrixTrace::kComponent:
1333 gep = "OpUntypedAccessChainKHR %ptr_wg %mat2x2 %var_mat2x2 %index %index";
1334 break;
1335 default:
1336 gep = "OpUntypedAccessChainKHR %ptr_wg %float %var_float";
1337 break;
1338 }
1339
1340 const std::string spirv = R"(
1341 OpCapability Shader
1342 OpCapability VariablePointers
1343 OpCapability UntypedPointersKHR
1344 OpExtension "SPV_KHR_untyped_pointers"
1345 OpMemoryModel Logical GLSL450
1346 OpEntryPoint GLCompute %main "main"
1347 OpExecutionMode %main LocalSize 1 1 1
1348 %void = OpTypeVoid
1349 %bool = OpTypeBool
1350 %bool_cond = OpUndef %bool
1351 %int = OpTypeInt 32 0
1352 %int_0 = OpConstant %int 0
1353 %int_1 = OpConstant %int 1
1354 %int_2 = OpConstant %int 2
1355 %index = OpUndef %int
1356 %float = OpTypeFloat 32
1357 %v2float = OpTypeVector %float 2
1358 %mat2x2 = OpTypeMatrix %v2float 2
1359 %array = OpTypeArray %mat2x2 %int_2
1360 %struct = OpTypeStruct %array %float
1361 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1362 %func_array = OpTypeArray %ptr_wg %int_1
1363 %func_struct = OpTypeStruct %func_array
1364 %ptr_func_wg_struct = OpTypePointer Function %func_struct
1365 %ptr_func_wg = OpTypePointer Function %ptr_wg
1366 %var_mat2x2 = OpUntypedVariableKHR %ptr_wg Workgroup %mat2x2
1367 %var_float = OpUntypedVariableKHR %ptr_wg Workgroup %float
1368 %void_fn = OpTypeFunction %void
1369 %main = OpFunction %void None %void_fn
1370 %entry = OpLabel
1371 %func_var = OpVariable %ptr_func_wg_struct Function
1372 %gep = )" + gep + R"(
1373 %store_gep = OpAccessChain %ptr_func_wg %func_var %int_0 %index
1374 %store_gep_copy = OpCopyObject %ptr_func_wg %store_gep
1375 OpStore %store_gep_copy %gep
1376 %ld_gep = OpAccessChain %ptr_func_wg %func_var %int_0 %index
1377 %ld = OpLoad %ptr_wg %ld_gep
1378 OpReturn
1379 OpFunctionEnd
1380 )";
1381
1382 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1383 ? SPV_SUCCESS
1384 : SPV_ERROR_INVALID_DATA;
1385 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1386 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1387 if (expected) {
1388 EXPECT_THAT(getDiagnosticString(),
1389 HasSubstr("Variable pointer must not point to a column or a "
1390 "component of a column of a matrix"));
1391 }
1392 }
1393
1394 TEST_P(MatrixTraceUntypedTest, FunctionCallParam1) {
1395 const auto trace_type = GetParam();
1396 std::string gep;
1397 switch (trace_type) {
1398 case MatrixTrace::kColumn:
1399 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1400 break;
1401 case MatrixTrace::kComponent:
1402 gep =
1403 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1404 "%index";
1405 break;
1406 default:
1407 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1408 break;
1409 }
1410
1411 const std::string spirv = R"(
1412 OpCapability Shader
1413 OpCapability VariablePointers
1414 OpCapability UntypedPointersKHR
1415 OpExtension "SPV_KHR_untyped_pointers"
1416 OpMemoryModel Logical GLSL450
1417 OpEntryPoint GLCompute %main "main"
1418 OpExecutionMode %main LocalSize 1 1 1
1419 %void = OpTypeVoid
1420 %bool = OpTypeBool
1421 %bool_cond = OpUndef %bool
1422 %int = OpTypeInt 32 0
1423 %int_0 = OpConstant %int 0
1424 %int_1 = OpConstant %int 1
1425 %int_2 = OpConstant %int 2
1426 %index = OpUndef %int
1427 %float = OpTypeFloat 32
1428 %v2float = OpTypeVector %float 2
1429 %mat2x2 = OpTypeMatrix %v2float 2
1430 %array = OpTypeArray %mat2x2 %int_2
1431 %struct = OpTypeStruct %array %float
1432 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1433 %null = OpConstantNull %ptr_wg
1434 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1435 %void_fn = OpTypeFunction %void
1436 %foo_fn = OpTypeFunction %void %ptr_wg %bool
1437 %main = OpFunction %void None %void_fn
1438 %entry = OpLabel
1439 %gep = )" + gep + R"(
1440 %copy = OpCopyObject %ptr_wg %gep
1441 %call = OpFunctionCall %void %foo %copy %bool_cond
1442 OpReturn
1443 OpFunctionEnd
1444 %foo = OpFunction %void None %foo_fn
1445 %ptr_param = OpFunctionParameter %ptr_wg
1446 %bool_param = OpFunctionParameter %bool
1447 %foo_entry = OpLabel
1448 %sel = OpSelect %ptr_wg %bool_param %ptr_param %null
1449 OpReturn
1450 OpFunctionEnd
1451 )";
1452
1453 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1454 ? SPV_SUCCESS
1455 : SPV_ERROR_INVALID_DATA;
1456 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1457 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1458 if (expected) {
1459 EXPECT_THAT(getDiagnosticString(),
1460 HasSubstr("Variable pointer must not point to a column or a "
1461 "component of a column of a matrix"));
1462 }
1463 }
1464
1465 TEST_P(MatrixTraceUntypedTest, FunctionCallParam2) {
1466 const auto trace_type = GetParam();
1467 std::string gep;
1468 switch (trace_type) {
1469 case MatrixTrace::kColumn:
1470 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1471 break;
1472 case MatrixTrace::kComponent:
1473 gep =
1474 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1475 "%index";
1476 break;
1477 default:
1478 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1479 break;
1480 }
1481
1482 const std::string spirv = R"(
1483 OpCapability Shader
1484 OpCapability VariablePointers
1485 OpCapability UntypedPointersKHR
1486 OpExtension "SPV_KHR_untyped_pointers"
1487 OpMemoryModel Logical GLSL450
1488 OpEntryPoint GLCompute %main "main"
1489 OpExecutionMode %main LocalSize 1 1 1
1490 %void = OpTypeVoid
1491 %bool = OpTypeBool
1492 %bool_cond = OpUndef %bool
1493 %int = OpTypeInt 32 0
1494 %int_0 = OpConstant %int 0
1495 %int_1 = OpConstant %int 1
1496 %int_2 = OpConstant %int 2
1497 %index = OpUndef %int
1498 %float = OpTypeFloat 32
1499 %v2float = OpTypeVector %float 2
1500 %mat2x2 = OpTypeMatrix %v2float 2
1501 %array = OpTypeArray %mat2x2 %int_2
1502 %struct = OpTypeStruct %array %float
1503 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1504 %null = OpConstantNull %ptr_wg
1505 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1506 %void_fn = OpTypeFunction %void
1507 %foo_fn = OpTypeFunction %void %bool %ptr_wg
1508 %main = OpFunction %void None %void_fn
1509 %entry = OpLabel
1510 %gep = )" + gep + R"(
1511 %copy = OpCopyObject %ptr_wg %gep
1512 %call = OpFunctionCall %void %foo %bool_cond %copy
1513 OpReturn
1514 OpFunctionEnd
1515 %foo = OpFunction %void None %foo_fn
1516 %bool_param = OpFunctionParameter %bool
1517 %ptr_param = OpFunctionParameter %ptr_wg
1518 %foo_entry = OpLabel
1519 %sel = OpSelect %ptr_wg %bool_param %ptr_param %null
1520 OpReturn
1521 OpFunctionEnd
1522 )";
1523
1524 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1525 ? SPV_SUCCESS
1526 : SPV_ERROR_INVALID_DATA;
1527 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1528 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1529 if (expected) {
1530 EXPECT_THAT(getDiagnosticString(),
1531 HasSubstr("Variable pointer must not point to a column or a "
1532 "component of a column of a matrix"));
1533 }
1534 }
1535
1536 TEST_P(MatrixTraceUntypedTest, FunctionCall) {
1537 const auto trace_type = GetParam();
1538 std::string gep;
1539 switch (trace_type) {
1540 case MatrixTrace::kColumn:
1541 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1542 break;
1543 case MatrixTrace::kComponent:
1544 gep =
1545 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1546 "%index";
1547 break;
1548 default:
1549 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1550 break;
1551 }
1552
1553 const std::string spirv = R"(
1554 OpCapability Shader
1555 OpCapability VariablePointers
1556 OpCapability UntypedPointersKHR
1557 OpExtension "SPV_KHR_untyped_pointers"
1558 OpMemoryModel Logical GLSL450
1559 OpEntryPoint GLCompute %main "main"
1560 OpExecutionMode %main LocalSize 1 1 1
1561 %void = OpTypeVoid
1562 %bool = OpTypeBool
1563 %bool_cond = OpUndef %bool
1564 %int = OpTypeInt 32 0
1565 %int_0 = OpConstant %int 0
1566 %int_1 = OpConstant %int 1
1567 %int_2 = OpConstant %int 2
1568 %index = OpUndef %int
1569 %float = OpTypeFloat 32
1570 %v2float = OpTypeVector %float 2
1571 %mat2x2 = OpTypeMatrix %v2float 2
1572 %array = OpTypeArray %mat2x2 %int_2
1573 %struct = OpTypeStruct %array %float
1574 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1575 %null = OpConstantNull %ptr_wg
1576 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1577 %void_fn = OpTypeFunction %void
1578 %foo_ty = OpTypeFunction %ptr_wg
1579 %main = OpFunction %void None %void_fn
1580 %entry = OpLabel
1581 %call = OpFunctionCall %ptr_wg %foo
1582 OpReturn
1583 OpFunctionEnd
1584 %foo = OpFunction %ptr_wg None %foo_ty
1585 %foo_entry = OpLabel
1586 %gep = )" + gep + R"(
1587 OpReturnValue %gep
1588 OpFunctionEnd
1589 )";
1590
1591 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1592 ? SPV_SUCCESS
1593 : SPV_ERROR_INVALID_DATA;
1594 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1595 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1596 if (expected) {
1597 EXPECT_THAT(getDiagnosticString(),
1598 HasSubstr("Variable pointer must not point to a column or a "
1599 "component of a column of a matrix"));
1600 }
1601 }
1602
1603 TEST_P(MatrixTraceUntypedTest, FunctionCallMultiReturn1) {
1604 const auto trace_type = GetParam();
1605 std::string gep;
1606 switch (trace_type) {
1607 case MatrixTrace::kColumn:
1608 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1609 break;
1610 case MatrixTrace::kComponent:
1611 gep =
1612 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1613 "%index";
1614 break;
1615 default:
1616 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1617 break;
1618 }
1619
1620 const std::string spirv = R"(
1621 OpCapability Shader
1622 OpCapability VariablePointers
1623 OpCapability UntypedPointersKHR
1624 OpExtension "SPV_KHR_untyped_pointers"
1625 OpMemoryModel Logical GLSL450
1626 OpEntryPoint GLCompute %main "main"
1627 OpExecutionMode %main LocalSize 1 1 1
1628 %void = OpTypeVoid
1629 %bool = OpTypeBool
1630 %bool_cond = OpUndef %bool
1631 %int = OpTypeInt 32 0
1632 %int_0 = OpConstant %int 0
1633 %int_1 = OpConstant %int 1
1634 %int_2 = OpConstant %int 2
1635 %index = OpUndef %int
1636 %float = OpTypeFloat 32
1637 %v2float = OpTypeVector %float 2
1638 %mat2x2 = OpTypeMatrix %v2float 2
1639 %array = OpTypeArray %mat2x2 %int_2
1640 %struct = OpTypeStruct %array %float
1641 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1642 %null = OpConstantNull %ptr_wg
1643 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1644 %void_fn = OpTypeFunction %void
1645 %foo_ty = OpTypeFunction %ptr_wg
1646 %main = OpFunction %void None %void_fn
1647 %entry = OpLabel
1648 %call = OpFunctionCall %ptr_wg %foo
1649 OpReturn
1650 OpFunctionEnd
1651 %foo = OpFunction %ptr_wg None %foo_ty
1652 %foo_entry = OpLabel
1653 %gep = )" + gep + R"(
1654 OpSelectionMerge %merge None
1655 OpBranchConditional %bool_cond %then %merge
1656 %then = OpLabel
1657 OpReturnValue %gep
1658 %merge = OpLabel
1659 OpReturnValue %null
1660 OpFunctionEnd
1661 )";
1662
1663 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1664 ? SPV_SUCCESS
1665 : SPV_ERROR_INVALID_DATA;
1666 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1667 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1668 if (expected) {
1669 EXPECT_THAT(getDiagnosticString(),
1670 HasSubstr("Variable pointer must not point to a column or a "
1671 "component of a column of a matrix"));
1672 }
1673 }
1674
1675 TEST_P(MatrixTraceUntypedTest, FunctionCallMultiReturn2) {
1676 const auto trace_type = GetParam();
1677 std::string gep;
1678 switch (trace_type) {
1679 case MatrixTrace::kColumn:
1680 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index";
1681 break;
1682 case MatrixTrace::kComponent:
1683 gep =
1684 "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_0 %index %index "
1685 "%index";
1686 break;
1687 default:
1688 gep = "OpUntypedAccessChainKHR %ptr_wg %struct %var %int_1";
1689 break;
1690 }
1691
1692 const std::string spirv = R"(
1693 OpCapability Shader
1694 OpCapability VariablePointers
1695 OpCapability UntypedPointersKHR
1696 OpExtension "SPV_KHR_untyped_pointers"
1697 OpMemoryModel Logical GLSL450
1698 OpEntryPoint GLCompute %main "main"
1699 OpExecutionMode %main LocalSize 1 1 1
1700 %void = OpTypeVoid
1701 %bool = OpTypeBool
1702 %bool_cond = OpUndef %bool
1703 %int = OpTypeInt 32 0
1704 %int_0 = OpConstant %int 0
1705 %int_1 = OpConstant %int 1
1706 %int_2 = OpConstant %int 2
1707 %index = OpUndef %int
1708 %float = OpTypeFloat 32
1709 %v2float = OpTypeVector %float 2
1710 %mat2x2 = OpTypeMatrix %v2float 2
1711 %array = OpTypeArray %mat2x2 %int_2
1712 %struct = OpTypeStruct %array %float
1713 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1714 %null = OpConstantNull %ptr_wg
1715 %var = OpUntypedVariableKHR %ptr_wg Workgroup %struct
1716 %void_fn = OpTypeFunction %void
1717 %foo_ty = OpTypeFunction %ptr_wg
1718 %main = OpFunction %void None %void_fn
1719 %entry = OpLabel
1720 %call = OpFunctionCall %ptr_wg %foo
1721 OpReturn
1722 OpFunctionEnd
1723 %foo = OpFunction %ptr_wg None %foo_ty
1724 %foo_entry = OpLabel
1725 %gep = )" + gep + R"(
1726 OpSelectionMerge %merge None
1727 OpBranchConditional %bool_cond %then %merge
1728 %then = OpLabel
1729 OpReturnValue %null
1730 %merge = OpLabel
1731 OpReturnValue %gep
1732 OpFunctionEnd
1733 )";
1734
1735 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1736 ? SPV_SUCCESS
1737 : SPV_ERROR_INVALID_DATA;
1738 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1739 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1740 if (expected) {
1741 EXPECT_THAT(getDiagnosticString(),
1742 HasSubstr("Variable pointer must not point to a column or a "
1743 "component of a column of a matrix"));
1744 }
1745 }
1746
1747 TEST_P(MatrixTraceUntypedTest, MixedTypes) {
1748 const auto trace_type = GetParam();
1749 std::string gep;
1750 switch (trace_type) {
1751 case MatrixTrace::kColumn:
1752 gep = "OpAccessChain %ptr_wg_v2float %var %int_0 %index %index";
1753 break;
1754 case MatrixTrace::kComponent:
1755 gep = "OpAccessChain %ptr_wg_float %var %int_0 %index %index %index";
1756 break;
1757 default:
1758 gep = "OpAccessChain %ptr_wg_float %var %int_1";
1759 break;
1760 }
1761
1762 const std::string spirv = R"(
1763 OpCapability Shader
1764 OpCapability VariablePointers
1765 OpCapability UntypedPointersKHR
1766 OpExtension "SPV_KHR_untyped_pointers"
1767 OpMemoryModel Logical GLSL450
1768 OpEntryPoint GLCompute %main "main"
1769 OpExecutionMode %main LocalSize 1 1 1
1770 %void = OpTypeVoid
1771 %bool = OpTypeBool
1772 %bool_cond = OpUndef %bool
1773 %int = OpTypeInt 32 0
1774 %int_0 = OpConstant %int 0
1775 %int_1 = OpConstant %int 1
1776 %int_2 = OpConstant %int 2
1777 %index = OpUndef %int
1778 %float = OpTypeFloat 32
1779 %v2float = OpTypeVector %float 2
1780 %mat2x2 = OpTypeMatrix %v2float 2
1781 %array = OpTypeArray %mat2x2 %int_2
1782 %struct = OpTypeStruct %array %float
1783 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
1784 %ptr_wg_struct = OpTypePointer Workgroup %struct
1785 %ptr_wg_v2float = OpTypePointer Workgroup %v2float
1786 %ptr_wg_float = OpTypePointer Workgroup %float
1787 %null = OpConstantNull %ptr_wg
1788 %var = OpVariable %ptr_wg_struct Workgroup
1789 %void_fn = OpTypeFunction %void
1790 %main = OpFunction %void None %void_fn
1791 %entry = OpLabel
1792 %gep = )" + gep + R"(
1793 %copy = OpUntypedAccessChainKHR %ptr_wg %struct %gep
1794 %sel = OpSelect %ptr_wg %bool_cond %copy %null
1795 OpReturn
1796 OpFunctionEnd
1797 )";
1798
1799 const auto expected = trace_type == MatrixTrace::kNotAMatrix
1800 ? SPV_SUCCESS
1801 : SPV_ERROR_INVALID_DATA;
1802 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1803 EXPECT_EQ(expected, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1804 if (expected) {
1805 EXPECT_THAT(getDiagnosticString(),
1806 HasSubstr("Variable pointer must not point to a column or a "
1807 "component of a column of a matrix"));
1808 }
1809 }
1810
1811 INSTANTIATE_TEST_SUITE_P(ValidateLogicalPointersMatrixTraceUntyped,
1812 MatrixTraceUntypedTest, ValuesIn(traces));
1813
1814 TEST_F(ValidateLogicalPointersTest, SelectDifferentBuffersTyped) {
1815 const std::string spirv = R"(
1816 OpCapability VariablePointersStorageBuffer
1817 OpCapability Shader
1818 OpMemoryModel Logical GLSL450
1819 OpEntryPoint GLCompute %main "main"
1820 OpExecutionMode %main LocalSize 1 1 1
1821 %void = OpTypeVoid
1822 %bool = OpTypeBool
1823 %bool_cond = OpUndef %bool
1824 %int = OpTypeInt 32 0
1825 %struct = OpTypeStruct %int
1826 %ptr_struct = OpTypePointer StorageBuffer %struct
1827 %v1 = OpVariable %ptr_struct StorageBuffer
1828 %v2 = OpVariable %ptr_struct StorageBuffer
1829 %void_fn = OpTypeFunction %void
1830 %main = OpFunction %void None %void_fn
1831 %entry = OpLabel
1832 %select = OpSelect %ptr_struct %bool_cond %v1 %v2
1833 OpReturn
1834 OpFunctionEnd
1835 )";
1836
1837 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1838 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1839 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1840 EXPECT_THAT(getDiagnosticString(),
1841 HasSubstr("Variable pointers must point into the same structure "
1842 "(or OpConstantNull)"));
1843 }
1844
1845 TEST_F(ValidateLogicalPointersTest, SelectDifferentBuffersUntyped) {
1846 const std::string spirv = R"(
1847 OpCapability VariablePointersStorageBuffer
1848 OpCapability Shader
1849 OpCapability UntypedPointersKHR
1850 OpExtension "SPV_KHR_untyped_pointers"
1851 OpMemoryModel Logical GLSL450
1852 OpEntryPoint GLCompute %main "main"
1853 OpExecutionMode %main LocalSize 1 1 1
1854 %void = OpTypeVoid
1855 %bool = OpTypeBool
1856 %bool_cond = OpUndef %bool
1857 %int = OpTypeInt 32 0
1858 %struct = OpTypeStruct %int
1859 %ptr = OpTypeUntypedPointerKHR StorageBuffer
1860 %v1 = OpUntypedVariableKHR %ptr StorageBuffer %struct
1861 %v2 = OpUntypedVariableKHR %ptr StorageBuffer %struct
1862 %void_fn = OpTypeFunction %void
1863 %main = OpFunction %void None %void_fn
1864 %entry = OpLabel
1865 %select = OpSelect %ptr %bool_cond %v1 %v2
1866 OpReturn
1867 OpFunctionEnd
1868 )";
1869
1870 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1871 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1872 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1873 EXPECT_THAT(getDiagnosticString(),
1874 HasSubstr("Variable pointers must point into the same structure "
1875 "(or OpConstantNull)"));
1876 }
1877
1878 TEST_F(ValidateLogicalPointersTest, PhiDifferentBuffersTyped) {
1879 const std::string spirv = R"(
1880 OpCapability VariablePointersStorageBuffer
1881 OpCapability Shader
1882 OpMemoryModel Logical GLSL450
1883 OpEntryPoint GLCompute %main "main"
1884 OpExecutionMode %main LocalSize 1 1 1
1885 %void = OpTypeVoid
1886 %bool = OpTypeBool
1887 %bool_cond = OpUndef %bool
1888 %int = OpTypeInt 32 0
1889 %struct = OpTypeStruct %int
1890 %ptr_struct = OpTypePointer StorageBuffer %struct
1891 %v1 = OpVariable %ptr_struct StorageBuffer
1892 %v2 = OpVariable %ptr_struct StorageBuffer
1893 %void_fn = OpTypeFunction %void
1894 %main = OpFunction %void None %void_fn
1895 %entry = OpLabel
1896 OpSelectionMerge %merge None
1897 OpBranchConditional %bool_cond %then %merge
1898 %then = OpLabel
1899 OpBranch %merge
1900 %merge = OpLabel
1901 %phi = OpPhi %ptr_struct %v1 %entry %v2 %then
1902 OpReturn
1903 OpFunctionEnd
1904 )";
1905
1906 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1907 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1908 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1909 EXPECT_THAT(getDiagnosticString(),
1910 HasSubstr("Variable pointers must point into the same structure "
1911 "(or OpConstantNull)"));
1912 }
1913
1914 TEST_F(ValidateLogicalPointersTest, PhiDifferentBuffersUntyped) {
1915 const std::string spirv = R"(
1916 OpCapability VariablePointersStorageBuffer
1917 OpCapability Shader
1918 OpCapability UntypedPointersKHR
1919 OpExtension "SPV_KHR_untyped_pointers"
1920 OpMemoryModel Logical GLSL450
1921 OpEntryPoint GLCompute %main "main"
1922 OpExecutionMode %main LocalSize 1 1 1
1923 %void = OpTypeVoid
1924 %bool = OpTypeBool
1925 %bool_cond = OpUndef %bool
1926 %int = OpTypeInt 32 0
1927 %struct = OpTypeStruct %int
1928 %ptr = OpTypeUntypedPointerKHR StorageBuffer
1929 %v1 = OpUntypedVariableKHR %ptr StorageBuffer %struct
1930 %v2 = OpUntypedVariableKHR %ptr StorageBuffer %struct
1931 %void_fn = OpTypeFunction %void
1932 %main = OpFunction %void None %void_fn
1933 %entry = OpLabel
1934 OpSelectionMerge %merge None
1935 OpBranchConditional %bool_cond %then %merge
1936 %then = OpLabel
1937 OpBranch %merge
1938 %merge = OpLabel
1939 %phi = OpPhi %ptr %v1 %entry %v2 %then
1940 OpReturn
1941 OpFunctionEnd
1942 )";
1943
1944 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1945 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1946 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1947 EXPECT_THAT(getDiagnosticString(),
1948 HasSubstr("Variable pointers must point into the same structure "
1949 "(or OpConstantNull)"));
1950 }
1951
1952 TEST_F(ValidateLogicalPointersTest, BlockArrayTyped) {
1953 const std::string spirv = R"(
1954 OpCapability Shader
1955 OpCapability VariablePointers
1956 OpMemoryModel Logical GLSL450
1957 OpEntryPoint GLCompute %main "main"
1958 OpExecutionMode %main LocalSize 1 1 1
1959 OpDecorate %struct Block
1960 OpMemberDecorate %struct 0 Offset 0
1961 %void = OpTypeVoid
1962 %int = OpTypeInt 32 0
1963 %int_0 = OpConstant %int 0
1964 %int_4 = OpConstant %int 4
1965 %bool = OpTypeBool
1966 %bool_cond = OpUndef %bool
1967 %struct = OpTypeStruct %int
1968 %array = OpTypeArray %struct %int_4
1969 %ptr_array = OpTypePointer StorageBuffer %array
1970 %ptr_int = OpTypePointer StorageBuffer %int
1971 %var = OpVariable %ptr_array StorageBuffer
1972 %null = OpConstantNull %ptr_array
1973 %void_fn = OpTypeFunction %void
1974 %main = OpFunction %void None %void_fn
1975 %entry = OpLabel
1976 %select = OpSelect %ptr_array %bool_cond %null %var
1977 OpReturn
1978 OpFunctionEnd
1979 )";
1980
1981 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1982 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1983 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1984 EXPECT_THAT(getDiagnosticString(),
1985 HasSubstr("Variable pointer must not point to an array of Block- "
1986 "or BufferBlock-decorated structs"));
1987 }
1988
1989 TEST_F(ValidateLogicalPointersTest, BlockArrayUntyped) {
1990 const std::string spirv = R"(
1991 OpCapability Shader
1992 OpCapability VariablePointers
1993 OpCapability UntypedPointersKHR
1994 OpExtension "SPV_KHR_untyped_pointers"
1995 OpMemoryModel Logical GLSL450
1996 OpEntryPoint GLCompute %main "main"
1997 OpExecutionMode %main LocalSize 1 1 1
1998 OpDecorate %struct Block
1999 OpMemberDecorate %struct 0 Offset 0
2000 %void = OpTypeVoid
2001 %int = OpTypeInt 32 0
2002 %int_0 = OpConstant %int 0
2003 %int_4 = OpConstant %int 4
2004 %bool = OpTypeBool
2005 %bool_cond = OpUndef %bool
2006 %struct = OpTypeStruct %int
2007 %array = OpTypeArray %struct %int_4
2008 %ptr = OpTypeUntypedPointerKHR StorageBuffer
2009 %var = OpUntypedVariableKHR %ptr StorageBuffer %array
2010 %null = OpConstantNull %ptr
2011 %void_fn = OpTypeFunction %void
2012 %main = OpFunction %void None %void_fn
2013 %entry = OpLabel
2014 %select = OpSelect %ptr %bool_cond %null %var
2015 OpReturn
2016 OpFunctionEnd
2017 )";
2018
2019 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2020 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2021 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2022 EXPECT_THAT(getDiagnosticString(),
2023 HasSubstr("Variable pointer must not point to an array of Block- "
2024 "or BufferBlock-decorated structs"));
2025 }
2026
2027 TEST_F(ValidateLogicalPointersTest, UntypedMatrixLoad) {
2028 const std::string spirv = R"(
2029 OpCapability Shader
2030 OpCapability VariablePointers
2031 OpCapability UntypedPointersKHR
2032 OpExtension "SPV_KHR_untyped_pointers"
2033 OpMemoryModel Logical GLSL450
2034 OpEntryPoint GLCompute %main "main"
2035 OpExecutionMode %main LocalSize 1 1 1
2036 %void = OpTypeVoid
2037 %int = OpTypeInt 32 0
2038 %int_0 = OpConstant %int 0
2039 %float = OpTypeFloat 32
2040 %v2float = OpTypeVector %float 2
2041 %mat2x2 = OpTypeMatrix %v2float 2
2042 %struct = OpTypeStruct %mat2x2
2043 %ptr = OpTypeUntypedPointerKHR StorageBuffer
2044 %var = OpUntypedVariableKHR %ptr StorageBuffer %struct
2045 %void_fn = OpTypeFunction %void
2046 %main = OpFunction %void None %void_fn
2047 %entry = OpLabel
2048 %ld = OpLoad %struct %var
2049 OpReturn
2050 OpFunctionEnd
2051 )";
2052
2053 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2054 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2055 }
2056
2057 TEST_F(ValidateLogicalPointersTest, UntypedMatrixLoadVariablePointer) {
2058 const std::string spirv = R"(
2059 OpCapability Shader
2060 OpCapability VariablePointers
2061 OpCapability UntypedPointersKHR
2062 OpExtension "SPV_KHR_untyped_pointers"
2063 OpMemoryModel Logical GLSL450
2064 OpEntryPoint GLCompute %main "main"
2065 OpExecutionMode %main LocalSize 1 1 1
2066 %void = OpTypeVoid
2067 %int = OpTypeInt 32 0
2068 %int_0 = OpConstant %int 0
2069 %float = OpTypeFloat 32
2070 %v2float = OpTypeVector %float 2
2071 %mat2x2 = OpTypeMatrix %v2float 2
2072 %struct = OpTypeStruct %mat2x2
2073 %ptr = OpTypeUntypedPointerKHR StorageBuffer
2074 %var = OpUntypedVariableKHR %ptr StorageBuffer %struct
2075 %null = OpConstantNull %ptr
2076 %bool = OpTypeBool
2077 %cond = OpUndef %bool
2078 %void_fn = OpTypeFunction %void
2079 %main = OpFunction %void None %void_fn
2080 %entry = OpLabel
2081 %sel = OpSelect %ptr %cond %var %null
2082 %ld = OpLoad %struct %sel
2083 OpReturn
2084 OpFunctionEnd
2085 )";
2086
2087 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2088 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2089 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2090 EXPECT_THAT(getDiagnosticString(),
2091 HasSubstr("Variable pointer must not point to an object that is "
2092 "or contains a matrix"));
2093 }
2094
2095 TEST_F(ValidateLogicalPointersTest, UntypedMatrixStore) {
2096 const std::string spirv = R"(
2097 OpCapability Shader
2098 OpCapability VariablePointers
2099 OpCapability UntypedPointersKHR
2100 OpExtension "SPV_KHR_untyped_pointers"
2101 OpMemoryModel Logical GLSL450
2102 OpEntryPoint GLCompute %main "main"
2103 OpExecutionMode %main LocalSize 1 1 1
2104 %void = OpTypeVoid
2105 %int = OpTypeInt 32 0
2106 %int_0 = OpConstant %int 0
2107 %float = OpTypeFloat 32
2108 %v2float = OpTypeVector %float 2
2109 %mat2x2 = OpTypeMatrix %v2float 2
2110 %zero = OpConstantNull %mat2x2
2111 %struct = OpTypeStruct %mat2x2
2112 %ptr = OpTypeUntypedPointerKHR StorageBuffer
2113 %var = OpUntypedVariableKHR %ptr StorageBuffer %struct
2114 %void_fn = OpTypeFunction %void
2115 %main = OpFunction %void None %void_fn
2116 %entry = OpLabel
2117 OpStore %var %zero
2118 OpReturn
2119 OpFunctionEnd
2120 )";
2121
2122 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2123 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2124 }
2125
2126 TEST_F(ValidateLogicalPointersTest, UntypedMatrixStoreVariablePointer) {
2127 const std::string spirv = R"(
2128 OpCapability Shader
2129 OpCapability VariablePointers
2130 OpCapability UntypedPointersKHR
2131 OpExtension "SPV_KHR_untyped_pointers"
2132 OpMemoryModel Logical GLSL450
2133 OpEntryPoint GLCompute %main "main"
2134 OpExecutionMode %main LocalSize 1 1 1
2135 %void = OpTypeVoid
2136 %int = OpTypeInt 32 0
2137 %int_0 = OpConstant %int 0
2138 %float = OpTypeFloat 32
2139 %v2float = OpTypeVector %float 2
2140 %mat2x2 = OpTypeMatrix %v2float 2
2141 %zero = OpConstantNull %mat2x2
2142 %struct = OpTypeStruct %mat2x2
2143 %ptr = OpTypeUntypedPointerKHR StorageBuffer
2144 %var = OpUntypedVariableKHR %ptr StorageBuffer %struct
2145 %null = OpConstantNull %ptr
2146 %bool = OpTypeBool
2147 %cond = OpUndef %bool
2148 %void_fn = OpTypeFunction %void
2149 %main = OpFunction %void None %void_fn
2150 %entry = OpLabel
2151 %sel = OpSelect %ptr %cond %var %null
2152 OpStore %sel %zero
2153 OpReturn
2154 OpFunctionEnd
2155 )";
2156
2157 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2158 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2159 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2160 EXPECT_THAT(getDiagnosticString(),
2161 HasSubstr("Variable pointer must not point to an object that is "
2162 "or contains a matrix"));
2163 }
2164
2165 TEST_F(ValidateLogicalPointersTest, LogicalPointerOperandFailure) {
2166 const std::string spirv = R"(
2167 OpCapability Shader
2168 OpCapability Kernel
2169 OpCapability Addresses
2170 OpCapability UntypedPointersKHR
2171 OpExtension "SPV_KHR_untyped_pointers"
2172 OpMemoryModel Logical GLSL450
2173 OpEntryPoint GLCompute %main "main"
2174 OpExecutionMode %main LocalSize 1 1 1
2175 %void = OpTypeVoid
2176 %int = OpTypeInt 32 0
2177 %int_0 = OpConstant %int 0
2178 %v2int = OpTypeVector %int 2
2179 %ptr_v2int = OpTypePointer StorageBuffer %v2int
2180 %var = OpVariable %ptr_v2int StorageBuffer
2181 %void_fn = OpTypeFunction %void
2182 %main = OpFunction %void None %void_fn
2183 %entry = OpLabel
2184 %gep = OpInBoundsPtrAccessChain %ptr_v2int %var %int_0
2185 OpReturn
2186 OpFunctionEnd
2187 )";
2188
2189 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2190 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2191 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2192 EXPECT_THAT(getDiagnosticString(),
2193 HasSubstr("Instruction may not have a logical pointer operand"));
2194 }
2195
2196 TEST_F(ValidateLogicalPointersTest,
2197 LogicalPointerOperandVariablePointerStorageBuffer) {
2198 const std::string spirv = R"(
2199 OpCapability Shader
2200 OpCapability Linkage
2201 OpCapability Addresses
2202 OpMemoryModel Logical GLSL450
2203 OpDecorate %ptr_int ArrayStride 4
2204 %int = OpTypeInt 32 0
2205 %int_0 = OpConstant %int 0
2206 %int_4 = OpConstant %int 4
2207 %array = OpTypeArray %int %int_4
2208 %ptr_array = OpTypePointer StorageBuffer %array
2209 %ptr_int = OpTypePointer StorageBuffer %int
2210 %var = OpVariable %ptr_array StorageBuffer
2211 %void = OpTypeVoid
2212 %void_fn = OpTypeFunction %void
2213 %foo = OpFunction %void None %void_fn
2214 %entry = OpLabel
2215 %gep = OpAccessChain %ptr_int %var %int_0
2216 %ptr_gep = OpPtrAccessChain %ptr_int %gep %int_0
2217 OpReturn
2218 OpFunctionEnd
2219 )";
2220
2221 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2222 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2223 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2224 EXPECT_THAT(
2225 getDiagnosticString(),
2226 HasSubstr("Instruction may only have a logical pointer operand in the "
2227 "StorageBuffer or "
2228 "Workgroup storage classes with appropriate variable pointers "
2229 "capability"));
2230 }
2231
2232 TEST_F(ValidateLogicalPointersTest,
2233 LogicalPointerOperandVariablePointerWorkgroup) {
2234 const std::string spirv = R"(
2235 OpCapability Shader
2236 OpCapability Linkage
2237 OpCapability VariablePointersStorageBuffer
2238 OpMemoryModel Logical GLSL450
2239 %int = OpTypeInt 32 0
2240 %int_0 = OpConstant %int 0
2241 %int_4 = OpConstant %int 4
2242 %array = OpTypeArray %int %int_4
2243 %ptr_array = OpTypePointer Workgroup %array
2244 %ptr_int = OpTypePointer Workgroup %int
2245 %var = OpVariable %ptr_array Workgroup
2246 %void = OpTypeVoid
2247 %void_fn = OpTypeFunction %void
2248 %foo = OpFunction %void None %void_fn
2249 %entry = OpLabel
2250 %gep = OpAccessChain %ptr_int %var %int_0
2251 %ptr_gep = OpPtrAccessChain %ptr_int %gep %int_0
2252 OpReturn
2253 OpFunctionEnd
2254 )";
2255
2256 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2257 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2258 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2259 EXPECT_THAT(
2260 getDiagnosticString(),
2261 HasSubstr("Instruction may only have a logical pointer operand in the "
2262 "StorageBuffer or "
2263 "Workgroup storage classes with appropriate variable pointers "
2264 "capability"));
2265 }
2266
2267 TEST_F(ValidateLogicalPointersTest,
2268 LogicalPointerReturnVariablePointerStorageBuffer) {
2269 const std::string spirv = R"(
2270 OpCapability Shader
2271 OpCapability Linkage
2272 OpMemoryModel Logical GLSL450
2273 %int = OpTypeInt 32 0
2274 %ptr = OpTypePointer StorageBuffer %int
2275 %null = OpConstantNull %ptr
2276 )";
2277
2278 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2279 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2280 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2281 EXPECT_THAT(
2282 getDiagnosticString(),
2283 HasSubstr("Instruction may only return a logical pointer in the "
2284 "StorageBuffer or "
2285 "Workgroup storage classes with appropriate variable pointers "
2286 "capability"));
2287 }
2288
2289 TEST_F(ValidateLogicalPointersTest,
2290 LogicalPointerReturnVariablePointerWorkgroup) {
2291 const std::string spirv = R"(
2292 OpCapability Shader
2293 OpCapability Linkage
2294 OpCapability VariablePointersStorageBuffer
2295 OpMemoryModel Logical GLSL450
2296 %int = OpTypeInt 32 0
2297 %ptr = OpTypePointer Workgroup %int
2298 %null = OpConstantNull %ptr
2299 )";
2300
2301 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2302 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2303 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2304 EXPECT_THAT(
2305 getDiagnosticString(),
2306 HasSubstr("Instruction may only return a logical pointer in the "
2307 "StorageBuffer or "
2308 "Workgroup storage classes with appropriate variable pointers "
2309 "capability"));
2310 }
2311
2312 TEST_F(ValidateLogicalPointersTest, ArrayLengthInvalidVariablePointer) {
2313 const std::string spirv = R"(
2314 OpCapability Shader
2315 OpCapability VariablePointers
2316 OpMemoryModel Logical GLSL450
2317 OpEntryPoint GLCompute %main "main"
2318 OpExecutionMode %main LocalSize 1 1 1
2319 %void = OpTypeVoid
2320 %int = OpTypeInt 32 0
2321 %array = OpTypeRuntimeArray %int
2322 %struct = OpTypeStruct %array
2323 %ptr = OpTypePointer StorageBuffer %struct
2324 %var = OpVariable %ptr StorageBuffer
2325 %null = OpConstantNull %ptr
2326 %bool = OpTypeBool
2327 %cond = OpUndef %bool
2328 %void_fn = OpTypeFunction %void
2329 %main = OpFunction %void None %void_fn
2330 %entry = OpLabel
2331 %sel = OpSelect %ptr %cond %null %var
2332 %len = OpArrayLength %int %sel 0
2333 OpReturn
2334 OpFunctionEnd
2335 )";
2336
2337 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2338 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2339 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2340 EXPECT_THAT(getDiagnosticString(),
2341 HasSubstr("Pointer operand must not be a variable pointer"));
2342 }
2343
2344 TEST_F(ValidateLogicalPointersTest, ArrayLengthUntypedInvalidVariablePointer) {
2345 const std::string spirv = R"(
2346 OpCapability Shader
2347 OpCapability VariablePointers
2348 OpCapability UntypedPointersKHR
2349 OpExtension "SPV_KHR_untyped_pointers"
2350 OpMemoryModel Logical GLSL450
2351 OpEntryPoint GLCompute %main "main"
2352 OpExecutionMode %main LocalSize 1 1 1
2353 %void = OpTypeVoid
2354 %int = OpTypeInt 32 0
2355 %array = OpTypeRuntimeArray %int
2356 %struct = OpTypeStruct %array
2357 %ptr = OpTypeUntypedPointerKHR StorageBuffer
2358 %var = OpUntypedVariableKHR %ptr StorageBuffer %struct
2359 %null = OpConstantNull %ptr
2360 %bool = OpTypeBool
2361 %cond = OpUndef %bool
2362 %void_fn = OpTypeFunction %void
2363 %main = OpFunction %void None %void_fn
2364 %entry = OpLabel
2365 %sel = OpSelect %ptr %cond %null %var
2366 %len = OpUntypedArrayLengthKHR %int %struct %sel 0
2367 OpReturn
2368 OpFunctionEnd
2369 )";
2370
2371 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
2372 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2373 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2374 EXPECT_THAT(getDiagnosticString(),
2375 HasSubstr("Pointer operand must not be a variable pointer"));
2376 }
2377
2378 TEST_F(ValidateLogicalPointersTest, FunctionParameterOperandOfFunctionCall) {
2379 const std::string spirv = R"(
2380 OpCapability ClipDistance
2381 %1 = OpExtInstImport "GLSL.std.450"
2382 OpMemoryModel Logical GLSL450
2383 OpEntryPoint Fragment %2 " "
2384 OpExecutionMode %2 OriginUpperLeft
2385 %void = OpTypeVoid
2386 %32 = OpTypeFunction %void
2387 %float = OpTypeFloat 32
2388 %_ptr_Function_float = OpTypePointer Function %float
2389 %35 = OpTypeFunction %float %_ptr_Function_float
2390 %float_1_35631564en19 = OpConstant %float 1.35631564e-19
2391 %float_1_35631564en19_0 = OpConstant %float 1.35631564e-19
2392 %2 = OpFunction %void None %32
2393 %8447 = OpLabel
2394 OpUnreachable
2395 OpFunctionEnd
2396 %9 = OpFunction %float None %35
2397 %10 = OpFunctionParameter %_ptr_Function_float
2398 %65535 = OpLabel
2399 %217 = OpVariable %_ptr_Function_float Function
2400 %218 = OpLoad %float %10
2401 %2097407 = OpFunctionCall %float %9 %10
2402 %231 = OpLoad %float %217
2403 %232 = OpFDiv %float %218 %231
2404 %233 = OpExtInst %float %1 SmoothStep %float_1_35631564en19 %float_1_35631564en19_0 %232
2405 OpReturnValue %233
2406 OpFunctionEnd
2407 )";
2408
2409 CompileSuccessfully(spirv);
2410 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2411 }
2412
2413 TEST_F(ValidateLogicalPointersTest, RecursiveCalls) {
2414 const std::string spirv = R"(
2415 OpCapability ClipDistance
2416 %1 = OpExtInstImport "GLSL.std.450"
2417 OpMemoryModel Logical GLSL450
2418 OpEntryPoint Fragment %2 " "
2419 OpExecutionMode %2 OriginUpperLeft
2420 %void = OpTypeVoid
2421 %32 = OpTypeFunction %void
2422 %float = OpTypeFloat 32
2423 %_ptr_Function_float = OpTypePointer Function %float
2424 %35 = OpTypeFunction %float %_ptr_Function_float
2425 %float_1_35631564en19 = OpConstant %float 1.35631564e-19
2426 %float_1_35631564en19_0 = OpConstant %float 1.35631564e-19
2427 %2 = OpFunction %void None %32
2428 %8447 = OpLabel
2429 OpUnreachable
2430 OpFunctionEnd
2431 %9 = OpFunction %float None %35
2432 %10 = OpFunctionParameter %_ptr_Function_float
2433 %65535 = OpLabel
2434 %217 = OpVariable %_ptr_Function_float Function
2435 %218 = OpLoad %float %10
2436 %2097407 = OpFunctionCall %float %20 %10
2437 %231 = OpLoad %float %217
2438 %232 = OpFDiv %float %218 %231
2439 %233 = OpExtInst %float %1 SmoothStep %float_1_35631564en19 %float_1_35631564en19_0 %232
2440 OpReturnValue %233
2441 OpFunctionEnd
2442 %20 = OpFunction %float None %35
2443 %21 = OpFunctionParameter %_ptr_Function_float
2444 %22 = OpLabel
2445 %23 = OpFunctionCall %float %9 %21
2446 OpReturnValue %float_1_35631564en19
2447 OpFunctionEnd
2448 )";
2449
2450 CompileSuccessfully(spirv);
2451 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2452 }
2453
2454 } // namespace
2455 } // namespace val
2456 } // namespace spvtools