MLIR  20.0.0git
Utils.h
Go to the documentation of this file.
1 //===- Utils.h - MLIR ROCDL target utils ------------------------*- 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 files declares ROCDL target related utility classes and functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_TARGET_LLVM_ROCDL_UTILS_H
14 #define MLIR_TARGET_LLVM_ROCDL_UTILS_H
15 
19 #include "mlir/Support/LLVM.h"
21 
22 namespace mlir {
23 namespace ROCDL {
24 /// Searches & returns the path ROCM toolkit path, the search order is:
25 /// 1. The `ROCM_PATH` environment variable.
26 /// 2. The `ROCM_ROOT` environment variable.
27 /// 3. The `ROCM_HOME` environment variable.
28 /// 4. The ROCM path detected by CMake.
29 /// 5. Returns an empty string.
30 StringRef getROCMPath();
31 
32 /// Helper enum for specifying the AMD GCN device libraries required for
33 /// compilation.
34 enum class AMDGCNLibraries : uint32_t {
35  None = 0,
36  Ockl = 1,
37  Ocml = 2,
38  OpenCL = 4,
39  Hip = 8,
40  LastLib = Hip,
42  All = (LastLib << 1) - 1
43 };
44 
45 /// Base class for all ROCDL serializations from GPU modules into binary
46 /// strings. By default this class serializes into LLVM bitcode.
48 public:
49  /// Initializes the `toolkitPath` with the path in `targetOptions` or if empty
50  /// with the path in `getROCMPath`.
52  const gpu::TargetOptions &targetOptions = {});
53 
54  /// Initializes the LLVM AMDGPU target by safely calling
55  /// `LLVMInitializeAMDGPU*` methods if available.
56  static void init();
57 
58  /// Returns the target attribute.
59  ROCDLTargetAttr getTarget() const;
60 
61  /// Returns the ROCM toolkit path.
62  StringRef getToolkitPath() const;
63 
64  /// Returns the bitcode files to be loaded.
66 
67  /// Appends standard ROCm device libraries to `fileList`.
68  LogicalResult appendStandardLibs(AMDGCNLibraries libs);
69 
70  /// Loads the bitcode files in `fileList`.
71  virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
72  loadBitcodeFiles(llvm::Module &module) override;
73 
74  /// Adds `oclc` control variables to the LLVM module.
75  void handleModulePreLink(llvm::Module &module) override;
76 
77  /// Removes unnecessary metadata from the loaded bitcode files.
78  LogicalResult handleBitcodeFile(llvm::Module &module) override;
79 
80 protected:
81  /// Adds `oclc` control variables to the LLVM module.
82  void addControlVariables(llvm::Module &module, AMDGCNLibraries libs,
83  bool wave64, bool daz, bool finiteOnly,
84  bool unsafeMath, bool fastMath, bool correctSqrt,
85  StringRef abiVer);
86 
87  /// Compiles assembly to a binary.
88  virtual std::optional<SmallVector<char, 0>>
89  compileToBinary(const std::string &serializedISA);
90 
91  /// Default implementation of `ModuleToObject::moduleToObject`.
92  std::optional<SmallVector<char, 0>>
93  moduleToObjectImpl(const gpu::TargetOptions &targetOptions,
94  llvm::Module &llvmModule);
95 
96  /// Returns the assembled ISA.
97  std::optional<SmallVector<char, 0>> assembleIsa(StringRef isa);
98 
99  /// ROCDL target attribute.
100  ROCDLTargetAttr target;
101 
102  /// ROCM toolkit path.
103  std::string toolkitPath;
104 
105  /// List of LLVM bitcode files to link to.
107 
108  /// AMD GCN libraries to use when linking, the default is using none.
110 };
111 
112 /// Returns a map containing the `amdhsa.kernels` ELF metadata for each of the
113 /// kernels in the binary, or `std::nullopt` if the metadata couldn't be
114 /// retrieved. The map associates the name of the kernel with the list of named
115 /// attributes found in `amdhsa.kernels`. For more information on the ELF
116 /// metadata see: https://llvm.org/docs/AMDGPUUsage.html#amdhsa
117 std::optional<DenseMap<StringAttr, NamedAttrList>>
119 
120 /// Returns a `#gpu.kernel_table` containing kernel metadata for each of the
121 /// kernels in `gpuModule`. If `elfData` is valid, then the `amdhsa.kernels` ELF
122 /// metadata will be added to the `#gpu.kernel_table`.
123 gpu::KernelTableAttr getKernelMetadata(Operation *gpuModule,
124  ArrayRef<char> elfData = {});
125 } // namespace ROCDL
126 } // namespace mlir
127 
128 #endif // MLIR_TARGET_LLVM_ROCDL_UTILS_H
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
Utility base class for transforming operations into binary objects, by default it returns the seriali...
Operation & module
Module to transform to a binary object.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Base class for all ROCDL serializations from GPU modules into binary strings.
Definition: Utils.h:47
ROCDLTargetAttr getTarget() const
Returns the target attribute.
Definition: Target.cpp:127
ArrayRef< std::string > getFileList() const
Returns the bitcode files to be loaded.
Definition: Target.cpp:131
AMDGCNLibraries deviceLibs
AMD GCN libraries to use when linking, the default is using none.
Definition: Utils.h:109
ROCDLTargetAttr target
ROCDL target attribute.
Definition: Utils.h:100
SerializeGPUModuleBase(Operation &module, ROCDLTargetAttr target, const gpu::TargetOptions &targetOptions={})
Initializes the toolkitPath with the path in targetOptions or if empty with the path in getROCMPath.
Definition: Target.cpp:94
std::optional< SmallVector< char, 0 > > moduleToObjectImpl(const gpu::TargetOptions &targetOptions, llvm::Module &llvmModule)
Default implementation of ModuleToObject::moduleToObject.
Definition: Target.cpp:404
virtual std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module) override
Loads the bitcode files in fileList.
Definition: Target.cpp:179
void addControlVariables(llvm::Module &module, AMDGCNLibraries libs, bool wave64, bool daz, bool finiteOnly, bool unsafeMath, bool fastMath, bool correctSqrt, StringRef abiVer)
Adds oclc control variables to the LLVM module.
Definition: Target.cpp:230
std::optional< SmallVector< char, 0 > > assembleIsa(StringRef isa)
Returns the assembled ISA.
Definition: Target.cpp:281
SmallVector< std::string > fileList
List of LLVM bitcode files to link to.
Definition: Utils.h:106
std::string toolkitPath
ROCM toolkit path.
Definition: Utils.h:103
static void init()
Initializes the LLVM AMDGPU target by safely calling LLVMInitializeAMDGPU* methods if available.
Definition: Target.cpp:113
StringRef getToolkitPath() const
Returns the ROCM toolkit path.
Definition: Target.cpp:129
LogicalResult appendStandardLibs(AMDGCNLibraries libs)
Appends standard ROCm device libraries to fileList.
Definition: Target.cpp:135
LogicalResult handleBitcodeFile(llvm::Module &module) override
Removes unnecessary metadata from the loaded bitcode files.
Definition: Target.cpp:192
virtual std::optional< SmallVector< char, 0 > > compileToBinary(const std::string &serializedISA)
Compiles assembly to a binary.
Definition: Target.cpp:345
void handleModulePreLink(llvm::Module &module) override
Adds oclc control variables to the LLVM module.
Definition: Target.cpp:205
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
AMDGCNLibraries
Helper enum for specifying the AMD GCN device libraries required for compilation.
Definition: Utils.h:34
std::optional< DenseMap< StringAttr, NamedAttrList > > getAMDHSAKernelsELFMetadata(Builder &builder, ArrayRef< char > elfData)
Returns a map containing the amdhsa.kernels ELF metadata for each of the kernels in the binary,...
Definition: Utils.cpp:24
gpu::KernelTableAttr getKernelMetadata(Operation *gpuModule, ArrayRef< char > elfData={})
Returns a #gpu.kernel_table containing kernel metadata for each of the kernels in gpuModule.
Definition: Utils.cpp:71
StringRef getROCMPath()
Searches & returns the path ROCM toolkit path, the search order is:
Definition: Target.cpp:84
Include the generated interface declarations.