1//===- DeserializeOps.cpp - MLIR SPIR-V Deserialization (Ops) -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Deserializer methods for SPIR-V binary instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Deserializer.h"
14
15#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
16#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
17#include "mlir/IR/Builders.h"
18#include "mlir/IR/Location.h"
19#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
20#include "mlir/Target/SPIRV/SPIRVExtInstSets.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/Support/Debug.h"
24#include <optional>
25
26using namespace mlir;
27
28#define DEBUG_TYPE "spirv-deserialization"
29
30//===----------------------------------------------------------------------===//
31// Utility Functions
32//===----------------------------------------------------------------------===//
33
34/// Extracts the opcode from the given first word of a SPIR-V instruction.
35static inline spirv::Opcode extractOpcode(uint32_t word) {
36 return static_cast<spirv::Opcode>(word & 0xffff);
37}
38
39/// Returns a NameLoc location from the given debug info string.
40static NameLoc getLocFromDebugInfoString(OpBuilder &builder, StringRef source) {
41 return NameLoc::get(name: builder.getStringAttr(bytes: source));
42}
43
44//===----------------------------------------------------------------------===//
45// Instruction
46//===----------------------------------------------------------------------===//
47
48Value spirv::Deserializer::getValue(uint32_t id) {
49 if (std::optional<std::pair<Attribute, Type>> constCompositeReplicateInfo =
50 getConstantCompositeReplicate(id)) {
51 Value &value = blockConstantMap[{opBuilder.getInsertionBlock(), id}];
52 if (!value)
53 value = spirv::EXTConstantCompositeReplicateOp::create(
54 builder&: opBuilder, location: unknownLoc, replicated_constant: constCompositeReplicateInfo->second,
55 value: constCompositeReplicateInfo->first);
56 return value;
57 }
58 if (auto constInfo = getConstant(id)) {
59 // Materialize a `spirv.Constant` op once per block.
60 Value &value = blockConstantMap[{opBuilder.getInsertionBlock(), id}];
61 if (!value) {
62 Location loc = unknownLoc;
63 if (LocationAttr locAttr = constantLocMap.lookup(Val: id))
64 loc = Location(locAttr);
65 value = spirv::ConstantOp::create(builder&: opBuilder, location: loc, constant: constInfo->second,
66 value: constInfo->first);
67 }
68 return value;
69 }
70 if (auto varOp = getGlobalVariable(id)) {
71 auto addressOfOp =
72 spirv::AddressOfOp::create(builder&: opBuilder, location: unknownLoc, pointer: varOp.getType(),
73 variable: SymbolRefAttr::get(symbol: varOp.getOperation()));
74 return addressOfOp.getPointer();
75 }
76 if (auto constOp = getSpecConstant(id)) {
77 auto referenceOfOp = spirv::ReferenceOfOp::create(
78 builder&: opBuilder, location: unknownLoc, reference: constOp.getDefaultValue().getType(),
79 spec_const: SymbolRefAttr::get(symbol: constOp.getOperation()));
80 return referenceOfOp.getReference();
81 }
82 if (SpecConstantCompositeOp specConstCompositeOp =
83 getSpecConstantComposite(id)) {
84 auto referenceOfOp = spirv::ReferenceOfOp::create(
85 builder&: opBuilder, location: unknownLoc, reference: specConstCompositeOp.getType(),
86 spec_const: SymbolRefAttr::get(symbol: specConstCompositeOp.getOperation()));
87 return referenceOfOp.getReference();
88 }
89 if (auto specConstCompositeReplicateOp =
90 getSpecConstantCompositeReplicate(id)) {
91 auto referenceOfOp = spirv::ReferenceOfOp::create(
92 builder&: opBuilder, location: unknownLoc, reference: specConstCompositeReplicateOp.getType(),
93 spec_const: SymbolRefAttr::get(symbol: specConstCompositeReplicateOp.getOperation()));
94 return referenceOfOp.getReference();
95 }
96 if (auto specConstOperationInfo = getSpecConstantOperation(id)) {
97 return materializeSpecConstantOperation(
98 resultID: id, enclosedOpcode: specConstOperationInfo->enclodesOpcode,
99 resultTypeID: specConstOperationInfo->resultTypeID,
100 enclosedOpOperands: specConstOperationInfo->enclosedOpOperands);
101 }
102 if (auto undef = getUndefType(id)) {
103 Value &value = blockConstantMap[{opBuilder.getInsertionBlock(), id}];
104 if (!value)
105 value = spirv::UndefOp::create(builder&: opBuilder, location: unknownLoc, result: undef);
106 return value;
107 }
108 if (std::optional<spirv::GraphConstantARMOpMaterializationInfo>
109 graphConstantARMInfo = getGraphConstantARM(id)) {
110 IntegerAttr graphConstantID = graphConstantARMInfo->graphConstantID;
111 Type resultType = graphConstantARMInfo->resultType;
112 return spirv::GraphConstantARMOp::create(builder&: opBuilder, location: unknownLoc, output: resultType,
113 graph_constant_id: graphConstantID);
114 }
115 return valueMap.lookup(Val: id);
116}
117
118LogicalResult spirv::Deserializer::sliceInstruction(
119 spirv::Opcode &opcode, ArrayRef<uint32_t> &operands,
120 std::optional<spirv::Opcode> expectedOpcode) {
121 auto binarySize = binary.size();
122 if (curOffset >= binarySize) {
123 return emitError(loc: unknownLoc, message: "expected ")
124 << (expectedOpcode ? spirv::stringifyOpcode(*expectedOpcode)
125 : "more")
126 << " instruction";
127 }
128
129 // For each instruction, get its word count from the first word to slice it
130 // from the stream properly, and then dispatch to the instruction handler.
131
132 uint32_t wordCount = binary[curOffset] >> 16;
133
134 if (wordCount == 0)
135 return emitError(loc: unknownLoc, message: "word count cannot be zero");
136
137 uint32_t nextOffset = curOffset + wordCount;
138 if (nextOffset > binarySize)
139 return emitError(loc: unknownLoc, message: "insufficient words for the last instruction");
140
141 opcode = extractOpcode(word: binary[curOffset]);
142 operands = binary.slice(N: curOffset + 1, M: wordCount - 1);
143 curOffset = nextOffset;
144 return success();
145}
146
147void spirv::Deserializer::mergeLongCompositeContinuations(
148 spirv::Opcode opcode, ArrayRef<uint32_t> &operands,
149 SmallVectorImpl<uint32_t> &mergedStorage) {
150 std::optional<spirv::Opcode> continuationOp = getContinuationOpcode(parent: opcode);
151 if (!continuationOp)
152 return;
153
154 size_t binarySize = binary.size();
155 auto isNextContinuation = [&]() {
156 if (curOffset >= binarySize)
157 return false;
158 uint32_t wordCount = binary[curOffset] >> 16;
159 if (wordCount == 0 || curOffset + wordCount > binarySize)
160 return false;
161 return extractOpcode(word: binary[curOffset]) == *continuationOp;
162 };
163
164 if (!isNextContinuation())
165 return;
166
167 mergedStorage.assign(AR: operands);
168 do {
169 spirv::Opcode contOpcode;
170 ArrayRef<uint32_t> contOperands;
171 if (failed(Result: sliceInstruction(opcode&: contOpcode, operands&: contOperands, expectedOpcode: *continuationOp)))
172 return;
173 llvm::append_range(C&: mergedStorage, R&: contOperands);
174 } while (isNextContinuation());
175 operands = mergedStorage;
176}
177
178LogicalResult spirv::Deserializer::processInstruction(
179 spirv::Opcode opcode, ArrayRef<uint32_t> operands, bool deferInstructions) {
180 LLVM_DEBUG(logger.startLine() << "[inst] processing instruction "
181 << spirv::stringifyOpcode(opcode) << "\n");
182
183 SmallVector<uint32_t, 0> mergedStorage;
184 mergeLongCompositeContinuations(opcode, operands, mergedStorage);
185
186 // First dispatch all the instructions whose opcode does not correspond to
187 // those that have a direct mirror in the SPIR-V dialect
188 switch (opcode) {
189 case spirv::Opcode::OpCapability:
190 return processCapability(operands);
191 case spirv::Opcode::OpExtension:
192 return processExtension(words: operands);
193 case spirv::Opcode::OpExtInst: {
194 DenseMap<uint32_t, StringRef>::iterator setIt =
195 operands.size() >= 4 ? extendedInstSets.find(Val: operands[2])
196 : extendedInstSets.end();
197 if (setIt != extendedInstSets.end() && setIt->second == extDebugInfo)
198 return processDebugInfoExtInst(operands, deferInstructions);
199 return processExtInst(operands);
200 }
201 case spirv::Opcode::OpExtInstImport:
202 return processExtInstImport(words: operands);
203 case spirv::Opcode::OpMemberName:
204 return processMemberName(words: operands);
205 case spirv::Opcode::OpMemoryModel:
206 return processMemoryModel(operands);
207 case spirv::Opcode::OpEntryPoint:
208 case spirv::Opcode::OpExecutionMode:
209 case spirv::Opcode::OpExecutionModeId:
210 if (deferInstructions) {
211 deferredInstructions.emplace_back(Args&: opcode, Args&: operands);
212 return success();
213 }
214 break;
215 case spirv::Opcode::OpVariable:
216 if (isa<spirv::ModuleOp>(Val: opBuilder.getBlock()->getParentOp())) {
217 return processGlobalVariable(operands);
218 }
219 break;
220 case spirv::Opcode::OpLine:
221 return processDebugLine(operands);
222 case spirv::Opcode::OpNoLine:
223 clearDebugLine();
224 return success();
225 case spirv::Opcode::OpName:
226 return processName(operands);
227 case spirv::Opcode::OpString:
228 return processDebugString(operands);
229 case spirv::Opcode::OpModuleProcessed:
230 case spirv::Opcode::OpSource:
231 case spirv::Opcode::OpSourceContinued:
232 case spirv::Opcode::OpSourceExtension:
233 // TODO: This is debug information embedded in the binary which should be
234 // translated into the spirv.module.
235 return success();
236 case spirv::Opcode::OpTypeVoid:
237 case spirv::Opcode::OpTypeBool:
238 case spirv::Opcode::OpTypeInt:
239 case spirv::Opcode::OpTypeFloat:
240 case spirv::Opcode::OpTypeVector:
241 case spirv::Opcode::OpTypeMatrix:
242 case spirv::Opcode::OpTypeArray:
243 case spirv::Opcode::OpTypeFunction:
244 case spirv::Opcode::OpTypeImage:
245 case spirv::Opcode::OpTypeSampler:
246 case spirv::Opcode::OpTypeNamedBarrier:
247 case spirv::Opcode::OpTypeSampledImage:
248 case spirv::Opcode::OpTypeRuntimeArray:
249 case spirv::Opcode::OpTypeStruct:
250 case spirv::Opcode::OpTypePointer:
251 case spirv::Opcode::OpTypeTensorARM:
252 case spirv::Opcode::OpTypeGraphARM:
253 case spirv::Opcode::OpTypeCooperativeMatrixKHR:
254 return processType(opcode, operands);
255 case spirv::Opcode::OpTypeForwardPointer:
256 return processTypeForwardPointer(operands);
257 case spirv::Opcode::OpConstant:
258 return processConstant(operands, /*isSpec=*/false);
259 case spirv::Opcode::OpSpecConstant:
260 return processConstant(operands, /*isSpec=*/true);
261 case spirv::Opcode::OpConstantComposite:
262 return processConstantComposite(operands);
263 case spirv::Opcode::OpConstantCompositeReplicateEXT:
264 return processConstantCompositeReplicateEXT(operands);
265 case spirv::Opcode::OpSpecConstantComposite:
266 return processSpecConstantComposite(operands);
267 case spirv::Opcode::OpSpecConstantCompositeReplicateEXT:
268 return processSpecConstantCompositeReplicateEXT(operands);
269 case spirv::Opcode::OpSpecConstantOp:
270 return processSpecConstantOperation(operands);
271 case spirv::Opcode::OpConstantTrue:
272 return processConstantBool(/*isTrue=*/true, operands, /*isSpec=*/false);
273 case spirv::Opcode::OpSpecConstantTrue:
274 return processConstantBool(/*isTrue=*/true, operands, /*isSpec=*/true);
275 case spirv::Opcode::OpConstantFalse:
276 return processConstantBool(/*isTrue=*/false, operands, /*isSpec=*/false);
277 case spirv::Opcode::OpSpecConstantFalse:
278 return processConstantBool(/*isTrue=*/false, operands, /*isSpec=*/true);
279 case spirv::Opcode::OpConstantNull:
280 return processConstantNull(operands);
281 case spirv::Opcode::OpGraphConstantARM:
282 return processGraphConstantARM(operands);
283 case spirv::Opcode::OpDecorate:
284 case spirv::Opcode::OpDecorateId:
285 return processDecoration(words: operands);
286 case spirv::Opcode::OpMemberDecorate:
287 return processMemberDecoration(words: operands);
288 case spirv::Opcode::OpFunction:
289 return processFunction(operands);
290 case spirv::Opcode::OpGraphEntryPointARM:
291 if (deferInstructions) {
292 deferredInstructions.emplace_back(Args&: opcode, Args&: operands);
293 return success();
294 }
295 return processGraphEntryPointARM(operands);
296 case spirv::Opcode::OpGraphARM:
297 return processGraphARM(operands);
298 case spirv::Opcode::OpGraphSetOutputARM:
299 return processOpGraphSetOutputARM(operands);
300 case spirv::Opcode::OpGraphEndARM:
301 return processGraphEndARM(operands);
302 case spirv::Opcode::OpLabel:
303 return processLabel(operands);
304 case spirv::Opcode::OpBranch:
305 return processBranch(operands);
306 case spirv::Opcode::OpBranchConditional:
307 return processBranchConditional(operands);
308 case spirv::Opcode::OpSelectionMerge:
309 return processSelectionMerge(operands);
310 case spirv::Opcode::OpLoopMerge:
311 return processLoopMerge(operands);
312 case spirv::Opcode::OpPhi:
313 return processPhi(operands);
314 case spirv::Opcode::OpSwitch:
315 return processSwitch(operands);
316 case spirv::Opcode::OpUndef:
317 return processUndef(operands);
318 default:
319 break;
320 }
321 return dispatchToAutogenDeserialization(opcode, words: operands);
322}
323
324LogicalResult spirv::Deserializer::processOpWithoutGrammarAttr(
325 ArrayRef<uint32_t> words, StringRef opName, bool hasResult,
326 unsigned numOperands) {
327 SmallVector<Type, 1> resultTypes;
328 uint32_t valueID = 0;
329
330 size_t wordIndex = 0;
331 if (hasResult) {
332 if (wordIndex >= words.size())
333 return emitError(loc: unknownLoc,
334 message: "expected result type <id> while deserializing for ")
335 << opName;
336
337 // Decode the type <id>
338 auto type = getType(id: words[wordIndex]);
339 if (!type)
340 return emitError(loc: unknownLoc, message: "unknown type result <id>: ")
341 << words[wordIndex];
342 resultTypes.push_back(Elt: type);
343 ++wordIndex;
344
345 // Decode the result <id>
346 if (wordIndex >= words.size())
347 return emitError(loc: unknownLoc,
348 message: "expected result <id> while deserializing for ")
349 << opName;
350 valueID = words[wordIndex];
351 ++wordIndex;
352 }
353
354 SmallVector<Value, 4> operands;
355 SmallVector<NamedAttribute, 4> attributes;
356
357 // Decode operands
358 size_t operandIndex = 0;
359 for (; operandIndex < numOperands && wordIndex < words.size();
360 ++operandIndex, ++wordIndex) {
361 auto arg = getValue(id: words[wordIndex]);
362 if (!arg)
363 return emitError(loc: unknownLoc, message: "unknown result <id>: ") << words[wordIndex];
364 operands.push_back(Elt: arg);
365 }
366 if (operandIndex != numOperands) {
367 return emitError(
368 loc: unknownLoc,
369 message: "found less operands than expected when deserializing for ")
370 << opName << "; only " << operandIndex << " of " << numOperands
371 << " processed";
372 }
373 if (wordIndex != words.size()) {
374 return emitError(
375 loc: unknownLoc,
376 message: "found more operands than expected when deserializing for ")
377 << opName << "; only " << wordIndex << " of " << words.size()
378 << " processed";
379 }
380
381 // Attach attributes from decorations
382 if (decorations.count(Val: valueID)) {
383 auto attrs = decorations[valueID].getAttrs();
384 attributes.append(in_start: attrs.begin(), in_end: attrs.end());
385 }
386
387 // Create the op and update bookkeeping maps
388 Location loc = createFileLineColLoc(opBuilder);
389 OperationState opState(loc, opName);
390 opState.addOperands(newOperands: operands);
391 if (hasResult)
392 opState.addTypes(newTypes: resultTypes);
393 opState.addAttributes(newAttributes: attributes);
394 Operation *op = opBuilder.create(state: opState);
395 if (hasResult)
396 valueMap[valueID] = op->getResult(idx: 0);
397
398 if (op->hasTrait<OpTrait::IsTerminator>())
399 clearDebugLine();
400
401 return success();
402}
403
404LogicalResult spirv::Deserializer::processUndef(ArrayRef<uint32_t> operands) {
405 if (operands.size() != 2) {
406 return emitError(loc: unknownLoc, message: "OpUndef instruction must have two operands");
407 }
408 auto type = getType(id: operands[0]);
409 if (!type) {
410 return emitError(loc: unknownLoc, message: "unknown type <id> with OpUndef instruction");
411 }
412 undefMap[operands[1]] = type;
413 return success();
414}
415
416LogicalResult
417spirv::Deserializer::processDebugInfoExtInst(ArrayRef<uint32_t> operands,
418 bool deferInstructions) {
419 if (deferInstructions) {
420 deferredInstructions.emplace_back(Args: spirv::Opcode::OpExtInst, Args&: operands);
421 return success();
422 }
423
424 if (operands.size() < 4) {
425 return emitError(loc: unknownLoc,
426 message: "OpExtInst must have at least 4 operands, result type "
427 "<id>, result <id>, set <id> and instruction opcode");
428 }
429
430 Type resultType = getType(id: operands[0]);
431 if (!resultType || !isVoidType(type: resultType))
432 return emitError(loc: unknownLoc,
433 message: "DebugInfo instructions must have OpTypeVoid result type");
434
435 auto getDebugLoc = [&](uint32_t stringID) -> FailureOr<Location> {
436 DenseMap<uint32_t, StringRef>::iterator stringIt =
437 debugInfoMap.find(Val: stringID);
438 if (stringIt == debugInfoMap.end()) {
439 return emitError(loc: unknownLoc, message: "undefined string <id> ")
440 << stringID << " in DebugInfo";
441 }
442 return Location(getLocFromDebugInfoString(builder&: opBuilder, source: stringIt->second));
443 };
444
445 if (!spirv::isValidGraphDebugInfoExtInst(opcode: operands[3]))
446 return emitError(loc: unknownLoc, message: "unknown DebugInfo instruction opcode: ")
447 << operands[3];
448
449 auto instructionID = static_cast<spirv::GraphDebugInfoExtInst>(operands[3]);
450 switch (instructionID) {
451 case spirv::GraphDebugInfoExtInst::DebugGraph: {
452 if (operands.size() < 6)
453 return emitError(loc: unknownLoc, message: "DebugGraph must have graph and string IDs");
454 uint32_t graphID = operands[4];
455 uint32_t stringID = operands[5];
456 DenseMap<uint32_t, spirv::GraphARMOp>::iterator graphIt =
457 graphMap.find(Val: graphID);
458 if (graphIt == graphMap.end())
459 return emitError(loc: unknownLoc, message: "undefined graph <id> ")
460 << graphID << " in DebugGraph";
461 FailureOr<Location> loc = getDebugLoc(stringID);
462 if (failed(Result: loc))
463 return failure();
464 graphIt->second->setLoc(*loc);
465 break;
466 }
467 case spirv::GraphDebugInfoExtInst::DebugOperation: {
468 if (operands.size() < 7)
469 return emitError(loc: unknownLoc, message: "DebugOperation must have graph, string and "
470 "instruction IDs");
471 uint32_t stringID = operands[5];
472 FailureOr<Location> loc = getDebugLoc(stringID);
473 if (failed(Result: loc))
474 return failure();
475 SmallVector<uint32_t> operationIDs;
476 operationIDs.append(in_start: std::next(x: operands.begin(), n: 6), in_end: operands.end());
477 for (uint32_t operationID : operationIDs) {
478 DenseMap<uint32_t, Value>::iterator valueIt = valueMap.find(Val: operationID);
479 if (valueIt == valueMap.end())
480 return emitError(loc: unknownLoc, message: "undefined operation <id> ")
481 << operationID << " in DebugOperation";
482 valueIt->second.setLoc(*loc);
483 }
484 break;
485 }
486 case spirv::GraphDebugInfoExtInst::DebugTensor: {
487 if (operands.size() < 6)
488 return emitError(loc: unknownLoc,
489 message: "DebugTensor must have tensor and string IDs");
490 uint32_t stringID = operands[5];
491 uint32_t tensorID = operands[4];
492 FailureOr<Location> loc = getDebugLoc(stringID);
493 if (failed(Result: loc))
494 return failure();
495 if (constantMap.contains(Val: tensorID)) {
496 constantLocMap[tensorID] = *loc;
497 break;
498 }
499 DenseMap<uint32_t, Value>::iterator valueIt = valueMap.find(Val: tensorID);
500 if (valueIt == valueMap.end())
501 return emitError(loc: unknownLoc, message: "undefined tensor <id> ")
502 << tensorID << " in DebugTensor";
503 valueIt->second.setLoc(*loc);
504 break;
505 }
506 }
507
508 return success();
509}
510
511LogicalResult spirv::Deserializer::processExtInst(ArrayRef<uint32_t> operands) {
512 if (operands.size() < 4) {
513 return emitError(loc: unknownLoc,
514 message: "OpExtInst must have at least 4 operands, result type "
515 "<id>, result <id>, set <id> and instruction opcode");
516 }
517 if (!extendedInstSets.count(Val: operands[2])) {
518 return emitError(loc: unknownLoc, message: "undefined set <id> in OpExtInst");
519 }
520 SmallVector<uint32_t, 4> slicedOperands;
521 slicedOperands.append(in_start: operands.begin(), in_end: std::next(x: operands.begin(), n: 2));
522 slicedOperands.append(in_start: std::next(x: operands.begin(), n: 4), in_end: operands.end());
523 return dispatchToExtensionSetAutogenDeserialization(
524 extensionSetName: extendedInstSets[operands[2]], instructionID: operands[3], words: slicedOperands);
525}
526
527namespace mlir {
528namespace spirv {
529
530template <>
531LogicalResult
532Deserializer::processOp<spirv::EntryPointOp>(ArrayRef<uint32_t> words) {
533 unsigned wordIndex = 0;
534 if (wordIndex >= words.size()) {
535 return emitError(loc: unknownLoc,
536 message: "missing Execution Model specification in OpEntryPoint");
537 }
538 auto execModel = spirv::ExecutionModelAttr::get(
539 context, value: static_cast<spirv::ExecutionModel>(words[wordIndex++]));
540 if (wordIndex >= words.size()) {
541 return emitError(loc: unknownLoc, message: "missing <id> in OpEntryPoint");
542 }
543 // Get the function <id>
544 auto fnID = words[wordIndex++];
545 // Get the function name
546 auto fnName = decodeStringLiteral(words, wordIndex);
547 // Verify that the function <id> matches the fnName
548 auto parsedFunc = getFunction(id: fnID);
549 if (!parsedFunc) {
550 return emitError(loc: unknownLoc, message: "no function matching <id> ") << fnID;
551 }
552 if (parsedFunc.getName() != fnName) {
553 // The deserializer uses "spirv_fn_<id>" as the function name if the input
554 // SPIR-V blob does not contain a name for it. We should use a more clear
555 // indication for such case rather than relying on naming details.
556 if (!parsedFunc.getName().starts_with(Prefix: "spirv_fn_"))
557 return emitError(loc: unknownLoc,
558 message: "function name mismatch between OpEntryPoint "
559 "and OpFunction with <id> ")
560 << fnID << ": " << fnName << " vs. " << parsedFunc.getName();
561 parsedFunc.setName(fnName);
562 }
563 SmallVector<Attribute, 4> interface;
564 while (wordIndex < words.size()) {
565 auto arg = getGlobalVariable(id: words[wordIndex]);
566 if (!arg) {
567 return emitError(loc: unknownLoc, message: "undefined result <id> ")
568 << words[wordIndex] << " while decoding OpEntryPoint";
569 }
570 interface.push_back(Elt: SymbolRefAttr::get(symbol: arg.getOperation()));
571 wordIndex++;
572 }
573 spirv::EntryPointOp::create(
574 builder&: opBuilder, location: unknownLoc, execution_model: execModel,
575 fn: SymbolRefAttr::get(ctx: opBuilder.getContext(), value: fnName),
576 interface: opBuilder.getArrayAttr(value: interface));
577 return success();
578}
579
580template <>
581LogicalResult
582Deserializer::processOp<spirv::ExecutionModeOp>(ArrayRef<uint32_t> words) {
583 unsigned wordIndex = 0;
584 if (wordIndex >= words.size()) {
585 return emitError(loc: unknownLoc,
586 message: "missing function result <id> in OpExecutionMode");
587 }
588 // Get the function <id> to get the name of the function
589 auto fnID = words[wordIndex++];
590 auto fn = getFunction(id: fnID);
591 if (!fn) {
592 return emitError(loc: unknownLoc, message: "no function matching <id> ") << fnID;
593 }
594 // Get the Execution mode
595 if (wordIndex >= words.size()) {
596 return emitError(loc: unknownLoc, message: "missing Execution Mode in OpExecutionMode");
597 }
598 auto execMode = spirv::ExecutionModeAttr::get(
599 context, value: static_cast<spirv::ExecutionMode>(words[wordIndex++]));
600
601 // Get the values
602 SmallVector<Attribute, 4> attrListElems;
603 while (wordIndex < words.size()) {
604 attrListElems.push_back(Elt: opBuilder.getI32IntegerAttr(value: words[wordIndex++]));
605 }
606 auto values = opBuilder.getArrayAttr(value: attrListElems);
607 spirv::ExecutionModeOp::create(
608 builder&: opBuilder, location: unknownLoc,
609 fn: SymbolRefAttr::get(ctx: opBuilder.getContext(), value: fn.getName()), execution_mode: execMode,
610 values);
611 return success();
612}
613
614template <>
615LogicalResult
616Deserializer::processOp<spirv::ExecutionModeIdOp>(ArrayRef<uint32_t> words) {
617 unsigned wordIndex = 0;
618 unsigned const wordsSize = words.size();
619 if (wordIndex >= wordsSize)
620 return emitError(loc: unknownLoc,
621 message: "missing function result <id> in OpExecutionModeId");
622
623 // Get the function <id> to get the name of the function.
624 uint32_t fnID = words[wordIndex++];
625 FuncOp fn = getFunction(id: fnID);
626 if (!fn)
627 return emitError(loc: unknownLoc, message: "no function matching <id> ") << fnID;
628
629 // Get the Execution mode.
630 if (wordIndex >= wordsSize)
631 return emitError(loc: unknownLoc, message: "missing Execution Mode in OpExecutionModeId");
632
633 ExecutionModeAttr execMode = spirv::ExecutionModeAttr::get(
634 context, value: static_cast<spirv::ExecutionMode>(words[wordIndex++]));
635
636 // Get the values.
637 SmallVector<Attribute, 4> attrListElems;
638 while (wordIndex < words.size()) {
639 std::string id = getSpecConstantSymbol(id: words[wordIndex++]);
640 attrListElems.push_back(Elt: FlatSymbolRefAttr::get(ctx: context, value: id));
641 }
642 ArrayAttr values = opBuilder.getArrayAttr(value: attrListElems);
643 spirv::ExecutionModeIdOp::create(
644 builder&: opBuilder, location: unknownLoc,
645 fn: SymbolRefAttr::get(ctx: opBuilder.getContext(), value: fn.getName()), execution_mode: execMode,
646 values);
647 return success();
648}
649
650template <>
651LogicalResult
652Deserializer::processOp<spirv::FunctionCallOp>(ArrayRef<uint32_t> operands) {
653 if (operands.size() < 3) {
654 return emitError(loc: unknownLoc,
655 message: "OpFunctionCall must have at least 3 operands");
656 }
657
658 Type resultType = getType(id: operands[0]);
659 if (!resultType) {
660 return emitError(loc: unknownLoc, message: "undefined result type from <id> ")
661 << operands[0];
662 }
663
664 // Use null type to mean no result type.
665 if (isVoidType(type: resultType))
666 resultType = nullptr;
667
668 auto resultID = operands[1];
669 auto functionID = operands[2];
670
671 auto functionName = getFunctionSymbol(id: functionID);
672
673 SmallVector<Value, 4> arguments;
674 for (auto operand : llvm::drop_begin(RangeOrContainer&: operands, N: 3)) {
675 auto value = getValue(id: operand);
676 if (!value) {
677 return emitError(loc: unknownLoc, message: "unknown <id> ")
678 << operand << " used by OpFunctionCall";
679 }
680 arguments.push_back(Elt: value);
681 }
682
683 auto opFunctionCall = spirv::FunctionCallOp::create(
684 builder&: opBuilder, location: unknownLoc, returnType: resultType,
685 callee: SymbolRefAttr::get(ctx: opBuilder.getContext(), value: functionName), arguments);
686
687 if (auto it = decorations.find(Val: resultID); it != decorations.end())
688 for (NamedAttribute attr : it->second)
689 opFunctionCall->setAttr(name: attr.getName(), value: attr.getValue());
690
691 if (resultType)
692 valueMap[resultID] = opFunctionCall.getResult(i: 0);
693 return success();
694}
695
696template <>
697LogicalResult
698Deserializer::processOp<spirv::CopyMemoryOp>(ArrayRef<uint32_t> words) {
699 SmallVector<Type, 1> resultTypes;
700 size_t wordIndex = 0;
701 SmallVector<Value, 4> operands;
702 SmallVector<NamedAttribute, 4> attributes;
703
704 if (wordIndex < words.size()) {
705 auto arg = getValue(id: words[wordIndex]);
706
707 if (!arg) {
708 return emitError(loc: unknownLoc, message: "unknown result <id> : ")
709 << words[wordIndex];
710 }
711
712 operands.push_back(Elt: arg);
713 wordIndex++;
714 }
715
716 if (wordIndex < words.size()) {
717 auto arg = getValue(id: words[wordIndex]);
718
719 if (!arg) {
720 return emitError(loc: unknownLoc, message: "unknown result <id> : ")
721 << words[wordIndex];
722 }
723
724 operands.push_back(Elt: arg);
725 wordIndex++;
726 }
727
728 bool isAlignedAttr = false;
729
730 if (wordIndex < words.size()) {
731 auto attrValue = words[wordIndex++];
732 auto attr = opBuilder.getAttr<spirv::MemoryAccessAttr>(
733 args: static_cast<spirv::MemoryAccess>(attrValue));
734 attributes.push_back(
735 Elt: opBuilder.getNamedAttr(name: attributeName<MemoryAccess>(), val: attr));
736 isAlignedAttr = (attrValue == 2);
737 }
738
739 if (isAlignedAttr && wordIndex < words.size()) {
740 attributes.push_back(Elt: opBuilder.getNamedAttr(
741 name: "alignment", val: opBuilder.getI32IntegerAttr(value: words[wordIndex++])));
742 }
743
744 if (wordIndex < words.size()) {
745 auto attrValue = words[wordIndex++];
746 auto attr = opBuilder.getAttr<spirv::MemoryAccessAttr>(
747 args: static_cast<spirv::MemoryAccess>(attrValue));
748 attributes.push_back(Elt: opBuilder.getNamedAttr(name: "source_memory_access", val: attr));
749 }
750
751 if (wordIndex < words.size()) {
752 attributes.push_back(Elt: opBuilder.getNamedAttr(
753 name: "source_alignment", val: opBuilder.getI32IntegerAttr(value: words[wordIndex++])));
754 }
755
756 if (wordIndex != words.size()) {
757 return emitError(loc: unknownLoc,
758 message: "found more operands than expected when deserializing "
759 "spirv::CopyMemoryOp, only ")
760 << wordIndex << " of " << words.size() << " processed";
761 }
762
763 Location loc = createFileLineColLoc(opBuilder);
764 spirv::CopyMemoryOp::Properties properties{};
765 spirv::CopyMemoryOp::populateDefaultProperties(
766 opName: OperationName(spirv::CopyMemoryOp::getOperationName(),
767 opBuilder.getContext()),
768 properties);
769 if (failed(Result: spirv::CopyMemoryOp::setPropertiesFromAttr(
770 prop&: properties, attr: opBuilder.getDictionaryAttr(value: attributes),
771 emitError: [&]() { return emitError(loc); })))
772 return failure();
773 spirv::CopyMemoryOp::create(builder&: opBuilder, location: loc, resultTypes, operands, properties,
774 /*discardableAttributes=*/{});
775
776 return success();
777}
778
779template <>
780LogicalResult Deserializer::processOp<spirv::GenericCastToPtrExplicitOp>(
781 ArrayRef<uint32_t> words) {
782 if (words.size() != 4) {
783 return emitError(loc: unknownLoc,
784 message: "expected 4 words in GenericCastToPtrExplicitOp"
785 " but got : ")
786 << words.size();
787 }
788 SmallVector<Type, 1> resultTypes;
789 SmallVector<Value, 4> operands;
790 uint32_t valueID = 0;
791 auto type = getType(id: words[0]);
792
793 if (!type)
794 return emitError(loc: unknownLoc, message: "unknown type result <id> : ") << words[0];
795 resultTypes.push_back(Elt: type);
796
797 valueID = words[1];
798
799 auto arg = getValue(id: words[2]);
800 if (!arg)
801 return emitError(loc: unknownLoc, message: "unknown result <id> : ") << words[2];
802 operands.push_back(Elt: arg);
803
804 Location loc = createFileLineColLoc(opBuilder);
805 Operation *op = spirv::GenericCastToPtrExplicitOp::create(
806 builder&: opBuilder, location: loc, resultTypes, operands);
807 valueMap[valueID] = op->getResult(idx: 0);
808 return success();
809}
810
811// Pull in auto-generated Deserializer::dispatchToAutogenDeserialization() and
812// various Deserializer::processOp<...>() specializations.
813#define GET_DESERIALIZATION_FNS
814#include "mlir/Dialect/SPIRV/IR/SPIRVSerialization.inc"
815
816} // namespace spirv
817} // namespace mlir
818