| 1 | //===- TranslateRegistration.cpp - hooks to mlir-translate ----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a translation from SPIR-V binary module to MLIR SPIR-V |
| 10 | // ModuleOp. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" |
| 15 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
| 16 | #include "mlir/IR/Builders.h" |
| 17 | #include "mlir/IR/Verifier.h" |
| 18 | #include "mlir/Parser/Parser.h" |
| 19 | #include "mlir/Support/FileUtilities.h" |
| 20 | #include "mlir/Target/SPIRV/Deserialization.h" |
| 21 | #include "mlir/Target/SPIRV/Serialization.h" |
| 22 | #include "mlir/Tools/mlir-translate/Translation.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/Support/FileSystem.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
| 26 | #include "llvm/Support/Path.h" |
| 27 | #include "llvm/Support/SourceMgr.h" |
| 28 | #include "llvm/Support/ToolOutputFile.h" |
| 29 | |
| 30 | using namespace mlir; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Deserialization registration |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | // Deserializes the SPIR-V binary module stored in the file named as |
| 37 | // `inputFilename` and returns a module containing the SPIR-V module. |
| 38 | static OwningOpRef<Operation *> |
| 39 | deserializeModule(const llvm::MemoryBuffer *input, MLIRContext *context, |
| 40 | const spirv::DeserializationOptions &options) { |
| 41 | context->loadDialect<spirv::SPIRVDialect>(); |
| 42 | |
| 43 | // Make sure the input stream can be treated as a stream of SPIR-V words |
| 44 | auto *start = input->getBufferStart(); |
| 45 | auto size = input->getBufferSize(); |
| 46 | if (size % sizeof(uint32_t) != 0) { |
| 47 | emitError(loc: UnknownLoc::get(context)) |
| 48 | << "SPIR-V binary module must contain integral number of 32-bit words" ; |
| 49 | return {}; |
| 50 | } |
| 51 | |
| 52 | auto binary = llvm::ArrayRef(reinterpret_cast<const uint32_t *>(start), |
| 53 | size / sizeof(uint32_t)); |
| 54 | return spirv::deserialize(binary, context, options); |
| 55 | } |
| 56 | |
| 57 | namespace mlir { |
| 58 | void registerFromSPIRVTranslation() { |
| 59 | static llvm::cl::opt<bool> enableControlFlowStructurization( |
| 60 | "spirv-structurize-control-flow" , |
| 61 | llvm::cl::desc( |
| 62 | "Enable control flow structurization into `spirv.mlir.selection` and " |
| 63 | "`spirv.mlir.loop`. This may need to be disabled to support " |
| 64 | "deserialization of early exits (see #138688)" ), |
| 65 | llvm::cl::init(Val: true)); |
| 66 | |
| 67 | TranslateToMLIRRegistration fromBinary( |
| 68 | "deserialize-spirv" , "deserializes the SPIR-V module" , |
| 69 | [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { |
| 70 | assert(sourceMgr.getNumBuffers() == 1 && "expected one buffer" ); |
| 71 | return deserializeModule( |
| 72 | input: sourceMgr.getMemoryBuffer(i: sourceMgr.getMainFileID()), context, |
| 73 | options: {.enableControlFlowStructurization: enableControlFlowStructurization}); |
| 74 | }); |
| 75 | } |
| 76 | } // namespace mlir |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // Serialization registration |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
| 82 | static LogicalResult |
| 83 | serializeModule(spirv::ModuleOp moduleOp, raw_ostream &output, |
| 84 | const spirv::SerializationOptions &options) { |
| 85 | SmallVector<uint32_t, 0> binary; |
| 86 | if (failed(Result: spirv::serialize(moduleOp, binary, options))) |
| 87 | return failure(); |
| 88 | |
| 89 | size_t sizeInBytes = binary.size() * sizeof(uint32_t); |
| 90 | |
| 91 | output.write(Ptr: reinterpret_cast<char *>(binary.data()), Size: sizeInBytes); |
| 92 | |
| 93 | if (options.saveModuleForValidation) { |
| 94 | size_t dirSeparator = |
| 95 | options.validationFilePrefix.find(svt: llvm::sys::path::get_separator()); |
| 96 | // If file prefix includes directory check if that directory exists. |
| 97 | if (dirSeparator != std::string::npos) { |
| 98 | llvm::StringRef parentDir = |
| 99 | llvm::sys::path::parent_path(path: options.validationFilePrefix); |
| 100 | if (!llvm::sys::fs::is_directory(Path: parentDir)) |
| 101 | return moduleOp.emitError( |
| 102 | message: "validation prefix directory does not exist\n" ); |
| 103 | } |
| 104 | |
| 105 | SmallString<128> filename; |
| 106 | int fd = 0; |
| 107 | |
| 108 | std::error_code errorCode = llvm::sys::fs::createUniqueFile( |
| 109 | Model: options.validationFilePrefix + "%%%%%%.spv" , ResultFD&: fd, ResultPath&: filename); |
| 110 | if (errorCode) |
| 111 | return moduleOp.emitError(message: "error creating validation output file: " ) |
| 112 | << errorCode.message() << "\n" ; |
| 113 | |
| 114 | llvm::raw_fd_ostream validationOutput(fd, /*shouldClose=*/true); |
| 115 | validationOutput.write(Ptr: reinterpret_cast<char *>(binary.data()), |
| 116 | Size: sizeInBytes); |
| 117 | validationOutput.flush(); |
| 118 | } |
| 119 | |
| 120 | return mlir::success(); |
| 121 | } |
| 122 | |
| 123 | namespace mlir { |
| 124 | void registerToSPIRVTranslation() { |
| 125 | static llvm::cl::opt<bool> emitDebugInfo( |
| 126 | "spirv-emit-debug-info" , |
| 127 | llvm::cl::desc("Emit SPIR-V debug information during serialization" ), |
| 128 | llvm::cl::init(Val: false)); |
| 129 | |
| 130 | static llvm::cl::opt<std::string> validationFilesPrefix( |
| 131 | "spirv-save-validation-files-with-prefix" , |
| 132 | llvm::cl::desc( |
| 133 | "When non-empty string is passed each serialized SPIR-V module is " |
| 134 | "saved to an additional file that starts with the given prefix. This " |
| 135 | "is used to generate separate binaries for validation, where " |
| 136 | "`--split-input-file` normally combines all outputs into one. The " |
| 137 | "one combined output (`-o`) is still written. Created files need to " |
| 138 | "be removed manually once processed." ), |
| 139 | llvm::cl::init(Val: "" )); |
| 140 | |
| 141 | TranslateFromMLIRRegistration toBinary( |
| 142 | "serialize-spirv" , "serialize SPIR-V dialect" , |
| 143 | [](spirv::ModuleOp moduleOp, raw_ostream &output) { |
| 144 | return serializeModule(moduleOp, output, |
| 145 | options: {.emitSymbolName: true, .emitDebugInfo: emitDebugInfo, |
| 146 | .saveModuleForValidation: !validationFilesPrefix.empty(), |
| 147 | .validationFilePrefix: validationFilesPrefix}); |
| 148 | }, |
| 149 | [](DialectRegistry ®istry) { |
| 150 | registry.insert<spirv::SPIRVDialect>(); |
| 151 | }); |
| 152 | } |
| 153 | } // namespace mlir |
| 154 | |
| 155 | //===----------------------------------------------------------------------===// |
| 156 | // Round-trip registration |
| 157 | //===----------------------------------------------------------------------===// |
| 158 | |
| 159 | static LogicalResult roundTripModule(spirv::ModuleOp module, bool emitDebugInfo, |
| 160 | raw_ostream &output) { |
| 161 | SmallVector<uint32_t, 0> binary; |
| 162 | MLIRContext *context = module->getContext(); |
| 163 | |
| 164 | spirv::SerializationOptions options; |
| 165 | options.emitDebugInfo = emitDebugInfo; |
| 166 | if (failed(Result: spirv::serialize(moduleOp: module, binary, options))) |
| 167 | return failure(); |
| 168 | |
| 169 | MLIRContext deserializationContext(context->getDialectRegistry()); |
| 170 | // TODO: we should only load the required dialects instead of all dialects. |
| 171 | deserializationContext.loadAllAvailableDialects(); |
| 172 | // Then deserialize to get back a SPIR-V module. |
| 173 | OwningOpRef<spirv::ModuleOp> spirvModule = |
| 174 | spirv::deserialize(binary, context: &deserializationContext); |
| 175 | if (!spirvModule) |
| 176 | return failure(); |
| 177 | spirvModule->print(os&: output); |
| 178 | |
| 179 | return mlir::success(); |
| 180 | } |
| 181 | |
| 182 | namespace mlir { |
| 183 | void registerTestRoundtripSPIRV() { |
| 184 | TranslateFromMLIRRegistration roundtrip( |
| 185 | "test-spirv-roundtrip" , "test roundtrip in SPIR-V dialect" , |
| 186 | [](spirv::ModuleOp module, raw_ostream &output) { |
| 187 | return roundTripModule(module, /*emitDebugInfo=*/false, output); |
| 188 | }, |
| 189 | [](DialectRegistry ®istry) { |
| 190 | registry.insert<spirv::SPIRVDialect>(); |
| 191 | }); |
| 192 | } |
| 193 | |
| 194 | void registerTestRoundtripDebugSPIRV() { |
| 195 | TranslateFromMLIRRegistration roundtrip( |
| 196 | "test-spirv-roundtrip-debug" , "test roundtrip debug in SPIR-V" , |
| 197 | [](spirv::ModuleOp module, raw_ostream &output) { |
| 198 | return roundTripModule(module, /*emitDebugInfo=*/true, output); |
| 199 | }, |
| 200 | [](DialectRegistry ®istry) { |
| 201 | registry.insert<spirv::SPIRVDialect>(); |
| 202 | }); |
| 203 | } |
| 204 | } // namespace mlir |
| 205 | |