Skip to content

Commit 6b08a61

Browse files
[DependencyScan] Prevent command-line flags added again on re-scan
Add a flag `finalized` to indicate that a module entry in the dependency cache is finalized and no longer needs to be updated. This prevents the command-line flags from dependency inputs get added multiple times on re-scan with the same service. While during normal compilation, adding the same command-line flags multiple times are fine, it is bad for caching builds as a new compilation cache key needs to be computed every time.
1 parent 8282c17 commit 6b08a61

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class ModuleDependencyInfoStorageBase {
116116
const std::vector<std::string> &moduleImports,
117117
StringRef moduleCacheKey = "")
118118
: dependencyKind(dependencyKind), moduleImports(moduleImports),
119-
moduleCacheKey(moduleCacheKey.str()), resolved(false) {}
119+
moduleCacheKey(moduleCacheKey.str()), resolved(false), finalized(false) {}
120120

121121
virtual ModuleDependencyInfoStorageBase *clone() const = 0;
122122

@@ -137,7 +137,11 @@ class ModuleDependencyInfoStorageBase {
137137
/// The cache key for the produced module.
138138
std::string moduleCacheKey;
139139

140+
/// The direct dependency of the module is resolved by scanner.
140141
bool resolved;
142+
/// ModuleDependencyInfo is finalized (with all transitive dependencies
143+
/// and inputs).
144+
bool finalized;
141145
};
142146

143147
struct CommonSwiftTextualModuleDependencyDetails {
@@ -604,6 +608,13 @@ class ModuleDependencyInfo {
604608
storage->resolved = isResolved;
605609
}
606610

611+
bool isFinalized() const {
612+
return storage->finalized;
613+
}
614+
void setIsFinalized(bool isFinalized) {
615+
storage->finalized = isFinalized;
616+
}
617+
607618
/// For a Source dependency, register a `Testable` import
608619
void addTestableImport(ImportPath::Module module);
609620

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ static llvm::Error resolveExplicitModuleInputs(
302302
if (moduleID.second == ModuleDependencyKind::SwiftPlaceholder)
303303
return llvm::Error::success();
304304

305+
// If the dependency is already finalized, nothing needs to be done.
306+
if (resolvingDepInfo.isFinalized())
307+
return llvm::Error::success();
308+
305309
std::vector<std::string> rootIDs;
306310
if (auto ID = resolvingDepInfo.getCASFSRootID())
307311
rootIDs.push_back(*ID);
@@ -505,6 +509,7 @@ static llvm::Error resolveExplicitModuleInputs(
505509
return E;
506510
}
507511
}
512+
dependencyInfoCopy.setIsFinalized(true);
508513
cache.updateDependency(moduleID, dependencyInfoCopy);
509514

510515
return llvm::Error::success();
@@ -522,8 +527,9 @@ resolveDirectDependencies(CompilerInstance &instance, ModuleDependencyID moduleI
522527
auto knownDependencies = optionalKnownDependencies.value();
523528

524529
// If this dependency has already been resolved, return the result.
525-
if (knownDependencies->isResolved() &&
526-
knownDependencies->getKind() != ModuleDependencyKind::SwiftSource)
530+
if (knownDependencies->isFinalized() ||
531+
(knownDependencies->isResolved() &&
532+
knownDependencies->getKind() != ModuleDependencyKind::SwiftSource))
527533
return knownDependencies->getModuleDependencies();
528534

529535
auto isSwiftInterfaceOrSource = knownDependencies->isSwiftInterfaceModule() ||

0 commit comments

Comments
 (0)