Skip to content

[libc] Implement stdio writing functions for the GPU port #65809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.fclose
libc.src.stdio.fread
libc.src.stdio.fputs
libc.src.stdio.fwrite
libc.src.stdio.fputc
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.stdin
libc.src.stdio.stdout
libc.src.stdio.stderr
Expand Down
8 changes: 6 additions & 2 deletions libc/docs/gpu/support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,24 @@ strtoumax |check|
============= ========= ============

stdio.h
--------
-------

============= ========= ============
Function Name Available RPC Required
============= ========= ============
puts |check| |check|
fputs |check| |check|
fputc |check| |check|
fwrite |check| |check|
putc |check| |check|
putchar |check| |check|
fclose |check| |check|
fopen |check| |check|
fread |check| |check|
============= ========= ============

time.h
--------
------

============= ========= ============
Function Name Available RPC Required
Expand Down
70 changes: 5 additions & 65 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,71 +229,6 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fwrite_unlocked
SRCS
fwrite_unlocked.cpp
HDRS
fwrite_unlocked.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fwrite
SRCS
fwrite.cpp
HDRS
fwrite.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fputc
SRCS
fputc.cpp
HDRS
fputc.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
putc
SRCS
putc.cpp
HDRS
putc.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
putchar
SRCS
putchar.cpp
HDRS
putchar.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fseek
SRCS
Expand Down Expand Up @@ -538,6 +473,11 @@ add_stdio_entrypoint_object(fread_unlocked)
add_stdio_entrypoint_object(fread)
add_stdio_entrypoint_object(puts)
add_stdio_entrypoint_object(fputs)
add_stdio_entrypoint_object(fwrite_unlocked)
add_stdio_entrypoint_object(fwrite)
add_stdio_entrypoint_object(fputc)
add_stdio_entrypoint_object(putc)
add_stdio_entrypoint_object(putchar)
add_stdio_entrypoint_object(stdin)
add_stdio_entrypoint_object(stdout)
add_stdio_entrypoint_object(stderr)
65 changes: 65 additions & 0 deletions libc/src/stdio/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,71 @@ add_entrypoint_object(
libc.src.__support.File.platform_stdout
)

add_entrypoint_object(
fwrite_unlocked
SRCS
fwrite_unlocked.cpp
HDRS
../fwrite_unlocked.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fwrite
SRCS
fwrite.cpp
HDRS
../fwrite.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fputc
SRCS
fputc.cpp
HDRS
../fputc.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
putc
SRCS
putc.cpp
HDRS
../putc.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
putchar
SRCS
putchar.cpp
HDRS
../putchar.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
stdin
SRCS
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions libc/src/stdio/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,50 @@ add_entrypoint_object(
.gpu_file
)

add_entrypoint_object(
fwrite
SRCS
fwrite.cpp
HDRS
../fwrite.h
DEPENDS
libc.include.stdio
.gpu_file
)

add_entrypoint_object(
fputc
SRCS
fputc.cpp
HDRS
../fputc.h
DEPENDS
libc.include.stdio
.gpu_file
)

add_entrypoint_object(
putc
SRCS
putc.cpp
HDRS
../putc.h
DEPENDS
libc.include.stdio
.gpu_file
)

add_entrypoint_object(
putchar
SRCS
putchar.cpp
HDRS
../putchar.h
DEPENDS
libc.include.stdio
.gpu_file
)

add_entrypoint_object(
stdin
SRCS
Expand Down
26 changes: 26 additions & 0 deletions libc/src/stdio/gpu/fputc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- GPU implementation of fputc ---------------------------------------===//
//
// 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 "file.h"
#include "src/stdio/fputc.h"

#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, fputc, (int c, ::FILE *stream)) {
unsigned char uc = static_cast<unsigned char>(c);

size_t written = file::write(stream, &uc, 1);
if (1 != written)
return EOF;

return 0;
}

} // namespace __llvm_libc
26 changes: 26 additions & 0 deletions libc/src/stdio/gpu/fwrite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- GPU implementation of fwrite --------------------------------------===//
//
// 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/fwrite.h"
#include "file.h"

#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(size_t, fwrite,
(const void *__restrict buffer, size_t size, size_t nmemb,
::FILE *stream)) {
if (size == 0 || nmemb == 0)
return 0;

auto result = file::write(stream, buffer, size * nmemb);
return result / size;
}

} // namespace __llvm_libc
26 changes: 26 additions & 0 deletions libc/src/stdio/gpu/putc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- GPU implementation of putc ----------------------------------------===//
//
// 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/putc.h"
#include "file.h"

#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, putc, (int c, ::FILE *stream)) {
unsigned char uc = static_cast<unsigned char>(c);

size_t written = file::write(stream, &uc, 1);
if (1 != written)
return EOF;

return 0;
}

} // namespace __llvm_libc
26 changes: 26 additions & 0 deletions libc/src/stdio/gpu/putchar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- GPU implementation of putchar -------------------------------------===//
//
// 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 "file.h"
#include "src/stdio/putchar.h"

#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
unsigned char uc = static_cast<unsigned char>(c);

size_t written = file::write(stdout, &uc, 1);
if (1 != written)
return EOF;

return 0;
}

} // namespace __llvm_libc
16 changes: 15 additions & 1 deletion libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ add_libc_test(
libc.src.stdio.stderr
)

add_libc_test(
fputc_test
HERMETIC_TEST_ONLY # writes to libc's stdout and stderr
SUITE
libc_stdio_unittests
SRCS
fputc_test.cpp
DEPENDS
libc.src.stdio.fputc
libc.src.stdio.putchar
libc.src.stdio.stdout
libc.src.stdio.stderr
)

add_libc_test(
fopen_test
SUITE
Expand All @@ -279,7 +293,7 @@ add_libc_test(
fopen_test.cpp
DEPENDS
libc.src.stdio.fread
libc.src.stdio.fputs
libc.src.stdio.fwrite
libc.src.stdio.fclose
libc.src.stdio.fopen
)
Expand Down
Loading