[llvm] [Offload] Do not do not re-initialize RPC server (PR #218757)
Joseph Huber via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 26 06:15:55 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/3] [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/3] 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;
>From 751979b31423e682c4f86714cc08e079e1d2f24e Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 26 Aug 2026 08:15:38 -0500
Subject: [PATCH 3/3] actually do it right
---
offload/plugins-nextgen/common/src/RPC.cpp | 58 +++++++++++-----------
1 file changed, 30 insertions(+), 28 deletions(-)
diff --git a/offload/plugins-nextgen/common/src/RPC.cpp b/offload/plugins-nextgen/common/src/RPC.cpp
index cbc97e59cf395..93076d221eda8 100644
--- a/offload/plugins-nextgen/common/src/RPC.cpp
+++ b/offload/plugins-nextgen/common/src/RPC.cpp
@@ -208,35 +208,39 @@ 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(
- rpc::Server::allocation_size(Device.getRPCNumLanes(), NumPorts), nullptr,
- TARGET_ALLOC_HOST);
- if (!RPCBufferOrErr)
- return RPCBufferOrErr.takeError();
-
- void *RPCBuffer = *RPCBufferOrErr;
- if (!RPCBuffer)
- return plugin::Plugin::error(
- error::ErrorCode::UNKNOWN,
- "failed to initialize RPC server for device %d", Device.getDeviceId());
-
- // The doorbell is used by AMDGPU targets to let the server thread be
- // descheduled. It is optional and will be ignored if the fields are null.
- rpc::Doorbell Doorbell{};
- if (auto Err = Device.Plugin.initRPCDoorbell(Doorbell.value, Doorbell.mailbox,
- Doorbell.event_id))
- return Err;
-
- auto *DoorbellPtr = reinterpret_cast<rpc::Doorbell *>(
- static_cast<uint8_t *>(RPCBuffer) + rpc::Server::doorbell_offset());
- std::memcpy(DoorbellPtr, &Doorbell, sizeof(rpc::Doorbell));
+ void *RPCBuffer = Buffers[Device.getDeviceId()];
+ if (!RPCBuffer) {
+ auto RPCBufferOrErr = Device.allocate(
+ rpc::Server::allocation_size(Device.getRPCNumLanes(), NumPorts),
+ nullptr, TARGET_ALLOC_HOST);
+ if (!RPCBufferOrErr)
+ return RPCBufferOrErr.takeError();
+
+ RPCBuffer = *RPCBufferOrErr;
+ if (!RPCBuffer)
+ return plugin::Plugin::error(
+ error::ErrorCode::UNKNOWN,
+ "failed to initialize RPC server for device %d",
+ Device.getDeviceId());
+
+ // The doorbell is used by AMDGPU targets to let the server thread be
+ // descheduled. It is optional and will be ignored if the fields are null.
+ rpc::Doorbell Doorbell{};
+ if (auto Err = Device.Plugin.initRPCDoorbell(
+ Doorbell.value, Doorbell.mailbox, Doorbell.event_id))
+ return Err;
+
+ auto *DoorbellPtr = reinterpret_cast<rpc::Doorbell *>(
+ static_cast<uint8_t *>(RPCBuffer) + rpc::Server::doorbell_offset());
+ std::memcpy(DoorbellPtr, &Doorbell, sizeof(rpc::Doorbell));
+
+ Buffers[Device.getDeviceId()] = RPCBuffer;
+ Devices[Device.getDeviceId()] = &Device;
+ }
- // Get the address of the RPC client from the device.
+ // Each image has its own client that must point at the shared buffer.
plugin::GlobalTy ClientGlobal("__llvm_rpc_client", sizeof(rpc::Client));
if (auto Err =
Handler.getGlobalMetadataFromDevice(Device, Image, ClientGlobal))
@@ -246,8 +250,6 @@ Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
if (auto Err = Device.dataSubmit(ClientGlobal.getPtr(), &client,
sizeof(rpc::Client), nullptr))
return Err;
- Buffers[Device.getDeviceId()] = RPCBuffer;
- Devices[Device.getDeviceId()] = &Device;
return Error::success();
}
More information about the llvm-commits
mailing list