From 7a8bd1d080b603f820c40b81543c1d6807eaf453 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Wed, 18 Jun 2025 16:57:57 +0100 Subject: [PATCH 01/12] Add reference counting to handle_base struct to allow all UR handles to inherit common reference counting functionality. Update L0 and L0V2 to use this. --- unified-runtime/source/adapters/level_zero/adapter.cpp | 4 ++-- unified-runtime/source/adapters/level_zero/event.cpp | 2 +- unified-runtime/source/ur/ur.hpp | 8 ++++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/unified-runtime/source/adapters/level_zero/adapter.cpp b/unified-runtime/source/adapters/level_zero/adapter.cpp index 0c1abe7667bd3..4cd6128e7d137 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.cpp +++ b/unified-runtime/source/adapters/level_zero/adapter.cpp @@ -709,7 +709,7 @@ ur_result_t urAdapterRelease([[maybe_unused]] ur_adapter_handle_t Adapter) { return UR_RESULT_SUCCESS; } -ur_result_t urAdapterRetain([[maybe_unused]] ur_adapter_handle_t Adapter) { +ur_result_t urAdapterRetain(ur_adapter_handle_t) { assert(GlobalAdapter && GlobalAdapter == Adapter); GlobalAdapter->RefCount.retain(); @@ -745,7 +745,7 @@ ur_result_t urAdapterGetInfo(ur_adapter_handle_t, ur_adapter_info_t PropName, #ifdef UR_ADAPTER_LEVEL_ZERO_V2 uint32_t adapterVersion = 2; #else - uint32_t adapterVersion = 1; + uint32_t adapterVersion = 1; #endif return ReturnValue(adapterVersion); } diff --git a/unified-runtime/source/adapters/level_zero/event.cpp b/unified-runtime/source/adapters/level_zero/event.cpp index e1834376f0b41..5e9d62afd3685 100644 --- a/unified-runtime/source/adapters/level_zero/event.cpp +++ b/unified-runtime/source/adapters/level_zero/event.cpp @@ -1429,7 +1429,7 @@ ur_result_t ur_event_handle_t_::reset() { CommandType = UR_EXT_COMMAND_TYPE_USER; WaitList = {}; RefCountExternal = 0; - RefCount.reset(); + resetRefCount(); CommandList = std::nullopt; completionBatch = std::nullopt; OriginAllocEvent = nullptr; diff --git a/unified-runtime/source/ur/ur.hpp b/unified-runtime/source/ur/ur.hpp index 5b0914868777a..480fd16c769f4 100644 --- a/unified-runtime/source/ur/ur.hpp +++ b/unified-runtime/source/ur/ur.hpp @@ -228,6 +228,14 @@ template struct handle_base { // Handles are non-copyable. handle_base(const handle_base &) = delete; handle_base &operator=(const handle_base &) = delete; + + uint32_t getRefCount() const noexcept { return Count.load(); } + uint32_t incrementRefCount() { return ++Count; } + uint32_t decrementRefCount() { return --Count; } + void resetRefCount() { Count = 1; } + +private: + std::atomic_uint32_t Count{1}; }; template From 4083a59045ac37bb9d525d4135739f11ee782692 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Thu, 19 Jun 2025 10:19:10 +0100 Subject: [PATCH 02/12] Add decrementAndTest back in to fix logic in checks. --- unified-runtime/source/ur/ur.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/unified-runtime/source/ur/ur.hpp b/unified-runtime/source/ur/ur.hpp index 480fd16c769f4..7331ebc4742f5 100644 --- a/unified-runtime/source/ur/ur.hpp +++ b/unified-runtime/source/ur/ur.hpp @@ -232,6 +232,7 @@ template struct handle_base { uint32_t getRefCount() const noexcept { return Count.load(); } uint32_t incrementRefCount() { return ++Count; } uint32_t decrementRefCount() { return --Count; } + bool decrementAndTest() { return --Count == 0; } void resetRefCount() { Count = 1; } private: From ddbef0f42f5f1c6a6fbb866bccaecd48db03a2a4 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Fri, 20 Jun 2025 14:33:03 +0100 Subject: [PATCH 03/12] change resetRefCount to have a param and a default value so l0 adapter can start ref count from 0 --- unified-runtime/source/adapters/level_zero/adapter.cpp | 2 ++ unified-runtime/source/ur/ur.hpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/unified-runtime/source/adapters/level_zero/adapter.cpp b/unified-runtime/source/adapters/level_zero/adapter.cpp index 4cd6128e7d137..591b3ec17e2a6 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.cpp +++ b/unified-runtime/source/adapters/level_zero/adapter.cpp @@ -301,6 +301,8 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() ZeInitResult = ZE_RESULT_ERROR_UNINITIALIZED; ZesResult = ZE_RESULT_ERROR_UNINITIALIZED; + resetRefCount(0); + #ifdef UR_STATIC_LEVEL_ZERO // Given static linking of the L0 Loader, we must delay the loader's // destruction of its context until after the UR Adapter is destroyed. diff --git a/unified-runtime/source/ur/ur.hpp b/unified-runtime/source/ur/ur.hpp index 7331ebc4742f5..ec41529d9f575 100644 --- a/unified-runtime/source/ur/ur.hpp +++ b/unified-runtime/source/ur/ur.hpp @@ -233,7 +233,7 @@ template struct handle_base { uint32_t incrementRefCount() { return ++Count; } uint32_t decrementRefCount() { return --Count; } bool decrementAndTest() { return --Count == 0; } - void resetRefCount() { Count = 1; } + void resetRefCount(uint32_t value = 1) { Count = value; } private: std::atomic_uint32_t Count{1}; From 462bf9bfc690a0f5f6aed241228ff7ed9598584a Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Wed, 25 Jun 2025 12:17:06 +0100 Subject: [PATCH 04/12] Change implementation to a new URRefCount class and add it as member to any relevant handles that need ref counting. --- unified-runtime/source/adapters/level_zero/adapter.cpp | 6 +++--- unified-runtime/source/adapters/level_zero/event.cpp | 2 +- unified-runtime/source/adapters/level_zero/memory.hpp | 3 +++ unified-runtime/source/adapters/level_zero/program.hpp | 2 ++ unified-runtime/source/adapters/level_zero/usm.hpp | 2 ++ .../source/adapters/level_zero/v2/command_buffer.hpp | 2 ++ .../source/adapters/level_zero/v2/context.hpp | 2 ++ unified-runtime/source/adapters/level_zero/v2/event.hpp | 2 ++ unified-runtime/source/common/ur_ref_count.hpp | 6 +++--- unified-runtime/source/ur/ur.hpp | 9 --------- 10 files changed, 20 insertions(+), 16 deletions(-) diff --git a/unified-runtime/source/adapters/level_zero/adapter.cpp b/unified-runtime/source/adapters/level_zero/adapter.cpp index 591b3ec17e2a6..c78bdffca0ac1 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.cpp +++ b/unified-runtime/source/adapters/level_zero/adapter.cpp @@ -301,7 +301,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() ZeInitResult = ZE_RESULT_ERROR_UNINITIALIZED; ZesResult = ZE_RESULT_ERROR_UNINITIALIZED; - resetRefCount(0); + RefCount.reset(0); #ifdef UR_STATIC_LEVEL_ZERO // Given static linking of the L0 Loader, we must delay the loader's @@ -711,7 +711,7 @@ ur_result_t urAdapterRelease([[maybe_unused]] ur_adapter_handle_t Adapter) { return UR_RESULT_SUCCESS; } -ur_result_t urAdapterRetain(ur_adapter_handle_t) { +ur_result_t urAdapterRetain([[maybe_unused]] ur_adapter_handle_t Adapter) { assert(GlobalAdapter && GlobalAdapter == Adapter); GlobalAdapter->RefCount.retain(); @@ -747,7 +747,7 @@ ur_result_t urAdapterGetInfo(ur_adapter_handle_t, ur_adapter_info_t PropName, #ifdef UR_ADAPTER_LEVEL_ZERO_V2 uint32_t adapterVersion = 2; #else - uint32_t adapterVersion = 1; + uint32_t adapterVersion = 1; #endif return ReturnValue(adapterVersion); } diff --git a/unified-runtime/source/adapters/level_zero/event.cpp b/unified-runtime/source/adapters/level_zero/event.cpp index 5e9d62afd3685..e1834376f0b41 100644 --- a/unified-runtime/source/adapters/level_zero/event.cpp +++ b/unified-runtime/source/adapters/level_zero/event.cpp @@ -1429,7 +1429,7 @@ ur_result_t ur_event_handle_t_::reset() { CommandType = UR_EXT_COMMAND_TYPE_USER; WaitList = {}; RefCountExternal = 0; - resetRefCount(); + RefCount.reset(); CommandList = std::nullopt; completionBatch = std::nullopt; OriginAllocEvent = nullptr; diff --git a/unified-runtime/source/adapters/level_zero/memory.hpp b/unified-runtime/source/adapters/level_zero/memory.hpp index f58f189b21c77..2a1eb6f91561e 100644 --- a/unified-runtime/source/adapters/level_zero/memory.hpp +++ b/unified-runtime/source/adapters/level_zero/memory.hpp @@ -104,6 +104,9 @@ struct ur_mem_handle_t_ : ur_object { // Since the destructor isn't virtual, callers must destruct it via ur_buffer // or ur_image ~ur_mem_handle_t_() {}; + +private: + URRefCount RefCount; }; struct ur_buffer final : ur_mem_handle_t_ { diff --git a/unified-runtime/source/adapters/level_zero/program.hpp b/unified-runtime/source/adapters/level_zero/program.hpp index fb2cc1f12ee5e..1067b27fe6c1a 100644 --- a/unified-runtime/source/adapters/level_zero/program.hpp +++ b/unified-runtime/source/adapters/level_zero/program.hpp @@ -267,4 +267,6 @@ struct ur_program_handle_t_ : ur_object { // handle from the program. // TODO: Currently interoparability UR API does not support multiple devices. ze_module_handle_t InteropZeModule = nullptr; + + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/usm.hpp b/unified-runtime/source/adapters/level_zero/usm.hpp index f99a2f79c87b2..caaf93859fc10 100644 --- a/unified-runtime/source/adapters/level_zero/usm.hpp +++ b/unified-runtime/source/adapters/level_zero/usm.hpp @@ -59,6 +59,8 @@ struct ur_usm_pool_handle_t_ : ur_object { private: UsmPool *getPool(const usm::pool_descriptor &Desc); usm::pool_manager PoolManager; + + URRefCount RefCount; }; // Exception type to pass allocation errors diff --git a/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp b/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp index c7f1d585a5b77..36beadfed32f9 100644 --- a/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp @@ -90,4 +90,6 @@ struct ur_exp_command_buffer_handle_t_ : public ur_object { ur_event_handle_t currentExecution = nullptr; v2::raii::cache_borrowed_event_pool eventPool; + + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/v2/context.hpp b/unified-runtime/source/adapters/level_zero/v2/context.hpp index b1500092a727b..933f0958aff2d 100644 --- a/unified-runtime/source/adapters/level_zero/v2/context.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/context.hpp @@ -84,4 +84,6 @@ struct ur_context_handle_t_ : ur_object { ur_usm_pool_handle_t_ defaultUSMPool; ur_usm_pool_handle_t_ asyncPool; std::list usmPoolHandles; + + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/v2/event.hpp b/unified-runtime/source/adapters/level_zero/v2/event.hpp index 9a31c47358947..79e1ae974ca9e 100644 --- a/unified-runtime/source/adapters/level_zero/v2/event.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/event.hpp @@ -119,6 +119,8 @@ struct ur_event_handle_t_ : ur_object { ur_event_handle_t_(ur_context_handle_t hContext, event_variant hZeEvent, v2::event_flags_t flags, v2::event_pool *pool); + URRefCount RefCount; + protected: ur_context_handle_t hContext; diff --git a/unified-runtime/source/common/ur_ref_count.hpp b/unified-runtime/source/common/ur_ref_count.hpp index 7815e1dab65d0..add4fe0af2d53 100644 --- a/unified-runtime/source/common/ur_ref_count.hpp +++ b/unified-runtime/source/common/ur_ref_count.hpp @@ -8,8 +8,8 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * */ -#ifndef URREFCOUNT_HPP -#define URREFCOUNT_HPP 1 +#ifndef REFCOUNT_HPP +#define REFCOUNT_HPP 1 #include #include @@ -33,4 +33,4 @@ class RefCount { } // namespace ur -#endif // URREFCOUNT_HPP +#endif // REFCOUNT_HPP diff --git a/unified-runtime/source/ur/ur.hpp b/unified-runtime/source/ur/ur.hpp index ec41529d9f575..5b0914868777a 100644 --- a/unified-runtime/source/ur/ur.hpp +++ b/unified-runtime/source/ur/ur.hpp @@ -228,15 +228,6 @@ template struct handle_base { // Handles are non-copyable. handle_base(const handle_base &) = delete; handle_base &operator=(const handle_base &) = delete; - - uint32_t getRefCount() const noexcept { return Count.load(); } - uint32_t incrementRefCount() { return ++Count; } - uint32_t decrementRefCount() { return --Count; } - bool decrementAndTest() { return --Count == 0; } - void resetRefCount(uint32_t value = 1) { Count = value; } - -private: - std::atomic_uint32_t Count{1}; }; template From 2017e7ad6d6a44fcd0bd79f9da9dcffefa22223c Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Fri, 27 Jun 2025 12:46:54 +0100 Subject: [PATCH 05/12] Address feedback: rename URRefCount to RefCount and put inside ur namespace. Add a constructor for the class with a default value and remove use of reset() inside the Adapter constructor, using the RefCount constructor to set default value instead. --- unified-runtime/source/adapters/level_zero/adapter.cpp | 2 -- unified-runtime/source/adapters/level_zero/memory.hpp | 2 +- unified-runtime/source/adapters/level_zero/program.hpp | 2 +- unified-runtime/source/adapters/level_zero/usm.hpp | 2 +- .../source/adapters/level_zero/v2/command_buffer.hpp | 2 +- unified-runtime/source/adapters/level_zero/v2/context.hpp | 2 +- unified-runtime/source/adapters/level_zero/v2/event.hpp | 2 +- 7 files changed, 6 insertions(+), 8 deletions(-) diff --git a/unified-runtime/source/adapters/level_zero/adapter.cpp b/unified-runtime/source/adapters/level_zero/adapter.cpp index c78bdffca0ac1..0c1abe7667bd3 100644 --- a/unified-runtime/source/adapters/level_zero/adapter.cpp +++ b/unified-runtime/source/adapters/level_zero/adapter.cpp @@ -301,8 +301,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() ZeInitResult = ZE_RESULT_ERROR_UNINITIALIZED; ZesResult = ZE_RESULT_ERROR_UNINITIALIZED; - RefCount.reset(0); - #ifdef UR_STATIC_LEVEL_ZERO // Given static linking of the L0 Loader, we must delay the loader's // destruction of its context until after the UR Adapter is destroyed. diff --git a/unified-runtime/source/adapters/level_zero/memory.hpp b/unified-runtime/source/adapters/level_zero/memory.hpp index 2a1eb6f91561e..b96e9aa809f4a 100644 --- a/unified-runtime/source/adapters/level_zero/memory.hpp +++ b/unified-runtime/source/adapters/level_zero/memory.hpp @@ -106,7 +106,7 @@ struct ur_mem_handle_t_ : ur_object { ~ur_mem_handle_t_() {}; private: - URRefCount RefCount; + ur::RefCount RefCount; }; struct ur_buffer final : ur_mem_handle_t_ { diff --git a/unified-runtime/source/adapters/level_zero/program.hpp b/unified-runtime/source/adapters/level_zero/program.hpp index 1067b27fe6c1a..263ebc0624b58 100644 --- a/unified-runtime/source/adapters/level_zero/program.hpp +++ b/unified-runtime/source/adapters/level_zero/program.hpp @@ -268,5 +268,5 @@ struct ur_program_handle_t_ : ur_object { // TODO: Currently interoparability UR API does not support multiple devices. ze_module_handle_t InteropZeModule = nullptr; - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/usm.hpp b/unified-runtime/source/adapters/level_zero/usm.hpp index caaf93859fc10..76c891a4015f4 100644 --- a/unified-runtime/source/adapters/level_zero/usm.hpp +++ b/unified-runtime/source/adapters/level_zero/usm.hpp @@ -60,7 +60,7 @@ struct ur_usm_pool_handle_t_ : ur_object { UsmPool *getPool(const usm::pool_descriptor &Desc); usm::pool_manager PoolManager; - URRefCount RefCount; + ur::RefCount RefCount; }; // Exception type to pass allocation errors diff --git a/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp b/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp index 36beadfed32f9..8b86f1f70ed00 100644 --- a/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp @@ -91,5 +91,5 @@ struct ur_exp_command_buffer_handle_t_ : public ur_object { v2::raii::cache_borrowed_event_pool eventPool; - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/v2/context.hpp b/unified-runtime/source/adapters/level_zero/v2/context.hpp index 933f0958aff2d..969a3930ee134 100644 --- a/unified-runtime/source/adapters/level_zero/v2/context.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/context.hpp @@ -85,5 +85,5 @@ struct ur_context_handle_t_ : ur_object { ur_usm_pool_handle_t_ asyncPool; std::list usmPoolHandles; - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/v2/event.hpp b/unified-runtime/source/adapters/level_zero/v2/event.hpp index 79e1ae974ca9e..82e4afa19b3f0 100644 --- a/unified-runtime/source/adapters/level_zero/v2/event.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/event.hpp @@ -119,7 +119,7 @@ struct ur_event_handle_t_ : ur_object { ur_event_handle_t_(ur_context_handle_t hContext, event_variant hZeEvent, v2::event_flags_t flags, v2::event_pool *pool); - URRefCount RefCount; + ur::RefCount RefCount; protected: ur_context_handle_t hContext; From 80bd3e48c2dbf9b696b02e3f61e2b5fc8cfb5a84 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Fri, 27 Jun 2025 15:18:06 +0100 Subject: [PATCH 06/12] Address pr feedback: rename ref count functions increment -> retain and decrementAndTest -> release, and remove decrement as it doesn't get used. --- .../adapters/level_zero/v2/queue_immediate_out_of_order.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp b/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp index 07e8743154ded..28d2cf49b57ed 100644 --- a/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp @@ -50,6 +50,8 @@ struct ur_queue_immediate_out_of_order_t : ur_object, ur_queue_t_ { numCommandLists; } + ur::RefCount RefCount; + public: ur_queue_immediate_out_of_order_t(ur_context_handle_t, ur_device_handle_t, uint32_t ordinal, From 53e5235e7418cda38e5a74ead707cc9f0a011020 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Wed, 2 Jul 2025 09:37:17 +0100 Subject: [PATCH 07/12] Address pr feedback - make RefCount public and remove getRefCount(). --- unified-runtime/source/adapters/level_zero/memory.hpp | 3 --- unified-runtime/source/adapters/level_zero/program.hpp | 2 -- unified-runtime/source/adapters/level_zero/usm.hpp | 2 -- .../source/adapters/level_zero/v2/command_buffer.hpp | 2 -- unified-runtime/source/adapters/level_zero/v2/context.hpp | 2 -- unified-runtime/source/adapters/level_zero/v2/event.hpp | 2 -- .../adapters/level_zero/v2/queue_immediate_out_of_order.hpp | 2 -- 7 files changed, 15 deletions(-) diff --git a/unified-runtime/source/adapters/level_zero/memory.hpp b/unified-runtime/source/adapters/level_zero/memory.hpp index b96e9aa809f4a..f58f189b21c77 100644 --- a/unified-runtime/source/adapters/level_zero/memory.hpp +++ b/unified-runtime/source/adapters/level_zero/memory.hpp @@ -104,9 +104,6 @@ struct ur_mem_handle_t_ : ur_object { // Since the destructor isn't virtual, callers must destruct it via ur_buffer // or ur_image ~ur_mem_handle_t_() {}; - -private: - ur::RefCount RefCount; }; struct ur_buffer final : ur_mem_handle_t_ { diff --git a/unified-runtime/source/adapters/level_zero/program.hpp b/unified-runtime/source/adapters/level_zero/program.hpp index 263ebc0624b58..fb2cc1f12ee5e 100644 --- a/unified-runtime/source/adapters/level_zero/program.hpp +++ b/unified-runtime/source/adapters/level_zero/program.hpp @@ -267,6 +267,4 @@ struct ur_program_handle_t_ : ur_object { // handle from the program. // TODO: Currently interoparability UR API does not support multiple devices. ze_module_handle_t InteropZeModule = nullptr; - - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/usm.hpp b/unified-runtime/source/adapters/level_zero/usm.hpp index 76c891a4015f4..f99a2f79c87b2 100644 --- a/unified-runtime/source/adapters/level_zero/usm.hpp +++ b/unified-runtime/source/adapters/level_zero/usm.hpp @@ -59,8 +59,6 @@ struct ur_usm_pool_handle_t_ : ur_object { private: UsmPool *getPool(const usm::pool_descriptor &Desc); usm::pool_manager PoolManager; - - ur::RefCount RefCount; }; // Exception type to pass allocation errors diff --git a/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp b/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp index 8b86f1f70ed00..c7f1d585a5b77 100644 --- a/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/command_buffer.hpp @@ -90,6 +90,4 @@ struct ur_exp_command_buffer_handle_t_ : public ur_object { ur_event_handle_t currentExecution = nullptr; v2::raii::cache_borrowed_event_pool eventPool; - - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/v2/context.hpp b/unified-runtime/source/adapters/level_zero/v2/context.hpp index 969a3930ee134..b1500092a727b 100644 --- a/unified-runtime/source/adapters/level_zero/v2/context.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/context.hpp @@ -84,6 +84,4 @@ struct ur_context_handle_t_ : ur_object { ur_usm_pool_handle_t_ defaultUSMPool; ur_usm_pool_handle_t_ asyncPool; std::list usmPoolHandles; - - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/level_zero/v2/event.hpp b/unified-runtime/source/adapters/level_zero/v2/event.hpp index 82e4afa19b3f0..9a31c47358947 100644 --- a/unified-runtime/source/adapters/level_zero/v2/event.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/event.hpp @@ -119,8 +119,6 @@ struct ur_event_handle_t_ : ur_object { ur_event_handle_t_(ur_context_handle_t hContext, event_variant hZeEvent, v2::event_flags_t flags, v2::event_pool *pool); - ur::RefCount RefCount; - protected: ur_context_handle_t hContext; diff --git a/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp b/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp index 28d2cf49b57ed..07e8743154ded 100644 --- a/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp +++ b/unified-runtime/source/adapters/level_zero/v2/queue_immediate_out_of_order.hpp @@ -50,8 +50,6 @@ struct ur_queue_immediate_out_of_order_t : ur_object, ur_queue_t_ { numCommandLists; } - ur::RefCount RefCount; - public: ur_queue_immediate_out_of_order_t(ur_context_handle_t, ur_device_handle_t, uint32_t ordinal, From fe582cf138faff1f0d0c89f02f82896d41478863 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Thu, 26 Jun 2025 16:33:03 +0100 Subject: [PATCH 08/12] Replace UR OpenCL reference counting with new URRefCount class. --- unified-runtime/source/adapters/opencl/adapter.cpp | 8 ++++---- unified-runtime/source/adapters/opencl/adapter.hpp | 7 ++++++- .../source/adapters/opencl/command_buffer.cpp | 6 +++--- .../source/adapters/opencl/command_buffer.hpp | 12 ++++++------ unified-runtime/source/adapters/opencl/context.cpp | 6 +++--- unified-runtime/source/adapters/opencl/context.hpp | 14 ++++++-------- unified-runtime/source/adapters/opencl/device.cpp | 6 +++--- unified-runtime/source/adapters/opencl/device.hpp | 14 ++++++-------- unified-runtime/source/adapters/opencl/event.cpp | 6 +++--- unified-runtime/source/adapters/opencl/event.hpp | 14 ++++++-------- unified-runtime/source/adapters/opencl/kernel.cpp | 6 +++--- unified-runtime/source/adapters/opencl/kernel.hpp | 14 ++++++-------- unified-runtime/source/adapters/opencl/memory.cpp | 6 +++--- unified-runtime/source/adapters/opencl/memory.hpp | 14 ++++++-------- unified-runtime/source/adapters/opencl/program.cpp | 6 +++--- unified-runtime/source/adapters/opencl/program.hpp | 14 ++++++-------- unified-runtime/source/adapters/opencl/queue.cpp | 6 +++--- unified-runtime/source/adapters/opencl/queue.hpp | 14 ++++++-------- unified-runtime/source/adapters/opencl/sampler.cpp | 6 +++--- unified-runtime/source/adapters/opencl/sampler.hpp | 10 ++++------ 20 files changed, 89 insertions(+), 100 deletions(-) diff --git a/unified-runtime/source/adapters/opencl/adapter.cpp b/unified-runtime/source/adapters/opencl/adapter.cpp index 797e1c8582ef8..d2e773a58232f 100644 --- a/unified-runtime/source/adapters/opencl/adapter.cpp +++ b/unified-runtime/source/adapters/opencl/adapter.cpp @@ -78,7 +78,7 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, } auto &adapter = *phAdapters; - adapter->RefCount++; + adapter->getRefCount().increment(); } if (pNumAdapters) { @@ -90,13 +90,13 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t hAdapter) { - ++hAdapter->RefCount; + hAdapter->getRefCount().increment(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t hAdapter) { - if (--hAdapter->RefCount == 0) { + if (hAdapter->getRefCount().decrementAndTest()) { delete hAdapter; } return UR_RESULT_SUCCESS; @@ -119,7 +119,7 @@ urAdapterGetInfo(ur_adapter_handle_t hAdapter, ur_adapter_info_t propName, case UR_ADAPTER_INFO_BACKEND: return ReturnValue(UR_BACKEND_OPENCL); case UR_ADAPTER_INFO_REFERENCE_COUNT: - return ReturnValue(hAdapter->RefCount.load()); + return ReturnValue(hAdapter->getRefCount().getCount()); case UR_ADAPTER_INFO_VERSION: return ReturnValue(uint32_t{1}); default: diff --git a/unified-runtime/source/adapters/opencl/adapter.hpp b/unified-runtime/source/adapters/opencl/adapter.hpp index 1a83963343c9c..dd010a7457d8f 100644 --- a/unified-runtime/source/adapters/opencl/adapter.hpp +++ b/unified-runtime/source/adapters/opencl/adapter.hpp @@ -15,6 +15,7 @@ #include "CL/cl.h" #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "logger/ur_logger.hpp" struct ur_adapter_handle_t_ : ur::opencl::handle_base { @@ -24,7 +25,6 @@ struct ur_adapter_handle_t_ : ur::opencl::handle_base { ur_adapter_handle_t_(ur_adapter_handle_t_ &) = delete; ur_adapter_handle_t_ &operator=(const ur_adapter_handle_t_ &) = delete; - std::atomic RefCount = 0; logger::Logger &log = logger::get_logger("opencl"); cl_ext::ExtFuncPtrCacheT fnCache{}; @@ -37,6 +37,11 @@ struct ur_adapter_handle_t_ : ur::opencl::handle_base { #define CL_CORE_FUNCTION(FUNC) decltype(::FUNC) *FUNC = nullptr; #include "core_functions.def" #undef CL_CORE_FUNCTION + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; namespace ur { diff --git a/unified-runtime/source/adapters/opencl/command_buffer.cpp b/unified-runtime/source/adapters/opencl/command_buffer.cpp index e048b2d22175c..c5539ff404533 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.cpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.cpp @@ -108,13 +108,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp( UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - hCommandBuffer->incrementReferenceCount(); + hCommandBuffer->getRefCount().increment(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - if (hCommandBuffer->decrementReferenceCount() == 0) { + if (hCommandBuffer->getRefCount().decrementAndTest()) { delete hCommandBuffer; } @@ -783,7 +783,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp( switch (propName) { case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT: - return ReturnValue(hCommandBuffer->getReferenceCount()); + return ReturnValue(hCommandBuffer->getRefCount().getCount()); case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: { ur_exp_command_buffer_desc_t Descriptor{}; Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC; diff --git a/unified-runtime/source/adapters/opencl/command_buffer.hpp b/unified-runtime/source/adapters/opencl/command_buffer.hpp index e14989d0ff34f..4c4c554f98e6b 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.hpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.hpp @@ -9,6 +9,7 @@ //===----------------------------------------------------------------------===// #include "common.hpp" +#include "common/ur_ref_count.hpp" #include /// Handle to a kernel command. @@ -53,8 +54,6 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base { /// List of commands in the command-buffer. std::vector> CommandHandles; - /// Object reference count - std::atomic_uint32_t RefCount; /// Track last submission of the command-buffer cl_event LastSubmission; @@ -66,11 +65,12 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base { : handle_base(), hInternalQueue(hQueue), hContext(hContext), hDevice(hDevice), CLCommandBuffer(CLCommandBuffer), IsUpdatable(IsUpdatable), IsInOrder(IsInOrder), IsFinalized(false), - RefCount(0), LastSubmission(nullptr) {} + LastSubmission(nullptr) {} ~ur_exp_command_buffer_handle_t_(); - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - uint32_t getReferenceCount() const noexcept { return RefCount; } + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/context.cpp b/unified-runtime/source/adapters/opencl/context.cpp index 9409dd960838a..11a7631ac2329 100644 --- a/unified-runtime/source/adapters/opencl/context.cpp +++ b/unified-runtime/source/adapters/opencl/context.cpp @@ -108,7 +108,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName, return ReturnValue(&hContext->Devices[0], hContext->DeviceCount); } case UR_CONTEXT_INFO_REFERENCE_COUNT: { - return ReturnValue(hContext->getReferenceCount()); + return ReturnValue(hContext->getRefCount().getCount()); } default: return UR_RESULT_ERROR_INVALID_ENUMERATION; @@ -117,7 +117,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urContextRelease(ur_context_handle_t hContext) { - if (hContext->decrementReferenceCount() == 0) { + if (hContext->getRefCount().decrementAndTest()) { delete hContext; } @@ -126,7 +126,7 @@ urContextRelease(ur_context_handle_t hContext) { UR_APIEXPORT ur_result_t UR_APICALL urContextRetain(ur_context_handle_t hContext) { - hContext->incrementReferenceCount(); + hContext->getRefCount().increment(); return UR_RESULT_SUCCESS; } diff --git a/unified-runtime/source/adapters/opencl/context.hpp b/unified-runtime/source/adapters/opencl/context.hpp index b3a136ca2de8b..448c76ec6b4db 100644 --- a/unified-runtime/source/adapters/opencl/context.hpp +++ b/unified-runtime/source/adapters/opencl/context.hpp @@ -11,6 +11,7 @@ #include "adapter.hpp" #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "device.hpp" #include @@ -20,7 +21,6 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { native_type CLContext; std::vector Devices; uint32_t DeviceCount; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; ur_context_handle_t_(native_type Ctx, uint32_t DevCount, @@ -30,15 +30,8 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { Devices.emplace_back(phDevices[i]); urDeviceRetain(phDevices[i]); } - RefCount = 1; } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount, const ur_device_handle_t *phDevices, ur_context_handle_t &Context); @@ -56,4 +49,9 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { clReleaseContext(CLContext); } } + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/device.cpp b/unified-runtime/source/adapters/opencl/device.cpp index 4e0de3da8e710..c323e7fcd6ab2 100644 --- a/unified-runtime/source/adapters/opencl/device.cpp +++ b/unified-runtime/source/adapters/opencl/device.cpp @@ -1019,7 +1019,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, return UR_RESULT_SUCCESS; } case UR_DEVICE_INFO_REFERENCE_COUNT: { - return ReturnValue(hDevice->getReferenceCount()); + return ReturnValue(hDevice->getRefCount().getCount()); } case UR_DEVICE_INFO_PARTITION_MAX_SUB_DEVICES: { CL_RETURN_ON_FAILURE(clGetDeviceInfo(hDevice->CLDevice, @@ -1561,7 +1561,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( // Root devices ref count are unchanged through out the program lifetime. UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - hDevice->incrementReferenceCount(); + hDevice->getRefCount().increment(); } return UR_RESULT_SUCCESS; @@ -1571,7 +1571,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { UR_APIEXPORT ur_result_t UR_APICALL urDeviceRelease(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - if (hDevice->decrementReferenceCount() == 0) { + if (hDevice->getRefCount().decrementAndTest()) { delete hDevice; } } diff --git a/unified-runtime/source/adapters/opencl/device.hpp b/unified-runtime/source/adapters/opencl/device.hpp index 1c100535c6643..1dbceec5980b9 100644 --- a/unified-runtime/source/adapters/opencl/device.hpp +++ b/unified-runtime/source/adapters/opencl/device.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "device.hpp" #include "platform.hpp" @@ -19,13 +20,11 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { ur_platform_handle_t Platform; cl_device_type Type = 0; ur_device_handle_t ParentDevice = nullptr; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat, ur_device_handle_t Parent) : handle_base(), CLDevice(Dev), Platform(Plat), ParentDevice(Parent) { - RefCount = 1; if (Parent) { Type = Parent->Type; [[maybe_unused]] auto Res = clRetainDevice(CLDevice); @@ -51,12 +50,6 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - ur_result_t getDeviceVersion(oclv::OpenCLVersion &Version) { size_t DevVerSize = 0; CL_RETURN_ON_FAILURE( @@ -114,4 +107,9 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/event.cpp b/unified-runtime/source/adapters/opencl/event.cpp index bb13f297b60bf..ad98aa6202c5e 100644 --- a/unified-runtime/source/adapters/opencl/event.cpp +++ b/unified-runtime/source/adapters/opencl/event.cpp @@ -136,14 +136,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) { - if (hEvent->decrementReferenceCount() == 0) { + if (hEvent->getRefCount().decrementAndTest()) { delete hEvent; } return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) { - hEvent->incrementReferenceCount(); + hEvent->getRefCount().increment(); return UR_RESULT_SUCCESS; } @@ -188,7 +188,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent, return ReturnValue(hEvent->Queue); } case UR_EVENT_INFO_REFERENCE_COUNT: { - return ReturnValue(hEvent->getReferenceCount()); + return ReturnValue(hEvent->getRefCount().getCount()); } default: { size_t CheckPropSize = 0; diff --git a/unified-runtime/source/adapters/opencl/event.hpp b/unified-runtime/source/adapters/opencl/event.hpp index dacddf3d8e1f6..58834bfcf868d 100644 --- a/unified-runtime/source/adapters/opencl/event.hpp +++ b/unified-runtime/source/adapters/opencl/event.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "queue.hpp" #include @@ -19,13 +20,11 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { native_type CLEvent; ur_context_handle_t Context; ur_queue_handle_t Queue; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx, ur_queue_handle_t Queue) : handle_base(), CLEvent(Event), Context(Ctx), Queue(Queue) { - RefCount = 1; urContextRetain(Context); if (Queue) { urQueueRetain(Queue); @@ -42,12 +41,6 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - ur_result_t ensureQueue() { if (!Queue) { cl_command_queue native_queue; @@ -60,6 +53,11 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; inline cl_event *ifUrEvent(ur_event_handle_t *ReturnedEvent, cl_event &Event) { diff --git a/unified-runtime/source/adapters/opencl/kernel.cpp b/unified-runtime/source/adapters/opencl/kernel.cpp index f0c22a99749a9..f80440bd51a81 100644 --- a/unified-runtime/source/adapters/opencl/kernel.cpp +++ b/unified-runtime/source/adapters/opencl/kernel.cpp @@ -152,7 +152,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelGetInfo(ur_kernel_handle_t hKernel, return ReturnValue(hKernel->Context); } case UR_KERNEL_INFO_REFERENCE_COUNT: { - return ReturnValue(hKernel->getReferenceCount()); + return ReturnValue(hKernel->getRefCount().getCount()); } default: { size_t CheckPropSize = 0; @@ -343,13 +343,13 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice, } UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) { - hKernel->incrementReferenceCount(); + hKernel->getRefCount().increment(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urKernelRelease(ur_kernel_handle_t hKernel) { - if (hKernel->decrementReferenceCount() == 0) { + if (hKernel->getRefCount().decrementAndTest()) { delete hKernel; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/kernel.hpp b/unified-runtime/source/adapters/opencl/kernel.hpp index ef73e47c1e319..f17532f62128b 100644 --- a/unified-runtime/source/adapters/opencl/kernel.hpp +++ b/unified-runtime/source/adapters/opencl/kernel.hpp @@ -11,6 +11,7 @@ #include "adapter.hpp" #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include "program.hpp" @@ -21,14 +22,12 @@ struct ur_kernel_handle_t_ : ur::opencl::handle_base { native_type CLKernel; ur_program_handle_t Program; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; clSetKernelArgMemPointerINTEL_fn clSetKernelArgMemPointerINTEL = nullptr; ur_kernel_handle_t_(native_type Kernel, ur_program_handle_t Program, ur_context_handle_t Context) : handle_base(), CLKernel(Kernel), Program(Program), Context(Context) { - RefCount = 1; urProgramRetain(Program); urContextRetain(Context); @@ -46,14 +45,13 @@ struct ur_kernel_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type NativeKernel, ur_program_handle_t Program, ur_context_handle_t Context, ur_kernel_handle_t &Kernel); + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/memory.cpp b/unified-runtime/source/adapters/opencl/memory.cpp index 62698e6105520..d2e05459399a1 100644 --- a/unified-runtime/source/adapters/opencl/memory.cpp +++ b/unified-runtime/source/adapters/opencl/memory.cpp @@ -521,7 +521,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemGetInfo(ur_mem_handle_t hMemory, return ReturnValue(hMemory->Context); } case UR_MEM_INFO_REFERENCE_COUNT: { - return ReturnValue(hMemory->getReferenceCount()); + return ReturnValue(hMemory->getRefCount().getCount()); } default: { size_t CheckPropSize = 0; @@ -569,12 +569,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, } UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { - hMem->incrementReferenceCount(); + hMem->getRefCount().increment(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) { - if (hMem->decrementReferenceCount() == 0) { + if (hMem->getRefCount().decrementAndTest()) { delete hMem; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/memory.hpp b/unified-runtime/source/adapters/opencl/memory.hpp index a0f8410e3df03..a70e0d3072c56 100644 --- a/unified-runtime/source/adapters/opencl/memory.hpp +++ b/unified-runtime/source/adapters/opencl/memory.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include @@ -18,12 +19,10 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { using native_type = cl_mem; native_type CLMemory; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; ur_mem_handle_t_(native_type Mem, ur_context_handle_t Ctx) : handle_base(), CLMemory(Mem), Context(Ctx) { - RefCount = 1; urContextRetain(Context); } @@ -34,13 +33,12 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type NativeMem, ur_context_handle_t Ctx, ur_mem_handle_t &Mem); + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/program.cpp b/unified-runtime/source/adapters/opencl/program.cpp index 1c3a5e45b3bd5..5da4435b75bfa 100644 --- a/unified-runtime/source/adapters/opencl/program.cpp +++ b/unified-runtime/source/adapters/opencl/program.cpp @@ -223,7 +223,7 @@ urProgramGetInfo(ur_program_handle_t hProgram, ur_program_info_t propName, return ReturnValue(hProgram->Devices.data(), hProgram->NumDevices); } case UR_PROGRAM_INFO_REFERENCE_COUNT: { - return ReturnValue(hProgram->getReferenceCount()); + return ReturnValue(hProgram->getRefCount().getCount()); } default: { size_t CheckPropSize = 0; @@ -383,13 +383,13 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UR_APIEXPORT ur_result_t UR_APICALL urProgramRetain(ur_program_handle_t hProgram) { - hProgram->incrementReferenceCount(); + hProgram->getRefCount().increment(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urProgramRelease(ur_program_handle_t hProgram) { - if (hProgram->decrementReferenceCount() == 0) { + if (hProgram->getRefCount().decrementAndTest()) { delete hProgram; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/program.hpp b/unified-runtime/source/adapters/opencl/program.hpp index 69b3430d2bc3a..7ddfc49683793 100644 --- a/unified-runtime/source/adapters/opencl/program.hpp +++ b/unified-runtime/source/adapters/opencl/program.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include @@ -18,7 +19,6 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { using native_type = cl_program; native_type CLProgram; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; uint32_t NumDevices = 0; std::vector Devices; @@ -26,7 +26,6 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx, uint32_t NumDevices, ur_device_handle_t *Devs) : handle_base(), CLProgram(Prog), Context(Ctx), NumDevices(NumDevices) { - RefCount = 1; urContextRetain(Context); for (uint32_t i = 0; i < NumDevices; i++) { Devices.push_back(Devs[i]); @@ -40,13 +39,12 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type NativeProg, ur_context_handle_t Context, ur_program_handle_t &Program); + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/queue.cpp b/unified-runtime/source/adapters/opencl/queue.cpp index 040186e701647..21b65edfb6f9f 100644 --- a/unified-runtime/source/adapters/opencl/queue.cpp +++ b/unified-runtime/source/adapters/opencl/queue.cpp @@ -234,7 +234,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueGetInfo(ur_queue_handle_t hQueue, return ReturnValue(mapCLQueuePropsToUR(QueueProperties)); } case UR_QUEUE_INFO_REFERENCE_COUNT: { - return ReturnValue(hQueue->getReferenceCount()); + return ReturnValue(hQueue->getRefCount().getCount()); } default: { size_t CheckPropSize = 0; @@ -289,12 +289,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush(ur_queue_handle_t hQueue) { } UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { - hQueue->incrementReferenceCount(); + hQueue->getRefCount().increment(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urQueueRelease(ur_queue_handle_t hQueue) { - if (hQueue->decrementReferenceCount() == 0) { + if (hQueue->getRefCount().decrementAndTest()) { delete hQueue; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/queue.hpp b/unified-runtime/source/adapters/opencl/queue.hpp index 5fd429d7b0518..841ef0e1ac2ec 100644 --- a/unified-runtime/source/adapters/opencl/queue.hpp +++ b/unified-runtime/source/adapters/opencl/queue.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include "device.hpp" @@ -22,7 +23,6 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { ur_device_handle_t Device; // Used to keep a handle to the default queue alive if it is different std::optional DeviceDefault = std::nullopt; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; // Used to implement UR_QUEUE_INFO_EMPTY query bool IsInOrder; @@ -32,7 +32,6 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { ur_device_handle_t Dev, bool InOrder) : handle_base(), CLQueue(Queue), Context(Ctx), Device(Dev), IsInOrder(InOrder) { - RefCount = 1; urDeviceRetain(Device); urContextRetain(Context); } @@ -53,12 +52,6 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - // Stores last event for in-order queues. Has no effect if queue is Out Of // Order. The last event is used to implement UR_QUEUE_INFO_EMPTY query. ur_result_t storeLastEvent(ur_event_handle_t Event) { @@ -74,4 +67,9 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { } return UR_RESULT_SUCCESS; } + + URRefCount &getRefCount() noexcept { return RefCount; } + +private: + URRefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/sampler.cpp b/unified-runtime/source/adapters/opencl/sampler.cpp index 69f3f5167a986..a617c44ec26c2 100644 --- a/unified-runtime/source/adapters/opencl/sampler.cpp +++ b/unified-runtime/source/adapters/opencl/sampler.cpp @@ -175,7 +175,7 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, return ReturnValue(hSampler->Context); } case UR_SAMPLER_INFO_REFERENCE_COUNT: { - return ReturnValue(hSampler->getReferenceCount()); + return ReturnValue(hSampler->getRefCount().getCount()); } // ur_bool_t have a size of uint8_t, but cl_bool size have the size of // uint32_t so this adjust UR_SAMPLER_INFO_NORMALIZED_COORDS info to map @@ -221,13 +221,13 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urSamplerRetain(ur_sampler_handle_t hSampler) { - hSampler->incrementReferenceCount(); + hSampler->getRefCount().increment(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urSamplerRelease(ur_sampler_handle_t hSampler) { - if (hSampler->decrementReferenceCount() == 0) { + if (hSampler->getRefCount().decrementAndTest()) { delete hSampler; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/sampler.hpp b/unified-runtime/source/adapters/opencl/sampler.hpp index b661fe195bcb2..6d220f8a6c072 100644 --- a/unified-runtime/source/adapters/opencl/sampler.hpp +++ b/unified-runtime/source/adapters/opencl/sampler.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include @@ -17,12 +18,10 @@ struct ur_sampler_handle_t_ : ur::opencl::handle_base { using native_type = cl_sampler; native_type CLSampler; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = false; ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx) : handle_base(), CLSampler(Sampler), Context(Ctx) { - RefCount = 1; urContextRetain(Context); } @@ -33,9 +32,8 @@ struct ur_sampler_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } + URRefCount &getRefCount() noexcept { return RefCount; } - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } +private: + URRefCount RefCount; }; From 17707339cc8fefc3277bd7a5a5ac714879e40dbc Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Fri, 27 Jun 2025 16:02:52 +0100 Subject: [PATCH 09/12] apply changes from #19057 --- unified-runtime/source/adapters/opencl/adapter.cpp | 6 +++--- unified-runtime/source/adapters/opencl/adapter.hpp | 4 ++-- unified-runtime/source/adapters/opencl/command_buffer.cpp | 4 ++-- unified-runtime/source/adapters/opencl/command_buffer.hpp | 4 ++-- unified-runtime/source/adapters/opencl/context.cpp | 4 ++-- unified-runtime/source/adapters/opencl/context.hpp | 4 ++-- unified-runtime/source/adapters/opencl/device.cpp | 4 ++-- unified-runtime/source/adapters/opencl/device.hpp | 4 ++-- unified-runtime/source/adapters/opencl/event.cpp | 4 ++-- unified-runtime/source/adapters/opencl/event.hpp | 4 ++-- unified-runtime/source/adapters/opencl/kernel.cpp | 4 ++-- unified-runtime/source/adapters/opencl/kernel.hpp | 4 ++-- unified-runtime/source/adapters/opencl/memory.cpp | 4 ++-- unified-runtime/source/adapters/opencl/memory.hpp | 4 ++-- unified-runtime/source/adapters/opencl/program.cpp | 4 ++-- unified-runtime/source/adapters/opencl/program.hpp | 4 ++-- unified-runtime/source/adapters/opencl/queue.cpp | 4 ++-- unified-runtime/source/adapters/opencl/queue.hpp | 4 ++-- unified-runtime/source/adapters/opencl/sampler.cpp | 4 ++-- unified-runtime/source/adapters/opencl/sampler.hpp | 4 ++-- 20 files changed, 41 insertions(+), 41 deletions(-) diff --git a/unified-runtime/source/adapters/opencl/adapter.cpp b/unified-runtime/source/adapters/opencl/adapter.cpp index d2e773a58232f..79e4c7cbb98ea 100644 --- a/unified-runtime/source/adapters/opencl/adapter.cpp +++ b/unified-runtime/source/adapters/opencl/adapter.cpp @@ -78,7 +78,7 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, } auto &adapter = *phAdapters; - adapter->getRefCount().increment(); + adapter->getRefCount().retain(); } if (pNumAdapters) { @@ -90,13 +90,13 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t hAdapter) { - hAdapter->getRefCount().increment(); + hAdapter->getRefCount().retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t hAdapter) { - if (hAdapter->getRefCount().decrementAndTest()) { + if (hAdapter->getRefCount().release()) { delete hAdapter; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/adapter.hpp b/unified-runtime/source/adapters/opencl/adapter.hpp index dd010a7457d8f..fe41b9f0ef757 100644 --- a/unified-runtime/source/adapters/opencl/adapter.hpp +++ b/unified-runtime/source/adapters/opencl/adapter.hpp @@ -38,10 +38,10 @@ struct ur_adapter_handle_t_ : ur::opencl::handle_base { #include "core_functions.def" #undef CL_CORE_FUNCTION - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; namespace ur { diff --git a/unified-runtime/source/adapters/opencl/command_buffer.cpp b/unified-runtime/source/adapters/opencl/command_buffer.cpp index c5539ff404533..35093329a88e3 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.cpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.cpp @@ -108,13 +108,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp( UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - hCommandBuffer->getRefCount().increment(); + hCommandBuffer->getRefCount().retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - if (hCommandBuffer->getRefCount().decrementAndTest()) { + if (hCommandBuffer->getRefCount().release()) { delete hCommandBuffer; } diff --git a/unified-runtime/source/adapters/opencl/command_buffer.hpp b/unified-runtime/source/adapters/opencl/command_buffer.hpp index 4c4c554f98e6b..018a4260e13db 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.hpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.hpp @@ -69,8 +69,8 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base { ~ur_exp_command_buffer_handle_t_(); - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/context.cpp b/unified-runtime/source/adapters/opencl/context.cpp index 11a7631ac2329..d8a87476e9749 100644 --- a/unified-runtime/source/adapters/opencl/context.cpp +++ b/unified-runtime/source/adapters/opencl/context.cpp @@ -117,7 +117,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urContextRelease(ur_context_handle_t hContext) { - if (hContext->getRefCount().decrementAndTest()) { + if (hContext->getRefCount().release()) { delete hContext; } @@ -126,7 +126,7 @@ urContextRelease(ur_context_handle_t hContext) { UR_APIEXPORT ur_result_t UR_APICALL urContextRetain(ur_context_handle_t hContext) { - hContext->getRefCount().increment(); + hContext->getRefCount().retain(); return UR_RESULT_SUCCESS; } diff --git a/unified-runtime/source/adapters/opencl/context.hpp b/unified-runtime/source/adapters/opencl/context.hpp index 448c76ec6b4db..64c0dcc285ddb 100644 --- a/unified-runtime/source/adapters/opencl/context.hpp +++ b/unified-runtime/source/adapters/opencl/context.hpp @@ -50,8 +50,8 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { } } - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/device.cpp b/unified-runtime/source/adapters/opencl/device.cpp index c323e7fcd6ab2..f3d6b57c7871a 100644 --- a/unified-runtime/source/adapters/opencl/device.cpp +++ b/unified-runtime/source/adapters/opencl/device.cpp @@ -1561,7 +1561,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( // Root devices ref count are unchanged through out the program lifetime. UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - hDevice->getRefCount().increment(); + hDevice->getRefCount().retain(); } return UR_RESULT_SUCCESS; @@ -1571,7 +1571,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { UR_APIEXPORT ur_result_t UR_APICALL urDeviceRelease(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - if (hDevice->getRefCount().decrementAndTest()) { + if (hDevice->getRefCount().release()) { delete hDevice; } } diff --git a/unified-runtime/source/adapters/opencl/device.hpp b/unified-runtime/source/adapters/opencl/device.hpp index 1dbceec5980b9..b4b6e2f99a73b 100644 --- a/unified-runtime/source/adapters/opencl/device.hpp +++ b/unified-runtime/source/adapters/opencl/device.hpp @@ -108,8 +108,8 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/event.cpp b/unified-runtime/source/adapters/opencl/event.cpp index ad98aa6202c5e..4938a7bf88e14 100644 --- a/unified-runtime/source/adapters/opencl/event.cpp +++ b/unified-runtime/source/adapters/opencl/event.cpp @@ -136,14 +136,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) { - if (hEvent->getRefCount().decrementAndTest()) { + if (hEvent->getRefCount().release()) { delete hEvent; } return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) { - hEvent->getRefCount().increment(); + hEvent->getRefCount().retain(); return UR_RESULT_SUCCESS; } diff --git a/unified-runtime/source/adapters/opencl/event.hpp b/unified-runtime/source/adapters/opencl/event.hpp index 58834bfcf868d..f2d6a300ae54d 100644 --- a/unified-runtime/source/adapters/opencl/event.hpp +++ b/unified-runtime/source/adapters/opencl/event.hpp @@ -54,10 +54,10 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; inline cl_event *ifUrEvent(ur_event_handle_t *ReturnedEvent, cl_event &Event) { diff --git a/unified-runtime/source/adapters/opencl/kernel.cpp b/unified-runtime/source/adapters/opencl/kernel.cpp index f80440bd51a81..4260a6fc94e79 100644 --- a/unified-runtime/source/adapters/opencl/kernel.cpp +++ b/unified-runtime/source/adapters/opencl/kernel.cpp @@ -343,13 +343,13 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice, } UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) { - hKernel->getRefCount().increment(); + hKernel->getRefCount().retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urKernelRelease(ur_kernel_handle_t hKernel) { - if (hKernel->getRefCount().decrementAndTest()) { + if (hKernel->getRefCount().release()) { delete hKernel; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/kernel.hpp b/unified-runtime/source/adapters/opencl/kernel.hpp index f17532f62128b..c9314f3c06a23 100644 --- a/unified-runtime/source/adapters/opencl/kernel.hpp +++ b/unified-runtime/source/adapters/opencl/kernel.hpp @@ -50,8 +50,8 @@ struct ur_kernel_handle_t_ : ur::opencl::handle_base { ur_context_handle_t Context, ur_kernel_handle_t &Kernel); - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/memory.cpp b/unified-runtime/source/adapters/opencl/memory.cpp index d2e05459399a1..8da843129637e 100644 --- a/unified-runtime/source/adapters/opencl/memory.cpp +++ b/unified-runtime/source/adapters/opencl/memory.cpp @@ -569,12 +569,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, } UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { - hMem->getRefCount().increment(); + hMem->getRefCount().retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) { - if (hMem->getRefCount().decrementAndTest()) { + if (hMem->getRefCount().release()) { delete hMem; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/memory.hpp b/unified-runtime/source/adapters/opencl/memory.hpp index a70e0d3072c56..3f3849504633c 100644 --- a/unified-runtime/source/adapters/opencl/memory.hpp +++ b/unified-runtime/source/adapters/opencl/memory.hpp @@ -37,8 +37,8 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { ur_context_handle_t Ctx, ur_mem_handle_t &Mem); - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/program.cpp b/unified-runtime/source/adapters/opencl/program.cpp index 5da4435b75bfa..0e21cffe25a24 100644 --- a/unified-runtime/source/adapters/opencl/program.cpp +++ b/unified-runtime/source/adapters/opencl/program.cpp @@ -383,13 +383,13 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UR_APIEXPORT ur_result_t UR_APICALL urProgramRetain(ur_program_handle_t hProgram) { - hProgram->getRefCount().increment(); + hProgram->getRefCount().retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urProgramRelease(ur_program_handle_t hProgram) { - if (hProgram->getRefCount().decrementAndTest()) { + if (hProgram->getRefCount().release()) { delete hProgram; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/program.hpp b/unified-runtime/source/adapters/opencl/program.hpp index 7ddfc49683793..1640108b8b3c8 100644 --- a/unified-runtime/source/adapters/opencl/program.hpp +++ b/unified-runtime/source/adapters/opencl/program.hpp @@ -43,8 +43,8 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { ur_context_handle_t Context, ur_program_handle_t &Program); - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/queue.cpp b/unified-runtime/source/adapters/opencl/queue.cpp index 21b65edfb6f9f..09ee19a5cb789 100644 --- a/unified-runtime/source/adapters/opencl/queue.cpp +++ b/unified-runtime/source/adapters/opencl/queue.cpp @@ -289,12 +289,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush(ur_queue_handle_t hQueue) { } UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { - hQueue->getRefCount().increment(); + hQueue->getRefCount().retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urQueueRelease(ur_queue_handle_t hQueue) { - if (hQueue->getRefCount().decrementAndTest()) { + if (hQueue->getRefCount().release()) { delete hQueue; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/queue.hpp b/unified-runtime/source/adapters/opencl/queue.hpp index 841ef0e1ac2ec..c719f11fcc554 100644 --- a/unified-runtime/source/adapters/opencl/queue.hpp +++ b/unified-runtime/source/adapters/opencl/queue.hpp @@ -68,8 +68,8 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/sampler.cpp b/unified-runtime/source/adapters/opencl/sampler.cpp index a617c44ec26c2..361061b177bf6 100644 --- a/unified-runtime/source/adapters/opencl/sampler.cpp +++ b/unified-runtime/source/adapters/opencl/sampler.cpp @@ -221,13 +221,13 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urSamplerRetain(ur_sampler_handle_t hSampler) { - hSampler->getRefCount().increment(); + hSampler->getRefCount().retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urSamplerRelease(ur_sampler_handle_t hSampler) { - if (hSampler->getRefCount().decrementAndTest()) { + if (hSampler->getRefCount().release()) { delete hSampler; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/sampler.hpp b/unified-runtime/source/adapters/opencl/sampler.hpp index 6d220f8a6c072..939bca1ebb5e3 100644 --- a/unified-runtime/source/adapters/opencl/sampler.hpp +++ b/unified-runtime/source/adapters/opencl/sampler.hpp @@ -32,8 +32,8 @@ struct ur_sampler_handle_t_ : ur::opencl::handle_base { } } - URRefCount &getRefCount() noexcept { return RefCount; } + ur::RefCount &getRefCount() noexcept { return RefCount; } private: - URRefCount RefCount; + ur::RefCount RefCount; }; From 6b45464f500051bb17acd61313b249e4b5bc7914 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Wed, 2 Jul 2025 10:03:59 +0100 Subject: [PATCH 10/12] make RefCount public and remove getRefCount(). --- unified-runtime/source/adapters/opencl/adapter.cpp | 8 ++++---- unified-runtime/source/adapters/opencl/adapter.hpp | 7 ++----- unified-runtime/source/adapters/opencl/command_buffer.cpp | 6 +++--- unified-runtime/source/adapters/opencl/command_buffer.hpp | 7 ++----- unified-runtime/source/adapters/opencl/context.cpp | 6 +++--- unified-runtime/source/adapters/opencl/context.hpp | 5 +---- unified-runtime/source/adapters/opencl/device.cpp | 6 +++--- unified-runtime/source/adapters/opencl/device.hpp | 5 +---- unified-runtime/source/adapters/opencl/event.cpp | 6 +++--- unified-runtime/source/adapters/opencl/event.hpp | 6 +----- unified-runtime/source/adapters/opencl/kernel.cpp | 6 +++--- unified-runtime/source/adapters/opencl/kernel.hpp | 6 +----- unified-runtime/source/adapters/opencl/memory.cpp | 6 +++--- unified-runtime/source/adapters/opencl/memory.hpp | 5 +---- unified-runtime/source/adapters/opencl/program.cpp | 6 +++--- unified-runtime/source/adapters/opencl/program.hpp | 5 +---- unified-runtime/source/adapters/opencl/queue.cpp | 6 +++--- unified-runtime/source/adapters/opencl/queue.hpp | 6 +----- unified-runtime/source/adapters/opencl/sampler.cpp | 6 +++--- unified-runtime/source/adapters/opencl/sampler.hpp | 6 +----- 20 files changed, 43 insertions(+), 77 deletions(-) diff --git a/unified-runtime/source/adapters/opencl/adapter.cpp b/unified-runtime/source/adapters/opencl/adapter.cpp index 79e4c7cbb98ea..7f79b95b8903a 100644 --- a/unified-runtime/source/adapters/opencl/adapter.cpp +++ b/unified-runtime/source/adapters/opencl/adapter.cpp @@ -78,7 +78,7 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, } auto &adapter = *phAdapters; - adapter->getRefCount().retain(); + adapter->RefCount.retain(); } if (pNumAdapters) { @@ -90,13 +90,13 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t hAdapter) { - hAdapter->getRefCount().retain(); + hAdapter->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t hAdapter) { - if (hAdapter->getRefCount().release()) { + if (hAdapter->RefCount.release()) { delete hAdapter; } return UR_RESULT_SUCCESS; @@ -119,7 +119,7 @@ urAdapterGetInfo(ur_adapter_handle_t hAdapter, ur_adapter_info_t propName, case UR_ADAPTER_INFO_BACKEND: return ReturnValue(UR_BACKEND_OPENCL); case UR_ADAPTER_INFO_REFERENCE_COUNT: - return ReturnValue(hAdapter->getRefCount().getCount()); + return ReturnValue(hAdapter->RefCount.getCount()); case UR_ADAPTER_INFO_VERSION: return ReturnValue(uint32_t{1}); default: diff --git a/unified-runtime/source/adapters/opencl/adapter.hpp b/unified-runtime/source/adapters/opencl/adapter.hpp index fe41b9f0ef757..58dbaed6d6668 100644 --- a/unified-runtime/source/adapters/opencl/adapter.hpp +++ b/unified-runtime/source/adapters/opencl/adapter.hpp @@ -31,17 +31,14 @@ struct ur_adapter_handle_t_ : ur::opencl::handle_base { std::vector> URPlatforms; uint32_t NumPlatforms = 0; + ur::RefCount RefCount; + // Function pointers to core OpenCL entry points which may not exist in older // versions of the OpenCL-ICD-Loader are tracked here and initialized by // dynamically loading the symbol by name. #define CL_CORE_FUNCTION(FUNC) decltype(::FUNC) *FUNC = nullptr; #include "core_functions.def" #undef CL_CORE_FUNCTION - - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; namespace ur { diff --git a/unified-runtime/source/adapters/opencl/command_buffer.cpp b/unified-runtime/source/adapters/opencl/command_buffer.cpp index 35093329a88e3..f7981973ff548 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.cpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.cpp @@ -108,13 +108,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp( UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - hCommandBuffer->getRefCount().retain(); + hCommandBuffer->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - if (hCommandBuffer->getRefCount().release()) { + if (hCommandBuffer->RefCount.release()) { delete hCommandBuffer; } @@ -783,7 +783,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp( switch (propName) { case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT: - return ReturnValue(hCommandBuffer->getRefCount().getCount()); + return ReturnValue(hCommandBuffer->RefCount.getCount()); case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: { ur_exp_command_buffer_desc_t Descriptor{}; Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC; diff --git a/unified-runtime/source/adapters/opencl/command_buffer.hpp b/unified-runtime/source/adapters/opencl/command_buffer.hpp index 018a4260e13db..486150152065b 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.hpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.hpp @@ -57,6 +57,8 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base { /// Track last submission of the command-buffer cl_event LastSubmission; + ur::RefCount RefCount; + ur_exp_command_buffer_handle_t_(ur_queue_handle_t hQueue, ur_context_handle_t hContext, ur_device_handle_t hDevice, @@ -68,9 +70,4 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base { LastSubmission(nullptr) {} ~ur_exp_command_buffer_handle_t_(); - - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/context.cpp b/unified-runtime/source/adapters/opencl/context.cpp index d8a87476e9749..30f6f5b878e5e 100644 --- a/unified-runtime/source/adapters/opencl/context.cpp +++ b/unified-runtime/source/adapters/opencl/context.cpp @@ -108,7 +108,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName, return ReturnValue(&hContext->Devices[0], hContext->DeviceCount); } case UR_CONTEXT_INFO_REFERENCE_COUNT: { - return ReturnValue(hContext->getRefCount().getCount()); + return ReturnValue(hContext->RefCount.getCount()); } default: return UR_RESULT_ERROR_INVALID_ENUMERATION; @@ -117,7 +117,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urContextRelease(ur_context_handle_t hContext) { - if (hContext->getRefCount().release()) { + if (hContext->RefCount.release()) { delete hContext; } @@ -126,7 +126,7 @@ urContextRelease(ur_context_handle_t hContext) { UR_APIEXPORT ur_result_t UR_APICALL urContextRetain(ur_context_handle_t hContext) { - hContext->getRefCount().retain(); + hContext->RefCount.retain(); return UR_RESULT_SUCCESS; } diff --git a/unified-runtime/source/adapters/opencl/context.hpp b/unified-runtime/source/adapters/opencl/context.hpp index 64c0dcc285ddb..6ad438880f006 100644 --- a/unified-runtime/source/adapters/opencl/context.hpp +++ b/unified-runtime/source/adapters/opencl/context.hpp @@ -22,6 +22,7 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { std::vector Devices; uint32_t DeviceCount; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_context_handle_t_(native_type Ctx, uint32_t DevCount, const ur_device_handle_t *phDevices) @@ -50,8 +51,4 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { } } - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/device.cpp b/unified-runtime/source/adapters/opencl/device.cpp index f3d6b57c7871a..d0ab2376f5ca4 100644 --- a/unified-runtime/source/adapters/opencl/device.cpp +++ b/unified-runtime/source/adapters/opencl/device.cpp @@ -1019,7 +1019,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, return UR_RESULT_SUCCESS; } case UR_DEVICE_INFO_REFERENCE_COUNT: { - return ReturnValue(hDevice->getRefCount().getCount()); + return ReturnValue(hDevice->RefCount.getCount()); } case UR_DEVICE_INFO_PARTITION_MAX_SUB_DEVICES: { CL_RETURN_ON_FAILURE(clGetDeviceInfo(hDevice->CLDevice, @@ -1561,7 +1561,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( // Root devices ref count are unchanged through out the program lifetime. UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - hDevice->getRefCount().retain(); + hDevice->RefCount.retain(); } return UR_RESULT_SUCCESS; @@ -1571,7 +1571,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { UR_APIEXPORT ur_result_t UR_APICALL urDeviceRelease(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - if (hDevice->getRefCount().release()) { + if (hDevice->RefCount.release()) { delete hDevice; } } diff --git a/unified-runtime/source/adapters/opencl/device.hpp b/unified-runtime/source/adapters/opencl/device.hpp index b4b6e2f99a73b..8928d328fbfc5 100644 --- a/unified-runtime/source/adapters/opencl/device.hpp +++ b/unified-runtime/source/adapters/opencl/device.hpp @@ -21,6 +21,7 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { cl_device_type Type = 0; ur_device_handle_t ParentDevice = nullptr; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat, ur_device_handle_t Parent) @@ -108,8 +109,4 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/event.cpp b/unified-runtime/source/adapters/opencl/event.cpp index 4938a7bf88e14..3452cae59ac98 100644 --- a/unified-runtime/source/adapters/opencl/event.cpp +++ b/unified-runtime/source/adapters/opencl/event.cpp @@ -136,14 +136,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) { - if (hEvent->getRefCount().release()) { + if (hEvent->RefCount.release()) { delete hEvent; } return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) { - hEvent->getRefCount().retain(); + hEvent->RefCount.retain(); return UR_RESULT_SUCCESS; } @@ -188,7 +188,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent, return ReturnValue(hEvent->Queue); } case UR_EVENT_INFO_REFERENCE_COUNT: { - return ReturnValue(hEvent->getRefCount().getCount()); + return ReturnValue(hEvent->RefCount.getCount()); } default: { size_t CheckPropSize = 0; diff --git a/unified-runtime/source/adapters/opencl/event.hpp b/unified-runtime/source/adapters/opencl/event.hpp index f2d6a300ae54d..33b74386fa77c 100644 --- a/unified-runtime/source/adapters/opencl/event.hpp +++ b/unified-runtime/source/adapters/opencl/event.hpp @@ -21,6 +21,7 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { ur_context_handle_t Context; ur_queue_handle_t Queue; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx, ur_queue_handle_t Queue) @@ -53,11 +54,6 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } - - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; inline cl_event *ifUrEvent(ur_event_handle_t *ReturnedEvent, cl_event &Event) { diff --git a/unified-runtime/source/adapters/opencl/kernel.cpp b/unified-runtime/source/adapters/opencl/kernel.cpp index 4260a6fc94e79..b673f9743766c 100644 --- a/unified-runtime/source/adapters/opencl/kernel.cpp +++ b/unified-runtime/source/adapters/opencl/kernel.cpp @@ -152,7 +152,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelGetInfo(ur_kernel_handle_t hKernel, return ReturnValue(hKernel->Context); } case UR_KERNEL_INFO_REFERENCE_COUNT: { - return ReturnValue(hKernel->getRefCount().getCount()); + return ReturnValue(hKernel->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -343,13 +343,13 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice, } UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) { - hKernel->getRefCount().retain(); + hKernel->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urKernelRelease(ur_kernel_handle_t hKernel) { - if (hKernel->getRefCount().release()) { + if (hKernel->RefCount.release()) { delete hKernel; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/kernel.hpp b/unified-runtime/source/adapters/opencl/kernel.hpp index c9314f3c06a23..23bbe62face79 100644 --- a/unified-runtime/source/adapters/opencl/kernel.hpp +++ b/unified-runtime/source/adapters/opencl/kernel.hpp @@ -24,6 +24,7 @@ struct ur_kernel_handle_t_ : ur::opencl::handle_base { ur_context_handle_t Context; bool IsNativeHandleOwned = true; clSetKernelArgMemPointerINTEL_fn clSetKernelArgMemPointerINTEL = nullptr; + ur::RefCount RefCount; ur_kernel_handle_t_(native_type Kernel, ur_program_handle_t Program, ur_context_handle_t Context) @@ -49,9 +50,4 @@ struct ur_kernel_handle_t_ : ur::opencl::handle_base { ur_program_handle_t Program, ur_context_handle_t Context, ur_kernel_handle_t &Kernel); - - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/memory.cpp b/unified-runtime/source/adapters/opencl/memory.cpp index 8da843129637e..19e9509987825 100644 --- a/unified-runtime/source/adapters/opencl/memory.cpp +++ b/unified-runtime/source/adapters/opencl/memory.cpp @@ -521,7 +521,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemGetInfo(ur_mem_handle_t hMemory, return ReturnValue(hMemory->Context); } case UR_MEM_INFO_REFERENCE_COUNT: { - return ReturnValue(hMemory->getRefCount().getCount()); + return ReturnValue(hMemory->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -569,12 +569,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, } UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { - hMem->getRefCount().retain(); + hMem->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) { - if (hMem->getRefCount().release()) { + if (hMem->RefCount.release()) { delete hMem; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/memory.hpp b/unified-runtime/source/adapters/opencl/memory.hpp index 3f3849504633c..1f3de2f9e692d 100644 --- a/unified-runtime/source/adapters/opencl/memory.hpp +++ b/unified-runtime/source/adapters/opencl/memory.hpp @@ -20,6 +20,7 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { native_type CLMemory; ur_context_handle_t Context; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_mem_handle_t_(native_type Mem, ur_context_handle_t Ctx) : handle_base(), CLMemory(Mem), Context(Ctx) { @@ -37,8 +38,4 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { ur_context_handle_t Ctx, ur_mem_handle_t &Mem); - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/program.cpp b/unified-runtime/source/adapters/opencl/program.cpp index 0e21cffe25a24..dffd9aed9074b 100644 --- a/unified-runtime/source/adapters/opencl/program.cpp +++ b/unified-runtime/source/adapters/opencl/program.cpp @@ -223,7 +223,7 @@ urProgramGetInfo(ur_program_handle_t hProgram, ur_program_info_t propName, return ReturnValue(hProgram->Devices.data(), hProgram->NumDevices); } case UR_PROGRAM_INFO_REFERENCE_COUNT: { - return ReturnValue(hProgram->getRefCount().getCount()); + return ReturnValue(hProgram->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -383,13 +383,13 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UR_APIEXPORT ur_result_t UR_APICALL urProgramRetain(ur_program_handle_t hProgram) { - hProgram->getRefCount().retain(); + hProgram->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urProgramRelease(ur_program_handle_t hProgram) { - if (hProgram->getRefCount().release()) { + if (hProgram->RefCount.release()) { delete hProgram; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/program.hpp b/unified-runtime/source/adapters/opencl/program.hpp index 1640108b8b3c8..c658c0a2f8180 100644 --- a/unified-runtime/source/adapters/opencl/program.hpp +++ b/unified-runtime/source/adapters/opencl/program.hpp @@ -22,6 +22,7 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { bool IsNativeHandleOwned = true; uint32_t NumDevices = 0; std::vector Devices; + ur::RefCount RefCount; ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx, uint32_t NumDevices, ur_device_handle_t *Devs) @@ -43,8 +44,4 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { ur_context_handle_t Context, ur_program_handle_t &Program); - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/queue.cpp b/unified-runtime/source/adapters/opencl/queue.cpp index 09ee19a5cb789..e143d6b912a87 100644 --- a/unified-runtime/source/adapters/opencl/queue.cpp +++ b/unified-runtime/source/adapters/opencl/queue.cpp @@ -234,7 +234,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueGetInfo(ur_queue_handle_t hQueue, return ReturnValue(mapCLQueuePropsToUR(QueueProperties)); } case UR_QUEUE_INFO_REFERENCE_COUNT: { - return ReturnValue(hQueue->getRefCount().getCount()); + return ReturnValue(hQueue->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -289,12 +289,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush(ur_queue_handle_t hQueue) { } UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { - hQueue->getRefCount().retain(); + hQueue->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urQueueRelease(ur_queue_handle_t hQueue) { - if (hQueue->getRefCount().release()) { + if (hQueue->RefCount.release()) { delete hQueue; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/queue.hpp b/unified-runtime/source/adapters/opencl/queue.hpp index c719f11fcc554..9456c8ae8618d 100644 --- a/unified-runtime/source/adapters/opencl/queue.hpp +++ b/unified-runtime/source/adapters/opencl/queue.hpp @@ -27,6 +27,7 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { // Used to implement UR_QUEUE_INFO_EMPTY query bool IsInOrder; ur_event_handle_t LastEvent = nullptr; + ur::RefCount RefCount; ur_queue_handle_t_(native_type Queue, ur_context_handle_t Ctx, ur_device_handle_t Dev, bool InOrder) @@ -67,9 +68,4 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { } return UR_RESULT_SUCCESS; } - - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; diff --git a/unified-runtime/source/adapters/opencl/sampler.cpp b/unified-runtime/source/adapters/opencl/sampler.cpp index 361061b177bf6..ee2a1f9958b5d 100644 --- a/unified-runtime/source/adapters/opencl/sampler.cpp +++ b/unified-runtime/source/adapters/opencl/sampler.cpp @@ -175,7 +175,7 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, return ReturnValue(hSampler->Context); } case UR_SAMPLER_INFO_REFERENCE_COUNT: { - return ReturnValue(hSampler->getRefCount().getCount()); + return ReturnValue(hSampler->RefCount.getCount()); } // ur_bool_t have a size of uint8_t, but cl_bool size have the size of // uint32_t so this adjust UR_SAMPLER_INFO_NORMALIZED_COORDS info to map @@ -221,13 +221,13 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urSamplerRetain(ur_sampler_handle_t hSampler) { - hSampler->getRefCount().retain(); + hSampler->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urSamplerRelease(ur_sampler_handle_t hSampler) { - if (hSampler->getRefCount().release()) { + if (hSampler->RefCount.release()) { delete hSampler; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/sampler.hpp b/unified-runtime/source/adapters/opencl/sampler.hpp index 939bca1ebb5e3..26a116c8df0f7 100644 --- a/unified-runtime/source/adapters/opencl/sampler.hpp +++ b/unified-runtime/source/adapters/opencl/sampler.hpp @@ -19,6 +19,7 @@ struct ur_sampler_handle_t_ : ur::opencl::handle_base { native_type CLSampler; ur_context_handle_t Context; bool IsNativeHandleOwned = false; + ur::RefCount RefCount; ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx) : handle_base(), CLSampler(Sampler), Context(Ctx) { @@ -31,9 +32,4 @@ struct ur_sampler_handle_t_ : ur::opencl::handle_base { clReleaseSampler(CLSampler); } } - - ur::RefCount &getRefCount() noexcept { return RefCount; } - -private: - ur::RefCount RefCount; }; From 573ac1ddbfc724cb276bb1d9c0dffe1c08cc3e06 Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Wed, 2 Jul 2025 10:20:57 +0100 Subject: [PATCH 11/12] formatting --- unified-runtime/source/adapters/opencl/context.hpp | 1 - unified-runtime/source/adapters/opencl/device.hpp | 1 - unified-runtime/source/adapters/opencl/memory.hpp | 1 - unified-runtime/source/adapters/opencl/program.hpp | 1 - 4 files changed, 4 deletions(-) diff --git a/unified-runtime/source/adapters/opencl/context.hpp b/unified-runtime/source/adapters/opencl/context.hpp index 6ad438880f006..237907e3ad5b6 100644 --- a/unified-runtime/source/adapters/opencl/context.hpp +++ b/unified-runtime/source/adapters/opencl/context.hpp @@ -50,5 +50,4 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { clReleaseContext(CLContext); } } - }; diff --git a/unified-runtime/source/adapters/opencl/device.hpp b/unified-runtime/source/adapters/opencl/device.hpp index 8928d328fbfc5..71adb063964f0 100644 --- a/unified-runtime/source/adapters/opencl/device.hpp +++ b/unified-runtime/source/adapters/opencl/device.hpp @@ -108,5 +108,4 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { return UR_RESULT_SUCCESS; } - }; diff --git a/unified-runtime/source/adapters/opencl/memory.hpp b/unified-runtime/source/adapters/opencl/memory.hpp index 1f3de2f9e692d..847ffafa76021 100644 --- a/unified-runtime/source/adapters/opencl/memory.hpp +++ b/unified-runtime/source/adapters/opencl/memory.hpp @@ -37,5 +37,4 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { static ur_result_t makeWithNative(native_type NativeMem, ur_context_handle_t Ctx, ur_mem_handle_t &Mem); - }; diff --git a/unified-runtime/source/adapters/opencl/program.hpp b/unified-runtime/source/adapters/opencl/program.hpp index c658c0a2f8180..70487dd8e0edc 100644 --- a/unified-runtime/source/adapters/opencl/program.hpp +++ b/unified-runtime/source/adapters/opencl/program.hpp @@ -43,5 +43,4 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { static ur_result_t makeWithNative(native_type NativeProg, ur_context_handle_t Context, ur_program_handle_t &Program); - }; From b21cd168892feeaf1b7cbc074dbf2b26020c6f9c Mon Sep 17 00:00:00 2001 From: Martin Morrison-Grant Date: Thu, 3 Jul 2025 11:20:08 +0100 Subject: [PATCH 12/12] Update #ifndef in ur_ref_count.hpp --- unified-runtime/source/common/ur_ref_count.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/unified-runtime/source/common/ur_ref_count.hpp b/unified-runtime/source/common/ur_ref_count.hpp index add4fe0af2d53..5f0eebd9a6f03 100644 --- a/unified-runtime/source/common/ur_ref_count.hpp +++ b/unified-runtime/source/common/ur_ref_count.hpp @@ -8,8 +8,8 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * */ -#ifndef REFCOUNT_HPP -#define REFCOUNT_HPP 1 +#ifndef UR_REF_COUNT_HPP +#define UR_REF_COUNT_HPP 1 #include #include @@ -33,4 +33,4 @@ class RefCount { } // namespace ur -#endif // REFCOUNT_HPP +#endif // UR_REF_COUNT_HPP