Commit Graph

128 Commits

Author SHA1 Message Date
Joseph Huber
d04494ccc9 [libc] Rework the file handling for the GPU
The GPU has much tighter requirements for handling IO functions.
Previously we attempted to define the GPU as one of the platform files.
Using a common interface allowed us to easily define these functions
without much extra work. However, it became more clear that this was a
poor fit for the GPU. The file interface uses function pointers, which
prevented inlining and caused bad perfromance and resource usage on the
GPU. Further, using an actual `FILE` type rather than referring to it as
a host stub prevented us from usin files coming from the host on the GPU
device.

After talking with @sivachandra, the approach now is to simply define
GPU specific versions of the functions we intend to support. Also, we
are ignoring `errno` for the time being as it is unlikely we will ever
care about supporting it fully.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D157427
2023-08-09 14:42:20 -05:00
Guillaume Chatelet
575310330b [libc][doc] Update macros documentation
Update documentaiton now that macros are laid out in a more structured way.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143911
2023-08-08 08:02:30 +00:00
Joseph Huber
c381a94753 [libc] Remove test RPC opcodes from the exported header
This patch does the noisy work of removing the test opcodes from the
exported interface to an interface that is only visible in `libc`. The
benefit of this is that we both test the exported RPC registration more
directly, and we do not need to give this interface to users.

I have decided to export any opcode that is not a "core" libc feature as
having its MSB set in the opcode. We can think of these as non-libc
"extensions".

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154848
2023-07-21 15:36:36 -05:00
Joseph Huber
b454e7aa7c [libc] Remove GPU string functions incompatible with C++
These functions have definitions differing between C and C++. GNU
respects the C++ definitions while the LLVM libc does not. This causes
many bugs and the current hack creates other issues. Rather than hack
around this I'd rather temporarily disable these than regress with the
integration into other offloading languages. We lose test support for
them but we should be able to re-enable these once the `libc` headers
provide these correctly.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154850
2023-07-10 10:40:10 -05:00
Joseph Huber
c850ea1498 [libc] Support fopen / fclose on the GPU
This patch adds the necessary support for the fopen and fclose functions
to work on the GPU via RPC. I added a new test that enables testing this
with the minimal features we have on the GPU. I will update it once we
have `fread` and `fwrite` to actually check the outputted strings. For
now I just relied on checking manually via the outpuot temp file.

Reviewed By: JonChesterfield, sivachandra

Differential Revision: https://reviews.llvm.org/D154519
2023-07-05 18:31:58 -05:00
Joseph Huber
7e88e26d38 [libc] Add GPU support for the 'inttypes.h' functions
Another low hanging fruit we can put on the GPU, this ports the tests
over to the hermetic framework so we can run them on the GPU.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D154540
2023-07-05 17:47:10 -05:00
Joseph Huber
b15ac1fd89 [libc] Enable the 'div' routines on the GPU
This patch simply enables the `div`, `ldiv,` and, `lldiv` functions on
the GPU. This should be straightforward enough.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D154143
2023-06-29 15:42:46 -05:00
Joseph Huber
667c10353e [libc] Fix the implementation of exit on the GPU
The RPC calls all have delays associated with them. Currently the `exit`
function does an async send and immediately exits the GPU. This can have
the effect that the RPC server never sees the exit call and we continue.
This patch changes that to first sync with the server before continuing
to perform its exit. There is still a hazard here, where the kernel can
complete before the RPC call reads back its response, but this is simply
multi-threaded hazards. This change ensures that the server *will*
always exit some time after the GPU exits.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D154112
2023-06-29 13:22:23 -05:00
Tue Ly
f320fefc4a [libc][math] Implement erff function correctly rounded to all rounding modes.
Implement correctly rounded `erff` functions.

For `x >= 4`, `erff(x) = 1` for `FE_TONEAREST` or `FE_UPWARD`, `0x1.ffffep-1` for `FE_DOWNWARD` or `FE_TOWARDZERO`.

For `0 <= x < 4`, we divide into 32 sub-intervals of length `1/8`, and use a degree-15 odd polynomial to approximate `erff(x)` in each sub-interval:
```
  erff(x) ~ x * (c0 + c1 * x^2 + c2 * x^4 + ... + c7 * x^14).
```

For `x < 0`, we can use the same formula as above, since the odd part is factored out.

