Skip to content

Commit 1d810ec

Browse files
authored
[libc] Move libc server handlers to a shared header (#117908)
Summary: We can simply include this header from the shared directory now and do not need to have this level of indirection. Simply stash it with the other libc opcode handlers. If we were able to move the printf handlers to the shared directory then this could just be a header as well, which would HEAVILY simplify the mess associated with building the RPC server first in the projects build, then copying it to the runtimes build.
1 parent 89d8e70 commit 1d810ec

File tree

7 files changed

+27
-48
lines changed

7 files changed

+27
-48
lines changed

libc/shared/rpc_opcodes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_LIBC_SHARED_RPC_OPCODES_H
1010
#define LLVM_LIBC_SHARED_RPC_OPCODES_H
1111

12+
#include "rpc.h"
13+
1214
#define LLVM_LIBC_RPC_BASE 'c'
1315
#define LLVM_LIBC_OPCODE(n) (LLVM_LIBC_RPC_BASE << 24 | n)
1416

@@ -46,4 +48,12 @@ typedef enum {
4648
RPC_LAST = 0xFFFFFFFF,
4749
} rpc_opcode_t;
4850

51+
#undef LLVM_LIBC_OPCODE
52+
53+
namespace rpc {
54+
// The implementation of this function currently lives in the utility directory
55+
// at 'utils/gpu/server/rpc_server.cpp'.
56+
rpc::Status handle_libc_opcodes(rpc::Server::Port &port, uint32_t num_lanes);
57+
} // namespace rpc
58+
4959
#endif // LLVM_LIBC_SHARED_RPC_OPCODES_H

libc/utils/gpu/loader/Loader.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#ifndef LLVM_LIBC_UTILS_GPU_LOADER_LOADER_H
1010
#define LLVM_LIBC_UTILS_GPU_LOADER_LOADER_H
1111

12-
#include "utils/gpu/server/llvmlibc_rpc_server.h"
13-
1412
#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
1513

1614
#include "shared/rpc.h"
@@ -183,7 +181,7 @@ inline uint32_t handle_server(rpc::Server &server, uint32_t index,
183181
break;
184182
}
185183
default:
186-
status = libc_handle_rpc_port(&*port, num_lanes);
184+
status = handle_libc_opcodes(*port, num_lanes);
187185
break;
188186
}
189187

libc/utils/gpu/server/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ target_compile_definitions(llvmlibc_rpc_server PUBLIC
2323
LIBC_NAMESPACE=${LIBC_NAMESPACE})
2424

2525
# Install the server and associated header.
26-
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/llvmlibc_rpc_server.h
27-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
28-
COMPONENT libc-headers)
2926
install(TARGETS llvmlibc_rpc_server
3027
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}"
3128
COMPONENT libc)

libc/utils/gpu/server/llvmlibc_rpc_server.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include "shared/rpc.h"
1818
#include "shared/rpc_opcodes.h"
1919

20-
#include "llvmlibc_rpc_server.h"
21-
2220
#include "src/__support/arg_list.h"
2321
#include "src/stdio/printf_core/converter.h"
2422
#include "src/stdio/printf_core/parser.h"
@@ -445,15 +443,19 @@ rpc::Status handle_port_impl(rpc::Server::Port &port) {
445443
return rpc::SUCCESS;
446444
}
447445

448-
int libc_handle_rpc_port(void *port, uint32_t num_lanes) {
446+
namespace rpc {
447+
// The implementation of this function currently lives in the utility directory
448+
// at 'utils/gpu/server/rpc_server.cpp'.
449+
rpc::Status handle_libc_opcodes(rpc::Server::Port &port, uint32_t num_lanes) {
449450
switch (num_lanes) {
450451
case 1:
451-
return handle_port_impl<1>(*reinterpret_cast<rpc::Server::Port *>(port));
452+
return handle_port_impl<1>(port);
452453
case 32:
453-
return handle_port_impl<32>(*reinterpret_cast<rpc::Server::Port *>(port));
454+
return handle_port_impl<32>(port);
454455
case 64:
455-
return handle_port_impl<64>(*reinterpret_cast<rpc::Server::Port *>(port));
456+
return handle_port_impl<64>(port);
456457
default:
457458
return rpc::ERROR;
458459
}
459460
}
461+
} // namespace rpc

offload/plugins-nextgen/common/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ elseif(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
3232
target_link_libraries(PluginCommon PRIVATE ${llvmlibc_rpc_server})
3333
target_compile_definitions(PluginCommon PRIVATE LIBOMPTARGET_RPC_SUPPORT)
3434
# We may need to get the headers directly from the 'libc' source directory.
35-
target_include_directories(PluginCommon PRIVATE
36-
${CMAKE_SOURCE_DIR}/../libc/utils/gpu/server
37-
${CMAKE_SOURCE_DIR}/../libc/
38-
${CMAKE_SOURCE_DIR}/../libc/include)
35+
target_include_directories(PluginCommon PRIVATE ${CMAKE_SOURCE_DIR}/../libc/)
3936
endif()
4037
endif()
4138

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
// TODO: This should be included unconditionally and cleaned up.
1616
#if defined(LIBOMPTARGET_RPC_SUPPORT)
17-
#include "llvmlibc_rpc_server.h"
1817
#include "shared/rpc.h"
1918
#include "shared/rpc_opcodes.h"
2019
#endif
@@ -74,32 +73,32 @@ Error RPCServerTy::runServer(plugin::GenericDeviceTy &Device) {
7473
std::min(Device.requestedRPCPortCount(), rpc::MAX_PORT_COUNT);
7574
rpc::Server Server(NumPorts, Buffers[Device.getDeviceId()]);
7675

77-
auto port = Server.try_open(Device.getWarpSize());
78-
if (!port)
76+
auto Port = Server.try_open(Device.getWarpSize());
77+
if (!Port)
7978
return Error::success();
8079

8180
int Status = rpc::SUCCESS;
82-
switch (port->get_opcode()) {
81+
switch (Port->get_opcode()) {
8382
case RPC_MALLOC: {
84-
port->recv_and_send([&](rpc::Buffer *Buffer, uint32_t) {
83+
Port->recv_and_send([&](rpc::Buffer *Buffer, uint32_t) {
8584
Buffer->data[0] = reinterpret_cast<uintptr_t>(Device.allocate(
8685
Buffer->data[0], nullptr, TARGET_ALLOC_DEVICE_NON_BLOCKING));
8786
});
8887
break;
8988
}
9089
case RPC_FREE: {
91-
port->recv([&](rpc::Buffer *Buffer, uint32_t) {
90+
Port->recv([&](rpc::Buffer *Buffer, uint32_t) {
9291
Device.free(reinterpret_cast<void *>(Buffer->data[0]),
9392
TARGET_ALLOC_DEVICE_NON_BLOCKING);
9493
});
9594
break;
9695
}
9796
default:
9897
// Let the `libc` library handle any other unhandled opcodes.
99-
Status = libc_handle_rpc_port(&*port, Device.getWarpSize());
98+
Status = handle_libc_opcodes(*Port, Device.getWarpSize());
10099
break;
101100
}
102-
port->close();
101+
Port->close();
103102

104103
if (Status != rpc::SUCCESS)
105104
return createStringError("RPC server given invalid opcode!");

0 commit comments

Comments
 (0)