From 329c8e3bbdbe37c20172767141e08fda8f44f9c2 Mon Sep 17 00:00:00 2001 From: "Ptak, Slawomir" Date: Wed, 15 Jul 2026 10:06:15 +0000 Subject: [PATCH 1/2] [SYCL] Reusable Events limitation for cross-context dependencies. Currently, cross-context dependencies are not supported for the Reusable Events APIs. Cross-context dependencies use host tasks internally, so the event wait might be queued behind the host task in the runtime, which is currently not supported by the extension APIs. --- sycl/source/detail/queue_impl.cpp | 8 +++- sycl/source/reusable_events.cpp | 26 +++++++++++ sycl/unittests/Extensions/ReusableEvents.cpp | 46 -------------------- 3 files changed, 33 insertions(+), 47 deletions(-) diff --git a/sycl/source/detail/queue_impl.cpp b/sycl/source/detail/queue_impl.cpp index fbfadfc8c39e8..ac84c9491653e 100644 --- a/sycl/source/detail/queue_impl.cpp +++ b/sycl/source/detail/queue_impl.cpp @@ -615,11 +615,17 @@ EventImplPtr queue_impl::submit_barrier_direct_impl( /*SchedulerBypass*/ true}; } - if (EventForReuse) { + if (EventForReuse || !CallerNeedsEvent) { // Current limitation: reusable events require scheduler bypass so that // the barrier can be submitted directly to the backend with the reusable // event's handle as the output event. Scheduler bypass is not possible // when dependencies include host tasks or cross-context dependencies. + // + // This limitation applies to both: enqueue_signal_event and + // enqueue_wait_event(s). + // + // The !CallerNeedsEvent condition is used to detect the + // enqueue_wait_event(s) function calls. throw sycl::exception(sycl::make_error_code(errc::invalid), "An event cannot be enqueued for signaling behind " "a command which is not enqueued in the backend."); diff --git a/sycl/source/reusable_events.cpp b/sycl/source/reusable_events.cpp index 642edd4e298d8..2cf719051cdb7 100644 --- a/sycl/source/reusable_events.cpp +++ b/sycl/source/reusable_events.cpp @@ -60,6 +60,18 @@ __SYCL_EXPORT sycl::event make_event(const sycl::context &ctxt, __SYCL_EXPORT void enqueue_wait_event(sycl::queue q, const event &evt) { detail::queue_impl &QueueImpl = *sycl::detail::getSyclObjImpl(q); + detail::event_impl &EventImpl = *sycl::detail::getSyclObjImpl(evt); + + // Current limitation: + // The queue and an event need to be in the same context. The reason + // is, that cross-context dependencies use host tasks, and the wait + // command might be queued in the runtime. This flow is currently + // not supported by the Reusable Events APIs. + if (&QueueImpl.getContextImpl() != &EventImpl.getContextImpl()) { + throw sycl::exception( + sycl::make_error_code(errc::invalid), + "A queue and an event need to be in the same context."); + } QueueImpl.submit_barrier_direct_without_event( sycl::span(&evt, 1), detail::CGType::BarrierWaitlist, @@ -70,6 +82,20 @@ __SYCL_EXPORT void enqueue_wait_events(sycl::queue q, const std::vector &evts) { detail::queue_impl &QueueImpl = *sycl::detail::getSyclObjImpl(q); + // Current limitation: + // The queue and all the events need to be in the same context. The + // reason is, that cross-context dependencies use host tasks, and the + // wait command might be queued in the runtime. This flow is currently + // not supported by the Reusable Events APIs. + for (sycl::event evt : evts) { + detail::event_impl &EventImpl = *sycl::detail::getSyclObjImpl(evt); + if (&QueueImpl.getContextImpl() != &EventImpl.getContextImpl()) { + throw sycl::exception( + sycl::make_error_code(errc::invalid), + "A queue and all the events need to be in the same context."); + } + } + QueueImpl.submit_barrier_direct_without_event( evts, detail::CGType::BarrierWaitlist, detail::code_location::current()); } diff --git a/sycl/unittests/Extensions/ReusableEvents.cpp b/sycl/unittests/Extensions/ReusableEvents.cpp index f572f25e8fcba..f3fbb7164956d 100644 --- a/sycl/unittests/Extensions/ReusableEvents.cpp +++ b/sycl/unittests/Extensions/ReusableEvents.cpp @@ -443,52 +443,6 @@ TEST_F(ReusableEventsTest, EventInDependsOn) { Queue.wait(); } -// Cross-context events with wait -TEST_F(ReusableEventsTest, CrossContextEventWait) { - mock::getCallbacks().set_replace_callback("urDeviceGet", - &redefinedUrDeviceGet); - mock::getCallbacks().set_replace_callback("urDeviceRelease", - &redefinedUrDeviceRelease); - - sycl::platform Plt = sycl::platform(); - auto Devices = Plt.get_devices(); - - if (Devices.size() < 2) { - GTEST_SKIP() << "Need at least 2 devices for this test"; - } - - const sycl::device Dev1 = Devices[0]; - const sycl::device Dev2 = Devices[1]; - sycl::context Ctx1{Dev1}; - sycl::context Ctx2{Dev2}; - - sycl::queue Queue1{Ctx1, Dev1}; - sycl::queue Queue2{Ctx2, Dev2}; - - auto event = syclex::make_event(Ctx1); - - mock::getCallbacks().set_replace_callback( - "urEnqueueEventsWaitWithBarrierExt", - &redefinedUrEnqueueEventsWaitWithBarrierExt_signal); - - syclex::enqueue_signal_event(Queue1, event); - - EXPECT_EQ(RedefinedUrEnqueueEventsWaitWithBarrierExt_signal_counter, 1); - - ExpectedNumEventsInWaitList = 1; - mock::getCallbacks().set_replace_callback( - "urEnqueueEventsWaitWithBarrierExt", - &redefinedUrEnqueueEventsWaitWithBarrierExt_wait); - - // Event from different context should still work with enqueue_wait_event - EXPECT_NO_THROW({ syclex::enqueue_wait_event(Queue2, event); }); - - EXPECT_EQ(RedefinedUrEnqueueEventsWaitWithBarrierExt_wait_counter, 1); - - Queue1.wait(); - Queue2.wait(); -} - // Cross-context make_event and signal event - not allowed TEST_F(ReusableEventsTest, CrossContextMakeEventSignalEvent) { mock::getCallbacks().set_replace_callback("urDeviceGet", From f8fb98a77858194b3c52d6fbf8b529f100179bb0 Mon Sep 17 00:00:00 2001 From: "Ptak, Slawomir" Date: Fri, 17 Jul 2026 09:47:30 +0000 Subject: [PATCH 2/2] Update the unit tests to check for the exception, change the exception messages. --- sycl/source/reusable_events.cpp | 5 +- sycl/unittests/Extensions/ReusableEvents.cpp | 93 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/sycl/source/reusable_events.cpp b/sycl/source/reusable_events.cpp index 2cf719051cdb7..c103ba0637312 100644 --- a/sycl/source/reusable_events.cpp +++ b/sycl/source/reusable_events.cpp @@ -70,7 +70,7 @@ __SYCL_EXPORT void enqueue_wait_event(sycl::queue q, const event &evt) { if (&QueueImpl.getContextImpl() != &EventImpl.getContextImpl()) { throw sycl::exception( sycl::make_error_code(errc::invalid), - "A queue and an event need to be in the same context."); + "Not implemented yet. Event context must match the queue context."); } QueueImpl.submit_barrier_direct_without_event( @@ -92,7 +92,8 @@ __SYCL_EXPORT void enqueue_wait_events(sycl::queue q, if (&QueueImpl.getContextImpl() != &EventImpl.getContextImpl()) { throw sycl::exception( sycl::make_error_code(errc::invalid), - "A queue and all the events need to be in the same context."); + "Not implemented yet. Context of all events must match the " + "queue context."); } } diff --git a/sycl/unittests/Extensions/ReusableEvents.cpp b/sycl/unittests/Extensions/ReusableEvents.cpp index f3fbb7164956d..828176517660d 100644 --- a/sycl/unittests/Extensions/ReusableEvents.cpp +++ b/sycl/unittests/Extensions/ReusableEvents.cpp @@ -443,6 +443,99 @@ TEST_F(ReusableEventsTest, EventInDependsOn) { Queue.wait(); } +// Cross-context event with wait. +// Current limitation is that the cross-context wait +// is not supported. +TEST_F(ReusableEventsTest, CrossContextEventWait) { + mock::getCallbacks().set_replace_callback("urDeviceGet", + &redefinedUrDeviceGet); + mock::getCallbacks().set_replace_callback("urDeviceRelease", + &redefinedUrDeviceRelease); + + sycl::platform Plt = sycl::platform(); + auto Devices = Plt.get_devices(); + + if (Devices.size() < 2) { + GTEST_SKIP() << "Need at least 2 devices for this test"; + } + + const sycl::device Dev1 = Devices[0]; + const sycl::device Dev2 = Devices[1]; + sycl::context Ctx1{Dev1}; + sycl::context Ctx2{Dev2}; + + sycl::queue Queue1{Ctx1, Dev1}; + sycl::queue Queue2{Ctx2, Dev2}; + + auto event = syclex::make_event(Ctx1); + + bool exception = false; + + // Current limiation is that an event from different context + // cannot be used with enqueue_wait_event + try { + syclex::enqueue_wait_event(Queue2, event); + } catch (sycl::exception const &e) { + exception = true; + EXPECT_EQ(e.code(), sycl::errc::invalid); + EXPECT_STREQ(e.what(), "Not implemented yet. Event context must " + "match the queue context."); + } + + EXPECT_TRUE(exception); + + Queue1.wait(); + Queue2.wait(); +} + +// Cross-context events with wait (multiple events). +// Current limitation is that the cross-context wait +// is not supported. +TEST_F(ReusableEventsTest, CrossContextEventsWait) { + mock::getCallbacks().set_replace_callback("urDeviceGet", + &redefinedUrDeviceGet); + mock::getCallbacks().set_replace_callback("urDeviceRelease", + &redefinedUrDeviceRelease); + + sycl::platform Plt = sycl::platform(); + auto Devices = Plt.get_devices(); + + if (Devices.size() < 2) { + GTEST_SKIP() << "Need at least 2 devices for this test"; + } + + const sycl::device Dev1 = Devices[0]; + const sycl::device Dev2 = Devices[1]; + sycl::context Ctx1{Dev1}; + sycl::context Ctx2{Dev2}; + + sycl::queue Queue1{Ctx1, Dev1}; + sycl::queue Queue2{Ctx2, Dev2}; + + auto event1 = syclex::make_event(Ctx1); + auto event2 = syclex::make_event(Ctx1); + + std::vector events{event1, event2}; + + bool exception = false; + + // Current limiation is that an event from different context + // cannot be used with enqueue_wait_event + try { + syclex::enqueue_wait_events(Queue2, events); + } catch (sycl::exception const &e) { + exception = true; + EXPECT_EQ(e.code(), sycl::errc::invalid); + EXPECT_STREQ(e.what(), "Not implemented yet. Context of all events must " + "match the queue context."); + } + + EXPECT_TRUE(exception); + + Queue1.wait(); + Queue2.wait(); +} + // Cross-context make_event and signal event - not allowed TEST_F(ReusableEventsTest, CrossContextMakeEventSignalEvent) { mock::getCallbacks().set_replace_callback("urDeviceGet",