Skip to content

[clang] Frontend: Destroy compiling compiler instance before read #154455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AnthonyLatsis
Copy link
Contributor

Upstreams swiftlang#10943.

#134887 added a clone for the compiler instance in compileModuleAndReadASTImpl, which would then be destroyed after the corresponding read in the importing instance.

Swift has a SwiftNameLookupExtension module extension which updates (effectively) global state - populating the lookup table for a module on read and removing it when the module is destroyed.

With newly cloned instance, we would then see:

  • Module compiled with cloned instance
  • Module read with importing instance
  • Lookup table for that module added
  • Cloned instance destroyed
  • Module from that cloned instance destroyed
  • Lookup table for that module name removed

Depending on the original semantics is incredibly fragile, but for now it's good enough to ensure that the read in the importing instance is after the cloned instanced is destroyed. Ideally we'd only ever add to the lookup tables in the original importing instance, never its clones.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Aug 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-clang

Author: Anthony Latsis (AnthonyLatsis)

Changes

Upstreams swiftlang#10943.

#134887 added a clone for the compiler instance in compileModuleAndReadASTImpl, which would then be destroyed after the corresponding read in the importing instance.

Swift has a SwiftNameLookupExtension module extension which updates (effectively) global state - populating the lookup table for a module on read and removing it when the module is destroyed.

With newly cloned instance, we would then see:

  • Module compiled with cloned instance
  • Module read with importing instance
  • Lookup table for that module added
  • Cloned instance destroyed
  • Module from that cloned instance destroyed
  • Lookup table for that module name removed

Depending on the original semantics is incredibly fragile, but for now it's good enough to ensure that the read in the importing instance is after the cloned instanced is destroyed. Ideally we'd only ever add to the lookup tables in the original importing instance, never its clones.


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

1 Files Affected:

  • (modified) clang/lib/Frontend/CompilerInstance.cpp (+12-10)
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 9f99edadbb70f..28f65b7f8912f 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -1473,16 +1473,18 @@ static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
                                         SourceLocation ModuleNameLoc,
                                         Module *Module,
                                         StringRef ModuleFileName) {
-  auto Instance = ImportingInstance.cloneForModuleCompile(ModuleNameLoc, Module,
-                                                          ModuleFileName);
-
-  if (!ImportingInstance.compileModule(ModuleNameLoc,
-                                       Module->getTopLevelModuleName(),
-                                       ModuleFileName, *Instance)) {
-    ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
-                                              diag::err_module_not_built)
-        << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
-    return false;
+  {
+    auto Instance = ImportingInstance.cloneForModuleCompile(ModuleNameLoc, Module,
+                                                            ModuleFileName);
+
+    if (!ImportingInstance.compileModule(ModuleNameLoc,
+                                         Module->getTopLevelModuleName(),
+                                         ModuleFileName, *Instance)) {
+      ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
+                                                diag::err_module_not_built)
+          << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
+      return false;
+    }
   }
 
   // The module is built successfully, we can update its timestamp now.

Upstreams #10943.

llvm#134887 added a clone for the
compiler instance in `compileModuleAndReadASTImpl`, which would then be
destroyed *after* the corresponding read in the importing instance.

Swift has a `SwiftNameLookupExtension` module extension which updates
(effectively) global state - populating the lookup table for a
module on read and removing it when the module is destroyed.

With newly cloned instance, we would then see:
  - Module compiled with cloned instance
  - Module read with importing instance
  - Lookup table for that module added
  - Cloned instance destroyed
  - Module from that cloned instance destroyed
  - Lookup table for that module name removed

Depending on the original semantics is incredibly fragile, but for now
it's good enough to ensure that the read in the importing instance is
after the cloned instanced is destroyed. Ideally we'd only ever add to
the lookup tables in the original importing instance, never its clones.
@AnthonyLatsis AnthonyLatsis changed the title [Frontend] Destroy compiling compiler instance before read [clang] Frontend: Destroy compiling compiler instance before read Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants