[llvm] [OFFLOAD] Add a check before calling dataExchange (PR #176853)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 19 21:50:43 PST 2026


https://github.com/fineg74 updated https://github.com/llvm/llvm-project/pull/176853

>From 11eb123bb9821a1886d9aa39be76842e2767db70 Mon Sep 17 00:00:00 2001
From: "Fine, Gregory" <gregory.fine at intel.com>
Date: Mon, 19 Jan 2026 20:21:50 -0800
Subject: [PATCH 1/5] Add a check before calling dataExchange

---
 offload/liboffload/src/OffloadImpl.cpp | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 4a4f6652054a1..1cc0294ccecb7 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1006,9 +1006,29 @@ Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
             DstDevice->Device->dataSubmit(DstPtr, SrcPtr, Size, QueueImpl))
       return Res;
   } else {
-    if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, *DstDevice->Device,
-                                                   DstPtr, Size, QueueImpl))
+    if (SrcDevice->Platform.Plugin == DstDevice->Platform.Plugin &&
+      SrcDevice->Platform.Plugin->is_data_exchangable(
+        SrcDevice->Device->getDeviceId(), DstDevice->Device->getDeviceId())) {
+      if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, 
+                                                  *DstDevice->Device,
+                                                  DstPtr, Size, QueueImpl))
+        return Res;
+    } else {
+      void *Buffer = malloc(Size);
+      ol_queue_handle_t Queue;
+      olCreateQueue(SrcDevice, &Queue);
+      Error Res = SrcDevice->Device->dataRetrieve(Buffer, SrcPtr, Size, Queue->AsyncInfo);
+      olDestroyQueue(Queue);
+
+      if (!Res) {
+        ol_queue_handle_t Queue;
+        olCreateQueue(DstDevice, &Queue);
+        Res = DstDevice->Device->dataSubmit(DstPtr, Buffer, Size, Queue->AsyncInfo);
+        olDestroyQueue(Queue);
+      }
+      free(Buffer);
       return Res;
+    }
   }
 
   return Error::success();

>From 527042d20a39d86baa713d649d58341bf58e5595 Mon Sep 17 00:00:00 2001
From: "Fine, Gregory" <gregory.fine at intel.com>
Date: Mon, 19 Jan 2026 20:35:21 -0800
Subject: [PATCH 2/5] Use correct API

