Commit Graph

389 Commits

Author SHA1 Message Date
Guillaume Chatelet 7c2ece523d [libc] Normalize LIBC_TARGET_MACHINE
Current implementation defines LIBC_TARGET_MACHINE with the use of CMAKE_SYSTEM_PROCESSOR.
Unfortunately CMAKE_SYSTEM_PROCESSOR is OS dependent and can produce different results.
An evidence of this is the various matchers used to detect whether the architecture is x86.

This patch normalizes LIBC_TARGET_MACHINE and renames it LIBC_TARGET_ARCHITECTURE.
I've added many architectures but we may want to limit ourselves to x86 and ARM.

Differential Revision: https://reviews.llvm.org/D101524
2021-05-05 15:52:42 +00:00
Raman Tenneti a72499e475 [libc] Introduce asctime, asctime_r to LLVM libc
[libc] Introduce asctime, asctime_r to LLVM libc

asctime and asctime_r share the same common code. They call asctime_internal
a static inline function.

asctime uses snprintf to return the string representation in a buffer.
It uses the following format (26 characters is the buffer size) as per
7.27.3.1 section in http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2478.pdf.
The buf parameter for asctime_r shall point to a buffer of at least 26 bytes.

snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",...)

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D99686
2021-05-03 17:15:00 -07:00
Guillaume Chatelet 0e97e84a65 [libc] warns about missing linting only in full build mode
Differential Revision: https://reviews.llvm.org/D101609
2021-05-03 08:39:26 +00:00
Siva Chandra Reddy c6aa206b42 [libc] Add differential quality and perf analysis targets for sinf and cosf.
Infrastructure needed for setting up the diff binaries has been added.
 Along the way, an exhaustive test for sinf and cosf have also been added.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D101276
2021-04-26 19:39:33 +00:00
Guillaume Chatelet b5f04d81a2 [libc] Use different alignment for memcpy between ARM and x86.
Aligned copy used to be 'destination aligned' for x86 but this decision was reverted in D93457 where we noticed that it was better for ARM to be 'source aligned'.
More benchmarking confirmed that it can be up to 30% faster to align copy to destination for x86. This Patch offers both implementations and switches x86 back to destination aligned.
It also fixes alignment to 32 byte on x86.

Differential Revision: https://reviews.llvm.org/D101296
2021-04-26 19:30:00 +00:00
Guillaume Chatelet fa404ae43a [libc] Enhance ArrayRef + unittests
This patch mostly adds unittests for `ArrayRef` and `MutableArrayRef`, additionnaly:
 - We mimic the behavior of `std::vector` and disallow CV qualified type (`ArrayRef<const X>` is not allowed).
   This is to make sure that the type traits are always valid (e.g. `value_type`, `pointer`, ...).
 - In the previous implementation `ArrayRef` would define `value_type` as `const T` but this is not correct, it should be `T` for both `MutableArrayRef` and `ArrayRef`.
 - We add the `equals` method to ease testing,
 - We define the constructor taking an `Array` outside of the base implementation to ensure we match `const Array<T>&` and not `Array<const T>&` in the case of `ArrayRef`.

