Commit Graph

1250 Commits

Author SHA1 Message Date
Guillaume Chatelet cb888971d2 [libc] Improve bcmp performance for aarch64 2022-11-24 19:24:44 +00:00
Tue Ly 0d41b32b74 [libc][math] Remove UInt<128>::operation* specialization.
Remove UInt<128>::operation* specialization since the generic implementation
works well enough:
https://quick-bench.com/q/Bz_J5HgAsz5m5V5Ze-X1fI77hPo

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D138239
2022-11-19 16:16:24 -05:00
Tue Ly e205fc1836 [libc] Add sub_with_borrow to builtin_wrapper.h
Add sub_with_borrow to builtin_wrapper.h to be used in UInt.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138182
2022-11-19 16:12:26 -05:00
Michael Jones af029d383a [libc][reland] Fix builtin definition for memory functions
The memory functions are highly performance sensitive and use builtins
where possible, but also need to define those functions names when they
don't exist to avoid compilation errors. Previously all those
redefinitions were behind the SSE2 flag for x86, which caused errors on
CPUs that supported SSE2 but not AVX512. This patch splits the various
CPU extensions out to avoid errors on such CPUs.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D138163
2022-11-18 14:25:23 -08:00
Michael Jones 36991d8342 [libc] add scanf entrypoints
This patch adds scanf, sscanf, and fscanf entrypoints. It also adds unit
tests for sscanf and a basic test to fscanf. The scanf function is
basically impossible to test in an automated fashion due to it recieving
user input.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D138076
2022-11-17 15:37:52 -08:00
Fangrui Song 310b619e95 Revert D137868 "[libc] Fix builtin definition for memory functions"
This reverts commit da5d00ad0c.

This caused -Wmacro-redefined
```
.../libc/src/string/memory_utils/op_x86.h:31:9: error: '_mm512_cmpneq_epi8_mask' macro redefined [-Werror,-Wmacro-redefined]
        ^
.../lib/clang/google3-trunk/include/avx512bwintrin.h:294:9: note: previous definition is here
        ^
```
2022-11-16 22:47:53 +00:00
Michael Jones da5d00ad0c [libc] Fix builtin definition for memory functions
The memory functions are highly performance sensitive and use builtins
where possible, but also need to define those functions names when they
don't exist to avoid compilation errors. Previously all those
redefinitions were behind the SSE2 flag for x86, which caused errors on
CPUs that supported SSE2 but not AVX512. This patch splits the various
CPU extensions out to avoid errors on such CPUs.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D137868
2022-11-16 13:03:39 -08:00
Joseph Huber dabb7514f5 [libc] Fix assert.h and ctype.h not being built
The `assert.h` and `ctype.h` headers are never built despite their
entrypoints being present in the generated library. This patch adds a
dependency on these headers so that they will be built properly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138142
2022-11-16 12:00:41 -06:00
Guillaume Chatelet 2a6d4c5d8a [libc][benchmark] Fix wrong BatchSize leading to data not fitting in L1.
Differential Revision: https://reviews.llvm.org/D138111
2022-11-16 12:55:37 +00:00
Guillaume Chatelet a5f4f12b5e [libc][NFC] move memmove implementation
Moving memmove implementation to its own file for symmetry with other mem functions.

Differential Revision: https://reviews.llvm.org/D136687
2022-11-16 10:36:14 +00:00
Guillaume Chatelet fe8d211f1a [libc] deprecate LLVM_LIBC_MEMCPY_X86_USE_ONLY_REPMOVSB
Differential Revision: https://reviews.llvm.org/D136693
2022-11-16 10:33:20 +00:00
Guillaume Chatelet 534f4bca58 [libc] remove mem functions dead code
Let's wait a bit for https://reviews.llvm.org/D136595 to settle before
cleaning up. Just in case we would need to revert.

Differential Revision: https://reviews.llvm.org/D136673
2022-11-16 10:29:29 +00:00
Raman Tenneti 191ab4f46e [libc] [Obvious] Cleanup.
+ Avoid the `clockid` var and just passed `long(CLOCK_REALTIME)`
  to the syscall.

Reviewed By: rtenneti

