/home/runner/work/DirectXShaderCompiler/DirectXShaderCompiler/external/SPIRV-Tools/source/binary.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 22.1k | spv_header_t* pHeader) { |
42 | 22.1k | if (!binary->code) return SPV_ERROR_INVALID_BINARY0 ; |
43 | 22.1k | if (binary->wordCount < SPV_INDEX_INSTRUCTION) |
44 | 0 | return SPV_ERROR_INVALID_BINARY; |
45 | 22.1k | if (!pHeader) return SPV_ERROR_INVALID_POINTER0 ; |
46 | | |
47 | | // TODO: Validation checking? |
48 | 22.1k | pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian); |
49 | 22.1k | pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian); |
50 | | // Per 2.3.1 version's high and low bytes are 0 |
51 | 22.1k | 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 | 22.1k | if (pHeader->version < SPV_SPIRV_VERSION_WORD(1, 0) || |
55 | 22.1k | pHeader->version > SPV_VERSION) |
56 | 0 | return SPV_ERROR_INVALID_BINARY; |
57 | | |
58 | 22.1k | pHeader->generator = |
59 | 22.1k | spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian); |
60 | 22.1k | pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian); |
61 | 22.1k | pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian); |
62 | 22.1k | pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION]; |
63 | | |
64 | 22.1k | return SPV_SUCCESS; |
65 | 22.1k | } |
66 | | |
67 | | std::string spvDecodeLiteralStringOperand(const spv_parsed_instruction_t& inst, |
68 | 184k | const uint16_t operand_index) { |
69 | 184k | assert(operand_index < inst.num_operands); |
70 | 184k | const spv_parsed_operand_t& operand = inst.operands[operand_index]; |
71 | | |
72 | 184k | return spvtools::utils::MakeString(inst.words + operand.offset, |
73 | 184k | operand.num_words); |
74 | 184k | } |
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 | 19.2k | : grammar_(context), |
87 | 19.2k | consumer_(context->consumer), |
88 | 19.2k | user_data_(user_data), |
89 | 19.2k | parsed_header_fn_(parsed_header_fn), |
90 | 19.2k | 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 | 7.00M | uint32_t peek() const { return peekAt(_.word_index); } |
180 | | |
181 | | // Returns the endian-corrected word at the given position. |
182 | 7.07M | uint32_t peekAt(size_t index) const { |
183 | 7.07M | assert(index < _.num_words); |
184 | 7.07M | return spvFixWord(_.words[index], _.endian); |
185 | 7.07M | } |
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 | 57.6k | : words(words_arg), |
211 | 57.6k | num_words(num_words_arg), |
212 | 57.6k | diagnostic(diagnostic_arg), |
213 | 57.6k | word_index(0), |
214 | 57.6k | instruction_count(0), |
215 | | endian(), |
216 | 57.6k | 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 | 57.6k | operands.reserve(25); |
220 | 57.6k | endian_converted_words.reserve(25); |
221 | 57.6k | expected_operands.reserve(25); |
222 | 57.6k | } |
223 | 38.4k | 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 | 19.2k | spv_diagnostic* diagnostic_arg) { |
258 | 19.2k | _ = State(words, num_words, diagnostic_arg); |
259 | | |
260 | 19.2k | const spv_result_t result = parseModule(); |
261 | | |
262 | | // Clear the module state. The tables might be big. |
263 | 19.2k | _ = State(); |
264 | | |
265 | 19.2k | return result; |
266 | 19.2k | } |
267 | | |
268 | 19.2k | spv_result_t Parser::parseModule() { |
269 | 19.2k | if (!_.words) return diagnostic() << "Missing module."0 ; |
270 | | |
271 | 19.2k | 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 | 19.2k | spv_const_binary_t binary{_.words, _.num_words}; |
277 | 19.2k | if (spvBinaryEndianness(&binary, &_.endian)) { |
278 | 0 | return diagnostic() << "Invalid SPIR-V magic number '" << std::hex |
279 | 0 | << _.words[0] << "'."; |
280 | 0 | } |
281 | 19.2k | _.requires_endian_conversion = !spvIsHostEndian(_.endian); |
282 | | |
283 | | // Process the header. |
284 | 19.2k | spv_header_t header; |
285 | 19.2k | 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 | 19.2k | if (parsed_header_fn_) { |
292 | 7.38k | if (auto error = parsed_header_fn_(user_data_, _.endian, header.magic, |
293 | 7.38k | header.version, header.generator, |
294 | 7.38k | header.bound, header.schema)) { |
295 | 0 | return error; |
296 | 0 | } |
297 | 7.38k | } |
298 | | |
299 | | // Process the instructions. |
300 | 19.2k | _.word_index = SPV_INDEX_INSTRUCTION; |
301 | 1.96M | while (_.word_index < _.num_words) |
302 | 1.94M | if (auto error = parseInstruction()) return error2.95k ; |
303 | | |
304 | | // Running off the end should already have been reported earlier. |
305 | 16.2k | assert(_.word_index == _.num_words); |
306 | | |
307 | 16.2k | return SPV_SUCCESS; |
308 | 19.2k | } |
309 | | |
310 | 1.94M | spv_result_t Parser::parseInstruction() { |
311 | 1.94M | _.instruction_count++; |
312 | | |
313 | | // The zero values for all members except for opcode are the |
314 | | // correct initial values. |
315 | 1.94M | spv_parsed_instruction_t inst = {}; |
316 | | |
317 | 1.94M | 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 | 1.94M | _.endian_converted_words.clear(); |
323 | 1.94M | _.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 | 1.94M | _.operands.clear(); |
328 | | |
329 | 1.94M | assert(_.word_index < _.num_words); |
330 | | // Decompose and check the first word. |
331 | 1.94M | uint16_t inst_word_count = 0; |
332 | 1.94M | spvOpcodeSplit(first_word, &inst_word_count, &inst.opcode); |
333 | 1.94M | if (inst_word_count < 1) { |
334 | 0 | return diagnostic() << "Invalid instruction word count: " |
335 | 0 | << inst_word_count; |
336 | 0 | } |
337 | 1.94M | const spvtools::InstructionDesc* opcode_desc = nullptr; |
338 | 1.94M | const bool opcode_known = |
339 | 1.94M | spvtools::LookupOpcode(static_cast<spv::Op>(inst.opcode), &opcode_desc) == |
340 | 1.94M | SPV_SUCCESS; |
341 | 1.94M | if (!opcode_known && !handle_unknown_opcodes_0 ) |
342 | 0 | return diagnostic() << "Invalid opcode: " << inst.opcode; |
343 | | |
344 | | // Advance past the opcode word. But remember the start of the instruction. |
345 | 1.94M | const size_t inst_offset = _.word_index; |
346 | 1.94M | _.word_index++; |
347 | | |
348 | | // Emits the instruction at inst_offset as raw data with no decoded operands. |
349 | 1.94M | 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 | 1.94M | 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 | 1.94M | _.expected_operands.clear(); |
389 | | |
390 | 1.94M | spvPushOperandTypes(opcode_desc->operands(), &_.expected_operands); |
391 | | |
392 | 7.00M | while (_.word_index < inst_offset + inst_word_count) { |
393 | 5.06M | const uint16_t inst_word_index = uint16_t(_.word_index - inst_offset); |
394 | 5.06M | if (_.expected_operands.empty()) { |
395 | 0 | return diagnostic() << "Invalid instruction Op" |
396 | 0 | << opcode_desc->name().data() << " starting at word " |
397 | 0 | << inst_offset << ": expected no more operands after " |
398 | 0 | << inst_word_index |
399 | 0 | << " words, but stated word count is " |
400 | 0 | << inst_word_count << "."; |
401 | 0 | } |
402 | | |
403 | 5.06M | spv_operand_type_t type = |
404 | 5.06M | spvTakeFirstMatchableOperand(&_.expected_operands); |
405 | | |
406 | 5.06M | if (auto error = |
407 | 5.06M | parseOperand(inst_offset, &inst, type, &_.endian_converted_words, |
408 | 5.06M | &_.operands, &_.expected_operands)) { |
409 | 6 | if (_.retry_instruction_as_unknown_) { |
410 | 0 | _.retry_instruction_as_unknown_ = false; |
411 | 0 | return emitAsUnknown(); |
412 | 0 | } |
413 | 6 | return error; |
414 | 6 | } |
415 | 5.06M | } |
416 | | |
417 | 1.94M | if (!_.expected_operands.empty() && |
418 | 1.94M | !spvOperandIsOptional(_.expected_operands.back())601k ) { |
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 | 1.94M | 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 | 1.94M | assert(!_.requires_endian_conversion || |
439 | 1.94M | (inst_word_count == _.endian_converted_words.size())); |
440 | 1.94M | assert(_.requires_endian_conversion || |
441 | 1.94M | (_.endian_converted_words.size() == 1)); |
442 | | |
443 | 1.94M | 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 | 1.94M | } else { |
448 | | // If no conversion is required, then just point to the underlying binary. |
449 | | // This saves time and space. |
450 | 1.94M | inst.words = _.words + inst_offset; |
451 | 1.94M | } |
452 | 1.94M | inst.num_words = inst_word_count; |
453 | | |
454 | 1.94M | 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 | 1.94M | inst.operands = _.operands.data(); |
459 | 1.94M | 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 | 1.94M | if (parsed_instruction_fn_) { |
464 | 1.94M | if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error2.95k ; |
465 | 1.94M | } |
466 | | |
467 | 1.94M | return SPV_SUCCESS; |
468 | 1.94M | } |
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 | 5.06M | spv_operand_pattern_t* expected_operands) { |
476 | 5.06M | const spv::Op opcode = static_cast<spv::Op>(inst->opcode); |
477 | | // We'll fill in this result as we go along. |
478 | 5.06M | spv_parsed_operand_t parsed_operand; |
479 | 5.06M | parsed_operand.offset = uint16_t(_.word_index - inst_offset); |
480 | | // Most operands occupy one word. This might be be adjusted later. |
481 | 5.06M | 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 | 5.06M | parsed_operand.type = type; |
488 | | |
489 | | // Assume non-numeric values. This will be updated for literal numbers. |
490 | 5.06M | parsed_operand.number_kind = SPV_NUMBER_NONE; |
491 | 5.06M | parsed_operand.number_bit_width = 0; |
492 | | |
493 | 5.06M | if (_.word_index >= _.num_words) |
494 | 0 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
495 | | |
496 | 5.06M | const uint32_t word = peek(); |
497 | | |
498 | | // Do the words in this operand have to be converted to native endianness? |
499 | | // True for all but literal strings. |
500 | 5.06M | bool convert_operand_endianness = true; |
501 | | |
502 | 5.06M | switch (type) { |
503 | 650k | case SPV_OPERAND_TYPE_TYPE_ID: |
504 | 650k | if (!word) |
505 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Type Id is 0"; |
506 | 650k | inst->type_id = word; |
507 | 650k | break; |
508 | | |
509 | 982k | case SPV_OPERAND_TYPE_RESULT_ID: |
510 | 982k | if (!word) |
511 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Result Id is 0"; |
512 | 982k | inst->result_id = word; |
513 | | // Save the result ID to type ID mapping. |
514 | | // In the grammar, type ID always appears before result ID. |
515 | 982k | if (_.id_to_type_id.find(inst->result_id) != _.id_to_type_id.end()) |
516 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) |
517 | 0 | << "Id " << inst->result_id << " is defined more than once"; |
518 | | // Record it. |
519 | | // A regular value maps to its type. Some instructions (e.g. OpLabel) |
520 | | // have no type Id, and will map to 0. The result Id for a |
521 | | // type-generating instruction (e.g. OpTypeInt) maps to itself. |
522 | 982k | _.id_to_type_id[inst->result_id] = |
523 | 982k | spvOpcodeGeneratesType(opcode) ? inst->result_id271k : inst->type_id710k ; |
524 | 982k | break; |
525 | | |
526 | 1.48M | case SPV_OPERAND_TYPE_ID: |
527 | 1.77M | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
528 | 1.77M | if (!word) return diagnostic(SPV_ERROR_INVALID_ID) << "Id is 0"0 ; |
529 | 1.77M | parsed_operand.type = SPV_OPERAND_TYPE_ID; |
530 | | |
531 | 1.77M | if (spvIsExtendedInstruction(opcode) && parsed_operand.offset == 3148k ) { |
532 | | // The current word is the extended instruction set Id. |
533 | | // Set the extended instruction set type for the current instruction. |
534 | 34.4k | auto ext_inst_type_iter = _.import_id_to_ext_inst_type.find(word); |
535 | 34.4k | if (ext_inst_type_iter == _.import_id_to_ext_inst_type.end()) { |
536 | 0 | return diagnostic(SPV_ERROR_INVALID_ID) |
537 | 0 | << "OpExtInst set Id " << word |
538 | 0 | << " does not reference an OpExtInstImport result Id"; |
539 | 0 | } |
540 | 34.4k | inst->ext_inst_type = ext_inst_type_iter->second; |
541 | 34.4k | } |
542 | 1.77M | break; |
543 | | |
544 | 1.77M | case SPV_OPERAND_TYPE_SCOPE_ID: |
545 | 11.2k | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
546 | | // Check for trivially invalid values. The operand descriptions already |
547 | | // have the word "ID" in them. |
548 | 11.2k | if (!word) return diagnostic() << spvOperandTypeStr(type) << " is 0"0 ; |
549 | 11.2k | break; |
550 | | |
551 | 34.4k | case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: { |
552 | 34.4k | assert(spvIsExtendedInstruction(opcode)); |
553 | 34.4k | assert(inst->ext_inst_type != SPV_EXT_INST_TYPE_NONE); |
554 | | |
555 | 34.4k | const spvtools::ExtInstDesc* desc = nullptr; |
556 | 34.4k | if (spvtools::LookupExtInst(inst->ext_inst_type, word, &desc) == |
557 | 34.4k | SPV_SUCCESS) { |
558 | | // Push VARIABLE_ID so extra trailing operands from future NSDI |
559 | | // versions are silently absorbed after the instruction-specific ones. |
560 | 34.3k | if (spvExtInstIsNonSemantic(inst->ext_inst_type)) { |
561 | 18.0k | expected_operands->push_back(SPV_OPERAND_TYPE_VARIABLE_ID); |
562 | 18.0k | } |
563 | | |
564 | | // if we know about this ext inst, push the expected operands |
565 | 34.3k | spvPushOperandTypes(desc->operands(), expected_operands); |
566 | 34.3k | } else { |
567 | | // If we don't know this extended instruction and the set is semantic, |
568 | | // fail unless handle_unknown_opcodes_ is set. For non-semantic sets, |
569 | | // always continue regardless of the flag. In both non-error cases the |
570 | | // remaining operands are exposed as variable IDs. For non-semantic |
571 | | // sets the disassembler emits the instruction via its normal operand |
572 | | // loop; for semantic sets with handle_unknown_opcodes_ set, the |
573 | | // disassembler independently detects the unknown number via |
574 | | // LookupExtInst and emits the entire instruction as OpUnknown. |
575 | 98 | if (!spvExtInstIsNonSemantic(inst->ext_inst_type) && |
576 | 98 | !handle_unknown_opcodes_0 ) { |
577 | 0 | return diagnostic() |
578 | 0 | << "Invalid extended instruction number: " << word; |
579 | 0 | } |
580 | 98 | expected_operands->push_back(SPV_OPERAND_TYPE_VARIABLE_ID); |
581 | 98 | } |
582 | 34.4k | } break; |
583 | | |
584 | 34.4k | case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: { |
585 | 10 | assert(spv::Op::OpSpecConstantOp == opcode); |
586 | 10 | if (word > static_cast<uint32_t>(spv::Op::Max) || |
587 | 10 | grammar_.lookupSpecConstantOpcode(spv::Op(word))) { |
588 | 0 | return diagnostic() |
589 | 0 | << "Invalid " << spvOperandTypeStr(type) << ": " << word; |
590 | 0 | } |
591 | 10 | const spvtools::InstructionDesc* opcode_entry = nullptr; |
592 | 10 | if (spvtools::LookupOpcode(spv::Op(word), &opcode_entry)) { |
593 | 0 | return diagnostic(SPV_ERROR_INTERNAL) |
594 | 0 | << "OpSpecConstant opcode table out of sync"; |
595 | 0 | } |
596 | | // OpSpecConstant opcodes must have a type and result. We've already |
597 | | // processed them, so skip them when preparing to parse the other |
598 | | // operants for the opcode. |
599 | 10 | assert(opcode_entry->hasType); |
600 | 10 | assert(opcode_entry->hasResult); |
601 | 10 | assert(opcode_entry->operands().size() >= 2); |
602 | 10 | spvPushOperandTypes(opcode_entry->operands().subspan(2), |
603 | 10 | expected_operands); |
604 | 10 | } break; |
605 | | |
606 | 367k | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
607 | 423k | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: |
608 | | // These are regular single-word literal integer operands. |
609 | | // Post-parsing validation should check the range of the parsed value. |
610 | 423k | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_INTEGER; |
611 | | // It turns out they are always unsigned integers! |
612 | 423k | parsed_operand.number_kind = SPV_NUMBER_UNSIGNED_INT; |
613 | 423k | parsed_operand.number_bit_width = 32; |
614 | 423k | break; |
615 | | |
616 | 0 | case SPV_OPERAND_TYPE_LITERAL_FLOAT: |
617 | | // These are regular single-word literal float operands. |
618 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_FLOAT; |
619 | 0 | parsed_operand.number_kind = SPV_NUMBER_FLOATING; |
620 | 0 | parsed_operand.number_bit_width = 32; |
621 | 0 | break; |
622 | | |
623 | 76.1k | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
624 | 77.0k | case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
625 | 77.0k | parsed_operand.type = SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER; |
626 | 77.0k | if (opcode == spv::Op::OpSwitch) { |
627 | | // The literal operands have the same type as the value |
628 | | // referenced by the selector Id. |
629 | 934 | const uint32_t selector_id = peekAt(inst_offset + 1); |
630 | 934 | const auto type_id_iter = _.id_to_type_id.find(selector_id); |
631 | 934 | if (type_id_iter == _.id_to_type_id.end() || |
632 | 934 | type_id_iter->second == 0) { |
633 | 0 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
634 | 0 | << " has no type"; |
635 | 0 | } |
636 | 934 | uint32_t type_id = type_id_iter->second; |
637 | | |
638 | 934 | if (selector_id == type_id) { |
639 | | // Recall that by convention, a result ID that is a type definition |
640 | | // maps to itself. |
641 | 0 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
642 | 0 | << " is a type, not a value"; |
643 | 0 | } |
644 | 934 | if (auto error = setNumericTypeInfoForType(&parsed_operand, type_id)) |
645 | 0 | return error; |
646 | 934 | if (parsed_operand.number_kind != SPV_NUMBER_UNSIGNED_INT && |
647 | 934 | parsed_operand.number_kind != SPV_NUMBER_SIGNED_INT662 ) { |
648 | 0 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
649 | 0 | << " is not a scalar integer"; |
650 | 0 | } |
651 | 76.1k | } else { |
652 | 76.1k | assert(opcode == spv::Op::OpConstant || |
653 | 76.1k | opcode == spv::Op::OpSpecConstant); |
654 | | // The literal number type is determined by the type Id for the |
655 | | // constant. |
656 | 76.1k | assert(inst->type_id); |
657 | 76.1k | if (auto error = |
658 | 76.1k | setNumericTypeInfoForType(&parsed_operand, inst->type_id)) |
659 | 0 | return error; |
660 | 76.1k | } |
661 | 77.0k | break; |
662 | | |
663 | 371k | case SPV_OPERAND_TYPE_LITERAL_STRING: |
664 | 372k | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
665 | 372k | const size_t max_words = _.num_words - _.word_index; |
666 | 372k | std::string string = |
667 | 372k | spvtools::utils::MakeString(_.words + _.word_index, max_words, false); |
668 | | |
669 | 372k | if (string.length() == max_words * 4) |
670 | 0 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
671 | | |
672 | | // Make sure we can record the word count without overflow. |
673 | | // |
674 | | // This error can't currently be triggered because of validity |
675 | | // checks elsewhere. |
676 | 372k | const size_t string_num_words = string.length() / 4 + 1; |
677 | 372k | if (string_num_words > std::numeric_limits<uint16_t>::max()) { |
678 | 0 | return diagnostic() << "Literal string is longer than " |
679 | 0 | << std::numeric_limits<uint16_t>::max() |
680 | 0 | << " words: " << string_num_words << " words long"; |
681 | 0 | } |
682 | 372k | parsed_operand.num_words = uint16_t(string_num_words); |
683 | 372k | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_STRING; |
684 | | |
685 | 372k | if (spv::Op::OpExtInstImport == opcode) { |
686 | | // Record the extended instruction type for the ID for this import. |
687 | | // There is only one string literal argument to OpExtInstImport, |
688 | | // so it's sufficient to guard this just on the opcode. |
689 | 1.72k | const spv_ext_inst_type_t ext_inst_type = |
690 | 1.72k | spvExtInstImportTypeGet(string.c_str()); |
691 | 1.72k | if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) { |
692 | 0 | return diagnostic() |
693 | 0 | << "Invalid extended instruction import '" << string << "'"; |
694 | 0 | } |
695 | | // We must have parsed a valid result ID. It's a condition |
696 | | // of the grammar, and we only accept non-zero result Ids. |
697 | 1.72k | assert(inst->result_id); |
698 | 1.72k | _.import_id_to_ext_inst_type[inst->result_id] = ext_inst_type; |
699 | 1.72k | } |
700 | 372k | } break; |
701 | | |
702 | 372k | case SPV_OPERAND_TYPE_CAPABILITY: |
703 | 219k | case SPV_OPERAND_TYPE_OPTIONAL_CAPABILITY: |
704 | 236k | case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
705 | 255k | case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
706 | 274k | case SPV_OPERAND_TYPE_MEMORY_MODEL: |
707 | 292k | case SPV_OPERAND_TYPE_EXECUTION_MODE: |
708 | 507k | case SPV_OPERAND_TYPE_STORAGE_CLASS: |
709 | 514k | case SPV_OPERAND_TYPE_DIMENSIONALITY: |
710 | 514k | case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
711 | 514k | case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
712 | 522k | case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
713 | 522k | case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
714 | 522k | case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
715 | 522k | case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
716 | 522k | case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER: |
717 | 522k | case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
718 | 656k | case SPV_OPERAND_TYPE_DECORATION: |
719 | 664k | case SPV_OPERAND_TYPE_BUILT_IN: |
720 | 664k | case SPV_OPERAND_TYPE_GROUP_OPERATION: |
721 | 664k | case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
722 | 664k | case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
723 | 664k | case SPV_OPERAND_TYPE_RAY_FLAGS: |
724 | 664k | case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION: |
725 | 664k | case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE: |
726 | 664k | case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE: |
727 | 664k | case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
728 | 664k | case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
729 | 664k | case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
730 | 664k | case SPV_OPERAND_TYPE_DEBUG_OPERATION: |
731 | 665k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
732 | 665k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE: |
733 | 665k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER: |
734 | 665k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION: |
735 | 665k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY: |
736 | 665k | case SPV_OPERAND_TYPE_FPDENORM_MODE: |
737 | 665k | case SPV_OPERAND_TYPE_FPOPERATION_MODE: |
738 | 665k | case SPV_OPERAND_TYPE_QUANTIZATION_MODES: |
739 | 665k | case SPV_OPERAND_TYPE_OVERFLOW_MODES: |
740 | 665k | case SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT: |
741 | 666k | case SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT: |
742 | 666k | case SPV_OPERAND_TYPE_FPENCODING: |
743 | 666k | case SPV_OPERAND_TYPE_OPTIONAL_FPENCODING: |
744 | 666k | case SPV_OPERAND_TYPE_HOST_ACCESS_QUALIFIER: |
745 | 666k | case SPV_OPERAND_TYPE_LOAD_CACHE_CONTROL: |
746 | 666k | case SPV_OPERAND_TYPE_STORE_CACHE_CONTROL: |
747 | 666k | case SPV_OPERAND_TYPE_NAMED_MAXIMUM_NUMBER_OF_REGISTERS: { |
748 | | // A single word that is a plain enum value. |
749 | | |
750 | | // Map an optional operand type to its corresponding concrete type. |
751 | 666k | if (type == SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER) |
752 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_ACCESS_QUALIFIER; |
753 | 666k | if (type == SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT) |
754 | 20 | parsed_operand.type = SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT; |
755 | 666k | if (type == SPV_OPERAND_TYPE_OPTIONAL_FPENCODING) |
756 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_FPENCODING; |
757 | 666k | if (type == SPV_OPERAND_TYPE_OPTIONAL_CAPABILITY) |
758 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_CAPABILITY; |
759 | | |
760 | 666k | const spvtools::OperandDesc* entry = nullptr; |
761 | 666k | if (spvtools::LookupOperand(type, word, &entry)) { |
762 | 6 | if (handle_unknown_opcodes_) _.retry_instruction_as_unknown_ = true0 ; |
763 | 6 | return diagnostic() |
764 | 6 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
765 | 6 | << " operand: " << word; |
766 | 6 | } |
767 | | // Prepare to accept operands to this operand, if needed. |
768 | 665k | spvPushOperandTypes(entry->operands(), expected_operands); |
769 | 665k | } break; |
770 | | |
771 | 16.0k | case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: { |
772 | 16.0k | const spvtools::OperandDesc* entry = nullptr; |
773 | 16.0k | if (spvtools::LookupOperand(type, word, &entry)) { |
774 | 0 | if (handle_unknown_opcodes_) _.retry_instruction_as_unknown_ = true; |
775 | 0 | return diagnostic() |
776 | 0 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
777 | 0 | << " operand: " << word |
778 | 0 | << ", if you are creating a new source language please use " |
779 | 0 | "value 0 " |
780 | 0 | "(Unknown) and when ready, add your source language to " |
781 | 0 | "SPIRV-Headers"; |
782 | 0 | } |
783 | | // Prepare to accept operands to this operand, if needed. |
784 | 16.0k | spvPushOperandTypes(entry->operands(), expected_operands); |
785 | 16.0k | } break; |
786 | | |
787 | 0 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
788 | 35.3k | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
789 | 36.3k | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
790 | 38.0k | case SPV_OPERAND_TYPE_IMAGE: |
791 | 46.0k | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
792 | 46.0k | case SPV_OPERAND_TYPE_MEMORY_ACCESS: |
793 | 46.0k | case SPV_OPERAND_TYPE_TENSOR_OPERANDS: |
794 | 46.0k | case SPV_OPERAND_TYPE_OPTIONAL_TENSOR_OPERANDS: |
795 | 47.6k | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
796 | 47.6k | case SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS: |
797 | 51.1k | case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
798 | 54.9k | case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS: |
799 | 54.9k | case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: |
800 | 54.9k | case SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_OPERANDS: |
801 | 55.0k | case SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS: |
802 | 55.0k | case SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_REDUCE: |
803 | 55.0k | case SPV_OPERAND_TYPE_TENSOR_ADDRESSING_OPERANDS: |
804 | 55.0k | case SPV_OPERAND_TYPE_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS: |
805 | 55.0k | case SPV_OPERAND_TYPE_OPTIONAL_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS: { |
806 | | // This operand is a mask. |
807 | | |
808 | | // Map an optional operand type to its corresponding concrete type. |
809 | 55.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_IMAGE) |
810 | 8.00k | parsed_operand.type = SPV_OPERAND_TYPE_IMAGE; |
811 | 55.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS) |
812 | 1.58k | parsed_operand.type = SPV_OPERAND_TYPE_MEMORY_ACCESS; |
813 | 55.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS) |
814 | 36 | parsed_operand.type = SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_OPERANDS; |
815 | 55.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS) |
816 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_RAW_ACCESS_CHAIN_OPERANDS; |
817 | 55.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS) |
818 | 0 | parsed_operand.type = |
819 | 0 | SPV_OPERAND_TYPE_MATRIX_MULTIPLY_ACCUMULATE_OPERANDS; |
820 | 55.0k | if (type == SPV_OPERAND_TYPE_OPTIONAL_TENSOR_OPERANDS) |
821 | 0 | parsed_operand.type = SPV_OPERAND_TYPE_TENSOR_OPERANDS; |
822 | | |
823 | | // Check validity of set mask bits. Also prepare for operands for those |
824 | | // masks if they have any. To get operand order correct, scan from |
825 | | // MSB to LSB since we can only prepend operands to a pattern. |
826 | | // The only case in the grammar where you have more than one mask bit |
827 | | // having an operand is for image operands. See SPIR-V 3.14 Image |
828 | | // Operands. |
829 | 55.0k | uint32_t remaining_word = word; |
830 | 363k | for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1308k ) { |
831 | 308k | if (remaining_word & mask) { |
832 | 15.6k | const spvtools::OperandDesc* entry = nullptr; |
833 | 15.6k | if (spvtools::LookupOperand(type, mask, &entry)) { |
834 | 0 | if (handle_unknown_opcodes_) _.retry_instruction_as_unknown_ = true; |
835 | 0 | return diagnostic() |
836 | 0 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
837 | 0 | << " operand: " << word << " has invalid mask component " |
838 | 0 | << mask; |
839 | 0 | } |
840 | 15.6k | remaining_word ^= mask; |
841 | 15.6k | spvPushOperandTypes(entry->operands(), expected_operands); |
842 | 15.6k | } |
843 | 308k | } |
844 | 55.0k | if (word == 0) { |
845 | | // An all-zeroes mask *might* also be valid. |
846 | 44.8k | const spvtools::OperandDesc* entry = nullptr; |
847 | 44.8k | if (SPV_SUCCESS == spvtools::LookupOperand(type, 0, &entry)) { |
848 | | // Prepare for its operands, if any. |
849 | 44.8k | spvPushOperandTypes(entry->operands(), expected_operands); |
850 | 44.8k | } |
851 | 44.8k | } |
852 | 55.0k | } break; |
853 | 0 | default: |
854 | 0 | return diagnostic() << "Internal error: Unhandled operand type: " << type; |
855 | 5.06M | } |
856 | | |
857 | 5.06M | assert(spvOperandIsConcrete(parsed_operand.type)); |
858 | | |
859 | 5.06M | operands->push_back(parsed_operand); |
860 | | |
861 | 5.06M | const size_t index_after_operand = _.word_index + parsed_operand.num_words; |
862 | | |
863 | | // Avoid buffer overrun for the cases where the operand has more than one |
864 | | // word, and where it isn't a string. (Those other cases have already been |
865 | | // handled earlier.) For example, this error can occur for a multi-word |
866 | | // argument to OpConstant, or a multi-word case literal operand for OpSwitch. |
867 | 5.06M | if (_.num_words < index_after_operand) |
868 | 0 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
869 | | |
870 | 5.06M | if (_.requires_endian_conversion) { |
871 | | // Copy instruction words. Translate to native endianness as needed. |
872 | 0 | if (convert_operand_endianness) { |
873 | 0 | const spv_endianness_t endianness = _.endian; |
874 | 0 | std::transform(_.words + _.word_index, _.words + index_after_operand, |
875 | 0 | std::back_inserter(*words), |
876 | 0 | [endianness](const uint32_t raw_word) { |
877 | 0 | return spvFixWord(raw_word, endianness); |
878 | 0 | }); |
879 | 0 | } else { |
880 | 0 | words->insert(words->end(), _.words + _.word_index, |
881 | 0 | _.words + index_after_operand); |
882 | 0 | } |
883 | 0 | } |
884 | | |
885 | | // Advance past the operand. |
886 | 5.06M | _.word_index = index_after_operand; |
887 | | |
888 | 5.06M | return SPV_SUCCESS; |
889 | 5.06M | } |
890 | | |
891 | | spv_result_t Parser::setNumericTypeInfoForType( |
892 | 77.0k | spv_parsed_operand_t* parsed_operand, uint32_t type_id) { |
893 | 77.0k | assert(type_id != 0); |
894 | 77.0k | auto type_info_iter = _.type_id_to_number_type_info.find(type_id); |
895 | 77.0k | if (type_info_iter == _.type_id_to_number_type_info.end()) { |
896 | 0 | return diagnostic() << "Type Id " << type_id << " is not a type"; |
897 | 0 | } |
898 | 77.0k | const NumberType& info = type_info_iter->second; |
899 | 77.0k | if (info.type == SPV_NUMBER_NONE) { |
900 | | // This is a valid type, but for something other than a scalar number. |
901 | 0 | return diagnostic() << "Type Id " << type_id |
902 | 0 | << " is not a scalar numeric type"; |
903 | 0 | } |
904 | | |
905 | 77.0k | parsed_operand->number_kind = info.type; |
906 | 77.0k | parsed_operand->number_bit_width = info.bit_width; |
907 | 77.0k | parsed_operand->fp_encoding = info.encoding; |
908 | | // Round up the word count. |
909 | 77.0k | parsed_operand->num_words = static_cast<uint16_t>((info.bit_width + 31) / 32); |
910 | 77.0k | return SPV_SUCCESS; |
911 | 77.0k | } |
912 | | |
913 | | void Parser::recordNumberType(size_t inst_offset, |
914 | 1.94M | const spv_parsed_instruction_t* inst) { |
915 | 1.94M | const spv::Op opcode = static_cast<spv::Op>(inst->opcode); |
916 | 1.94M | if (spvOpcodeGeneratesType(opcode)) { |
917 | 271k | NumberType info = {SPV_NUMBER_NONE, 0}; |
918 | 271k | if (spv::Op::OpTypeInt == opcode) { |
919 | 26.3k | const bool is_signed = peekAt(inst_offset + 3) != 0; |
920 | 26.3k | info.type = is_signed ? SPV_NUMBER_SIGNED_INT12.4k : SPV_NUMBER_UNSIGNED_INT13.8k ; |
921 | 26.3k | info.bit_width = peekAt(inst_offset + 2); |
922 | 245k | } else if (spv::Op::OpTypeFloat == opcode) { |
923 | 13.0k | info.type = SPV_NUMBER_FLOATING; |
924 | 13.0k | info.bit_width = peekAt(inst_offset + 2); |
925 | 13.0k | if (inst->num_words >= 4) { |
926 | 0 | const spvtools::OperandDesc* desc = nullptr; |
927 | 0 | spv_result_t status = spvtools::LookupOperand( |
928 | 0 | SPV_OPERAND_TYPE_FPENCODING, peekAt(inst_offset + 3), &desc); |
929 | 0 | if (status == SPV_SUCCESS) { |
930 | 0 | info.encoding = spvFPEncodingFromOperandFPEncoding( |
931 | 0 | static_cast<spv::FPEncoding>(desc->value)); |
932 | 0 | } else { |
933 | 0 | info.encoding = SPV_FP_ENCODING_UNKNOWN; |
934 | 0 | } |
935 | 0 | } |
936 | 13.0k | } |
937 | | // The *result* Id of a type generating instruction is the type Id. |
938 | 271k | _.type_id_to_number_type_info[inst->result_id] = info; |
939 | 271k | } |
940 | 1.94M | } |
941 | | |
942 | | } // anonymous namespace |
943 | | |
944 | | spv_result_t spvBinaryParse(const spv_const_context context, void* user_data, |
945 | | const uint32_t* code, const size_t num_words, |
946 | | spv_parsed_header_fn_t parsed_header, |
947 | | spv_parsed_instruction_fn_t parsed_instruction, |
948 | 10.2k | spv_diagnostic* diagnostic) { |
949 | 10.2k | return spvBinaryParseWithOptions(context, user_data, code, num_words, |
950 | 10.2k | parsed_header, parsed_instruction, |
951 | 10.2k | diagnostic, 0); |
952 | 10.2k | } |
953 | | |
954 | | spv_result_t spvBinaryParseWithOptions( |
955 | | const spv_const_context context, void* user_data, const uint32_t* code, |
956 | | const size_t num_words, spv_parsed_header_fn_t parsed_header, |
957 | | spv_parsed_instruction_fn_t parsed_instruction, spv_diagnostic* diagnostic, |
958 | 19.2k | uint32_t options) { |
959 | 19.2k | spv_context_t hijack_context = *context; |
960 | 19.2k | if (diagnostic) { |
961 | 8.89k | *diagnostic = nullptr; |
962 | 8.89k | spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, diagnostic); |
963 | 8.89k | } |
964 | 19.2k | Parser parser(&hijack_context, user_data, parsed_header, parsed_instruction); |
965 | 19.2k | if (options & SPV_BINARY_TO_TEXT_OPTION_HANDLE_UNKNOWN_OPCODES) { |
966 | 0 | parser.SetHandleUnknownOpcodes(true); |
967 | 0 | } |
968 | 19.2k | return parser.parse(code, num_words, diagnostic); |
969 | 19.2k | } |
970 | | |
971 | | // TODO(dneto): This probably belongs in text.cpp since that's the only place |
972 | | // that a spv_binary_t value is created. |
973 | 0 | void spvBinaryDestroy(spv_binary binary) { |
974 | 0 | if (binary) { |
975 | 0 | if (binary->code) delete[] binary->code; |
976 | 0 | delete binary; |
977 | 0 | } |
978 | 0 | } |
979 | | |
980 | 0 | size_t spv_strnlen_s(const char* str, size_t strsz) { |
981 | 0 | if (!str) return 0; |
982 | 0 | for (size_t i = 0; i < strsz; i++) { |
983 | 0 | if (!str[i]) return i; |
984 | 0 | } |
985 | 0 | return strsz; |
986 | 0 | } |