Skip to content

[Dependency Scanning] Support loading scanner cache state for caching #79172

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
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
10 changes: 8 additions & 2 deletions include/swift/AST/DiagnosticsCommon.def
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,14 @@ REMARK(remark_scanner_invalidate_upstream, none,
"Incremental module scan: Dependency info for module '%0' invalidated due to an out-of-date"
" dependency.", (StringRef))

REMARK(remark_cache_reuse_disabled_with_cas, none,
"Incremental module scan: Re-using serialized module scanning dependency cache disabled in Caching build", ())
REMARK(remark_scanner_invalidate_configuration, none,
"Incremental module scan: Dependency info for module '%0' invalidated due to wrong configuration.", (StringRef))

REMARK(remark_scanner_invalidate_cas_error, none,
"Incremental module scan: Dependency info for module '%0' invalidated due to cas error: %1", (StringRef, StringRef))

REMARK(remark_scanner_invalidate_missing_cas, none,
"Incremental module scan: Dependency info for module '%0' invalidated due to missing CAS input '%1'.", (StringRef, StringRef))

//------------------------------------------------------------------------------
// MARK: custom attribute diagnostics
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/ModuleDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,8 @@ class SwiftDependencyScanningService {
return Mapper->mapToString(Path);
}

bool hasCAS() const { return (bool)CAS; }

/// Setup caching service.
bool setupCachingDependencyScanningService(CompilerInstance &Instance);

Expand Down
14 changes: 10 additions & 4 deletions include/swift/DependencyScan/ScanDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@
#define SWIFT_DEPENDENCY_SCANDEPENDENCIES_H

#include "swift-c/DependencyScan/DependencyScan.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Error.h"
#include "swift/AST/DiagnosticEngine.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/ErrorOr.h"
#include <unordered_set>

namespace llvm {
class StringSaver;
}
namespace vfs {
class FileSystem;
} // namespace vfs
} // namespace llvm

