Commit Graph

1600 Commits

Author SHA1 Message Date
Joseph Huber
fd059ea7ec [libc] Simplify enabling the GPU build for libc
Currently the GPU build requires the `LLVM_LIBC_FULL_BUILD` option to be
set. This patch changes the logic so that it is always enabled when
targeting the GPU. Also, this patch allows `LIBC_GPU_BUILD` and
`LIBC_GPU_ARCHITECTURES` to both enable a GPU build. Now, enabling the
GPU support should only require the following CMake:

```
  -DLLVM_ENABLE_RUNTIMES=libc -DLIBC_GPU_ARCHITECTURES=gfx1030
```

Reviewed By: jdoerfert, sivachandra

Differential Revision: https://reviews.llvm.org/D146979
2023-03-27 13:22:28 -05:00
Joseph Huber
b26970ec4b [libc] Enable integration tests targeting NVIDIA GPUs
This patch adds the necessary build infrastructure to build and run the
integration tests on NVIDIA GPUs. The NVIDIA `nvlink` linker utility is
what is ultimately used to combine these files into a single executable
image. Unfortunately, their tool does not support static libraries. So
we need to link with every object directly instead. This could be solved
by impelementing a "wrapper" utility around `nvlink` like we used to use
for OpenMP. But for now this should be sufficient.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146861
2023-03-27 08:47:16 -05:00
Roland McGrath
69a0924fac [libc] Add missing cast in fputil sqrt code
A cast is necessary to avoid implicit narrowing warnings
when those are enabled.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D146886
2023-03-25 16:23:48 -07:00
Roland McGrath
d30de9844e [libc] Define LLVM_LIBC_FUNCTION with a layer of macro expansion
Move the real LLVM_LIBC_FUNCTION macro definitions to
LLVM_LIBC_FUNCTION_IMPL and make LLVM_LIBC_FUNCTION a wrapper to
expand macros in its arguments.  This makes it possible to
compile libc implementation and test files with -Dfunc=othername.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146863
2023-03-25 11:30:48 -07:00
Roland McGrath
0be1fbac2a [libc] Remove unused aarch64 sqrt and sqrtf implementations
These files are not used because the generic sqrt and sqrtf
functions already go through internal layers that reach the
machine-specific internal implemenations.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146865
2023-03-24 21:15:20 -07:00
Joseph Huber
58f5e5e6b0 [libc] Implement the RPC client / server for NVPTX
This patch adds the necessary code to impelement the existing RPC client
/ server interface when targeting NVPTX GPUs. This follows closely to
the implementation in the AMDGPU version. This does not yet enable unit
testing as the `nvlink` linker does not support static libraries. So
that will need to be worked around.

I am ignoring the RPC duplication between the AMDGPU and NVPTX loaders. This
will be changed completely later so there's no point unifying the code at this
stage. The implementation was tested manually with the following file and
compilation flags.

```
namespace __llvm_libc {
void write_to_stderr(const char *msg);
void quick_exit(int);
} // namespace __llvm_libc

using namespace __llvm_libc;

int main(int argc, char **argv, char **envp) {
  for (int i = 0; i < argc; ++i) {
    write_to_stderr(argv[i]);
    write_to_stderr("\n");
  }
  quick_exit(255);
}
```

```
$ clang++ crt1.o rpc_client.o quick_exit.o io.o main.cpp --target=nvptx64-nvidia-cuda -march=sm_70 -o image
$ ./nvptx_loader image 1 2 3
image
1
2
3
$ echo $?
255
```

Depends on D146681

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D146846
2023-03-24 20:04:43 -05:00
Joseph Huber
2bef46d2ad [libc] Add a loader utility for NVPTX architectures for testing
This patch adds a loader utility targeting the CUDA driver API to launch
NVPTX images called `nvptx_loader`. This takes a GPU image on the
command line and launches the `_start` kernel with the appropriate
arguments. The `_start` kernel is provided by the already implemented
`nvptx/start.cpp`. So, an application with a `main` function can be
compiled and run as follows.

