llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: SahilPatidar

<details>
<summary>Changes</summary>

Adds CodeGenModule::forgetDecl(GlobalDecl GD), exposed through 
CodeGenerator/ModuleBuilder, to clear cached entries in DeferredDecls and 
Manglings for a declaration.

This is useful for clang-repl, where a failed partial translation unit (PTU) 
may be rolled back and a new definition emitted with the same mangled name. 
Without clearing the old state, CodeGen may reuse stale entries from the 
discarded PTU.

This is also related to #<!-- -->223305 (module contamination after a failed 
template instantiation), where CodeGen holds onto state from a discarded PTU.

---
Full diff: https://github.com/llvm/llvm-project/pull/234493.diff


4 Files Affected:

- (modified) clang/include/clang/CodeGen/ModuleBuilder.h (+5) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+6) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+2) 
- (modified) clang/lib/CodeGen/ModuleBuilder.cpp (+6) 


``````````diff
diff --git a/clang/include/clang/CodeGen/ModuleBuilder.h 
b/clang/include/clang/CodeGen/ModuleBuilder.h
index 99e3bdf013a8e8..672f97bbae6bdb 100644
--- a/clang/include/clang/CodeGen/ModuleBuilder.h
+++ b/clang/include/clang/CodeGen/ModuleBuilder.h
@@ -113,6 +113,11 @@ class CodeGenerator : public ASTConsumer {
   /// Create a new \c llvm::Module after calling HandleTranslationUnit. This
   /// enable codegen in interactive processing environments.
   llvm::Module* StartModule(llvm::StringRef ModuleName, llvm::LLVMContext &C);
+
+  /// Clear any CodeGen state associated with \p GD’s mangled name so that a
+  /// new declaration using the same name is treated as if CodeGen has
+  /// never seen it before.
+  void forgetDecl(GlobalDecl GD);
 };
 
 /// CreateLLVMCodeGen - Create a CodeGenerator instance.
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index a058d1fe7a2a3b..f26c09a9e7bb19 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -8665,6 +8665,12 @@ bool CodeGenModule::lookupRepresentativeDecl(StringRef 
MangledName,
   return true;
 }
 
+void CodeGenModule::forgetDecl(GlobalDecl GD) {
+  StringRef MangledName = getMangledName(GD);
+  DeferredDecls.erase(MangledName);
+  Manglings.erase(MangledName);
+}
+
 /// Emits metadata nodes associating all the global values in the
 /// current module with the Decls they came from.  This is useful for
 /// projects using IR gen as a subroutine.
diff --git a/clang/lib/CodeGen/CodeGenModule.h 
b/clang/lib/CodeGen/CodeGenModule.h
index 9e3f073c20f4a0..60fbc8167a8d7f 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -863,6 +863,8 @@ class CodeGenModule : public CodeGenTypeCache {
   bool lookupRepresentativeDecl(StringRef MangledName,
                                 GlobalDecl &Result) const;
 
+  void forgetDecl(GlobalDecl GD);
+
   llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
     return AtomicSetterHelperFnMap[Ty];
   }
diff --git a/clang/lib/CodeGen/ModuleBuilder.cpp 
b/clang/lib/CodeGen/ModuleBuilder.cpp
index 0b00362487d2af..4be2aac9d6e8e2 100644
--- a/clang/lib/CodeGen/ModuleBuilder.cpp
+++ b/clang/lib/CodeGen/ModuleBuilder.cpp
@@ -155,6 +155,8 @@ namespace {
       return M.get();
     }
 
+    void forgetDecl(GlobalDecl GD) { Builder->forgetDecl(GD); }
+
     void Initialize(ASTContext &Context) override {
       Ctx = &Context;
 
@@ -394,6 +396,10 @@ llvm::Module *CodeGenerator::StartModule(llvm::StringRef 
ModuleName,
   return static_cast<CodeGeneratorImpl*>(this)->StartModule(ModuleName, C);
 }
 
+void CodeGenerator::forgetDecl(GlobalDecl GD) {
+  static_cast<CodeGeneratorImpl *>(this)->forgetDecl(GD);
+}
+
 std::unique_ptr<CodeGenerator>
 clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
                          IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,

``````````

</details>


https://github.com/llvm/llvm-project/pull/234493
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to