Skip to content

Commit

Permalink
[mlir][Target] Improve ROCDL gpu serialization API (#95456)
Browse files Browse the repository at this point in the history
This patch improves the ROCDL gpu serialization API by:
- Introducing the enum `AMDGCNLibraries` for specifying the AMD GCN
device code libraries to use during linking.
- Removing `getCommonBitcodeLibs` in favor of `AMDGCNLibraries`.
Previously `getCommonBitcodeLibs` would try to load all AMD GCN bitcode
librariesm now it will only load the requested libraries.
- Exposing the `compileToBinary` method and making it virtual, allowing
downstream users to re-use this method.
- Exposing `moduleToObjectImpl`, this method provides a prototype flow
for compiling to binary, allowing downstream users to re-use this
method.
- It also avoids constructing the control variables if no device
libraries are being used.

This patch also changes the behavior of the CMake flag
`DEFAULT_ROCM_PATH`. Before it would fall back to a default value of
`/opt/rocm` if not specified. However, that default value causes fragile
builds in environments with ROCm. Now, the flag falls back to the empty
string, making it clear that **the user must provide a value at LLVM
build time**.
  • Loading branch information
fabianmcg authored Jun 17, 2024
1 parent 534f856 commit 954cb5f
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 136 deletions.
41 changes: 31 additions & 10 deletions mlir/include/mlir/Target/LLVM/ROCDL/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ namespace ROCDL {
/// 5. Returns an empty string.
StringRef getROCMPath();

/// Helper enum for specifying the AMD GCN device libraries required for
/// compilation.
enum class AMDGCNLibraries : uint32_t {
None = 0,
Ockl = 1,
Ocml = 2,
OpenCL = 4,
Hip = 8,
LastLib = Hip,
LLVM_MARK_AS_BITMASK_ENUM(LastLib),
All = (LastLib << 1) - 1
};

/// Base class for all ROCDL serializations from GPU modules into binary
/// strings. By default this class serializes into LLVM bitcode.
class SerializeGPUModuleBase : public LLVM::ModuleToObject {
Expand All @@ -49,8 +62,8 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
/// Returns the bitcode files to be loaded.
ArrayRef<std::string> getFileList() const;

/// Appends standard ROCm device libraries like `ocml.bc`, `ockl.bc`, etc.
LogicalResult appendStandardLibs();
/// Appends standard ROCm device libraries to `fileList`.
LogicalResult appendStandardLibs(AMDGCNLibraries libs);

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

protected:
/// Appends the paths of common ROCm device libraries to `libs`.
LogicalResult getCommonBitcodeLibs(llvm::SmallVector<std::string> &libs,
SmallVector<char, 256> &libPath,
StringRef isaVersion);

/// Adds `oclc` control variables to the LLVM module.
void addControlVariables(llvm::Module &module, bool wave64, bool daz,
bool finiteOnly, bool unsafeMath, bool fastMath,
bool correctSqrt, StringRef abiVer);
void addControlVariables(llvm::Module &module, AMDGCNLibraries libs,
bool wave64, bool daz, bool finiteOnly,
bool unsafeMath, bool fastMath, bool correctSqrt,
StringRef abiVer);

/// Compiles assembly to a binary.
virtual std::optional<SmallVector<char, 0>>
compileToBinary(const std::string &serializedISA);

/// Default implementation of `ModuleToObject::moduleToObject`.
std::optional<SmallVector<char, 0>>
moduleToObjectImpl(const gpu::TargetOptions &targetOptions,
llvm::Module &llvmModule);

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

/// List of LLVM bitcode files to link to.
SmallVector<std::string> fileList;

/// AMD GCN libraries to use when linking, the default is using none.
AMDGCNLibraries deviceLibs = AMDGCNLibraries::None;
};
} // namespace ROCDL
} // namespace mlir
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/GPU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ if(MLIR_ENABLE_ROCM_CONVERSIONS)
"Building mlir with ROCm support requires the AMDGPU backend")
endif()

set(DEFAULT_ROCM_PATH "/opt/rocm" CACHE PATH "Fallback path to search for ROCm installs")
set(DEFAULT_ROCM_PATH "" CACHE PATH "Fallback path to search for ROCm installs")
target_compile_definitions(obj.MLIRGPUTransforms
PRIVATE
__DEFAULT_ROCM_PATH__="${DEFAULT_ROCM_PATH}"
Expand Down
7 changes: 1 addition & 6 deletions mlir/lib/Target/LLVM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,12 @@ add_mlir_dialect_library(MLIRROCDLTarget
)

if(MLIR_ENABLE_ROCM_CONVERSIONS)
if (NOT ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD))
message(SEND_ERROR
"Building mlir with ROCm support requires the AMDGPU backend")
endif()

if (DEFINED ROCM_PATH)
set(DEFAULT_ROCM_PATH "${ROCM_PATH}" CACHE PATH "Fallback path to search for ROCm installs")
elseif(DEFINED ENV{ROCM_PATH})
set(DEFAULT_ROCM_PATH "$ENV{ROCM_PATH}" CACHE PATH "Fallback path to search for ROCm installs")
else()
set(DEFAULT_ROCM_PATH "/opt/rocm" CACHE PATH "Fallback path to search for ROCm installs")
set(DEFAULT_ROCM_PATH "" CACHE PATH "Fallback path to search for ROCm installs")
endif()
message(VERBOSE "MLIR Default ROCM toolkit path: ${DEFAULT_ROCM_PATH}")

Expand Down
Loading

0 comments on commit 954cb5f

Please sign in to comment.