| 1 | //===- ModuleToObject.cpp - Module to object base class ---------*- C++ -*-===// |
| 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 the base class for transforming Operations into binary |
| 10 | // objects. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Target/LLVM/ModuleToObject.h" |
| 15 | |
| 16 | #include "mlir/ExecutionEngine/OptUtils.h" |
| 17 | #include "mlir/IR/BuiltinAttributeInterfaces.h" |
| 18 | #include "mlir/IR/BuiltinAttributes.h" |
| 19 | #include "mlir/Remark/LLVMRemarkImport.h" |
| 20 | #include "mlir/Target/LLVMIR/Export.h" |
| 21 | #include "mlir/Target/LLVMIR/ModuleTranslation.h" |
| 22 | |
| 23 | #include "llvm/Bitcode/BitcodeWriter.h" |
| 24 | #include "llvm/IR/LegacyPassManager.h" |
| 25 | #include "llvm/IRReader/IRReader.h" |
| 26 | #include "llvm/Linker/Linker.h" |
| 27 | #include "llvm/MC/TargetRegistry.h" |
| 28 | #include "llvm/Support/FileSystem.h" |
| 29 | #include "llvm/Support/LibraryOptionsContext.h" |
| 30 | #include "llvm/Support/MemoryBuffer.h" |
| 31 | #include "llvm/Support/SourceMgr.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include "llvm/Transforms/IPO/Internalize.h" |
| 35 | |
| 36 | using namespace mlir; |
| 37 | using namespace mlir::LLVM; |
| 38 | |
| 39 | ModuleToObject::ModuleToObject( |
| 40 | Operation &module, StringRef triple, StringRef chip, StringRef features, |
| 41 | int optLevel, function_ref<void(llvm::Module &)> initialLlvmIRCallback, |
| 42 | function_ref<void(llvm::Module &)> linkedLlvmIRCallback, |
| 43 | function_ref<void(llvm::Module &)> optimizedLlvmIRCallback, |
| 44 | function_ref<void(StringRef)> isaCallback, |
| 45 | const llvm::cl::LibraryOptionsContext *libraryOptions) |
| 46 | : module(module), triple(triple), chip(chip), features(features), |
| 47 | optLevel(optLevel), initialLlvmIRCallback(initialLlvmIRCallback), |
| 48 | linkedLlvmIRCallback(linkedLlvmIRCallback), |
| 49 | optimizedLlvmIRCallback(optimizedLlvmIRCallback), |
| 50 | isaCallback(isaCallback), libraryOptions(libraryOptions) {} |
| 51 | |
| 52 | ModuleToObject::~ModuleToObject() = default; |
| 53 | |
| 54 | Operation &ModuleToObject::getOperation() { return module; } |
| 55 | |
| 56 | FailureOr<llvm::TargetMachine *> ModuleToObject::getOrCreateTargetMachine() { |
| 57 | if (targetMachine) |
| 58 | return targetMachine.get(); |
| 59 | // Load the target. |
| 60 | std::string error; |
| 61 | llvm::Triple parsedTriple(triple); |
| 62 | const llvm::Target *target = |
| 63 | llvm::TargetRegistry::lookupTarget(TheTriple: parsedTriple, Error&: error); |
| 64 | if (!target) |
| 65 | return getOperation().emitError() |
| 66 | << "Failed to lookup target for triple '" << triple << "' " << error; |
| 67 | |
| 68 | // Create the target machine using the target. |
| 69 | llvm::TargetOptions options; |
| 70 | options.MCOptions.LibraryOptions = libraryOptions; |
| 71 | targetMachine.reset( |
| 72 | p: target->createTargetMachine(TT: parsedTriple, CPU: chip, Features: features, Options: options, RM: {})); |
| 73 | if (!targetMachine) |
| 74 | return getOperation().emitError() |
| 75 | << "Failed to create target machine for triple '" << triple << "'" ; |
| 76 | |
| 77 | return targetMachine.get(); |
| 78 | } |
| 79 | |
| 80 | std::unique_ptr<llvm::Module> |
| 81 | ModuleToObject::loadBitcodeFile(llvm::LLVMContext &context, StringRef path) { |
| 82 | llvm::SMDiagnostic error; |
| 83 | std::unique_ptr<llvm::Module> library = |
| 84 | llvm::getLazyIRFileModule(Filename: path, Err&: error, Context&: context); |
| 85 | if (!library) { |
| 86 | getOperation().emitError() << "Failed loading file from " << path |
| 87 | << ", error: " << error.getMessage(); |
| 88 | return nullptr; |
| 89 | } |
| 90 | if (failed(Result: handleBitcodeFile(module&: *library))) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | return library; |
| 94 | } |
| 95 | |
| 96 | LogicalResult ModuleToObject::loadBitcodeFilesFromList( |
| 97 | llvm::LLVMContext &context, ArrayRef<Attribute> librariesToLink, |
| 98 | SmallVector<std::unique_ptr<llvm::Module>> &llvmModules, |
| 99 | bool failureOnError) { |
| 100 | for (Attribute linkLib : librariesToLink) { |
| 101 | // Attributes in this list can be either list of file paths using |
| 102 | // StringAttr, or a resource attribute pointing to the LLVM bitcode in |
| 103 | // memory. |
| 104 | if (auto filePath = dyn_cast<StringAttr>(Val&: linkLib)) { |
| 105 | // Test if the path exists, if it doesn't abort. |
| 106 | if (!llvm::sys::fs::is_regular_file(Path: filePath.strref())) { |
| 107 | getOperation().emitError() |
| 108 | << "File path: " << filePath << " does not exist or is not a file." ; |
| 109 | return failure(); |
| 110 | } |
| 111 | // Load the file or abort on error. |
| 112 | if (auto bcFile = loadBitcodeFile(context, path: filePath)) |
| 113 | llvmModules.push_back(Elt: std::move(bcFile)); |
| 114 | else if (failureOnError) |
| 115 | return failure(); |
| 116 | continue; |
| 117 | } |
| 118 | if (auto blobAttr = dyn_cast<BlobAttr>(Val&: linkLib)) { |
| 119 | // Load the file or abort on error. |
| 120 | llvm::SMDiagnostic error; |
| 121 | ArrayRef<char> data = blobAttr.getData(); |
| 122 | std::unique_ptr<llvm::MemoryBuffer> buffer = |
| 123 | llvm::MemoryBuffer::getMemBuffer(InputData: StringRef(data.data(), data.size()), |
| 124 | BufferName: "blobLinkedLib" , |
| 125 | /*RequiresNullTerminator=*/false); |
| 126 | std::unique_ptr<llvm::Module> mod = |
| 127 | getLazyIRModule(Buffer: std::move(buffer), Err&: error, Context&: context); |
| 128 | if (mod) { |
| 129 | if (failed(Result: handleBitcodeFile(module&: *mod))) |
| 130 | return failure(); |
| 131 | llvmModules.push_back(Elt: std::move(mod)); |
| 132 | } else if (failureOnError) { |
| 133 | getOperation().emitError() |
| 134 | << "Couldn't load LLVM library for linking: " << error.getMessage(); |
| 135 | return failure(); |
| 136 | } |
| 137 | continue; |
| 138 | } |
| 139 | if (failureOnError) { |
| 140 | getOperation().emitError() |
| 141 | << "Unknown attribute describing LLVM library to load: " << linkLib; |
| 142 | return failure(); |
| 143 | } |
| 144 | } |
| 145 | return success(); |
| 146 | } |
| 147 | |
| 148 | std::unique_ptr<llvm::Module> |
| 149 | ModuleToObject::translateToLLVMIR(llvm::LLVMContext &llvmContext) { |
| 150 | if (libraryOptions) |
| 151 | llvmContext.getLibraryOptions().assign(Other: *libraryOptions); |
| 152 | return translateModuleToLLVMIR(module: &getOperation(), llvmContext); |
| 153 | } |
| 154 | |
| 155 | LogicalResult |
| 156 | ModuleToObject::linkFiles(llvm::Module &module, |
| 157 | SmallVector<std::unique_ptr<llvm::Module>> &&libs) { |
| 158 | if (libs.empty()) |
| 159 | return success(); |
| 160 | llvm::Linker linker(module); |
| 161 | for (std::unique_ptr<llvm::Module> &libModule : libs) { |
| 162 | // This bitcode linking imports the library functions into the module, |
| 163 | // allowing LLVM optimization passes (which must run after linking) to |
| 164 | // optimize across the libraries and the module's code. We also only import |
| 165 | // symbols if they are referenced by the module or a previous library since |
| 166 | // there will be no other source of references to those symbols in this |
| 167 | // compilation and since we don't want to bloat the resulting code object. |
| 168 | bool err = linker.linkInModule( |
| 169 | Src: std::move(libModule), Flags: llvm::Linker::Flags::LinkOnlyNeeded, |
| 170 | InternalizeCallback: [](llvm::Module &m, const StringSet<> &gvs) { |
| 171 | llvm::internalizeModule(TheModule&: m, MustPreserveGV: [&gvs](const llvm::GlobalValue &gv) { |
| 172 | return !gv.hasName() || (gvs.count(Key: gv.getName()) == 0); |
| 173 | }); |
| 174 | }); |
| 175 | // True is linker failure |
| 176 | if (err) { |
| 177 | getOperation().emitError(message: "Unrecoverable failure during bitcode linking." ); |
| 178 | // We have no guaranties about the state of `ret`, so bail |
| 179 | return failure(); |
| 180 | } |
| 181 | } |
| 182 | return success(); |
| 183 | } |
| 184 | |
| 185 | LogicalResult ModuleToObject::optimizeModule(llvm::Module &module, |
| 186 | |
| 187 | int optLevel) { |
| 188 | if (optLevel < 0 || optLevel > 3) |
| 189 | return getOperation().emitError() |
| 190 | << "Invalid optimization level: " << optLevel << "." ; |
| 191 | |
| 192 | FailureOr<llvm::TargetMachine *> targetMachine = getOrCreateTargetMachine(); |
| 193 | if (failed(Result: targetMachine)) |
| 194 | return getOperation().emitError() |
| 195 | << "Target Machine unavailable for triple " << triple |
| 196 | << ", can't optimize with LLVM\n" ; |
| 197 | (*targetMachine)->setOptLevel(static_cast<llvm::CodeGenOptLevel>(optLevel)); |
| 198 | |
| 199 | auto transformer = |
| 200 | makeOptimizingTransformer(optLevel, /*sizeLevel=*/0, targetMachine: *targetMachine); |
| 201 | auto error = transformer(&module); |
| 202 | if (error) { |
| 203 | InFlightDiagnostic mlirError = getOperation().emitError(); |
| 204 | llvm::handleAllErrors( |
| 205 | E: std::move(error), Handlers: [&mlirError](const llvm::ErrorInfoBase &ei) { |
| 206 | mlirError << "Could not optimize LLVM IR: " << ei.message() << "\n" ; |
| 207 | }); |
| 208 | return mlirError; |
| 209 | } |
| 210 | return success(); |
| 211 | } |
| 212 | |
| 213 | FailureOr<SmallString<0>> ModuleToObject::translateModuleToISA( |
| 214 | llvm::Module &llvmModule, llvm::TargetMachine &targetMachine, |
| 215 | function_ref<InFlightDiagnostic()> emitError) { |
| 216 | SmallString<0> targetISA; |
| 217 | llvm::raw_svector_ostream stream(targetISA); |
| 218 | |
| 219 | { // Drop pstream after this to prevent the ISA from being stuck buffering |
| 220 | llvm::buffer_ostream pstream(stream); |
| 221 | llvm::legacy::PassManager codegenPasses; |
| 222 | |
| 223 | if (targetMachine.addPassesToEmitFile(codegenPasses, pstream, nullptr, |
| 224 | llvm::CodeGenFileType::AssemblyFile)) |
| 225 | return emitError() << "Target machine cannot emit assembly" ; |
| 226 | |
| 227 | codegenPasses.run(M&: llvmModule); |
| 228 | } |
| 229 | return targetISA; |
| 230 | } |
| 231 | |
| 232 | void ModuleToObject::setDataLayoutAndTriple(llvm::Module &module) { |
| 233 | // Create the target machine. |
| 234 | FailureOr<llvm::TargetMachine *> targetMachine = getOrCreateTargetMachine(); |
| 235 | if (failed(Result: targetMachine)) |
| 236 | return; |
| 237 | |
| 238 | // Set the data layout and target triple of the module. |
| 239 | module.setDataLayout((*targetMachine)->getTargetTriple().computeDataLayout()); |
| 240 | module.setTargetTriple((*targetMachine)->getTargetTriple()); |
| 241 | } |
| 242 | |
| 243 | FailureOr<SmallVector<char, 0>> |
| 244 | ModuleToObject::moduleToObject(llvm::Module &llvmModule) { |
| 245 | SmallVector<char, 0> binaryData; |
| 246 | // Write the LLVM module bitcode to a buffer. |
| 247 | llvm::raw_svector_ostream outputStream(binaryData); |
| 248 | llvm::WriteBitcodeToFile(M: llvmModule, Out&: outputStream); |
| 249 | return binaryData; |
| 250 | } |
| 251 | |
| 252 | void ModuleToObject::setupLLVMContext(llvm::LLVMContext &llvmContext) { |
| 253 | llvmContext.setDiagnosticHandler( |
| 254 | DH: std::make_unique<remark::LLVMToMLIRDiagnosticHandler>(args: &getOperation()), |
| 255 | /*RespectFilters=*/true); |
| 256 | } |
| 257 | |
| 258 | std::optional<SmallVector<char, 0>> ModuleToObject::run() { |
| 259 | // Translate the module to LLVM IR. |
| 260 | llvm::LLVMContext llvmContext; |
| 261 | setupLLVMContext(llvmContext); |
| 262 | std::unique_ptr<llvm::Module> llvmModule = translateToLLVMIR(llvmContext); |
| 263 | if (!llvmModule) { |
| 264 | getOperation().emitError() << "Failed creating the llvm::Module." ; |
| 265 | return std::nullopt; |
| 266 | } |
| 267 | setDataLayoutAndTriple(*llvmModule); |
| 268 | |
| 269 | if (initialLlvmIRCallback) |
| 270 | initialLlvmIRCallback(*llvmModule); |
| 271 | |
| 272 | // Link bitcode files. |
| 273 | handleModulePreLink(module&: *llvmModule); |
| 274 | { |
| 275 | auto libs = loadBitcodeFiles(module&: *llvmModule); |
| 276 | if (!libs) |
| 277 | return std::nullopt; |
| 278 | if (!libs->empty()) |
| 279 | if (failed(Result: linkFiles(module&: *llvmModule, libs: std::move(*libs)))) |
| 280 | return std::nullopt; |
| 281 | handleModulePostLink(module&: *llvmModule); |
| 282 | } |
| 283 | |
| 284 | if (linkedLlvmIRCallback) |
| 285 | linkedLlvmIRCallback(*llvmModule); |
| 286 | |
| 287 | // Optimize the module. |
| 288 | if (failed(Result: optimizeModule(module&: *llvmModule, optLevel))) |
| 289 | return std::nullopt; |
| 290 | |
| 291 | if (optimizedLlvmIRCallback) |
| 292 | optimizedLlvmIRCallback(*llvmModule); |
| 293 | |
| 294 | // Return the serialized object. |
| 295 | return moduleToObject(llvmModule&: *llvmModule); |
| 296 | } |
| 297 | |