MLIR  20.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 
31 /// This class indicates that the attribute associated with this trait is a GPU
32 /// offloading translation attribute. These kinds of attributes must implement
33 /// an interface for handling the translation of GPU offloading operations like
34 /// `gpu.binary` & `gpu.launch_func`.
35 template <typename ConcreteType>
37  : public AttributeTrait::TraitBase<ConcreteType,
38  OffloadingTranslationAttrTrait> {
39  // TODO: Verify the attribute promises or implements the interface.
40 };
41 
42 /// This class serves as an opaque interface for passing options to the
43 /// `TargetAttrInterface` methods. Users of this class must implement the
44 /// `classof` method as well as using the macros `MLIR_*_EXPLICIT_TYPE_ID` to
45 /// ensure type safeness. Targets are free to ignore these options.
47 public:
48  /// Constructor initializing the toolkit path, the list of files to link to,
49  /// extra command line options, the compilation target and a callback for
50  /// obtaining the parent symbol table. The default compilation target is
51  /// `Fatbin`.
53  StringRef toolkitPath = {}, ArrayRef<std::string> linkFiles = {},
54  StringRef cmdOptions = {},
55  CompilationTarget compilationTarget = getDefaultCompilationTarget(),
57  function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
58  function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
59  function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
60  function_ref<void(StringRef)> isaCallback = {});
61 
62  /// Returns the typeID.
63  TypeID getTypeID() const;
64 
65  /// Returns the toolkit path.
66  StringRef getToolkitPath() const;
67 
68  /// Returns the files to link to.
70 
71  /// Returns the command line options.
72  StringRef getCmdOptions() const;
73 
74  /// Returns a tokenization of the command line options.
75  std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
76  tokenizeCmdOptions() const;
77 
78  /// Returns the compilation target.
79  CompilationTarget getCompilationTarget() const;
80 
81  /// Returns the result of the `getSymbolTableCallback` callback or a nullptr
82  /// if no callback was provided.
83  /// Note: The callback itself can return nullptr. It is up to the target how
84  /// to react to getting a nullptr, e.g., emitting an error or constructing the
85  /// table.
86  SymbolTable *getSymbolTable() const;
87 
88  /// Returns the callback invoked with the initial LLVM IR for the device
89  /// module.
90  function_ref<void(llvm::Module &)> getInitialLlvmIRCallback() const;
91 
92  /// Returns the callback invoked with LLVM IR for the device module
93  /// after linking the device libraries.
94  function_ref<void(llvm::Module &)> getLinkedLlvmIRCallback() const;
95 
96  /// Returns the callback invoked with LLVM IR for the device module after
97  /// LLVM optimizations but before codegen.
98  function_ref<void(llvm::Module &)> getOptimizedLlvmIRCallback() const;
99 
100  /// Returns the callback invoked with the target ISA for the device,
101  /// for example PTX assembly.
102  function_ref<void(StringRef)> getISACallback() const;
103 
104  /// Returns the default compilation target: `CompilationTarget::Fatbin`.
105  static CompilationTarget getDefaultCompilationTarget();
106 
107 protected:
108  /// Derived classes must use this constructor to initialize `typeID` to the
109  /// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.
111  TypeID typeID, StringRef toolkitPath = {},
112  ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
113  CompilationTarget compilationTarget = getDefaultCompilationTarget(),
115  function_ref<void(llvm::Module &)> initialLlvmIRCallback = {},
116  function_ref<void(llvm::Module &)> linkedLlvmIRCallback = {},
117  function_ref<void(llvm::Module &)> optimizedLlvmIRCallback = {},
118  function_ref<void(StringRef)> isaCallback = {});
119 
120  /// Path to the target toolkit.
121  std::string toolkitPath;
122 
123  /// List of files to link with the LLVM module.
125 
126  /// An optional set of command line options to be used by the compilation
127  /// process.
128  std::string cmdOptions;
129 
130  /// Compilation process target format.
131  CompilationTarget compilationTarget;
132 
133  /// Callback for obtaining the parent symbol table of all the GPU modules
134  /// being serialized.
136 
137  /// Callback invoked with the initial LLVM IR for the device module.
138  function_ref<void(llvm::Module &)> initialLlvmIRCallback;
139 
140  /// Callback invoked with LLVM IR for the device module after
141  /// linking the device libraries.
142  function_ref<void(llvm::Module &)> linkedLlvmIRCallback;
143 
144  /// Callback invoked with LLVM IR for the device module after
145  /// LLVM optimizations but before codegen.
146  function_ref<void(llvm::Module &)> optimizedLlvmIRCallback;
147 
148  /// Callback invoked with the target ISA for the device,
149  /// for example PTX assembly.
150  function_ref<void(StringRef)> isaCallback;
151 
152 private:
153  TypeID typeID;
154 };
155 } // namespace gpu
156 } // namespace mlir
157 
159 
160 #include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.h.inc"
161 
162 #endif // MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
#define MLIR_DECLARE_EXPLICIT_TYPE_ID(CLASS_NAME)
Definition: TypeID.h:249
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:104
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.
TargetOptions(StringRef toolkitPath={}, ArrayRef< std::string > linkFiles={}, StringRef cmdOptions={}, 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,...
ArrayRef< std::string > getLinkFiles() const
Returns the files to link to.
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 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.
function_ref< void(llvm::Module &)> getOptimizedLlvmIRCallback() const
Returns the callback invoked with LLVM IR for the device module after LLVM optimizations but before c...
StringRef getToolkitPath() const
Returns the toolkit path.
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.
SmallVector< std::string > linkFiles
List of files to link with the LLVM 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.
CompilationTarget getCompilationTarget() const
Returns the compilation target.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
Include the generated interface declarations.