Skip to content

Commit 471317d

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 471317d

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
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: 7 additions & 1 deletion
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);
@@ -311,7 +315,8 @@ static llvm::Error resolveExplicitModuleInputs(
311315
includeTrees.push_back(*ID);
312316

313317
auto addBridgingHeaderDeps =
314-
[&](const ModuleDependencyInfo &depInfo) -> llvm::Error {
318+
[&rootIDs, &includeTrees,
319+
&cache](const ModuleDependencyInfo &depInfo) -> llvm::Error {
315320
auto sourceDepDetails = depInfo.getAsSwiftSourceModule();
316321
if (!sourceDepDetails)
317322
return llvm::Error::success();
@@ -505,6 +510,7 @@ static llvm::Error resolveExplicitModuleInputs(
505510
return E;
506511
}
507512
}
513+
dependencyInfoCopy.setIsFinalized(true);
508514
cache.updateDependency(moduleID, dependencyInfoCopy);
509515

510516
return llvm::Error::success();

test/Frontend/output_determinism_check.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/// FAIL: %target-swift-frontend -module-name test -emit-reference-dependencies-path %t/test.swiftdeps -c -o %t/test.o -primary-file %s -enable-deterministic-check -always-compile-output-files
1313

1414
/// Explicit module build. Check building swiftmodule from interface file.
15-
// RUN: %target-swift-frontend -scan-dependencies -module-name test -o %t/test.json %s -enable-deterministic-check 2>&1 | %FileCheck %s --check-prefix=DEPSCAN_OUTPUT
15+
// RUN: %target-swift-frontend -scan-dependencies -module-name test -o %t/test.json %s -enable-deterministic-check -load-dependency-scan-cache -dependency-scan-cache-path %t/deps-cache -serialize-dependency-scan-cache 2>&1 | %FileCheck %s --check-prefix=DEPSCAN_OUTPUT --check-prefix=DEPSCAN_CACHE_OUTPUT
1616
/// TODO: Implicit module build use a different compiler instance so it doesn't support checking yet.
1717
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t/test.swiftinterface %s -O -enable-deterministic-check 2>&1 | %FileCheck %s --check-prefix=INTERFACE_OUTPUT
1818
/// Hit cache and not emit the second time.
@@ -21,8 +21,6 @@
2121
/// Force swiftmodule generation.
2222
// RUN: %target-swift-frontend -compile-module-from-interface %t/test.swiftinterface -explicit-interface-module-build -o %t/test.swiftmodule -enable-deterministic-check -always-compile-output-files 2>&1 | %FileCheck --check-prefix=MODULE_OUTPUT %s
2323

24-
// RUN: %target-swift-frontend -scan-dependencies -module-name test %s -o %t/test.deps.json -enable-deterministic-check 2>&1 | %FileCheck %s --check-prefix=DEPS_JSON_OUTPUT
25-
2624
// RUN: %target-swift-frontend -emit-pcm -module-name UserClangModule -o %t/test.pcm %S/Inputs/dependencies/module.modulemap -enable-deterministic-check 2>&1 | %FileCheck %s --check-prefix=PCM_OUTPUT
2725

2826
// DOCC_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.docc'
@@ -33,10 +31,10 @@
3331
// DEPS_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.d'
3432
// OBJECT_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.o'
3533
// OBJECT_MISMATCH: error: output file '{{.*}}{{/|\\}}test.o' is missing from second compilation for deterministic check
34+
// DEPSCAN_CACHE_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}deps-cache'
3635
// DEPSCAN_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.json'
3736
// INTERFACE_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.swiftinterface'
3837
// MODULE_MISMATCH: error: output file '{{.*}}{{/|\\}}test.swiftmodule' is missing from second compilation for deterministic check
39-
// DEPS_JSON_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.deps.json'
4038
// PCM_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.pcm'
4139

4240
public var x = 1

0 commit comments

Comments
 (0)