Differential Revision: https://reviews.llvm.org/D138070
2022-11-15 15:30:15 -08:00
Michael Jones c46a465eb9 [libc] disable flakey tests
The mprotect test has been failing and causing significant buildbot
noise. Given that this is a simple functions it's more important that
the buildbot errors be useful than having more thorough tests for this
function.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D138061
2022-11-15 15:13:37 -08:00
Raman Tenneti dbd31935ed [libc] cleanup changes to gettimeofday.
+ Deleted duplicate definitions of StructTimeVal and StructTimeValPtr.
+ Caled syscall  clock_gettime to get timespec data.
+ Added tests to test for sleeping 200 and 1000 microseconds.
+ Fixed comments from https://reviews.llvm.org/D137881

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138064
2022-11-15 15:06:35 -08:00
Michael Jones 7ffa52dfd1 [libc] re-enable assert
The assert functions were disabled while the signal functions were being
fixed. This patch re-enables them.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138056
2022-11-15 13:39:34 -08:00
Fangrui Song 7dde2a59e6 [libc] Include stddef.h after D137871
C standard does not require stdint.h to define size_t.
2022-11-15 20:22:33 +00:00
Tue Ly bc10a41080 [libc][math] Improve the performance and error printing of UInt.
Use add_with_carry builtin to improve the performance of
addition and multiplication of UInt class.  For 128-bit, it is as
fast as using __uint128_t.

Microbenchmark for addition:
https://quick-bench.com/q/-5a6xM4T8rIXBhqMTtLE-DD2h8w

Microbenchmark for multiplication:
https://quick-bench.com/q/P2muLAzJ_W-VqWCuxEJ0CU0bLDg

Microbenchmark for shift right:
https://quick-bench.com/q/N-jkKXaVsGQ4AAv3k8VpsVkua5Y

Microbenchmark for shift left:
https://quick-bench.com/q/5-RzwF8UdslC-zuhNajXtXdzLRM

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137871
2022-11-15 14:17:59 -05:00
Joseph Huber 93d1a7bfc1 [libc] Fix tablegen when using a runtimes build
When using `LLVM_ENABLE_RUNTIMES=libc` we need to perform a few extra
steps to include LLVM utilities similar to if we were performing a
standalone build. Libc depends on the tablegen utilities and the LLVM
libraries when performing a full build. When using an
`LLVM_ENABLE_PROJECTS=libc` build these are included as a part of the
greater LLVM build, but here we need to perform it maunally. This patch
should allow using `LLVM_LIBC_FULL_BUILD=ON` when building with
runtimes.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D138040
2022-11-15 12:09:52 -06:00
Raman Tenneti 35155ee7d3 Removed tabs. 2022-11-11 18:11:06 -08:00
Raman Tenneti 78f172e45a [libc] Implement gettimeofday
Implement gettimeofday per
.../onlinepubs/9699919799/functions/gettimeofday.html.
This call clock_gettime to implement gettimeofday function.

Tested:
Limited unit test: This makes a call and checks that no error was
returned. Used nanosleep for 100 microseconds and verfified it
returns a value that elapses more than 100 microseconds and less
than 300 microseconds.

Co-authored-by: Jeff Bailey <jeffbailey@google.com>

Differential Revision: https://reviews.llvm.org/D137881
2022-11-11 18:02:33 -08:00
Michael Jones 4d25761b07 [libc] move fork into threads folder
Fork, as a thread function, should go in the threads folder.
Additionally, it depends on the thread mutex, and it was causing build
issues for targets where we don't support threads.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D137867
2022-11-11 16:01:39 -08:00
Michael Jones 430ca14af8 [libc][obvious] fix tests using wrong size for string
In the code
const char *str = "abc"
if you do sizeof(str) you get the size of the pointer, not the string.
This patch fixes that mistake.

Differential Revision: https://reviews.llvm.org/D137586
2022-11-07 14:04:39 -08:00
Michael Jones 1c40d5ec7d [libc] add scanf string converters
This patch adds the basic conversion facilities to scanf as well as unit
tests for them. It also adds scanf_main which will be used for the
eventual scanf entrypoints.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137376
2022-11-07 13:49:01 -08:00
Michael Jones 0c8db885f6 [libc] add scanf reader
This is the interface that will be used to read from a file or string in
scanf. This patch also adds the string and file implementations of the
reader, although the file reader is not yet complete since ungetc has
not yet been implemented.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137117
2022-11-07 13:48:58 -08:00
Siva Chandra Reddy 6a6101958a [libc] Add implementation of getc, getc_unlocked and fgetc_unlocked.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137507
2022-11-07 21:47:29 +00:00
Siva Chandra Reddy 43e52ad553 [libc] Add implementations of ftell.
Reviewed By: michaelrj, lntue

Differential Revision: https://reviews.llvm.org/D137395
2022-11-07 21:38:53 +00:00
Michael Jones 28e312cbf0 [libc][obvious] fix printf failing to stop on %\0
Previously, the printf parser would treat "%\0" as a conversion with the
name "\0", and advance past the null byte causing a buffer overflow.
This patch corrects that in both printf and scanf.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D137367
2022-11-07 10:44:30 -08:00
Siva Chandra Reddy b12c677033 [libc][docs] Add a build and test document. 2022-11-05 01:06:01 -07:00
Siva Chandra Reddy 64821f178e [libc][docs] Fix typos. 2022-11-05 00:21:09 -07:00
Siva Chandra Reddy 1bb6f1bb8f [libc][docs] Move links to internal developer guides out of the main side bar.
The links are all now from a "Developer Guides" page which is linked from the
main side bar.
2022-11-05 00:04:50 -07:00
Tue Ly 6c6dff7e2c [libc] Add add_with_carry to builtin wrapper.
Add add_with_carry to builtin wrapper to be used by UInt class.

