Skip to content

Commit be0c67c

Browse files
authored
[libc] Remove dependency on cpp::function in rpc.h (#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 224f62d commit be0c67c

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
}

libc/src/stdio/gpu/fgets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LLVM_LIBC_FUNCTION(char *, fgets,
2727
uint64_t recv_size;
2828
void *buf = nullptr;
2929
rpc::Client::Port port = rpc::client.open<RPC_READ_FGETS>();
30-
port.send([=](rpc::Buffer *buffer) {
30+
port.send([=](rpc::Buffer *buffer, uint32_t) {
3131
buffer->data[0] = count;
3232
buffer->data[1] = file::from_stream(stream);
3333
});

libc/src/stdio/gpu/file.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ LIBC_INLINE uint64_t write_impl(::FILE *file, const void *data, size_t size) {
5555
rpc::Client::Port port = rpc::client.open<opcode>();
5656

5757
if constexpr (opcode == RPC_WRITE_TO_STREAM) {
58-
port.send([&](rpc::Buffer *buffer) {
58+
port.send([&](rpc::Buffer *buffer, uint32_t) {
5959
buffer->data[0] = reinterpret_cast<uintptr_t>(file);
6060
});
6161
}
6262

6363
port.send_n(data, size);
64-
port.recv([&](rpc::Buffer *buffer) {
64+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
6565
ret = reinterpret_cast<uint64_t *>(buffer->data)[0];
6666
});
6767
port.close();
@@ -81,12 +81,12 @@ LIBC_INLINE uint64_t read_from_stream(::FILE *file, void *buf, size_t size) {
8181
uint64_t ret = 0;
8282
uint64_t recv_size;
8383
rpc::Client::Port port = rpc::client.open<RPC_READ_FROM_STREAM>();
84-
port.send([=](rpc::Buffer *buffer) {
84+
port.send([=](rpc::Buffer *buffer, uint32_t) {
8585
buffer->data[0] = size;
8686
buffer->data[1] = from_stream(file);
8787
});
8888
port.recv_n(&buf, &recv_size, [&](uint64_t) { return buf; });
89-
port.recv([&](rpc::Buffer *buffer) { ret = buffer->data[0]; });
89+
port.recv([&](rpc::Buffer *buffer, uint32_t) { ret = buffer->data[0]; });
9090
port.close();
9191
return ret;
9292
}

libc/src/stdio/gpu/fopen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ LLVM_LIBC_FUNCTION(::FILE *, fopen,
2121
rpc::Client::Port port = rpc::client.open<RPC_OPEN_FILE>();
2222
port.send_n(path, internal::string_length(path) + 1);
2323
port.send_and_recv(
24-
[=](rpc::Buffer *buffer) {
24+
[=](rpc::Buffer *buffer, uint32_t) {
2525
inline_memcpy(buffer->data, mode, internal::string_length(mode) + 1);
2626
},
27-
[&](rpc::Buffer *buffer) { file = buffer->data[0]; });
27+
[&](rpc::Buffer *buffer, uint32_t) { file = buffer->data[0]; });
2828
port.close();
2929

3030
return reinterpret_cast<FILE *>(file);

libc/src/stdio/gpu/fseek.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ LLVM_LIBC_FUNCTION(int, fseek, (::FILE * stream, long offset, int whence)) {
1818
int ret;
1919
rpc::Client::Port port = rpc::client.open<RPC_FSEEK>();
2020
port.send_and_recv(
21-
[=](rpc::Buffer *buffer) {
21+
[=](rpc::Buffer *buffer, uint32_t) {
2222
buffer->data[0] = file::from_stream(stream);
2323
buffer->data[1] = static_cast<uint64_t>(offset);
2424
buffer->data[2] = static_cast<uint64_t>(whence);
2525
},
26-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
26+
[&](rpc::Buffer *buffer, uint32_t) {
27+
ret = static_cast<int>(buffer->data[0]);
28+
});
2729
port.close();
2830
return ret;
2931
}

libc/src/stdio/gpu/ftell.cpp

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

libc/src/stdio/gpu/remove.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ LLVM_LIBC_FUNCTION(int, remove, (const char *path)) {
1818
int ret;
1919
rpc::Client::Port port = rpc::client.open<RPC_REMOVE>();
2020
port.send_n(path, internal::string_length(path) + 1);
21-
port.recv(
22-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
21+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
22+
ret = static_cast<int>(buffer->data[0]);
23+
});
2324
port.close();
2425
return ret;
2526
}

libc/src/stdio/gpu/rename.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) {
2020
rpc::Client::Port port = rpc::client.open<RPC_RENAME>();
2121
port.send_n(oldpath, internal::string_length(oldpath) + 1);
2222
port.send_n(newpath, internal::string_length(newpath) + 1);
23-
port.recv(
24-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
23+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
24+
ret = static_cast<int>(buffer->data[0]);
25+
});
2526
port.close();
2627

2728
return ret;

libc/src/stdio/gpu/ungetc.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ LLVM_LIBC_FUNCTION(int, ungetc, (int c, ::FILE *stream)) {
1818
int ret;
1919
rpc::Client::Port port = rpc::client.open<RPC_UNGETC>();
2020
port.send_and_recv(
21-
[=](rpc::Buffer *buffer) {
21+
[=](rpc::Buffer *buffer, uint32_t) {
2222
buffer->data[0] = c;
2323
buffer->data[1] = file::from_stream(stream);
2424
},
25-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
25+
[&](rpc::Buffer *buffer, uint32_t) {
26+
ret = static_cast<int>(buffer->data[0]);
27+
});
2628
port.close();
2729
return ret;
2830
}

libc/src/stdio/gpu/vfprintf_utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ LIBC_INLINE int vfprintf_impl(::FILE *__restrict file,
2323

2424
if constexpr (opcode == RPC_PRINTF_TO_STREAM ||
2525
opcode == RPC_PRINTF_TO_STREAM_PACKED) {
26-
port.send([&](rpc::Buffer *buffer) {
26+
port.send([&](rpc::Buffer *buffer, uint32_t) {
2727
buffer->data[0] = reinterpret_cast<uintptr_t>(file);
2828
});
2929
}
3030

3131
size_t args_size = 0;
3232
port.send_n(format, format_size);
33-
port.recv([&](rpc::Buffer *buffer) {
33+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
3434
args_size = static_cast<size_t>(buffer->data[0]);
3535
});
3636
port.send_n(vlist, args_size);
3737

3838
uint32_t ret = 0;
3939
for (;;) {
4040
const char *str = nullptr;
41-
port.recv([&](rpc::Buffer *buffer) {
41+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
4242
ret = static_cast<uint32_t>(buffer->data[0]);
4343
str = reinterpret_cast<const char *>(buffer->data[1]);
4444
});

libc/src/stdlib/gpu/abort.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ namespace LIBC_NAMESPACE_DECL {
1717
LLVM_LIBC_FUNCTION(void, abort, ()) {
1818
// We want to first make sure the server is listening before we abort.
1919
rpc::Client::Port port = rpc::client.open<RPC_ABORT>();
20-
port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
21-
port.send([&](rpc::Buffer *) {});
20+
port.send_and_recv([](rpc::Buffer *, uint32_t) {},
21+
[](rpc::Buffer *, uint32_t) {});
22+
port.send([&](rpc::Buffer *, uint32_t) {});
2223
port.close();
2324

2425
gpu::end_program();

libc/src/stdlib/gpu/system.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ LLVM_LIBC_FUNCTION(int, system, (const char *command)) {
1919
int ret;
2020
rpc::Client::Port port = rpc::client.open<RPC_SYSTEM>();
2121
port.send_n(command, internal::string_length(command) + 1);
22-
port.recv(
23-
[&](rpc::Buffer *buffer) { ret = static_cast<int>(buffer->data[0]); });
22+
port.recv([&](rpc::Buffer *buffer, uint32_t) {
23+
ret = static_cast<int>(buffer->data[0]);
24+
});
2425
port.close();
2526

2627
return ret;

0 commit comments

Comments
 (0)