Commit Graph

841 Commits

Author SHA1 Message Date
Alex Brachet 60d4a10710 [libc] Guard common macro names
Differential revision: https://reviews.llvm.org/D127692
2022-06-14 15:05:16 +00:00
Alex Brachet 6d1543a167 [libc] Add explicit casts for string functions
This fixes warnings from `-Wimplicit-int-conversion`

Differential revision: https://reviews.llvm.org/D127694
2022-06-13 21:07:45 +00:00
Alex Brachet 15db8c306d [libc] Add Fuchsia implementation of ::testing::Test 2022-06-13 21:03:51 +00:00
Siva Chandra Reddy 5db4177817 [libc] Add pthread_detach and thrd_detach.
Tests for pthread_detach and thrd_detach have not been added. Instead, a
test for the underlying implementation has been added as it makes use of
an internal wait method to synchronize with detached threads.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D127479
2022-06-11 05:29:40 +00:00
Tue Ly ee89927707 [libc] Implement double precision FMA for targets without FMA instructions.
Implement double precision FMA (Fused Multiply-Add) for targets without
FMA instructions using __uint128_t to store the intermediate results.

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D124495
2022-06-10 20:57:27 -04:00
Michael Jones 02f4affe2d [libc] add EXP_MAT_MASK to x87 long double
A previous patch added the constant EXP_MANT_MASK to the FloatProperties
for other types of long double. This patch adds it to the special 80-bit
x87 long double.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D127550
2022-06-10 16:26:29 -07:00
Tue Ly 2a746ebf1a [libc][Obvious] Change all __builtin_clz* calls to clz in builtin_wrappers.h. 2022-06-10 16:03:10 -04:00
Michael Jones 6ce490e5a6 [libc] add buffering to FILE writes
Previously all FILE objects were fully buffered, this patch adds line
buffering and unbuffered output, as well as applying them to stdout and
stderr.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D126829
2022-06-10 09:58:46 -07:00
Benjamin Kramer 6ee5baeb97 [libc][math] Add EXP_MANT_MASK when long double is double or quad 2022-06-10 18:48:31 +02:00
Kirill Okhotnikov b03567fe2b [libc][math] Improved FBits performance and readablity.
Some function added in preparation to fmod commit.

Differential Revision: https://reviews.llvm.org/D127097
2022-06-10 10:21:44 +02:00
Kirill Okhotnikov 76b57ef88c [libc][math] Differential "diff" test for hypot/hypotf functions.
Added test handler in preparation to fmod/fmodf commit.

Differential Revision: https://reviews.llvm.org/D127091
2022-06-10 10:08:47 +02:00
Kirill Okhotnikov 081aba27b1 [libc][math] Separated builtin function in special FPUtils header.
A small refactoring of builtin functions in preparation to adding fmod/fmodf function.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D127088
2022-06-10 03:18:35 +02:00
Michael Jones e1c54d4ddc [libc] move printf_main in to object library
Previously printf_main was a header library, but header library
dependencies don't work properly so it's been moved to an object
library. Additionally, the writers have been marked inline.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D126830
2022-06-09 14:35:18 -07:00
Michael Jones 1be3669dda [libc] add printf base 10 integer conversion
This patch adds support for d, i, and u conversions in printf, as well
as comprehensive unit tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D125929
2022-06-09 10:27:11 -07:00
Siva Chandra Reddy 214be9d1cd [libc] Add compile options to pthread_create target.
The compile options now match that of thrd_create. Two compile options
are of importance:
1. -O3 - This is required so that stack is not used between the clone
   syscall and the start function in the child thread.
2. -fno-omit-frame-pointer - This is required so that we can sniff out
   the thread start args from the child thread's stack memory.

Without these two options, pthread_create will exhibit flaky behavior.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D127381
2022-06-09 17:26:19 +00:00
Michael Jones 0bff6a3e39 [libc] simplify printf converter tests
previously the printf converter tests reused the same string_writer,
which meant that each test depended on the tests before it to succeed.
This makes a new string_writer for each test to simplify and clarify the
tests.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D127341
2022-06-09 10:05:22 -07:00
Alex Brachet 6504b15992 [libc][NFC] Mark some methods constexpr
gcc is complaining that these methods are being called
from a function that is marked constexpr but these
aren't.
2022-06-08 22:41:09 +00:00
Alex Brachet 122da690b3 [libc] Fix build when __FE_DENORM is defined
Differential revision: https://reviews.llvm.org/D127222
2022-06-08 16:21:53 +00:00
Jeff Bailey ef3db4fcab Replace Goals and Why section with Introduction
Rewrite the introduction of the page to state clearly the goals of
LLVM's libc project.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D127174
2022-06-07 06:53:54 +00:00
Siva Chandra 0539a6b420 [libc] Align the new thread stack as required by the target ABI. 2022-06-06 14:45:43 -07:00
Tue Ly 667863d8a8 [libc] Fix cmake compatibility issue with list(POP_FRONT).
list(POP_FRONT) is only added to cmake in 3.15, while our base line
version is 3.13

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D127129
2022-06-06 13:36:03 -04:00
Fangrui Song d86a206f06 Remove unneeded cl::ZeroOrMore for cl::opt/cl::list options 2022-06-05 00:31:44 -07:00
Tue Ly 484319f497 [libc] Make expm1f correctly rounded when the targets have no FMA instructions.
Add another exceptional value and fix the case when |x| is small.

Performance tests with CORE-MATH project scripts:
With FMA instructions on Ryzen 1700:
```
$ ./perf.sh expm1f
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
CORE-MATH reciprocal throughput   : 15.362
System LIBC reciprocal throughput : 53.194
LIBC reciprocal throughput        : 14.595
$ ./perf.sh expm1f --latency
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
CORE-MATH latency   : 57.755
System LIBC latency : 147.020
LIBC latency        : 60.269
```
Without FMA instructions:
```
$ ./perf.sh expm1f
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
CORE-MATH reciprocal throughput   : 15.362
System LIBC reciprocal throughput : 53.300
LIBC reciprocal throughput        : 18.020
$ ./perf.sh expm1f --latency
LIBC-location: /home/lnt/experiment/llvm/llvm-project/build/projects/libc/lib/libllvmlibc.a
CORE-MATH latency   : 57.758
System LIBC latency : 147.025
LIBC latency        : 70.304
```

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D123440
2022-06-03 15:57:48 -04:00
Tue Ly 614567a7bf [libc] Automatically add -mfma flag for architectures supporting FMA.
Detect if the architecture supports FMA instructions and if
the targets depend on fma.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D123615
2022-06-03 01:21:20 -04:00
Siva Chandra Reddy 70c8d12b79 [libc] Add pthread_create and pthread_join functions.
They do not yet support all the feature/attributes in pthread_attr_t.
Future changes will add such support.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D126718
2022-06-02 01:47:24 +00:00
Siva Chandra Reddy ad89cf4e2d [libc] Keep all thread state information separate from the thread structure.
The state is now stored on the thread's stack memory. This enables
implementing pthread API like pthread_detach which takes the pthread_t
structure argument by value.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D126716
2022-06-01 17:36:58 +00:00
Guillaume Chatelet b2a9ea4420 [libc] Apply no-builtin everywhere, remove unnecessary flags
Note, this is a re-submission of D125894 with `features = ["-header_modules"]`
added to the main BUILD.bazel file.

Some functions like `stpncpy` are implemented in terms of `memset` but are not
currently using `-fno-builtin-memset`. This is somewhat hidden by the fact that
we use `-ffreestanding` globally and that `-ffreestanding` implies
`-fno-builtin` for Clang.

This patch also removes `-mllvm -combiner-global-alias-analysis` that is Clang
specific and that does not bring substantial gains on modern processors.

Also we keep `-mllvm --tail-merge-threshold=0` for aarch64 in CMakeLists.txt
but we omit it in the Bazel config. This is because Bazel consumes the source
files directly and so it can use PGO to take optimal decisions locally.

Differential Revision: https://reviews.llvm.org/D126773
2022-06-01 13:34:36 +00:00
Guillaume Chatelet 4cbfd2e7eb [libc][mem*] Address facility + test enum support
This patch is a subpart of D125768 intented to make the review easier.

