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 void 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);
129 LogicalResult processUndefOp(spirv::UndefOp op);
132 LogicalResult processName(uint32_t resultID, StringRef name);
135 LogicalResult processFuncOp(spirv::FuncOp op);
136 LogicalResult processFuncParameter(spirv::FuncOp op);
138 LogicalResult processVariableOp(spirv::VariableOp op);
141 LogicalResult processGlobalVariableOp(spirv::GlobalVariableOp varOp);
144 LogicalResult processDecorationAttr(
Location loc, uint32_t resultID,
146 LogicalResult processDecoration(
Location loc, uint32_t resultID,
149 template <
typename DType>
150 LogicalResult processTypeDecoration(
Location loc, DType type,
152 return emitError(loc,
"unhandled decoration for type:") << type;
156 LogicalResult processMemberDecoration(
164 uint32_t getTypeID(
Type type)
const {
return typeIDMap.lookup(type); }
168 bool isVoidType(
Type type)
const {
return isa<NoneType>(type); }
172 bool isInterfaceStructPtrType(
Type type)
const;
176 LogicalResult processType(
Location loc,
Type type, uint32_t &typeID);
177 LogicalResult processTypeImpl(
Location loc,
Type type, uint32_t &typeID,
182 LogicalResult prepareBasicType(
Location loc,
Type type, uint32_t resultID,
183 spirv::Opcode &typeEnum,
185 bool &deferSerialization,
188 LogicalResult prepareFunctionType(
Location loc, FunctionType type,
189 spirv::Opcode &typeEnum,
196 uint32_t getConstantID(
Attribute value)
const {
197 return constIDMap.lookup(value);
200 uint32_t getConstantCompositeReplicateID(
201 std::pair<Attribute, Type> valueTypePair)
const {
202 return constCompositeReplicateIDMap.lookup(valueTypePair);
215 uint32_t prepareArrayConstant(
Location loc,
Type constType, ArrayAttr attr);
224 uint32_t prepareDenseElementsConstant(
Location loc,
Type constType,
233 bool isSpec =
false);
236 bool isSpec =
false);
238 uint32_t prepareConstantInt(
Location loc, IntegerAttr intAttr,
239 bool isSpec =
false);
241 uint32_t prepareConstantFp(
Location loc, FloatAttr floatAttr,
242 bool isSpec =
false);
247 uint32_t prepareConstantCompositeReplicate(
Location loc,
Type resultType,
255 uint32_t getBlockID(
Block *block)
const {
return blockIDMap.lookup(block); }
259 uint32_t getOrCreateBlockID(
Block *block);
263 void printBlock(
Block *block, raw_ostream &os);
271 LogicalResult processBlock(
Block *block,
bool omitLabel =
false,
275 LogicalResult emitPhiForBlockArguments(
Block *block);
277 LogicalResult processSelectionOp(spirv::SelectionOp selectionOp);
279 LogicalResult processLoopOp(spirv::LoopOp loopOp);
281 LogicalResult processBranchConditionalOp(spirv::BranchConditionalOp);
283 LogicalResult processBranchOp(spirv::BranchOp branchOp);
289 LogicalResult encodeExtensionInstruction(
Operation *op,
290 StringRef extensionSetName,
294 uint32_t getValueID(
Value val)
const {
return valueIDMap.lookup(val); }
296 LogicalResult processAddressOfOp(spirv::AddressOfOp addressOfOp);
298 LogicalResult processReferenceOfOp(spirv::ReferenceOfOp referenceOfOp);
301 LogicalResult processOperation(
Operation *op);
308 LogicalResult processOpWithoutGrammarAttr(
Operation *op, StringRef extInstSet,
315 LogicalResult dispatchToAutogenSerialization(
Operation *op);
320 template <
typename OpTy>
321 LogicalResult processOp(OpTy op) {
322 return op.emitError(
"unsupported op serialization");
331 LogicalResult emitDecoration(uint32_t target, spirv::Decoration decoration,
340 spirv::ModuleOp module;
352 bool lastProcessedWasMergeInst =
false;
381 struct RecursiveStructPointerInfo {
382 uint32_t pointerTypeID;
383 spirv::StorageClass storageClass;
388 recursiveStructInfos;
413 llvm::StringMap<uint32_t> specConstIDMap;
416 llvm::StringMap<uint32_t> globalVarIDMap;
419 llvm::StringMap<uint32_t> funcIDMap;
431 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.