```
clang++ --target=nvptx64-nvidia-cuda main.cpp crt1.o -march=sm_70 -o image
./nvptx_loader image args to kernel
```

This implementation is not tested and does not yet support RPC. This
requires further development to work around NVIDIA specific limitations
in atomics and linking.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D146681
2023-03-24 20:04:42 -05:00
Siva Chandra Reddy
9fee241122 [libc][Obvious] Remove a compile opt to x86_64 longjmp in a previous commit.
The option -fno-omit-frame-pointer was accidentally added to the x86_64
longjmp target. This change not only removes it, but makes it
-fomit-frame-pointer.
2023-03-24 20:51:45 +00:00
Joseph Huber
1fce1d341b [libc] Use nvptx_kernel attribute in NVPTX startup code
Summary:
A recent patch allowed us to emit a callable kernel from freestanding
NVPTX code. This allows us to move away from using the CUDA language.
This has several advantages in that it works around an entire assortment
of errors I was seeing while implementing RPC for Nvidia.
2023-03-24 14:46:26 -05:00
Mikhail R. Gadelha
0f6fd1b704 [libc] Add support for setjmp and longjmp in riscv
This patch implements setjmp and longjmp in riscv using inline asm. The
following changes were required:

* Omit frame pointer: otherwise gcc won't allow us to use s0
* Use __attribute__((naked)): otherwise both gcc and clang will generate
function prologue and epilogue in both functions. This doesn't happen
in x86_64, so we guard it to only riscv

Furthermore, using __attribute__((naked)) causes two problems: we
can't use `return 0` (both gcc and clang) and the function arguments in
the function body (clang only), so we had to use a0 and a1 directly.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145584
2023-03-24 16:16:31 -03:00
Joseph Huber
53627ffb3c [libc] Change RPC outbox stores to be relaxed
Summary:
These stored previously used `RELEASE`. This was done originally to
ensure that the stores to the shared memory buffer were flushed prior to
signaling that the other side can begin accessing it. However, this
should be accomplished by the memory fence above the store. This change
is required because NVPTX does not support non-relaxed atomics used on
unified shared memory.
2023-03-24 09:38:51 -05:00
Joseph Huber
d11e49f0c8 [libc][NFC] Fix misspelled variable name in cmake message 2023-03-23 16:30:31 -05:00
Alex Brachet
a86cc8341d [libc] Move fma and fmaf into generic dir
Differential Revision: https://reviews.llvm.org/D146740
2023-03-23 18:43:09 +00:00
Alex Brachet
7d11a592c5 [libc] Fix some math conversion warnings
Differential Revision: https://reviews.llvm.org/D146738
2023-03-23 17:07:19 +00:00
Michael Jones
de939c6cd8 [libc] enable printf using system FILE
The printf and fprintf implementations use our internal implementation
to improve performance when it's available, but this patch enables using
the public FILE API for overlay mode.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D146001
2023-03-23 09:56:45 -07:00
Joseph Huber
e33f8ac9d8 [libc] Fix inline assembly for nvptx quick_exit
Summary:
The `exit` function in NVPTX has no intrinsic, but the assembly requires
a semicolon in the ptx, otherwise it will fail.
2023-03-23 11:28:33 -05:00
Joseph Huber
9c8bdbcbc5 [libc] Implement memory fences on NVPTX
Memory fences are not handled by the NVPTX backend. We need to replace
them with a memory barrier intrinsic function. This doesn't include the
ordering, but should perform the necessary functionality, albeit slower.

Reviewed By: tianshilei1992

