| Issue |
61425
|
| Summary |
[C++20] [constexpr] Should we cache the result of constexpr/consteval _expression_?
|
| Labels |
c++20,
consteval
|
| Assignees |
|
| Reporter |
ChuanqiXu9
|
I found this one when I investigate the building time in modules.
The reproducer:
```
// a.cpp
constexpr bool f() {
for (unsigned n = 0; n != 1'000'000; ++n) {
}
return true;
}
template<typename>
struct s {
static constexpr auto a = f();
#ifndef NO_MULTIPLE_CALL
static constexpr auto b = f();
static constexpr auto c = f();
static constexpr auto d = f();
#endif
};
template struct s<int>;
template struct s<long>;
```
Let's run it with:
```
set x
echo "Build time for duplicated"
time clang++ -std=c++20 a.cpp -c -o a.o
echo "Build time for not duplicated"
time clang++ -std=c++20 a.cpp -c -o a.o -DNO_MULTIPLE_CALL
```
The result in my machine shows:
```
Build time for duplicated
real 0m11.753s
user 0m11.611s
sys 0m0.016s
Build time for not duplicated
real 0m3.631s
user 0m3.576s
sys 0m0.013s
```
So now clang doesn't cache the result of constexpr/consteval clearly. This is significant when we try to use some metaprogramming techniques. Is there any special reason? If not, I guess we may solve the problem by something like:
```
ExprResult ASTContext::EvaluateXXX(Expr *E) {
ODRHash Hash;
llvm::FoldingSetNodeID &ID;
E->ProcessODRHash(ID, Hash);
ExprResult Result = EvaluteCache.FindNodeOrInsert(Hash);
if (!Result) {
Evalute it actually...
}
return Result;
}
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs