23 Commits

Author SHA1 Message Date
Victor Campos
6c2b155f4f
[libc] Fix build failures in fuzzing tests (#185017)
The tests:
 - __support/freelist_heap_fuzz.cpp
 - fuzzing/string/strlen_fuzz.cpp

had build failures for different reasons. This patch fixes these
failures.

freelist_heap_fuzz.cpp had this error:

```
llvm-project/libc/fuzzing/__support/freelist_heap_fuzz.cpp:150:26: error: use of undeclared identifier 'Block'; did you mean '__llvm_libc_23_0_0_git::Block'?
  150 |       size_t alignment = Block::MIN_ALIGN;
      |                          ^~~~~
      |                          __llvm_libc_23_0_0_git::Block
```

The issue stems from the fact that Block was not available in scope. It
needs to be referenced via LIBC_NAMESPACE.

strlen_fuzz.cpp had this error:

```
In file included from Workspace/llvm-project/libc/fuzzing/string/strlen_fuzz.cpp:14:
In file included from /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/cstdint:38:
In file included from /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13/bits/c++config.h:679:
/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13/bits/os_defines.h:44:5: error: function-like macro '__GLIBC_PREREQ' is not defined
   44 | #if __GLIBC_PREREQ(2,15) && defined(_GNU_SOURCE)

```

This issue is more cryptic to me, but I managed to fix it by changing
the includes from cstdint and cstring to stdint.h and string.h.
2026-03-16 11:14:57 +00:00
Schrodinger ZHU Yifan
db713325d5
[libc][tsearch] add weak AVL tree for tsearch implementation (#172411)
Related to #114695.

This PR adds a Weak AVL Tree for tsearch APIs. The symbol
implementations are coming in a
following up PR to avoid creating a huge patch. The work is based on
@MaskRay's recent post (see below).

A general self-balancing binary search tree where the node pointer can
be used as stable handles to the stored values.

The self-balancing strategy is the Weak AVL (WAVL) tree, based on the
following foundational references:
1. https://maskray.me/blog/2025-12-14-weak-avl-tree
2. https://reviews.freebsd.org/D25480
3. https://ics.uci.edu/~goodrich/teach/cs165/notes/WeakAVLTrees.pdf
4. https://dl.acm.org/doi/10.1145/2689412 (Rank-Balanced Trees)

WAVL trees belong to the rank-balanced binary search tree framework
(reference 4), alongside AVL and Red-Black trees.

Key Properties of WAVL Trees:
1. Relationship to Red-Black Trees: A WAVL tree can always be colored as
a
   Red-Black tree.
2. Relationship to AVL Trees: An AVL tree meets all the requirements of
a
WAVL tree. Insertion-only WAVL trees maintain the same structure as AVL
   trees.

Rank-Based Balancing:
In rank-balanced trees, each node is assigned a rank (conceptually
similar
to height). In AVL/WAVL, the rank difference between a parent and its
child is
strictly enforced to be either **1** or **2**.

- **AVL Trees:** Rank is equivalent to height. The strict condition is
that
there are no 2-2 nodes (a parent with rank difference 2 to both
children).
- **WAVL Trees:** The no 2-2 node rule is relaxed for internal nodes
during
the deletion fixup process, making WAVL trees less strictly balanced
than
  AVL trees but easier to maintain than Red-Black trees.

Balancing Mechanics (Promotion/Demotion):
- **Null nodes** are considered to have rank -1.
- **External/leaf nodes** have rank 0.
- **Insertion:** Inserting a node may create a situation where a parent
and
child
have the same rank (difference 0). This is fixed by **promoting** the
rank
of the parent and propagating the fix upwards using at most two
rotations
  (trinode fixup).
- **Deletion:** Deleting a node may result in a parent being 3 ranks
higher
than a child (difference 3). This is fixed by **demoting** the parent's
  rank and propagating the fix upwards.

Implementation Detail:
The rank is **implicitly** maintained. We never store the full rank.
Instead,
a 2-bit tag is used on each node to record the rank difference to each
child:
- Bit cleared (0) -> Rank difference is **1**.
- Bit set (1)     -> Rank difference is **2**.

---------

Co-authored-by: Michael Jones <michaelrj@google.com>
2026-01-21 12:19:34 -05:00
Daniel Thornburgh
b76300acc5
[libc][malloc] Ensure a minimum block alignment of 4 (#169447)
Most platforms inherently have a size_t alignment of 4, but this isn't
true on every platform LLVM has some degree of backend support for.
Accordingly, it's simple enough to just set the min alignment of Block
to 4 and lose the static_assert.
2025-12-01 14:41:20 -08:00
Schrodinger ZHU Yifan
8751f26a1b
[libc] add an SVE implementation of strlen (#167259)
This PR creates an SVE-based implementation for strlen by translating
from the AOR code in tree. Microbenchmark shows improvements against
NEON when N>=64. Although both implementations fall behind glibc by a
large margin,
this may be a good start point to explore SVE implementations.

Together with the PR:

1. Added two more tests of strlen with special nul symbols.
2. Added strlen's fuzzer and fix a typo in previous heap fuzzer.

```
=== strlen(16 bytes) ===
libc: 1.56115 ns/call, 9.54499 GiB/s
neon: 1.59393 ns/call, 9.34867 GiB/s
sve: 1.66097 ns/call, 8.97134 GiB/s

=== strlen(64 bytes) ===
libc: 2.06967 ns/call, 28.7991 GiB/s
neon: 2.59914 ns/call, 22.9325 GiB/s
sve: 2.58628 ns/call, 23.0465 GiB/s

=== strlen(256 bytes) ===
libc: 3.74165 ns/call, 63.7202 GiB/s
neon: 8.98243 ns/call, 26.5428 GiB/s
sve: 7.36426 ns/call, 32.3751 GiB/s

=== strlen(1024 bytes) ===
libc: 10.5327 ns/call, 90.5438 GiB/s
neon: 34.363 ns/call, 27.7529 GiB/s
sve: 26.9329 ns/call, 35.4092 GiB/s

=== strlen(4096 bytes) ===
libc: 37.7304 ns/call, 101.104 GiB/s
neon: 145.911 ns/call, 26.144 GiB/s
sve: 103.208 ns/call, 36.9612 GiB/s

=== strlen(1048576 bytes) ===
libc: 9623.4 ns/call, 101.478 GiB/s
neon: 36138.2 ns/call, 27.023 GiB/s
sve: 26605.6 ns/call, 36.7051 GiB/s
```
2025-11-10 19:15:34 -05:00
lntue
e581f1cc9a
[libc] Add proxy header for ENTRY type. (#139746)
https://github.com/llvm/llvm-project/issues/139561
2025-05-13 14:07:21 -04:00
Daniel Thornburgh
710ffb69bf
[libc] Fix warnings for freelist_heap_test/fuzz (#136634)
Fixes #122367
2025-04-22 11:11:31 -07:00
Joseph Huber
db6b7a84e6
[libc][NFC] Strip all training whitespace and missing newlines (#124163) 2025-01-23 12:02:54 -06:00
Petr Hosek
51a0919412
[libc] Exclude FreeListHeap test and fuzzer on GPU (#120137)
FreeListHeap uses the _end symbol which conflicts with the _end symbol
defined by GPU start.cpp files so for now we exclude the test and the
fuzzer on GPU.
2024-12-16 13:28:42 -08:00
Petr Hosek
7bf3137c39
[libc] Breakup freelist_malloc into separate files (#119806)
This better matches the structure we use for the rest of libc.
2024-12-16 10:30:27 -08:00
Daniel Thornburgh
385961d7b2 Reapply "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"
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-21 15:26:24 -08:00
Daniel Thornburgh
93b83642ee Revert "[libc] Use best-fit binary trie to make malloc logarithmic (#117065)"
This reverts commit b05600d96f46697e21f6b1b7ad901391326243a8.
riscv32 unit test still broken
2024-11-21 11:56:05 -08:00
Daniel Thornburgh
b05600d96f Reapply "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
- 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-21 11:30:51 -08:00
Daniel Thornburgh
9be475af81
Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
Reverts llvm/llvm-project#106259

Unit tests break on AArch64.
2024-11-20 14:00:07 -08:00
Daniel Thornburgh
c3207c31fc
[libc] Use best-fit binary trie to make malloc logarithmic (#106259)
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-20 13:54:00 -08:00
Schrodinger ZHU Yifan
e59582b6f8
[libc] avoid type-punning with inactive union member (#116685) 2024-11-18 16:04:41 -05:00
Petr Hosek
5ff3ff33ff
[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)
This is a part of #97655.
2024-07-12 09:28:41 -07:00
Mehdi Amini
ce9035f5bd
Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration" (#98593)
Reverts llvm/llvm-project#98075

bots are broken
2024-07-12 09:12:13 +02:00
Petr Hosek
3f30effe1b
[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)
This is a part of #97655.
2024-07-11 12:35:22 -07:00
Schrodinger ZHU Yifan
0e5ff6251f
[libc] add hashtable fuzzing (#87949) 2024-05-02 15:36:10 -04:00
Guillaume Chatelet
09efe848cf
[libc][NFC] Rename UInt.h to big_int.h and UInt128.h to uint128.h for consistency (#87808) 2024-04-06 10:39:55 +02:00
Guillaume Chatelet
71c3f5d617
[reland][libc] Refactor BigInt (#87613)
This is a reland of #86137 with a fix for platforms / compiler that do
not support trivially constructible int128 types.
2024-04-04 11:41:27 +02:00
Guillaume Chatelet
12735916bd
Revert "[libc] Refactor BigInt" (#87612)
Reverts llvm/llvm-project#86137

Some aarch64 compilers seem to consider that `uint128_t` is not
`is_trivially_constructible` which prevents `bit_cast`-ing.
2024-04-04 11:10:30 +02:00
Guillaume Chatelet
a2306b65d2
[libc] Refactor BigInt (#86137)
This patch moves most of the multiprecision logic to the `multiword`
namespace and simplifies some logic in `BigInt`. It also fully
implements the mask and count functions and increases test coverage.

`math_extras.h` is also reworked to make it more concise.
2024-04-04 10:27:08 +02:00