diff --git a/sycl/include/sycl/accessor.hpp b/sycl/include/sycl/accessor.hpp index 3e9bfd250b11d..af3ceb06e45ee 100644 --- a/sycl/include/sycl/accessor.hpp +++ b/sycl/include/sycl/accessor.hpp @@ -2937,61 +2937,24 @@ host_accessor(buffer, Type1, Type2, Type3, Type4, } // namespace _V1 } // namespace sycl -namespace std { template -struct hash> { - using AccType = sycl::accessor; - - size_t operator()(const AccType &A) const { -#ifdef __SYCL_DEVICE_ONLY__ - // Hash is not supported on DEVICE. Just return 0 here. - (void)A; - return 0; -#else - // getSyclObjImpl() here returns a pointer to either AccessorImplHost - // or LocalAccessorImplHost depending on the AccessTarget. - auto AccImplPtr = sycl::detail::getSyclObjImpl(A); - return hash()(AccImplPtr); -#endif - } -}; +struct std::hash< + sycl::accessor> + : public sycl::detail::sycl_obj_hash< + sycl::accessor, + false /*SupportedOnDevice*/> {}; template -struct hash> { - using AccType = sycl::host_accessor; - - size_t operator()(const AccType &A) const { -#ifdef __SYCL_DEVICE_ONLY__ - // Hash is not supported on DEVICE. Just return 0 here. - (void)A; - return 0; -#else - // getSyclObjImpl() here returns a pointer to AccessorImplHost. - auto AccImplPtr = sycl::detail::getSyclObjImpl(A); - return hash()(AccImplPtr); -#endif - } -}; +struct std::hash> + : public sycl::detail::sycl_obj_hash< + sycl::host_accessor, + false /*SupportedOnDevice*/> {}; template -struct hash> { - using AccType = sycl::local_accessor; - - size_t operator()(const AccType &A) const { -#ifdef __SYCL_DEVICE_ONLY__ - // Hash is not supported on DEVICE. Just return 0 here. - (void)A; - return 0; -#else - // getSyclObjImpl() here returns a pointer to LocalAccessorImplHost. - auto AccImplPtr = sycl::detail::getSyclObjImpl(A); - return hash()(AccImplPtr); -#endif - } -}; - -} // namespace std +struct std::hash> + : public sycl::detail::sycl_obj_hash< + sycl::local_accessor, + false /*SupportedOnDevice*/> {}; diff --git a/sycl/include/sycl/accessor_image.hpp b/sycl/include/sycl/accessor_image.hpp index d697efc56aa83..468bb8d62dacd 100644 --- a/sycl/include/sycl/accessor_image.hpp +++ b/sycl/include/sycl/accessor_image.hpp @@ -1357,62 +1357,29 @@ class __SYCL_EBO host_sampled_image_accessor } // namespace _V1 } // namespace sycl -namespace std { template -struct hash> { - using AccType = sycl::unsampled_image_accessor; - - size_t operator()(const AccType &A) const { -#ifdef __SYCL_DEVICE_ONLY__ - // Hash is not supported on DEVICE. Just return 0 here. - (void)A; - return 0; -#else - auto AccImplPtr = sycl::detail::getSyclObjImpl(A); - return hash()(AccImplPtr); -#endif - } -}; +struct std::hash< + sycl::unsampled_image_accessor> + : public sycl::detail::sycl_obj_hash< + sycl::unsampled_image_accessor, + false /*SupportedOnDevice*/> {}; template -struct hash< - sycl::host_unsampled_image_accessor> { - using AccType = - sycl::host_unsampled_image_accessor; - - size_t operator()(const AccType &A) const { - auto AccImplPtr = sycl::detail::getSyclObjImpl(A); - return hash()(AccImplPtr); - } +struct std::hash< + sycl::host_unsampled_image_accessor> + : public sycl::detail::sycl_obj_hash< + sycl::host_unsampled_image_accessor> { }; template -struct hash> { - using AccType = sycl::sampled_image_accessor; - - size_t operator()(const AccType &A) const { -#ifdef __SYCL_DEVICE_ONLY__ - // Hash is not supported on DEVICE. Just return 0 here. - (void)A; - return 0; -#else - auto AccImplPtr = sycl::detail::getSyclObjImpl(A); - return hash()(AccImplPtr); -#endif - } -}; +struct std::hash> + : public sycl::detail::sycl_obj_hash< + sycl::sampled_image_accessor, + false /*SupportedOnDevice*/> {}; template -struct hash> { - using AccType = sycl::host_sampled_image_accessor; - - size_t operator()(const AccType &A) const { - auto AccImplPtr = sycl::detail::getSyclObjImpl(A); - return hash()(AccImplPtr); - } -}; - -} // namespace std +struct std::hash> + : public sycl::detail::sycl_obj_hash< + sycl::host_sampled_image_accessor> {}; diff --git a/sycl/include/sycl/buffer.hpp b/sycl/include/sycl/buffer.hpp index 8b3a14af607f2..b7c4535c8a40c 100644 --- a/sycl/include/sycl/buffer.hpp +++ b/sycl/include/sycl/buffer.hpp @@ -863,12 +863,7 @@ buffer(const T *, const range &, } // namespace _V1 } // namespace sycl -namespace std { template -struct hash> { - size_t operator()(const sycl::buffer &b) const { - return hash>()( - sycl::detail::getSyclObjImpl(b)); - } -}; -} // namespace std +struct std::hash> + : public sycl::detail::sycl_obj_hash< + sycl::buffer> {}; diff --git a/sycl/include/sycl/context.hpp b/sycl/include/sycl/context.hpp index 047d83f734b74..08fb09ca91daf 100644 --- a/sycl/include/sycl/context.hpp +++ b/sycl/include/sycl/context.hpp @@ -311,11 +311,6 @@ inline exception::exception(context Ctx, int EV, } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::context &Context) const { - return hash>()( - sycl::detail::getSyclObjImpl(Context)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {}; diff --git a/sycl/include/sycl/detail/impl_utils.hpp b/sycl/include/sycl/detail/impl_utils.hpp index b4cd173a6f2b6..00f61558a88d4 100644 --- a/sycl/include/sycl/detail/impl_utils.hpp +++ b/sycl/include/sycl/detail/impl_utils.hpp @@ -9,6 +9,7 @@ #pragma once #include // for assert +#include // for hash #include // for add_pointer_t #include // for forward @@ -58,6 +59,23 @@ T createSyclObjFromImpl( return createSyclObjFromImpl(ImplRef.shared_from_this()); } +template struct sycl_obj_hash { + size_t operator()(const T &Obj) const { + if constexpr (SupportedOnDevice) { + auto &Impl = sycl::detail::getSyclObjImpl(Obj); + return std::hash>{}(Impl); + } else { +#ifdef __SYCL_DEVICE_ONLY__ + (void)Obj; + return 0; +#else + auto &Impl = sycl::detail::getSyclObjImpl(Obj); + return std::hash>{}(Impl); +#endif + } + } +}; + } // namespace detail } // namespace _V1 } // namespace sycl diff --git a/sycl/include/sycl/device.hpp b/sycl/include/sycl/device.hpp index 24ed5a03ed525..8d2aa1b75b51b 100644 --- a/sycl/include/sycl/device.hpp +++ b/sycl/include/sycl/device.hpp @@ -407,11 +407,6 @@ class __SYCL_STANDALONE_DEBUG __SYCL_EXPORT device } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::device &Device) const { - return hash>()( - sycl::detail::getSyclObjImpl(Device)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {}; diff --git a/sycl/include/sycl/event.hpp b/sycl/include/sycl/event.hpp index 444306d4686a2..8dfedac6b3231 100644 --- a/sycl/include/sycl/event.hpp +++ b/sycl/include/sycl/event.hpp @@ -175,11 +175,6 @@ class __SYCL_EXPORT event : public detail::OwnerLessBase { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::event &e) const { - return hash>()( - sycl::detail::getSyclObjImpl(e)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {}; diff --git a/sycl/include/sycl/ext/oneapi/bindless_images_memory.hpp b/sycl/include/sycl/ext/oneapi/bindless_images_memory.hpp index 74e16d0c27be3..88c5bdbb59c74 100644 --- a/sycl/include/sycl/ext/oneapi/bindless_images_memory.hpp +++ b/sycl/include/sycl/ext/oneapi/bindless_images_memory.hpp @@ -117,13 +117,7 @@ enum class image_memory_handle_type : unsigned int { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()( - const sycl::ext::oneapi::experimental::image_mem &image_mem) const { - return hash>()( - sycl::detail::getSyclObjImpl(image_mem)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash< + sycl::ext::oneapi::experimental::image_mem> {}; diff --git a/sycl/include/sycl/ext/oneapi/experimental/async_alloc/memory_pool.hpp b/sycl/include/sycl/ext/oneapi/experimental/async_alloc/memory_pool.hpp index b8584de79a23d..d21c32a6945b4 100644 --- a/sycl/include/sycl/ext/oneapi/experimental/async_alloc/memory_pool.hpp +++ b/sycl/include/sycl/ext/oneapi/experimental/async_alloc/memory_pool.hpp @@ -110,13 +110,7 @@ class __SYCL_EXPORT memory_pool { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()( - const sycl::ext::oneapi::experimental::memory_pool &mem_pool) const { - return hash>()( - sycl::detail::getSyclObjImpl(mem_pool)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash< + sycl::ext::oneapi::experimental::memory_pool> {}; diff --git a/sycl/include/sycl/ext/oneapi/virtual_mem/physical_mem.hpp b/sycl/include/sycl/ext/oneapi/virtual_mem/physical_mem.hpp index 34e2153165058..c7338474e50ac 100644 --- a/sycl/include/sycl/ext/oneapi/virtual_mem/physical_mem.hpp +++ b/sycl/include/sycl/ext/oneapi/virtual_mem/physical_mem.hpp @@ -71,12 +71,7 @@ class __SYCL_EXPORT physical_mem } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()( - const sycl::ext::oneapi::experimental::physical_mem &PhysicalMem) const { - return hash>()( - sycl::detail::getSyclObjImpl(PhysicalMem)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash< + sycl::ext::oneapi::experimental::physical_mem> {}; diff --git a/sycl/include/sycl/image.hpp b/sycl/include/sycl/image.hpp index 4503312e49b18..004763c8d601d 100644 --- a/sycl/include/sycl/image.hpp +++ b/sycl/include/sycl/image.hpp @@ -1160,30 +1160,17 @@ class sampled_image } // namespace _V1 } // namespace sycl -namespace std { template -struct hash> { - size_t operator()(const sycl::image &I) const { - return hash>()( - sycl::detail::getSyclObjImpl(I)); - } +struct std::hash> + : public sycl::detail::sycl_obj_hash> { }; template -struct hash> { - size_t - operator()(const sycl::unsampled_image &I) const { - return hash>()( - sycl::detail::getSyclObjImpl(I)); - } -}; +struct std::hash> + : public sycl::detail::sycl_obj_hash< + sycl::unsampled_image> {}; template -struct hash> { - size_t - operator()(const sycl::sampled_image &I) const { - return hash>()( - sycl::detail::getSyclObjImpl(I)); - } -}; -} // namespace std +struct std::hash> + : public sycl::detail::sycl_obj_hash< + sycl::sampled_image> {}; diff --git a/sycl/include/sycl/kernel.hpp b/sycl/include/sycl/kernel.hpp index b332c09e4772d..164ff270e237d 100644 --- a/sycl/include/sycl/kernel.hpp +++ b/sycl/include/sycl/kernel.hpp @@ -280,11 +280,6 @@ class __SYCL_EXPORT kernel : public detail::OwnerLessBase { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::kernel &Kernel) const { - return hash>()( - sycl::detail::getSyclObjImpl(Kernel)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {}; diff --git a/sycl/include/sycl/kernel_bundle.hpp b/sycl/include/sycl/kernel_bundle.hpp index d8a7a0cd9cd99..76b18f69b2722 100644 --- a/sycl/include/sycl/kernel_bundle.hpp +++ b/sycl/include/sycl/kernel_bundle.hpp @@ -1362,25 +1362,14 @@ handler::get_specialization_constant() const { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::kernel_id &KernelID) const { - return hash>()( - sycl::detail::getSyclObjImpl(KernelID)); - } -}; +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {}; -template struct hash> { - size_t operator()(const sycl::device_image &DeviceImage) const { - return hash>()( - sycl::detail::getSyclObjImpl(DeviceImage)); - } -}; +template +struct std::hash> + : public sycl::detail::sycl_obj_hash> {}; -template struct hash> { - size_t operator()(const sycl::kernel_bundle &KernelBundle) const { - return hash>()( - sycl::detail::getSyclObjImpl(KernelBundle)); - } -}; -} // namespace std +template +struct std::hash> + : public sycl::detail::sycl_obj_hash> {}; diff --git a/sycl/include/sycl/platform.hpp b/sycl/include/sycl/platform.hpp index 7c696a3f11bb2..f63a68e9e6031 100644 --- a/sycl/include/sycl/platform.hpp +++ b/sycl/include/sycl/platform.hpp @@ -246,11 +246,6 @@ class __SYCL_EXPORT platform : public detail::OwnerLessBase { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::platform &p) const { - return hash>()( - sycl::detail::getSyclObjImpl(p)); - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {}; diff --git a/sycl/include/sycl/sampler.hpp b/sycl/include/sycl/sampler.hpp index ed66446f65e62..dcf9286b03e54 100644 --- a/sycl/include/sycl/sampler.hpp +++ b/sycl/include/sycl/sampler.hpp @@ -146,16 +146,7 @@ struct image_sampler { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::sampler &s) const { -#ifdef __SYCL_DEVICE_ONLY__ - (void)s; - return 0; -#else - return hash>()( - sycl::detail::getSyclObjImpl(s)); -#endif - } -}; -} // namespace std +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {}; diff --git a/sycl/include/sycl/stream.hpp b/sycl/include/sycl/stream.hpp index a42b6b4a4380e..b583b03635e82 100644 --- a/sycl/include/sycl/stream.hpp +++ b/sycl/include/sycl/stream.hpp @@ -1304,16 +1304,8 @@ inline const stream &operator<<(const stream &Out, const T &RHS) { } // namespace _V1 } // namespace sycl -namespace std { -template <> struct hash { - size_t operator()(const sycl::stream &S) const { -#ifdef __SYCL_DEVICE_ONLY__ - (void)S; - return 0; -#else - return hash>()( - sycl::detail::getSyclObjImpl(S)); -#endif - } -}; -} // namespace std + +template <> +struct std::hash + : public sycl::detail::sycl_obj_hash {};