[clang][Interp] Don't create global variables more than once
authorTimm Bäder <tbaeder@redhat.com>
Fri, 27 Jan 2023 08:32:20 +0000 (09:32 +0100)
committerTimm Bäder <tbaeder@redhat.com>
Thu, 13 Apr 2023 13:41:43 +0000 (15:41 +0200)
just because we're being told to evaluate it twice. This sometimes
happens when a variable is evaluated again during codegen.

Differential Revision: https://reviews.llvm.org/D147535

clang/lib/AST/Interp/ByteCodeExprGen.cpp

index 6ced8ca..a8e8b29 100644 (file)
@@ -1562,7 +1562,11 @@ bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
   std::optional<PrimType> VarT = classify(VD->getType());
 
   if (shouldBeGloballyIndexed(VD)) {
-    std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(VD, Init);
+    // We've already seen and initialized this global.
+    if (P.getGlobal(VD))
+      return true;
+
+    std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
 
     if (!GlobalIndex)
       return this->bail(VD);