Commit Graph

1266 Commits

Author SHA1 Message Date
Tue Ly 28f13353b6 [libc][Obvious] Update error bounds for uint_test.QuickMulHiTests. 2022-12-02 18:13:35 -05:00
Tue Ly b80f535879 [libc][math] Implement full multiplication and quick_mul_hi for UInt class.
Implement full multiplication `UInt<A> * UInt<B> -> UInt<A + B>` and
`quick_mul_hi` that returns the higher half of the product `UInt<A> * UInt<A>`.
These 2 functions will be used for dyadic floating point class.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138541
2022-12-02 17:53:38 -05:00
Joseph Huber c648112592 [libc] Skip tests that depend on undefined targets
When we search through the dependencies for a test we somtimes query
`get_target_property` on a target that has not been defined. This is the
case for targets that have minimal entrypoints defined such as the WIP
GPU target. This patch changes all tests whose dependencies are
undefined to be skipped entirely.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D139216
2022-12-02 16:39:45 -06:00
Siva Chandra Reddy 5d6ed9ff0c [libc][Obvious] Fix the build after e748db0f7f.
That changed some parts of the Support API to use std::optional instead
of llvm::Optional. This patch updates the libc uses to the new API.
2022-12-01 23:39:09 +00:00
Guillaume Chatelet bcf394c4d1 [libc][benchmark] more precise estimate of throughput
This patch increases precision by performing the integer divivion after the
multiplication.
2022-12-01 10:21:13 +00:00
Guillaume Chatelet 436c8f4420 [reland][libc] Add bcopy
Differential Revision: https://reviews.llvm.org/D138994
2022-12-01 10:07:04 +00:00
Guillaume Chatelet c5fe7eb216 Revert D138994 "[libc] Add bcopy"
Broke build bot

This reverts commit 186a15f7a9.
2022-12-01 09:55:36 +00:00
Guillaume Chatelet 186a15f7a9 [libc] Add bcopy
Differential Revision: https://reviews.llvm.org/D138994
2022-12-01 09:52:10 +00:00
Michael Jones a3b745818d [libc] add unsafe mode to strlen
The only safe way to implement strlen involves reading the string one
char at a time. It is faster to read in larger blocks, but this leads to
reading beyond the string boundary, which is undefined behavior. This
patch adds an implementation and flag to use this fast but unsafe
version of strlen.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D129808
2022-11-30 16:48:35 -08:00
Tue Ly 926dd32aab [libc][math] Improve the performance of subtraction for UInt<>.
Use built-in sub_with_borrow to improve the performance of subtraction
for UInt<>.

Microbenchmark: https://quick-bench.com/q/UEg6Z6VK4PP2yLBtTi9C0cL9xpY

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138506
2022-11-30 10:51:02 -05:00
Joseph Huber db923a610b [libc][docs] Fix code block not being emitted 2022-11-30 09:33:40 -06:00
Joseph Huber 21d9f725df [libc] Fix test not including 'free'
Summary:
A previous change removed a transient inclusion of `stdlib.h` from the
`string_utils.h` file which this test depended on. Include it directly
here.
2022-11-29 15:03:46 -06:00
Joseph Huber 194788b2fd [libc][docs] Add documentation for the new GPU mode
This patch introduces documentation for the new GPU mode added in
D138608. The documentation includes instructions for building and using
the library, along with a description of the supported functions and
headers.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D138856
2022-11-29 14:51:55 -06:00
Joseph Huber 55151e138d [libc] Add initial support for a libc implementation for the GPU
This patch contains the initial support for building LLVM's libc as a
target for the GPU. Currently this only supports a handful of very basic
functions that can be implemented without an operating system. The GPU
code is build using the existing OpenMP toolchain. This allows us to
minimally change the existing codebase and get a functioning static
library. This patch allows users to create a static library called
`libcgpu.a` that contains fat binaries containing device IR.

Current limitations are the lack of test support and the fact that only
one target OS can be built at a time. That is, the user cannot get a
`libc` for Linux and one for the GPU simultaneously.

This introduces two new CMake variables to control the behavior
`LLVM_LIBC_TARET_OS` is exported so the user can now specify it to equal
`"gpu"`. `LLVM_LIBC_GPU_ARCHITECTURES` is also used to configure how
many targets to build for at once.

Depends on D138607

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138608
2022-11-29 14:51:54 -06:00
Joseph Huber d85699eb7d [libc] Move strdup implementation to a new header
The `strdup` family of functions rely on `malloc` to be implemented.
Its presence in the `string_utils.h` header meant that compiling many of
the string functions relied on `malloc` being implementated as well.
This patch simply moves the implementation into a new file to avoid
including `stdlib.h` from the other string functions. This was a barrier
for compiling string functions for the GPU where there is no malloc
currently.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138607
2022-11-29 14:51:52 -06:00
Petr Hosek bec8a372fc [CMake] Use LLVM_TARGET_TRIPLE in runtimes
This variable is derived from LLVM_DEFAULT_TARGET_TRIPLE by default,
but using a separate variable allows additional normalization to be
performed if needed.

Differential Revision: https://reviews.llvm.org/D137451
2022-11-29 04:08:24 +00:00
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