MLIR 23.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/IR/Attributes.h"
20#include "mlir/Support/LLVM.h"
22#include "llvm/ADT/BitmaskEnum.h"
23
24namespace mlir {
25namespace ROCDL {
26/// Searches & returns the path ROCM toolkit path, the search order is:
27/// 1. The `ROCM_PATH` environment variable.
28/// 2. The `ROCM_ROOT` environment variable.
29/// 3. The `ROCM_HOME` environment variable.
30/// 4. The ROCM path detected by CMake.
31/// 5. Returns an empty string.
32StringRef getROCMPath();
33
34/// Helper enum for specifying the AMD GCN device libraries required for
35/// compilation.
36enum class AMDGCNLibraries : uint32_t {
37 None = 0,
38 Ockl = 1,
39 Ocml = 2,
40 OpenCL = 4,
41 Hip = 8,
44 All = (LastLib << 1) - 1
45};
46
48
49/// Assembles ISA to an object code.
50FailureOr<SmallVector<char, 0>>
51assembleIsa(StringRef isa, StringRef targetTriple, StringRef chip,
52 StringRef features, function_ref<InFlightDiagnostic()> emitError);
53
54FailureOr<SmallVector<char, 0>>
55linkObjectCode(ArrayRef<char> objectCode, StringRef lldPath,
57
58/// Base class for all ROCDL serializations from GPU modules into binary
59/// strings. By default this class serializes into LLVM bitcode.
61public:
62 /// Initializes the `toolkitPath` with the path in `targetOptions` or if empty
63 /// with the path in `getROCMPath`.
65 const gpu::TargetOptions &targetOptions = {});
66
67 /// Initializes the LLVM AMDGPU target by safely calling
68 /// `LLVMInitializeAMDGPU*` methods if available.
69 static void init();
70
71 /// Returns the target attribute.
72 ROCDLTargetAttr getTarget() const;
73
74 /// Returns the ROCM toolkit path.
75 StringRef getToolkitPath() const;
76
77 /// Returns the LLVM bitcode libraries to be linked.
79
80 /// Appends standard ROCm device libraries to `fileList`.
81 LogicalResult appendStandardLibs(AMDGCNLibraries libs);
82
83 /// Loads the bitcode files in `fileList`.
84 virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
85 loadBitcodeFiles(llvm::Module &module) override;
86
87 /// Determines required Device Libraries and adds `oclc` control variables to
88 /// the LLVM Module if needed. Also sets
89 /// `amdhsa_code_object_version` module flag.
90 void handleModulePreLink(llvm::Module &module) override;
91
92 /// Removes unnecessary metadata from the loaded bitcode files.
93 LogicalResult handleBitcodeFile(llvm::Module &module) override;
94
95protected:
96 /// Adds `oclc` control variables to the LLVM Module if needed. It also sets
97 /// `amdhsa_code_object_version` module flag which is equal to ABI version and
98 /// it uses "llvm::Module::Error" to set that flag.
99 void addControlVariables(llvm::Module &module, AMDGCNLibraries libs,
100 bool wave64, bool daz, bool finiteOnly,
101 bool unsafeMath, bool fastMath, bool correctSqrt,
102 StringRef abiVer);
103
104 /// Compiles assembly to a binary.
105 virtual FailureOr<SmallVector<char, 0>>
106 compileToBinary(StringRef serializedISA);
107
108 /// Default implementation of `ModuleToObject::moduleToObject`.
109 FailureOr<SmallVector<char, 0>>
110 moduleToObjectImpl(const gpu::TargetOptions &targetOptions,
111 llvm::Module &llvmModule);
112
113 /// ROCDL target attribute.
114 ROCDLTargetAttr target;
115
116 /// ROCM toolkit path.
117 std::string toolkitPath;
118
119 /// List of LLVM bitcode files to link to.
121
122 /// AMD GCN libraries to use when linking, the default is using none.
124};
125
126/// Returns a map containing the `amdhsa.kernels` ELF metadata for each of the
127/// kernels in the binary, or `std::nullopt` if the metadata couldn't be
128/// retrieved. The map associates the name of the kernel with the list of named
129/// attributes found in `amdhsa.kernels`. For more information on the ELF
130/// metadata see: https://llvm.org/docs/AMDGPUUsage.html#amdhsa
131std::optional<DenseMap<StringAttr, NamedAttrList>>
133
134/// Returns a `#gpu.kernel_table` containing kernel metadata for each of the
135/// kernels in `gpuModule`. If `elfData` is valid, then the `amdhsa.kernels` ELF
136/// metadata will be added to the `#gpu.kernel_table`.
137gpu::KernelTableAttr getKernelMetadata(Operation *gpuModule,
138 ArrayRef<char> elfData = {});
139} // namespace ROCDL
140} // namespace mlir
141
142#endif // MLIR_TARGET_LLVM_ROCDL_UTILS_H
This class is a general helper class for creating context-global objects like types,...
Definition Builders.h:51
This class represents a diagnostic that is inflight and set to be reported.
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:87
ROCDLTargetAttr getTarget() const
Returns the target attribute.
Definition Target.cpp:126
ArrayRef< Attribute > getLibrariesToLink() const
Returns the LLVM bitcode libraries to be linked.
Definition Target.cpp:130
AMDGCNLibraries deviceLibs
AMD GCN libraries to use when linking, the default is using none.
Definition Utils.h:123
ROCDLTargetAttr target
ROCDL target attribute.
Definition Utils.h:114
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:95
FailureOr< SmallVector< char, 0 > > moduleToObjectImpl(const gpu::TargetOptions &targetOptions, llvm::Module &llvmModule)
Default implementation of ModuleToObject::moduleToObject.
Definition Target.cpp:403
virtual FailureOr< SmallVector< char, 0 > > compileToBinary(StringRef serializedISA)
Compiles assembly to a binary.
Definition Target.cpp:383
virtual std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module) override
Loads the bitcode files in fileList.
Definition Target.cpp:178
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 if needed.
Definition Target.cpp:229
std::string toolkitPath
ROCM toolkit path.
Definition Utils.h:117
SmallVector< Attribute > librariesToLink
List of LLVM bitcode files to link to.
Definition Utils.h:120
static void init()
Initializes the LLVM AMDGPU target by safely calling LLVMInitializeAMDGPU* methods if available.
Definition Target.cpp:112
StringRef getToolkitPath() const
Returns the ROCM toolkit path.
Definition Target.cpp:128
LogicalResult appendStandardLibs(AMDGCNLibraries libs)
Appends standard ROCm device libraries to fileList.
Definition Target.cpp:134
LogicalResult handleBitcodeFile(llvm::Module &module) override
Removes unnecessary metadata from the loaded bitcode files.
Definition Target.cpp:191
void handleModulePreLink(llvm::Module &module) override
Determines required Device Libraries and adds oclc control variables to the LLVM Module if needed.
Definition Target.cpp:204
This class serves as an opaque interface for passing options to the TargetAttrInterface methods.
FailureOr< SmallVector< char, 0 > > assembleIsa(StringRef isa, StringRef targetTriple, StringRef chip, StringRef features, function_ref< InFlightDiagnostic()> emitError)
Assembles ISA to an object code.
Definition Target.cpp:282
FailureOr< SmallVector< char, 0 > > linkObjectCode(ArrayRef< char > objectCode, StringRef lldPath, function_ref< InFlightDiagnostic()> emitError)
Definition Target.cpp:339
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE()
AMDGCNLibraries
Helper enum for specifying the AMD GCN device libraries required for compilation.
Definition Utils.h:36
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:23
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:70
StringRef getROCMPath()
Searches & returns the path ROCM toolkit path, the search order is:
Definition Target.cpp:85
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147