13 #ifndef MLIR_LIB_TARGET_SPIRV_SERIALIZATION_SERIALIZER_H
14 #define MLIR_LIB_TARGET_SPIRV_SERIALIZATION_SERIALIZER_H
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/raw_ostream.h"
27 ArrayRef<uint32_t> operands);
79 uint32_t getNextID() {
return nextID++; }
85 uint32_t getSpecConstID(StringRef constName)
const {
86 return specConstIDMap.lookup(constName);
89 uint32_t getVariableID(StringRef varName)
const {
90 return globalVarIDMap.lookup(varName);
93 uint32_t getFunctionID(StringRef fnName)
const {
94 return funcIDMap.lookup(fnName);
99 uint32_t getOrCreateFunctionID(StringRef fnName);
101 void processCapability();
103 void processDebugInfo();
105 LogicalResult processExtension();
107 void processMemoryModel();
109 LogicalResult processConstantOp(spirv::ConstantOp op);
111 LogicalResult processConstantCompositeReplicateOp(
112 spirv::EXTConstantCompositeReplicateOp op);
114 LogicalResult processSpecConstantOp(spirv::SpecConstantOp op);
117 processSpecConstantCompositeOp(spirv::SpecConstantCompositeOp op);
119 LogicalResult processSpecConstantCompositeReplicateOp(
120 spirv::EXTSpecConstantCompositeReplicateOp op);
123 processSpecConstantOperationOp(spirv::SpecConstantOperationOp op);
125 LogicalResult processGraphConstantARMOp(spirv::GraphConstantARMOp op);
131 LogicalResult processUndefOp(spirv::UndefOp op);
134 LogicalResult processName(uint32_t resultID, StringRef name);
137 LogicalResult processFuncOp(spirv::FuncOp op);
138 LogicalResult processFuncParameter(spirv::FuncOp op);
141 LogicalResult processGraphARMOp(spirv::GraphARMOp op);
144 LogicalResult processGraphEntryPointARMOp(spirv::GraphEntryPointARMOp op);
147 LogicalResult processGraphOutputsARMOp(spirv::GraphOutputsARMOp op);
149 LogicalResult processVariableOp(spirv::VariableOp op);
152 LogicalResult processGlobalVariableOp(spirv::GlobalVariableOp varOp);
155 LogicalResult processDecorationAttr(
Location loc, uint32_t resultID,
157 LogicalResult processDecoration(
Location loc, uint32_t resultID,
160 template <
typename DType>
161 LogicalResult processTypeDecoration(
Location loc, DType type,
163 return emitError(loc,
"unhandled decoration for type:") << type;
167 LogicalResult processMemberDecoration(
175 uint32_t getTypeID(
Type type)
const {
return typeIDMap.lookup(type); }
179 bool isVoidType(
Type type)
const {
return isa<NoneType>(type); }
183 bool isInterfaceStructPtrType(
Type type)
const;
187 LogicalResult processType(
Location loc,
Type type, uint32_t &typeID);
188 LogicalResult processTypeImpl(
Location loc,
Type type, uint32_t &typeID,
193 LogicalResult prepareBasicType(
Location loc,
Type type, uint32_t resultID,
194 spirv::Opcode &typeEnum,
196 bool &deferSerialization,
199 LogicalResult prepareFunctionType(
Location loc, FunctionType type,
200 spirv::Opcode &typeEnum,
203 LogicalResult prepareGraphType(
Location loc, GraphType type,
204 spirv::Opcode &typeEnum,
211 uint32_t getConstantID(
Attribute value)
const {
212 return constIDMap.lookup(value);
215 uint32_t getConstantCompositeReplicateID(
216 std::pair<Attribute, Type> valueTypePair)
const {
217 return constCompositeReplicateIDMap.lookup(valueTypePair);
230 uint32_t prepareArrayConstant(
Location loc,
Type constType, ArrayAttr attr);
239 uint32_t prepareDenseElementsConstant(
Location loc,
Type constType,
248 bool isSpec =
false);
251 bool isSpec =
false);
253 uint32_t prepareConstantInt(
Location loc, IntegerAttr intAttr,
254 bool isSpec =
false);
256 uint32_t getGraphConstantARMId(
Attribute value)
const {
257 return graphConstIDMap.lookup(value);
260 uint32_t prepareGraphConstantId(
Location loc,
Type graphConstType,
261 IntegerAttr intAttr);
263 uint32_t prepareConstantFp(
Location loc, FloatAttr floatAttr,
264 bool isSpec =
false);
269 uint32_t prepareConstantCompositeReplicate(
Location loc,
Type resultType,
277 uint32_t getBlockID(
Block *block)
const {
return blockIDMap.lookup(block); }
281 uint32_t getOrCreateBlockID(
Block *block);
285 void printBlock(
Block *block, raw_ostream &os);
293 LogicalResult processBlock(
Block *block,
bool omitLabel =
false,
297 LogicalResult emitPhiForBlockArguments(
Block *block);
299 LogicalResult processSelectionOp(spirv::SelectionOp selectionOp);
301 LogicalResult processLoopOp(spirv::LoopOp loopOp);
303 LogicalResult processBranchConditionalOp(spirv::BranchConditionalOp);
305 LogicalResult processBranchOp(spirv::BranchOp branchOp);
311 LogicalResult encodeExtensionInstruction(
Operation *op,
312 StringRef extensionSetName,
316 uint32_t getValueID(
Value val)
const {
return valueIDMap.lookup(val); }
318 LogicalResult processAddressOfOp(spirv::AddressOfOp addressOfOp);
320 LogicalResult processReferenceOfOp(spirv::ReferenceOfOp referenceOfOp);
323 LogicalResult processOperation(
Operation *op);
330 LogicalResult processOpWithoutGrammarAttr(
Operation *op, StringRef extInstSet,
337 LogicalResult dispatchToAutogenSerialization(
Operation *op);
342 template <
typename OpTy>
343 LogicalResult processOp(OpTy op) {
344 return op.emitError(
"unsupported op serialization");
353 LogicalResult emitDecoration(uint32_t target, spirv::Decoration decoration,
362 spirv::ModuleOp module;
374 bool lastProcessedWasMergeInst =
false;
404 struct RecursiveStructPointerInfo {
405 uint32_t pointerTypeID;
406 spirv::StorageClass storageClass;
411 recursiveStructInfos;
443 llvm::StringMap<uint32_t> specConstIDMap;
449 llvm::StringMap<uint32_t> globalVarIDMap;
452 llvm::StringMap<uint32_t> funcIDMap;
464 llvm::StringMap<uint32_t> extendedInstSetIDMap;
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
This class is a general helper class for creating context-global objects like types,...
An attribute that represents a reference to a dense vector or tensor object.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
NamedAttribute represents a combination of a name and an Attribute value.
Operation is the basic unit of execution within MLIR.
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...
A SPIR-V module serializer.
void printValueIDMap(raw_ostream &os)
(For debugging) prints each value and its corresponding result <id>.
Serializer(spirv::ModuleOp module, const SerializationOptions &options)
Creates a serializer for the given SPIR-V module.
LogicalResult serialize()
Serializes the remembered SPIR-V module.
void collect(SmallVectorImpl< uint32_t > &binary)
Collects the final SPIR-V binary.
void encodeInstructionInto(SmallVectorImpl< uint32_t > &binary, spirv::Opcode op, ArrayRef< uint32_t > operands)
Encodes an SPIR-V instruction with the given opcode and operands into the given binary vector.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.