MLIR 24.0.0git
ModuleToObject.h
Go to the documentation of this file.
1//===- ModuleToObject.h - 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 declares the base class for transforming operations into binary
10// objects.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_TARGET_LLVM_MODULETOOBJECT_H
15#define MLIR_TARGET_LLVM_MODULETOOBJECT_H
16
17#include "mlir/IR/Operation.h"
18#include "llvm/IR/Module.h"
19
20namespace llvm {
21class TargetMachine;
22namespace cl {
23class LibraryOptionsContext;
24} // namespace cl
25} // namespace llvm
26
27namespace mlir {
28namespace LLVM {
30/// Utility base class for transforming operations into binary objects, by
31/// default it returns the serialized LLVM bitcode for the module. The
32/// operations being transformed must be translatable into LLVM IR.
34public:
36 Operation &module, StringRef triple, StringRef chip,
37 StringRef features = {}, int optLevel = 3,
38 function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
39 function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
40 function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
41 function_ref<void(StringRef)> isaCallback = {},
42 const llvm::cl::LibraryOptionsContext *libraryOptions = nullptr);
43 virtual ~ModuleToObject();
44
45 /// Returns the operation being serialized.
47
48 /// Runs the serialization pipeline, returning `std::nullopt` on error.
49 virtual std::optional<SmallVector<char, 0>> run();
50
51 /// Translate LLVM module to textual ISA.
52 static FailureOr<SmallString<0>>
53 translateModuleToISA(llvm::Module &llvmModule,
54 llvm::TargetMachine &targetMachine,
56
57protected:
58 // Hooks to be implemented by derived classes.
59
60 /// Hook for configuring the LLVM context, called by `run` before translating
61 /// the operation. By default it installs a
62 /// `remark::LLVMToMLIRDiagnosticHandler` to report LLVM diagnostics and
63 /// remarks through MLIR.
64 virtual void setupLLVMContext(llvm::LLVMContext &llvmContext);
65
66 /// Hook for computing the Datalayout
67 virtual void setDataLayoutAndTriple(llvm::Module &module);
68
69 /// Hook for loading bitcode files, returns std::nullopt on failure.
70 virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
74
75 /// Hook for performing additional actions on a loaded bitcode file.
76 virtual LogicalResult handleBitcodeFile(llvm::Module &module) {
77 return success();
78 }
79
80 /// Hook for performing additional actions on the llvmModule pre linking.
81 virtual void handleModulePreLink(llvm::Module &module) {}
82
83 /// Hook for performing additional actions on the llvmModule post linking.
84 virtual void handleModulePostLink(llvm::Module &module) {}
85
86 /// Serializes the LLVM IR bitcode to an object file, by default it serializes
87 /// to LLVM bitcode.
88 virtual FailureOr<SmallVector<char, 0>>
89 moduleToObject(llvm::Module &llvmModule);
90
91protected:
92 /// Create the target machine based on the target triple and chip.
93 /// This can fail if the target is not available.
94 FailureOr<llvm::TargetMachine *> getOrCreateTargetMachine();
95
96 /// Loads a bitcode file from path.
97 std::unique_ptr<llvm::Module> loadBitcodeFile(llvm::LLVMContext &context,
98 StringRef path);
99
100 /// Loads multiple bitcode files.
101 LogicalResult loadBitcodeFilesFromList(
102 llvm::LLVMContext &context, ArrayRef<Attribute> librariesToLink,
103 SmallVector<std::unique_ptr<llvm::Module>> &llvmModules,
104 bool failureOnError = true);
105
106 /// Translates the operation to LLVM IR.
107 std::unique_ptr<llvm::Module>
108 translateToLLVMIR(llvm::LLVMContext &llvmContext);
109
110 /// Link the llvmModule to other bitcode file.
111 LogicalResult linkFiles(llvm::Module &module,
112 SmallVector<std::unique_ptr<llvm::Module>> &&libs);
113
114 /// Optimize the module.
115 virtual LogicalResult optimizeModule(llvm::Module &module, int optL);
116
117protected:
118 /// Module to transform to a binary object.
120
121 /// Target triple.
122 StringRef triple;
123
124 /// Target chip.
125 StringRef chip;
126
127 /// Target features.
128 StringRef features;
129
130 /// Optimization level.
132
133 /// Callback invoked with the initial LLVM IR for the device module.
135
136 /// Callback invoked with LLVM IR for the device module after
137 /// linking the device libraries.
139
140 /// Callback invoked with LLVM IR for the device module after
141 /// LLVM optimizations but before codegen.
143
144 /// Callback invoked with the target ISA for the device,
145 /// for example PTX assembly.
147
148 /// LLVM library options for the LLVMContext and TargetMachine that
149 /// serialization creates, or nullptr for their defaults. They must outlive
150 /// this object.
151 const llvm::cl::LibraryOptionsContext *libraryOptions;
152
153private:
154 /// The TargetMachine created for the given Triple, if available.
155 /// Accessible through `getOrCreateTargetMachine()`.
156 std::unique_ptr<llvm::TargetMachine> targetMachine;
157};
158} // namespace LLVM
159} // namespace mlir
160
161#endif // MLIR_TARGET_LLVM_MODULETOOBJECT_H
return success()
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.
Implementation class for module translation.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155