-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LLVM][ConstantFolding] missed fold for libcalls with long double constants #19916
Comments
Please provide your command-lines for both compilers. |
clang++ -O3 -std=c++11 random.cpp and g++ -O3 -std=c++11 random.cpp |
https://compiler-explorer.com/z/v4rjbzd8T GCC 13: float_mt_64: 100ms so |
The IR here is full of %32 = tail call noundef x86_fp80 @logl(x86_fp80 noundef 0xK401F8000000000000000) #8
%33 = tail call noundef x86_fp80 @logl(x86_fp80 noundef 0xK40008000000000000000) #8 are we failing to fold these constants? |
A simpler reproducer: https://compiler-explorer.com/z/b4hjW7x5s (not sure that's the only issue here) |
If we just call |
|
Constant-folding long double math is hard because we currently use the host libc to fold transcendental functions... and which forms of "long double" libc supports depends on the host. That said, there's ongoing work; see #90611. Eventually, I think we want to add native support to APFloat for these operations, but correctly rounded floating-point ops are hard to write, so nobody has attempted it. That said, since llvm-libc needs them anyway, maybe we can reuse that code once it's written. |
|
Seems like similar issues exist wrt |
From the code reuse side it should be doable eventually. I'm currently working on Project Hand in Hand to share code between libc and libc++. Once we get that working we can look into expanding it to more projects inside of LLVM. As for the long double functions, I don't think they're currently a priority. There's been a good amount of work done on float128 functions, but the special intel 80 bit long double versions will require special handling for correct rounding. |
Constant folding of libm functions is controversial (cf. https://discourse.llvm.org/t/fp-constant-folding-of-floating-point-operations/73138), although I don't think we've reached a consensus other than "it's sometimes acceptable and sometimes not acceptable". (As a side note, using |
Perhaps worth filing a bug report to libstdc++ (for the original issue) noting the current issue in LLVM. |
There does seem to be some precedent for libstdc++ being willing to use double over long double here for speed: gcc-mirror/gcc@60d9f25 |
Question: 🙋 , I know it's nice for APFloat to be able to handle Arbitrary Precision, but surely if we knew the constant was a float with the same precision as the platform's |
Yes, but you need a bunch of complicated ifdefs to make sure you don't break cross-compiling. This is what #90611 does. |
Also, just invoking the host's logl isn't feasible for some uses. For example, for C++ constexpr math functions, if we don't consistently evaluate math functions to the same result, we break ODR, and the program has undefined behavior. |
Also, there's no guarantee that the host math function is the same as the function used by the runtime implementation. Especially in cross-compilation scenarios. (Also also, while I would hope that no one who compiles llvm/clang is doing so with |
I imagine that GCC does exactly that; assume the host math function IS used by the runtime implementation? |
gcc uses MPFR (which always produces correctly rounded results). |
Extended Description
When using mt19937 or mt19937_64 in combination with a uniform_real_distribution, the performance is about an order of magnitude worse than gcc's. But it only happens with that particular combination. Those generators are fine with uniform_int_distribution, and another generator like default_random_engine works fine with uniform_real_distribution. A program to reproduce this and my results:
The text was updated successfully, but these errors were encountered: