2408 Commits

Author SHA1 Message Date
Kazu Hirata
b8f89b84bc Use StringRef::{starts,ends}_with (NFC)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-16 15:02:17 -08:00
Guillaume Chatelet
57fcc23896
[libc] Fix improper initialization of StorageType (#75610)
`StorageType` may be a `BigInt` under the hood. Initializing it with
`-1` does not yields the maximum value.
2023-12-15 15:51:10 +01:00
Guillaume Chatelet
3546f4da19
[libc][NFC] Rename MANTISSA_WIDTH in FRACTION_LEN (#75489)
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-15 13:57:35 +01:00
Guillaume Chatelet
b4fa7f4c94
[libc][NFC] Split str_to_float tests and reduce type clutter (#75471)
This is a follow up on #75353.
2023-12-15 09:44:10 +01:00
Nick Desaulniers
7c6b4be615
[libc] fix msan failure in mempcpy_test (#75532)
Internal builds of the unittests with msan flagged mempcpy_test.

    ==6862==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x55e34d7d734a in length
llvm-project/libc/src/__support/CPP/string_view.h:41:11
#1 0x55e34d7d734a in string_view
llvm-project/libc/src/__support/CPP/string_view.h:71:24
#2 0x55e34d7d734a in
__llvm_libc_9999_0_0_git::testing::Test::testStrEq(char const*, char
const*, char const*, char const*,
__llvm_libc_9999_0_0_git::testing::internal::Location)
llvm-project/libc/test/UnitTest/LibcTest.cpp:284:13
#3 0x55e34d7d4e09 in LlvmLibcMempcpyTest_Simple::Run()
llvm-project/libc/test/src/string/mempcpy_test.cpp:20:3
#4 0x55e34d7d6dff in
__llvm_libc_9999_0_0_git::testing::Test::runTests(char const*)
llvm-project/libc/test/UnitTest/LibcTest.cpp:133:8
#5 0x55e34d7d86e0 in main
llvm-project/libc/test/UnitTest/LibcTestMain.cpp:21:10

SUMMARY: MemorySanitizer: use-of-uninitialized-value
llvm-project/libc/src/__support/CPP/string_view.h:41:11 in length

What's going on here is that mempcpy_test.cpp's Simple test is using
ASSERT_STREQ with a partially initialized char array. ASSERT_STREQ calls
Test::testStrEq which constructs a cpp:string_view. That constructor
calls the
private method cpp::string_view::length. When built with msan, the loop
is
transformed into multi-byte access, which then fails upon access.

I took a look at libc++'s __constexpr_strlen which just calls
__builtin_strlen(). Replacing the implementation of
cpp::string_view::length
with a call to __builtin_strlen() may still result in out of bounds
access when
the test is built with msan.

It's not safe to use ASSERT_STREQ with a partially initialized array.
Initialize the whole array so that the test passes.
2023-12-14 13:39:33 -08:00
Guillaume Chatelet
493cc71d72
[libc][NFC] Remove MantissaWidth traits (#75458)
Same as #75362, the traits does not bring a lot of value over
`FloatProperties::MANTISSA_WIDTH` (or `FPBits::MANTISSA_WIDTH`).
2023-12-14 15:07:09 +01:00
Guillaume Chatelet
f69c83f8da
[libc][NFC] Remove ExponentWidth traits (#75362)
Is it redundant with `FloatProperties::EXPONENT_WIDTH` and does bear its
weight.
2023-12-14 10:07:48 +01:00
Nick Desaulniers
c0ad6e2fa6
[libc] fix unit tests (#75361)
Fixes #75261
2023-12-13 09:55:29 -08:00
Guillaume Chatelet
6b37d8b73f
[libc][NFC] Shorten type names in tests (#75358) 2023-12-13 18:09:40 +01:00
Nick Desaulniers
eaa11526c8
[libc] fix -Wmacro-redefined (#75261)
When building with compiler-rt enabled, warnings such as the following
are
observed:


llvm-project/llvm/build/projects/compiler-rt/../libc/include/llvm-libc-macros/linux/sys-stat-macros.h:46:9:
    warning: 'S_IXOTH' macro redefined [-Wmacro-redefined]
    #define S_IXOTH 00001
            ^

llvm-project/llvm/build/projects/compiler-rt/../libc/include/llvm-libc-macros/linux/fcntl-macros.h:61:9:
    note: previous definition is here
    #define S_IXOTH 01
            ^
It looks like we have these multiply defined. Deduplicate these flags;
users
should expect to find them in sys/stat.h. S_FIFO was wrong anyways
(should
have been S_IFIFO).
2023-12-13 08:39:39 -08:00
Guillaume Chatelet
213c49807f
[reland][libc][NFC] Implement FPBits in terms of FloatProperties to reduce clutter (#75196) (#75318)
Also make type naming consistent by using `UIntType` instead of
`intU_t`.
This patch is a reland of #75196 but does not include the "use `FPBits`
instead of `FPBits_t`, `FPB`" part. This needs more work.
2023-12-13 12:22:02 +01:00
Guillaume Chatelet
a9af317fbe
Revert "[libc][NFC] Implement FPBits in terms of FloatProperties to reduce clutter" (#75304)
Reverts llvm/llvm-project#75196

GCC complains about change of meaning for `FPBits`
```
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/generic/FMod.h:188:9: error: declaration of ‘using FPBits = struct __llvm_libc_18_0_0_git::fputil::FPBits<T>’ changes meaning of ‘FPBits’ [-fpermissive]
  188 |   using FPBits = FPBits<T>;
      |         ^~~~~~
```

I'll reland with a different name.
2023-12-13 09:55:27 +01:00
Guillaume Chatelet
f64a0576ad
[libc][NFC] Implement FPBits in terms of FloatProperties to reduce clutter (#75196)
Also make type naming consistent:
 - `UIntType` instead of `intU_t`
 - `FPBits` instead of `FPBits_t`, `FPB`
2023-12-13 09:23:06 +01:00
Schrodinger ZHU Yifan
2c0abdf2df
[libc] [riscv] support build with scudo on riscv64 (#74951)
This patch fixes cmake configuration when building with
LLVM_LIBC_INCLUDE_SCUDO. In libc, LIBC_TARGET_ARCHITECTURE is renamed
from `riscv64` to `riscv`. However, `compiler-rt`, hence `scudo`,
distinguishes `riscv32` and `riscv64` in the support list. As a result,
we need to translate the architecture name accordingly.
2023-12-13 16:14:11 +08:00
michaelrj-google
8180ea8694
[libc] Add bind function (#74014)
This patch adds the bind function to go with the socket function. It
also cleans up a lot of socket related data structures.
2023-12-12 13:36:11 -08:00
Schrodinger ZHU Yifan
3568521e3d
[libc] fix issues around stack protector (#74567)
If a function is declared with stack-protector, the compiler may try to
load the TLS. However, inside certain runtime functions, TLS may not be
available. This patch disables stack protectors for such routines to fix
the problem.

Closes #74487.
2023-12-12 12:31:51 -08:00
Guillaume Chatelet
f22a65c158
[libc][NFC] Reuse FloatProperties constant instead of creating new ones (#75187)
Also exposes a bit more of `FloatProperties` and adds documentation.
This should have been several patches but I was lazy.
2023-12-12 15:56:06 +01:00
Guillaume Chatelet
1e47a157f8
[libc][NFC] Remove last use of BitsType in FloatProperties (#75174)
Also start to expose some of the internals to avoid duplication.
2023-12-12 14:00:25 +01:00
Guillaume Chatelet
dd85e67dc4
[libc] Add mask functions to math_extras (#75169) 2023-12-12 13:23:21 +01:00
Guillaume Chatelet
198238a598
[libc][NFC] Remove remaining specializations in FloatProperties (#75165)
This patch sinks `EXPLICIT_BIT_MASK` into `get_explicit_mantissa` - the
only function using it. Then it sinks the content of
`FPCommonProperties` directly into `FPProperties`.
2023-12-12 13:06:02 +01:00
Guillaume Chatelet
b00e445c4d
[libc][NFC] Make EXPONENT_BIAS int32_t (#75046)
`EXPONENT_BIAS` is almost always used with signed arithmetic. Making it
an `int32_t` from the start reduces the chances to run into
implementation defined behavior (cast from unsigned to signed is
implementation-defined until C++20).


https://en.cppreference.com/w/cpp/language/implicit_conversion#:~:text=If%20the%20destination%20type%20is%20signed,arithmetic%20overflow%2C%20which%20is%20undefined).
2023-12-12 13:03:26 +01:00
Guillaume Chatelet
b9be9f66c7
[libc] Make the bit header compatible with uint128 types (#75161) 2023-12-12 11:54:54 +01:00
Guillaume Chatelet
2bab0e0167
[libc][NFC] Swap ifdef logic for UInt128 (#75160) 2023-12-12 11:44:27 +01:00
michaelrj-google
b1a91b7ee0
[libc][NFC] fix uint128 init in float properties (#75084)
An unsigned integer was being initialized with -1 to set all its bits to
1, but this doesn't work for the BigInt class which may be used as an
integer type on systems that don't support uint128 natively. This patch
moves the initialization to use ~T(0) instead.
2023-12-11 10:46:10 -08:00
Guillaume Chatelet
e4f3ec2579
[libc][NFC] Simplify FloatProperties implementation (#74821)
This is a follow up on #74481 that migrates code from all
specializations into `FPCommonProperties` (except for
`EXPLICIT_BIT_MASK` in the `X86_Binary80` specialization)
2023-12-11 17:40:47 +01:00
Guillaume Chatelet
7e761ba854
[libc][NFC] Simplify FloatProperties implementation (#74481)
Factor in common properties of `FloatProperties`.

This is a first patch of a series to simplify `FloatProperties` and
`FPBits` implementations.
2023-12-11 17:14:20 +01:00
Guillaume Chatelet
c703e65765
[libc][NFC] Remove custom leading_zeroes, factor in frequent typenames (#74825) 2023-12-11 17:10:42 +01:00
Guillaume Chatelet
7b387d2758
[libc][NFC] Fix mixed up biased/unbiased exponent (#75037)
According to [wikipedia](https://en.wikipedia.org/wiki/Exponent_bias)
the "biased exponent" is the encoded form that is always positive
whereas the unbiased form is the actual "real" exponent that can be
positive or negative.
`FPBits` seems to be using `unbiased_exponent` to describe the encoded
form (unsigned). This patch simply use `biased` instead of `unbiased`.
2023-12-11 17:06:48 +01:00
Nishant Mittal
ac640c627c
[libc][math] Ensure fputil::nextafter is called with correct type args (#75009)
Follow up to
https://github.com/llvm/llvm-project/pull/73698#discussion_r1411134482
2023-12-10 15:48:31 -05:00
Guillaume Chatelet
05420a1754
Revert "[libc] Make BigInt bit_cast-able to compatible types" (#74887)
This reverts the following commits:
 - a539a090009378ecfcfbfaaa280eeac8f5b9d695
 - 31316b3f8511d659cc14ebc72fb2b226f78478a9

Rationale for revert:
https://github.com/llvm/llvm-project/issues/74258#issuecomment-1847836861
2023-12-08 22:10:57 +01:00
Guillaume Chatelet
a539a09000
[libc] Fix missing UInt initialization (#74869)
Fix forward for #74862
2023-12-08 18:12:35 +01:00
Guillaume Chatelet
31316b3f85
[reland][libc] Make BigInt bit_cast-able to compatible types (#74862)
Fix #74258
This is a reland of #74837, the error went unnoticed because it compiles
fine on
clang-16 but not on clang-12 which is the version used on the buildbots.

The fix was to explicitly initialize `BigInt` variables in `constexpr`
operations: `BigInt<Bits, Signed> result(0);` instead of `BigInt<Bits,
Signed> result;`
2023-12-08 18:03:24 +01:00
Guillaume Chatelet
d5199b43be
Revert "[libc] Make BigInt bit_cast-able to compatible types" (#74856)
Reverts llvm/llvm-project#74837

Some build bot are failing because of missing constexpr.

https://lab.llvm.org/buildbot/#/builders/138/builds/56468/steps/7/logs/stdio
2023-12-08 17:15:10 +01:00
Guillaume Chatelet
a2b7ded40c
[libc] Make BigInt bit_cast-able to compatible types (#74837)
Fix #74258
2023-12-08 17:11:05 +01:00
lntue
b8f7c2ce89
[libc][NFC] Clean up conversion warnings in math function implementations. (#74697) 2023-12-08 09:54:21 -05:00
Nick Desaulniers
1ee6a1e38a
[libc] fix -Wshift-count-overflow in UInt.h (#74649)
Not that I'm very good at SFINAE, but it seems that conversion operators
are
perhaps difficult to compose with SFINAE. I saw an example that used one
layer
of indirection to have an explicit return type that could then be used
with
enable_if_t.

Link: https://stackoverflow.com/a/7604580
Fixes: #74623
2023-12-07 08:20:46 -08:00
Nick Desaulniers
079ca05eea
[libc] fix -Wtype-limits in wctob (#74511)
GCC warns:
```
libc/src/__support/wctype_utils.h:33:20: error: comparison of unsigned
expression in ‘< 0’ is always false [-Werror=type-limits]
   33 |   if (c > 127 || c < 0)
      |                  ~~^~~
```

Looking into the signedness of wint_t, it looks like depending on the
platform,
__WINT_TYPE__ is defined to int or unsigned int depending on the
platform.

Link:
https://lab.llvm.org/buildbot/#/builders/250/builds/14891/steps/6/logs/stdio
2023-12-05 15:37:27 -08:00
Schrodinger ZHU Yifan
86bde5adc8
[libc] implement prctl (#74386)
Implement `prctl` as specified in
https://man7.org/linux/man-pages/man2/prctl.2.html.

This patch also includes test cases covering two simple use cases:

1. `PR_GET_NAME/PR_SET_NAME`: where userspace data is passed via arg2.
2. `PR_GET_THP_DISABLE`: where return value is passed via syscal retval.
2023-12-05 12:31:00 -08:00
Nick Desaulniers
9c2e5449f4
[libc] remove spurious LIBC_INLINE (#74505)
This function is marked noinline; the inline keyword (or whatever
LIBC_INLINE
resolves to) will have no effect.
2023-12-05 10:53:48 -08:00
Schrodinger ZHU Yifan
86e99e11e5
[libc] [search] improve hsearch robustness (#73896)
Following up the discussion at
https://github.com/llvm/llvm-project/pull/73469#discussion_r1409593911
by @nickdesaulniers.

According to FreeBSD implementation
(https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/upstream-freebsd/lib/libc/stdlib/hcreate.c),
`hsearch` is able to handle the cases where the global table is not
properly initialized. To do this, FreeBSD actually allows hash table to
be dynamically resized. If the global table is uninitialized at the
first call, the table will be initialized with a minimal size; hence
subsequent insertion will be reasonable as the table grows
automatically.

This patch mimic such behaviors. More precisely, this patch introduces:

1. a full table iterator that scans each element in the table,
2. a resize routine that is automatically triggered whenever the load
factor is reached where it iterates the old table and insert the entries
into a new one,
3. more tests that cover the growth of the table.
2023-12-05 09:06:48 -08:00
Joseph Huber
a28e4eac26
[libc] Default to a single threaded thread pool for GPU tests (#74486)
Summary:
The GPU tests tend to fail when run massively in parallel. This is why
we use a CMake job pool to limit it to 1 in most cases. We should
default to the configuration that is most likely to work, that being a
single thread. There aren't enough GPU tests for this to be a massive
increase in test time on the bots, so we should default to what works
guaranteed.
2023-12-05 15:25:50 +00:00
Guillaume Chatelet
21b986637b
[libc] Fix arm32 tests (#74457)
`ASSERT_EQ` requires that both operands have the same type but on arm32
`size_t` is `unsigned int` instead of `unsigned long`. Using `size_t`
explicitely to avoid "conflicting types for parameter 'ValType"
2023-12-05 13:53:19 +01:00
Guillaume Chatelet
1d89478830
[reland][libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead (#73939) (#74446)
Same as #73939 but also fix `libc/src/string/memory_utils/op_aarch64.h`
that was still using `deferred_static_assert`.
2023-12-05 11:35:13 +01:00
Guillaume Chatelet
de7fdc5b54
Revert "[libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead" (#74444)
Reverts llvm/llvm-project#73939

This broke libc-aarch64-ubuntu build bot 
https://lab.llvm.org/buildbot/#/builders/138/builds/56186
2023-12-05 11:25:39 +01:00
Guillaume Chatelet
b140948850
[libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead (#73939) 2023-12-05 11:21:07 +01:00
michaelrj-google
ab65c9c3bb
[libc][NFC] fix int warnings in float conversion (#74379)
The printf float to string conversion functions had some implicit
integer conversion warnings on gcc. This patch adds explicit casts to
these places.
2023-12-04 16:34:24 -08:00
Schrodinger ZHU Yifan
6fd1c1b8ef
[libc] fix HashTable warnings and build problems (#74371)
According to https://lab.llvm.org/buildbot/#/builders/163/builds/48002,
the generic build on HashTable fails with two major issues with
`werror`:
1. warnings on `error: suggest braces around initialization of
subobject`.
2. `__support/HashTable` tests are built regardless of its entrypoints`

This PR attempts to fix such issues.
2023-12-04 14:06:57 -08:00
Nick Desaulniers
0d59cfc7a3
[libc] fix -Wconversion in float_to_string.h (#74369)
Fixes:
libc/src/__support/float_to_string.h:551:48: error: conversion from
‘long
unsigned int’ to ‘int32_t’ {aka ‘int’} may change value
[-Werror=conversion]
551 | const int32_t shift_amount = SHIFT_CONST + (-exponent - IDX_SIZE *
idx);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Observed in gcc fullbuilds.

IDX_SIZE is a size_t (aka 'long unsigned int'), but has the value 128,
so the
expression is undergoing implicit promotion.

Link: https://lab.llvm.org/buildbot/#/builders/250/builds/14891
2023-12-04 13:12:51 -08:00
Schrodinger ZHU Yifan
ff51b60b18
[libc] Revert #73704 and subsequent fixes #73984, #74026 (#74355)
The test cases of mincore require getting correct page size from OS. As
`sysconf` is not functioning correctly, these patches are implemented in
a somewhat confusing way. We revert such patches and will reintroduce
mincore after we correct sysconf.

This reverts 54878b8, 985c0d1 and 418a3a4.
2023-12-04 12:49:12 -08:00
Nick Desaulniers
6886a52d6d Revert "[libc] build with -Werror (#73966)"
This reverts commit 606653091d1a66d1a83a1bfdea2883cc8d46687e.

Post submit buildbots are now red. We can use these explicit errors to better
clean up existing warnings, then reland this.

Link: #73966
2023-12-04 11:31:59 -08:00