From c03048d68ef97f1309c04f377ad9b6e6f9b452e6 Mon Sep 17 00:00:00 2001 From: Jimbo <57198431+jiabaxie@users.noreply.github.com> Date: Tue, 26 Aug 2025 11:34:49 -0400 Subject: [PATCH] Implement hipMemAllocationTypeUncached in hipMemCreate (#747) * Revert "SWDEV-547589 - Add hipDeviceMallocUncached to hipMemCreate (#815)" This reverts commit 5ce7103555c8483e04d84ad314fb86dd98b7afdc. * Revert "SWDEV-547589 - comment for flag hipDeviceMallocUncached in hipMemcreate (#339)" This reverts commit 04dac5eae38c8a86aa59fa1c7530233e1a01327c. * SWDEV-551942 - implement hipMemAllocationTypeUncached in hipMemCreate --- projects/clr/CHANGELOG.md | 4 +++- projects/clr/hipamd/src/hip_vm.cpp | 24 ++++++++++------------ projects/hip/include/hip/hip_runtime_api.h | 3 ++- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/projects/clr/CHANGELOG.md b/projects/clr/CHANGELOG.md index 8549873d1b..101ac50e84 100644 --- a/projects/clr/CHANGELOG.md +++ b/projects/clr/CHANGELOG.md @@ -27,7 +27,9 @@ Full documentation for HIP is available at [rocm.docs.amd.com](https://rocm.docs - `hipMemAdvise_v2` advise about the usage of a given memory range - `hipGetDriverEntryPoint ` gets function pointer of a HIP API. * Changed HIP APIs - - `hipMemCreate` now can take hipDeviceMallocUncached as a flag to allocate uncached memory + - `hipMemAllocationType` now has hip exclusive enum hipMemAllocationTypeUncached + - `hipMemCreate` now checks for hipMemAllocationTypeUncached enum from + hipMemAllocationType and allocates uncached memory if so ### Optimized diff --git a/projects/clr/hipamd/src/hip_vm.cpp b/projects/clr/hipamd/src/hip_vm.cpp index 1ef80a72bc..37dbc8a219 100644 --- a/projects/clr/hipamd/src/hip_vm.cpp +++ b/projects/clr/hipamd/src/hip_vm.cpp @@ -85,12 +85,9 @@ hipError_t hipMemCreate(hipMemGenericAllocationHandle_t* handle, size_t size, HIP_INIT_API(hipMemCreate, handle, size, prop, flags); // Currently we do not support Pinned memory - if (handle == nullptr || size == 0 || prop == nullptr || - prop->type != hipMemAllocationTypePinned || prop->location.type != hipMemLocationTypeDevice) { - HIP_RETURN(hipErrorInvalidValue); - } - - if (flags != hipDeviceMallocUncached && flags != 0) { + if (handle == nullptr || size == 0 || flags != 0 || prop == nullptr || + (prop->type != hipMemAllocationTypePinned && prop->type != hipMemAllocationTypeUncached) || + prop->location.type != hipMemLocationTypeDevice) { HIP_RETURN(hipErrorInvalidValue); } @@ -103,6 +100,12 @@ hipError_t hipMemCreate(hipMemGenericAllocationHandle_t* handle, size_t size, HIP_RETURN(hipErrorNotSupported); } + // When ROCCLR_MEM_PHYMEM is set, ROCr impl gets and stores unique hsa handle. Flag no-op on PAL. + unsigned int ihipFlags = ROCCLR_MEM_PHYMEM; + if (prop->type == hipMemAllocationTypeUncached) { + ihipFlags |= CL_MEM_SVM_ATOMICS | ROCCLR_MEM_HSA_UNCACHED; + } + // Device info validation const auto& dev_info = g_devices[prop->location.id]->devices()[0]->info(); @@ -115,13 +118,8 @@ hipError_t hipMemCreate(hipMemGenericAllocationHandle_t* handle, size_t size, amd::Context* amdContext = g_devices[prop->location.id]->asContext(); - // When ROCCLR_MEM_PHYMEM is set, ROCr impl gets and stores unique hsa handle. Flag no-op on PAL. - uint64_t ihipFlags = ROCCLR_MEM_PHYMEM; - if (flags == hipDeviceMallocUncached) { - ihipFlags |= ROCCLR_MEM_HSA_UNCACHED | CL_MEM_SVM_ATOMICS; - } - void* ptr = - amd::SvmBuffer::malloc(*amdContext, ihipFlags, size, dev_info.memBaseAddrAlign_, nullptr); + void* ptr = amd::SvmBuffer::malloc(*amdContext, ihipFlags, size, + dev_info.memBaseAddrAlign_, nullptr); // Handle out of memory cases, if (ptr == nullptr) { diff --git a/projects/hip/include/hip/hip_runtime_api.h b/projects/hip/include/hip/hip_runtime_api.h index 381f5ea923..b3b8889d1f 100644 --- a/projects/hip/include/hip/hip_runtime_api.h +++ b/projects/hip/include/hip/hip_runtime_api.h @@ -1194,6 +1194,7 @@ typedef enum hipMemAllocationType { * location while the application is actively using it */ hipMemAllocationTypePinned = 0x1, + hipMemAllocationTypeUncached = 0x40000000, hipMemAllocationTypeMax = 0x7FFFFFFF } hipMemAllocationType; /** @@ -9137,7 +9138,7 @@ hipError_t hipMemAddressReserve(void** ptr, size_t size, size_t alignment, void* * @param [out] handle - value of the returned handle. * @param [in] size - size of the allocation. * @param [in] prop - properties of the allocation. - * @param [in] flags - hipDeviceMallocUncached for uncached allocation, or 0 for default + * @param [in] flags - currently unused, must be zero. * @returns #hipSuccess, #hipErrorInvalidValue, #hipErrorNotSupported * @warning This API is marked as Beta. While this feature is complete, it can * change and might have outstanding issues.