forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc_spirv.cpp
More file actions
3640 lines (3325 loc) · 219 KB
/
cc_spirv.cpp
File metadata and controls
3640 lines (3325 loc) · 219 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2015-2026 The Khronos Group Inc.
* Copyright (c) 2015-2026 Valve Corporation
* Copyright (c) 2015-2026 LunarG, Inc.
* Copyright (C) 2015-2026 Google Inc.
* Copyright (c) 2025 Arm Limited.
* Modifications Copyright (C) 2020,2025-2026 Advanced Micro Devices, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cassert>
#include <cinttypes>
#include <cstdint>
#include <memory>
#include <spirv/unified1/spirv.hpp>
#include <sstream>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "containers/custom_containers.h"
#include "error_message/error_strings.h"
#include <vulkan/vk_enum_string_helper.h>
#include <vulkan/utility/vk_format_utils.h>
#include <vulkan/vulkan_core.h>
#include "core_checks/cc_vuid_maps.h"
#include "core_validation.h"
#include "generated/spirv_grammar_helper.h"
#include "generated/spirv_validation_helper.h"
#include "state_tracker/shader_instruction.h"
#include "state_tracker/shader_module.h"
#include "state_tracker/shader_stage_state.h"
#include "state_tracker/pipeline_state.h"
#include "utils/shader_utils.h"
#include "utils/hash_util.h"
#include "chassis/chassis_modification_state.h"
#include "state_tracker/descriptor_sets.h"
#include "state_tracker/descriptor_set_layouts.h"
#include "state_tracker/render_pass_state.h"
#include "spirv-tools/optimizer.hpp"
#include "containers/limits.h"
#include "containers/container_utils.h"
#include "utils/math_utils.h"
// Validate use of input attachments against subpass structure
bool CoreChecks::ValidateShaderInputAttachment(const spirv::Module &module_state, const ShaderStageState &stage_state,
const vvl::Pipeline &pipeline, const spirv::ResourceInterfaceVariable &variable,
const Location &loc) const {
bool skip = false;
assert(variable.is_input_attachment);
const auto &rp_state = pipeline.RenderPassState();
if (!rp_state) {
return skip;
}
auto print_index = [variable](uint32_t i) {
std::ostringstream ss;
if (variable.IsArray()) {
ss << variable.DescribeDescriptor() << " has an effective InputAttachmentIndex of " << i;
ss << " (started at InputAttachmentIndex " << variable.decorations.input_attachment_index_start << " plus index "
<< (i - variable.decorations.input_attachment_index_start) << " into the array)";
} else {
ss << variable.DescribeDescriptor() << " has an InputAttachmentIndex of "
<< variable.decorations.input_attachment_index_start;
}
return ss.str();
};
// VUID 06061 requires dynamicRenderingLocalRead and if they have, we can just check the colorAttachmentCount
if (rp_state->UsesDynamicRendering()) {
const uint32_t color_count = rp_state->dynamic_pipeline_rendering_create_info.colorAttachmentCount;
for (const auto i : variable.input_attachment_index_read) {
// offsets by the InputAttachmentIndex decoration
const uint32_t input_attachment_index = variable.decorations.input_attachment_index_start + i;
if (input_attachment_index >= color_count) {
const LogObjectList objlist(module_state.handle(), pipeline.Handle(), rp_state->Handle());
skip |= LogError("VUID-VkGraphicsPipelineCreateInfo-renderPass-09652", objlist, loc,
"%s which is not less than VkPipelineRenderingCreateInfo::colorAttachmentCount (%" PRIu32
")\nIf VkRenderingInputAttachmentIndexInfo is provided, the index can be set, but without it, it "
"uses the default index values.",
print_index(input_attachment_index).c_str(), color_count);
}
}
} else {
const auto rpci = rp_state->create_info.ptr();
const uint32_t subpass = pipeline.Subpass();
const auto subpass_description = rpci->pSubpasses[subpass];
const auto input_attachments = subpass_description.pInputAttachments;
for (const auto i : variable.input_attachment_index_read) {
// offsets by the InputAttachmentIndex decoration
const uint32_t input_attachment_index = variable.decorations.input_attachment_index_start + i;
// Same error, but provide more useful message 'how' VK_ATTACHMENT_UNUSED is derived
if (!input_attachments) {
const LogObjectList objlist(module_state.handle(), pipeline.Handle(), rp_state->Handle());
skip |= LogError("VUID-VkGraphicsPipelineCreateInfo-renderPass-06038", objlist, loc,
"%s but pSubpasses[%" PRIu32 "].pInputAttachments is NULL.",
print_index(input_attachment_index).c_str(), subpass);
} else if (input_attachment_index >= subpass_description.inputAttachmentCount) {
const LogObjectList objlist(module_state.handle(), pipeline.Handle(), rp_state->Handle());
skip |= LogError("VUID-VkGraphicsPipelineCreateInfo-renderPass-06038", objlist, loc,
"%s but that is not less than the pSubpasses[%" PRIu32 "].inputAttachmentCount (%" PRIu32 ").",
print_index(input_attachment_index).c_str(), subpass, subpass_description.inputAttachmentCount);
} else if (input_attachments[input_attachment_index].attachment == VK_ATTACHMENT_UNUSED) {
const LogObjectList objlist(module_state.handle(), pipeline.Handle(), rp_state->Handle());
skip |=
LogError("VUID-VkGraphicsPipelineCreateInfo-renderPass-06038", objlist, loc,
"%s but pSubpasses[%" PRIu32 "].pInputAttachments[%" PRIu32 "].attachment is VK_ATTACHMENT_UNUSED.",
print_index(input_attachment_index).c_str(), subpass, input_attachment_index);
}
}
}
return skip;
}
bool CoreChecks::ValidatePushConstantUsage(const spirv::Module &module_state, const spirv::EntryPoint &entrypoint,
const vvl::Pipeline *pipeline, const ShaderStageState &stage_state,
const Location &loc) const {
bool skip = false;
if (stage_state.descriptor_heap_mode) {
return skip;
} else if (module_state.static_data_.has_specialization_constants) {
// TODO - Workaround for https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5911
return skip;
}
const VkShaderStageFlagBits stage = entrypoint.stage;
const auto push_constant_variable = entrypoint.push_constant_variable;
if (!push_constant_variable) {
return skip;
}
PushConstantRangesId shader_object_push_constant_ranges_id;
std::vector<VkPushConstantRange> const *push_constant_ranges;
if (pipeline) {
push_constant_ranges = pipeline->PipelineLayoutState()->push_constant_ranges_layout.get();
} else {
shader_object_push_constant_ranges_id = GetCanonicalId(stage_state.shader_object_create_info->pushConstantRangeCount,
stage_state.shader_object_create_info->pPushConstantRanges);
push_constant_ranges = shader_object_push_constant_ranges_id.get();
}
if (!push_constant_ranges || push_constant_ranges->empty()) {
LogObjectList objlist(module_state.handle());
std::string msg = "";
if (pipeline) {
objlist.add(pipeline->PipelineLayoutState()->Handle());
msg = FormatHandle(pipeline->PipelineLayoutState()->Handle());
} else {
msg = "VkShaderCreateInfoEXT::pPushConstantRanges";
}
skip |= LogError(GetSpirvInterfaceVariableVUID(loc, vvl::SpirvInterfaceVariableError::PushConstantStage_07987), objlist,
loc, "SPIR-V (%s) is using push constants, but no VkPushConstantRange were found in %s.",
string_VkShaderStageFlags(stage).c_str(), msg.c_str());
return skip;
}
bool found_stage = false;
for (auto const &range : *push_constant_ranges) {
if (range.stageFlags & stage) {
found_stage = true;
const uint32_t range_end = range.offset + range.size;
const uint32_t push_constant_end = push_constant_variable->offset + push_constant_variable->size;
// spec: "If a push constant block is declared in a shader"
// Is checked regardless if element in Block is not statically used
if ((push_constant_variable->offset < range.offset) | (push_constant_end > range_end)) {
LogObjectList objlist(module_state.handle());
if (pipeline) {
objlist.add(pipeline->PipelineLayoutState()->Handle());
}
skip |= LogError(GetSpirvInterfaceVariableVUID(loc, vvl::SpirvInterfaceVariableError::PushConstantRange_10069),
objlist, loc,
"SPIR-V (%s) has a push constant buffer Block with range [%" PRIu32 ", %" PRIu32
"] which outside the VkPushConstantRange of [%" PRIu32 ", %" PRIu32 "].",
string_VkShaderStageFlags(stage).c_str(), push_constant_variable->offset, push_constant_end,
range.offset, range_end);
break;
}
}
}
if (!found_stage) {
LogObjectList objlist(module_state.handle());
std::stringstream ss;
ss << "SPIR-V (" << string_VkShaderStageFlags(stage) << ") is using push constants, but ";
if (pipeline) {
objlist.add(pipeline->PipelineLayoutState()->Handle());
ss << FormatHandle(pipeline->PipelineLayoutState()->Handle());
} else {
ss << "VkShaderCreateInfoEXT::pPushConstantRanges";
}
ss << " doesn't set any with " << string_VkShaderStageFlags(stage) << "\nCurrent VkPushConstantRange:";
for (auto const &range : *push_constant_ranges) {
ss << "\n - " << string_VkPushConstantRange(range);
}
skip |= LogError(GetSpirvInterfaceVariableVUID(loc, vvl::SpirvInterfaceVariableError::PushConstantStage_07987), objlist,
loc, "%s", ss.str().c_str());
}
return skip;
}
struct ShaderResourceType {
// All possible VkDescriptorSet
vvl::unordered_set<uint32_t> descriptor_type_set;
// Way to print out extra useful information
bool is_buffer_block{false};
bool HasType(VkDescriptorType type) { return descriptor_type_set.find(type) != descriptor_type_set.end(); }
std::string Describe(bool hints) {
std::ostringstream ss;
for (auto it = descriptor_type_set.begin(); it != descriptor_type_set.end(); ++it) {
if (ss.tellp()) ss << " or ";
ss << string_VkDescriptorType(VkDescriptorType(*it));
}
// Currently this is used for 2 checks
// - When there is no binding found at all
// - When it is found, but the mismatch, here we want to help give hints
if (hints) {
ss << "\nInfo on SPIR-V mapping for each type:";
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_SAMPLER)) {
ss << "\n - VK_DESCRIPTOR_TYPE_SAMPLER is an OpTypeSampler with UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
ss << "\n - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER is an OpTypeSampledImage that consumes both a OpTypeSampler "
"and OpTypeImage in UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE)) {
ss << "\n - VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE is an OpTypeImage, with Sampled = 1, in UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)) {
ss << "\n - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE is an OpTypeImage, with Sampled = 2, in UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER)) {
ss << "\n - VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER is an OpTypeImage, with Sampled = 1 and Dim = Buffer, in "
"UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) {
ss << "\n - VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER is an OpTypeImage, with Sampled = 2 and Dim = Buffer, in "
"UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)) {
ss << "\n - VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER/VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK is an OpTypeStruct as "
"Uniform";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
ss << "\n - VK_DESCRIPTOR_TYPE_STORAGE_BUFFER is an OpTypeStruct as ";
if (is_buffer_block) {
ss << "Uniform, with BufferBlock (Vulkan 1.0 didn't have a dedicated StorageBuffer storage class, more info at "
"https://docs.vulkan.org/guide/latest/extensions/"
"shader_features.html#VK_KHR_storage_buffer_storage_class)";
} else {
ss << "StorageBuffer";
}
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
ss << "\n - VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT is an OpTypeImage, with Sampled = 2 and Dim = SubpassData, in "
"UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)) {
ss << "\n - VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR is an OpTypeAccelerationStructureKHR in UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_PARTITIONED_ACCELERATION_STRUCTURE_NV)) {
ss << "\n - VK_DESCRIPTOR_TYPE_PARTITIONED_ACCELERATION_STRUCTURE_NV is an OpTypeAccelerationStructureKHR in "
"UniformConstant";
}
if (descriptor_type_set.count(VK_DESCRIPTOR_TYPE_TENSOR_ARM)) {
ss << "\n - VK_DESCRIPTOR_TYPE_TENSOR_ARM is an OpTypeTensorARM in UniformConstant";
}
ss << "\nFull list of mappings can be found at "
"https://docs.vulkan.org/spec/latest/chapters/interfaces.html#interfaces-resources-storage-class-correspondence";
}
return ss.str();
}
};
// This function is matching the VkDescriptorType to the SPIR-V.
// We return back a set, because things like a Uniform in SPIR-V could be one of many possible matching VkDescriptorType.
// https://docs.vulkan.org/spec/latest/chapters/interfaces.html#interfaces-resources-storage-class-correspondence
static void TypeToDescriptorTypeSet(const spirv::Module &module_state, uint32_t type_id, uint32_t data_type_id,
ShaderResourceType &out_data) {
const spirv::Instruction *type = module_state.FindDef(type_id);
assert(type->Opcode() == spv::OpTypePointer || type->Opcode() == spv::OpTypeUntypedPointerKHR);
bool is_storage_buffer = type->StorageClass() == spv::StorageClassStorageBuffer;
if (data_type_id != 0) {
type = module_state.FindDef(data_type_id);
}
// Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
while (type->IsArray() || type->Opcode() == spv::OpTypePointer) {
if (type->Opcode() == spv::OpTypeRuntimeArray) {
type = module_state.FindDef(type->Word(2));
} else if (type->Opcode() == spv::OpTypeArray) {
type = module_state.FindDef(type->Word(2));
} else {
if (type->StorageClass() == spv::StorageClassStorageBuffer) {
is_storage_buffer = true;
}
type = module_state.FindDef(type->Word(3));
}
}
switch (type->Opcode()) {
case spv::OpTypeStruct: {
for (const spirv::Instruction *insn : module_state.static_data_.decoration_inst) {
if (insn->Word(1) == type->ResultId()) {
if (insn->Word(2) == spv::DecorationBlock) {
if (is_storage_buffer) {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
} else {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK);
}
} else if (insn->Word(2) == spv::DecorationBufferBlock) {
out_data.is_buffer_block = true;
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
}
break;
}
}
return;
}
case spv::OpTypeSampler:
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
return;
case spv::OpTypeSampledImage: {
// Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
// buffer descriptor doesn't really provide one. Allow this slight mismatch.
const spirv::Instruction *image_type = module_state.FindDef(type->Word(2));
auto dim = image_type->Word(3);
auto sampled = image_type->Word(7);
if (dim == spv::DimBuffer && sampled == 1) {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
} else {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
}
return;
}
case spv::OpTypeImage: {
// Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
// SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
auto dim = type->Word(3);
auto sampled = type->Word(7);
if (dim == spv::DimSubpassData) {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
} else if (dim == spv::DimBuffer) {
if (sampled == 1) {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
} else {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
}
} else if (sampled == 1) {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
} else {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
}
return;
}
// The OpType are alias, but the Descriptor Types are different
case spv::OpTypeAccelerationStructureKHR:
// Only KHR or NV base acceleration structure is selected
if (module_state.HasCapability(spv::CapabilityRayTracingNV)) {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
} else {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR);
}
// Additionally allow PTLAS if shader uses cluster acceleration structure features
if (module_state.HasCapability(spv::CapabilityRayTracingClusterAccelerationStructureNV)) {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_PARTITIONED_ACCELERATION_STRUCTURE_NV);
}
return;
case spv::OpTypeTensorARM: {
out_data.descriptor_type_set.insert(VK_DESCRIPTOR_TYPE_TENSOR_ARM);
return;
}
default:
// We shouldn't really see any other junk types -- but if we do, they're a mismatch.
return; // Matches nothing
}
}
// Map SPIR-V type to VK_COMPONENT_TYPE enum
VkComponentTypeKHR GetComponentType(const spirv::Instruction *insn, bool is_signed_int) {
if (insn->Opcode() == spv::OpTypeInt) {
switch (insn->Word(2)) {
case 8:
return is_signed_int ? VK_COMPONENT_TYPE_SINT8_KHR : VK_COMPONENT_TYPE_UINT8_KHR;
case 16:
return is_signed_int ? VK_COMPONENT_TYPE_SINT16_KHR : VK_COMPONENT_TYPE_UINT16_KHR;
case 32:
return is_signed_int ? VK_COMPONENT_TYPE_SINT32_KHR : VK_COMPONENT_TYPE_UINT32_KHR;
case 64:
return is_signed_int ? VK_COMPONENT_TYPE_SINT64_KHR : VK_COMPONENT_TYPE_UINT64_KHR;
default:
return VK_COMPONENT_TYPE_MAX_ENUM_KHR;
}
} else if (insn->Opcode() == spv::OpTypeFloat) {
switch (insn->Word(2)) {
case 8: {
assert(insn->Length() > 3); // all float8 have an encoding
const uint32_t encoding = insn->Word(3);
if (encoding == spv::FPEncodingFloat8E4M3EXT) {
return VK_COMPONENT_TYPE_FLOAT8_E4M3_EXT;
} else if (encoding == spv::FPEncodingFloat8E5M2EXT) {
return VK_COMPONENT_TYPE_FLOAT8_E5M2_EXT;
} else {
assert(false); // New float8 encoding
}
} break;
case 16: {
if (insn->Length() > 3) {
const uint32_t encoding = insn->Word(3);
if (encoding == spv::FPEncodingBFloat16KHR) {
return VK_COMPONENT_TYPE_BFLOAT16_KHR;
} else {
assert(false); // New float16 encoding
}
} else {
return VK_COMPONENT_TYPE_FLOAT16_KHR;
}
} break;
case 32:
return VK_COMPONENT_TYPE_FLOAT32_KHR;
case 64:
return VK_COMPONENT_TYPE_FLOAT64_KHR;
default:
return VK_COMPONENT_TYPE_MAX_ENUM_KHR;
}
}
return VK_COMPONENT_TYPE_MAX_ENUM_KHR;
}
static bool IsSignedIntEnum(const VkComponentTypeKHR component_type) {
switch (component_type) {
case VK_COMPONENT_TYPE_SINT8_KHR:
case VK_COMPONENT_TYPE_SINT16_KHR:
case VK_COMPONENT_TYPE_SINT32_KHR:
case VK_COMPONENT_TYPE_SINT64_KHR:
return true;
default:
return false;
}
}
// Validate SPV_KHR_cooperative_matrix (and SPV_NV_cooperative_matrix) behavior that can't be statically validated in SPIRV-Tools
// (e.g. due to specialization constant usage).
bool CoreChecks::ValidateCooperativeMatrix(const spirv::Module &module_state, const spirv::EntryPoint &entrypoint,
const ShaderStageState &stage_state, const spirv::LocalSize &local_size,
const Location &loc) const {
bool skip = false;
const uint64_t workgroup_size = local_size.x * local_size.y * local_size.z;
uint32_t effective_subgroup_size = phys_dev_props_core11.subgroupSize;
if (const auto *required_subgroup_size_ci =
vku::FindStructInPNextChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfo>(stage_state.GetPNext())) {
effective_subgroup_size = required_subgroup_size_ci->requiredSubgroupSize;
}
const auto &IsSignedIntType = [&module_state](const uint32_t type_id) {
const spirv::Instruction *type = module_state.FindDef(type_id);
if (type->Opcode() == spv::OpTypeCooperativeMatrixKHR || type->Opcode() == spv::OpTypeCooperativeMatrixNV) {
type = module_state.FindDef(type->Word(2));
}
return type->Opcode() == spv::OpTypeInt && type->Word(3) != 0;
};
struct CoopMatType {
VkScopeKHR scope;
uint32_t rows;
uint32_t cols;
VkComponentTypeKHR component_type;
uint32_t use;
bool all_constant;
CoopMatType(uint32_t id, const spirv::Module &module_state, bool is_signed_int) {
const spirv::Instruction *insn = module_state.FindDef(id);
const spirv::Instruction *component_type_insn = module_state.FindDef(insn->Word(2));
const spirv::Instruction *scope_insn = module_state.FindDef(insn->Word(3));
const spirv::Instruction *rows_insn = module_state.FindDef(insn->Word(4));
const spirv::Instruction *cols_insn = module_state.FindDef(insn->Word(5));
all_constant = true;
uint32_t tmp_scope = 0;
if (!module_state.GetInt32IfConstant(*scope_insn, &tmp_scope)) {
all_constant = false;
}
scope = VkScopeKHR(tmp_scope);
if (!module_state.GetInt32IfConstant(*rows_insn, &rows)) {
all_constant = false;
}
if (!module_state.GetInt32IfConstant(*cols_insn, &cols)) {
all_constant = false;
}
component_type = GetComponentType(component_type_insn, is_signed_int);
if (insn->Opcode() == spv::OpTypeCooperativeMatrixKHR) {
const spirv::Instruction *use_insn = module_state.FindDef(insn->Word(6));
if (!module_state.GetInt32IfConstant(*use_insn, &use)) {
all_constant = false;
}
}
}
std::string Describe() {
std::ostringstream ss;
ss << "rows: " << rows << ", cols: " << cols << ", scope: " << string_VkScopeKHR(scope)
<< ", type: " << string_VkComponentTypeKHR(component_type) << ", use: " << use;
return ss.str();
}
};
if (module_state.HasCapability(spv::CapabilityCooperativeMatrixKHR)) {
if (!(entrypoint.stage & phys_dev_ext_props.cooperative_matrix_props_khr.cooperativeMatrixSupportedStages)) {
skip |=
LogError("VUID-RuntimeSpirv-cooperativeMatrixSupportedStages-08985", module_state.handle(), loc,
"SPIR-V contains OpTypeCooperativeMatrixKHR used in shader stage %s but is not in "
"cooperativeMatrixSupportedStages (%s)",
string_VkShaderStageFlagBits(entrypoint.stage),
string_VkShaderStageFlags(phys_dev_ext_props.cooperative_matrix_props_khr.cooperativeMatrixSupportedStages)
.c_str());
}
} else if (module_state.HasCapability(spv::CapabilityCooperativeMatrixNV)) {
if (!(entrypoint.stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
skip |= LogError(
"VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322", module_state.handle(), loc,
"SPIR-V contains OpTypeCooperativeMatrixNV used in shader stage %s but is not in cooperativeMatrixSupportedStages "
"(%s)",
string_VkShaderStageFlagBits(entrypoint.stage),
string_VkShaderStageFlags(phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages).c_str());
}
} else {
return skip; // If the capability isn't enabled, don't bother with the rest of this function.
}
if (!module_state.static_data_.cooperative_matrix_inst.empty() && api_version < VK_API_VERSION_1_3) {
bool has_full_subgroups = false;
if (stage_state.pipeline_create_info) {
has_full_subgroups =
stage_state.pipeline_create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT;
} else {
has_full_subgroups = stage_state.shader_object_create_info->flags & VK_SHADER_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT;
}
if (!has_full_subgroups) {
const char *vuid = stage_state.HasPipeline() ? "VUID-RuntimeSpirv-OpTypeCooperativeMatrixKHR-10770"
: "VUID-RuntimeSpirv-OpTypeCooperativeMatrixKHR-10771";
skip |= LogError(vuid, module_state.handle(), loc,
"SPIR-V (%s) contains SPV_KHR_cooperative_matrix which requires SPIR-V 1.6 (Vulkan 1.3). In order to "
"use it with older versions, you need to use %s (which requires VK_EXT_subgroup_size_control).",
string_VkShaderStageFlagBits(entrypoint.stage),
stage_state.HasPipeline() ? "VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT"
: "VK_SHADER_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT");
}
}
// Map SPIR-V result ID to the ID of its type.
// TODO - Should have more robust way in ModuleState to find the type
vvl::unordered_map<uint32_t, uint32_t> id_to_type_id;
for (const spirv::Instruction &insn : module_state.GetInstructions()) {
if (OpcodeHasType(insn.Opcode()) && OpcodeHasResult(insn.Opcode())) {
id_to_type_id[insn.Word(2)] = insn.Word(1);
}
}
auto print_properties = [this]() {
std::ostringstream ss;
for (uint32_t i = 0; i < device_state->cooperative_matrix_properties_khr.size(); ++i) {
const auto &prop = device_state->cooperative_matrix_properties_khr[i];
ss << "[" << i << "] MSize = " << prop.MSize << " | NSize = " << prop.NSize << " | KSize = " << prop.KSize
<< " | AType = " << string_VkComponentTypeKHR(prop.AType) << " | BType = " << string_VkComponentTypeKHR(prop.BType)
<< " | CType = " << string_VkComponentTypeKHR(prop.CType)
<< " | ResultType = " << string_VkComponentTypeKHR(prop.ResultType) << " | scope = " << string_VkScopeKHR(prop.scope)
<< '\n';
}
return ss.str();
};
auto print_flexible_properties = [this]() {
std::ostringstream ss;
for (uint32_t i = 0; i < device_state->cooperative_matrix_flexible_dimensions_properties.size(); ++i) {
const auto &prop = device_state->cooperative_matrix_flexible_dimensions_properties[i];
ss << "[" << i << "] MGranularity = " << prop.MGranularity << " | NGranularity = " << prop.NGranularity
<< " | KGranularity = " << prop.KGranularity << " | AType = " << string_VkComponentTypeKHR(prop.AType)
<< " | BType = " << string_VkComponentTypeKHR(prop.BType) << " | CType = " << string_VkComponentTypeKHR(prop.CType)
<< " | ResultType = " << string_VkComponentTypeKHR(prop.ResultType) << " | scope = " << string_VkScopeKHR(prop.scope)
<< " | workgroupInvocations = " << prop.workgroupInvocations << '\n';
}
return ss.str();
};
for (const spirv::Instruction *cooperative_matrix_inst : module_state.static_data_.cooperative_matrix_inst) {
const spirv::Instruction &insn = *cooperative_matrix_inst;
switch (insn.Opcode()) {
case spv::OpTypeCooperativeMatrixKHR: {
CoopMatType m(insn.ResultId(), module_state, IsSignedIntType(insn.Word(2)));
if ((entrypoint.stage & VK_SHADER_STAGE_COMPUTE_BIT) != 0) {
if (!IsIntegerMultipleOf(local_size.x, effective_subgroup_size)) {
const auto vuid_string = m.scope == VK_SCOPE_SUBGROUP_KHR
? "VUID-VkPipelineShaderStageCreateInfo-module-08987"
: "VUID-VkPipelineShaderStageCreateInfo-module-10169";
skip |= LogError(vuid_string, module_state.handle(), loc,
"SPIR-V (compute stage) Local workgroup size in the X dimension (%" PRIu32
") is not a multiple of subgroupSize (%" PRIu32 ").",
local_size.x, effective_subgroup_size);
}
if (m.scope == VK_SCOPE_WORKGROUP_KHR) {
if (workgroup_size >
phys_dev_ext_props.cooperative_matrix_props2_nv.cooperativeMatrixWorkgroupScopeMaxWorkgroupSize) {
skip |= LogError(
"VUID-VkPipelineShaderStageCreateInfo-module-10169", module_state.handle(), loc,
"SPIR-V (compute stage) Total local workgroup size (%" PRIu64
") is larger than cooperativeMatrixWorkgroupScopeMaxWorkgroupSize (%" PRIu32 ").",
workgroup_size,
phys_dev_ext_props.cooperative_matrix_props2_nv.cooperativeMatrixWorkgroupScopeMaxWorkgroupSize);
}
}
}
if (!m.all_constant) {
break;
}
if (m.scope == VK_SCOPE_WORKGROUP_KHR && !enabled_features.cooperativeMatrixWorkgroupScope) {
skip |= LogError("VUID-RuntimeSpirv-cooperativeMatrixWorkgroupScope-10164", module_state.handle(), loc,
"SPIR-V (compute stage) Cooperative matrix uses workgroup scope but "
"cooperativeMatrixWorkgroupScope is not enabled.");
}
// Validate that the type parameters are all supported for one of the
// operands of a cooperative matrix khr property.
bool valid = false;
for (uint32_t i = 0; i < device_state->cooperative_matrix_properties_khr.size(); ++i) {
const auto &property = device_state->cooperative_matrix_properties_khr[i];
if (property.AType == m.component_type && property.MSize == m.rows && property.KSize == m.cols &&
property.scope == m.scope && m.use == spv::CooperativeMatrixUseMatrixAKHR) {
valid = true;
break;
}
if (property.BType == m.component_type && property.KSize == m.rows && property.NSize == m.cols &&
property.scope == m.scope && m.use == spv::CooperativeMatrixUseMatrixBKHR) {
valid = true;
break;
}
if (property.CType == m.component_type && property.MSize == m.rows && property.NSize == m.cols &&
property.scope == m.scope && m.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR) {
valid = true;
break;
}
if (property.ResultType == m.component_type && property.MSize == m.rows && property.NSize == m.cols &&
property.scope == m.scope && m.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR) {
valid = true;
break;
}
}
if (enabled_features.cooperativeMatrixFlexibleDimensions) {
for (uint32_t i = 0; i < device_state->cooperative_matrix_flexible_dimensions_properties.size(); ++i) {
const auto &property = device_state->cooperative_matrix_flexible_dimensions_properties[i];
if (property.scope == VK_SCOPE_WORKGROUP_KHR && workgroup_size != property.workgroupInvocations) {
continue;
}
if (property.AType == m.component_type && IsIntegerMultipleOf(m.rows, property.MGranularity) &&
IsIntegerMultipleOf(m.cols, property.KGranularity) && property.scope == m.scope &&
m.use == spv::CooperativeMatrixUseMatrixAKHR) {
valid = true;
break;
}
if (property.BType == m.component_type && IsIntegerMultipleOf(m.rows, property.KGranularity) &&
IsIntegerMultipleOf(m.cols, property.NGranularity) && property.scope == m.scope &&
m.use == spv::CooperativeMatrixUseMatrixBKHR) {
valid = true;
break;
}
if (property.CType == m.component_type && IsIntegerMultipleOf(m.rows, property.MGranularity) &&
IsIntegerMultipleOf(m.cols, property.NGranularity) && property.scope == m.scope &&
m.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR) {
valid = true;
break;
}
if (property.ResultType == m.component_type && IsIntegerMultipleOf(m.rows, property.MGranularity) &&
IsIntegerMultipleOf(m.cols, property.NGranularity) && property.scope == m.scope &&
m.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR) {
valid = true;
break;
}
}
}
if (!valid) {
if (!enabled_features.cooperativeMatrixFlexibleDimensions) {
skip |= LogError("VUID-RuntimeSpirv-OpTypeCooperativeMatrixKHR-10163", module_state.handle(), loc,
"SPIR-V (%s) has\n%s (%s)\nbut doesn't match any VkCooperativeMatrixPropertiesKHR\n%s.",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Describe().c_str(),
m.Describe().c_str(), print_properties().c_str());
} else {
skip |= LogError("VUID-RuntimeSpirv-cooperativeMatrixFlexibleDimensions-10165", module_state.handle(), loc,
"SPIR-V (%s) has\n%s (%s)\nbut doesn't match any VkCooperativeMatrixPropertiesKHR or "
"VkCooperativeMatrixFlexibleDimensionsPropertiesNV\n%s\n%s.",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Describe().c_str(),
m.Describe().c_str(), print_properties().c_str(), print_flexible_properties().c_str());
}
}
if (IsExtEnabled(extensions.vk_nv_cooperative_matrix2)) {
if (m.rows > phys_dev_ext_props.cooperative_matrix_props2_nv.cooperativeMatrixFlexibleDimensionsMaxDimension ||
m.cols > phys_dev_ext_props.cooperative_matrix_props2_nv.cooperativeMatrixFlexibleDimensionsMaxDimension) {
skip |= LogError(
"VUID-RuntimeSpirv-cooperativeMatrixFlexibleDimensionsMaxDimension-10167", module_state.handle(), loc,
"SPIR-V (%s) has\n%s (%s)\nbut number of rows or columns is greater than "
"cooperativeMatrixFlexibleDimensionsMaxDimension (%" PRIu32 ").",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Describe().c_str(), m.Describe().c_str(),
phys_dev_ext_props.cooperative_matrix_props2_nv.cooperativeMatrixFlexibleDimensionsMaxDimension);
}
}
break;
}
case spv::OpCooperativeMatrixMulAddKHR: {
const uint32_t flags = insn.Length() > 6 ? insn.Word(6) : 0u;
CoopMatType r(id_to_type_id[insn.Word(2)], module_state,
(flags & spv::CooperativeMatrixOperandsMatrixResultSignedComponentsKHRMask));
CoopMatType a(id_to_type_id[insn.Word(3)], module_state,
(flags & spv::CooperativeMatrixOperandsMatrixASignedComponentsKHRMask));
CoopMatType b(id_to_type_id[insn.Word(4)], module_state,
(flags & spv::CooperativeMatrixOperandsMatrixBSignedComponentsKHRMask));
CoopMatType c(id_to_type_id[insn.Word(5)], module_state,
(flags & spv::CooperativeMatrixOperandsMatrixCSignedComponentsKHRMask));
if (a.all_constant && b.all_constant && c.all_constant && r.all_constant) {
// Validate that the type parameters are all supported for the same
// cooperative matrix property.
bool found_matching_prop = false;
for (uint32_t i = 0; i < device_state->cooperative_matrix_properties_khr.size(); ++i) {
const auto &property = device_state->cooperative_matrix_properties_khr[i];
bool valid = true;
valid &= property.AType == a.component_type && property.MSize == a.rows && property.KSize == a.cols &&
property.scope == a.scope && a.use == spv::CooperativeMatrixUseMatrixAKHR;
valid &= property.BType == b.component_type && property.KSize == b.rows && property.NSize == b.cols &&
property.scope == b.scope && b.use == spv::CooperativeMatrixUseMatrixBKHR;
valid &= property.CType == c.component_type && property.MSize == c.rows && property.NSize == c.cols &&
property.scope == c.scope && c.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR;
valid &= property.ResultType == r.component_type && property.MSize == r.rows && property.NSize == r.cols &&
property.scope == r.scope && r.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR;
valid &= !IsSignedIntEnum(property.AType) ||
(flags & spv::CooperativeMatrixOperandsMatrixASignedComponentsKHRMask);
valid &= !IsSignedIntEnum(property.BType) ||
(flags & spv::CooperativeMatrixOperandsMatrixBSignedComponentsKHRMask);
valid &= !IsSignedIntEnum(property.CType) ||
(flags & spv::CooperativeMatrixOperandsMatrixCSignedComponentsKHRMask);
valid &= !IsSignedIntEnum(property.ResultType) ||
(flags & spv::CooperativeMatrixOperandsMatrixResultSignedComponentsKHRMask);
valid &= property.saturatingAccumulation ==
!!(flags & spv::CooperativeMatrixOperandsSaturatingAccumulationKHRMask);
if (valid) {
found_matching_prop = true;
break;
}
}
bool found_matching_flexible_prop = false;
if (enabled_features.cooperativeMatrixFlexibleDimensions) {
for (uint32_t i = 0; i < device_state->cooperative_matrix_flexible_dimensions_properties.size(); ++i) {
const auto &property = device_state->cooperative_matrix_flexible_dimensions_properties[i];
bool valid = true;
valid &= property.AType == a.component_type && IsIntegerMultipleOf(a.rows, property.MGranularity) &&
IsIntegerMultipleOf(a.cols, property.KGranularity) && property.scope == a.scope &&
a.use == spv::CooperativeMatrixUseMatrixAKHR;
valid &= property.BType == b.component_type && IsIntegerMultipleOf(b.rows, property.KGranularity) &&
IsIntegerMultipleOf(b.cols, property.NGranularity) && property.scope == b.scope &&
b.use == spv::CooperativeMatrixUseMatrixBKHR;
valid &= property.CType == c.component_type && IsIntegerMultipleOf(c.rows, property.MGranularity) &&
IsIntegerMultipleOf(c.cols, property.NGranularity) && property.scope == c.scope &&
c.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR;
valid &= property.ResultType == r.component_type &&
IsIntegerMultipleOf(r.rows, property.MGranularity) &&
IsIntegerMultipleOf(r.cols, property.NGranularity) && property.scope == r.scope &&
r.use == spv::CooperativeMatrixUseMatrixAccumulatorKHR;
valid &= !IsSignedIntEnum(property.AType) ||
(flags & spv::CooperativeMatrixOperandsMatrixASignedComponentsKHRMask);
valid &= !IsSignedIntEnum(property.BType) ||
(flags & spv::CooperativeMatrixOperandsMatrixBSignedComponentsKHRMask);
valid &= !IsSignedIntEnum(property.CType) ||
(flags & spv::CooperativeMatrixOperandsMatrixCSignedComponentsKHRMask);
valid &= !IsSignedIntEnum(property.ResultType) ||
(flags & spv::CooperativeMatrixOperandsMatrixResultSignedComponentsKHRMask);
valid &= property.saturatingAccumulation ==
!!(flags & spv::CooperativeMatrixOperandsSaturatingAccumulationKHRMask);
valid &= property.scope != VK_SCOPE_WORKGROUP_KHR || workgroup_size == property.workgroupInvocations;
if (valid) {
found_matching_flexible_prop = true;
break;
}
}
}
if (!found_matching_prop && !found_matching_flexible_prop) {
if (!enabled_features.cooperativeMatrixFlexibleDimensions) {
skip |= LogError("VUID-RuntimeSpirv-OpCooperativeMatrixMulAddKHR-10060", module_state.handle(), loc,
"SPIR-V (%s) instruction\n%s\ndoesn't match a supported matrix "
"VkCooperativeMatrixPropertiesKHR\n%s\n%s\n%s\n%s\n%s\n",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Describe().c_str(),
a.Describe().c_str(), b.Describe().c_str(), c.Describe().c_str(), r.Describe().c_str(),
print_properties().c_str());
} else {
skip |=
LogError("VUID-RuntimeSpirv-cooperativeMatrixFlexibleDimensions-10166", module_state.handle(), loc,
"SPIR-V (%s) instruction\n%s\ndoesn't match a supported matrix "
"VkCooperativeMatrixPropertiesKHR or "
"VkPhysicalDeviceCooperativeMatrix2PropertiesNV\n%s\n%s\n%s\n%s\n%s\n%s\n",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Describe().c_str(),
a.Describe().c_str(), b.Describe().c_str(), c.Describe().c_str(), r.Describe().c_str(),
print_properties().c_str(), print_flexible_properties().c_str());
}
}
}
break;
}
case spv::OpTypeCooperativeMatrixNV: {
CoopMatType m(insn.ResultId(), module_state, IsSignedIntType(insn.Word(2)));
if (!m.all_constant) {
break;
}
// Validate that the type parameters are all supported for one of the
// operands of a cooperative matrix property.
bool valid = false;
for (uint32_t i = 0; i < device_state->cooperative_matrix_properties_nv.size(); ++i) {
const auto &property = device_state->cooperative_matrix_properties_nv[i];
if (property.AType == m.component_type && property.MSize == m.rows && property.KSize == m.cols &&
property.scope == m.scope) {
valid = true;
break;
}
if (property.BType == m.component_type && property.KSize == m.rows && property.NSize == m.cols &&
property.scope == m.scope) {
valid = true;
break;
}
if (property.CType == m.component_type && property.MSize == m.rows && property.NSize == m.cols &&
property.scope == m.scope) {
valid = true;
break;
}
if (property.DType == m.component_type && property.MSize == m.rows && property.NSize == m.cols &&
property.scope == m.scope) {
valid = true;
break;
}
}
if (!valid) {
skip |= LogError("VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06316", module_state.handle(), loc,
"SPIR-V (%s) has an OpTypeCooperativeMatrixNV (result id = %" PRIu32
") operand that don't match a supported matrix type (%s).",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Word(1), m.Describe().c_str());
}
break;
}
case spv::OpCooperativeMatrixMulAddNV: {
CoopMatType d(id_to_type_id[insn.Word(2)], module_state, IsSignedIntType(id_to_type_id[insn.Word(2)]));
CoopMatType a(id_to_type_id[insn.Word(3)], module_state, IsSignedIntType(id_to_type_id[insn.Word(3)]));
CoopMatType b(id_to_type_id[insn.Word(4)], module_state, IsSignedIntType(id_to_type_id[insn.Word(4)]));
CoopMatType c(id_to_type_id[insn.Word(5)], module_state, IsSignedIntType(id_to_type_id[insn.Word(5)]));
if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
// Validate that the type parameters are all supported for the same
// cooperative matrix property.
bool valid_a = false;
bool valid_b = false;
bool valid_c = false;
bool valid_d = false;
for (uint32_t i = 0; i < device_state->cooperative_matrix_properties_nv.size(); ++i) {
const auto &property = device_state->cooperative_matrix_properties_nv[i];
valid_a |= property.AType == a.component_type && property.MSize == a.rows && property.KSize == a.cols &&
property.scope == a.scope;
valid_b |= property.BType == b.component_type && property.KSize == b.rows && property.NSize == b.cols &&
property.scope == b.scope;
valid_c |= property.CType == c.component_type && property.MSize == c.rows && property.NSize == c.cols &&
property.scope == c.scope;
valid_d |= property.DType == d.component_type && property.MSize == d.rows && property.NSize == d.cols &&
property.scope == d.scope;
if (valid_a && valid_b && valid_c && valid_d) {
break;
}
}
if (!valid_a) {
skip |= LogError("VUID-RuntimeSpirv-OpTypeCooperativeMatrixMulAddNV-10059", module_state.handle(), loc,
"SPIR-V (%s) OpCooperativeMatrixMulAddNV (result id = %" PRIu32
") operands don't match a supported matrix "
"VkCooperativeMatrixPropertiesNV for A type (%s).",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Word(2), a.Describe().c_str());
} else if (!valid_b) {
skip |= LogError("VUID-RuntimeSpirv-OpTypeCooperativeMatrixMulAddNV-10059", module_state.handle(), loc,
"SPIR-V (%s) OpCooperativeMatrixMulAddNV (result id = %" PRIu32
") operands don't match a supported matrix "
"VkCooperativeMatrixPropertiesNV for B type (%s).",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Word(2), b.Describe().c_str());
} else if (!valid_c) {
skip |= LogError("VUID-RuntimeSpirv-OpTypeCooperativeMatrixMulAddNV-10059", module_state.handle(), loc,
"SPIR-V (%s) OpCooperativeMatrixMulAddNV (result id = %" PRIu32
") operands don't match a supported matrix "
"VkCooperativeMatrixPropertiesNV for C type (%s).",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Word(2), c.Describe().c_str());
} else if (!valid_d) {
skip |= LogError("VUID-RuntimeSpirv-OpTypeCooperativeMatrixMulAddNV-10059", module_state.handle(), loc,
"SPIR-V (%s) OpCooperativeMatrixMulAddNV (result id = %" PRIu32
") operands don't match a supported matrix "
"VkCooperativeMatrixPropertiesNV for D type (%s).",
string_VkShaderStageFlagBits(entrypoint.stage), insn.Word(2), d.Describe().c_str());
}
}
break;
}
default:
break;
}
}
return skip;
}
bool CoreChecks::ValidateCooperativeVector(const spirv::Module &module_state, const spirv::EntryPoint &entrypoint,
const Location &loc) const {
bool skip = false;
struct CoopVecType {
VkComponentTypeKHR component_type;
uint32_t component_count;
bool all_constant;
CoopVecType(uint32_t id, const spirv::Module &module_state, bool is_signed) {
const spirv::Instruction *insn = module_state.FindDef(id);
const spirv::Instruction *component_type_insn = module_state.FindDef(insn->Word(2));
const spirv::Instruction *component_count_insn = module_state.FindDef(insn->Word(3));
all_constant = true;
if (!module_state.GetInt32IfConstant(*component_count_insn, &component_count)) {
all_constant = false;
}
component_type = GetComponentType(component_type_insn, is_signed);
}
std::string Describe() {
std::ostringstream ss;
ss << "component count: " << component_count << ", type: " << string_VkComponentTypeKHR(component_type);
return ss.str();
}
};
if (module_state.HasCapability(spv::CapabilityCooperativeVectorNV) ||
module_state.HasCapability(spv::CapabilityCooperativeVectorTrainingNV)) {
if (!(entrypoint.stage & phys_dev_ext_props.cooperative_vector_props_nv.cooperativeVectorSupportedStages)) {
skip |= LogError(
"VUID-RuntimeSpirv-cooperativeVectorSupportedStages-10091", module_state.handle(), loc,
"SPIR-V contains cooperative vector capability used in shader stage %s but is not in "
"cooperativeVectorSupportedStages (%s)",
string_VkShaderStageFlagBits(entrypoint.stage),
string_VkShaderStageFlags(phys_dev_ext_props.cooperative_vector_props_nv.cooperativeVectorSupportedStages).c_str());
}
} else {
return skip;
}
vvl::unordered_map<uint32_t, uint32_t> id_to_type_id;
for (const spirv::Instruction &insn : module_state.GetInstructions()) {
if (OpcodeHasType(insn.Opcode()) && OpcodeHasResult(insn.Opcode())) {
id_to_type_id[insn.Word(2)] = insn.Word(1);
}