Skip to content

Commit b85ea4f

Browse files
committed
[libc] Implement more input functions on the GPU
Summary: This patch implements the `fgets`, `getc`, `fgetc`, and `getchar` functions on the GPU. Their implementations are straightforward enough. One thing worth noting is that the implementation of `fgets` will be extremely slow due to the high latency to read a single char. A faster solution would be to make a new RPC call to call `fgets` (due to the special rule that newline or null breaks the stream). But this is left out because performance isn't the primary concern here.
1 parent 089b811 commit b85ea4f

27 files changed

+522
-166
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ set(TARGET_LIBC_ENTRYPOINTS
8686
libc.src.errno.errno
8787

8888
# stdio.h entrypoints
89+
libc.src.stdio.feof
90+
libc.src.stdio.ferror
91+
libc.src.stdio.clearerr
8992
libc.src.stdio.puts
9093
libc.src.stdio.fopen
9194
libc.src.stdio.fclose
@@ -95,6 +98,10 @@ set(TARGET_LIBC_ENTRYPOINTS
9598
libc.src.stdio.fputc
9699
libc.src.stdio.putc
97100
libc.src.stdio.putchar
101+
libc.src.stdio.fgets
102+
libc.src.stdio.fgetc
103+
libc.src.stdio.getc
104+
libc.src.stdio.getchar
98105
libc.src.stdio.stdin
99106
libc.src.stdio.stdout
100107
libc.src.stdio.stderr

libc/include/llvm-libc-types/rpc_opcodes_t.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ typedef enum : unsigned short {
2323
RPC_FREE = 10,
2424
RPC_HOST_CALL = 11,
2525
RPC_ABORT = 12,
26+
RPC_FEOF = 13,
27+
RPC_FERROR = 14,
28+
RPC_CLEARERR = 15,
2629
} rpc_opcode_t;
2730

2831
#endif // __LLVM_LIBC_TYPES_RPC_OPCODE_H__

libc/src/stdio/CMakeLists.txt

Lines changed: 10 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -29,169 +29,6 @@ endif()
2929
add_subdirectory(printf_core)
3030
add_subdirectory(scanf_core)
3131

32-
add_entrypoint_object(
33-
clearerr
34-
SRCS
35-
clearerr.cpp
36-
HDRS
37-
clearerr.h
38-
DEPENDS
39-
libc.include.stdio
40-
libc.src.__support.File.file
41-
libc.src.__support.File.platform_file
42-
)
43-
44-
add_entrypoint_object(
45-
clearerr_unlocked
46-
SRCS
47-
clearerr_unlocked.cpp
48-
HDRS
49-
clearerr_unlocked.h
50-
DEPENDS
51-
libc.include.stdio
52-
libc.src.__support.File.file
53-
libc.src.__support.File.platform_file
54-
)
55-
56-
add_entrypoint_object(
57-
feof
58-
SRCS
59-
feof.cpp
60-
HDRS
61-
feof.h
62-
DEPENDS
63-
libc.include.stdio
64-
libc.src.__support.File.file
65-
libc.src.__support.File.platform_file
66-
)
67-
68-
add_entrypoint_object(
69-
feof_unlocked
70-
SRCS
71-
feof_unlocked.cpp
72-
HDRS
73-
feof_unlocked.h
74-
DEPENDS
75-
libc.include.stdio
76-
libc.src.__support.File.file
77-
libc.src.__support.File.platform_file
78-
)
79-
80-
add_entrypoint_object(
81-
ferror
82-
SRCS
83-
ferror.cpp
84-
HDRS
85-
ferror.h
86-
DEPENDS
87-
libc.include.stdio
88-
libc.src.__support.File.file
89-
libc.src.__support.File.platform_file
90-
)
91-
92-
add_entrypoint_object(
93-
ferror_unlocked
94-
SRCS
95-
ferror_unlocked.cpp
96-
HDRS
97-
ferror_unlocked.h
98-
DEPENDS
99-
libc.include.stdio
100-
libc.src.__support.File.file
101-
libc.src.__support.File.platform_file
102-
)
103-
104-
add_entrypoint_object(
105-
fgetc
106-
SRCS
107-
fgetc.cpp
108-
HDRS
109-
fgetc.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-
fgetc_unlocked
119-
SRCS
120-
fgetc_unlocked.cpp
121-
HDRS
122-
fgetc_unlocked.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-
getc
132-
SRCS
133-
getc.cpp
134-
HDRS
135-
getc.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-
143-
add_entrypoint_object(
144-
getc_unlocked
145-
SRCS
146-
getc_unlocked.cpp
147-
HDRS
148-
getc_unlocked.h
149-
DEPENDS
150-
libc.src.errno.errno
151-
libc.include.stdio
152-
libc.src.__support.File.file
153-
libc.src.__support.File.platform_file
154-
)
155-
156-
add_entrypoint_object(
157-
getchar
158-
SRCS
159-
getchar.cpp
160-
HDRS
161-
getchar.h
162-
DEPENDS
163-
libc.src.errno.errno
164-
libc.include.stdio
165-
libc.src.__support.File.file
166-
libc.src.__support.File.platform_file
167-
)
168-
169-
add_entrypoint_object(
170-
getchar_unlocked
171-
SRCS
172-
getc_unlocked.cpp
173-
HDRS
174-
getc_unlocked.h
175-
DEPENDS
176-
libc.src.errno.errno
177-
libc.include.stdio
178-
libc.src.__support.File.file
179-
libc.src.__support.File.platform_file
180-
)
181-
182-
add_entrypoint_object(
183-
fgets
184-
SRCS
185-
fgets.cpp
186-
HDRS
187-
fgets.h
188-
DEPENDS
189-
libc.src.errno.errno
190-
libc.include.stdio
191-
libc.src.__support.File.file
192-
libc.src.__support.File.platform_file
193-
)
194-
19532
add_entrypoint_object(
19633
fflush
19734
SRCS
@@ -470,6 +307,9 @@ add_entrypoint_object(
470307
)
471308

472309
# These entrypoints have multiple potential implementations.
310+
add_stdio_entrypoint_object(feof)
311+
add_stdio_entrypoint_object(ferror)
312+
add_stdio_entrypoint_object(clearerr)
473313
add_stdio_entrypoint_object(fopen)
474314
add_stdio_entrypoint_object(fclose)
475315
add_stdio_entrypoint_object(fread_unlocked)
@@ -481,6 +321,13 @@ add_stdio_entrypoint_object(fwrite)
481321
add_stdio_entrypoint_object(fputc)
482322
add_stdio_entrypoint_object(putc)
483323
add_stdio_entrypoint_object(putchar)
324+
add_stdio_entrypoint_object(fgetc)
325+
add_stdio_entrypoint_object(fgetc_unlocked)
326+
add_stdio_entrypoint_object(getc)
327+
add_stdio_entrypoint_object(getc_unlocked)
328+
add_stdio_entrypoint_object(getchar)
329+
add_stdio_entrypoint_object(getchar_unlocked)
330+
add_stdio_entrypoint_object(fgets)
484331
add_stdio_entrypoint_object(stdin)
485332
add_stdio_entrypoint_object(stdout)
486333
add_stdio_entrypoint_object(stderr)

libc/src/stdio/generic/CMakeLists.txt

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,75 @@
1+
add_entrypoint_object(
2+
clearerr
3+
SRCS
4+
clearerr.cpp
5+
HDRS
6+
../clearerr.h
7+
DEPENDS
8+
libc.include.stdio
9+
libc.src.__support.File.file
10+
libc.src.__support.File.platform_file
11+
)
12+
13+
add_entrypoint_object(
14+
clearerr_unlocked
15+
SRCS
16+
clearerr_unlocked.cpp
17+
HDRS
18+
../clearerr_unlocked.h
19+
DEPENDS
20+
libc.include.stdio
21+
libc.src.__support.File.file
22+
libc.src.__support.File.platform_file
23+
)
24+
25+
add_entrypoint_object(
26+
feof
27+
SRCS
28+
feof.cpp
29+
HDRS
30+
../feof.h
31+
DEPENDS
32+
libc.include.stdio
33+
libc.src.__support.File.file
34+
libc.src.__support.File.platform_file
35+
)
36+
37+
add_entrypoint_object(
38+
feof_unlocked
39+
SRCS
40+
feof_unlocked.cpp
41+
HDRS
42+
../feof_unlocked.h
43+
DEPENDS
44+
libc.include.stdio
45+
libc.src.__support.File.file
46+
libc.src.__support.File.platform_file
47+
)
48+
49+
add_entrypoint_object(
50+
ferror
51+
SRCS
52+
ferror.cpp
53+
HDRS
54+
../ferror.h
55+
DEPENDS
56+
libc.include.stdio
57+
libc.src.__support.File.file
58+
libc.src.__support.File.platform_file
59+
)
60+
61+
add_entrypoint_object(
62+
ferror_unlocked
63+
SRCS
64+
ferror_unlocked.cpp
65+
HDRS
66+
../ferror_unlocked.h
67+
DEPENDS
68+
libc.include.stdio
69+
libc.src.__support.File.file
70+
libc.src.__support.File.platform_file
71+
)
72+
173
add_entrypoint_object(
274
fopen
375
SRCS
@@ -140,6 +212,97 @@ add_entrypoint_object(
140212
libc.src.__support.File.platform_file
141213
)
142214

215+
add_entrypoint_object(
216+
fgetc
217+
SRCS
218+
fgetc.cpp
219+
HDRS
220+
../fgetc.h
221+
DEPENDS
222+
libc.src.errno.errno
223+
libc.include.stdio
224+
libc.src.__support.File.file
225+
libc.src.__support.File.platform_file
226+
)
227+
228+
add_entrypoint_object(
229+
fgetc_unlocked
230+
SRCS
231+
fgetc_unlocked.cpp
232+
HDRS
233+
../fgetc_unlocked.h
234+
DEPENDS
235+
libc.src.errno.errno
236+
libc.include.stdio
237+
libc.src.__support.File.file
238+
libc.src.__support.File.platform_file
239+
)
240+
241+
add_entrypoint_object(
242+
getc
243+
SRCS
244+
getc.cpp
245+
HDRS
246+
../getc.h
247+
DEPENDS
248+
libc.src.errno.errno
249+
libc.include.stdio
250+
libc.src.__support.File.file
251+
libc.src.__support.File.platform_file
252+
)
253+
254+
add_entrypoint_object(
255+
getc_unlocked
256+
SRCS
257+
getc_unlocked.cpp
258+
HDRS
259+
../getc_unlocked.h
260+
DEPENDS
261+
libc.src.errno.errno
262+
libc.include.stdio
263+
libc.src.__support.File.file
264+
libc.src.__support.File.platform_file
265+
)
266+
267+
add_entrypoint_object(
268+
getchar
269+
SRCS
270+
getchar.cpp
271+
HDRS
272+
../getchar.h
273+
DEPENDS
274+
libc.src.errno.errno
275+
libc.include.stdio
276+
libc.src.__support.File.file
277+
libc.src.__support.File.platform_file
278+
)
279+
280+
add_entrypoint_object(
281+
getchar_unlocked
282+
SRCS
283+
getc_unlocked.cpp
284+
HDRS
285+
../getc_unlocked.h
286+
DEPENDS
287+
libc.src.errno.errno
288+
libc.include.stdio
289+
libc.src.__support.File.file
290+
libc.src.__support.File.platform_file
291+
)
292+
293+
add_entrypoint_object(
294+
fgets
295+
SRCS
296+
fgets.cpp
297+
HDRS
298+
../fgets.h
299+
DEPENDS
300+
libc.src.errno.errno
301+
libc.include.stdio
302+
libc.src.__support.File.file
303+
libc.src.__support.File.platform_file
304+
)
305+
143306
add_entrypoint_object(
144307
stdin
145308
SRCS
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)