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