This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Support constexpr for builtin fmax, fmin, ilogb, logb, scalbn
AbandonedPublic

Authored by Izaron on Sep 18 2022, 7:28 AM.

Details

Summary

Support constexpr versions of builtin_fmax, builtin_fmin,
builtin_ilogb, builtin_logb, __builtin_scalbn and their
variations.

Diff Detail

Event Timeline

Izaron created this revision.Sep 18 2022, 7:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 18 2022, 7:28 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
Izaron requested review of this revision.Sep 18 2022, 7:28 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptSep 18 2022, 7:28 AM

libc++ doesn't support constexpr-related patches from C++20 in <complex>: see issue https://github.com/llvm/llvm-project/issues/55370

I reviewed the code in <complex> and found out that since we use a dozen of math functions, we need to support more constexpr builtin math function.

I implemented constexpr versions for five functions. This will be enough to implement a 2017 paper: https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0415r1.html (in a new pull request)

(Also there is a 2019 paper with more constexpr functions for <complex>: https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2019/p1383r0.pdf)

Also builtin implementations will be useful for new C++23 constexpr functions: for example https://en.cppreference.com/w/cpp/numeric/math/scalbn

Izaron added inline comments.Sep 18 2022, 7:35 AM
clang/test/Sema/constant-builtins-math.cpp
17–18

runtime version of __builtin_ilogb/std::ilogb returns INT_MIN (-2147483647) on this argument, while constexpr version returns INT_MIN+1 🤔

This constant is here:
https://llvm.org/doxygen/APFloat_8cpp_source.html#l04187
https://llvm.org/doxygen/APFloat_8h_source.html#l00229
(I guess we shouldn't change this)

17–18

quick fix: INT_MIN is -2147483648, INT_MIN+1 is 2147483647

shafik added a subscriber: shafik.Sep 18 2022, 2:58 PM
shafik added inline comments.
llvm/lib/Support/APFloat.cpp
4206

This is the format as documented by clang-tidy bugprone argument comment see here: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/argument-comment.html

IIRC it depends on strict mode but I would just avoid the whitespace.

Izaron updated this revision to Diff 461312.Sep 19 2022, 12:39 PM

Removed whitespaces in comments.

P.S. I wonder if smaller review requests will make things better? Say a review request for fmax+fmin, then a new one for ilogb+logb, then for scalbn.
This may lead to a more understandable and robust review process =)

These patches are hard to review, yes; the reviewer has to go through and verify for each function that the substitutes you've written actually match the C library semantics. (It's very easy to make mistakes with that sort of thing.)

On that front, I'm pretty sure the NaN handling in your fmin/fmax implementations are wrong.

Izaron abandoned this revision.Sep 20 2022, 2:08 AM

These patches are hard to review, yes; the reviewer has to go through and verify for each function that the substitutes you've written actually match the C library semantics. (It's very easy to make mistakes with that sort of thing.)

On that front, I'm pretty sure the NaN handling in your fmin/fmax implementations are wrong.

Thanks! I'm abandoning this patch, will make new smaller but better patches.

each function that the substitutes you've written actually match the C library semantics

I think this can be verified with libc++/libc tests that would substitute either std::fmax or (constant) __builtin_fmax on same checks and assert that they behave in the same way.