Skip to content

[Dependency Scanning][C++Interop] Do not query CxxStdlib Swift overlay for textual modules which were not built with c++interop #81415

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
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions include/swift/AST/ModuleDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ class ModuleDependencyInfo {
storage->importedSwiftModules.assign(dependencyIDs.begin(),
dependencyIDs.end());
}
const ArrayRef<ModuleDependencyID> getImportedSwiftDependencies() const {
ArrayRef<ModuleDependencyID> getImportedSwiftDependencies() const {
return storage->importedSwiftModules;
}

Expand All @@ -698,7 +698,7 @@ class ModuleDependencyInfo {
storage->importedClangModules.assign(dependencyIDs.begin(),
dependencyIDs.end());
}
const ArrayRef<ModuleDependencyID> getImportedClangDependencies() const {
ArrayRef<ModuleDependencyID> getImportedClangDependencies() const {
return storage->importedClangModules;
}

Expand Down Expand Up @@ -732,7 +732,7 @@ class ModuleDependencyInfo {
}
}
}
const ArrayRef<ModuleDependencyID> getHeaderClangDependencies() const {
ArrayRef<ModuleDependencyID> getHeaderClangDependencies() const {
switch (getKind()) {
case swift::ModuleDependencyKind::SwiftInterface: {
auto swiftInterfaceStorage =
Expand Down Expand Up @@ -760,7 +760,7 @@ class ModuleDependencyInfo {
storage->swiftOverlayDependencies.assign(dependencyIDs.begin(),
dependencyIDs.end());
}
const ArrayRef<ModuleDependencyID> getSwiftOverlayDependencies() const {
ArrayRef<ModuleDependencyID> getSwiftOverlayDependencies() const {
return storage->swiftOverlayDependencies;
}

Expand All @@ -770,11 +770,11 @@ class ModuleDependencyInfo {
storage->crossImportOverlayModules.assign(dependencyIDs.begin(),
dependencyIDs.end());
}
const ArrayRef<ModuleDependencyID> getCrossImportOverlayDependencies() const {
ArrayRef<ModuleDependencyID> getCrossImportOverlayDependencies() const {
return storage->crossImportOverlayModules;
}

const ArrayRef<LinkLibrary> getLinkLibraries() const {
ArrayRef<LinkLibrary> getLinkLibraries() const {
return storage->linkLibraries;
}

Expand All @@ -783,7 +783,7 @@ class ModuleDependencyInfo {
storage->linkLibraries.assign(linkLibraries.begin(), linkLibraries.end());
}

const ArrayRef<std::string> getAuxiliaryFiles() const {
ArrayRef<std::string> getAuxiliaryFiles() const {
return storage->auxiliaryFiles;
}

Expand All @@ -795,7 +795,7 @@ class ModuleDependencyInfo {
return false;
}

const ArrayRef<std::string> getHeaderInputSourceFiles() const {
ArrayRef<std::string> getHeaderInputSourceFiles() const {
if (auto *detail = getAsSwiftInterfaceModule())
return detail->textualModuleDetails.bridgingSourceFiles;
if (auto *detail = getAsSwiftSourceModule())
Expand All @@ -805,7 +805,7 @@ class ModuleDependencyInfo {
return {};
}

std::vector<std::string> getCommandline() const {
ArrayRef<std::string> getCommandline() const {
if (auto *detail = getAsClangModule())
return detail->buildCommandLine;
if (auto *detail = getAsSwiftInterfaceModule())
Expand All @@ -828,7 +828,7 @@ class ModuleDependencyInfo {
llvm_unreachable("Unexpected type");
}

std::vector<std::string> getBridgingHeaderCommandline() const {
ArrayRef<std::string> getBridgingHeaderCommandline() const {
if (auto *detail = getAsSwiftSourceModule())
return detail->bridgingHeaderBuildCommandLine;
return {};
Expand Down
50 changes: 29 additions & 21 deletions lib/DependencyScan/ModuleDependencyScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ ModuleDependencyScanner::getMainModuleDependencyInfo(ModuleDecl *mainModule) {
// build command to main module to ensure frontend gets the same result.
// This needs to happen after visiting all the top-level decls from all
// SourceFiles.
auto buildArgs = mainDependencies.getCommandline();
std::vector<std::string> buildArgs = mainDependencies.getCommandline();
mainModule->getASTContext().forEachCanImportVersionCheck(
[&](StringRef moduleName, const llvm::VersionTuple &Version,
const llvm::VersionTuple &UnderlyingVersion) {
Expand Down Expand Up @@ -1328,25 +1328,33 @@ void ModuleDependencyScanner::resolveSwiftOverlayDependenciesForModule(
recordResult(clangDep);

// C++ Interop requires additional handling
if (ScanCompilerInvocation.getLangOptions().EnableCXXInterop) {
for (const auto &clangDepName : allClangDependencies) {
// If this Clang module is a part of the C++ stdlib, and we haven't loaded
// the overlay for it so far, it is a split libc++ module (e.g.
// std_vector). Load the CxxStdlib overlay explicitly.
const auto &clangDepInfo =
cache.findDependency(clangDepName, ModuleDependencyKind::Clang)
.value()
->getAsClangModule();
if (importer::isCxxStdModule(clangDepName, clangDepInfo->IsSystem) &&
!swiftOverlayDependencies.contains(
{clangDepName, ModuleDependencyKind::SwiftInterface}) &&
!swiftOverlayDependencies.contains(
{clangDepName, ModuleDependencyKind::SwiftBinary})) {
ScanningThreadPool.async(
scanForSwiftDependency,
getModuleImportIdentifier(ScanASTContext.Id_CxxStdlib.str()));
ScanningThreadPool.wait();
recordResult(ScanASTContext.Id_CxxStdlib.str().str());
if (ScanCompilerInvocation.getLangOptions().EnableCXXInterop &&
moduleID.Kind == ModuleDependencyKind::SwiftInterface) {
const auto &moduleInfo = cache.findKnownDependency(moduleID);
const auto commandLine = moduleInfo.getCommandline();

// If the textual interface was built without C++ interop, do not query
// the C++ Standard Library Swift overlay for its compilation.
if (llvm::find(commandLine, "-formal-cxx-interoperability-mode=off") ==
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the reason we do a textual search here because we haven't loaded the module yet?

Copy link
Contributor Author

@artemcm artemcm May 9, 2025

Choose a reason for hiding this comment

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

Yes. The dependency scanner doesn't actually load modules. If/Once it finds a textual interface for a module dependency, we create a sub-instance to parse the module for its imports and compute a command-line recipe to build it, storing it in the ModuleInfo in the scanner. The build system client then uses this information to actually build this module for ingestion into compilation.

Because the module doesn't actually get loaded, we currently can only get this information from the command line. It would be reasonable to record this as a boolean flag in the ModuleInfo when we create one for this module, but I wanted to have a narrow fix first.

commandLine.end()) {
for (const auto &clangDepName : allClangDependencies) {
// If this Clang module is a part of the C++ stdlib, and we haven't
// loaded the overlay for it so far, it is a split libc++ module (e.g.
// std_vector). Load the CxxStdlib overlay explicitly.
const auto &clangDepInfo =
cache.findDependency(clangDepName, ModuleDependencyKind::Clang)
.value()
->getAsClangModule();
if (importer::isCxxStdModule(clangDepName, clangDepInfo->IsSystem) &&
!swiftOverlayDependencies.contains(
{clangDepName, ModuleDependencyKind::SwiftInterface}) &&
!swiftOverlayDependencies.contains(
{clangDepName, ModuleDependencyKind::SwiftBinary})) {
scanForSwiftDependency(
getModuleImportIdentifier(ScanASTContext.Id_CxxStdlib.str()));
recordResult(ScanASTContext.Id_CxxStdlib.str().str());
break;
}
}
}
}
Expand Down Expand Up @@ -1401,7 +1409,7 @@ void ModuleDependencyScanner::resolveCrossImportOverlayDependencies(
// Update the command-line on the main module to
// disable implicit cross-import overlay search.
auto mainDep = cache.findKnownDependency(actualMainID);
auto cmdCopy = mainDep.getCommandline();
std::vector<std::string> cmdCopy = mainDep.getCommandline();
cmdCopy.push_back("-disable-cross-import-overlay-search");
for (auto &entry : overlayFiles) {
mainDep.addAuxiliaryFile(entry.second);
Expand Down
2 changes: 1 addition & 1 deletion lib/DependencyScan/ScanDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ generateFullDependencyGraph(const CompilerInstance &instance,
moduleInfo->details = getModuleDetails();

// Create a link libraries set for this module
auto &linkLibraries = moduleDependencyInfo.getLinkLibraries();
auto linkLibraries = moduleDependencyInfo.getLinkLibraries();
swiftscan_link_library_set_t *linkLibrarySet =
new swiftscan_link_library_set_t;
linkLibrarySet->count = linkLibraries.size();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/deps)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -scan-dependencies -o %t/deps.json %t/clientWithInteropDep.swift -I %t/deps -cxx-interoperability-mode=default -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -verify
// RUN: cat %t/deps.json | %FileCheck %s -check-prefix=ENABLE-CHECK

// RUN: %target-swift-frontend -scan-dependencies -o %t/deps_no_interop_dep.json %t/clientNoInteropDep.swift -I %t/deps -cxx-interoperability-mode=default -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -verify
// RUN: cat %t/deps_no_interop_dep.json | %FileCheck %s -check-prefix=DISABLE-CHECK

//--- deps/bar.h
void bar(void);

//--- deps/module.modulemap
module std_Bar [system] {
header "bar.h"
export *
}

//--- deps/Foo.swiftinterface
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name Foo -enable-library-evolution
import std_Bar
public struct Foo1 {}

//--- deps/FooNoInterop.swiftinterface
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name FooNoInterop -enable-library-evolution
// swift-module-flags-ignorable: -formal-cxx-interoperability-mode=off
import std_Bar
public struct Foo2 {}

//--- clientWithInteropDep.swift
import Foo

//--- clientNoInteropDep.swift
import FooNoInterop

// Ensure that when the 'Foo' dependency was built with C++ interop enabled,
// it gets the C++ standard library overlay for its 'std_*' dependency
//
// 'Foo' as it appears in direct deps
// ENABLE-CHECK: "swift": "Foo"
// 'Foo' as it appears in source-import deps
// ENABLE-CHECK: "swift": "Foo"
// Actual dependency info node
// ENABLE-CHECK: "swift": "Foo"
// ENABLE-CHECK: "directDependencies": [
// ENABLE-CHECK: {
// ENABLE-CHECK: "swift": "SwiftOnoneSupport"
// ENABLE-CHECK: },
// ENABLE-CHECK: {
// ENABLE-CHECK: "swift": "CxxStdlib"
// ENABLE-CHECK: },
// ENABLE-CHECK: {
// ENABLE-CHECK: "clang": "std_Bar"
// ENABLE-CHECK: }
// ENABLE-CHECK: ],

// Ensure that when the 'Foo' dependency was *not* built with C++ interop enabled,
// it does not get the C++ standard library overlay for its 'std_*' dependency
//
// 'Foo' as it appears in direct deps
// DISABLE-CHECK: "swift": "FooNoInterop"
// 'Foo' as it appears in source-import deps
// DISABLE-CHECK: "swift": "FooNoInterop"
// Actual dependency info node
// DISABLE-CHECK: "swift": "FooNoInterop"
// DISABLE-CHECK: "directDependencies": [
// DISABLE-CHECK: {
// DISABLE-CHECK: "swift": "SwiftOnoneSupport"
// DISABLE-CHECK: },
// DISABLE-CHECK: {
// DISABLE-CHECK: "clang": "std_Bar"
// DISABLE-CHECK: }
// DISABLE-CHECK: ],