-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[C++20] Unintuitive compile times when caching the result of constexpr/consteval in modules #62796
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
Comments
@llvm/issue-subscribers-clang-modules |
(I didn't look into the details carefully) What is the compiler version do you use? Since recently we have a fix for a similar issue. See #61226 |
Here's my clang++ --version:
I have had the issue last friday or saturday, if that helps. I didn't find the time to report until yesterday. Unfortunately I haven't tried this approach before that day. Just in case I updated to
The issue remains, however now I get the warning On a side note: Typo |
Got it. I'll try to take a look.
This is a warning for build systems. If you're interested, you can take a look at #62707 and #62707. For personal uses, I think you can turn off the warning simply. |
This should be a compiler defect. I'll try to fix this. |
Thanks for the quick fix! Unfortunately, after updating to |
I checked this with non-modular codes. And it shows this issue exists with non-modules code. I've filed an issue for this: #62947 |
Close llvm/llvm-project#62796. Previously, we didn't serialize the evaluated result for VarDecl. This caused the compilation of template metaprogramming become slower than expect. This patch fixes the issue.
…r VarDecl Close llvm/llvm-project#62796. Previously, we didn't serialize the evaluated result for VarDecl. This caused the compilation of template metaprogramming become slower than expect. This patch fixes the issue. This is a recommit tested with asan built clang.
This may or may not be related to #61425.
I have some meta-programming facilities that do the following: A templated
constexpr
variable calls a yet to be defined function via ADL and caches the result. In separate modules, these functions are defined for 1 concrete argument. There is a second templated argument which can change how the result is computed from previous results. These functions may depend on the result of other computations. In each module, the result for the Default Strategy is pre-computed. To illustrate this, I've substituted it with the Fibonacci sequence, going from 30 to 32 to get noticable compile times.Cache.cppm
:Fib0.cppm
:Fib1.cppm
:Fib2.cppm
:Main.cpp
:Compiled with the following commands on a recent Clang 17 build:
Now what I would expect is that in
Fib0
the cache for 30 gets computed and inFib1
the cache for 31 gets computed, each using the default strategy. The results get re-used inFib2
and the result ofFib2
gets re-used inMain
. What ends up happening is that inFib2
both the cache for 30 and 31 are computed once again and inMain
, the result ofFib2
gets re-computed once again.Case 1: All
constexpr
Fib0: 4.6 sec
Fib1: 7.3 sec
Fib2: 11.8 sec
Main: 11.9 sec
However, if we substitute some
consteval
forconstexpr
it gets more unintuitive:Case 2: Making
Recursive
consteval
Fib0: 9.2 sec
Fib1: 14.7 sec
Fib2: 0.02 sec
Main: 0.02 sec
Case 3: Making
DefaultStrategy::operator()
consteval
:Fib0: 4.6 sec
Fib1: 7.4 sec
Fib2: 0.02 sec
Main: 0.02 sec
(This is what I expected in every case)
Case 4: Making
Recursive
andDefaultStrategy::operator()
consteval
Fib0: 9.2 sec
Fib1: 14.9 sec
Fib2: 0.02 sec
Main: 0.02 sec
Case 5: Making each
Compute
consteval
Fib0: 9.1 sec
Fib1: 14.5 sec
Fib2: 0.02 sec
Main: 0.02 sec
Case 6: Making
Recursive
and eachCompute
consteval
Fib0: 9.2 sec
Fib1: 14.6 sec
Fib2: 0.02 sec
Main: 0.02 sec
Case 7: Making
DefaultStrategy::operator()
and eachCompute
consteval
Fib0: 9.1 sec
Fib1: 14.8 sec
Fib2: 0.02 sec
Main: 0.02 sec
Case 8: Making all
consteval
Fib0: 9.0 sec
Fib1: 14.8 sec
Fib2: 0.02 sec
Main: 0.02 sec
What makes this so confusing is that replacing
constexpr
withconsteval
can be EITHER an optimization OR a pessimization, and I cannot derive a good rule from this. Ideally, I would like all cases to behave like Case 3, which has by far the lowest compilation time. If for some reason, the above behaviour is working as intended, I would appreciate some guiding rules when consteval is beneficial or detrimental to compile times.The text was updated successfully, but these errors were encountered: