summaryrefslogtreecommitdiff
path: root/libc/src/stdio/generic
diff options
context:
space:
mode:
authorJoseph Huber <huberjn@outlook.com>2024-07-12 19:36:13 -0500
committerGitHub <noreply@github.com>2024-07-12 19:36:13 -0500
commit40effc7af5679b7d54d3176a5eef2cdee1962ecd (patch)
tree730e0f5425c004cb0bffb073f2902489f680ba21 /libc/src/stdio/generic
parentee4661e0f8eaff5da011e008187362f4ba947860 (diff)
[libc] Implement (v|f)printf on the GPU (#96369)
Summary: This patch implements the `printf` family of functions on the GPU using the new variadic support. This patch adapts the old handling in the `rpc_fprintf` placeholder, but adds an extra RPC call to get the size of the buffer to copy. This prevents the GPU from needing to parse the string. While it's theoretically possible for the pass to know the size of the struct, it's prohibitively difficult to do while maintaining ABI compatibility with NVIDIA's varargs. Depends on https://github.com/llvm/llvm-project/pull/96015.
Diffstat (limited to 'libc/src/stdio/generic')
-rw-r--r--libc/src/stdio/generic/CMakeLists.txt39
-rw-r--r--libc/src/stdio/generic/fprintf.cpp34
-rw-r--r--libc/src/stdio/generic/vfprintf.cpp31
3 files changed, 91 insertions, 13 deletions
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index b0ce8ccc08bd..a86b86a32dce 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -363,19 +363,6 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)
-list(APPEND printf_deps
- libc.src.__support.arg_list
- libc.src.stdio.printf_core.vfprintf_internal
-)
-
-if(LLVM_LIBC_FULL_BUILD)
- list(APPEND printf_deps
- libc.src.__support.File.file
- libc.src.__support.File.platform_file
- libc.src.__support.File.platform_stdout
- )
-endif()
-
add_entrypoint_object(
printf
SRCS
@@ -397,6 +384,32 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ fprintf
+ SRCS
+ fprintf.cpp
+ HDRS
+ ../fprintf.h
+ DEPENDS
+ libc.hdr.types.FILE
+ libc.src.__support.arg_list
+ libc.src.stdio.printf_core.vfprintf_internal
+ ${printf_deps}
+)
+
+add_entrypoint_object(
+ vfprintf
+ SRCS
+ vfprintf.cpp
+ HDRS
+ ../vfprintf.h
+ DEPENDS
+ libc.hdr.types.FILE
+ libc.src.__support.arg_list
+ libc.src.stdio.printf_core.vfprintf_internal
+ ${printf_deps}
+)
+
+add_entrypoint_object(
fgets
SRCS
fgets.cpp
diff --git a/libc/src/stdio/generic/fprintf.cpp b/libc/src/stdio/generic/fprintf.cpp
new file mode 100644
index 000000000000..087aeadfc52c
--- /dev/null
+++ b/libc/src/stdio/generic/fprintf.cpp
@@ -0,0 +1,34 @@
+//===-- Implementation of fprintf -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/fprintf.h"
+
+#include "src/__support/File/file.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/printf_core/vfprintf_internal.h"
+
+#include "hdr/types/FILE.h"
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, fprintf,
+ (::FILE *__restrict stream, const char *__restrict format,
+ ...)) {
+ va_list vlist;
+ va_start(vlist, format);
+ internal::ArgList args(vlist); // This holder class allows for easier copying
+ // and pointer semantics, as well as handling
+ // destruction automatically.
+ va_end(vlist);
+ int ret_val = printf_core::vfprintf_internal(stream, format, args);
+ return ret_val;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/generic/vfprintf.cpp b/libc/src/stdio/generic/vfprintf.cpp
new file mode 100644
index 000000000000..01f4265f118a
--- /dev/null
+++ b/libc/src/stdio/generic/vfprintf.cpp
@@ -0,0 +1,31 @@
+//===-- Implementation of vfprintf ------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/vfprintf.h"
+
+#include "src/__support/File/file.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/macros/config.h"
+#include "src/stdio/printf_core/vfprintf_internal.h"
+
+#include "hdr/types/FILE.h"
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, vfprintf,
+ (::FILE *__restrict stream, const char *__restrict format,
+ va_list vlist)) {
+ internal::ArgList args(vlist); // This holder class allows for easier copying
+ // and pointer semantics, as well as handling
+ // destruction automatically.
+ int ret_val = printf_core::vfprintf_internal(stream, format, args);
+ return ret_val;
+}
+
+} // namespace LIBC_NAMESPACE_DECL