aboutsummaryrefslogtreecommitdiff
path: root/libc/src/stdlib
AgeCommit message (Collapse)AuthorFilesLines
2025-07-02[libc] Efficiently implement `aligned_alloc` for AMDGPU (#146585)Joseph Huber1-9/+9
Summary: This patch uses the actual allocator interface to implement `aligned_alloc`. We do this by simply rounding up the amount allocated. Because of how index calculation works, any offset within an allocated pointer will still map to the same chunk, so we can just adjust internally and it will free all the same.
2025-06-30[libc] Efficiently implement 'realloc' for AMDGPU devices (#145960)Joseph Huber1-11/+9
Summary: Now that we have `malloc` we can implement `realloc` efficiently. This uses the known chunk sizes to avoid unnecessary allocations. We just return nullptr for NVPTX. I'd remove the list for the entrypoint but then the libc++ code would stop working. When someone writes the NVPTX support this will be trivial.
2025-06-11[libc] Move libc_errno.h to libc/src/__support and make ↵lntue18-18/+18
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-05-05qsort_r.h: qsort_r() is POSIX now. (#138545)enh-google1-4/+3
2025-03-28[libc] implement `memalignment` (#132493)Mohamed Emad3-0/+57
This patch adds the `memalignment` function to LLVM-libc, following its description in [WG14 N3220, §7.24.2.1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=387). - [x] Add the implementation of `memalignment` in [`/src/stdlib`](https://github.com/llvm/llvm-project/tree/main/libc/src/stdlib) - [x] Add tests for `memalignment` in [`/test/src/stdlib`](https://github.com/llvm/llvm-project/tree/main/libc/test/src/stdlib) - [x] Add `memalignment` to [`entrypoints.txt`](https://github.com/llvm/llvm-project/blob/main/libc/config/linux/x86_64/entrypoints.txt) for at least x86_64 and whatever you're building on - [x] Add `memalignment` to [`include/stdlib.yaml`](https://github.com/llvm/llvm-project/blob/main/libc/include/stdlib.yaml) Closes #132300 --------- Co-authored-by: Joseph Huber <huberjn@outlook.com>
2025-03-12[libc] Template the writing mode for the writer class (#111559)Joseph Huber4-8/+14
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 Deshmukh1-1/+3
Relates to https://github.com/llvm/llvm-project/issues/119281#issuecomment-2699470459
2025-03-05Revert "[libc] Enable -Wconversion for tests. (#127523)"Augie Fackler1-3/+1
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 Deshmukh1-1/+3
Relates to: #119281
2025-03-04[libc] Add a missing includeDmitri Gribenko1-0/+2
This is a fixup for https://github.com/llvm/llvm-project/commit/da6d5fa79a558b66c281bed3f5ce848a69a65208.
2025-03-03Add missing LIBC_INLINE to qsort_pivot.h (#126249)Lukas Bergdoll1-5/+6
Fixes #111495
2025-02-27[libc] implement l64a (#129099)Michael Jones4-0/+96
Adds l64a, which generates the base 64 string expected by a64l.
2025-02-25[libc] implement a64l (#128758)Michael Jones3-0/+95
Implement the posix function a64l. Standard: https://pubs.opengroup.org/onlinepubs/9799919799/functions/a64l.html
2025-01-24[libc] Use the NVIDIA device allocator for GPU malloc (#124277)Joseph Huber2-0/+8
Summary: This is a blocker on another patch in the OpenMP runtime. The problem is that NVIDIA truly doesn't handle RPC-based allocations very well. It cannot reliably update the MMU while a kernel is running and it will usually deadlock if called from a separate thread due to internal use of TLS. This patch just removes the definition of `malloc` and `free` for NVPTX. The result here is that they will be undefined, which is the cue for the `nvlink` linker to define them for us. So, as far as `libc` is concerned it still implements malloc.
2025-01-23[libc][NFC] Strip all training whitespace and missing newlines (#124163)Joseph Huber1-3/+3
2025-01-08[libc] Add hardening for FixedVector data structure and fix exposed bug. ↵Alexey Samsonov1-1/+1
(#122159) Add LIBC_ASSERT statements to FixedVector implementation, and zero out the memory when the elements are removed to flag out-of-bound access and dangling pointer/reference access. This change unmasks the bug in one of FixedVector uses for atexit handlers: dangling reference use, which was actually led to crashes in the wild (with prod blockstore implementation). Fix it in this CL.
2025-01-06Fix after #121482 (#121764)JoelWee1-1/+1
2025-01-05[libc] Improve qsort (with build fix) (#121482)Lukas Bergdoll7-149/+390
2024-12-29Revert "[libc] Improve qsort" (#121303)Schrodinger ZHU Yifan7-390/+149
Reverts llvm/llvm-project#120450
2024-12-29[libc] Improve qsort (#120450)Lukas Bergdoll7-149/+390
2024-12-16[libc] Breakup freelist_malloc into separate files (#119806)Petr Hosek7-50/+143
This better matches the structure we use for the rest of libc.
2024-12-12Revert "[libc] Breakup freelist_malloc into separate files" (#119749)Petr Hosek7-143/+50
Reverts llvm/llvm-project#98784 which broke libc builders.
2024-12-12[libc] Breakup freelist_malloc into separate files (#98784)Petr Hosek7-50/+143
This better matches the structure we use for the rest of libc.
2024-12-02[libc][NFC] Rename RPC opcodes to better reflect their usageJoseph Huber2-2/+2
Summary: RPC_ is a generic prefix here, use LIBC_ to indicate that these are opcodes used to implement the C library
2024-11-21Reapply "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"Daniel Thornburgh1-2/+2
This reverts commit 93b83642ee34d0092b94776728dad0117c2b72a1. - Correct riscv32 assumption about alignment (bit of a hack). - Fix test case where the largest_small and smallest sizes are the same.
2024-11-21Revert "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"Daniel Thornburgh1-2/+2
This reverts commit b05600d96f46697e21f6b1b7ad901391326243a8. riscv32 unit test still broken
2024-11-21Reapply "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)Daniel Thornburgh1-2/+2
- Fix assertion expressions. - Fix incorrect small size in freestore_test. - There may only be one small size for high alignment and small pointers (riscv32). - Don't rely on stack alignment in FreeList test.
2024-11-20Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)Daniel Thornburgh1-2/+2
Reverts llvm/llvm-project#106259 Unit tests break on AArch64.
2024-11-20[libc] Use best-fit binary trie to make malloc logarithmic (#106259)Daniel Thornburgh1-2/+2
This reworks the free store implementation in libc's malloc to use a dlmalloc-style binary trie of circularly linked FIFO free lists. This data structure can be maintained in logarithmic time, but it still permits a relatively small implementation compared to other logarithmic-time ordered maps. The implementation doesn't do the various bitwise tricks or optimizations used in actual dlmalloc; it instead optimizes for (relative) readability and minimum code size. Specific optimization can be added as necessary given future profiling.
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.
2024-11-01[libc] Remove the #include <stdlib.h> header (#114453)Job Henandez Lara11-17/+13
2024-10-15[libc] Remove dependency on `cpp::function` in `rpc.h` (#112422)Joseph Huber2-4/+6
Summary: I'm going to attempt to move the `rpc.h` header to a separate folder that we can install and include outside of `libc`. Before doing this I'm going to try to trim up the file so there's not as many things I need to copy to make it work. This dependency on `cpp::functional` is a low hanging fruit. I only did it so that I could overload the argument of the work function so that passing the id was optional in the lambda, that's not a *huge* deal and it makes it more explicit I suppose.
2024-10-08[libc] Bound the worst-case stack usage in qsort(). (#110849)Simon Tatham2-12/+33
Previously, the Quicksort implementation was written in the obvious way: after each partitioning step, it explicitly recursed twice to sort the two sublists. Now it compares the two sublists' sizes, and recurses only to sort the smaller one. To handle the larger list it loops back round to the top of the function, so as to handle it within the existing stack frame. This means that every recursive call is handling a list at most half that of its caller. So the maximum recursive call depth is O(lg N). Otherwise, in Quicksort's bad cases where each partition step peels off a small constant number of array elements, the stack usage could grow linearly with the array being sorted, i.e. it might be Θ(N). I tested this code by manually constructing a List Of Doom that causes this particular quicksort implementation to hit its worst case, and confirming that it recursed very deeply in the old code and doesn't in the new code. But I haven't added that list to the test suite, because the List Of Doom has to be constructed in a way based on every detail of the quicksort algorithm (pivot choice and partitioning strategy), so it would silently stop being a useful regression test as soon as any detail changed.
2024-10-03[libc] Add malloc.h header defining mallopt (#110908)Fabio D'Urso1-0/+5
This patch adds the malloc.h header, declaring Scudo's mallopt entrypoint when built LLVM_LIBC_INCLUDE_SCUDO, as well as two constants that can be passed to it (M_PURGE and M_PURGE_ALL). Due to limitations of the current build system, only the declaration of mallopt is gated by LLVM_LIBC_INCLUDE_SCUDO, and the two new constants are defined irrespectively of it. We may need to refine this in the future. Note that some allocators other than Scudo may offer a mallopt implementation too (e.g. man 3 mallopt), albeit with different supported input values. This patch only supports the specific case of LLVM_LIBC_INCLUDE_SCUDO.
2024-09-23[libc] Add GPU support for the 'system' function (#109687)Joseph Huber4-1/+68
Summary: This function can easily be implemented by forwarding it to the host process. This shows up in a few places that we might want to test the GPU so it should be provided. Also, I find the idea of the GPU offloading work to the CPU via `system` very funny.
2024-09-11[libc] fix tls teardown while being used (#108229)Schrodinger ZHU Yifan2-1/+8
The call chain to `Mutex:lock` can be polluted by stack protector. For completely safe, let's postpone the main TLS tearing down to a separate phase. fix #108030
2024-08-29[libc] Implement locale variants for 'stdlib.h' functions (#105718)Joseph Huber15-0/+442
Summary: This provides the `_l` variants for the `stdlib.h` functions. These are just copies of the same entrypoint and don't do anything with the locale information.
2024-08-08[libc] Implement 'getenv' on the GPU target (#102376)Joseph Huber2-2/+2
Summary: This patch implements 'getenv'. I was torn on how to implement this, since realistically we only have access to this environment pointer in the "loader" interface. An alternative would be to use an RPC call every time, but I think that's overkill for what this will be used for. A better solution is just to emit a common `DataEnvironment` that contains all of the host visible resources to initialize. Right now this is the `env_ptr`, `clock_freq`, and `rpc_client`. I did this by making the `app.h` interface that Linux uses more general, could possibly move that into a separate patch, but I figured it's easier to see with the usage.
2024-08-03[libc] enable most of the entrypoints on aarch64 (#101797)Schrodinger ZHU Yifan1-6/+8
This is a non-feature change that enables most of the entrypoints for aarch64 based runtime builds. It fixes an additional problem that some compiler-rt targets are not defined at the time of dependency checking thus leading to false-negatives.
2024-07-30[libc] Implement placeholder memory functions on the GPU (#101082)Joseph Huber5-5/+139
Summary: These functions are needed for `libc++` to link successfully. We can't implement them well currently, so simply provide some stand-in implementations. `realloc` will currently copy garbage and potentially fault and `aligned_alloc` will work unless your alignment is more than 4K alignment. However, these should work in practice to get tests running. I will write a real allocator soon™.
2024-07-25[libc] Lazily initialize freelist malloc using symbols (#99254)Daniel Thornburgh2-13/+2
This requires the user to set the upper bounds of the heap by defining the symbol `__libc_heap_limit`. The heap begins at `_end` and ends `__libc_heap_limit` bytes afterwards. This prevents a completely unused heap from requiring any space, and it prevents the heap from being zeroed at initialization time as part of BSS. It also allows users to customize the available heap location without recompiling libc. I'd think this should eventually be replaced with an implemenation based on a morecore() library. This would allow the same implementation to use sbrk() on POSIX, `_end` and `__libc_heap_limit` on embedded, and a buffer in tests. It would also provide better "wilderness" behavior that tends to decrease heap fragementation (see Wilson et al.) See #98096
2024-07-23[libc] Fix math tests for macos arm64. (#100060)lntue1-1/+6
Some problem with current build on macos: - no libatomic. - death tests do not work yet.
2024-07-22[libc] Fix callback type in `exit_handlers.cpp` not matching (#97642)Joseph Huber5-55/+36
Summary: This file is an object library, but uses the `LIBC_COPT_PUBLIC_PACKAING` option. This will always be undefined which leads to a type mismatch when uses actually try to link against it. This patch simply removes this and turns it into a header only library. This means that the implementations of the callback lists and the mutexes need to live in their respective files. The result is that `atexit` needs to be defined for `at_quick_exit` to be valid.
2024-07-17[libc] Change rand implementation so all tests pass in both 32- and 64-bit ↵Mikhail R. Gadelha1-11/+30
systems (#98692) This patch makes rand select different algorithms depending on the arch. This is needed to avoid a test failure in 32-bit systems where the LSB of rand was not uniform enough when the 64-bit constants are used in 32-bit systems.
2024-07-16[libc][stdlib] Implement heap sort. (#98582)lntue7-137/+271
2024-07-12[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)Petr Hosek80-160/+263
This is a part of #97655.
2024-07-12Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace ↵Mehdi Amini80-263/+160
declaration" (#98593) Reverts llvm/llvm-project#98075 bots are broken
2024-07-11[libc] Fix atexit not getting linked on linux (#98537)Michael Jones1-2/+11
Atexit needs to be linked into exit on linux since atexit defines __cxa_finalize. This should probably be fixed a different way but this works for now.
2024-07-11[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)Petr Hosek80-160/+263
This is a part of #97655.
2024-07-09[libc] Move the internal extern "C" symbols inside the namespace (#98232)Petr Hosek1-2/+2
This ensures that these symbols inherit the namespace visibility.