Skip to content

Commit a1be5d6

Browse files
authored
[libc] Implement more input functions on the GPU (#66288)
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 96ccc81 commit a1be5d6

29 files changed

+555
-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/docs/gpu/support.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ stdio.h
126126
============= ========= ============
127127
Function Name Available RPC Required
128128
============= ========= ============
129+
feof |check| |check|
130+
ferror |check| |check|
131+
clearerr |check| |check|
132+
fgetc |check| |check|
133+
fgets |check| |check|
134+
getc |check| |check|
135+
getchar |check| |check|
129136
puts |check| |check|
130137
fputs |check| |check|
131138
fputc |check| |check|

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)

0 commit comments

Comments
 (0)