The `Address` struct represents a pointer but also adds compile time knowledge
like alignment or temporal/non-temporal that helps with downstream instruction
selection.

Differential Revision: https://reviews.llvm.org/D125966
2022-06-01 09:09:43 +00:00
Guillaume Chatelet 299baac64d [libc] Add support for enum in EXPECT_EQ 2022-06-01 08:42:18 +00:00
Tue Ly 800051487f [libc] Implement FLAGS option for generating all combinations for targets.
Add FLAGS option for add_header_library, add_object_library,
add_entrypoint_object, and add_libc_unittest.

In general, a flag is a string provided for supported functions under the
multi-valued option `FLAGS`.  It should be one of the following forms:
  FLAG_NAME
  FLAG_NAME__NO
  FLAG_NAME__ONLY
A target will inherit all the flags of its upstream dependency.

When we create a target `TARGET_NAME` with a flag using (add_header_library,
add_object_library, ...), its behavior will depend on the flag form as follow:
- FLAG_NAME: The following 2 targets will be generated:
    `TARGET_NAME` that has `FLAG_NAME` in its `FLAGS` property.
    `TARGET_NAME.__NO_FLAG_NAME` that depends on `DEP.__NO_FLAG_NAME` if
       `TARGET_NAME` depends on `DEP` and `DEP` has `FLAG_NAME` in its `FLAGS`
       property.
- FLAG_NAME__ONLY: Only generate 1 target `TARGET_NAME` that has `FLAG_NAME`
    in its `FLAGS` property.
- FLAG_NAME__NO: Only generate 1 target `TARGET_NAME.__NO_FLAG_NAME` that
    depends on `DEP.__NO_FLAG_NAME` if `DEP` is in its DEPENDS list and `DEP`
    has `FLAG_NAME` in its `FLAGS` property.

To show all the targets generated, pass SHOW_INTERMEDIATE_OBJECTS=ON to cmake.
To show all the targets' dependency and flags, pass
`SHOW_INTERMEDIATE_OBJECTS=DEPS` to cmake.

To completely disable a flag FLAG_NAME expansion, set the variable
`SKIP_FLAG_EXPANSION_FLAG_NAME=TRUE`.

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D125174
2022-06-01 00:54:07 -04:00
Michael Jones ba7e1cddda [libc] add fprintf and file_writer
This patch adds the file_writer header, which just provides a wrapper
for File->write, as well as fprintf to use it. There are no unit tests
for file_writer since it's too simple to need them, but fprintf does
have a simple test of writing to a file.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D125939
2022-05-31 13:59:19 -07:00
Siva Chandra Reddy 9b8ca3c1f1 [libc] Add global stdout and stderr objects.
They are added as entrypoint object targets. The header-gen
infrastructure has been extended to enable handling standard required
global objects. The libc-api-test has also been extended to verify the
global object declarations.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D126329
2022-05-27 05:43:49 +00:00
Siva Chandra Reddy 2a5d5078d5 [libc] Add the pthread_mutex_t type.
Simple implementations of the functions pthread_mutex_init,
pthread_mutex_destroy, pthread_mutex_lock and pthread_mutex_unlock have
have also been added. Future patches will extend these functions to add
features required by the POSIX specification.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D126235
2022-05-24 22:48:14 +00:00
Alex Brachet 8725dc5e2f [libc][docs] Use same formatting for headers in source_layout
utils looks different from the other directory names
in the docs, see
https://libc.llvm.org/source_layout.html#the-utils-directory

Differential revision: https://reviews.llvm.org/D126211
2022-05-23 21:47:22 +00:00
Guillaume Chatelet 0443bfabe7 Revert "[libc] Apply no-builtin everywhere, remove unnecessary flags"
This reverts commit 94d6dd9057.
2022-05-20 14:37:17 +00:00
Alex Brachet b1183305f8 [libc] Add strlcat
Differential Revision: https://reviews.llvm.org/D125978
2022-05-19 21:48:39 +00:00
Guillaume Chatelet 94d6dd9057 [libc] Apply no-builtin everywhere, remove unnecessary flags
Some functions like `stpncpy` are implemented in terms of `memset` but are not
currently using `-fno-builtin-memset`. This is somewhat hidden by the fact that
we use `-ffreestanding` globally and that `-ffreestanding` implies
`-fno-builtin` for Clang.

