[clang] 02e6ae3 - [clang][bytecode] Lazily evaluate static variables (#208794)

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 19 07:23:24 PDT 2026


Author: Timm Baeder
Date: 2026-09-19T16:23:18+02:00
New Revision: 02e6ae3a2812d6dd3707f8818cbf124d0a3ee0df

URL: https://github.com/llvm/llvm-project/commit/02e6ae3a2812d6dd3707f8818cbf124d0a3ee0df
DIFF: https://github.com/llvm/llvm-project/commit/02e6ae3a2812d6dd3707f8818cbf124d0a3ee0df.diff

LOG: [clang][bytecode] Lazily evaluate static variables (#208794)

They usually go through `evaluateAsInitializer()`, which is where we
register them as global variables. But if they're imported, they don't.
Just try to evaluate them in `VisitMemberExpr()`.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/test/ASTMerge/class-template-partial-spec/test.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index d7bdb2f217a9a..a2a3516ac3d78 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2920,8 +2920,12 @@ bool Compiler<Emitter>::VisitMemberExpr(const MemberExpr *E) {
     return this->discard(Base);
 
   if (const auto *VD = dyn_cast<VarDecl>(Member)) {
-    // I am almost confident in saying that a var decl must be static
-    // and therefore registered as a global variable.
+    // If the member is a VarDecl, this is a static variable.
+    // We need to try to lazily evaluate its initializer here since the
+    // variable might've been deserialized and not registered
+    // as a global variable yet.
+    if (VD->getInit() && !VD->getInit()->isValueDependent())
+      VD->evaluateValue();
     if (auto GlobalIndex = P.getGlobal(VD)) {
       if (!this->emitGetPtrGlobal(*GlobalIndex, E))
         return false;

diff  --git a/clang/test/ASTMerge/class-template-partial-spec/test.cpp b/clang/test/ASTMerge/class-template-partial-spec/test.cpp
index 6ab5ed5a7d5fb..6f2191d048455 100644
--- a/clang/test/ASTMerge/class-template-partial-spec/test.cpp
+++ b/clang/test/ASTMerge/class-template-partial-spec/test.cpp
@@ -2,6 +2,10 @@
 // RUN: %clang_cc1 -emit-pch -std=c++1z -o %t.2.ast %S/Inputs/class-template-partial-spec2.cpp
 // RUN: %clang_cc1 -std=c++1z -ast-merge %t.1.ast -ast-merge %t.2.ast -fsyntax-only %s 2>&1 | FileCheck %s
 
+// RUN: %clang_cc1 -emit-pch -std=c++1z -o %t.1.ast %S/Inputs/class-template-partial-spec1.cpp -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -emit-pch -std=c++1z -o %t.2.ast %S/Inputs/class-template-partial-spec2.cpp -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -std=c++1z -ast-merge %t.1.ast -ast-merge %t.2.ast -fsyntax-only %s 2>&1 -fexperimental-new-constant-interpreter | FileCheck %s
+
 static_assert(sizeof(**SingleSource.member) == sizeof(**SingleDest.member));
 static_assert(sizeof(SecondDoubleSource.member) == sizeof(SecondDoubleDest.member));
 static_assert(NumberSource.val == 42);


        


More information about the cfe-commits mailing list