[PATCH] D128782: [CodeGen] Keep track of decls that were deferred and have been emitted.

Jun Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 1 05:25:04 PDT 2022


junaire updated this revision to Diff 441667.
junaire added a comment.

- Rebase
- turn clang-format off in tests


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D128782

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/Interpreter/code-undo.cpp


Index: clang/test/Interpreter/code-undo.cpp
===================================================================
--- clang/test/Interpreter/code-undo.cpp
+++ clang/test/Interpreter/code-undo.cpp
@@ -1,3 +1,4 @@
+// clang-format off
 // RUN: clang-repl "int i = 10;" 'extern "C" int printf(const char*,...);' \
 // RUN:            'auto r1 = printf("i = %d\n", i);' | FileCheck --check-prefix=CHECK-DRIVER %s
 // REQUIRES: host-supports-jit
@@ -20,4 +21,9 @@
 auto r3 = printf("foo() = %d\n", foo());
 // CHECK-NEXT: foo() = 2
 
+inline int bar() { return 42;}
+auto r4 = bar();
+%undo
+auto r5 = bar();
+
 %quit
Index: clang/lib/CodeGen/CodeGenModule.h
===================================================================
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -344,6 +344,20 @@
   std::vector<GlobalDecl> DeferredDeclsToEmit;
   void addDeferredDeclToEmit(GlobalDecl GD) {
     DeferredDeclsToEmit.emplace_back(GD);
+    addEmittedDeferredDecl(GD);
+  }
+
+  /// Decls that were DeferredDecls and have now been emitted.
+  llvm::DenseMap<llvm::StringRef, GlobalDecl> EmittedDeferredDecls;
+
+  void addEmittedDeferredDecl(GlobalDecl GD) {
+    if (!llvm::isa<FunctionDecl>(GD.getDecl()))
+      return;
+    llvm::GlobalVariable::LinkageTypes L = getFunctionLinkage(GD);
+    if (llvm::GlobalValue::isLinkOnceLinkage(L) ||
+        llvm::GlobalValue::isWeakLinkage(L)) {
+      EmittedDeferredDecls[getMangledName(GD)] = GD;
+    }
   }
 
   /// List of alias we have emitted. Used to make sure that what they point to
@@ -1516,6 +1530,10 @@
     NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
 
     NewBuilder->TBAA = std::move(TBAA);
+    assert(NewBuilder->EmittedDeferredDecls.empty() &&
+           "Still have (unmerged) EmittedDeferredDecls deferred decls");
+
+    NewBuilder->EmittedDeferredDecls = std::move(EmittedDeferredDecls);
   }
 
 private:
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -445,6 +445,7 @@
 
 void CodeGenModule::clear() {
   DeferredDeclsToEmit.clear();
+  EmittedDeferredDecls.clear();
   if (OpenMPRuntime)
     OpenMPRuntime->clear();
 }
@@ -510,6 +511,9 @@
 
 void CodeGenModule::Release() {
   EmitDeferred();
+  DeferredDecls.insert(EmittedDeferredDecls.begin(),
+                       EmittedDeferredDecls.end());
+  EmittedDeferredDecls.clear();
   EmitVTablesOpportunistically();
   applyGlobalValReplacements();
   applyReplacements();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128782.441667.patch
Type: text/x-patch
Size: 2582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220701/918c07a6/attachment.bin>


More information about the cfe-commits mailing list