aboutsummaryrefslogtreecommitdiff
path: root/libc/utils
AgeCommit message (Collapse)AuthorFilesLines
2024-02-22[libc] Rework the GPU build to be a regular target (#81921)Joseph Huber7-30/+51
Summary: This is a massive patch because it reworks the entire build and everything that depends on it. This is not split up because various bots would fail otherwise. I will attempt to describe the necessary changes here. This patch completely reworks how the GPU build is built and targeted. Previously, we used a standard runtimes build and handled both NVPTX and AMDGPU in a single build via multi-targeting. This added a lot of divergence in the build system and prevented us from doing various things like building for the CPU / GPU at the same time, or exporting the startup libraries or running tests without a full rebuild. The new appraoch is to handle the GPU builds as strict cross-compiling runtimes. The first step required https://github.com/llvm/llvm-project/pull/81557 to allow the `LIBC` target to build for the GPU without touching the other targets. This means that the GPU uses all the same handling as the other builds in `libc`. The new expected way to build the GPU libc is with `LLVM_LIBC_RUNTIME_TARGETS=amdgcn-amd-amdhsa;nvptx64-nvidia-cuda`. The second step was reworking how we generated the embedded GPU library by moving it into the library install step. Where we previously had one `libcgpu.a` we now have `libcgpu-amdgpu.a` and `libcgpu-nvptx.a`. This patch includes the necessary clang / OpenMP changes to make that not break the bots when this lands. We unfortunately still require that the NVPTX target has an `internal` target for tests. This is because the NVPTX target needs to do LTO for the provided version (The offloading toolchain can handle it) but cannot use it for the native toolchain which is used for making tests. This approach is vastly superior in every way, allowing us to treat the GPU as a standard cross-compiling target. We can now install the GPU utilities to do things like use the offload tests and other fun things. Some certain utilities need to be built with `--target=${LLVM_HOST_TRIPLE}` as well. I think this is a fine workaround as we will always assume that the GPU `libc` is a cross-build with a functioning host. Depends on https://github.com/llvm/llvm-project/pull/81557
2024-02-15[libc] add support for function level attributes (#79891)Schrodinger ZHU Yifan2-19/+196
See discussion at https://discourse.llvm.org/t/rfc-support-function-attributes-in-user-interface/76624 Demo macro: ```c++ #if !defined(__LIBC_CONST_ATTR) && defined(__cplusplus) && defined(__GNUC__) #if __has_attribute(const) #define __LIBC_CONST_ATTR [[gnu::const]] #endif #endif #if !defined(__LIBC_CONST_ATTR) && defined(__GNUC__) #if __has_attribute(const) #define __LIBC_CONST_ATTR __attribute__((const)) #endif #endif #if !defined(__LIBC_CONST_ATTR) #define __LIBC_CONST_ATTR #endif ```
2024-02-13[libc] Rework the RPC interface to accept runtime wave sizes (#80914)Joseph Huber1-38/+18
Summary: The RPC interface needs to handle an entire warp or wavefront at once. This is currently done by using a compile time constant indicating the size of the buffer, which right now defaults to some value on the client (GPU) side. However, there are currently attempts to move the `libc` library to a single IR build. This is problematic as the size of the wave fronts changes between ISAs on AMDGPU. The builitin `__builtin_amdgcn_wavefrontsize()` will return the appropriate value, but it is only known at runtime now. In order to support this, this patch restructures the packet. Now instead of having an array of arrays, we simply have a large array of buffers and slice it according to the runtime value if we don't know it ahead of time. This also somewhat has the advantage of making the buffer contiguous within a page now that the header has been moved out of it.
2024-02-08[libc] Only declare float128 math functions in the generated math.h if ↵lntue1-1/+14
float128 type is supported. (#81010)
2024-01-30[libc] Change the starting port index to use the SMID (#79200)Joseph Huber1-1/+1
Summary: The RPC interface uses several ports to provide parallel access. Right now we begin the search at the beginning, which heavily contests the early ports. Using the SMID allows us to stagger the starting index based off of the cluster identifier that is executing the current warp. Multiple warps can share an SM, but it will guaruntee that the contention for the low indices is lower. This also increases the maximum port size to around 4096, this is because 512 isn't enough to cover the full hardare parallelism needed to guarantee this doesdn't deadlock.
2024-01-23[libc][NFC] Remove `FPBits` cast operator (#79142)Guillaume Chatelet1-1/+1
The semantics for casting can range from "bitcast" (same representation) to "different representation", to "type promotion". Here we remove the cast operator and force usage of `get_val` as the only function to get the floating point value, making the intent clearer and more consistent.
2024-01-23[reland][libc] Remove unnecessary `FPBits` functions and properties (#79128)Guillaume Chatelet1-1/+1
- reland #79113 - Fix aarch64 RISC-V build
2024-01-23Revert "[libc] Remove unnecessary `FPBits` functions and properties" (#79118)Guillaume Chatelet1-1/+1
Reverts llvm/llvm-project#79113 It broke aarch64 build bot machines.
2024-01-23[libc] Remove unnecessary `FPBits` functions and properties (#79113)Guillaume Chatelet1-1/+1
This patch reduces the surface of `FPBits`.
2024-01-21[libc] fix unit tests in fullbuild (#78864)Schrodinger ZHU Yifan1-4/+8
fixes https://github.com/llvm/llvm-project/issues/78743 - For normal objects, the patch removes `RTTI` and exceptions in `fullbuild` - For FP tests, the patch adds links to `stdc++` and `gcc_s` if `MPFR` is used.
2024-01-19[libc] Redo the install targets (#78795)Petr Hosek1-1/+1
Prior to this change, we wouldn't build headers that aren't referenced by other parts of the libc which would result in a build error during installation. To address this, we make the header target a dependency of the libc archive. Additionally, we also redo the install targets, moving the install targets closer to build targets and simplifying the hierarchy and generally matching what we do for other runtimes.
2024-01-19[libc] Fix is_subnormal for Intel Extended Precision (#78592)Guillaume Chatelet1-4/+4
Also turn a set of `get_biased_exponent() == 0` into `is_subnormal()` which is clearer.
2024-01-16[libc] Fix libc-hdrgen crosscompiling (#78227)Petr Hosek1-2/+0
The support introduced in 675702f356b0c3a540fa2e8af4192f7d658b2988 is not working correctly in all scenarios. Instead of setup_host_tool function, we can use the existing targets introduced by add_tablegen macro.
2024-01-12[libc] Build native libc-hdrgen when crosscompiling (#77848)Petr Hosek1-0/+2
When crosscompiling tools for a different architecture, we need to build native libc-hdrgen which can be achieved using the existing CMake support for crosscompiling tablegen tools.
2024-01-03[libc][NFC] Remove `FloatProperties` (#76508)Guillaume Chatelet1-1/+1
Access is now done through `FPBits` exclusively. This patch also renames a few internal structs and uses `T` instead of `FP` as a template parameter.
2023-12-28[libc][NFC] Integrate `FloatProperties` into `FPBits` (#76506)Guillaume Chatelet2-2/+0
`FloatProperties` is always included when `FPBits` is. This will help further refactoring.
2023-12-16Use StringRef::{starts,ends}_with (NFC)Kazu Hirata1-3/+3
This patch replaces uses of StringRef::{starts,ends}with with StringRef::{starts,ends}_with for consistency with std::{string,string_view}::{starts,ends}_with in C++20. I'm planning to deprecate and eventually remove StringRef::{starts,ends}with.
2023-12-15[libc][NFC] Rename `MANTISSA_WIDTH` in `FRACTION_LEN` (#75489)Guillaume Chatelet1-16/+13
This one might be a bit controversial since the terminology has been introduced from the start but I think `FRACTION_LEN` is a better name here. AFAICT it really is "the number of bits after the decimal dot when the number is in normal form." `MANTISSA_WIDTH` is less precise as it's unclear whether we take the leading bit into account. This patch also renames most of the properties to use the `_LEN` suffix and fixes useless casts or variables.
2023-12-14[libc][NFC] Remove MantissaWidth traits (#75458)Guillaume Chatelet1-3/+3
Same as #75362, the traits does not bring a lot of value over `FloatProperties::MANTISSA_WIDTH` (or `FPBits::MANTISSA_WIDTH`).
2023-12-11[libc][NFC] Fix mixed up biased/unbiased exponent (#75037)Guillaume Chatelet1-4/+4
According to [wikipedia](https://en.wikipedia.org/wiki/Exponent_bias) the "biased exponent" is the encoded form that is always positive whereas the unbiased form is the actual "real" exponent that can be positive or negative. `FPBits` seems to be using `unbiased_exponent` to describe the encoded form (unsigned). This patch simply use `biased` instead of `unbiased`.
2023-12-01[libc] Allocate fine-grained memory for the RPC host symbolJoseph Huber1-1/+2
Summary: This pointer has been causing issues. Allocating and reading from coarse memory on the CPU is not guaranteed and varies depending on the kernel version and support. Previously we attempted to pin the memory but this caused unexpected failures. This should be a legal operation and work around the problem as fine-grained memory should be always legal to write to by both sides.
2023-12-01Revert "[libc] Explicitly pin memory for the client symbol lookup (#73988)"Joseph Huber1-10/+7
Summary: This caused the bots to begin failing. Revert for now to get the bot green. This reverts commit 8bea804923a1b028e86b177caccb3258708ca01c. This reverts commit e1395c7bdbe74b632ba7fbd90e2be2b4d82ee09e.
2023-12-01[libc] Move the pointer to pin off the stack to the heap (#74118)Joseph Huber1-2/+3
Summary: This may be problematic to pin a stack pointer. Allocate it via the OS allocator instead as the documentation suggests. For some reason, if you attempt to free this pointer after the memory region has been unlocked, it will return an invalid pointer.
2023-11-30[libc] Explicitly pin memory for the client symbol lookup (#73988)Joseph Huber1-7/+9
Summary: Previously, we determined that coarse grained memory cannot be used in the general case. That removed the buffer used to transfer the memory, however we still had this lookup. Though we do not access the symbol directly, it still conflicts with the agents apparently. Pin this as well. This resolves the problems @lntue was having with the `libc` GPU build.
2023-11-30[libc] Explicitly pin memory for the HSA memory transfer (#73973)Joseph Huber1-6/+6
Summary: This portion of code handles mapping the RPC client memory over to the device. HSA copies need to be between two slices of memory that HSA has allocated. Previously we used coarse-grained memory to act as the host source. However, the support for this varies depending on the kernel and version and should not be relied upon. This patch changes that handling to use the `hsa_amd_memory_lock` API to explicitly pin memory to a location sufficient for a DMA transfer to the GPU.
2023-11-27[libc] Use file lock to join newline on RPC puts call (#73373)Joseph Huber1-2/+4
Summary: The puts call appends a newline. With multiple threads, this can be done out of order such that another thread puts something before we finish appending the newline. Add a flockfile and funlockfile to ensure that the whole string is printed before another string can appear.
2023-11-23[libc][NFC] Sink "PlatformDefs.h" into "FloatProperties.h" (#73226)Guillaume Chatelet2-2/+0
`PlatformDefs.h` does not bring a lot of value as a separate file. It is transitively included in `FloatProperties.h` and `FPBits.h`. This patch sinks it into `FloatProperties.h` and removes the associated build targets.
2023-11-21[libc] Update the AMDGPU implementation to use code object 5 (#72580)Joseph Huber1-4/+31
Summary: This patch includes the necessary changes to make the `libc` tests running on AMD GPUs run using the newer code object version. The 'code object version' is AMD's internal ABI for making kernel calls. The move from 4 to 5 changed how we handle arguments for builtins such as obtaining the grid size or setting up the size of the private stack. Fixes: https://github.com/llvm/llvm-project/issues/72517
2023-11-09[libc][fix] Call GPU destructors in the correct orderJoseph Huber1-1/+0
Summary: I was mistakenly iterating the list backwards. Regular semantics puts both arrays in priority order but the destructors are called backwards.
2023-11-06[libc][math] Implement powf function correctly rounded to all rounding ↵lntue2-0/+9
modes. (#71188) We compute `pow(x, y)` using the formula ``` pow(x, y) = x^y = 2^(y * log2(x)) ``` We follow similar steps as in `log2f(x)` and `exp2f(x)`, by breaking down into `hi + mid + lo` parts, in which `hi` parts are computed using the exponent field directly, `mid` parts will use look-up tables, and `lo` parts are approximated by polynomials. We add some speedup for common use-cases: ``` pow(2, y) = exp2(y) pow(10, y) = exp10(y) pow(x, 2) = x * x pow(x, 1/2) = sqrt(x) pow(x, -1/2) = rsqrt(x) - to be added ```
2023-11-01[amdgpu][openmp] Treat missing TIMESTAMP_FREQUENCY as non-fatal (#70987)Jon Chesterfield1-15/+16
If you build with dynamic_hsa, the symbol is known and compilation succeeds. If you then run with a slightly older libhsa, this argument is not recognised and an error returned. I'd rather the program runs with a misleading omp wtime than refuses to run at all.
2023-10-30[libc][Obvious] Fix missing semicolon in AMDGPU loader implementationJoseph Huber1-3/+2
Summary: Title
2023-10-30[amdgpu][openmp] Avoiding writing to packet header twice (#70695)Jon Chesterfield1-7/+8
I think it follows from the HSA spec that a write to the first byte is deemed significant to the GPU in which case writing to the second short and reading back from it later would be safe. However, the examples for this all involve an atomic write to the first 32 bits and it seems a credible risk that the occasional CI errors abound invalid packets have as their root cause that the firmware notices the early write to packet->setup and treats that as a sign that the packet is ready to go. That was overly-paranoid, however in passing noticed the code in libc is genuinely invalid. The memset writes a zero to the header byte, changing it from type_invalid (1) to type_vendor (0), at which point the GPU is free to read the 64 byte packet and interpret it as a vendor packet, which is probably why libc CI periodically errors about invalid packets. Also a drive by change to do the atomic store on a uint32_t consistently. I'm not sure offhand what __atomic_store_n on a uint16_t* and an int resolves to, seems better to be unambiguous there.
2023-10-23Typos: 'maxium', 'minium'Hans Wennborg1-1/+1
2023-10-19[libc] Rework the 'fgets' implementation on the GPU (#69635)Joseph Huber1-0/+16
Summary: The `fgets` function as implemented is not functional currently when called with multiple threads. This is because we rely on reapeatedly polling the character to detect EOF. This doesn't work when there are multiple threads that may with to poll the characters. this patch pulls out the logic into a standalone RPC call to handle this in a single operation such that calling it from multiple threads functions as expected. It also makes it less slow because we no longer make N RPC calls for N characters.
2023-10-19[libc] Fix accidental LIBC_NAMESPACE_clock_freq (#69620)alfredfo1-1/+1
See-also: https://github.com/llvm/llvm-project/pull/69548
2023-10-17[libc] Implement the 'ungetc' function on the GPU (#69248)Joseph Huber1-0/+7
Summary: This function follows closely with the pattern of all the other functions. That is, making a new opcode and forwarding the call to the host. However, this also required modifying the test somewhat. It seems that not all `libc` implementations follow the same error rules as are tested here, and it is not explicit in the standard, so we simply disable these EOF checks when targeting the GPU.
2023-10-04[libc][NFC] Fix -Wdangling-else when compiling libc with gcc >= 7 (#67833)Mikhail R. Gadelha1-8/+16
Explicit braces were added to fix the "suggest explicit braces to avoid ambiguous ‘else’" warning since the current solution (switch (0) case 0: default:) doesn't work since gcc 7 (see https://github.com/google/googletest/issues/1119) gcc 13 generates about 5000 of these warnings when building libc without this patch.
2023-10-02[libc][NFC] Couple of small warning fixes (#67847)Mikhail R. Gadelha1-0/+1
This patch fixes a couple of warnings when compiling with gcc 13: * CPP/type_traits_test.cpp: 'apply' overrides a member function but is not marked 'override' * UnitTest/LibcTest.cpp:98: control reaches end of non-void function * MPFRWrapper/MPFRUtils.cpp:75: control reaches end of non-void function * smoke/FrexpTest.h:92: backslash-newline at end of file * __support/float_to_string.h:118: comparison of unsigned expression in ‘>= 0’ is always true * test/src/__support/CPP/bitset_test.cpp:197: comparison of unsigned expression in ‘>= 0’ is always true --------- Signed-off-by: Mikhail R. Gadelha <mikhail@igalia.com>
2023-09-26[libc] Scan the ports more fairly in the RPC server (#66680)Joseph Huber1-6/+12
Summary: Currently, we use the RPC server to respond to different ports which each contain a request from some client thread wishing to do work on the server. This scan starts at zero and continues until its checked all ports at which point it resets. If we find an active port, we service it and then restart the search. This is bad for two reasons. First, it means that we will always bias the lower ports. If a thread grabs a high port it will be stuck for a very long time until all the other work is done. Second, it means that the `handle_server` function can technically run indefinitely as long as the client is always pushing new work. Because the OpenMP implementation uses the user thread to service the kernel, this means that it could be stalled with another asyncrhonous device's kernels. This patch addresses this by making the server restart at the next port over. This means we will always do a full scan of the ports before quitting.
2023-09-26[libc] Fix RPC server global after mass replace of __llvm_libcJoseph Huber1-1/+1
Summary: This variable needs a reserved name starting with `__`. It was mistakenly changed with a mass replace. It happened to work because the tests still picked up the associated symbol, but it just became a bad name because it's not reserved anymore.
2023-09-26[libc] Implement `fseek`, `fflush`, and `ftell` on the GPU (#67160)Joseph Huber1-0/+20
Summary: This patch adds the necessary entrypoints to handle the `fseek`, `fflush`, and `ftell` functions. These are all very straightfoward, we simply make RPC calls to the associated function on the other end. Implementing it this way allows us to more or less borrow the state of the stream from the server as we intentionally maintain no internal state on the GPU device. However, this does not implement the `errno` functinality so that must be ignored.
2023-09-26[libc] Mass replace enclosing namespace (#67032)Guillaume Chatelet6-27/+30
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-25[libc] Change the `puts` implementation on the GPU (#67189)Joseph Huber1-9/+13
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-21[libc] Fix and simplify the implementation of 'fread' on the GPU (#66948)Joseph Huber1-10/+5
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[libc] Fix Off By One Errors In Printf Long Double (#66957)michaelrj-google1-2/+2
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[libc][NFC] Remove unused function from the RPC serverJoseph Huber1-11/+4
Summary: I missed removing this now-unused function in the previous patch. Remove it to clean up the interface.
2023-09-21[libc] Remove the 'rpc_reset' routine from the RPC implementation (#66700)Joseph Huber5-45/+96
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-20[reland][libc][cmake] Tidy compiler includes (#66783) (#66878)Guillaume Chatelet2-1/+2
This is a reland of #66783 a35a3b75b219247eb9ff6784d1a0fe562f72d415 fixing the benchmark breakage.
2023-09-19Revert "[libc][cmake] Tidy compiler includes (#66783)" (#66822)Guillaume Chatelet2-2/+1
This reverts commit a35a3b75b219247eb9ff6784d1a0fe562f72d415. This broke libc benchmarks.