namespace swift {

class CompilerInvocation;
class CompilerInstance;
class DiagnosticEngine;
class ModuleDependenciesCache;
struct ModuleDependencyID;
struct ModuleDependencyIDHash;
Expand Down
3 changes: 3 additions & 0 deletions lib/DependencyScan/ModuleDependencyCacheSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,7 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays(
.CASBridgingHeaderIncludeTreeRootID);
addIdentifier(swiftTextDeps->moduleCacheKey);
addIdentifier(swiftTextDeps->userModuleVersion);
addIdentifier(swiftTextDeps->moduleCacheKey);
break;
}
case swift::ModuleDependencyKind::SwiftBinary: {
Expand All @@ -1831,6 +1832,7 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays(
addIdentifier(swiftBinDeps->headerImport);
addIdentifier(swiftBinDeps->definingModuleInterfacePath);
addIdentifier(swiftBinDeps->userModuleVersion);
addIdentifier(swiftBinDeps->moduleCacheKey);
addStringArray(moduleID,
ModuleIdentifierArrayKind::HeaderInputModuleDependencies,
clangHeaderDependencyNames);
Expand Down Expand Up @@ -1872,6 +1874,7 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays(
swiftSourceDeps->textualModuleDetails.CASFileSystemRootID);
addIdentifier(swiftSourceDeps->chainedBridgingHeaderPath);
addIdentifier(swiftSourceDeps->chainedBridgingHeaderContent);
addIdentifier(swiftSourceDeps->moduleCacheKey);
break;
}
case swift::ModuleDependencyKind::Clang: {
Expand Down
85 changes: 58 additions & 27 deletions lib/DependencyScan/ScanDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "swift-c/DependencyScan/DependencyScan.h"
#include "swift/AST/DiagnosticsCommon.h"
#include "swift/Basic/PrettyStackTrace.h"

#include "swift/AST/ASTContext.h"
Expand Down Expand Up @@ -1273,32 +1274,27 @@ swift::dependencies::performModuleScan(
// Load the dependency cache if -reuse-dependency-scan-cache
// is specified
if (opts.ReuseDependencyScannerCache) {
// For the time being, incremental scanner cache validation
// is not compatible with CAS-enabled builds.
if (!ctx.CASOpts.EnableCaching) {
auto cachePath = opts.SerializedDependencyScannerCachePath;
if (opts.EmitDependencyScannerCacheRemarks)
ctx.Diags.diagnose(SourceLoc(), diag::remark_reuse_cache, cachePath);

llvm::sys::TimePoint<> serializedCacheTimeStamp;
bool loadFailure = module_dependency_cache_serialization::
readInterModuleDependenciesCache(cachePath, cache,
serializedCacheTimeStamp);
if (opts.EmitDependencyScannerCacheRemarks && loadFailure)
ctx.Diags.diagnose(SourceLoc(), diag::warn_scanner_deserialize_failed,
cachePath);

if (!loadFailure && opts.ValidatePriorDependencyScannerCache) {
auto mainModuleID =
ModuleDependencyID{instance.getMainModule()->getNameStr().str(),
ModuleDependencyKind::SwiftSource};
incremental::validateInterModuleDependenciesCache(
mainModuleID, cache, serializedCacheTimeStamp,
*instance.getSourceMgr().getFileSystem(), ctx.Diags,
opts.EmitDependencyScannerCacheRemarks);
}
} else if (opts.EmitDependencyScannerCacheRemarks)
ctx.Diags.diagnose(SourceLoc(), diag::remark_cache_reuse_disabled_with_cas);
auto cachePath = opts.SerializedDependencyScannerCachePath;
if (opts.EmitDependencyScannerCacheRemarks)
ctx.Diags.diagnose(SourceLoc(), diag::remark_reuse_cache, cachePath);

llvm::sys::TimePoint<> serializedCacheTimeStamp;
bool loadFailure =
module_dependency_cache_serialization::readInterModuleDependenciesCache(
cachePath, cache, serializedCacheTimeStamp);
if (opts.EmitDependencyScannerCacheRemarks && loadFailure)
ctx.Diags.diagnose(SourceLoc(), diag::warn_scanner_deserialize_failed,
cachePath);

if (!loadFailure && opts.ValidatePriorDependencyScannerCache) {
auto mainModuleID =
ModuleDependencyID{instance.getMainModule()->getNameStr().str(),
ModuleDependencyKind::SwiftSource};
incremental::validateInterModuleDependenciesCache(
mainModuleID, cache, serializedCacheTimeStamp,
*instance.getSourceMgr().getFileSystem(), ctx.Diags,
opts.EmitDependencyScannerCacheRemarks);
}
}

auto scanner = ModuleDependencyScanner(
Expand Down Expand Up @@ -1334,7 +1330,7 @@ swift::dependencies::performModuleScan(

// Serialize the dependency cache if -serialize-dependency-scan-cache
// is specified
if (opts.SerializeDependencyScannerCache && !ctx.CASOpts.EnableCaching) {
if (opts.SerializeDependencyScannerCache) {
auto savePath = opts.SerializedDependencyScannerCachePath;
module_dependency_cache_serialization::writeInterModuleDependenciesCache(
ctx.Diags, instance.getOutputBackend(), savePath, cache);
Expand Down Expand Up @@ -1449,6 +1445,41 @@ bool swift::dependencies::incremental::verifyModuleDependencyUpToDate(
return true;
};

auto verifyCASID = [&cache, &diags, emitRemarks](StringRef moduleName,
const std::string &casID) {
if (!cache.getScanService().hasCAS()) {
// If the wrong cache is passed.
if (emitRemarks)
diags.diagnose(SourceLoc(),
diag::remark_scanner_invalidate_configuration,
moduleName);
return false;
}
auto &CAS = cache.getScanService().getCAS();
auto ID = CAS.parseID(casID);
if (!ID) {
if (emitRemarks)
diags.diagnose(SourceLoc(), diag::remark_scanner_invalidate_cas_error,
moduleName, toString(ID.takeError()));
return false;
}
if (!CAS.getReference(*ID)) {
if (emitRemarks)
diags.diagnose(SourceLoc(), diag::remark_scanner_invalidate_missing_cas,
moduleName, casID);
return false;
}
return true;
};

// Check CAS inputs exist
if (const auto casID = moduleInfo.getClangIncludeTree())
if (!verifyCASID(moduleID.ModuleName, *casID))
return false;
if (const auto casID = moduleInfo.getCASFSRootID())
if (!verifyCASID(moduleID.ModuleName, *casID))
return false;

// Check interface file for Swift textual modules
if (const auto &textualModuleDetails = moduleInfo.getAsSwiftInterfaceModule())
if (!verifyInputOlderThanCacheTimeStamp(
Expand Down
18 changes: 18 additions & 0 deletions test/CAS/incremental_scan.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/module-cache)

// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/../ScanDependencies/Inputs/Swift -cache-compile-job -cas-path %t/cas -no-clang-include-tree -Rdependency-scan-cache -load-dependency-scan-cache -dependency-scan-cache-path %t/cache.moddepcache -serialize-dependency-scan-cache -validate-prior-dependency-scan-cache 2>&1 | %FileCheck %s --check-prefix=REUSE --check-prefix=FAILED-LOAD

/// Test reuse
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/../ScanDependencies/Inputs/Swift -cache-compile-job -cas-path %t/cas -no-clang-include-tree -Rdependency-scan-cache -load-dependency-scan-cache -dependency-scan-cache-path %t/cache.moddepcache -serialize-dependency-scan-cache -validate-prior-dependency-scan-cache 2>&1 | %FileCheck %s --check-prefix=REUSE

/// Test invalidation after removing CAS.
// RUN: rm -rf %t/cas
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/../ScanDependencies/Inputs/Swift -cache-compile-job -cas-path %t/cas -no-clang-include-tree -Rdependency-scan-cache -load-dependency-scan-cache -dependency-scan-cache-path %t/cache.moddepcache -serialize-dependency-scan-cache -validate-prior-dependency-scan-cache 2>&1 | %FileCheck %s --check-prefix=REUSE --check-prefix=INVALIDATE

// REUSE: Incremental module scan: Re-using serialized module scanning dependency cache from:
// FAILED-LOD: Incremental module scan: Failed to load module scanning dependency cache from
// INVALIDATE: Incremental module scan: Dependency info for module '{{.*}}' invalidated due to missing CAS input
// INVALIDATE: Incremental module scan: Dependency info for module '{{.*}}' invalidated due to an out-of-date dependency.
// REUSE: Incremental module scan: Serializing module scanning dependency cache to
import E
8 changes: 0 additions & 8 deletions test/CAS/no_incremental_scan_for_you.swift

This file was deleted.