llvm-project
71 строка · 2.4 Кб
1//===-- GPU implementation of fprintf -------------------------------------===//
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 "rpc_fprintf.h"
10
11#include "src/__support/CPP/string_view.h"
12#include "src/__support/GPU/utils.h"
13#include "src/__support/RPC/rpc_client.h"
14#include "src/__support/common.h"
15#include "src/stdio/gpu/file.h"
16
17namespace LIBC_NAMESPACE {
18
19template <uint16_t opcode>
20int fprintf_impl(::FILE *__restrict file, const char *__restrict format,
21size_t format_size, void *args, size_t args_size) {
22uint64_t mask = gpu::get_lane_mask();
23rpc::Client::Port port = rpc::client.open<opcode>();
24
25if constexpr (opcode == RPC_PRINTF_TO_STREAM) {
26port.send([&](rpc::Buffer *buffer) {
27buffer->data[0] = reinterpret_cast<uintptr_t>(file);
28});
29}
30
31port.send_n(format, format_size);
32port.send_n(args, args_size);
33
34uint32_t ret = 0;
35for (;;) {
36const char *str = nullptr;
37port.recv([&](rpc::Buffer *buffer) {
38ret = static_cast<uint32_t>(buffer->data[0]);
39str = reinterpret_cast<const char *>(buffer->data[1]);
40});
41// If any lanes have a string argument it needs to be copied back.
42if (!gpu::ballot(mask, str))
43break;
44
45uint64_t size = str ? internal::string_length(str) + 1 : 0;
46port.send_n(str, size);
47}
48
49port.close();
50return ret;
51}
52
53// TODO: This is a stand-in function that uses a struct pointer and size in
54// place of varargs. Once varargs support is added we will use that to
55// implement the real version.
56LLVM_LIBC_FUNCTION(int, rpc_fprintf,
57(::FILE *__restrict stream, const char *__restrict format,
58void *args, size_t size)) {
59cpp::string_view str(format);
60if (stream == stdout)
61return fprintf_impl<RPC_PRINTF_TO_STDOUT>(stream, format, str.size() + 1,
62args, size);
63else if (stream == stderr)
64return fprintf_impl<RPC_PRINTF_TO_STDERR>(stream, format, str.size() + 1,
65args, size);
66else
67return fprintf_impl<RPC_PRINTF_TO_STREAM>(stream, format, str.size() + 1,
68args, size);
69}
70
71} // namespace LIBC_NAMESPACE
72