MLIR 24.0.0git
ModuleToObject.cpp
Go to the documentation of this file.
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
15
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
36using namespace mlir;
37using namespace mlir::LLVM;
38
51
53
55
56FailureOr<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(parsedTriple, 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 target->createTargetMachine(parsedTriple, chip, features, options, {}));
73 if (!targetMachine)
74 return getOperation().emitError()
75 << "Failed to create target machine for triple '" << triple << "'";
76
77 return targetMachine.get();
78}
79
80std::unique_ptr<llvm::Module>
81ModuleToObject::loadBitcodeFile(llvm::LLVMContext &context, StringRef path) {
82 llvm::SMDiagnostic error;
83 std::unique_ptr<llvm::Module> library =
84 llvm::getLazyIRFileModule(path, error, context);
85 if (!library) {
86 getOperation().emitError() << "Failed loading file from " << path
87 << ", error: " << error.getMessage();
88 return nullptr;
89 }
90 if (failed(handleBitcodeFile(*library))) {
91 return nullptr;
92 }
93 return library;
94}
95
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>(linkLib)) {
105 // Test if the path exists, if it doesn't abort.
106 if (!llvm::sys::fs::is_regular_file(filePath.strref())) {
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, filePath))
113 llvmModules.push_back(std::move(bcFile));
114 else if (failureOnError)
115 return failure();
116 continue;
117 }
118 if (auto blobAttr = dyn_cast<BlobAttr>(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(StringRef(data.data(), data.size()),
124 "blobLinkedLib",
125 /*RequiresNullTerminator=*/false);
126 std::unique_ptr<llvm::Module> mod =
127 getLazyIRModule(std::move(buffer), error, context);
128 if (mod) {
129 if (failed(handleBitcodeFile(*mod)))
130 return failure();
131 llvmModules.push_back(std::move(mod));
132 } else if (failureOnError) {
134 << "Couldn't load LLVM library for linking: " << error.getMessage();
135 return failure();
136 }
137 continue;
138 }
139 if (failureOnError) {
141 << "Unknown attribute describing LLVM library to load: " << linkLib;
142 return failure();
143 }
144 }
145 return success();
146}
147
148std::unique_ptr<llvm::Module>
149ModuleToObject::translateToLLVMIR(llvm::LLVMContext &llvmContext) {
150 if (libraryOptions)
151 llvmContext.getLibraryOptions().assign(*libraryOptions);
152 return translateModuleToLLVMIR(&getOperation(), llvmContext);
153}
154
155LogicalResult
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 std::move(libModule), llvm::Linker::Flags::LinkOnlyNeeded,
170 [](llvm::Module &m, const StringSet<> &gvs) {
171 llvm::internalizeModule(m, [&gvs](const llvm::GlobalValue &gv) {
172 return !gv.hasName() || (gvs.count(gv.getName()) == 0);
173 });
174 });
175 // True is linker failure
176 if (err) {
177 getOperation().emitError("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
185LogicalResult ModuleToObject::optimizeModule(llvm::Module &module,
186
187 int optLevel) {
189 return getOperation().emitError()
190 << "Invalid optimization level: " << optLevel << ".";
191
192 FailureOr<llvm::TargetMachine *> targetMachine = getOrCreateTargetMachine();
193 if (failed(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);
201 auto error = transformer(&module);
202 if (error) {
204 llvm::handleAllErrors(
205 std::move(error), [&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
213FailureOr<SmallString<0>> ModuleToObject::translateModuleToISA(
214 llvm::Module &llvmModule, llvm::TargetMachine &targetMachine,
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(llvmModule);
228 }
229 return targetISA;
230}
231
233 // Create the target machine.
234 FailureOr<llvm::TargetMachine *> targetMachine = getOrCreateTargetMachine();
235 if (failed(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
243FailureOr<SmallVector<char, 0>>
244ModuleToObject::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(llvmModule, outputStream);
249 return binaryData;
250}
251
252void ModuleToObject::setupLLVMContext(llvm::LLVMContext &llvmContext) {
253 llvmContext.setDiagnosticHandler(
254 std::make_unique<remark::LLVMToMLIRDiagnosticHandler>(&getOperation()),
255 /*RespectFilters=*/true);
256}
257
258std::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
270 initialLlvmIRCallback(*llvmModule);
271
272 // Link bitcode files.
273 handleModulePreLink(*llvmModule);
274 {
275 auto libs = loadBitcodeFiles(*llvmModule);
276 if (!libs)
277 return std::nullopt;
278 if (!libs->empty())
279 if (failed(linkFiles(*llvmModule, std::move(*libs))))
280 return std::nullopt;
281 handleModulePostLink(*llvmModule);
282 }
283
285 linkedLlvmIRCallback(*llvmModule);
286
287 // Optimize the module.
288 if (failed(optimizeModule(*llvmModule, optLevel)))
289 return std::nullopt;
290
292 optimizedLlvmIRCallback(*llvmModule);
293
294 // Return the serialized object.
295 return moduleToObject(*llvmModule);
296}
return success()
static llvm::ManagedStatic< PassManagerOptions > options
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class represents a diagnostic that is inflight and set to be reported.
LogicalResult loadBitcodeFilesFromList(llvm::LLVMContext &context, ArrayRef< Attribute > librariesToLink, SmallVector< std::unique_ptr< llvm::Module > > &llvmModules, bool failureOnError=true)
Loads multiple bitcode files.
FailureOr< llvm::TargetMachine * > getOrCreateTargetMachine()
Create the target machine based on the target triple and chip.
StringRef features
Target features.
std::unique_ptr< llvm::Module > translateToLLVMIR(llvm::LLVMContext &llvmContext)
Translates the operation to LLVM IR.
const llvm::cl::LibraryOptionsContext * libraryOptions
LLVM library options for the LLVMContext and TargetMachine that serialization creates,...
virtual std::optional< SmallVector< char, 0 > > run()
Runs the serialization pipeline, returning std::nullopt on error.
virtual void setupLLVMContext(llvm::LLVMContext &llvmContext)
Hook for configuring the LLVM context, called by run before translating the operation.
function_ref< void(llvm::Module &)> initialLlvmIRCallback
Callback invoked with the initial LLVM IR for the device module.
function_ref< void(llvm::Module &)> optimizedLlvmIRCallback
Callback invoked with LLVM IR for the device module after LLVM optimizations but before codegen.
virtual void setDataLayoutAndTriple(llvm::Module &module)
Hook for computing the Datalayout.
virtual FailureOr< SmallVector< char, 0 > > moduleToObject(llvm::Module &llvmModule)
Serializes the LLVM IR bitcode to an object file, by default it serializes to LLVM bitcode.
virtual void handleModulePreLink(llvm::Module &module)
Hook for performing additional actions on the llvmModule pre linking.
StringRef triple
Target triple.
int optLevel
Optimization level.
virtual LogicalResult handleBitcodeFile(llvm::Module &module)
Hook for performing additional actions on a loaded bitcode file.
Operation & getOperation()
Returns the operation being serialized.
LogicalResult linkFiles(llvm::Module &module, SmallVector< std::unique_ptr< llvm::Module > > &&libs)
Link the llvmModule to other bitcode file.
function_ref< void(StringRef)> isaCallback
Callback invoked with the target ISA for the device, for example PTX assembly.
virtual std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module)
Hook for loading bitcode files, returns std::nullopt on failure.
static FailureOr< SmallString< 0 > > translateModuleToISA(llvm::Module &llvmModule, llvm::TargetMachine &targetMachine, function_ref< InFlightDiagnostic()> emitError)
Translate LLVM module to textual ISA.
virtual LogicalResult optimizeModule(llvm::Module &module, int optL)
Optimize the module.
function_ref< void(llvm::Module &)> linkedLlvmIRCallback
Callback invoked with LLVM IR for the device module after linking the device libraries.
StringRef chip
Target chip.
ModuleToObject(Operation &module, StringRef triple, StringRef chip, StringRef features={}, int optLevel=3, function_ref< void(llvm::Module &)> initialLlvmIRCallback={}, function_ref< void(llvm::Module &)> linkedLlvmIRCallback={}, function_ref< void(llvm::Module &)> optimizedLlvmIRCallback={}, function_ref< void(StringRef)> isaCallback={}, const llvm::cl::LibraryOptionsContext *libraryOptions=nullptr)
std::unique_ptr< llvm::Module > loadBitcodeFile(llvm::LLVMContext &context, StringRef path)
Loads a bitcode file from path.
virtual void handleModulePostLink(llvm::Module &module)
Hook for performing additional actions on the llvmModule post linking.
Operation & module
Module to transform to a binary object.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Include the generated interface declarations.
std::function< llvm::Error(llvm::Module *)> makeOptimizingTransformer(unsigned optLevel, unsigned sizeLevel, llvm::TargetMachine *targetMachine)
Create a module transformer function for MLIR ExecutionEngine that runs LLVM IR passes corresponding ...
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::StringSet< AllocatorTy > StringSet
Definition LLVM.h:135
std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false, llvm::vfs::FileSystem *fs=nullptr)
Translates a given LLVM dialect module into an LLVM IR module living in the given context.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155