25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/SMLoc.h"
28 #include "llvm/Support/SourceMgr.h"
29 #include "llvm/Support/ToolOutputFile.h"
45 auto *start = input->getBufferStart();
46 auto size = input->getBufferSize();
47 if (size %
sizeof(uint32_t) != 0) {
49 <<
"SPIR-V binary module must contain integral number of 32-bit words";
53 auto binary =
llvm::ArrayRef(
reinterpret_cast<const uint32_t *
>(start),
54 size /
sizeof(uint32_t));
60 static llvm::cl::opt<bool> enableControlFlowStructurization(
61 "spirv-structurize-control-flow",
63 "Enable control flow structurization into `spirv.mlir.selection` and "
64 "`spirv.mlir.loop`. This may need to be disabled to support "
65 "deserialization of early exits (see #138688)"),
66 llvm::cl::init(
true));
69 "deserialize-spirv",
"deserializes the SPIR-V module",
70 [](llvm::SourceMgr &sourceMgr,
MLIRContext *context) {
71 assert(sourceMgr.getNumBuffers() == 1 &&
"expected one buffer");
73 sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), context,
74 {enableControlFlowStructurization});
84 raw_ostream &output) {
89 output.write(
reinterpret_cast<char *
>(binary.data()),
90 binary.size() *
sizeof(uint32_t));
92 return mlir::success();
98 "serialize-spirv",
"serialize SPIR-V dialect",
99 [](spirv::ModuleOp module, raw_ostream &output) {
103 registry.insert<spirv::SPIRVDialect>();
113 raw_ostream &output) {
118 options.emitDebugInfo = emitDebugInfo;
130 spirvModule->print(output);
132 return mlir::success();
138 "test-spirv-roundtrip",
"test roundtrip in SPIR-V dialect",
139 [](spirv::ModuleOp module, raw_ostream &output) {
143 registry.insert<spirv::SPIRVDialect>();
149 "test-spirv-roundtrip-debug",
"test roundtrip debug in SPIR-V",
150 [](spirv::ModuleOp module, raw_ostream &output) {
154 registry.insert<spirv::SPIRVDialect>();
static llvm::ManagedStatic< PassManagerOptions > options
static LogicalResult serializeModule(spirv::ModuleOp module, raw_ostream &output)
static OwningOpRef< Operation * > deserializeModule(const llvm::MemoryBuffer *input, MLIRContext *context, const spirv::DeserializationOptions &options)
static LogicalResult roundTripModule(spirv::ModuleOp module, bool emitDebugInfo, raw_ostream &output)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
const DialectRegistry & getDialectRegistry()
Return the dialect registry associated with this context.
void loadDialect()
Load a dialect in the context.
void loadAllAvailableDialects()
Load all dialects available in the registry in this context.
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
OwningOpRef< spirv::ModuleOp > deserialize(ArrayRef< uint32_t > binary, MLIRContext *context, const DeserializationOptions &options={})
Deserializes the given SPIR-V binary module and creates a MLIR ModuleOp in the given context.
LogicalResult serialize(ModuleOp module, SmallVectorImpl< uint32_t > &binary, const SerializationOptions &options={})
Serializes the given SPIR-V module and writes to binary.
Include the generated interface declarations.
void registerTestRoundtripSPIRV()
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
void registerFromSPIRVTranslation()
void registerTestRoundtripDebugSPIRV()
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void registerToSPIRVTranslation()
Use Translate[ToMLIR|FromMLIR]Registration as an initializer that registers a function and associates...