Commit Graph

2670 Commits

Author SHA1 Message Date
Michael Jones
f7a21a837c [libc] Remove optimization flags on entrypoints (#165782)
Optimization flags are now handled through a common flag. These are no
longer necessary.

Fixes #112409
2025-10-30 15:52:50 -07:00
Marcell Leleszi
784b74c6ef [libc] Fix off by one error in strftime (#165711)
This patch fixes a bug in strftime's return value when the formatted
output exactly fills the buffer, not including the null terminator. The
previous check failed to account for the null terminator in this case,
incorrectly returning the written count instead of 0.
2025-10-30 10:35:42 -07:00
Sterling-Augustine
7d1538cd3d Move LIBC_CONF_STRING_UNSAFE_WIDE_READ to top-level libc-configuration (#165046)
This options sets a compile option when building sources inside the
string directory, and this option affects string_utils.h. But
string_utils.h is #included from more places than just the string
directory (such as from __support/CPP/string.h), leading to both
narrow-reads in those cases, but more seriously, ODR violations when the
two different string_length implementations are included int he same
program.

Having this option at the top level avoids this problem.
2025-10-27 14:39:46 -07:00
Shreeyash Pandey
51427079f7 [libc] fix architecture guarding for 32bit sigsetjmp (#164923)
Fixes: https://github.com/llvm/llvm-project/issues/164653

Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com>
2025-10-26 16:40:01 -04:00
Shreeyash Pandey
a1dc546f7f [libc] add missing headers in stdfix (#162078)
Fixes https://github.com/llvm/llvm-project/issues/129361

@michaelrj-google @PiJoules

---------

Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com>
Co-authored-by: Michael Jones <michaelrj@google.com>
2025-10-24 10:42:31 -07:00
Alexey Samsonov
9fc353ee36 [libc] Fix a couple issues in <wchar.h> header (#164666)
* Add FILE type declaration, as it should be presented in `<wchar.h>`,
as well as in `<stdio.h>`
* Fix argument type in `wcsrtombs` / `wcsnrtombs` function - it should
be restrict pointer to `mbstate_t`. Add restrict qualifier to internal
implementation as well.

This brings us closer to being able to build libcxx with wide-character
support against llvm-libc headers.
2025-10-22 11:09:42 -07:00
lntue
2bc4ad35e7 [libc][math] Add tolerance to math tests so that they still work when accurate path is skipped. (#164522) 2025-10-21 19:09:41 -05:00
Alexey Samsonov
12abe8aed6 [libc] Stub out message catalog functions from <nl_types.h> (#164360)
Create a POSIX `<nl_types.h>` header with `catopen`, `catclose`, and
`catgets` function declarations.
Provide the stub/placeholder implementations which always return error.
This is consistent with the way
locales are currently (un-)implemented in llvm-libc.

Notably, providing `<nl_types.h>` fixes the last remaining issue with
building libc++ against llvm-libc
(on certain configuration of x86_64 Linux) after disabling threads and
wide-characters in libc++.
2025-10-21 10:34:35 -07:00
lntue
513f9c50e7 [libc][math] Add float-only implementation for sinf / cosf. (#161680)
Based on the double precision's sin/cos fast path algorithm:

Step 1: Perform range reduction `y = x mod pi/8` with target errors <
2^-54.
This is because the worst case mod pi/8 for single precision is ~2^-31,
so to have up to 1 ULP errors from
the range reduction, the targeted errors should `be 2^(-31 - 23) =
2^-54`.

Step 2: Polynomial approximation
We use degree-5 and degree-4 polynomials to approximate sin and cos of
the reduced angle respectively.

Step 3: Combine the results using trig identities
```math
\begin{align*}
  \sin(x) &= \sin(y) \cdot \cos(k \cdot \frac{\pi}{8}) + \cos(y) \cdot \sin(k \cdot \frac{\pi}{8}) \\
  \cos(x) &= \cos(y) \cdot \cos(k \cdot \frac{\pi}{8}) - \sin(y) \cdot \sin(k \cdot \frac{\pi}{8})
\end{align*}
```

Overall errors: <= 3 ULPs for default rounding modes (tested
exhaustively).

Current limitation: large range reduction requires FMA instructions for
binary32. This restriction will be removed in the followup PR.

---------

Co-authored-by: Petr Hosek <phosek@google.com>
2025-10-21 13:33:09 -04:00
Connector Switch
5b3416caf9 [libc] implement inet_aton (#162651)
This patch adds the implementation for `inet_aton` function. Since this
function is not explicitly included in POSIX, I have marked it with
`llvm_libc_ext`. It is widely available and commonly used, and can also
be used to implement `inet_addr`, which is included in POSIX.
2025-10-21 23:47:38 +08:00
Ren Hiyama
fedbb0f7e1 [libc] Capture 'POLY_COEFFS' by reference in asinpi_polyeval lambda. (#164404)
Closes #164403

GCC fails to compile libc currently. This PR fixes this by capturing
'POLY_COEFFS' by reference.
2025-10-21 10:40:00 -04:00
Schrodinger ZHU Yifan
2505df0a4f Revert "[libc] add basic lifetime annotations for support data structures" (#164012)
Reverts llvm/llvm-project#145933 due to broken aarch64 buildbots.
2025-10-17 16:35:14 -04:00
Schrodinger ZHU Yifan
c0e0a33f48 [libc] add basic lifetime annotations for support data structures (#145933)
fix #145932

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-17 16:10:44 -04:00
Muhammad Bassiouni
a55c4c812e [libc][math] Refactor exp2m1f implementation to header-only in src/__support/math folder. (#162017)
Part of #147386

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-10-17 18:27:51 +03:00
lntue
e6afe2a854 [libc] Fix temporary types for fputil::split template. (#163813)
Fix #163711.
2025-10-16 12:56:09 -04:00
Guillaume Chatelet
57726bdca2 Revert "[libc] Implement branchless head-tail comparison for bcmp" (#162859)
Reverts llvm/llvm-project#107540

This PR demonstrated improvements on micro-benchmarks but the gains did
not seem to materialize in production. We are reverting this change for
now to get more data. This PR might be reintegrated later once we're
more confident in its effects.
2025-10-13 23:25:42 +02:00
Peter Collingbourne
7905ec387e [libc] Use UMAXV.4S to reduce bcmp result.
We can use UMAXV.4S to reduce the comparison result in a single
instruction. This improves performance by roughly 4% on Apple M1:

Summary
  bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10 ran
    1.01 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.01 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.01 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark3 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.01 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.02 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.03 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.03 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark2 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.03 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10
    1.05 ± 0.02 times faster than bin/libc.src.string.bcmp_benchmark1 --study-name="new bcmp" --sweep-mode --sweep-max-size=128 --output=/dev/null --num-trials=10

(1 = original, 2 = a variant of this patch that uses UMAXV.16B, 3 = this patch)

Reviewers: michaelrj-google, gchatelet, overmighty, SchrodingerZhu

Pull Request: https://github.com/llvm/llvm-project/pull/99260
2025-10-13 11:21:48 -07:00
Muhammad Bassiouni
878527651a [libc][math] Refactor exp2f16 implementation to header-only in src/__support/math folder. (#161993)
Part of #147386

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-10-11 23:27:00 +03:00
Joseph Huber
c18de24d9d [libc] Add a config option to disable slab reclaiming (#151599)
Summary:
Without slab reclaiming this interface is much simpler and it can speed
up cases with a lot of churn. Basically, wastes memory for performance.
2025-10-10 21:05:51 -05:00
Schrodinger ZHU Yifan
e07914b563 [libc] fix sysconf test for rv32 (#162685) 2025-10-09 14:26:07 -04:00
Anton Shepelev
1244fd24d9 [libc][math][c23] Add rsqrtf() function (#159615)
Closes #159614

**Changes:**
- Initial implementation of rsqrt for single precision float

**Some small unrelated style changes to this PR (that I missed in my
rsqrtf16 PR):**
- Added extra - to the top comments to make it look nicer in
libc/shared/math/rsqrtf16.h
- Put rsqrtf16 inside of libc/src/__support/math/CMakeLists.txt in
sorted order
- Rearanged libc_math_function rsqrtf16 in Bazel to match alphabetical
order
2025-10-08 23:37:01 -04:00
Schrodinger ZHU Yifan
23981c4222 [libc] Refactor internal auxv usage to reduce getauxval dependencies (#162489) 2025-10-08 15:26:26 -04:00
Shreeyash Pandey
c57796dcb2 [libc][stdfix] Implement fxdivi functions (rdivi) (#154914)
This PR includes only one of the fxdivi functions (rdivi). It uses a
polynomial function for initial approximation followed by 4
newton-raphson iterations to calculate the reciprocal and finally
multiplies the numerator with it to get the result.


---------

Signed-off-by: Shreeyash Pandey <shreeyash335@gmail.com>
2025-10-08 15:17:31 -04:00
Schrodinger ZHU Yifan
1892e6fbb3 [libc] fix rv32 mmap in auxv library (#162519)
Fix auxv rv32 build problem
2025-10-08 14:34:47 -04:00
Jackson Stogel
6308cd871c [libc] Fix integer overflow for large offsets in lseek. (#162394)
Currently, the return value `LIBC_NAMESPACE::syscall_impl<int>(SYS_lseek, fd, offset, whence)` will
overflow when seeking on files >4GB.
2025-10-08 17:04:52 +00:00
Schrodinger ZHU Yifan
8c3d929f0e [libc] use PR_GET_AUXV only if UAPI provides it (#162492) 2025-10-08 10:37:49 -04:00
Schrodinger ZHU Yifan
0db5ba0ce6 [libc] Refactor AUXV handling with new auxv.h header library (#162326)
Closes https://github.com/llvm/llvm-project/issues/153666

This patch introduces a new centralized AUXV (auxiliary vector) handling
mechanism for LLVM libc on Linux, replacing the previous scattered
implementation across multiple files.

## Key Changes:

### New Files:
- **libc/src/__support/OSUtil/linux/auxv.h**: New header library
providing
  a clean interface for AUXV access with:
  - `auxv::Entry` struct for AUXV entries (type and value)
  - `auxv::Vector` class with iterator support for traversing AUXV
  - `auxv::get()` function for retrieving specific AUXV values
- Thread-safe initialization with fallback mechanisms (prctl and
/proc/self/auxv)

### Modified Files:

1. **libc/src/__support/OSUtil/linux/CMakeLists.txt**:
   - Added `auxv` header library declaration with proper dependencies:
     - libc.hdr.fcntl_macros
     - libc.src.__support.OSUtil.osutil
     - libc.src.__support.common
     - libc.src.__support.CPP.optional
     - libc.src.__support.threads.callonce

2. **libc/config/linux/app.h**:
   - Removed `AuxEntry` struct (moved to auxv.h as `auxv::Entry`)
   - Removed `auxv_ptr` from `AppProperties` struct
   - Simplified application properties structure

3. **libc/src/sys/auxv/linux/getauxval.cpp**:
   - Completely refactored to use new auxv.h interface
   - Removed ~200 lines of complex initialization code
   - Simplified to just call `auxv::get()` function
- Removed dependencies to external symbols (mman, prctl, fcntl, read,
close, open)

4. **libc/src/sys/auxv/linux/CMakeLists.txt**:
   - Updated dependencies to use new auxv header library
- Removed dependencies to external symbols (prctl, mman, fcntl, unistd,
etc.)

5. **libc/startup/linux/do_start.cpp**:
   - Updated to use new `auxv::Vector` interface
   - Changed from pointer-based to iterator-based AUXV traversal
- Updated field names (`aux_entry->id` → `aux_entry.type`,
`aux_entry->value` → `aux_entry.val`)
- Added call to `auxv::Vector::initialize_unsafe()` for early AUXV setup

6. **libc/startup/linux/CMakeLists.txt**:
   - Added dependency on `libc.src.__support.OSUtil.linux.auxv`
2025-10-08 10:00:05 -04:00
Joseph Huber
11fb8358fa [libc] Make LIBC_EXIT RPC code use quick exit
Summary:
This RPC call does the final exiting. The callbacks were handled on the
GPU side and this is only 'valid' in the pretend mode where we treat the
GPU like a CPU program. Doing this keeps us from crashing and burning
if people continue using the program while this is running as `exit`
would tear down the offloading library in memory and lead to segfaults.
This just drops everything where it is and lets the process manager
clean it up for us.
2025-10-07 14:16:34 -05:00
Michael Jones
f265353f97 [libc] Clean up sys/resource (#161749)
Needed to support sys/resource in bazel
2025-10-06 15:42:37 -07:00
Muhammad Bassiouni
596ff855d0 [libc][math] Refactor exp2f implementation to header-only in src/__support/math folder. (#161992)
Part of #147386

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-10-06 21:23:44 +03:00
Muhammad Bassiouni
3e78c313bc [libc][math] Refactor exp2 implementation to header-only in src/__support/math folder. (#161297)
Part of #147386

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-10-05 06:31:39 +03:00
Marcell Leleszi
44d471e706 [libc] Implement faccessat (#161065)
#160404

- Implement POSIX function "faccessat"
- Remove redundant param in facessat syscall in access implementation,
faccessat syscall does not take a flags arg
2025-10-02 12:33:21 -07:00
Alexey Samsonov
5f0f4972c4 [libc] Unify and extend no_sanitize attributes for strlen. (#161316)
Fast strlen implementations (naive wide-reads, SIMD-based, and
x86_64/aarch64-optimized versions) all may perform
technically-out-of-bound reads, which leads to reports under ASan,
HWASan (on ARM machines), and also TSan (which also has the capability
to detect heap out-of-bound reads). So, we need to explicitly disable
instrumentation in all three cases.

Tragically, Clang didn't support `[[gnu::no_sanitize]]` syntax until
recently, and since we're supporting both GCC and Clang, we have to
revert to `__attribute__` syntax.
2025-10-01 17:26:22 -07:00
Muhammad Bassiouni
6c032fa600 [libc][math] Refactor exp10m1f16 implementation to header-only in src/__support/math folder. (#161119)
Part of #147386

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-10-01 11:21:19 +03:00
Schrodinger ZHU Yifan
38a4c9c639 [libc][msvc] fix mathlib build on WoA (#161258)
Fix build errors encountered when building math library on WoA.

1. Skip FEnv equality check for MSVC
2. Provide a placeholder type for vector types.
2025-09-29 15:40:21 -04:00
Muhammad Bassiouni
23d3caf854 [libc][math] Refactor exp10m1f implementation to header-only in src/__support/math folder. (#159897)
Part of #147386

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-09-29 07:58:03 +03:00
Joseph Huber
e2d5efd59f [libc] Update the memory helper functions for simd types (#160174)
Summary:
This unifies the interface to just be a bunch of `load` and `store`
functions that optionally accept a mask / indices for gathers and
scatters with masks.

I had to rename this from `load` and `store` because it conflicts with
the other version in `op_generic`. I might just work around that with a
trait instead.
2025-09-26 08:18:00 -05:00
lntue
3c53adec68 [libc][NFC] Remove usage of the C keyword I. (#160567) 2025-09-24 14:18:32 -04:00
Krishna Pandey
1fda257728 [libc][math][c++23] Add log_bf16 math function (#157811)
This PR adds log_bf16 higher math function for BFloat16 type along with
the tests.

---------

Signed-off-by: Krishna Pandey <kpandey81930@gmail.com>
2025-09-24 20:47:10 +05:30
Michael Jones
2e83f10121 [libc] Cleanup mman functions and tests (#159657)
Remove functions depending on each other, cleanup internal errno in
shm_common, remove various unnecessary includes.
2025-09-23 11:08:33 -07:00
Zaky Hermawan
5ad347d1a1 [libc][POSIX][unistd] Implement gethostname (#128142)
The implementation is based on the specifications from:
https://man7.org/linux/man-pages/man2/gethostname.2.html

Closes https://github.com/llvm/llvm-project/issues/126602

---------

Signed-off-by: ZakyHermawan <zaky.hermawan9615@gmail.com>
2025-09-23 09:54:35 -04:00
Joseph Huber
0b9d816b3e [libc] Remove separate RPC test handling (#160206)
Summary:
This was originally kept separate so it didn't pollute the name space,
but now I'm thinking it's just easier to bundle it in with the default
interface. This means that we'll have a bit of extra code for people
using the server.h file to handle libc opcodes, but it's minimal (3
functions) and it simplifies this.

I'm doing this because I'm hoping to move the GPU tester binary to
liboffload which handles `libc` opcodes internally except these. This is
the easier option compared to adding a hook to register custom handlers
there.
2025-09-22 19:44:41 -05:00
Muhammad Bassiouni
2b937daa13 [libc][math] Refactor dsqrtl implementation to header-only in src/__support/math folder. (#154868)
Part of #147386

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-09-20 07:24:21 +03:00
Michael Jones
b7f0bb9c29 [libc][bazel] Add (v)asprintf targets and tests (#159476) 2025-09-18 10:17:57 -07:00
lntue
7cc5989593 [libc] Some MSVC compatibility fixes in src/__support. (#159428) 2025-09-17 17:26:47 -04:00
lntue
f549bb2ddc [libc][math] Adjust rsqrtf16 exception checks. (#159411) 2025-09-17 14:17:53 -04:00
Anton Shepelev
80f9c72a1e [libc][math][c23] Add rsqrtf16() function (#137545)
Addresses #132818 
Part of #95250
2025-09-17 10:19:20 -04:00
Joseph Huber
b76dc2b9ba [libc] Clean up mask helpers after allowing implicit conversions (#158681)
Summary:
I landed a change in clang that allows integral vectors to implicitly
convert to boolean ones. This means I can simplify the interface and
remove the need to cast to bool on every use. Also do some other
cleanups of the traits.
2025-09-16 12:39:56 -05:00
lntue
7aad3830fb [libc] Some MSVC compatibility changes for src/string/memory_utils. (#158393) 2025-09-12 21:49:34 -04:00
lntue
f019e2368b [libc] Change __builtin_memcpy to inline_memcpy. (#158345) 2025-09-12 17:57:08 -04:00