Skip to content

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.

@AnthonyLatsis AnthonyLatsis changed the title [Frontend] Destroy compiling compiler instance before read [clang] Frontend: Destroy compiling compiler instance before read Aug 20, 2025
Copy link

github-actions bot commented Aug 26, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

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.
@bnbarham bnbarham merged commit 1532116 into llvm:main Aug 29, 2025
9 checks passed
@bnbarham bnbarham deleted the jepa-main3 branch August 29, 2025 23:59
Copy link

@AnthonyLatsis Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

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.

4 participants