Differential Revision: https://reviews.llvm.org/D100732
2021-04-21 13:25:24 +00:00
Siva Chandra Reddy f76fb7d420 [libc] Add fma to the C standard spec. 2021-04-21 06:00:35 +00:00
Siva Chandra Reddy 653345155a [libc] Disable fma and fmaf for x86_64.
The version of clang installed on the buildbot workers is not able to
compile them. However, the version of gcc installed is able to compile
them fine. So, this change disables them until we can find a way to
compile them using clang on the buildbot workers.
2021-04-21 05:01:15 +00:00
Siva Chandra 95934c3a37 [libc] Add hardware implementations of fma and fmaf for x86_64 and aarch64.
The current generic implementation of the fmaf function has been moved
to the FPUtil directory. This allows one use the fma operation from
implementations of other math functions like the trignometric functions
without depending on/requiring the fma/fmaf/fmal function targets. If
this pattern ends being convenient, we will switch all generic math
implementations to this pattern.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D100811
2021-04-21 04:31:27 +00:00
Siva Chandra bbba69425c [libc][NFC] Use explicit conversion in aarch64 FEnv. 2021-04-16 22:53:12 -07:00
Siva Chandra Reddy 7db1102a10 [libc]NFC] Use explicit conversion on frexpl_test and logbl_test. 2021-04-17 05:50:00 +00:00
Siva Chandra Reddy bb8aa2ad1a [libc][NFC] Use explicit conversion in modfl_test. 2021-04-17 05:42:17 +00:00
Siva Chandra Reddy fb706e086c [libc][NFC] Make conversion from FPBits to the float point type explicit.
This will help us catch errors like the ones fixed by the commit
31ed45d9cf
2021-04-17 05:22:20 +00:00
Siva Chandra Reddy fb69b92c7b [libc][NFC] Add common template test class for sqrt, sqrtf and sqrtl. 2021-04-17 05:13:27 +00:00
Guillaume Chatelet 2bfe15810d [libc] Fix wrongly deduced type 2021-04-16 21:59:25 +00:00
Guillaume Chatelet 7e075ad0b2 [libc] Add endianness support
Add endianness detection support. This will be useful to implement `memcmp`.

Differential Revision: https://reviews.llvm.org/D100571
2021-04-16 21:35:08 +00:00
Guillaume Chatelet 7c02dc22e4 [libc] Extends the testing framework to support typed test
This patch provides `TYPED_TEST` and `TYPED_TEST_F` (similar in functionnality to gtest).
This is needed to extensively test building blocks for memory functions.

Example for `TYPED_TEST_F`:
```
template <typename T> class LlvmLibcMyTestFixture : public testing::Test {};

using Types = testing::TypeList<char, int, long>;

TYPED_TEST_F(LlvmLibcMyTestFixture, Simple, Types) {
  EXPECT_LE(sizeof(ParamType), 8UL);
}
```

Example for `TYPED_TEST`:
```
using Types = testing::TypeList<char, int, long>;

TYPED_TEST(LlvmLibcMyTest, Simple, Types) {
  EXPECT_LE(sizeof(ParamType), 8UL);
}
```

`ParamType` is displayed as fully qualified canonical type which can be difficult to read, the user can provide a more readable name by using the `REGISTER_TYPE_NAME` macro.

Differential Revision: https://reviews.llvm.org/D100631
2021-04-16 21:21:35 +00:00
Siva Chandra Reddy 80e166f81a [libc][NFC] Add template tests for a bunch of math functions.
Namely, template tests have been added for the following functions:
ceil, copysign, fabs, fmax, fmin, floor, trunc, round.
2021-04-16 17:28:17 +00:00
Guillaume Chatelet 907b52d1a7 [libc] Fix typo 2021-04-16 08:09:28 +00:00
Guillaume Chatelet f6b6568536 [libc] Add slice/take/drop methods to ArrayRef
Add various methods from llvm::ArrayRef. Refactor implementation to remove code duplication.

Differential Revision: https://reviews.llvm.org/D100569
2021-04-16 07:54:48 +00:00
Guillaume Chatelet 03375089f5 [libc] Add index operator[] to StringView 2021-04-15 15:55:37 +00:00
Siva Chandra Reddy 31ed45d9cf [libc][Obvious] Fix nextafter* implementation.
It broke when FPBits was converted to a union.
2021-04-13 11:38:24 -07:00
Siva Chandra Reddy 6666e0d7a2 [libc] Make FPBits a union.
This helps us avoid the uncomfortable reinterpret-casts. Avoiding the
reinterpret casts prevents us from tripping the sanitizers as well.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D100360
2021-04-13 09:21:35 -07:00
Nathan Chancellor 4e0045cc9b
libc: Default LIBC_INSTALL_PREFIX to ${CMAKE_INSTALL_PREFIX}
b22f448c21 added a rule to install libllvmlibc.a to
${LIBC_INSTALL_PREFIX}/${LIBC_INSTALL_LIBRARY_DIR}, which will be /lib
by default, which is disruptive to builds that stay within a user's
/home holder:

  $ ninja install
  ...
  -- Installing: /lib/libllvmlibc.a
  CMake Error at projects/libc/lib/cmake_install.cmake:54 (file):
    file INSTALL cannot copy file
   "/home/nathan/cbl/github/tc-build/build/llvm/stage1/projects/libc/lib/libllvmlibc.a"
    to "/lib/libllvmlibc.a": Permission denied.
  Call Stack (most recent call first):
    projects/libc/cmake_install.cmake:51 (include)
    projects/cmake_install.cmake:47 (include)
    cmake_install.cmake:76 (include)
  ...

