-
Notifications
You must be signed in to change notification settings - Fork 580
/
Copy pathvalidation_state.cpp
2560 lines (2293 loc) · 88 KB
/
validation_state.cpp
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-2016 The Khronos Group Inc.
// Modifications Copyright (C) 2024 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 "source/val/validation_state.h"
#include <cassert>
#include <stack>
#include <utility>
#include "source/opcode.h"
#include "source/spirv_constant.h"
#include "source/spirv_target_env.h"
#include "source/util/make_unique.h"
#include "source/val/basic_block.h"
#include "source/val/construct.h"
#include "source/val/function.h"
#include "spirv-tools/libspirv.h"
namespace spvtools {
namespace val {
namespace {
ModuleLayoutSection InstructionLayoutSection(
ModuleLayoutSection current_section, spv::Op op) {
// See Section 2.4
if (spvOpcodeGeneratesType(op) || spvOpcodeIsConstant(op))
return kLayoutTypes;
switch (op) {
case spv::Op::OpCapability:
return kLayoutCapabilities;
case spv::Op::OpExtension:
return kLayoutExtensions;
case spv::Op::OpExtInstImport:
return kLayoutExtInstImport;
case spv::Op::OpMemoryModel:
return kLayoutMemoryModel;
case spv::Op::OpEntryPoint:
return kLayoutEntryPoint;
case spv::Op::OpExecutionMode:
case spv::Op::OpExecutionModeId:
return kLayoutExecutionMode;
case spv::Op::OpSourceContinued:
case spv::Op::OpSource:
case spv::Op::OpSourceExtension:
case spv::Op::OpString:
return kLayoutDebug1;
case spv::Op::OpName:
case spv::Op::OpMemberName:
return kLayoutDebug2;
case spv::Op::OpModuleProcessed:
return kLayoutDebug3;
case spv::Op::OpDecorate:
case spv::Op::OpMemberDecorate:
case spv::Op::OpGroupDecorate:
case spv::Op::OpGroupMemberDecorate:
case spv::Op::OpDecorationGroup:
case spv::Op::OpDecorateId:
case spv::Op::OpDecorateStringGOOGLE:
case spv::Op::OpMemberDecorateStringGOOGLE:
return kLayoutAnnotations;
case spv::Op::OpTypeForwardPointer:
return kLayoutTypes;
case spv::Op::OpVariable:
case spv::Op::OpUntypedVariableKHR:
if (current_section == kLayoutTypes) return kLayoutTypes;
return kLayoutFunctionDefinitions;
case spv::Op::OpExtInst:
case spv::Op::OpExtInstWithForwardRefsKHR:
// spv::Op::OpExtInst is only allowed in types section for certain
// extended instruction sets. This will be checked separately.
if (current_section == kLayoutTypes) return kLayoutTypes;
return kLayoutFunctionDefinitions;
case spv::Op::OpLine:
case spv::Op::OpNoLine:
case spv::Op::OpUndef:
if (current_section == kLayoutTypes) return kLayoutTypes;
return kLayoutFunctionDefinitions;
case spv::Op::OpFunction:
case spv::Op::OpFunctionParameter:
case spv::Op::OpFunctionEnd:
if (current_section == kLayoutFunctionDeclarations)
return kLayoutFunctionDeclarations;
return kLayoutFunctionDefinitions;
case spv::Op::OpSamplerImageAddressingModeNV:
return kLayoutSamplerImageAddressMode;
default:
break;
}
return kLayoutFunctionDefinitions;
}
bool IsInstructionInLayoutSection(ModuleLayoutSection layout, spv::Op op) {
return layout == InstructionLayoutSection(layout, op);
}
// Counts the number of instructions and functions in the file.
spv_result_t CountInstructions(void* user_data,
const spv_parsed_instruction_t* inst) {
ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data));
if (spv::Op(inst->opcode) == spv::Op::OpFunction) {
_.increment_total_functions();
}
_.increment_total_instructions();
return SPV_SUCCESS;
}
spv_result_t setHeader(void* user_data, spv_endianness_t, uint32_t,
uint32_t version, uint32_t generator, uint32_t id_bound,
uint32_t) {
ValidationState_t& vstate =
*(reinterpret_cast<ValidationState_t*>(user_data));
vstate.setIdBound(id_bound);
vstate.setGenerator(generator);
vstate.setVersion(version);
return SPV_SUCCESS;
}
// Add features based on SPIR-V core version number.
void UpdateFeaturesBasedOnSpirvVersion(ValidationState_t::Feature* features,
uint32_t version) {
assert(features);
if (version >= SPV_SPIRV_VERSION_WORD(1, 4)) {
features->select_between_composites = true;
features->copy_memory_permits_two_memory_accesses = true;
features->uconvert_spec_constant_op = true;
features->nonwritable_var_in_function_or_private = true;
}
}
} // namespace
ValidationState_t::ValidationState_t(const spv_const_context ctx,
const spv_const_validator_options opt,
const uint32_t* words,
const size_t num_words,
const uint32_t max_warnings)
: context_(ctx),
options_(opt),
words_(words),
num_words_(num_words),
unresolved_forward_ids_{},
operand_names_{},
current_layout_section_(kLayoutCapabilities),
module_functions_(),
module_capabilities_(),
module_extensions_(),
ordered_instructions_(),
all_definitions_(),
global_vars_(),
local_vars_(),
struct_nesting_depth_(),
struct_has_nested_blockorbufferblock_struct_(),
grammar_(ctx),
addressing_model_(spv::AddressingModel::Max),
memory_model_(spv::MemoryModel::Max),
pointer_size_and_alignment_(0),
sampler_image_addressing_mode_(0),
in_function_(false),
num_of_warnings_(0),
max_num_of_warnings_(max_warnings) {
assert(opt && "Validator options may not be Null.");
const auto env = context_->target_env;
if (spvIsVulkanEnv(env)) {
// Vulkan 1.1 includes VK_KHR_relaxed_block_layout in core.
if (env != SPV_ENV_VULKAN_1_0) {
features_.env_relaxed_block_layout = true;
}
}
// LocalSizeId is only disallowed prior to Vulkan 1.3 without maintenance4.
switch (env) {
case SPV_ENV_VULKAN_1_0:
case SPV_ENV_VULKAN_1_1:
case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
case SPV_ENV_VULKAN_1_2:
features_.env_allow_localsizeid = false;
break;
default:
features_.env_allow_localsizeid = true;
break;
}
// Only attempt to count if we have words, otherwise let the other validation
// fail and generate an error.
if (num_words > 0) {
// Count the number of instructions in the binary.
// This parse should not produce any error messages. Hijack the context and
// replace the message consumer so that we do not pollute any state in input
// consumer.
spv_context_t hijacked_context = *ctx;
hijacked_context.consumer = [](spv_message_level_t, const char*,
const spv_position_t&, const char*) {};
spvBinaryParse(&hijacked_context, this, words, num_words, setHeader,
CountInstructions,
/* diagnostic = */ nullptr);
preallocateStorage();
}
UpdateFeaturesBasedOnSpirvVersion(&features_, version_);
name_mapper_ = spvtools::GetTrivialNameMapper();
if (options_->use_friendly_names) {
friendly_mapper_ = spvtools::MakeUnique<spvtools::FriendlyNameMapper>(
context_, words_, num_words_);
name_mapper_ = friendly_mapper_->GetNameMapper();
}
}
void ValidationState_t::preallocateStorage() {
ordered_instructions_.reserve(total_instructions_);
module_functions_.reserve(total_functions_);
}
spv_result_t ValidationState_t::ForwardDeclareId(uint32_t id) {
unresolved_forward_ids_.insert(id);
return SPV_SUCCESS;
}
spv_result_t ValidationState_t::RemoveIfForwardDeclared(uint32_t id) {
unresolved_forward_ids_.erase(id);
return SPV_SUCCESS;
}
spv_result_t ValidationState_t::RegisterForwardPointer(uint32_t id) {
forward_pointer_ids_.insert(id);
return SPV_SUCCESS;
}
bool ValidationState_t::IsForwardPointer(uint32_t id) const {
return (forward_pointer_ids_.find(id) != forward_pointer_ids_.end());
}
void ValidationState_t::AssignNameToId(uint32_t id, std::string name) {
operand_names_[id] = name;
}
std::string ValidationState_t::getIdName(uint32_t id) const {
const std::string id_name = name_mapper_(id);
std::stringstream out;
out << "'" << id << "[%" << id_name << "]'";
return out.str();
}
size_t ValidationState_t::unresolved_forward_id_count() const {
return unresolved_forward_ids_.size();
}
std::vector<uint32_t> ValidationState_t::UnresolvedForwardIds() const {
std::vector<uint32_t> out(std::begin(unresolved_forward_ids_),
std::end(unresolved_forward_ids_));
return out;
}
bool ValidationState_t::IsDefinedId(uint32_t id) const {
return all_definitions_.find(id) != std::end(all_definitions_);
}
const Instruction* ValidationState_t::FindDef(uint32_t id) const {
auto it = all_definitions_.find(id);
if (it == all_definitions_.end()) return nullptr;
return it->second;
}
Instruction* ValidationState_t::FindDef(uint32_t id) {
auto it = all_definitions_.find(id);
if (it == all_definitions_.end()) return nullptr;
return it->second;
}
ModuleLayoutSection ValidationState_t::current_layout_section() const {
return current_layout_section_;
}
void ValidationState_t::ProgressToNextLayoutSectionOrder() {
// Guard against going past the last element(kLayoutFunctionDefinitions)
if (current_layout_section_ <= kLayoutFunctionDefinitions) {
current_layout_section_ =
static_cast<ModuleLayoutSection>(current_layout_section_ + 1);
}
}
bool ValidationState_t::IsOpcodeInPreviousLayoutSection(spv::Op op) {
ModuleLayoutSection section =
InstructionLayoutSection(current_layout_section_, op);
return section < current_layout_section_;
}
bool ValidationState_t::IsOpcodeInCurrentLayoutSection(spv::Op op) {
return IsInstructionInLayoutSection(current_layout_section_, op);
}
DiagnosticStream ValidationState_t::diag(spv_result_t error_code,
const Instruction* inst) {
if (error_code == SPV_WARNING) {
if (num_of_warnings_ == max_num_of_warnings_) {
DiagnosticStream({0, 0, 0}, context_->consumer, "", error_code)
<< "Other warnings have been suppressed.\n";
}
if (num_of_warnings_ >= max_num_of_warnings_) {
return DiagnosticStream({0, 0, 0}, nullptr, "", error_code);
}
++num_of_warnings_;
}
std::string disassembly;
if (inst) disassembly = Disassemble(*inst);
return DiagnosticStream({0, 0, inst ? inst->LineNum() : 0},
context_->consumer, disassembly, error_code);
}
std::vector<Function>& ValidationState_t::functions() {
return module_functions_;
}
Function& ValidationState_t::current_function() {
assert(in_function_body());
return module_functions_.back();
}
const Function& ValidationState_t::current_function() const {
assert(in_function_body());
return module_functions_.back();
}
const Function* ValidationState_t::function(uint32_t id) const {
const auto it = id_to_function_.find(id);
if (it == id_to_function_.end()) return nullptr;
return it->second;
}
Function* ValidationState_t::function(uint32_t id) {
auto it = id_to_function_.find(id);
if (it == id_to_function_.end()) return nullptr;
return it->second;
}
bool ValidationState_t::in_function_body() const { return in_function_; }
bool ValidationState_t::in_block() const {
return module_functions_.empty() == false &&
module_functions_.back().current_block() != nullptr;
}
void ValidationState_t::RegisterCapability(spv::Capability cap) {
// Avoid redundant work. Otherwise the recursion could induce work
// quadrdatic in the capability dependency depth. (Ok, not much, but
// it's something.)
if (module_capabilities_.contains(cap)) return;
module_capabilities_.insert(cap);
spv_operand_desc desc;
if (SPV_SUCCESS == grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
uint32_t(cap), &desc)) {
for (auto capability :
CapabilitySet(desc->numCapabilities, desc->capabilities)) {
RegisterCapability(capability);
}
}
switch (cap) {
case spv::Capability::Kernel:
features_.group_ops_reduce_and_scans = true;
break;
case spv::Capability::Int8:
features_.use_int8_type = true;
features_.declare_int8_type = true;
break;
case spv::Capability::StorageBuffer8BitAccess:
case spv::Capability::UniformAndStorageBuffer8BitAccess:
case spv::Capability::StoragePushConstant8:
case spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR:
features_.declare_int8_type = true;
break;
case spv::Capability::Int16:
features_.declare_int16_type = true;
break;
case spv::Capability::Float16:
case spv::Capability::Float16Buffer:
features_.declare_float16_type = true;
break;
case spv::Capability::StorageUniformBufferBlock16:
case spv::Capability::StorageUniform16:
case spv::Capability::StoragePushConstant16:
case spv::Capability::StorageInputOutput16:
case spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR:
features_.declare_int16_type = true;
features_.declare_float16_type = true;
features_.free_fp_rounding_mode = true;
break;
case spv::Capability::VariablePointers:
case spv::Capability::VariablePointersStorageBuffer:
features_.variable_pointers = true;
break;
default:
// TODO(dneto): For now don't validate SPV_NV_ray_tracing, which uses
// capability spv::Capability::RayTracingNV.
// spv::Capability::RayTracingProvisionalKHR would need the same
// treatment. One of the differences going from SPV_KHR_ray_tracing from
// provisional to final spec was the provisional spec uses Locations
// for variables in certain storage classes, just like the
// SPV_NV_ray_tracing extension. So it mimics the NVIDIA extension.
// The final SPV_KHR_ray_tracing uses a different capability token
// number, so it doesn't fall into this case.
break;
}
}
void ValidationState_t::RegisterExtension(Extension ext) {
if (module_extensions_.contains(ext)) return;
module_extensions_.insert(ext);
switch (ext) {
case kSPV_AMD_gpu_shader_half_float:
case kSPV_AMD_gpu_shader_half_float_fetch:
// SPV_AMD_gpu_shader_half_float enables float16 type.
// https://github.com/KhronosGroup/SPIRV-Tools/issues/1375
features_.declare_float16_type = true;
break;
case kSPV_AMD_gpu_shader_int16:
// This is not yet in the extension, but it's recommended for it.
// See https://github.com/KhronosGroup/glslang/issues/848
features_.uconvert_spec_constant_op = true;
break;
case kSPV_AMD_shader_ballot:
// The grammar doesn't encode the fact that SPV_AMD_shader_ballot
// enables the use of group operations Reduce, InclusiveScan,
// and ExclusiveScan. Enable it manually.
// https://github.com/KhronosGroup/SPIRV-Tools/issues/991
features_.group_ops_reduce_and_scans = true;
break;
default:
break;
}
}
bool ValidationState_t::HasAnyOfCapabilities(
const CapabilitySet& capabilities) const {
return module_capabilities_.HasAnyOf(capabilities);
}
bool ValidationState_t::HasAnyOfExtensions(
const ExtensionSet& extensions) const {
return module_extensions_.HasAnyOf(extensions);
}
void ValidationState_t::set_addressing_model(spv::AddressingModel am) {
addressing_model_ = am;
switch (am) {
case spv::AddressingModel::Physical32:
pointer_size_and_alignment_ = 4;
break;
default:
// fall through
case spv::AddressingModel::Physical64:
case spv::AddressingModel::PhysicalStorageBuffer64:
pointer_size_and_alignment_ = 8;
break;
}
}
spv::AddressingModel ValidationState_t::addressing_model() const {
return addressing_model_;
}
void ValidationState_t::set_memory_model(spv::MemoryModel mm) {
memory_model_ = mm;
}
spv::MemoryModel ValidationState_t::memory_model() const {
return memory_model_;
}
void ValidationState_t::set_samplerimage_variable_address_mode(
uint32_t bit_width) {
sampler_image_addressing_mode_ = bit_width;
}
uint32_t ValidationState_t::samplerimage_variable_address_mode() const {
return sampler_image_addressing_mode_;
}
spv_result_t ValidationState_t::RegisterFunction(
uint32_t id, uint32_t ret_type_id,
spv::FunctionControlMask function_control, uint32_t function_type_id) {
assert(in_function_body() == false &&
"RegisterFunction can only be called when parsing the binary outside "
"of another function");
in_function_ = true;
module_functions_.emplace_back(id, ret_type_id, function_control,
function_type_id);
id_to_function_.emplace(id, ¤t_function());
// TODO(umar): validate function type and type_id
return SPV_SUCCESS;
}
spv_result_t ValidationState_t::RegisterFunctionEnd() {
assert(in_function_body() == true &&
"RegisterFunctionEnd can only be called when parsing the binary "
"inside of another function");
assert(in_block() == false &&
"RegisterFunctionParameter can only be called when parsing the binary "
"outside of a block");
current_function().RegisterFunctionEnd();
in_function_ = false;
return SPV_SUCCESS;
}
Instruction* ValidationState_t::AddOrderedInstruction(
const spv_parsed_instruction_t* inst) {
ordered_instructions_.emplace_back(inst);
ordered_instructions_.back().SetLineNum(ordered_instructions_.size());
return &ordered_instructions_.back();
}
// Improves diagnostic messages by collecting names of IDs
void ValidationState_t::RegisterDebugInstruction(const Instruction* inst) {
switch (inst->opcode()) {
case spv::Op::OpName: {
const auto target = inst->GetOperandAs<uint32_t>(0);
const std::string str = inst->GetOperandAs<std::string>(1);
AssignNameToId(target, str);
break;
}
case spv::Op::OpMemberName: {
const auto target = inst->GetOperandAs<uint32_t>(0);
const std::string str = inst->GetOperandAs<std::string>(2);
AssignNameToId(target, str);
break;
}
case spv::Op::OpSourceContinued:
case spv::Op::OpSource:
case spv::Op::OpSourceExtension:
case spv::Op::OpString:
case spv::Op::OpLine:
case spv::Op::OpNoLine:
default:
break;
}
}
void ValidationState_t::RegisterInstruction(Instruction* inst) {
if (inst->id()) all_definitions_.insert(std::make_pair(inst->id(), inst));
// Some validation checks are easier by getting all the consumers
for (size_t i = 0; i < inst->operands().size(); ++i) {
const spv_parsed_operand_t& operand = inst->operand(i);
if ((SPV_OPERAND_TYPE_ID == operand.type) ||
(SPV_OPERAND_TYPE_TYPE_ID == operand.type)) {
const uint32_t operand_word = inst->word(operand.offset);
Instruction* operand_inst = FindDef(operand_word);
if (!operand_inst) {
continue;
}
// If the instruction is using an OpTypeSampledImage as an operand, it
// should be recorded. The validator will ensure that all usages of an
// OpTypeSampledImage and its definition are in the same basic block.
if ((SPV_OPERAND_TYPE_ID == operand.type) &&
(spv::Op::OpSampledImage == operand_inst->opcode())) {
RegisterSampledImageConsumer(operand_word, inst);
}
// In order to track storage classes (not Function) used per execution
// model we can't use RegisterExecutionModelLimitation on instructions
// like OpTypePointer which are going to be in the pre-function section.
// Instead just need to register storage class usage for consumers in a
// function block.
if (inst->function()) {
if (operand_inst->opcode() == spv::Op::OpTypePointer) {
RegisterStorageClassConsumer(
operand_inst->GetOperandAs<spv::StorageClass>(1), inst);
} else if (operand_inst->opcode() == spv::Op::OpVariable) {
RegisterStorageClassConsumer(
operand_inst->GetOperandAs<spv::StorageClass>(2), inst);
}
}
}
}
}
std::vector<Instruction*> ValidationState_t::getSampledImageConsumers(
uint32_t sampled_image_id) const {
std::vector<Instruction*> result;
auto iter = sampled_image_consumers_.find(sampled_image_id);
if (iter != sampled_image_consumers_.end()) {
result = iter->second;
}
return result;
}
void ValidationState_t::RegisterSampledImageConsumer(uint32_t sampled_image_id,
Instruction* consumer) {
sampled_image_consumers_[sampled_image_id].push_back(consumer);
}
void ValidationState_t::RegisterQCOMImageProcessingTextureConsumer(
uint32_t texture_id, const Instruction* consumer0,
const Instruction* consumer1) {
if (HasDecoration(texture_id, spv::Decoration::WeightTextureQCOM) ||
HasDecoration(texture_id, spv::Decoration::BlockMatchTextureQCOM) ||
HasDecoration(texture_id, spv::Decoration::BlockMatchSamplerQCOM)) {
qcom_image_processing_consumers_.insert(consumer0->id());
if (consumer1) {
qcom_image_processing_consumers_.insert(consumer1->id());
}
}
}
void ValidationState_t::RegisterStorageClassConsumer(
spv::StorageClass storage_class, Instruction* consumer) {
if (spvIsVulkanEnv(context()->target_env)) {
if (storage_class == spv::StorageClass::Output) {
std::string errorVUID = VkErrorID(4644);
function(consumer->function()->id())
->RegisterExecutionModelLimitation([errorVUID](
spv::ExecutionModel model,
std::string* message) {
if (model == spv::ExecutionModel::GLCompute ||
model == spv::ExecutionModel::RayGenerationKHR ||
model == spv::ExecutionModel::IntersectionKHR ||
model == spv::ExecutionModel::AnyHitKHR ||
model == spv::ExecutionModel::ClosestHitKHR ||
model == spv::ExecutionModel::MissKHR ||
model == spv::ExecutionModel::CallableKHR) {
if (message) {
*message =
errorVUID +
"in Vulkan environment, Output Storage Class must not be "
"used in GLCompute, RayGenerationKHR, IntersectionKHR, "
"AnyHitKHR, ClosestHitKHR, MissKHR, or CallableKHR "
"execution models";
}
return false;
}
return true;
});
}
if (storage_class == spv::StorageClass::Workgroup) {
std::string errorVUID = VkErrorID(4645);
function(consumer->function()->id())
->RegisterExecutionModelLimitation([errorVUID](
spv::ExecutionModel model,
std::string* message) {
if (model != spv::ExecutionModel::GLCompute &&
model != spv::ExecutionModel::TaskNV &&
model != spv::ExecutionModel::MeshNV &&
model != spv::ExecutionModel::TaskEXT &&
model != spv::ExecutionModel::MeshEXT) {
if (message) {
*message =
errorVUID +
"in Vulkan environment, Workgroup Storage Class is limited "
"to MeshNV, TaskNV, and GLCompute execution model";
}
return false;
}
return true;
});
}
}
if (storage_class == spv::StorageClass::CallableDataKHR) {
std::string errorVUID = VkErrorID(4704);
function(consumer->function()->id())
->RegisterExecutionModelLimitation(
[errorVUID](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::RayGenerationKHR &&
model != spv::ExecutionModel::ClosestHitKHR &&
model != spv::ExecutionModel::CallableKHR &&
model != spv::ExecutionModel::MissKHR) {
if (message) {
*message =
errorVUID +
"CallableDataKHR Storage Class is limited to "
"RayGenerationKHR, ClosestHitKHR, CallableKHR, and "
"MissKHR execution model";
}
return false;
}
return true;
});
} else if (storage_class == spv::StorageClass::IncomingCallableDataKHR) {
std::string errorVUID = VkErrorID(4705);
function(consumer->function()->id())
->RegisterExecutionModelLimitation(
[errorVUID](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::CallableKHR) {
if (message) {
*message =
errorVUID +
"IncomingCallableDataKHR Storage Class is limited to "
"CallableKHR execution model";
}
return false;
}
return true;
});
} else if (storage_class == spv::StorageClass::RayPayloadKHR) {
std::string errorVUID = VkErrorID(4698);
function(consumer->function()->id())
->RegisterExecutionModelLimitation([errorVUID](
spv::ExecutionModel model,
std::string* message) {
if (model != spv::ExecutionModel::RayGenerationKHR &&
model != spv::ExecutionModel::ClosestHitKHR &&
model != spv::ExecutionModel::MissKHR) {
if (message) {
*message =
errorVUID +
"RayPayloadKHR Storage Class is limited to RayGenerationKHR, "
"ClosestHitKHR, and MissKHR execution model";
}
return false;
}
return true;
});
} else if (storage_class == spv::StorageClass::HitAttributeKHR) {
std::string errorVUID = VkErrorID(4701);
function(consumer->function()->id())
->RegisterExecutionModelLimitation(
[errorVUID](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::IntersectionKHR &&
model != spv::ExecutionModel::AnyHitKHR &&
model != spv::ExecutionModel::ClosestHitKHR) {
if (message) {
*message = errorVUID +
"HitAttributeKHR Storage Class is limited to "
"IntersectionKHR, AnyHitKHR, sand ClosestHitKHR "
"execution model";
}
return false;
}
return true;
});
} else if (storage_class == spv::StorageClass::IncomingRayPayloadKHR) {
std::string errorVUID = VkErrorID(4699);
function(consumer->function()->id())
->RegisterExecutionModelLimitation(
[errorVUID](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::AnyHitKHR &&
model != spv::ExecutionModel::ClosestHitKHR &&
model != spv::ExecutionModel::MissKHR) {
if (message) {
*message =
errorVUID +
"IncomingRayPayloadKHR Storage Class is limited to "
"AnyHitKHR, ClosestHitKHR, and MissKHR execution model";
}
return false;
}
return true;
});
} else if (storage_class == spv::StorageClass::ShaderRecordBufferKHR) {
std::string errorVUID = VkErrorID(7119);
function(consumer->function()->id())
->RegisterExecutionModelLimitation(
[errorVUID](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::RayGenerationKHR &&
model != spv::ExecutionModel::IntersectionKHR &&
model != spv::ExecutionModel::AnyHitKHR &&
model != spv::ExecutionModel::ClosestHitKHR &&
model != spv::ExecutionModel::CallableKHR &&
model != spv::ExecutionModel::MissKHR) {
if (message) {
*message =
errorVUID +
"ShaderRecordBufferKHR Storage Class is limited to "
"RayGenerationKHR, IntersectionKHR, AnyHitKHR, "
"ClosestHitKHR, CallableKHR, and MissKHR execution model";
}
return false;
}
return true;
});
} else if (storage_class == spv::StorageClass::TaskPayloadWorkgroupEXT) {
function(consumer->function()->id())
->RegisterExecutionModelLimitation(
[](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::TaskEXT &&
model != spv::ExecutionModel::MeshEXT) {
if (message) {
*message =
"TaskPayloadWorkgroupEXT Storage Class is limited to "
"TaskEXT and MeshKHR execution model";
}
return false;
}
return true;
});
} else if (storage_class == spv::StorageClass::HitObjectAttributeNV) {
function(consumer->function()->id())
->RegisterExecutionModelLimitation([](spv::ExecutionModel model,
std::string* message) {
if (model != spv::ExecutionModel::RayGenerationKHR &&
model != spv::ExecutionModel::ClosestHitKHR &&
model != spv::ExecutionModel::MissKHR) {
if (message) {
*message =
"HitObjectAttributeNV Storage Class is limited to "
"RayGenerationKHR, ClosestHitKHR or MissKHR execution model";
}
return false;
}
return true;
});
}
}
uint32_t ValidationState_t::getIdBound() const { return id_bound_; }
void ValidationState_t::setIdBound(const uint32_t bound) { id_bound_ = bound; }
bool ValidationState_t::RegisterUniqueTypeDeclaration(const Instruction* inst) {
std::vector<uint32_t> key;
key.push_back(static_cast<uint32_t>(inst->opcode()));
for (size_t index = 0; index < inst->operands().size(); ++index) {
const spv_parsed_operand_t& operand = inst->operand(index);
if (operand.type == SPV_OPERAND_TYPE_RESULT_ID) continue;
const int words_begin = operand.offset;
const int words_end = words_begin + operand.num_words;
assert(words_end <= static_cast<int>(inst->words().size()));
key.insert(key.end(), inst->words().begin() + words_begin,
inst->words().begin() + words_end);
}
return unique_type_declarations_.insert(std::move(key)).second;
}
uint32_t ValidationState_t::GetTypeId(uint32_t id) const {
const Instruction* inst = FindDef(id);
return inst ? inst->type_id() : 0;
}
spv::Op ValidationState_t::GetIdOpcode(uint32_t id) const {
const Instruction* inst = FindDef(id);
return inst ? inst->opcode() : spv::Op::OpNop;
}
uint32_t ValidationState_t::GetComponentType(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
switch (inst->opcode()) {
case spv::Op::OpTypeFloat:
case spv::Op::OpTypeInt:
case spv::Op::OpTypeBool:
return id;
case spv::Op::OpTypeArray:
return inst->word(2);
case spv::Op::OpTypeVector:
return inst->word(2);
case spv::Op::OpTypeMatrix:
return GetComponentType(inst->word(2));
case spv::Op::OpTypeCooperativeMatrixNV:
case spv::Op::OpTypeCooperativeMatrixKHR:
case spv::Op::OpTypeCooperativeVectorNV:
return inst->word(2);
default:
break;
}
if (inst->type_id()) return GetComponentType(inst->type_id());
assert(0);
return 0;
}
uint32_t ValidationState_t::GetDimension(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
switch (inst->opcode()) {
case spv::Op::OpTypeFloat:
case spv::Op::OpTypeInt:
case spv::Op::OpTypeBool:
return 1;
case spv::Op::OpTypeVector:
case spv::Op::OpTypeMatrix:
return inst->word(3);
case spv::Op::OpTypeCooperativeMatrixNV:
case spv::Op::OpTypeCooperativeMatrixKHR:
case spv::Op::OpTypeCooperativeVectorNV:
// Actual dimension isn't known, return 0
return 0;
default:
break;
}
if (inst->type_id()) return GetDimension(inst->type_id());
assert(0);
return 0;
}
uint32_t ValidationState_t::GetBitWidth(uint32_t id) const {
const uint32_t component_type_id = GetComponentType(id);
const Instruction* inst = FindDef(component_type_id);
assert(inst);
if (inst->opcode() == spv::Op::OpTypeFloat ||
inst->opcode() == spv::Op::OpTypeInt)
return inst->word(2);
if (inst->opcode() == spv::Op::OpTypeBool) return 1;
assert(0);
return 0;
}
bool ValidationState_t::IsVoidType(uint32_t id) const {
const Instruction* inst = FindDef(id);
return inst && inst->opcode() == spv::Op::OpTypeVoid;
}
bool ValidationState_t::IsFloatScalarType(uint32_t id) const {
const Instruction* inst = FindDef(id);
return inst && inst->opcode() == spv::Op::OpTypeFloat;
}
bool ValidationState_t::IsFloatArrayType(uint32_t id) const {
const Instruction* inst = FindDef(id);
if (!inst) {
return false;
}
if (inst->opcode() == spv::Op::OpTypeArray) {
return IsFloatScalarType(GetComponentType(id));
}
return false;
}
bool ValidationState_t::IsFloatVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
if (!inst) {
return false;
}
if (inst->opcode() == spv::Op::OpTypeVector) {
return IsFloatScalarType(GetComponentType(id));
}
return false;
}
bool ValidationState_t::IsFloat16Vector2Or4Type(uint32_t id) const {
const Instruction* inst = FindDef(id);
assert(inst);
if (inst->opcode() == spv::Op::OpTypeVector) {
uint32_t vectorDim = GetDimension(id);
return IsFloatScalarType(GetComponentType(id)) &&
(vectorDim == 2 || vectorDim == 4) &&
(GetBitWidth(GetComponentType(id)) == 16);
}
return false;
}
bool ValidationState_t::IsFloatScalarOrVectorType(uint32_t id) const {
const Instruction* inst = FindDef(id);
if (!inst) {
return false;
}
if (inst->opcode() == spv::Op::OpTypeFloat) {