From c0929c355de3d4ec07917235ed5cf88ee208b768 Mon Sep 17 00:00:00 2001 From: Andrei Elovikov Date: Thu, 17 Jul 2025 11:50:40 -0700 Subject: [PATCH] [NFC][SYCL] Simplify `variadic_iterator` usage `std::weak_ptr` aren't used to create `nodes_range` anymore (https://github.com/intel/llvm/pull/19438 being the last change that enabled that, I think) and we also added `SyclTy` template parameter to `variadic_iterator` so that the dereference operator can be implemented in a generic way in the base class. --- sycl/source/detail/device_impl.hpp | 22 ++++------------------ sycl/source/detail/graph/node_impl.hpp | 26 +------------------------- sycl/source/detail/helpers.hpp | 21 ++++++++++++++------- 3 files changed, 19 insertions(+), 50 deletions(-) diff --git a/sycl/source/detail/device_impl.hpp b/sycl/source/detail/device_impl.hpp index 1b86d170267db..faeaba0c8b492 100644 --- a/sycl/source/detail/device_impl.hpp +++ b/sycl/source/detail/device_impl.hpp @@ -2282,24 +2282,10 @@ class device_impl : public std::enable_shared_from_this { }; // class device_impl -struct devices_deref_impl { - template static device_impl &dereference(T &Elem) { - using Ty = std::decay_t; - if constexpr (std::is_same_v) { - return *getSyclObjImpl(Elem); - } else if constexpr (std::is_same_v) { - return Elem; - } else { - return *Elem; - } - } -}; -using devices_iterator = - variadic_iterator>::const_iterator, - std::vector::const_iterator, - std::vector::const_iterator, - device_impl *>; +using devices_iterator = variadic_iterator< + device, std::vector>::const_iterator, + std::vector::const_iterator, + std::vector::const_iterator, device_impl *>; class devices_range : public iterator_range { private: diff --git a/sycl/source/detail/graph/node_impl.hpp b/sycl/source/detail/graph/node_impl.hpp index 3501830f14cb3..bfcdb18f63a4f 100644 --- a/sycl/source/detail/graph/node_impl.hpp +++ b/sycl/source/detail/graph/node_impl.hpp @@ -749,37 +749,13 @@ class node_impl : public std::enable_shared_from_this { } }; -struct nodes_deref_impl { - template static node_impl &dereference(T &Elem) { - using Ty = std::decay_t; - if constexpr (std::is_same_v>) { - // This assumes that weak_ptr doesn't actually manage lifetime and - // the object is guaranteed to be alive (which seems to be the - // assumption across all graph code). - return *Elem.lock(); - } else if constexpr (std::is_same_v) { - return *getSyclObjImpl(Elem); - } else { - return *Elem; - } - } -}; - template using nodes_iterator_impl = - variadic_iterator; + variadic_iterator; using nodes_iterator = nodes_iterator_impl< std::vector>, std::vector, - // Next one is temporary. It looks like `weak_ptr`s aren't - // used for the actual lifetime management and the objects are - // always guaranteed to be alive. Once the code is cleaned - // from `weak_ptr`s this alternative should be removed too. - std::vector>, - // std::set>, std::set, - // std::list, std::vector>; class nodes_range : public iterator_range { diff --git a/sycl/source/detail/helpers.hpp b/sycl/source/detail/helpers.hpp index a1a49361e5755..0629327878297 100644 --- a/sycl/source/detail/helpers.hpp +++ b/sycl/source/detail/helpers.hpp @@ -35,8 +35,7 @@ const RTDeviceBinaryImage * retrieveKernelBinary(queue_impl &Queue, KernelNameStrRefT KernelName, CGExecKernel *CGKernel = nullptr); -template -class variadic_iterator { +template class variadic_iterator { using storage_iter = std::variant; storage_iter It; @@ -44,12 +43,11 @@ class variadic_iterator { public: using iterator_category = std::forward_iterator_tag; using difference_type = std::ptrdiff_t; - using reference = decltype(DereferenceImpl::dereference( - *std::declval>())); - using value_type = std::remove_reference_t; + using value_type = std::remove_reference_t()))>; using sycl_type = SyclTy; using pointer = value_type *; - static_assert(std::is_same_v); + using reference = value_type &; variadic_iterator(const variadic_iterator &) = default; variadic_iterator(variadic_iterator &&) = default; @@ -79,7 +77,16 @@ class variadic_iterator { decltype(auto) operator*() { return std::visit( [](auto &&It) -> decltype(auto) { - return DereferenceImpl::dereference(*It); + decltype(auto) Elem = *It; + using Ty = std::decay_t; + static_assert(!std::is_same_v); + if constexpr (std::is_same_v) { + return *getSyclObjImpl(Elem); + } else if constexpr (std::is_same_v) { + return Elem; + } else { + return *Elem; + } }, It); }