Skip to content

Commit 6071fc3

Browse files
jhuber6DanielCChen
authored andcommitted
[libc] Remove dependency on cpp::function in rpc.h (llvm#112422)
Summary: I'm going to attempt to move the `rpc.h` header to a separate folder that we can install and include outside of `libc`. Before doing this I'm going to try to trim up the file so there's not as many things I need to copy to make it work. This dependency on `cpp::functional` is a low hanging fruit. I only did it so that I could overload the argument of the work function so that passing the id was optional in the lambda, that's not a *huge* deal and it makes it more explicit I suppose.
1 parent 442aabb commit 6071fc3

File tree

22 files changed

+92
-77
lines changed

22 files changed

+92
-77
lines changed

libc/src/__support/GPU/allocator.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ namespace {
1818
void *rpc_allocate(uint64_t size) {
1919
void *ptr = nullptr;
2020
rpc::Client::Port port = rpc::client.open<RPC_MALLOC>();
21-
port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; },
22-
[&](rpc::Buffer *buffer) {
23-
ptr = reinterpret_cast<void *>(buffer->data[0]);
24-
});
21+
port.send_and_recv(
22+
[=](rpc::Buffer *buffer, uint32_t) { buffer->data[0] = size; },
23+
[&](rpc::Buffer *buffer, uint32_t) {
24+
ptr = reinterpret_cast<void *>(buffer->data[0]);
25+
});
2526
port.close();
2627
return ptr;
2728
}
2829

