Background
compiler-rt builtins provide essential floating-point computation routines to support runtime execution when the underlying hardware does not support specific floating-point types natively. Unfortunately, the current implementations are under tested, leaving subtle numerical bugs lying dormant for years (see for instance PR#119449). Additionally, they are written in a mix of C and hand-written assembly, which substantially increases the maintenance burden.
On the other hand, LLVM libc implements these basic floating-point operations with rigorous and extensive testing. A recent effort (see also Issue #147386) has refactored LLVM libc math routines to be free-standing and header-only. This allows other LLVM projects to consume LLVM libc logic directly via header inclusions without introducing complex build and link dependencies.
We would like to explore the feasibilities of replacing the compiler-rt floating point builtins with LLVM libc’s equivalents (either generic C versions or specialized ASM versions, or both).
Proposed Plan
- Ensure basic floating-point operations in LLVM libc are exposed as shared, free-standing headers suitable for inclusion in compiler-rt.
- Add C++ generic implementations of compiler-rt builtins such as
__addtf3that simply include the corresponding LLVM libc shared headers and call equivalent functions. - Add a CMake configuration flag like
COMPILER_RT_USE_LIBC_MATHand put all the C++ generic implementations behind this flag. - Optimize the code size and performance, especially for embedded targets.
Other Considerations
- By putting everything behind a CMake configuration flag, we would like to make sure that this exploration is completely transparent and non-disruptive to all the current usage of compiler-rt. It will also help us to spin up build bots and ease the future transitions if it is proved to work well.
- It is then possible to easily extend support for other floating point math features in compiler-rt builtins in coordination with libc, such as emulating floating point exceptions or rounding modes, without direct or circular dependencies between the projects.
Please let me know if you have any feedback or objections. Thanks!
@compnerd @scanon @statham-arm @petrhosek