Change LIBC_INSTALL_PREFIX's default value to ${CMAKE_INSTALL_PREFIX} so
that 'ninja install' does not attempt to install anything outside of the
user's requested installation location.

Differential Revision: https://reviews.llvm.org/D99636

Reviewed By: sivachandra

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
2021-03-30 21:43:25 -07:00
Guillaume Chatelet 77d81c2270 [libc] Fix msan/asan memcpy reentrancy
This is needed to prevent asan/msan instrumentation to redirect CopyBlock to `__asan_memcpy` (resp. `__msan_memcpy`).
These functions would then differ operation to `memcpy` which leads to reentrancy issues.

With this patch, `memcpy` is fully instrumented and covered by asan/msan.

If this turns out to be too expensive, instrumentation can be selectively or fully disabled through the use of the `__attribute__((no_sanitize(address, memory)))` annotation.

Differential Revision: https://reviews.llvm.org/D99598
2021-03-30 15:28:47 +00:00
Raman Tenneti 8b35159ac7 [libc] Introduces gmtime_r to LLVM libc, based on C99/C2X/Single Unix Sp.
gmtime and gmtime_r share the same common code. They call gmtime_internal
a static inline function. Thus added only validation tests for gmtime_r.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D99046
2021-03-22 14:04:05 -07:00
Siva Chandra 1e01f2f410 [libc][NFC] Add an alias named "check-llvmlibc" for "check-libc". 2021-03-22 16:55:34 +00:00
Siva Chandra b22f448c21 [libc] Add a target "install-llvmlibc" to install LLVM libc static archive. 2021-03-22 04:45:14 +00:00
Siva Chandra 3d155157bf [libc] Use add_library in add_entrypoint_library instead of invoking ar. 2021-03-20 04:25:51 +00:00
Raman Tenneti eaae52c1fd This introduces gmtime to LLVM libc, based on C99/C2X/Single Unix Spec.
This change doesn't handle TIMEZONE, tm_isdst and leap seconds.

Moved shared code between mktime and gmtime into time_utils.cpp.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D98467
2021-03-16 16:44:48 -07:00
Siva Chandra Reddy 7c0179129f [libc][Obvious] Fix except flags reading overflow detected by asan. 2021-03-13 16:30:33 -08:00
Siva Chandra Reddy 7b5ab956b1 [libc][Obvious] Add string tests to non-full-build mode. 2021-03-12 14:01:18 -08:00
Siva Chandra 954a7289d2 [libc] Adjust full build entrypoints for aarch64. 2021-03-12 13:50:56 -08:00
Siva Chandra Reddy e9e788d145 [libc] Introduce a full build mode CMake option.
This option will build LLVM libc as a full libc by itself. In this mode,
it is not expected that it will be mixed with other libcs. The
non-full-build mode will be the default LLVM libc build mode. In a future
where LLVM libc is complete enough, the full libc build will be made the
default mode.
2021-03-12 13:28:40 -08:00
Siva Chandra Reddy b937908c37 [libc][NFC] Move the template implementation of integer_abs to __support.
This eliminates cross-header dependency from stdlib to string.
2021-03-11 20:20:18 -08:00
Leonard Chan baf637dcde Rename top-level LICENSE.txt files to LICENSE.TXT
This makes all the license filenames uniform across subprojects.