Differential Revision: https://reviews.llvm.org/D146725
2023-03-23 11:26:35 -05:00
Joseph Huber
ae63b1a576 [libc] Adjust NVPTX startup code
Summary:
The startup code needs to include the environment pointer so we add this
to the arguments. Also we need to ensure that the `crt1.o` file is made
with `-fgpu-rdc` set so we can actually use it without undefined
reference errors.
2023-03-22 20:08:08 -05:00
Joseph Huber
ebcc6dba5f [libc] Don't install the GPU startup code for now
Summary:
This startup code is only intended to be used internally, we shouldn't
export it under a conflicting name. In the future we may package this in
an exportable format.
2023-03-21 21:01:27 -05:00
Michael Jones
9e2b164570 [libc] temporarily disable wctob entrypoint
Differential Revision: https://reviews.llvm.org/D146484
2023-03-20 17:42:00 -07:00
Michael Jones
3d9e646c7c [libc][obvious] fix missing stdio dependency
Differential Revision: https://reviews.llvm.org/D146482
2023-03-20 17:37:09 -07:00
Michael Jones
23ea2c4f4a [libc][obvious] remove currently unused types
Somehow having MBState and StructTmType in the definition for wchar was
causing test failures. This should fix those.

Differential Revision: https://reviews.llvm.org/D146476
2023-03-20 17:22:52 -07:00
Michael Jones
07cdf4a10d [libc][obvious] fix missing type headers
forgot to add wint_t and wchar_t type headers to the wchar
headergen definition.

Differential Revision: https://reviews.llvm.org/D146472
2023-03-20 16:43:09 -07:00
Michael Jones
46b5087227 [libc] add basic wide char functions
This patch adds the wchar header, as well as the functions to convert to
and from wide chars. The header also sets up the definitions for wint
and wchar.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145995
2023-03-20 16:36:21 -07:00
Michael Jones
99047c0501 [libc][bazel] add targets for sprintf
The bazel build is currently overlay mode only, so the FILE functions
are still out of reach for it, but sprintf only uses strings. This adds
targets for sprintf, snprintf, and all the interal printf pieces, as
well as tests.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D146100
2023-03-20 15:09:20 -07:00
Joseph Huber
6bd4d717d5 [libc] Add environment variables to GPU libc test for AMDGPU
This patch performs the same operation to copy over the `argv` array to
the `envp` array. This allows the GPU tests to use environment
variables.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146322
2023-03-20 13:16:58 -05:00
Roland McGrath
4e298c32d5 [libc] Make string tests compatible with the Fuchsia build
Some test code was doing loose conversions caught by compiler
warnings in  the Fuchsia build.  This included duplicated code
in a few tests that was reconsolidated with the existing header
file copy of the same functions.

The MemoryMatcher abstraction presumes gtest-style matcher support,
which is not available in Fuchsia's zxtest library.  It's avoided
in favor of simpler memory-comparing assertions.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D146343
2023-03-20 10:22:52 -07:00
Joseph Huber
ae30ae23aa [libc][NFC] Add some missing comments to the RPC implementation
Summary:
These comments were accidentally dropped from the committed version. Add
them back in.
2023-03-20 09:30:12 -05:00
Mark de Wever
d0398d3593 Revert "Reland "[CMake] Bumps minimum version to 3.20.0.""
This reverts commit a72165e5df.

Some buildbots have not been updated yet.
2023-03-18 20:32:43 +01:00
Mark de Wever
a72165e5df Reland "[CMake] Bumps minimum version to 3.20.0."
This reverts commit 92523a35a8.

Test whether all CI runners are updated.
2023-03-18 13:33:42 +01:00
Joseph Huber
179a7f1ea2 [libc] Fix dependency to unit tests, and quitting in non-GPU mode
Summary:
Fixes the lack of a dependency after changing the order of some
includes. Also we weren't running any tests as the GPU was always
disabling them. Fix the logic.
2023-03-17 13:08:01 -05:00
Joseph Huber
39e91098b5 [libc] Enable integration tests targeting the GPU
This patch enables integration tests running on the GPU. This uses the
RPC interface implemented in D145913 to compile the necessary
dependencies for the integration test object. We can then use this to
compile the objects for the GPU directly and execute them using the AMD
HSA loader combined with its RPC server. For example, the compiler is
performing the following actions to execute the integration tests.

```
$ clang++ --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -nostdlib -flto -ffreestanding \
    crt1.o io.o quick_exit.o test.o rpc_client.o args_test.o -o image
$ ./amdhsa_loader image 1 2 5
args_test.cpp:24: Expected 'my_streq(argv[3], "3")' to be true, but is false
```

