Skip to content

Commit bc4046e

Browse files
committed
[libc] Make RPC server handling header only
Summary: This patch moves the RPC server handling to be a header only utility stored in the `shared/` directory. This is intended to be shared within LLVM for the loaders and `offload/` handling. Generally, this makes it easier to share code without weird cross-project binaries being plucked out of the build system. It also allows us to soon move the loader interface out of the `libc` project so that we don't need to bootstrap those and can build them in LLVM.
1 parent f13d583 commit bc4046e

File tree

12 files changed

+71
-87
lines changed

12 files changed

+71
-87
lines changed

libc/docs/gpu/rpc.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ but the following example shows how it can be used by a standard user.
184184
185185
#include <shared/rpc.h>
186186
#include <shared/rpc_opcodes.h>
187+
#include <shared/rpc_server.h>
187188
188189
[[noreturn]] void handle_error(cudaError_t err) {
189190
fprintf(stderr, "CUDA error: %s\n", cudaGetErrorString(err));
@@ -230,10 +231,10 @@ but the following example shows how it can be used by a standard user.
230231
// Requires non-blocking CUDA kernels but avoids a separate thread.
231232
do {
232233
auto port = server.try_open(warp_size, /*index=*/0);
233-
// From libllvmlibc_rpc_server.a in the installation.
234234
if (!port)
235235
continue;
236236
237+
// Only available in-tree from the 'libc' sources.
237238
handle_libc_opcodes(*port, warp_size);
238239
port->close();
239240
} while (cudaStreamQuery(stream) == cudaErrorNotReady);
@@ -242,14 +243,16 @@ but the following example shows how it can be used by a standard user.
242243
The above code must be compiled in CUDA's relocatable device code mode and with
243244
the advanced offloading driver to link in the library. Currently this can be
244245
done with the following invocation. Using LTO avoids the overhead normally
245-
associated with relocatable device code linking. The C library for GPUs is
246-
linked in by forwarding the static library to the device-side link job.
246+
associated with relocatable device code linking. The C library for GPU's
247+
handling is included through the ``shared/`` directory. This is not currently
248+
installed as it does not use a stable interface.
249+
247250

248251
.. code-block:: sh
249252
250253
$> clang++ -x cuda rpc.cpp --offload-arch=native -fgpu-rdc -lcudart \
251-
-I<install-path>include -L<install-path>/lib -lllvmlibc_rpc_server \
252-
-Xoffload-linker -lc -O3 -foffload-lto -o hello
254+
-I<install-path>include -L<install-path>/lib -Xoffload-linker -lc \
255+
-O3 -foffload-lto -o hello
253256
$> ./hello
254257
Hello world!
255258

libc/shared/rpc_opcodes.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,4 @@ typedef enum {
5050

5151
#undef LLVM_LIBC_OPCODE
5252

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-
5953
#endif // LLVM_LIBC_SHARED_RPC_OPCODES_H

libc/utils/gpu/server/rpc_server.cpp renamed to libc/shared/rpc_server.h

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1-
//===-- Shared memory RPC server instantiation ------------------*- C++ -*-===//
1+
//===-- RPC server handling -----------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef LLVM_LIBC_SHARED_RPC_SERVER_H
10+
#define LLVM_LIBC_SHARED_RPC_SERVER_H
11+
912
// Workaround for missing __has_builtin in < GCC 10.
1013
#ifndef __has_builtin
1114
#define __has_builtin(x) 0
1215
#endif
1316

17+
// Configs for using the LLVM libc writer interface.
18+
#define LIBC_COPT_USE_C_ASSERT
19+
#define LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY
20+
#define LIBC_COPT_ARRAY_ARG_LIST
21+
#define LIBC_COPT_PRINTF_DISABLE_WRITE_INT
22+
#define LIBC_COPT_PRINTF_DISABLE_INDEX_MODE
23+
#define LIBC_COPT_PRINTF_DISABLE_STRERROR
24+
25+
// The 'long double' type is 8 byte
26+
#define LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
27+
1428
#include "shared/rpc.h"
1529
#include "shared/rpc_opcodes.h"
1630

@@ -24,6 +38,7 @@
2438
#include <stdlib.h>
2539

2640
namespace LIBC_NAMESPACE {
41+
namespace internal {
2742

2843
// Minimal replacement for 'std::vector' that works for trivial types.
2944
template <typename T> class TempVector {
@@ -88,15 +103,15 @@ struct TempStorage {
88103
TempVector<char *> storage;
89104
};
90105

91-
enum Stream {
92-
File = 0,
93-
Stdin = 1,
94-
Stdout = 2,
95-
Stderr = 3,
96-
};
97-
98106
// Get the associated stream out of an encoded number.
99-
LIBC_INLINE ::FILE *to_stream(uintptr_t f) {
107+
static inline ::FILE *to_stream(uintptr_t f) {
108+
enum Stream {
109+
File = 0,
110+
Stdin = 1,
111+
Stdout = 2,
112+
Stderr = 3,
113+
};
114+
100115
::FILE *stream = reinterpret_cast<FILE *>(f & ~0x3ull);
101116
Stream type = static_cast<Stream>(f & 0x3ull);
102117
if (type == Stdin)
@@ -109,7 +124,8 @@ LIBC_INLINE ::FILE *to_stream(uintptr_t f) {
109124
}
110125

111126
template <bool packed, uint32_t num_lanes>
112-
static void handle_printf(rpc::Server::Port &port, TempStorage &temp_storage) {
127+
static inline void handle_printf(rpc::Server::Port &port,
128+
TempStorage &temp_storage) {
113129
FILE *files[num_lanes] = {nullptr};
114130
// Get the appropriate output stream to use.
115131
if (port.get_opcode() == LIBC_PRINTF_TO_STREAM ||
@@ -282,7 +298,7 @@ static void handle_printf(rpc::Server::Port &port, TempStorage &temp_storage) {
282298
}
283299

284300
template <uint32_t num_lanes>
285-
rpc::Status handle_port_impl(rpc::Server::Port &port) {
301+
static inline rpc::Status handle_port_impl(rpc::Server::Port &port) {
286302
TempStorage temp_storage;
287303

288304
switch (port.get_opcode()) {
@@ -498,21 +514,24 @@ rpc::Status handle_port_impl(rpc::Server::Port &port) {
498514
return rpc::RPC_SUCCESS;
499515
}
500516

517+
} // namespace internal
501518
} // namespace LIBC_NAMESPACE
502519

503520
namespace rpc {
504-
// The implementation of this function currently lives in the utility directory
505-
// at 'utils/gpu/server/rpc_server.cpp'.
506-
rpc::Status handle_libc_opcodes(rpc::Server::Port &port, uint32_t num_lanes) {
521+
// Handles any opcode generated from the 'libc' client code.
522+
static inline rpc::Status handle_libc_opcodes(rpc::Server::Port &port,
523+
uint32_t num_lanes) {
507524
switch (num_lanes) {
508525
case 1:
509-
return LIBC_NAMESPACE::handle_port_impl<1>(port);
526+
return LIBC_NAMESPACE::internal::handle_port_impl<1>(port);
510527
case 32:
511-
return LIBC_NAMESPACE::handle_port_impl<32>(port);
528+
return LIBC_NAMESPACE::internal::handle_port_impl<32>(port);
512529
case 64:
513-
return LIBC_NAMESPACE::handle_port_impl<64>(port);
530+
return LIBC_NAMESPACE::internal::handle_port_impl<64>(port);
514531
default:
515532
return rpc::RPC_ERROR;
516533
}
517534
}
518535
} // namespace rpc
536+
537+
#endif // LLVM_LIBC_SHARED_RPC_SERVER_H

libc/utils/gpu/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
add_subdirectory(server)
21
add_subdirectory(loader)

libc/utils/gpu/loader/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
add_library(gpu_loader OBJECT Main.cpp)
22

3+
include(FindLibcCommonUtils)
4+
target_link_libraries(gpu_loader PUBLIC llvm-libc-common-utilities)
5+
36
target_include_directories(gpu_loader PUBLIC
47
${CMAKE_CURRENT_SOURCE_DIR}
58
${LIBC_SOURCE_DIR}/include

libc/utils/gpu/loader/Loader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "shared/rpc.h"
1515
#include "shared/rpc_opcodes.h"
16+
#include "shared/rpc_server.h"
1617

1718
#include <cstddef>
1819
#include <cstdint>

libc/utils/gpu/loader/amdgpu/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,4 @@ set(LLVM_LINK_COMPONENTS
77
)
88

99
add_llvm_executable(amdhsa-loader amdhsa-loader.cpp)
10-
11-
target_link_libraries(amdhsa-loader
12-
PRIVATE
13-
hsa-runtime64::hsa-runtime64
14-
gpu_loader
15-
llvmlibc_rpc_server
16-
)
10+
target_link_libraries(amdhsa-loader PRIVATE hsa-runtime64::hsa-runtime64 gpu_loader)

libc/utils/gpu/loader/nvptx/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@ set(LLVM_LINK_COMPONENTS
66
)
77

88
add_llvm_executable(nvptx-loader nvptx-loader.cpp)
9-
10-
target_link_libraries(nvptx-loader
11-
PRIVATE
12-
gpu_loader
13-
llvmlibc_rpc_server
14-
CUDA::cuda_driver
15-
)
9+
target_link_libraries(nvptx-loader PRIVATE gpu_loader CUDA::cuda_driver)

libc/utils/gpu/server/CMakeLists.txt

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#===--------------------------------------------------------------------===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for details.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
#===--------------------------------------------------------------------===//
8+
9+
if(NOT TARGET llvm-libc-common-utilities)
10+
set(libc_path ${CMAKE_CURRENT_LIST_DIR}/../../../libc)
11+
if (EXISTS ${libc_path} AND IS_DIRECTORY ${libc_path})
12+
add_library(llvm-libc-common-utilities INTERFACE)
13+
# TODO: Reorganize the libc shared section so that it can be included without
14+
# adding the root "libc" directory to the include path.
15+
target_include_directories(llvm-libc-common-utilities INTERFACE ${libc_path})
16+
target_compile_definitions(llvm-libc-common-utilities INTERFACE LIBC_NAMESPACE=__llvm_libc_common_utils)
17+
target_compile_features(llvm-libc-common-utilities INTERFACE cxx_std_17)
18+
endif()
19+
endif()

0 commit comments

Comments
 (0)