20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/Support/Debug.h"
27#define DEBUG_TYPE "spirv-deserialization"
35 return static_cast<spirv::Opcode
>(word & 0xffff);
45 return spirv::ConstantOp::create(opBuilder, unknownLoc, constInfo->second,
48 if (std::optional<std::pair<Attribute, Type>> constCompositeReplicateInfo =
50 return spirv::EXTConstantCompositeReplicateOp::create(
51 opBuilder, unknownLoc, constCompositeReplicateInfo->second,
52 constCompositeReplicateInfo->first);
56 spirv::AddressOfOp::create(opBuilder, unknownLoc, varOp.getType(),
57 SymbolRefAttr::get(varOp.getOperation()));
58 return addressOfOp.getPointer();
61 auto referenceOfOp = spirv::ReferenceOfOp::create(
62 opBuilder, unknownLoc, constOp.getDefaultValue().getType(),
63 SymbolRefAttr::get(constOp.getOperation()));
64 return referenceOfOp.getReference();
66 if (SpecConstantCompositeOp specConstCompositeOp =
68 auto referenceOfOp = spirv::ReferenceOfOp::create(
69 opBuilder, unknownLoc, specConstCompositeOp.getType(),
70 SymbolRefAttr::get(specConstCompositeOp.getOperation()));
71 return referenceOfOp.getReference();
73 if (
auto specConstCompositeReplicateOp =
75 auto referenceOfOp = spirv::ReferenceOfOp::create(
76 opBuilder, unknownLoc, specConstCompositeReplicateOp.getType(),
77 SymbolRefAttr::get(specConstCompositeReplicateOp.getOperation()));
78 return referenceOfOp.getReference();
82 id, specConstOperationInfo->enclodesOpcode,
83 specConstOperationInfo->resultTypeID,
84 specConstOperationInfo->enclosedOpOperands);
87 return spirv::UndefOp::create(opBuilder, unknownLoc, undef);
89 if (std::optional<spirv::GraphConstantARMOpMaterializationInfo>
91 IntegerAttr graphConstantID = graphConstantARMInfo->graphConstantID;
92 Type resultType = graphConstantARMInfo->resultType;
93 return spirv::GraphConstantARMOp::create(opBuilder, unknownLoc, resultType,
96 return valueMap.lookup(
id);
101 std::optional<spirv::Opcode> expectedOpcode) {
102 auto binarySize = binary.size();
103 if (curOffset >= binarySize) {
104 return emitError(unknownLoc,
"expected ")
105 << (expectedOpcode ? spirv::stringifyOpcode(*expectedOpcode)
113 uint32_t wordCount = binary[curOffset] >> 16;
116 return emitError(unknownLoc,
"word count cannot be zero");
118 uint32_t nextOffset = curOffset + wordCount;
119 if (nextOffset > binarySize)
120 return emitError(unknownLoc,
"insufficient words for the last instruction");
123 operands = binary.slice(curOffset + 1, wordCount - 1);
124 curOffset = nextOffset;
130 LLVM_DEBUG(logger.startLine() <<
"[inst] processing instruction "
131 << spirv::stringifyOpcode(opcode) <<
"\n");
136 case spirv::Opcode::OpCapability:
137 return processCapability(operands);
138 case spirv::Opcode::OpExtension:
139 return processExtension(operands);
140 case spirv::Opcode::OpExtInst:
142 case spirv::Opcode::OpExtInstImport:
143 return processExtInstImport(operands);
144 case spirv::Opcode::OpMemberName:
145 return processMemberName(operands);
146 case spirv::Opcode::OpMemoryModel:
147 return processMemoryModel(operands);
148 case spirv::Opcode::OpEntryPoint:
149 case spirv::Opcode::OpExecutionMode:
150 case spirv::Opcode::OpExecutionModeId:
151 if (deferInstructions) {
152 deferredInstructions.emplace_back(opcode, operands);
156 case spirv::Opcode::OpVariable:
157 if (isa<spirv::ModuleOp>(opBuilder.getBlock()->getParentOp())) {
161 case spirv::Opcode::OpLine:
163 case spirv::Opcode::OpNoLine:
166 case spirv::Opcode::OpName:
167 return processName(operands);
168 case spirv::Opcode::OpString:
170 case spirv::Opcode::OpModuleProcessed:
171 case spirv::Opcode::OpSource:
172 case spirv::Opcode::OpSourceContinued:
173 case spirv::Opcode::OpSourceExtension:
177 case spirv::Opcode::OpTypeVoid:
178 case spirv::Opcode::OpTypeBool:
179 case spirv::Opcode::OpTypeInt:
180 case spirv::Opcode::OpTypeFloat:
181 case spirv::Opcode::OpTypeVector:
182 case spirv::Opcode::OpTypeMatrix:
183 case spirv::Opcode::OpTypeArray:
184 case spirv::Opcode::OpTypeFunction:
185 case spirv::Opcode::OpTypeImage:
186 case spirv::Opcode::OpTypeSampledImage:
187 case spirv::Opcode::OpTypeRuntimeArray:
188 case spirv::Opcode::OpTypeStruct:
189 case spirv::Opcode::OpTypePointer:
190 case spirv::Opcode::OpTypeTensorARM:
191 case spirv::Opcode::OpTypeGraphARM:
192 case spirv::Opcode::OpTypeCooperativeMatrixKHR:
194 case spirv::Opcode::OpTypeForwardPointer:
196 case spirv::Opcode::OpConstant:
198 case spirv::Opcode::OpSpecConstant:
200 case spirv::Opcode::OpConstantComposite:
202 case spirv::Opcode::OpConstantCompositeReplicateEXT:
204 case spirv::Opcode::OpSpecConstantComposite:
206 case spirv::Opcode::OpSpecConstantCompositeReplicateEXT:
208 case spirv::Opcode::OpSpecConstantOp:
210 case spirv::Opcode::OpConstantTrue:
212 case spirv::Opcode::OpSpecConstantTrue:
214 case spirv::Opcode::OpConstantFalse:
216 case spirv::Opcode::OpSpecConstantFalse:
218 case spirv::Opcode::OpConstantNull:
220 case spirv::Opcode::OpGraphConstantARM:
222 case spirv::Opcode::OpDecorate:
223 return processDecoration(operands);
224 case spirv::Opcode::OpMemberDecorate:
225 return processMemberDecoration(operands);
226 case spirv::Opcode::OpFunction:
228 case spirv::Opcode::OpGraphEntryPointARM:
229 if (deferInstructions) {
230 deferredInstructions.emplace_back(opcode, operands);
234 case spirv::Opcode::OpGraphARM:
236 case spirv::Opcode::OpGraphSetOutputARM:
238 case spirv::Opcode::OpGraphEndARM:
240 case spirv::Opcode::OpLabel:
242 case spirv::Opcode::OpBranch:
244 case spirv::Opcode::OpBranchConditional:
246 case spirv::Opcode::OpSelectionMerge:
248 case spirv::Opcode::OpLoopMerge:
250 case spirv::Opcode::OpPhi:
252 case spirv::Opcode::OpSwitch:
254 case spirv::Opcode::OpUndef:
264 unsigned numOperands) {
266 uint32_t valueID = 0;
268 size_t wordIndex = 0;
270 if (wordIndex >= words.size())
272 "expected result type <id> while deserializing for ")
276 auto type =
getType(words[wordIndex]);
278 return emitError(unknownLoc,
"unknown type result <id>: ")
280 resultTypes.push_back(type);
284 if (wordIndex >= words.size())
286 "expected result <id> while deserializing for ")
288 valueID = words[wordIndex];
296 size_t operandIndex = 0;
297 for (; operandIndex < numOperands && wordIndex < words.size();
298 ++operandIndex, ++wordIndex) {
299 auto arg =
getValue(words[wordIndex]);
301 return emitError(unknownLoc,
"unknown result <id>: ") << words[wordIndex];
302 operands.push_back(arg);
304 if (operandIndex != numOperands) {
307 "found less operands than expected when deserializing for ")
308 << opName <<
"; only " << operandIndex <<
" of " << numOperands
311 if (wordIndex != words.size()) {
314 "found more operands than expected when deserializing for ")
315 << opName <<
"; only " << wordIndex <<
" of " << words.size()
320 if (decorations.count(valueID)) {
321 auto attrs = decorations[valueID].getAttrs();
322 attributes.append(attrs.begin(), attrs.end());
343 if (operands.size() != 2) {
344 return emitError(unknownLoc,
"OpUndef instruction must have two operands");
346 auto type =
getType(operands[0]);
348 return emitError(unknownLoc,
"unknown type <id> with OpUndef instruction");
350 undefMap[operands[1]] = type;
355 if (operands.size() < 4) {
357 "OpExtInst must have at least 4 operands, result type "
358 "<id>, result <id>, set <id> and instruction opcode");
360 if (!extendedInstSets.count(operands[2])) {
361 return emitError(unknownLoc,
"undefined set <id> in OpExtInst");
364 slicedOperands.append(operands.begin(), std::next(operands.begin(), 2));
365 slicedOperands.append(std::next(operands.begin(), 4), operands.end());
367 extendedInstSets[operands[2]], operands[3], slicedOperands);
376 unsigned wordIndex = 0;
377 if (wordIndex >= words.size()) {
379 "missing Execution Model specification in OpEntryPoint");
381 auto execModel = spirv::ExecutionModelAttr::get(
382 context,
static_cast<spirv::ExecutionModel
>(words[wordIndex++]));
383 if (wordIndex >= words.size()) {
384 return emitError(unknownLoc,
"missing <id> in OpEntryPoint");
387 auto fnID = words[wordIndex++];
391 auto parsedFunc = getFunction(fnID);
393 return emitError(unknownLoc,
"no function matching <id> ") << fnID;
395 if (parsedFunc.getName() != fnName) {
399 if (!parsedFunc.getName().starts_with(
"spirv_fn_"))
401 "function name mismatch between OpEntryPoint "
402 "and OpFunction with <id> ")
403 << fnID <<
": " << fnName <<
" vs. " << parsedFunc.getName();
404 parsedFunc.setName(fnName);
407 while (wordIndex < words.size()) {
408 auto arg = getGlobalVariable(words[wordIndex]);
410 return emitError(unknownLoc,
"undefined result <id> ")
411 << words[wordIndex] <<
" while decoding OpEntryPoint";
413 interface.push_back(SymbolRefAttr::get(arg.getOperation()));
416 spirv::EntryPointOp::create(
417 opBuilder, unknownLoc, execModel,
418 SymbolRefAttr::get(opBuilder.getContext(), fnName),
419 opBuilder.getArrayAttr(interface));
426 unsigned wordIndex = 0;
427 if (wordIndex >= words.size()) {
429 "missing function result <id> in OpExecutionMode");
432 auto fnID = words[wordIndex++];
433 auto fn = getFunction(fnID);
435 return emitError(unknownLoc,
"no function matching <id> ") << fnID;
438 if (wordIndex >= words.size()) {
439 return emitError(unknownLoc,
"missing Execution Mode in OpExecutionMode");
441 auto execMode = spirv::ExecutionModeAttr::get(
442 context,
static_cast<spirv::ExecutionMode
>(words[wordIndex++]));
446 while (wordIndex < words.size()) {
447 attrListElems.push_back(opBuilder.getI32IntegerAttr(words[wordIndex++]));
449 auto values = opBuilder.getArrayAttr(attrListElems);
450 spirv::ExecutionModeOp::create(
451 opBuilder, unknownLoc,
452 SymbolRefAttr::get(opBuilder.getContext(), fn.getName()), execMode,
460 unsigned wordIndex = 0;
461 unsigned const wordsSize = words.size();
462 if (wordIndex >= wordsSize)
464 "missing function result <id> in OpExecutionModeId");
467 uint32_t fnID = words[wordIndex++];
468 FuncOp fn = getFunction(fnID);
470 return emitError(unknownLoc,
"no function matching <id> ") << fnID;
473 if (wordIndex >= wordsSize)
474 return emitError(unknownLoc,
"missing Execution Mode in OpExecutionModeId");
476 ExecutionModeAttr execMode = spirv::ExecutionModeAttr::get(
477 context,
static_cast<spirv::ExecutionMode
>(words[wordIndex++]));
481 while (wordIndex < words.size()) {
482 std::string
id = getSpecConstantSymbol(words[wordIndex++]);
485 ArrayAttr values = opBuilder.getArrayAttr(attrListElems);
486 spirv::ExecutionModeIdOp::create(
487 opBuilder, unknownLoc,
488 SymbolRefAttr::get(opBuilder.getContext(), fn.getName()), execMode,
496 if (operands.size() < 3) {
498 "OpFunctionCall must have at least 3 operands");
503 return emitError(unknownLoc,
"undefined result type from <id> ")
508 if (isVoidType(resultType))
509 resultType =
nullptr;
511 auto resultID = operands[1];
512 auto functionID = operands[2];
514 auto functionName = getFunctionSymbol(functionID);
517 for (
auto operand : llvm::drop_begin(operands, 3)) {
518 auto value = getValue(operand);
520 return emitError(unknownLoc,
"unknown <id> ")
521 << operand <<
" used by OpFunctionCall";
523 arguments.push_back(value);
526 auto opFunctionCall = spirv::FunctionCallOp::create(
527 opBuilder, unknownLoc, resultType,
528 SymbolRefAttr::get(opBuilder.getContext(), functionName), arguments);
531 valueMap[resultID] = opFunctionCall.getResult(0);
539 size_t wordIndex = 0;
543 if (wordIndex < words.size()) {
544 auto arg = getValue(words[wordIndex]);
547 return emitError(unknownLoc,
"unknown result <id> : ")
551 operands.push_back(arg);
555 if (wordIndex < words.size()) {
556 auto arg = getValue(words[wordIndex]);
559 return emitError(unknownLoc,
"unknown result <id> : ")
563 operands.push_back(arg);
567 bool isAlignedAttr =
false;
569 if (wordIndex < words.size()) {
570 auto attrValue = words[wordIndex++];
571 auto attr = opBuilder.getAttr<spirv::MemoryAccessAttr>(
572 static_cast<spirv::MemoryAccess
>(attrValue));
573 attributes.push_back(
574 opBuilder.getNamedAttr(attributeName<MemoryAccess>(), attr));
575 isAlignedAttr = (attrValue == 2);
578 if (isAlignedAttr && wordIndex < words.size()) {
579 attributes.push_back(opBuilder.getNamedAttr(
580 "alignment", opBuilder.getI32IntegerAttr(words[wordIndex++])));
583 if (wordIndex < words.size()) {
584 auto attrValue = words[wordIndex++];
585 auto attr = opBuilder.getAttr<spirv::MemoryAccessAttr>(
586 static_cast<spirv::MemoryAccess
>(attrValue));
587 attributes.push_back(opBuilder.getNamedAttr(
"source_memory_access", attr));
590 if (wordIndex < words.size()) {
591 attributes.push_back(opBuilder.getNamedAttr(
592 "source_alignment", opBuilder.getI32IntegerAttr(words[wordIndex++])));
595 if (wordIndex != words.size()) {
597 "found more operands than expected when deserializing "
598 "spirv::CopyMemoryOp, only ")
599 << wordIndex <<
" of " << words.size() <<
" processed";
602 Location loc = createFileLineColLoc(opBuilder);
603 spirv::CopyMemoryOp::create(opBuilder, loc, resultTypes, operands,
612 if (words.size() != 4) {
614 "expected 4 words in GenericCastToPtrExplicitOp"
620 uint32_t valueID = 0;
624 return emitError(unknownLoc,
"unknown type result <id> : ") << words[0];
625 resultTypes.push_back(type);
629 auto arg = getValue(words[2]);
631 return emitError(unknownLoc,
"unknown result <id> : ") << words[2];
632 operands.push_back(arg);
634 Location loc = createFileLineColLoc(opBuilder);
635 Operation *op = spirv::GenericCastToPtrExplicitOp::create(
636 opBuilder, loc, resultTypes, operands);
643#define GET_DESERIALIZATION_FNS
644#include "mlir/Dialect/SPIRV/IR/SPIRVSerialization.inc"
static spirv::Opcode extractOpcode(uint32_t word)
Extracts the opcode from the given first word of a SPIR-V instruction.
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class provides the API for ops that are known to be terminators.
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, PropertyRef properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Value materializeSpecConstantOperation(uint32_t resultID, spirv::Opcode enclosedOpcode, uint32_t resultTypeID, ArrayRef< uint32_t > enclosedOpOperands)
Materializes/emits an OpSpecConstantOp instruction.
Value getValue(uint32_t id)
Get the Value associated with a result <id>.
LogicalResult processGlobalVariable(ArrayRef< uint32_t > operands)
Processes the OpVariable instructions at current offset into binary.
std::optional< SpecConstOperationMaterializationInfo > getSpecConstantOperation(uint32_t id)
Gets the info needed to materialize the spec constant operation op associated with the given <id>.
LogicalResult dispatchToAutogenDeserialization(spirv::Opcode opcode, ArrayRef< uint32_t > words)
Method to dispatch to the specialized deserialization function for an operation in SPIR-V dialect tha...
LogicalResult processConstantNull(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpConstantNull instruction with the given operands.
LogicalResult processSpecConstantComposite(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpSpecConstantComposite instruction with the given operands.
LogicalResult processInstruction(spirv::Opcode opcode, ArrayRef< uint32_t > operands, bool deferInstructions=true)
Processes a SPIR-V instruction with the given opcode and operands.
LogicalResult processBranchConditional(ArrayRef< uint32_t > operands)
spirv::GlobalVariableOp getGlobalVariable(uint32_t id)
Gets the global variable associated with a result <id> of OpVariable.
LogicalResult processGraphARM(ArrayRef< uint32_t > operands)
LogicalResult processLabel(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpLabel instruction with the given operands.
LogicalResult processExtInst(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpExtInst with given operands.
std::optional< spirv::GraphConstantARMOpMaterializationInfo > getGraphConstantARM(uint32_t id)
Gets the GraphConstantARM ID attribute and result type with the given result <id>.
std::optional< std::pair< Attribute, Type > > getConstant(uint32_t id)
Gets the constant's attribute and type associated with the given <id>.
LogicalResult processType(spirv::Opcode opcode, ArrayRef< uint32_t > operands)
Processes a SPIR-V type instruction with given opcode and operands and registers the type into module...
LogicalResult processLoopMerge(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpLoopMerge instruction with the given operands.
LogicalResult sliceInstruction(spirv::Opcode &opcode, ArrayRef< uint32_t > &operands, std::optional< spirv::Opcode > expectedOpcode=std::nullopt)
Slices the first instruction out of binary and returns its opcode and operands via opcode and operand...
spirv::SpecConstantCompositeOp getSpecConstantComposite(uint32_t id)
Gets the composite specialization constant with the given result <id>.
spirv::EXTSpecConstantCompositeReplicateOp getSpecConstantCompositeReplicate(uint32_t id)
Gets the replicated composite specialization constant with the given result <id>.
LogicalResult processOp(ArrayRef< uint32_t > words)
Method to deserialize an operation in the SPIR-V dialect that is a mirror of an instruction in the SP...
Type getUndefType(uint32_t id)
Get the type associated with the result <id> of an OpUndef.
LogicalResult processSpecConstantCompositeReplicateEXT(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpSpecConstantCompositeReplicateEXT instruction with the given operands.
LogicalResult processGraphEntryPointARM(ArrayRef< uint32_t > operands)
LogicalResult processFunction(ArrayRef< uint32_t > operands)
Creates a deserializer for the given SPIR-V binary module.
LogicalResult processDebugString(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpString instruction with the given operands.
LogicalResult dispatchToExtensionSetAutogenDeserialization(StringRef extensionSetName, uint32_t instructionID, ArrayRef< uint32_t > words)
Dispatches the deserialization of extended instruction set operation based on the extended instructio...
LogicalResult processPhi(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpPhi instruction with the given operands.
void clearDebugLine()
Discontinues any source-level location information that might be active from a previous OpLine instru...
LogicalResult processTypeForwardPointer(ArrayRef< uint32_t > operands)
LogicalResult processSwitch(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpSwitch instruction with the given operands.
LogicalResult processOpWithoutGrammarAttr(ArrayRef< uint32_t > words, StringRef opName, bool hasResult, unsigned numOperands)
Processes a SPIR-V instruction from the given operands.
LogicalResult processGraphEndARM(ArrayRef< uint32_t > operands)
LogicalResult processConstantComposite(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpConstantComposite instruction with the given operands.
LogicalResult processBranch(ArrayRef< uint32_t > operands)
std::optional< std::pair< Attribute, Type > > getConstantCompositeReplicate(uint32_t id)
Gets the replicated composite constant's attribute and type associated with the given <id>.
LogicalResult processUndef(ArrayRef< uint32_t > operands)
Processes a OpUndef instruction.
LogicalResult processSpecConstantOperation(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpSpecConstantOp instruction with the given operands.
LogicalResult processConstant(ArrayRef< uint32_t > operands, bool isSpec)
Processes a SPIR-V Op{|Spec}Constant instruction with the given operands.
Location createFileLineColLoc(OpBuilder opBuilder)
Creates a FileLineColLoc with the OpLine location information.
LogicalResult processGraphConstantARM(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpGraphConstantARM instruction with the given operands.
LogicalResult processConstantBool(bool isTrue, ArrayRef< uint32_t > operands, bool isSpec)
Processes a SPIR-V Op{|Spec}Constant{True|False} instruction with the given operands.
spirv::SpecConstantOp getSpecConstant(uint32_t id)
Gets the specialization constant with the given result <id>.
LogicalResult processConstantCompositeReplicateEXT(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpConstantCompositeReplicateEXT instruction with the given operands.
LogicalResult processSelectionMerge(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpSelectionMerge instruction with the given operands.
LogicalResult processOpGraphSetOutputARM(ArrayRef< uint32_t > operands)
LogicalResult processDebugLine(ArrayRef< uint32_t > operands)
Processes a SPIR-V OpLine instruction with the given operands.
StringRef decodeStringLiteral(ArrayRef< uint32_t > words, unsigned &wordIndex)
Decodes a string literal in words starting at wordIndex.
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
void addOperands(ValueRange newOperands)
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.
void addTypes(ArrayRef< Type > newTypes)