[llvm] [libsycl] Initial prefetch implementation for queue (PR #212104)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 26 02:08:46 PDT 2026


https://github.com/Robertkq created https://github.com/llvm/llvm-project/pull/212104

Adds the `prefetch` API for `queue` class as per SYCL 20202 specifications.
Adds `unit tests` for prefetch use cases with the appropriate mock additions

I believe that in the future when we have `sycl::handler` we should modify queue's prefetch to submit it via a handler

>From 0734e18047da229babb2fdaeeb9098bfa0711138 Mon Sep 17 00:00:00 2001
From: Robertkq <robertvuia06 at gmail.com>
Date: Sat, 25 Jul 2026 14:35:09 +0300
Subject: [PATCH 1/2] Initial implementation of prefetch for queue

---
 libsycl/include/sycl/__impl/queue.hpp  | 35 ++++++++++++++++++++++++++
 libsycl/src/detail/queue_impl.cpp      | 27 ++++++++++++++++++++
 libsycl/src/detail/queue_impl.hpp      |  9 +++++++
 libsycl/src/queue.cpp                  |  8 ++++++
 libsycl/unittests/queue/CMakeLists.txt |  1 +
 libsycl/unittests/queue/prefetch.cpp   | 15 +++++++++++
 6 files changed, 95 insertions(+)
 create mode 100644 libsycl/unittests/queue/prefetch.cpp

diff --git a/libsycl/include/sycl/__impl/queue.hpp b/libsycl/include/sycl/__impl/queue.hpp
index 32b057104a165..30cc89720a8e3 100644
--- a/libsycl/include/sycl/__impl/queue.hpp
+++ b/libsycl/include/sycl/__impl/queue.hpp
@@ -413,6 +413,41 @@ class _LIBSYCL_EXPORT queue {
   event memcpy(void *dest, const void *src, std::size_t numBytes,
                const std::vector<event> &depEvents);
 
+  /// Provides hints to the runtime library that data should be made available
+  /// on a device earlier than Unified Shared Memory would normally require it
+  /// to be available.
+  ///
+  /// \param ptr is a USM pointer to the memory to be prefetched to the device.
+  /// \param numBytes is a number of bytes to be prefetched.
+  /// \return an event representing prefetch operation.
+  event prefetch(void *ptr, std::size_t numBytes) {
+    return prefetch(ptr, numBytes, std::vector<event>{});
+  }
+
+  /// Provides hints to the runtime library that data should be made available
+  /// on a device earlier than Unified Shared Memory would normally require it
+  /// to be available.
+  ///
+  /// \param ptr is a USM pointer to the memory to be prefetched to the device.
+  /// \param numBytes is a number of bytes to be prefetched.
+  /// \param depEvent is an event that specifies the kernel dependencies.
+  /// \return an event representing prefetch operation.
+  event prefetch(void *ptr, std::size_t numBytes, event depEvent) {
+    return prefetch(ptr, numBytes, std::vector<event>{depEvent});
+  }
+
+  /// Provides hints to the runtime library that data should be made available
+  /// on a device earlier than Unified Shared Memory would normally require it
+  /// to be available.
+  ///
+  /// \param ptr is a USM pointer to the memory to be prefetched to the device.
+  /// \param numBytes is a number of bytes to be prefetched.
+  /// \param depEvents is a vector of events that specify the kernel
+  /// dependencies.
+  /// \return an event representing prefetch operation.
+  event prefetch(void *ptr, std::size_t numBytes,
+                 const std::vector<event> &depEvents);
+
 private:
   template <typename KernelName, int Dims, template <int> class Range,
             typename... Rest>
diff --git a/libsycl/src/detail/queue_impl.cpp b/libsycl/src/detail/queue_impl.cpp
index 6116e2d787e6f..c81db257e22b2 100644
--- a/libsycl/src/detail/queue_impl.cpp
+++ b/libsycl/src/detail/queue_impl.cpp
@@ -199,6 +199,33 @@ QueueImpl::memcpy(void *Dest, const void *Src, std::size_t NumBytes,
   return createEvent();
 }
 
+EventImplPtr QueueImpl::prefetch(void *Ptr, std::size_t NumBytes,
+                                 const std::vector<EventImplPtr> &DepEvents) {
+  checkEventsPlatformMatch(DepEvents, MDevice.getPlatformImpl());
+
+  if (NumBytes == 0) {
+    handleEventDependencies(DepEvents);
+    return createEvent();
+  }
+  if (!Ptr) {
+    throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
+                          "Nullptr argument in prefetch operation");
+  }
+
+  auto [TargetDevice, IsHost] = getAllocDevice(Ptr);
+
+  std::size_t Count = 1;
+  const void *Mems[] = {Ptr};
+  const std::size_t Sizes[] = {NumBytes};
+
+  ol_mem_migration_flags_t Flag = OL_MEM_MIGRATION_FLAG_HOST_TO_DEVICE;
+
+  handleEventDependencies(DepEvents);
+  callAndThrow(olMemPrefetch, MOffloadQueue, Count, Mems, Sizes, Flag);
+
+  return nullptr;
+}
+
 void QueueImpl::handleEventDependencies(const std::vector<EventImplPtr> &Deps) {
   // TODO: liboffload supports only in-order queues and no cross context waiting
   // is available now that means that this code is excessive but correct. I
diff --git a/libsycl/src/detail/queue_impl.hpp b/libsycl/src/detail/queue_impl.hpp
index e062546de5c5f..c889a189a7ff4 100644
--- a/libsycl/src/detail/queue_impl.hpp
+++ b/libsycl/src/detail/queue_impl.hpp
@@ -117,6 +117,15 @@ class QueueImpl : public std::enable_shared_from_this<QueueImpl> {
   EventImplPtr memcpy(void *Dest, const void *Src, std::size_t NumBytes,
                       const std::vector<EventImplPtr> &DepEvents);
 
+  /// Submits a prefetch operation for a USM pointer.
+  ///
+  /// \param Ptr is a USM pointer to the memory to be prefetched to the device.
+  /// \param NumBytes is a number of bytes to be prefetched.
+  /// \param DepEvents is a vector of dependencies for the operation.
+  /// \return an event impl object that represents the status of the operation.
+  EventImplPtr prefetch(void *Ptr, std::size_t NumBytes,
+                        const std::vector<EventImplPtr> &DepEvents);
+
 private:
   void handleEventDependencies(const std::vector<EventImplPtr> &Dep);
   EventImplPtr createEvent(std::vector<EventImplPtr> &&Deps = {});
diff --git a/libsycl/src/queue.cpp b/libsycl/src/queue.cpp
index 6d15537636936..c257a09367f3f 100644
--- a/libsycl/src/queue.cpp
+++ b/libsycl/src/queue.cpp
@@ -47,6 +47,14 @@ event queue::memcpy(void *dest, const void *src, std::size_t numBytes,
   return detail::createSyclObjFromImpl<event>(EventImplPtr);
 }
 
+event queue::prefetch(void *ptr, std::size_t numBytes,
+                      const std::vector<event> &depEvents) {
+  std::shared_ptr<detail::EventImpl> EventImplPtr =
+      impl->prefetch(ptr, numBytes, detail::getSyclObjImpls(depEvents));
+  assert(EventImplPtr);
+  return detail::createSyclObjFromImpl<event>(EventImplPtr);
+}
+
 event queue::getLastEvent() {
   return detail::createSyclObjFromImpl<event>(impl->getLastEvent());
 }
diff --git a/libsycl/unittests/queue/CMakeLists.txt b/libsycl/unittests/queue/CMakeLists.txt
index ea98c6d0d48fe..418fd1789ca76 100644
--- a/libsycl/unittests/queue/CMakeLists.txt
+++ b/libsycl/unittests/queue/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_sycl_unittest(QueueTests
+    prefetch.cpp
     memcpy.cpp
     queue.cpp
     sycl_kernel_launch.cpp
diff --git a/libsycl/unittests/queue/prefetch.cpp b/libsycl/unittests/queue/prefetch.cpp
new file mode 100644
index 0000000000000..4068c165a3734
--- /dev/null
+++ b/libsycl/unittests/queue/prefetch.cpp
@@ -0,0 +1,15 @@
+#include <mock/helpers.hpp>
+
+#include <sycl/__impl/device.hpp>
+#include <sycl/__impl/queue.hpp>
+
+#include <detail/device_impl.hpp>
+#include <detail/queue_impl.hpp>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace sycl;
+using namespace ::testing;
+
+TEST(Queue, Prefetch) {}
\ No newline at end of file

>From 6dc384857e9e7c49f708c6840c45dea82604a474 Mon Sep 17 00:00:00 2001
From: Robertkq <robertvuia06 at gmail.com>
Date: Sat, 25 Jul 2026 23:32:40 +0300
Subject: [PATCH 2/2] Switch between appropriate flag & introduce unit tests
 for normal use case and no-op case

---
 libsycl/src/detail/queue_impl.cpp    |  6 ++--
 libsycl/unittests/mock/helpers.hpp   |  3 ++
 libsycl/unittests/mock/mock.cpp      |  7 +++++
 libsycl/unittests/queue/prefetch.cpp | 47 +++++++++++++++++++++++++++-
 4 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/libsycl/src/detail/queue_impl.cpp b/libsycl/src/detail/queue_impl.cpp
index c81db257e22b2..b90c9211b3076 100644
--- a/libsycl/src/detail/queue_impl.cpp
+++ b/libsycl/src/detail/queue_impl.cpp
@@ -218,12 +218,14 @@ EventImplPtr QueueImpl::prefetch(void *Ptr, std::size_t NumBytes,
   const void *Mems[] = {Ptr};
   const std::size_t Sizes[] = {NumBytes};
 
-  ol_mem_migration_flags_t Flag = OL_MEM_MIGRATION_FLAG_HOST_TO_DEVICE;
+  ol_mem_migration_flags_t Flag = (MDevice.isCPU())
+                                      ? OL_MEM_MIGRATION_FLAG_DEVICE_TO_HOST
+                                      : OL_MEM_MIGRATION_FLAG_HOST_TO_DEVICE;
 
   handleEventDependencies(DepEvents);
   callAndThrow(olMemPrefetch, MOffloadQueue, Count, Mems, Sizes, Flag);
 
-  return nullptr;
+  return createEvent();
 }
 
 void QueueImpl::handleEventDependencies(const std::vector<EventImplPtr> &Deps) {
diff --git a/libsycl/unittests/mock/helpers.hpp b/libsycl/unittests/mock/helpers.hpp
index 0690970f1f99b..a1700616fdca7 100644
--- a/libsycl/unittests/mock/helpers.hpp
+++ b/libsycl/unittests/mock/helpers.hpp
@@ -121,6 +121,9 @@ class MockLiboffload {
               (ol_queue_handle_t Queue, void *DstPtr,
                ol_device_handle_t DstDevice, const void *SrcPtr,
                ol_device_handle_t SrcDevice, size_t Size));
+  MOCK_METHOD(ol_result_t, olMemPrefetch,
+              (ol_queue_handle_t Queue, size_t Count, const void **Mems,
+               const size_t *Sizes, ol_mem_migration_flags_t Flags));
   MOCK_METHOD(ol_result_t, olGetMemInfo,
               (const void *Ptr, ol_mem_info_t PropName, size_t PropSize,
                void *PropValue));
diff --git a/libsycl/unittests/mock/mock.cpp b/libsycl/unittests/mock/mock.cpp
index 8f5db4b5cb6ec..cde713b685d27 100644
--- a/libsycl/unittests/mock/mock.cpp
+++ b/libsycl/unittests/mock/mock.cpp
@@ -106,6 +106,13 @@ ol_result_t olMemcpy(ol_queue_handle_t Queue, void *DstPtr,
                                             SrcDevice, Size);
 }
 
+ol_result_t olMemPrefetch(ol_queue_handle_t Queue, size_t Count,
+                          const void **Mems, const size_t *Sizes,
+                          ol_mem_migration_flags_t Flags) {
+  return mock::getMockLiboffload().olMemPrefetch(Queue, Count, Mems, Sizes,
+                                                 Flags);
+}
+
 ol_result_t olGetMemInfo(const void *Ptr, ol_mem_info_t PropName,
                          size_t PropSize, void *PropValue) {
   return mock::getMockLiboffload().olGetMemInfo(Ptr, PropName, PropSize,
diff --git a/libsycl/unittests/queue/prefetch.cpp b/libsycl/unittests/queue/prefetch.cpp
index 4068c165a3734..025271ac6980d 100644
--- a/libsycl/unittests/queue/prefetch.cpp
+++ b/libsycl/unittests/queue/prefetch.cpp
@@ -12,4 +12,49 @@
 using namespace sycl;
 using namespace ::testing;
 
-TEST(Queue, Prefetch) {}
\ No newline at end of file
+TEST(Queue, TwoPrefetches) {
+  constexpr std::size_t NumBytes = 1024;
+  constexpr int NPrefetches = 2;
+
+  mock::MockWrapper Mock;
+  queue Q;
+
+  void *Ptr = reinterpret_cast<void *>(1);
+
+  bool IsCpu = Q.get_device().is_cpu();
+  ol_mem_migration_flags_t ExpectedFlag =
+      IsCpu ? OL_MEM_MIGRATION_FLAG_DEVICE_TO_HOST
+            : OL_MEM_MIGRATION_FLAG_HOST_TO_DEVICE;
+
+  EXPECT_CALL(Mock.get(), olMemPrefetch(_, 1, _, _, ExpectedFlag))
+      .Times(NPrefetches)
+      .WillRepeatedly([&](ol_queue_handle_t Queue, size_t Count,
+                          const void **Mems, const size_t *Sizes,
+                          ol_mem_migration_flags_t Flags) -> ol_result_t {
+        EXPECT_NE(Queue, nullptr);
+        EXPECT_EQ(Count, 1u);
+        EXPECT_EQ(Mems[0], Ptr);
+        EXPECT_EQ(Sizes[0], NumBytes);
+        EXPECT_EQ(Flags, ExpectedFlag);
+        return OL_SUCCESS;
+      });
+
+  EXPECT_CALL(Mock.get(), olCreateEvent(_, _, _)).Times(NPrefetches);
+
+  event Event = Q.prefetch(Ptr, NumBytes);
+
+  // second prefetch depeends on the first one
+  EXPECT_CALL(Mock.get(), olWaitEvents(_, _, 1));
+  Q.prefetch(Ptr, NumBytes, Event);
+}
+
+TEST(Queue, PrefetchZeroBytes) {
+  mock::MockWrapper Mock;
+  queue Q;
+
+  EXPECT_CALL(Mock.get(), olMemPrefetch(_, _, _, _, _)).Times(0);
+  EXPECT_CALL(Mock.get(), olWaitEvents(_, _, 1)).Times(1);
+
+  event Event = Q.prefetch(nullptr, 0);
+  Q.prefetch(nullptr, 0, Event);
+}



More information about the llvm-commits mailing list