xref: /llvm-project/libc/src/__support/OSUtil/gpu/io.cpp (revision 9a99afb455601aaa3a28d307c7bbcfdaad3fa4fc)
1 //===-------------- GPU implementation of IO utils --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "io.h"
10 
11 #include "src/__support/CPP/string_view.h"
12 #include "src/__support/RPC/rpc_client.h"
13 #include "src/string/memory_utils/memcpy_implementations.h"
14 
15 namespace __llvm_libc {
16 
17 namespace internal {
18 
19 static constexpr size_t BUFFER_SIZE = sizeof(rpc::Buffer) - sizeof(uint64_t);
20 static constexpr size_t MAX_STRING_SIZE = BUFFER_SIZE;
21 
22 LIBC_INLINE void send_null_terminated(cpp::string_view src) {
23   rpc::client.run(
24       [&](rpc::Buffer *buffer) {
25         buffer->data[0] = rpc::Opcode::PRINT_TO_STDERR;
26         char *data = reinterpret_cast<char *>(&buffer->data[1]);
27         inline_memcpy(data, src.data(), src.size());
28         data[src.size()] = '\0';
29       },
30       [](rpc::Buffer *) { /* void */ });
31 }
32 
33 } // namespace internal
34 
35 void write_to_stderr(cpp::string_view msg) {
36   bool send_empty_string = true;
37   for (; !msg.empty();) {
38     const auto chunk = msg.substr(0, internal::MAX_STRING_SIZE);
39     internal::send_null_terminated(chunk);
40     msg.remove_prefix(chunk.size());
41     send_empty_string = false;
42   }
43   if (send_empty_string)
44     internal::send_null_terminated("");
45 }
46 
47 } // namespace __llvm_libc
48