From 0f2db0176d816fac301668e08ee4039318953ba7 Mon Sep 17 00:00:00 2001 From: Andrei Elovikov Date: Thu, 24 Jul 2025 12:15:09 -0700 Subject: [PATCH] [NFC][SYCL] No need for `std::optional` in `device_global_map_entry` Using `nullptr` for not having an event is just fine. --- .../source/detail/device_global_map_entry.cpp | 34 +++++++++---------- .../source/detail/device_global_map_entry.hpp | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/sycl/source/detail/device_global_map_entry.cpp b/sycl/source/detail/device_global_map_entry.cpp index 2bad4a896958a..1f82a605056dc 100644 --- a/sycl/source/detail/device_global_map_entry.cpp +++ b/sycl/source/detail/device_global_map_entry.cpp @@ -22,23 +22,23 @@ DeviceGlobalUSMMem::~DeviceGlobalUSMMem() { // and the event. When asserts are enabled the values are set, so we check // these here. assert(MPtr == nullptr && "MPtr has not been cleaned up."); - assert(!MInitEvent.has_value() && "MInitEvent has not been cleaned up."); + assert(MInitEvent == nullptr && "MInitEvent has not been cleaned up."); } OwnedUrEvent DeviceGlobalUSMMem::getInitEvent(adapter_impl &Adapter) { std::lock_guard Lock(MInitEventMutex); + if (MInitEvent == nullptr) + return OwnedUrEvent(Adapter); + // If there is a init event we can remove it if it is done. - if (MInitEvent.has_value()) { - if (get_event_info( - *MInitEvent, Adapter) == info::event_command_status::complete) { - Adapter.call(*MInitEvent); - MInitEvent = {}; - return OwnedUrEvent(Adapter); - } else { - return OwnedUrEvent(*MInitEvent, Adapter); - } + if (get_event_info( + MInitEvent, Adapter) == info::event_command_status::complete) { + Adapter.call(MInitEvent); + MInitEvent = nullptr; + return OwnedUrEvent(Adapter); + } else { + return OwnedUrEvent(MInitEvent, Adapter); } - return OwnedUrEvent(Adapter); } DeviceGlobalUSMMem & @@ -158,14 +158,14 @@ void DeviceGlobalMapEntry::removeAssociatedResources( if (USMPtrIt != MDeviceToUSMPtrMap.end()) { DeviceGlobalUSMMem &USMMem = USMPtrIt->second; detail::usm::freeInternal(USMMem.MPtr, CtxImpl); - if (USMMem.MInitEvent.has_value()) + if (USMMem.MInitEvent != nullptr) CtxImpl->getAdapter().call( - *USMMem.MInitEvent); + USMMem.MInitEvent); #ifndef NDEBUG // For debugging we set the event and memory to some recognizable values // to allow us to check that this cleanup happens before erasure. USMMem.MPtr = nullptr; - USMMem.MInitEvent = {}; + USMMem.MInitEvent = nullptr; #endif MDeviceToUSMPtrMap.erase(USMPtrIt); } @@ -183,13 +183,13 @@ void DeviceGlobalMapEntry::cleanup() { const context_impl *CtxImpl = USMPtrIt.first.second; DeviceGlobalUSMMem &USMMem = USMPtrIt.second; detail::usm::freeInternal(USMMem.MPtr, CtxImpl); - if (USMMem.MInitEvent.has_value()) - CtxImpl->getAdapter().call(*USMMem.MInitEvent); + if (USMMem.MInitEvent != nullptr) + CtxImpl->getAdapter().call(USMMem.MInitEvent); #ifndef NDEBUG // For debugging we set the event and memory to some recognizable values // to allow us to check that this cleanup happens before erasure. USMMem.MPtr = nullptr; - USMMem.MInitEvent = {}; + USMMem.MInitEvent = nullptr; #endif } MDeviceToUSMPtrMap.clear(); diff --git a/sycl/source/detail/device_global_map_entry.hpp b/sycl/source/detail/device_global_map_entry.hpp index b25fdac4ca683..1796e8d179db1 100644 --- a/sycl/source/detail/device_global_map_entry.hpp +++ b/sycl/source/detail/device_global_map_entry.hpp @@ -44,7 +44,7 @@ struct DeviceGlobalUSMMem { private: void *MPtr; std::mutex MInitEventMutex; - std::optional MInitEvent; + ur_event_handle_t MInitEvent = nullptr; friend struct DeviceGlobalMapEntry; };