Skip to content

Commit

Permalink
[SYCL] Make queue hash dependent on its unique ID (intel#12578)
Browse files Browse the repository at this point in the history
Using implementation pointer as the hash can lead to situations where
queues consistently get assigned the same hash as a queue that has been
destroyed (due to being allocated at the same address). In some cases,
this can cause consistent hash collisions if a library uses these hashes
as a fast way to detect a different queue being passed to it.
  • Loading branch information
sergey-semenov authored Feb 21, 2024
1 parent 3031733 commit 6b5cb12
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 43 deletions.
7 changes: 2 additions & 5 deletions sycl/include/sycl/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2983,11 +2983,8 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> {
} // namespace sycl

namespace std {
template <> struct hash<sycl::queue> {
size_t operator()(const sycl::queue &Q) const {
return std::hash<std::shared_ptr<sycl::detail::queue_impl>>()(
sycl::detail::getSyclObjImpl(Q));
}
template <> struct __SYCL_EXPORT hash<sycl::queue> {
size_t operator()(const sycl::queue &Q) const;
};
} // namespace std

Expand Down
3 changes: 3 additions & 0 deletions sycl/source/ld-version-script.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
__sycl_register_lib;
__sycl_unregister_lib;

/* Export std::hash specializations */
_ZNKSt4hashIN4sycl3_V15queueEEclERKS2_;

local:
*;
};
7 changes: 7 additions & 0 deletions sycl/source/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,10 @@ void queue::ext_oneapi_set_external_event(const event &external_event) {

} // namespace _V1
} // namespace sycl

