Debian Codegraph @

third_party/spirv-tools/source/val/validate_builtins.cpp

raw
1 // Copyright (c) 2018 Google LLC.
2 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
3 // reserved.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 // Validates correctness of built-in variables.
18
19 #include <functional>
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <sstream>
24 #include <stack>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28
29 #include "source/diagnostic.h"
30 #include "source/opcode.h"
31 #include "source/spirv_target_env.h"
32 #include "source/util/bitutils.h"
33 #include "source/val/instruction.h"
34 #include "source/val/validate.h"
35 #include "source/val/validation_state.h"
36
37 namespace spvtools {
38 namespace val {
39 namespace {
40
41 // Returns a short textual description of the id defined by the given
42 // instruction.
43 std::string GetIdDesc(const Instruction& inst) {
44 std::ostringstream ss;
45 ss << "ID <" << inst.id() << "> (Op" << spvOpcodeString(inst.opcode()) << ")";
46 return ss.str();
47 }
48
49 // Gets underlying data type which is
50 // - member type if instruction is OpTypeStruct
51 // (member index is taken from decoration).
52 // - data type if id creates a pointer.
53 // - type of the constant if instruction is OpConst or OpSpecConst.
54 //
55 // Fails in any other case. The function is based on built-ins allowed by
56 // the Vulkan spec.
57 // TODO: If non-Vulkan validation rules are added then it might need
58 // to be refactored.
59 spv_result_t GetUnderlyingType(ValidationState_t& _,
60 const Decoration& decoration,
61 const Instruction& inst,
62 uint32_t* underlying_type) {
63 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
64 if (inst.opcode() != SpvOpTypeStruct) {
65 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
66 << GetIdDesc(inst)
67 << "Attempted to get underlying data type via member index for "
68 "non-struct type.";
69 }
70 *underlying_type = inst.word(decoration.struct_member_index() + 2);
71 return SPV_SUCCESS;
72 }
73
74 if (inst.opcode() == SpvOpTypeStruct) {
75 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
76 << GetIdDesc(inst)
77 << " did not find an member index to get underlying data type for "
78 "struct type.";
79 }
80
81 if (spvOpcodeIsConstant(inst.opcode())) {
82 *underlying_type = inst.type_id();
83 return SPV_SUCCESS;
84 }
85
86 uint32_t storage_class = 0;
87 if (!_.GetPointerTypeInfo(inst.type_id(), underlying_type, &storage_class)) {
88 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
89 << GetIdDesc(inst)
90 << " is decorated with BuiltIn. BuiltIn decoration should only be "
91 "applied to struct types, variables and constants.";
92 }
93 return SPV_SUCCESS;
94 }
95
96 // Returns Storage Class used by the instruction if applicable.
97 // Returns SpvStorageClassMax if not.
98 SpvStorageClass GetStorageClass(const Instruction& inst) {
99 switch (inst.opcode()) {
100 case SpvOpTypePointer:
101 case SpvOpTypeForwardPointer: {
102 return SpvStorageClass(inst.word(2));
103 }
104 case SpvOpVariable: {
105 return SpvStorageClass(inst.word(3));
106 }
107 case SpvOpGenericCastToPtrExplicit: {
108 return SpvStorageClass(inst.word(4));
109 }
110 default: { break; }
111 }
112 return SpvStorageClassMax;
113 }
114
115 bool IsBuiltInValidForWebGPU(SpvBuiltIn label) {
116 switch (label) {
117 case SpvBuiltInPosition:
118 case SpvBuiltInVertexIndex:
119 case SpvBuiltInInstanceIndex:
120 case SpvBuiltInFrontFacing:
121 case SpvBuiltInFragCoord:
122 case SpvBuiltInFragDepth:
123 case SpvBuiltInNumWorkgroups:
124 case SpvBuiltInWorkgroupSize:
125 case SpvBuiltInLocalInvocationId:
126 case SpvBuiltInGlobalInvocationId:
127 case SpvBuiltInLocalInvocationIndex: {
128 return true;
129 }
130 default:
131 break;
132 }
133
134 return false;
135 }
136
137 // Helper class managing validation of built-ins.
138 // TODO: Generic functionality of this class can be moved into
139 // ValidationState_t to be made available to other users.
140 class BuiltInsValidator {
141 public:
142 BuiltInsValidator(ValidationState_t& vstate) : _(vstate) {}
143
144 // Run validation.
145 spv_result_t Run();
146
147 private:
148 // Goes through all decorations in the module, if decoration is BuiltIn
149 // calls ValidateSingleBuiltInAtDefinition().
150 spv_result_t ValidateBuiltInsAtDefinition();
151
152 // Validates the instruction defining an id with built-in decoration.
153 // Can be called multiple times for the same id, if multiple built-ins are
154 // specified. Seeds id_to_at_reference_checks_ with decorated ids if needed.
155 spv_result_t ValidateSingleBuiltInAtDefinition(const Decoration& decoration,
156 const Instruction& inst);
157
158 // The following section contains functions which are called when id defined
159 // by |inst| is decorated with BuiltIn |decoration|.
160 // Most functions are specific to a single built-in and have naming scheme:
161 // ValidateXYZAtDefinition. Some functions are common to multiple kinds of
162 // BuiltIn.
163 spv_result_t ValidateClipOrCullDistanceAtDefinition(
164 const Decoration& decoration, const Instruction& inst);
165 spv_result_t ValidateFragCoordAtDefinition(const Decoration& decoration,
166 const Instruction& inst);
167 spv_result_t ValidateFragDepthAtDefinition(const Decoration& decoration,
168 const Instruction& inst);
169 spv_result_t ValidateFrontFacingAtDefinition(const Decoration& decoration,
170 const Instruction& inst);
171 spv_result_t ValidateHelperInvocationAtDefinition(
172 const Decoration& decoration, const Instruction& inst);
173 spv_result_t ValidateInvocationIdAtDefinition(const Decoration& decoration,
174 const Instruction& inst);
175 spv_result_t ValidateInstanceIndexAtDefinition(const Decoration& decoration,
176 const Instruction& inst);
177 spv_result_t ValidateLayerOrViewportIndexAtDefinition(
178 const Decoration& decoration, const Instruction& inst);
179 spv_result_t ValidatePatchVerticesAtDefinition(const Decoration& decoration,
180 const Instruction& inst);
181 spv_result_t ValidatePointCoordAtDefinition(const Decoration& decoration,
182 const Instruction& inst);
183 spv_result_t ValidatePointSizeAtDefinition(const Decoration& decoration,
184 const Instruction& inst);
185 spv_result_t ValidatePositionAtDefinition(const Decoration& decoration,
186 const Instruction& inst);
187 spv_result_t ValidatePrimitiveIdAtDefinition(const Decoration& decoration,
188 const Instruction& inst);
189 spv_result_t ValidateSampleIdAtDefinition(const Decoration& decoration,
190 const Instruction& inst);
191 spv_result_t ValidateSampleMaskAtDefinition(const Decoration& decoration,
192 const Instruction& inst);
193 spv_result_t ValidateSamplePositionAtDefinition(const Decoration& decoration,
194 const Instruction& inst);
195 spv_result_t ValidateTessCoordAtDefinition(const Decoration& decoration,
196 const Instruction& inst);
197 spv_result_t ValidateTessLevelOuterAtDefinition(const Decoration& decoration,
198 const Instruction& inst);
199 spv_result_t ValidateTessLevelInnerAtDefinition(const Decoration& decoration,
200 const Instruction& inst);
201 spv_result_t ValidateVertexIndexAtDefinition(const Decoration& decoration,
202 const Instruction& inst);
203 spv_result_t ValidateVertexIdOrInstanceIdAtDefinition(
204 const Decoration& decoration, const Instruction& inst);
205 spv_result_t ValidateLocalInvocationIndexAtDefinition(
206 const Decoration& decoration, const Instruction& inst);
207 spv_result_t ValidateWorkgroupSizeAtDefinition(const Decoration& decoration,
208 const Instruction& inst);
209 spv_result_t ValidateBaseInstanceOrVertexAtDefinition(
210 const Decoration& decoration, const Instruction& inst);
211 spv_result_t ValidateDrawIndexAtDefinition(const Decoration& decoration,
212 const Instruction& inst);
213 spv_result_t ValidateViewIndexAtDefinition(const Decoration& decoration,
214 const Instruction& inst);
215 spv_result_t ValidateDeviceIndexAtDefinition(const Decoration& decoration,
216 const Instruction& inst);
217 // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
218 spv_result_t ValidateComputeShaderI32Vec3InputAtDefinition(
219 const Decoration& decoration, const Instruction& inst);
220 spv_result_t ValidateSMBuiltinsAtDefinition(const Decoration& decoration,
221 const Instruction& inst);
222
223 // Used for SubgroupEqMask, SubgroupGeMask, SubgroupGtMask, SubgroupLtMask,
224 // SubgroupLeMask.
225 spv_result_t ValidateI32Vec4InputAtDefinition(const Decoration& decoration,
226 const Instruction& inst);
227 // Used for SubgroupLocalInvocationId, SubgroupSize.
228 spv_result_t ValidateI32InputAtDefinition(const Decoration& decoration,
229 const Instruction& inst);
230 // Used for SubgroupId, NumSubgroups.
231 spv_result_t ValidateComputeI32InputAtDefinition(const Decoration& decoration,
232 const Instruction& inst);
233
234 spv_result_t ValidatePrimitiveShadingRateAtDefinition(
235 const Decoration& decoration, const Instruction& inst);
236
237 spv_result_t ValidateShadingRateAtDefinition(const Decoration& decoration,
238 const Instruction& inst);
239
240 // The following section contains functions which are called when id defined
241 // by |referenced_inst| is
242 // 1. referenced by |referenced_from_inst|
243 // 2. dependent on |built_in_inst| which is decorated with BuiltIn
244 // |decoration|. Most functions are specific to a single built-in and have
245 // naming scheme: ValidateXYZAtReference. Some functions are common to
246 // multiple kinds of BuiltIn.
247 spv_result_t ValidateFragCoordAtReference(
248 const Decoration& decoration, const Instruction& built_in_inst,
249 const Instruction& referenced_inst,
250 const Instruction& referenced_from_inst);
251
252 spv_result_t ValidateFragDepthAtReference(
253 const Decoration& decoration, const Instruction& built_in_inst,
254 const Instruction& referenced_inst,
255 const Instruction& referenced_from_inst);
256
257 spv_result_t ValidateFrontFacingAtReference(
258 const Decoration& decoration, const Instruction& built_in_inst,
259 const Instruction& referenced_inst,
260 const Instruction& referenced_from_inst);
261
262 spv_result_t ValidateHelperInvocationAtReference(
263 const Decoration& decoration, const Instruction& built_in_inst,
264 const Instruction& referenced_inst,
265 const Instruction& referenced_from_inst);
266
267 spv_result_t ValidateInvocationIdAtReference(
268 const Decoration& decoration, const Instruction& built_in_inst,
269 const Instruction& referenced_inst,
270 const Instruction& referenced_from_inst);
271
272 spv_result_t ValidateInstanceIdAtReference(
273 const Decoration& decoration, const Instruction& built_in_inst,
274 const Instruction& referenced_inst,
275 const Instruction& referenced_from_inst);
276
277 spv_result_t ValidateInstanceIndexAtReference(
278 const Decoration& decoration, const Instruction& built_in_inst,
279 const Instruction& referenced_inst,
280 const Instruction& referenced_from_inst);
281
282 spv_result_t ValidatePatchVerticesAtReference(
283 const Decoration& decoration, const Instruction& built_in_inst,
284 const Instruction& referenced_inst,
285 const Instruction& referenced_from_inst);
286
287 spv_result_t ValidatePointCoordAtReference(
288 const Decoration& decoration, const Instruction& built_in_inst,
289 const Instruction& referenced_inst,
290 const Instruction& referenced_from_inst);
291
292 spv_result_t ValidatePointSizeAtReference(
293 const Decoration& decoration, const Instruction& built_in_inst,
294 const Instruction& referenced_inst,
295 const Instruction& referenced_from_inst);
296
297 spv_result_t ValidatePositionAtReference(
298 const Decoration& decoration, const Instruction& built_in_inst,
299 const Instruction& referenced_inst,
300 const Instruction& referenced_from_inst);
301
302 spv_result_t ValidatePrimitiveIdAtReference(
303 const Decoration& decoration, const Instruction& built_in_inst,
304 const Instruction& referenced_inst,
305 const Instruction& referenced_from_inst);
306
307 spv_result_t ValidateSampleIdAtReference(
308 const Decoration& decoration, const Instruction& built_in_inst,
309 const Instruction& referenced_inst,
310 const Instruction& referenced_from_inst);
311
312 spv_result_t ValidateSampleMaskAtReference(
313 const Decoration& decoration, const Instruction& built_in_inst,
314 const Instruction& referenced_inst,
315 const Instruction& referenced_from_inst);
316
317 spv_result_t ValidateSamplePositionAtReference(
318 const Decoration& decoration, const Instruction& built_in_inst,
319 const Instruction& referenced_inst,
320 const Instruction& referenced_from_inst);
321
322 spv_result_t ValidateTessCoordAtReference(
323 const Decoration& decoration, const Instruction& built_in_inst,
324 const Instruction& referenced_inst,
325 const Instruction& referenced_from_inst);
326
327 spv_result_t ValidateTessLevelAtReference(
328 const Decoration& decoration, const Instruction& built_in_inst,
329 const Instruction& referenced_inst,
330 const Instruction& referenced_from_inst);
331
332 spv_result_t ValidateLocalInvocationIndexAtReference(
333 const Decoration& decoration, const Instruction& built_in_inst,
334 const Instruction& referenced_inst,
335 const Instruction& referenced_from_inst);
336
337 spv_result_t ValidateVertexIndexAtReference(
338 const Decoration& decoration, const Instruction& built_in_inst,
339 const Instruction& referenced_inst,
340 const Instruction& referenced_from_inst);
341
342 spv_result_t ValidateLayerOrViewportIndexAtReference(
343 const Decoration& decoration, const Instruction& built_in_inst,
344 const Instruction& referenced_inst,
345 const Instruction& referenced_from_inst);
346
347 spv_result_t ValidateWorkgroupSizeAtReference(
348 const Decoration& decoration, const Instruction& built_in_inst,
349 const Instruction& referenced_inst,
350 const Instruction& referenced_from_inst);
351
352 spv_result_t ValidateClipOrCullDistanceAtReference(
353 const Decoration& decoration, const Instruction& built_in_inst,
354 const Instruction& referenced_inst,
355 const Instruction& referenced_from_inst);
356
357 spv_result_t ValidateBaseInstanceOrVertexAtReference(
358 const Decoration& decoration, const Instruction& built_in_inst,
359 const Instruction& referenced_inst,
360 const Instruction& referenced_from_inst);
361
362 spv_result_t ValidateDrawIndexAtReference(
363 const Decoration& decoration, const Instruction& built_in_inst,
364 const Instruction& referenced_inst,
365 const Instruction& referenced_from_inst);
366
367 spv_result_t ValidateViewIndexAtReference(
368 const Decoration& decoration, const Instruction& built_in_inst,
369 const Instruction& referenced_inst,
370 const Instruction& referenced_from_inst);
371
372 spv_result_t ValidateDeviceIndexAtReference(
373 const Decoration& decoration, const Instruction& built_in_inst,
374 const Instruction& referenced_inst,
375 const Instruction& referenced_from_inst);
376
377 // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
378 spv_result_t ValidateComputeShaderI32Vec3InputAtReference(
379 const Decoration& decoration, const Instruction& built_in_inst,
380 const Instruction& referenced_inst,
381 const Instruction& referenced_from_inst);
382 // Used for SubgroupId and NumSubgroups.
383 spv_result_t ValidateComputeI32InputAtReference(
384 const Decoration& decoration, const Instruction& built_in_inst,
385 const Instruction& referenced_inst,
386 const Instruction& referenced_from_inst);
387
388 spv_result_t ValidateSMBuiltinsAtReference(
389 const Decoration& decoration, const Instruction& built_in_inst,
390 const Instruction& referenced_inst,
391 const Instruction& referenced_from_inst);
392
393 spv_result_t ValidatePrimitiveShadingRateAtReference(
394 const Decoration& decoration, const Instruction& built_in_inst,
395 const Instruction& referenced_inst,
396 const Instruction& referenced_from_inst);
397
398 spv_result_t ValidateShadingRateAtReference(
399 const Decoration& decoration, const Instruction& built_in_inst,
400 const Instruction& referenced_inst,
401 const Instruction& referenced_from_inst);
402
403 // Validates that |built_in_inst| is not (even indirectly) referenced from
404 // within a function which can be called with |execution_model|.
405 //
406 // |comment| - text explaining why the restriction was imposed.
407 // |decoration| - BuiltIn decoration which causes the restriction.
408 // |referenced_inst| - instruction which is dependent on |built_in_inst| and
409 // defines the id which was referenced.
410 // |referenced_from_inst| - instruction which references id defined by
411 // |referenced_inst| from within a function.
412 spv_result_t ValidateNotCalledWithExecutionModel(
413 std::string comment, SpvExecutionModel execution_model,
414 const Decoration& decoration, const Instruction& built_in_inst,
415 const Instruction& referenced_inst,
416 const Instruction& referenced_from_inst);
417
418 // The following section contains functions which check that the decorated
419 // variable has the type specified in the function name. |diag| would be
420 // called with a corresponding error message, if validation is not successful.
421 spv_result_t ValidateBool(
422 const Decoration& decoration, const Instruction& inst,
423 const std::function<spv_result_t(const std::string& message)>& diag);
424 spv_result_t ValidateI32(
425 const Decoration& decoration, const Instruction& inst,
426 const std::function<spv_result_t(const std::string& message)>& diag);
427 spv_result_t ValidateI32Vec(
428 const Decoration& decoration, const Instruction& inst,
429 uint32_t num_components,
430 const std::function<spv_result_t(const std::string& message)>& diag);
431 spv_result_t ValidateI32Arr(
432 const Decoration& decoration, const Instruction& inst,
433 const std::function<spv_result_t(const std::string& message)>& diag);
434 spv_result_t ValidateOptionalArrayedI32(
435 const Decoration& decoration, const Instruction& inst,
436 const std::function<spv_result_t(const std::string& message)>& diag);
437 spv_result_t ValidateI32Helper(
438 const Decoration& decoration, const Instruction& inst,
439 const std::function<spv_result_t(const std::string& message)>& diag,
440 uint32_t underlying_type);
441 spv_result_t ValidateF32(
442 const Decoration& decoration, const Instruction& inst,
443 const std::function<spv_result_t(const std::string& message)>& diag);
444 spv_result_t ValidateOptionalArrayedF32(
445 const Decoration& decoration, const Instruction& inst,
446 const std::function<spv_result_t(const std::string& message)>& diag);
447 spv_result_t ValidateF32Helper(
448 const Decoration& decoration, const Instruction& inst,
449 const std::function<spv_result_t(const std::string& message)>& diag,
450 uint32_t underlying_type);
451 spv_result_t ValidateF32Vec(
452 const Decoration& decoration, const Instruction& inst,
453 uint32_t num_components,
454 const std::function<spv_result_t(const std::string& message)>& diag);
455 spv_result_t ValidateOptionalArrayedF32Vec(
456 const Decoration& decoration, const Instruction& inst,
457 uint32_t num_components,
458 const std::function<spv_result_t(const std::string& message)>& diag);
459 spv_result_t ValidateF32VecHelper(
460 const Decoration& decoration, const Instruction& inst,
461 uint32_t num_components,
462 const std::function<spv_result_t(const std::string& message)>& diag,
463 uint32_t underlying_type);
464 // If |num_components| is zero, the number of components is not checked.
465 spv_result_t ValidateF32Arr(
466 const Decoration& decoration, const Instruction& inst,
467 uint32_t num_components,
468 const std::function<spv_result_t(const std::string& message)>& diag);
469 spv_result_t ValidateOptionalArrayedF32Arr(
470 const Decoration& decoration, const Instruction& inst,
471 uint32_t num_components,
472 const std::function<spv_result_t(const std::string& message)>& diag);
473 spv_result_t ValidateF32ArrHelper(
474 const Decoration& decoration, const Instruction& inst,
475 uint32_t num_components,
476 const std::function<spv_result_t(const std::string& message)>& diag,
477 uint32_t underlying_type);
478
479 // Generates strings like "Member #0 of struct ID <2>".
480 std::string GetDefinitionDesc(const Decoration& decoration,
481 const Instruction& inst) const;
482
483 // Generates strings like "ID <51> (OpTypePointer) is referencing ID <2>
484 // (OpTypeStruct) which is decorated with BuiltIn Position".
485 std::string GetReferenceDesc(
486 const Decoration& decoration, const Instruction& built_in_inst,
487 const Instruction& referenced_inst,
488 const Instruction& referenced_from_inst,
489 SpvExecutionModel execution_model = SpvExecutionModelMax) const;
490
491 // Generates strings like "ID <51> (OpTypePointer) uses storage class
492 // UniformConstant".
493 std::string GetStorageClassDesc(const Instruction& inst) const;
494
495 // Updates inner working of the class. Is called sequentially for every
496 // instruction.
497 void Update(const Instruction& inst);
498
499 ValidationState_t& _;
500
501 // Mapping id -> list of rules which validate instruction referencing the
502 // id. Rules can create new rules and add them to this container.
503 // Using std::map, and not std::unordered_map to avoid iterator invalidation
504 // during rehashing.
505 std::map<uint32_t, std::list<std::function<spv_result_t(const Instruction&)>>>
506 id_to_at_reference_checks_;
507
508 // Id of the function we are currently inside. 0 if not inside a function.
509 uint32_t function_id_ = 0;
510
511 // Entry points which can (indirectly) call the current function.
512 // The pointer either points to a vector inside to function_to_entry_points_
513 // or to no_entry_points_. The pointer is guaranteed to never be null.
514 const std::vector<uint32_t> no_entry_points;
515 const std::vector<uint32_t>* entry_points_ = &no_entry_points;
516
517 // Execution models with which the current function can be called.
518 std::set<SpvExecutionModel> execution_models_;
519 };
520
521 void BuiltInsValidator::Update(const Instruction& inst) {
522 const SpvOp opcode = inst.opcode();
523 if (opcode == SpvOpFunction) {
524 // Entering a function.
525 assert(function_id_ == 0);
526 function_id_ = inst.id();
527 execution_models_.clear();
528 entry_points_ = &_.FunctionEntryPoints(function_id_);
529 // Collect execution models from all entry points from which the current
530 // function can be called.
531 for (const uint32_t entry_point : *entry_points_) {
532 if (const auto* models = _.GetExecutionModels(entry_point)) {
533 execution_models_.insert(models->begin(), models->end());
534 }
535 }
536 }
537
538 if (opcode == SpvOpFunctionEnd) {
539 // Exiting a function.
540 assert(function_id_ != 0);
541 function_id_ = 0;
542 entry_points_ = &no_entry_points;
543 execution_models_.clear();
544 }
545 }
546
547 std::string BuiltInsValidator::GetDefinitionDesc(
548 const Decoration& decoration, const Instruction& inst) const {
549 std::ostringstream ss;
550 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
551 assert(inst.opcode() == SpvOpTypeStruct);
552 ss << "Member #" << decoration.struct_member_index();
553 ss << " of struct ID <" << inst.id() << ">";
554 } else {
555 ss << GetIdDesc(inst);
556 }
557 return ss.str();
558 }
559
560 std::string BuiltInsValidator::GetReferenceDesc(
561 const Decoration& decoration, const Instruction& built_in_inst,
562 const Instruction& referenced_inst, const Instruction& referenced_from_inst,
563 SpvExecutionModel execution_model) const {
564 std::ostringstream ss;
565 ss << GetIdDesc(referenced_from_inst) << " is referencing "
566 << GetIdDesc(referenced_inst);
567 if (built_in_inst.id() != referenced_inst.id()) {
568 ss << " which is dependent on " << GetIdDesc(built_in_inst);
569 }
570
571 ss << " which is decorated with BuiltIn ";
572 ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
573 decoration.params()[0]);
574 if (function_id_) {
575 ss << " in function <" << function_id_ << ">";
576 if (execution_model != SpvExecutionModelMax) {
577 ss << " called with execution model ";
578 ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_EXECUTION_MODEL,
579 execution_model);
580 }
581 }
582 ss << ".";
583 return ss.str();
584 }
585
586 std::string BuiltInsValidator::GetStorageClassDesc(
587 const Instruction& inst) const {
588 std::ostringstream ss;
589 ss << GetIdDesc(inst) << " uses storage class ";
590 ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_STORAGE_CLASS,
591 GetStorageClass(inst));
592 ss << ".";
593 return ss.str();
594 }
595
596 spv_result_t BuiltInsValidator::ValidateBool(
597 const Decoration& decoration, const Instruction& inst,
598 const std::function<spv_result_t(const std::string& message)>& diag) {
599 uint32_t underlying_type = 0;
600 if (spv_result_t error =
601 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
602 return error;
603 }
604
605 if (!_.IsBoolScalarType(underlying_type)) {
606 return diag(GetDefinitionDesc(decoration, inst) + " is not a bool scalar.");
607 }
608
609 return SPV_SUCCESS;
610 }
611
612 spv_result_t BuiltInsValidator::ValidateI32(
613 const Decoration& decoration, const Instruction& inst,
614 const std::function<spv_result_t(const std::string& message)>& diag) {
615 uint32_t underlying_type = 0;
616 if (spv_result_t error =
617 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
618 return error;
619 }
620
621 return ValidateI32Helper(decoration, inst, diag, underlying_type);
622 }
623
624 spv_result_t BuiltInsValidator::ValidateOptionalArrayedI32(
625 const Decoration& decoration, const Instruction& inst,
626 const std::function<spv_result_t(const std::string& message)>& diag) {
627 uint32_t underlying_type = 0;
628 if (spv_result_t error =
629 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
630 return error;
631 }
632
633 // Strip the array, if present.
634 if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
635 underlying_type = _.FindDef(underlying_type)->word(2u);
636 }
637
638 return ValidateI32Helper(decoration, inst, diag, underlying_type);
639 }
640
641 spv_result_t BuiltInsValidator::ValidateI32Helper(
642 const Decoration& decoration, const Instruction& inst,
643 const std::function<spv_result_t(const std::string& message)>& diag,
644 uint32_t underlying_type) {
645 if (!_.IsIntScalarType(underlying_type)) {
646 return diag(GetDefinitionDesc(decoration, inst) + " is not an int scalar.");
647 }
648
649 const uint32_t bit_width = _.GetBitWidth(underlying_type);
650 if (bit_width != 32) {
651 std::ostringstream ss;
652 ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
653 << ".";
654 return diag(ss.str());
655 }
656
657 return SPV_SUCCESS;
658 }
659
660 spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32(
661 const Decoration& decoration, const Instruction& inst,
662 const std::function<spv_result_t(const std::string& message)>& diag) {
663 uint32_t underlying_type = 0;
664 if (spv_result_t error =
665 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
666 return error;
667 }
668
669 // Strip the array, if present.
670 if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
671 underlying_type = _.FindDef(underlying_type)->word(2u);
672 }
673
674 return ValidateF32Helper(decoration, inst, diag, underlying_type);
675 }
676
677 spv_result_t BuiltInsValidator::ValidateF32(
678 const Decoration& decoration, const Instruction& inst,
679 const std::function<spv_result_t(const std::string& message)>& diag) {
680 uint32_t underlying_type = 0;
681 if (spv_result_t error =
682 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
683 return error;
684 }
685
686 return ValidateF32Helper(decoration, inst, diag, underlying_type);
687 }
688
689 spv_result_t BuiltInsValidator::ValidateF32Helper(
690 const Decoration& decoration, const Instruction& inst,
691 const std::function<spv_result_t(const std::string& message)>& diag,
692 uint32_t underlying_type) {
693 if (!_.IsFloatScalarType(underlying_type)) {
694 return diag(GetDefinitionDesc(decoration, inst) +
695 " is not a float scalar.");
696 }
697
698 const uint32_t bit_width = _.GetBitWidth(underlying_type);
699 if (bit_width != 32) {
700 std::ostringstream ss;
701 ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
702 << ".";
703 return diag(ss.str());
704 }
705
706 return SPV_SUCCESS;
707 }
708
709 spv_result_t BuiltInsValidator::ValidateI32Vec(
710 const Decoration& decoration, const Instruction& inst,
711 uint32_t num_components,
712 const std::function<spv_result_t(const std::string& message)>& diag) {
713 uint32_t underlying_type = 0;
714 if (spv_result_t error =
715 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
716 return error;
717 }
718
719 if (!_.IsIntVectorType(underlying_type)) {
720 return diag(GetDefinitionDesc(decoration, inst) + " is not an int vector.");
721 }
722
723 const uint32_t actual_num_components = _.GetDimension(underlying_type);
724 if (_.GetDimension(underlying_type) != num_components) {
725 std::ostringstream ss;
726 ss << GetDefinitionDesc(decoration, inst) << " has "
727 << actual_num_components << " components.";
728 return diag(ss.str());
729 }
730
731 const uint32_t bit_width = _.GetBitWidth(underlying_type);
732 if (bit_width != 32) {
733 std::ostringstream ss;
734 ss << GetDefinitionDesc(decoration, inst)
735 << " has components with bit width " << bit_width << ".";
736 return diag(ss.str());
737 }
738
739 return SPV_SUCCESS;
740 }
741
742 spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Vec(
743 const Decoration& decoration, const Instruction& inst,
744 uint32_t num_components,
745 const std::function<spv_result_t(const std::string& message)>& diag) {
746 uint32_t underlying_type = 0;
747 if (spv_result_t error =
748 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
749 return error;
750 }
751
752 // Strip the array, if present.
753 if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
754 underlying_type = _.FindDef(underlying_type)->word(2u);
755 }
756
757 return ValidateF32VecHelper(decoration, inst, num_components, diag,
758 underlying_type);
759 }
760
761 spv_result_t BuiltInsValidator::ValidateF32Vec(
762 const Decoration& decoration, const Instruction& inst,
763 uint32_t num_components,
764 const std::function<spv_result_t(const std::string& message)>& diag) {
765 uint32_t underlying_type = 0;
766 if (spv_result_t error =
767 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
768 return error;
769 }
770
771 return ValidateF32VecHelper(decoration, inst, num_components, diag,
772 underlying_type);
773 }
774
775 spv_result_t BuiltInsValidator::ValidateF32VecHelper(
776 const Decoration& decoration, const Instruction& inst,
777 uint32_t num_components,
778 const std::function<spv_result_t(const std::string& message)>& diag,
779 uint32_t underlying_type) {
780 if (!_.IsFloatVectorType(underlying_type)) {
781 return diag(GetDefinitionDesc(decoration, inst) +
782 " is not a float vector.");
783 }
784
785 const uint32_t actual_num_components = _.GetDimension(underlying_type);
786 if (_.GetDimension(underlying_type) != num_components) {
787 std::ostringstream ss;
788 ss << GetDefinitionDesc(decoration, inst) << " has "
789 << actual_num_components << " components.";
790 return diag(ss.str());
791 }
792
793 const uint32_t bit_width = _.GetBitWidth(underlying_type);
794 if (bit_width != 32) {
795 std::ostringstream ss;
796 ss << GetDefinitionDesc(decoration, inst)
797 << " has components with bit width " << bit_width << ".";
798 return diag(ss.str());
799 }
800
801 return SPV_SUCCESS;
802 }
803
804 spv_result_t BuiltInsValidator::ValidateI32Arr(
805 const Decoration& decoration, const Instruction& inst,
806 const std::function<spv_result_t(const std::string& message)>& diag) {
807 uint32_t underlying_type = 0;
808 if (spv_result_t error =
809 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
810 return error;
811 }
812
813 const Instruction* const type_inst = _.FindDef(underlying_type);
814 if (type_inst->opcode() != SpvOpTypeArray) {
815 return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
816 }
817
818 const uint32_t component_type = type_inst->word(2);
819 if (!_.IsIntScalarType(component_type)) {
820 return diag(GetDefinitionDesc(decoration, inst) +
821 " components are not int scalar.");
822 }
823
824 const uint32_t bit_width = _.GetBitWidth(component_type);
825 if (bit_width != 32) {
826 std::ostringstream ss;
827 ss << GetDefinitionDesc(decoration, inst)
828 << " has components with bit width " << bit_width << ".";
829 return diag(ss.str());
830 }
831
832 return SPV_SUCCESS;
833 }
834
835 spv_result_t BuiltInsValidator::ValidateF32Arr(
836 const Decoration& decoration, const Instruction& inst,
837 uint32_t num_components,
838 const std::function<spv_result_t(const std::string& message)>& diag) {
839 uint32_t underlying_type = 0;
840 if (spv_result_t error =
841 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
842 return error;
843 }
844
845 return ValidateF32ArrHelper(decoration, inst, num_components, diag,
846 underlying_type);
847 }
848
849 spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Arr(
850 const Decoration& decoration, const Instruction& inst,
851 uint32_t num_components,
852 const std::function<spv_result_t(const std::string& message)>& diag) {
853 uint32_t underlying_type = 0;
854 if (spv_result_t error =
855 GetUnderlyingType(_, decoration, inst, &underlying_type)) {
856 return error;
857 }
858
859 // Strip an extra layer of arraying if present.
860 if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
861 uint32_t subtype = _.FindDef(underlying_type)->word(2u);
862 if (_.GetIdOpcode(subtype) == SpvOpTypeArray) {
863 underlying_type = subtype;
864 }
865 }
866
867 return ValidateF32ArrHelper(decoration, inst, num_components, diag,
868 underlying_type);
869 }
870
871 spv_result_t BuiltInsValidator::ValidateF32ArrHelper(
872 const Decoration& decoration, const Instruction& inst,
873 uint32_t num_components,
874 const std::function<spv_result_t(const std::string& message)>& diag,
875 uint32_t underlying_type) {
876 const Instruction* const type_inst = _.FindDef(underlying_type);
877 if (type_inst->opcode() != SpvOpTypeArray) {
878 return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
879 }
880
881 const uint32_t component_type = type_inst->word(2);
882 if (!_.IsFloatScalarType(component_type)) {
883 return diag(GetDefinitionDesc(decoration, inst) +
884 " components are not float scalar.");
885 }
886
887 const uint32_t bit_width = _.GetBitWidth(component_type);
888 if (bit_width != 32) {
889 std::ostringstream ss;
890 ss << GetDefinitionDesc(decoration, inst)
891 << " has components with bit width " << bit_width << ".";
892 return diag(ss.str());
893 }
894
895 if (num_components != 0) {
896 uint64_t actual_num_components = 0;
897 if (!_.GetConstantValUint64(type_inst->word(3), &actual_num_components)) {
898 assert(0 && "Array type definition is corrupt");
899 }
900 if (actual_num_components != num_components) {
901 std::ostringstream ss;
902 ss << GetDefinitionDesc(decoration, inst) << " has "
903 << actual_num_components << " components.";
904 return diag(ss.str());
905 }
906 }
907
908 return SPV_SUCCESS;
909 }
910
911 spv_result_t BuiltInsValidator::ValidateNotCalledWithExecutionModel(
912 std::string comment, SpvExecutionModel execution_model,
913 const Decoration& decoration, const Instruction& built_in_inst,
914 const Instruction& referenced_inst,
915 const Instruction& referenced_from_inst) {
916 if (function_id_) {
917 if (execution_models_.count(execution_model)) {
918 const char* execution_model_str = _.grammar().lookupOperandName(
919 SPV_OPERAND_TYPE_EXECUTION_MODEL, execution_model);
920 const char* built_in_str = _.grammar().lookupOperandName(
921 SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0]);
922 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
923 << comment << " " << GetIdDesc(referenced_inst) << " depends on "
924 << GetIdDesc(built_in_inst) << " which is decorated with BuiltIn "
925 << built_in_str << "."
926 << " Id <" << referenced_inst.id() << "> is later referenced by "
927 << GetIdDesc(referenced_from_inst) << " in function <"
928 << function_id_ << "> which is called with execution model "
929 << execution_model_str << ".";
930 }
931 } else {
932 // Propagate this rule to all dependant ids in the global scope.
933 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
934 std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
935 comment, execution_model, decoration, built_in_inst,
936 referenced_from_inst, std::placeholders::_1));
937 }
938 return SPV_SUCCESS;
939 }
940
941 spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtDefinition(
942 const Decoration& decoration, const Instruction& inst) {
943 // Seed at reference checks with this built-in.
944 return ValidateClipOrCullDistanceAtReference(decoration, inst, inst, inst);
945 }
946
947 spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtReference(
948 const Decoration& decoration, const Instruction& built_in_inst,
949 const Instruction& referenced_inst,
950 const Instruction& referenced_from_inst) {
951 uint32_t operand = decoration.params()[0];
952 if (spvIsVulkanEnv(_.context()->target_env)) {
953 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
954 if (storage_class != SpvStorageClassMax &&
955 storage_class != SpvStorageClassInput &&
956 storage_class != SpvStorageClassOutput) {
957 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
958 << "Vulkan spec allows BuiltIn "
959 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
960 operand)
961 << " to be only used for variables with Input or Output storage "
962 "class. "
963 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
964 referenced_from_inst)
965 << " " << GetStorageClassDesc(referenced_from_inst);
966 }
967
968 if (storage_class == SpvStorageClassInput) {
969 assert(function_id_ == 0);
970 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
971 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
972 "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
973 "used for variables with Input storage class if execution model is "
974 "Vertex.",
975 SpvExecutionModelVertex, decoration, built_in_inst,
976 referenced_from_inst, std::placeholders::_1));
977 }
978
979 if (storage_class == SpvStorageClassOutput) {
980 assert(function_id_ == 0);
981 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
982 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
983 "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
984 "used for variables with Output storage class if execution model is "
985 "Fragment.",
986 SpvExecutionModelFragment, decoration, built_in_inst,
987 referenced_from_inst, std::placeholders::_1));
988 }
989
990 for (const SpvExecutionModel execution_model : execution_models_) {
991 switch (execution_model) {
992 case SpvExecutionModelFragment:
993 case SpvExecutionModelVertex: {
994 if (spv_result_t error = ValidateF32Arr(
995 decoration, built_in_inst, /* Any number of components */ 0,
996 [this, &decoration, &referenced_from_inst](
997 const std::string& message) -> spv_result_t {
998 uint32_t vuid =
999 (decoration.params()[0] == SpvBuiltInClipDistance)
1000 ? 4191
1001 : 4200;
1002 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1003 << _.VkErrorID(vuid)
1004 << "According to the Vulkan spec BuiltIn "
1005 << _.grammar().lookupOperandName(
1006 SPV_OPERAND_TYPE_BUILT_IN,
1007 decoration.params()[0])
1008 << " variable needs to be a 32-bit float array. "
1009 << message;
1010 })) {
1011 return error;
1012 }
1013 break;
1014 }
1015 case SpvExecutionModelTessellationControl:
1016 case SpvExecutionModelTessellationEvaluation:
1017 case SpvExecutionModelGeometry:
1018 case SpvExecutionModelMeshNV: {
1019 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
1020 // The outer level of array is applied on the variable.
1021 if (spv_result_t error = ValidateF32Arr(
1022 decoration, built_in_inst, /* Any number of components */ 0,
1023 [this, &decoration, &referenced_from_inst](
1024 const std::string& message) -> spv_result_t {
1025 uint32_t vuid =
1026 (decoration.params()[0] == SpvBuiltInClipDistance)
1027 ? 4191
1028 : 4200;
1029 return _.diag(SPV_ERROR_INVALID_DATA,
1030 &referenced_from_inst)
1031 << _.VkErrorID(vuid)
1032 << "According to the Vulkan spec BuiltIn "
1033 << _.grammar().lookupOperandName(
1034 SPV_OPERAND_TYPE_BUILT_IN,
1035 decoration.params()[0])
1036 << " variable needs to be a 32-bit float array. "
1037 << message;
1038 })) {
1039 return error;
1040 }
1041 } else {
1042 if (spv_result_t error = ValidateOptionalArrayedF32Arr(
1043 decoration, built_in_inst, /* Any number of components */ 0,
1044 [this, &decoration, &referenced_from_inst](
1045 const std::string& message) -> spv_result_t {
1046 uint32_t vuid =
1047 (decoration.params()[0] == SpvBuiltInClipDistance)
1048 ? 4191
1049 : 4200;
1050 return _.diag(SPV_ERROR_INVALID_DATA,
1051 &referenced_from_inst)
1052 << _.VkErrorID(vuid)
1053 << "According to the Vulkan spec BuiltIn "
1054 << _.grammar().lookupOperandName(
1055 SPV_OPERAND_TYPE_BUILT_IN,
1056 decoration.params()[0])
1057 << " variable needs to be a 32-bit float array. "
1058 << message;
1059 })) {
1060 return error;
1061 }
1062 }
1063 break;
1064 }
1065
1066 default: {
1067 uint32_t vuid =
1068 (decoration.params()[0] == SpvBuiltInClipDistance) ? 4187 : 4196;
1069 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1070 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
1071 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
1072 operand)
1073 << " to be used only with Fragment, Vertex, "
1074 "TessellationControl, TessellationEvaluation or Geometry "
1075 "execution models. "
1076 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1077 referenced_from_inst, execution_model);
1078 }
1079 }
1080 }
1081 }
1082
1083 if (function_id_ == 0) {
1084 // Propagate this rule to all dependant ids in the global scope.
1085 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
1086 std::bind(&BuiltInsValidator::ValidateClipOrCullDistanceAtReference,
1087 this, decoration, built_in_inst, referenced_from_inst,
1088 std::placeholders::_1));
1089 }
1090
1091 return SPV_SUCCESS;
1092 }
1093
1094 spv_result_t BuiltInsValidator::ValidateFragCoordAtDefinition(
1095 const Decoration& decoration, const Instruction& inst) {
1096 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1097 if (spv_result_t error = ValidateF32Vec(
1098 decoration, inst, 4,
1099 [this, &inst](const std::string& message) -> spv_result_t {
1100 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1101 << _.VkErrorID(4212) << "According to the "
1102 << spvLogStringForEnv(_.context()->target_env)
1103 << " spec BuiltIn FragCoord "
1104 "variable needs to be a 4-component 32-bit float "
1105 "vector. "
1106 << message;
1107 })) {
1108 return error;
1109 }
1110 }
1111
1112 // Seed at reference checks with this built-in.
1113 return ValidateFragCoordAtReference(decoration, inst, inst, inst);
1114 }
1115
1116 spv_result_t BuiltInsValidator::ValidateFragCoordAtReference(
1117 const Decoration& decoration, const Instruction& built_in_inst,
1118 const Instruction& referenced_inst,
1119 const Instruction& referenced_from_inst) {
1120 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1121 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1122 if (storage_class != SpvStorageClassMax &&
1123 storage_class != SpvStorageClassInput) {
1124 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1125 << _.VkErrorID(4211) << spvLogStringForEnv(_.context()->target_env)
1126 << " spec allows BuiltIn FragCoord to be only used for "
1127 "variables with Input storage class. "
1128 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1129 referenced_from_inst)
1130 << " " << GetStorageClassDesc(referenced_from_inst);
1131 }
1132
1133 for (const SpvExecutionModel execution_model : execution_models_) {
1134 if (execution_model != SpvExecutionModelFragment) {
1135 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1136 << _.VkErrorID(4210)
1137 << spvLogStringForEnv(_.context()->target_env)
1138 << " spec allows BuiltIn FragCoord to be used only with "
1139 "Fragment execution model. "
1140 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1141 referenced_from_inst, execution_model);
1142 }
1143 }
1144 }
1145
1146 if (function_id_ == 0) {
1147 // Propagate this rule to all dependant ids in the global scope.
1148 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1149 &BuiltInsValidator::ValidateFragCoordAtReference, this, decoration,
1150 built_in_inst, referenced_from_inst, std::placeholders::_1));
1151 }
1152
1153 return SPV_SUCCESS;
1154 }
1155
1156 spv_result_t BuiltInsValidator::ValidateFragDepthAtDefinition(
1157 const Decoration& decoration, const Instruction& inst) {
1158 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1159 if (spv_result_t error = ValidateF32(
1160 decoration, inst,
1161 [this, &inst](const std::string& message) -> spv_result_t {
1162 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1163 << _.VkErrorID(4215) << "According to the "
1164 << spvLogStringForEnv(_.context()->target_env)
1165 << " spec BuiltIn FragDepth "
1166 "variable needs to be a 32-bit float scalar. "
1167 << message;
1168 })) {
1169 return error;
1170 }
1171 }
1172
1173 // Seed at reference checks with this built-in.
1174 return ValidateFragDepthAtReference(decoration, inst, inst, inst);
1175 }
1176
1177 spv_result_t BuiltInsValidator::ValidateFragDepthAtReference(
1178 const Decoration& decoration, const Instruction& built_in_inst,
1179 const Instruction& referenced_inst,
1180 const Instruction& referenced_from_inst) {
1181 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1182 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1183 if (storage_class != SpvStorageClassMax &&
1184 storage_class != SpvStorageClassOutput) {
1185 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1186 << _.VkErrorID(4214) << spvLogStringForEnv(_.context()->target_env)
1187 << " spec allows BuiltIn FragDepth to be only used for "
1188 "variables with Output storage class. "
1189 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1190 referenced_from_inst)
1191 << " " << GetStorageClassDesc(referenced_from_inst);
1192 }
1193
1194 for (const SpvExecutionModel execution_model : execution_models_) {
1195 if (execution_model != SpvExecutionModelFragment) {
1196 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1197 << _.VkErrorID(4213)
1198 << spvLogStringForEnv(_.context()->target_env)
1199 << " spec allows BuiltIn FragDepth to be used only with "
1200 "Fragment execution model. "
1201 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1202 referenced_from_inst, execution_model);
1203 }
1204 }
1205
1206 for (const uint32_t entry_point : *entry_points_) {
1207 // Every entry point from which this function is called needs to have
1208 // Execution Mode DepthReplacing.
1209 const auto* modes = _.GetExecutionModes(entry_point);
1210 if (!modes || !modes->count(SpvExecutionModeDepthReplacing)) {
1211 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1212 << _.VkErrorID(4216)
1213 << spvLogStringForEnv(_.context()->target_env)
1214 << " spec requires DepthReplacing execution mode to be "
1215 "declared when using BuiltIn FragDepth. "
1216 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1217 referenced_from_inst);
1218 }
1219 }
1220 }
1221
1222 if (function_id_ == 0) {
1223 // Propagate this rule to all dependant ids in the global scope.
1224 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1225 &BuiltInsValidator::ValidateFragDepthAtReference, this, decoration,
1226 built_in_inst, referenced_from_inst, std::placeholders::_1));
1227 }
1228
1229 return SPV_SUCCESS;
1230 }
1231
1232 spv_result_t BuiltInsValidator::ValidateFrontFacingAtDefinition(
1233 const Decoration& decoration, const Instruction& inst) {
1234 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1235 if (spv_result_t error = ValidateBool(
1236 decoration, inst,
1237 [this, &inst](const std::string& message) -> spv_result_t {
1238 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1239 << _.VkErrorID(4231) << "According to the "
1240 << spvLogStringForEnv(_.context()->target_env)
1241 << " spec BuiltIn FrontFacing "
1242 "variable needs to be a bool scalar. "
1243 << message;
1244 })) {
1245 return error;
1246 }
1247 }
1248
1249 // Seed at reference checks with this built-in.
1250 return ValidateFrontFacingAtReference(decoration, inst, inst, inst);
1251 }
1252
1253 spv_result_t BuiltInsValidator::ValidateFrontFacingAtReference(
1254 const Decoration& decoration, const Instruction& built_in_inst,
1255 const Instruction& referenced_inst,
1256 const Instruction& referenced_from_inst) {
1257 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1258 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1259 if (storage_class != SpvStorageClassMax &&
1260 storage_class != SpvStorageClassInput) {
1261 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1262 << _.VkErrorID(4230) << spvLogStringForEnv(_.context()->target_env)
1263 << " spec allows BuiltIn FrontFacing to be only used for "
1264 "variables with Input storage class. "
1265 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1266 referenced_from_inst)
1267 << " " << GetStorageClassDesc(referenced_from_inst);
1268 }
1269
1270 for (const SpvExecutionModel execution_model : execution_models_) {
1271 if (execution_model != SpvExecutionModelFragment) {
1272 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1273 << _.VkErrorID(4229)
1274 << spvLogStringForEnv(_.context()->target_env)
1275 << " spec allows BuiltIn FrontFacing to be used only with "
1276 "Fragment execution model. "
1277 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1278 referenced_from_inst, execution_model);
1279 }
1280 }
1281 }
1282
1283 if (function_id_ == 0) {
1284 // Propagate this rule to all dependant ids in the global scope.
1285 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1286 &BuiltInsValidator::ValidateFrontFacingAtReference, this, decoration,
1287 built_in_inst, referenced_from_inst, std::placeholders::_1));
1288 }
1289
1290 return SPV_SUCCESS;
1291 }
1292
1293 spv_result_t BuiltInsValidator::ValidateHelperInvocationAtDefinition(
1294 const Decoration& decoration, const Instruction& inst) {
1295 if (spvIsVulkanEnv(_.context()->target_env)) {
1296 if (spv_result_t error = ValidateBool(
1297 decoration, inst,
1298 [this, &inst](const std::string& message) -> spv_result_t {
1299 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1300 << _.VkErrorID(4241)
1301 << "According to the Vulkan spec BuiltIn HelperInvocation "
1302 "variable needs to be a bool scalar. "
1303 << message;
1304 })) {
1305 return error;
1306 }
1307 }
1308
1309 // Seed at reference checks with this built-in.
1310 return ValidateHelperInvocationAtReference(decoration, inst, inst, inst);
1311 }
1312
1313 spv_result_t BuiltInsValidator::ValidateHelperInvocationAtReference(
1314 const Decoration& decoration, const Instruction& built_in_inst,
1315 const Instruction& referenced_inst,
1316 const Instruction& referenced_from_inst) {
1317 if (spvIsVulkanEnv(_.context()->target_env)) {
1318 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1319 if (storage_class != SpvStorageClassMax &&
1320 storage_class != SpvStorageClassInput) {
1321 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1322 << _.VkErrorID(4240)
1323 << "Vulkan spec allows BuiltIn HelperInvocation to be only used "
1324 "for variables with Input storage class. "
1325 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1326 referenced_from_inst)
1327 << " " << GetStorageClassDesc(referenced_from_inst);
1328 }
1329
1330 for (const SpvExecutionModel execution_model : execution_models_) {
1331 if (execution_model != SpvExecutionModelFragment) {
1332 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1333 << _.VkErrorID(4239)
1334 << "Vulkan spec allows BuiltIn HelperInvocation to be used only "
1335 "with Fragment execution model. "
1336 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1337 referenced_from_inst, execution_model);
1338 }
1339 }
1340 }
1341
1342 if (function_id_ == 0) {
1343 // Propagate this rule to all dependant ids in the global scope.
1344 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
1345 std::bind(&BuiltInsValidator::ValidateHelperInvocationAtReference, this,
1346 decoration, built_in_inst, referenced_from_inst,
1347 std::placeholders::_1));
1348 }
1349
1350 return SPV_SUCCESS;
1351 }
1352
1353 spv_result_t BuiltInsValidator::ValidateInvocationIdAtDefinition(
1354 const Decoration& decoration, const Instruction& inst) {
1355 if (spvIsVulkanEnv(_.context()->target_env)) {
1356 if (spv_result_t error = ValidateI32(
1357 decoration, inst,
1358 [this, &inst](const std::string& message) -> spv_result_t {
1359 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1360 << _.VkErrorID(4259)
1361 << "According to the Vulkan spec BuiltIn InvocationId "
1362 "variable needs to be a 32-bit int scalar. "
1363 << message;
1364 })) {
1365 return error;
1366 }
1367 }
1368
1369 // Seed at reference checks with this built-in.
1370 return ValidateInvocationIdAtReference(decoration, inst, inst, inst);
1371 }
1372
1373 spv_result_t BuiltInsValidator::ValidateInvocationIdAtReference(
1374 const Decoration& decoration, const Instruction& built_in_inst,
1375 const Instruction& referenced_inst,
1376 const Instruction& referenced_from_inst) {
1377 if (spvIsVulkanEnv(_.context()->target_env)) {
1378 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1379 if (storage_class != SpvStorageClassMax &&
1380 storage_class != SpvStorageClassInput) {
1381 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1382 << _.VkErrorID(4258)
1383 << "Vulkan spec allows BuiltIn InvocationId to be only used for "
1384 "variables with Input storage class. "
1385 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1386 referenced_from_inst)
1387 << " " << GetStorageClassDesc(referenced_from_inst);
1388 }
1389
1390 for (const SpvExecutionModel execution_model : execution_models_) {
1391 if (execution_model != SpvExecutionModelTessellationControl &&
1392 execution_model != SpvExecutionModelGeometry) {
1393 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1394 << _.VkErrorID(4257)
1395 << "Vulkan spec allows BuiltIn InvocationId to be used only "
1396 "with TessellationControl or Geometry execution models. "
1397 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1398 referenced_from_inst, execution_model);
1399 }
1400 }
1401 }
1402
1403 if (function_id_ == 0) {
1404 // Propagate this rule to all dependant ids in the global scope.
1405 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1406 &BuiltInsValidator::ValidateInvocationIdAtReference, this, decoration,
1407 built_in_inst, referenced_from_inst, std::placeholders::_1));
1408 }
1409
1410 return SPV_SUCCESS;
1411 }
1412
1413 spv_result_t BuiltInsValidator::ValidateInstanceIndexAtDefinition(
1414 const Decoration& decoration, const Instruction& inst) {
1415 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1416 if (spv_result_t error = ValidateI32(
1417 decoration, inst,
1418 [this, &inst](const std::string& message) -> spv_result_t {
1419 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1420 << _.VkErrorID(4265) << "According to the "
1421 << spvLogStringForEnv(_.context()->target_env)
1422 << " spec BuiltIn InstanceIndex "
1423 "variable needs to be a 32-bit int scalar. "
1424 << message;
1425 })) {
1426 return error;
1427 }
1428 }
1429
1430 // Seed at reference checks with this built-in.
1431 return ValidateInstanceIndexAtReference(decoration, inst, inst, inst);
1432 }
1433
1434 spv_result_t BuiltInsValidator::ValidateInstanceIndexAtReference(
1435 const Decoration& decoration, const Instruction& built_in_inst,
1436 const Instruction& referenced_inst,
1437 const Instruction& referenced_from_inst) {
1438 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
1439 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1440 if (storage_class != SpvStorageClassMax &&
1441 storage_class != SpvStorageClassInput) {
1442 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1443 << _.VkErrorID(4264) << spvLogStringForEnv(_.context()->target_env)
1444 << " spec allows BuiltIn InstanceIndex to be only used for "
1445 "variables with Input storage class. "
1446 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1447 referenced_from_inst)
1448 << " " << GetStorageClassDesc(referenced_from_inst);
1449 }
1450
1451 for (const SpvExecutionModel execution_model : execution_models_) {
1452 if (execution_model != SpvExecutionModelVertex) {
1453 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1454 << _.VkErrorID(4263)
1455 << spvLogStringForEnv(_.context()->target_env)
1456 << " spec allows BuiltIn InstanceIndex to be used only "
1457 "with Vertex execution model. "
1458 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1459 referenced_from_inst, execution_model);
1460 }
1461 }
1462 }
1463
1464 if (function_id_ == 0) {
1465 // Propagate this rule to all dependant ids in the global scope.
1466 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1467 &BuiltInsValidator::ValidateInstanceIndexAtReference, this, decoration,
1468 built_in_inst, referenced_from_inst, std::placeholders::_1));
1469 }
1470
1471 return SPV_SUCCESS;
1472 }
1473
1474 spv_result_t BuiltInsValidator::ValidatePatchVerticesAtDefinition(
1475 const Decoration& decoration, const Instruction& inst) {
1476 if (spvIsVulkanEnv(_.context()->target_env)) {
1477 if (spv_result_t error = ValidateI32(
1478 decoration, inst,
1479 [this, &inst](const std::string& message) -> spv_result_t {
1480 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1481 << _.VkErrorID(4310)
1482 << "According to the Vulkan spec BuiltIn PatchVertices "
1483 "variable needs to be a 32-bit int scalar. "
1484 << message;
1485 })) {
1486 return error;
1487 }
1488 }
1489
1490 // Seed at reference checks with this built-in.
1491 return ValidatePatchVerticesAtReference(decoration, inst, inst, inst);
1492 }
1493
1494 spv_result_t BuiltInsValidator::ValidatePatchVerticesAtReference(
1495 const Decoration& decoration, const Instruction& built_in_inst,
1496 const Instruction& referenced_inst,
1497 const Instruction& referenced_from_inst) {
1498 if (spvIsVulkanEnv(_.context()->target_env)) {
1499 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1500 if (storage_class != SpvStorageClassMax &&
1501 storage_class != SpvStorageClassInput) {
1502 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1503 << _.VkErrorID(4309)
1504 << "Vulkan spec allows BuiltIn PatchVertices to be only used for "
1505 "variables with Input storage class. "
1506 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1507 referenced_from_inst)
1508 << " " << GetStorageClassDesc(referenced_from_inst);
1509 }
1510
1511 for (const SpvExecutionModel execution_model : execution_models_) {
1512 if (execution_model != SpvExecutionModelTessellationControl &&
1513 execution_model != SpvExecutionModelTessellationEvaluation) {
1514 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1515 << _.VkErrorID(4308)
1516 << "Vulkan spec allows BuiltIn PatchVertices to be used only "
1517 "with TessellationControl or TessellationEvaluation "
1518 "execution models. "
1519 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1520 referenced_from_inst, execution_model);
1521 }
1522 }
1523 }
1524
1525 if (function_id_ == 0) {
1526 // Propagate this rule to all dependant ids in the global scope.
1527 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1528 &BuiltInsValidator::ValidatePatchVerticesAtReference, this, decoration,
1529 built_in_inst, referenced_from_inst, std::placeholders::_1));
1530 }
1531
1532 return SPV_SUCCESS;
1533 }
1534
1535 spv_result_t BuiltInsValidator::ValidatePointCoordAtDefinition(
1536 const Decoration& decoration, const Instruction& inst) {
1537 if (spvIsVulkanEnv(_.context()->target_env)) {
1538 if (spv_result_t error = ValidateF32Vec(
1539 decoration, inst, 2,
1540 [this, &inst](const std::string& message) -> spv_result_t {
1541 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1542 << _.VkErrorID(4313)
1543 << "According to the Vulkan spec BuiltIn PointCoord "
1544 "variable needs to be a 2-component 32-bit float "
1545 "vector. "
1546 << message;
1547 })) {
1548 return error;
1549 }
1550 }
1551
1552 // Seed at reference checks with this built-in.
1553 return ValidatePointCoordAtReference(decoration, inst, inst, inst);
1554 }
1555
1556 spv_result_t BuiltInsValidator::ValidatePointCoordAtReference(
1557 const Decoration& decoration, const Instruction& built_in_inst,
1558 const Instruction& referenced_inst,
1559 const Instruction& referenced_from_inst) {
1560 if (spvIsVulkanEnv(_.context()->target_env)) {
1561 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1562 if (storage_class != SpvStorageClassMax &&
1563 storage_class != SpvStorageClassInput) {
1564 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1565 << _.VkErrorID(4312)
1566 << "Vulkan spec allows BuiltIn PointCoord to be only used for "
1567 "variables with Input storage class. "
1568 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1569 referenced_from_inst)
1570 << " " << GetStorageClassDesc(referenced_from_inst);
1571 }
1572
1573 for (const SpvExecutionModel execution_model : execution_models_) {
1574 if (execution_model != SpvExecutionModelFragment) {
1575 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1576 << _.VkErrorID(4311)
1577 << "Vulkan spec allows BuiltIn PointCoord to be used only with "
1578 "Fragment execution model. "
1579 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1580 referenced_from_inst, execution_model);
1581 }
1582 }
1583 }
1584
1585 if (function_id_ == 0) {
1586 // Propagate this rule to all dependant ids in the global scope.
1587 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1588 &BuiltInsValidator::ValidatePointCoordAtReference, this, decoration,
1589 built_in_inst, referenced_from_inst, std::placeholders::_1));
1590 }
1591
1592 return SPV_SUCCESS;
1593 }
1594
1595 spv_result_t BuiltInsValidator::ValidatePointSizeAtDefinition(
1596 const Decoration& decoration, const Instruction& inst) {
1597 // Seed at reference checks with this built-in.
1598 return ValidatePointSizeAtReference(decoration, inst, inst, inst);
1599 }
1600
1601 spv_result_t BuiltInsValidator::ValidatePointSizeAtReference(
1602 const Decoration& decoration, const Instruction& built_in_inst,
1603 const Instruction& referenced_inst,
1604 const Instruction& referenced_from_inst) {
1605 if (spvIsVulkanEnv(_.context()->target_env)) {
1606 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1607 if (storage_class != SpvStorageClassMax &&
1608 storage_class != SpvStorageClassInput &&
1609 storage_class != SpvStorageClassOutput) {
1610 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1611 << _.VkErrorID(4316)
1612 << "Vulkan spec allows BuiltIn PointSize to be only used for "
1613 "variables with Input or Output storage class. "
1614 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1615 referenced_from_inst)
1616 << " " << GetStorageClassDesc(referenced_from_inst);
1617 }
1618
1619 if (storage_class == SpvStorageClassInput) {
1620 assert(function_id_ == 0);
1621 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1622 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
1623 std::string(
1624 _.VkErrorID(4315) +
1625 "Vulkan spec doesn't allow BuiltIn PointSize to be used for "
1626 "variables with Input storage class if execution model is "
1627 "Vertex."),
1628 SpvExecutionModelVertex, decoration, built_in_inst,
1629 referenced_from_inst, std::placeholders::_1));
1630 }
1631
1632 for (const SpvExecutionModel execution_model : execution_models_) {
1633 switch (execution_model) {
1634 case SpvExecutionModelVertex: {
1635 if (spv_result_t error = ValidateF32(
1636 decoration, built_in_inst,
1637 [this, &referenced_from_inst](
1638 const std::string& message) -> spv_result_t {
1639 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1640 << _.VkErrorID(4317)
1641 << "According to the Vulkan spec BuiltIn PointSize "
1642 "variable needs to be a 32-bit float scalar. "
1643 << message;
1644 })) {
1645 return error;
1646 }
1647 break;
1648 }
1649 case SpvExecutionModelTessellationControl:
1650 case SpvExecutionModelTessellationEvaluation:
1651 case SpvExecutionModelGeometry:
1652 case SpvExecutionModelMeshNV: {
1653 // PointSize can be a per-vertex variable for tessellation control,
1654 // tessellation evaluation and geometry shader stages. In such cases
1655 // variables will have an array of 32-bit floats.
1656 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
1657 // The array is on the variable, so this must be a 32-bit float.
1658 if (spv_result_t error = ValidateF32(
1659 decoration, built_in_inst,
1660 [this, &referenced_from_inst](
1661 const std::string& message) -> spv_result_t {
1662 return _.diag(SPV_ERROR_INVALID_DATA,
1663 &referenced_from_inst)
1664 << _.VkErrorID(4317)
1665 << "According to the Vulkan spec BuiltIn "
1666 "PointSize variable needs to be a 32-bit "
1667 "float scalar. "
1668 << message;
1669 })) {
1670 return error;
1671 }
1672 } else {
1673 if (spv_result_t error = ValidateOptionalArrayedF32(
1674 decoration, built_in_inst,
1675 [this, &referenced_from_inst](
1676 const std::string& message) -> spv_result_t {
1677 return _.diag(SPV_ERROR_INVALID_DATA,
1678 &referenced_from_inst)
1679 << _.VkErrorID(4317)
1680 << "According to the Vulkan spec BuiltIn "
1681 "PointSize variable needs to be a 32-bit "
1682 "float scalar. "
1683 << message;
1684 })) {
1685 return error;
1686 }
1687 }
1688 break;
1689 }
1690
1691 default: {
1692 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1693 << _.VkErrorID(4314)
1694 << "Vulkan spec allows BuiltIn PointSize to be used only with "
1695 "Vertex, TessellationControl, TessellationEvaluation or "
1696 "Geometry execution models. "
1697 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1698 referenced_from_inst, execution_model);
1699 }
1700 }
1701 }
1702 }
1703
1704 if (function_id_ == 0) {
1705 // Propagate this rule to all dependant ids in the global scope.
1706 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1707 &BuiltInsValidator::ValidatePointSizeAtReference, this, decoration,
1708 built_in_inst, referenced_from_inst, std::placeholders::_1));
1709 }
1710
1711 return SPV_SUCCESS;
1712 }
1713
1714 spv_result_t BuiltInsValidator::ValidatePositionAtDefinition(
1715 const Decoration& decoration, const Instruction& inst) {
1716 // Seed at reference checks with this built-in.
1717 return ValidatePositionAtReference(decoration, inst, inst, inst);
1718 }
1719
1720 spv_result_t BuiltInsValidator::ValidatePositionAtReference(
1721 const Decoration& decoration, const Instruction& built_in_inst,
1722 const Instruction& referenced_inst,
1723 const Instruction& referenced_from_inst) {
1724 if (spvIsVulkanEnv(_.context()->target_env)) {
1725 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1726 if (storage_class != SpvStorageClassMax &&
1727 storage_class != SpvStorageClassInput &&
1728 storage_class != SpvStorageClassOutput) {
1729 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1730 << "Vulkan spec allows BuiltIn Position to be only used for "
1731 "variables with Input or Output storage class. "
1732 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1733 referenced_from_inst)
1734 << " " << GetStorageClassDesc(referenced_from_inst);
1735 }
1736
1737 if (storage_class == SpvStorageClassInput) {
1738 assert(function_id_ == 0);
1739 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1740 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
1741 std::string(_.VkErrorID(4320) +
1742 "Vulkan spec doesn't allow BuiltIn Position to be used "
1743 "for variables "
1744 "with Input storage class if execution model is Vertex."),
1745 SpvExecutionModelVertex, decoration, built_in_inst,
1746 referenced_from_inst, std::placeholders::_1));
1747 }
1748
1749 for (const SpvExecutionModel execution_model : execution_models_) {
1750 switch (execution_model) {
1751 case SpvExecutionModelVertex: {
1752 if (spv_result_t error = ValidateF32Vec(
1753 decoration, built_in_inst, 4,
1754 [this, &referenced_from_inst](
1755 const std::string& message) -> spv_result_t {
1756 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1757 << _.VkErrorID(4321)
1758 << "According to the Vulkan spec BuiltIn Position "
1759 "variable needs to be a 4-component 32-bit float "
1760 "vector. "
1761 << message;
1762 })) {
1763 return error;
1764 }
1765 break;
1766 }
1767 case SpvExecutionModelGeometry:
1768 case SpvExecutionModelTessellationControl:
1769 case SpvExecutionModelTessellationEvaluation:
1770 case SpvExecutionModelMeshNV: {
1771 // Position can be a per-vertex variable for tessellation control,
1772 // tessellation evaluation, geometry and mesh shader stages. In such
1773 // cases variables will have an array of 4-component 32-bit float
1774 // vectors.
1775 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
1776 // The array is on the variable, so this must be a 4-component
1777 // 32-bit float vector.
1778 if (spv_result_t error = ValidateF32Vec(
1779 decoration, built_in_inst, 4,
1780 [this, &referenced_from_inst](
1781 const std::string& message) -> spv_result_t {
1782 return _.diag(SPV_ERROR_INVALID_DATA,
1783 &referenced_from_inst)
1784 << _.VkErrorID(4321)
1785 << "According to the Vulkan spec BuiltIn Position "
1786 "variable needs to be a 4-component 32-bit "
1787 "float vector. "
1788 << message;
1789 })) {
1790 return error;
1791 }
1792 } else {
1793 if (spv_result_t error = ValidateOptionalArrayedF32Vec(
1794 decoration, built_in_inst, 4,
1795 [this, &referenced_from_inst](
1796 const std::string& message) -> spv_result_t {
1797 return _.diag(SPV_ERROR_INVALID_DATA,
1798 &referenced_from_inst)
1799 << _.VkErrorID(4321)
1800 << "According to the Vulkan spec BuiltIn Position "
1801 "variable needs to be a 4-component 32-bit "
1802 "float vector. "
1803 << message;
1804 })) {
1805 return error;
1806 }
1807 }
1808 break;
1809 }
1810
1811 default: {
1812 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1813 << _.VkErrorID(4318)
1814 << "Vulkan spec allows BuiltIn Position to be used only "
1815 "with Vertex, TessellationControl, TessellationEvaluation"
1816 " or Geometry execution models. "
1817 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1818 referenced_from_inst, execution_model);
1819 }
1820 }
1821 }
1822 }
1823
1824 if (spvIsWebGPUEnv(_.context()->target_env)) {
1825 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1826 if (storage_class != SpvStorageClassMax &&
1827 storage_class != SpvStorageClassOutput) {
1828 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1829 << "WebGPU spec allows BuiltIn Position to be only used for "
1830 "variables with Output storage class. "
1831 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1832 referenced_from_inst)
1833 << " " << GetStorageClassDesc(referenced_from_inst);
1834 }
1835
1836 for (const SpvExecutionModel execution_model : execution_models_) {
1837 switch (execution_model) {
1838 case SpvExecutionModelVertex: {
1839 if (spv_result_t error = ValidateF32Vec(
1840 decoration, built_in_inst, 4,
1841 [this, &referenced_from_inst](
1842 const std::string& message) -> spv_result_t {
1843 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1844 << "According to the WebGPU spec BuiltIn Position "
1845 "variable needs to be a 4-component 32-bit float "
1846 "vector. "
1847 << message;
1848 })) {
1849 return error;
1850 }
1851 break;
1852 }
1853 default: {
1854 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1855 << "WebGPU spec allows BuiltIn Position to be used only "
1856 "with the Vertex execution model. "
1857 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1858 referenced_from_inst, execution_model);
1859 }
1860 }
1861 }
1862 }
1863
1864 if (function_id_ == 0) {
1865 // Propagate this rule to all dependant ids in the global scope.
1866 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1867 &BuiltInsValidator::ValidatePositionAtReference, this, decoration,
1868 built_in_inst, referenced_from_inst, std::placeholders::_1));
1869 }
1870
1871 return SPV_SUCCESS;
1872 }
1873
1874 spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtDefinition(
1875 const Decoration& decoration, const Instruction& inst) {
1876 if (spvIsVulkanEnv(_.context()->target_env)) {
1877 // PrimitiveId can be a per-primitive variable for mesh shader stage.
1878 // In such cases variable will have an array of 32-bit integers.
1879 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
1880 // This must be a 32-bit int scalar.
1881 if (spv_result_t error = ValidateI32(
1882 decoration, inst,
1883 [this, &inst](const std::string& message) -> spv_result_t {
1884 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1885 << _.VkErrorID(4337)
1886 << "According to the Vulkan spec BuiltIn PrimitiveId "
1887 "variable needs to be a 32-bit int scalar. "
1888 << message;
1889 })) {
1890 return error;
1891 }
1892 } else {
1893 if (spv_result_t error = ValidateOptionalArrayedI32(
1894 decoration, inst,
1895 [this, &inst](const std::string& message) -> spv_result_t {
1896 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
1897 << _.VkErrorID(4337)
1898 << "According to the Vulkan spec BuiltIn PrimitiveId "
1899 "variable needs to be a 32-bit int scalar. "
1900 << message;
1901 })) {
1902 return error;
1903 }
1904 }
1905 }
1906
1907 // Seed at reference checks with this built-in.
1908 return ValidatePrimitiveIdAtReference(decoration, inst, inst, inst);
1909 }
1910
1911 spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtReference(
1912 const Decoration& decoration, const Instruction& built_in_inst,
1913 const Instruction& referenced_inst,
1914 const Instruction& referenced_from_inst) {
1915 if (spvIsVulkanEnv(_.context()->target_env)) {
1916 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
1917 if (storage_class != SpvStorageClassMax &&
1918 storage_class != SpvStorageClassInput &&
1919 storage_class != SpvStorageClassOutput) {
1920 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1921 << "Vulkan spec allows BuiltIn PrimitiveId to be only used for "
1922 "variables with Input or Output storage class. "
1923 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1924 referenced_from_inst)
1925 << " " << GetStorageClassDesc(referenced_from_inst);
1926 }
1927
1928 if (storage_class == SpvStorageClassOutput) {
1929 assert(function_id_ == 0);
1930 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1931 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
1932 std::string(
1933 _.VkErrorID(4334) +
1934 "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
1935 "variables with Output storage class if execution model is "
1936 "TessellationControl."),
1937 SpvExecutionModelTessellationControl, decoration, built_in_inst,
1938 referenced_from_inst, std::placeholders::_1));
1939 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1940 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
1941 std::string(
1942 _.VkErrorID(4334) +
1943 "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
1944 "variables with Output storage class if execution model is "
1945 "TessellationEvaluation."),
1946 SpvExecutionModelTessellationEvaluation, decoration, built_in_inst,
1947 referenced_from_inst, std::placeholders::_1));
1948 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1949 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
1950 std::string(
1951 _.VkErrorID(4334) +
1952 "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
1953 "variables with Output storage class if execution model is "
1954 "Fragment."),
1955 SpvExecutionModelFragment, decoration, built_in_inst,
1956 referenced_from_inst, std::placeholders::_1));
1957 }
1958
1959 for (const SpvExecutionModel execution_model : execution_models_) {
1960 switch (execution_model) {
1961 case SpvExecutionModelFragment:
1962 case SpvExecutionModelTessellationControl:
1963 case SpvExecutionModelTessellationEvaluation:
1964 case SpvExecutionModelGeometry:
1965 case SpvExecutionModelMeshNV:
1966 case SpvExecutionModelRayGenerationNV:
1967 case SpvExecutionModelIntersectionNV:
1968 case SpvExecutionModelAnyHitNV:
1969 case SpvExecutionModelClosestHitNV:
1970 case SpvExecutionModelMissNV:
1971 case SpvExecutionModelCallableNV: {
1972 // Ok.
1973 break;
1974 }
1975
1976 default: {
1977 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
1978 << _.VkErrorID(4330)
1979 << "Vulkan spec allows BuiltIn PrimitiveId to be used only "
1980 "with Fragment, TessellationControl, "
1981 "TessellationEvaluation or Geometry execution models. "
1982 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
1983 referenced_from_inst, execution_model);
1984 }
1985 }
1986 }
1987 }
1988
1989 if (function_id_ == 0) {
1990 // Propagate this rule to all dependant ids in the global scope.
1991 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
1992 &BuiltInsValidator::ValidatePrimitiveIdAtReference, this, decoration,
1993 built_in_inst, referenced_from_inst, std::placeholders::_1));
1994 }
1995
1996 return SPV_SUCCESS;
1997 }
1998
1999 spv_result_t BuiltInsValidator::ValidateSampleIdAtDefinition(
2000 const Decoration& decoration, const Instruction& inst) {
2001 if (spvIsVulkanEnv(_.context()->target_env)) {
2002 if (spv_result_t error = ValidateI32(
2003 decoration, inst,
2004 [this, &inst](const std::string& message) -> spv_result_t {
2005 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2006 << _.VkErrorID(4356)
2007 << "According to the Vulkan spec BuiltIn SampleId "
2008 "variable needs to be a 32-bit int scalar. "
2009 << message;
2010 })) {
2011 return error;
2012 }
2013 }
2014
2015 // Seed at reference checks with this built-in.
2016 return ValidateSampleIdAtReference(decoration, inst, inst, inst);
2017 }
2018
2019 spv_result_t BuiltInsValidator::ValidateSampleIdAtReference(
2020 const Decoration& decoration, const Instruction& built_in_inst,
2021 const Instruction& referenced_inst,
2022 const Instruction& referenced_from_inst) {
2023 if (spvIsVulkanEnv(_.context()->target_env)) {
2024 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2025 if (storage_class != SpvStorageClassMax &&
2026 storage_class != SpvStorageClassInput) {
2027 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2028 << _.VkErrorID(4355)
2029 << "Vulkan spec allows BuiltIn SampleId to be only used for "
2030 "variables with Input storage class. "
2031 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2032 referenced_from_inst)
2033 << " " << GetStorageClassDesc(referenced_from_inst);
2034 }
2035
2036 for (const SpvExecutionModel execution_model : execution_models_) {
2037 if (execution_model != SpvExecutionModelFragment) {
2038 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2039 << _.VkErrorID(4354)
2040 << "Vulkan spec allows BuiltIn SampleId to be used only with "
2041 "Fragment execution model. "
2042 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2043 referenced_from_inst, execution_model);
2044 }
2045 }
2046 }
2047
2048 if (function_id_ == 0) {
2049 // Propagate this rule to all dependant ids in the global scope.
2050 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2051 &BuiltInsValidator::ValidateSampleIdAtReference, this, decoration,
2052 built_in_inst, referenced_from_inst, std::placeholders::_1));
2053 }
2054
2055 return SPV_SUCCESS;
2056 }
2057
2058 spv_result_t BuiltInsValidator::ValidateSampleMaskAtDefinition(
2059 const Decoration& decoration, const Instruction& inst) {
2060 if (spvIsVulkanEnv(_.context()->target_env)) {
2061 if (spv_result_t error = ValidateI32Arr(
2062 decoration, inst,
2063 [this, &inst](const std::string& message) -> spv_result_t {
2064 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2065 << _.VkErrorID(4359)
2066 << "According to the Vulkan spec BuiltIn SampleMask "
2067 "variable needs to be a 32-bit int array. "
2068 << message;
2069 })) {
2070 return error;
2071 }
2072 }
2073
2074 // Seed at reference checks with this built-in.
2075 return ValidateSampleMaskAtReference(decoration, inst, inst, inst);
2076 }
2077
2078 spv_result_t BuiltInsValidator::ValidateSampleMaskAtReference(
2079 const Decoration& decoration, const Instruction& built_in_inst,
2080 const Instruction& referenced_inst,
2081 const Instruction& referenced_from_inst) {
2082 if (spvIsVulkanEnv(_.context()->target_env)) {
2083 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2084 if (storage_class != SpvStorageClassMax &&
2085 storage_class != SpvStorageClassInput &&
2086 storage_class != SpvStorageClassOutput) {
2087 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2088 << _.VkErrorID(4358)
2089 << "Vulkan spec allows BuiltIn SampleMask to be only used for "
2090 "variables with Input or Output storage class. "
2091 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2092 referenced_from_inst)
2093 << " " << GetStorageClassDesc(referenced_from_inst);
2094 }
2095
2096 for (const SpvExecutionModel execution_model : execution_models_) {
2097 if (execution_model != SpvExecutionModelFragment) {
2098 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2099 << _.VkErrorID(4357)
2100 << "Vulkan spec allows BuiltIn SampleMask to be used only "
2101 "with "
2102 "Fragment execution model. "
2103 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2104 referenced_from_inst, execution_model);
2105 }
2106 }
2107 }
2108
2109 if (function_id_ == 0) {
2110 // Propagate this rule to all dependant ids in the global scope.
2111 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2112 &BuiltInsValidator::ValidateSampleMaskAtReference, this, decoration,
2113 built_in_inst, referenced_from_inst, std::placeholders::_1));
2114 }
2115
2116 return SPV_SUCCESS;
2117 }
2118
2119 spv_result_t BuiltInsValidator::ValidateSamplePositionAtDefinition(
2120 const Decoration& decoration, const Instruction& inst) {
2121 if (spvIsVulkanEnv(_.context()->target_env)) {
2122 if (spv_result_t error = ValidateF32Vec(
2123 decoration, inst, 2,
2124 [this, &inst](const std::string& message) -> spv_result_t {
2125 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2126 << _.VkErrorID(4362)
2127 << "According to the Vulkan spec BuiltIn SamplePosition "
2128 "variable needs to be a 2-component 32-bit float "
2129 "vector. "
2130 << message;
2131 })) {
2132 return error;
2133 }
2134 }
2135
2136 // Seed at reference checks with this built-in.
2137 return ValidateSamplePositionAtReference(decoration, inst, inst, inst);
2138 }
2139
2140 spv_result_t BuiltInsValidator::ValidateSamplePositionAtReference(
2141 const Decoration& decoration, const Instruction& built_in_inst,
2142 const Instruction& referenced_inst,
2143 const Instruction& referenced_from_inst) {
2144 if (spvIsVulkanEnv(_.context()->target_env)) {
2145 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2146 if (storage_class != SpvStorageClassMax &&
2147 storage_class != SpvStorageClassInput) {
2148 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2149 << _.VkErrorID(4361)
2150 << "Vulkan spec allows BuiltIn SamplePosition to be only used "
2151 "for "
2152 "variables with Input storage class. "
2153 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2154 referenced_from_inst)
2155 << " " << GetStorageClassDesc(referenced_from_inst);
2156 }
2157
2158 for (const SpvExecutionModel execution_model : execution_models_) {
2159 if (execution_model != SpvExecutionModelFragment) {
2160 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2161 << _.VkErrorID(4360)
2162 << "Vulkan spec allows BuiltIn SamplePosition to be used only "
2163 "with "
2164 "Fragment execution model. "
2165 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2166 referenced_from_inst, execution_model);
2167 }
2168 }
2169 }
2170
2171 if (function_id_ == 0) {
2172 // Propagate this rule to all dependant ids in the global scope.
2173 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2174 &BuiltInsValidator::ValidateSamplePositionAtReference, this, decoration,
2175 built_in_inst, referenced_from_inst, std::placeholders::_1));
2176 }
2177
2178 return SPV_SUCCESS;
2179 }
2180
2181 spv_result_t BuiltInsValidator::ValidateTessCoordAtDefinition(
2182 const Decoration& decoration, const Instruction& inst) {
2183 if (spvIsVulkanEnv(_.context()->target_env)) {
2184 if (spv_result_t error = ValidateF32Vec(
2185 decoration, inst, 3,
2186 [this, &inst](const std::string& message) -> spv_result_t {
2187 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2188 << _.VkErrorID(4389)
2189 << "According to the Vulkan spec BuiltIn TessCoord "
2190 "variable needs to be a 3-component 32-bit float "
2191 "vector. "
2192 << message;
2193 })) {
2194 return error;
2195 }
2196 }
2197
2198 // Seed at reference checks with this built-in.
2199 return ValidateTessCoordAtReference(decoration, inst, inst, inst);
2200 }
2201
2202 spv_result_t BuiltInsValidator::ValidateTessCoordAtReference(
2203 const Decoration& decoration, const Instruction& built_in_inst,
2204 const Instruction& referenced_inst,
2205 const Instruction& referenced_from_inst) {
2206 if (spvIsVulkanEnv(_.context()->target_env)) {
2207 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2208 if (storage_class != SpvStorageClassMax &&
2209 storage_class != SpvStorageClassInput) {
2210 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2211 << _.VkErrorID(4388)
2212 << "Vulkan spec allows BuiltIn TessCoord to be only used for "
2213 "variables with Input storage class. "
2214 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2215 referenced_from_inst)
2216 << " " << GetStorageClassDesc(referenced_from_inst);
2217 }
2218
2219 for (const SpvExecutionModel execution_model : execution_models_) {
2220 if (execution_model != SpvExecutionModelTessellationEvaluation) {
2221 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2222 << _.VkErrorID(4387)
2223 << "Vulkan spec allows BuiltIn TessCoord to be used only with "
2224 "TessellationEvaluation execution model. "
2225 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2226 referenced_from_inst, execution_model);
2227 }
2228 }
2229 }
2230
2231 if (function_id_ == 0) {
2232 // Propagate this rule to all dependant ids in the global scope.
2233 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2234 &BuiltInsValidator::ValidateTessCoordAtReference, this, decoration,
2235 built_in_inst, referenced_from_inst, std::placeholders::_1));
2236 }
2237
2238 return SPV_SUCCESS;
2239 }
2240
2241 spv_result_t BuiltInsValidator::ValidateTessLevelOuterAtDefinition(
2242 const Decoration& decoration, const Instruction& inst) {
2243 if (spvIsVulkanEnv(_.context()->target_env)) {
2244 if (spv_result_t error = ValidateF32Arr(
2245 decoration, inst, 4,
2246 [this, &inst](const std::string& message) -> spv_result_t {
2247 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2248 << _.VkErrorID(4393)
2249 << "According to the Vulkan spec BuiltIn TessLevelOuter "
2250 "variable needs to be a 4-component 32-bit float "
2251 "array. "
2252 << message;
2253 })) {
2254 return error;
2255 }
2256 }
2257
2258 // Seed at reference checks with this built-in.
2259 return ValidateTessLevelAtReference(decoration, inst, inst, inst);
2260 }
2261
2262 spv_result_t BuiltInsValidator::ValidateTessLevelInnerAtDefinition(
2263 const Decoration& decoration, const Instruction& inst) {
2264 if (spvIsVulkanEnv(_.context()->target_env)) {
2265 if (spv_result_t error = ValidateF32Arr(
2266 decoration, inst, 2,
2267 [this, &inst](const std::string& message) -> spv_result_t {
2268 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2269 << _.VkErrorID(4397)
2270 << "According to the Vulkan spec BuiltIn TessLevelOuter "
2271 "variable needs to be a 2-component 32-bit float "
2272 "array. "
2273 << message;
2274 })) {
2275 return error;
2276 }
2277 }
2278
2279 // Seed at reference checks with this built-in.
2280 return ValidateTessLevelAtReference(decoration, inst, inst, inst);
2281 }
2282
2283 spv_result_t BuiltInsValidator::ValidateTessLevelAtReference(
2284 const Decoration& decoration, const Instruction& built_in_inst,
2285 const Instruction& referenced_inst,
2286 const Instruction& referenced_from_inst) {
2287 uint32_t operand = decoration.params()[0];
2288 if (spvIsVulkanEnv(_.context()->target_env)) {
2289 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2290 if (storage_class != SpvStorageClassMax &&
2291 storage_class != SpvStorageClassInput &&
2292 storage_class != SpvStorageClassOutput) {
2293 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2294 << "Vulkan spec allows BuiltIn "
2295 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2296 operand)
2297 << " to be only used for variables with Input or Output storage "
2298 "class. "
2299 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2300 referenced_from_inst)
2301 << " " << GetStorageClassDesc(referenced_from_inst);
2302 }
2303
2304 if (storage_class == SpvStorageClassInput) {
2305 assert(function_id_ == 0);
2306 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2307 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
2308 "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
2309 "used "
2310 "for variables with Input storage class if execution model is "
2311 "TessellationControl.",
2312 SpvExecutionModelTessellationControl, decoration, built_in_inst,
2313 referenced_from_inst, std::placeholders::_1));
2314 }
2315
2316 if (storage_class == SpvStorageClassOutput) {
2317 assert(function_id_ == 0);
2318 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2319 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
2320 "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
2321 "used "
2322 "for variables with Output storage class if execution model is "
2323 "TessellationEvaluation.",
2324 SpvExecutionModelTessellationEvaluation, decoration, built_in_inst,
2325 referenced_from_inst, std::placeholders::_1));
2326 }
2327
2328 for (const SpvExecutionModel execution_model : execution_models_) {
2329 switch (execution_model) {
2330 case SpvExecutionModelTessellationControl:
2331 case SpvExecutionModelTessellationEvaluation: {
2332 // Ok.
2333 break;
2334 }
2335
2336 default: {
2337 uint32_t vuid = (operand == SpvBuiltInTessLevelOuter) ? 4390 : 4394;
2338 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2339 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
2340 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2341 operand)
2342 << " to be used only with TessellationControl or "
2343 "TessellationEvaluation execution models. "
2344 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2345 referenced_from_inst, execution_model);
2346 }
2347 }
2348 }
2349 }
2350
2351 if (function_id_ == 0) {
2352 // Propagate this rule to all dependant ids in the global scope.
2353 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2354 &BuiltInsValidator::ValidateTessLevelAtReference, this, decoration,
2355 built_in_inst, referenced_from_inst, std::placeholders::_1));
2356 }
2357
2358 return SPV_SUCCESS;
2359 }
2360
2361 spv_result_t BuiltInsValidator::ValidateVertexIndexAtDefinition(
2362 const Decoration& decoration, const Instruction& inst) {
2363 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
2364 if (spv_result_t error = ValidateI32(
2365 decoration, inst,
2366 [this, &inst](const std::string& message) -> spv_result_t {
2367 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2368 << _.VkErrorID(4400) << "According to the "
2369 << spvLogStringForEnv(_.context()->target_env)
2370 << " spec BuiltIn VertexIndex variable needs to be a "
2371 "32-bit int scalar. "
2372 << message;
2373 })) {
2374 return error;
2375 }
2376 }
2377
2378 // Seed at reference checks with this built-in.
2379 return ValidateVertexIndexAtReference(decoration, inst, inst, inst);
2380 }
2381
2382 spv_result_t BuiltInsValidator::ValidateVertexIdOrInstanceIdAtDefinition(
2383 const Decoration& decoration, const Instruction& inst) {
2384 const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]);
2385 bool allow_instance_id =
2386 (_.HasCapability(SpvCapabilityRayTracingNV) ||
2387 _.HasCapability(SpvCapabilityRayTracingProvisionalKHR)) &&
2388 label == SpvBuiltInInstanceId;
2389
2390 if (spvIsVulkanEnv(_.context()->target_env) && !allow_instance_id) {
2391 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2392 << "Vulkan spec doesn't allow BuiltIn VertexId/InstanceId "
2393 "to be used.";
2394 }
2395
2396 if (label == SpvBuiltInInstanceId) {
2397 return ValidateInstanceIdAtReference(decoration, inst, inst, inst);
2398 }
2399 return SPV_SUCCESS;
2400 }
2401
2402 spv_result_t BuiltInsValidator::ValidateInstanceIdAtReference(
2403 const Decoration& decoration, const Instruction& built_in_inst,
2404 const Instruction& referenced_inst,
2405 const Instruction& referenced_from_inst) {
2406 if (spvIsVulkanEnv(_.context()->target_env)) {
2407 for (const SpvExecutionModel execution_model : execution_models_) {
2408 switch (execution_model) {
2409 case SpvExecutionModelIntersectionNV:
2410 case SpvExecutionModelClosestHitNV:
2411 case SpvExecutionModelAnyHitNV:
2412 // Do nothing, valid stages
2413 break;
2414 default:
2415 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2416 << "Vulkan spec allows BuiltIn InstanceId to be used "
2417 "only with IntersectionNV, ClosestHitNV and AnyHitNV "
2418 "execution models. "
2419 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2420 referenced_from_inst);
2421 break;
2422 }
2423 }
2424 }
2425
2426 if (function_id_ == 0) {
2427 // Propagate this rule to all dependant ids in the global scope.
2428 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2429 &BuiltInsValidator::ValidateInstanceIdAtReference, this, decoration,
2430 built_in_inst, referenced_from_inst, std::placeholders::_1));
2431 }
2432
2433 return SPV_SUCCESS;
2434 }
2435
2436 spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtDefinition(
2437 const Decoration& decoration, const Instruction& inst) {
2438 if (spvIsWebGPUEnv(_.context()->target_env)) {
2439 if (spv_result_t error = ValidateI32(
2440 decoration, inst,
2441 [this, &inst](const std::string& message) -> spv_result_t {
2442 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2443 << "According to the WebGPU spec BuiltIn "
2444 "LocalInvocationIndex variable needs to be a 32-bit "
2445 "int."
2446 << message;
2447 })) {
2448 return error;
2449 }
2450 }
2451
2452 // Seed at reference checks with this built-in.
2453 return ValidateLocalInvocationIndexAtReference(decoration, inst, inst, inst);
2454 }
2455
2456 spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtReference(
2457 const Decoration& decoration, const Instruction& built_in_inst,
2458 const Instruction& referenced_inst,
2459 const Instruction& referenced_from_inst) {
2460 if (spvIsWebGPUEnv(_.context()->target_env)) {
2461 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2462 if (storage_class != SpvStorageClassMax &&
2463 storage_class != SpvStorageClassInput) {
2464 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2465 << "WebGPU spec allows BuiltIn LocalInvocationIndex to be only "
2466 "used for variables with Input storage class. "
2467 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2468 referenced_from_inst)
2469 << " " << GetStorageClassDesc(referenced_from_inst);
2470 }
2471
2472 for (const SpvExecutionModel execution_model : execution_models_) {
2473 if (execution_model != SpvExecutionModelGLCompute) {
2474 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2475 << "WebGPU spec allows BuiltIn VertexIndex to be used only "
2476 "with GLCompute execution model. "
2477 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2478 referenced_from_inst, execution_model);
2479 }
2480 }
2481 }
2482
2483 if (function_id_ == 0) {
2484 // Propagate this rule to all dependant ids in the global scope.
2485 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2486 std::bind(&BuiltInsValidator::ValidateLocalInvocationIndexAtReference,
2487 this, decoration, built_in_inst, referenced_from_inst,
2488 std::placeholders::_1));
2489 }
2490
2491 return SPV_SUCCESS;
2492 }
2493
2494 spv_result_t BuiltInsValidator::ValidateVertexIndexAtReference(
2495 const Decoration& decoration, const Instruction& built_in_inst,
2496 const Instruction& referenced_inst,
2497 const Instruction& referenced_from_inst) {
2498 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
2499 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2500 if (storage_class != SpvStorageClassMax &&
2501 storage_class != SpvStorageClassInput) {
2502 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2503 << _.VkErrorID(4399) << spvLogStringForEnv(_.context()->target_env)
2504 << " spec allows BuiltIn VertexIndex to be only used for "
2505 "variables with Input storage class. "
2506 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2507 referenced_from_inst)
2508 << " " << GetStorageClassDesc(referenced_from_inst);
2509 }
2510
2511 for (const SpvExecutionModel execution_model : execution_models_) {
2512 if (execution_model != SpvExecutionModelVertex) {
2513 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2514 << _.VkErrorID(4398)
2515 << spvLogStringForEnv(_.context()->target_env)
2516 << " spec allows BuiltIn VertexIndex to be used only with "
2517 "Vertex execution model. "
2518 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2519 referenced_from_inst, execution_model);
2520 }
2521 }
2522 }
2523
2524 if (function_id_ == 0) {
2525 // Propagate this rule to all dependant ids in the global scope.
2526 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2527 &BuiltInsValidator::ValidateVertexIndexAtReference, this, decoration,
2528 built_in_inst, referenced_from_inst, std::placeholders::_1));
2529 }
2530
2531 return SPV_SUCCESS;
2532 }
2533
2534 spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtDefinition(
2535 const Decoration& decoration, const Instruction& inst) {
2536 if (spvIsVulkanEnv(_.context()->target_env)) {
2537 // This can be a per-primitive variable for mesh shader stage.
2538 // In such cases variable will have an array of 32-bit integers.
2539 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
2540 // This must be a 32-bit int scalar.
2541 if (spv_result_t error = ValidateI32(
2542 decoration, inst,
2543 [this, &decoration,
2544 &inst](const std::string& message) -> spv_result_t {
2545 uint32_t vuid =
2546 (decoration.params()[0] == SpvBuiltInLayer) ? 4276 : 4408;
2547 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2548 << _.VkErrorID(vuid)
2549 << "According to the Vulkan spec BuiltIn "
2550 << _.grammar().lookupOperandName(
2551 SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
2552 << "variable needs to be a 32-bit int scalar. "
2553 << message;
2554 })) {
2555 return error;
2556 }
2557 } else {
2558 if (spv_result_t error = ValidateOptionalArrayedI32(
2559 decoration, inst,
2560 [this, &decoration,
2561 &inst](const std::string& message) -> spv_result_t {
2562 uint32_t vuid =
2563 (decoration.params()[0] == SpvBuiltInLayer) ? 4276 : 4408;
2564 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2565 << _.VkErrorID(vuid)
2566 << "According to the Vulkan spec BuiltIn "
2567 << _.grammar().lookupOperandName(
2568 SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
2569 << "variable needs to be a 32-bit int scalar. "
2570 << message;
2571 })) {
2572 return error;
2573 }
2574 }
2575 }
2576
2577 // Seed at reference checks with this built-in.
2578 return ValidateLayerOrViewportIndexAtReference(decoration, inst, inst, inst);
2579 }
2580
2581 spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtReference(
2582 const Decoration& decoration, const Instruction& built_in_inst,
2583 const Instruction& referenced_inst,
2584 const Instruction& referenced_from_inst) {
2585 uint32_t operand = decoration.params()[0];
2586 if (spvIsVulkanEnv(_.context()->target_env)) {
2587 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2588 if (storage_class != SpvStorageClassMax &&
2589 storage_class != SpvStorageClassInput &&
2590 storage_class != SpvStorageClassOutput) {
2591 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2592 << "Vulkan spec allows BuiltIn "
2593 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2594 operand)
2595 << " to be only used for variables with Input or Output storage "
2596 "class. "
2597 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2598 referenced_from_inst)
2599 << " " << GetStorageClassDesc(referenced_from_inst);
2600 }
2601
2602 if (storage_class == SpvStorageClassInput) {
2603 assert(function_id_ == 0);
2604 for (const auto em :
2605 {SpvExecutionModelVertex, SpvExecutionModelTessellationEvaluation,
2606 SpvExecutionModelGeometry, SpvExecutionModelMeshNV}) {
2607 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2608 std::bind(
2609 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
2610 std::string(
2611 _.VkErrorID((operand == SpvBuiltInLayer) ? 4274 : 4406) +
2612 "Vulkan spec doesn't allow BuiltIn Layer and "
2613 "ViewportIndex to be "
2614 "used for variables with Input storage class if "
2615 "execution model is Vertex, TessellationEvaluation, "
2616 "Geometry, or MeshNV."),
2617 em, decoration, built_in_inst, referenced_from_inst,
2618 std::placeholders::_1));
2619 }
2620 }
2621
2622 if (storage_class == SpvStorageClassOutput) {
2623 assert(function_id_ == 0);
2624 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2625 &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
2626 std::string(_.VkErrorID((operand == SpvBuiltInLayer) ? 4275 : 4407) +
2627 "Vulkan spec doesn't allow BuiltIn Layer and "
2628 "ViewportIndex to be "
2629 "used for variables with Output storage class if "
2630 "execution model is "
2631 "Fragment."),
2632 SpvExecutionModelFragment, decoration, built_in_inst,
2633 referenced_from_inst, std::placeholders::_1));
2634 }
2635
2636 for (const SpvExecutionModel execution_model : execution_models_) {
2637 switch (execution_model) {
2638 case SpvExecutionModelGeometry:
2639 case SpvExecutionModelFragment:
2640 case SpvExecutionModelMeshNV:
2641 // Ok.
2642 break;
2643 case SpvExecutionModelVertex:
2644 case SpvExecutionModelTessellationEvaluation: {
2645 if (!_.HasCapability(SpvCapabilityShaderViewportIndexLayerEXT)) {
2646 if (operand == SpvBuiltInViewportIndex &&
2647 _.HasCapability(SpvCapabilityShaderViewportIndex))
2648 break; // Ok
2649 if (operand == SpvBuiltInLayer &&
2650 _.HasCapability(SpvCapabilityShaderLayer))
2651 break; // Ok
2652
2653 const char* capability = "ShaderViewportIndexLayerEXT";
2654
2655 if (operand == SpvBuiltInViewportIndex)
2656 capability = "ShaderViewportIndexLayerEXT or ShaderViewportIndex";
2657 if (operand == SpvBuiltInLayer)
2658 capability = "ShaderViewportIndexLayerEXT or ShaderLayer";
2659
2660 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2661 << "Using BuiltIn "
2662 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2663 operand)
2664 << " in Vertex or Tessellation execution model requires the "
2665 << capability << " capability.";
2666 }
2667 break;
2668 }
2669 default: {
2670 uint32_t vuid = (operand == SpvBuiltInLayer) ? 4272 : 4404;
2671 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2672 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
2673 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2674 operand)
2675 << " to be used only with Vertex, TessellationEvaluation, "
2676 "Geometry, or Fragment execution models. "
2677 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2678 referenced_from_inst, execution_model);
2679 }
2680 }
2681 }
2682 }
2683
2684 if (function_id_ == 0) {
2685 // Propagate this rule to all dependant ids in the global scope.
2686 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2687 std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference,
2688 this, decoration, built_in_inst, referenced_from_inst,
2689 std::placeholders::_1));
2690 }
2691
2692 return SPV_SUCCESS;
2693 }
2694
2695 spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtDefinition(
2696 const Decoration& decoration, const Instruction& inst) {
2697 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
2698 if (spv_result_t error = ValidateI32Vec(
2699 decoration, inst, 3,
2700 [this, &decoration,
2701 &inst](const std::string& message) -> spv_result_t {
2702 uint32_t operand = decoration.params()[0];
2703 uint32_t vuid = 0;
2704 switch (operand) {
2705 case SpvBuiltInGlobalInvocationId:
2706 vuid = 4238;
2707 break;
2708 case SpvBuiltInLocalInvocationId:
2709 vuid = 4283;
2710 break;
2711 case SpvBuiltInNumWorkgroups:
2712 vuid = 4298;
2713 break;
2714 case SpvBuiltInWorkgroupId:
2715 vuid = 4424;
2716 break;
2717 };
2718 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2719 << _.VkErrorID(vuid) << "According to the "
2720 << spvLogStringForEnv(_.context()->target_env)
2721 << " spec BuiltIn "
2722 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2723 operand)
2724 << " variable needs to be a 3-component 32-bit int "
2725 "vector. "
2726 << message;
2727 })) {
2728 return error;
2729 }
2730 }
2731
2732 // Seed at reference checks with this built-in.
2733 return ValidateComputeShaderI32Vec3InputAtReference(decoration, inst, inst,
2734 inst);
2735 }
2736
2737 spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference(
2738 const Decoration& decoration, const Instruction& built_in_inst,
2739 const Instruction& referenced_inst,
2740 const Instruction& referenced_from_inst) {
2741 uint32_t operand = decoration.params()[0];
2742 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
2743 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2744 if (storage_class != SpvStorageClassMax &&
2745 storage_class != SpvStorageClassInput) {
2746 uint32_t vuid = 0;
2747 switch (operand) {
2748 case SpvBuiltInGlobalInvocationId:
2749 vuid = 4237;
2750 break;
2751 case SpvBuiltInLocalInvocationId:
2752 vuid = 4282;
2753 break;
2754 case SpvBuiltInNumWorkgroups:
2755 vuid = 4297;
2756 break;
2757 case SpvBuiltInWorkgroupId:
2758 vuid = 4423;
2759 break;
2760 };
2761 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2762 << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
2763 << " spec allows BuiltIn "
2764 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2765 decoration.params()[0])
2766 << " to be only used for variables with Input storage class. "
2767 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2768 referenced_from_inst)
2769 << " " << GetStorageClassDesc(referenced_from_inst);
2770 }
2771
2772 for (const SpvExecutionModel execution_model : execution_models_) {
2773 bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
2774 execution_model == SpvExecutionModelTaskNV ||
2775 execution_model == SpvExecutionModelMeshNV;
2776 bool has_webgpu_model = execution_model == SpvExecutionModelGLCompute;
2777 if ((spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) ||
2778 (spvIsWebGPUEnv(_.context()->target_env) && !has_webgpu_model)) {
2779 uint32_t vuid = 0;
2780 switch (operand) {
2781 case SpvBuiltInGlobalInvocationId:
2782 vuid = 4236;
2783 break;
2784 case SpvBuiltInLocalInvocationId:
2785 vuid = 4281;
2786 break;
2787 case SpvBuiltInNumWorkgroups:
2788 vuid = 4296;
2789 break;
2790 case SpvBuiltInWorkgroupId:
2791 vuid = 4422;
2792 break;
2793 };
2794 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2795 << _.VkErrorID(vuid)
2796 << spvLogStringForEnv(_.context()->target_env)
2797 << " spec allows BuiltIn "
2798 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2799 decoration.params()[0])
2800 << " to be used only with GLCompute execution model. "
2801 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2802 referenced_from_inst, execution_model);
2803 }
2804 }
2805 }
2806
2807 if (function_id_ == 0) {
2808 // Propagate this rule to all dependant ids in the global scope.
2809 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
2810 &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this,
2811 decoration, built_in_inst, referenced_from_inst,
2812 std::placeholders::_1));
2813 }
2814
2815 return SPV_SUCCESS;
2816 }
2817
2818 spv_result_t BuiltInsValidator::ValidateComputeI32InputAtDefinition(
2819 const Decoration& decoration, const Instruction& inst) {
2820 if (spvIsVulkanEnv(_.context()->target_env)) {
2821 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
2822 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2823 << "BuiltIn "
2824 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2825 decoration.params()[0])
2826 << " cannot be used as a member decoration ";
2827 }
2828 if (spv_result_t error = ValidateI32(
2829 decoration, inst,
2830 [this, &decoration,
2831 &inst](const std::string& message) -> spv_result_t {
2832 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2833 << "According to the "
2834 << spvLogStringForEnv(_.context()->target_env)
2835 << " spec BuiltIn "
2836 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2837 decoration.params()[0])
2838 << " variable needs to be a 32-bit int "
2839 "vector. "
2840 << message;
2841 })) {
2842 return error;
2843 }
2844 }
2845
2846 // Seed at reference checks with this built-in.
2847 return ValidateComputeI32InputAtReference(decoration, inst, inst, inst);
2848 }
2849
2850 spv_result_t BuiltInsValidator::ValidateComputeI32InputAtReference(
2851 const Decoration& decoration, const Instruction& built_in_inst,
2852 const Instruction& referenced_inst,
2853 const Instruction& referenced_from_inst) {
2854 if (spvIsVulkanEnv(_.context()->target_env)) {
2855 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
2856 if (storage_class != SpvStorageClassMax &&
2857 storage_class != SpvStorageClassInput) {
2858 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2859 << spvLogStringForEnv(_.context()->target_env)
2860 << " spec allows BuiltIn "
2861 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2862 decoration.params()[0])
2863 << " to be only used for variables with Input storage class. "
2864 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2865 referenced_from_inst)
2866 << " " << GetStorageClassDesc(referenced_from_inst);
2867 }
2868
2869 for (const SpvExecutionModel execution_model : execution_models_) {
2870 bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
2871 execution_model == SpvExecutionModelTaskNV ||
2872 execution_model == SpvExecutionModelMeshNV;
2873 if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
2874 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
2875 << spvLogStringForEnv(_.context()->target_env)
2876 << " spec allows BuiltIn "
2877 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2878 decoration.params()[0])
2879 << " to be used only with GLCompute execution model. "
2880 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
2881 referenced_from_inst, execution_model);
2882 }
2883 }
2884 }
2885
2886 if (function_id_ == 0) {
2887 // Propagate this rule to all dependant ids in the global scope.
2888 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
2889 std::bind(&BuiltInsValidator::ValidateComputeI32InputAtReference, this,
2890 decoration, built_in_inst, referenced_from_inst,
2891 std::placeholders::_1));
2892 }
2893
2894 return SPV_SUCCESS;
2895 }
2896
2897 spv_result_t BuiltInsValidator::ValidateI32InputAtDefinition(
2898 const Decoration& decoration, const Instruction& inst) {
2899 if (spvIsVulkanEnv(_.context()->target_env)) {
2900 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
2901 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2902 << "BuiltIn "
2903 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2904 decoration.params()[0])
2905 << " cannot be used as a member decoration ";
2906 }
2907 if (spv_result_t error = ValidateI32(
2908 decoration, inst,
2909 [this, &decoration,
2910 &inst](const std::string& message) -> spv_result_t {
2911 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2912 << "According to the "
2913 << spvLogStringForEnv(_.context()->target_env)
2914 << " spec BuiltIn "
2915 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2916 decoration.params()[0])
2917 << " variable needs to be a 32-bit int. " << message;
2918 })) {
2919 return error;
2920 }
2921
2922 const SpvStorageClass storage_class = GetStorageClass(inst);
2923 if (storage_class != SpvStorageClassMax &&
2924 storage_class != SpvStorageClassInput) {
2925 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2926 << spvLogStringForEnv(_.context()->target_env)
2927 << " spec allows BuiltIn "
2928 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2929 decoration.params()[0])
2930 << " to be only used for variables with Input storage class. "
2931 << GetReferenceDesc(decoration, inst, inst, inst) << " "
2932 << GetStorageClassDesc(inst);
2933 }
2934 }
2935
2936 return SPV_SUCCESS;
2937 }
2938
2939 spv_result_t BuiltInsValidator::ValidateI32Vec4InputAtDefinition(
2940 const Decoration& decoration, const Instruction& inst) {
2941 if (spvIsVulkanEnv(_.context()->target_env)) {
2942 if (decoration.struct_member_index() != Decoration::kInvalidMember) {
2943 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2944 << "BuiltIn "
2945 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2946 decoration.params()[0])
2947 << " cannot be used as a member decoration ";
2948 }
2949 if (spv_result_t error = ValidateI32Vec(
2950 decoration, inst, 4,
2951 [this, &decoration,
2952 &inst](const std::string& message) -> spv_result_t {
2953 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2954 << "According to the "
2955 << spvLogStringForEnv(_.context()->target_env)
2956 << " spec BuiltIn "
2957 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2958 decoration.params()[0])
2959 << " variable needs to be a 4-component 32-bit int "
2960 "vector. "
2961 << message;
2962 })) {
2963 return error;
2964 }
2965
2966 const SpvStorageClass storage_class = GetStorageClass(inst);
2967 if (storage_class != SpvStorageClassMax &&
2968 storage_class != SpvStorageClassInput) {
2969 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2970 << spvLogStringForEnv(_.context()->target_env)
2971 << " spec allows BuiltIn "
2972 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
2973 decoration.params()[0])
2974 << " to be only used for variables with Input storage class. "
2975 << GetReferenceDesc(decoration, inst, inst, inst) << " "
2976 << GetStorageClassDesc(inst);
2977 }
2978 }
2979
2980 return SPV_SUCCESS;
2981 }
2982
2983 spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtDefinition(
2984 const Decoration& decoration, const Instruction& inst) {
2985 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
2986 if (spvIsVulkanEnv(_.context()->target_env) &&
2987 !spvOpcodeIsConstant(inst.opcode())) {
2988 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2989 << _.VkErrorID(4426)
2990 << "Vulkan spec requires BuiltIn WorkgroupSize to be a "
2991 "constant. "
2992 << GetIdDesc(inst) << " is not a constant.";
2993 }
2994
2995 if (spv_result_t error = ValidateI32Vec(
2996 decoration, inst, 3,
2997 [this, &inst](const std::string& message) -> spv_result_t {
2998 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
2999 << _.VkErrorID(4427) << "According to the "
3000 << spvLogStringForEnv(_.context()->target_env)
3001 << " spec BuiltIn WorkgroupSize variable needs to be a "
3002 "3-component 32-bit int vector. "
3003 << message;
3004 })) {
3005 return error;
3006 }
3007 }
3008
3009 // Seed at reference checks with this built-in.
3010 return ValidateWorkgroupSizeAtReference(decoration, inst, inst, inst);
3011 }
3012
3013 spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference(
3014 const Decoration& decoration, const Instruction& built_in_inst,
3015 const Instruction& referenced_inst,
3016 const Instruction& referenced_from_inst) {
3017 if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
3018 for (const SpvExecutionModel execution_model : execution_models_) {
3019 if (execution_model != SpvExecutionModelGLCompute) {
3020 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3021 << _.VkErrorID(4425)
3022 << spvLogStringForEnv(_.context()->target_env)
3023 << " spec allows BuiltIn "
3024 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3025 decoration.params()[0])
3026 << " to be used only with GLCompute execution model. "
3027 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3028 referenced_from_inst, execution_model);
3029 }
3030 }
3031 }
3032
3033 if (function_id_ == 0) {
3034 // Propagate this rule to all dependant ids in the global scope.
3035 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3036 &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration,
3037 built_in_inst, referenced_from_inst, std::placeholders::_1));
3038 }
3039
3040 return SPV_SUCCESS;
3041 }
3042
3043 spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtDefinition(
3044 const Decoration& decoration, const Instruction& inst) {
3045 if (spvIsVulkanEnv(_.context()->target_env)) {
3046 if (spv_result_t error = ValidateI32(
3047 decoration, inst,
3048 [this, &inst,
3049 &decoration](const std::string& message) -> spv_result_t {
3050 uint32_t vuid = (decoration.params()[0] == SpvBuiltInBaseInstance)
3051 ? 4183
3052 : 4186;
3053 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3054 << _.VkErrorID(vuid)
3055 << "According to the Vulkan spec BuiltIn "
3056 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3057 decoration.params()[0])
3058 << " variable needs to be a 32-bit int scalar. "
3059 << message;
3060 })) {
3061 return error;
3062 }
3063 }
3064
3065 return ValidateBaseInstanceOrVertexAtReference(decoration, inst, inst, inst);
3066 }
3067
3068 spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference(
3069 const Decoration& decoration, const Instruction& built_in_inst,
3070 const Instruction& referenced_inst,
3071 const Instruction& referenced_from_inst) {
3072 uint32_t operand = decoration.params()[0];
3073 if (spvIsVulkanEnv(_.context()->target_env)) {
3074 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
3075 if (storage_class != SpvStorageClassMax &&
3076 storage_class != SpvStorageClassInput) {
3077 uint32_t vuid = (operand == SpvBuiltInBaseInstance) ? 4182 : 4185;
3078 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3079 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
3080 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3081 operand)
3082 << " to be only used for variables with Input storage class. "
3083 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3084 referenced_from_inst)
3085 << " " << GetStorageClassDesc(referenced_from_inst);
3086 }
3087
3088 for (const SpvExecutionModel execution_model : execution_models_) {
3089 if (execution_model != SpvExecutionModelVertex) {
3090 uint32_t vuid = (operand == SpvBuiltInBaseInstance) ? 4181 : 4184;
3091 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3092 << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
3093 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3094 operand)
3095 << " to be used only with Vertex execution model. "
3096 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3097 referenced_from_inst, execution_model);
3098 }
3099 }
3100 }
3101
3102 if (function_id_ == 0) {
3103 // Propagate this rule to all dependant ids in the global scope.
3104 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
3105 std::bind(&BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference,
3106 this, decoration, built_in_inst, referenced_from_inst,
3107 std::placeholders::_1));
3108 }
3109
3110 return SPV_SUCCESS;
3111 }
3112
3113 spv_result_t BuiltInsValidator::ValidateDrawIndexAtDefinition(
3114 const Decoration& decoration, const Instruction& inst) {
3115 if (spvIsVulkanEnv(_.context()->target_env)) {
3116 if (spv_result_t error = ValidateI32(
3117 decoration, inst,
3118 [this, &inst,
3119 &decoration](const std::string& message) -> spv_result_t {
3120 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3121 << _.VkErrorID(4209)
3122 << "According to the Vulkan spec BuiltIn "
3123 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3124 decoration.params()[0])
3125 << " variable needs to be a 32-bit int scalar. "
3126 << message;
3127 })) {
3128 return error;
3129 }
3130 }
3131
3132 return ValidateDrawIndexAtReference(decoration, inst, inst, inst);
3133 }
3134
3135 spv_result_t BuiltInsValidator::ValidateDrawIndexAtReference(
3136 const Decoration& decoration, const Instruction& built_in_inst,
3137 const Instruction& referenced_inst,
3138 const Instruction& referenced_from_inst) {
3139 uint32_t operand = decoration.params()[0];
3140 if (spvIsVulkanEnv(_.context()->target_env)) {
3141 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
3142 if (storage_class != SpvStorageClassMax &&
3143 storage_class != SpvStorageClassInput) {
3144 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3145 << _.VkErrorID(4208) << "Vulkan spec allows BuiltIn "
3146 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3147 operand)
3148 << " to be only used for variables with Input storage class. "
3149 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3150 referenced_from_inst)
3151 << " " << GetStorageClassDesc(referenced_from_inst);
3152 }
3153
3154 for (const SpvExecutionModel execution_model : execution_models_) {
3155 if (execution_model != SpvExecutionModelVertex &&
3156 execution_model != SpvExecutionModelMeshNV &&
3157 execution_model != SpvExecutionModelTaskNV) {
3158 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3159 << _.VkErrorID(4207) << "Vulkan spec allows BuiltIn "
3160 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3161 operand)
3162 << " to be used only with Vertex, MeshNV, or TaskNV execution "
3163 "model. "
3164 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3165 referenced_from_inst, execution_model);
3166 }
3167 }
3168 }
3169
3170 if (function_id_ == 0) {
3171 // Propagate this rule to all dependant ids in the global scope.
3172 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3173 &BuiltInsValidator::ValidateDrawIndexAtReference, this, decoration,
3174 built_in_inst, referenced_from_inst, std::placeholders::_1));
3175 }
3176
3177 return SPV_SUCCESS;
3178 }
3179
3180 spv_result_t BuiltInsValidator::ValidateViewIndexAtDefinition(
3181 const Decoration& decoration, const Instruction& inst) {
3182 if (spvIsVulkanEnv(_.context()->target_env)) {
3183 if (spv_result_t error = ValidateI32(
3184 decoration, inst,
3185 [this, &inst,
3186 &decoration](const std::string& message) -> spv_result_t {
3187 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3188 << _.VkErrorID(4403)
3189 << "According to the Vulkan spec BuiltIn "
3190 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3191 decoration.params()[0])
3192 << " variable needs to be a 32-bit int scalar. "
3193 << message;
3194 })) {
3195 return error;
3196 }
3197 }
3198
3199 return ValidateViewIndexAtReference(decoration, inst, inst, inst);
3200 }
3201
3202 spv_result_t BuiltInsValidator::ValidateViewIndexAtReference(
3203 const Decoration& decoration, const Instruction& built_in_inst,
3204 const Instruction& referenced_inst,
3205 const Instruction& referenced_from_inst) {
3206 uint32_t operand = decoration.params()[0];
3207 if (spvIsVulkanEnv(_.context()->target_env)) {
3208 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
3209 if (storage_class != SpvStorageClassMax &&
3210 storage_class != SpvStorageClassInput) {
3211 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3212 << _.VkErrorID(4402) << "Vulkan spec allows BuiltIn "
3213 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3214 operand)
3215 << " to be only used for variables with Input storage class. "
3216 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3217 referenced_from_inst)
3218 << " " << GetStorageClassDesc(referenced_from_inst);
3219 }
3220
3221 for (const SpvExecutionModel execution_model : execution_models_) {
3222 if (execution_model == SpvExecutionModelGLCompute) {
3223 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3224 << _.VkErrorID(4401) << "Vulkan spec allows BuiltIn "
3225 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3226 operand)
3227 << " to be not be used with GLCompute execution model. "
3228 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3229 referenced_from_inst, execution_model);
3230 }
3231 }
3232 }
3233
3234 if (function_id_ == 0) {
3235 // Propagate this rule to all dependant ids in the global scope.
3236 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3237 &BuiltInsValidator::ValidateViewIndexAtReference, this, decoration,
3238 built_in_inst, referenced_from_inst, std::placeholders::_1));
3239 }
3240
3241 return SPV_SUCCESS;
3242 }
3243
3244 spv_result_t BuiltInsValidator::ValidateDeviceIndexAtDefinition(
3245 const Decoration& decoration, const Instruction& inst) {
3246 if (spvIsVulkanEnv(_.context()->target_env)) {
3247 if (spv_result_t error = ValidateI32(
3248 decoration, inst,
3249 [this, &inst,
3250 &decoration](const std::string& message) -> spv_result_t {
3251 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3252 << _.VkErrorID(4206)
3253 << "According to the Vulkan spec BuiltIn "
3254 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3255 decoration.params()[0])
3256 << " variable needs to be a 32-bit int scalar. "
3257 << message;
3258 })) {
3259 return error;
3260 }
3261 }
3262
3263 return ValidateDeviceIndexAtReference(decoration, inst, inst, inst);
3264 }
3265
3266 spv_result_t BuiltInsValidator::ValidateDeviceIndexAtReference(
3267 const Decoration& decoration, const Instruction& built_in_inst,
3268 const Instruction& referenced_inst,
3269 const Instruction& referenced_from_inst) {
3270 uint32_t operand = decoration.params()[0];
3271 if (spvIsVulkanEnv(_.context()->target_env)) {
3272 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
3273 if (storage_class != SpvStorageClassMax &&
3274 storage_class != SpvStorageClassInput) {
3275 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3276 << _.VkErrorID(4205) << "Vulkan spec allows BuiltIn "
3277 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3278 operand)
3279 << " to be only used for variables with Input storage class. "
3280 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3281 referenced_from_inst)
3282 << " " << GetStorageClassDesc(referenced_from_inst);
3283 }
3284 }
3285
3286 if (function_id_ == 0) {
3287 // Propagate this rule to all dependant ids in the global scope.
3288 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3289 &BuiltInsValidator::ValidateDeviceIndexAtReference, this, decoration,
3290 built_in_inst, referenced_from_inst, std::placeholders::_1));
3291 }
3292
3293 return SPV_SUCCESS;
3294 }
3295
3296 spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtDefinition(
3297 const Decoration& decoration, const Instruction& inst) {
3298 if (spvIsVulkanEnv(_.context()->target_env)) {
3299 if (spv_result_t error = ValidateI32(
3300 decoration, inst,
3301 [this, &inst,
3302 &decoration](const std::string& message) -> spv_result_t {
3303 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3304 << "According to the "
3305 << spvLogStringForEnv(_.context()->target_env)
3306 << " spec BuiltIn "
3307 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3308 decoration.params()[0])
3309 << " variable needs to be a 32-bit int scalar. "
3310 << message;
3311 })) {
3312 return error;
3313 }
3314 }
3315
3316 // Seed at reference checks with this built-in.
3317 return ValidateSMBuiltinsAtReference(decoration, inst, inst, inst);
3318 }
3319
3320 spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtReference(
3321 const Decoration& decoration, const Instruction& built_in_inst,
3322 const Instruction& referenced_inst,
3323 const Instruction& referenced_from_inst) {
3324 if (spvIsVulkanEnv(_.context()->target_env)) {
3325 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
3326 if (storage_class != SpvStorageClassMax &&
3327 storage_class != SpvStorageClassInput) {
3328 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3329 << spvLogStringForEnv(_.context()->target_env)
3330 << " spec allows BuiltIn "
3331 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3332 decoration.params()[0])
3333 << " to be only used for "
3334 "variables with Input storage class. "
3335 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3336 referenced_from_inst)
3337 << " " << GetStorageClassDesc(referenced_from_inst);
3338 }
3339 }
3340
3341 if (function_id_ == 0) {
3342 // Propagate this rule to all dependant ids in the global scope.
3343 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3344 &BuiltInsValidator::ValidateSMBuiltinsAtReference, this, decoration,
3345 built_in_inst, referenced_from_inst, std::placeholders::_1));
3346 }
3347
3348 return SPV_SUCCESS;
3349 }
3350
3351 spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtDefinition(
3352 const Decoration& decoration, const Instruction& inst) {
3353 if (spvIsVulkanEnv(_.context()->target_env)) {
3354 if (spv_result_t error = ValidateI32(
3355 decoration, inst,
3356 [this, &inst,
3357 &decoration](const std::string& message) -> spv_result_t {
3358 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3359 << _.VkErrorID(4486)
3360 << "According to the Vulkan spec BuiltIn "
3361 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3362 decoration.params()[0])
3363 << " variable needs to be a 32-bit int scalar. "
3364 << message;
3365 })) {
3366 return error;
3367 }
3368 }
3369
3370 // Seed at reference checks with this built-in.
3371 return ValidatePrimitiveShadingRateAtReference(decoration, inst, inst, inst);
3372 }
3373
3374 spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtReference(
3375 const Decoration& decoration, const Instruction& built_in_inst,
3376 const Instruction& referenced_inst,
3377 const Instruction& referenced_from_inst) {
3378 if (spvIsVulkanEnv(_.context()->target_env)) {
3379 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
3380 if (storage_class != SpvStorageClassMax &&
3381 storage_class != SpvStorageClassOutput) {
3382 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3383 << _.VkErrorID(4485) << "Vulkan spec allows BuiltIn "
3384 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3385 decoration.params()[0])
3386 << " to be only used for variables with Output storage class. "
3387 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3388 referenced_from_inst)
3389 << " " << GetStorageClassDesc(referenced_from_inst);
3390 }
3391
3392 for (const SpvExecutionModel execution_model : execution_models_) {
3393 switch (execution_model) {
3394 case SpvExecutionModelVertex:
3395 case SpvExecutionModelGeometry:
3396 case SpvExecutionModelMeshNV:
3397 break;
3398 default: {
3399 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3400 << _.VkErrorID(4484) << "Vulkan spec allows BuiltIn "
3401 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3402 decoration.params()[0])
3403 << " to be used only with Vertex, Geometry, or MeshNV "
3404 "execution models. "
3405 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3406 referenced_from_inst, execution_model);
3407 }
3408 }
3409 }
3410 }
3411
3412 if (function_id_ == 0) {
3413 // Propagate this rule to all dependant ids in the global scope.
3414 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
3415 std::bind(&BuiltInsValidator::ValidatePrimitiveShadingRateAtReference,
3416 this, decoration, built_in_inst, referenced_from_inst,
3417 std::placeholders::_1));
3418 }
3419
3420 return SPV_SUCCESS;
3421 }
3422
3423 spv_result_t BuiltInsValidator::ValidateShadingRateAtDefinition(
3424 const Decoration& decoration, const Instruction& inst) {
3425 if (spvIsVulkanEnv(_.context()->target_env)) {
3426 if (spv_result_t error = ValidateI32(
3427 decoration, inst,
3428 [this, &inst,
3429 &decoration](const std::string& message) -> spv_result_t {
3430 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3431 << _.VkErrorID(4492)
3432 << "According to the Vulkan spec BuiltIn "
3433 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3434 decoration.params()[0])
3435 << " variable needs to be a 32-bit int scalar. "
3436 << message;
3437 })) {
3438 return error;
3439 }
3440 }
3441
3442 // Seed at reference checks with this built-in.
3443 return ValidateShadingRateAtReference(decoration, inst, inst, inst);
3444 }
3445
3446 spv_result_t BuiltInsValidator::ValidateShadingRateAtReference(
3447 const Decoration& decoration, const Instruction& built_in_inst,
3448 const Instruction& referenced_inst,
3449 const Instruction& referenced_from_inst) {
3450 if (spvIsVulkanEnv(_.context()->target_env)) {
3451 const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
3452 if (storage_class != SpvStorageClassMax &&
3453 storage_class != SpvStorageClassInput) {
3454 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3455 << _.VkErrorID(4491) << "Vulkan spec allows BuiltIn "
3456 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3457 decoration.params()[0])
3458 << " to be only used for variables with Input storage class. "
3459 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3460 referenced_from_inst)
3461 << " " << GetStorageClassDesc(referenced_from_inst);
3462 }
3463
3464 for (const SpvExecutionModel execution_model : execution_models_) {
3465 if (execution_model != SpvExecutionModelFragment) {
3466 return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
3467 << _.VkErrorID(4490) << "Vulkan spec allows BuiltIn "
3468 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3469 decoration.params()[0])
3470 << " to be used only with the Fragment execution model. "
3471 << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
3472 referenced_from_inst, execution_model);
3473 }
3474 }
3475 }
3476
3477 if (function_id_ == 0) {
3478 // Propagate this rule to all dependant ids in the global scope.
3479 id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
3480 &BuiltInsValidator::ValidateShadingRateAtReference, this, decoration,
3481 built_in_inst, referenced_from_inst, std::placeholders::_1));
3482 }
3483
3484 return SPV_SUCCESS;
3485 }
3486
3487 spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition(
3488 const Decoration& decoration, const Instruction& inst) {
3489 const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]);
3490
3491 // Builtins can only be applied to variables, structures or constants.
3492 auto target_opcode = inst.opcode();
3493 if (target_opcode != SpvOpTypeStruct && target_opcode != SpvOpVariable &&
3494 !spvOpcodeIsConstant(target_opcode)) {
3495 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3496 << "BuiltIns can only target variables, structs or constants";
3497 }
3498
3499 if (!spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
3500 // Early return. All currently implemented rules are based on Vulkan or
3501 // WebGPU spec.
3502 //
3503 // TODO: If you are adding validation rules for environments other than
3504 // Vulkan or WebGPU (or general rules which are not environment
3505 // independent), then you need to modify or remove this condition. Consider
3506 // also adding early returns into BuiltIn-specific rules, so that the system
3507 // doesn't spawn new rules which don't do anything.
3508 return SPV_SUCCESS;
3509 }
3510
3511 if (spvIsWebGPUEnv(_.context()->target_env) &&
3512 !IsBuiltInValidForWebGPU(label)) {
3513 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
3514 << "WebGPU does not allow BuiltIn "
3515 << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
3516 decoration.params()[0]);
3517 }
3518
3519 // If you are adding a new BuiltIn enum, please register it here.
3520 // If the newly added enum has validation rules associated with it
3521 // consider leaving a TODO and/or creating an issue.
3522 switch (label) {
3523 case SpvBuiltInClipDistance:
3524 case SpvBuiltInCullDistance: {
3525 return ValidateClipOrCullDistanceAtDefinition(decoration, inst);
3526 }
3527 case SpvBuiltInFragCoord: {
3528 return ValidateFragCoordAtDefinition(decoration, inst);
3529 }
3530 case SpvBuiltInFragDepth: {
3531 return ValidateFragDepthAtDefinition(decoration, inst);
3532 }
3533 case SpvBuiltInFrontFacing: {
3534 return ValidateFrontFacingAtDefinition(decoration, inst);
3535 }
3536 case SpvBuiltInGlobalInvocationId:
3537 case SpvBuiltInLocalInvocationId:
3538 case SpvBuiltInNumWorkgroups:
3539 case SpvBuiltInWorkgroupId: {
3540 return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, inst);
3541 }
3542 case SpvBuiltInHelperInvocation: {
3543 return ValidateHelperInvocationAtDefinition(decoration, inst);
3544 }
3545 case SpvBuiltInInvocationId: {
3546 return ValidateInvocationIdAtDefinition(decoration, inst);
3547 }
3548 case SpvBuiltInInstanceIndex: {
3549 return ValidateInstanceIndexAtDefinition(decoration, inst);
3550 }
3551 case SpvBuiltInLayer:
3552 case SpvBuiltInViewportIndex: {
3553 return ValidateLayerOrViewportIndexAtDefinition(decoration, inst);
3554 }
3555 case SpvBuiltInPatchVertices: {
3556 return ValidatePatchVerticesAtDefinition(decoration, inst);
3557 }
3558 case SpvBuiltInPointCoord: {
3559 return ValidatePointCoordAtDefinition(decoration, inst);
3560 }
3561 case SpvBuiltInPointSize: {
3562 return ValidatePointSizeAtDefinition(decoration, inst);
3563 }
3564 case SpvBuiltInPosition: {
3565 return ValidatePositionAtDefinition(decoration, inst);
3566 }
3567 case SpvBuiltInPrimitiveId: {
3568 return ValidatePrimitiveIdAtDefinition(decoration, inst);
3569 }
3570 case SpvBuiltInSampleId: {
3571 return ValidateSampleIdAtDefinition(decoration, inst);
3572 }
3573 case SpvBuiltInSampleMask: {
3574 return ValidateSampleMaskAtDefinition(decoration, inst);
3575 }
3576 case SpvBuiltInSamplePosition: {
3577 return ValidateSamplePositionAtDefinition(decoration, inst);
3578 }
3579 case SpvBuiltInSubgroupId:
3580 case SpvBuiltInNumSubgroups: {
3581 return ValidateComputeI32InputAtDefinition(decoration, inst);
3582 }
3583 case SpvBuiltInSubgroupLocalInvocationId:
3584 case SpvBuiltInSubgroupSize: {
3585 return ValidateI32InputAtDefinition(decoration, inst);
3586 }
3587 case SpvBuiltInSubgroupEqMask:
3588 case SpvBuiltInSubgroupGeMask:
3589 case SpvBuiltInSubgroupGtMask:
3590 case SpvBuiltInSubgroupLeMask:
3591 case SpvBuiltInSubgroupLtMask: {
3592 return ValidateI32Vec4InputAtDefinition(decoration, inst);
3593 }
3594 case SpvBuiltInTessCoord: {
3595 return ValidateTessCoordAtDefinition(decoration, inst);
3596 }
3597 case SpvBuiltInTessLevelOuter: {
3598 return ValidateTessLevelOuterAtDefinition(decoration, inst);
3599 }
3600 case SpvBuiltInTessLevelInner: {
3601 return ValidateTessLevelInnerAtDefinition(decoration, inst);
3602 }
3603 case SpvBuiltInVertexIndex: {
3604 return ValidateVertexIndexAtDefinition(decoration, inst);
3605 }
3606 case SpvBuiltInWorkgroupSize: {
3607 return ValidateWorkgroupSizeAtDefinition(decoration, inst);
3608 }
3609 case SpvBuiltInVertexId:
3610 case SpvBuiltInInstanceId: {
3611 return ValidateVertexIdOrInstanceIdAtDefinition(decoration, inst);
3612 }
3613 case SpvBuiltInLocalInvocationIndex: {
3614 return ValidateLocalInvocationIndexAtDefinition(decoration, inst);
3615 }
3616 case SpvBuiltInWarpsPerSMNV:
3617 case SpvBuiltInSMCountNV:
3618 case SpvBuiltInWarpIDNV:
3619 case SpvBuiltInSMIDNV: {
3620 return ValidateSMBuiltinsAtDefinition(decoration, inst);
3621 }
3622 case SpvBuiltInBaseInstance:
3623 case SpvBuiltInBaseVertex: {
3624 return ValidateBaseInstanceOrVertexAtDefinition(decoration, inst);
3625 }
3626 case SpvBuiltInDrawIndex: {
3627 return ValidateDrawIndexAtDefinition(decoration, inst);
3628 }
3629 case SpvBuiltInViewIndex: {
3630 return ValidateViewIndexAtDefinition(decoration, inst);
3631 }
3632 case SpvBuiltInDeviceIndex: {
3633 return ValidateDeviceIndexAtDefinition(decoration, inst);
3634 }
3635 case SpvBuiltInWorkDim:
3636 case SpvBuiltInGlobalSize:
3637 case SpvBuiltInEnqueuedWorkgroupSize:
3638 case SpvBuiltInGlobalOffset:
3639 case SpvBuiltInGlobalLinearId:
3640 case SpvBuiltInSubgroupMaxSize:
3641 case SpvBuiltInNumEnqueuedSubgroups:
3642 case SpvBuiltInBaryCoordNoPerspAMD:
3643 case SpvBuiltInBaryCoordNoPerspCentroidAMD:
3644 case SpvBuiltInBaryCoordNoPerspSampleAMD:
3645 case SpvBuiltInBaryCoordSmoothAMD:
3646 case SpvBuiltInBaryCoordSmoothCentroidAMD:
3647 case SpvBuiltInBaryCoordSmoothSampleAMD:
3648 case SpvBuiltInBaryCoordPullModelAMD:
3649 case SpvBuiltInFragStencilRefEXT:
3650 case SpvBuiltInViewportMaskNV:
3651 case SpvBuiltInSecondaryPositionNV:
3652 case SpvBuiltInSecondaryViewportMaskNV:
3653 case SpvBuiltInPositionPerViewNV:
3654 case SpvBuiltInViewportMaskPerViewNV:
3655 case SpvBuiltInFullyCoveredEXT:
3656 case SpvBuiltInMax:
3657 case SpvBuiltInTaskCountNV:
3658 case SpvBuiltInPrimitiveCountNV:
3659 case SpvBuiltInPrimitiveIndicesNV:
3660 case SpvBuiltInClipDistancePerViewNV:
3661 case SpvBuiltInCullDistancePerViewNV:
3662 case SpvBuiltInLayerPerViewNV:
3663 case SpvBuiltInMeshViewCountNV:
3664 case SpvBuiltInMeshViewIndicesNV:
3665 case SpvBuiltInBaryCoordNV:
3666 case SpvBuiltInBaryCoordNoPerspNV:
3667 case SpvBuiltInFragmentSizeNV: // alias SpvBuiltInFragSizeEXT
3668 case SpvBuiltInInvocationsPerPixelNV: // alias
3669 // SpvBuiltInFragInvocationCountEXT
3670 case SpvBuiltInLaunchIdNV:
3671 case SpvBuiltInLaunchSizeNV:
3672 case SpvBuiltInWorldRayOriginNV:
3673 case SpvBuiltInWorldRayDirectionNV:
3674 case SpvBuiltInObjectRayOriginNV:
3675 case SpvBuiltInObjectRayDirectionNV:
3676 case SpvBuiltInRayTminNV:
3677 case SpvBuiltInRayTmaxNV:
3678 case SpvBuiltInInstanceCustomIndexNV:
3679 case SpvBuiltInObjectToWorldNV:
3680 case SpvBuiltInWorldToObjectNV:
3681 case SpvBuiltInHitTNV:
3682 case SpvBuiltInHitKindNV:
3683 case SpvBuiltInIncomingRayFlagsNV:
3684 case SpvBuiltInRayGeometryIndexKHR: {
3685 // No validation rules (for the moment).
3686 break;
3687
3688 case SpvBuiltInPrimitiveShadingRateKHR:
3689 return ValidatePrimitiveShadingRateAtDefinition(decoration, inst);
3690 case SpvBuiltInShadingRateKHR:
3691 return ValidateShadingRateAtDefinition(decoration, inst);
3692 }
3693 }
3694 return SPV_SUCCESS;
3695 }
3696
3697 spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() {
3698 for (const auto& kv : _.id_decorations()) {
3699 const uint32_t id = kv.first;
3700 const auto& decorations = kv.second;
3701 if (decorations.empty()) {
3702 continue;
3703 }
3704
3705 const Instruction* inst = _.FindDef(id);
3706 assert(inst);
3707
3708 for (const auto& decoration : kv.second) {
3709 if (decoration.dec_type() != SpvDecorationBuiltIn) {
3710 continue;
3711 }
3712
3713 if (spv_result_t error =
3714 ValidateSingleBuiltInAtDefinition(decoration, *inst)) {
3715 return error;
3716 }
3717 }
3718 }
3719
3720 return SPV_SUCCESS;
3721 }
3722
3723 spv_result_t BuiltInsValidator::Run() {
3724 // First pass: validate all built-ins at definition and seed
3725 // id_to_at_reference_checks_ with built-ins.
3726 if (auto error = ValidateBuiltInsAtDefinition()) {
3727 return error;
3728 }
3729
3730 if (id_to_at_reference_checks_.empty()) {
3731 // No validation tasks were seeded. Nothing else to do.
3732 return SPV_SUCCESS;
3733 }
3734
3735 // Second pass: validate every id reference in the module using
3736 // rules in id_to_at_reference_checks_.
3737 for (const Instruction& inst : _.ordered_instructions()) {
3738 Update(inst);
3739
3740 std::set<uint32_t> already_checked;
3741
3742 for (const auto& operand : inst.operands()) {
3743 if (!spvIsIdType(operand.type)) {
3744 // Not id.
3745 continue;
3746 }
3747
3748 const uint32_t id = inst.word(operand.offset);
3749 if (id == inst.id()) {
3750 // No need to check result id.
3751 continue;
3752 }
3753
3754 if (!already_checked.insert(id).second) {
3755 // The instruction has already referenced this id.
3756 continue;
3757 }
3758
3759 // Instruction references the id. Run all checks associated with the id
3760 // on the instruction. id_to_at_reference_checks_ can be modified in the
3761 // process, iterators are safe because it's a tree-based map.
3762 const auto it = id_to_at_reference_checks_.find(id);
3763 if (it != id_to_at_reference_checks_.end()) {
3764 for (const auto& check : it->second) {
3765 if (spv_result_t error = check(inst)) {
3766 return error;
3767 }
3768 }
3769 }
3770 }
3771 }
3772
3773 return SPV_SUCCESS;
3774 }
3775
3776 } // namespace
3777
3778 // Validates correctness of built-in variables.
3779 spv_result_t ValidateBuiltIns(ValidationState_t& _) {
3780 BuiltInsValidator validator(_);
3781 return validator.Run();
3782 }
3783
3784 } // namespace val
3785 } // namespace spvtools