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 processSpecConstantOp(spirv::SpecConstantOp op);
114 processSpecConstantCompositeOp(spirv::SpecConstantCompositeOp op);
117 processSpecConstantOperationOp(spirv::SpecConstantOperationOp op);
123 LogicalResult processUndefOp(spirv::UndefOp op);
126 LogicalResult processName(uint32_t resultID, StringRef name);
129 LogicalResult processFuncOp(spirv::FuncOp op);
130 LogicalResult processFuncParameter(spirv::FuncOp op);
132 LogicalResult processVariableOp(spirv::VariableOp op);
135 LogicalResult processGlobalVariableOp(spirv::GlobalVariableOp varOp);
138 LogicalResult processDecorationAttr(
Location loc, uint32_t resultID,
140 LogicalResult processDecoration(
Location loc, uint32_t resultID,
143 template <
typename DType>
144 LogicalResult processTypeDecoration(
Location loc, DType type,
146 return emitError(loc,
"unhandled decoration for type:") << type;
150 LogicalResult processMemberDecoration(
158 uint32_t getTypeID(
Type type)
const {
return typeIDMap.lookup(type); }
162 bool isVoidType(
Type type)
const {
return isa<NoneType>(type); }
166 bool isInterfaceStructPtrType(
Type type)
const;
170 LogicalResult processType(
Location loc,
Type type, uint32_t &typeID);
171 LogicalResult processTypeImpl(
Location loc,
Type type, uint32_t &typeID,
176 LogicalResult prepareBasicType(
Location loc,
Type type, uint32_t resultID,
177 spirv::Opcode &typeEnum,
179 bool &deferSerialization,
182 LogicalResult prepareFunctionType(
Location loc, FunctionType type,
183 spirv::Opcode &typeEnum,
190 uint32_t getConstantID(
Attribute value)
const {
191 return constIDMap.lookup(value);
204 uint32_t prepareArrayConstant(
Location loc,
Type constType, ArrayAttr attr);
213 uint32_t prepareDenseElementsConstant(
Location loc,
Type constType,
222 bool isSpec =
false);
225 bool isSpec =
false);
227 uint32_t prepareConstantInt(
Location loc, IntegerAttr intAttr,
228 bool isSpec =
false);
230 uint32_t prepareConstantFp(
Location loc, FloatAttr floatAttr,
231 bool isSpec =
false);
238 uint32_t getBlockID(
Block *block)
const {
return blockIDMap.lookup(block); }
242 uint32_t getOrCreateBlockID(
Block *block);
246 void printBlock(
Block *block, raw_ostream &os);
254 LogicalResult processBlock(
Block *block,
bool omitLabel =
false,
258 LogicalResult emitPhiForBlockArguments(
Block *block);
260 LogicalResult processSelectionOp(spirv::SelectionOp selectionOp);
262 LogicalResult processLoopOp(spirv::LoopOp loopOp);
264 LogicalResult processBranchConditionalOp(spirv::BranchConditionalOp);
266 LogicalResult processBranchOp(spirv::BranchOp branchOp);
272 LogicalResult encodeExtensionInstruction(
Operation *op,
273 StringRef extensionSetName,
277 uint32_t getValueID(
Value val)
const {
return valueIDMap.lookup(val); }
279 LogicalResult processAddressOfOp(spirv::AddressOfOp addressOfOp);
281 LogicalResult processReferenceOfOp(spirv::ReferenceOfOp referenceOfOp);
284 LogicalResult processOperation(
Operation *op);
291 LogicalResult processOpWithoutGrammarAttr(
Operation *op, StringRef extInstSet,
298 LogicalResult dispatchToAutogenSerialization(
Operation *op);
303 template <
typename OpTy>
304 LogicalResult processOp(OpTy op) {
305 return op.emitError(
"unsupported op serialization");
314 LogicalResult emitDecoration(uint32_t target, spirv::Decoration decoration,
323 spirv::ModuleOp module;
335 bool lastProcessedWasMergeInst =
false;
364 struct RecursiveStructPointerInfo {
365 uint32_t pointerTypeID;
366 spirv::StorageClass storageClass;
371 recursiveStructInfos;
393 llvm::StringMap<uint32_t> specConstIDMap;
396 llvm::StringMap<uint32_t> globalVarIDMap;
399 llvm::StringMap<uint32_t> funcIDMap;
411 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.