Commit Graph

3971 Commits

Author SHA1 Message Date
Nick Desaulniers
3e02069afe [libc][pthread] fix -Wmissing-field-initializers (#126314)
Fixes:


llvm-project/libc/test/integration/src/pthread/pthread_rwlock_test.cpp:59:29:
    warning: missing field '__preference' initializer
    [-Wmissing-field-initializers]
       59 |   pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
          |                             ^

Also, add a test that demonstrates the same issue for
PTHREAD_MUTEX_INITIALIZER, and fix that, too.

PTHREAD_ONCE_INIT does not have this issue and does have test coverage.
2025-02-12 14:28:29 -08:00
Petr Hosek
b0d7820805 [libc] Move __LLVM_LIBC__ define to __llvm-libc-common.h (#126877)
Relying on features.h is problematic since codebases are free to have
such a header on their search path, which breaks compilation. libc
should instead provide a more standard way of getting __LLVM_LIBC__.
Since __llvm-libc-common.h is included from all libc headers, defining
__LLVM_LIBC__ there ensures that this define is available whenever any
of the standard header is included.
2025-02-12 11:37:07 -08:00
Krishna Pandey
1f51038036 [libc][stdfix] Implement countlsfx functions in libc. (#126597)
fixes #113357

---------

Signed-off-by: krishna2803 <kpandey81930@gmail.com>
2025-02-12 14:12:44 -05:00
Michael Jones
16a5f7eb73 [libc] Fix implict cast to time_t warning (#126947)
On some systems time_t is 32 bit, causing build errors (with -Werror)
in get_epoch which attempts to implicitly convert an int64_t to a
time_t.

Fixes:

error: implicit conversion loses integer precision: 'int64_t' (aka 'long
long') to 'time_t' (aka 'int') [-Werror,-Wshorten-64-to-32]
      332 |     return mktime_internal(timeptr);
          |     ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~
2025-02-12 10:08:06 -08:00
Nick Desaulniers
a3e2075e50 [libc][bit_test] fix -Wimplicit-int-conversion (#126317)
Fixes:

    llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      235 |   return (value << rotate) | (value >> (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      247 |   return (value >> rotate) | (value << (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
implicit conversion loses integer precision: 'int' to 'unsigned char'
    [-Werror,-Wimplicit-int-conversion]
       45 |     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
          |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.
2025-02-12 08:59:02 -08:00
c8ef
6de4de8931 [libc] implement endian related macros (#126368)
Follow up of #125168.

This patch adds endian-related macros to `endian.h`. We utilize compiler
built-ins for byte swap functions, which are already included in our
minimal supported compiler version.
2025-02-12 10:17:09 +08:00
Michael Jones
574ccc6d1b [libc] fix get_epoch constexpr error (#126818)
get_epoch calls mktime_internal which isn't constexpr. For now, just
remove the constexpr from get_epoch.
2025-02-11 16:08:20 -08:00
lntue
17721b7b14 [libc][NFC] Remove DEFAULT_DOUBLE_SPLIT macro. (#126822) 2025-02-11 18:29:35 -05:00
Michael Jones
a760e7faac [libc] create TimeReader to look at a struct tm (#126138)
In the process of adding strftime (#122556) I wrote this utility class
to simplify reading from a struct tm. It provides helper functions that
return basically everything needed by strftime. It's not tested
directly, but it is thoroughly exercised by the strftime tests.
2025-02-11 14:37:15 -08:00
lntue
fd41393e2e [libc][math] Add float-only option for atan2f. (#122979)
For targets that have single precision FPU but not double precision FPU
such as Cortex M4, only using float-float in the intermediate
computations might reduce the code size compared to using double. In
this case, when the exact pass is skipped, the float-only option for
atan2f implemented in this PR reduces the code size of this function by
~1 KB compared to the double precision version.
2025-02-11 17:36:24 -05:00
Nick Desaulniers
0419db6b95 [libc][docgen] make note of sys/time.h interfaces removed in POSIX.1-2024 (#126612)
One of these days, we'll be able to specify time to a computer...

Also, POSIX can remove stuff all they want. Folks probably will continue to
depend on broken interfaces forever.

Link: #124654
Link: https://austingroupbugs.net/view.php?id=1330
2025-02-11 14:14:41 -08:00
Nick Desaulniers
b8ba266820 [libc][test][stdbit] fix -Wimplicit-int-conversion (#126616)
When cross compiling the libc-stdbit-tests, the existing tests trigger numerous
instances of -Wimplicit-int-conversion. The truncation of these implicit
promotions is intentional.
2025-02-11 09:31:01 -08:00
Petr Hosek
7af4ab4f07 [libc] Don't manually override the optimization level for math (#126322)
This was originally done for testing purposes, but after #126315 we now
do testing through GitHub Actions and should be instead using the
optimization setting chosen by the user.
2025-02-10 23:56:47 -08:00
Joseph Huber
3d9409f5bc [NVPTX] Make ctor/dtor lowering always enabled in NVPTX (#126544)
Summary:
Currently we conditionally enable NVPTX lowering depending on the
language (C/C++/OpenMP). Unfortunately this causes problems because this
option is only present if the backend was enabled, which causes this to
error if you try to make LLVM-IR.

This patch instead makes it the only accepted lowering. The reason we
had it as opt-in before is because it is not handled by CUDA. So, this
pach also introduces diagnostics to prevent *all* creation of
device-side global constructors and destructors. We already did this for
variables, now we do it for attributes as well.

This inverts the responsibility of blocking this from the backend to the
langauage like it should be given that support for this is language
dependent.
2025-02-10 14:25:44 -06:00
Prashanth
6a8439b593 [libc][docs] Add sys/statvfs to documentation and YAML definitions (#126413)
These changes ensure that the sys/statvfs header is documented properly
with respect to the issue (
https://github.com/llvm/llvm-project/issues/122006 ) .
2025-02-10 08:58:46 -08:00
wldfngrs
7ee56b9afc [libc][math][c23] Add asinf16() function (#124212)
Co-authored-by: OverMighty <its.overmighty@gmail.com>
2025-02-10 12:38:55 +01:00
Petr Hosek
6dbe542290 [libc] Use ${libc_opt_high_flag} instead of -O3 (#123233)
This is preferable since `${libc_opt_high_flag}` will be set correctly
for the compiler used.
2025-02-07 13:36:06 -08:00
Vinay Deshmukh
dee20925e7 [libc][test] -Wimplicit-fallthrough, -Wwrite-strings and non-GCC warnings (#124036)
* Enabled `-Wimplicit-fallthrough`
* Enabled `-Wwrite-strings`
* Enabled non-GCC warnings
* Fix non-GCC to mean `clang`
* Move `-Wstrict-prototypes` to common section

See:
https://github.com/llvm/llvm-project/pull/122835#discussion_r1924109264

Relates to #119281
2025-02-07 12:50:26 -08:00
Joseph Huber
7623d91784 Revert "[libc][stdfix] Fix buildbot failure because of a typo. (#126291)"
This reverts commit bada9220b8.

Revert "[libc][stdfix] Implement fixed point `countlsfx` functions in llvm-libc (#125356)"

This reverts commit f2a1103b32.
2025-02-07 14:34:40 -06:00
Krishna Pandey
bada9220b8 [libc][stdfix] Fix buildbot failure because of a typo. (#126291)
Fix build-bot failure caused by #125356
2025-02-07 14:10:18 -06:00
Krishna Pandey
f2a1103b32 [libc][stdfix] Implement fixed point countlsfx functions in llvm-libc (#125356)
fixes #113357
2025-02-07 13:53:17 -05:00
Nick Desaulniers
e566313a1f [libc][signal] clean up usage of sighandler_t (#125745)
`man 3 signal`'s declaration has a face _only a mother could love_.

sighandler_t and __sighandler_t are not defined in the C standard, or POSIX.

They are helpful typedefs provided by glibc and the Linux kernel UAPI headers
respectively since working with function pointers' syntax can be painful. But
we should not rely on them; in C++ we have `auto*` and `using` statements.

Remove the proxy header, and only include a typedef for sighandler_t when
targeting Linux, for compatibility with glibc.

Fixes: #125598
2025-02-07 10:43:56 -08:00
s.vgys
51759ffc44 fix: removes invalid token from LLVM_VERSION_SUFFIX in LIBC namespace (#126193)
Resolves #125831
2025-02-07 11:45:38 -06:00
c8ef
6807164500 [libc] Add the <endian.h> header. (#125168)
Closes [#124631](https://github.com/llvm/llvm-project/issues/124631).
ref:
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html

This patch adds the implementation of `endian.h`, which includes the
header itself and three related macros. These macros in the header rely
on the compiler preprocessor, similar to how
https://github.com/llvm/llvm-project/blob/main/libc/src/__support/endian_internal.h
does. Hopefully this will meet the requirements for compiling llvm with
llvm-libc.
2025-02-07 09:20:18 +08:00
Alex Prabhat Bara
74c2e9a82a [libc] generate sys/wait.h for aarch64 (#125171)
Fixes: #125102
2025-02-06 13:15:45 -08:00
Alan Zhao
4c8acbded1 [libc][minor] Fix assertion in LlvmLibcFILETest.SimpleFileOperations (#126109)
The file descriptor of the first opened file is not necessarily 3, so we
change the assertion so that it's >= 0 (i.e. not an error.)

Fixes #126106
2025-02-06 12:57:22 -08:00
PiJoules
ec7167bac7 [libc] Disable fixed point printing for baremetal (#126115)
This follows suite with disabling float printing.
2025-02-06 11:31:05 -08:00
Simon Tatham
7ef33e609c [libc] Fix recently introduced integer-type warnings (#125864)
These warnings all come up in code modified by one of my two recent
commits c06d0ff806 and b53da77c50, 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-06 09:14:59 +00:00
c8ef
e1c63bbde5 [libc] implement sys/uio/readv (#124718)
Closes #124694.

This patch adds the `sys/uio/readv` function.
ref:
https://pubs.opengroup.org/onlinepubs/009696699/functions/readv.html
2025-02-06 09:28:20 +08:00
Alan Zhao
fd4c4ed712 [libc] Add returns_twice attribute to setjmp(3) (#124370)
This is to ensure that calls to `setjmp(3)` result in correct code
generation that respects `setjmp(3)`'s `returns_twice` behavior.
Otherwise, we might run into bugs (for example, Clang may perform
tail-call optimization on this function if `-fno-builtins` is set
(#122840)).

---------

Co-authored-by: Nick Desaulniers <nick.desaulniers@gmail.com>
2025-02-05 14:37:03 -08:00
Nick Desaulniers
72aa3889fb [libc][poll] remove entrypoint for riscv (#125941)
riscv32 specifically doesn't provide EITHER SYS_poll or SYS_ppoll. We may be
able to reimplement poll in terms of syscalls to SYS_ppoll_time64, but will
leave that as a TODO for the future. (Such as when we want to be able to cross
compile for riscv32).

Link: #125940
Fixes: #125118
2025-02-05 13:56:30 -08:00
Nick Desaulniers
718b16a0fc [libc][wait] add comment about __W_CONTINUED value (#125934) 2025-02-05 13:43:48 -08:00
Nick Desaulniers
8e35b3d29b [libc][POSIX][poll.h] implement poll (#125118)
Simple syscall.

Fixes: #124647
2025-02-05 13:24:39 -08:00
Nick Desaulniers
b4d547ac82 [libc][wait] hard code __W_CONTINUED for SYS_waitid fallback (#125929)
riscv32 currently doesn't have SYS_wait4, so wait4 is implemented via fallback
to SYS_waitid. In #125572, I missed that we had one use of the removed
__W_CONTINUED value. Hard code it here.

Fixes: #125572
2025-02-05 13:10:53 -08:00
Roland McGrath
4eab219487 [libc] Remove LlvmLibcStackChkFail.Smash test (#125919)
This test was problematic, and also unnecessary.  It's not really
a test of the libc functionality or ABI.  That's already covered
by the LlvmLibcStackChkFail.Death test.  The Smash test was in
fact just testing that the compiler produces the call in the
expected situation.  That's a compiler test, not a libc test.

It's not really feasible to make a test like this both reliable
and safe.  Since it's not something libc needs to test, it's not
worth trying.
2025-02-05 12:34:19 -08:00
lntue
46c4845244 [libc][cmake] Do not overwrite SKIP_FLAG_EXPANSION_*. (#125762)
So that users can set these manually if needed.
2025-02-05 14:01:25 -05:00
Joseph Huber
2d8106cb5a [Clang] Add width handling for <gpuintrin.h> shuffle helper (#125896)
Summary:
The CUDA impelementation has long supported the `width` argument on its
shuffle instrucitons, which makes it more difficult to replace those
uses with this helper. This patch just correctly implements that for
AMDGPU and NVPTX so it's equivalent to `__shfl_sync` in CUDA. This will
ease porting.

Fortunately these get optimized out correctly when passing in known
widths.
2025-02-05 12:38:48 -06:00
Krishna Pandey
6f750cfb96 [libc][unistd] Implement setsid (#125704)
https://man7.org/linux/man-pages/man2/setsid.2.html

closes #124632
2025-02-05 10:08:16 -08:00
Nick Desaulniers
57614a340c [libc][sys/wait][linux] add missing and clean up existing macros (#125572)
This patch does a few things:
- replace macro definitions with an inclusion of the linux/wait.h kernel
  header.
  - WNOHANG
  - WUNTRACED
  - WEXITED
  - WCONTINUED
  - WSTOPPED
  - P_ALL
  - P_PID
  - P_PGID
  - P_PIDFD
- Add missing macro definitions mandated by POSIX. Some are needed to
  build LLVM.
  - WCOREDUMP
  - WIFCONTINUED
  - WIFSIGNALELD
  - WIFSTOPPED
  - WSTOPSIG
- Remove glibc style __W* macros. Users should stick with the POSIX
  macros. We can re-add them if necessary.
  - __WEXITSTATUS
  - __WTERMSIG
  - __WIFEXITED
  - __WIFSIGNALED
  - __WIFSTOPPED
  - __WIFCONTINUED
  - __WCOREDUMP
  - __W_EXITCODE
  - __W_STOPCODE
  - __W_CONTINUED
  - __WCOREFLAG

Fixes: #124944
2025-02-05 10:01:50 -08:00
Krishna Pandey
19c3e2f7de [libc] Fix all imports of src/string/memory_utils (#114939)
Fixed imports for all files *within* `libc/src/string/memory_utils`.
Note: This doesn't include **all** files that need to be fixed.

Fixes #86579
2025-02-05 09:24:44 -08:00
Roland McGrath
66ce716676 Revert "[libc] Make LlvmLibcStackChkFail.Smash test compatible with asan, hwasan" (#125785)
Reverts llvm/llvm-project#125763

This causes failures in asan. More thought is needed.
2025-02-04 16:06:21 -08:00
Roland McGrath
1e7624ca4f [libc] Make LlvmLibcStackChkFail.Smash test compatible with asan, hwasan (#125763)
Previously this test was entirely disabled under asan, but not
hwasan.  Instead of disabling the test, make the test compatible
with both asan and hwasan by disabling sanitizers only on the
subroutine that does the stack-smashing.
2025-02-04 13:42:23 -08:00
Simon Tatham
5afb31dbd6 [libc][float_dec_converter_limited] Add missing LIBC_INLINE (#125655)
This caused a build failure in check-libc introduced by commit
b53da77c50.
2025-02-04 09:20:36 +00:00
Simon Tatham
b53da77c50 [libc] Alternative algorithm for decimal FP printf (#123643)
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.
2025-02-04 08:57:54 +00:00
Simon Tatham
c06d0ff806 [libc] Optimize BigInt→decimal in IntegerToString (#123580)
When IntegerToString converts a BigInt into decimal, it determines each
digit by computing `n % 10` and then resets n to `n / 10`, until the
number becomes zero. The div and mod operations are done using
`BigInt::divide_unsigned`, which uses the simplest possible bit-by-bit
iteration, which is a slow algorithm in general, but especially so if
the divisor 10 must first be promoted to a BigInt the same size as the
dividend. The effect is to make each division take quadratic time, so
that the overall decimal conversion is cubic – and the division is
quadratic in the number of _bits_, so the constant of proportionality is
also large.

In this patch I've provided custom code to extract decimal digits much
faster, based on knowing that the divisor is always 10, and processing a
word at a time. So each digit extraction is linear-time with a much
smaller constant of proportionality.

Full comments are in the code. The general strategy is to do the
reduction mod 10 first to determine the output digit; then subtract it
off, so that what's left is guaranteed to be an exact multiple of 10;
and finally divide by 10 using modular-arithmetic techniques rather than
reciprocal-approximation-based ordinary integer division.

I didn't find any existing tests of IntegerToString on a BigInt, so I've
added one.
2025-02-04 08:57:41 +00:00
Joseph Huber
36d6b63f85 [libc][NFC] Silence C99 extension warnings on clang for RPC
Summary:
This only shows up during the build of the server, silence it.
2025-02-03 16:16:18 -06:00
Roland McGrath
648981f913 [libc] Build with -Wdeprecated, fix some warnings (#125373)
While GCC's -Wdeprecated is on by default and doesn't do much,
Clang's -Wdeprecated enables many more things.  More apply in
C++20, so switch a test file that tickled one to using that.  In
future, C++20 should probably be made the baseline for compiling
all the libc code.
2025-02-01 18:22:18 -08:00
Alex Prabhat Bara
9b52dbe0e0 [libc] added _POSIX_ARG_MAX in limits.h (#124954)
Fixes: #124947
2025-01-30 12:39:49 -08:00
lntue
bcf306e0eb [libc] Update include directory for libcMPCWrapper target when LIBC_MPC_INSTALL_PATH is set. (#124810) 2025-01-29 15:19:25 -05:00
Joseph Huber
26f4e2a701 [libc][docs] Fix the RPC documentation leaking ports
Summary:
Forgot to close it, that'll make it deadlock after awhile.
2025-01-28 15:26:10 -06:00