367 Commits

Author SHA1 Message Date
lntue
cadfd0b4ca
[libc] Also add -Wno-pedantic when building with clang. (#112286) 2024-10-14 19:55:33 -04:00
lntue
9b8dbe2c8d
[libc][complex] Fix compiler warnings for usage of complex.h header. (#112282) 2024-10-14 19:33:22 -04:00
Shourya Goel
8906bff5ab
[libc][complex] Silence pedantic warning (#112239)
Fix buildbot errors due to #111659
2024-10-14 14:11:01 -04:00
Shourya Goel
f4ba6a654d
[libc][complex] Set up headers and add documentation for complex.h. (#111659)
Refer: 7.3.1 from [ISO
SPEC](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)

I have added complex variants of F16 and F128 in libc doc but have
omitted support for them since we will have to first investigate how
their support matrix for clang and gcc looks like, and then add header
guards for them accordingly. Planning to add them in follow up PRs once
this gets landed.
2024-10-14 13:35:04 -04:00
Shourya Goel
4e33afee5a
[libc][math] Implement issignaling and iscanonical macro. (#111403)
#109201
2024-10-09 10:25:49 -07:00
OverMighty
127349fcba
[libc][math] Add floating-point cast independent of compiler runtime (#105152)
Fixes build and tests with compiler-rt on x86.
2024-09-23 19:35:39 +02:00
Joseph Huber
ba8c96593c
[Clang] Do not implicitly link C libraries for the GPU targets (#109052)
Summary:
I initially thought that it would be convenient to automatically link
these libraries like they are for standard C/C++ targets. However, this
created issues when trying to use C++ as a GPU target. This patch moves
the logic to now implicitly pass it as part of the offloading toolchain
instead, if found. This means that the user needs to set the target
toolchain for the link job for automatic detection, but can still be
done manually via `-Xoffload-linker -lc`.
2024-09-18 06:44:07 -07:00
Jeff Bailey
50985d23e5
[libc][nfc] Fix typo in header generation message. (#108813)
Fix a typo in the header generation message.

Before:
Generating header from
/home/vscode/llvm-project/llvm/../libc/newhdrgen/yaml/ctype.yaml and
/home/vscode/llvm-project/libc/include/ctype.h.def

After:
Generating header ctype.h from
/home/vscode/llvm-project/llvm/../libc/newhdrgen/yaml/ctype.yaml and
/home/vscode/llvm-project/libc/include/ctype.h.def
2024-09-16 16:53:43 +01:00
Sirui Mu
ded080152a
[libc] Add osutils for Windows and make libc and its tests build on Windows target (#104676)
This PR first adds osutils for Windows, and changes some libc code to
make libc and its tests build on the Windows target. It then temporarily
disables some libc tests that are currently problematic on Windows.

Specifically, the changes besides the addition of osutils include:

- Macro `LIBC_TYPES_HAS_FLOAT16` is disabled on Windows. `clang-cl`
generates calls to functions in `compiler-rt` to handle float16
arithmetic and these functions are currently not linked in on Windows.
- Macro `LIBC_TYPES_HAS_INT128` is disabled on Windows.
- The invocation to `::aligned_malloc` is changed to an invocation to
`::_aligned_malloc`.
- The following unit tests are temporarily disabled because they
currently fail on Windows:
  - `test.src.__support.big_int_test`
  - `test.src.__support.arg_list_test`
  - `test.src.fenv.getenv_and_setenv_test`
- Tests involving `__m128i`, `__m256i`, and `__m512i` in
`test.src.string.memory_utils.op_tests.cpp`
- `test_range_errors` in `libc/test/src/math/smoke/AddTest.h` and
`libc/test/src/math/smoke/SubTest.h`
2024-09-11 23:41:32 -04:00
lntue
1896ee3889
[libc] Fix undefined behavior for nan functions. (#106468)
Currently the nan* functions use nullptr dereferencing to crash with
SIGSEGV if the input is nullptr. Both `nan(nullptr)` and `nullptr`
dereferencing are undefined behaviors according to the C standard.
Employing `nullptr` dereference in the `nan` function implementation is
ok if users only linked against the pre-built library, but it might be
completely removed by the compilers' optimizations if it is built from
source together with the users' code.

See for instance:  https://godbolt.org/z/fd8KcM9bx

This PR uses volatile load to prevent the undefined behavior if libc is
built without sanitizers, and leave the current undefined behavior if
libc is built with sanitizers, so that the undefined behavior can be
caught for users' codes.
2024-09-11 14:13:31 -04:00
lntue
80cf21dad1
[libc] Fix unit test compile flags propagation. (#106128)
With this change, I was able to build and test for aarch64 & riscv64 on
x86-64 host as follow:

Pre-requisite:
- cross build toolchain for aarch64
```
$ sudo apt install binutils-aarch64-linux-gnu gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
```
- cross build toolchain for riscv64
```
$ sudo apt install binutils-riscv64-linux-gnu gcc-riscv64-linux-gnu g++-riscv64-linux-gnu
```
- qemu user:
```
$ sudo apt install qemu qemu-user qemu-user-static
```

CMake invocation:
```
$ cmake ../runtimes -GNinja -DLLVM_ENABLE_RUNTIMES=libc -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBC_TARGET_TRIPLE=<aarch64-linux-gnu/riscv64-linux-gnu> -DCMAKE_BUILD_TYPE=Release -DLIBC_TEST_COMPILE_OPTIONS_DEFAULT="-static"
$ ninja libc
$ ninja check-libc
```
2024-09-06 11:56:07 -04:00
Joseph Huber
8fd9ec5817 [libc] Fix incorrect check for NVPTX backend 2024-08-28 07:04:15 -05:00
Simon Tatham
722c066c59
[libc] Make use of 32-bit time_t a config option (#102012)
The 32-bit Arm builds of libc define time_t to be `__INTPTR_TYPE__`,
i.e. a 32-bit integer. This is commented in the commit introducing it
(75398f28ebdb600) as being for compatibility with glibc. But in the near
future not even every AArch32 build of glibc will have a 32-bit time_t:
Debian is planning that their next release (trixie) will have switched
to 64-bit. And non-Linux builds of this libc (e.g. baremetal) have no
reason to need glibc compatibility in the first place – and every reason
_not_ to want to start using a 32-bit time_t in 2024 or later.

So I've replaced the `#ifdef` in `llvm-libc-types/time_t.h` with two
versions of the header file, chosen in `CMakeLists.txt` via a new
configuration option. This involved adding an extra parameter to the
cmake `add_header` function to specify different names for the header
file in the source and destination directories.
2024-08-08 16:36:05 +01:00
Sirui Mu
2f28378317
[libc] Fix builds on Windows (#102162)
This PR changes several places in the CMake scripts to make libc build
on Windows. It adds the `errno` entrypoint to the Windows target.

A mistake in the overlay build doc is also fixed.

Tests still cannot be built on Windows because of the lack of osutils.
2024-08-06 08:38:05 -07:00
Schrodinger ZHU Yifan
8252d4d468
[libc] enable most of the entrypoints on aarch64 (#101797)
This is a non-feature change that enables most of the entrypoints for
aarch64 based runtime builds. It fixes an additional problem that some
compiler-rt targets are not defined at the time of dependency checking
thus leading to false-negatives.
2024-08-03 12:22:38 -07:00
Joseph Huber
2bf58f5d27
[Clang] Suppress missing architecture error when doing LTO (#100652)
Summary:
The `nvlink-wrapper` can do LTO now, which means we can still create
some LLVM-IR without needing an architecture. In the case that we try to
invoke `nvlink` internally, that will still fail. This patch simply
defers the error until later so we can use `--lto-emit-llvm` to get the
IR without specifying an architecture.
2024-07-31 14:41:55 -05:00
Joseph Huber
79afb94da1 [libc] Make NVPTX benchmarks use LTO for linking
Summary:
Now that we can do LTO, we can make the benchmarks more accurate by
allowing optimization + inlining of the implementation.
2024-07-27 06:53:12 -05:00
Joseph Huber
f8cd4c505f
[libc] Fix building bitcode library for GPU (#100491)
Summary:
The GPU build provides a bitcode version of the library to mimic how
NVIDIA and AMD provide their libraries. Previously we had the fatbinary
archive that created the necessary dependency for this to actually
build. Since it was removed we no longer had anything triggering this to
build.

I have no idea if there's a better way to force a custom command to
actually be run in the same space as libraries, but the only thing I
could come up with was a dummy target.
2024-07-26 13:17:17 -05:00
OverMighty
81ce796095
[libc][math][c23] Enable C23 _Float16 math functions on GPUs (#99248) 2024-07-25 21:09:49 +02:00
Joseph Huber
8e43acbfed
[libc] Only add '-fno-builtin-*' on the entrypoints that use them (#100481)
Summary:
The GPU build needs to be able to inline stuff in LTO. Builtin
transformations cause problems on the functions that the optimizer does
heavy libcall recognition on. Previously we moved to using
`-fno-builtin-*` to allow us to only disable the problematic ones.
However, this still didn't allow inlining because each function had the
attribute that told the inliner not to inlining a nobuiltin function
into a non-nobuiltin function

This patch fixes that by only applying these attributes to the
entrypoints that define them. That is enough to prevent recursive calls
within the definitoins themselves.
2024-07-24 21:06:19 -05:00
Joseph Huber
8d8fa01a66 Reapply "[libc] Remove 'packaged' GPU build support (#100208)"
This reverts commit 550b83d658755664a7f0f93b36242e885743a91b.
2024-07-24 10:24:53 -05:00
Joseph Huber
550b83d658 Revert "[libc] Remove 'packaged' GPU build support (#100208)"
Summary:
I forgot that the OpenMP tests still look for this, reverting for now
until I can make a fix.

This reverts commit c1c6ed83e9ac13c511961e5f5791034a63168e7e.
2024-07-24 07:51:47 -05:00
Joseph Huber
c1c6ed83e9
[libc] Remove 'packaged' GPU build support (#100208)
Summary:
Previously, the GPU built the `libc` in a fat binary version that was
used to pass this to the link job in offloading languages like CUDA or
OpenMP. This was mostly required because NVIDIA couldn't consume the
standard static library version. Recent patches have now created the
`clang-nvlink-wrapper` which lets us do that. Now, the C library is just
included implicitly by the toolchain (or passed with -Xoffload-linker
-lc).

This code can be fully removed, which will heavily simplify the build
(and removed some bugs and garbage files I've encoutnered).
2024-07-24 07:22:49 -05:00
Joseph Huber
0420d2f97e [libc] Fix leftover debug commandline argument
Summary:
Fixes https://github.com/llvm/llvm-project/issues/100289
2024-07-23 21:35:42 -05:00
OverMighty
e7f8d4be5a
[libc][math] Optimize maximum and minimum functions using builtins when available (#100002) 2024-07-23 23:59:55 +02:00
Joseph Huber
14e20eebd1
[libc] Fix missing default value for errno config (#100175)
Summary:
The configs all need default values which targets then override. This
one was an empty string which made the logic report an error. The only
reason it wasn't a build failure was because of a stray `:`.
2024-07-23 15:05:40 -05:00
Joseph Huber
e7a2405383
[libc] Remove workarounds for lack of functional NVPTX linker (#96972)
Summary:
Currently we have several hacks to work around the fact that the NVPTX
linker, 'nvlink', does not support static libraries or LTO linking.
The patch in https://github.com/llvm/llvm-project/pull/96561 introduces
a wrapper in the toolchain that allows us to use a standard `ld.lld`
like interface. This means all the divergence with this target can be
removed.

Depends on https://github.com/llvm/llvm-project/pull/96561
2024-07-22 22:16:50 -05:00
OverMighty
70843bf658
[libc][math] Optimize copysign{,f,f16} and fabs{,f,f16} with builtins when available (#99037) 2024-07-22 18:37:44 +02:00
Mikhail R. Gadelha
ac11430983
[libc] Fix missing sysroot path for kernel headers when crosscompiling (#99588)
When crosscompiling, we need to search for the linux kernel headers in the sysroot but since #97486 the linux kernel headers were always searched in /usr/include.

This patch fixes this behaviour by prepending a '=' to where we search for the kernel headers. As per the gcc/clang's documentation a '=' before the path is replaced by the sysroot.

This patch also includes a fix for rv32, that fails to compile due to a missing definition of CLOCK_REALTIME after this change.
2024-07-19 12:20:16 -03:00
Michael Jones
adacb5010f
[libc] Restore DECLS_FILE_PATH property (#99583)
The DECLS_FILE_PATH property is supposed to be set on the targets for
the generated headers for the GPU build installation. It got missed when
creating the cmake rule for new headergen.
2024-07-18 16:00:29 -07:00
aaryanshukla
b1fd6f0996
[libc] newheadergen: cmake config newhdrgen (#99543)
- revert to revert for patch
https://github.com/llvm/llvm-project/pull/98828
- revert to revert https://github.com/llvm/llvm-project/pull/99410
- revert to revert https://github.com/llvm/llvm-project/pull/99413
2024-07-18 13:16:00 -07:00
aaryanshukla
58d4ca06bd
Revert "[libc] newheadergen: configured cmake" (#99414)
Reverts llvm/llvm-project#98828
2024-07-17 17:17:05 -07:00
aaryanshukla
83fbd79319
[libc] newheadergen: configured cmake (#98828)
- all headers in the build system are generated by newheadergen
- tested on gpu-build

---------

Co-authored-by: Rose Zhang <rosezhang@google.com>
2024-07-17 16:23:15 -07:00
jameshu15869
1ecffdaf27
[libc] Add Kernel Resource Usage to nvptx-loader (#97503)
This PR allows `nvptx-loader` to read the resource usage of `_start`,
`_begin`, and `_end` when executing CUDA binaries.

Example output:
```
$ nvptx-loader --print-resource-usage libc/benchmarks/gpu/src/ctype/libc.benchmarks.gpu.src.ctype.isalnum_benchmark.__build__
[ RUN      ] LlvmLibcIsAlNumGpuBenchmark.IsAlnumWrapper
[       OK ] LlvmLibcIsAlNumGpuBenchmark.IsAlnumWrapper: 93 cycles, 76 min, 470 max, 23 iterations, 78000 ns, 80 stddev
_begin registers: 25
_start registers: 80
_end registers: 62
  ```

---------

Co-authored-by: Joseph Huber <huberjn@outlook.com>
2024-07-17 16:07:12 -05:00
Mikhail R. Gadelha
e5ccc7136d
[libc] Add missing -latomic for rv32 (#99337)
On rv32, libatomic is needed to build libc when mpfr and gmp are enabled.
2024-07-17 13:41:12 -03:00
lntue
a6d2da8b9d
[libc][stdlib] Implement heap sort. (#98582) 2024-07-16 08:13:25 -04:00
OverMighty
4531f82c1a
[libc][math] Optimize nearest integer functions using builtins when available (#98376) 2024-07-15 21:49:47 +02:00
Petr Hosek
5ff3ff33ff
[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)
This is a part of #97655.
2024-07-12 09:28:41 -07:00
Mehdi Amini
ce9035f5bd
Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration" (#98593)
Reverts llvm/llvm-project#98075

bots are broken
2024-07-12 09:12:13 +02:00
Petr Hosek
3f30effe1b
[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)
This is a part of #97655.
2024-07-11 12:35:22 -07:00
Joseph Huber
221d5c570c
[libc] Disable -ffreestanding and -fno-builtin on the GPU build (#97636)
Summary:
This patch removed the `-ffreestanding` and `-fno-builtin` flags from
the publically installed version of the GPU library. The presense of
`-fno-builtin` caused issues that prevented all inlining in the GPU C
library, see
https://discourse.llvm.org/t/rfc-libc-ffreestanding-fno-builtin/75883.
Previously, I attempted to fix this by loosening the restriction that
`"no-builtins"` functions cannot be inlined into functions without that
attribute. However, that opened up a lot of extra issues that stalled
that approach.

This patch instead removes that and instead passes `-fno-builtin-<xyz>`
for the few calls that are known to be problematic. I believe this works
in general as the GPU backends do not emit any libcalls and the
implementations of most of these simply reduce to builtins right now.
This is a very useful patch as we can now actually inline calls.
2024-07-09 07:51:51 -05:00
Joseph Huber
12e47aabd4
[libc] Add config option for fast math optimizations (#98029)
Summary:
This patch adds `LIBC_COPT_MATH_OPTIMIZATIONS` that allows users to
configure the
different math optimizations.
2024-07-08 11:01:03 -05:00
Petr Hosek
a1c4926dd0
[libc] Include Linux kernel headers in the full build (#97486)
When doing a full build for Linux, as of #97461 we no longer include
system headers, but we need to include Linux kernel headers.
2024-07-02 16:55:15 -07:00
Petr Hosek
3584a82333
[libc] Only use COMPILER_RESOURCE_DIR if available (#97465)
This avoids build error when COMPILER_RESOURCE_DIR is unset.
2024-07-02 12:48:48 -07:00
Petr Hosek
b8bbc57b68
[libc] Use -nostdlibinc in the full build mode (#97461)
This avoids accidentally including system headers.
2024-07-02 12:34:06 -07:00
jameshu15869
02b57dedb7
[libc] NVPTX Profiling (#92009)
PR for adding microbenchmarking infrastructure for NVPTX. `nvlink`
cannot perform LTO, so we cannot inline `libc` functions and this
function call overhead is not adjusted for during microbenchmarking.
2024-06-26 16:38:39 -05:00
Joseph Huber
6c9916d0d8
[libc] Configure CMAKE_REQUIRED_FLAGS so the GPU can use flag checks (#95424)
Summary:
This patch adds `CMAKE_REQUIRED_FLAGS` for the GPU build so checks like
`check_cxx_compiler_flags` work as expected. This is required because we
need to hack around the potential lack of `nvlink` and `ptxas` for NVPTX
targets and the fact that the AMDGPU target needs `-nogpulib` to avoid
errors on lack of ROCm. This makes a few of the checks pass and also
allows us to just check `-mcpu=native` for architecture detection
instead of finding the tools manually.
2024-06-25 07:02:57 -05:00
Schrodinger ZHU Yifan
2efe3d7fc0
Reland "[libc] fix aarch64 linux full build (#95358)" (#95423)
Reverts llvm/llvm-project#95419 and Reland #95358.

This PR is full of temporal fixes. After a discussion with @lntue, it is
better to avoid further changes to the cmake infrastructure for now as a
rework to the cmake utilities will be landed in the future.
2024-06-13 20:34:32 -07:00
Joseph Huber
2c3723d321 [libc] Correctly pass the C++ standard to NVPTX internal builds
Summary:
The NVPTX build wasn't getting the `C++20` standard necessary for a few
files.
2024-06-07 07:55:06 -05:00
Joseph Huber
cf3b37c92f [libc] Add GPU utility dependencies if present
Summary:
These tools need to be built before we can do the library creation
stage. If they are generated in the same build then this is not
guaranteed so we should add explicit dependencies.
2024-06-03 14:37:39 -05:00