[PATCH] D156453: [clang][Interp] Create only globals when initializing a global variable

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 13 07:46:44 PDT 2023


aaron.ballman added a comment.

In D156453#4652939 <https://reviews.llvm.org/D156453#4652939>, @tbaeder wrote:

> It gets interpreted as a constant expression in `Sema::CheckCompleteVariableInitialization()`:
>
>   * #0: Context.cpp:73  clang::interp::Context::evaluateAsInitializer(this=0x0000608000005d20, Parent=0x00007fff6dc2a8d0, VD=0x0000621000073b48, Result=0x0000621000099e90)
>     #1: ExprConstant.cpp:15600  clang::Expr::EvaluateAsInitializer(this=0x0000621000099e40, Value=0x0000621000099e90, Ctx=0x000062a000000200, VD=0x0000621000073b48, Notes=0x00007fff6deac850, IsConstantInitialization=true) const
>     #2: Decl.cpp:2555  clang::VarDecl::evaluateValueImpl(this=0x0000621000073b48, Notes=0x00007fff6deac850, IsConstantInitialization=true) const
>     #3: Decl.cpp:2626  clang::VarDecl::checkForConstantInitialization(this=0x0000621000073b48, Notes=0x00007fff6deac850) const
>     #4: SemaDecl.cpp:14399  clang::Sema::CheckCompleteVariableDeclaration(this=0x000062900000a200, var=0x0000621000073b48)

Ahh, we are doing this for static initialization cases with thread locals (https://godbolt.org/z/73dMx55xq), okay, that makes more sense now. Thank you!



================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:1213
   std::optional<PrimType> SubExprT = classify(SubExpr);
-  if (E->getStorageDuration() == SD_Static) {
+  bool IsStatic = E->getStorageDuration() == SD_Static;
+  if (GlobalDecl || IsStatic) {
----------------
Should we be looking at the TLS kind of the extended declaration? (`CheckCompleteVariableDeclaration()` is using `VarDecl::getTLSKind() == VarDecl::TLS_Static`)

Would something along these lines work instead?
```
bool EmitGlobalTemp = E->getStorageDuration() == SD_Static;
if (!EmitGlobalTemp) {
  if (const LifetimeExtendedTemporaryDecl *LETD = E->getLifetimeExtendedTemporaryDecl()) {
    if (const auto *VD = dyn_cast_if_present<VarDecl>(LETD->getExtendingDecl()) {
      EmitGlobalTemp= VD->getTLSKind() == VarDecl::TLS_Static;
    }
  }
}
```


================
Comment at: clang/test/AST/Interp/records.cpp:931
+  /// Not constexpr!
+  O o1(0);
 }
----------------
It'd be nice to add another test for:
```
constinit O o2(0); // error: can't bind to temporary
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156453/new/

https://reviews.llvm.org/D156453



More information about the cfe-commits mailing list