Skip to content

Clang should provide builtin that lowers directly to llvm.fma.* intrinsic #126025

Open
@petrhosek

Description

@petrhosek

Clang already provides the __builtin_fma builtin intrinsic which is lowered either to fma call or llvm.fma.* Intrinsic depending on whether math errno is used or not (i.e. -f[no-]math-errno). This behavior makes [__builtin_fma unsuitable for implementing libc fma because unless libc is compiled with -fno-math-errno, on targets where math errno is used, this would result in an infinite recursion.

We encountered this issue in LLVM libc:

template <> LIBC_INLINE float fma(float x, float y, float z) {
return __builtin_fmaf(x, y, z);
}
template <> LIBC_INLINE double fma(double x, double y, double z) {
return __builtin_fma(x, y, z);
}

On baremetal targets where math errno is used, the __builtin_fma call is lowered to fma call resulting a function trivially calling itself. The current workaround is to compile LLVM libc with -fno-math-errno, but that is generally undesirable.

Ideally, Clang would provide a separate builtin intrinsic that always lowers to llvm.fma.* Intrinsic regardless of math errno behavior. We could then use this intrinsic to implement fma.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clangClang issues not falling into any other categoryfloating-pointFloating-point math

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions