Skip to content

Commit 89d8e70

Browse files
authored
[libc] Export a pointer to the RPC client directly (#117913)
Summary: We currently have an unnecessary level of indirection when initializing the RPC client. This is a holdover from when the RPC client was not trivially copyable and simply makes it more complicated. Here we use the `asm` syntax to give the C++ variable a valid name so that we can just copy to it directly. Another advantage to this, is that if users want to piggy-back on the same RPC interface they need only declare theirs as extern with the same symbol name, or make it weak to optionally use it if LIBC isn't avaialb.e
1 parent 175051b commit 89d8e70

File tree

8 files changed

+20
-48
lines changed

8 files changed

+20
-48
lines changed

libc/src/__support/RPC/rpc_client.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@
1313
namespace LIBC_NAMESPACE_DECL {
1414
namespace rpc {
1515

16-
/// The libc client instance used to communicate with the server.
17-
Client client;
18-
19-
/// Externally visible symbol to signify the usage of an RPC client to
20-
/// whomever needs to run the server as well as provide a way to initialize
21-
/// the client with a copy..
22-
extern "C" [[gnu::visibility("protected")]] const void *__llvm_libc_rpc_client =
23-
&client;
16+
/// The libc client instance used to communicate with the server. Externally
17+
/// visible symbol to signify the usage of an RPC client to whomever needs to
18+
/// run the server as well as provide a way to initialize the client.
19+
[[gnu::visibility("protected")]] Client client;
2420

2521
} // namespace rpc
2622
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/RPC/rpc_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static_assert(cpp::is_trivially_copyable<Client>::value &&
2929
"The client is not trivially copyable from the server");
3030

3131
/// The libc client instance used to communicate with the server.
32-
extern Client client;
32+
[[gnu::visibility("protected")]] extern Client client asm("__llvm_rpc_client");
3333

3434
} // namespace rpc
3535
} // namespace LIBC_NAMESPACE_DECL

libc/utils/gpu/loader/amdgpu/amdhsa-loader.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -477,27 +477,15 @@ int load(int argc, const char **argv, const char **envp, void *image,
477477
// device's internal pointer.
478478
hsa_executable_symbol_t rpc_client_sym;
479479
if (hsa_status_t err = hsa_executable_get_symbol_by_name(
480-
executable, "__llvm_libc_rpc_client", &dev_agent, &rpc_client_sym))
480+
executable, "__llvm_rpc_client", &dev_agent, &rpc_client_sym))
481481
handle_error(err);
482482

483-
void *rpc_client_host;
484-
if (hsa_status_t err =
485-
hsa_amd_memory_pool_allocate(finegrained_pool, sizeof(void *),
486-
/*flags=*/0, &rpc_client_host))
487-
handle_error(err);
488-
hsa_amd_agents_allow_access(1, &dev_agent, nullptr, rpc_client_host);
489-
490483
void *rpc_client_dev;
491484
if (hsa_status_t err = hsa_executable_symbol_get_info(
492485
rpc_client_sym, HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ADDRESS,
493486
&rpc_client_dev))
494487
handle_error(err);
495488

