MLIR 22.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 computing the Datalayout
57 virtual void setDataLayoutAndTriple(llvm::Module &module);
58
59 /// Hook for loading bitcode files, returns std::nullopt on failure.
60 virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
64
65 /// Hook for performing additional actions on a loaded bitcode file.
66 virtual LogicalResult handleBitcodeFile(llvm::Module &module) {
67 return success();
68 }
69
70 /// Hook for performing additional actions on the llvmModule pre linking.
71 virtual void handleModulePreLink(llvm::Module &module) {}
72
73 /// Hook for performing additional actions on the llvmModule post linking.
74 virtual void handleModulePostLink(llvm::Module &module) {}
75
76 /// Serializes the LLVM IR bitcode to an object file, by default it serializes
77 /// to LLVM bitcode.
78 virtual FailureOr<SmallVector<char, 0>>
79 moduleToObject(llvm::Module &llvmModule);
80
81protected:
82 /// Create the target machine based on the target triple and chip.
83 /// This can fail if the target is not available.
84 FailureOr<llvm::TargetMachine *> getOrCreateTargetMachine();
85
86 /// Loads a bitcode file from path.
87 std::unique_ptr<llvm::Module> loadBitcodeFile(llvm::LLVMContext &context,
88 StringRef path);
89
90 /// Loads multiple bitcode files.
91 LogicalResult loadBitcodeFilesFromList(
92 llvm::LLVMContext &context, ArrayRef<Attribute> librariesToLink,
93 SmallVector<std::unique_ptr<llvm::Module>> &llvmModules,
94 bool failureOnError = true);
95
96 /// Translates the operation to LLVM IR.
97 std::unique_ptr<llvm::Module>
98 translateToLLVMIR(llvm::LLVMContext &llvmContext);
99
100 /// Link the llvmModule to other bitcode file.
101 LogicalResult linkFiles(llvm::Module &module,
102 SmallVector<std::unique_ptr<llvm::Module>> &&libs);
103
104 /// Optimize the module.
105 virtual LogicalResult optimizeModule(llvm::Module &module, int optL);
106
107protected:
108 /// Module to transform to a binary object.
110
111 /// Target triple.
112 StringRef triple;
113
114 /// Target chip.
115 StringRef chip;
116
117 /// Target features.
118 StringRef features;
119
120 /// Optimization level.
122
123 /// Callback invoked with the initial LLVM IR for the device module.
125
126 /// Callback invoked with LLVM IR for the device module after
127 /// linking the device libraries.
129
130 /// Callback invoked with LLVM IR for the device module after
131 /// LLVM optimizations but before codegen.
133
134 /// Callback invoked with the target ISA for the device,
135 /// for example PTX assembly.
137
138private:
139 /// The TargetMachine created for the given Triple, if available.
140 /// Accessible through `getOrCreateTargetMachine()`.
141 std::unique_ptr<llvm::TargetMachine> targetMachine;
142};
143} // namespace LLVM
144} // namespace mlir
145
146#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.
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:88
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:152