Performance tested with `perf.sh` tool from the CORE-MATH project on AMD Ryzen 9 5900X:

Reciprocal throughput (clock cycles / op)
```
$ ./perf.sh erff --path2
GNU libc version: 2.35
GNU libc release: stable
-- CORE-MATH reciprocal throughput --  with -march=native      (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 11.790 + 0.182 clc/call; Median-Min = 0.154 clc/call; Max = 12.255 clc/call;
-- CORE-MATH reciprocal throughput --  with -march=x86-64-v2      (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 14.205 + 0.151 clc/call; Median-Min = 0.159 clc/call; Max = 15.893 clc/call;

-- System LIBC reciprocal throughput --
[####################] 100 %
Ntrial = 20 ; Min = 45.519 + 0.445 clc/call; Median-Min = 0.552 clc/call; Max = 46.345 clc/call;

-- LIBC reciprocal throughput --  with -mavx2 -mfma     (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 9.595 + 0.214 clc/call; Median-Min = 0.220 clc/call; Max = 9.887 clc/call;
-- LIBC reciprocal throughput --  with -msse4.2     (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 10.223 + 0.190 clc/call; Median-Min = 0.222 clc/call; Max = 10.474 clc/call;
```

and latency (clock cycles / op):
```
$ ./perf.sh erff --path2
GNU libc version: 2.35
GNU libc release: stable
-- CORE-MATH latency --  with -march=native      (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 38.566 + 0.391 clc/call; Median-Min = 0.503 clc/call; Max = 39.170 clc/call;
-- CORE-MATH latency --  with -march=x86-64-v2      (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 43.223 + 0.667 clc/call; Median-Min = 0.680 clc/call; Max = 43.913 clc/call;

-- System LIBC latency --
[####################] 100 %
Ntrial = 20 ; Min = 111.613 + 1.267 clc/call; Median-Min = 1.696 clc/call; Max = 113.444 clc/call;

-- LIBC latency --  with -mavx2 -mfma     (with FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 40.138 + 0.410 clc/call; Median-Min = 0.536 clc/call; Max = 40.729 clc/call;
-- LIBC latency --  with -msse4.2     (without FMA instructions)
[####################] 100 %
Ntrial = 20 ; Min = 44.858 + 0.872 clc/call; Median-Min = 0.814 clc/call; Max = 46.019 clc/call;
```

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D153683
2023-06-28 13:58:37 -04:00
Tue Ly
d9428945df [libc][Obvious] Fix docs warning. 2023-06-26 14:32:28 +00:00
Joseph Huber
70b1c3999c [libc][Docs] Add some motivation for the GPU libc
This provides some basic motivation behind the GPU libc. Suggests are welcome.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D152028
2023-06-16 15:19:45 -05:00
Alex Brachet
61c9052cec [libc] Add LIBC_INLINE_VAR for inline variables
These are the only variables I could find that use LIBC_INLINE. Note, these are namespace scoped constexpr so local linkage is implied. inline is useful here to silence clang's unused-const-variable variable. For Fuchsia, the distinction between LIBC_INLINE and LIBC_INLINE_VAR is helpful because we define LIBC_INLINE as `[[gnu::always_inline]] inline` when building with gcc. This isn't meaningful on variables.

Alternatively, we could make these variables simply constexpr and also add `[[maybe_unused]]`

Reviewed By: sivachandra, mcgrathr

Differential Revision: https://reviews.llvm.org/D152951
2023-06-16 15:46:32 +00:00
Joseph Huber
fd14f7adbe [libc] Enable conversion functions on the GPU
These functions were previously removed due to problems running the
tests with `errno` in them. This was resolved previously by making the
internal implementation of these functions use a global `errno` so that
tests can still use `errno` functionality as long as they are run with a
single thread. This allows us to re-enable these tests as a previous
patch has also resolved the issue where the `stdlib` tests could not be
hermetic due to the dependence on system rounding functions.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D153016
2023-06-15 09:38:12 -05:00
Michael Jones
99686c5ed1 [libc][docs] Add undefined behavior doc to site
This document is based on the RFC posted to discourse:
https://discourse.llvm.org/t/rfc-defining-undefined-behavior-in-libc/

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D152588
2023-06-12 11:13:51 -07:00
Joseph Huber
8aad5012cc [libc][Docs] Add support for the printing functions 2023-06-06 14:33:08 -05:00
Tue Ly
cfc5c6cb8d [libc][docs] Update implementation status table for Date and Time Functions.
Update implementation status table for Date and Time Functions to include different targets.

Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D151809
2023-05-31 15:09:06 -04:00
Tue Ly
e557b8a142 [libc][RISCV] Add log, log2, log1p, log10 for RISC-V64 entrypoints.
Add log, log2, log1p, log10 RISCV64 entrypoints.

