From fe98a1916083b7b7ad3ecaa660be2333f57adfc1 Mon Sep 17 00:00:00 2001 From: Zhiming Wang Date: Wed, 12 Nov 2025 16:04:52 +0800 Subject: [PATCH 1/3] [SYCL][Graph] try to opt duplicateNodes() function. --- sycl/source/detail/graph/graph_impl.cpp | 6 ++++++ sycl/source/detail/graph/graph_impl.hpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/sycl/source/detail/graph/graph_impl.cpp b/sycl/source/detail/graph/graph_impl.cpp index 28b66d573fa61..28c2b840a3b0f 100644 --- a/sycl/source/detail/graph/graph_impl.cpp +++ b/sycl/source/detail/graph/graph_impl.cpp @@ -1266,6 +1266,11 @@ exec_graph_impl::enqueue(sycl::detail::queue_impl &Queue, } void exec_graph_impl::duplicateNodes() { + if (MGraphImpl->IsLinearRecorded()) { + MNodeStorage = MGraphImpl->MNodeStorage; + return; + } + // Map of original modifiable nodes (keys) to new duplicated nodes (values) std::unordered_map NodesMap; nodes_range ModifiableNodes{MGraphImpl->MNodeStorage}; @@ -1974,6 +1979,7 @@ void modifiable_command_graph::begin_recording( } void modifiable_command_graph::end_recording() { + impl->endRecording(); impl->clearQueues(true /*Needs lock*/); } diff --git a/sycl/source/detail/graph/graph_impl.hpp b/sycl/source/detail/graph/graph_impl.hpp index d35b271493ed0..77d46393908cb 100644 --- a/sycl/source/detail/graph/graph_impl.hpp +++ b/sycl/source/detail/graph/graph_impl.hpp @@ -472,6 +472,9 @@ class graph_impl : public std::enable_shared_from_this { /// of recording queues associated with this graph. /// @param[in] Queue The queue to be recorded from. void beginRecording(sycl::detail::queue_impl &Queue); + void endRecording(); + + bool IsLinearRecorded() const { return MIsLinearRecorded; } /// Store the last barrier node that was submitted to the queue. /// @param[in] Queue The queue the barrier was recorded from. @@ -600,6 +603,9 @@ class graph_impl : public std::enable_shared_from_this { // The number of live executable graphs that have been created from this // modifiable graph std::atomic MExecGraphCount = 0; + + // True if the graph is recorded from a single in-order queue + bool MIsLinearRecorded = false; }; /// Class representing the implementation of command_graph. From 4c37e248747cc8ef0b0873cd62ea2940d78392ca Mon Sep 17 00:00:00 2001 From: Zhiming Wang Date: Thu, 13 Nov 2025 16:43:55 +0800 Subject: [PATCH 2/3] Fix failed lit test case --- sycl/source/detail/graph/graph_impl.cpp | 2 +- sycl/source/detail/graph/graph_impl.hpp | 6 +++++- sycl/source/handler.cpp | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/sycl/source/detail/graph/graph_impl.cpp b/sycl/source/detail/graph/graph_impl.cpp index 28c2b840a3b0f..c75a593f44ada 100644 --- a/sycl/source/detail/graph/graph_impl.cpp +++ b/sycl/source/detail/graph/graph_impl.cpp @@ -1266,7 +1266,7 @@ exec_graph_impl::enqueue(sycl::detail::queue_impl &Queue, } void exec_graph_impl::duplicateNodes() { - if (MGraphImpl->IsLinearRecorded()) { + if (MGraphImpl->isLinearRecorded() && !MGraphImpl->hasSubGraph()) { MNodeStorage = MGraphImpl->MNodeStorage; return; } diff --git a/sycl/source/detail/graph/graph_impl.hpp b/sycl/source/detail/graph/graph_impl.hpp index 77d46393908cb..9ced72197a1ec 100644 --- a/sycl/source/detail/graph/graph_impl.hpp +++ b/sycl/source/detail/graph/graph_impl.hpp @@ -474,7 +474,9 @@ class graph_impl : public std::enable_shared_from_this { void beginRecording(sycl::detail::queue_impl &Queue); void endRecording(); - bool IsLinearRecorded() const { return MIsLinearRecorded; } + bool isLinearRecorded() const { return MIsLinearRecorded; } + void setHasSubGraph() { MOriginGraphHasSubGraph = true; } + bool hasSubGraph() const { return MOriginGraphHasSubGraph; } /// Store the last barrier node that was submitted to the queue. /// @param[in] Queue The queue the barrier was recorded from. @@ -606,6 +608,8 @@ class graph_impl : public std::enable_shared_from_this { // True if the graph is recorded from a single in-order queue bool MIsLinearRecorded = false; + // True if the graph contains subgraph when constructed by recording + bool MOriginGraphHasSubGraph = false; }; /// Class representing the implementation of command_graph. diff --git a/sycl/source/handler.cpp b/sycl/source/handler.cpp index 8fd04c79cf224..a3e81d665dfa5 100644 --- a/sycl/source/handler.cpp +++ b/sycl/source/handler.cpp @@ -788,6 +788,7 @@ event handler::finalize() { ParentLock = ext::oneapi::experimental::detail::graph_impl::WriteLock( ParentGraph->MMutex); } + ParentGraph->setHasSubGraph(); impl->CGData.MRequirements = impl->MExecGraph->getRequirements(); // Here we are using the CommandGroup without passing a CommandBuffer to // pass the exec_graph_impl and event dependencies. Since this subgraph CG From ad1ad1ffbd5fd229a286f076860f5d448607c272 Mon Sep 17 00:00:00 2001 From: Zhiming Wang Date: Tue, 18 Nov 2025 10:40:40 +0800 Subject: [PATCH 3/3] Address review comments --- sycl/source/detail/graph/graph_impl.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sycl/source/detail/graph/graph_impl.cpp b/sycl/source/detail/graph/graph_impl.cpp index c75a593f44ada..d03d5d4e42bf7 100644 --- a/sycl/source/detail/graph/graph_impl.cpp +++ b/sycl/source/detail/graph/graph_impl.cpp @@ -564,6 +564,7 @@ void graph_impl::removeQueue(sycl::detail::queue_impl &RecordingQueue) { MRecordingQueues.erase(RecordingQueue.weak_from_this()); } + void graph_impl::clearQueues(bool NeedsLock) { graph_impl::RecQueuesStorage SwappedQueues; { @@ -709,6 +710,14 @@ void graph_impl::beginRecording(sycl::detail::queue_impl &Queue) { addQueue(Queue); } } +void graph_impl::endRecording() { + // Collect attribute(s) for recorded Graph + if (MRecordingQueues.size() == 1) { + if (auto ValidQueue = MRecordingQueues.begin()->lock(); ValidQueue) { + MIsLinearRecorded = ValidQueue->isInOrder(); + } + } +} // Check if nodes do not require enqueueing and if so loop back through // predecessors until we find the real dependency.