1 //===- ModuleToObject.cpp - Module to object base class ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the base class for transforming Operations into binary
12 //===----------------------------------------------------------------------===//
14 #include "mlir/Target/LLVM/ModuleToObject.h"
16 #include "mlir/ExecutionEngine/OptUtils.h"
17 #include "mlir/IR/BuiltinOps.h"
18 #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
19 #include "mlir/Target/LLVMIR/Export.h"
20 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
22 #include "llvm/Bitcode/BitcodeWriter.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IRReader/IRReader.h"
25 #include "llvm/Linker/Linker.h"
26 #include "llvm/MC/TargetRegistry.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Transforms/IPO/Internalize.h"
35 using namespace mlir::LLVM
;
37 ModuleToObject::ModuleToObject(Operation
&module
, StringRef triple
,
38 StringRef chip
, StringRef features
, int optLevel
)
39 : module(module
), triple(triple
), chip(chip
), features(features
),
42 Operation
&ModuleToObject::getOperation() { return module
; }
44 std::unique_ptr
<llvm::TargetMachine
> ModuleToObject::createTargetMachine() {
47 const llvm::Target
*target
=
48 llvm::TargetRegistry::lookupTarget(triple
, error
);
50 getOperation().emitError() << "Failed to lookup target: " << error
;
54 // Create the target machine using the target.
55 llvm::TargetMachine
*machine
=
56 target
->createTargetMachine(triple
, chip
, features
, {}, {});
58 getOperation().emitError() << "Failed to create the target machine.";
61 return std::unique_ptr
<llvm::TargetMachine
>{machine
};
64 std::unique_ptr
<llvm::Module
>
65 ModuleToObject::loadBitcodeFile(llvm::LLVMContext
&context
,
66 llvm::TargetMachine
&targetMachine
,
68 llvm::SMDiagnostic error
;
69 std::unique_ptr
<llvm::Module
> library
=
70 llvm::getLazyIRFileModule(path
, error
, context
);
72 getOperation().emitError() << "Failed loading file from " << path
73 << ", error: " << error
.getMessage();
76 if (failed(handleBitcodeFile(*library
, targetMachine
))) {
82 LogicalResult
ModuleToObject::loadBitcodeFilesFromList(
83 llvm::LLVMContext
&context
, llvm::TargetMachine
&targetMachine
,
84 ArrayRef
<std::string
> fileList
,
85 SmallVector
<std::unique_ptr
<llvm::Module
>> &llvmModules
,
86 bool failureOnError
) {
87 for (const std::string
&str
: fileList
) {
88 // Test if the path exists, if it doesn't abort.
89 StringRef pathRef
= StringRef(str
.data(), str
.size());
90 if (!llvm::sys::fs::is_regular_file(pathRef
)) {
91 getOperation().emitError()
92 << "File path: " << pathRef
<< " does not exist or is not a file.\n";
95 // Load the file or abort on error.
96 if (auto bcFile
= loadBitcodeFile(context
, targetMachine
, pathRef
))
97 llvmModules
.push_back(std::move(bcFile
));
98 else if (failureOnError
)
104 std::unique_ptr
<llvm::Module
>
105 ModuleToObject::translateToLLVMIR(llvm::LLVMContext
&llvmContext
) {
106 return translateModuleToLLVMIR(&getOperation(), llvmContext
);
110 ModuleToObject::linkFiles(llvm::Module
&module
,
111 SmallVector
<std::unique_ptr
<llvm::Module
>> &&libs
) {
114 llvm::Linker
linker(module
);
115 for (std::unique_ptr
<llvm::Module
> &libModule
: libs
) {
116 // This bitcode linking imports the library functions into the module,
117 // allowing LLVM optimization passes (which must run after linking) to
118 // optimize across the libraries and the module's code. We also only import
119 // symbols if they are referenced by the module or a previous library since
120 // there will be no other source of references to those symbols in this
121 // compilation and since we don't want to bloat the resulting code object.
122 bool err
= linker
.linkInModule(
123 std::move(libModule
), llvm::Linker::Flags::LinkOnlyNeeded
,
124 [](llvm::Module
&m
, const StringSet
<> &gvs
) {
125 llvm::internalizeModule(m
, [&gvs
](const llvm::GlobalValue
&gv
) {
126 return !gv
.hasName() || (gvs
.count(gv
.getName()) == 0);
129 // True is linker failure
131 getOperation().emitError("Unrecoverable failure during bitcode linking.");
132 // We have no guaranties about the state of `ret`, so bail
139 LogicalResult
ModuleToObject::optimizeModule(llvm::Module
&module
,
140 llvm::TargetMachine
&targetMachine
,
142 if (optLevel
< 0 || optLevel
> 3)
143 return getOperation().emitError()
144 << "Invalid optimization level: " << optLevel
<< ".";
146 targetMachine
.setOptLevel(static_cast<llvm::CodeGenOptLevel
>(optLevel
));
149 makeOptimizingTransformer(optLevel
, /*sizeLevel=*/0, &targetMachine
);
150 auto error
= transformer(&module
);
152 InFlightDiagnostic mlirError
= getOperation().emitError();
153 llvm::handleAllErrors(
154 std::move(error
), [&mlirError
](const llvm::ErrorInfoBase
&ei
) {
155 mlirError
<< "Could not optimize LLVM IR: " << ei
.message() << "\n";
162 std::optional
<std::string
>
163 ModuleToObject::translateToISA(llvm::Module
&llvmModule
,
164 llvm::TargetMachine
&targetMachine
) {
165 std::string targetISA
;
166 llvm::raw_string_ostream
stream(targetISA
);
168 { // Drop pstream after this to prevent the ISA from being stuck buffering
169 llvm::buffer_ostream
pstream(stream
);
170 llvm::legacy::PassManager codegenPasses
;
172 if (targetMachine
.addPassesToEmitFile(codegenPasses
, pstream
, nullptr,
173 llvm::CodeGenFileType::AssemblyFile
))
176 codegenPasses
.run(llvmModule
);
181 std::optional
<SmallVector
<char, 0>>
182 ModuleToObject::moduleToObject(llvm::Module
&llvmModule
,
183 llvm::TargetMachine
&targetMachine
) {
184 SmallVector
<char, 0> binaryData
;
185 // Write the LLVM module bitcode to a buffer.
186 llvm::raw_svector_ostream
outputStream(binaryData
);
187 llvm::WriteBitcodeToFile(llvmModule
, outputStream
);
191 std::optional
<SmallVector
<char, 0>> ModuleToObject::run() {
192 // Translate the module to LLVM IR.
193 llvm::LLVMContext llvmContext
;
194 std::unique_ptr
<llvm::Module
> llvmModule
= translateToLLVMIR(llvmContext
);
196 getOperation().emitError() << "Failed creating the llvm::Module.";
200 // Create the target machine.
201 std::unique_ptr
<llvm::TargetMachine
> targetMachine
= createTargetMachine();
205 // Set the data layout and target triple of the module.
206 llvmModule
->setDataLayout(targetMachine
->createDataLayout());
207 llvmModule
->setTargetTriple(targetMachine
->getTargetTriple().getTriple());
209 // Link bitcode files.
210 handleModulePreLink(*llvmModule
, *targetMachine
);
212 auto libs
= loadBitcodeFiles(*llvmModule
, *targetMachine
);
216 if (failed(linkFiles(*llvmModule
, std::move(*libs
))))
218 handleModulePostLink(*llvmModule
, *targetMachine
);
221 // Optimize the module.
222 if (failed(optimizeModule(*llvmModule
, *targetMachine
, optLevel
)))
225 // Return the serialized object.
226 return moduleToObject(*llvmModule
, *targetMachine
);