This patch also removes `-mllvm -combiner-global-alias-analysis` that is Clang
specific and that does not bring substantial gains on modern processors.

Also we keep `-mllvm --tail-merge-threshold=0` for aarch64 in CMakeLists.txt
but we omit it in the Bazel config. This is because Bazel consumes the source
files directly and so it can use PGO to take optimal decisions locally.

Differential Revision: https://reviews.llvm.org/D125894
2022-05-19 09:08:42 +00:00
Michael Jones 72f6dfb378 [libc][windows] fix strlcpy tests
Generally, size_t is an alias for unsigned long long. In the strlcpy
tests, the return value of strlcpy (a size_t) is compared to an unsigned
long. On Linux unsigned long and unsigned long long are both 64 bits,
but on windows unsigned long is 32 bits. Since the macros require
identical types for both sides, this caused a build failure on windows.
This patch changes the constants to be explicit size_t values.

Differential Revision: https://reviews.llvm.org/D125917
2022-05-18 14:11:53 -07:00
Michael Jones f8ae591fc9 [libc] fix missing semicolon in bsd_ext.td
Fix typo in previous commit

Differential Revision: https://reviews.llvm.org/D125913
2022-05-18 11:22:26 -07:00
Alex Brachet 6adbcd2b10 [libc] Add String to bsd headers 2022-05-18 18:01:02 +00:00
Alex Brachet fc2c8b2371 [libc] Add strlcpy
Differential Revision: https://reviews.llvm.org/D125806
2022-05-18 17:45:05 +00:00
Michael Jones 9f1d905f39 [libc] add snprintf
After adding sprintf, snprintf is simple. The functions are very
similar. The tests only cover the behavior of the max length since the
sprintf tests should cover the other behavior.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D125826
2022-05-17 13:32:59 -07:00
Michael Jones ff6fe39eca [libc] add sprintf
This adds the sprintf entrypoint, as well as unit tests. Currently
sprintf only supports %%, %s, and %c, but the other conversions are on
the way.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D125573
2022-05-17 11:32:20 -07:00
Michael Jones 6a22b185d6 [libc] add printf converter
This adds the main pieces of the last piece of printf, the converter.
This takes the completed format section from the parser and then
converts it to a string for the writer, which is why it was the last
piece to be written. So far it supports chars and strings, but more
pieces are coming. Additionally, it supports replacing all of the
conversion functions with user supplied versions at compile time to
allow for additional functionality.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D125327
2022-05-12 13:10:05 -07:00
Michael Jones 12aae7d9a6 [libc][docs] Add doc for libc stdio functions
This patch adds a document describing the status of the string functions
in LLVM-libc.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D123823
2022-05-12 13:02:23 -07:00
Michael Jones dd7f30464b [libc] fix uint includes and libc bazel
This patch fixes the includes for the new UInt class so that the api
test now passes, additionally it fixes the bazel files to account for
the new dependencies.

Differential Revision: https://reviews.llvm.org/D125490
2022-05-12 11:40:52 -07:00
Michael Jones 1170951c73 [libc] add uint128 implementation
Some platforms don't support proper 128 bit integers, but some
algorithms use them, such as any that use long doubles. This patch
modifies the existing UInt class to support the necessary operators.
This does not put this new class into use, that will be in followup
patches.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D124959
2022-05-12 11:16:53 -07:00
Tue Ly 6d92f4022d [libc][Obvious] Fix cmake usage of list PREPEND (unavailable pre-3.15). 2022-05-08 13:58:05 -04:00
Tue Ly 13f358376a [libc] Add LINK_LIBRARIES option to add_fp_unittest and add_libc_unittest.
This is needed to prepare for adding FLAGS option.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D125055
2022-05-08 17:33:45 +00:00
Michael Jones 945fa672c6 [libc][NFC] add index mode to printf parser
This patch is a followup to the previous patch which implemented the
main printf parsing logic as well as sequential mode. This patch adds
index mode.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D123424
2022-05-06 12:06:08 -07:00