Skip to content

[scanner] setup the clang importer injected redirecting overlay vfs f… #78184

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

hyp
Copy link
Contributor

@hyp hyp commented Dec 14, 2024

…or correct clang dependency scanning

This makes its possible to use -explicit-module-build to correctly find 'crt' swift module on windows

@hyp
Copy link
Contributor Author

hyp commented Dec 14, 2024

@swift-ci please test

Copy link
Contributor

@cachemeifyoucan cachemeifyoucan left a comment

Choose a reason for hiding this comment

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

Can you be more specific about what problem is being fixed here. I see you added SilentClangImporterOverlayWrapperFS to avoid assertions in clang dependency scanner, is that the problem? But I don't the class being used anywhere.

Also I am unsure where is the VFS for ucrt module coming from. It would be good to elaborate the process of how the module map is found and what problem it has for dependency scanner right now.

@hyp
Copy link
Contributor Author

hyp commented Dec 14, 2024

Sorry, the SilentClangImporterOverlayWrapperFS is no longer needed after the rebranch (my original fix was on a pre-rebranch branch which had a strong assertion related to VFS overlay optimization count that is different now in the new branch). Now it should be more clear hopefully! Answers inline:

Also I am unsure where is the VFS for ucrt module coming from. It would be good to elaborate the process of how the module map is found and what problem it has for dependency scanner right now.

The module map for UCRT is shipped alongside swift (ucrt.modulemap). The clang importer then constructs the redirecting file system, that redirects from the virtual module map (doesn't exist on disk) located windows SDK in MSVC to the real one that's located next to Swift. This lets Swift import windows C runtime and C++ stdlib correctly in implicit module-based compilation, as clang can then actually load the injected module map file for UCRT.

Right now the Clang scanner that the Swift scanner uses for Clang modules doesn't know about the injected files that Clang requires to correctly import Clang modules that have system dependencies like UCRT on windows, and as such it is impossible to use -explicit-module-build on Windows as the Clang scanner is unable to find the injected module map for UCRT (it reports - Unable to find module dependency: 'ucrt').

This change ensures that the Swift scanner sets up the exact same injection redirect & overlay VFS for the Clang dependency scanner as it does for the Clang importer. This lets the scanner correctly find the module map and construct appropriate invocation for the build of the UCRT module, that will later go through -direct-clang-cc1-module-build.

I see you added SilentClangImporterOverlayWrapperFS to avoid assertions in clang dependency scanner, is that the problem? But I don't the class being used anywhere.

The SilentClangImporterOverlayWrapperFS is no longer needed, thanks!

@hyp
Copy link
Contributor Author

hyp commented Dec 14, 2024

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Dec 14, 2024

@swift-ci please test windows platform

@cachemeifyoucan
Copy link
Contributor

The module map for UCRT is shipped alongside swift (ucrt.modulemap). The clang importer then constructs the redirecting file system, that redirects from the virtual module map (doesn't exist on disk) located windows SDK in MSVC to the real one that's located next to Swift. This lets Swift import windows C runtime and C++ stdlib correctly in implicit module-based compilation, as clang can then actually load the injected module map file for UCRT.

Is the process to import the ucrt from clang the same as swift or is this a swift specific module? If it is the previous, it might better to do it inside clang. The fix looks good to me as I think dependency scanner should have the file available. I just want to understand it better so it is possible to get rid of that redirecting file system if possible. It is fine to have redirecting file system during dependency scanner but we want a path forward to get rid of that after dependency scanner if scanner can build a VFS that contains all inputs.

@hyp
Copy link
Contributor Author

hyp commented Dec 16, 2024

The CI doesn't seem to find the overlay still, even though locally I can do it. Need to trace the overlay path in the CI:

error: Unable to find module dependency: '_complex'

@hyp
Copy link
Contributor Author

hyp commented Dec 16, 2024

Is the process to import the ucrt from clang the same as swift or is this a swift specific module? If it is the previous, it might better to do it inside clang. The fix looks good to me as I think dependency scanner should have the file available. I just want to understand it better so it is possible to get rid of that redirecting file system if possible. It is fine to have redirecting file system during dependency scanner but we want a path forward to get rid of that after dependency scanner if scanner can build a VFS that contains all inputs.

Standalone clang currently doesn't import anything from the WinSDK using clang modules, this is all just for Swift for now.
Can the scanner add these files into the VFS of inputs it constructs? I think that should be fine if I'm understanding what you're suggesting, so after scanning Swift wouldn't have to setup the overlay anymore if the scanner sets up the VFS with these files.

Copy link
Contributor

@cachemeifyoucan cachemeifyoucan left a comment

Choose a reason for hiding this comment

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

Seems fine to me if windows clang has no plan to support module in winSDK for now.

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

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

LGTM modulo the comment from @cachemeifyoucan

…or correct clang dependency scanning

This makes its possible to use -explicit-module-build to correctly find 'crt' swift module on windows
@hjyamauchi hjyamauchi force-pushed the eng/windows-i-c-u-scan-u-c-r-t branch from 7e70067 to 8237e1b Compare February 14, 2025 22:49
@hjyamauchi
Copy link
Contributor

hjyamauchi commented Feb 14, 2025

I'd like to drive this PR to merge on behalf of @hyp
@hyp I took the liberty of pushing an improvement for this PR/branch
@cachemeifyoucan can you take another look? Your comment should be addressed.
CC @egorzhdan please take another look
@swift-ci please test

@cachemeifyoucan
Copy link
Contributor

LGTM

@hjyamauchi
Copy link
Contributor

Thanks @cachemeifyoucan
The CI got a lot of assertion failures:

swift-frontend: /home/build-user/llvm-project/clang/lib/Lex/HeaderSearch.cpp:162: std::vector<bool> clang::HeaderSearch::collectVFSUsageAndClear() const: Assertion `VFSUsage.size() == getHeaderSearchOpts().VFSOverlayFiles.size() && "A different number of RedirectingFileSystem's were present than " "-ivfsoverlay options passed to Clang!"' failed.

So something needs to be fixed further :)

@hjyamauchi
Copy link
Contributor

Please test with following PR:
swiftlang/llvm-project#10075

@swift-ci Please test

@hjyamauchi
Copy link
Contributor

Please test with following PR:
swiftlang/llvm-project#10075

@swift-ci Please test Linux Platform

@hjyamauchi
Copy link
Contributor

@cachemeifyoucan @egorzhdan @compnerd I sent this PR for review as an alternative to this PR because the win-crt test in this PR doesn't pass in this PR. Can you take a look?

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.

4 participants