<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61425>61425</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[C++20] [constexpr] Should we cache the result of constexpr/consteval expression?
</td>
</tr>
<tr>
<th>Labels</th>
<td>
c++20,
consteval
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ChuanqiXu9
</td>
</tr>
</table>
<pre>
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;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl1v6jgT_jXmZkSUOCSBCy6AgF6kvqdHp12pd0eOMyHedewcf7Tl36-cQKFd2t2VFiHHHk-e-fDM4zBrxUEhLkm2Jlk5Yd612iw3rWfql3jyi0ml6-NyD432qgbXCgtaIby0qGAPQj2jdeLAHIJ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