Reviewed By: orex

Differential Revision: https://reviews.llvm.org/D137453
2022-11-04 23:20:37 -04:00
Siva Chandra Reddy 086757a47a [libc][NFC] Remove "$>" from another command block on examples/README.md. 2022-11-04 08:37:05 +00:00
Siva Chandra Reddy 3d2165f738 [libc][NFC] Remove "$>" from recipe commands blocks in examples/README.md.
The blocks come with a copy button so removing the "$>" makes it easy to
copy paste the commands.
2022-11-04 08:35:39 +00:00
Siva Chandra Reddy 9145bf13b7 [libc] Add example programs and their CMake build and instructions.
These examples are serve as an examples for people wanting to start
using the libc.

Reviewed By: michaelrj, jeffbailey

Differential Revision: https://reviews.llvm.org/D137090
2022-11-04 08:31:06 +00:00
Siva Chandra Reddy 4eea884959 [libc] Add implementation of setbuf and setvbuf.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137356
2022-11-04 07:15:13 +00:00
Siva Chandra Reddy c9da0352a3 [libc] Convert the api-test in to an integration test.
It used to pass but showed an annoying message about _start not being
available. That will not happen anymore.
2022-11-04 07:09:18 +00:00
Siva Chandra Reddy 1ceafe5e0f [libc] Add implementation of ungetc.
A bug in the file read logic has also been fixed along the way. Parts
of the ungetc tests will fail without that bug fixed.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137286
2022-11-02 22:45:57 +00:00
Siva Chandra Reddy 136b927c9e [libc] Add a doc about the libc fullbuild mode.
Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D137061
2022-11-02 08:18:48 -07:00
Siva Chandra Reddy f7fd4f319a [libc] Add links to libc Discourse and to the libc Discord channel.
Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D137229
2022-11-02 08:03:53 -07:00
Siva Chandra Reddy a2a87ee7e9 [libc] Add a contributing guide to the docs.
Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D136961
2022-11-02 08:02:11 -07:00
Siva Chandra Reddy 9dd7388668 [libc] Add a porting guide to the docs.
Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D136960
2022-11-02 07:59:07 -07:00
Guillaume Chatelet 67437dd014 [reland][libc] Switch to new implementation of mem* functions
The new framework makes it explicit which processor feature is being
used and allows for easier per platform customization:
 - ARM cpu now uses trivial implementations to reduce code size.
 - Memcmp, Bcmp and Memmove have been optimized for x86
 - Bcmp has been optimized for aarch64.

This is a reland of https://reviews.llvm.org/D135134 (b3f1d58, 0284148813)

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D136595
2022-11-02 09:09:46 +00:00
Guillaume Chatelet 3635195e0d [libc] Improve testing of mem functions
This patch extracts the testing logic from `op_tests.cpp` into
`memory_check_utils.h` so we can reuse it for mem* function integration
tests.

This makes testing consistent and thorough.
For instance this catches a bug that got unnoticed during submission of
D136595 and D135134. Integration test for memcmp was only testing a
single size.

This also leverages ASAN to make sure that data is not read / written
outside permitted boundaries

Differential Revision: https://reviews.llvm.org/D136865
2022-11-02 08:55:46 +00:00
Siva Chandra Reddy 15ae08c1a1 [libc] Add definitions of a few missing macros and types. 2022-11-02 07:17:33 +00:00
Siva Chandra Reddy f357a41265 [libc][Obvious] Fix a typo in a CMake file. 2022-11-01 23:18:34 +00:00
Siva Chandra Reddy 3756227ef6 [libc] Skip setjmp tests under sanitizers. 2022-11-01 23:13:57 +00:00
Siva Chandra Reddy 3b82b4fbd5 [libc] Add x86_64 implementation of setjmp and longjmp.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137147
2022-11-01 22:58:35 +00:00
Tue Ly 45233cc1ca [libc][math] Add place-holder implementation for pow function.
Add place-holder implementation for pow function to unblock libc demo
examples.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137109
2022-10-31 17:23:33 -04:00
Tue Ly 97b4cc83e1 [libc][math] Add place-holder implementation for asin to unblock demo examples.
Add a place-holder implementation for asin to unblock libc demo
examples.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137105
2022-10-31 17:22:12 -04:00