Skip to content

Commit 7c63334

Browse files
committed
Revert "[libc] Make RPC server handling header only (llvm#131205)"
This reverts commit 8437b7f.
1 parent 313abfb commit 7c63334

File tree

14 files changed

+106
-110
lines changed

14 files changed

+106
-110
lines changed

libc/docs/gpu/rpc.rst

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ 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>
188187
189188
[[noreturn]] void handle_error(cudaError_t err) {
190189
fprintf(stderr, "CUDA error: %s\n", cudaGetErrorString(err));
@@ -231,10 +230,10 @@ but the following example shows how it can be used by a standard user.
231230
// Requires non-blocking CUDA kernels but avoids a separate thread.
232231
do {
233232
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.
238237
handle_libc_opcodes(*port, warp_size);
239238
port->close();
240239
} while (cudaStreamQuery(stream) == cudaErrorNotReady);
@@ -243,16 +242,14 @@ but the following example shows how it can be used by a standard user.
243242
The above code must be compiled in CUDA's relocatable device code mode and with
244243
the advanced offloading driver to link in the library. Currently this can be
245244
done with the following invocation. Using LTO avoids the overhead normally
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-
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.
250247

251248
.. code-block:: sh
252249
253250
$> clang++ -x cuda rpc.cpp --offload-arch=native -fgpu-rdc -lcudart \
254-
-I<install-path>include -L<install-path>/lib -Xoffload-linker -lc \
255-
-O3 -foffload-lto -o hello
251+
-I<install-path>include -L<install-path>/lib -lllvmlibc_rpc_server \
252+
-Xoffload-linker -lc -O3 -foffload-lto -o hello
256253
$> ./hello
257254
Hello world!
258255

libc/shared/rpc_opcodes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ 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+
5359
#endif // LLVM_LIBC_SHARED_RPC_OPCODES_H

libc/shared/rpc_server.h

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

libc/utils/gpu/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
add_subdirectory(server)
12
add_subdirectory(loader)

libc/utils/gpu/loader/CMakeLists.txt

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

3-
include(FindLibcCommonUtils)
4-
target_link_libraries(gpu_loader PUBLIC llvm-libc-common-utilities)
5-
63
target_include_directories(gpu_loader PUBLIC
74
${CMAKE_CURRENT_SOURCE_DIR}
85
${LIBC_SOURCE_DIR}/include

libc/utils/gpu/loader/Loader.h

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

1414
#include "shared/rpc.h"
1515
#include "shared/rpc_opcodes.h"
16-
#include "shared/rpc_server.h"
1716

1817
#include <cstddef>
1918
#include <cstdint>
@@ -182,7 +181,7 @@ inline uint32_t handle_server(rpc::Server &server, uint32_t index,
182181
break;
183182
}
184183
default:
185-
status = LIBC_NAMESPACE::shared::handle_libc_opcodes(*port, num_lanes);
184+
status = handle_libc_opcodes(*port, num_lanes);
186185
break;
187186
}
188187

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

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

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

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

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

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

libc/utils/gpu/server/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
add_library(llvmlibc_rpc_server STATIC rpc_server.cpp)
2+
3+
# Include the RPC implemenation from libc.
4+
target_include_directories(llvmlibc_rpc_server PRIVATE ${LIBC_SOURCE_DIR})
5+
target_include_directories(llvmlibc_rpc_server PUBLIC ${LIBC_SOURCE_DIR}/include)
6+
target_include_directories(llvmlibc_rpc_server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
7+
8+
# Ignore unsupported clang attributes if we're using GCC.
9+
target_compile_options(llvmlibc_rpc_server PUBLIC
10+
$<$<CXX_COMPILER_ID:Clang>:-Wno-c99-extensions>
11+
$<$<CXX_COMPILER_ID:GNU>:-Wno-attributes>)
12+
target_compile_definitions(llvmlibc_rpc_server PUBLIC
13+
LIBC_COPT_USE_C_ASSERT
14+
LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY
15+
LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
16+
LIBC_COPT_ARRAY_ARG_LIST
17+
LIBC_COPT_PRINTF_DISABLE_WRITE_INT
18+
LIBC_COPT_PRINTF_DISABLE_INDEX_MODE
19+
LIBC_COPT_PRINTF_DISABLE_STRERROR
20+
LIBC_NAMESPACE=${LIBC_NAMESPACE})
21+
22+
# Install the server and associated header.
23+
install(FILES ${LIBC_SOURCE_DIR}/shared/rpc.h
24+
${LIBC_SOURCE_DIR}/shared/rpc_util.h
25+
${LIBC_SOURCE_DIR}/shared/rpc_opcodes.h
26+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/shared
27+
COMPONENT libc-headers)
28+
install(TARGETS llvmlibc_rpc_server
29+
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}"
30+
COMPONENT libc)

libc/src/__support/RPC/rpc_server.h renamed to libc/utils/gpu/server/rpc_server.cpp

Lines changed: 41 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,25 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// This file is intended to be used externally as part of the `shared/`
10-
// interface. For that purpose, we manually define a few options normally
11-
// handled by the libc build system.
12-
//
13-
//===----------------------------------------------------------------------===//
14-
15-
#ifndef LLVM_LIBC_SRC___SUPPORT_RPC_RPC_SERVER_H
16-
#define LLVM_LIBC_SRC___SUPPORT_RPC_RPC_SERVER_H
178

189
// Workaround for missing __has_builtin in < GCC 10.
1910
#ifndef __has_builtin
2011
#define __has_builtin(x) 0
2112
#endif
2213

23-
// Configs for using the LLVM libc writer interface.
24-
#define LIBC_COPT_USE_C_ASSERT
25-
#define LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY
26-
#define LIBC_COPT_ARRAY_ARG_LIST
27-
#define LIBC_COPT_PRINTF_DISABLE_WRITE_INT
28-
#define LIBC_COPT_PRINTF_DISABLE_INDEX_MODE
29-
#define LIBC_COPT_PRINTF_DISABLE_STRERROR
30-
31-
// The 'long double' type is 8 byte
32-
#define LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
33-
3414
#include "shared/rpc.h"
3515
#include "shared/rpc_opcodes.h"
3616

17+
#include "src/__support/CPP/type_traits.h"
3718
#include "src/__support/arg_list.h"
3819
#include "src/stdio/printf_core/converter.h"
3920
#include "src/stdio/printf_core/parser.h"
4021
#include "src/stdio/printf_core/writer.h"
4122

42-
#include "hdr/stdio_overlay.h"
43-
#include "hdr/stdlib_overlay.h"
23+
#include <stdio.h>
24+
#include <stdlib.h>
4425

45-
namespace LIBC_NAMESPACE_DECL {
46-
namespace internal {
26+
namespace LIBC_NAMESPACE {
4727

4828
// Minimal replacement for 'std::vector' that works for trivial types.
4929
template <typename T> class TempVector {
@@ -55,66 +35,68 @@ template <typename T> class TempVector {
5535
size_t capacity;
5636

5737
public:
58-
LIBC_INLINE TempVector() : data(nullptr), current(0), capacity(0) {}
38+
TempVector() : data(nullptr), current(0), capacity(0) {}
5939

60-
LIBC_INLINE ~TempVector() { free(data); }
40+
~TempVector() { free(data); }
6141

62-
LIBC_INLINE void push_back(const T &value) {
42+
void push_back(const T &value) {
6343
if (current == capacity)
6444
grow();
6545
data[current] = T(value);
6646
++current;
6747
}
6848

69-
LIBC_INLINE void push_back(T &&value) {
49+
void push_back(T &&value) {
7050
if (current == capacity)
7151
grow();
7252
data[current] = T(static_cast<T &&>(value));
7353
++current;
7454
}
7555

76-
LIBC_INLINE void pop_back() { --current; }
56+
void pop_back() { --current; }
7757

78-
LIBC_INLINE bool empty() { return current == 0; }
58+
bool empty() { return current == 0; }
7959

80-
LIBC_INLINE size_t size() { return current; }
60+
size_t size() { return current; }
8161

82-
LIBC_INLINE T &operator[](size_t index) { return data[index]; }
62+
T &operator[](size_t index) { return data[index]; }
8363

84-
LIBC_INLINE T &back() { return data[current - 1]; }
64+
T &back() { return data[current - 1]; }
8565

8666
private:
87-
LIBC_INLINE void grow() {
67+
void grow() {
8868
size_t new_capacity = capacity ? capacity * 2 : 1;
8969
void *new_data = realloc(data, new_capacity * sizeof(T));
70+
if (!new_data)
71+
abort();
9072
data = static_cast<T *>(new_data);
9173
capacity = new_capacity;
9274
}
9375
};
9476

9577
struct TempStorage {
96-
LIBC_INLINE char *alloc(size_t size) {
78+
char *alloc(size_t size) {
9779
storage.push_back(reinterpret_cast<char *>(malloc(size)));
9880
return storage.back();
9981
}
10082

101-
LIBC_INLINE ~TempStorage() {
83+
~TempStorage() {
10284
for (size_t i = 0; i < storage.size(); ++i)
10385
free(storage[i]);
10486
}
10587

10688
TempVector<char *> storage;
10789
};
10890

109-
// Get the associated stream out of an encoded number.
110-
LIBC_INLINE static ::FILE *to_stream(uintptr_t f) {
111-
enum Stream {
112-
File = 0,
113-
Stdin = 1,
114-
Stdout = 2,
115-
Stderr = 3,
116-
};
91+
enum Stream {
92+
File = 0,
93+
Stdin = 1,
94+
Stdout = 2,
95+
Stderr = 3,
96+
};
11797

98+
// Get the associated stream out of an encoded number.
99+
LIBC_INLINE ::FILE *to_stream(uintptr_t f) {
118100
::FILE *stream = reinterpret_cast<FILE *>(f & ~0x3ull);
119101
Stream type = static_cast<Stream>(f & 0x3ull);
120102
if (type == Stdin)
@@ -127,8 +109,7 @@ LIBC_INLINE static ::FILE *to_stream(uintptr_t f) {
127109
}
128110

129111
template <bool packed, uint32_t num_lanes>
130-
LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
131-
TempStorage &temp_storage) {
112+
static void handle_printf(rpc::Server::Port &port, TempStorage &temp_storage) {
132113
FILE *files[num_lanes] = {nullptr};
133114
// Get the appropriate output stream to use.
134115
if (port.get_opcode() == LIBC_PRINTF_TO_STREAM ||
@@ -287,8 +268,7 @@ LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
287268
}
288269
}
289270

290-
results[lane] = static_cast<int>(
291-
fwrite(buffer, 1, writer.get_chars_written(), files[lane]));
271+
results[lane] = fwrite(buffer, 1, writer.get_chars_written(), files[lane]);
292272
if (results[lane] != writer.get_chars_written() || ret == -1)
293273
results[lane] = -1;
294274
}
@@ -302,7 +282,7 @@ LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
302282
}
303283

304284
template <uint32_t num_lanes>
305-
LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
285+
rpc::Status handle_port_impl(rpc::Server::Port &port) {
306286
TempStorage temp_storage;
307287

308288
switch (port.get_opcode()) {
@@ -353,9 +333,8 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
353333
void *data[num_lanes] = {nullptr};
354334
port.recv([&](rpc::Buffer *buffer, uint32_t id) {
355335
data[id] = temp_storage.alloc(buffer->data[0]);
356-
const char *str = ::fgets(reinterpret_cast<char *>(data[id]),
357-
static_cast<int>(buffer->data[0]),
358-
to_stream(buffer->data[1]));
336+
const char *str = fgets(reinterpret_cast<char *>(data[id]),
337+
buffer->data[0], to_stream(buffer->data[1]));
359338
sizes[id] = !str ? 0 : __builtin_strlen(str) + 1;
360339
});
361340
port.send_n(data, sizes);
@@ -374,9 +353,9 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
374353
break;
375354
}
376355
case LIBC_CLOSE_FILE: {
377-
port.recv_and_send([&](rpc::Buffer *buffer, uint32_t) {
356+
port.recv_and_send([&](rpc::Buffer *buffer, uint32_t id) {
378357
FILE *file = reinterpret_cast<FILE *>(buffer->data[0]);
379-
buffer->data[0] = ::fclose(file);
358+
buffer->data[0] = fclose(file);
380359
});
381360
break;
382361
}
@@ -519,28 +498,21 @@ LIBC_INLINE static rpc::Status handle_port_impl(rpc::Server::Port &port) {
519498
return rpc::RPC_SUCCESS;
520499
}
521500

522-
} // namespace internal
523-
} // namespace LIBC_NAMESPACE_DECL
501+
} // namespace LIBC_NAMESPACE
524502

525-
namespace LIBC_NAMESPACE_DECL {
526503
namespace rpc {
527-
528-
// Handles any opcode generated from the 'libc' client code.
529-
LIBC_INLINE ::rpc::Status handle_libc_opcodes(::rpc::Server::Port &port,
530-
uint32_t num_lanes) {
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) {
531507
switch (num_lanes) {
532508
case 1:
533-
return internal::handle_port_impl<1>(port);
509+
return LIBC_NAMESPACE::handle_port_impl<1>(port);
534510
case 32:
535-
return internal::handle_port_impl<32>(port);
511+
return LIBC_NAMESPACE::handle_port_impl<32>(port);
536512
case 64:
537-
return internal::handle_port_impl<64>(port);
513+
return LIBC_NAMESPACE::handle_port_impl<64>(port);
538514
default:
539-
return ::rpc::RPC_ERROR;
515+
return rpc::RPC_ERROR;
540516
}
541517
}
542-
543518
} // namespace rpc
544-
} // namespace LIBC_NAMESPACE_DECL
545-
546-
#endif // LLVM_LIBC_SRC___SUPPORT_RPC_RPC_SERVER_H

0 commit comments

Comments
 (0)