Build `mlir_rocm_runtime` as a standalone runtime library by removing its last source-level dependency on LLVM support utilities. `mlir_rocm_runtime` is intended to be loaded dynamically by tools such as `mlir-runner` via `--shared-libs`. However, `add_mlir_library` links `LLVMSupport` by default unless the target is marked `STANDALONE`. The ROCm runtime was previously left on `DISABLE_PCH_REUSE` as a workaround because `RocmRuntimeWrappers.cpp` still used `llvm::SmallVector` and `llvm::ArrayRef` inside `mgpuMemHostRegisterMemRef`. This patch replaces that small LLVM ADT use with equivalent plain C++ logic and marks `mlir_rocm_runtime` `STANDALONE`, matching the CUDA runtime. ## Motivation This follows the direction from prior upstream changes: - [`4edc9e2acf1d`](https://github.com/llvm/llvm-project/commit/4edc9e2acf1d9350ce4da77c93e8869f774a24e2) / [D108684](https://reviews.llvm.org/D108684), by Uday Bondhugula, removed the same `SmallVector`/`ArrayRef` dependency from CUDA's `mgpuMemHostRegisterMemRef`, noting that the dependency on `LLVMSupport` adds unnecessary complexity for external users linking or loading the runtime. - [`351ae0ca550c`](https://github.com/llvm/llvm-project/commit/351ae0ca550c791b0c504dfb7dcb4fd08480d98b) / [#182850](https://github.com/llvm/llvm-project/pull/182850) made several dlopen-able MLIR runtime libraries `STANDALONE`, including `mlir_cuda_runtime`, and left `mlir_rocm_runtime` on `DISABLE_PCH_REUSE` only because it still depended on `LLVMSupport`. - [`78f259fcc14b`](https://github.com/llvm/llvm-project/commit/78f259fcc14bb0a567d45ba394fcde48de2bc299) / [#182942](https://github.com/llvm/llvm-project/pull/182942) applied the same cleanup pattern to `mlir_levelzero_runtime` by removing a small remaining LLVM dependency and marking the runtime `STANDALONE`. With the remaining LLVM ADT use removed from the ROCm wrapper, the workaround is no longer needed. ## Testing Verified and tested downstream in rocMLIR. No new upstream test is added. This is a build/linkage cleanup for a runtime library that is already exercised by the ROCm integration tests when `MLIR_ENABLE_ROCM_RUNNER` is enabled. The rewritten dense-stride computation in `mgpuMemHostRegisterMemRef` is equivalent to the previous implementation and matches the existing CUDA runtime implementation. ## AI Assistance Disclosure Assisted-by: GPT-5.5 I reviewed the generated content and verified the changes before submitting, including testing them downstream in rocMLIR. --------- Co-authored-by: Cursor <cursoragent@cursor.com>
227 lines
8.6 KiB
C++
227 lines
8.6 KiB
C++
//===- RocmRuntimeWrappers.cpp - MLIR ROCM runtime wrapper library --------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Implements C wrappers around the ROCM library for easy linking in ORC jit.
|
|
// Also adds some debugging helpers that are helpful when writing MLIR code to
|
|
// run on GPUs.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
|
|
#include "mlir/ExecutionEngine/CRunnerUtils.h"
|
|
|
|
#include "hip/hip_runtime.h"
|
|
|
|
#define HIP_REPORT_IF_ERROR(expr) \
|
|
[](hipError_t result) { \
|
|
if (!result) \
|
|
return; \
|
|
const char *name = hipGetErrorName(result); \
|
|
if (!name) \
|
|
name = "<unknown>"; \
|
|
fprintf(stderr, "'%s' failed with '%s'\n", #expr, name); \
|
|
}(expr)
|
|
|
|
thread_local static int32_t defaultDevice = 0;
|
|
|
|
extern "C" hipModule_t mgpuModuleLoad(void *data, size_t /*gpuBlobSize*/) {
|
|
hipModule_t module = nullptr;
|
|
HIP_REPORT_IF_ERROR(hipModuleLoadData(&module, data));
|
|
return module;
|
|
}
|
|
|
|
extern "C" hipModule_t mgpuModuleLoadJIT(void *data, int optLevel,
|
|
size_t /*assmeblySize*/) {
|
|
assert(false && "This function is not available in HIP.");
|
|
return nullptr;
|
|
}
|
|
|
|
extern "C" void mgpuModuleUnload(hipModule_t module) {
|
|
HIP_REPORT_IF_ERROR(hipModuleUnload(module));
|
|
}
|
|
|
|
extern "C" hipFunction_t mgpuModuleGetFunction(hipModule_t module,
|
|
const char *name) {
|
|
hipFunction_t function = nullptr;
|
|
HIP_REPORT_IF_ERROR(hipModuleGetFunction(&function, module, name));
|
|
return function;
|
|
}
|
|
|
|
// The wrapper uses intptr_t instead of ROCM's unsigned int to match
|
|
// the type of MLIR's index type. This avoids the need for casts in the
|
|
// generated MLIR code.
|
|
extern "C" void mgpuLaunchKernel(hipFunction_t function, intptr_t gridX,
|
|
intptr_t gridY, intptr_t gridZ,
|
|
intptr_t blockX, intptr_t blockY,
|
|
intptr_t blockZ, int32_t smem,
|
|
hipStream_t stream, void **params,
|
|
void **extra, size_t /*paramsCount*/) {
|
|
HIP_REPORT_IF_ERROR(hipModuleLaunchKernel(function, gridX, gridY, gridZ,
|
|
blockX, blockY, blockZ, smem,
|
|
stream, params, extra));
|
|
}
|
|
|
|
// Cooperative launch entry point. The cluster dimensions are accepted to
|
|
// match the CUDA wrapper signature, but HIP does not support thread block
|
|
// clusters; passing nonzero cluster dimensions is a usage error.
|
|
extern "C" void mgpuLaunchKernelCooperative(
|
|
hipFunction_t function, intptr_t gridX, intptr_t gridY, intptr_t gridZ,
|
|
intptr_t clusterX, intptr_t clusterY, intptr_t clusterZ, intptr_t blockX,
|
|
intptr_t blockY, intptr_t blockZ, int32_t smem, hipStream_t stream,
|
|
void **params, void ** /*extra*/) {
|
|
if (clusterX != 0 || clusterY != 0 || clusterZ != 0) {
|
|
fprintf(stderr,
|
|
"mgpuLaunchKernelCooperative: HIP does not support thread block "
|
|
"clusters (got cluster=%ld,%ld,%ld)\n",
|
|
clusterX, clusterY, clusterZ);
|
|
abort();
|
|
}
|
|
HIP_REPORT_IF_ERROR(
|
|
hipModuleLaunchCooperativeKernel(function, gridX, gridY, gridZ, blockX,
|
|
blockY, blockZ, smem, stream, params));
|
|
}
|
|
|
|
extern "C" hipStream_t mgpuStreamCreate() {
|
|
hipStream_t stream = nullptr;
|
|
HIP_REPORT_IF_ERROR(hipStreamCreate(&stream));
|
|
return stream;
|
|
}
|
|
|
|
extern "C" void mgpuStreamDestroy(hipStream_t stream) {
|
|
HIP_REPORT_IF_ERROR(hipStreamDestroy(stream));
|
|
}
|
|
|
|
extern "C" void mgpuStreamSynchronize(hipStream_t stream) {
|
|
return HIP_REPORT_IF_ERROR(hipStreamSynchronize(stream));
|
|
}
|
|
|
|
extern "C" void mgpuStreamWaitEvent(hipStream_t stream, hipEvent_t event) {
|
|
HIP_REPORT_IF_ERROR(hipStreamWaitEvent(stream, event, /*flags=*/0));
|
|
}
|
|
|
|
extern "C" hipEvent_t mgpuEventCreate() {
|
|
hipEvent_t event = nullptr;
|
|
HIP_REPORT_IF_ERROR(hipEventCreateWithFlags(&event, hipEventDisableTiming));
|
|
return event;
|
|
}
|
|
|
|
extern "C" void mgpuEventDestroy(hipEvent_t event) {
|
|
HIP_REPORT_IF_ERROR(hipEventDestroy(event));
|
|
}
|
|
|
|
extern "C" void mgpuEventSynchronize(hipEvent_t event) {
|
|
HIP_REPORT_IF_ERROR(hipEventSynchronize(event));
|
|
}
|
|
|
|
extern "C" void mgpuEventRecord(hipEvent_t event, hipStream_t stream) {
|
|
HIP_REPORT_IF_ERROR(hipEventRecord(event, stream));
|
|
}
|
|
|
|
extern "C" void *mgpuMemAlloc(uint64_t sizeBytes, hipStream_t /*stream*/,
|
|
bool /*isHostShared*/) {
|
|
void *ptr;
|
|
HIP_REPORT_IF_ERROR(hipMalloc(&ptr, sizeBytes));
|
|
return ptr;
|
|
}
|
|
|
|
extern "C" void mgpuMemFree(void *ptr, hipStream_t /*stream*/) {
|
|
HIP_REPORT_IF_ERROR(hipFree(ptr));
|
|
}
|
|
|
|
extern "C" void mgpuMemcpy(void *dst, void *src, size_t sizeBytes,
|
|
hipStream_t stream) {
|
|
HIP_REPORT_IF_ERROR(
|
|
hipMemcpyAsync(dst, src, sizeBytes, hipMemcpyDefault, stream));
|
|
}
|
|
|
|
extern "C" void mgpuMemset32(void *dst, int value, size_t count,
|
|
hipStream_t stream) {
|
|
HIP_REPORT_IF_ERROR(hipMemsetD32Async(reinterpret_cast<hipDeviceptr_t>(dst),
|
|
value, count, stream));
|
|
}
|
|
|
|
extern "C" void mgpuMemset16(void *dst, int short value, size_t count,
|
|
hipStream_t stream) {
|
|
HIP_REPORT_IF_ERROR(hipMemsetD16Async(reinterpret_cast<hipDeviceptr_t>(dst),
|
|
value, count, stream));
|
|
}
|
|
|
|
/// Helper functions for writing mlir example code
|
|
|
|
// Allows to register byte array with the ROCM runtime. Helpful until we have
|
|
// transfer functions implemented.
|
|
extern "C" void mgpuMemHostRegister(void *ptr, uint64_t sizeBytes) {
|
|
HIP_REPORT_IF_ERROR(hipHostRegister(ptr, sizeBytes, /*flags=*/0));
|
|
}
|
|
|
|
// Allows to register a MemRef with the ROCm runtime. Helpful until we have
|
|
// transfer functions implemented.
|
|
extern "C" void
|
|
mgpuMemHostRegisterMemRef(int64_t rank, StridedMemRefType<char, 1> *descriptor,
|
|
int64_t elementSizeBytes) {
|
|
int64_t *sizes = descriptor->sizes;
|
|
[[maybe_unused]] int64_t *strides = &sizes[rank];
|
|
int64_t runningStride = 1;
|
|
// Only densely packed tensors are currently supported.
|
|
for (int64_t i = rank - 1; i >= 0; --i) {
|
|
assert(strides[i] == runningStride && "Mismatch in computed dense strides");
|
|
runningStride *= sizes[i];
|
|
}
|
|
uint64_t sizeBytes = runningStride * elementSizeBytes;
|
|
|
|
auto *ptr = descriptor->data + descriptor->offset * elementSizeBytes;
|
|
mgpuMemHostRegister(ptr, sizeBytes);
|
|
}
|
|
|
|
// Allows to unregister byte array with the ROCM runtime. Helpful until we have
|
|
// transfer functions implemented.
|
|
extern "C" void mgpuMemHostUnregister(void *ptr) {
|
|
HIP_REPORT_IF_ERROR(hipHostUnregister(ptr));
|
|
}
|
|
|
|
// Allows to unregister a MemRef with the ROCm runtime. Helpful until we have
|
|
// transfer functions implemented.
|
|
extern "C" void
|
|
mgpuMemHostUnregisterMemRef(int64_t rank,
|
|
StridedMemRefType<char, 1> *descriptor,
|
|
int64_t elementSizeBytes) {
|
|
auto ptr = descriptor->data + descriptor->offset * elementSizeBytes;
|
|
mgpuMemHostUnregister(ptr);
|
|
}
|
|
|
|
template <typename T>
|
|
void mgpuMemGetDevicePointer(T *hostPtr, T **devicePtr) {
|
|
HIP_REPORT_IF_ERROR(hipSetDevice(0));
|
|
HIP_REPORT_IF_ERROR(
|
|
hipHostGetDevicePointer((void **)devicePtr, hostPtr, /*flags=*/0));
|
|
}
|
|
|
|
extern "C" StridedMemRefType<float, 1>
|
|
mgpuMemGetDeviceMemRef1dFloat(float *allocated, float *aligned, int64_t offset,
|
|
int64_t size, int64_t stride) {
|
|
float *devicePtr = nullptr;
|
|
mgpuMemGetDevicePointer(aligned, &devicePtr);
|
|
return {devicePtr, devicePtr, offset, {size}, {stride}};
|
|
}
|
|
|
|
extern "C" StridedMemRefType<int32_t, 1>
|
|
mgpuMemGetDeviceMemRef1dInt32(int32_t *allocated, int32_t *aligned,
|
|
int64_t offset, int64_t size, int64_t stride) {
|
|
int32_t *devicePtr = nullptr;
|
|
mgpuMemGetDevicePointer(aligned, &devicePtr);
|
|
return {devicePtr, devicePtr, offset, {size}, {stride}};
|
|
}
|
|
|
|
extern "C" void mgpuSetDefaultDevice(int32_t device) {
|
|
defaultDevice = device;
|
|
HIP_REPORT_IF_ERROR(hipSetDevice(device));
|
|
}
|