#include "Serializer.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/ADT/bit.h"
#include "llvm/Support/Debug.h"
#include <cstdint>
#include <optional>
#define DEBUG_TYPE …
usingnamespacemlir;
static Block *getStructuredControlFlowOpMergeBlock(Operation *op) { … }
static Block *getPhiIncomingBlock(Block *block) { … }
namespace mlir {
namespace spirv {
void encodeInstructionInto(SmallVectorImpl<uint32_t> &binary, spirv::Opcode op,
ArrayRef<uint32_t> operands) { … }
Serializer::Serializer(spirv::ModuleOp module,
const SerializationOptions &options)
: … { … }
LogicalResult Serializer::serialize() { … }
void Serializer::collect(SmallVectorImpl<uint32_t> &binary) { … }
#ifndef NDEBUG
void Serializer::printValueIDMap(raw_ostream &os) {
os << "\n= Value <id> Map =\n\n";
for (auto valueIDPair : valueIDMap) {
Value val = valueIDPair.first;
os << " " << val << " "
<< "id = " << valueIDPair.second << ' ';
if (auto *op = val.getDefiningOp()) {
os << "from op '" << op->getName() << "'";
} else if (auto arg = dyn_cast<BlockArgument>(val)) {
Block *block = arg.getOwner();
os << "from argument of block " << block << ' ';
os << " in op '" << block->getParentOp()->getName() << "'";
}
os << '\n';
}
}
#endif
uint32_t Serializer::getOrCreateFunctionID(StringRef fnName) { … }
void Serializer::processCapability() { … }
void Serializer::processDebugInfo() { … }
void Serializer::processExtension() { … }
void Serializer::processMemoryModel() { … }
static std::string getDecorationName(StringRef attrName) { … }
LogicalResult Serializer::processDecorationAttr(Location loc, uint32_t resultID,
Decoration decoration,
Attribute attr) { … }
LogicalResult Serializer::processDecoration(Location loc, uint32_t resultID,
NamedAttribute attr) { … }
LogicalResult Serializer::processName(uint32_t resultID, StringRef name) { … }
template <>
LogicalResult Serializer::processTypeDecoration<spirv::ArrayType>(
Location loc, spirv::ArrayType type, uint32_t resultID) { … }
template <>
LogicalResult Serializer::processTypeDecoration<spirv::RuntimeArrayType>(
Location loc, spirv::RuntimeArrayType type, uint32_t resultID) { … }
LogicalResult Serializer::processMemberDecoration(
uint32_t structID,
const spirv::StructType::MemberDecorationInfo &memberDecoration) { … }
bool Serializer::isInterfaceStructPtrType(Type type) const { … }
LogicalResult Serializer::processType(Location loc, Type type,
uint32_t &typeID) { … }
LogicalResult
Serializer::processTypeImpl(Location loc, Type type, uint32_t &typeID,
SetVector<StringRef> &serializationCtx) { … }
LogicalResult Serializer::prepareBasicType(
Location loc, Type type, uint32_t resultID, spirv::Opcode &typeEnum,
SmallVectorImpl<uint32_t> &operands, bool &deferSerialization,
SetVector<StringRef> &serializationCtx) { … }
LogicalResult
Serializer::prepareFunctionType(Location loc, FunctionType type,
spirv::Opcode &typeEnum,
SmallVectorImpl<uint32_t> &operands) { … }
uint32_t Serializer::prepareConstant(Location loc, Type constType,
Attribute valueAttr) { … }
uint32_t Serializer::prepareArrayConstant(Location loc, Type constType,
ArrayAttr attr) { … }
uint32_t
Serializer::prepareDenseElementsConstant(Location loc, Type constType,
DenseElementsAttr valueAttr, int dim,
MutableArrayRef<uint64_t> index) { … }
uint32_t Serializer::prepareConstantScalar(Location loc, Attribute valueAttr,
bool isSpec) { … }
uint32_t Serializer::prepareConstantBool(Location loc, BoolAttr boolAttr,
bool isSpec) { … }
uint32_t Serializer::prepareConstantInt(Location loc, IntegerAttr intAttr,
bool isSpec) { … }
uint32_t Serializer::prepareConstantFp(Location loc, FloatAttr floatAttr,
bool isSpec) { … }
uint32_t Serializer::getOrCreateBlockID(Block *block) { … }
#ifndef NDEBUG
void Serializer::printBlock(Block *block, raw_ostream &os) {
os << "block " << block << " (id = ";
if (uint32_t id = getBlockID(block))
os << id;
else
os << "unknown";
os << ")\n";
}
#endif
LogicalResult
Serializer::processBlock(Block *block, bool omitLabel,
function_ref<LogicalResult()> emitMerge) { … }
LogicalResult Serializer::emitPhiForBlockArguments(Block *block) { … }
LogicalResult Serializer::encodeExtensionInstruction(
Operation *op, StringRef extensionSetName, uint32_t extensionOpcode,
ArrayRef<uint32_t> operands) { … }
LogicalResult Serializer::processOperation(Operation *opInst) { … }
LogicalResult Serializer::processOpWithoutGrammarAttr(Operation *op,
StringRef extInstSet,
uint32_t opcode) { … }
LogicalResult Serializer::emitDecoration(uint32_t target,
spirv::Decoration decoration,
ArrayRef<uint32_t> params) { … }
LogicalResult Serializer::emitDebugLine(SmallVectorImpl<uint32_t> &binary,
Location loc) { … }
}
}