MLIR 23.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;
22}
23
24namespace mlir {
25class SymbolTable;
26namespace LLVM {
28}
29namespace gpu {
30enum class CompilationTarget : uint32_t;
31constexpr StringLiteral elfSectionName = "section";
32
33/// This class indicates that the attribute associated with this trait is a GPU
34/// offloading translation attribute. These kinds of attributes must implement
35/// an interface for handling the translation of GPU offloading operations like
36/// `gpu.binary` & `gpu.launch_func`.
37template <typename ConcreteType>
39 : public AttributeTrait::TraitBase<ConcreteType,
40 OffloadingTranslationAttrTrait> {
41 // TODO: Verify the attribute promises or implements the interface.
42};
43
44/// This class serves as an opaque interface for passing options to the
45/// `TargetAttrInterface` methods. Users of this class must implement the
46/// `classof` method as well as using the macros `MLIR_*_EXPLICIT_TYPE_ID` to
47/// ensure type safeness. Targets are free to ignore these options.
49public:
50 /// Constructor initializing the toolkit path, the list of files to link to,
51 /// extra command line options, the compilation target and a callback for
52 /// obtaining the parent symbol table. The default compilation target is
53 /// `Fatbin`.
56 StringRef cmdOptions = {}, StringRef elfSection = {},
59 function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
60 function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
61 function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
62 function_ref<void(StringRef)> isaCallback = {});
63
64 /// Returns the typeID.
65 TypeID getTypeID() const;
66
67 /// Returns the toolkit path.
68 StringRef getToolkitPath() const;
69
70 /// Returns the LLVM libraries to link to.
72
73 /// Returns the command line options.
74 StringRef getCmdOptions() const;
75
76 /// Returns the ELF section.
77 StringRef getELFSection() const;
78
79 /// Returns a tokenization of the command line options.
80 std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
81 tokenizeCmdOptions() const;
82
83 /// Returns a tokenization of the substr of the command line options that
84 /// starts with `startsWith` and ends with end of the command line options and
85 /// consumes it.
86 std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
87 tokenizeAndRemoveSuffixCmdOptions(llvm::StringRef startsWith);
88
89 /// Returns the compilation target.
90 CompilationTarget getCompilationTarget() const;
91
92 /// Returns the result of the `getSymbolTableCallback` callback or a nullptr
93 /// if no callback was provided.
94 /// Note: The callback itself can return nullptr. It is up to the target how
95 /// to react to getting a nullptr, e.g., emitting an error or constructing the
96 /// table.
98
99 /// Returns the callback invoked with the initial LLVM IR for the device
100 /// module.
101 function_ref<void(llvm::Module &)> getInitialLlvmIRCallback() const;
102
103 /// Returns the callback invoked with LLVM IR for the device module
104 /// after linking the device libraries.
105 function_ref<void(llvm::Module &)> getLinkedLlvmIRCallback() const;
106
107 /// Returns the callback invoked with LLVM IR for the device module after
108 /// LLVM optimizations but before codegen.
109 function_ref<void(llvm::Module &)> getOptimizedLlvmIRCallback() const;
110
111 /// Returns the callback invoked with the target ISA for the device,
112 /// for example PTX assembly.
113 function_ref<void(StringRef)> getISACallback() const;
114
115 /// Returns the default compilation target: `CompilationTarget::Fatbin`.
116 static CompilationTarget getDefaultCompilationTarget();
117
118 /// Returns a tokenization of the command line options.
119 static std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
120 tokenizeCmdOptions(const std::string &cmdOptions);
121
122protected:
123 /// Derived classes must use this constructor to initialize `typeID` to the
124 /// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.
126 TypeID typeID, StringRef toolkitPath = {},
128 StringRef elfSection = {},
131 function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
132 function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
133 function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
134 function_ref<void(StringRef)> isaCallback = {});
135
136 /// Path to the target toolkit.
137 std::string toolkitPath;
138
139 /// List of files to link with the LLVM module.
141
142 /// An optional set of command line options to be used by the compilation
143 /// process.
144 std::string cmdOptions;
145
146 /// ELF Section where the binary needs to be located
147 std::string elfSection;
148
149 /// Compilation process target format.
150 CompilationTarget compilationTarget;
151
152 /// Callback for obtaining the parent symbol table of all the GPU modules
153 /// being serialized.
155
156 /// Callback invoked with the initial LLVM IR for the device module.
158
159 /// Callback invoked with LLVM IR for the device module after
160 /// linking the device libraries.
162
163 /// Callback invoked with LLVM IR for the device module after
164 /// LLVM optimizations but before codegen.
166
167 /// Callback invoked with the target ISA for the device,
168 /// for example PTX assembly.
170
171private:
172 TypeID typeID;
173};
174
175/// This class represents a serialized object (GPU binary) with metadata (e.g.
176/// timings, logs, ...).
178public:
180 DictionaryAttr metadata = {})
181 : object(std::move(object)), metadata(metadata) {}
182
183 const SmallVector<char, 0> &getObject() const { return object; }
184
185 DictionaryAttr getMetadata() const { return metadata; }
186
187private:
189 DictionaryAttr metadata;
190};
191
192} // namespace gpu
193} // namespace mlir
194
196
197#include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.h.inc"
198
199#endif // MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
#define MLIR_DECLARE_EXPLICIT_TYPE_ID(CLASS_NAME)
Definition TypeID.h:321
Implementation class for module translation.
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:24
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.
TypeID getTypeID() const
Returns the typeID.
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.
StringRef getELFSection() const
Returns the ELF section.
StringRef getCmdOptions() const
Returns the command line options.
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.
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.
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,...
function_ref< void(llvm::Module &)> getOptimizedLlvmIRCallback() const
Returns the callback invoked with LLVM IR for the device module after LLVM optimizations but before c...
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...
StringRef getToolkitPath() const
Returns the toolkit path.
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.
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.
function_ref< void(llvm::Module &)> getLinkedLlvmIRCallback() const
Returns the callback invoked with LLVM IR for the device module after linking the device libraries.
std::string elfSection
ELF Section where the binary needs to be located.
CompilationTarget getCompilationTarget() const
Returns the compilation target.
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:242
constexpr StringLiteral elfSectionName
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:144