Skip to content

Commit 15593e5

Browse files
committed
[Dependency Scanning] Restrict Swift overlay lookup to "visible" Clang modules only
Previously Swift overlay lookup was performed for every directly and transitively-imported Clang module. llvm/llvm-project#147969 introduced the concept of "visible" Clang modules from a given named Clang dependency scanner query which closely maps to the set of modules for which Swift will attempt to load a Swift overlay. This change switches overlay querying to apply only to the set of such visible modules. Resolves rdar://144797648
1 parent 0ff7bb8 commit 15593e5

File tree

6 files changed

+197
-82
lines changed

6 files changed

+197
-82
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ class ModuleDependencyInfoStorageBase {
212212
/// The macro dependencies.
213213
std::map<std::string, MacroPluginDependency> macroDependencies;
214214

215+
/// A list of Clang modules that are visible to this Swift module. This
216+
/// includes both direct Clang modules as well as transitive Clang
217+
/// module dependencies when they are exported
218+
llvm::StringSet<> visibleClangModules;
219+
215220
/// ModuleDependencyInfo is finalized (with all transitive dependencies
216221
/// and inputs).
217222
bool finalized;
@@ -816,7 +821,17 @@ class ModuleDependencyInfo {
816821
cast<ClangModuleDependencyStorage>(storage.get())->CASFileSystemRootID =
817822
rootID;
818823
else
819-
llvm_unreachable("Unexpected type");
824+
llvm_unreachable("Unexpected module dependency kind");
825+
}
826+
827+
llvm::StringSet<> &getVisibleClangModules() const {
828+
return storage->visibleClangModules;
829+
}
830+
831+
void
832+
addVisibleClangModules(const std::vector<std::string> &moduleNames) const {
833+
storage->visibleClangModules.insert(moduleNames.begin(),
834+
moduleNames.end());
820835
}
821836

822837
/// Whether explicit input paths of all the module dependencies
@@ -1057,6 +1072,9 @@ class ModuleDependenciesCache {
10571072
/// Query all cross-import overlay dependencies
10581073
llvm::ArrayRef<ModuleDependencyID>
10591074
getCrossImportOverlayDependencies(const ModuleDependencyID &moduleID) const;
1075+
/// Query all visible Clang modules for a given Swift dependency
1076+
llvm::StringSet<>&
1077+
getVisibleClangModules(ModuleDependencyID moduleID) const;
10601078

10611079
/// Look for module dependencies for a module with the given ID
10621080
///
@@ -1123,6 +1141,11 @@ class ModuleDependenciesCache {
11231141
setCrossImportOverlayDependencies(ModuleDependencyID moduleID,
11241142
const ArrayRef<ModuleDependencyID> dependencyIDs);
11251143

1144+
/// Add to this module's set of visible Clang modules
1145+
void
1146+
addVisibleClangModules(ModuleDependencyID moduleID,
1147+
const std::vector<std::string> &moduleNames);
1148+
11261149
StringRef getMainModuleName() const { return mainScanModuleName; }
11271150

11281151
private:

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ModuleDependencyScanningWorker {
4040

4141
private:
4242
/// Retrieve the module dependencies for the Clang module with the given name.
43-
ModuleDependencyVector scanFilesystemForClangModuleDependency(
43+
ClangModuleScannerQueryResult scanFilesystemForClangModuleDependency(
4444
Identifier moduleName,
4545
const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
4646
&alreadySeenModules);
@@ -72,6 +72,7 @@ class ModuleDependencyScanningWorker {
7272
ModuleDependencyIDSetVector &headerClangModuleDependencies,
7373
std::vector<std::string> &headerFileInputs,
7474
std::vector<std::string> &bridgingHeaderCommandLine,
75+
std::vector<std::string> &visibleClangModules,
7576
std::optional<std::string> &includeTreeID);
7677

7778

include/swift/Serialization/ScanningLoaders.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ struct SwiftModuleScannerQueryResult {
4040
std::vector<IncompatibleCandidate> incompatibleCandidates;
4141
};
4242

43+
/// Result of looking up a Clang module on the current filesystem
44+
/// search paths.
45+
struct ClangModuleScannerQueryResult {
46+
ClangModuleScannerQueryResult(const ModuleDependencyVector &dependencyModuleGraph,
47+
const std::vector<std::string> &visibleModuleIdentifiers)
48+
: foundDependencyModuleGraph(dependencyModuleGraph),
49+
visibleModuleIdentifiers(visibleModuleIdentifiers) {}
50+
51+
ModuleDependencyVector foundDependencyModuleGraph;
52+
std::vector<std::string> visibleModuleIdentifiers;
53+
};
54+
4355
/// A module "loader" that looks for .swiftinterface and .swiftmodule files
4456
/// for the purpose of determining dependencies, but does not attempt to
4557
/// load the module files.

lib/AST/ModuleDependencies.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,24 @@ ModuleDependenciesCache::setCrossImportOverlayDependencies(ModuleDependencyID mo
918918
updateDependency(moduleID, updatedDependencyInfo);
919919
}
920920

921+
void
922+
ModuleDependenciesCache::addVisibleClangModules(ModuleDependencyID moduleID,
923+
const std::vector<std::string> &moduleNames) {
924+
if (moduleNames.empty())
925+
return;
926+
auto dependencyInfo = findKnownDependency(moduleID);
927+
auto updatedDependencyInfo = dependencyInfo;
928+
updatedDependencyInfo.addVisibleClangModules(moduleNames);
929+
updateDependency(moduleID, updatedDependencyInfo);
930+
}
931+
932+
llvm::StringSet<> &ModuleDependenciesCache::getVisibleClangModules(ModuleDependencyID moduleID) const {
933+
ASSERT(moduleID.Kind == ModuleDependencyKind::SwiftSource ||
934+
moduleID.Kind == ModuleDependencyKind::SwiftInterface ||
935+
moduleID.Kind == ModuleDependencyKind::SwiftBinary);
936+
return findKnownDependency(moduleID).getVisibleClangModules();
937+
}
938+
921939
ModuleDependencyIDSetVector
922940
ModuleDependenciesCache::getAllDependencies(const ModuleDependencyID &moduleID) const {
923941
const auto &moduleInfo = findKnownDependency(moduleID);

0 commit comments

Comments
 (0)