/src/shaderc/third_party/spirv-tools/source/binary.cpp
Line | Count | Source |
1 | | // Copyright (c) 2015-2020 The Khronos Group Inc. |
2 | | // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights |
3 | | // reserved. |
4 | | // |
5 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | // you may not use this file except in compliance with the License. |
7 | | // You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, software |
12 | | // distributed under the License is distributed on an "AS IS" BASIS, |
13 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | // See the License for the specific language governing permissions and |
15 | | // limitations under the License. |
16 | | |
17 | | #include "source/binary.h" |
18 | | |
19 | | #include <algorithm> |
20 | | #include <cassert> |
21 | | #include <cstring> |
22 | | #include <iterator> |
23 | | #include <limits> |
24 | | #include <string> |
25 | | #include <unordered_map> |
26 | | #include <vector> |
27 | | |
28 | | #include "source/assembly_grammar.h" |
29 | | #include "source/diagnostic.h" |
30 | | #include "source/ext_inst.h" |
31 | | #include "source/latest_version_spirv_header.h" |
32 | | #include "source/opcode.h" |
33 | | #include "source/operand.h" |
34 | | #include "source/spirv_constant.h" |
35 | | #include "source/spirv_endian.h" |
36 | | #include "source/table2.h" |
37 | | #include "source/util/string_utils.h" |
38 | | |
39 | | spv_result_t spvBinaryHeaderGet(const spv_const_binary binary, |
40 | | const spv_endianness_t endian, |
41 | 2.11k | spv_header_t* pHeader) { |
42 | 2.11k | if (!binary->code) return SPV_ERROR_INVALID_BINARY; |
43 | 2.11k | if (binary->wordCount < SPV_INDEX_INSTRUCTION) |
44 | 0 | return SPV_ERROR_INVALID_BINARY; |
45 | 2.11k | if (!pHeader) return SPV_ERROR_INVALID_POINTER; |
46 | | |
47 | | // TODO: Validation checking? |
48 | 2.11k | pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian); |
49 | 2.11k | pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian); |
50 | | // Per 2.3.1 version's high and low bytes are 0 |
51 | 2.11k | if ((pHeader->version & 0x000000ff) || pHeader->version & 0xff000000) |
52 | 0 | return SPV_ERROR_INVALID_BINARY; |
53 | | // Minimum version was 1.0 and max version is defined by SPV_VERSION. |
54 | 2.11k | if (pHeader->version < SPV_SPIRV_VERSION_WORD(1, 0) || |
55 | 2.11k | pHeader->version > SPV_VERSION) |
56 | 0 | return SPV_ERROR_INVALID_BINARY; |
57 | | |
58 | 2.11k | pHeader->generator = |
59 | 2.11k | spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian); |
60 | 2.11k | pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian); |
61 | 2.11k | pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian); |
62 | 2.11k | pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION]; |
63 | | |
64 | 2.11k | return SPV_SUCCESS; |
65 | 2.11k | } |
66 | | |
67 | | std::string spvDecodeLiteralStringOperand(const spv_parsed_instruction_t& inst, |
68 | 3.65k | const uint16_t operand_index) { |
69 | 3.65k | assert(operand_index < inst.num_operands); |
70 | 3.65k | const spv_parsed_operand_t& operand = inst.operands[operand_index]; |
71 | | |
72 | 3.65k | return spvtools::utils::MakeString(inst.words + operand.offset, |
73 | 3.65k | operand.num_words); |
74 | 3.65k | } |
75 | | |
76 | | namespace { |
77 | | |
78 | | // A SPIR-V binary parser. A parser instance communicates detailed parse |
79 | | // results via callbacks. |
80 | | class Parser { |
81 | | public: |
82 | | // The user_data value is provided to the callbacks as context. |
83 | | Parser(const spv_const_context context, void* user_data, |
84 | | spv_parsed_header_fn_t parsed_header_fn, |
85 | | spv_parsed_instruction_fn_t parsed_instruction_fn) |
86 | 1.43k | : grammar_(context), |
87 | 1.43k | consumer_(context->consumer), |
88 | 1.43k | user_data_(user_data), |
89 | 1.43k | parsed_header_fn_(parsed_header_fn), |
90 | 1.43k | parsed_instruction_fn_(parsed_instruction_fn) {} |
91 | | |
92 | | // Parses the specified binary SPIR-V module, issuing callbacks on a parsed |
93 | | // header and for each parsed instruction. Returns SPV_SUCCESS on success. |
94 | | // Otherwise returns an error code and issues a diagnostic. |
95 | | spv_result_t parse(const uint32_t* words, size_t num_words, |
96 | | spv_diagnostic* diagnostic); |
97 | | |
98 | | // Sets whether to handle, rather than reject, unrecognized content: |
99 | | // unknown opcodes, unknown extended instruction numbers in semantic sets, |
100 | | // and known opcodes with unknown enum operands. When set, unknown |
101 | | // instructions are re-emitted as raw OpUnknown data instead of returning |
102 | | // an error. |
103 | 0 | void SetHandleUnknownOpcodes(bool value) { handle_unknown_opcodes_ = value; } |
104 | | |
105 | | private: |
106 | | // All remaining methods work on the current module parse state. |
107 | | |
108 | | // Like the parse method, but works on the current module parse state. |
109 | | spv_result_t parseModule(); |
110 | | |
111 | | // Parses an instruction at the current position of the binary. Assumes |
112 | | // the header has been parsed, the endian has been set, and the word index is |
113 | | // still in range. Advances the parsing position past the instruction, and |
114 | | // updates other parsing state for the current module. |
115 | | // On success, returns SPV_SUCCESS and issues the parsed-instruction callback. |
116 | | // On failure, returns an error code and issues a diagnostic. |
117 | | spv_result_t parseInstruction(); |
118 | | |
119 | | // Parses an instruction operand with the given type, for an instruction |
120 | | // starting at inst_offset words into the SPIR-V binary. |
121 | | // If the SPIR-V binary is the same endianness as the host, then the |
122 | | // endian_converted_inst_words parameter is ignored. Otherwise, this method |
123 | | // appends the words for this operand, converted to host native endianness, |
124 | | // to the end of endian_converted_inst_words. This method also updates the |
125 | | // expected_operands parameter, and the scalar members of the inst parameter. |
126 | | // On success, returns SPV_SUCCESS, advances past the operand, and pushes a |
127 | | // new entry on to the operands vector. Otherwise returns an error code and |
128 | | // issues a diagnostic. |
129 | | spv_result_t parseOperand(size_t inst_offset, spv_parsed_instruction_t* inst, |
130 | | const spv_operand_type_t type, |
131 | | std::vector<uint32_t>* endian_converted_inst_words, |
132 | | std::vector<spv_parsed_operand_t>* operands, |
133 | | spv_operand_pattern_t* expected_operands); |
134 | | |
135 | | // Records the numeric type for an operand according to the type information |
136 | | // associated with the given non-zero type Id. This can fail if the type Id |
137 | | // is not a type Id, or if the type Id does not reference a scalar numeric |
138 | | // type. On success, return SPV_SUCCESS and populates the num_words, |
139 | | // number_kind, and number_bit_width fields of parsed_operand. |
140 | | spv_result_t setNumericTypeInfoForType(spv_parsed_operand_t* parsed_operand, |
141 | | uint32_t type_id); |
142 | | |
143 | | // Records the number type for an instruction at the given offset, if that |
144 | | // instruction generates a type. For types that aren't scalar numbers, |
145 | | // record something with number kind SPV_NUMBER_NONE. |
146 | | void recordNumberType(size_t inst_offset, |
147 | | const spv_parsed_instruction_t* inst); |
148 | | |
149 | | // Returns a diagnostic stream object initialized with current position in |
150 | | // the input stream, and for the given error code. Any data written to the |
151 | | // returned object will be propagated to the current parse's diagnostic |
152 | | // object. |
153 | 6 | spvtools::DiagnosticStream diagnostic(spv_result_t error) { |
154 | 6 | return spvtools::DiagnosticStream({0, 0, _.instruction_count}, consumer_, |
155 | 6 | "", error); |
156 | 6 | } |
157 | | |
158 | | // Returns a diagnostic stream object with the default parse error code. |
159 | 6 | spvtools::DiagnosticStream diagnostic() { |
160 | | // The default failure for parsing is invalid binary. |
161 | 6 | return diagnostic(SPV_ERROR_INVALID_BINARY); |
162 | 6 | } |
163 | | |
164 | | // Issues a diagnostic describing an exhaustion of input condition when |
165 | | // trying to decode an instruction operand, and returns |
166 | | // SPV_ERROR_INVALID_BINARY. |
167 | | spv_result_t exhaustedInputDiagnostic(size_t inst_offset, spv::Op opcode, |
168 | 0 | spv_operand_type_t type) { |
169 | 0 | return diagnostic() << "End of input reached while decoding Op" |
170 | 0 | << spvOpcodeString(opcode) << " starting at word " |
171 | 0 | << inst_offset |
172 | 0 | << ((_.word_index < _.num_words) ? ": truncated " |
173 | 0 | : ": missing ") |
174 | 0 | << spvOperandTypeStr(type) << " operand at word offset " |
175 | 0 | << _.word_index - inst_offset << "."; |
176 | 0 | } |
177 | | |
178 | | // Returns the endian-corrected word at the current position. |
179 | 1.47M | uint32_t peek() const { return peekAt(_.word_index); } |
180 | | |
181 | | // Returns the endian-corrected word at the given position. |
182 | 1.47M | uint32_t peekAt(size_t index) const { |
183 | 1.47M | assert(index < _.num_words); |
184 | 1.47M | return spvFixWord(_.words[index], _.endian); |
185 | 1.47M | } |
186 | | |
187 | | // Data members |
188 | | |
189 | | const spvtools::AssemblyGrammar grammar_; // SPIR-V syntax utility. |
190 | | const spvtools::MessageConsumer& consumer_; // Message consumer callback. |
191 | | void* const user_data_; // Context for the callbacks |
192 | | const spv_parsed_header_fn_t parsed_header_fn_; // Parsed header callback |
193 | | const spv_parsed_instruction_fn_t |
194 | | parsed_instruction_fn_; // Parsed instruction callback |
195 | | // When true, unrecognized opcodes, ext inst numbers, and enum operands are |
196 | | // passed to the callback as raw OpUnknown data instead of returning an error. |
197 | | bool handle_unknown_opcodes_ = false; |
198 | | |
199 | | // Describes the format of a typed literal number. |
200 | | struct NumberType { |
201 | | spv_number_kind_t type; |
202 | | uint32_t bit_width; |
203 | | spv_fp_encoding_t encoding; |
204 | | }; |
205 | | |
206 | | // The state used to parse a single SPIR-V binary module. |
207 | | struct State { |
208 | | State(const uint32_t* words_arg, size_t num_words_arg, |
209 | | spv_diagnostic* diagnostic_arg) |
210 | 4.30k | : words(words_arg), |
211 | 4.30k | num_words(num_words_arg), |
212 | 4.30k | diagnostic(diagnostic_arg), |
213 | 4.30k | word_index(0), |
214 | 4.30k | instruction_count(0), |
215 | | endian(), |
216 | 4.30k | requires_endian_conversion(false) { |
217 | | // Temporary storage for parser state within a single instruction. |
218 | | // Most instructions require fewer than 25 words or operands. |
219 | 4.30k | operands.reserve(25); |
220 | 4.30k | endian_converted_words.reserve(25); |
221 | 4.30k | expected_operands.reserve(25); |
222 | 4.30k | } |
223 | 2.87k | State() : State(0, 0, nullptr) {} |
224 | | const uint32_t* words; // Words in the binary SPIR-V module. |
225 | | size_t num_words; // Number of words in the module. |
226 | | spv_diagnostic* diagnostic; // Where diagnostics go. |
227 | | size_t word_index; // The current position in words. |
228 | | size_t instruction_count; // The count of processed instructions |
229 | | spv_endianness_t endian; // The endianness of the binary. |
230 | | // Is the SPIR-V binary in a different endianness from the host native |
231 | | // endianness? |
232 | | bool requires_endian_conversion; |
233 | | // Set by parseOperand when LookupOperand fails for an enum operand and |
234 | | // handle_unknown_opcodes_ is set. Signals parseInstruction to discard |
235 | | // the partially-decoded instruction and re-emit it as raw OpUnknown data. |
236 | | // Cleared by parseInstruction immediately before calling emitAsUnknown. |
237 | | bool retry_instruction_as_unknown_ = false; |
238 | | |
239 | | // Maps a result ID to its type ID. By convention: |
240 | | // - a result ID that is a type definition maps to itself. |
241 | | // - a result ID without a type maps to 0. (E.g. for OpLabel) |
242 | | std::unordered_map<uint32_t, uint32_t> id_to_type_id; |
243 | | // Maps a type ID to its number type description. |
244 | | std::unordered_map<uint32_t, NumberType> type_id_to_number_type_info; |
245 | | // Maps an ExtInstImport id to the extended instruction type. |
246 | | std::unordered_map<uint32_t, spv_ext_inst_type_t> |
247 | | import_id_to_ext_inst_type; |
248 | | |
249 | | // Used by parseOperand |
250 | | std::vector<spv_parsed_operand_t> operands; |
251 | | std::vector<uint32_t> endian_converted_words; |
252 | | spv_operand_pattern_t expected_operands; |
253 | | } _; |
254 | | }; |
255 | | |
256 | | spv_result_t Parser::parse(const uint32_t* words, size_t num_words, |
257 | 1.43k | spv_diagnostic* diagnostic_arg) { |
258 | 1.43k | _ = State(words, num_words, diagnostic_arg); |
259 | | |
260 | 1.43k | const spv_result_t result = parseModule(); |
261 | | |
262 | | // Clear the module state. The tables might be big. |
263 | 1.43k | _ = State(); |
264 | | |
265 | 1.43k | return result; |
266 | 1.43k | } |
267 | | |
268 | 1.43k | spv_result_t Parser::parseModule() { |
269 | 1.43k | if (!_.words) return diagnostic() << "Missing module."; |
270 | | |
271 | 1.43k | if (_.num_words < SPV_INDEX_INSTRUCTION) |
272 | 0 | return diagnostic() << "Module has incomplete header: only " << _.num_words |
273 | 0 | << " words instead of " << SPV_INDEX_INSTRUCTION; |
274 | | |
275 | | // Check the magic number and detect the module's endianness. |
276 | 1.43k | spv_const_binary_t binary{_.words, _.num_words}; |
277 | 1.43k | if (spvBinaryEndianness(&binary, &_.endian)) { |
278 | 0 | return diagnostic() << "Invalid SPIR-V magic number '" << std::hex |
279 | 0 | << _.words[0] << "'."; |
280 | 0 | } |
281 | 1.43k | _.requires_endian_conversion = !spvIsHostEndian(_.endian); |
282 | | |
283 | | // Process the header. |
284 | 1.43k | spv_header_t header; |
285 | 1.43k | if (spvBinaryHeaderGet(&binary, _.endian, &header)) { |
286 | | // It turns out there is no way to trigger this error since the only |
287 | | // failure cases are already handled above, with better messages. |
288 | 0 | return diagnostic(SPV_ERROR_INTERNAL) |
289 | 0 | << "Internal error: unhandled header parse failure"; |
290 | 0 | } |
291 | 1.43k | if (parsed_header_fn_) { |
292 | 522 | if (auto error = parsed_header_fn_(user_data_, _.endian, header.magic, |
293 | 522 | header.version, header.generator, |
294 | 522 | header.bound, header.schema)) { |
295 | 0 | return error; |
296 | 0 | } |
297 | 522 | } |
298 | | |
299 | | // Process the instructions. |
300 | 1.43k | _.word_index = SPV_INDEX_INSTRUCTION; |
301 | 368k | while (_.word_index < _.num_words) |
302 | 367k | if (auto error = parseInstruction()) return error; |
303 | | |
304 | | // Running off the end should already have been reported earlier. |
305 | 1.43k | assert(_.word_index == _.num_words); |
306 | | |
307 | 1.04k | return SPV_SUCCESS; |
308 | 1.43k | } |
309 | | |
310 | 367k | spv_result_t Parser::parseInstruction() { |
311 | 367k | _.instruction_count++; |
312 | | |
313 | | // The zero values for all members except for opcode are the |
314 | | // correct initial values. |
315 | 367k | spv_parsed_instruction_t inst = {}; |
316 | | |
317 | 367k | const uint32_t first_word = peek(); |
318 | | |
319 | | // If the module's endianness is different from the host native endianness, |
320 | | // then converted_words contains the endian-translated words in the |
321 | | // instruction. |
322 | 367k | _.endian_converted_words.clear(); |
323 | 367k | _.endian_converted_words.push_back(first_word); |
324 | | |
325 | | // After a successful parse of the instruction, the inst.operands member |
326 | | // will point to this vector's storage. |
327 | 367k | _.operands.clear(); |
328 | | |
329 | 367k | assert(_.word_index < _.num_words); |
330 | | // Decompose and check the first word. |
331 | 367k | uint16_t inst_word_count = 0; |
332 | 367k | spvOpcodeSplit(first_word, &inst_word_count, &inst.opcode); |
333 | 367k | if (inst_word_count < 1) { |
334 | 0 | return diagnostic() << "Invalid instruction word count: " |
335 | 0 | << inst_word_count; |
336 | 0 | } |
337 | 367k | const spvtools::InstructionDesc* opcode_desc = nullptr; |
338 | 367k | const bool opcode_known = |
339 | 367k | spvtools::LookupOpcode(static_cast<spv::Op>(inst.opcode), &opcode_desc) == |
340 | 367k | SPV_SUCCESS; |
341 | 367k | if (!opcode_known && !handle_unknown_opcodes_) |
342 | 0 | return diagnostic() << "Invalid opcode: " << inst.opcode; |
343 | | |
344 | | // Advance past the opcode word. But remember the start of the instruction. |
345 | 367k | const size_t inst_offset = _.word_index; |
346 | 367k | _.word_index++; |
347 | | |
348 | | // Emits the instruction at inst_offset as raw data with no decoded operands. |
349 | 367k | auto emitAsUnknown = [&]() -> spv_result_t { |
350 | 0 | if (inst_offset + inst_word_count > _.num_words) { |
351 | 0 | return diagnostic() << "Truncated binary: instruction at word " |
352 | 0 | << inst_offset << " claims " << inst_word_count |
353 | 0 | << " words but binary ends at " << _.num_words; |
354 | 0 | } |
355 | | // Repopulate endian_converted_words from scratch. The operand loop may |
356 | | // have partially filled it before the unknown enum was detected. |
357 | 0 | _.endian_converted_words.clear(); |
358 | 0 | _.endian_converted_words.push_back(first_word); |
359 | 0 | if (_.requires_endian_conversion) { |
360 | 0 | for (uint16_t i = 1; i < inst_word_count; i++) { |
361 | 0 | _.endian_converted_words.push_back(peekAt(inst_offset + i)); |
362 | 0 | } |
363 | 0 | } |
364 | 0 | _.word_index = inst_offset + inst_word_count; |
365 | 0 | inst.words = _.requires_endian_conversion ? _.endian_converted_words.data() |
366 | 0 | : _.words + inst_offset; |
367 | 0 | inst.num_words = inst_word_count; |
368 | 0 | _.operands.clear(); |
369 | 0 | inst.operands = _.operands.data(); |
370 | 0 | inst.num_operands = 0; |
371 | 0 | if (parsed_instruction_fn_) { |
372 | 0 | if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error; |
373 | 0 | } |
374 | 0 | return SPV_SUCCESS; |
375 | 0 | }; |
376 | | |
377 | 367k | if (!opcode_known) { |
378 | 0 | return emitAsUnknown(); |
379 | 0 | } |
380 | | |
381 | | // Maintains the ordered list of expected operand types. |
382 | | // For many instructions we only need the {numTypes, operandTypes} |
383 | | // entries in opcode_desc. However, sometimes we need to modify |
384 | | // the list as we parse the operands. This occurs when an operand |
385 | | // has its own logical operands (such as the LocalSize operand for |
386 | | // ExecutionMode), or for extended instructions that may have their |
387 | | // own operands depending on the selected extended instruction. |
388 | 367k | _.expected_operands.clear(); |
389 | | |
390 | 367k | spvPushOperandTypes(opcode_desc->operands(), &_.expected_operands); |
391 | | |
392 | 1.47M | while (_.word_index < inst_offset + inst_word_count) { |
393 | 1.10M | const uint16_t inst_word_index = uint16_t(_.word_index - inst_offset); |
394 | 1.10M | if (_.expected_operands.empty()) { |
395 | 6 | return diagnostic() << "Invalid instruction Op" |
396 | 6 | << opcode_desc->name().data() << " starting at word " |
397 | 6 | << inst_offset << ": expected no more operands after " |
398 | 6 | << inst_word_index |
399 | 6 | << " words, but stated word count is " |
400 | 6 | << inst_word_count << "."; |
401 | 6 | } |
402 | | |
403 | 1.10M | spv_operand_type_t type = |
404 | 1.10M | spvTakeFirstMatchableOperand(&_.expected_operands); |
405 | | |
406 | 1.10M | if (auto error = |
407 | 1.10M | parseOperand(inst_offset, &inst, type, &_.endian_converted_words, |
408 | 1.10M | &_.operands, &_.expected_operands)) { |
409 | 0 | if (_.retry_instruction_as_unknown_) { |
410 | 0 | _.retry_instruction_as_unknown_ = false; |
411 | 0 | return emitAsUnknown(); |
412 | 0 | } |
413 | 0 | return error; |
414 | 0 | } |
415 | 1.10M | } |
416 | | |
417 | 367k | if (!_.expected_operands.empty() && |
418 | 168k | !spvOperandIsOptional(_.expected_operands.back())) { |
419 | 0 | return diagnostic() << "End of input reached while decoding Op" |
420 | 0 | << opcode_desc->name().data() << " starting at word " |
421 | 0 | << inst_offset << ": expected more operands after " |
422 | 0 | << inst_word_count << " words."; |
423 | 0 | } |
424 | | |
425 | 367k | if ((inst_offset + inst_word_count) != _.word_index) { |
426 | 0 | return diagnostic() << "Invalid word count: Op" |
427 | 0 | << opcode_desc->name().data() << " starting at word " |
428 | 0 | << inst_offset << " says it has " << inst_word_count |
429 | 0 | << " words, but found " << _.word_index - inst_offset |
430 | 0 | << " words instead."; |
431 | 0 | } |
432 | | |
433 | | // Check the computed length of the endian-converted words vector against |
434 | | // the declared number of words in the instruction. If endian conversion |
435 | | // is required, then they should match. If no endian conversion was |
436 | | // performed, then the vector only contains the initial opcode/word-count |
437 | | // word. |
438 | 367k | assert(!_.requires_endian_conversion || |
439 | 367k | (inst_word_count == _.endian_converted_words.size())); |
440 | 367k | assert(_.requires_endian_conversion || |
441 | 367k | (_.endian_converted_words.size() == 1)); |
442 | | |
443 | 367k | if (_.requires_endian_conversion) { |
444 | | // We must wait until here to set this pointer, because the vector might |
445 | | // have been be resized while we accumulated its elements. |
446 | 0 | inst.words = _.endian_converted_words.data(); |
447 | 367k | } else { |
448 | | // If no conversion is required, then just point to the underlying binary. |
449 | | // This saves time and space. |
450 | 367k | inst.words = _.words + inst_offset; |
451 | 367k | } |
452 | 367k | inst.num_words = inst_word_count; |
453 | | |
454 | 367k | recordNumberType(inst_offset, &inst); |
455 | | |
456 | | // We must wait until here to set this pointer, because the vector might |
457 | | // have been be resized while we accumulated its elements. |
458 | 367k | inst.operands = _.operands.data(); |
459 | 367k | inst.num_operands = uint16_t(_.operands.size()); |
460 | | |
461 | | // Issue the callback. The callee should know that all the storage in inst |
462 | | // is transient, and will disappear immediately afterward. |
463 | 367k | if (parsed_instruction_fn_) { |
464 | 367k | if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error; |
465 | 367k | } |
466 | | |
467 | 366k | return SPV_SUCCESS; |
468 | 367k | } |
469 | | |
470 | | spv_result_t Parser::parseOperand(size_t inst_offset, |
471 | | spv_parsed_instruction_t* inst, |
472 | | const spv_operand_type_t type, |
473 | | std::vector<uint32_t>* words, |
474 | | std::vector<spv_parsed_operand_t>* operands, |
475 | 1.10M | spv_operand_pattern_t* expected_operands) { |
476 | 1.10M | const spv::Op opcode = static_cast<spv::Op>(inst->opcode); |
477 | | // We'll fill in this result as we go along. |
478 | 1.10M | spv_parsed_operand_t parsed_operand; |
479 | 1.10M | parsed_operand.offset = uint16_t(_.word_index - inst_offset); |
480 | | // Most operands occupy one word. This might be be adjusted later. |
481 | 1.10M | parsed_operand.num_words = 1; |
482 | | // The type argument is the one used by the grammar to parse the instruction. |
483 | | // But it can exposes internal parser details such as whether an operand is |
484 | | // optional or actually represents a variable-length sequence of operands. |
485 | | // The resulting type should be adjusted to avoid those internal details. |
486 | | // In most cases, the resulting operand type is the same as the grammar type. |
487 | 1.10M | parsed_operand.type = type; |
488 | | |
489 | | // Assume non-numeric values. This will be updated for literal numbers. |
490 | 1.10M | parsed_operand.number_kind = SPV_NUMBER_NONE; |
491 | 1.10M | parsed_operand.number_bit_width = 0; |
492 | 1.10M | parsed_operand.fp_encoding = SPV_FP_ENCODING_UNKNOWN; |
493 | | |
494 | 1.10M | if (_.word_index >= _.num_words) |
495 | 0 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
496 | | |
497 | 1.10M | const uint32_t word = peek(); |
498 | | |
499 | | // Do the words in this operand have to be converted to native endianness? |
500 | | // True for all but literal strings. |
501 | 1.10M | bool convert_operand_endianness = true; |
502 | | |
503 | 1.10M | switch (type) { |
504 | 186k | case SPV_OPERAND_TYPE_TYPE_ID: |
505 | 186k | if (!word) |
506 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Type Id is 0"; |
507 | 186k | inst->type_id = word; |
508 | 186k | break; |
509 | | |
510 | 241k | case SPV_OPERAND_TYPE_RESULT_ID: |
511 | 241k | if (!word) |
512 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Result Id is 0"; |
513 | 241k | inst->result_id = word; |
514 | | // Save the result ID to type ID mapping. |
515 | | // In the grammar, type ID always appears before result ID. |
516 | 241k | if (_.id_to_type_id.find(inst->result_id) != _.id_to_type_id.end()) |
517 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) |
518 | 0 | << "Id " << inst->result_id << " is defined more than once"; |
519 | | // Record it. |
520 | | // A regular value maps to its type. Some instructions (e.g. OpLabel) |
521 | | // have no type Id, and will map to 0. The result Id for a |
522 | | // type-generating instruction (e.g. OpTypeInt) maps to itself. |
523 | 241k | _.id_to_type_id[inst->result_id] = |
524 | 241k | spvOpcodeGeneratesType(opcode) ? inst->result_id : inst->type_id; |
525 | 241k | break; |
526 | | |
527 | 388k | case SPV_OPERAND_TYPE_ID: |
528 | 462k | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
529 | 462k | if (!word) return diagnostic(SPV_ERROR_INVALID_ID) << "Id is 0"; |
530 | 462k | parsed_operand.type = SPV_OPERAND_TYPE_ID; |
531 | | |
532 | 462k | if (spvIsExtendedInstruction(opcode) && parsed_operand.offset == 3) { |
533 | | // The current word is the extended instruction set Id. |
534 | | // Set the extended instruction set type for the current instruction. |
535 | 4.46k | auto ext_inst_type_iter = _.import_id_to_ext_inst_type.find(word); |
536 | 4.46k | if (ext_inst_type_iter == _.import_id_to_ext_inst_type.end()) { |
537 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) |
538 | 0 | << "OpExtInst set Id " << word |
539 | 0 | << " does not reference an OpExtInstImport result Id"; |
540 | 0 | } |
541 | 4.46k | inst->ext_inst_type = ext_inst_type_iter->second; |
542 | 4.46k | } |
543 | 462k | break; |
544 | | |
545 | 462k | case SPV_OPERAND_TYPE_SCOPE_ID: |
546 | 6.18k | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
547 | | // Check for trivially invalid values. The operand descriptions already |
548 | | // have the word "ID" in them. |
549 | 6.18k | if (!word) return diagnostic() << spvOperandTypeStr(type) << " is 0"; |
550 | 6.18k | break; |
551 | | |
552 | 6.18k | case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: { |
553 | 4.46k | assert(spvIsExtendedInstruction(opcode)); |
554 | 4.46k | assert(inst->ext_inst_type != SPV_EXT_INST_TYPE_NONE); |
555 | | |
556 | 4.46k | const spvtools::ExtInstDesc* desc = nullptr; |
557 | 4.46k | if (spvtools::LookupExtInst(inst->ext_inst_type, word, &desc) == |
558 | 4.46k | SPV_SUCCESS) { |
559 | | // Push VARIABLE_ID so extra trailing operands from future NSDI |
560 | | // versions are silently absorbed after the instruction-specific ones. |
561 | 4.46k | if (spvExtInstIsNonSemantic(inst->ext_inst_type)) { |
562 | 0 | expected_operands->push_back(SPV_OPERAND_TYPE_VARIABLE_ID); |
563 | 0 | } |
564 | | |
565 | | // if we know about this ext inst, push the expected operands |
566 | 4.46k | spvPushOperandTypes(desc->operands(), expected_operands); |
567 | 4.46k | } else { |
568 | | // If we don't know this extended instruction and the set is semantic, |
569 | | // fail unless handle_unknown_opcodes_ is set. For non-semantic sets, |
570 | | // always continue regardless of the flag. In both non-error cases the |
571 | | // remaining operands are exposed as variable IDs. For non-semantic |
572 | | // sets the disassembler emits the instruction via its normal operand |
573 | | // loop; for semantic sets with handle_unknown_opcodes_ set, the |
574 | | // disassembler independently detects the unknown number via |
575 | | // LookupExtInst and emits the entire instruction as OpUnknown. |
576 | 0 | if (!spvExtInstIsNonSemantic(inst->ext_inst_type) && |
577 | 0 | !handle_unknown_opcodes_) { |
578 | 0 | return diagnostic() |
579 | 0 | << "Invalid extended instruction number: " << word; |
580 | 0 | } |
581 | 0 | expected_operands->push_back(SPV_OPERAND_TYPE_VARIABLE_ID); |
582 | 0 | } |
583 | 4.46k | } break; |
584 | | |
585 | 4.46k | case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: { |
586 | 294 | assert(spv::Op::OpSpecConstantOp == opcode); |
587 | 294 | if (word > static_cast<uint32_t>(spv::Op::Max) || |
588 | 294 | grammar_.lookupSpecConstantOpcode(spv::Op(word))) { |
589 | 0 | return diagnostic() |
590 | 0 | << "Invalid " << spvOperandTypeStr(type) << ": " << word; |
591 | 0 | } |
592 | 294 | const spvtools::InstructionDesc* opcode_entry = nullptr; |
593 | 294 | if (spvtools::LookupOpcode(spv::Op(word), &opcode_entry)) { |
594 | 0 | return diagnostic(SPV_ERROR_INTERNAL) |
595 | 0 | << "OpSpecConstant opcode table out of sync"; |
596 | 0 | } |
597 | | // OpSpecConstant opcodes must have a type and result. We've already |
598 | | // processed them, so skip them when preparing to parse the other |
599 | | // operants for the opcode. |
600 | 294 | assert(opcode_entry->hasType); |
601 | 294 | assert(opcode_entry->hasResult); |
602 | 294 | assert(opcode_entry->operands().size() >= 2); |
603 | 294 | spvPushOperandTypes(opcode_entry->operands().subspan(2), |
604 | 294 | expected_operands); |
605 | 294 | } break; |
606 | | |
607 | 47.1k | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
608 | 78.3k | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: |
609 | | // These are regular single-word literal integer operands. |
610 | | // Post-parsing validation should check the range of the parsed value. |
611 | 78.3k | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_INTEGER; |
612 | | // It turns out they are always unsigned integers! |
613 | 78.3k | parsed_operand.number_kind = SPV_NUMBER_UNSIGNED_INT; |
614 | 78.3k | parsed_operand.number_bit_width = 32; |
615 | 78.3k | break; |
616 | | |
617 | 0 | case SPV_OPERAND_TYPE_LITERAL_FLOAT: |
618 | | // These are regular single-word literal float operands. |
619 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_FLOAT; |
620 | 0 | parsed_operand.number_kind = SPV_NUMBER_FLOATING; |
621 | 0 | parsed_operand.number_bit_width = 32; |
622 | 0 | break; |
623 | | |
624 | 12.5k | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
625 | 15.0k | case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
626 | 15.0k | parsed_operand.type = SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER; |
627 | 15.0k | if (opcode == spv::Op::OpSwitch) { |
628 | | // The literal operands have the same type as the value |
629 | | // referenced by the selector Id. |
630 | 2.49k | const uint32_t selector_id = peekAt(inst_offset + 1); |
631 | 2.49k | const auto type_id_iter = _.id_to_type_id.find(selector_id); |
632 | 2.49k | if (type_id_iter == _.id_to_type_id.end() || |
633 | 2.49k | type_id_iter->second == 0) { |
634 | 0 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
635 | 0 | << " has no type"; |
636 | 0 | } |
637 | 2.49k | uint32_t type_id = type_id_iter->second; |
638 | | |
639 | 2.49k | if (selector_id == type_id) { |
640 | | // Recall that by convention, a result ID that is a type definition |
641 | | // maps to itself. |
642 | 0 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
643 | 0 | << " is a type, not a value"; |
644 | 0 | } |
645 | 2.49k | if (auto error = setNumericTypeInfoForType(&parsed_operand, type_id)) |
646 | 0 | return error; |
647 | 2.49k | if (parsed_operand.number_kind != SPV_NUMBER_UNSIGNED_INT && |
648 | 1.94k | parsed_operand.number_kind != SPV_NUMBER_SIGNED_INT) { |
649 | 0 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
650 | 0 | << " is not a scalar integer"; |
651 | 0 | } |
652 | 12.5k | } else { |
653 | 12.5k | assert(opcode == spv::Op::OpConstant || |
654 | 12.5k | opcode == spv::Op::OpSpecConstant); |
655 | | // The literal number type is determined by the type Id for the |
656 | | // constant. |
657 | 12.5k | assert(inst->type_id); |
658 | 12.5k | if (auto error = |
659 | 12.5k | setNumericTypeInfoForType(&parsed_operand, inst->type_id)) |
660 | 0 | return error; |
661 | 12.5k | } |
662 | 15.0k | break; |
663 | | |
664 | 35.0k | case SPV_OPERAND_TYPE_LITERAL_STRING: |
665 | 35.0k | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
666 | 35.0k | const size_t max_words = _.num_words - _.word_index; |
667 | 35.0k | std::string string = |
668 | 35.0k | spvtools::utils::MakeString(_.words + _.word_index, max_words, false); |
669 | | |
670 | 35.0k | if (string.length() == max_words * 4) |
671 | 0 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
672 | | |
673 | | // Make sure we can record the word count without overflow. |
674 | | // |
675 | | // This error can't currently be triggered because of validity |
676 | | // checks elsewhere. |
677 | 35.0k | const size_t string_num_words = string.length() / 4 + 1; |
678 | 35.0k | if (string_num_words > std::numeric_limits<uint16_t>::max()) { |
679 | 0 | return diagnostic() << "Literal string is longer than " |
680 | 0 | << std::numeric_limits<uint16_t>::max() |
681 | 0 | << " words: " << string_num_words << " words long"; |
682 | 0 | } |
683 | 35.0k | parsed_operand.num_words = uint16_t(string_num_words); |
684 | 35.0k | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_STRING; |
685 | | |
686 | 35.0k | if (spv::Op::OpExtInstImport == opcode) { |
687 | | // Record the extended instruction type for the ID for this import. |
688 | | // There is only one string literal argument to OpExtInstImport, |
689 | | // so it's sufficient to guard this just on the opcode. |
690 | 1.41k | const spv_ext_inst_type_t ext_inst_type = |
691 | 1.41k | spvExtInstImportTypeGet(string.c_str()); |
692 | 1.41k | if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) { |
693 | 0 | return diagnostic() |
694 | 0 | << "Invalid extended instruction import '" << string << "'"; |
695 | 0 | } |
696 | | // We must have parsed a valid result ID. It's a condition |
697 | | // of the grammar, and we only accept non-zero result Ids. |
698 | 1.41k | assert(inst->result_id); |
699 | 1.41k | _.import_id_to_ext_inst_type[inst->result_id] = ext_inst_type; |
700 | 1.41k | } |
701 | 35.0k | } break; |
702 | | |
703 | 35.0k | case SPV_OPERAND_TYPE_CAPABILITY: |
704 | 3.91k | case SPV_OPERAND_TYPE_OPTIONAL_CAPABILITY: |
705 | 4.96k | case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
706 | 6.02k | case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
707 | 7.08k | case SPV_OPERAND_TYPE_MEMORY_MODEL: |
708 | 7.08k | case SPV_OPERAND_TYPE_EXECUTION_MODE: |
709 | 37.3k | case SPV_OPERAND_TYPE_STORAGE_CLASS: |
710 | 39.2k | case SPV_OPERAND_TYPE_DIMENSIONALITY: |
711 | 39.2k | case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
712 | 39.2k | case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
713 | 41.1k | case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
714 | 41.1k | case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
715 | 41.1k | case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
716 | 41.1k | case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
717 | 41.1k | case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER: |
718 | 41.1k | case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
719 | 60.7k | case SPV_OPERAND_TYPE_DECORATION: |
720 | 60.8k | case SPV_OPERAND_TYPE_BUILT_IN: |
721 | 60.8k | case SPV_OPERAND_TYPE_GROUP_OPERATION: |
722 | 60.8k | case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
723 | 60.8k | case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
724 | 60.8k | case SPV_OPERAND_TYPE_RAY_FLAGS: |
725 | 60.8k | case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION: |
726 | 60.8k | case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE: |
727 | 60.8k | case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE: |
728 | 60.8k | case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
729 | 60.8k | case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
730 | 60.8k | case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
731 | 60.8k | case SPV_OPERAND_TYPE_DEBUG_OPERATION: |
732 | 60.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
733 | 60.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE: |
734 | 60.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER: |
735 | 60.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION: |
736 | 60.8k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY: |
737 | 60.8k | case SPV_OPERAND_TYPE_FPDENORM_MODE: |
738 | 60.8k | case SPV_OPERAND_TYPE_FPOPERATION_MODE: |
739 | 60.8k | case SPV_OPERAND_TYPE_QUANTIZATION_MODES: |
740 | 60.8k | case SPV_OPERAND_TYPE_OVERFLOW_MODES: |
741 | 60.8k | case SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT: |
742 | 61.0k | case SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT: |
743 | 61.0k | case SPV_OPERAND_TYPE_FPENCODING: |
744 | 61.0k | case SPV_OPERAND_TYPE_OPTIONAL_FPENCODING: |
745 | 61.0k | case SPV_OPERAND_TYPE_HOST_ACCESS_QUALIFIER: |
746 | 61.0k | case SPV_OPERAND_TYPE_LOAD_CACHE_CONTROL: |
747 | 61.0k | case SPV_OPERAND_TYPE_STORE_CACHE_CONTROL: |
748 | 61.0k | case SPV_OPERAND_TYPE_NAMED_MAXIMUM_NUMBER_OF_REGISTERS: |
749 | 61.0k | case SPV_OPERAND_TYPE_GATHER_MODES: { |
750 | | // A single word that is a plain enum value. |
751 | | |
752 | | // Map an optional operand type to its corresponding concrete type. |
753 | 61.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER) |
754 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_ACCESS_QUALIFIER; |
755 | 61.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT) |
756 | 250 | parsed_operand.type = SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT; |
757 | 61.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_FPENCODING) |
758 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_FPENCODING; |
759 | 61.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_CAPABILITY) |
760 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_CAPABILITY; |
761 | | |
762 | 61.0k | const spvtools::OperandDesc* entry = nullptr; |
763 | 61.0k | if (spvtools::LookupOperand(type, word, &entry)) { |
764 | 0 | if (handle_unknown_opcodes_) _.retry_instruction_as_unknown_ = true; |
765 | 0 | return diagnostic() |
766 | 0 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
767 | 0 | << " operand: " << word; |
768 | 0 | } |
769 | | // Prepare to accept operands to this operand, if needed. |
770 | 61.0k | spvPushOperandTypes(entry->operands(), expected_operands); |
771 | 61.0k | } break; |
772 | | |
773 | 772 | case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: { |
774 | 772 | const spvtools::OperandDesc* entry = nullptr; |
775 | 772 | if (spvtools::LookupOperand(type, word, &entry)) { |
776 | 0 | if (handle_unknown_opcodes_) _.retry_instruction_as_unknown_ = true; |
777 | 0 | return diagnostic() |
778 | 0 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
779 | 0 | << " operand: " << word |
780 | 0 | << ", if you are creating a new source language please use " |
781 | 0 | "value 0 " |
782 | 0 | "(Unknown) and when ready, add your source language to " |
783 | 0 | "SPIRV-Headers"; |
784 | 0 | } |
785 | | // Prepare to accept operands to this operand, if needed. |
786 | 772 | spvPushOperandTypes(entry->operands(), expected_operands); |
787 | 772 | } break; |
788 | | |
789 | 0 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
790 | 2.04k | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
791 | 4.15k | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
792 | 5.85k | case SPV_OPERAND_TYPE_IMAGE: |
793 | 6.07k | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
794 | 6.07k | case SPV_OPERAND_TYPE_MEMORY_ACCESS: |
795 | 6.07k | case SPV_OPERAND_TYPE_TENSOR_OPERANDS: |
796 | 6.07k | case SPV_OPERAND_TYPE_OPTIONAL_TENSOR_OPERANDS: |
797 | 6.19k | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
798 | 6.19k | case SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS: |
799 | 12.1k | case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
800 | 12.1k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS: |
801 | 12.1k | case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: |
802 | 12.1k | case SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_OPERANDS: |
803 | 12.1k | case SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS: |
804 | 12.1k | case SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_REDUCE: |
805 | 12.1k | case SPV_OPERAND_TYPE_TENSOR_ADDRESSING_OPERANDS: |
806 | 12.1k | case SPV_OPERAND_TYPE_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS: |
807 | 12.1k | case SPV_OPERAND_TYPE_OPTIONAL_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS: { |
808 | | // This operand is a mask. |
809 | | |
810 | | // Map an optional operand type to its corresponding concrete type. |
811 | 12.1k | if (type == SPV_OPERAND_TYPE_OPTIONAL_IMAGE) |
812 | 220 | parsed_operand.type = SPV_OPERAND_TYPE_IMAGE; |
813 | 12.1k | if (type == SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS) |
814 | 126 | parsed_operand.type = SPV_OPERAND_TYPE_MEMORY_ACCESS; |
815 | 12.1k | if (type == SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS) |
816 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_OPERANDS; |
817 | 12.1k | if (type == SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS) |
818 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_RAW_ACCESS_CHAIN_OPERANDS; |
819 | 12.1k | if (type == SPV_OPERAND_TYPE_OPTIONAL_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS) |
820 | 0 | parsed_operand.type = |
821 | 0 | SPV_OPERAND_TYPE_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS; |
822 | 12.1k | if (type == SPV_OPERAND_TYPE_OPTIONAL_TENSOR_OPERANDS) |
823 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_TENSOR_OPERANDS; |
824 | | |
825 | | // Check validity of set mask bits. Also prepare for operands for those |
826 | | // masks if they have any. To get operand order correct, scan from |
827 | | // MSB to LSB since we can only prepend operands to a pattern. |
828 | | // The only case in the grammar where you have more than one mask bit |
829 | | // having an operand is for image operands. See SPIR-V 3.14 Image |
830 | | // Operands. |
831 | 12.1k | uint32_t remaining_word = word; |
832 | 82.7k | for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1) { |
833 | 70.6k | if (remaining_word & mask) { |
834 | 2.32k | const spvtools::OperandDesc* entry = nullptr; |
835 | 2.32k | if (spvtools::LookupOperand(type, mask, &entry)) { |
836 | 0 | if (handle_unknown_opcodes_) _.retry_instruction_as_unknown_ = true; |
837 | 0 | return diagnostic() |
838 | 0 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
839 | 0 | << " operand: " << word << " has invalid mask component " |
840 | 0 | << mask; |
841 | 0 | } |
842 | 2.32k | remaining_word ^= mask; |
843 | 2.32k | spvPushOperandTypes(entry->operands(), expected_operands); |
844 | 2.32k | } |
845 | 70.6k | } |
846 | 12.1k | if (word == 0) { |
847 | | // An all-zeroes mask *might* also be valid. |
848 | 9.84k | const spvtools::OperandDesc* entry = nullptr; |
849 | 9.84k | if (SPV_SUCCESS == spvtools::LookupOperand(type, 0, &entry)) { |
850 | | // Prepare for its operands, if any. |
851 | 9.84k | spvPushOperandTypes(entry->operands(), expected_operands); |
852 | 9.84k | } |
853 | 9.84k | } |
854 | 12.1k | } break; |
855 | 0 | default: |
856 | 0 | return diagnostic() << "Internal error: Unhandled operand type: " << type; |
857 | 1.10M | } |
858 | | |
859 | 1.10M | assert(spvOperandIsConcrete(parsed_operand.type)); |
860 | | |
861 | 1.10M | operands->push_back(parsed_operand); |
862 | | |
863 | 1.10M | const size_t index_after_operand = _.word_index + parsed_operand.num_words; |
864 | | |
865 | | // Avoid buffer overrun for the cases where the operand has more than one |
866 | | // word, and where it isn't a string. (Those other cases have already been |
867 | | // handled earlier.) For example, this error can occur for a multi-word |
868 | | // argument to OpConstant, or a multi-word case literal operand for OpSwitch. |
869 | 1.10M | if (_.num_words < index_after_operand) |
870 | 0 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
871 | | |
872 | 1.10M | if (_.requires_endian_conversion) { |
873 | | // Copy instruction words. Translate to native endianness as needed. |
874 | 0 | if (convert_operand_endianness) { |
875 | 0 | const spv_endianness_t endianness = _.endian; |
876 | 0 | std::transform(_.words + _.word_index, _.words + index_after_operand, |
877 | 0 | std::back_inserter(*words), |
878 | 0 | [endianness](const uint32_t raw_word) { |
879 | 0 | return spvFixWord(raw_word, endianness); |
880 | 0 | }); |
881 | 0 | } else { |
882 | 0 | words->insert(words->end(), _.words + _.word_index, |
883 | 0 | _.words + index_after_operand); |
884 | 0 | } |
885 | 0 | } |
886 | | |
887 | | // Advance past the operand. |
888 | 1.10M | _.word_index = index_after_operand; |
889 | | |
890 | 1.10M | return SPV_SUCCESS; |
891 | 1.10M | } |
892 | | |
893 | | spv_result_t Parser::setNumericTypeInfoForType( |
894 | 15.0k | spv_parsed_operand_t* parsed_operand, uint32_t type_id) { |
895 | 15.0k | assert(type_id != 0); |
896 | 15.0k | auto type_info_iter = _.type_id_to_number_type_info.find(type_id); |
897 | 15.0k | if (type_info_iter == _.type_id_to_number_type_info.end()) { |
898 | 0 | return diagnostic() << "Type Id " << type_id << " is not a type"; |
899 | 0 | } |
900 | 15.0k | const NumberType& info = type_info_iter->second; |
901 | 15.0k | if (info.type == SPV_NUMBER_NONE) { |
902 | | // This is a valid type, but for something other than a scalar number. |
903 | 0 | return diagnostic() << "Type Id " << type_id |
904 | 0 | << " is not a scalar numeric type"; |
905 | 0 | } |
906 | | |
907 | 15.0k | parsed_operand->number_kind = info.type; |
908 | 15.0k | parsed_operand->number_bit_width = info.bit_width; |
909 | 15.0k | parsed_operand->fp_encoding = info.encoding; |
910 | | // Round up the word count. |
911 | 15.0k | parsed_operand->num_words = static_cast<uint16_t>((info.bit_width + 31) / 32); |
912 | 15.0k | return SPV_SUCCESS; |
913 | 15.0k | } |
914 | | |
915 | | void Parser::recordNumberType(size_t inst_offset, |
916 | 367k | const spv_parsed_instruction_t* inst) { |
917 | 367k | const spv::Op opcode = static_cast<spv::Op>(inst->opcode); |
918 | 367k | if (spvOpcodeGeneratesType(opcode)) { |
919 | 28.2k | NumberType info = {SPV_NUMBER_NONE, 0}; |
920 | 28.2k | if (spv::Op::OpTypeInt == opcode) { |
921 | 2.37k | const bool is_signed = peekAt(inst_offset + 3) != 0; |
922 | 2.37k | info.type = is_signed ? SPV_NUMBER_SIGNED_INT : SPV_NUMBER_UNSIGNED_INT; |
923 | 2.37k | info.bit_width = peekAt(inst_offset + 2); |
924 | 25.8k | } else if (spv::Op::OpTypeFloat == opcode) { |
925 | 956 | info.type = SPV_NUMBER_FLOATING; |
926 | 956 | info.bit_width = peekAt(inst_offset + 2); |
927 | 956 | if (inst->num_words >= 4) { |
928 | 0 | const spvtools::OperandDesc* desc = nullptr; |
929 | 0 | spv_result_t status = spvtools::LookupOperand( |
930 | 0 | SPV_OPERAND_TYPE_FPENCODING, peekAt(inst_offset + 3), &desc); |
931 | 0 | if (status == SPV_SUCCESS) { |
932 | 0 | info.encoding = spvFPEncodingFromOperandFPEncoding( |
933 | 0 | static_cast<spv::FPEncoding>(desc->value)); |
934 | 0 | } else { |
935 | 0 | info.encoding = SPV_FP_ENCODING_UNKNOWN; |
936 | 0 | } |
937 | 0 | } |
938 | 956 | } |
939 | | // The *result* Id of a type generating instruction is the type Id. |
940 | 28.2k | _.type_id_to_number_type_info[inst->result_id] = info; |
941 | 28.2k | } |
942 | 367k | } |
943 | | |
944 | | } // anonymous namespace |
945 | | |
946 | | spv_result_t spvBinaryParse(const spv_const_context context, void* user_data, |
947 | | const uint32_t* code, const size_t num_words, |
948 | | spv_parsed_header_fn_t parsed_header, |
949 | | spv_parsed_instruction_fn_t parsed_instruction, |
950 | 960 | spv_diagnostic* diagnostic) { |
951 | 960 | return spvBinaryParseWithOptions(context, user_data, code, num_words, |
952 | 960 | parsed_header, parsed_instruction, |
953 | 960 | diagnostic, 0); |
954 | 960 | } |
955 | | |
956 | | spv_result_t spvBinaryParseWithOptions( |
957 | | const spv_const_context context, void* user_data, const uint32_t* code, |
958 | | const size_t num_words, spv_parsed_header_fn_t parsed_header, |
959 | | spv_parsed_instruction_fn_t parsed_instruction, spv_diagnostic* diagnostic, |
960 | 1.43k | uint32_t options) { |
961 | 1.43k | spv_context_t hijack_context = *context; |
962 | 1.43k | if (diagnostic) { |
963 | 576 | *diagnostic = nullptr; |
964 | 576 | spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, diagnostic); |
965 | 576 | } |
966 | 1.43k | Parser parser(&hijack_context, user_data, parsed_header, parsed_instruction); |
967 | 1.43k | if (options & SPV_BINARY_TO_TEXT_OPTION_HANDLE_UNKNOWN_OPCODES) { |
968 | 0 | parser.SetHandleUnknownOpcodes(true); |
969 | 0 | } |
970 | 1.43k | return parser.parse(code, num_words, diagnostic); |
971 | 1.43k | } |
972 | | |
973 | | // TODO(dneto): This probably belongs in text.cpp since that's the only place |
974 | | // that a spv_binary_t value is created. |
975 | 0 | void spvBinaryDestroy(spv_binary binary) { |
976 | 0 | if (binary) { |
977 | 0 | if (binary->code) delete[] binary->code; |
978 | 0 | delete binary; |
979 | 0 | } |
980 | 0 | } |
981 | | |
982 | 0 | size_t spv_strnlen_s(const char* str, size_t strsz) { |
983 | 0 | if (!str) return 0; |
984 | 0 | for (size_t i = 0; i < strsz; i++) { |
985 | 0 | if (!str[i]) return i; |
986 | 0 | } |
987 | 0 | return strsz; |
988 | 0 | } |