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;
22} // namespace llvm
23
24namespace mlir {
25namespace LLVM {
27/// Utility base class for transforming operations into binary objects, by
28/// default it returns the serialized LLVM bitcode for the module. The
29/// operations being transformed must be translatable into LLVM IR.
31public:
33 Operation &module, StringRef triple, StringRef chip,
34 StringRef features = {}, int optLevel = 3,
35 function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
36 function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
37 function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
38 function_ref<void(StringRef)> isaCallback = {});
39 virtual ~ModuleToObject();
40
41 /// Returns the operation being serialized.
43
44 /// Runs the serialization pipeline, returning `std::nullopt` on error.
45 virtual std::optional<SmallVector<char, 0>> run();
46
47 /// Translate LLVM module to textual ISA.
48 static FailureOr<SmallString<0>>
49 translateModuleToISA(llvm::Module &llvmModule,
50 llvm::TargetMachine &targetMachine,
52
53protected:
54 // Hooks to be implemented by derived classes.
55
56 /// Hook for configuring the LLVM context, called by `run` before translating
57 /// the operation. By default it installs a
58 /// `remark::LLVMToMLIRDiagnosticHandler` to report LLVM diagnostics and
59 /// remarks through MLIR.
60 virtual void setupLLVMContext(llvm::LLVMContext &llvmContext);
61
62 /// Hook for computing the Datalayout
63 virtual void setDataLayoutAndTriple(llvm::Module &module);
64
65 /// Hook for loading bitcode files, returns std::nullopt on failure.
66 virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
70
71 /// Hook for performing additional actions on a loaded bitcode file.
72 virtual LogicalResult handleBitcodeFile(llvm::Module &module) {
73 return success();
74 }
75
76 /// Hook for performing additional actions on the llvmModule pre linking.
77 virtual void handleModulePreLink(llvm::Module &module) {}
78
79 /// Hook for performing additional actions on the llvmModule post linking.
80 virtual void handleModulePostLink(llvm::Module &module) {}
81
82 /// Serializes the LLVM IR bitcode to an object file, by default it serializes
83 /// to LLVM bitcode.
84 virtual FailureOr<SmallVector<char, 0>>
85 moduleToObject(llvm::Module &llvmModule);
86
87protected:
88 /// Create the target machine based on the target triple and chip.
89 /// This can fail if the target is not available.
90 FailureOr<llvm::TargetMachine *> getOrCreateTargetMachine();
91
92 /// Loads a bitcode file from path.
93 std::unique_ptr<llvm::Module> loadBitcodeFile(llvm::LLVMContext &context,
94 StringRef path);
95
96 /// Loads multiple bitcode files.
97 LogicalResult loadBitcodeFilesFromList(
98 llvm::LLVMContext &context, ArrayRef<Attribute> librariesToLink,
99 SmallVector<std::unique_ptr<llvm::Module>> &llvmModules,
100 bool failureOnError = true);
101
102 /// Translates the operation to LLVM IR.
103 std::unique_ptr<llvm::Module>
104 translateToLLVMIR(llvm::LLVMContext &llvmContext);
105
106 /// Link the llvmModule to other bitcode file.
107 LogicalResult linkFiles(llvm::Module &module,
108 SmallVector<std::unique_ptr<llvm::Module>> &&libs);
109
110 /// Optimize the module.
111 virtual LogicalResult optimizeModule(llvm::Module &module, int optL);
112
113protected:
114 /// Module to transform to a binary object.
116
117 /// Target triple.
118 StringRef triple;
119
120 /// Target chip.
121 StringRef chip;
122
123 /// Target features.
124 StringRef features;
125
126 /// Optimization level.
128
129 /// Callback invoked with the initial LLVM IR for the device module.
131
132 /// Callback invoked with LLVM IR for the device module after
133 /// linking the device libraries.
135
136 /// Callback invoked with LLVM IR for the device module after
137 /// LLVM optimizations but before codegen.
139
140 /// Callback invoked with the target ISA for the device,
141 /// for example PTX assembly.
143
144private:
145 /// The TargetMachine created for the given Triple, if available.
146 /// Accessible through `getOrCreateTargetMachine()`.
147 std::unique_ptr<llvm::TargetMachine> targetMachine;
148};
149} // namespace LLVM
150} // namespace mlir
151
152#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.
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.
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.
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={})
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:227
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:147