From 291ff6c46833e4e27a3a8653e8c995a77871114a Mon Sep 17 00:00:00 2001 From: Jin Jung Date: Fri, 7 Nov 2025 12:47:01 -0800 Subject: [PATCH] SWDEV-558855 - Enable Interop Map Buffer on Windows (#1748) * Support Windows HANDLE in interop_map_buffer * Refactored Windows HANDLE in interop_map_buffer * ROCr System Dependent Handle Type * Fix for ROCr Handle Conversion Bug * Remove Windows Header --- projects/clr/rocclr/device/rocm/rocmemory.cpp | 5 ----- projects/clr/rocclr/device/rocm/rocrctx.hpp | 3 ++- .../core/common/hsa_table_interface.cpp | 2 +- .../hsa-runtime/core/inc/hsa_ext_amd_impl.h | 2 +- .../runtime/hsa-runtime/core/inc/runtime.h | 3 ++- .../hsa-runtime/core/runtime/hsa_ext_amd.cpp | 3 ++- .../hsa-runtime/core/runtime/runtime.cpp | 17 ++++++++++++++--- .../rocr-runtime/runtime/hsa-runtime/inc/hsa.h | 9 +++++++++ .../runtime/hsa-runtime/inc/hsa_ext_amd.h | 2 +- 9 files changed, 32 insertions(+), 14 deletions(-) diff --git a/projects/clr/rocclr/device/rocm/rocmemory.cpp b/projects/clr/rocclr/device/rocm/rocmemory.cpp index 743f33173f..4cfaabfa8c 100644 --- a/projects/clr/rocclr/device/rocm/rocmemory.cpp +++ b/projects/clr/rocclr/device/rocm/rocmemory.cpp @@ -206,12 +206,7 @@ hsa_status_t Memory::interopMapBuffer(amd::Os::FileDesc fdn) { size_t size; size_t metadata_size = 0; void* metadata; -#if IS_WINDOWS - int fd = 0; - assert(!"Unimplemented"); -#else auto fd = fdn; -#endif hsa_status_t status = Hsa::interop_map_buffer(1, &agent, fd, 0, &size, &interop_deviceMemory_, &metadata_size, (const void**)&metadata); ClPrint(amd::LOG_DEBUG, amd::LOG_MEM, "Map Interop memory %p, size 0x%zx", interop_deviceMemory_, diff --git a/projects/clr/rocclr/device/rocm/rocrctx.hpp b/projects/clr/rocclr/device/rocm/rocrctx.hpp index aa7e841fb6..fb811aa67c 100644 --- a/projects/clr/rocclr/device/rocm/rocrctx.hpp +++ b/projects/clr/rocclr/device/rocm/rocrctx.hpp @@ -386,7 +386,8 @@ class Hsa : public amd::AllStatic { return ROCR_DYN(hsa_amd_memory_unlock)(host_ptr); } static hsa_status_t interop_map_buffer(uint32_t num_agents, hsa_agent_t* agents, - int interop_handle, uint32_t flags, size_t* size, + amd::Os::FileDesc interop_handle, + uint32_t flags, size_t* size, void** ptr, size_t* metadata_size, const void** metadata) { return ROCR_DYN(hsa_amd_interop_map_buffer)(num_agents, agents, interop_handle, flags, size, ptr, metadata_size, metadata); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp index e65d7302af..0c06d8c774 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp @@ -1094,7 +1094,7 @@ hsa_status_t HSA_API // Mirrors Amd Extension Apis hsa_status_t HSA_API hsa_amd_interop_map_buffer(uint32_t num_agents, hsa_agent_t* agents, - int interop_handle, + hsa_handle_t interop_handle, uint32_t flags, size_t* size, void** ptr, diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h index e4e7c58499..68288f2d0a 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/hsa_ext_amd_impl.h @@ -219,7 +219,7 @@ hsa_status_t // Mirrors Amd Extension Apis hsa_status_t hsa_amd_interop_map_buffer(uint32_t num_agents, hsa_agent_t* agents, - int interop_handle, + hsa_handle_t interop_handle, uint32_t flags, size_t* size, void** ptr, diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h index 07b461b820..251298857a 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/runtime.h @@ -346,7 +346,8 @@ class Runtime { hsa_amd_signal_handler handler, void* arg); hsa_status_t InteropMap(uint32_t num_agents, Agent** agents, - int interop_handle, uint32_t flags, size_t* size, + hsa_handle_t interop_handle, + uint32_t flags, size_t* size, void** ptr, size_t* metadata_size, const void** metadata); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp index 310bf0a16a..9e71e71f94 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/hsa_ext_amd.cpp @@ -993,7 +993,8 @@ hsa_status_t hsa_amd_agent_memory_pool_get_info( } hsa_status_t hsa_amd_interop_map_buffer(uint32_t num_agents, - hsa_agent_t* agents, int interop_handle, + hsa_agent_t* agents, + hsa_handle_t interop_handle, uint32_t flags, size_t* size, void** ptr, size_t* metadata_size, const void** metadata) { diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp index 63704e6b87..16736780b6 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -889,7 +889,8 @@ hsa_status_t Runtime::SetAsyncSignalHandler(hsa_signal_t signal, } hsa_status_t Runtime::InteropMap(uint32_t num_agents, Agent** agents, - int interop_handle, uint32_t flags, + hsa_handle_t interop_handle, + uint32_t flags, size_t* size, void** ptr, size_t* metadata_size, const void** metadata) { static const int tinyArraySize=8; @@ -897,6 +898,16 @@ hsa_status_t Runtime::InteropMap(uint32_t num_agents, Agent** agents, HSAuint32 short_nodes[tinyArraySize]; HSAuint32* nodes = short_nodes; + + static_assert(sizeof(HSAint64) >= sizeof(interop_handle), + "HSAint64 too small for interop_handle"); + HSAint64 resource_handle = +#ifdef _WIN32 + static_cast(reinterpret_cast(interop_handle)); +#else + static_cast(interop_handle); +#endif + if (num_agents > tinyArraySize) { nodes = new HSAuint32[num_agents]; if (nodes == NULL) return HSA_STATUS_ERROR_OUT_OF_RESOURCES; @@ -912,7 +923,7 @@ hsa_status_t Runtime::InteropMap(uint32_t num_agents, Agent** agents, agents[i]->GetInfo(static_cast(HSA_AMD_AGENT_INFO_DRIVER_NODE_ID), &nodes[i]); } - if (HSAKMT_CALL(hsaKmtRegisterGraphicsHandleToNodes(interop_handle, &info, num_agents, + if (HSAKMT_CALL(hsaKmtRegisterGraphicsHandleToNodes(resource_handle, &info, num_agents, nodes)) != HSAKMT_STATUS_SUCCESS) return HSA_STATUS_ERROR; @@ -1416,7 +1427,7 @@ hsa_status_t Runtime::IPCCreate(void* ptr, size_t len, hsa_amd_ipc_memory_t* han if (agent->device_type() == Agent::kAmdGpuDevice) { #if defined(__linux__) - AMD::GpuAgent* agent_ = reinterpret_cast(agent); + AMD::GpuAgent* agent_ = reinterpret_cast(agent); amdgpu_bo_import_result res; srand(static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count())); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa.h index 00753e992e..9d98bef5a0 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa.h @@ -5682,6 +5682,15 @@ typedef enum { HSA_CODE_SYMBOL_INFO_KERNEL_WAVEFRONT_SIZE = 19 } hsa_code_symbol_info_t; +/** + * @brief System dependent handle type. + */ +#if defined(_WIN32) +typedef void* hsa_handle_t; +#else +typedef int hsa_handle_t; +#endif + /** * @deprecated * diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h index ab2958a639..18c9d4e45f 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_ext_amd.h @@ -2321,7 +2321,7 @@ hsa_status_t HSA_API */ hsa_status_t HSA_API hsa_amd_interop_map_buffer(uint32_t num_agents, hsa_agent_t* agents, - int interop_handle, + hsa_handle_t interop_handle, uint32_t flags, size_t* size, void** ptr,