size_t std::hash<sycl::queue>::operator()(const sycl::queue &Q) const {
// Compared to using the impl pointer, the unique ID helps avoid hash
// collisions with previously destroyed queues.
return std::hash<unsigned long long>()(
sycl::detail::getSyclObjImpl(Q)->getQueueID());
}
1 change: 1 addition & 0 deletions sycl/test/abi/sycl_symbols_linux.dump
Original file line number Diff line number Diff line change
Expand Up @@ -4792,5 +4792,6 @@ _ZNK4sycl3_V19exception4codeEv
_ZNK4sycl3_V19exception4whatEv
_ZNK4sycl3_V19exception8categoryEv
_ZNK4sycl3_V19kernel_id8get_nameEv
_ZNKSt4hashIN4sycl3_V15queueEEclERKS2_
__sycl_register_lib
__sycl_unregister_lib
79 changes: 41 additions & 38 deletions sycl/test/abi/sycl_symbols_windows.dump
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@
??4?$OwnerLessBase@Vqueue@_V1@sycl@@@detail@_V1@sycl@@QEAAAEAV0123@AEBV0123@@Z
??4?$OwnerLessBase@Vstream@_V1@sycl@@@detail@_V1@sycl@@QEAAAEAV0123@$$QEAV0123@@Z
??4?$OwnerLessBase@Vstream@_V1@sycl@@@detail@_V1@sycl@@QEAAAEAV0123@AEBV0123@@Z
??4?$hash@Vqueue@_V1@sycl@@@std@@QEAAAEAU01@$$QEAU01@@Z
??4?$hash@Vqueue@_V1@sycl@@@std@@QEAAAEAU01@AEBU01@@Z
??4AccessorBaseHost@detail@_V1@sycl@@QEAAAEAV0123@$$QEAV0123@@Z
??4AccessorBaseHost@detail@_V1@sycl@@QEAAAEAV0123@AEBV0123@@Z
??4AccessorImplHost@detail@_V1@sycl@@QEAAAEAV0123@AEBV0123@@Z
Expand Down Expand Up @@ -810,6 +812,7 @@
??Fhalf@host_half_impl@detail@_V1@sycl@@QEAA?AV01234@H@Z
??Fhalf@host_half_impl@detail@_V1@sycl@@QEAAAEAV01234@XZ
??Ghalf@host_half_impl@detail@_V1@sycl@@QEAAAEAV01234@XZ
??R?$hash@Vqueue@_V1@sycl@@@std@@QEBA_KAEBVqueue@_V1@sycl@@@Z
??Raccelerator_selector@_V1@sycl@@UEBAHAEBVdevice@12@@Z
??Rcpu_selector@_V1@sycl@@UEBAHAEBVdevice@12@@Z
??Rdefault_selector@_V1@sycl@@UEBAHAEBVdevice@12@@Z
Expand Down Expand Up @@ -1011,6 +1014,7 @@
?ext_codeplay_supports_fusion@queue@_V1@sycl@@QEBA_NXZ
?ext_intel_read_host_pipe@handler@_V1@sycl@@AEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAX_K_N@Z
?ext_intel_write_host_pipe@handler@_V1@sycl@@AEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAX_K_N@Z
?ext_oneapi_advise_usm_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAU_pi_ext_command_buffer@@PEBX_KW4_pi_mem_advice@@V?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_architecture_is@device@_V1@sycl@@QEAA_NW4architecture@experimental@oneapi@ext@23@@Z
?ext_oneapi_barrier@handler@_V1@sycl@@QEAAXAEBV?$vector@Vevent@_V1@sycl@@V?$allocator@Vevent@_V1@sycl@@@std@@@std@@@Z
?ext_oneapi_barrier@handler@_V1@sycl@@QEAAXXZ
Expand Down Expand Up @@ -1043,8 +1047,6 @@
?ext_oneapi_copyD2H_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAU_pi_ext_command_buffer@@PEAVSYCLMemObjI@234@PEAXIV?$range@$02@34@4V?$id@$02@34@IPEADI45IV?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_copyH2D_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAU_pi_ext_command_buffer@@PEAVSYCLMemObjI@234@PEADIV?$range@$02@34@V?$id@$02@34@IPEAXI445IV?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_copy_usm_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEBXPEAU_pi_ext_command_buffer@@_KPEAXV?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_prefetch_usm_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAU_pi_ext_command_buffer@@PEAX_KV?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_advise_usm_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAU_pi_ext_command_buffer@@PEBX_KW4_pi_mem_advice@@V?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_disable_peer_access@device@_V1@sycl@@QEAAXAEBV123@@Z
?ext_oneapi_empty@queue@_V1@sycl@@QEBA_NXZ
?ext_oneapi_enable_peer_access@device@_V1@sycl@@QEAAXAEBV123@@Z
Expand All @@ -1053,9 +1055,9 @@
?ext_oneapi_fill_usm_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAU_pi_ext_command_buffer@@PEAX_KHV?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_get_composite_devices@platform@_V1@sycl@@QEBA?AV?$vector@Vdevice@_V1@sycl@@V?$allocator@Vdevice@_V1@sycl@@@std@@@std@@XZ
?ext_oneapi_get_default_context@platform@_V1@sycl@@QEBA?AVcontext@23@XZ
?ext_oneapi_get_graph@queue@_V1@sycl@@QEBA?AV?$command_graph@$0A@@experimental@oneapi@ext@23@XZ
?ext_oneapi_get_kernel@kernel_bundle_plain@detail@_V1@sycl@@QEAA?AVkernel@34@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z
?ext_oneapi_get_last_event@queue@_V1@sycl@@QEBA?AVevent@23@XZ
?ext_oneapi_get_graph@queue@_V1@sycl@@QEBA?AV?$command_graph@$0A@@experimental@oneapi@ext@23@XZ
?ext_oneapi_get_state@queue@_V1@sycl@@QEBA?AW4queue_state@experimental@oneapi@ext@23@XZ
?ext_oneapi_graph@handler@_V1@sycl@@QEAAXV?$command_graph@$00@experimental@oneapi@ext@23@@Z
?ext_oneapi_graph@queue@_V1@sycl@@QEAA?AVevent@23@V?$command_graph@$00@experimental@oneapi@ext@23@AEBUcode_location@detail@23@@Z
Expand All @@ -1080,6 +1082,7 @@
?ext_oneapi_owner_before@?$OwnerLessBase@Vqueue@_V1@sycl@@@detail@_V1@sycl@@QEBA_NAEBVqueue@34@@Z
?ext_oneapi_owner_before@?$OwnerLessBase@Vstream@_V1@sycl@@@detail@_V1@sycl@@QEBA_NAEBV?$weak_object_base@Vstream@_V1@sycl@@@2oneapi@ext@34@@Z
?ext_oneapi_owner_before@?$OwnerLessBase@Vstream@_V1@sycl@@@detail@_V1@sycl@@QEBA_NAEBVstream@34@@Z
?ext_oneapi_prefetch_usm_cmd_buffer@MemoryManager@detail@_V1@sycl@@SAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAU_pi_ext_command_buffer@@PEAX_KV?$vector@IV?$allocator@I@std@@@6@PEAI@Z
?ext_oneapi_set_external_event@queue@_V1@sycl@@QEAAXAEBVevent@23@@Z
?ext_oneapi_signal_external_semaphore@handler@_V1@sycl@@QEAAXUinterop_semaphore_handle@experimental@oneapi@ext@23@@Z
?ext_oneapi_signal_external_semaphore@queue@_V1@sycl@@QEAA?AVevent@23@Uinterop_semaphore_handle@experimental@oneapi@ext@23@AEBUcode_location@detail@23@@Z
Expand Down Expand Up @@ -1278,38 +1281,38 @@
?get_max_statement_size@stream@_V1@sycl@@QEBA_KXZ
?get_max_statement_size@stream_impl@detail@_V1@sycl@@QEBA_KXZ
?get_mip_level_mem_handle@experimental@oneapi@ext@_V1@sycl@@YA?AUimage_mem_handle@12345@U612345@IAEBVdevice@45@AEBVcontext@45@@Z
?get_mip_level_mem_handle@experimental@oneapi@ext@_V1@sycl@@YA?AUimage_mem_handle@12345@U612345@IAEBVqueue@45@@Z
?get_mip_level_mem_handle@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBA?AUimage_mem_handle@23456@I@Z
?get_name@kernel_id@_V1@sycl@@QEBAPEBDXZ
?get_node_from_event@node@experimental@oneapi@ext@_V1@sycl@@SA?AV123456@Vevent@56@@Z
?get_nodes@modifiable_command_graph@detail@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_num_channels@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBAIXZ
?get_pipe_name@pipe_base@experimental@intel@ext@_V1@sycl@@KA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEBX@Z
?get_pitch@image_impl@detail@_V1@sycl@@QEBA?AV?$range@$01@34@XZ
?get_mip_level_mem_handle@experimental@oneapi@ext@_V1@sycl@@YA?AUimage_mem_handle@12345@U612345@IAEBVqueue@45@@Z
?get_mip_level_mem_handle@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBA?AUimage_mem_handle@23456@I@Z
?get_name@kernel_id@_V1@sycl@@QEBAPEBDXZ
?get_node_from_event@node@experimental@oneapi@ext@_V1@sycl@@SA?AV123456@Vevent@56@@Z
?get_nodes@modifiable_command_graph@detail@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_num_channels@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBAIXZ
?get_pipe_name@pipe_base@experimental@intel@ext@_V1@sycl@@KA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEBX@Z
?get_pitch@image_impl@detail@_V1@sycl@@QEBA?AV?$range@$01@34@XZ
?get_pitch@image_plain@detail@_V1@sycl@@IEBA?AV?$range@$01@34@XZ
?get_platform@context@_V1@sycl@@QEBA?AVplatform@23@XZ
?get_platform@device@_V1@sycl@@QEBA?AVplatform@23@XZ
?get_platforms@platform@_V1@sycl@@SA?AV?$vector@Vplatform@_V1@sycl@@V?$allocator@Vplatform@_V1@sycl@@@std@@@std@@XZ
?get_pointer_device@_V1@sycl@@YA?AVdevice@12@PEBXAEBVcontext@12@@Z
?get_pointer_type@_V1@sycl@@YA?AW4alloc@usm@12@PEBXAEBVcontext@12@@Z
?get_precision@stream@_V1@sycl@@QEBA_KXZ
?get_predecessors@node@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_queue@fusion_wrapper@experimental@codeplay@ext@_V1@sycl@@QEBA?AVqueue@56@XZ
?get_range@image_impl@detail@_V1@sycl@@QEBA?AV?$range@$02@34@XZ
?get_range@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$range@$02@56@XZ
?get_range@image_plain@detail@_V1@sycl@@IEBA?AV?$range@$02@34@XZ
?get_root_nodes@modifiable_command_graph@detail@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_size@image_plain@detail@_V1@sycl@@IEBA_KXZ
?get_size@stream@_V1@sycl@@QEBA_KXZ
?get_size@stream_impl@detail@_V1@sycl@@QEBA_KXZ
?get_specialization_constant_impl@kernel_bundle_plain@detail@_V1@sycl@@IEBAXPEBDPEAX@Z
?get_stream_mode@stream@_V1@sycl@@QEBA?AW4stream_manipulator@23@XZ
?get_successors@node@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_type@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBA?AW4image_type@23456@XZ
?get_type@node@experimental@oneapi@ext@_V1@sycl@@QEBA?AW4node_type@23456@XZ
?get_wait_list@event@_V1@sycl@@QEAA?AV?$vector@Vevent@_V1@sycl@@V?$allocator@Vevent@_V1@sycl@@@std@@@std@@XZ
?get_width@stream@_V1@sycl@@QEBA_KXZ
?get_work_item_buffer_size@stream@_V1@sycl@@QEBA_KXZ
?get_pointer_device@_V1@sycl@@YA?AVdevice@12@PEBXAEBVcontext@12@@Z
?get_pointer_type@_V1@sycl@@YA?AW4alloc@usm@12@PEBXAEBVcontext@12@@Z
?get_precision@stream@_V1@sycl@@QEBA_KXZ
?get_predecessors@node@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_queue@fusion_wrapper@experimental@codeplay@ext@_V1@sycl@@QEBA?AVqueue@56@XZ
?get_range@image_impl@detail@_V1@sycl@@QEBA?AV?$range@$02@34@XZ
?get_range@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$range@$02@56@XZ
?get_range@image_plain@detail@_V1@sycl@@IEBA?AV?$range@$02@34@XZ
?get_root_nodes@modifiable_command_graph@detail@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_size@image_plain@detail@_V1@sycl@@IEBA_KXZ
?get_size@stream@_V1@sycl@@QEBA_KXZ
?get_size@stream_impl@detail@_V1@sycl@@QEBA_KXZ
?get_specialization_constant_impl@kernel_bundle_plain@detail@_V1@sycl@@IEBAXPEBDPEAX@Z
?get_stream_mode@stream@_V1@sycl@@QEBA?AW4stream_manipulator@23@XZ
?get_successors@node@experimental@oneapi@ext@_V1@sycl@@QEBA?AV?$vector@Vnode@experimental@oneapi@ext@_V1@sycl@@V?$allocator@Vnode@experimental@oneapi@ext@_V1@sycl@@@std@@@std@@XZ
?get_type@image_mem@experimental@oneapi@ext@_V1@sycl@@QEBA?AW4image_type@23456@XZ
?get_type@node@experimental@oneapi@ext@_V1@sycl@@QEBA?AW4node_type@23456@XZ
?get_wait_list@event@_V1@sycl@@QEAA?AV?$vector@Vevent@_V1@sycl@@V?$allocator@Vevent@_V1@sycl@@@std@@@std@@XZ
?get_width@stream@_V1@sycl@@QEBA_KXZ
?get_work_item_buffer_size@stream@_V1@sycl@@QEBA_KXZ
?get_work_item_buffer_size@stream_impl@detail@_V1@sycl@@QEBA_KXZ
?gpu_selector_v@_V1@sycl@@YAHAEBVdevice@12@@Z
?handleHostData@SYCLMemObjT@detail@_V1@sycl@@QEAAXAEBV?$function@$$A6AXPEAX@Z@std@@_K_N@Z
Expand Down Expand Up @@ -1493,13 +1496,13 @@
?setLocalAccessorArgHelper@handler@_V1@sycl@@AEAAXHAEAVLocalAccessorBaseHost@detail@23@@Z
?setPitches@image_impl@detail@_V1@sycl@@AEAAXAEBV?$range@$01@34@@Z
?setPitches@image_impl@detail@_V1@sycl@@AEAAXXZ
?setStateExplicitKernelBundle@handler@_V1@sycl@@AEAAXXZ
?setStateSpecConstSet@handler@_V1@sycl@@AEAAXXZ
?setType@handler@_V1@sycl@@AEAAXW4CGTYPE@CG@detail@23@@Z
?setUserFacingNodeType@handler@_V1@sycl@@AEAAXW4node_type@experimental@oneapi@ext@23@@Z
?set_final_data@SYCLMemObjT@detail@_V1@sycl@@QEAAX$$T@Z
?set_final_data@SYCLMemObjT@detail@_V1@sycl@@QEAAXAEBV?$function@$$A6AXAEBV?$function@$$A6AXPEAX@Z@std@@@Z@std@@@Z
?set_final_data_from_storage@SYCLMemObjT@detail@_V1@sycl@@QEAAXXZ
?setStateExplicitKernelBundle@handler@_V1@sycl@@AEAAXXZ
?setStateSpecConstSet@handler@_V1@sycl@@AEAAXXZ
?setType@handler@_V1@sycl@@AEAAXW4CGTYPE@CG@detail@23@@Z
?setUserFacingNodeType@handler@_V1@sycl@@AEAAXW4node_type@experimental@oneapi@ext@23@@Z
?set_final_data@SYCLMemObjT@detail@_V1@sycl@@QEAAX$$T@Z
?set_final_data@SYCLMemObjT@detail@_V1@sycl@@QEAAXAEBV?$function@$$A6AXAEBV?$function@$$A6AXPEAX@Z@std@@@Z@std@@@Z
?set_final_data_from_storage@SYCLMemObjT@detail@_V1@sycl@@QEAAXXZ
?set_final_data_internal@buffer_plain@detail@_V1@sycl@@IEAAXAEBV?$function@$$A6AXAEBV?$function@$$A6AXPEAX@Z@std@@@Z@std@@@Z
?set_final_data_internal@buffer_plain@detail@_V1@sycl@@IEAAXXZ
?set_final_data_internal@image_plain@detail@_V1@sycl@@IEAAXAEBV?$function@$$A6AXAEBV?$function@$$A6AXPEAX@Z@std@@@Z@std@@@Z
Expand Down
1 change: 1 addition & 0 deletions sycl/unittests/queue/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_sycl_unittest(QueueTests OBJECT
DeviceCheck.cpp
EventClear.cpp
Hash.cpp
USM.cpp
Wait.cpp
GetProfilingInfo.cpp
Expand Down
14 changes: 14 additions & 0 deletions sycl/unittests/queue/Hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <detail/queue_impl.hpp>
#include <gtest/gtest.h>
#include <helpers/PiMock.hpp>
#include <sycl/queue.hpp>

using namespace sycl;

// Checks that the queue hash uses its unique ID.
TEST(QueueHash, QueueHashUsesID) {
unittest::PiMock Mock;
queue Q;
unsigned long long ID = detail::getSyclObjImpl(Q)->getQueueID();
ASSERT_EQ(std::hash<unsigned long long>{}(ID), std::hash<queue>{}(Q));
}

0 comments on commit 6b5cb12

Please sign in to comment.