This currently only works with a single threaded client implementation
running on AMDGPU. Further work will implement multiple clients for AMD
and the ability to run on NVPTX as well.

Depends on D145913

Reviewed By: sivachandra, JonChesterfield

Differential Revision: https://reviews.llvm.org/D146256
2023-03-17 12:55:32 -05:00
Joseph Huber
8e4f9b1fcb [libc] Add initial support for an RPC mechanism for the GPU
This patch adds initial support for an RPC client / server architecture.
The GPU is unable to perform several system utilities on its own, so in
order to implement features like printing or memory allocation we need
to be able to communicate with the executing process. This is done via a
buffer of "sharable" memory. That is, a buffer with a unified pointer
that both the client and server can use to communicate.

The implementation here is based off of Jon Chesterfields minimal RPC
example in his work. We use an `inbox` and `outbox` to communicate
between if there is an RPC request and to signify when work is done.
We use a fixed-size buffer for the communication channel. This is fixed
size so that we can ensure that there is enough space for all
compute-units on the GPU to issue work to any of the ports. Right now
the implementation is single threaded so there is only a single buffer
that is not shared.

This implementation still has several features missing to be complete.
Such as multi-threaded support and asynchrnonous calls.

Depends on D145912

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145913
2023-03-17 12:55:31 -05:00
Tue Ly
11da3de82d [libc][Obvious] Reduce the size of the tests for hypot(f). 2023-03-16 21:17:20 -04:00
Sam James
2e8ef93439 [libc] [test] Add missing <cstdint> include
Fixes build w/ GCC 13:
```
[587/7274] Building CXX object projects/libc/test/src/__support/CMakeFiles/libc_system_str_to_float_comparison_test.dir/str_to_float_comparison_test.cpp.o
FAILED: projects/libc/test/src/__support/CMakeFiles/libc_system_str_to_float_comparison_test.dir/str_to_float_comparison_test.cpp.o
/usr/lib/llvm/16/bin/clang++ -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/sam/git/llvm-project/build/projects/libc/test/src/__support -I/home/sam/git/llvm-project/libc/test/src/__support -I/home/sam/git/llvm-project/build/include -I/home/sam/git/llvm-project/llvm/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -std=c++17 -MD -MT projects/libc/test/src/__support/CMakeFiles/libc_system_str_to_float_comparison_test.dir/str_to_float_comparison_test.cpp.o -MF projects/libc/test/src/__support/CMakeFiles/libc_system_str_to_float_comparison_test.dir/str_to_float_comparison_test.cpp.o.d -o projects/libc/test/src/__support/CMakeFiles/libc_system_str_to_float_comparison_test.dir/str_to_float_comparison_test.cpp.o -c /home/sam/git/llvm-project/libc/test/src/__support/str_to_float_comparison_test.cpp
/home/sam/git/llvm-project/libc/test/src/__support/str_to_float_comparison_test.cpp:34:15: error: unknown type name 'uint32_t'
static inline uint32_t hexCharToU32(char in) {
              ^
/home/sam/git/llvm-project/libc/test/src/__support/str_to_float_comparison_test.cpp:39:15: error: unknown type name 'uint32_t'
static inline uint32_t fastHexToU32(const char *inStr) {
              ^
```

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D146111
2023-03-16 19:03:59 +00:00
Siva Chandra Reddy
b0f9206057 [libc][Obvious] Remove the STARTUP attribute from the api test listing. 2023-03-16 17:48:17 +00:00
Joseph Huber
06c8602526 [libc] Remove unused startup source file
Summary:
This was not removed from the previous patch. Fix that.
2023-03-16 12:41:52 -05:00
Joseph Huber
d9c1dbc204 [libc] Remove startup option from integration tests
This generalizes handling of the integration tests. We now implicitly
depend on the `libc.startup.${LIBC_TARGET_OS}.crt1` file rather than
passing it in manually. This simplifies the interface.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146237
2023-03-16 12:38:41 -05:00
Joseph Huber
a704c312f1 [libc] Add missing dependencies to RISC-V startup implementation
Summary:
Just like the last patch, the threads and envrion dependencies were
missing. This lead to linker failures when building the tests.
2023-03-16 09:13:18 -05:00
Joseph Huber
501a8724bd [libc] Add missing environ dependency to AArch64 startup implementation
Summary:
Just like the last patch, this dependency was missing. This lead to
linker failures when building the tests.
2023-03-16 08:06:14 -05:00
Joseph Huber
2389f00019 [libc] Add missing threads dependency to AArch64 startup implementation
Summary:
The changes in D146184 made the integration tests use the inhereted
dependencies from the startup code like a normal target. For the AArch64
target this resulted in the threads depenency not being pulled in
because it was not present in the original code.
2023-03-16 08:00:14 -05:00
Joseph Huber
f62523032d [libc] Inherit integration test dependencies from the startup target
All integration tests rely on the startup code to be run. Currently we
manually include a few of these dependencies that are relevant for the
Linux target. This patch changes this to make the integration test's
dependencies include all the dependencies of the startup code. This
simplifies the code and makes it easier to support different targets.
The changes here cause the integration test to be dependent on more
targets than previously necessary, but it should be fine.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146184
2023-03-16 07:33:22 -05:00
Mikhail R. Gadelha
1990ce74dc [libc] Enable __llvm_libc_syscall and fork
This patch enables the remaining calls from unistd.

