[PATCH] D126781: [CodeGen] Keep track info of lazy-emitted symbols in ModuleBuilder

Jun Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 6 07:47:57 PDT 2022


junaire updated this revision to Diff 434474.
junaire marked an inline comment as done.
junaire added a comment.

fix a typo


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126781

Files:
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/CodeGen/ModuleBuilder.cpp
  clang/test/Interpreter/execute.cpp


Index: clang/test/Interpreter/execute.cpp
===================================================================
--- clang/test/Interpreter/execute.cpp
+++ clang/test/Interpreter/execute.cpp
@@ -13,4 +13,8 @@
 
 auto r2 = printf("S[f=%f, m=0x%llx]\n", s.f, reinterpret_cast<unsigned long long>(s.m));
 // CHECK-NEXT: S[f=1.000000, m=0x0]
+
+inline int foo() { return 42; }
+int r3 = foo();
+
 quit
Index: clang/lib/CodeGen/ModuleBuilder.cpp
===================================================================
--- clang/lib/CodeGen/ModuleBuilder.cpp
+++ clang/lib/CodeGen/ModuleBuilder.cpp
@@ -133,8 +133,13 @@
     llvm::Module *StartModule(llvm::StringRef ModuleName,
                               llvm::LLVMContext &C) {
       assert(!M && "Replacing existing Module?");
-      M.reset(new llvm::Module(ExpandModuleName(ModuleName, CodeGenOpts), C));
-      Initialize(*Ctx);
+
+      {
+        CodeGenModule::KeepLazyEmiitedSymRAII RAIIKeeper(Builder);
+        M.reset(new llvm::Module(ExpandModuleName(ModuleName, CodeGenOpts), C));
+        Initialize(*Ctx);
+      }
+
       return M.get();
     }
 
Index: clang/lib/CodeGen/CodeGenModule.h
===================================================================
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1477,6 +1477,44 @@
   void printPostfixForExternalizedDecl(llvm::raw_ostream &OS,
                                        const Decl *D) const;
 
+  friend struct KeepLazyEmiitedSymRAII;
+
+  struct KeepLazyEmiitedSymRAII {
+    std::unique_ptr<CodeGenModule> OldBuilder;
+    std::unique_ptr<CodeGenModule> &Self;
+
+    explicit KeepLazyEmiitedSymRAII(std::unique_ptr<CodeGenModule> &Builder)
+        : Self(Builder) {
+      OldBuilder.swap(Self);
+    }
+
+    ~KeepLazyEmiitedSymRAII() {
+      assert(OldBuilder->DeferredDeclsToEmit.empty() &&
+             "Should have emitted all decls deferred to emit.");
+      assert(Self->DeferredDecls.empty() &&
+             "Newly created module should not have deferred decls");
+
+      std::swap(Self->DeferredDecls, OldBuilder->DeferredDecls);
+
+      assert(Self->DeferredVTables.empty() &&
+             "Newly created module should not have deferred vtables");
+
+      std::swap(Self->DeferredVTables, OldBuilder->DeferredVTables);
+
+      assert(Self->MangledDeclNames.empty() &&
+             "Newly created module should not have mangled decl names");
+      assert(Self->Manglings.empty() &&
+             "Newly created module should not have manglings");
+
+      Self->Manglings = std::move(OldBuilder->Manglings);
+
+      assert(OldBuilder->WeakRefReferences.empty() &&
+             "Not all WeakRefRefs have been applied");
+
+      std::swap(Self->TBAA, OldBuilder->TBAA);
+    }
+  };
+
 private:
   llvm::Constant *GetOrCreateLLVMFunction(
       StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126781.434474.patch
Type: text/x-patch
Size: 2889 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220606/f78c1818/attachment.bin>


More information about the cfe-commits mailing list