Reviewed By: michaelrj, sivachandra

Differential Revision: https://reviews.llvm.org/D151674
2023-05-30 14:18:19 -04:00
Tue Ly
0bda541829 [libc][doc] Update math function status page to show more targets.
Show availability of math functions on each target.

Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D151489
2023-05-25 19:24:33 -04:00
Tobias Hieta
f98ee40f4b
[NFC][Py Reformat] Reformat python files in the rest of the dirs
This is an ongoing series of commits that are reformatting our
Python code. This catches the last of the python files to
reformat. Since they where so few I bunched them together.

Reformatting is done with `black`.

If you end up having problems merging this commit because you
have made changes to a python file, the best way to handle that
is to run git checkout --ours <yourfile> and then reformat it
with black.

If you run into any problems, post to discourse about it and
we will try to help.

RFC Thread below:

https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style

Reviewed By: jhenderson, #libc, Mordante, sivachandra

Differential Revision: https://reviews.llvm.org/D150784
2023-05-25 11:17:05 +02:00
Kazu Hirata
9a515d8142 [libc] Fix typos in documentation 2023-05-22 23:27:59 -07:00
Kazu Hirata
282798fefd [libc] Fix typos in documentation 2023-05-06 11:26:32 -07:00
Joseph Huber
023bfc196b [libc][docs] Fix incorrect CMake argument in GPU documentation
Summary;
This was changed a long time ago to drop the `LLVM_` prefix.

Differential Revision: https://reviews.llvm.org/D150012
2023-05-05 20:16:47 -05:00
Joseph Huber
62a2a07cbe [libc][Docs] Add warning about running GPU tests with parallelism
Now that a large amount of GPU tests can be run in parallel, we should
document spurious failures. It is a well-known issue that launching many
GPU applications in parallel can lead to various problems, the worst of
which being an indefinite hang.

Differential Revision: https://reviews.llvm.org/D149857
2023-05-04 10:18:06 -05:00
Joseph Huber
632fa3798c [libc] Enable running libc unit tests on AMDGPU
The previous patches added the necessary support for global constructors
used to register tests. This patch enables the AMDGPU target to build
and run the unit tests on the GPU. Currently this only tests the `ctype`
tests, but adding more should be straightforward from here on.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149517
2023-05-04 06:32:52 -05:00
Joseph Huber
f7d5e9bc4c [libc] Support the abs functions in the GPU libc.
This patch simply enables building the integer `abs` functions for the
GPU.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149317
2023-04-27 20:31:59 -05:00
Joseph Huber
72bfe2c05a [libc] Support the string conversion methods on the GPU
This patch enables us to use the existing `libc` support for string
conversion functions on the GPU. This required setting the `fenv_t` and
long double configuration. As far as I am aware, long doubles are
converted to doubles on the GPU and the floating point environment is
just an `uint32_t`.

This code is still untested as we are still working out how to run the
unit tests on the GPU.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D149306
2023-04-27 20:31:58 -05:00
Joseph Huber
807f058487 [libc][Docs] Begin improving documentation for the GPU libc
This patch updates some of the documentation for the GPU libc project.
There is a lot of work still to be done, but this sets the general
outline.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D149194
2023-04-26 10:30:54 -05:00
Kazu Hirata
e042efdab6 [libc] Fix typos in documentation 2023-04-24 23:31:48 -07:00
Siva Chandra Reddy
733ac920be [libc] Add two more recipes to do a cross build.
Reviewed By: jeffbailey