2930
void rpc_free(void *ptr) {
3031
rpc::Client::Port port = rpc::client.open<RPC_FREE>();
31-
port.send([=](rpc::Buffer *buffer) {
32+
port.send([=](rpc::Buffer *buffer, uint32_t) {
3233
buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
3334
});
3435
port.close();

libc/src/__support/OSUtil/gpu/exit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ namespace internal {
1818
[[noreturn]] void exit(int status) {
1919
// We want to first make sure the server is listening before we exit.
2020
rpc::Client::Port port = rpc::client.open<RPC_EXIT>();
21-
port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
22-
port.send([&](rpc::Buffer *buffer) {
21+
port.send_and_recv([](rpc::Buffer *, uint32_t) {},
22+
[](rpc::Buffer *, uint32_t) {});
23+
port.send([&](rpc::Buffer *buffer, uint32_t) {
2324
reinterpret_cast<uint32_t *>(buffer->data)[0] = status;
2425
});
2526
port.close();

libc/src/__support/OSUtil/gpu/io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
1717
void write_to_stderr(cpp::string_view msg) {
1818
rpc::Client::Port port = rpc::client.open<RPC_WRITE_TO_STDERR>();
1919
port.send_n(msg.data(), msg.size());
20-
port.recv([](rpc::Buffer *) { /* void */ });
20+
port.recv([](rpc::Buffer *, uint32_t) { /* void */ });
2121
port.close();
2222
}
2323

libc/src/__support/RPC/rpc.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "rpc_util.h"
2222
#include "src/__support/CPP/algorithm.h" // max
2323
#include "src/__support/CPP/atomic.h"
24-
#include "src/__support/CPP/functional.h"
2524
#include "src/__support/CPP/optional.h"
2625
#include "src/__support/GPU/utils.h"
2726
#include "src/__support/macros/config.h"
@@ -266,22 +265,9 @@ template <bool Invert> struct Process {
266265
};
267266

268267
/// Invokes a function accross every active buffer across the total lane size.
269-
static LIBC_INLINE void invoke_rpc(cpp::function<void(Buffer *)> fn,
270-
uint32_t lane_size, uint64_t lane_mask,
271-
Buffer *slot) {
272-
if constexpr (is_process_gpu()) {
273-
fn(&slot[gpu::get_lane_id()]);
274-
} else {
275-
for (uint32_t i = 0; i < lane_size; i += gpu::get_lane_size())
276-
if (lane_mask & (1ul << i))
277-
fn(&slot[i]);
278-
}
279-
}
280-
281-
/// Alternate version that also provides the index of the current lane.
282-
static LIBC_INLINE void invoke_rpc(cpp::function<void(Buffer *, uint32_t)> fn,
283-
uint32_t lane_size, uint64_t lane_mask,
284-
Buffer *slot) {
268+
template <typename F>
269+
LIBC_INLINE static void invoke_rpc(F &&fn, uint32_t lane_size,
270+
uint64_t lane_mask, Buffer *slot) {
285271
if constexpr (is_process_gpu()) {
286272
fn(&slot[gpu::get_lane_id()], gpu::get_lane_id());
287273
} else {
@@ -444,7 +430,7 @@ template <bool T>
444430
template <typename W>
445431
LIBC_INLINE void Port<T>::recv_and_send(W work) {
446432
recv(work);
447-
send([](Buffer *) { /* no-op */ });
433+
send([](Buffer *, uint32_t) { /* no-op */ });
448434
}
449435

450436
/// Helper routine to simplify the interface when sending from the GPU using

libc/src/gpu/rpc_host_call.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ LLVM_LIBC_FUNCTION(unsigned long long, rpc_host_call,
2121
(void *fn, void *data, size_t size)) {
2222
rpc::Client::Port port = rpc::client.open<RPC_HOST_CALL>();
2323
port.send_n(data, size);
24-
port.send([=](rpc::Buffer *buffer) {
24+
port.send([=](rpc::Buffer *buffer, uint32_t) {
2525
buffer->data[0] = reinterpret_cast<uintptr_t>(fn);
2626
});
2727
unsigned long long ret;
28-
port.recv([&](rpc::Buffer *buffer) {
28+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
2929
ret = static_cast<unsigned long long>(buffer->data[0]);
3030
});
3131
port.close();

libc/src/stdio/gpu/clearerr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ namespace LIBC_NAMESPACE_DECL {
1717
LLVM_LIBC_FUNCTION(void, clearerr, (::FILE * stream)) {
1818
rpc::Client::Port port = rpc::client.open<RPC_CLEARERR>();
1919
port.send_and_recv(
20-
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
21-
[&](rpc::Buffer *) {});
20+
[=](rpc::Buffer *buffer, uint32_t) {
21+
buffer->data[0] = file::from_stream(stream);
22+
},
23+
[&](rpc::Buffer *, uint32_t) {});
2224
port.close();
2325
}
2426

libc/src/stdio/gpu/fclose.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ LLVM_LIBC_FUNCTION(int, fclose, (::FILE * stream)) {
1919
uint64_t ret = 0;
2020
uintptr_t file = reinterpret_cast<uintptr_t>(stream);
2121
rpc::Client::Port port = rpc::client.open<RPC_CLOSE_FILE>();
22-
port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = file; },
23-
[&](rpc::Buffer *buffer) { ret = buffer->data[0]; });
22+
port.send_and_recv(
23+
[=](rpc::Buffer *buffer, uint32_t) { buffer->data[0] = file; },
24+
[&](rpc::Buffer *buffer, uint32_t) { ret = buffer->data[0]; });
2425
port.close();
2526

2627
if (ret != 0)

libc/src/stdio/gpu/feof.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ LLVM_LIBC_FUNCTION(int, feof, (::FILE * stream)) {
1818
int ret;
1919
rpc::Client::Port port = rpc::client.open<RPC_FEOF>();
2020
port.send_and_recv(
21-
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
22-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
21+
[=](rpc::Buffer *buffer, uint32_t) {
22+
buffer->data[0] = file::from_stream(stream);
23+
},
24+
[&](rpc::Buffer *buffer, uint32_t) {
25+
ret = static_cast<int>(buffer->data[0]);
26+
});
2327
port.close();
2428
return ret;
2529
}

libc/src/stdio/gpu/ferror.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ LLVM_LIBC_FUNCTION(int, ferror, (::FILE * stream)) {
1818
int ret;
1919
rpc::Client::Port port = rpc::client.open<RPC_FERROR>();
2020
port.send_and_recv(
21-
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
22-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
21+
[=](rpc::Buffer *buffer, uint32_t) {
22+
buffer->data[0] = file::from_stream(stream);
23+
},
24+
[&](rpc::Buffer *buffer, uint32_t) {
25+
ret = static_cast<int>(buffer->data[0]);
26+
});
2327
port.close();
2428
return ret;
2529
}

libc/src/stdio/gpu/fflush.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
1818
int ret;
1919
rpc::Client::Port port = rpc::client.open<RPC_FFLUSH>();
2020
port.send_and_recv(
21-
[=](rpc::Buffer *buffer) { buffer->data[0] = file::from_stream(stream); },
22-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
21+
[=](rpc::Buffer *buffer, uint32_t) {
22+
buffer->data[0] = file::from_stream(stream);
23+
},
24+
[&](rpc::Buffer *buffer, uint32_t) {
25+
ret = static_cast<int>(buffer->data[0]);
26+
});
2327
port.close();
2428
return ret;
2529
}

0 commit comments

Comments
 (0)