[libcxx-commits] [PATCH] D138629: [libc++][math.h] Add double overloads

Evgeny Shulgin via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 25 13:54:24 PST 2022


Izaron added a comment.

I'll take `__builtin_fmax` as an example (but other builtins are fairly similar to it).

In D138629#3949782 <https://reviews.llvm.org/D138629#3949782>, @ldionne wrote:

> 1. Which math functions does Clang define as builtin? Does it define all the overloads, or only the `double` one that's usually coming from the C library?

The canonical builtin versions take `double`s as arguments, but there are builtin versions for other types (float, half, long double, long long double), here is the list of builtins:
https://github.com/llvm/llvm-project/blob/410c1f6269779a01ad24909974eafb0f2e8d8cac/clang/include/clang/Basic/Builtins.def#L262-L266

> 2. What do these builtins do, exactly? I assume they try to do something clever and then fall back to a library-provided implementation (and they assume there's a suitable C-linkage definition somewhere)?

If we are inside a constexpr expression, some builtins can be calculated there, in this case Clang emulates the builtin's logic:
https://github.com/llvm/llvm-project/blob/e672f5126fcfca650534ee5fd81425df36c76eb6/clang/lib/AST/ExprConstant.cpp#L14060-L14076

If we are inside a runtime-calculated expression, then builtins are mapped to some other code. For example, `__builtin_fmax` is mapped into calling the `llvm.maxnum.f64` intrinsic:
godbolt - https://godbolt.org/z/fboz3Phf8
intrinsics docs - https://releases.llvm.org/3.6.1/docs/LangRef.html#llvm-maxnum-intrinsic

> 4. More generally speaking, what's the purpose of -fno-builtins?

This is a loanword from GCC. GCC artificially treats some functions (`alloca`, `cos`, `sin`, ...) as builtins, because this way it might generate more effective code instead calling these functions directly.
>From docs: https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html

> for instance, calls to `alloca` may become single instructions which adjust the stack directly, and calls to memcpy may become inline copy loops. The resulting code is often both smaller and faster, but since the function calls no longer appear as such, you cannot set a breakpoint on those calls, nor can you change the behavior of the functions by linking with a different library

The flag disables this behaviour.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138629/new/

https://reviews.llvm.org/D138629



More information about the libcxx-commits mailing list