The test cases had to be updated to:
1. Use SYS_symlinkat if SYS_symlink is not available
2. Use SYS_readlinkat if SYS_readlink is not available
3. Use SYS_unlinkat if SYS_unlink is not available
4. Use SYS_openat if SYS_open is not available

We also abort compilation if neither of the syscalls mentioned above are
available.

Differential Revision: https://reviews.llvm.org/D146161
2023-03-16 06:16:14 -03:00
Mikhail R. Gadelha
fe99de31d9 [libc] Enable spawn lib in riscv
In this patch we add support for the spawn lib in riscv.

Only small changes were required, the biggest one was to use of dup3
instead of dup2, if the latter is not available. This follows our
implementation of dup2.

Differential Revision: https://reviews.llvm.org/D146145
2023-03-16 06:16:14 -03:00
Mikhail R. Gadelha
63ed8ab4db [libc] Update supported riscv libs
This patch removes some duplicated libs added to entrypoints.txt, adds
new libs supported to entrypoints.txt and updates header.txt

Differential Revision: https://reviews.llvm.org/D146065
2023-03-16 06:16:14 -03:00
Siva Chandra Reddy
08b9835072 [libc][docs] Add a section about allocations and deallocations to the style doc.
Fixes #59277 - The main part of that bug has already been addressed. This commit
just adds documentation.

Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D146115
2023-03-15 20:41:51 -07:00
Siva Chandra Reddy
be721a38c6 [libc][docs] Move the link to "Compiler Support" under "Status".
Also, fixed a minor typo in compiler_support.rst.

Reviewed By: jeffbailey, gchatelet

Differential Revision: https://reviews.llvm.org/D146112
2023-03-15 20:41:10 -07:00
Joseph Huber
38d7f855b8 [libc] Add missing return statements to wrapper functions
Summary:
I forgot to add return statements to these memory comparison functions.
This should hopefully resolve some BB errors.
2023-03-15 22:16:27 -05:00
Joseph Huber
22838f6084 [libc] Do not attempt to determine CPU features in GPU mode
Summary:
We don't use these features in the GPU build, trying to determine them
can cause errors. We should just return early if this is the case.
2023-03-15 20:44:12 -05:00
Joseph Huber
929ad8bc7c [libc] Add aliases to C memory functions for integration tests
The integration tests require the C memory functions as the compiler may
emit calls to them directly. The tests normally use the `__internal__`
variant that is built for testing, but these memory functions were
linked directly to preserve the entrypoint. Instead, we forward delcare
the internal versions and map the entrypoints to them manually inside
the integration test. This allows us to use the internal versions of
these files like the rest of the test objects.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146177
2023-03-15 20:15:05 -05:00