Skip to content

[Frontend] Destroy compiling compiler instance before read #10943

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

Merged
merged 1 commit into from
Jul 3, 2025

Conversation

bnbarham
Copy link

@bnbarham bnbarham commented Jul 2, 2025

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.

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
Copy link
Author

bnbarham commented Jul 2, 2025

Just on stable/20250601 for now as I'm unsure this is actually the change we want upstream

@bnbarham
Copy link
Author

bnbarham commented Jul 2, 2025

@swift-ci please test llvm

@bnbarham
Copy link
Author

bnbarham commented Jul 2, 2025

@swift-ci please test

Copy link

@jansvoboda11 jansvoboda11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me. Can you clarify what acts as the effectively global state in SwiftNameLookupExtension ?

@bnbarham
Copy link
Author

bnbarham commented Jul 2, 2025

Can you clarify what acts as the effectively global state in SwiftNameLookupExtension ?

SwiftNameLookupExtension::createExtensionReader adds to lookupTables (whose lifetime is that of the ClangImporter) after reading the module. It also passes a OnRemove (to remove the module from the lookup table) down to SwiftLookupTableReader::create, which is called on ~SwiftLookupTableReader.

And readers have the lifetime of the module they were created for:

      if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
                                                             F, Stream)) {
        F.ExtensionReaders.push_back(std::move(Reader));
      }

@bnbarham
Copy link
Author

bnbarham commented Jul 2, 2025

swiftlang/swift#82708

@swift-ci please test

@bnbarham
Copy link
Author

bnbarham commented Jul 3, 2025

Ah, that branch was main :( I'm just going to merge, tested it locally.

@bnbarham bnbarham merged commit 632b65d into swiftlang:stable/20250601 Jul 3, 2025
2 of 5 checks passed
@bnbarham bnbarham deleted the instance-lifetime branch July 3, 2025 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants