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