Differential Revision: https://reviews.llvm.org/D147350
2023-04-18 12:40:14 -07:00
Tue Ly
f63025f52f [libc][Obvious] Fix the performance table in math function documentation. 2023-04-18 14:10:26 -04:00
Michael Jones
1c261e360f [libc] Add implementation of getchar
added getchar and getchar_unlocked which are just wrappers getc and getc_unlocked respectively.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D147919
2023-04-14 15:40:05 -07:00
Tue Ly
9af8dca70f [libc][math] Update range reduction step for log10f and reduce its latency.
Simplify the range reduction steps by choosing the reduction constants
carefully so that the reduced arguments v = r*m_x - 1 and v^2 are exact in double
precision, even without FMA instructions, and -2^-8 <= v < 2^-7.  This allows the
polynomial evaluations to be parallelized more efficiently.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D147676
2023-04-07 10:31:46 -04: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
Jeff Bailey
1b89aeb517 [libc] Add instructions for linux headers
Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D146049
2023-03-15 03:02:16 +00:00
Jeff Bailey
ea471e2aa8 [libc] Update cross-compilation instructions
* Add various options so that it uses the build lld and compiler-rt
 * Add instructions on how to use the newly built libc
 * Remove trailing comments in code-block for cut and pastability

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145878
2023-03-14 05:31:20 +00:00
Michael Jones
08df6c78b5 [libc][docs] update list of completed stdio funcs
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145633
2023-03-09 10:44:50 -08:00
Michael Jones
60178f04fc [libc][docs] add link to join the discord
Previously we only linked to the channel within the discord.

Differential Revision: https://reviews.llvm.org/D145283
2023-03-07 16:33:27 -08:00
Siva Chandra Reddy
4155503b71 [libc] Add a note about using assertions in the libc runtime code.
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145466
2023-03-07 10:16:50 -08:00
Siva Chandra Reddy
4052bc8674 [libc] Make errno an entrypoint.
The entrypoint has been added to the various entrypoint lists. The libc
code style doc has been updated with information on how errno should be
set from the libc runtime code.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D145179
2023-03-03 06:59:19 +00:00
Siva Chandra Reddy
54faf22f7b [libc][NFC] Move all developer docs into a sub-directory docs/dev. 2023-03-02 10:59:49 -08:00
Tue Ly
6c7894a8e6 [libc][doc] Move docs/math.rst to docs/math/index.rst
Move docs/math.rst to docs/math/index.rst

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D144028
2023-02-14 13:41:44 -05:00
Renyi Chen
6cb14adbfa [libc][math] Implement scalbn, scalbnf, scalbnl.
Implement scalbn via `fptuil::ldexp` for `FLT_RADIX==2` case.
"unimplemented" otherwise.

Reviewed By: lntue, sivachandra

Differential Revision: https://reviews.llvm.org/D143116
2023-02-09 05:55:34 +00:00
Guillaume Chatelet
a2569a76e0 [libc][NFC] Rename macros 2023-02-07 20:43:33 +00:00
Guillaume Chatelet
83e98c8556 [libc][doc] Update compiler
Differential Revision: https://reviews.llvm.org/D143389
2023-02-07 09:32:34 +00:00
Guillaume Chatelet
c1832e903e [libc][doc] Add macros guidelines
This is a first follow up on the libc tuning RFC
https://discourse.llvm.org/t/rfc-llvm-libc-tuning/67980

Once we agree on the format. I'll land a couple of patches to match the guidelines.

Differential Revision: https://reviews.llvm.org/D143413
2023-02-07 09:29:36 +00:00
Tue Ly
a48c4a45e0 [libc][Obvious] Change ninja llvmlibc to ninja libc in the documentations. 2023-02-03 15:16:01 -05:00
Tue Ly
1ad8d9d66d [libc][bazel] Add bazel targets and unit tests for math functions.
Add bazel targets and unit tests for single precision exponential,
logarithm, trigonometric, inverse trig, hyperbolic, and inverse hyperbolic
functions.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143275
2023-02-03 15:11:20 -05:00
David Spickett
008acfb519 [libc] Fix quotation marks in overlay mode instructions
These were using ” ("right double quotation mark") instead of
the usual ". This means CMake took the value literally and you
got:
```
CMake Error at CMakeLists.txt:139 (MESSAGE):
  ”libc” isn't a known project:
  bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl;flang.
  Did you mean to enable it as a runtime in LLVM_ENABLE_RUNTIMES?
```
`="libc"` works fine.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D143254
2023-02-03 14:44:40 +00:00
Tue Ly
9b30f6b6d7 [libc][math] Implement acoshf function correctly rounded to all rounding modes.
Implement acoshf function correctly rounded to all rounding modes.

Reviewed By: zimmermann6

Differential Revision: https://reviews.llvm.org/D142781
2023-02-01 11:35:15 -05:00