[PATCH] D126781: [CodeGen] Correctly handle weak symbols in the codegen

Jun Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 3 00:23:48 PDT 2022


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

Update commit message


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,37 @@
     llvm::Module *StartModule(llvm::StringRef ModuleName,
                               llvm::LLVMContext &C) {
       assert(!M && "Replacing existing Module?");
+
+      std::unique_ptr<CodeGen::CodeGenModule> OldBuilder;
+      OldBuilder.swap(Builder);
+
       M.reset(new llvm::Module(ExpandModuleName(ModuleName, CodeGenOpts), C));
       Initialize(*Ctx);
+
+      assert(OldBuilder->getDeferredDeclsToEmit().empty() &&
+             "Should have emitted all decls deferred to emit.");
+      assert(Builder->getDeferredDecls().empty() &&
+             "Newly created module should not have deferred decls");
+
+      Builder->getDeferredDecls().swap(OldBuilder->getDeferredDecls());
+
+      assert(Builder->getDeferredVTables().empty() &&
+             "Newly created module should not have deferred vtables");
+
+      Builder->getDeferredVTables().swap(OldBuilder->getDeferredVTables());
+
+      assert(Builder->getMangledDeclNames().empty() &&
+             "Newly created module should not have mangled decl names");
+      assert(Builder->getManglings().empty() &&
+             "Newly created module should not have manglings");
+
+      Builder->getManglings() = std::move(OldBuilder->getManglings());
+
+      assert(OldBuilder->getWeakRefReferences().empty() &&
+             "Not all WeakRefRefs have been applied");
+
+      Builder->getTBAA().swap(OldBuilder->getTBAA());
+
       return M.get();
     }
 
Index: clang/lib/CodeGen/CodeGenModule.h
===================================================================
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1477,6 +1477,36 @@
   void printPostfixForExternalizedDecl(llvm::raw_ostream &OS,
                                        const Decl *D) const;
 
+  llvm::SmallPtrSetImpl<clang::Module *> &getEmittedDeferredDecls() {
+    return EmittedModuleInitializers;
+  }
+
+  llvm::DenseMap<StringRef, GlobalDecl> &getDeferredDecls() {
+    return DeferredDecls;
+  }
+
+  std::vector<GlobalDecl> &getDeferredDeclsToEmit() {
+    return DeferredDeclsToEmit;
+  }
+
+  std::vector<const CXXRecordDecl *> &getDeferredVTables() {
+    return DeferredVTables;
+  }
+
+  llvm::MapVector<GlobalDecl, StringRef> &getMangledDeclNames() {
+    return MangledDeclNames;
+  }
+
+  llvm::StringMap<GlobalDecl, llvm::BumpPtrAllocator> &getManglings() {
+    return Manglings;
+  }
+
+  llvm::SmallPtrSetImpl<llvm::GlobalValue *> &getWeakRefReferences() {
+    return WeakRefReferences;
+  }
+
+  std::unique_ptr<CodeGenTBAA> &getTBAA() { return TBAA; }
+
 private:
   llvm::Constant *GetOrCreateLLVMFunction(
       StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,


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


More information about the cfe-commits mailing list