-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Cannot use operator<=>
in __builtin_assume
#55636
Comments
@llvm/issue-subscribers-clang-frontend |
__builtin_assume would be a lot more useful in general if it could handle function calls for inlineable functions which are not manually declared pure and if it did not throw away entire compound expressions as soon as it can't reason about any part of them. As-is, it cannot be used with most expressions involving library types, including standard library types, which for the most part are not annotated with [[gnu::pure]] or [[gnu::const]] because doing that for functions with an inline definition is silly. Ideally But then again, __builtin_assume easily crashes clang which nobody has bothered to fix for two years:
¯\_(ツ)_/¯ |
is anyone working on this currently? It seems like the referenced issue #45902 has been fixed |
@ldionne Is that something you want to look at on the library side? |
As stated by @davidstone, libc++ annotating its code won't be of much help. I believe it's a clang-frontend issue and the frontend should try harder to prove that a function contains no side-effects. I believe the following example shows that the frontend is unable to prove constness of simple const functions. without const (https://clang.godbolt.org/z/Ea3xfPvaq): inline int square(int a) {
return a*a;
}
int foo(int x) {
#ifdef __clang__
__builtin_assume(square(x) == 16);
#else
__attribute__((assume(square(x) == 16)));
#endif
return square(x);
} warns about
with const (https://clang.godbolt.org/z/9Wsn7c3Kb): __attribute__((const)) inline int square(int a) {
return a*a;
}
int foo(int x) {
#ifdef __clang__
__builtin_assume(square(x) == 16);
#else
__attribute__((assume(square(x) == 16)));
#endif
return square(x);
} no warning, and clang is able to use the assumption. |
@cor3ntin Because of the reasons stated above, I don't think it makes a lot of sense to try to bandage this from within libc++, we need a compiler solution here. |
Clang never tries to infer pureness automatically. So calling a function without one of the pure/const attribute always has side effects GCC will suggest a pure attribute but AFAIK, they don't infere pureness either If the goal is to make |
Building the project with `Clang` generates the following warning: ``` the argument to '__builtin_assume' has side effects that will be discarded [-Wassume] ``` Seems like Clang doesn't handle the hint well unless we pass a const/pure attribute to the function call inside the assume. See: llvm/llvm-project#55636 and llvm/llvm-project#93077 With this change performance for the fn fused nabla examples is significantly improved on clang-cuda and is now much faster than (our old) nvcc, which doesn't have `assume` support.
Building the project with `Clang` generates the following warning: ``` the argument to '__builtin_assume' has side effects that will be discarded [-Wassume] ``` Seems like Clang doesn't handle the hint well unless we pass a const/pure attribute to the function call inside the assume. See: llvm/llvm-project#55636 and llvm/llvm-project#93077 With this change performance for the fn fused nabla examples is significantly improved on clang-cuda and is now much faster than (our old) nvcc, which doesn't have `assume` support.
The following translation unit
causes clang to warn
See it live: https://godbolt.org/z/docxWvcM6
This in turn causes
__builtin_assume(x <= x);
to also fail to be assumed.I suspect this is because the comparison categories do not have their functions marked
[[gnu::const]]
. However, one of the functions that would need to be so marked is the helper type that's constructible from a literal 0, and it's unclear whether you are allowed to mark constructorsgnu::const
orgnu::pure
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51971). Furthermore, libc++ annotating its code won't help if the user is using libstdc++ or MSVC's standard library.You can also get the same behavior with no user types defined at all:
Warns about
See it live: https://godbolt.org/z/EseTasTY3
The text was updated successfully, but these errors were encountered: