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"
38 StringRef chip, StringRef features,
int optLevel)
39 : module(module), triple(triple), chip(chip), features(features),
46 std::optional<llvm::TargetMachine *>
49 return targetMachine.get();
52 const llvm::Target *target =
53 llvm::TargetRegistry::lookupTarget(
triple, error);
56 <<
"Failed to lookup target for triple '" <<
triple <<
"' " << error;
65 return targetMachine.get();
68 std::unique_ptr<llvm::Module>
70 llvm::SMDiagnostic error;
71 std::unique_ptr<llvm::Module> library =
72 llvm::getLazyIRFileModule(path, error, context);
75 <<
", error: " << error.getMessage();
86 SmallVector<std::unique_ptr<llvm::Module>> &llvmModules,
87 bool failureOnError) {
88 for (
const std::string &str : fileList) {
90 StringRef pathRef = StringRef(str.data(), str.size());
91 if (!llvm::sys::fs::is_regular_file(pathRef)) {
93 <<
"File path: " << pathRef <<
" does not exist or is not a file.\n";
98 llvmModules.push_back(std::move(bcFile));
99 else if (failureOnError)
105 std::unique_ptr<llvm::Module>
112 SmallVector<std::unique_ptr<llvm::Module>> &&libs) {
115 llvm::Linker linker(
module);
116 for (std::unique_ptr<llvm::Module> &libModule : libs) {
123 bool err = linker.linkInModule(
124 std::move(libModule), llvm::Linker::Flags::LinkOnlyNeeded,
126 llvm::internalizeModule(m, [&gvs](
const llvm::GlobalValue &gv) {
127 return !gv.hasName() || (gvs.count(gv.getName()) == 0);
143 if (optLevel < 0 || optLevel > 3)
145 <<
"Invalid optimization level: " <<
optLevel <<
".";
147 std::optional<llvm::TargetMachine *> targetMachine =
151 <<
"Target Machine unavailable for triple " <<
triple
152 <<
", can't optimize with LLVM\n";
153 (*targetMachine)->setOptLevel(
static_cast<llvm::CodeGenOptLevel
>(
optLevel));
157 auto error = transformer(&
module);
160 llvm::handleAllErrors(
161 std::move(error), [&mlirError](
const llvm::ErrorInfoBase &ei) {
162 mlirError <<
"Could not optimize LLVM IR: " << ei.message() <<
"\n";
169 std::optional<std::string>
171 llvm::TargetMachine &targetMachine) {
172 std::string targetISA;
173 llvm::raw_string_ostream stream(targetISA);
176 llvm::buffer_ostream pstream(stream);
177 llvm::legacy::PassManager codegenPasses;
179 if (targetMachine.addPassesToEmitFile(codegenPasses, pstream,
nullptr,
180 llvm::CodeGenFileType::AssemblyFile))
183 codegenPasses.run(llvmModule);
190 std::optional<llvm::TargetMachine *> targetMachine =
194 module.setDataLayout((*targetMachine)->createDataLayout());
195 module.setTargetTriple((*targetMachine)->getTargetTriple().getTriple());
199 std::optional<SmallVector<char, 0>>
203 llvm::raw_svector_ostream outputStream(binaryData);
204 llvm::WriteBitcodeToFile(llvmModule, outputStream);
210 llvm::LLVMContext llvmContext;
225 if (failed(
linkFiles(*llvmModule, std::move(*libs))))
This class represents a diagnostic that is inflight and set to be reported.
StringRef features
Target features.
std::unique_ptr< llvm::Module > translateToLLVMIR(llvm::LLVMContext &llvmContext)
Translates the operation to LLVM IR.
virtual std::optional< SmallVector< char, 0 > > run()
Runs the serialization pipeline, returning std::nullopt on error.
static std::optional< std::string > translateToISA(llvm::Module &llvmModule, llvm::TargetMachine &targetMachine)
Utility function for translating to ISA, returns std::nullopt on failure.
virtual std::optional< 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 setDataLayoutAndTriple(llvm::Module &module)
Hook for computing the Datalayout.
virtual void handleModulePreLink(llvm::Module &module)
Hook for performing additional actions on the llvmModule pre linking.
StringRef triple
Target triple.
int optLevel
Optimization level.
ModuleToObject(Operation &module, StringRef triple, StringRef chip, StringRef features={}, int optLevel=3)
virtual LogicalResult handleBitcodeFile(llvm::Module &module)
Hook for performing additional actions on a loaded bitcode file.
std::optional< llvm::TargetMachine * > getOrCreateTargetMachine()
Create the target machine based on the target triple and chip.
Operation & getOperation()
Returns the operation being serialized.
LogicalResult loadBitcodeFilesFromList(llvm::LLVMContext &context, ArrayRef< std::string > fileList, SmallVector< std::unique_ptr< llvm::Module >> &llvmModules, bool failureOnError=true)
Loads multiple bitcode files.
virtual LogicalResult optimizeModule(llvm::Module &module, int optL)
Optimize the module.
LogicalResult linkFiles(llvm::Module &module, SmallVector< std::unique_ptr< llvm::Module >> &&libs)
Link the llvmModule to other bitcode file.
virtual ~ModuleToObject()
StringRef chip
Target chip.
std::unique_ptr< llvm::Module > loadBitcodeFile(llvm::LLVMContext &context, StringRef path)
Loads a bitcode file from path.
virtual std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module)
Hook for loading bitcode files, returns std::nullopt on failure.
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.
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::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false)
Translates a given LLVM dialect module into an LLVM IR module living in the given context.
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 ...