---
 offload/liboffload/src/OffloadImpl.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 1cc0294ccecb7..6766f6f1e0768 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1007,7 +1007,7 @@ Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
       return Res;
   } else {
     if (SrcDevice->Platform.Plugin == DstDevice->Platform.Plugin &&
-      SrcDevice->Platform.Plugin->is_data_exchangable(
+      SrcDevice->Platform.Plugin->isDataExchangable(
         SrcDevice->Device->getDeviceId(), DstDevice->Device->getDeviceId())) {
       if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, 
                                                   *DstDevice->Device,

>From ec43290b687845638aad7e4019b1bd69b155f308 Mon Sep 17 00:00:00 2001
From: "Fine, Gregory" <gregory.fine at intel.com>
Date: Mon, 19 Jan 2026 20:41:05 -0800
Subject: [PATCH 3/5] Fix formatting

---
 offload/liboffload/src/OffloadImpl.cpp | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 6766f6f1e0768..936c132a011a8 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1007,23 +1007,25 @@ Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
       return Res;
   } else {
     if (SrcDevice->Platform.Plugin == DstDevice->Platform.Plugin &&
-      SrcDevice->Platform.Plugin->isDataExchangable(
-        SrcDevice->Device->getDeviceId(), DstDevice->Device->getDeviceId())) {
-      if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, 
-                                                  *DstDevice->Device,
-                                                  DstPtr, Size, QueueImpl))
+        SrcDevice->Platform.Plugin->isDataExchangable(
+            SrcDevice->Device->getDeviceId(),
+            DstDevice->Device->getDeviceId())) {
+      if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, *DstDevice->Device,
+                                                     DstPtr, Size, QueueImpl))
         return Res;
     } else {
       void *Buffer = malloc(Size);
       ol_queue_handle_t Queue;
       olCreateQueue(SrcDevice, &Queue);
-      Error Res = SrcDevice->Device->dataRetrieve(Buffer, SrcPtr, Size, Queue->AsyncInfo);
+      Error Res = SrcDevice->Device->dataRetrieve(Buffer, SrcPtr, Size,
+                                                  Queue->AsyncInfo);
       olDestroyQueue(Queue);
 
       if (!Res) {
         ol_queue_handle_t Queue;
         olCreateQueue(DstDevice, &Queue);
-        Res = DstDevice->Device->dataSubmit(DstPtr, Buffer, Size, Queue->AsyncInfo);
+        Res = DstDevice->Device->dataSubmit(DstPtr, Buffer, Size,
+                                            Queue->AsyncInfo);
         olDestroyQueue(Queue);
       }
       free(Buffer);

>From e23c60922c61fd8056c8c9899111cd46863c7943 Mon Sep 17 00:00:00 2001
From: "Fine, Gregory" <gregory.fine at intel.com>
Date: Mon, 19 Jan 2026 21:44:39 -0800
Subject: [PATCH 4/5] Address PR comments

---
 offload/liboffload/src/OffloadImpl.cpp | 42 +++++++++++---------------
 1 file changed, 18 insertions(+), 24 deletions(-)

diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 936c132a011a8..49f0e4b515aa8 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1005,32 +1005,26 @@ Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
     if (auto Res =
             DstDevice->Device->dataSubmit(DstPtr, SrcPtr, Size, QueueImpl))
       return Res;
-  } else {
-    if (SrcDevice->Platform.Plugin == DstDevice->Platform.Plugin &&
-        SrcDevice->Platform.Plugin->isDataExchangable(
-            SrcDevice->Device->getDeviceId(),
-            DstDevice->Device->getDeviceId())) {
-      if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, *DstDevice->Device,
-                                                     DstPtr, Size, QueueImpl))
-        return Res;
-    } else {
-      void *Buffer = malloc(Size);
-      ol_queue_handle_t Queue;
-      olCreateQueue(SrcDevice, &Queue);
-      Error Res = SrcDevice->Device->dataRetrieve(Buffer, SrcPtr, Size,
-                                                  Queue->AsyncInfo);
-      olDestroyQueue(Queue);
-
-      if (!Res) {
-        ol_queue_handle_t Queue;
-        olCreateQueue(DstDevice, &Queue);
-        Res = DstDevice->Device->dataSubmit(DstPtr, Buffer, Size,
-                                            Queue->AsyncInfo);
-        olDestroyQueue(Queue);
-      }
-      free(Buffer);
+  } else if (SrcDevice->Platform.Plugin == DstDevice->Platform.Plugin &&
+             SrcDevice->Platform.Plugin->isDataExchangable(
+                SrcDevice->Device->getDeviceId(),
+                DstDevice->Device->getDeviceId())) {
+    if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, *DstDevice->Device,
+                                                   DstPtr, Size, QueueImpl))
       return Res;
+  } else {
+    if (Queue)
+      olSyncQueue(Queue);
+
+    void *Buffer = malloc(Size);
+    Error Res = SrcDevice->Device->dataRetrieve(Buffer, SrcPtr, Size,
+                                                nullptr);
+    if (!Res) {
+      Res = DstDevice->Device->dataSubmit(DstPtr, Buffer, Size,
+                                         nullptr);
     }
+    free(Buffer);
+    return Res;
   }
 
   return Error::success();

>From 58e93e8482864ae0dd59e14f57068e32f03e8f77 Mon Sep 17 00:00:00 2001
From: "Fine, Gregory" <gregory.fine at intel.com>
Date: Mon, 19 Jan 2026 21:50:32 -0800
Subject: [PATCH 5/5] Fix formatting

---
 offload/liboffload/src/OffloadImpl.cpp | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 49f0e4b515aa8..8f6119bbf763b 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1007,8 +1007,8 @@ Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
       return Res;
   } else if (SrcDevice->Platform.Plugin == DstDevice->Platform.Plugin &&
              SrcDevice->Platform.Plugin->isDataExchangable(
-                SrcDevice->Device->getDeviceId(),
-                DstDevice->Device->getDeviceId())) {
+                 SrcDevice->Device->getDeviceId(),
+                 DstDevice->Device->getDeviceId())) {
     if (auto Res = SrcDevice->Device->dataExchange(SrcPtr, *DstDevice->Device,
                                                    DstPtr, Size, QueueImpl))
       return Res;
@@ -1017,11 +1017,9 @@ Error olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
       olSyncQueue(Queue);
 
     void *Buffer = malloc(Size);
-    Error Res = SrcDevice->Device->dataRetrieve(Buffer, SrcPtr, Size,
-                                                nullptr);
+    Error Res = SrcDevice->Device->dataRetrieve(Buffer, SrcPtr, Size, nullptr);
     if (!Res) {
-      Res = DstDevice->Device->dataSubmit(DstPtr, Buffer, Size,
-                                         nullptr);
+      Res = DstDevice->Device->dataSubmit(DstPtr, Buffer, Size, nullptr);
     }
     free(Buffer);
     return Res;



More information about the llvm-commits mailing list