aboutsummaryrefslogtreecommitdiff
path: root/libc/src/stdio
AgeCommit message (Collapse)AuthorFilesLines
2025-12-22[libc] Split out src/__support/alloc-checker.h (#173104)Roland McGrath1-0/+1
This moves the libc-internal AllocChecker API out of src/__support/CPP/new.h and updates CPP/README.md to state the intent to keep src/__support/CPP and the LIBC_NAMESPACE::cpp namespace a "pure" subset of standard C++ API polyfills.
2025-12-17[libc] Cast the error value to int (#172724)Petr Hosek1-1/+1
The negative value represents an error and we already use cast in this scenario elsewhere.
2025-12-17[libc] Support opaque FILE* on baremetal (#168931)Petr Hosek26-188/+906
This change expands the stdio support on baremetal to support opaque FILE*. This builds on top of the existing baremetal embedding API; we treat the standard FILE* streams as pointers that point to the cookie symbols which are a part of the embedding API. This also allows users to define their own FILE* streams, but we don't (yet) support the API that return FILE* such as fopen or fopencookie.
2025-12-11[libc] Refactor static polymorphism in WriteBuffer (NFC). (#169089)Alexey Samsonov9-85/+119
There are three flavors of WriteBuffer currently, all of which could be passed into `printf_core::Writer` class. It's a tricky class, since it chooses a flavor-specific logic either based on runtime dispatch (to save code size and prevent generating three versions of the entirety of printf_core), or based on template arguments (to avoid dealing with function pointers in codegen for `FILL_BUFF_AND_DROP_OVERFLOW` path). Refactor this somewhat convoluted logic to have three concrete subclasses inheriting from the templated base class, and use static polymorphism with `reinterpret_cast` to implement dispatching above. Now we can actually have flavor-specific fields, constructors, and methods (e.g. `flush_to_stream` is now a method of `FlushingBuffer`), and the code on the user side is cleaner: the complexity of enabling/disabling runtime-dispatch and using proper template arguments is now localized in `writer.h`. This code will need to be further templatized to support buffers of type `wchar_t` to implement `swprintf()` and friends. This change would make it (ever so slightly) easier.
2025-11-10[libc] fwrite_unlocked: only return errno if an actual error occurred. (#167350)Sterling-Augustine1-2/+5
fwrite and friends don't modify errno if no error occurred. Therefore frite_unlocked's return value shouldn't be constructed from errno without checking if an error actually occurred. This fixes an error introduced by https://github.com/llvm/llvm-project/commit/9e2f73fe9052a4fbf382a06e30b2441c6d99fb7e
2025-11-05[libc] Add printf error handling (with fixes #2) (#166517)Marcell Leleszi27-67/+456
https://github.com/llvm/llvm-project/issues/159474 Another try of trying to land https://github.com/llvm/llvm-project/pull/166382 - Fix some leftover tests checking for specific errnos - Guard errno checking tests to not run on the GPU @michaelrj-google
2025-11-05[libc] Migrate ctype_utils to use char instead of int where applicable. ↵Alexey Samsonov2-6/+5
(#166225) Functions like isalpha / tolower can operate on chars internally. This allows us to get rid of unnecessary casts and open a way to creating wchar_t overloads with the same names (e.g. for isalpha), that would simplify templated code for conversion functions (see 315dfe5865962d8a3d60e21d1fffce5214fe54ef). Add the int->char converstion to public entrypoints implementation instead. We also need to introduce bounds check on the input argument values - these functions' behavior is unspecified if the argument is neither EOF nor fits in "unsigned char" range, but the tests we've had verified that they always return false for small negative values. To preserve this behavior, cover it explicitly.
2025-11-04Revert commit d8e5698 and 15b19c7 (#166498)Kewen Meng27-456/+67
2025-11-04[libc] Add printf error handling (with fixes) (#166382)Marcell Leleszi27-67/+456
https://github.com/llvm/llvm-project/issues/159474 Resubmitting https://github.com/llvm/llvm-project/pull/162876 with fixes as it broke some buildbots: - Fix comparisons of integer expressions of different signedness - Not check for specific errnos in tests, as they might not be available on all platforms
2025-11-03Revert "[libc] Add printf error handling" (#166232)Kewen Meng27-456/+67
2025-11-03[libc] Add printf error handling (#162876)Marcell Leleszi27-67/+456
[#159474](https://github.com/llvm/llvm-project/issues/159474) - All printf variants set errno and consistently return -1 on error, instead of returning various predefined error codes - Return value overflow handling is added
2025-09-18[libc][bazel] Add (v)asprintf targets and tests (#159476)Michael Jones1-1/+0
2025-09-12[libc] Change __builtin_memcpy to inline_memcpy. (#158345)lntue2-2/+4
2025-09-01Revert "[libc] Migrate from baremetal stdio.h to generic stdio.h" (#156371)William Huynh7-5/+317
Reverts llvm/llvm-project#152748
2025-09-01[libc] Migrate from baremetal stdio.h to generic stdio.h (#152748)William Huynh7-317/+5
This is a follow up to the RFC here: https://discourse.llvm.org/t/rfc-implementation-of-stdio-on-baremetal/86944 This provides the stdout/stderr/stdin symbols (which now don't have to provided by the user). This allows the user to have access to all functions, currently I've only tested `fprintf` but in theory everything that works in the generic folder should work in the baremetal configuration. All streams are _non-buffered_, which does NOT require flushing. It is based on the CookieFile that already existed
2025-07-27[libc] Add misssing inttypes dependencies (#150861)Haowei2-0/+3
This is a follow up of 9e7999147de757107482d8a2cedab4155a0b6635. It attempts to fix the CI flakes we saw on fuchsia-linux-x64 builder.
2025-07-24[libc] Add missing libc.include.inttypes for targets including <inttypes.h>. ↵lntue1-0/+2
(#150345)
2025-07-23[libc][NFC] Add stdint.h proxy header to fix dependency issue with ↵lntue2-2/+2
<stdint.h> includes. (#150303) https://github.com/llvm/llvm-project/issues/149993
2025-06-16[libc] Change default behaviour of baremetal/printf to use stdout (#143703)William Huynh4-11/+11
In #94078, `write_to_stdout` had not been fully implemented. However, now that it has been implemented, to conform with the C standard (7.23.6.3. The printf function, specifically point 2), we use `stdout`. This issue is tracked in #94685. - Also prefer `static constexpr` - Made it explicit that we are writing to `stdout`
2025-06-12[libc] Implement perror (#143624)Michael Jones4-0/+117
The perror function writes an error message directly to stderr. This patch adds an implementation, tests, and header generation details.
2025-06-11[libc] Move libc_errno.h to libc/src/__support and make ↵lntue32-32/+32
LIBC_ERRNO_MODE_SYSTEM to be header-only. (#143187) This is the first step in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-06-06[libc][GPU] clean up includes (#143203)Michael Jones32-68/+69
The GPU stdio functions were depending on indirect inclusion for some of their dependencies. This patch should fix all of that.
2025-06-06[libc] Fix missing includes after transitive dependency changedJoseph Huber2-0/+3
2025-05-01[libc] Fix typo in uint128 type for PPC printf handling (#138157)Joseph Huber1-1/+1
2025-04-23[libc] Special case PPC double double for print (#136614)Joseph Huber1-1/+6
Summary: We use the storage class for `long double` in the printing implementations. We don't fully support the PPC double double type, which that maps to, but we can stub out just the support needed for the print interface to works. This required using the internal interface for storage type, but it should be good enough. Fixes: https://github.com/llvm/llvm-project/issues/136596
2025-04-17[libc] Fix incorrect unsigned comparison (#135595)Wu Yingcong1-9/+7
There is a problem with such unsigned comparison pattern: ``` if(unsigned_a - unsigned_b > 0) { /* only NOT go here when unsigned_a==unsigned_b */ } ``` When `unsigned_a` < `unsigned_b`, the result will still be `>0` due to underflow. This patch fixes two of the occurrences I found. Also remove two redundant `if` where its condition is guaranteed by outer `if`.
2025-03-21[libc] Fix unresolved stdio symbols (#132403)Joseph Huber2-46/+55
Summary: We have 'generic' implementations for some functions stdio functions. The current logic mandates that all generic functions are implemented by the target. This obviously isn't true and this caused the GPU builds to fail once baremtal added some extra ones. This patch changes the logic to always include the generic sources only if they aren't already defined. This can probably be cleaned up and formalized later, since this pattern is copied in many places, but for now this fixes the failing GPU build bots.
2025-03-20[libc] Support for scanf on baremetal (#131043)Petr Hosek12-64/+197
This uses the templatized scanf Reader interface introduced in #131037.
2025-03-18[libc] Fix the GPU build after scanf reworkJoseph Huber1-0/+2
Summary: Needs these to be included.
2025-03-17[libc] Templatize the scanf Reader interface (#131037)Petr Hosek20-856/+715
This allows specializing the implementation for different targets without including unnecessary logic and is similar to #111559 which did the same for printf Writer interface.
2025-03-13[stdio][baremetal] Fix templating for print functions (#131232)PiJoules2-4/+4
This addresses build errors from https://github.com/llvm/llvm-project/pull/111559.
2025-03-13[libc] Fix non-templated uses of `printf_core::Writer` (#131149)Simon Tatham3-19/+29
Commit 598e882ee88a1e3 turned `Writer` into a template, and updated most of the call sites that use it. But not all. The alternative FP printf system in `float_dec_converter_limited.h` wasn't updated, and neither was `baremetal/printf.cpp` or `baremetal/vprintf.cpp`. This patch updates `float_dec_converter_limited.h` in the same way that the previous commit updated `float_dec_converter.h`: propagate the templatedness through everything in the header, so that anything using a `Writer` at all has a `write_mode` template parameter to pass on to it. `printf.cpp` and `vprintf.cpp` are updated in the same way that the previous commit updated `printf_core/vfprintf_internal.h`: the `WriteBuffer` has parameter `WriteMode::FLUSH_TO_STREAM`, and `Writer` picks it up implicitly from that.
2025-03-12[libc] Template the writing mode for the writer class (#111559)Joseph Huber23-289/+293
Summary: Currently we dispatch the writing mode off of a runtime enum passed in by the constructor. This causes very unfortunate codegen for the GPU targets where we get worst-case codegen because of the unused function pointer for `sprintf`. Instead, this patch moves all of this to a template so it can be masked out. This results in no dynamic stack and uses 60 VGPRs instead of 117. It also compiles about 5x as fast.
2025-03-10[libc] Add `-Wno-sign-conversion` & re-attempt `-Wconversion` (#129811)Vinay Deshmukh3-15/+19
Relates to https://github.com/llvm/llvm-project/issues/119281#issuecomment-2699470459
2025-03-05Revert "[libc] Enable -Wconversion for tests. (#127523)"Augie Fackler3-19/+15
This reverts commit 1e6e845d49a336e9da7ca6c576ec45c0b419b5f6 because it changed the 1st parameter of adjust() to be unsigned, but libc itself calls adjust() with a negative argument in align_backward() in op_generic.h.
2025-03-04[libc] Enable -Wconversion for tests. (#127523)Vinay Deshmukh3-15/+19
Relates to: #119281
2025-02-25[libc] Move fileno and fdopen to fullbuild only (#128762)Michael Jones1-2/+1
Both fileno and fdopen require interfacing with the opaque FILE struct, so they shouldn't be enabled in overlay mode. This patch moves both into fullbuild only on all platforms. Fixes #128643
2025-02-22[libc] Fix scanf dependencies on the GPU buildJoseph Huber1-1/+2
Summary: The GPU build doesn't have `file` as a struct but does use these targets. We need to add an escape for this.
2025-02-21[libc] Skip scanf internals when no file available (#128097)Michael Jones1-0/+6
A temporary fix based on discussions in #128079 Currently, baremetal targets are failing to build because the scanf internals require FILE* (either from the system's libc or our libc). Normally we'd just turn off the broken entrypoint, but since the scanf internals are built separately that doesn't work. This patch adds extra conditions to building those internals, which we can hopefully remove once we have a proper way to build scanf for embedded.
2025-02-20[libc] Fix scanf cmake for targets without FILE (#128056)Michael Jones1-34/+23
Another followup fix to #121215 The new cmake wouldn't define the readerat all if the target wasn't GPU or didn't have a definition of FILE. This patch rewrites the cmake to be more general. As a followup, I'd like to make `use_system_file` consistent between /test and /src. Currently in /src it includes the `COMPILE_OPTIONS` and in /test it does not.
2025-02-20[libc] Add missing dependencies on `getc` and `ungetc` for GPU (#128069)Joseph Huber1-0/+2
Summary: These are required because we don't use the `file` interface.
2025-02-20[libc] Fix missing includes in GPU scanf reader (#128049)Michael Jones2-2/+5
In #121215 the reader was reorganized and the definitions of the internal getc and ungetc functions were moved, but the includes that the GPU builder depends on were not. This patch moves the includes to the correct new place.
2025-02-20[libc]: Clean up unnecessary function pointers in scanf (#121215)Vinay Deshmukh4-78/+86
Resolves #115394 1. Move definitions of cross-platform `getc` `ungetc` to `reader.h`. 2. Remove function pointer members to define them once per platform in `.h` 3. Built in overlay mode in macOS m1 4. Remove `reader.cpp` as it's empty now Also, full build doesn't yet build on macos m1 AFAIK
2025-02-06[libc] Fix recently introduced integer-type warnings (#125864)Simon Tatham1-4/+4
These warnings all come up in code modified by one of my two recent commits c06d0ff806b72b1 and b53da77c505a2d3, and all relate to implicit integer type conversion. In a build of ours with strict compile options two of them became errors. Even without that problem, it's worth fixing them to reduce noise that might hide a more serious warning.
2025-02-04[libc][float_dec_converter_limited] Add missing LIBC_INLINE (#125655)Simon Tatham1-1/+1
This caused a build failure in check-libc introduced by commit b53da77c505a2d3.
2025-02-04[libc] Alternative algorithm for decimal FP printf (#123643)Simon Tatham3-0/+698
The existing options for bin→dec float conversion are all based on the Ryū algorithm, which generates 9 output digits at a time using a table lookup. For users who can't afford the space cost of the table, the table-lookup subroutine is replaced with one that computes the needed table entry on demand, but the algorithm is otherwise unmodified. The performance problem with computing table entries on demand is that now you need to calculate a power of 10 for each 9 digits you output. But if you're calculating a custom power of 10 anyway, it's easier to just compute one, and multiply the _whole_ mantissa by it. This patch adds a header file alongside `float_dec_converter.h`, which replaces the whole Ryū system instead of just the table-lookup routine, implementing this alternative simpler algorithm. The result is accurate enough to satisfy (minimally) the accuracy demands of IEEE 754-2019 even in 128-bit long double. The new float128 test cases demonstrate this by testing the cases closest to the 39-digit rounding boundary. In my tests of generating 39 output digits (the maximum number supported by this algorithm) this code is also both faster and smaller than the USE_DYADIC_FLOAT version of the existing Ryū code.
2024-12-03[libc] Change ctype to be encoding independent (#110574)Michael Jones9-62/+51
The previous implementation of the ctype functions assumed ASCII. This patch changes to a switch/case implementation that looks odd, but actually is easier for the compiler to understand and optimize.
2024-12-02[libc][NFC] Rename RPC opcodes to better reflect their usageJoseph Huber15-32/+32
Summary: RPC_ is a generic prefix here, use LIBC_ to indicate that these are opcodes used to implement the C library
2024-11-19[libc] Increase RPC opcode to 32-bit and use a class byte (#116905)Joseph Huber2-2/+2
Summary: Currently, the RPC interface uses a basic opcode to communicate with the server. This currently is 16 bits. There's no reason for this to be 16 bits, because on the GPU a 32-bit write is the same as a 16-bit write performance wise. Additionally, I am now making all the `libc` based opcodes qualified with the 'c' type, mimiciing how Linux handles `ioctls` all coming from the same driver. This will make it easier to extend the interface when it's exported directly.
2024-11-19[libc] Replace usage of GPU helpers with ones from 'gpuintrin.h' (#116454)Joseph Huber1-0/+1
Summary: These are provided by a resource header now, cut these from the dependencies and only provide the ones we use for RPC.