2432 Commits

Author SHA1 Message Date
Guillaume Chatelet
b6bc9d72f6
[libc] Mass replace enclosing namespace (#67032)
This is step 4 of
https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079
2023-09-26 11:45:04 +02:00
michaelrj-google
23552fe220
[libc] Acquire the lock for scanf files (#67357)
When creating the new scanf reader design, I forgot to add back the
calls to flockfile and funlockfile in vfscanf_internal. This patch fixes
that, and also changes the system file version to use the normal
variants since ungetc_unlocked isn't always available.
2023-09-25 15:00:03 -07:00
Joseph Huber
791b279924
[libc] Change the puts implementation on the GPU (#67189)
Summary:
Normally, the implementation of `puts` simply writes a second newline
charcter after printing the first string. However, because the GPU does
everything in batches of the SIMT group size, this will end up with very
poor output where you get the strings printed and then 1-64 newline
characters all in a row. Optimizations like to turn `printf` calls into
`puts` so it's a good idea to make this produce the expected output.

The least invasive way I could do this was to add a new opcode. It's a
little bloated, but it avoids an unneccessary and slow send operation to
configure this.
2023-09-25 11:17:22 -05:00
Joseph Huber
6f4ed39b4a
[libc] Enable hermetic tests for the stdio test suite (#67339)
Summary:
There are several tests here that are not yet using the `add_libc_test`.
Rather than do this individually we should just update these all at
once. These all pass on my x64 build so I'm assuming it should be fine.
2023-09-25 11:14:17 -05:00
Joseph Huber
b5440e443a [libc] Fix cyclical dependency on errno matcher for NVPTX architectures
Summary:
The NVPTX backend cannot handle cyclical dependencies on global variable
initializers. That is, a global variable cannot be used to initialize or
reference another global variable inside of it. This situation was
encountered with the new errno tests. This patch simply replaces the
offending function with a constant version to break the dependency and
alllow the tests to run again.
2023-09-23 08:59:34 -05:00
michaelrj-google
a5a008ff4f
[libc] Refactor scanf reader to match printf (#66023)
In a previous patch, the printf writer was rewritten to use a single
writer class with a buffer and a callback hook. This patch refactors
scanf's reader to match conceptually.
2023-09-22 12:50:02 -07:00
Mikhail R. Gadelha
2f98ff716c
[libc] Update integration test's linking options (#67158)
This patch set the integration test's linking options to be the same one
used in the hermetic tests.

In particular, by removing -nostdlib the tests are linked with
libgcc/compiler-rt and this fixes an issue undefined reference to
__udivdi3 and __umoddi3 in rv32.
2023-09-22 12:06:27 -04:00
Siva Chandra
62a3d84f5c
[libc][NFC] Extend ErrnoSetterMatcher to test expected inequalities. (#67153)
Before this change, ErrnoSetterMatcher only allowed testing for equality
of the expected return and errno values. This change extends it to allow
testing for expected inequalities of the return and errno values. The
test libc.test.src.stdio.fileop_test has been updated to use the
ErrnoSetterMatcher with tests for inequalities.
2023-09-22 08:59:10 -07:00
Mikhail R. Gadelha
7db91b4abe
[libc] Fix pthread_create_test for 32 bit systems (#66564)
The test tries to set the guard_size and stack_size of a thread to
SIZE_MAX / 4, which is a huge value in 64-bit systems but 1GB in 32-bit
ones.

We increase the size to 3 * (SIZE_MAX / 4) so it can also fail in 32-bit 
systems.
2023-09-22 10:10:07 -04:00
Mikhail R. Gadelha
50d1500447
[libc] Add ${CMAKE_CROSSCOMPILING_EMULATOR} to custom test cmdlines (#66565)
${CMAKE_CROSSCOMPILING_EMULATOR} will be used in the new rv32 buildbot
and is prepended automatically when we call add_custom_target in CMake,
except when we use a custom command.

There are two places where custom commands are used in libc, so we
explicitly add the ${CMAKE_CROSSCOMPILING_EMULATOR} variable there.
Other systems that don't use ${CMAKE_CROSSCOMPILING_EMULATOR} are
unaffected
2023-09-22 09:45:27 -04:00
Jeff Bailey
c618e13161
[libc] Pull more definitions from linux/stat.h (#67071)
For file handling, we need more definitions from
linux/stat.h, so this pulls them in. It also adjusts other definitions
to match the kernel's exactly [NFC] so that it's easy to verify that
there's been no divergence one day when it's time to use linux/stat.h
directly.

Tested:
check-libc
2023-09-21 22:34:54 -07:00
Joseph Huber
e0be78be42
[libc] Template the printf / scanf parser class (#66277)
Summary:
The parser class for stdio currently accepts different argument
providers. In-tree this is only used for a fuzzer test, however, the
proposed implementation of the GPU handling of printf / scanf will
require custom argument handlers. This makes the current approach of
using a preprocessor macro messier. This path proposed folding this
logic into a template instantiation. The downside to this is that
because the implementation of the parser class is placed into an
implementation file we need to manually instantiate the needed templates
which will slightly bloat binary size. Alternatively we could remove the
implementation file, or key off of the `libc` external packaging macro
so it is not present in the installed version.
2023-09-21 17:02:26 -05:00
Joseph Huber
f548d19fc8
[libc] Fix and simplify the implementation of 'fread' on the GPU (#66948)
Summary:
Previously, the `fread` operation was wrong in cases when we read less
data than was requested. That is, if we tried to read N bytes while the
file was in EOF, it would still copy N bytes of garbage. This is fixed
by only copying over the sizes we got from locally opening it rather
than just using the provided size.

Additionally, this patch simplifies the interface. The output functions
have special variants for writing to stdout / stderr. This is primarily
an optimization for these common cases so we can avoid sending the
stream as an argument which has a high delay. Because for input, we
already need to start with a `send` to tell the server how much data to
read, it costs us nothing to send the file along with it so this is
redundant. Re-use the file encoding scheme from the other
implementations, the one that stores the stream type in the LSBs of the
FILE pointer.
2023-09-21 14:28:06 -05:00
michaelrj-google
5bd34e0a55
[libc] Fix Off By One Errors In Printf Long Double (#66957)
Two major off-by-one errors are fixed in this patch. The first is in
float_to_string.h with length_for_num, which wasn't accounting for the
implicit leading bit when calculating the length of a number, causing
a missing digit on 80 bit float max. The other off-by-one is the
ryu_long_double_constants.h (a.k.a the Mega Table) not having any
entries for the last POW10_OFFSET in POW10_SPLIT. This was also found on
80 bit float max. Finally, the integer calculation mode was using a
slightly too short integer, again on 80 bit float max, not accounting
for the mantissa width. All of these are fixed in this patch.
2023-09-21 11:43:29 -07:00
Joseph Huber
e2bc0f9266 [libc][NFC] Remove unused function from the RPC server
Summary:
I missed removing this now-unused function in the previous patch. Remove
it to clean up the interface.
2023-09-21 11:56:48 -05:00
Joseph Huber
59896c168a
[libc] Remove the 'rpc_reset' routine from the RPC implementation (#66700)
Summary:
This patch removes the `rpc_reset` function. This was previously used to
initialize the RPC client on the device by setting up the pointers to
communicate with the server. The purpose of this was to make it easier
to initialize the device for testing. However, this prevented us from
enforcing an invariant that the buffers are all read-only from the
client side.

The expected way to initialize the server is now to copy it from the
host runtime. This will allow us to maintain that the RPC client is in
the constant address space on the GPU, potentially through inference,
and improving caching behaviour.
2023-09-21 11:07:09 -05:00
Mikhail R. Gadelha
8d7ca08b9f
[libc] Update siginfo_t to match kernel definition (#66560)
This patch updates the siginfo_t struct definition to match the
definition from the kernel here:

https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/siginfo.h

In particular, there are two main changes:

1. swap position of si_code and si_errno: si_code show come after
si_errno in all systems except MIPS. Since we don't MIPS, the order is
fixed for now, but can be easily \#ifdef'd if MIPS support is
implemented in the future.

2. We add a union of structs that are filled depending on the signal
raised.

This change was required for the fork and spawn integration tests in
rv32, since they fork/clone the running process, call
wait/waitid/waitpid, and read the status, which was wrong in rv32
because wait/waitid/waitpid are implemented in rv32 using SYS_waitid.

SYS_waitid takes a pointer to a siginfo_t and fills the proper fields in
the struct. The previous siginfo_t definition was being incorrectly
filled due to not taking into account the signal raised.
2023-09-21 10:59:03 -04:00
Guillaume Chatelet
270547f3bf
[libc][clang-tidy] Add llvm-header-guard to get consistant naming and prevent file copy/paste issues. (#66477) 2023-09-21 11:14:47 +02:00
Joseph Huber
3641d18557 [libc][Obvious] Fix incorrect RPC opcode for clearerr
Summary:
This was mistakenly using the opcode for `ferror` which wasn't noticed
because tests using this weren't yet activated. This patch fixes this
mistake.
2023-09-20 11:54:35 -05:00
Guillaume Chatelet
467077796a
[reland][libc][cmake] Tidy compiler includes (#66783) (#66878)
This is a reland of #66783 a35a3b75b219247eb9ff6784d1a0fe562f72d415
fixing the benchmark breakage.
2023-09-20 11:21:46 +02:00
michaelrj-google
d37496e75a
[libc] Fix printf config not working (#66834)
The list of printf copts available in config.json wasn't working because
the printf_core subdirectory was included before the printf_copts
variable was defined, making it effectively nothing for the printf
internals. Additionally, the tests weren't respecting the flags so they
would cause the tests to fail. This patch reorders the cmake in src and
adds flag handling in test.
2023-09-19 15:36:14 -07:00
Guillaume Chatelet
9feb0c9b6e
Revert "[libc][cmake] Tidy compiler includes (#66783)" (#66822)
This reverts commit a35a3b75b219247eb9ff6784d1a0fe562f72d415. This broke
libc benchmarks.
2023-09-19 23:18:08 +02:00
Guillaume Chatelet
a35a3b75b2
[libc][cmake] Tidy compiler includes (#66783)
We want to activate `llvm-header-guard` (#66477) but the current CMake
configuration includes paths that should be `isystem`. This PR restricts
the number of `-I` passed to the clang command line and correctly marks
the llvm libc include path as `isystem`.
2023-09-19 23:08:29 +02:00
Tue Ly
84c899b235 [libc][math] Extract non-MPFR math tests into libc-math-smoke-tests.
Extract non-MPFR math tests into libc-math-smoke-tests.

Reviewed By: sivachandra, jhuber6

Differential Revision: https://reviews.llvm.org/D159477
2023-09-19 12:10:21 -04:00
Jeff Bailey
acfb99d9fd
[libc] Specify path for making include/ subdirs (#66589)
When doing a clean build from vscode, it makes the subdirectories in the
source tree rather than in the build folder. Elsehwere in LLVM, they
prefix the MAKE_DIRECTORY calls, so this appears to be the correct
approach.
2023-09-18 21:00:51 -07:00
Joseph Huber
c354ee8d18
[libc][GPU] Fix dependencies for externally installed stub files (#66653)
Summary:
The GPU build has a lot of magic around how we package the output.
Generally, the GPU needs to exist as a secondary fatbinary image for
offloading languages. This is because offloading languages pretend like
offloading to an accelerator is a single file. This then needs to be put
into a single file to make it mesh with the existing build
infrastructure. To work with this, the `libc` makes an installed version
of the library that simply embeds the GPU code into an empty stub file.

This wasn't being updated correctly, which lead to the installed `libc`
static library not being updated correctly when the underlying file was
changed. The previous behaviour only updated when the entrypoint itself
was modified, but not any of its headers. By adding a dependcy on the
actual *object* file we should now capture the regular CMake semantics.
2023-09-18 10:15:02 -05:00
Joseph Huber
b8f64431ea
[libc] Add GPU config file using the new format (#66635)
Summary:
This patch copies a config file for the GPU similar to the
baremetal/embedded implementation. This will configure the
implementations of functions like `sprintf` and `snprintf` to be
compiled into more simple versions that can be run on the GPU. These
functions cannot be enabled yet as Vararg support hasn't landed, but it
will be used then.
2023-09-18 08:06:59 -05:00
Siva Chandra
7d7df7f237
[libc] Add a developer doc about adding new config options. (#66432) 2023-09-15 14:34:25 -07:00
Guillaume Chatelet
c21be63228
[libc][cmake] Report invalid clang-tidy path (#66475)
Adds better error reporting for missing clang-tidy.
2023-09-15 17:43:48 +02:00
Guillaume Chatelet
2dbdc9fc85
[libc] Add invoke / invoke_result type traits (#65750) 2023-09-15 11:15:41 +02:00
Joseph Huber
bbe7eb92b4 [libc][Obvious] Fix missing entrypoints after moving to generic
Summary:
The previous patch moved the implementations of these to generic/ and
accidentally did not add the unlocked variants. This patch fixes that
2023-09-14 15:59:08 -05:00
Joseph Huber
a1be5d69df
[libc] Implement more input functions on the GPU (#66288)
Summary:
This patch implements the `fgets`, `getc`, `fgetc`, and `getchar`
functions on the GPU. Their implementations are straightforward enough.
One thing worth noting is that the implementation of `fgets` will be
extremely slow due to the high latency to read a single char. A faster
solution would be to make a new RPC call to call `fgets` (due to the
special rule that newline or null breaks the stream). But this is left
out because performance isn't the primary concern here.
2023-09-14 15:39:29 -05:00
Mikhail R. Gadelha
72e6f06119
[libc] Fix start up crash on 32 bit systems (#66210)
This patch changes the default types of argc/argv so it's no longer a
uint64_t in all systems, instead, it's now a uintptr_t, which fixes
crashes in 32-bit systems that expect 32-bit types. This patch also adds
two uintptr_t types (EnvironType and AuxEntryType) for the same reason.

The patch also adds a PgrHdrTableType type behind an ifdef that's
Elf64_Phdr in 64-bit systems and Elf32_Phdr in 32-bit systems.
2023-09-14 09:02:35 -04:00
Alex Brachet
2ad7a06cb1
[libc] Fix some warnings (#66366)
Some compilers will warn about dangling else and missleading lack of
parentheses.
2023-09-14 08:47:21 -04:00
Guillaume Chatelet
aee8f8784a
[libc][utils] cpp::always_false to enable static_assert(false) (#66209) 2023-09-14 10:28:43 +02:00
Siva Chandra
f8f934e22c
[libc][NFC] Make the dummy header target under overlay build a library. (#66329)
This fixes the broken overlay builders.
2023-09-13 22:52:20 -07:00
Siva Chandra
17114f8b19
[libc] Remove common_libc_tuners.cmake and move options into config.json. (#66226)
The name has been changed to adhere to the config option naming format.
The necessary build changes to use the new option have also been made.
2023-09-13 22:17:00 -07:00
Michael Jones
3fb63c2921 [libc] simplify printf float writing
The two decimal float printing styles are similar, but different in how
they end. For simplicity of writing I initially gave them different
"write_last_block" functions. This patch unifies them into one function.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158036
2023-09-13 13:53:29 -07:00
Michael Jones
aa1eacd10c [libc][docs] Printf behavior doc
In the document on undefined behavior, I noted that writing down your
decisions is very important. This document contains all the information
for compile flags and undefined behavior for our printf.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D158311
2023-09-13 13:42:30 -07:00
Joseph Huber
089b81105a [libc][NFC][Docs] Update some GPU testing information
Summary:
This comment is outddated and can be removed, also mention an option for
limiting parallelism during tests in the documentation.
2023-09-13 14:30:30 -05:00
michaelrj-google
380eb46b13
[libc] Move long double table option to new config (#66151)
This patch adds the long double table option for printf into the new
configuration scheme. This allows it to be set for most targets but
unset for baremetal.
2023-09-13 10:43:05 -07:00
Joseph Huber
bf85f27370
[libc] Implement 'qsort' and 'bsearch' on the GPU (#66230)
Summary:
This patch simply adds the necessary config to enable qsort and bsearch
on the GPU. It is *highly* unlikely that anyone will use these, as they
are single threaded, but we may as well support all entrypoints that we
can.
2023-09-13 12:06:34 -05:00
Siva Chandra
d25b4fae93
[libc][NFC] Make entrypoint alias targets real library targets. (#66044)
This is part of a libc wide CMake cleanup which aims to eliminate
certain explicitly duplicated logic which is available in CMake-3.20.
This change in particular makes the entrypoint aliases real library
targets so that they can be treated as normal library targets by other
libc build rules.
2023-09-13 08:35:23 -07:00
Mikhail R. Gadelha
75398f28eb [libc] Make time_t 64 bits long on all platforms but arm32
This patch changes the size of time_t to be an int64_t. This still
follows the POSIX standard which only requires time_t to be an integer.

Making time_t a 64-bit integer also fixes two cases in 32 bits platforms
that use SYS_clock_nanosleep_time64 and SYS_clock_gettime64, as the name
of these calls implies, they require a 64-bit time_t. For instance, in rv32,
the 32-bit version of these syscalls is not available.

We also follow glibc here, where time_t is still a 32-bit integer in
arm32.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D159125
2023-09-13 10:49:39 -03:00
Joseph Huber
ef169f5707
[libc] Improve the implementation of the rand() function (#66131)
Summary:
This patch improves the implementation of the standard `rand()` function
by implementing it in terms of the xorshift64star pRNG as described in
https://en.wikipedia.org/wiki/Xorshift#xorshift*. This is a good,
general purpose random number generator that is sufficient for most
applications that do not require an extremely long period. This patch
also correctly initializes the seed to be `1` as described by the
standard. We also increase the `RAND_MAX` value to be `INT_MAX` as the
standard only specifies that it can be larger than 32768.
2023-09-12 16:52:20 -05:00
Joseph Huber
688019851e
[libc][NFC] Factor GPU exiting into a common function (#66093)
Summary:
We currently call the GPU routine to terminate the current thread in
three separate locations .This should be wrapped into a helper function
to simplify the implementation.
2023-09-12 14:59:02 -05:00
Siva Chandra
c5ad6c7781
[libc] Fix a typo in a CMakeLists.txt - replace DEPS with DEPENDS. (#66130) 2023-09-12 12:24:27 -07:00
Siva Chandra
0f31e5697b
[libc] Add missing deps for header libraries. (#66125)
Also, we removed CMP0076 exception sometime back but did not adjust the
build rules. The adjustment in the build rules is also done in this
patch.
2023-09-12 11:53:03 -07:00
Siva Chandra
9048aa71af
[libc] Make add_header and add_gen_header targets normal library targets. (#66045)
This way, they can be added as deps to other library targets without any
special handling.
2023-09-12 08:50:05 -07:00
Guillaume Chatelet
7329816285
[libc] Add is_object (#65749)
Add the is_object type traits.
Implementation comes from
https://en.cppreference.com/w/cpp/types/is_object
2023-09-12 10:35:22 +02:00