/src/spirv-tools/source/val/validate_memory.cpp
Line | Count | Source |
1 | | // Copyright (c) 2018 Google LLC. |
2 | | // Modifications Copyright (C) 2020-2024 Advanced Micro Devices, Inc. All |
3 | | // rights reserved. |
4 | | // Copyright (C) 2026 Qualcomm Technologies, Inc. |
5 | | // |
6 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | // you may not use this file except in compliance with the License. |
8 | | // You may obtain a copy of the License at |
9 | | // |
10 | | // http://www.apache.org/licenses/LICENSE-2.0 |
11 | | // |
12 | | // Unless required by applicable law or agreed to in writing, software |
13 | | // distributed under the License is distributed on an "AS IS" BASIS, |
14 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | // See the License for the specific language governing permissions and |
16 | | // limitations under the License. |
17 | | |
18 | | #include <algorithm> |
19 | | #include <cstdint> |
20 | | #include <string> |
21 | | #include <vector> |
22 | | |
23 | | #include "source/opcode.h" |
24 | | #include "source/spirv_target_env.h" |
25 | | #include "source/table2.h" |
26 | | #include "source/val/instruction.h" |
27 | | #include "source/val/validate.h" |
28 | | #include "source/val/validate_scopes.h" |
29 | | #include "source/val/validation_state.h" |
30 | | |
31 | | namespace spvtools { |
32 | | namespace val { |
33 | | namespace { |
34 | | |
35 | | bool AreLayoutCompatibleStructs(ValidationState_t&, const Instruction*, |
36 | | const Instruction*); |
37 | | bool HaveLayoutCompatibleMembers(ValidationState_t&, const Instruction*, |
38 | | const Instruction*); |
39 | | bool HaveSameLayoutDecorations(ValidationState_t&, const Instruction*, |
40 | | const Instruction*); |
41 | | bool HasConflictingMemberOffsets(const std::set<Decoration>&, |
42 | | const std::set<Decoration>&); |
43 | | |
44 | | bool IsAllowedTypeOrArrayOfSame(ValidationState_t& _, const Instruction& type, |
45 | 0 | std::initializer_list<spv::Op> allowed) { |
46 | 0 | if (std::find(allowed.begin(), allowed.end(), type.opcode()) != |
47 | 0 | allowed.end()) { |
48 | 0 | return true; |
49 | 0 | } |
50 | 0 | if (type.opcode() == spv::Op::OpTypeArray || |
51 | 0 | type.opcode() == spv::Op::OpTypeRuntimeArray) { |
52 | 0 | auto elem_type = _.FindDef(type.word(2)); |
53 | 0 | return std::find(allowed.begin(), allowed.end(), elem_type->opcode()) != |
54 | 0 | allowed.end(); |
55 | 0 | } |
56 | 0 | return false; |
57 | 0 | } |
58 | | |
59 | | // Returns true if the two instructions represent structs that, as far as the |
60 | | // validator can tell, have the exact same data layout. |
61 | | bool AreLayoutCompatibleStructs(ValidationState_t& _, const Instruction* type1, |
62 | 0 | const Instruction* type2) { |
63 | 0 | if (type1->opcode() != spv::Op::OpTypeStruct) { |
64 | 0 | return false; |
65 | 0 | } |
66 | 0 | if (type2->opcode() != spv::Op::OpTypeStruct) { |
67 | 0 | return false; |
68 | 0 | } |
69 | | |
70 | 0 | if (!HaveLayoutCompatibleMembers(_, type1, type2)) return false; |
71 | | |
72 | 0 | return HaveSameLayoutDecorations(_, type1, type2); |
73 | 0 | } |
74 | | |
75 | | // Returns true if the operands to the OpTypeStruct instruction defining the |
76 | | // types are the same or are layout compatible types. |type1| and |type2| must |
77 | | // be OpTypeStruct instructions. |
78 | | bool HaveLayoutCompatibleMembers(ValidationState_t& _, const Instruction* type1, |
79 | 0 | const Instruction* type2) { |
80 | 0 | assert(type1->opcode() == spv::Op::OpTypeStruct && |
81 | 0 | "type1 must be an OpTypeStruct instruction."); |
82 | 0 | assert(type2->opcode() == spv::Op::OpTypeStruct && |
83 | 0 | "type2 must be an OpTypeStruct instruction."); |
84 | 0 | const auto& type1_operands = type1->operands(); |
85 | 0 | const auto& type2_operands = type2->operands(); |
86 | 0 | if (type1_operands.size() != type2_operands.size()) { |
87 | 0 | return false; |
88 | 0 | } |
89 | | |
90 | 0 | for (size_t operand = 2; operand < type1_operands.size(); ++operand) { |
91 | 0 | if (type1->word(operand) != type2->word(operand)) { |
92 | 0 | auto def1 = _.FindDef(type1->word(operand)); |
93 | 0 | auto def2 = _.FindDef(type2->word(operand)); |
94 | 0 | if (!AreLayoutCompatibleStructs(_, def1, def2)) { |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | } |
98 | 0 | } |
99 | 0 | return true; |
100 | 0 | } |
101 | | |
102 | | // Returns true if all decorations that affect the data layout of the struct |
103 | | // (like Offset), are the same for the two types. |type1| and |type2| must be |
104 | | // OpTypeStruct instructions. |
105 | | bool HaveSameLayoutDecorations(ValidationState_t& _, const Instruction* type1, |
106 | 0 | const Instruction* type2) { |
107 | 0 | assert(type1->opcode() == spv::Op::OpTypeStruct && |
108 | 0 | "type1 must be an OpTypeStruct instruction."); |
109 | 0 | assert(type2->opcode() == spv::Op::OpTypeStruct && |
110 | 0 | "type2 must be an OpTypeStruct instruction."); |
111 | 0 | const std::set<Decoration>& type1_decorations = _.id_decorations(type1->id()); |
112 | 0 | const std::set<Decoration>& type2_decorations = _.id_decorations(type2->id()); |
113 | | |
114 | | // TODO: Will have to add other check for arrays an matricies if we want to |
115 | | // handle them. |
116 | 0 | if (HasConflictingMemberOffsets(type1_decorations, type2_decorations)) { |
117 | 0 | return false; |
118 | 0 | } |
119 | | |
120 | 0 | return true; |
121 | 0 | } |
122 | | |
123 | | bool HasConflictingMemberOffsets( |
124 | | const std::set<Decoration>& type1_decorations, |
125 | 0 | const std::set<Decoration>& type2_decorations) { |
126 | 0 | { |
127 | | // We are interested in conflicting decoration. If a decoration is in one |
128 | | // list but not the other, then we will assume the code is correct. We are |
129 | | // looking for things we know to be wrong. |
130 | | // |
131 | | // We do not have to traverse type2_decoration because, after traversing |
132 | | // type1_decorations, anything new will not be found in |
133 | | // type1_decoration. Therefore, it cannot lead to a conflict. |
134 | 0 | for (const Decoration& decoration : type1_decorations) { |
135 | 0 | switch (decoration.dec_type()) { |
136 | 0 | case spv::Decoration::Offset: { |
137 | | // Since these affect the layout of the struct, they must be present |
138 | | // in both structs. |
139 | 0 | auto compare = [&decoration](const Decoration& rhs) { |
140 | 0 | if (rhs.dec_type() != spv::Decoration::Offset) return false; |
141 | 0 | return decoration.struct_member_index() == |
142 | 0 | rhs.struct_member_index(); |
143 | 0 | }; |
144 | 0 | auto i = std::find_if(type2_decorations.begin(), |
145 | 0 | type2_decorations.end(), compare); |
146 | 0 | if (i != type2_decorations.end() && |
147 | 0 | decoration.params().front() != i->params().front()) { |
148 | 0 | return true; |
149 | 0 | } |
150 | 0 | } break; |
151 | 0 | default: |
152 | | // This decoration does not affect the layout of the structure, so |
153 | | // just moving on. |
154 | 0 | break; |
155 | 0 | } |
156 | 0 | } |
157 | 0 | } |
158 | 0 | return false; |
159 | 0 | } |
160 | | |
161 | | // If |skip_builtin| is true, returns true if |storage| contains bool within |
162 | | // it and no storage that contains the bool is builtin. |
163 | | // If |skip_builtin| is false, returns true if |storage| contains bool within |
164 | | // it. |
165 | | bool ContainsInvalidBool(ValidationState_t& _, const Instruction* storage, |
166 | 84.9k | bool skip_builtin) { |
167 | 84.9k | if (skip_builtin) { |
168 | 48.4k | for (const Decoration& decoration : _.id_decorations(storage->id())) { |
169 | 27.2k | if (decoration.dec_type() == spv::Decoration::BuiltIn) return false; |
170 | 27.2k | } |
171 | 48.4k | } |
172 | | |
173 | 84.8k | const size_t elem_type_index = 1; |
174 | 84.8k | uint32_t elem_type_id; |
175 | 84.8k | Instruction* elem_type; |
176 | | |
177 | 84.8k | switch (storage->opcode()) { |
178 | 15 | case spv::Op::OpTypeBool: |
179 | 15 | return true; |
180 | 30.8k | case spv::Op::OpTypeVector: |
181 | 31.1k | case spv::Op::OpTypeMatrix: |
182 | 32.9k | case spv::Op::OpTypeArray: |
183 | 33.7k | case spv::Op::OpTypeRuntimeArray: |
184 | 33.7k | elem_type_id = storage->GetOperandAs<uint32_t>(elem_type_index); |
185 | 33.7k | elem_type = _.FindDef(elem_type_id); |
186 | 33.7k | return ContainsInvalidBool(_, elem_type, skip_builtin); |
187 | 13.6k | case spv::Op::OpTypeStruct: |
188 | 13.6k | for (size_t member_type_index = 1; |
189 | 29.2k | member_type_index < storage->operands().size(); |
190 | 15.5k | ++member_type_index) { |
191 | 15.5k | auto member_type_id = |
192 | 15.5k | storage->GetOperandAs<uint32_t>(member_type_index); |
193 | 15.5k | auto member_type = _.FindDef(member_type_id); |
194 | 15.5k | if (ContainsInvalidBool(_, member_type, skip_builtin)) return true; |
195 | 15.5k | } |
196 | 51.1k | default: |
197 | 51.1k | break; |
198 | 84.8k | } |
199 | 51.1k | return false; |
200 | 84.8k | } |
201 | | |
202 | | std::pair<Instruction*, Instruction*> GetPointerTypes(ValidationState_t& _, |
203 | 675k | const Instruction* inst) { |
204 | 675k | Instruction* dst_pointer_type = nullptr; |
205 | 675k | Instruction* src_pointer_type = nullptr; |
206 | 675k | switch (inst->opcode()) { |
207 | 0 | case spv::Op::OpCooperativeMatrixLoadNV: |
208 | 0 | case spv::Op::OpCooperativeMatrixLoadTensorNV: |
209 | 0 | case spv::Op::OpCooperativeMatrixLoadKHR: |
210 | 0 | case spv::Op::OpCooperativeVectorLoadNV: |
211 | 320k | case spv::Op::OpLoad: |
212 | 320k | case spv::Op::OpPredicatedLoadINTEL: { |
213 | 320k | auto load_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(2)); |
214 | 320k | dst_pointer_type = _.FindDef(load_pointer->type_id()); |
215 | 320k | break; |
216 | 320k | } |
217 | 0 | case spv::Op::OpCooperativeMatrixStoreNV: |
218 | 0 | case spv::Op::OpCooperativeMatrixStoreTensorNV: |
219 | 0 | case spv::Op::OpCooperativeMatrixStoreKHR: |
220 | 0 | case spv::Op::OpCooperativeVectorStoreNV: |
221 | 263k | case spv::Op::OpStore: |
222 | 263k | case spv::Op::OpPredicatedStoreINTEL: { |
223 | 263k | auto store_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(0)); |
224 | 263k | dst_pointer_type = _.FindDef(store_pointer->type_id()); |
225 | 263k | break; |
226 | 263k | } |
227 | | // Spec: "Matching Storage Class is not required" |
228 | 91.6k | case spv::Op::OpCopyMemory: |
229 | 91.6k | case spv::Op::OpCopyMemorySized: { |
230 | 91.6k | auto dst_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(0)); |
231 | 91.6k | dst_pointer_type = _.FindDef(dst_pointer->type_id()); |
232 | 91.6k | auto src_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(1)); |
233 | 91.6k | src_pointer_type = _.FindDef(src_pointer->type_id()); |
234 | 91.6k | break; |
235 | 91.6k | } |
236 | 0 | default: |
237 | 0 | break; |
238 | 675k | } |
239 | | |
240 | 675k | return std::make_pair(dst_pointer_type, src_pointer_type); |
241 | 675k | } |
242 | | |
243 | | // Returns the number of instruction words taken up by a memory access |
244 | | // argument and its implied operands. |
245 | 43.9k | int MemoryAccessNumWords(uint32_t mask) { |
246 | 43.9k | int result = 1; // Count the mask |
247 | 43.9k | if (mask & uint32_t(spv::MemoryAccessMask::Aligned)) ++result; |
248 | 43.9k | if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) ++result; |
249 | 43.9k | if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) ++result; |
250 | 43.9k | if (mask & uint32_t(spv::MemoryAccessMask::AliasScopeINTELMask)) ++result; |
251 | 43.9k | if (mask & uint32_t(spv::MemoryAccessMask::NoAliasINTELMask)) ++result; |
252 | 43.9k | return result; |
253 | 43.9k | } |
254 | | |
255 | | // Returns the scope ID operand for MakeAvailable memory access with mask |
256 | | // at the given operand index. |
257 | | // This function is only called for OpLoad, OpStore, OpCopyMemory and |
258 | | // OpCopyMemorySized, OpCooperativeMatrixLoadNV, |
259 | | // OpCooperativeMatrixStoreNV, OpCooperativeVectorLoadNV, |
260 | | // OpCooperativeVectorStoreNV. |
261 | | uint32_t GetMakeAvailableScope(const Instruction* inst, uint32_t mask, |
262 | 0 | uint32_t mask_index) { |
263 | 0 | assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)); |
264 | 0 | uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR); |
265 | 0 | uint32_t index = |
266 | 0 | mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1))); |
267 | 0 | return inst->GetOperandAs<uint32_t>(index); |
268 | 0 | } |
269 | | |
270 | | // This function is only called for OpLoad, OpStore, OpCopyMemory, |
271 | | // OpCopyMemorySized, OpCooperativeMatrixLoadNV, |
272 | | // OpCooperativeMatrixStoreNV, OpCooperativeVectorLoadNV, |
273 | | // OpCooperativeVectorStoreNV. |
274 | | uint32_t GetMakeVisibleScope(const Instruction* inst, uint32_t mask, |
275 | 0 | uint32_t mask_index) { |
276 | 0 | assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)); |
277 | 0 | uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR); |
278 | 0 | uint32_t index = |
279 | 0 | mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1))); |
280 | 0 | return inst->GetOperandAs<uint32_t>(index); |
281 | 0 | } |
282 | | |
283 | 0 | bool DoesStructContainRTA(const ValidationState_t& _, const Instruction* inst) { |
284 | 0 | for (size_t member_index = 1; member_index < inst->operands().size(); |
285 | 0 | ++member_index) { |
286 | 0 | const auto member_id = inst->GetOperandAs<uint32_t>(member_index); |
287 | 0 | const auto member_type = _.FindDef(member_id); |
288 | 0 | if (member_type->opcode() == spv::Op::OpTypeRuntimeArray) return true; |
289 | 0 | } |
290 | 0 | return false; |
291 | 0 | } |
292 | | |
293 | | spv_result_t CheckMemoryAccess(ValidationState_t& _, const Instruction* inst, |
294 | 675k | uint32_t index) { |
295 | 675k | Instruction* dst_pointer_type = nullptr; |
296 | 675k | Instruction* src_pointer_type = nullptr; // only used for OpCopyMemory |
297 | 675k | std::tie(dst_pointer_type, src_pointer_type) = GetPointerTypes(_, inst); |
298 | | |
299 | 675k | const spv::StorageClass dst_sc = |
300 | 675k | dst_pointer_type ? dst_pointer_type->GetOperandAs<spv::StorageClass>(1) |
301 | 675k | : spv::StorageClass::Max; |
302 | 675k | const spv::StorageClass src_sc = |
303 | 675k | src_pointer_type ? src_pointer_type->GetOperandAs<spv::StorageClass>(1) |
304 | 675k | : spv::StorageClass::Max; |
305 | | |
306 | 675k | if (inst->operands().size() <= index) { |
307 | | // Cases where lack of some operand is invalid |
308 | 574k | if (src_sc == spv::StorageClass::PhysicalStorageBuffer || |
309 | 574k | dst_sc == spv::StorageClass::PhysicalStorageBuffer) { |
310 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
311 | 0 | << _.VkErrorID(4708) |
312 | 0 | << "Memory accesses with PhysicalStorageBuffer must use Aligned."; |
313 | 0 | } |
314 | 574k | return SPV_SUCCESS; |
315 | 574k | } |
316 | | |
317 | 101k | const uint32_t mask = inst->GetOperandAs<uint32_t>(index); |
318 | 101k | if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) { |
319 | 0 | if (inst->opcode() == spv::Op::OpLoad || |
320 | 0 | inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV || |
321 | 0 | inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV || |
322 | 0 | inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR || |
323 | 0 | inst->opcode() == spv::Op::OpCooperativeVectorLoadNV || |
324 | 0 | inst->opcode() == spv::Op::OpPredicatedLoadINTEL) { |
325 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
326 | 0 | << "MakePointerAvailableKHR cannot be used with OpLoad."; |
327 | 0 | } |
328 | | |
329 | 0 | if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) { |
330 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
331 | 0 | << "NonPrivatePointerKHR must be specified if " |
332 | 0 | "MakePointerAvailableKHR is specified."; |
333 | 0 | } |
334 | | |
335 | | // Check the associated scope for MakeAvailableKHR. |
336 | 0 | const auto available_scope = GetMakeAvailableScope(inst, mask, index); |
337 | 0 | if (auto error = ValidateMemoryScope(_, inst, available_scope)) |
338 | 0 | return error; |
339 | 0 | } |
340 | | |
341 | 101k | if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) { |
342 | 0 | if (inst->opcode() == spv::Op::OpStore || |
343 | 0 | inst->opcode() == spv::Op::OpCooperativeMatrixStoreNV || |
344 | 0 | inst->opcode() == spv::Op::OpCooperativeMatrixStoreKHR || |
345 | 0 | inst->opcode() == spv::Op::OpCooperativeMatrixStoreTensorNV || |
346 | 0 | inst->opcode() == spv::Op::OpCooperativeVectorStoreNV || |
347 | 0 | inst->opcode() == spv::Op::OpPredicatedStoreINTEL) { |
348 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
349 | 0 | << "MakePointerVisibleKHR cannot be used with OpStore."; |
350 | 0 | } |
351 | | |
352 | 0 | if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) { |
353 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
354 | 0 | << "NonPrivatePointerKHR must be specified if " |
355 | 0 | << "MakePointerVisibleKHR is specified."; |
356 | 0 | } |
357 | | |
358 | | // Check the associated scope for MakeVisibleKHR. |
359 | 0 | const auto visible_scope = GetMakeVisibleScope(inst, mask, index); |
360 | 0 | if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error; |
361 | 0 | } |
362 | | |
363 | 101k | if (mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR)) { |
364 | 0 | if (dst_sc != spv::StorageClass::Uniform && |
365 | 0 | dst_sc != spv::StorageClass::Workgroup && |
366 | 0 | dst_sc != spv::StorageClass::CrossWorkgroup && |
367 | 0 | dst_sc != spv::StorageClass::Generic && |
368 | 0 | dst_sc != spv::StorageClass::Image && |
369 | 0 | dst_sc != spv::StorageClass::StorageBuffer && |
370 | 0 | dst_sc != spv::StorageClass::PhysicalStorageBuffer) { |
371 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
372 | 0 | << "NonPrivatePointerKHR requires a pointer in Uniform, " |
373 | 0 | << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer " |
374 | 0 | << "storage classes."; |
375 | 0 | } |
376 | 0 | if (src_sc != spv::StorageClass::Max && |
377 | 0 | src_sc != spv::StorageClass::Uniform && |
378 | 0 | src_sc != spv::StorageClass::Workgroup && |
379 | 0 | src_sc != spv::StorageClass::CrossWorkgroup && |
380 | 0 | src_sc != spv::StorageClass::Generic && |
381 | 0 | src_sc != spv::StorageClass::Image && |
382 | 0 | src_sc != spv::StorageClass::StorageBuffer && |
383 | 0 | src_sc != spv::StorageClass::PhysicalStorageBuffer) { |
384 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
385 | 0 | << "NonPrivatePointerKHR requires a pointer in Uniform, " |
386 | 0 | << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer " |
387 | 0 | << "storage classes."; |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | 101k | if (!(mask & uint32_t(spv::MemoryAccessMask::Aligned))) { |
392 | 101k | if (src_sc == spv::StorageClass::PhysicalStorageBuffer || |
393 | 101k | dst_sc == spv::StorageClass::PhysicalStorageBuffer) { |
394 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
395 | 0 | << _.VkErrorID(4708) |
396 | 0 | << "Memory accesses with PhysicalStorageBuffer must use Aligned."; |
397 | 0 | } |
398 | 101k | } else { |
399 | | // even if there are other masks, the Aligned operand will be next |
400 | 32 | const uint32_t aligned_value = inst->GetOperandAs<uint32_t>(index + 1); |
401 | 32 | const bool is_power_of_two = |
402 | 32 | aligned_value && !(aligned_value & (aligned_value - 1)); |
403 | 32 | if (!is_power_of_two) { |
404 | 13 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
405 | 13 | << "Memory accesses Aligned operand value " << aligned_value |
406 | 13 | << " is not a power of two."; |
407 | 13 | } |
408 | | |
409 | 19 | uint32_t largest_scalar = 0; |
410 | 19 | if (dst_sc == spv::StorageClass::PhysicalStorageBuffer) { |
411 | 0 | if (dst_pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR) { |
412 | 0 | largest_scalar = |
413 | 0 | _.GetLargestScalarType(dst_pointer_type->GetOperandAs<uint32_t>(2)); |
414 | 0 | } else if (inst->type_id() != 0) { |
415 | 0 | largest_scalar = _.GetLargestScalarType(inst->type_id()); |
416 | 0 | } else { |
417 | | // TODO need to handle cases like OpStore and OpCopyMemorySized which |
418 | | // don't have a result type |
419 | 0 | } |
420 | 0 | } |
421 | | // TODO - Handle Untyped in OpCopyMemory |
422 | 19 | if (src_sc == spv::StorageClass::PhysicalStorageBuffer && |
423 | 0 | src_pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR) { |
424 | 0 | largest_scalar = std::max( |
425 | 0 | largest_scalar, |
426 | 0 | _.GetLargestScalarType(src_pointer_type->GetOperandAs<uint32_t>(2))); |
427 | 0 | } |
428 | 19 | if (aligned_value < largest_scalar) { |
429 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
430 | 0 | << _.VkErrorID(6314) << "Memory accesses Aligned operand value " |
431 | 0 | << aligned_value << " is too small, the largest scalar type is " |
432 | 0 | << largest_scalar << " bytes."; |
433 | 0 | } |
434 | 19 | } |
435 | | |
436 | 101k | return SPV_SUCCESS; |
437 | 101k | } |
438 | | |
439 | | spv_result_t ValidateVariableInitializer(ValidationState_t& _, |
440 | | const Instruction* inst, |
441 | | spv::StorageClass storage_class, |
442 | 200k | uint32_t value_id) { |
443 | 200k | const bool untyped_pointer = inst->opcode() == spv::Op::OpUntypedVariableKHR; |
444 | 200k | const uint32_t initializer_index = untyped_pointer ? 4u : 3u; |
445 | 200k | if (initializer_index < inst->operands().size()) { |
446 | 6.55k | const uint32_t initializer_id = |
447 | 6.55k | inst->GetOperandAs<uint32_t>(initializer_index); |
448 | 6.55k | const Instruction* initializer = _.FindDef(initializer_id); |
449 | 6.55k | const uint32_t storage_class_index = 2u; |
450 | 6.55k | const bool is_module_scope_var = |
451 | 6.55k | initializer && |
452 | 6.55k | (initializer->opcode() == spv::Op::OpVariable || |
453 | 6.55k | initializer->opcode() == spv::Op::OpUntypedVariableKHR) && |
454 | 4 | (initializer->GetOperandAs<spv::StorageClass>(storage_class_index) != |
455 | 4 | spv::StorageClass::Function); |
456 | 6.55k | const bool is_constant = |
457 | 6.55k | initializer && spvOpcodeIsConstant(initializer->opcode()); |
458 | 6.55k | if (!initializer || !(is_constant || is_module_scope_var)) { |
459 | 6 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
460 | 6 | << "Variable Initializer <id> " << _.getIdName(initializer_id) |
461 | 6 | << " is not a constant or module-scope variable."; |
462 | 6 | } |
463 | 6.55k | if (initializer->type_id() != value_id) { |
464 | 4 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
465 | 4 | << "Initializer type must match the data type"; |
466 | 4 | } |
467 | 6.55k | } |
468 | | |
469 | | // Vulkan Appendix A: Check that if contains initializer, then |
470 | | // storage class is Output, Private, or Function. |
471 | 200k | if (inst->operands().size() > initializer_index && |
472 | 6.54k | storage_class != spv::StorageClass::Output && |
473 | 6.41k | storage_class != spv::StorageClass::Private && |
474 | 1.73k | storage_class != spv::StorageClass::Function) { |
475 | 20 | if (spvIsVulkanEnv(_.context()->target_env)) { |
476 | 0 | if (storage_class == spv::StorageClass::Workgroup) { |
477 | 0 | auto init_id = inst->GetOperandAs<uint32_t>(initializer_index); |
478 | 0 | auto init = _.FindDef(init_id); |
479 | 0 | if (init->opcode() != spv::Op::OpConstantNull) { |
480 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
481 | 0 | << _.VkErrorID(4734) << "OpVariable, <id> " |
482 | 0 | << _.getIdName(inst->id()) |
483 | 0 | << ", initializers are limited to OpConstantNull in " |
484 | 0 | "Workgroup " |
485 | 0 | "storage class"; |
486 | 0 | } |
487 | 0 | } else if (storage_class != spv::StorageClass::Output && |
488 | 0 | storage_class != spv::StorageClass::Private && |
489 | 0 | storage_class != spv::StorageClass::Function) { |
490 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
491 | 0 | << _.VkErrorID(4651) << "OpVariable, <id> " |
492 | 0 | << _.getIdName(inst->id()) |
493 | 0 | << ", has a disallowed initializer & storage class " |
494 | 0 | << "combination.\n" |
495 | 0 | << "From " << spvLogStringForEnv(_.context()->target_env) |
496 | 0 | << " spec:\n" |
497 | 0 | << "Variable declarations that include initializers must have " |
498 | 0 | << "one of the following storage classes: Output, Private, " |
499 | 0 | << "Function or Workgroup"; |
500 | 0 | } |
501 | 0 | } |
502 | 20 | } |
503 | | |
504 | 200k | if (initializer_index < inst->operands().size()) { |
505 | 6.54k | if (storage_class == spv::StorageClass::TaskPayloadWorkgroupEXT) { |
506 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
507 | 0 | << "OpVariable, <id> " << _.getIdName(inst->id()) |
508 | 0 | << ", initializer are not allowed for TaskPayloadWorkgroupEXT"; |
509 | 0 | } |
510 | 6.54k | if (storage_class == spv::StorageClass::Input) { |
511 | 5 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
512 | 5 | << "OpVariable, <id> " << _.getIdName(inst->id()) |
513 | 5 | << ", initializer are not allowed for Input"; |
514 | 5 | } |
515 | 6.54k | if (storage_class == spv::StorageClass::HitObjectAttributeNV) { |
516 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
517 | 0 | << "OpVariable, <id> " << _.getIdName(inst->id()) |
518 | 0 | << ", initializer are not allowed for HitObjectAttributeNV"; |
519 | 0 | } |
520 | 6.54k | if (storage_class == spv::StorageClass::HitObjectAttributeEXT) { |
521 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
522 | 0 | << "OpVariable, <id> " << _.getIdName(inst->id()) |
523 | 0 | << ", initializer are not allowed for HitObjectAttributeEXT"; |
524 | 0 | } |
525 | 6.54k | } |
526 | | |
527 | 200k | return SPV_SUCCESS; |
528 | 200k | } |
529 | | |
530 | | spv_result_t ValidateVariableStorageClass(ValidationState_t& _, |
531 | | const Instruction* inst, |
532 | | spv::StorageClass storage_class, |
533 | 200k | const Instruction* value_type) { |
534 | 200k | if (storage_class != spv::StorageClass::Workgroup && |
535 | 199k | storage_class != spv::StorageClass::CrossWorkgroup && |
536 | 199k | storage_class != spv::StorageClass::Private && |
537 | 186k | storage_class != spv::StorageClass::Function && |
538 | 42.1k | storage_class != spv::StorageClass::UniformConstant && |
539 | 39.0k | storage_class != spv::StorageClass::RayPayloadKHR && |
540 | 39.0k | storage_class != spv::StorageClass::IncomingRayPayloadKHR && |
541 | 39.0k | storage_class != spv::StorageClass::HitAttributeKHR && |
542 | 39.0k | storage_class != spv::StorageClass::CallableDataKHR && |
543 | 39.0k | storage_class != spv::StorageClass::IncomingCallableDataKHR && |
544 | 39.0k | storage_class != spv::StorageClass::TaskPayloadWorkgroupEXT && |
545 | 39.0k | storage_class != spv::StorageClass::HitObjectAttributeNV && |
546 | 39.0k | storage_class != spv::StorageClass::HitObjectAttributeEXT && |
547 | 39.0k | storage_class != spv::StorageClass::NodePayloadAMDX) { |
548 | 39.0k | bool storage_input_or_output = storage_class == spv::StorageClass::Input || |
549 | 27.9k | storage_class == spv::StorageClass::Output; |
550 | 39.0k | bool builtin = false; |
551 | 39.0k | if (storage_input_or_output) { |
552 | 26.7k | for (const Decoration& decoration : _.id_decorations(inst->id())) { |
553 | 16.9k | if (decoration.dec_type() == spv::Decoration::BuiltIn) { |
554 | 3.42k | builtin = true; |
555 | 3.42k | break; |
556 | 3.42k | } |
557 | 16.9k | } |
558 | 26.7k | } |
559 | 39.0k | if (!builtin && value_type && |
560 | 35.6k | ContainsInvalidBool(_, value_type, storage_input_or_output)) { |
561 | 15 | if (storage_input_or_output) { |
562 | 6 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
563 | 6 | << _.VkErrorID(7290) |
564 | 6 | << "If OpTypeBool is stored in conjunction with OpVariable " |
565 | 6 | "using Input or Output Storage Classes it requires a BuiltIn " |
566 | 6 | "decoration"; |
567 | | |
568 | 9 | } else { |
569 | 9 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
570 | 9 | << "If OpTypeBool is stored in conjunction with OpVariable, it " |
571 | 9 | "can only be used with non-externally visible shader Storage " |
572 | 9 | "Classes: Workgroup, CrossWorkgroup, Private, Function, " |
573 | 9 | "Input, Output, RayPayloadKHR, IncomingRayPayloadKHR, " |
574 | 9 | "HitAttributeKHR, CallableDataKHR, " |
575 | 9 | "IncomingCallableDataKHR, NodePayloadAMDX, or " |
576 | 9 | "UniformConstant"; |
577 | 9 | } |
578 | 15 | } |
579 | 39.0k | } |
580 | | |
581 | 200k | if (!_.IsValidStorageClass(storage_class)) { |
582 | 0 | return _.diag(SPV_ERROR_INVALID_BINARY, inst) |
583 | 0 | << _.VkErrorID(4643) |
584 | 0 | << "Invalid storage class for target environment"; |
585 | 0 | } |
586 | | |
587 | 200k | if (storage_class == spv::StorageClass::Generic) { |
588 | 3 | return _.diag(SPV_ERROR_INVALID_BINARY, inst) |
589 | 3 | << "Variable storage class cannot be Generic"; |
590 | 3 | } |
591 | | |
592 | 200k | if (inst->function() && storage_class != spv::StorageClass::Function) { |
593 | 30 | return _.diag(SPV_ERROR_INVALID_LAYOUT, inst) |
594 | 30 | << "Variables must have a function[7] storage class inside" |
595 | 30 | " of a function"; |
596 | 30 | } |
597 | | |
598 | 200k | if (!inst->function() && storage_class == spv::StorageClass::Function) { |
599 | 11 | return _.diag(SPV_ERROR_INVALID_LAYOUT, inst) |
600 | 11 | << "Variables can not have a function[7] storage class " |
601 | 11 | "outside of a function"; |
602 | 11 | } |
603 | | |
604 | | // SPIR-V 3.32.8: Check that pointer type and variable type have the same |
605 | | // storage class. |
606 | 200k | auto result_type = _.FindDef(inst->type_id()); |
607 | 200k | const auto result_storage_class_index = 1; |
608 | 200k | const auto result_storage_class = |
609 | 200k | result_type->GetOperandAs<spv::StorageClass>(result_storage_class_index); |
610 | 200k | if (storage_class != result_storage_class) { |
611 | 119 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
612 | 119 | << "Storage class must match result type storage class"; |
613 | 119 | } |
614 | | |
615 | 199k | if (storage_class == spv::StorageClass::PhysicalStorageBuffer) { |
616 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
617 | 0 | << "PhysicalStorageBuffer must not be used with OpVariable."; |
618 | 0 | } |
619 | | |
620 | 199k | if (storage_class == spv::StorageClass::TileAttachmentQCOM && |
621 | 0 | !_.HasCapability(spv::Capability::TileShadingQCOM)) { |
622 | 0 | return _.diag(SPV_ERROR_INVALID_CAPABILITY, inst) |
623 | 0 | << _.VkErrorID(10689) |
624 | 0 | << "the TileAttachmentQCOM storage class variable requires " |
625 | 0 | "TileShadingQCOM capability enabled."; |
626 | 0 | } |
627 | | |
628 | 199k | return SPV_SUCCESS; |
629 | 199k | } |
630 | | |
631 | | spv_result_t ValidateVariablePointer(ValidationState_t& _, |
632 | | const Instruction* inst, |
633 | | spv::StorageClass storage_class, |
634 | 199k | const Instruction& pointee) { |
635 | 199k | if ((_.addressing_model() == spv::AddressingModel::Logical || |
636 | 154 | _.addressing_model() == spv::AddressingModel::PhysicalStorageBuffer64) && |
637 | 199k | !_.options()->relax_logical_pointer) { |
638 | 199k | spv_result_t error = SPV_SUCCESS; |
639 | 199k | bool contains_logical_pointer = _.ContainsType( |
640 | 199k | pointee.id(), |
641 | 354k | [&_, inst, &error](const Instruction* type) { |
642 | 354k | if (type->opcode() == spv::Op::OpTypePointer || |
643 | 354k | type->opcode() == spv::Op::OpTypeUntypedPointerKHR) { |
644 | 23 | const auto sc = type->GetOperandAs<spv::StorageClass>(1u); |
645 | 23 | if (sc != spv::StorageClass::PhysicalStorageBuffer) { |
646 | 23 | if (sc != spv::StorageClass::StorageBuffer && |
647 | 23 | sc != spv::StorageClass::Workgroup) { |
648 | 19 | error = |
649 | 19 | _.diag(SPV_ERROR_INVALID_ID, inst) |
650 | 19 | << "In Logical addressing, variables can only allocate a " |
651 | 19 | "pointer to the StorageBuffer or Workgroup storage " |
652 | 19 | "classes"; |
653 | 19 | } else if (!_.HasCapability( |
654 | 4 | spv::Capability::VariablePointersStorageBuffer) && |
655 | 4 | sc == spv::StorageClass::StorageBuffer) { |
656 | 0 | error = |
657 | 0 | _.diag(SPV_ERROR_INVALID_ID, inst) |
658 | 0 | << "In Logical addressing, variables can only allocate a " |
659 | 0 | "storage buffer pointer if the " |
660 | 0 | "VariablePointersStorageBuffer capability is declared"; |
661 | 4 | } else if (!_.HasCapability(spv::Capability::VariablePointers) && |
662 | 4 | sc == spv::StorageClass::Workgroup) { |
663 | 4 | error = |
664 | 4 | _.diag(SPV_ERROR_INVALID_ID, inst) |
665 | 4 | << "In Logical addressing, variables can only allocate a " |
666 | 4 | "workgroup pointer if the VariablePointers capability " |
667 | 4 | "is " |
668 | 4 | "declared"; |
669 | 4 | } |
670 | 23 | return true; |
671 | 23 | } |
672 | 23 | } |
673 | 354k | return false; |
674 | 354k | }, |
675 | 199k | /* traverse_all_types = */ false); |
676 | | |
677 | 199k | if (error != SPV_SUCCESS) return error; |
678 | | |
679 | 199k | if (contains_logical_pointer) { |
680 | 0 | if (storage_class != spv::StorageClass::Function && |
681 | 0 | storage_class != spv::StorageClass::Private) { |
682 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
683 | 0 | << "In Logical addressing with variable pointers, variables " |
684 | 0 | << "that allocate pointers must be in Function or Private " |
685 | 0 | << "storage classes"; |
686 | 0 | } |
687 | 0 | } |
688 | 199k | } |
689 | | |
690 | 199k | return SPV_SUCCESS; |
691 | 199k | } |
692 | | |
693 | | spv_result_t ValidateVariableVulkanDescriptor(ValidationState_t& _, |
694 | | const Instruction* inst, |
695 | | spv::StorageClass storage_class, |
696 | 0 | const Instruction& pointee) { |
697 | | // Vulkan Push Constant Interface section: Check type of PushConstant |
698 | | // variables. |
699 | 0 | if (storage_class == spv::StorageClass::PushConstant) { |
700 | 0 | if (pointee.opcode() != spv::Op::OpTypeStruct) { |
701 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
702 | 0 | << _.VkErrorID(6808) << "PushConstant OpVariable <id> " |
703 | 0 | << _.getIdName(inst->id()) << " has illegal type.\n" |
704 | 0 | << "From Vulkan spec, Push Constant Interface section:\n" |
705 | 0 | << "Such variables must be typed as OpTypeStruct"; |
706 | 0 | } |
707 | 0 | } |
708 | | |
709 | | // Vulkan Descriptor Set Interface: Check type of UniformConstant and |
710 | | // Uniform variables. |
711 | 0 | if (storage_class == spv::StorageClass::UniformConstant) { |
712 | 0 | if (!IsAllowedTypeOrArrayOfSame( |
713 | 0 | _, pointee, |
714 | 0 | {spv::Op::OpTypeImage, spv::Op::OpTypeSampler, |
715 | 0 | spv::Op::OpTypeSampledImage, spv::Op::OpTypeTensorARM, |
716 | 0 | spv::Op::OpTypeAccelerationStructureKHR})) { |
717 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
718 | 0 | << _.VkErrorID(4655) << "UniformConstant OpVariable <id> " |
719 | 0 | << _.getIdName(inst->id()) << " has illegal type.\n" |
720 | 0 | << "Variables identified with the UniformConstant storage class " |
721 | 0 | << "are used only as handles to refer to opaque resources. Such " |
722 | 0 | << "variables must be typed as OpTypeImage, OpTypeSampler, " |
723 | 0 | << "OpTypeSampledImage, OpTypeAccelerationStructureKHR, " |
724 | 0 | << "or an array of one of these types."; |
725 | 0 | } |
726 | 0 | } |
727 | | |
728 | 0 | if (storage_class == spv::StorageClass::Uniform) { |
729 | 0 | if (!IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) { |
730 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
731 | 0 | << _.VkErrorID(6807) << "Uniform OpVariable <id> " |
732 | 0 | << _.getIdName(inst->id()) << " has illegal type.\n" |
733 | 0 | << "From Vulkan spec:\n" |
734 | 0 | << "Variables identified with the Uniform storage class are " |
735 | 0 | << "used to access transparent buffer backed resources. Such " |
736 | 0 | << "variables must be typed as OpTypeStruct, or an array of " |
737 | 0 | << "this type"; |
738 | 0 | } |
739 | 0 | } |
740 | | |
741 | 0 | if (storage_class == spv::StorageClass::StorageBuffer) { |
742 | 0 | if (!IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) { |
743 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
744 | 0 | << _.VkErrorID(6807) << "StorageBuffer OpVariable <id> " |
745 | 0 | << _.getIdName(inst->id()) << " has illegal type.\n" |
746 | 0 | << "From Vulkan spec:\n" |
747 | 0 | << "Variables identified with the StorageBuffer storage class " |
748 | 0 | "are used to access transparent buffer backed resources. " |
749 | 0 | "Such variables must be typed as OpTypeStruct, or an array " |
750 | 0 | "of this type"; |
751 | 0 | } |
752 | 0 | } |
753 | | |
754 | 0 | return SPV_SUCCESS; |
755 | 0 | } |
756 | | |
757 | | spv_result_t ValidateVariableVulkanInterface(ValidationState_t& _, |
758 | | const Instruction* inst, |
759 | | spv::StorageClass storage_class, |
760 | | const Instruction* value_type, |
761 | 0 | uint32_t value_id) { |
762 | | // Check for invalid use of Invariant |
763 | 0 | if (storage_class != spv::StorageClass::Input && |
764 | 0 | storage_class != spv::StorageClass::Output) { |
765 | 0 | if (_.HasDecoration(inst->id(), spv::Decoration::Invariant)) { |
766 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
767 | 0 | << _.VkErrorID(4677) |
768 | 0 | << "Variable decorated with Invariant must only be identified " |
769 | 0 | "with the Input or Output storage class in Vulkan " |
770 | 0 | "environment."; |
771 | 0 | } |
772 | | // Need to check if only the members in a struct are decorated |
773 | 0 | if (value_type && value_type->opcode() == spv::Op::OpTypeStruct) { |
774 | 0 | if (_.HasDecoration(value_id, spv::Decoration::Invariant)) { |
775 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
776 | 0 | << _.VkErrorID(4677) |
777 | 0 | << "Variable struct member decorated with Invariant must only " |
778 | 0 | "be identified with the Input or Output storage class in " |
779 | 0 | "Vulkan environment."; |
780 | 0 | } |
781 | 0 | } |
782 | 0 | } |
783 | | |
784 | 0 | return SPV_SUCCESS; |
785 | 0 | } |
786 | | |
787 | | spv_result_t ValidateVariableCoopMat(ValidationState_t& _, |
788 | | const Instruction* inst, |
789 | | spv::StorageClass storage_class, |
790 | 199k | const Instruction& pointee) { |
791 | | // Cooperative matrix types can only be allocated in Function or Private |
792 | 199k | if ((storage_class != spv::StorageClass::Function && |
793 | 55.1k | storage_class != spv::StorageClass::Private) && |
794 | 100k | _.ContainsType(pointee.id(), [](const Instruction* type_inst) { |
795 | 100k | auto opcode = type_inst->opcode(); |
796 | 100k | return opcode == spv::Op::OpTypeCooperativeMatrixNV || |
797 | 100k | opcode == spv::Op::OpTypeCooperativeMatrixKHR; |
798 | 100k | })) { |
799 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
800 | 0 | << "Cooperative matrix types (or types containing them) can only be " |
801 | 0 | "allocated " |
802 | 0 | << "in Function or Private storage classes or as function " |
803 | 0 | "parameters"; |
804 | 0 | } |
805 | | |
806 | 199k | return SPV_SUCCESS; |
807 | 199k | } |
808 | | |
809 | | // Vulkan specific validation rules for OpTypeRuntimeArray |
810 | | spv_result_t ValidateVariableVulkanArray(ValidationState_t& _, |
811 | | const Instruction* inst, |
812 | | spv::StorageClass storage_class, |
813 | | const Instruction& value_type, |
814 | 0 | uint32_t value_id) { |
815 | | // OpTypeRuntimeArray should only ever be in a container like OpTypeStruct, |
816 | | // so should never appear as a bare variable. |
817 | | // Unless the module has the RuntimeDescriptorArray capability. |
818 | 0 | if (value_type.opcode() == spv::Op::OpTypeRuntimeArray) { |
819 | 0 | if (!_.HasCapability(spv::Capability::RuntimeDescriptorArray)) { |
820 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
821 | 0 | << _.VkErrorID(4680) << "OpVariable, <id> " |
822 | 0 | << _.getIdName(inst->id()) |
823 | 0 | << ", is attempting to create memory for an illegal type, " |
824 | 0 | << "OpTypeRuntimeArray.\nFor Vulkan OpTypeRuntimeArray can only " |
825 | 0 | << "appear as the final member of an OpTypeStruct, thus cannot " |
826 | 0 | << "be instantiated via OpVariable, unless the " |
827 | 0 | "RuntimeDescriptorArray Capability is declared"; |
828 | 0 | } else { |
829 | | // A bare variable OpTypeRuntimeArray is allowed in this context, but |
830 | | // still need to check the storage class. |
831 | 0 | if (storage_class != spv::StorageClass::StorageBuffer && |
832 | 0 | storage_class != spv::StorageClass::Uniform && |
833 | 0 | storage_class != spv::StorageClass::UniformConstant) { |
834 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
835 | 0 | << _.VkErrorID(4680) |
836 | 0 | << "For Vulkan with RuntimeDescriptorArray, a variable " |
837 | 0 | << "containing OpTypeRuntimeArray must have storage class of " |
838 | 0 | << "StorageBuffer, Uniform, or UniformConstant."; |
839 | 0 | } |
840 | 0 | } |
841 | 0 | } |
842 | | |
843 | | // If an OpStruct has an OpTypeRuntimeArray somewhere within it, then it |
844 | | // must either have the storage class StorageBuffer and be decorated |
845 | | // with Block, or it must be in the Uniform storage class |
846 | 0 | if (value_type.opcode() == spv::Op::OpTypeStruct) { |
847 | 0 | if (DoesStructContainRTA(_, &value_type)) { |
848 | 0 | if (storage_class == spv::StorageClass::StorageBuffer || |
849 | 0 | storage_class == spv::StorageClass::PhysicalStorageBuffer) { |
850 | 0 | if (!_.HasDecoration(value_id, spv::Decoration::Block)) { |
851 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
852 | 0 | << _.VkErrorID(4680) |
853 | 0 | << "For Vulkan, an OpTypeStruct variable containing an " |
854 | 0 | << "OpTypeRuntimeArray must be decorated with Block if it " |
855 | 0 | << "has storage class StorageBuffer or " |
856 | 0 | "PhysicalStorageBuffer."; |
857 | 0 | } |
858 | 0 | } else if (storage_class == spv::StorageClass::Uniform) { |
859 | | // BufferBlock Uniform were always allowed. |
860 | | // |
861 | | // Block Uniform use to be invalid, but Vulkan added |
862 | | // VK_EXT_shader_uniform_buffer_unsized_array and now this is |
863 | | // validated at runtime |
864 | | // |
865 | | // The uniform must have either the Block or BufferBlock decoration |
866 | | // (see VUID-StandaloneSpirv-Uniform-06676) |
867 | 0 | } else { |
868 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
869 | 0 | << _.VkErrorID(4680) |
870 | 0 | << "For Vulkan, OpTypeStruct variables containing " |
871 | 0 | << "OpTypeRuntimeArray must have storage class of " |
872 | 0 | << "StorageBuffer, PhysicalStorageBuffer, or Uniform."; |
873 | 0 | } |
874 | 0 | } |
875 | 0 | } |
876 | | |
877 | 0 | return SPV_SUCCESS; |
878 | 0 | } |
879 | | |
880 | | // Vulkan-specific validation for long vectors |
881 | | spv_result_t ValidateVariableVulkanLongVector(ValidationState_t& _, |
882 | | const Instruction* inst, |
883 | | spv::StorageClass storage_class, |
884 | 0 | const Instruction& pointee) { |
885 | 0 | if (_.HasCapability(spv::Capability::LongVectorEXT)) { |
886 | 0 | if ((storage_class != spv::StorageClass::Function && |
887 | 0 | storage_class != spv::StorageClass::Private && |
888 | 0 | storage_class != spv::StorageClass::StorageBuffer && |
889 | 0 | storage_class != spv::StorageClass::PhysicalStorageBuffer && |
890 | 0 | storage_class != spv::StorageClass::Workgroup && |
891 | 0 | storage_class != spv::StorageClass::Uniform && |
892 | 0 | storage_class != spv::StorageClass::PushConstant && |
893 | 0 | storage_class != spv::StorageClass::ShaderRecordBufferKHR) && |
894 | 0 | _.ContainsType(pointee.id(), [&](const Instruction* type_inst) { |
895 | 0 | auto opcode = type_inst->opcode(); |
896 | 0 | if (opcode == spv::Op::OpTypeVector || |
897 | 0 | opcode == spv::Op::OpTypeVectorIdEXT) { |
898 | 0 | uint32_t dim = _.GetDimension(type_inst->id()); |
899 | 0 | return dim > 4; |
900 | 0 | } |
901 | 0 | return false; |
902 | 0 | })) { |
903 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
904 | 0 | << _.VkErrorID(12297) |
905 | 0 | << "Long vector types with more than 4 components (or types " |
906 | 0 | "containing them) not supported in storage class " |
907 | 0 | << StorageClassToString(storage_class); |
908 | 0 | } |
909 | | |
910 | 0 | if ((storage_class == spv::StorageClass::StorageBuffer || |
911 | 0 | storage_class == spv::StorageClass::PhysicalStorageBuffer || |
912 | 0 | storage_class == spv::StorageClass::Uniform || |
913 | 0 | storage_class == spv::StorageClass::PushConstant || |
914 | 0 | storage_class == spv::StorageClass::ShaderRecordBufferKHR || |
915 | 0 | (storage_class == spv::StorageClass::Workgroup && |
916 | 0 | _.HasDecoration(pointee.id(), spv::Decoration::Block))) && |
917 | 0 | _.ContainsType(pointee.id(), [&](const Instruction* type_inst) { |
918 | 0 | auto opcode = type_inst->opcode(); |
919 | 0 | if (opcode == spv::Op::OpTypeVectorIdEXT) { |
920 | 0 | auto component_count = |
921 | 0 | _.FindDef(type_inst->GetOperandAs<uint32_t>(2u)); |
922 | 0 | return (bool)spvOpcodeIsSpecConstant(component_count->opcode()); |
923 | 0 | } |
924 | 0 | return false; |
925 | 0 | })) { |
926 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
927 | 0 | << _.VkErrorID(12294) |
928 | 0 | << "Long vector types with spec constant component count " |
929 | 0 | "not supported in storage class with explicit layout " |
930 | 0 | << StorageClassToString(storage_class); |
931 | 0 | } |
932 | 0 | } else { |
933 | 0 | if ((storage_class != spv::StorageClass::Function && |
934 | 0 | storage_class != spv::StorageClass::Private) && |
935 | 0 | _.ContainsType(pointee.id(), [](const Instruction* type_inst) { |
936 | 0 | auto opcode = type_inst->opcode(); |
937 | 0 | return opcode == spv::Op::OpTypeVectorIdEXT; |
938 | 0 | })) { |
939 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
940 | 0 | << "Cooperative vector types (or types containing them) can " |
941 | 0 | "only be " |
942 | 0 | "allocated " |
943 | 0 | << "in Function or Private storage classes or as function " |
944 | 0 | "parameters"; |
945 | 0 | } |
946 | 0 | } |
947 | | |
948 | 0 | return SPV_SUCCESS; |
949 | 0 | } |
950 | | |
951 | | spv_result_t ValidateVariableShader(ValidationState_t& _, |
952 | | const Instruction* inst, |
953 | | spv::StorageClass storage_class, |
954 | | const Instruction* value_type, |
955 | 199k | uint32_t value_id) { |
956 | | // Don't allow variables containing 16-bit elements without the appropriate |
957 | | // capabilities. |
958 | 199k | if ((!_.HasCapability(spv::Capability::Int16) && |
959 | 198k | _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 16)) || |
960 | 199k | (!_.HasCapability(spv::Capability::Float16) && |
961 | 199k | _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeFloat, 16))) { |
962 | 7 | auto underlying_type = value_type; |
963 | 7 | while (underlying_type && |
964 | 7 | underlying_type->opcode() == spv::Op::OpTypePointer) { |
965 | 0 | storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u); |
966 | 0 | underlying_type = _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u)); |
967 | 0 | } |
968 | 7 | bool storage_class_ok = true; |
969 | 7 | std::string sc_name = _.grammar().lookupOperandName( |
970 | 7 | SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class)); |
971 | 7 | switch (storage_class) { |
972 | 0 | case spv::StorageClass::StorageBuffer: |
973 | 0 | case spv::StorageClass::PhysicalStorageBuffer: |
974 | 0 | if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess)) { |
975 | 0 | storage_class_ok = false; |
976 | 0 | } |
977 | 0 | break; |
978 | 0 | case spv::StorageClass::Uniform: |
979 | 0 | if (underlying_type && |
980 | 0 | !_.HasCapability( |
981 | 0 | spv::Capability::UniformAndStorageBuffer16BitAccess)) { |
982 | 0 | if (underlying_type->opcode() == spv::Op::OpTypeArray || |
983 | 0 | underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) { |
984 | 0 | underlying_type = |
985 | 0 | _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u)); |
986 | 0 | } |
987 | 0 | if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess) || |
988 | 0 | !_.HasDecoration(underlying_type->id(), |
989 | 0 | spv::Decoration::BufferBlock)) { |
990 | 0 | storage_class_ok = false; |
991 | 0 | } |
992 | 0 | } |
993 | 0 | break; |
994 | 2 | case spv::StorageClass::PushConstant: |
995 | 2 | if (!_.HasCapability(spv::Capability::StoragePushConstant16)) { |
996 | 2 | storage_class_ok = false; |
997 | 2 | } |
998 | 2 | break; |
999 | 0 | case spv::StorageClass::Input: |
1000 | 1 | case spv::StorageClass::Output: |
1001 | 1 | if (!_.HasCapability(spv::Capability::StorageInputOutput16)) { |
1002 | 1 | storage_class_ok = false; |
1003 | 1 | } |
1004 | 1 | break; |
1005 | 2 | case spv::StorageClass::Workgroup: |
1006 | 2 | if (!_.HasCapability( |
1007 | 2 | spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR)) { |
1008 | 2 | storage_class_ok = false; |
1009 | 2 | } |
1010 | 2 | break; |
1011 | 2 | default: |
1012 | 2 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1013 | 2 | << "Cannot allocate a variable containing a 16-bit type in " |
1014 | 2 | << sc_name << " storage class"; |
1015 | 7 | } |
1016 | 5 | if (!storage_class_ok) { |
1017 | 5 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1018 | 5 | << "Allocating a variable containing a 16-bit element in " |
1019 | 5 | << sc_name << " storage class requires an additional capability"; |
1020 | 5 | } |
1021 | 5 | } |
1022 | | // Don't allow variables containing 8-bit elements without the appropriate |
1023 | | // capabilities. |
1024 | 199k | if (!_.HasCapability(spv::Capability::Int8) && |
1025 | 199k | _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 8)) { |
1026 | 0 | auto underlying_type = value_type; |
1027 | 0 | while (underlying_type && |
1028 | 0 | underlying_type->opcode() == spv::Op::OpTypePointer) { |
1029 | 0 | storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u); |
1030 | 0 | underlying_type = _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u)); |
1031 | 0 | } |
1032 | 0 | bool storage_class_ok = true; |
1033 | 0 | std::string sc_name = _.grammar().lookupOperandName( |
1034 | 0 | SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class)); |
1035 | 0 | switch (storage_class) { |
1036 | 0 | case spv::StorageClass::StorageBuffer: |
1037 | 0 | case spv::StorageClass::PhysicalStorageBuffer: |
1038 | 0 | if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess)) { |
1039 | 0 | storage_class_ok = false; |
1040 | 0 | } |
1041 | 0 | break; |
1042 | 0 | case spv::StorageClass::Uniform: |
1043 | 0 | if (underlying_type && |
1044 | 0 | !_.HasCapability( |
1045 | 0 | spv::Capability::UniformAndStorageBuffer8BitAccess)) { |
1046 | 0 | if (underlying_type->opcode() == spv::Op::OpTypeArray || |
1047 | 0 | underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) { |
1048 | 0 | underlying_type = |
1049 | 0 | _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u)); |
1050 | 0 | } |
1051 | 0 | if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess) || |
1052 | 0 | !_.HasDecoration(underlying_type->id(), |
1053 | 0 | spv::Decoration::BufferBlock)) { |
1054 | 0 | storage_class_ok = false; |
1055 | 0 | } |
1056 | 0 | } |
1057 | 0 | break; |
1058 | 0 | case spv::StorageClass::PushConstant: |
1059 | 0 | if (!_.HasCapability(spv::Capability::StoragePushConstant8)) { |
1060 | 0 | storage_class_ok = false; |
1061 | 0 | } |
1062 | 0 | break; |
1063 | 0 | case spv::StorageClass::Workgroup: |
1064 | 0 | if (!_.HasCapability( |
1065 | 0 | spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR)) { |
1066 | 0 | storage_class_ok = false; |
1067 | 0 | } |
1068 | 0 | break; |
1069 | 0 | default: |
1070 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1071 | 0 | << "Cannot allocate a variable containing a 8-bit type in " |
1072 | 0 | << sc_name << " storage class"; |
1073 | 0 | } |
1074 | 0 | if (!storage_class_ok) { |
1075 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1076 | 0 | << "Allocating a variable containing a 8-bit element in " |
1077 | 0 | << sc_name << " storage class requires an additional capability"; |
1078 | 0 | } |
1079 | 0 | } |
1080 | | |
1081 | 199k | if (_.ContainsOCPMicroscalingNonByteType(value_id)) { |
1082 | 0 | auto underlying_type = value_type; |
1083 | 0 | auto sc = storage_class; |
1084 | 0 | while (underlying_type && |
1085 | 0 | underlying_type->opcode() == spv::Op::OpTypePointer) { |
1086 | 0 | sc = underlying_type->GetOperandAs<spv::StorageClass>(1u); |
1087 | 0 | underlying_type = _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u)); |
1088 | 0 | } |
1089 | 0 | if (sc != spv::StorageClass::Function && sc != spv::StorageClass::Private) { |
1090 | 0 | std::string sc_name = _.grammar().lookupOperandName( |
1091 | 0 | SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(sc)); |
1092 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1093 | 0 | << "Cannot allocate a variable containing a Float4EXT or " |
1094 | 0 | "Float6EXT type in " |
1095 | 0 | << sc_name << " storage class"; |
1096 | 0 | } |
1097 | 0 | } |
1098 | | |
1099 | 199k | return SPV_SUCCESS; |
1100 | 199k | } |
1101 | | |
1102 | | spv_result_t ValidateVariableTileShadingQCOM(ValidationState_t& _, |
1103 | 0 | const Instruction* inst) { |
1104 | 0 | auto result_type = _.FindDef(inst->type_id()); |
1105 | 0 | if (result_type->opcode() == spv::Op::OpTypePointer) { |
1106 | 0 | const auto pointee_type = _.FindDef(result_type->GetOperandAs<uint32_t>(2)); |
1107 | 0 | if (pointee_type && pointee_type->opcode() == spv::Op::OpTypeImage) { |
1108 | 0 | spv::Dim dim = static_cast<spv::Dim>(pointee_type->word(3)); |
1109 | 0 | if (dim != spv::Dim::Dim2D) { |
1110 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
1111 | 0 | << _.VkErrorID(10693) |
1112 | 0 | << "Any OpTypeImage variable in the TileAttachmentQCOM " |
1113 | 0 | "Storage Class must " |
1114 | 0 | "have 2D as its dimension"; |
1115 | 0 | } |
1116 | 0 | unsigned sampled = pointee_type->word(7); |
1117 | 0 | if (sampled != 1 && sampled != 2) { |
1118 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
1119 | 0 | << _.VkErrorID(10694) |
1120 | 0 | << "Any OpTypeImage variable in the TileAttachmentQCOM " |
1121 | 0 | "Storage Class must " |
1122 | 0 | "have 1 or 2 as Image 'Sampled' parameter"; |
1123 | 0 | } |
1124 | 0 | for (const auto& pair_o : inst->uses()) { |
1125 | 0 | const auto* use_inst_o = pair_o.first; |
1126 | 0 | if (use_inst_o->opcode() == spv::Op::OpLoad) { |
1127 | 0 | for (const auto& pair_i : use_inst_o->uses()) { |
1128 | 0 | const auto* use_inst_i = pair_i.first; |
1129 | 0 | switch (use_inst_i->opcode()) { |
1130 | 0 | case spv::Op::OpImageQueryFormat: |
1131 | 0 | case spv::Op::OpImageQueryOrder: |
1132 | 0 | case spv::Op::OpImageQuerySizeLod: |
1133 | 0 | case spv::Op::OpImageQuerySize: |
1134 | 0 | case spv::Op::OpImageQueryLod: |
1135 | 0 | case spv::Op::OpImageQueryLevels: |
1136 | 0 | case spv::Op::OpImageQuerySamples: |
1137 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
1138 | 0 | << _.VkErrorID(10697) |
1139 | 0 | << "Any variable in the TileAttachmentQCOM Storage " |
1140 | 0 | "Class must " |
1141 | 0 | "not be consumed by an OpImageQuery* instruction"; |
1142 | 0 | default: |
1143 | 0 | break; |
1144 | 0 | } |
1145 | 0 | } |
1146 | 0 | } |
1147 | 0 | } |
1148 | 0 | } |
1149 | 0 | } |
1150 | | |
1151 | 0 | if (!(_.HasDecoration(inst->id(), spv::Decoration::DescriptorSet) && |
1152 | 0 | _.HasDecoration(inst->id(), spv::Decoration::Binding))) { |
1153 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1154 | 0 | << _.VkErrorID(10695) |
1155 | 0 | << "Any variable in the TileAttachmentQCOM Storage Class must " |
1156 | 0 | "be decorated with DescriptorSet and Binding"; |
1157 | 0 | } |
1158 | 0 | if (_.HasDecoration(inst->id(), spv::Decoration::Component)) { |
1159 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1160 | 0 | << _.VkErrorID(10696) |
1161 | 0 | << "Any variable in the TileAttachmentQCOM Storage Class must " |
1162 | 0 | "not be decorated with Component decoration"; |
1163 | 0 | } |
1164 | | |
1165 | 0 | return SPV_SUCCESS; |
1166 | 0 | } |
1167 | | |
1168 | | spv_result_t ValidateVariableTileImageEXT(ValidationState_t& _, |
1169 | 0 | const Instruction* inst) { |
1170 | 0 | bool is_valid_decl = true; |
1171 | |
|
1172 | 0 | auto result_type = _.FindDef(inst->type_id()); |
1173 | 0 | if (result_type->opcode() == spv::Op::OpTypePointer) { |
1174 | 0 | auto pointee_type = _.FindDef(result_type->GetOperandAs<uint32_t>(2)); |
1175 | |
|
1176 | 0 | while (pointee_type && pointee_type->opcode() == spv::Op::OpTypeArray) { |
1177 | 0 | pointee_type = _.FindDef(pointee_type->GetOperandAs<uint32_t>(1)); |
1178 | 0 | } |
1179 | |
|
1180 | 0 | if (pointee_type && pointee_type->opcode() == spv::Op::OpTypeImage) { |
1181 | 0 | spv::Dim dim = static_cast<spv::Dim>(pointee_type->word(3)); |
1182 | 0 | if (dim != spv::Dim::TileImageDataEXT) { |
1183 | 0 | is_valid_decl = false; |
1184 | 0 | } |
1185 | 0 | } else { |
1186 | 0 | is_valid_decl = false; |
1187 | 0 | } |
1188 | 0 | } |
1189 | |
|
1190 | 0 | if (!is_valid_decl) { |
1191 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
1192 | 0 | << "The TileImageEXT Storage Class must only be used for declaring " |
1193 | 0 | "tile image variables"; |
1194 | 0 | } else { |
1195 | 0 | return SPV_SUCCESS; |
1196 | 0 | } |
1197 | 0 | } |
1198 | | |
1199 | 200k | spv_result_t ValidateVariable(ValidationState_t& _, const Instruction* inst) { |
1200 | 200k | const bool untyped_pointer = inst->opcode() == spv::Op::OpUntypedVariableKHR; |
1201 | | |
1202 | 200k | auto result_type = _.FindDef(inst->type_id()); |
1203 | 200k | if (untyped_pointer) { |
1204 | 0 | if (!result_type || |
1205 | 0 | result_type->opcode() != spv::Op::OpTypeUntypedPointerKHR) |
1206 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1207 | 0 | << "Result type must be an untyped pointer"; |
1208 | 200k | } else { |
1209 | 200k | if (!result_type || result_type->opcode() != spv::Op::OpTypePointer) { |
1210 | 25 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1211 | 25 | << "OpVariable Result Type <id> " << _.getIdName(inst->type_id()) |
1212 | 25 | << " is not a pointer type."; |
1213 | 25 | } |
1214 | 200k | } |
1215 | | |
1216 | 200k | const auto storage_class_index = 2u; |
1217 | 200k | auto storage_class = |
1218 | 200k | inst->GetOperandAs<spv::StorageClass>(storage_class_index); |
1219 | 200k | uint32_t value_id = 0; |
1220 | 200k | if (untyped_pointer) { |
1221 | 0 | const bool has_data_type = 3u < inst->operands().size(); |
1222 | 0 | if (has_data_type) { |
1223 | 0 | value_id = inst->GetOperandAs<uint32_t>(3u); |
1224 | 0 | auto data_type = _.FindDef(value_id); |
1225 | 0 | if (!data_type || !spvOpcodeGeneratesType(data_type->opcode())) { |
1226 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1227 | 0 | << "Data type must be a type instruction"; |
1228 | 0 | } |
1229 | 0 | } else { |
1230 | 0 | if (storage_class == spv::StorageClass::Function || |
1231 | 0 | storage_class == spv::StorageClass::Private || |
1232 | 0 | storage_class == spv::StorageClass::Workgroup) { |
1233 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1234 | 0 | << "Data type must be specified for Function, Private, and " |
1235 | 0 | "Workgroup storage classes"; |
1236 | 0 | } |
1237 | | // Added from SPV_EXT_descriptor_heap |
1238 | | // Vulkan allows untyped pointer without |Data Type| but only for heap |
1239 | | // decorated variable that are in UniformConstant |
1240 | 0 | if (spvIsVulkanEnv(_.context()->target_env)) { |
1241 | 0 | if (storage_class != spv::StorageClass::UniformConstant) { |
1242 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1243 | 0 | << _.VkErrorID(11167) << "Storage class is " |
1244 | 0 | << StorageClassToString(storage_class) |
1245 | 0 | << ", but Vulkan requires that Data Type be specified when " |
1246 | 0 | "not using UniformConstant storage class"; |
1247 | 0 | } else if (!(_.IsDescriptorHeapBaseVariable(inst))) { |
1248 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1249 | 0 | << _.VkErrorID(11347) |
1250 | 0 | << "Storage class is UniformConstant, but Vulkan requires " |
1251 | 0 | "that Data Type be specified if the variable is not " |
1252 | 0 | "decorated with SamplerHeapEXT or ResourceHeapEXT"; |
1253 | 0 | } |
1254 | 0 | } |
1255 | 0 | } |
1256 | 0 | } |
1257 | | |
1258 | | // For OpVariable the data type comes from pointee type of the result type, |
1259 | | // while for OpUntypedVariableKHR the data type comes from the operand. |
1260 | 200k | if (!untyped_pointer) { |
1261 | 200k | value_id = result_type->GetOperandAs<uint32_t>(2); |
1262 | 200k | } |
1263 | 200k | const Instruction* value_type = value_id == 0 ? nullptr : _.FindDef(value_id); |
1264 | | |
1265 | 200k | if (auto error = |
1266 | 200k | ValidateVariableInitializer(_, inst, storage_class, value_id)) |
1267 | 15 | return error; |
1268 | | |
1269 | 200k | if (auto error = |
1270 | 200k | ValidateVariableStorageClass(_, inst, storage_class, value_type)) |
1271 | 178 | return error; |
1272 | | |
1273 | | // Variable pointer related restrictions. |
1274 | 199k | const Instruction* pointee = |
1275 | 199k | untyped_pointer ? value_id == 0 ? nullptr : _.FindDef(value_id) |
1276 | 199k | : _.FindDef(result_type->word(3)); |
1277 | | |
1278 | 199k | if (pointee) { |
1279 | 199k | if (auto error = ValidateVariablePointer(_, inst, storage_class, *pointee)) |
1280 | 23 | return error; |
1281 | 199k | if (auto error = ValidateVariableCoopMat(_, inst, storage_class, *pointee)) |
1282 | 0 | return error; |
1283 | 199k | } |
1284 | | |
1285 | 199k | if (spvIsVulkanEnv(_.context()->target_env)) { |
1286 | 0 | if (pointee) { |
1287 | 0 | if (auto error = ValidateVariableVulkanDescriptor(_, inst, storage_class, |
1288 | 0 | *pointee)) |
1289 | 0 | return error; |
1290 | 0 | if (auto error = ValidateVariableVulkanLongVector(_, inst, storage_class, |
1291 | 0 | *pointee)) |
1292 | 0 | return error; |
1293 | 0 | } |
1294 | | |
1295 | 0 | if (auto error = ValidateVariableVulkanInterface(_, inst, storage_class, |
1296 | 0 | value_type, value_id)) |
1297 | 0 | return error; |
1298 | | |
1299 | 0 | if (value_type) { |
1300 | 0 | if (auto error = ValidateVariableVulkanArray(_, inst, storage_class, |
1301 | 0 | *value_type, value_id)) |
1302 | 0 | return error; |
1303 | 0 | } |
1304 | 0 | } |
1305 | | |
1306 | 199k | if (_.HasCapability(spv::Capability::Shader)) { |
1307 | 199k | if (auto error = ValidateVariableShader(_, inst, storage_class, value_type, |
1308 | 199k | value_id)) |
1309 | 7 | return error; |
1310 | 199k | } |
1311 | | |
1312 | 199k | if (_.HasCapability(spv::Capability::TileShadingQCOM) && |
1313 | 0 | storage_class == spv::StorageClass::TileAttachmentQCOM) { |
1314 | 0 | if (auto error = ValidateVariableTileShadingQCOM(_, inst)) return error; |
1315 | 0 | } |
1316 | | |
1317 | 199k | if (_.HasCapability(spv::Capability::TileImageColorReadAccessEXT) && |
1318 | 0 | storage_class == spv::StorageClass::TileImageEXT) { |
1319 | 0 | if (auto error = ValidateVariableTileImageEXT(_, inst)) return error; |
1320 | 0 | } |
1321 | | |
1322 | 199k | return SPV_SUCCESS; |
1323 | 199k | } |
1324 | | |
1325 | 320k | spv_result_t ValidateLoad(ValidationState_t& _, const Instruction* inst) { |
1326 | 320k | const auto result_type = _.FindDef(inst->type_id()); |
1327 | 320k | if (!result_type) { |
1328 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1329 | 0 | << "OpLoad Result Type <id> " << _.getIdName(inst->type_id()) |
1330 | 0 | << " is not defined."; |
1331 | 0 | } |
1332 | | |
1333 | 320k | const auto pointer_index = 2; |
1334 | 320k | const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index); |
1335 | 320k | const auto pointer = _.FindDef(pointer_id); |
1336 | 320k | if (!pointer || |
1337 | 320k | ((_.addressing_model() == spv::AddressingModel::Logical) && |
1338 | 320k | ((!_.features().variable_pointers && |
1339 | 320k | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
1340 | 320k | (_.features().variable_pointers && |
1341 | 20 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
1342 | 20 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1343 | 20 | << "OpLoad Pointer <id> " << _.getIdName(pointer_id) |
1344 | 20 | << " is not a logical pointer."; |
1345 | 20 | } |
1346 | | |
1347 | 320k | const auto pointer_type = _.FindDef(pointer->type_id()); |
1348 | 320k | if (!pointer_type || |
1349 | 320k | (pointer_type->opcode() != spv::Op::OpTypePointer && |
1350 | 6 | pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
1351 | 6 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1352 | 6 | << "OpLoad type for pointer <id> " << _.getIdName(pointer_id) |
1353 | 6 | << " is not a pointer type."; |
1354 | 6 | } |
1355 | | |
1356 | 320k | if (pointer_type->opcode() == spv::Op::OpTypePointer) { |
1357 | 320k | const auto pointee_type = |
1358 | 320k | _.FindDef(pointer_type->GetOperandAs<uint32_t>(2)); |
1359 | 320k | if (!pointee_type || result_type->id() != pointee_type->id()) { |
1360 | 35 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1361 | 35 | << "OpLoad Result Type <id> " << _.getIdName(inst->type_id()) |
1362 | 35 | << " does not match Pointer <id> " << _.getIdName(pointer->id()) |
1363 | 35 | << "s type."; |
1364 | 35 | } |
1365 | 320k | } |
1366 | | |
1367 | 320k | if (!_.options()->before_hlsl_legalization && |
1368 | 320k | _.ContainsRuntimeArray(inst->type_id())) { |
1369 | 3 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1370 | 3 | << "Cannot load a runtime-sized array"; |
1371 | 3 | } |
1372 | | |
1373 | 320k | if (auto error = CheckMemoryAccess(_, inst, 3)) return error; |
1374 | | |
1375 | 320k | if (_.HasCapability(spv::Capability::Shader) && |
1376 | 320k | _.ContainsLimitedUseIntOrFloatType(inst->type_id()) && |
1377 | 0 | result_type->opcode() != spv::Op::OpTypePointer) { |
1378 | 0 | if (result_type->opcode() != spv::Op::OpTypeInt && |
1379 | 0 | result_type->opcode() != spv::Op::OpTypeFloat && |
1380 | 0 | result_type->opcode() != spv::Op::OpTypeVector && |
1381 | 0 | result_type->opcode() != spv::Op::OpTypeMatrix) { |
1382 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1383 | 0 | << "8- or 16-bit loads must be a scalar, vector or matrix type"; |
1384 | 0 | } |
1385 | 0 | } |
1386 | | |
1387 | | // Skip checking if there is zero chance for this having a mesh shader |
1388 | | // entrypoint |
1389 | 320k | if (_.HasCapability(spv::Capability::MeshShadingEXT) && |
1390 | 0 | pointer_type->GetOperandAs<spv::StorageClass>(1) == |
1391 | 0 | spv::StorageClass::Output) { |
1392 | 0 | std::string errorVUID = _.VkErrorID(7107); |
1393 | 0 | _.function(inst->function()->id()) |
1394 | 0 | ->RegisterExecutionModelLimitation( |
1395 | 0 | [errorVUID](spv::ExecutionModel model, std::string* message) { |
1396 | | // Seems the NV Mesh extension was less strict and allowed |
1397 | | // writting to outputs |
1398 | 0 | if (model == spv::ExecutionModel::MeshEXT) { |
1399 | 0 | if (message) { |
1400 | 0 | *message = errorVUID + |
1401 | 0 | "The Output Storage Class in a Mesh Execution " |
1402 | 0 | "Model must not be read from"; |
1403 | 0 | } |
1404 | 0 | return false; |
1405 | 0 | } |
1406 | 0 | return true; |
1407 | 0 | }); |
1408 | 0 | } |
1409 | | |
1410 | 320k | _.RegisterQCOMImageProcessingTextureConsumer(pointer_id, inst, nullptr); |
1411 | | |
1412 | | // EXT_descriptor_heap |
1413 | 320k | if (spvIsVulkanEnv(_.context()->target_env) && |
1414 | 0 | (result_type->opcode() == spv::Op::OpTypeSampler || |
1415 | 0 | result_type->opcode() == spv::Op::OpTypeImage || |
1416 | 0 | result_type->opcode() == spv::Op::OpTypeAccelerationStructureKHR)) { |
1417 | 0 | if (_.IsDescriptorHeapBaseVariable(_.FindDef(pointer_id))) { |
1418 | 0 | if (auto descBaseVariable = |
1419 | 0 | _.FindUntypedBaseVariable(_.FindDef(pointer_id))) { |
1420 | 0 | auto descBaseVariableId = descBaseVariable->id(); |
1421 | 0 | if (!_.HasDecoration(descBaseVariableId, |
1422 | 0 | spv::Decoration::DescriptorSet) && |
1423 | 0 | !_.HasDecoration(descBaseVariableId, spv::Decoration::Binding)) { |
1424 | 0 | switch (result_type->opcode()) { |
1425 | 0 | case spv::Op::OpTypeSampler: |
1426 | 0 | if (!_.IsBuiltin(descBaseVariableId, |
1427 | 0 | spv::BuiltIn::SamplerHeapEXT)) { |
1428 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1429 | 0 | << _.VkErrorID(11336) |
1430 | 0 | << "OpTypeSampler pointer instruction has no descriptor " |
1431 | 0 | "set " |
1432 | 0 | << "or binding and is not derived from a variable " |
1433 | 0 | "decorated " |
1434 | 0 | "with " |
1435 | 0 | "SamplerHeapEXT"; |
1436 | 0 | } |
1437 | 0 | break; |
1438 | 0 | case spv::Op::OpTypeImage: |
1439 | 0 | if (!_.IsBuiltin(descBaseVariableId, |
1440 | 0 | spv::BuiltIn::ResourceHeapEXT)) { |
1441 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1442 | 0 | << _.VkErrorID(11337) |
1443 | 0 | << "OpTypeImage pointer instruction has no descriptor " |
1444 | 0 | "set " |
1445 | 0 | << "or binding and is not derived from a variable " |
1446 | 0 | "decorated " |
1447 | 0 | "with " |
1448 | 0 | "ResourceHeapEXT"; |
1449 | 0 | } |
1450 | 0 | break; |
1451 | 0 | case spv::Op::OpTypeAccelerationStructureKHR: |
1452 | 0 | uint32_t data_type; |
1453 | 0 | spv::StorageClass sc; |
1454 | 0 | if (_.GetPointerTypeInfo(descBaseVariable->type_id(), &data_type, |
1455 | 0 | &sc) && |
1456 | 0 | sc != spv::StorageClass::Private && |
1457 | 0 | sc != spv::StorageClass::Function && |
1458 | 0 | !_.IsBuiltin(descBaseVariableId, |
1459 | 0 | spv::BuiltIn::ResourceHeapEXT)) { |
1460 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1461 | 0 | << _.VkErrorID(11339) |
1462 | 0 | << "OpTypeAccelerationStructureKHR pointer instruction " |
1463 | 0 | "has " |
1464 | 0 | "no " |
1465 | 0 | << "descriptor set or binding and is not derived from a " |
1466 | 0 | "variable decorated with ResourceHeapEXT"; |
1467 | 0 | } |
1468 | 0 | break; |
1469 | 0 | default: |
1470 | 0 | break; |
1471 | 0 | } |
1472 | 0 | } |
1473 | 0 | } |
1474 | 0 | } |
1475 | 0 | } |
1476 | | |
1477 | 320k | return SPV_SUCCESS; |
1478 | 320k | } |
1479 | | |
1480 | 263k | spv_result_t ValidateStore(ValidationState_t& _, const Instruction* inst) { |
1481 | 263k | const auto pointer_index = 0; |
1482 | 263k | const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index); |
1483 | 263k | const auto pointer = _.FindDef(pointer_id); |
1484 | 263k | if (!pointer || |
1485 | 263k | (_.addressing_model() == spv::AddressingModel::Logical && |
1486 | 263k | ((!_.features().variable_pointers && |
1487 | 263k | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
1488 | 263k | (_.features().variable_pointers && |
1489 | 11 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
1490 | 11 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1491 | 11 | << "OpStore Pointer <id> " << _.getIdName(pointer_id) |
1492 | 11 | << " is not a logical pointer."; |
1493 | 11 | } |
1494 | 263k | const auto pointer_type = _.FindDef(pointer->type_id()); |
1495 | 263k | if (!pointer_type || |
1496 | 263k | (pointer_type->opcode() != spv::Op::OpTypePointer && |
1497 | 5 | pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
1498 | 5 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1499 | 5 | << "OpStore type for pointer <id> " << _.getIdName(pointer_id) |
1500 | 5 | << " is not a pointer type."; |
1501 | 5 | } |
1502 | | |
1503 | 263k | Instruction* type = nullptr; |
1504 | 263k | if (pointer_type->opcode() == spv::Op::OpTypePointer) { |
1505 | 263k | const auto type_id = pointer_type->GetOperandAs<uint32_t>(2); |
1506 | 263k | type = _.FindDef(type_id); |
1507 | 263k | if (!type || spv::Op::OpTypeVoid == type->opcode()) { |
1508 | 6 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1509 | 6 | << "OpStore Pointer <id> " << _.getIdName(pointer_id) |
1510 | 6 | << "s type is void."; |
1511 | 6 | } |
1512 | 263k | } |
1513 | | |
1514 | | // validate storage class |
1515 | 263k | { |
1516 | 263k | uint32_t data_type; |
1517 | 263k | spv::StorageClass storage_class; |
1518 | 263k | if (!_.GetPointerTypeInfo(pointer_type->id(), &data_type, &storage_class)) { |
1519 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1520 | 0 | << "OpStore Pointer <id> " << _.getIdName(pointer_id) |
1521 | 0 | << " is not pointer type"; |
1522 | 0 | } |
1523 | | |
1524 | 263k | if (storage_class == spv::StorageClass::UniformConstant || |
1525 | 263k | storage_class == spv::StorageClass::Input || |
1526 | 263k | storage_class == spv::StorageClass::PushConstant) { |
1527 | 13 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1528 | 13 | << "OpStore Pointer <id> " << _.getIdName(pointer_id) |
1529 | 13 | << " storage class is read-only"; |
1530 | 263k | } else if (storage_class == spv::StorageClass::ShaderRecordBufferKHR) { |
1531 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1532 | 0 | << "ShaderRecordBufferKHR Storage Class variables are read only"; |
1533 | 263k | } else if (storage_class == spv::StorageClass::HitAttributeKHR) { |
1534 | 0 | std::string errorVUID = _.VkErrorID(4703); |
1535 | 0 | _.function(inst->function()->id()) |
1536 | 0 | ->RegisterExecutionModelLimitation( |
1537 | 0 | [errorVUID](spv::ExecutionModel model, std::string* message) { |
1538 | 0 | if (model == spv::ExecutionModel::AnyHitKHR || |
1539 | 0 | model == spv::ExecutionModel::ClosestHitKHR) { |
1540 | 0 | if (message) { |
1541 | 0 | *message = |
1542 | 0 | errorVUID + |
1543 | 0 | "HitAttributeKHR Storage Class variables are read only " |
1544 | 0 | "with AnyHitKHR and ClosestHitKHR"; |
1545 | 0 | } |
1546 | 0 | return false; |
1547 | 0 | } |
1548 | 0 | return true; |
1549 | 0 | }); |
1550 | 0 | } |
1551 | | |
1552 | 263k | if (spvIsVulkanEnv(_.context()->target_env) && |
1553 | 0 | storage_class == spv::StorageClass::Uniform) { |
1554 | 0 | auto base_ptr = _.TracePointer(pointer); |
1555 | 0 | if (base_ptr->opcode() == spv::Op::OpVariable) { |
1556 | | // If it's not a variable a different check should catch the problem. |
1557 | 0 | auto base_type = _.FindDef(base_ptr->GetOperandAs<uint32_t>(0)); |
1558 | | // Get the pointed-to type. |
1559 | 0 | base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(2u)); |
1560 | 0 | if (base_type->opcode() == spv::Op::OpTypeArray || |
1561 | 0 | base_type->opcode() == spv::Op::OpTypeRuntimeArray) { |
1562 | 0 | base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(1u)); |
1563 | 0 | } |
1564 | 0 | if (_.HasDecoration(base_type->id(), spv::Decoration::Block)) { |
1565 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1566 | 0 | << _.VkErrorID(6925) |
1567 | 0 | << "In the Vulkan environment, cannot store to Uniform Blocks"; |
1568 | 0 | } |
1569 | 0 | } |
1570 | 0 | } |
1571 | 263k | } |
1572 | | |
1573 | 263k | const auto object_index = 1; |
1574 | 263k | const auto object_id = inst->GetOperandAs<uint32_t>(object_index); |
1575 | 263k | const auto object = _.FindDef(object_id); |
1576 | 263k | if (!object || !object->type_id()) { |
1577 | 5 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1578 | 5 | << "OpStore Object <id> " << _.getIdName(object_id) |
1579 | 5 | << " is not an object."; |
1580 | 5 | } |
1581 | 263k | const auto object_type = _.FindDef(object->type_id()); |
1582 | 263k | if (!object_type || spv::Op::OpTypeVoid == object_type->opcode()) { |
1583 | 5 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1584 | 5 | << "OpStore Object <id> " << _.getIdName(object_id) |
1585 | 5 | << "s type is void."; |
1586 | 5 | } |
1587 | | |
1588 | 263k | if (type && (type->id() != object_type->id())) { |
1589 | 37 | if (!_.options()->relax_struct_store || |
1590 | 0 | type->opcode() != spv::Op::OpTypeStruct || |
1591 | 37 | object_type->opcode() != spv::Op::OpTypeStruct) { |
1592 | 37 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1593 | 37 | << "OpStore Pointer <id> " << _.getIdName(pointer_id) |
1594 | 37 | << "s type does not match Object <id> " |
1595 | 37 | << _.getIdName(object->id()) << "s type."; |
1596 | 37 | } |
1597 | | |
1598 | | // TODO: Check for layout compatible matricies and arrays as well. |
1599 | 0 | if (!AreLayoutCompatibleStructs(_, type, object_type)) { |
1600 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1601 | 0 | << "OpStore Pointer <id> " << _.getIdName(pointer_id) |
1602 | 0 | << "s layout does not match Object <id> " |
1603 | 0 | << _.getIdName(object->id()) << "s layout."; |
1604 | 0 | } |
1605 | 0 | } |
1606 | | |
1607 | 263k | if (auto error = CheckMemoryAccess(_, inst, 2)) return error; |
1608 | | |
1609 | 263k | if (_.HasCapability(spv::Capability::Shader) && |
1610 | 263k | _.ContainsLimitedUseIntOrFloatType(inst->type_id()) && |
1611 | 0 | object_type->opcode() != spv::Op::OpTypePointer) { |
1612 | 0 | if (object_type->opcode() != spv::Op::OpTypeInt && |
1613 | 0 | object_type->opcode() != spv::Op::OpTypeFloat && |
1614 | 0 | object_type->opcode() != spv::Op::OpTypeVector && |
1615 | 0 | object_type->opcode() != spv::Op::OpTypeMatrix) { |
1616 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1617 | 0 | << "8- or 16-bit stores must be a scalar, vector or matrix type"; |
1618 | 0 | } |
1619 | 0 | } |
1620 | | |
1621 | 263k | if (spvIsVulkanEnv(_.context()->target_env) && |
1622 | 0 | !_.options()->before_hlsl_legalization) { |
1623 | 0 | const auto isForbiddenType = [](const Instruction* type_inst) { |
1624 | 0 | auto opcode = type_inst->opcode(); |
1625 | 0 | return opcode == spv::Op::OpTypeImage || |
1626 | 0 | opcode == spv::Op::OpTypeSampler || |
1627 | 0 | opcode == spv::Op::OpTypeSampledImage || |
1628 | 0 | opcode == spv::Op::OpTypeAccelerationStructureKHR; |
1629 | 0 | }; |
1630 | 0 | if (_.ContainsType(object_type->id(), isForbiddenType)) { |
1631 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1632 | 0 | << _.VkErrorID(6924) |
1633 | 0 | << "Cannot store to OpTypeImage, OpTypeSampler, " |
1634 | 0 | "OpTypeSampledImage, or OpTypeAccelerationStructureKHR objects"; |
1635 | 0 | } |
1636 | 0 | } |
1637 | | |
1638 | 263k | return SPV_SUCCESS; |
1639 | 263k | } |
1640 | | |
1641 | | spv_result_t ValidateCopyMemoryMemoryAccess(ValidationState_t& _, |
1642 | 47.6k | const Instruction* inst) { |
1643 | 47.6k | assert(inst->opcode() == spv::Op::OpCopyMemory || |
1644 | 47.6k | inst->opcode() == spv::Op::OpCopyMemorySized); |
1645 | 47.6k | const uint32_t first_access_index = |
1646 | 47.6k | inst->opcode() == spv::Op::OpCopyMemory ? 2 : 3; |
1647 | 47.6k | if (inst->operands().size() > first_access_index) { |
1648 | 43.9k | if (auto error = CheckMemoryAccess(_, inst, first_access_index)) |
1649 | 0 | return error; |
1650 | | |
1651 | 43.9k | const auto first_access = inst->GetOperandAs<uint32_t>(first_access_index); |
1652 | 43.9k | const uint32_t second_access_index = |
1653 | 43.9k | first_access_index + MemoryAccessNumWords(first_access); |
1654 | 43.9k | if (inst->operands().size() > second_access_index) { |
1655 | 3 | if (_.features().copy_memory_permits_two_memory_accesses) { |
1656 | 0 | if (auto error = CheckMemoryAccess(_, inst, second_access_index)) |
1657 | 0 | return error; |
1658 | | |
1659 | | // In the two-access form in SPIR-V 1.4 and later: |
1660 | | // - the first is the target (write) access and it can't have |
1661 | | // make-visible. |
1662 | | // - the second is the source (read) access and it can't have |
1663 | | // make-available. |
1664 | 0 | if (first_access & |
1665 | 0 | uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) { |
1666 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
1667 | 0 | << "Target memory access must not include " |
1668 | 0 | "MakePointerVisibleKHR"; |
1669 | 0 | } |
1670 | 0 | const auto second_access = |
1671 | 0 | inst->GetOperandAs<uint32_t>(second_access_index); |
1672 | 0 | if (second_access & |
1673 | 0 | uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) { |
1674 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
1675 | 0 | << "Source memory access must not include " |
1676 | 0 | "MakePointerAvailableKHR"; |
1677 | 0 | } |
1678 | 3 | } else { |
1679 | 3 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
1680 | 3 | << spvOpcodeString(inst->opcode()) |
1681 | 3 | << " with two memory access operands requires SPIR-V 1.4 or " |
1682 | 3 | "later"; |
1683 | 3 | } |
1684 | 3 | } |
1685 | 43.9k | } |
1686 | 47.6k | return SPV_SUCCESS; |
1687 | 47.6k | } |
1688 | | |
1689 | 47.7k | spv_result_t ValidateCopyMemory(ValidationState_t& _, const Instruction* inst) { |
1690 | 47.7k | const auto target_index = 0; |
1691 | 47.7k | const auto target_id = inst->GetOperandAs<uint32_t>(target_index); |
1692 | 47.7k | const auto target = _.FindDef(target_id); |
1693 | 47.7k | if (!target) { |
1694 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1695 | 0 | << "Target operand <id> " << _.getIdName(target_id) |
1696 | 0 | << " is not defined."; |
1697 | 0 | } |
1698 | | |
1699 | 47.7k | const auto source_index = 1; |
1700 | 47.7k | const auto source_id = inst->GetOperandAs<uint32_t>(source_index); |
1701 | 47.7k | const auto source = _.FindDef(source_id); |
1702 | 47.7k | if (!source) { |
1703 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1704 | 0 | << "Source operand <id> " << _.getIdName(source_id) |
1705 | 0 | << " is not defined."; |
1706 | 0 | } |
1707 | | |
1708 | 47.7k | const auto target_pointer_type = _.FindDef(target->type_id()); |
1709 | 47.7k | if (!target_pointer_type || |
1710 | 47.7k | (target_pointer_type->opcode() != spv::Op::OpTypePointer && |
1711 | 6 | target_pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
1712 | 6 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1713 | 6 | << "Target operand <id> " << _.getIdName(target_id) |
1714 | 6 | << " is not a pointer."; |
1715 | 6 | } |
1716 | | |
1717 | 47.7k | const auto source_pointer_type = _.FindDef(source->type_id()); |
1718 | 47.7k | if (!source_pointer_type || |
1719 | 47.7k | (source_pointer_type->opcode() != spv::Op::OpTypePointer && |
1720 | 14 | source_pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
1721 | 14 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1722 | 14 | << "Source operand <id> " << _.getIdName(source_id) |
1723 | 14 | << " is not a pointer."; |
1724 | 14 | } |
1725 | | |
1726 | 47.7k | if (inst->opcode() == spv::Op::OpCopyMemory) { |
1727 | 47.7k | const bool target_typed = |
1728 | 47.7k | target_pointer_type->opcode() == spv::Op::OpTypePointer; |
1729 | 47.7k | const bool source_typed = |
1730 | 47.7k | source_pointer_type->opcode() == spv::Op::OpTypePointer; |
1731 | 47.7k | Instruction* target_type = nullptr; |
1732 | 47.7k | Instruction* source_type = nullptr; |
1733 | 47.7k | if (target_typed) { |
1734 | 47.7k | target_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2)); |
1735 | | |
1736 | 47.7k | if (!target_type || target_type->opcode() == spv::Op::OpTypeVoid) { |
1737 | 4 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1738 | 4 | << "Target operand <id> " << _.getIdName(target_id) |
1739 | 4 | << " cannot be a void pointer."; |
1740 | 4 | } |
1741 | 47.7k | } |
1742 | | |
1743 | 47.7k | if (source_typed) { |
1744 | 47.7k | source_type = _.FindDef(source_pointer_type->GetOperandAs<uint32_t>(2)); |
1745 | 47.7k | if (!source_type || source_type->opcode() == spv::Op::OpTypeVoid) { |
1746 | 4 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1747 | 4 | << "Source operand <id> " << _.getIdName(source_id) |
1748 | 4 | << " cannot be a void pointer."; |
1749 | 4 | } |
1750 | 47.7k | } |
1751 | | |
1752 | 47.7k | if (target_type && source_type && target_type->id() != source_type->id()) { |
1753 | 5 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1754 | 5 | << "Target <id> " << _.getIdName(source_id) |
1755 | 5 | << "s type does not match Source <id> " |
1756 | 5 | << _.getIdName(source_type->id()) << "s type."; |
1757 | 5 | } |
1758 | | |
1759 | 47.6k | if (!target_type && !source_type) { |
1760 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1761 | 0 | << "One of Source or Target must be a typed pointer"; |
1762 | 0 | } |
1763 | | |
1764 | 47.6k | if (auto error = CheckMemoryAccess(_, inst, 2)) return error; |
1765 | 47.6k | } else { |
1766 | 0 | const auto size_id = inst->GetOperandAs<uint32_t>(2); |
1767 | 0 | const auto size = _.FindDef(size_id); |
1768 | 0 | if (!size) { |
1769 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1770 | 0 | << "Size operand <id> " << _.getIdName(size_id) |
1771 | 0 | << " is not defined."; |
1772 | 0 | } |
1773 | | |
1774 | 0 | const auto size_type = _.FindDef(size->type_id()); |
1775 | 0 | if (!_.IsIntScalarType(size_type->id())) { |
1776 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1777 | 0 | << "Size operand <id> " << _.getIdName(size_id) |
1778 | 0 | << " must be a scalar integer type."; |
1779 | 0 | } |
1780 | 0 | bool is_zero = true; |
1781 | 0 | switch (size->opcode()) { |
1782 | 0 | case spv::Op::OpConstantNull: |
1783 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1784 | 0 | << "Size operand <id> " << _.getIdName(size_id) |
1785 | 0 | << " cannot be a constant zero."; |
1786 | 0 | case spv::Op::OpConstant: |
1787 | 0 | if (size_type->word(3) == 1 && |
1788 | 0 | size->word(size->words().size() - 1) & 0x80000000) { |
1789 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1790 | 0 | << "Size operand <id> " << _.getIdName(size_id) |
1791 | 0 | << " cannot have the sign bit set to 1."; |
1792 | 0 | } |
1793 | 0 | for (size_t i = 3; is_zero && i < size->words().size(); ++i) { |
1794 | 0 | is_zero &= (size->word(i) == 0); |
1795 | 0 | } |
1796 | 0 | if (is_zero) { |
1797 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1798 | 0 | << "Size operand <id> " << _.getIdName(size_id) |
1799 | 0 | << " cannot be a constant zero."; |
1800 | 0 | } |
1801 | 0 | break; |
1802 | 0 | default: |
1803 | | // Cannot infer any other opcodes. |
1804 | 0 | break; |
1805 | 0 | } |
1806 | | |
1807 | 0 | if (_.HasCapability(spv::Capability::Shader)) { |
1808 | 0 | bool is_int = false; |
1809 | 0 | bool is_const = false; |
1810 | 0 | uint32_t value = 0; |
1811 | 0 | std::tie(is_int, is_const, value) = _.EvalInt32IfConst(size_id); |
1812 | 0 | if (is_const) { |
1813 | 0 | if (value % 4 != 0) { |
1814 | 0 | const auto source_sc = |
1815 | 0 | source_pointer_type->GetOperandAs<spv::StorageClass>(1); |
1816 | 0 | const auto target_sc = |
1817 | 0 | target_pointer_type->GetOperandAs<spv::StorageClass>(1); |
1818 | 0 | const bool int8 = _.HasCapability(spv::Capability::Int8); |
1819 | 0 | const bool ubo_int8 = _.HasCapability( |
1820 | 0 | spv::Capability::UniformAndStorageBuffer8BitAccess); |
1821 | 0 | const bool ssbo_int8 = |
1822 | 0 | _.HasCapability(spv::Capability::StorageBuffer8BitAccess) || |
1823 | 0 | ubo_int8; |
1824 | 0 | const bool pc_int8 = |
1825 | 0 | _.HasCapability(spv::Capability::StoragePushConstant8); |
1826 | 0 | const bool wg_int8 = _.HasCapability( |
1827 | 0 | spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR); |
1828 | 0 | const bool int16 = _.HasCapability(spv::Capability::Int16) || int8; |
1829 | 0 | const bool ubo_int16 = |
1830 | 0 | _.HasCapability( |
1831 | 0 | spv::Capability::UniformAndStorageBuffer16BitAccess) || |
1832 | 0 | ubo_int8; |
1833 | 0 | const bool ssbo_int16 = |
1834 | 0 | _.HasCapability(spv::Capability::StorageBuffer16BitAccess) || |
1835 | 0 | ubo_int16 || ssbo_int8; |
1836 | 0 | const bool pc_int16 = |
1837 | 0 | _.HasCapability(spv::Capability::StoragePushConstant16) || |
1838 | 0 | pc_int8; |
1839 | 0 | const bool io_int16 = |
1840 | 0 | _.HasCapability(spv::Capability::StorageInputOutput16); |
1841 | 0 | const bool wg_int16 = _.HasCapability( |
1842 | 0 | spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR); |
1843 | |
|
1844 | 0 | bool source_int16_match = false; |
1845 | 0 | bool target_int16_match = false; |
1846 | 0 | bool source_int8_match = false; |
1847 | 0 | bool target_int8_match = false; |
1848 | 0 | switch (source_sc) { |
1849 | 0 | case spv::StorageClass::StorageBuffer: |
1850 | 0 | source_int16_match = ssbo_int16; |
1851 | 0 | source_int8_match = ssbo_int8; |
1852 | 0 | break; |
1853 | 0 | case spv::StorageClass::Uniform: |
1854 | 0 | source_int16_match = ubo_int16; |
1855 | 0 | source_int8_match = ubo_int8; |
1856 | 0 | break; |
1857 | 0 | case spv::StorageClass::PushConstant: |
1858 | 0 | source_int16_match = pc_int16; |
1859 | 0 | source_int8_match = pc_int8; |
1860 | 0 | break; |
1861 | 0 | case spv::StorageClass::Input: |
1862 | 0 | case spv::StorageClass::Output: |
1863 | 0 | source_int16_match = io_int16; |
1864 | 0 | break; |
1865 | 0 | case spv::StorageClass::Workgroup: |
1866 | 0 | source_int16_match = wg_int16; |
1867 | 0 | source_int8_match = wg_int8; |
1868 | 0 | break; |
1869 | 0 | default: |
1870 | 0 | break; |
1871 | 0 | } |
1872 | 0 | switch (target_sc) { |
1873 | 0 | case spv::StorageClass::StorageBuffer: |
1874 | 0 | target_int16_match = ssbo_int16; |
1875 | 0 | target_int8_match = ssbo_int8; |
1876 | 0 | break; |
1877 | 0 | case spv::StorageClass::Uniform: |
1878 | 0 | target_int16_match = ubo_int16; |
1879 | 0 | target_int8_match = ubo_int8; |
1880 | 0 | break; |
1881 | 0 | case spv::StorageClass::PushConstant: |
1882 | 0 | target_int16_match = pc_int16; |
1883 | 0 | target_int8_match = pc_int8; |
1884 | 0 | break; |
1885 | | // Input is read-only so it cannot be the target pointer. |
1886 | 0 | case spv::StorageClass::Output: |
1887 | 0 | target_int16_match = io_int16; |
1888 | 0 | break; |
1889 | 0 | case spv::StorageClass::Workgroup: |
1890 | 0 | target_int16_match = wg_int16; |
1891 | 0 | target_int8_match = wg_int8; |
1892 | 0 | break; |
1893 | 0 | default: |
1894 | 0 | break; |
1895 | 0 | } |
1896 | 0 | if (!int8 && !int16 && !(source_int16_match && target_int16_match)) { |
1897 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1898 | 0 | << _.VkErrorID(11165) |
1899 | 0 | << "Size must be a multiple of 4. This is valid if Source (" |
1900 | 0 | << StorageClassToString(source_sc) << ") and Target (" |
1901 | 0 | << StorageClassToString(source_sc) |
1902 | 0 | << ") storage classes both support either 8-bit or 16-bit"; |
1903 | 0 | } |
1904 | 0 | if (value % 2 != 0) { |
1905 | 0 | if (!int8 && !(source_int8_match && target_int8_match)) { |
1906 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1907 | 0 | << _.VkErrorID(11165) |
1908 | 0 | << "Size must be a multiple of 2. This is valid if Source " |
1909 | 0 | "(" |
1910 | 0 | << StorageClassToString(source_sc) << ") and Target (" |
1911 | 0 | << StorageClassToString(source_sc) |
1912 | 0 | << ") storage classes both support 8-bit"; |
1913 | 0 | } |
1914 | 0 | } |
1915 | 0 | } |
1916 | 0 | } |
1917 | 0 | } |
1918 | | |
1919 | 0 | if (auto error = CheckMemoryAccess(_, inst, 3)) return error; |
1920 | 0 | } |
1921 | 47.6k | if (auto error = ValidateCopyMemoryMemoryAccess(_, inst)) return error; |
1922 | | |
1923 | | // Get past the pointers to avoid checking a pointer copy. |
1924 | 47.6k | if (target_pointer_type->opcode() == spv::Op::OpTypePointer) { |
1925 | 47.6k | auto sub_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2)); |
1926 | 47.6k | while (sub_type->opcode() == spv::Op::OpTypePointer) { |
1927 | 0 | sub_type = _.FindDef(sub_type->GetOperandAs<uint32_t>(2)); |
1928 | 0 | } |
1929 | 47.6k | if (_.HasCapability(spv::Capability::Shader) && |
1930 | 47.6k | _.ContainsLimitedUseIntOrFloatType(sub_type->id())) { |
1931 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1932 | 0 | << "Cannot copy memory of objects containing 8- or 16-bit types"; |
1933 | 0 | } |
1934 | 47.6k | } |
1935 | | |
1936 | 47.6k | return SPV_SUCCESS; |
1937 | 47.6k | } |
1938 | | |
1939 | | spv_result_t ValidateAccessChain(ValidationState_t& _, |
1940 | 150k | const Instruction* inst) { |
1941 | 150k | const spv::Op opcode = inst->opcode(); |
1942 | 150k | const bool untyped_pointer = spvOpcodeGeneratesUntypedPointer(inst->opcode()); |
1943 | | |
1944 | | // The result type must be OpTypePointer for regular access chains and an |
1945 | | // OpTypeUntypedPointerKHR for untyped access chains. |
1946 | 150k | auto result_type = _.FindDef(inst->type_id()); |
1947 | 150k | if (untyped_pointer) { |
1948 | 0 | if (!result_type || |
1949 | 0 | spv::Op::OpTypeUntypedPointerKHR != result_type->opcode()) { |
1950 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1951 | 0 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
1952 | 0 | << _.getIdName(inst->id()) |
1953 | 0 | << " must be OpTypeUntypedPointerKHR. Found Op" |
1954 | 0 | << spvOpcodeString(result_type->opcode()) << "."; |
1955 | 0 | } |
1956 | 150k | } else { |
1957 | 150k | if (!result_type || spv::Op::OpTypePointer != result_type->opcode()) { |
1958 | 19 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1959 | 19 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
1960 | 19 | << _.getIdName(inst->id()) << " must be OpTypePointer. Found Op" |
1961 | 19 | << spvOpcodeString(result_type->opcode()) << "."; |
1962 | 19 | } |
1963 | 150k | } |
1964 | | |
1965 | 150k | if (untyped_pointer) { |
1966 | | // Base type must be a non-pointer type. |
1967 | 0 | const auto base_type = _.FindDef(inst->GetOperandAs<uint32_t>(2)); |
1968 | 0 | if (!base_type || !spvOpcodeGeneratesType(base_type->opcode()) || |
1969 | 0 | base_type->opcode() == spv::Op::OpTypePointer || |
1970 | 0 | base_type->opcode() == spv::Op::OpTypeUntypedPointerKHR) { |
1971 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
1972 | 0 | << "Base type must be a non-pointer type"; |
1973 | 0 | } |
1974 | | |
1975 | 0 | const auto ContainsBlock = [&_](const Instruction* type_inst) { |
1976 | 0 | if (type_inst->opcode() == spv::Op::OpTypeStruct) { |
1977 | 0 | if (_.HasDecoration(type_inst->id(), spv::Decoration::Block) || |
1978 | 0 | _.HasDecoration(type_inst->id(), spv::Decoration::BufferBlock)) { |
1979 | 0 | return true; |
1980 | 0 | } |
1981 | 0 | } |
1982 | 0 | return false; |
1983 | 0 | }; |
1984 | | |
1985 | | // Block (and BufferBlock) arrays cannot be reinterpreted via untyped access |
1986 | | // chains. |
1987 | 0 | const bool base_type_block_array = |
1988 | 0 | base_type->opcode() == spv::Op::OpTypeArray && |
1989 | 0 | _.ContainsType(base_type->id(), ContainsBlock, |
1990 | 0 | /* traverse_all_types = */ false); |
1991 | |
|
1992 | 0 | const auto base_index = untyped_pointer ? 3 : 2; |
1993 | 0 | const auto base_id = inst->GetOperandAs<uint32_t>(base_index); |
1994 | 0 | auto base = _.FindDef(base_id); |
1995 | | // Strictly speaking this misses trivial access chains and function |
1996 | | // parameter chasing, but that would be a significant complication in the |
1997 | | // traversal. |
1998 | 0 | while (base->opcode() == spv::Op::OpCopyObject) { |
1999 | 0 | base = _.FindDef(base->GetOperandAs<uint32_t>(2)); |
2000 | 0 | } |
2001 | 0 | const Instruction* base_data_type = nullptr; |
2002 | 0 | if (base->opcode() == spv::Op::OpVariable) { |
2003 | 0 | const auto ptr_type = _.FindDef(base->type_id()); |
2004 | 0 | base_data_type = _.FindDef(ptr_type->GetOperandAs<uint32_t>(2)); |
2005 | 0 | } else if (base->opcode() == spv::Op::OpUntypedVariableKHR) { |
2006 | 0 | if (base->operands().size() > 3) { |
2007 | 0 | base_data_type = _.FindDef(base->GetOperandAs<uint32_t>(3)); |
2008 | 0 | } |
2009 | 0 | } |
2010 | |
|
2011 | 0 | if (base_data_type) { |
2012 | 0 | const bool base_block_array = |
2013 | 0 | base_data_type->opcode() == spv::Op::OpTypeArray && |
2014 | 0 | _.ContainsType(base_data_type->id(), ContainsBlock, |
2015 | 0 | /* traverse_all_types = */ false); |
2016 | |
|
2017 | 0 | if (base_type_block_array != base_block_array) { |
2018 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2019 | 0 | << "Both Base Type and Base must be Block or BufferBlock arrays " |
2020 | 0 | "or neither can be"; |
2021 | 0 | } else if (base_type_block_array && base_block_array && |
2022 | 0 | base_type->id() != base_data_type->id()) { |
2023 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2024 | 0 | << "If Base or Base Type is a Block or BufferBlock array, the " |
2025 | 0 | "other must also be the same array"; |
2026 | 0 | } |
2027 | 0 | } |
2028 | 0 | } |
2029 | | |
2030 | | // Base must be a pointer, pointing to the base of a composite object. |
2031 | 150k | const auto base_index = untyped_pointer ? 3 : 2; |
2032 | 150k | const auto base_id = inst->GetOperandAs<uint32_t>(base_index); |
2033 | 150k | const auto base = _.FindDef(base_id); |
2034 | 150k | const auto base_type = _.FindDef(base->type_id()); |
2035 | 150k | if (!base_type || !(spv::Op::OpTypePointer == base_type->opcode() || |
2036 | 9 | (untyped_pointer && spv::Op::OpTypeUntypedPointerKHR == |
2037 | 13 | base_type->opcode()))) { |
2038 | 13 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2039 | 13 | << "The Base <id> " << _.getIdName(base_id) << " in Op" |
2040 | 13 | << spvOpcodeString(opcode) << " instruction must be a pointer."; |
2041 | 13 | } |
2042 | | |
2043 | | // The result pointer storage class and base pointer storage class must match. |
2044 | | // Word 2 of OpTypePointer is the Storage Class. |
2045 | 150k | auto result_type_storage_class = result_type->word(2); |
2046 | 150k | auto base_type_storage_class = base_type->word(2); |
2047 | 150k | if (result_type_storage_class != base_type_storage_class) { |
2048 | 12 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2049 | 12 | << "The result pointer storage class and base " |
2050 | 12 | "pointer storage class in Op" |
2051 | 12 | << spvOpcodeString(opcode) << " do not match."; |
2052 | 12 | } |
2053 | | |
2054 | | // The type pointed to by OpTypePointer (word 3) must be a composite type. |
2055 | 150k | auto type_pointee = untyped_pointer |
2056 | 150k | ? _.FindDef(inst->GetOperandAs<uint32_t>(2)) |
2057 | 150k | : _.FindDef(base_type->word(3)); |
2058 | | |
2059 | | // Check Universal Limit (SPIR-V Spec. Section 2.17). |
2060 | | // The number of indexes passed to OpAccessChain may not exceed 255 |
2061 | | // The instruction includes 4 words + N words (for N indexes) |
2062 | 150k | size_t num_indexes = inst->words().size() - 4; |
2063 | 150k | if (inst->opcode() == spv::Op::OpPtrAccessChain || |
2064 | 150k | inst->opcode() == spv::Op::OpInBoundsPtrAccessChain || |
2065 | 150k | inst->opcode() == spv::Op::OpUntypedPtrAccessChainKHR || |
2066 | 150k | inst->opcode() == spv::Op::OpUntypedInBoundsPtrAccessChainKHR) { |
2067 | | // In pointer access chains, the element operand is required, but not |
2068 | | // counted as an index. |
2069 | 14 | --num_indexes; |
2070 | 14 | } |
2071 | 150k | const size_t num_indexes_limit = |
2072 | 150k | _.options()->universal_limits_.max_access_chain_indexes; |
2073 | 150k | if (num_indexes > num_indexes_limit) { |
2074 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2075 | 0 | << "The number of indexes in Op" << spvOpcodeString(opcode) |
2076 | 0 | << " may not exceed " << num_indexes_limit << ". Found " |
2077 | 0 | << num_indexes << " indexes."; |
2078 | 0 | } |
2079 | | // Indexes walk the type hierarchy to the desired depth, potentially down to |
2080 | | // scalar granularity. The first index in Indexes will select the top-level |
2081 | | // member/element/component/element of the base composite. All composite |
2082 | | // constituents use zero-based numbering, as described by their OpType... |
2083 | | // instruction. The second index will apply similarly to that result, and so |
2084 | | // on. Once any non-composite type is reached, there must be no remaining |
2085 | | // (unused) indexes. |
2086 | 150k | auto starting_index = untyped_pointer ? 5 : 4; |
2087 | 150k | if (inst->opcode() == spv::Op::OpPtrAccessChain || |
2088 | 150k | inst->opcode() == spv::Op::OpInBoundsPtrAccessChain || |
2089 | 150k | inst->opcode() == spv::Op::OpUntypedPtrAccessChainKHR || |
2090 | 150k | inst->opcode() == spv::Op::OpUntypedInBoundsPtrAccessChainKHR) { |
2091 | 14 | ++starting_index; |
2092 | 14 | } |
2093 | 342k | for (size_t i = starting_index; i < inst->words().size(); ++i) { |
2094 | 191k | const uint32_t cur_word = inst->words()[i]; |
2095 | | // Earlier ID checks ensure that cur_word definition exists. |
2096 | 191k | auto cur_word_instr = _.FindDef(cur_word); |
2097 | | // The index must be a scalar integer type (See OpAccessChain in the Spec.) |
2098 | 191k | auto index_type = _.FindDef(cur_word_instr->type_id()); |
2099 | 191k | if (!index_type || spv::Op::OpTypeInt != index_type->opcode()) { |
2100 | 10 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2101 | 10 | << "Indexes passed to Op" << spvOpcodeString(opcode) |
2102 | 10 | << " must be of type integer."; |
2103 | 10 | } |
2104 | | |
2105 | | // Logical pointer restrictions: any constant index with a signed integer |
2106 | | // type must not have its sign bit set. |
2107 | 191k | if (!_.options()->relax_logical_pointer && |
2108 | 191k | (_.addressing_model() == spv::AddressingModel::Logical || |
2109 | 20 | _.addressing_model() == |
2110 | 20 | spv::AddressingModel::PhysicalStorageBuffer64) && |
2111 | 191k | result_type_storage_class != |
2112 | 191k | static_cast<uint32_t>(spv::StorageClass::PhysicalStorageBuffer)) { |
2113 | 191k | if (index_type->GetOperandAs<uint32_t>(2) == 1) { |
2114 | 128k | int64_t val = 0; |
2115 | 128k | if (_.EvalConstantValInt64(cur_word, &val)) { |
2116 | 75.1k | if (val < 0) { |
2117 | 59 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2118 | 59 | << "Index at word " << i << " may not have a negative value"; |
2119 | 59 | } |
2120 | 75.1k | } |
2121 | 128k | } |
2122 | 191k | } |
2123 | | |
2124 | 191k | switch (type_pointee->opcode()) { |
2125 | 5.33k | case spv::Op::OpTypeMatrix: |
2126 | 75.1k | case spv::Op::OpTypeVector: |
2127 | 75.1k | case spv::Op::OpTypeVectorIdEXT: |
2128 | 75.1k | case spv::Op::OpTypeCooperativeMatrixNV: |
2129 | 75.1k | case spv::Op::OpTypeCooperativeMatrixKHR: |
2130 | 138k | case spv::Op::OpTypeArray: |
2131 | 139k | case spv::Op::OpTypeRuntimeArray: |
2132 | 139k | case spv::Op::OpTypeNodePayloadArrayAMDX: { |
2133 | | // In OpTypeMatrix, OpTypeVector, spv::Op::OpTypeCooperativeMatrixNV, |
2134 | | // OpTypeVectorIdEXT, OpTypeArray, and OpTypeRuntimeArray, word |
2135 | | // 2 is the Element Type. |
2136 | 139k | type_pointee = _.FindDef(type_pointee->word(2)); |
2137 | 139k | break; |
2138 | 139k | } |
2139 | 52.8k | case spv::Op::OpTypeStruct: { |
2140 | | // In case of structures, there is an additional constraint on the |
2141 | | // index: the index must be an OpConstant. |
2142 | 52.8k | int64_t cur_index; |
2143 | 52.8k | if (!_.EvalConstantValInt64(cur_word, &cur_index)) { |
2144 | 5 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2145 | 5 | << "The <id> passed to Op" << spvOpcodeString(opcode) |
2146 | 5 | << " to index " << _.getIdName(cur_word) |
2147 | 5 | << " into a " |
2148 | 5 | "structure must be an OpConstant."; |
2149 | 5 | } |
2150 | | |
2151 | | // The index points to the struct member we want, therefore, the index |
2152 | | // should be less than the number of struct members. |
2153 | 52.8k | const int64_t num_struct_members = |
2154 | 52.8k | static_cast<int64_t>(type_pointee->words().size() - 2); |
2155 | 52.8k | if (cur_index >= num_struct_members || cur_index < 0) { |
2156 | 60 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2157 | 60 | << "Index " << _.getIdName(cur_word) << " is out of bounds: Op" |
2158 | 60 | << spvOpcodeString(opcode) << " cannot find index " |
2159 | 60 | << cur_index << " into the structure <id> " |
2160 | 60 | << _.getIdName(type_pointee->id()) << ". This structure has " |
2161 | 60 | << num_struct_members << " members. Largest valid index is " |
2162 | 60 | << num_struct_members - 1 << "."; |
2163 | 60 | } |
2164 | | // Struct members IDs start at word 2 of OpTypeStruct. |
2165 | 52.7k | const size_t word_index = static_cast<size_t>(cur_index) + 2; |
2166 | 52.7k | auto structMemberId = type_pointee->word(word_index); |
2167 | 52.7k | type_pointee = _.FindDef(structMemberId); |
2168 | 52.7k | break; |
2169 | 52.8k | } |
2170 | 11 | default: { |
2171 | | // Give an error. reached non-composite type while indexes still remain. |
2172 | 11 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2173 | 11 | << "Op" << spvOpcodeString(opcode) |
2174 | 11 | << " reached non-composite type while indexes " |
2175 | 11 | "still remain to be traversed."; |
2176 | 52.8k | } |
2177 | 191k | } |
2178 | 191k | } |
2179 | | |
2180 | 150k | if (!untyped_pointer) { |
2181 | | // Result type is a pointer. Find out what it's pointing to. |
2182 | | // This will be used to make sure the indexing results in the same type. |
2183 | | // OpTypePointer word 3 is the type being pointed to. |
2184 | 150k | const auto result_type_pointee = _.FindDef(result_type->word(3)); |
2185 | | // At this point, we have fully walked down from the base using the indeces. |
2186 | | // The type being pointed to should be the same as the result type. |
2187 | 150k | if (type_pointee->id() != result_type_pointee->id()) { |
2188 | 15 | bool same_type = result_type_pointee->opcode() == type_pointee->opcode(); |
2189 | 15 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2190 | 15 | << "Op" << spvOpcodeString(opcode) << " result type <id> " |
2191 | 15 | << _.getIdName(result_type_pointee->id()) << " (Op" |
2192 | 15 | << spvOpcodeString(result_type_pointee->opcode()) |
2193 | 15 | << ") does not match the type that results from indexing into the " |
2194 | 15 | "base " |
2195 | 15 | "<id> " |
2196 | 15 | << _.getIdName(type_pointee->id()) << " (Op" |
2197 | 15 | << spvOpcodeString(type_pointee->opcode()) << ")." |
2198 | 15 | << (same_type ? " (The types must be the exact same Id, so the " |
2199 | 1 | "two types referenced are slighlty different)" |
2200 | 15 | : ""); |
2201 | 15 | } |
2202 | 150k | } |
2203 | | |
2204 | 150k | return SPV_SUCCESS; |
2205 | 150k | } |
2206 | | |
2207 | | spv_result_t ValidateRawAccessChain(ValidationState_t& _, |
2208 | 0 | const Instruction* inst) { |
2209 | 0 | const spv::Op opcode = inst->opcode(); |
2210 | | // The result type must be OpTypePointer. |
2211 | 0 | const auto result_type = _.FindDef(inst->type_id()); |
2212 | 0 | if (spv::Op::OpTypePointer != result_type->opcode()) { |
2213 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2214 | 0 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
2215 | 0 | << _.getIdName(inst->id()) << " must be OpTypePointer. Found Op" |
2216 | 0 | << spvOpcodeString(result_type->opcode()) << '.'; |
2217 | 0 | } |
2218 | | |
2219 | | // The pointed storage class must be valid. |
2220 | 0 | const auto storage_class = result_type->GetOperandAs<spv::StorageClass>(1); |
2221 | 0 | if (storage_class != spv::StorageClass::StorageBuffer && |
2222 | 0 | storage_class != spv::StorageClass::PhysicalStorageBuffer && |
2223 | 0 | storage_class != spv::StorageClass::Uniform) { |
2224 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2225 | 0 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
2226 | 0 | << _.getIdName(inst->id()) |
2227 | 0 | << " must point to a storage class of " |
2228 | 0 | "StorageBuffer, PhysicalStorageBuffer, or Uniform."; |
2229 | 0 | } |
2230 | | |
2231 | | // The pointed type must not be one in the list below. |
2232 | 0 | const auto result_type_pointee = |
2233 | 0 | _.FindDef(result_type->GetOperandAs<uint32_t>(2)); |
2234 | 0 | if (result_type_pointee->opcode() == spv::Op::OpTypeArray || |
2235 | 0 | result_type_pointee->opcode() == spv::Op::OpTypeMatrix || |
2236 | 0 | result_type_pointee->opcode() == spv::Op::OpTypeStruct) { |
2237 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2238 | 0 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
2239 | 0 | << _.getIdName(inst->id()) |
2240 | 0 | << " must not point to " |
2241 | 0 | "OpTypeArray, OpTypeMatrix, or OpTypeStruct."; |
2242 | 0 | } |
2243 | | |
2244 | | // Validate Stride is a OpConstant. |
2245 | 0 | const auto stride = _.FindDef(inst->GetOperandAs<uint32_t>(3)); |
2246 | 0 | if (stride->opcode() != spv::Op::OpConstant) { |
2247 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2248 | 0 | << "The Stride of Op" << spvOpcodeString(opcode) << " <id> " |
2249 | 0 | << _.getIdName(inst->id()) << " must be OpConstant. Found Op" |
2250 | 0 | << spvOpcodeString(stride->opcode()) << '.'; |
2251 | 0 | } |
2252 | | // Stride type must be OpTypeInt |
2253 | 0 | const auto stride_type = _.FindDef(stride->type_id()); |
2254 | 0 | if (stride_type->opcode() != spv::Op::OpTypeInt) { |
2255 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2256 | 0 | << "The type of Stride of Op" << spvOpcodeString(opcode) << " <id> " |
2257 | 0 | << _.getIdName(inst->id()) << " must be OpTypeInt. Found Op" |
2258 | 0 | << spvOpcodeString(stride_type->opcode()) << '.'; |
2259 | 0 | } |
2260 | | |
2261 | | // Index and Offset type must be OpTypeInt with a width of 32 |
2262 | 0 | const auto ValidateType = [&](const char* name, |
2263 | 0 | int operandIndex) -> spv_result_t { |
2264 | 0 | const auto value = _.FindDef(inst->GetOperandAs<uint32_t>(operandIndex)); |
2265 | 0 | const auto value_type = _.FindDef(value->type_id()); |
2266 | 0 | if (value_type->opcode() != spv::Op::OpTypeInt) { |
2267 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2268 | 0 | << "The type of " << name << " of Op" << spvOpcodeString(opcode) |
2269 | 0 | << " <id> " << _.getIdName(inst->id()) |
2270 | 0 | << " must be OpTypeInt. Found Op" |
2271 | 0 | << spvOpcodeString(value_type->opcode()) << '.'; |
2272 | 0 | } |
2273 | 0 | const auto width = value_type->GetOperandAs<uint32_t>(1); |
2274 | 0 | if (width != 32) { |
2275 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2276 | 0 | << "The integer width of " << name << " of Op" |
2277 | 0 | << spvOpcodeString(opcode) << " <id> " << _.getIdName(inst->id()) |
2278 | 0 | << " must be 32. Found " << width << '.'; |
2279 | 0 | } |
2280 | 0 | return SPV_SUCCESS; |
2281 | 0 | }; |
2282 | 0 | spv_result_t result; |
2283 | 0 | result = ValidateType("Index", 4); |
2284 | 0 | if (result != SPV_SUCCESS) { |
2285 | 0 | return result; |
2286 | 0 | } |
2287 | 0 | result = ValidateType("Offset", 5); |
2288 | 0 | if (result != SPV_SUCCESS) { |
2289 | 0 | return result; |
2290 | 0 | } |
2291 | | |
2292 | 0 | uint32_t access_operands = 0; |
2293 | 0 | if (inst->operands().size() >= 7) { |
2294 | 0 | access_operands = inst->GetOperandAs<uint32_t>(6); |
2295 | 0 | } |
2296 | 0 | if (access_operands & |
2297 | 0 | uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerElementNV)) { |
2298 | 0 | uint64_t stride_value = 0; |
2299 | 0 | if (_.EvalConstantValUint64(stride->id(), &stride_value) && |
2300 | 0 | stride_value == 0) { |
2301 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2302 | 0 | << "Stride must not be zero when per-element robustness is used."; |
2303 | 0 | } |
2304 | 0 | } |
2305 | 0 | if (access_operands & |
2306 | 0 | uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerComponentNV) || |
2307 | 0 | access_operands & |
2308 | 0 | uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerElementNV)) { |
2309 | 0 | if (storage_class == spv::StorageClass::PhysicalStorageBuffer) { |
2310 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2311 | 0 | << "Storage class cannot be PhysicalStorageBuffer when " |
2312 | 0 | "raw access chain robustness is used."; |
2313 | 0 | } |
2314 | 0 | } |
2315 | 0 | if (access_operands & |
2316 | 0 | uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerComponentNV) && |
2317 | 0 | access_operands & |
2318 | 0 | uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerElementNV)) { |
2319 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2320 | 0 | << "Per-component robustness and per-element robustness are " |
2321 | 0 | "mutually exclusive."; |
2322 | 0 | } |
2323 | | |
2324 | 0 | return SPV_SUCCESS; |
2325 | 0 | } |
2326 | | |
2327 | | spv_result_t ValidatePtrAccessChain(ValidationState_t& _, |
2328 | 6 | const Instruction* inst) { |
2329 | | // Need to call first, will make sure Base is a valid ID |
2330 | 6 | if (auto error = ValidateAccessChain(_, inst)) return error; |
2331 | | |
2332 | 3 | const bool untyped_pointer = spvOpcodeGeneratesUntypedPointer(inst->opcode()); |
2333 | | |
2334 | 3 | const auto base_idx = untyped_pointer ? 3 : 2; |
2335 | 3 | const auto base = _.FindDef(inst->GetOperandAs<uint32_t>(base_idx)); |
2336 | 3 | const auto base_type = _.FindDef(base->type_id()); |
2337 | 3 | const auto base_type_storage_class = |
2338 | 3 | base_type->GetOperandAs<spv::StorageClass>(1); |
2339 | | |
2340 | 3 | const auto element_idx = untyped_pointer ? 4 : 3; |
2341 | 3 | const auto element = _.FindDef(inst->GetOperandAs<uint32_t>(element_idx)); |
2342 | 3 | const auto element_type = _.FindDef(element->type_id()); |
2343 | 3 | if (!element_type || element_type->opcode() != spv::Op::OpTypeInt) { |
2344 | 3 | return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Element must be an integer"; |
2345 | 3 | } |
2346 | 0 | uint64_t element_val = 0; |
2347 | 0 | if (_.EvalConstantValUint64(element->id(), &element_val)) { |
2348 | 0 | if (element_val != 0) { |
2349 | 0 | const auto interp_type = |
2350 | 0 | untyped_pointer ? _.FindDef(inst->GetOperandAs<uint32_t>(2)) |
2351 | 0 | : _.FindDef(base_type->GetOperandAs<uint32_t>(2)); |
2352 | 0 | if (interp_type->opcode() == spv::Op::OpTypeStruct && |
2353 | 0 | (_.HasDecoration(interp_type->id(), spv::Decoration::Block) || |
2354 | 0 | _.HasDecoration(interp_type->id(), spv::Decoration::BufferBlock))) { |
2355 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2356 | 0 | << "Element must be 0 if the interpretation type is a Block- or " |
2357 | 0 | "BufferBlock-decorated structure"; |
2358 | 0 | } |
2359 | 0 | } |
2360 | 0 | } |
2361 | | |
2362 | 0 | if (_.HasCapability(spv::Capability::Shader) && |
2363 | 0 | (base_type_storage_class == spv::StorageClass::Uniform || |
2364 | 0 | base_type_storage_class == spv::StorageClass::StorageBuffer || |
2365 | 0 | base_type_storage_class == spv::StorageClass::PhysicalStorageBuffer || |
2366 | 0 | base_type_storage_class == spv::StorageClass::PushConstant || |
2367 | 0 | (_.HasCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR) && |
2368 | 0 | base_type_storage_class == spv::StorageClass::Workgroup)) && |
2369 | 0 | (!_.HasDecoration(base_type->id(), spv::Decoration::ArrayStride) && |
2370 | 0 | !_.HasDecoration(base_type->id(), spv::Decoration::ArrayStrideIdEXT))) { |
2371 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2372 | 0 | << "OpPtrAccessChain must have a Base whose type is decorated " |
2373 | 0 | "with ArrayStride or ArrayStrideIdEXT"; |
2374 | 0 | } |
2375 | | |
2376 | 0 | if (spvIsVulkanEnv(_.context()->target_env)) { |
2377 | 0 | const auto untyped_cap = |
2378 | 0 | untyped_pointer && _.HasCapability(spv::Capability::UntypedPointersKHR); |
2379 | 0 | if (base_type_storage_class == spv::StorageClass::Workgroup) { |
2380 | 0 | if (!_.HasCapability(spv::Capability::VariablePointers) && !untyped_cap) { |
2381 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2382 | 0 | << _.VkErrorID(7651) |
2383 | 0 | << "OpPtrAccessChain Base operand pointing to Workgroup " |
2384 | 0 | "storage class must use VariablePointers capability"; |
2385 | 0 | } |
2386 | 0 | } else if (base_type_storage_class == spv::StorageClass::StorageBuffer) { |
2387 | 0 | if (!_.features().variable_pointers && !untyped_cap) { |
2388 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2389 | 0 | << _.VkErrorID(7652) |
2390 | 0 | << "OpPtrAccessChain Base operand pointing to StorageBuffer " |
2391 | 0 | "storage class must use VariablePointers or " |
2392 | 0 | "VariablePointersStorageBuffer capability"; |
2393 | 0 | } |
2394 | 0 | } else if (base_type_storage_class != |
2395 | 0 | spv::StorageClass::PhysicalStorageBuffer && |
2396 | 0 | !untyped_cap) { |
2397 | 0 | return _.diag(SPV_ERROR_INVALID_DATA, inst) |
2398 | 0 | << _.VkErrorID(7650) |
2399 | 0 | << "OpPtrAccessChain Base operand must point to Workgroup, " |
2400 | 0 | "StorageBuffer, or PhysicalStorageBuffer storage class"; |
2401 | 0 | } |
2402 | 0 | } |
2403 | | |
2404 | 0 | return SPV_SUCCESS; |
2405 | 0 | } |
2406 | | |
2407 | | spv_result_t ValidateArrayLength(ValidationState_t& state, |
2408 | 34 | const Instruction* inst) { |
2409 | 34 | const spv::Op opcode = inst->opcode(); |
2410 | | |
2411 | | // Result type must be a 32- or 64-bit unsigned int. |
2412 | | // 64-bit requires CapabilityShader64BitIndexingEXT or a pipeline/shader |
2413 | | // flag and is validated in VVL. |
2414 | 34 | const uint32_t result_type_id = inst->type_id(); |
2415 | 34 | if (!state.IsIntScalarTypeWithSignedness(result_type_id, 0)) { |
2416 | 20 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2417 | 20 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
2418 | 20 | << state.getIdName(inst->id()) |
2419 | 20 | << " must be OpTypeInt with width 32 or 64 and signedness 0."; |
2420 | 20 | } |
2421 | 14 | const uint32_t result_type_width = state.GetBitWidth(inst->type_id()); |
2422 | 14 | if (result_type_width != 32 && result_type_width != 64) { |
2423 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2424 | 0 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
2425 | 0 | << state.getIdName(inst->id()) |
2426 | 0 | << " must be OpTypeInt with width 32 or 64 and signedness 0."; |
2427 | 0 | } |
2428 | | |
2429 | 14 | const bool untyped = inst->opcode() == spv::Op::OpUntypedArrayLengthKHR; |
2430 | 14 | auto pointer_ty_id = state.GetOperandTypeId(inst, (untyped ? 3 : 2)); |
2431 | 14 | auto pointer_ty = state.FindDef(pointer_ty_id); |
2432 | 14 | if (untyped) { |
2433 | 0 | if (!pointer_ty || |
2434 | 0 | pointer_ty->opcode() != spv::Op::OpTypeUntypedPointerKHR) { |
2435 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2436 | 0 | << "Pointer must be an untyped pointer object"; |
2437 | 0 | } |
2438 | 14 | } else if (!pointer_ty || pointer_ty->opcode() != spv::Op::OpTypePointer) { |
2439 | 8 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2440 | 8 | << "The Structure's type in Op" << spvOpcodeString(opcode) |
2441 | 8 | << " <id> " << state.getIdName(inst->id()) |
2442 | 8 | << " must be a pointer to an OpTypeStruct."; |
2443 | 8 | } |
2444 | | |
2445 | 6 | Instruction* structure_type = nullptr; |
2446 | 6 | if (untyped) { |
2447 | 0 | structure_type = state.FindDef(inst->GetOperandAs<uint32_t>(2)); |
2448 | 6 | } else { |
2449 | 6 | structure_type = state.FindDef(pointer_ty->GetOperandAs<uint32_t>(2)); |
2450 | 6 | } |
2451 | | |
2452 | 6 | if (structure_type->opcode() != spv::Op::OpTypeStruct) { |
2453 | 4 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2454 | 4 | << "The Structure's type in Op" << spvOpcodeString(opcode) |
2455 | 4 | << " <id> " << state.getIdName(inst->id()) |
2456 | 4 | << " must be a pointer to an OpTypeStruct."; |
2457 | 4 | } |
2458 | | |
2459 | 2 | auto num_of_members = structure_type->operands().size() - 1; |
2460 | 2 | auto last_member = |
2461 | 2 | state.FindDef(structure_type->GetOperandAs<uint32_t>(num_of_members)); |
2462 | 2 | if (last_member->opcode() != spv::Op::OpTypeRuntimeArray) { |
2463 | 2 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2464 | 2 | << "The Structure's last member in Op" << spvOpcodeString(opcode) |
2465 | 2 | << " <id> " << state.getIdName(inst->id()) |
2466 | 2 | << " must be an OpTypeRuntimeArray."; |
2467 | 2 | } |
2468 | | |
2469 | | // The array member must the index of the last element (the run time |
2470 | | // array). |
2471 | 0 | const auto index = untyped ? 4 : 3; |
2472 | 0 | if (inst->GetOperandAs<uint32_t>(index) != num_of_members - 1) { |
2473 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2474 | 0 | << "The array member in Op" << spvOpcodeString(opcode) << " <id> " |
2475 | 0 | << state.getIdName(inst->id()) |
2476 | 0 | << " must be the last member of the struct."; |
2477 | 0 | } |
2478 | | |
2479 | 0 | if (spvIsVulkanEnv(state.context()->target_env)) { |
2480 | 0 | const auto storage_class = pointer_ty->GetOperandAs<spv::StorageClass>(1); |
2481 | 0 | if (storage_class == spv::StorageClass::Uniform && |
2482 | 0 | state.HasDecoration(structure_type->id(), spv::Decoration::Block)) { |
2483 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2484 | 0 | << state.VkErrorID(11805) << "Op" << spvOpcodeString(opcode) |
2485 | 0 | << " must not be used on the OpTypeRuntimeArray inside a Uniform " |
2486 | 0 | "block"; |
2487 | 0 | } |
2488 | 0 | } |
2489 | | |
2490 | 0 | return SPV_SUCCESS; |
2491 | 0 | } |
2492 | | |
2493 | | spv_result_t ValidateCooperativeMatrixLength(ValidationState_t& state, |
2494 | | const Instruction* inst, |
2495 | | bool is_khr, |
2496 | 8 | uint32_t operand_index = 2) { |
2497 | 8 | const spv::Op opcode = inst->opcode(); |
2498 | | // Result type must be a 32-bit unsigned int. |
2499 | 8 | const uint32_t result_type_id = inst->type_id(); |
2500 | 8 | if (!state.IsIntScalarTypeWithSignedness(result_type_id, 0) || |
2501 | 8 | state.GetBitWidth(inst->type_id()) != 32) { |
2502 | 8 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2503 | 8 | << "The Result Type of Op" << spvOpcodeString(opcode) << " <id> " |
2504 | 8 | << state.getIdName(inst->id()) |
2505 | 8 | << " must be OpTypeInt with width 32 and signedness 0."; |
2506 | 8 | } |
2507 | | |
2508 | 0 | auto type_id = inst->GetOperandAs<uint32_t>(operand_index); |
2509 | 0 | auto type = state.FindDef(type_id); |
2510 | 0 | if (is_khr && type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) { |
2511 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2512 | 0 | << "The type in Op" << spvOpcodeString(opcode) << " <id> " |
2513 | 0 | << state.getIdName(type_id) |
2514 | 0 | << " must be OpTypeCooperativeMatrixKHR."; |
2515 | 0 | } else if (!is_khr && type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) { |
2516 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2517 | 0 | << "The type in Op" << spvOpcodeString(opcode) << " <id> " |
2518 | 0 | << state.getIdName(type_id) << " must be OpTypeCooperativeMatrixNV."; |
2519 | 0 | } |
2520 | 0 | return SPV_SUCCESS; |
2521 | 0 | } |
2522 | | |
2523 | | spv_result_t ValidateCooperativeMatrixGetCoordinateEXT( |
2524 | 0 | ValidationState_t& state, const Instruction* inst) { |
2525 | 0 | std::string instr_name = "OpCooperativeMatrixGetCoordinateEXT"; |
2526 | | |
2527 | | // Result type must be a uvec2 |
2528 | 0 | if (!state.IsIntVectorType(inst->type_id(), 32, 2)) { |
2529 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2530 | 0 | << instr_name << " Result Type <id> " |
2531 | 0 | << state.getIdName(inst->type_id()) |
2532 | 0 | << " must be OpTypeVector with two 32-bit integer components."; |
2533 | 0 | } |
2534 | | |
2535 | | // Matrix operand must be a cooperative matrix |
2536 | 0 | auto matrix_type_id = |
2537 | 0 | state.FindDef(inst->GetOperandAs<uint32_t>(2))->type_id(); |
2538 | 0 | if (!state.IsCooperativeMatrixKHRType(matrix_type_id)) { |
2539 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2540 | 0 | << instr_name << " Matrix <id> " |
2541 | 0 | << state.getIdName(inst->GetOperandAs<uint32_t>(2)) |
2542 | 0 | << " must be OpTypeCooperativeMatrixKHR."; |
2543 | 0 | } |
2544 | | |
2545 | | // Index operand must be a 32-bit int. |
2546 | 0 | auto index_type_id = |
2547 | 0 | state.FindDef(inst->GetOperandAs<uint32_t>(3))->type_id(); |
2548 | 0 | if (!state.IsIntScalarType(index_type_id, 32)) { |
2549 | 0 | return state.diag(SPV_ERROR_INVALID_ID, inst) |
2550 | 0 | << instr_name << " Index <id> " |
2551 | 0 | << state.getIdName(inst->GetOperandAs<uint32_t>(3)) |
2552 | 0 | << " must be OpTypeInt with width 32."; |
2553 | 0 | } |
2554 | | |
2555 | 0 | return SPV_SUCCESS; |
2556 | 0 | } |
2557 | | |
2558 | | spv_result_t ValidateCooperativeMatrixLoadStoreNV(ValidationState_t& _, |
2559 | 0 | const Instruction* inst) { |
2560 | 0 | uint32_t type_id; |
2561 | 0 | const char* opname; |
2562 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) { |
2563 | 0 | type_id = inst->type_id(); |
2564 | 0 | opname = "spv::Op::OpCooperativeMatrixLoadNV"; |
2565 | 0 | } else { |
2566 | | // get Object operand's type |
2567 | 0 | type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id(); |
2568 | 0 | opname = "spv::Op::OpCooperativeMatrixStoreNV"; |
2569 | 0 | } |
2570 | |
|
2571 | 0 | auto matrix_type = _.FindDef(type_id); |
2572 | |
|
2573 | 0 | if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) { |
2574 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) { |
2575 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2576 | 0 | << "spv::Op::OpCooperativeMatrixLoadNV Result Type <id> " |
2577 | 0 | << _.getIdName(type_id) << " is not a cooperative matrix type."; |
2578 | 0 | } else { |
2579 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2580 | 0 | << "spv::Op::OpCooperativeMatrixStoreNV Object type <id> " |
2581 | 0 | << _.getIdName(type_id) << " is not a cooperative matrix type."; |
2582 | 0 | } |
2583 | 0 | } |
2584 | | |
2585 | 0 | const auto pointer_index = |
2586 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 2u : 0u; |
2587 | 0 | const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index); |
2588 | 0 | const auto pointer = _.FindDef(pointer_id); |
2589 | 0 | if (!pointer || |
2590 | 0 | ((_.addressing_model() == spv::AddressingModel::Logical) && |
2591 | 0 | ((!_.features().variable_pointers && |
2592 | 0 | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
2593 | 0 | (_.features().variable_pointers && |
2594 | 0 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
2595 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2596 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer_id) |
2597 | 0 | << " is not a logical pointer."; |
2598 | 0 | } |
2599 | | |
2600 | 0 | const auto pointer_type_id = pointer->type_id(); |
2601 | 0 | const auto pointer_type = _.FindDef(pointer_type_id); |
2602 | 0 | if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) { |
2603 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2604 | 0 | << opname << " type for pointer <id> " << _.getIdName(pointer_id) |
2605 | 0 | << " is not a pointer type."; |
2606 | 0 | } |
2607 | | |
2608 | 0 | const auto storage_class_index = 1u; |
2609 | 0 | const auto storage_class = |
2610 | 0 | pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index); |
2611 | |
|
2612 | 0 | if (storage_class != spv::StorageClass::Workgroup && |
2613 | 0 | storage_class != spv::StorageClass::StorageBuffer && |
2614 | 0 | storage_class != spv::StorageClass::PhysicalStorageBuffer) { |
2615 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2616 | 0 | << opname << " storage class for pointer type <id> " |
2617 | 0 | << _.getIdName(pointer_type_id) |
2618 | 0 | << " is not Workgroup or StorageBuffer."; |
2619 | 0 | } |
2620 | | |
2621 | 0 | const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2); |
2622 | 0 | const auto pointee_type = _.FindDef(pointee_id); |
2623 | 0 | if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) || |
2624 | 0 | _.IsFloatScalarOrVectorType(pointee_id))) { |
2625 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2626 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer->id()) |
2627 | 0 | << "s Type must be a scalar or vector type."; |
2628 | 0 | } |
2629 | | |
2630 | 0 | const auto stride_index = |
2631 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 3u : 2u; |
2632 | 0 | const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index); |
2633 | 0 | const auto stride = _.FindDef(stride_id); |
2634 | 0 | if (!stride || !_.IsIntScalarType(stride->type_id())) { |
2635 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2636 | 0 | << "Stride operand <id> " << _.getIdName(stride_id) |
2637 | 0 | << " must be a scalar integer type."; |
2638 | 0 | } |
2639 | | |
2640 | 0 | const auto colmajor_index = |
2641 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 4u : 3u; |
2642 | 0 | const auto colmajor_id = inst->GetOperandAs<uint32_t>(colmajor_index); |
2643 | 0 | const auto colmajor = _.FindDef(colmajor_id); |
2644 | 0 | if (!colmajor || !_.IsBoolScalarType(colmajor->type_id()) || |
2645 | 0 | !(spvOpcodeIsConstant(colmajor->opcode()) || |
2646 | 0 | spvOpcodeIsSpecConstant(colmajor->opcode()))) { |
2647 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2648 | 0 | << "Column Major operand <id> " << _.getIdName(colmajor_id) |
2649 | 0 | << " must be a boolean constant instruction."; |
2650 | 0 | } |
2651 | | |
2652 | 0 | const auto memory_access_index = |
2653 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 5u : 4u; |
2654 | 0 | if (inst->operands().size() > memory_access_index) { |
2655 | 0 | if (auto error = CheckMemoryAccess(_, inst, memory_access_index)) |
2656 | 0 | return error; |
2657 | 0 | } |
2658 | | |
2659 | 0 | return SPV_SUCCESS; |
2660 | 0 | } |
2661 | | |
2662 | | spv_result_t ValidateCooperativeMatrixLoadStoreKHR(ValidationState_t& _, |
2663 | 0 | const Instruction* inst) { |
2664 | 0 | uint32_t type_id; |
2665 | 0 | const char* opname; |
2666 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) { |
2667 | 0 | type_id = inst->type_id(); |
2668 | 0 | opname = "spv::Op::OpCooperativeMatrixLoadKHR"; |
2669 | 0 | } else { |
2670 | | // get Object operand's type |
2671 | 0 | type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id(); |
2672 | 0 | opname = "spv::Op::OpCooperativeMatrixStoreKHR"; |
2673 | 0 | } |
2674 | |
|
2675 | 0 | auto matrix_type = _.FindDef(type_id); |
2676 | |
|
2677 | 0 | if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) { |
2678 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) { |
2679 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2680 | 0 | << "spv::Op::OpCooperativeMatrixLoadKHR Result Type <id> " |
2681 | 0 | << _.getIdName(type_id) << " is not a cooperative matrix type."; |
2682 | 0 | } else { |
2683 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2684 | 0 | << "spv::Op::OpCooperativeMatrixStoreKHR Object type <id> " |
2685 | 0 | << _.getIdName(type_id) << " is not a cooperative matrix type."; |
2686 | 0 | } |
2687 | 0 | } |
2688 | | |
2689 | 0 | const auto pointer_index = |
2690 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 2u : 0u; |
2691 | 0 | const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index); |
2692 | 0 | const auto pointer = _.FindDef(pointer_id); |
2693 | 0 | if (!pointer || |
2694 | 0 | ((_.addressing_model() == spv::AddressingModel::Logical) && |
2695 | 0 | ((!_.features().variable_pointers && |
2696 | 0 | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
2697 | 0 | (_.features().variable_pointers && |
2698 | 0 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
2699 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2700 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer_id) |
2701 | 0 | << " is not a logical pointer."; |
2702 | 0 | } |
2703 | | |
2704 | 0 | const auto pointer_type_id = pointer->type_id(); |
2705 | 0 | const auto pointer_type = _.FindDef(pointer_type_id); |
2706 | 0 | if (!pointer_type || |
2707 | 0 | !(pointer_type->opcode() == spv::Op::OpTypePointer || |
2708 | 0 | pointer_type->opcode() == spv::Op::OpTypeUntypedPointerKHR)) { |
2709 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2710 | 0 | << opname << " type for pointer <id> " << _.getIdName(pointer_id) |
2711 | 0 | << " is not a pointer type."; |
2712 | 0 | } |
2713 | | |
2714 | 0 | const bool untyped = |
2715 | 0 | pointer_type->opcode() == spv::Op::OpTypeUntypedPointerKHR; |
2716 | 0 | const auto storage_class_index = 1u; |
2717 | 0 | const auto storage_class = |
2718 | 0 | pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index); |
2719 | |
|
2720 | 0 | if (spvIsVulkanEnv(_.context()->target_env)) { |
2721 | 0 | if (storage_class != spv::StorageClass::Workgroup && |
2722 | 0 | storage_class != spv::StorageClass::StorageBuffer && |
2723 | 0 | storage_class != spv::StorageClass::PhysicalStorageBuffer) { |
2724 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2725 | 0 | << _.VkErrorID(8973) << opname |
2726 | 0 | << " storage class for pointer type <id> " |
2727 | 0 | << _.getIdName(pointer_type_id) |
2728 | 0 | << " is not Workgroup, StorageBuffer, or PhysicalStorageBuffer."; |
2729 | 0 | } |
2730 | 0 | } |
2731 | | |
2732 | 0 | if (!untyped) { |
2733 | 0 | const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2); |
2734 | 0 | const auto pointee_type = _.FindDef(pointee_id); |
2735 | 0 | if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) || |
2736 | 0 | _.IsFloatScalarOrVectorType(pointee_id))) { |
2737 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2738 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer->id()) |
2739 | 0 | << "s Type must be a scalar or vector type."; |
2740 | 0 | } |
2741 | 0 | } |
2742 | | |
2743 | 0 | const auto layout_index = |
2744 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 3u : 2u; |
2745 | 0 | const auto layout_id = inst->GetOperandAs<uint32_t>(layout_index); |
2746 | 0 | const auto layout_inst = _.FindDef(layout_id); |
2747 | 0 | if (!layout_inst || !_.IsIntScalarType(layout_inst->type_id()) || |
2748 | 0 | !spvOpcodeIsConstant(layout_inst->opcode())) { |
2749 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2750 | 0 | << "MemoryLayout operand <id> " << _.getIdName(layout_id) |
2751 | 0 | << " must be a 32-bit integer constant instruction."; |
2752 | 0 | } |
2753 | | |
2754 | 0 | bool stride_required = false; |
2755 | 0 | bool layout_requires_constant_stride = false; |
2756 | 0 | uint64_t layout; |
2757 | 0 | if (_.EvalConstantValUint64(layout_id, &layout)) { |
2758 | 0 | const bool is_arm_layout = |
2759 | 0 | (layout == |
2760 | 0 | (uint64_t)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM) || |
2761 | 0 | (layout == |
2762 | 0 | (uint64_t)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM); |
2763 | |
|
2764 | 0 | if (is_arm_layout) { |
2765 | 0 | if (!_.HasCapability(spv::Capability::CooperativeMatrixLayoutsARM)) { |
2766 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2767 | 0 | << "Using the RowBlockedInterleavedARM or " |
2768 | 0 | "ColumnBlockedInterleavedARM MemoryLayout requires the " |
2769 | 0 | "CooperativeMatrixLayoutsARM capability be declared"; |
2770 | 0 | } |
2771 | 0 | } |
2772 | | |
2773 | 0 | stride_required = |
2774 | 0 | (layout == (uint64_t)spv::CooperativeMatrixLayout::RowMajorKHR) || |
2775 | 0 | (layout == (uint64_t)spv::CooperativeMatrixLayout::ColumnMajorKHR) || |
2776 | 0 | is_arm_layout; |
2777 | 0 | layout_requires_constant_stride = is_arm_layout; |
2778 | 0 | } |
2779 | | |
2780 | 0 | const auto stride_index = |
2781 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 4u : 3u; |
2782 | 0 | if (inst->operands().size() > stride_index) { |
2783 | 0 | const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index); |
2784 | 0 | const auto stride_inst = _.FindDef(stride_id); |
2785 | 0 | if (!stride_inst || !_.IsIntScalarType(stride_inst->type_id())) { |
2786 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2787 | 0 | << "Stride operand <id> " << _.getIdName(stride_id) |
2788 | 0 | << " must be a scalar integer type."; |
2789 | 0 | } |
2790 | | // Check SPV_ARM_cooperative_matrix_layouts constraints |
2791 | 0 | if (layout_requires_constant_stride && |
2792 | 0 | !spvOpcodeIsConstant(stride_inst->opcode())) { |
2793 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2794 | 0 | << "MemoryLayout " << layout |
2795 | 0 | << " requires Stride come from a constant instruction."; |
2796 | 0 | } |
2797 | 0 | if (layout_requires_constant_stride) { |
2798 | 0 | uint64_t stride; |
2799 | 0 | if (_.EvalConstantValUint64(stride_id, &stride)) { |
2800 | 0 | if ((layout == |
2801 | 0 | (uint64_t) |
2802 | 0 | spv::CooperativeMatrixLayout::RowBlockedInterleavedARM) || |
2803 | 0 | (layout == |
2804 | 0 | (uint64_t) |
2805 | 0 | spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM)) { |
2806 | 0 | if ((stride != 1) && (stride != 2) && (stride != 4)) { |
2807 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2808 | 0 | << "MemoryLayout " << layout |
2809 | 0 | << " requires Stride be 1, 2, or 4."; |
2810 | 0 | } |
2811 | 0 | } |
2812 | 0 | const uint32_t elty_id = matrix_type->GetOperandAs<uint32_t>(1); |
2813 | 0 | const uint32_t rows_id = matrix_type->GetOperandAs<uint32_t>(3); |
2814 | 0 | const uint32_t cols_id = matrix_type->GetOperandAs<uint32_t>(4); |
2815 | 0 | uint64_t rows = 0, cols = 0; |
2816 | 0 | _.EvalConstantValUint64(rows_id, &rows); |
2817 | 0 | _.EvalConstantValUint64(cols_id, &cols); |
2818 | 0 | uint32_t sizeof_component_in_bytes = _.GetBitWidth(elty_id) / 8; |
2819 | 0 | uint64_t rows_required_multiple = 4; |
2820 | 0 | uint64_t cols_required_multiple = 16 / sizeof_component_in_bytes; |
2821 | |
|
2822 | 0 | if (layout == |
2823 | 0 | (uint64_t)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM) { |
2824 | 0 | cols_required_multiple *= stride; |
2825 | 0 | } |
2826 | 0 | if (layout == |
2827 | 0 | (uint64_t) |
2828 | 0 | spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM) { |
2829 | 0 | rows_required_multiple *= stride; |
2830 | 0 | } |
2831 | 0 | if ((rows != 0) && (rows % rows_required_multiple != 0)) { |
2832 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2833 | 0 | << "MemoryLayout " << layout << " with a Stride of " << stride |
2834 | 0 | << " requires that the number of rows be a multiple of " |
2835 | 0 | << rows_required_multiple; |
2836 | 0 | } |
2837 | 0 | if ((cols != 0) && (cols % cols_required_multiple != 0)) { |
2838 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2839 | 0 | << "MemoryLayout " << layout << " with a Stride of " << stride |
2840 | 0 | << " requires that the number of columns be a multiple of " |
2841 | 0 | << cols_required_multiple; |
2842 | 0 | } |
2843 | 0 | } |
2844 | 0 | } |
2845 | 0 | } else if (stride_required) { |
2846 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2847 | 0 | << "MemoryLayout " << layout << " requires a Stride."; |
2848 | 0 | } |
2849 | | |
2850 | 0 | const auto memory_access_index = |
2851 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 5u : 4u; |
2852 | 0 | if (inst->operands().size() > memory_access_index) { |
2853 | 0 | if (auto error = CheckMemoryAccess(_, inst, memory_access_index)) |
2854 | 0 | return error; |
2855 | 0 | } |
2856 | | |
2857 | 0 | return SPV_SUCCESS; |
2858 | 0 | } |
2859 | | |
2860 | | spv_result_t ValidateBufferPointerEXT(ValidationState_t& _, |
2861 | 0 | const Instruction* inst) { |
2862 | 0 | const auto storage_class_ptr = _.FindDef(inst->type_id()); |
2863 | 0 | if (storage_class_ptr->opcode() != spv::Op::OpTypeUntypedPointerKHR && |
2864 | 0 | storage_class_ptr->opcode() != spv::Op::OpTypePointer) { |
2865 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2866 | 0 | << "OpBufferPointerEXT's Result Type should be " |
2867 | 0 | << "a pointer type."; |
2868 | 0 | } |
2869 | | |
2870 | 0 | auto sc = storage_class_ptr->GetOperandAs<spv::StorageClass>(1u); |
2871 | 0 | if (sc != spv::StorageClass::StorageBuffer && |
2872 | 0 | sc != spv::StorageClass::Uniform) { |
2873 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2874 | 0 | << "OpBufferPointerEXT Result Type must be a pointer type " |
2875 | 0 | << "with a Storage Class of Uniform or StorageBuffer."; |
2876 | 0 | } |
2877 | | |
2878 | | // Buffer operand |
2879 | 0 | auto buffer = |
2880 | 0 | _.FindUntypedBaseVariable(_.FindDef(inst->GetOperandAs<uint32_t>(2))); |
2881 | 0 | if (!buffer || !_.IsBuiltin(buffer->id(), spv::BuiltIn::ResourceHeapEXT)) { |
2882 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2883 | 0 | << "OpBufferPointerEXT's buffer must be an untyped pointer" |
2884 | 0 | << " into a variable declared with the ResourceHeapEXT built-in"; |
2885 | 0 | } |
2886 | 0 | return SPV_SUCCESS; |
2887 | 0 | } |
2888 | | |
2889 | | // Returns the number of instruction words taken up by a tensor addressing |
2890 | | // operands argument and its implied operands. |
2891 | 0 | int TensorAddressingOperandsNumWords(spv::TensorAddressingOperandsMask mask) { |
2892 | 0 | int result = 1; // Count the mask |
2893 | 0 | if ((mask & spv::TensorAddressingOperandsMask::TensorView) != |
2894 | 0 | spv::TensorAddressingOperandsMask::MaskNone) |
2895 | 0 | ++result; |
2896 | 0 | if ((mask & spv::TensorAddressingOperandsMask::DecodeFunc) != |
2897 | 0 | spv::TensorAddressingOperandsMask::MaskNone) |
2898 | 0 | ++result; |
2899 | 0 | if ((mask & spv::TensorAddressingOperandsMask::DecodeVectorFunc) != |
2900 | 0 | spv::TensorAddressingOperandsMask::MaskNone) |
2901 | 0 | ++result; |
2902 | 0 | return result; |
2903 | 0 | } |
2904 | | |
2905 | | spv_result_t ValidateCooperativeMatrixLoadStoreTensorNV( |
2906 | 0 | ValidationState_t& _, const Instruction* inst) { |
2907 | 0 | uint32_t type_id; |
2908 | 0 | const char* opname; |
2909 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) { |
2910 | 0 | type_id = inst->type_id(); |
2911 | 0 | opname = "spv::Op::OpCooperativeMatrixLoadTensorNV"; |
2912 | 0 | } else { |
2913 | | // get Object operand's type |
2914 | 0 | type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id(); |
2915 | 0 | opname = "spv::Op::OpCooperativeMatrixStoreTensorNV"; |
2916 | 0 | } |
2917 | |
|
2918 | 0 | auto matrix_type = _.FindDef(type_id); |
2919 | |
|
2920 | 0 | if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) { |
2921 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) { |
2922 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2923 | 0 | << "spv::Op::OpCooperativeMatrixLoadTensorNV Result Type <id> " |
2924 | 0 | << _.getIdName(type_id) << " is not a cooperative matrix type."; |
2925 | 0 | } else { |
2926 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2927 | 0 | << "spv::Op::OpCooperativeMatrixStoreTensorNV Object type <id> " |
2928 | 0 | << _.getIdName(type_id) << " is not a cooperative matrix type."; |
2929 | 0 | } |
2930 | 0 | } |
2931 | | |
2932 | 0 | const auto pointer_index = |
2933 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) ? 2u : 0u; |
2934 | 0 | const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index); |
2935 | 0 | const auto pointer = _.FindDef(pointer_id); |
2936 | 0 | if (!pointer || |
2937 | 0 | ((_.addressing_model() == spv::AddressingModel::Logical) && |
2938 | 0 | ((!_.features().variable_pointers && |
2939 | 0 | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
2940 | 0 | (_.features().variable_pointers && |
2941 | 0 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
2942 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2943 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer_id) |
2944 | 0 | << " is not a logical pointer."; |
2945 | 0 | } |
2946 | | |
2947 | 0 | const auto pointer_type_id = pointer->type_id(); |
2948 | 0 | const auto pointer_type = _.FindDef(pointer_type_id); |
2949 | 0 | if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) { |
2950 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2951 | 0 | << opname << " type for pointer <id> " << _.getIdName(pointer_id) |
2952 | 0 | << " is not a pointer type."; |
2953 | 0 | } |
2954 | | |
2955 | 0 | const auto storage_class_index = 1u; |
2956 | 0 | const auto storage_class = |
2957 | 0 | pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index); |
2958 | |
|
2959 | 0 | if (storage_class != spv::StorageClass::Workgroup && |
2960 | 0 | storage_class != spv::StorageClass::StorageBuffer && |
2961 | 0 | storage_class != spv::StorageClass::PhysicalStorageBuffer) { |
2962 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2963 | 0 | << _.VkErrorID(8973) << opname |
2964 | 0 | << " storage class for pointer type <id> " |
2965 | 0 | << _.getIdName(pointer_type_id) |
2966 | 0 | << " is not Workgroup, StorageBuffer, or PhysicalStorageBuffer."; |
2967 | 0 | } |
2968 | | |
2969 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) { |
2970 | 0 | const auto object_index = 3; |
2971 | 0 | const auto object_id = inst->GetOperandAs<uint32_t>(object_index); |
2972 | 0 | const auto object = _.FindDef(object_id); |
2973 | 0 | if (!object || object->type_id() != type_id) { |
2974 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2975 | 0 | << opname << " Object <id> " << _.getIdName(object_id) |
2976 | 0 | << " type does not match Result Type."; |
2977 | 0 | } |
2978 | 0 | } |
2979 | | |
2980 | 0 | const auto tensor_layout_index = |
2981 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) ? 4u : 2u; |
2982 | 0 | const auto tensor_layout_id = |
2983 | 0 | inst->GetOperandAs<uint32_t>(tensor_layout_index); |
2984 | 0 | const auto tensor_layout = _.FindDef(tensor_layout_id); |
2985 | 0 | if (!tensor_layout || _.FindDef(tensor_layout->type_id())->opcode() != |
2986 | 0 | spv::Op::OpTypeTensorLayoutNV) { |
2987 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
2988 | 0 | << opname << " TensorLayout <id> " << _.getIdName(tensor_layout_id) |
2989 | 0 | << " does not have a tensor layout type."; |
2990 | 0 | } |
2991 | | |
2992 | 0 | const auto memory_access_index = |
2993 | 0 | (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) ? 5u : 3u; |
2994 | 0 | if (inst->operands().size() > memory_access_index) { |
2995 | 0 | if (auto error = CheckMemoryAccess(_, inst, memory_access_index)) |
2996 | 0 | return error; |
2997 | 0 | } |
2998 | | |
2999 | 0 | const auto memory_access_mask = |
3000 | 0 | inst->GetOperandAs<uint32_t>(memory_access_index); |
3001 | 0 | const auto tensor_operands_index = |
3002 | 0 | memory_access_index + MemoryAccessNumWords(memory_access_mask); |
3003 | 0 | const auto tensor_operands = |
3004 | 0 | inst->GetOperandAs<spv::TensorAddressingOperandsMask>( |
3005 | 0 | tensor_operands_index); |
3006 | |
|
3007 | 0 | if (inst->operands().size() < |
3008 | 0 | tensor_operands_index + |
3009 | 0 | TensorAddressingOperandsNumWords(tensor_operands)) { |
3010 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3011 | 0 | << opname << " not enough tensor addressing operands."; |
3012 | 0 | } |
3013 | | |
3014 | 0 | uint32_t tensor_operand_index = tensor_operands_index + 1; |
3015 | 0 | if ((tensor_operands & spv::TensorAddressingOperandsMask::TensorView) != |
3016 | 0 | spv::TensorAddressingOperandsMask::MaskNone) { |
3017 | 0 | const auto tensor_view_id = |
3018 | 0 | inst->GetOperandAs<uint32_t>(tensor_operand_index); |
3019 | 0 | const auto tensor_view = _.FindDef(tensor_view_id); |
3020 | 0 | if (!tensor_view || _.FindDef(tensor_view->type_id())->opcode() != |
3021 | 0 | spv::Op::OpTypeTensorViewNV) { |
3022 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3023 | 0 | << opname << " TensorView <id> " << _.getIdName(tensor_view_id) |
3024 | 0 | << " does not have a tensor view type."; |
3025 | 0 | } |
3026 | | |
3027 | 0 | tensor_operand_index++; |
3028 | 0 | } |
3029 | | |
3030 | 0 | const bool has_decode_func = |
3031 | 0 | (tensor_operands & spv::TensorAddressingOperandsMask::DecodeFunc) != |
3032 | 0 | spv::TensorAddressingOperandsMask::MaskNone; |
3033 | 0 | const bool has_decode_vector_func = |
3034 | 0 | (tensor_operands & spv::TensorAddressingOperandsMask::DecodeVectorFunc) != |
3035 | 0 | spv::TensorAddressingOperandsMask::MaskNone; |
3036 | |
|
3037 | 0 | if (has_decode_func || has_decode_vector_func) { |
3038 | 0 | if (inst->opcode() == spv::Op::OpCooperativeMatrixStoreTensorNV) { |
3039 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3040 | 0 | << "OpCooperativeMatrixStoreTensorNV does not support DecodeFunc " |
3041 | 0 | "or DecodeVectorFunc."; |
3042 | 0 | } |
3043 | 0 | } |
3044 | | |
3045 | 0 | const auto component_type_index = 1; |
3046 | 0 | const auto component_type_id = |
3047 | 0 | matrix_type->GetOperandAs<uint32_t>(component_type_index); |
3048 | 0 | const auto tensor_layout_type = _.FindDef(tensor_layout->type_id()); |
3049 | | |
3050 | | // Validate one decode-function operand (scalar DecodeFunc or vector |
3051 | | // DecodeVectorFunc). `expect_vector` selects which return-type rule to |
3052 | | // enforce. |
3053 | 0 | auto validate_decode_function = [&](uint32_t decode_func_id, |
3054 | 0 | const char* operand_name, |
3055 | 0 | bool expect_vector) -> spv_result_t { |
3056 | 0 | const auto decode_func = _.FindDef(decode_func_id); |
3057 | |
|
3058 | 0 | if (!decode_func || decode_func->opcode() != spv::Op::OpFunction) { |
3059 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3060 | 0 | << opname << " " << operand_name << " <id> " |
3061 | 0 | << _.getIdName(decode_func_id) << " is not a function."; |
3062 | 0 | } |
3063 | | |
3064 | 0 | const auto function_type = |
3065 | 0 | _.FindDef(decode_func->GetOperandAs<uint32_t>(3)); |
3066 | 0 | const auto return_type_id = function_type->GetOperandAs<uint32_t>(1); |
3067 | 0 | const auto return_type = _.FindDef(return_type_id); |
3068 | 0 | const bool return_is_scalar_match = (return_type_id == component_type_id); |
3069 | 0 | const bool return_is_vector = _.IsVectorType(return_type_id); |
3070 | 0 | const uint32_t return_vec_component_type_id = |
3071 | 0 | return_is_vector ? return_type->GetOperandAs<uint32_t>(1) : 0; |
3072 | |
|
3073 | 0 | if (!expect_vector) { |
3074 | 0 | if (!return_is_scalar_match) { |
3075 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3076 | 0 | << opname << " " << operand_name << " <id> " |
3077 | 0 | << _.getIdName(decode_func_id) |
3078 | 0 | << " return type must match matrix component type."; |
3079 | 0 | } |
3080 | 0 | } else { |
3081 | 0 | if (!return_is_vector || |
3082 | 0 | return_vec_component_type_id != component_type_id) { |
3083 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3084 | 0 | << opname << " " << operand_name << " <id> " |
3085 | 0 | << _.getIdName(decode_func_id) |
3086 | 0 | << " return type must be a vector of the matrix component " |
3087 | 0 | "type."; |
3088 | 0 | } |
3089 | | // GetDimension returns 0 for OpTypeVectorIdEXT whose count is a |
3090 | | // spec constant; skip the static check in that case. |
3091 | 0 | const uint32_t v = _.GetDimension(return_type_id); |
3092 | 0 | if (v != 0 && v != 2 && v != 4 && v != 8) { |
3093 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3094 | 0 | << opname << " " << operand_name << " <id> " |
3095 | 0 | << _.getIdName(decode_func_id) |
3096 | 0 | << " return vector length must be 2, 4, or 8."; |
3097 | 0 | } |
3098 | 0 | } |
3099 | | |
3100 | 0 | const auto decode_ptr_type_id = function_type->GetOperandAs<uint32_t>(2); |
3101 | 0 | const auto decode_ptr_type = _.FindDef(decode_ptr_type_id); |
3102 | 0 | auto decode_storage_class = |
3103 | 0 | decode_ptr_type->GetOperandAs<spv::StorageClass>(storage_class_index); |
3104 | |
|
3105 | 0 | if (decode_storage_class != spv::StorageClass::PhysicalStorageBuffer) { |
3106 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3107 | 0 | << opname << " " << operand_name << " <id> " |
3108 | 0 | << _.getIdName(decode_func_id) |
3109 | 0 | << " first parameter must be pointer to PhysicalStorageBuffer."; |
3110 | 0 | } |
3111 | | |
3112 | 0 | for (uint32_t param = 3; param < 5; ++param) { |
3113 | 0 | const auto param_type_id = function_type->GetOperandAs<uint32_t>(param); |
3114 | 0 | const auto param_type = _.FindDef(param_type_id); |
3115 | 0 | if (param_type->opcode() != spv::Op::OpTypeArray) { |
3116 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3117 | 0 | << opname << " " << operand_name << " <id> " |
3118 | 0 | << _.getIdName(decode_func_id) |
3119 | 0 | << " second/third parameter must be array of 32-bit integer " |
3120 | 0 | "with dimension equal to the tensor dimension."; |
3121 | 0 | } |
3122 | 0 | const auto length_index = 2u; |
3123 | 0 | uint64_t array_length; |
3124 | 0 | if (_.EvalConstantValUint64( |
3125 | 0 | param_type->GetOperandAs<uint32_t>(length_index), |
3126 | 0 | &array_length)) { |
3127 | 0 | const auto tensor_layout_dim_id = |
3128 | 0 | tensor_layout_type->GetOperandAs<uint32_t>(1); |
3129 | 0 | uint64_t dim_value; |
3130 | 0 | if (_.EvalConstantValUint64(tensor_layout_dim_id, &dim_value)) { |
3131 | 0 | if (array_length != dim_value) { |
3132 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3133 | 0 | << opname << " " << operand_name << " <id> " |
3134 | 0 | << _.getIdName(decode_func_id) |
3135 | 0 | << " second/third parameter must be array of 32-bit integer " |
3136 | 0 | "with dimension equal to the tensor dimension."; |
3137 | 0 | } |
3138 | 0 | } |
3139 | 0 | } |
3140 | 0 | } |
3141 | | |
3142 | 0 | return SPV_SUCCESS; |
3143 | 0 | }; |
3144 | |
|
3145 | 0 | if (has_decode_func) { |
3146 | 0 | const uint32_t decode_func_id = |
3147 | 0 | inst->GetOperandAs<uint32_t>(tensor_operand_index); |
3148 | 0 | if (auto error = validate_decode_function(decode_func_id, "DecodeFunc", |
3149 | 0 | /*expect_vector=*/false)) { |
3150 | 0 | return error; |
3151 | 0 | } |
3152 | 0 | tensor_operand_index++; |
3153 | 0 | } |
3154 | | |
3155 | 0 | if (has_decode_vector_func) { |
3156 | 0 | if (!has_decode_func) { |
3157 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3158 | 0 | << opname |
3159 | 0 | << " DecodeVectorFunc requires DecodeFunc to also be specified."; |
3160 | 0 | } |
3161 | | |
3162 | 0 | const uint32_t decode_vector_func_id = |
3163 | 0 | inst->GetOperandAs<uint32_t>(tensor_operand_index); |
3164 | 0 | if (auto error = |
3165 | 0 | validate_decode_function(decode_vector_func_id, "DecodeVectorFunc", |
3166 | 0 | /*expect_vector=*/true)) { |
3167 | 0 | return error; |
3168 | 0 | } |
3169 | | |
3170 | 0 | tensor_operand_index++; |
3171 | 0 | } |
3172 | | |
3173 | 0 | return SPV_SUCCESS; |
3174 | 0 | } |
3175 | | |
3176 | | spv_result_t ValidateInt32Operand(ValidationState_t& _, const Instruction* inst, |
3177 | | uint32_t operand_index, |
3178 | | const char* opcode_name, |
3179 | 0 | const char* operand_name) { |
3180 | 0 | const auto type_id = |
3181 | 0 | _.FindDef(inst->GetOperandAs<uint32_t>(operand_index))->type_id(); |
3182 | 0 | if (!_.IsIntScalarType(type_id, 32)) { |
3183 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3184 | 0 | << opcode_name << " " << operand_name << " type <id> " |
3185 | 0 | << _.getIdName(type_id) << " is not a 32 bit integer."; |
3186 | 0 | } |
3187 | 0 | return SPV_SUCCESS; |
3188 | 0 | } |
3189 | | |
3190 | | spv_result_t ValidateInt32Or64Operand(ValidationState_t& _, |
3191 | | const Instruction* inst, |
3192 | | uint32_t operand_index, |
3193 | | const char* opcode_name, |
3194 | 0 | const char* operand_name) { |
3195 | 0 | const auto type_id = |
3196 | 0 | _.FindDef(inst->GetOperandAs<uint32_t>(operand_index))->type_id(); |
3197 | 0 | if (!_.IsIntScalarType(type_id) || |
3198 | 0 | !(_.GetBitWidth(type_id) == 32 || _.GetBitWidth(type_id) == 64)) { |
3199 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3200 | 0 | << opcode_name << " " << operand_name << " type <id> " |
3201 | 0 | << _.getIdName(type_id) << " is not a 32 or 64 bit integer."; |
3202 | 0 | } |
3203 | | |
3204 | 0 | return SPV_SUCCESS; |
3205 | 0 | } |
3206 | | |
3207 | | spv_result_t ValidateCooperativeVectorPointer(ValidationState_t& _, |
3208 | | const Instruction* inst, |
3209 | | const char* opname, |
3210 | 0 | uint32_t pointer_index) { |
3211 | 0 | const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index); |
3212 | 0 | const auto pointer = _.FindDef(pointer_id); |
3213 | 0 | if (!pointer || |
3214 | 0 | ((_.addressing_model() == spv::AddressingModel::Logical) && |
3215 | 0 | ((!_.features().variable_pointers && |
3216 | 0 | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
3217 | 0 | (_.features().variable_pointers && |
3218 | 0 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
3219 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3220 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer_id) |
3221 | 0 | << " is not a logical pointer."; |
3222 | 0 | } |
3223 | | |
3224 | 0 | const auto pointer_type_id = pointer->type_id(); |
3225 | 0 | const auto pointer_type = _.FindDef(pointer_type_id); |
3226 | 0 | if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) { |
3227 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3228 | 0 | << opname << " type for pointer <id> " << _.getIdName(pointer_id) |
3229 | 0 | << " is not a pointer type."; |
3230 | 0 | } |
3231 | | |
3232 | 0 | const auto storage_class_index = 1u; |
3233 | 0 | const auto storage_class = |
3234 | 0 | pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index); |
3235 | |
|
3236 | 0 | if (storage_class != spv::StorageClass::Workgroup && |
3237 | 0 | storage_class != spv::StorageClass::StorageBuffer && |
3238 | 0 | storage_class != spv::StorageClass::PhysicalStorageBuffer) { |
3239 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3240 | 0 | << opname << " storage class for pointer type <id> " |
3241 | 0 | << _.getIdName(pointer_type_id) |
3242 | 0 | << " is not Workgroup or StorageBuffer."; |
3243 | 0 | } |
3244 | | |
3245 | 0 | const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2); |
3246 | 0 | const auto pointee_type = _.FindDef(pointee_id); |
3247 | 0 | if (!pointee_type || |
3248 | 0 | (pointee_type->opcode() != spv::Op::OpTypeArray && |
3249 | 0 | pointee_type->opcode() != spv::Op::OpTypeRuntimeArray)) { |
3250 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3251 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer->id()) |
3252 | 0 | << "s Type must be an array type."; |
3253 | 0 | } |
3254 | | |
3255 | 0 | const auto array_elem_type_id = pointee_type->GetOperandAs<uint32_t>(1); |
3256 | 0 | auto array_elem_type = _.FindDef(array_elem_type_id); |
3257 | 0 | if (!array_elem_type || !(_.IsIntScalarOrVectorType(array_elem_type_id) || |
3258 | 0 | _.IsFloatScalarOrVectorType(array_elem_type_id))) { |
3259 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3260 | 0 | << opname << " Pointer <id> " << _.getIdName(pointer->id()) |
3261 | 0 | << "s Type must be an array of scalar or vector type."; |
3262 | 0 | } |
3263 | | |
3264 | 0 | return SPV_SUCCESS; |
3265 | 0 | } |
3266 | | |
3267 | | spv_result_t ValidateCooperativeVectorLoadStoreNV(ValidationState_t& _, |
3268 | 0 | const Instruction* inst) { |
3269 | 0 | uint32_t type_id; |
3270 | 0 | const char* opname; |
3271 | 0 | if (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) { |
3272 | 0 | type_id = inst->type_id(); |
3273 | 0 | opname = "spv::Op::OpCooperativeVectorLoadNV"; |
3274 | 0 | } else { |
3275 | | // get Object operand's type |
3276 | 0 | type_id = _.FindDef(inst->GetOperandAs<uint32_t>(2))->type_id(); |
3277 | 0 | opname = "spv::Op::OpCooperativeVectorStoreNV"; |
3278 | 0 | } |
3279 | |
|
3280 | 0 | auto vector_type = _.FindDef(type_id); |
3281 | |
|
3282 | 0 | if (vector_type->opcode() != spv::Op::OpTypeVectorIdEXT) { |
3283 | 0 | if (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) { |
3284 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3285 | 0 | << "spv::Op::OpCooperativeVectorLoadNV Result Type <id> " |
3286 | 0 | << _.getIdName(type_id) << " is not a cooperative vector type."; |
3287 | 0 | } else { |
3288 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3289 | 0 | << "spv::Op::OpCooperativeVectorStoreNV Object type <id> " |
3290 | 0 | << _.getIdName(type_id) << " is not a cooperative vector type."; |
3291 | 0 | } |
3292 | 0 | } |
3293 | | |
3294 | 0 | const auto pointer_index = |
3295 | 0 | (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) ? 2u : 0u; |
3296 | |
|
3297 | 0 | const auto offset_index = |
3298 | 0 | (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) ? 3u : 1u; |
3299 | |
|
3300 | 0 | if (auto error = |
3301 | 0 | ValidateCooperativeVectorPointer(_, inst, opname, pointer_index)) { |
3302 | 0 | return error; |
3303 | 0 | } |
3304 | | |
3305 | 0 | if (auto error = |
3306 | 0 | ValidateInt32Or64Operand(_, inst, offset_index, opname, "Offset")) { |
3307 | 0 | return error; |
3308 | 0 | } |
3309 | | |
3310 | 0 | const auto memory_access_index = |
3311 | 0 | (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) ? 4u : 3u; |
3312 | 0 | if (inst->operands().size() > memory_access_index) { |
3313 | 0 | if (auto error = CheckMemoryAccess(_, inst, memory_access_index)) |
3314 | 0 | return error; |
3315 | 0 | } |
3316 | | |
3317 | 0 | return SPV_SUCCESS; |
3318 | 0 | } |
3319 | | |
3320 | | spv_result_t ValidateCooperativeVectorOuterProductNV(ValidationState_t& _, |
3321 | 0 | const Instruction* inst) { |
3322 | 0 | const auto pointer_index = 0u; |
3323 | 0 | const auto opcode_name = |
3324 | 0 | "spv::Op::OpCooperativeVectorOuterProductAccumulateNV"; |
3325 | |
|
3326 | 0 | if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name, |
3327 | 0 | pointer_index)) { |
3328 | 0 | return error; |
3329 | 0 | } |
3330 | | |
3331 | 0 | auto type_id = _.FindDef(inst->GetOperandAs<uint32_t>(2))->type_id(); |
3332 | 0 | auto a_type = _.FindDef(type_id); |
3333 | |
|
3334 | 0 | if (a_type->opcode() != spv::Op::OpTypeVectorIdEXT) { |
3335 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3336 | 0 | << opcode_name << " A type <id> " << _.getIdName(type_id) |
3337 | 0 | << " is not a cooperative vector type."; |
3338 | 0 | } |
3339 | | |
3340 | 0 | type_id = _.FindDef(inst->GetOperandAs<uint32_t>(3))->type_id(); |
3341 | 0 | auto b_type = _.FindDef(type_id); |
3342 | |
|
3343 | 0 | if (b_type->opcode() != spv::Op::OpTypeVectorIdEXT) { |
3344 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3345 | 0 | << opcode_name << " B type <id> " << _.getIdName(type_id) |
3346 | 0 | << " is not a cooperative vector type."; |
3347 | 0 | } |
3348 | | |
3349 | 0 | const auto a_component_type_id = a_type->GetOperandAs<uint32_t>(1); |
3350 | 0 | const auto b_component_type_id = b_type->GetOperandAs<uint32_t>(1); |
3351 | |
|
3352 | 0 | if (a_component_type_id != b_component_type_id) { |
3353 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3354 | 0 | << opcode_name << " A and B component types " |
3355 | 0 | << _.getIdName(a_component_type_id) << " and " |
3356 | 0 | << _.getIdName(b_component_type_id) << " do not match."; |
3357 | 0 | } |
3358 | | |
3359 | 0 | if (auto error = |
3360 | 0 | ValidateInt32Or64Operand(_, inst, 1, opcode_name, "Offset")) { |
3361 | 0 | return error; |
3362 | 0 | } |
3363 | | |
3364 | 0 | if (auto error = |
3365 | 0 | ValidateInt32Operand(_, inst, 4, opcode_name, "MemoryLayout")) { |
3366 | 0 | return error; |
3367 | 0 | } |
3368 | | |
3369 | 0 | if (auto error = ValidateInt32Operand(_, inst, 5, opcode_name, |
3370 | 0 | "MatrixInterpretation")) { |
3371 | 0 | return error; |
3372 | 0 | } |
3373 | | |
3374 | 0 | if (inst->operands().size() > 6) { |
3375 | 0 | if (auto error = |
3376 | 0 | ValidateInt32Operand(_, inst, 6, opcode_name, "MatrixStride")) { |
3377 | 0 | return error; |
3378 | 0 | } |
3379 | 0 | } |
3380 | | |
3381 | 0 | return SPV_SUCCESS; |
3382 | 0 | } |
3383 | | |
3384 | | spv_result_t ValidateCooperativeVectorReduceSumNV(ValidationState_t& _, |
3385 | 0 | const Instruction* inst) { |
3386 | 0 | const auto opcode_name = "spv::Op::OpCooperativeVectorReduceSumAccumulateNV"; |
3387 | 0 | const auto pointer_index = 0u; |
3388 | |
|
3389 | 0 | if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name, |
3390 | 0 | pointer_index)) { |
3391 | 0 | return error; |
3392 | 0 | } |
3393 | | |
3394 | 0 | auto type_id = _.FindDef(inst->GetOperandAs<uint32_t>(2))->type_id(); |
3395 | 0 | auto v_type = _.FindDef(type_id); |
3396 | |
|
3397 | 0 | if (v_type->opcode() != spv::Op::OpTypeVectorIdEXT) { |
3398 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3399 | 0 | << opcode_name << " V type <id> " << _.getIdName(type_id) |
3400 | 0 | << " is not a cooperative vector type."; |
3401 | 0 | } |
3402 | | |
3403 | 0 | if (auto error = |
3404 | 0 | ValidateInt32Or64Operand(_, inst, 1, opcode_name, "Offset")) { |
3405 | 0 | return error; |
3406 | 0 | } |
3407 | | |
3408 | 0 | return SPV_SUCCESS; |
3409 | 0 | } |
3410 | | |
3411 | 0 | bool InterpretationIsPacked(spv::ComponentType interp) { |
3412 | 0 | switch (interp) { |
3413 | 0 | case spv::ComponentType::SignedInt8PackedNV: |
3414 | 0 | case spv::ComponentType::UnsignedInt8PackedNV: |
3415 | 0 | return true; |
3416 | 0 | default: |
3417 | 0 | return false; |
3418 | 0 | } |
3419 | 0 | } |
3420 | | |
3421 | | using std::get; |
3422 | | |
3423 | | spv_result_t ValidateCooperativeVectorMatrixMulNV(ValidationState_t& _, |
3424 | 0 | const Instruction* inst) { |
3425 | 0 | const bool has_bias = |
3426 | 0 | inst->opcode() == spv::Op::OpCooperativeVectorMatrixMulAddNV; |
3427 | 0 | const auto opcode_name = has_bias |
3428 | 0 | ? "spv::Op::OpCooperativeVectorMatrixMulAddNV" |
3429 | 0 | : "spv::Op::OpCooperativeVectorMatrixMulNV"; |
3430 | |
|
3431 | 0 | const auto bias_offset = has_bias ? 3 : 0; |
3432 | |
|
3433 | 0 | const auto result_type_index = 0u; |
3434 | 0 | const auto input_index = 2u; |
3435 | 0 | const auto input_interpretation_index = 3u; |
3436 | 0 | const auto matrix_index = 4u; |
3437 | 0 | const auto matrix_offset_index = 5u; |
3438 | 0 | const auto matrix_interpretation_index = 6u; |
3439 | 0 | const auto bias_index = 7u; |
3440 | 0 | const auto bias_offset_index = 8u; |
3441 | 0 | const auto bias_interpretation_index = 9u; |
3442 | 0 | const auto m_index = 7u + bias_offset; |
3443 | 0 | const auto k_index = 8u + bias_offset; |
3444 | 0 | const auto memory_layout_index = 9u + bias_offset; |
3445 | 0 | const auto transpose_index = 10u + bias_offset; |
3446 | |
|
3447 | 0 | const auto result_type_id = inst->GetOperandAs<uint32_t>(result_type_index); |
3448 | 0 | const auto input_id = inst->GetOperandAs<uint32_t>(input_index); |
3449 | 0 | const auto input_interpretation_id = |
3450 | 0 | inst->GetOperandAs<uint32_t>(input_interpretation_index); |
3451 | 0 | const auto matrix_interpretation_id = |
3452 | 0 | inst->GetOperandAs<uint32_t>(matrix_interpretation_index); |
3453 | 0 | const auto bias_interpretation_id = |
3454 | 0 | inst->GetOperandAs<uint32_t>(bias_interpretation_index); |
3455 | 0 | const auto m_id = inst->GetOperandAs<uint32_t>(m_index); |
3456 | 0 | const auto k_id = inst->GetOperandAs<uint32_t>(k_index); |
3457 | 0 | const auto memory_layout_id = |
3458 | 0 | inst->GetOperandAs<uint32_t>(memory_layout_index); |
3459 | 0 | const auto transpose_id = inst->GetOperandAs<uint32_t>(transpose_index); |
3460 | |
|
3461 | 0 | if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name, |
3462 | 0 | matrix_index)) { |
3463 | 0 | return error; |
3464 | 0 | } |
3465 | | |
3466 | 0 | if (inst->opcode() == spv::Op::OpCooperativeVectorMatrixMulAddNV) { |
3467 | 0 | if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name, |
3468 | 0 | bias_index)) { |
3469 | 0 | return error; |
3470 | 0 | } |
3471 | 0 | } |
3472 | | |
3473 | 0 | const auto result_type = _.FindDef(result_type_id); |
3474 | |
|
3475 | 0 | if (result_type->opcode() != spv::Op::OpTypeVectorIdEXT) { |
3476 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3477 | 0 | << opcode_name << " result type <id> " << _.getIdName(result_type_id) |
3478 | 0 | << " is not a cooperative vector type."; |
3479 | 0 | } |
3480 | | |
3481 | 0 | const auto result_component_type_id = result_type->GetOperandAs<uint32_t>(1u); |
3482 | 0 | if (!_.IsIntScalarType(result_component_type_id, 32) && |
3483 | 0 | !_.IsFloatScalarType(result_component_type_id, 32) && |
3484 | 0 | !_.IsFloatScalarType(result_component_type_id, 16)) { |
3485 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3486 | 0 | << opcode_name << " result component type <id> " |
3487 | 0 | << _.getIdName(result_component_type_id) |
3488 | 0 | << " is not a 32 bit int or 16/32 bit float."; |
3489 | 0 | } |
3490 | | |
3491 | 0 | const auto m_eval = _.EvalInt32IfConst(m_id); |
3492 | 0 | const auto rc_eval = |
3493 | 0 | _.EvalInt32IfConst(result_type->GetOperandAs<uint32_t>(2u)); |
3494 | 0 | if (get<1>(m_eval) && get<1>(rc_eval) && get<2>(m_eval) != get<2>(rc_eval)) { |
3495 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3496 | 0 | << opcode_name << " result type number of components " |
3497 | 0 | << get<2>(rc_eval) << " does not match M " << get<2>(m_eval); |
3498 | 0 | } |
3499 | | |
3500 | 0 | const auto k_eval = _.EvalInt32IfConst(k_id); |
3501 | |
|
3502 | 0 | const auto input = _.FindDef(input_id); |
3503 | 0 | const auto input_type = _.FindDef(input->type_id()); |
3504 | 0 | const auto input_num_components_id = input_type->GetOperandAs<uint32_t>(2u); |
3505 | |
|
3506 | 0 | auto input_interp_eval = _.EvalInt32IfConst(input_interpretation_id); |
3507 | 0 | if (get<1>(input_interp_eval) && |
3508 | 0 | !InterpretationIsPacked(spv::ComponentType{get<2>(input_interp_eval)})) { |
3509 | 0 | const auto inc_eval = _.EvalInt32IfConst(input_num_components_id); |
3510 | 0 | if (get<1>(inc_eval) && get<1>(k_eval) && |
3511 | 0 | get<2>(inc_eval) != get<2>(k_eval)) { |
3512 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3513 | 0 | << opcode_name << " input number of components " |
3514 | 0 | << get<2>(inc_eval) << " does not match K " << get<2>(k_eval); |
3515 | 0 | } |
3516 | 0 | } |
3517 | | |
3518 | 0 | if (!_.IsBoolScalarType(_.FindDef(transpose_id)->type_id())) { |
3519 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3520 | 0 | << opcode_name << " Transpose <id> " << _.getIdName(transpose_id) |
3521 | 0 | << " is not a scalar boolean."; |
3522 | 0 | } |
3523 | | |
3524 | 0 | const auto check_constant = [&](uint32_t id, |
3525 | 0 | const char* operand_name) -> spv_result_t { |
3526 | 0 | if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) { |
3527 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3528 | 0 | << opcode_name << " " << operand_name << " <id> " |
3529 | 0 | << _.getIdName(id) << " is not a constant instruction."; |
3530 | 0 | } |
3531 | 0 | return SPV_SUCCESS; |
3532 | 0 | }; |
3533 | |
|
3534 | 0 | if (auto error = |
3535 | 0 | check_constant(input_interpretation_id, "InputInterpretation")) { |
3536 | 0 | return error; |
3537 | 0 | } |
3538 | 0 | if (auto error = |
3539 | 0 | check_constant(matrix_interpretation_id, "MatrixInterpretation")) { |
3540 | 0 | return error; |
3541 | 0 | } |
3542 | 0 | if (has_bias) { |
3543 | 0 | if (auto error = |
3544 | 0 | check_constant(bias_interpretation_id, "BiasInterpretation")) { |
3545 | 0 | return error; |
3546 | 0 | } |
3547 | 0 | } |
3548 | 0 | if (auto error = check_constant(m_id, "M")) { |
3549 | 0 | return error; |
3550 | 0 | } |
3551 | 0 | if (auto error = check_constant(k_id, "K")) { |
3552 | 0 | return error; |
3553 | 0 | } |
3554 | 0 | if (auto error = check_constant(memory_layout_id, "MemoryLayout")) { |
3555 | 0 | return error; |
3556 | 0 | } |
3557 | 0 | if (auto error = check_constant(transpose_id, "Transpose")) { |
3558 | 0 | return error; |
3559 | 0 | } |
3560 | | |
3561 | 0 | if (auto error = ValidateInt32Operand(_, inst, input_interpretation_index, |
3562 | 0 | opcode_name, "InputInterpretation")) { |
3563 | 0 | return error; |
3564 | 0 | } |
3565 | 0 | if (auto error = ValidateInt32Operand(_, inst, matrix_interpretation_index, |
3566 | 0 | opcode_name, "MatrixInterpretation")) { |
3567 | 0 | return error; |
3568 | 0 | } |
3569 | 0 | if (has_bias) { |
3570 | 0 | if (auto error = ValidateInt32Operand(_, inst, bias_interpretation_index, |
3571 | 0 | opcode_name, "BiasInterpretation")) { |
3572 | 0 | return error; |
3573 | 0 | } |
3574 | 0 | } |
3575 | 0 | if (auto error = ValidateInt32Operand(_, inst, m_index, opcode_name, "M")) { |
3576 | 0 | return error; |
3577 | 0 | } |
3578 | 0 | if (auto error = ValidateInt32Operand(_, inst, k_index, opcode_name, "K")) { |
3579 | 0 | return error; |
3580 | 0 | } |
3581 | 0 | if (auto error = ValidateInt32Operand(_, inst, memory_layout_index, |
3582 | 0 | opcode_name, "MemoryLayout")) { |
3583 | 0 | return error; |
3584 | 0 | } |
3585 | | |
3586 | 0 | if (auto error = ValidateInt32Or64Operand(_, inst, matrix_offset_index, |
3587 | 0 | opcode_name, "MatrixOffset")) { |
3588 | 0 | return error; |
3589 | 0 | } |
3590 | 0 | if (has_bias) { |
3591 | 0 | if (auto error = ValidateInt32Or64Operand(_, inst, bias_offset_index, |
3592 | 0 | opcode_name, "BiasOffset")) { |
3593 | 0 | return error; |
3594 | 0 | } |
3595 | 0 | } |
3596 | | |
3597 | 0 | return SPV_SUCCESS; |
3598 | 0 | } |
3599 | | |
3600 | | spv_result_t ValidatePtrComparison(ValidationState_t& _, |
3601 | 1 | const Instruction* inst) { |
3602 | 1 | const auto op1 = _.FindDef(inst->GetOperandAs<uint32_t>(2u)); |
3603 | 1 | const auto op2 = _.FindDef(inst->GetOperandAs<uint32_t>(3u)); |
3604 | 1 | const auto op1_type = _.FindDef(op1->type_id()); |
3605 | 1 | const auto op2_type = _.FindDef(op2->type_id()); |
3606 | 1 | spv::StorageClass sc = op1_type->GetOperandAs<spv::StorageClass>(1u); |
3607 | 1 | if ((_.addressing_model() == spv::AddressingModel::Logical || |
3608 | 0 | _.addressing_model() == spv::AddressingModel::PhysicalStorageBuffer64) && |
3609 | 1 | sc != spv::StorageClass::PhysicalStorageBuffer && |
3610 | 1 | !_.features().variable_pointers) { |
3611 | 1 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3612 | 1 | << "Instruction on logical pointers cannot be used without " |
3613 | 1 | "a variable pointers capability"; |
3614 | 1 | } |
3615 | | |
3616 | 0 | const auto result_type = _.FindDef(inst->type_id()); |
3617 | 0 | if (inst->opcode() == spv::Op::OpPtrDiff) { |
3618 | 0 | if (!result_type || result_type->opcode() != spv::Op::OpTypeInt) { |
3619 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3620 | 0 | << "Result Type must be an integer scalar"; |
3621 | 0 | } |
3622 | 0 | } else { |
3623 | 0 | if (!result_type || result_type->opcode() != spv::Op::OpTypeBool) { |
3624 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3625 | 0 | << "Result Type must be OpTypeBool"; |
3626 | 0 | } |
3627 | 0 | } |
3628 | | |
3629 | 0 | if (!op1_type || (op1_type->opcode() != spv::Op::OpTypePointer && |
3630 | 0 | op1_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
3631 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3632 | 0 | << "Operand type must be a pointer"; |
3633 | 0 | } |
3634 | | |
3635 | 0 | if (!op2_type || (op2_type->opcode() != spv::Op::OpTypePointer && |
3636 | 0 | op2_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
3637 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3638 | 0 | << "Operand type must be a pointer"; |
3639 | 0 | } |
3640 | | |
3641 | 0 | if (inst->opcode() == spv::Op::OpPtrDiff) { |
3642 | 0 | if (op1->type_id() != op2->type_id()) { |
3643 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3644 | 0 | << "The types of Operand 1 and Operand 2 must match"; |
3645 | 0 | } |
3646 | 0 | } else { |
3647 | 0 | const auto either_untyped = |
3648 | 0 | op1_type->opcode() == spv::Op::OpTypeUntypedPointerKHR || |
3649 | 0 | op2_type->opcode() == spv::Op::OpTypeUntypedPointerKHR; |
3650 | 0 | if (either_untyped) { |
3651 | 0 | const auto sc1 = op1_type->GetOperandAs<spv::StorageClass>(1); |
3652 | 0 | const auto sc2 = op2_type->GetOperandAs<spv::StorageClass>(1); |
3653 | 0 | if (sc1 != sc2) { |
3654 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3655 | 0 | << "Pointer storage classes must match"; |
3656 | 0 | } |
3657 | 0 | } else if (op1->type_id() != op2->type_id()) { |
3658 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3659 | 0 | << "The types of Operand 1 and Operand 2 must match"; |
3660 | 0 | } |
3661 | 0 | } |
3662 | | |
3663 | 0 | if (_.addressing_model() == spv::AddressingModel::Logical) { |
3664 | 0 | if (sc != spv::StorageClass::Workgroup && |
3665 | 0 | sc != spv::StorageClass::StorageBuffer) { |
3666 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3667 | 0 | << "Invalid pointer storage class"; |
3668 | 0 | } |
3669 | | |
3670 | 0 | if (sc == spv::StorageClass::Workgroup && |
3671 | 0 | !_.HasCapability(spv::Capability::VariablePointers)) { |
3672 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3673 | 0 | << "Workgroup storage class pointer requires VariablePointers " |
3674 | 0 | "capability to be specified"; |
3675 | 0 | } |
3676 | 0 | } else if (sc == spv::StorageClass::PhysicalStorageBuffer) { |
3677 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3678 | 0 | << "Cannot use a pointer in the PhysicalStorageBuffer storage class"; |
3679 | 0 | } |
3680 | | |
3681 | 0 | return SPV_SUCCESS; |
3682 | 0 | } |
3683 | | |
3684 | | spv_result_t ValidatePredicatedLoadINTEL(ValidationState_t& _, |
3685 | 0 | const Instruction* inst) { |
3686 | 0 | const auto result_type_id = inst->type_id(); |
3687 | 0 | if (!_.IsIntScalarOrVectorType(result_type_id) && |
3688 | 0 | !_.IsFloatScalarOrVectorType(result_type_id)) { |
3689 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3690 | 0 | << "OpPredicatedLoadINTEL Result Type <id> " |
3691 | 0 | << _.getIdName(result_type_id) |
3692 | 0 | << " must be a scalar or vector of numerical type."; |
3693 | 0 | } |
3694 | | |
3695 | 0 | const auto pointer_id = inst->GetOperandAs<uint32_t>(2); |
3696 | 0 | const auto pointer = _.FindDef(pointer_id); |
3697 | 0 | if (!pointer || |
3698 | 0 | ((_.addressing_model() == spv::AddressingModel::Logical) && |
3699 | 0 | ((!_.features().variable_pointers && |
3700 | 0 | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
3701 | 0 | (_.features().variable_pointers && |
3702 | 0 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
3703 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3704 | 0 | << "OpPredicatedLoadINTEL Pointer <id> " << _.getIdName(pointer_id) |
3705 | 0 | << " is not a logical pointer."; |
3706 | 0 | } |
3707 | | |
3708 | 0 | const auto pointer_type = _.FindDef(pointer->type_id()); |
3709 | 0 | if (!pointer_type || |
3710 | 0 | (pointer_type->opcode() != spv::Op::OpTypePointer && |
3711 | 0 | pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
3712 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3713 | 0 | << "OpPredicatedLoadINTEL type for pointer <id> " |
3714 | 0 | << _.getIdName(pointer_id) << " is not a pointer type."; |
3715 | 0 | } |
3716 | | |
3717 | 0 | if (pointer_type->opcode() == spv::Op::OpTypePointer) { |
3718 | 0 | const auto pointee_type = |
3719 | 0 | _.FindDef(pointer_type->GetOperandAs<uint32_t>(2)); |
3720 | 0 | if (!pointee_type || result_type_id != pointee_type->id()) { |
3721 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3722 | 0 | << "OpPredicatedLoadINTEL Result Type <id> " |
3723 | 0 | << _.getIdName(result_type_id) << " does not match Pointer <id> " |
3724 | 0 | << _.getIdName(pointer->id()) << "s type."; |
3725 | 0 | } |
3726 | 0 | } |
3727 | | |
3728 | 0 | const auto predicate_id = inst->GetOperandAs<uint32_t>(3); |
3729 | 0 | const auto predicate = _.FindDef(predicate_id); |
3730 | 0 | if (!predicate || !_.IsBoolScalarType(predicate->type_id())) { |
3731 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3732 | 0 | << "OpPredicatedLoadINTEL Predicate <id> " |
3733 | 0 | << _.getIdName(predicate_id) << " must be a Boolean scalar."; |
3734 | 0 | } |
3735 | | |
3736 | 0 | const auto default_value_id = inst->GetOperandAs<uint32_t>(4); |
3737 | 0 | const auto default_value = _.FindDef(default_value_id); |
3738 | 0 | if (!default_value || default_value->type_id() != result_type_id) { |
3739 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3740 | 0 | << "OpPredicatedLoadINTEL Default Value <id> " |
3741 | 0 | << _.getIdName(default_value_id) |
3742 | 0 | << " type does not match Result Type."; |
3743 | 0 | } |
3744 | | |
3745 | 0 | if (inst->operands().size() > 5) { |
3746 | 0 | const auto mask = inst->GetOperandAs<uint32_t>(5); |
3747 | 0 | if (mask & uint32_t(spv::MemoryAccessMask::Volatile)) { |
3748 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3749 | 0 | << "OpPredicatedLoadINTEL does not allow the Volatile memory " |
3750 | 0 | "operand."; |
3751 | 0 | } |
3752 | 0 | } |
3753 | | |
3754 | 0 | if (auto error = CheckMemoryAccess(_, inst, 5)) return error; |
3755 | | |
3756 | 0 | return SPV_SUCCESS; |
3757 | 0 | } |
3758 | | |
3759 | | spv_result_t ValidatePredicatedStoreINTEL(ValidationState_t& _, |
3760 | 0 | const Instruction* inst) { |
3761 | 0 | const auto pointer_id = inst->GetOperandAs<uint32_t>(0); |
3762 | 0 | const auto pointer = _.FindDef(pointer_id); |
3763 | 0 | if (!pointer || |
3764 | 0 | (_.addressing_model() == spv::AddressingModel::Logical && |
3765 | 0 | ((!_.features().variable_pointers && |
3766 | 0 | !spvOpcodeReturnsLogicalPointer(pointer->opcode())) || |
3767 | 0 | (_.features().variable_pointers && |
3768 | 0 | !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) { |
3769 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3770 | 0 | << "OpPredicatedStoreINTEL Pointer <id> " << _.getIdName(pointer_id) |
3771 | 0 | << " is not a logical pointer."; |
3772 | 0 | } |
3773 | | |
3774 | 0 | const auto pointer_type = _.FindDef(pointer->type_id()); |
3775 | 0 | if (!pointer_type || |
3776 | 0 | (pointer_type->opcode() != spv::Op::OpTypePointer && |
3777 | 0 | pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) { |
3778 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3779 | 0 | << "OpPredicatedStoreINTEL type for pointer <id> " |
3780 | 0 | << _.getIdName(pointer_id) << " is not a pointer type."; |
3781 | 0 | } |
3782 | | |
3783 | 0 | const auto object_id = inst->GetOperandAs<uint32_t>(1); |
3784 | 0 | const auto object = _.FindDef(object_id); |
3785 | 0 | if (!object || !object->type_id()) { |
3786 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3787 | 0 | << "OpPredicatedStoreINTEL Object <id> " << _.getIdName(object_id) |
3788 | 0 | << " is not an object."; |
3789 | 0 | } |
3790 | | |
3791 | 0 | const auto object_type_id = object->type_id(); |
3792 | 0 | if (!_.IsIntScalarOrVectorType(object_type_id) && |
3793 | 0 | !_.IsFloatScalarOrVectorType(object_type_id)) { |
3794 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3795 | 0 | << "OpPredicatedStoreINTEL Object <id> " << _.getIdName(object_id) |
3796 | 0 | << " type must be a scalar or vector of numerical type."; |
3797 | 0 | } |
3798 | | |
3799 | 0 | if (pointer_type->opcode() == spv::Op::OpTypePointer) { |
3800 | 0 | const auto pointee_type = |
3801 | 0 | _.FindDef(pointer_type->GetOperandAs<uint32_t>(2)); |
3802 | 0 | if (!pointee_type || pointee_type->id() != object_type_id) { |
3803 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3804 | 0 | << "OpPredicatedStoreINTEL Pointer <id> " |
3805 | 0 | << _.getIdName(pointer_id) << "s type does not match Object <id> " |
3806 | 0 | << _.getIdName(object->id()) << "s type."; |
3807 | 0 | } |
3808 | 0 | } |
3809 | | |
3810 | 0 | const auto predicate_id = inst->GetOperandAs<uint32_t>(2); |
3811 | 0 | const auto predicate = _.FindDef(predicate_id); |
3812 | 0 | if (!predicate || !_.IsBoolScalarType(predicate->type_id())) { |
3813 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3814 | 0 | << "OpPredicatedStoreINTEL Predicate <id> " |
3815 | 0 | << _.getIdName(predicate_id) << " must be a Boolean scalar."; |
3816 | 0 | } |
3817 | | |
3818 | 0 | if (inst->operands().size() > 3) { |
3819 | 0 | const auto mask = inst->GetOperandAs<uint32_t>(3); |
3820 | 0 | if (mask & uint32_t(spv::MemoryAccessMask::Volatile)) { |
3821 | 0 | return _.diag(SPV_ERROR_INVALID_ID, inst) |
3822 | 0 | << "OpPredicatedStoreINTEL does not allow the Volatile memory " |
3823 | 0 | "operand."; |
3824 | 0 | } |
3825 | 0 | } |
3826 | | |
3827 | 0 | if (auto error = CheckMemoryAccess(_, inst, 3)) return error; |
3828 | | |
3829 | 0 | return SPV_SUCCESS; |
3830 | 0 | } |
3831 | | |
3832 | | } // namespace |
3833 | | |
3834 | 14.9M | spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst) { |
3835 | 14.9M | switch (inst->opcode()) { |
3836 | 200k | case spv::Op::OpVariable: |
3837 | 200k | case spv::Op::OpUntypedVariableKHR: |
3838 | 200k | return ValidateVariable(_, inst); |
3839 | 0 | case spv::Op::OpBufferPointerEXT: |
3840 | 0 | return ValidateBufferPointerEXT(_, inst); |
3841 | 320k | case spv::Op::OpLoad: |
3842 | 320k | return ValidateLoad(_, inst); |
3843 | 263k | case spv::Op::OpStore: |
3844 | 263k | return ValidateStore(_, inst); |
3845 | 47.7k | case spv::Op::OpCopyMemory: |
3846 | 47.7k | case spv::Op::OpCopyMemorySized: |
3847 | 47.7k | return ValidateCopyMemory(_, inst); |
3848 | 6 | case spv::Op::OpPtrAccessChain: |
3849 | 6 | case spv::Op::OpUntypedPtrAccessChainKHR: |
3850 | 6 | case spv::Op::OpUntypedInBoundsPtrAccessChainKHR: |
3851 | 6 | return ValidatePtrAccessChain(_, inst); |
3852 | 149k | case spv::Op::OpAccessChain: |
3853 | 150k | case spv::Op::OpInBoundsAccessChain: |
3854 | 150k | case spv::Op::OpInBoundsPtrAccessChain: |
3855 | 150k | case spv::Op::OpUntypedAccessChainKHR: |
3856 | 150k | case spv::Op::OpUntypedInBoundsAccessChainKHR: |
3857 | 150k | return ValidateAccessChain(_, inst); |
3858 | 0 | case spv::Op::OpRawAccessChainNV: |
3859 | 0 | return ValidateRawAccessChain(_, inst); |
3860 | 34 | case spv::Op::OpArrayLength: |
3861 | 34 | case spv::Op::OpUntypedArrayLengthKHR: |
3862 | 34 | return ValidateArrayLength(_, inst); |
3863 | 0 | case spv::Op::OpCooperativeMatrixLoadNV: |
3864 | 0 | case spv::Op::OpCooperativeMatrixStoreNV: |
3865 | 0 | return ValidateCooperativeMatrixLoadStoreNV(_, inst); |
3866 | 0 | case spv::Op::OpCooperativeMatrixLengthKHR: |
3867 | 0 | return ValidateCooperativeMatrixLength(_, inst, true); |
3868 | 0 | case spv::Op::OpCooperativeMatrixLengthNV: |
3869 | 0 | return ValidateCooperativeMatrixLength(_, inst, false); |
3870 | 0 | case spv::Op::OpCooperativeMatrixGetCoordinateEXT: |
3871 | 0 | return ValidateCooperativeMatrixGetCoordinateEXT(_, inst); |
3872 | 0 | case spv::Op::OpCooperativeMatrixLoadKHR: |
3873 | 0 | case spv::Op::OpCooperativeMatrixStoreKHR: |
3874 | 0 | return ValidateCooperativeMatrixLoadStoreKHR(_, inst); |
3875 | 0 | case spv::Op::OpCooperativeMatrixLoadTensorNV: |
3876 | 0 | case spv::Op::OpCooperativeMatrixStoreTensorNV: |
3877 | 0 | return ValidateCooperativeMatrixLoadStoreTensorNV(_, inst); |
3878 | 0 | case spv::Op::OpCooperativeVectorLoadNV: |
3879 | 0 | case spv::Op::OpCooperativeVectorStoreNV: |
3880 | 0 | return ValidateCooperativeVectorLoadStoreNV(_, inst); |
3881 | 0 | case spv::Op::OpCooperativeVectorOuterProductAccumulateNV: |
3882 | 0 | return ValidateCooperativeVectorOuterProductNV(_, inst); |
3883 | 0 | case spv::Op::OpCooperativeVectorReduceSumAccumulateNV: |
3884 | 0 | return ValidateCooperativeVectorReduceSumNV(_, inst); |
3885 | 0 | case spv::Op::OpCooperativeVectorMatrixMulNV: |
3886 | 0 | case spv::Op::OpCooperativeVectorMatrixMulAddNV: |
3887 | 0 | return ValidateCooperativeVectorMatrixMulNV(_, inst); |
3888 | 0 | case spv::Op::OpPredicatedLoadINTEL: |
3889 | 0 | return ValidatePredicatedLoadINTEL(_, inst); |
3890 | 0 | case spv::Op::OpPredicatedStoreINTEL: |
3891 | 0 | return ValidatePredicatedStoreINTEL(_, inst); |
3892 | 0 | case spv::Op::OpPtrEqual: |
3893 | 0 | case spv::Op::OpPtrNotEqual: |
3894 | 1 | case spv::Op::OpPtrDiff: |
3895 | 1 | return ValidatePtrComparison(_, inst); |
3896 | 20 | case spv::Op::OpImageTexelPointer: |
3897 | 20 | case spv::Op::OpGenericPtrMemSemantics: |
3898 | 20 | break; // no validation currently |
3899 | 348 | case spv::Op::OpSpecConstantOp: { |
3900 | 348 | switch (inst->GetOperandAs<spv::Op>(2u)) { |
3901 | 2 | case spv::Op::OpCooperativeMatrixLengthKHR: |
3902 | 2 | return ValidateCooperativeMatrixLength(_, inst, true, 3); |
3903 | 6 | case spv::Op::OpCooperativeMatrixLengthNV: |
3904 | 6 | return ValidateCooperativeMatrixLength(_, inst, false, 3); |
3905 | | // TODO - Add AccesChains |
3906 | 340 | default: |
3907 | 340 | break; |
3908 | 348 | } |
3909 | 348 | } |
3910 | | |
3911 | 13.9M | default: |
3912 | 13.9M | break; |
3913 | 14.9M | } |
3914 | | |
3915 | 13.9M | return SPV_SUCCESS; |
3916 | 14.9M | } |
3917 | | } // namespace val |
3918 | | } // namespace spvtools |