496-
// Copy the address of the client buffer from the device to the host.
497-
if (hsa_status_t err = hsa_memcpy(rpc_client_host, host_agent, rpc_client_dev,
498-
dev_agent, sizeof(void *)))
499-
handle_error(err);
500-
501489
void *rpc_client_buffer;
502490
if (hsa_status_t err =
503491
hsa_amd_memory_lock(&client, sizeof(rpc::Client),
@@ -506,14 +494,12 @@ int load(int argc, const char **argv, const char **envp, void *image,
506494

507495
// Copy the RPC client buffer to the address pointed to by the symbol.
508496
if (hsa_status_t err =
509-
hsa_memcpy(*reinterpret_cast<void **>(rpc_client_host), dev_agent,
510-
rpc_client_buffer, host_agent, sizeof(rpc::Client)))
497+
hsa_memcpy(rpc_client_dev, dev_agent, rpc_client_buffer, host_agent,
498+
sizeof(rpc::Client)))
511499
handle_error(err);
512500

513501
if (hsa_status_t err = hsa_amd_memory_unlock(&client))
514502
handle_error(err);
515-
if (hsa_status_t err = hsa_amd_memory_pool_free(rpc_client_host))
516-
handle_error(err);
517503

518504
// Obtain the GPU's fixed-frequency clock rate and copy it to the GPU.
519505
// If the clock_freq symbol is missing, no work to do.

libc/utils/gpu/loader/nvptx/nvptx-loader.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,10 @@ int load(int argc, const char **argv, const char **envp, void *image,
314314
CUdeviceptr rpc_client_dev = 0;
315315
uint64_t client_ptr_size = sizeof(void *);
316316
if (CUresult err = cuModuleGetGlobal(&rpc_client_dev, &client_ptr_size,
317-
binary, "__llvm_libc_rpc_client"))
317+
binary, "__llvm_rpc_client"))
318318
handle_error(err);
319319

320-
CUdeviceptr rpc_client_host = 0;
321-
if (CUresult err =
322-
cuMemcpyDtoH(&rpc_client_host, rpc_client_dev, sizeof(void *)))
323-
handle_error(err);
324-
if (CUresult err =
325-
cuMemcpyHtoD(rpc_client_host, &client, sizeof(rpc::Client)))
320+
if (CUresult err = cuMemcpyHtoD(rpc_client_dev, &client, sizeof(rpc::Client)))
326321
handle_error(err);
327322

328323
LaunchParameters single_threaded_params = {1, 1, 1, 1, 1, 1};

llvm/lib/Transforms/IPO/OpenMPOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ struct OpenMPOpt {
15381538
// required an RPC server. If its users were all optimized out then we can
15391539
// safely remove it.
15401540
// TODO: This should be somewhere more common in the future.
1541-
if (GlobalVariable *GV = M.getNamedGlobal("__llvm_libc_rpc_client")) {
1541+
if (GlobalVariable *GV = M.getNamedGlobal("__llvm_rpc_client")) {
15421542
if (!GV->getType()->isPointerTy())
15431543
return false;
15441544

llvm/test/Transforms/OpenMP/keep_rpc_client.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
; RUN: opt -S -passes=openmp-opt < %s | FileCheck %s --check-prefix=PRELINK
44

55
@client = internal addrspace(1) global i64 zeroinitializer, align 8
6-
@__llvm_libc_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
6+
@__llvm_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
77

88
;.
99
; POSTLINK: @client = internal addrspace(1) global i64 0, align 8
10-
; POSTLINK: @__llvm_libc_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
10+
; POSTLINK: @__llvm_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
1111
;.
1212
; PRELINK: @client = internal addrspace(1) global i64 0, align 8
13-
; PRELINK: @__llvm_libc_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
13+
; PRELINK: @__llvm_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
1414
;.
1515
define i64 @a() {
1616
; POSTLINK-LABEL: define {{[^@]+}}@a

llvm/test/Transforms/OpenMP/remove_rpc_client.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
; RUN: opt -S -passes=openmp-opt < %s | FileCheck %s --check-prefix=PRELINK
44

55
@client = internal addrspace(1) global i32 zeroinitializer, align 8
6-
@__llvm_libc_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
6+
@__llvm_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
77

88
;.
99
; PRELINK: @client = internal addrspace(1) global i32 0, align 8
10-
; PRELINK: @__llvm_libc_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
10+
; PRELINK: @__llvm_rpc_client = protected local_unnamed_addr addrspace(1) global ptr addrspacecast (ptr addrspace(1) @client to ptr), align 8
1111
;.
1212
define void @a() {
1313
; POSTLINK-LABEL: define {{[^@]+}}@a() {

offload/plugins-nextgen/common/src/RPC.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ RPCServerTy::isDeviceUsingRPC(plugin::GenericDeviceTy &Device,
3131
plugin::GenericGlobalHandlerTy &Handler,
3232
plugin::DeviceImageTy &Image) {
3333
#ifdef LIBOMPTARGET_RPC_SUPPORT
34-
return Handler.isSymbolInImage(Device, Image, "__llvm_libc_rpc_client");
34+
return Handler.isSymbolInImage(Device, Image, "__llvm_rpc_client");
3535
#else
3636
return false;
3737
#endif
@@ -51,19 +51,14 @@ Error RPCServerTy::initDevice(plugin::GenericDeviceTy &Device,
5151
"Failed to initialize RPC server for device %d", Device.getDeviceId());
5252

5353
// Get the address of the RPC client from the device.
54-
void *ClientPtr;
55-
plugin::GlobalTy ClientGlobal("__llvm_libc_rpc_client", sizeof(void *));
54+
plugin::GlobalTy ClientGlobal("__llvm_rpc_client", sizeof(rpc::Client));
5655
if (auto Err =
5756
Handler.getGlobalMetadataFromDevice(Device, Image, ClientGlobal))
5857
return Err;
5958

60-
if (auto Err = Device.dataRetrieve(&ClientPtr, ClientGlobal.getPtr(),
61-
sizeof(void *), nullptr))
62-
return Err;
63-
6459
rpc::Client client(NumPorts, RPCBuffer);
65-
if (auto Err =
66-
Device.dataSubmit(ClientPtr, &client, sizeof(rpc::Client), nullptr))
60+
if (auto Err = Device.dataSubmit(ClientGlobal.getPtr(), &client,
61+
sizeof(rpc::Client), nullptr))
6762
return Err;
6863
Buffers[Device.getDeviceId()] = RPCBuffer;
6964

0 commit comments

Comments
 (0)