From afa15ddb892f42cf075fee2e87396ae82825c139 Mon Sep 17 00:00:00 2001 From: pghafari Date: Tue, 11 Apr 2023 13:37:45 -0400 Subject: [PATCH] SWDEV-367145 - adding support for timeline semaphore Change-Id: I654359b48abe8bbaac2def5118d3ad0bb05d9492 --- hipamd/src/hip_memory.cpp | 10 ++++++---- rocclr/device/device.hpp | 23 ++++++++++++++++++++++- rocclr/device/pal/paldevice.cpp | 9 ++++++++- rocclr/device/pal/paldevice.hpp | 9 +++++++-- rocclr/device/rocm/rocdevice.hpp | 3 ++- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/hipamd/src/hip_memory.cpp b/hipamd/src/hip_memory.cpp index 7fa3d8dea5..262ad421ef 100644 --- a/hipamd/src/hip_memory.cpp +++ b/hipamd/src/hip_memory.cpp @@ -181,14 +181,16 @@ hipError_t hipImportExternalSemaphore(hipExternalSemaphore_t* extSem_out, (semHandleDesc->type > hipExternalSemaphoreHandleTypeD3D12Fence)) { HIP_RETURN(hipErrorInvalidValue); } - amd::Device* device = hip::getCurrentDevice()->devices()[0]; #ifdef _WIN32 - if (device->importExtSemaphore(extSem_out, semHandleDesc->handle.win32.handle)) { + if (device->importExtSemaphore(extSem_out, semHandleDesc->handle.win32.handle, + static_cast + (semHandleDesc->type))) { #else - if (device->importExtSemaphore( - extSem_out, semHandleDesc->handle.fd)) { + if (device->importExtSemaphore(extSem_out, semHandleDesc->handle.fd, + static_cast + (semHandleDesc->type))) { #endif HIP_RETURN(hipSuccess); } diff --git a/rocclr/device/device.hpp b/rocclr/device/device.hpp index 25fcabdf87..1a37677864 100644 --- a/rocclr/device/device.hpp +++ b/rocclr/device/device.hpp @@ -123,6 +123,26 @@ enum MemRangeAttribute : uint32_t { constexpr int CpuDeviceId = static_cast(-1); constexpr int InvalidDeviceId = static_cast(-2); +enum class ExternalSemaphoreHandleType : uint32_t { + OpaqueFd = 1, // Handle is an opaque file descriptor + OpaqueWin32 = 2, // Handle is an opaque shared NT handle + OpaqueWin32Kmt = 3, // Handle is an opaque, globally shared handle + D3D12Fence = 4, // Handle is a shared NT handle referencing a + // D3D12 fence object + D3D11Fence = 5, // Handle is a shared NT handle referencing a + // D3D11 fence object + NvSciSync = 6, // Opaque handle to NvSciSync Object + KeyedMutex = 7, // Handle is a shared NT handle referencing a + // D3D11 keyed mutex object + KeyedMutexKmt = 8, // Handle is a shared KMT handle referencing a + // D3D11 keyed mutex object + TimelineSemaphoreFd = 9, // Handle is an opaque handle file + // descriptor referencing a timeline + // semaphore + TimelineSemaphoreWin32 = 10, // Handle is an opaque handle file + // descriptor referencing a timeline + // semaphore +}; } // namespace amd enum OclExtensions { @@ -1696,7 +1716,8 @@ class Device : public RuntimeObject { virtual bool globalFreeMemory(size_t* freeMemory //!< Free memory information on a GPU device ) const = 0; - virtual bool importExtSemaphore(void** extSemaphore, const amd::Os::FileDesc& handle) = 0; + virtual bool importExtSemaphore(void** extSemaphore, const amd::Os::FileDesc& handle, + amd::ExternalSemaphoreHandleType sem_handle_type) = 0; virtual void DestroyExtSemaphore(void* extSemaphore) = 0; /** * @return True if the device has its own custom host allocator to be used diff --git a/rocclr/device/pal/paldevice.cpp b/rocclr/device/pal/paldevice.cpp index 647d6ca496..c6146a3940 100644 --- a/rocclr/device/pal/paldevice.cpp +++ b/rocclr/device/pal/paldevice.cpp @@ -2545,11 +2545,18 @@ bool Device::SetClockMode(const cl_set_device_clock_mode_input_amd setClockModeI } -bool Device::importExtSemaphore(void** extSemaphore, const amd::Os::FileDesc& handle) { +bool Device::importExtSemaphore(void** extSemaphore, const amd::Os::FileDesc& handle, + amd::ExternalSemaphoreHandleType sem_handle_type) { Pal::ExternalQueueSemaphoreOpenInfo palOpenInfo = {}; palOpenInfo.externalSemaphore = handle; palOpenInfo.flags.crossProcess = false; palOpenInfo.flags.isReference = true; + palOpenInfo.flags.timeline = + palOpenInfo.flags.timeline = + (sem_handle_type == amd::ExternalSemaphoreHandleType::TimelineSemaphoreWin32 || + sem_handle_type == amd::ExternalSemaphoreHandleType::TimelineSemaphoreFd); + palOpenInfo.flags.sharedViaNtHandle = + (sem_handle_type == amd::ExternalSemaphoreHandleType::OpaqueWin32); Pal::Result result; size_t semaphoreSize = iDev()->GetExternalSharedQueueSemaphoreSize( diff --git a/rocclr/device/pal/paldevice.hpp b/rocclr/device/pal/paldevice.hpp index 0226ea4005..2bbd1c99f1 100644 --- a/rocclr/device/pal/paldevice.hpp +++ b/rocclr/device/pal/paldevice.hpp @@ -147,7 +147,10 @@ class NullDevice : public amd::Device { virtual void* virtualAlloc(void* addr, size_t size, size_t alignment) { return nullptr; }; virtual void virtualFree(void* addr) { }; - virtual bool importExtSemaphore(void** extSemaphore,const amd::Os::FileDesc& handle) { return false; } + virtual bool importExtSemaphore(void** extSemaphore,const amd::Os::FileDesc& handle, + amd::ExternalSemaphoreHandleType sem_handle_type) override { + return false; + } virtual void DestroyExtSemaphore(void* extSemaphore) { } void* Alloc(const Util::AllocInfo& allocInfo) { return allocator_.Alloc(allocInfo); } @@ -619,7 +622,9 @@ class Device : public NullDevice { return false; } - virtual bool importExtSemaphore(void** extSemaphore, const amd::Os::FileDesc& handle); + virtual bool importExtSemaphore(void** extSemaphore, const amd::Os::FileDesc& handle, + amd::ExternalSemaphoreHandleType sem_handle_type) override; + virtual void DestroyExtSemaphore(void* extSemaphore); #if defined(__clang__) #if __has_feature(address_sanitizer) diff --git a/rocclr/device/rocm/rocdevice.hpp b/rocclr/device/rocm/rocdevice.hpp index 348c6c0687..10a02b9b3d 100644 --- a/rocclr/device/rocm/rocdevice.hpp +++ b/rocclr/device/rocm/rocdevice.hpp @@ -221,7 +221,8 @@ class NullDevice : public amd::Device { return !settings().enableCoarseGrainSVM_ || (memory->getContext().devices().size() > 1); } - virtual bool importExtSemaphore(void** extSemahore, const amd::Os::FileDesc& handle) { + virtual bool importExtSemaphore(void** extSemahore, const amd::Os::FileDesc& handle, + amd::ExternalSemaphoreHandleType sem_handle_type) override { ShouldNotReachHere(); return false; }