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"
44 auto *start = input->getBufferStart();
45 auto size = input->getBufferSize();
46 if (size %
sizeof(uint32_t) != 0) {
48 <<
"SPIR-V binary module must contain integral number of 32-bit words";
52 auto binary =
llvm::ArrayRef(
reinterpret_cast<const uint32_t *
>(start),
53 size /
sizeof(uint32_t));
60 "deserialize-spirv",
"deserializes the SPIR-V module",
61 [](llvm::SourceMgr &sourceMgr,
MLIRContext *context) {
62 assert(sourceMgr.getNumBuffers() == 1 &&
"expected one buffer");
64 sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), context);
74 raw_ostream &output) {
79 output.write(
reinterpret_cast<char *
>(binary.data()),
80 binary.size() *
sizeof(uint32_t));
82 return mlir::success();
88 "serialize-spirv",
"serialize SPIR-V dialect",
89 [](spirv::ModuleOp module, raw_ostream &output) {
93 registry.insert<spirv::SPIRVDialect>();
103 raw_ostream &output) {
108 options.emitDebugInfo = emitDebugInfo;
120 spirvModule->print(output);
122 return mlir::success();
128 "test-spirv-roundtrip",
"test roundtrip in SPIR-V dialect",
129 [](spirv::ModuleOp module, raw_ostream &output) {
133 registry.insert<spirv::SPIRVDialect>();
139 "test-spirv-roundtrip-debug",
"test roundtrip debug in SPIR-V",
140 [](spirv::ModuleOp module, raw_ostream &output) {
144 registry.insert<spirv::SPIRVDialect>();
static llvm::ManagedStatic< PassManagerOptions > options
static LogicalResult serializeModule(spirv::ModuleOp module, raw_ostream &output)
static LogicalResult roundTripModule(spirv::ModuleOp module, bool emitDebugInfo, raw_ostream &output)
static OwningOpRef< Operation * > deserializeModule(const llvm::MemoryBuffer *input, MLIRContext *context)
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)
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...