Differential Revision: https://reviews.llvm.org/D98380
2021-03-10 21:26:24 -08:00
Siva Chandra Reddy 3f3f88fb95 [libc][Obvious] Add a file which was missed in 001a12ed59. 2021-03-08 15:39:29 -08:00
Siva Chandra Reddy 001a12ed59 [libc][NFC] Make x86_64 fenv functions msan safe.
These functions used inline asm to read FPU state. This change adds
explicit unpoisoning in these functions as the sanitizers don't see the
read operations.
2021-03-08 15:20:08 -08:00
Siva Chandra Reddy 9550f8ba9a [libc][NFC] Make few fenv functions work with fexcept_t from other libcs. 2021-03-04 11:25:28 -08:00
Siva Chandra Reddy 35e2e448ce [libc] Remove redundant header files included from internal paths. 2021-03-03 21:29:46 -08:00
Siva Chandra Reddy 6628387c9a [libc][NFC] Exclude few targets from the `all` target. 2021-02-24 08:59:55 -08:00
Siva Chandra Reddy 5f0800cc18 [libc][NFC] Remove headergen for the cacheline size macro.
We want to be able to build and test the string functions in contexts
like that of Fuchsia where LLVM pieces like tablegen are not available.
Since header generation uses tablegen, we are removing the dependency on
headergen here.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D97363
2021-02-24 07:29:09 -08:00
Siva Chandra Reddy 8d4ac5337f [libc][NFC] Merge llvmlibc and llvmlibm targets in to a single target.
The single target is called llvmlibc. We can add back smaller libm and
other targets in future if required.
2021-02-23 21:29:06 -08:00
Siva Chandra Reddy dbb131d53a [libc] Add a standalone flavor of an equivalent of std::string_view.
This class is to serve as a replacement for llvm::StringRef as part of
the plans to limit dependency on other parts of LLVM. One use of
llvm::StringRef in MPFRWrapper has been replaced with the new class.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D97330
2021-02-23 15:40:26 -08:00
Tue Ly b79507a4ac [libc] Add exhaustive test for sqrtf.
Differential Revision: https://reviews.llvm.org/D96985
2021-02-23 18:39:33 -05:00
Siva Chandra Reddy 881402ce62 [libc][NFC] Eliminate couple of dependencies on llvm/ADT/StringExtras.h. 2021-02-22 21:41:24 -08:00
Raman Tenneti 80bea4a0d5 [libc] [Obvious] Fix. 2021-02-22 19:15:35 -08:00
Raman Tenneti 034f562925 Changes to mktime to handle invalid dates, overflow and underflow andcalculating the correct date and thenumber of seconds even if invalid datesare passed as arguments.
Added tests for invalid dates like the following
  Date 1970-01-01 00:00:-1 is treated as 1969-12-31 23:59:59 and seconds
  are returned for the modified date.

Tested the code by doing ninja check-libc (and cmake).

Reviewed By: sivachandra, rtenneti

Differential Revision: https://reviews.llvm.org/D96684
2021-02-22 18:37:40 -08:00
Siva Chandra Reddy b7e05c874b [libc] Add implementations of the remaining fenv functions.
Namely, implementations of fegetexceptfflag, fesetexceptflag,
fegetenv, fesetenv, feholdexcept and feupdateenv have been added.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D96935
2021-02-18 13:29:40 -08:00
Siva Chandra Reddy dba14814a6 [libc][NFC] Make few maths functions buildable outside of LLVM libc build.
Few math functions manipulate errno. They assumed that LLVM libc's errno
is available. However, that might not be the case when these functions
are used in a libc which does not use LLVM libc's errno. This change
switches such uses of LLVM libc's errno to the normal public errno macro.
This does not affect LLVM libc's build because the include order ensures
we get LLVM libc's errno. Also, the header check rule ensures we are only
including LLVM libc's errno.h.
2021-02-16 09:14:29 -08:00