Skip to content

Commit 60c0d30

Browse files
authored
[libc] Implement stdio writing functions for the GPU port (#65809)
Summary: This patch implements fwrite, putc, putchar, and fputc on the GPU. These are very straightforward, the main difference for the GPU implementation is that we are currently ignoring `errno`. This patch also introduces a minimal smoke test for `putc` that is an exact copy of the `puts` test except we print the string char by char. This also modifies the `fopen` test to use `fwrite` to mirror its use of `fread` so that it is tested as well.
1 parent b82f786 commit 60c0d30

File tree

17 files changed

+275
-70
lines changed

17 files changed

+275
-70
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ set(TARGET_LIBC_ENTRYPOINTS
8888
libc.src.stdio.fclose
8989
libc.src.stdio.fread
9090
libc.src.stdio.fputs
91+
libc.src.stdio.fwrite
92+
libc.src.stdio.fputc
93+
libc.src.stdio.putc
94+
libc.src.stdio.putchar
9195
libc.src.stdio.stdin
9296
libc.src.stdio.stdout
9397
libc.src.stdio.stderr

libc/docs/gpu/support.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,24 @@ strtoumax |check|
118118
============= ========= ============
119119

120120
stdio.h
121-
--------
121+
-------
122122

123123
============= ========= ============
124124
Function Name Available RPC Required
125125
============= ========= ============
126126
puts |check| |check|
127127
fputs |check| |check|
128+
fputc |check| |check|
129+
fwrite |check| |check|
130+
putc |check| |check|
131+
putchar |check| |check|
128132
fclose |check| |check|
129133
fopen |check| |check|
130134
fread |check| |check|
131135
============= ========= ============
132136

133137
time.h
134-
--------
138+
------
135139

136140
============= ========= ============
137141
Function Name Available RPC Required

libc/src/stdio/CMakeLists.txt

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -229,71 +229,6 @@ add_entrypoint_object(
229229
libc.src.__support.File.platform_file
230230
)
231231

232-
add_entrypoint_object(
233-
fwrite_unlocked
234-
SRCS
235-
fwrite_unlocked.cpp
236-
HDRS
237-
fwrite_unlocked.h
238-
DEPENDS
239-
libc.src.errno.errno
240-
libc.include.stdio
241-
libc.src.__support.File.file
242-
libc.src.__support.File.platform_file
243-
)
244-
245-
add_entrypoint_object(
246-
fwrite
247-
SRCS
248-
fwrite.cpp
249-
HDRS
250-
fwrite.h
251-
DEPENDS
252-
libc.src.errno.errno
253-
libc.include.stdio
254-
libc.src.__support.File.file
255-
libc.src.__support.File.platform_file
256-
)
257-
258-
add_entrypoint_object(
259-
fputc
260-
SRCS
261-
fputc.cpp
262-
HDRS
263-
fputc.h
264-
DEPENDS
265-
libc.src.errno.errno
266-
libc.include.stdio
267-
libc.src.__support.File.file
268-
libc.src.__support.File.platform_file
269-
)
270-
271-
add_entrypoint_object(
272-
putc
273-
SRCS
274-
putc.cpp
275-
HDRS
276-
putc.h
277-
DEPENDS
278-
libc.src.errno.errno
279-
libc.include.stdio
280-
libc.src.__support.File.file
281-
libc.src.__support.File.platform_file
282-
)
283-
284-
add_entrypoint_object(
285-
putchar
286-
SRCS
287-
putchar.cpp
288-
HDRS
289-
putchar.h
290-
DEPENDS
291-
libc.src.errno.errno
292-
libc.include.stdio
293-
libc.src.__support.File.file
294-
libc.src.__support.File.platform_file
295-
)
296-
297232
add_entrypoint_object(
298233
fseek
299234
SRCS
@@ -538,6 +473,11 @@ add_stdio_entrypoint_object(fread_unlocked)
538473
add_stdio_entrypoint_object(fread)
539474
add_stdio_entrypoint_object(puts)
540475
add_stdio_entrypoint_object(fputs)
476+
add_stdio_entrypoint_object(fwrite_unlocked)
477+
add_stdio_entrypoint_object(fwrite)
478+
add_stdio_entrypoint_object(fputc)
479+
add_stdio_entrypoint_object(putc)
480+
add_stdio_entrypoint_object(putchar)
541481
add_stdio_entrypoint_object(stdin)
542482
add_stdio_entrypoint_object(stdout)
543483
add_stdio_entrypoint_object(stderr)

libc/src/stdio/generic/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,71 @@ add_entrypoint_object(
7575
libc.src.__support.File.platform_stdout
7676
)
7777

78+
add_entrypoint_object(
79+
fwrite_unlocked
80+
SRCS
81+
fwrite_unlocked.cpp
82+
HDRS
83+
../fwrite_unlocked.h
84+
DEPENDS
85+
libc.src.errno.errno
86+
libc.include.stdio
87+
libc.src.__support.File.file
88+
libc.src.__support.File.platform_file
89+
)
90+
91+
add_entrypoint_object(
92+
fwrite
93+
SRCS
94+
fwrite.cpp
95+
HDRS
96+
../fwrite.h
97+
DEPENDS
98+
libc.src.errno.errno
99+
libc.include.stdio
100+
libc.src.__support.File.file
101+
libc.src.__support.File.platform_file
102+
)
103+
104+
add_entrypoint_object(
105+
fputc
106+
SRCS
107+
fputc.cpp
108+
HDRS
109+
../fputc.h
110+
DEPENDS
111+
libc.src.errno.errno
112+
libc.include.stdio
113+
libc.src.__support.File.file
114+
libc.src.__support.File.platform_file
115+
)
116+
117+
add_entrypoint_object(
118+
putc
119+
SRCS
120+
putc.cpp
121+
HDRS
122+
../putc.h
123+
DEPENDS
124+
libc.src.errno.errno
125+
libc.include.stdio
126+
libc.src.__support.File.file
127+
libc.src.__support.File.platform_file
128+
)
129+
130+
add_entrypoint_object(
131+
putchar
132+
SRCS
133+
putchar.cpp
134+
HDRS
135+
../putchar.h
136+
DEPENDS
137+
libc.src.errno.errno
138+
libc.include.stdio
139+
libc.src.__support.File.file
140+
libc.src.__support.File.platform_file
141+
)
142+
78143
add_entrypoint_object(
79144
stdin
80145
SRCS
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

libc/src/stdio/gpu/CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,50 @@ add_entrypoint_object(
6161
.gpu_file
6262
)
6363

64+
add_entrypoint_object(
65+
fwrite
66+
SRCS
67+
fwrite.cpp
68+
HDRS
69+
../fwrite.h
70+
DEPENDS
71+
libc.include.stdio
72+
.gpu_file
73+
)
74+
75+
add_entrypoint_object(
76+
fputc
77+
SRCS
78+
fputc.cpp
79+
HDRS
80+
../fputc.h
81+
DEPENDS
82+
libc.include.stdio
83+
.gpu_file
84+
)
85+
86+
add_entrypoint_object(
87+
putc
88+
SRCS
89+
putc.cpp
90+
HDRS
91+
../putc.h
92+
DEPENDS
93+
libc.include.stdio
94+
.gpu_file
95+
)
96+
97+
add_entrypoint_object(
98+
putchar
99+
SRCS
100+
putchar.cpp
101+
HDRS
102+
../putchar.h
103+
DEPENDS
104+
libc.include.stdio
105+
.gpu_file
106+
)
107+
64108
add_entrypoint_object(
65109
stdin
66110
SRCS

libc/src/stdio/gpu/fputc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- GPU implementation of fputc ---------------------------------------===//
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 "file.h"
10+
#include "src/stdio/fputc.h"
11+
12+
#include <stdio.h>
13+
14+
namespace __llvm_libc {
15+
16+
LLVM_LIBC_FUNCTION(int, fputc, (int c, ::FILE *stream)) {
17+
unsigned char uc = static_cast<unsigned char>(c);
18+
19+
size_t written = file::write(stream, &uc, 1);
20+
if (1 != written)
21+
return EOF;
22+
23+
return 0;
24+
}
25+
26+
} // namespace __llvm_libc

libc/src/stdio/gpu/fwrite.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- GPU implementation of fwrite --------------------------------------===//
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 "src/stdio/fwrite.h"
10+
#include "file.h"
11+
12+
#include <stdio.h>
13+
14+
namespace __llvm_libc {
15+
16+
LLVM_LIBC_FUNCTION(size_t, fwrite,
17+
(const void *__restrict buffer, size_t size, size_t nmemb,
18+
::FILE *stream)) {
19+
if (size == 0 || nmemb == 0)
20+
return 0;
21+
22+
auto result = file::write(stream, buffer, size * nmemb);
23+
return result / size;
24+
}
25+
26+
} // namespace __llvm_libc

libc/src/stdio/gpu/putc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- GPU implementation of putc ----------------------------------------===//
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 "src/stdio/putc.h"
10+
#include "file.h"
11+
12+
#include <stdio.h>
13+
14+
namespace __llvm_libc {
15+
16+
LLVM_LIBC_FUNCTION(int, putc, (int c, ::FILE *stream)) {
17+
unsigned char uc = static_cast<unsigned char>(c);
18+
19+
size_t written = file::write(stream, &uc, 1);
20+
if (1 != written)
21+
return EOF;
22+
23+
return 0;
24+
}
25+
26+
} // namespace __llvm_libc

libc/src/stdio/gpu/putchar.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- GPU implementation of putchar -------------------------------------===//
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 "file.h"
10+
#include "src/stdio/putchar.h"
11+
12+
#include <stdio.h>
13+
14+
namespace __llvm_libc {
15+
16+
LLVM_LIBC_FUNCTION(int, putchar, (int c)) {
17+
unsigned char uc = static_cast<unsigned char>(c);
18+
19+
size_t written = file::write(stdout, &uc, 1);
20+
if (1 != written)
21+
return EOF;
22+
23+
return 0;
24+
}
25+
26+
} // namespace __llvm_libc

libc/test/src/stdio/CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,20 @@ add_libc_test(
271271
libc.src.stdio.stderr
272272
)
273273

274+
add_libc_test(
275+
fputc_test
276+
HERMETIC_TEST_ONLY # writes to libc's stdout and stderr
277+
SUITE
278+
libc_stdio_unittests
279+
SRCS
280+
fputc_test.cpp
281+
DEPENDS
282+
libc.src.stdio.fputc
283+
libc.src.stdio.putchar
284+
libc.src.stdio.stdout
285+
libc.src.stdio.stderr
286+
)
287+
274288
add_libc_test(
275289
fopen_test
276290
SUITE
@@ -279,7 +293,7 @@ add_libc_test(
279293
fopen_test.cpp
280294
DEPENDS
281295
libc.src.stdio.fread
282-
libc.src.stdio.fputs
296+
libc.src.stdio.fwrite
283297
libc.src.stdio.fclose
284298
libc.src.stdio.fopen
285299
)

0 commit comments

Comments
 (0)