MLIR 24.0.0git
CompilationInterfaces.h
Go to the documentation of this file.
1//===-- CompilationInterfaces.h - GPU compilation interfaces ---*- 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 defines interfaces for GPU compilation.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
14#define MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
15
16#include "mlir/IR/Attributes.h"
18#include "llvm/IR/Module.h"
19
20namespace llvm {
21class IRBuilderBase;
22namespace cl {
23class LibraryOptionsContext;
24} // namespace cl
25}
26
27namespace mlir {
28class SymbolTable;
29namespace LLVM {
30class ModuleTranslation;
31}
32namespace gpu {
33enum class CompilationTarget : uint32_t;
34constexpr StringLiteral elfSectionName = "section";
35
36/// This class indicates that the attribute associated with this trait is a GPU
37/// offloading translation attribute. These kinds of attributes must implement
38/// an interface for handling the translation of GPU offloading operations like
39/// `gpu.binary` & `gpu.launch_func`.
40template <typename ConcreteType>
42 : public AttributeTrait::TraitBase<ConcreteType,
43 OffloadingTranslationAttrTrait> {
44 // TODO: Verify the attribute promises or implements the interface.
45};
46
47/// This class serves as an opaque interface for passing options to the
48/// `TargetAttrInterface` methods. Users of this class must implement the
49/// `classof` method as well as using the macros `MLIR_*_EXPLICIT_TYPE_ID` to
50/// ensure type safeness. Targets are free to ignore these options.
52public:
53 /// Constructor initializing the toolkit path, the list of files to link to,
54 /// extra command line options, the compilation target and a callback for
55 /// obtaining the parent symbol table. The default compilation target is
56 /// `Fatbin`.
59 StringRef cmdOptions = {}, StringRef elfSection = {},
62 function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
63 function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
64 function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
65 function_ref<void(StringRef)> isaCallback = {});
66
67 /// Returns the typeID.
68 TypeID getTypeID() const;
69
70 /// Returns the toolkit path.
71 StringRef getToolkitPath() const;
72
73 /// Returns the LLVM libraries to link to.
75
76 /// Returns the command line options.
77 StringRef getCmdOptions() const;
78
79 /// Returns the ELF section.
80 StringRef getELFSection() const;
81
82 /// Returns a tokenization of the command line options.
83 std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
84 tokenizeCmdOptions() const;
85
86 /// Returns a tokenization of the substr of the command line options that
87 /// starts with `startsWith` and ends with end of the command line options and
88 /// consumes it.
89 std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
90 tokenizeAndRemoveSuffixCmdOptions(llvm::StringRef startsWith);
91
92 /// Returns the compilation target.
93 CompilationTarget getCompilationTarget() const;
94
95 /// Returns the result of the `getSymbolTableCallback` callback or a nullptr
96 /// if no callback was provided.
97 /// Note: The callback itself can return nullptr. It is up to the target how
98 /// to react to getting a nullptr, e.g., emitting an error or constructing the
99 /// table.
101
102 /// Returns the callback invoked with the initial LLVM IR for the device
103 /// module.
104 function_ref<void(llvm::Module &)> getInitialLlvmIRCallback() const;
105
106 /// Returns the callback invoked with LLVM IR for the device module
107 /// after linking the device libraries.
108 function_ref<void(llvm::Module &)> getLinkedLlvmIRCallback() const;
109
110 /// Returns the callback invoked with LLVM IR for the device module after
111 /// LLVM optimizations but before codegen.
112 function_ref<void(llvm::Module &)> getOptimizedLlvmIRCallback() const;
113
114 /// Returns the callback invoked with the target ISA for the device,
115 /// for example PTX assembly.
116 function_ref<void(StringRef)> getISACallback() const;
117
118 /// Returns the LLVM library options for the LLVMContext and TargetMachine
119 /// that serialization creates, or nullptr for their defaults.
120 const llvm::cl::LibraryOptionsContext *getLibraryOptions() const {
121 return libraryOptions;
122 }
123 /// Sets the LLVM library options. They must outlive the serialization.
124 void setLibraryOptions(const llvm::cl::LibraryOptionsContext *options) {
126 }
127
128 /// Returns the default compilation target: `CompilationTarget::Fatbin`.
129 static CompilationTarget getDefaultCompilationTarget();
130
131 /// Returns a tokenization of the command line options.
132 static std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
133 tokenizeCmdOptions(const std::string &cmdOptions);
134
135protected:
136 /// Derived classes must use this constructor to initialize `typeID` to the
137 /// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.
139 TypeID typeID, StringRef toolkitPath = {},
141 StringRef elfSection = {},
143 function_ref<SymbolTable *()> getSymbolTableCallback = {},
144 function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
145 function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
146 function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
147 function_ref<void(StringRef)> isaCallback = {});
148
149 /// Path to the target toolkit.
150 std::string toolkitPath;
151
152 /// List of files to link with the LLVM module.
154
155 /// An optional set of command line options to be used by the compilation
156 /// process.
157 std::string cmdOptions;
158
159 /// ELF Section where the binary needs to be located
160 std::string elfSection;
161
162 /// Compilation process target format.
163 CompilationTarget compilationTarget;
164
165 /// Callback for obtaining the parent symbol table of all the GPU modules
166 /// being serialized.
168
169 /// Callback invoked with the initial LLVM IR for the device module.
171
172 /// Callback invoked with LLVM IR for the device module after
173 /// linking the device libraries.
175
176 /// Callback invoked with LLVM IR for the device module after
177 /// LLVM optimizations but before codegen.
179
180 /// Callback invoked with the target ISA for the device,
181 /// for example PTX assembly.
183
184 const llvm::cl::LibraryOptionsContext *libraryOptions = nullptr;
185
186private:
187 TypeID typeID;
188};
189
190/// This class represents a serialized object (GPU binary) with metadata (e.g.
191/// timings, logs, ...).
193public:
195 DictionaryAttr metadata = {})
196 : object(std::move(object)), metadata(metadata) {}
197
198 const SmallVector<char, 0> &getObject() const { return object; }
199
200 DictionaryAttr getMetadata() const { return metadata; }
201
202private:
204 DictionaryAttr metadata;
205};
206
207} // namespace gpu
208} // namespace mlir
209
211
212#include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.h.inc"
213
214#endif // MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
static llvm::ManagedStatic< PassManagerOptions > options
#define MLIR_DECLARE_EXPLICIT_TYPE_ID(CLASS_NAME)
Definition TypeID.h:321
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:25
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
This class indicates that the attribute associated with this trait is a GPU offloading translation at...
SerializedObject(::mlir::SmallVector< char, 0 > object, DictionaryAttr metadata={})
const SmallVector< char, 0 > & getObject() const
DictionaryAttr getMetadata() const
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
function_ref< void(llvm::Module &)> optimizedLlvmIRCallback
Callback invoked with LLVM IR for the device module after LLVM optimizations but before codegen.
function_ref< void(StringRef)> getISACallback() const
Returns the callback invoked with the target ISA for the device, for example PTX assembly.
Definition GPUBase.cpp:436
const llvm::cl::LibraryOptionsContext * libraryOptions
TypeID getTypeID() const
Returns the typeID.
Definition GPUBase.cpp:405
void setLibraryOptions(const llvm::cl::LibraryOptionsContext *options)
Sets the LLVM library options. They must outlive the serialization.
std::string toolkitPath
Path to the target toolkit.
SymbolTable * getSymbolTable() const
Returns the result of the getSymbolTableCallback callback or a nullptr if no callback was provided.
Definition GPUBase.cpp:417
StringRef getELFSection() const
Returns the ELF section.
Definition GPUBase.cpp:415
StringRef getCmdOptions() const
Returns the command line options.
Definition GPUBase.cpp:413
const llvm::cl::LibraryOptionsContext * getLibraryOptions() const
Returns the LLVM library options for the LLVMContext and TargetMachine that serialization creates,...
std::string cmdOptions
An optional set of command line options to be used by the compilation process.
function_ref< void(StringRef)> isaCallback
Callback invoked with the target ISA for the device, for example PTX assembly.
CompilationTarget compilationTarget
Compilation process target format.
std::pair< llvm::BumpPtrAllocator, SmallVector< const char * > > tokenizeCmdOptions() const
Returns a tokenization of the command line options.
Definition GPUBase.cpp:472
function_ref< void(llvm::Module &)> initialLlvmIRCallback
Callback invoked with the initial LLVM IR for the device module.
ArrayRef< Attribute > getLibrariesToLink() const
Returns the LLVM libraries to link to.
Definition GPUBase.cpp:409
TargetOptions(StringRef toolkitPath={}, ArrayRef< Attribute > librariesToLink={}, StringRef cmdOptions={}, StringRef elfSection={}, CompilationTarget compilationTarget=getDefaultCompilationTarget(), function_ref< SymbolTable *()> getSymbolTableCallback={}, function_ref< void(llvm::Module &)> initialLlvmIRCallback={}, function_ref< void(llvm::Module &)> linkedLlvmIRCallback={}, function_ref< void(llvm::Module &)> optimizedLlvmIRCallback={}, function_ref< void(StringRef)> isaCallback={})
Constructor initializing the toolkit path, the list of files to link to, extra command line options,...
Definition GPUBase.cpp:372
function_ref< void(llvm::Module &)> getOptimizedLlvmIRCallback() const
Returns the callback invoked with LLVM IR for the device module after LLVM optimizations but before c...
Definition GPUBase.cpp:432
std::pair< llvm::BumpPtrAllocator, SmallVector< const char * > > tokenizeAndRemoveSuffixCmdOptions(llvm::StringRef startsWith)
Returns a tokenization of the substr of the command line options that starts with startsWith and ends...
Definition GPUBase.cpp:477
StringRef getToolkitPath() const
Returns the toolkit path.
Definition GPUBase.cpp:407
SmallVector< Attribute > librariesToLink
List of files to link with the LLVM module.
function_ref< void(llvm::Module &)> linkedLlvmIRCallback
Callback invoked with LLVM IR for the device module after linking the device libraries.
function_ref< void(llvm::Module &)> getInitialLlvmIRCallback() const
Returns the callback invoked with the initial LLVM IR for the device module.
Definition GPUBase.cpp:422
function_ref< SymbolTable *()> getSymbolTableCallback
Callback for obtaining the parent symbol table of all the GPU modules being serialized.
static CompilationTarget getDefaultCompilationTarget()
Returns the default compilation target: CompilationTarget::Fatbin.
Definition GPUBase.cpp:444
function_ref< void(llvm::Module &)> getLinkedLlvmIRCallback() const
Returns the callback invoked with LLVM IR for the device module after linking the device libraries.
Definition GPUBase.cpp:427
std::string elfSection
ELF Section where the binary needs to be located.
CompilationTarget getCompilationTarget() const
Returns the compilation target.
Definition GPUBase.cpp:440
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
detail::StorageUserTraitBase< ConcreteType, TraitType > TraitBase
This class represents the base of an attribute trait.
Definition Attributes.h:244
constexpr StringLiteral elfSectionName
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155