Skip to content

Commit 289a55e

Browse files
committed
[mlir][Target] ROCDL
1 parent cd94fa7 commit 289a55e

File tree

4 files changed

+237
-136
lines changed

4 files changed

+237
-136
lines changed

mlir/include/mlir/Target/LLVM/ROCDL/Utils.h

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,62 @@ namespace ROCDL {
2727
/// 5. Returns an empty string.
2828
StringRef getROCMPath();
2929

30+
/// Helper class for specifying the AMD GCN device libraries required for
31+
/// compilation.
32+
class AMDGCNLibraryList {
33+
public:
34+
typedef enum : uint32_t {
35+
None = 0,
36+
Ockl = 1,
37+
Ocml = 2,
38+
OpenCL = 4,
39+
Hip = 8,
40+
LastLib = Hip,
41+
All = (LastLib << 1) - 1
42+
} Library;
43+
44+
explicit AMDGCNLibraryList(uint32_t libs = All) : libList(All & libs) {}
45+
46+
/// Return a list with no libraries.
47+
static AMDGCNLibraryList getEmpty() { return AMDGCNLibraryList(None); }
48+
49+
/// Return the libraries needed for compiling code with OpenCL calls.
50+
static AMDGCNLibraryList getOpenCL() {
51+
return AMDGCNLibraryList(Ockl | Ocml | OpenCL);
52+
}
53+
54+
/// Returns true if the list is empty.
55+
bool isEmpty() const { return libList == None; }
56+
57+
/// Adds a library to the list.
58+
AMDGCNLibraryList addLibrary(Library lib) {
59+
libList = libList | lib;
60+
return *this;
61+
}
62+
63+
/// Adds all the libraries in `list` to the library list.
64+
AMDGCNLibraryList addList(AMDGCNLibraryList list) {
65+
libList = libList | list.libList;
66+
return *this;
67+
}
68+
69+
/// Removes a library from the list.
70+
AMDGCNLibraryList removeLibrary(Library lib) {
71+
libList = libList & ~lib;
72+
return *this;
73+
}
74+
75+
/// Returns true if `lib` is in the list of libraries.
76+
bool requiresLibrary(Library lib) const { return (libList & lib) == lib; }
77+
78+
/// Returns true if `libList` contains any of the libraries in `libs`.
79+
bool requiresAnyOf(uint32_t libs) const { return (libList & libs) != None; }
80+
81+
private:
82+
/// Library list.
83+
uint32_t libList;
84+
};
85+
3086
/// Base class for all ROCDL serializations from GPU modules into binary
3187
/// strings. By default this class serializes into LLVM bitcode.
3288
class SerializeGPUModuleBase : public LLVM::ModuleToObject {
@@ -49,8 +105,8 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
49105
/// Returns the bitcode files to be loaded.
50106
ArrayRef<std::string> getFileList() const;
51107

52-
/// Appends standard ROCm device libraries like `ocml.bc`, `ockl.bc`, etc.
53-
LogicalResult appendStandardLibs();
108+
/// Appends standard ROCm device libraries to `fileList`.
109+
LogicalResult appendStandardLibs(AMDGCNLibraryList libs);
54110

55111
/// Loads the bitcode files in `fileList`.
56112
virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
@@ -63,15 +119,20 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
63119
LogicalResult handleBitcodeFile(llvm::Module &module) override;
64120

65121
protected:
66-
/// Appends the paths of common ROCm device libraries to `libs`.
67-
LogicalResult getCommonBitcodeLibs(llvm::SmallVector<std::string> &libs,
68-
SmallVector<char, 256> &libPath,
69-
StringRef isaVersion);
70-
71122
/// Adds `oclc` control variables to the LLVM module.
72-
void addControlVariables(llvm::Module &module, bool wave64, bool daz,
73-
bool finiteOnly, bool unsafeMath, bool fastMath,
74-
bool correctSqrt, StringRef abiVer);
123+
void addControlVariables(llvm::Module &module, AMDGCNLibraryList libs,
124+
bool wave64, bool daz, bool finiteOnly,
125+
bool unsafeMath, bool fastMath, bool correctSqrt,
126+
StringRef abiVer);
127+
128+
/// Compiles assembly to a binary.
129+
virtual std::optional<SmallVector<char, 0>>
130+
compileToBinary(const std::string &serializedISA);
131+
132+
/// Default implementation of `ModuleToObject::moduleToObject`.
133+
std::optional<SmallVector<char, 0>>
134+
moduleToObjectImpl(const gpu::TargetOptions &targetOptions,
135+
llvm::Module &llvmModule);
75136

76137
/// Returns the assembled ISA.
77138
std::optional<SmallVector<char, 0>> assembleIsa(StringRef isa);
@@ -84,6 +145,9 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
84145

85146
/// List of LLVM bitcode files to link to.
86147
SmallVector<std::string> fileList;
148+
149+
/// AMD GCN libraries to use when linking, the default is using all.
150+
AMDGCNLibraryList deviceLibs = AMDGCNLibraryList::getEmpty();
87151
};
88152
} // namespace ROCDL
89153
} // namespace mlir

mlir/lib/Dialect/GPU/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ if(MLIR_ENABLE_ROCM_CONVERSIONS)
106106
"Building mlir with ROCm support requires the AMDGPU backend")
107107
endif()
108108

109-
set(DEFAULT_ROCM_PATH "/opt/rocm" CACHE PATH "Fallback path to search for ROCm installs")
109+
set(DEFAULT_ROCM_PATH "" CACHE PATH "Fallback path to search for ROCm installs")
110110
target_compile_definitions(obj.MLIRGPUTransforms
111111
PRIVATE
112112
__DEFAULT_ROCM_PATH__="${DEFAULT_ROCM_PATH}"

mlir/lib/Target/LLVM/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,12 @@ add_mlir_dialect_library(MLIRROCDLTarget
123123
)
124124

125125
if(MLIR_ENABLE_ROCM_CONVERSIONS)
126-
if (NOT ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD))
127-
message(SEND_ERROR
128-
"Building mlir with ROCm support requires the AMDGPU backend")
129-
endif()
130-
131126
if (DEFINED ROCM_PATH)
132127
set(DEFAULT_ROCM_PATH "${ROCM_PATH}" CACHE PATH "Fallback path to search for ROCm installs")
133128
elseif(DEFINED ENV{ROCM_PATH})
134129
set(DEFAULT_ROCM_PATH "$ENV{ROCM_PATH}" CACHE PATH "Fallback path to search for ROCm installs")
135130
else()
136-
set(DEFAULT_ROCM_PATH "/opt/rocm" CACHE PATH "Fallback path to search for ROCm installs")
131+
set(DEFAULT_ROCM_PATH "" CACHE PATH "Fallback path to search for ROCm installs")
137132
endif()
138133
message(VERBOSE "MLIR Default ROCM toolkit path: ${DEFAULT_ROCM_PATH}")
139134

0 commit comments

Comments
 (0)