aboutsummaryrefslogtreecommitdiff
path: root/libc/src/stdio
AgeCommit message (Collapse)AuthorFilesLines
2024-04-01[libc] fixup missing include for fullbuild (#87266)Nick Desaulniers2-0/+2
Fixes #86928
2024-04-01[libc][POSIX] implement fseeko, ftello (#86928)Shourya Goel8-4/+117
Fixes: #85287
2024-04-01[libc] Include algorithm.h to parser.h (#87125)Caslyn Tonelli2-0/+2
This includes algorithm.h directly to provide the definition for `cpp:max` in parser.h. This will define `max(...)` in the libc namespace for build systems that pull in parser.h explicitly.
2024-03-29Add bit width length modifier to printf (#82461)Om Prakaash5-18/+83
Resolves #81685. This adds support for wN and wfN length modifiers in fprintf.
2024-03-21[libc][stdio] implement rename via SYS_renameat2 (#86140)Nick Desaulniers1-1/+3
SYS_rename may be unavailable on architectures such as aarch64 and riscv. rename can be implemented in terms of SYS_rename, SYS_renameat, or SYS_renameat2. I don't have a full picture of the history here, but it seems that SYS_renameat might also be unavailable on some platforms. `man 2 rename` mentions that SYS_renameat2 was added in Linux 3.15. We don't need to support such ancient kernel versions prior. Link: #84980 Link: #85068
2024-03-21[libc][c11] Add stdio.h's rename() function (#85068)aniplcc4-0/+63
Adds stdio.h's rename() function as defined in n3096. Fixes #84980.
2024-03-20[libc][NFC] Move `Sign` type to separate header (#85930)Guillaume Chatelet1-1/+1
2024-03-19[libc] Make 'printf' converter output "(null)" instead of "null" (#85845)Joseph Huber1-1/+1
Summary: Currently we print `null` for the null pointer in a `%s` expression. Although it's not defined by the standard, other implementations choose to use `(null)` to indicate this. We also currently print `(nullptr)` so I think it's more consistent to use parens in both cases.
2024-03-18[libc] Implement fileno (#85628)Shourya Goel4-0/+64
fixes: #85150
2024-03-17[libc] remove.cpp dependency on libc.include.stdioPetr Hosek2-0/+3
This addresses a build error introduced by 5a75242bc898886b9833e328ad5f656246424a81.
2024-03-16[libc] Include empty remove in baremetal stdio.h (#85336)Petr Hosek2-0/+26
This is required to avoid compilation error in libc++. See #85335 for more details.
2024-03-08[libc][NFC] Move `BigInt` out of the `cpp` namespace (#84445)Guillaume Chatelet1-5/+5
As noted in https://github.com/llvm/llvm-project/pull/84035#discussion_r1516817755 only files under the CPP folder should be in the `cpp` namespace.
2024-03-07[libc] Fix forward missing `BigInt` specialization of `mask_leading_ones` / ↵Guillaume Chatelet1-2/+5
`mask_trailing_ones` (#84325) #84299 broke the arm32 build, this patch fixes it forward.
2024-02-29[libc] Revert https://github.com/llvm/llvm-project/pull/83199 since it broke ↵lntue2-2/+2
Fuchsia. (#83374) With some header fix forward for GPU builds.
2024-02-27[libc] Add "include/" to the LLVM include directories (#83199)Joseph Huber2-2/+2
Summary: Recent changes added an include path in the float128 type that used the internal `libc` path to find the macro. This doesn't work once it's installed because we need to search from the root of the install dir. This patch adds "include/" to the include path so that our inclusion of installed headers always match the internal use.
2024-02-27[libc] Add fixed point support to printf (#82707)Michael Jones9-7/+421
This patch adds the r, R, k, and K conversion specifiers to printf, with accompanying tests. They are guarded behind the LIBC_COPT_PRINTF_DISABLE_FIXED_POINT flag as well as automatic fixed point support detection.
2024-02-23[libc][NFC] Remove all trailing spaces from libc (#82831)Joseph Huber2-2/+2
Summary: There are a lot of random training spaces on various lines. This patch just got rid of all of them with `sed 's/\ \+$//g'.
2024-02-22[libc] Rework the GPU build to be a regular target (#81921)Joseph Huber1-1/+1
Summary: This is a massive patch because it reworks the entire build and everything that depends on it. This is not split up because various bots would fail otherwise. I will attempt to describe the necessary changes here. This patch completely reworks how the GPU build is built and targeted. Previously, we used a standard runtimes build and handled both NVPTX and AMDGPU in a single build via multi-targeting. This added a lot of divergence in the build system and prevented us from doing various things like building for the CPU / GPU at the same time, or exporting the startup libraries or running tests without a full rebuild. The new appraoch is to handle the GPU builds as strict cross-compiling runtimes. The first step required https://github.com/llvm/llvm-project/pull/81557 to allow the `LIBC` target to build for the GPU without touching the other targets. This means that the GPU uses all the same handling as the other builds in `libc`. The new expected way to build the GPU libc is with `LLVM_LIBC_RUNTIME_TARGETS=amdgcn-amd-amdhsa;nvptx64-nvidia-cuda`. The second step was reworking how we generated the embedded GPU library by moving it into the library install step. Where we previously had one `libcgpu.a` we now have `libcgpu-amdgpu.a` and `libcgpu-nvptx.a`. This patch includes the necessary clang / OpenMP changes to make that not break the bots when this lands. We unfortunately still require that the NVPTX target has an `internal` target for tests. This is because the NVPTX target needs to do LTO for the provided version (The offloading toolchain can handle it) but cannot use it for the native toolchain which is used for making tests. This approach is vastly superior in every way, allowing us to treat the GPU as a standard cross-compiling target. We can now install the GPU utilities to do things like use the offload tests and other fun things. Some certain utilities need to be built with `--target=${LLVM_HOST_TRIPLE}` as well. I think this is a fine workaround as we will always assume that the GPU `libc` is a cross-build with a functioning host. Depends on https://github.com/llvm/llvm-project/pull/81557
2024-02-07[libc] Support C23 'b' (binary) modifier in printf (#80851)Artem Tyurin3-6/+22
Reference: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2612.pdf. Fixes https://github.com/llvm/llvm-project/issues/80727.
2024-01-31[libc] Fix condition ordering in scanf (#80083)michaelrj-google1-4/+4
The inf and nan string index bounds checks were after the index was being used. This patch moves the index usage to the end of the condition. Fixes #79988
2024-01-23[libc][NFC] Remove `FPBits` cast operator (#79142)Guillaume Chatelet1-3/+3
The semantics for casting can range from "bitcast" (same representation) to "different representation", to "type promotion". Here we remove the cast operator and force usage of `get_val` as the only function to get the floating point value, making the intent clearer and more consistent.
2024-01-18[libc][NFC] Introduce a Sign type for FPBits (#78500)Guillaume Chatelet3-16/+13
Another patch is needed to cover `DyadicFloat` and `NormalFloat` constructors.
2024-01-03[libc][NFC] Remove `FloatProperties` (#76508)Guillaume Chatelet1-5/+4
Access is now done through `FPBits` exclusively. This patch also renames a few internal structs and uses `T` instead of `FP` as a template parameter.
2024-01-02[libc] Lock the output stream for the 'puts' call (#76513)Joseph Huber1-2/+19
Summary: The `puts` function consists of an initial write and then another write to append the newline. When executing code in parallel, it is possible for these writes to becomes disjointed. This code adds an explicit lock call to ensure that the string is always appended by the newline as the users expects. Wasn't sure if this required a test as it would be difficult since reproducing it would be flaky.
2023-12-28[libc][NFC] Integrate `FloatProperties` into `FPBits` (#76506)Guillaume Chatelet1-1/+0
`FloatProperties` is always included when `FPBits` is. This will help further refactoring.
2023-12-20[libc][NFC] clean up printf_core and scanf_core (#74535)michaelrj-google18-55/+31
Add LIBC_INLINE annotations to functions and fix variable cases within printf_core and scanf_core.
2023-12-15[libc][NFC] Rename `MANTISSA_WIDTH` in `FRACTION_LEN` (#75489)Guillaume Chatelet5-56/+56
This one might be a bit controversial since the terminology has been introduced from the start but I think `FRACTION_LEN` is a better name here. AFAICT it really is "the number of bits after the decimal dot when the number is in normal form." `MANTISSA_WIDTH` is less precise as it's unclear whether we take the leading bit into account. This patch also renames most of the properties to use the `_LEN` suffix and fixes useless casts or variables.
2023-12-14[libc][NFC] Remove MantissaWidth traits (#75458)Guillaume Chatelet2-4/+4
Same as #75362, the traits does not bring a lot of value over `FloatProperties::MANTISSA_WIDTH` (or `FPBits::MANTISSA_WIDTH`).
2023-12-14[libc][NFC] Remove ExponentWidth traits (#75362)Guillaume Chatelet1-11/+11
Is it redundant with `FloatProperties::EXPONENT_WIDTH` and does bear its weight.
2023-12-04[libc][NFC] fix int warnings in float conversion (#74379)michaelrj-google2-9/+12
The printf float to string conversion functions had some implicit integer conversion warnings on gcc. This patch adds explicit casts to these places.
2023-11-30[libc] fix getchar_unlocked (#73874)Nick Desaulniers1-2/+2
A typo was leading to getc_unlocked.cpp.o being included into libc.a twice. I only noticed because I was trying to convert libc.a to a shared object via $ ld.lld -o libc.so --whole-archive libc.a which errored since getc_unlocked was being defined twice.
2023-10-31[libc] Add a few missing casts (#70850)Roland McGrath1-2/+2
Stricter GCC warnings about implicit widening and narrowing cases necessitate additional explicit casts around some integer operations.
2023-10-30[libc] Fix printf long double truncation bound (#70705)michaelrj-google1-5/+7
The calculation for if a number being printed is truncated and should be rounded up assumed a double for one of its constants, causing occassional misrounding. This fixes that by making the constant based on the mantissa width.
2023-10-27[libc] Fix incorrect printing for alt mode ints (#70252)michaelrj-google1-2/+14
Previously, our printf would incorrectly handle conversions like ("%#x",0) and ("%#o",0). This patch corrects the behavior to match what is described in the standard.
2023-10-24[libc] Fix printf long double inf, bitcast in msan (#70067)michaelrj-google1-2/+2
These bugs were found with the new printf long double fuzzing. The long double inf vs nan bug was introduced when we changed to get_explicit_exponent. The bitcast msan issue hadn't come up previously, but isn't a real bug, just a poisoning confusion.
2023-10-19[libc] Rework the 'fgets' implementation on the GPU (#69635)Joseph Huber1-16/+12
Summary: The `fgets` function as implemented is not functional currently when called with multiple threads. This is because we rely on reapeatedly polling the character to detect EOF. This doesn't work when there are multiple threads that may with to poll the characters. this patch pulls out the logic into a standalone RPC call to handle this in a single operation such that calling it from multiple threads functions as expected. It also makes it less slow because we no longer make N RPC calls for N characters.
2023-10-17[libc] Implement the 'ungetc' function on the GPU (#69248)Joseph Huber5-12/+53
Summary: This function follows closely with the pattern of all the other functions. That is, making a new opcode and forwarding the call to the host. However, this also required modifying the test somewhat. It seems that not all `libc` implementations follow the same error rules as are tested here, and it is not explicit in the standard, so we simply disable these EOF checks when targeting the GPU.
2023-10-16[libc] Add simple long double to printf float fuzz (#68449)michaelrj-google1-2/+3
Recent testing has uncovered some hard-to-find bugs in printf's long double support. This patch adds an extra long double path to the fuzzer with minimal extra effort. While a more thorough long double fuzzer would be useful, it would need to handle the non-standard cases of 80 bit long doubles such as unnormal and pseudo-denormal numbers. For that reason, a standalone long double fuzzer is left for future development.
2023-10-11[libc] Mark operator== const to avoid ambiguity in C++20. (#68805)Samira Bazuzi1-1/+1
C++20 will automatically generate an operator== with reversed operand order, which is ambiguous with the written operator== when one argument is marked const and the other isn't. This operator currently triggers -Wambiguous-reversed-operator at usage site libc/test/UnitTest/PrintfMatcher.cpp:28.
2023-09-26[libc][NFC] Add compile options only to the header libraries which use them. ↵Siva Chandra3-6/+2
(#67447) Other libraries dependent on these libraries will automatically inherit those compile options. This change in particular affects the compile option "-DLIBC_COPT_STDIO_USE_SYSTEM_FILE".
2023-09-26[libc] Propagate printf config options from a single config header library. ↵Siva Chandra2-32/+34
(#66979) printf_core.parser is not yet updated to use the printf config options. It does not use them currently anyway and the corresponding parser_test should be updated to respect the config options.
2023-09-26[libc][Obvious] Fix incorrect filepath for ftell.h headerJoseph Huber1-1/+1
Summary: The previous patch moved the location of this CMake line but didn't update the header. Fix it.
2023-09-26[libc] Implement `fseek`, `fflush`, and `ftell` on the GPU (#67160)Joseph Huber9-39/+157
Summary: This patch adds the necessary entrypoints to handle the `fseek`, `fflush`, and `ftell` functions. These are all very straightfoward, we simply make RPC calls to the associated function on the other end. Implementing it this way allows us to more or less borrow the state of the stream from the server as we intentionally maintain no internal state on the GPU device. However, this does not implement the `errno` functinality so that must be ignored.
2023-09-26[libc] Mass replace enclosing namespace (#67032)Guillaume Chatelet147-350/+352
This is step 4 of https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079
2023-09-25[libc] Acquire the lock for scanf files (#67357)michaelrj-google1-3/+19
When creating the new scanf reader design, I forgot to add back the calls to flockfile and funlockfile in vfscanf_internal. This patch fixes that, and also changes the system file version to use the normal variants since ungetc_unlocked isn't always available.
2023-09-25[libc] Change the `puts` implementation on the GPU (#67189)Joseph Huber1-5/+3
Summary: Normally, the implementation of `puts` simply writes a second newline charcter after printing the first string. However, because the GPU does everything in batches of the SIMT group size, this will end up with very poor output where you get the strings printed and then 1-64 newline characters all in a row. Optimizations like to turn `printf` calls into `puts` so it's a good idea to make this produce the expected output. The least invasive way I could do this was to add a new opcode. It's a little bloated, but it avoids an unneccessary and slow send operation to configure this.
2023-09-22[libc] Refactor scanf reader to match printf (#66023)michaelrj-google16-248/+143
In a previous patch, the printf writer was rewritten to use a single writer class with a buffer and a callback hook. This patch refactors scanf's reader to match conceptually.
2023-09-21[libc] Template the printf / scanf parser class (#66277)Joseph Huber8-740/+625
Summary: The parser class for stdio currently accepts different argument providers. In-tree this is only used for a fuzzer test, however, the proposed implementation of the GPU handling of printf / scanf will require custom argument handlers. This makes the current approach of using a preprocessor macro messier. This path proposed folding this logic into a template instantiation. The downside to this is that because the implementation of the parser class is placed into an implementation file we need to manually instantiate the needed templates which will slightly bloat binary size. Alternatively we could remove the implementation file, or key off of the `libc` external packaging macro so it is not present in the installed version.
2023-09-21[libc] Fix and simplify the implementation of 'fread' on the GPU (#66948)Joseph Huber1-41/+36
Summary: Previously, the `fread` operation was wrong in cases when we read less data than was requested. That is, if we tried to read N bytes while the file was in EOF, it would still copy N bytes of garbage. This is fixed by only copying over the sizes we got from locally opening it rather than just using the provided size. Additionally, this patch simplifies the interface. The output functions have special variants for writing to stdout / stderr. This is primarily an optimization for these common cases so we can avoid sending the stream as an argument which has a high delay. Because for input, we already need to start with a `send` to tell the server how much data to read, it costs us nothing to send the file along with it so this is redundant. Re-use the file encoding scheme from the other implementations, the one that stores the stream type in the LSBs of the FILE pointer.
2023-09-20[libc][Obvious] Fix incorrect RPC opcode for `clearerr`Joseph Huber1-1/+1
Summary: This was mistakenly using the opcode for `ferror` which wasn't noticed because tests using this weren't yet activated. This patch fixes this mistake.