[PATCH] D39204: [CodeGen] __builtin_sqrt should map to the compiler's intrinsic sqrt function

Sanjay Patel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 24 09:25:15 PDT 2017


spatel added a comment.

In https://reviews.llvm.org/D39204#904312, @efriedma wrote:

> The gcc documentation says "GCC includes built-in versions of many of the functions in the standard C library. These functions come in two forms: one whose names start with the `__builtin_` prefix, and the other without. Both forms have the same type (including prototype), the same address (when their address is taken), and the same meaning as the C library functions".  And gcc specifically preserves the stated semantics.  Given that, I'm not sure it makes sense for us to try to redefine `__builtin_sqrt()` just because it's convenient.
>
> Note that this reasoning only applies if the user hasn't specified any fast-math flags; under -ffinite-math-only, we can assume the result isn't a NaN, and therefore we can use `llvm.sqrt.*`. (The definition of `llvm.sqrt.*` changed in https://reviews.llvm.org/D28797; I don't think we ever updated clang to take advantage of this).
>
> If we really need a name for the never-sets-errno sqrt, we should probably use a different name, e.g. `__builtin_ieee_sqrt()`.


Thanks for the explanation and link. Let me know if I've gone wrong:

1. We don't want to convert clang math builtins to llvm intrinsics because builtins are supposed to be exactly equivalent to C library functions (including setting errno).
2. LLVM intrinsics should be equivalent to C library functions except that they don't set errno (but this is currently wrong in some cases, and https://reviews.llvm.org/D28335 would fix that).
3. Therefore, the existing code in this file that is converting 'pow' and other builtin calls to intrinsics is correct for now, but only because 2 wrongs made it right? :)


https://reviews.llvm.org/D39204





More information about the cfe-commits mailing list