[llvm] [Offload] Do not do not re-initialize RPC server (PR #218757)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 20:56:05 PDT 2026


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/218757

>From 620a0f360c24dfcfb5d26e768a206f1db0e2d7ed Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 25 Aug 2026 14:46:58 -0500
Subject: [PATCH 1/2] [Offload] Do not do not re-initialize RPC server

Summary:
This was done for every executable that was loaded. In the current
OpenMP use this never caused issues, but we need to check if this was
already done so we don't leak the memory.
---
 offload/plugins-nextgen/common/src/RPC.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/offload/plugins-nextgen/common/src/RPC.cpp b/offload/plugins-nextgen/common/src/RPC.cpp
index 7c03c916058fb..35b7a88f00585 100644
--- a/offload/plugins-nextgen/common/src/RPC.cpp
+++ b/offload/plugins-nextgen/common/src/RPC.cpp
@@ -207,6 +207,12 @@ RPCServerTy::isDeviceUsingRPC(plugin::GenericDeviceTy &Device,
 Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
                               plugin::GenericGlobalHandlerTy &Handler,
                               plugin::DeviceImageTy &Image) {
+  {
+    std::lock_guard<decltype(BufferMutex)> Lock(BufferMutex);
+    if (Buffers[Device.getDeviceId()])
+      return Error::success();
+  }
+
   uint64_t NumPorts =
       std::min(Device.requestedRPCPortCount(), rpc::MAX_PORT_COUNT);
   auto RPCBufferOrErr = Device.allocate(

>From 5381fcb879e765344e53e015b077913d0d322d73 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 25 Aug 2026 22:55:54 -0500
Subject: [PATCH 2/2] safer lock

---
 offload/plugins-nextgen/common/src/RPC.cpp | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/offload/plugins-nextgen/common/src/RPC.cpp b/offload/plugins-nextgen/common/src/RPC.cpp
index 35b7a88f00585..cbc97e59cf395 100644
--- a/offload/plugins-nextgen/common/src/RPC.cpp
+++ b/offload/plugins-nextgen/common/src/RPC.cpp
@@ -207,11 +207,9 @@ RPCServerTy::isDeviceUsingRPC(plugin::GenericDeviceTy &Device,
 Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
                               plugin::GenericGlobalHandlerTy &Handler,
                               plugin::DeviceImageTy &Image) {
-  {
-    std::lock_guard<decltype(BufferMutex)> Lock(BufferMutex);
-    if (Buffers[Device.getDeviceId()])
-      return Error::success();
-  }
+  std::lock_guard<decltype(BufferMutex)> Lock(BufferMutex);
+  if (Buffers[Device.getDeviceId()])
+    return Error::success();
 
   uint64_t NumPorts =
       std::min(Device.requestedRPCPortCount(), rpc::MAX_PORT_COUNT);
@@ -248,7 +246,6 @@ Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
   if (auto Err = Device.dataSubmit(ClientGlobal.getPtr(), &client,
                                    sizeof(rpc::Client), nullptr))
     return Err;
-  std::lock_guard<decltype(BufferMutex)> Lock(BufferMutex);
   Buffers[Device.getDeviceId()] = RPCBuffer;
   Devices[Device.getDeviceId()] = &Device;
 



More information about the llvm-commits mailing list