[Caching] Add new CacheReplay APIs to libSwiftScan

Add new APIs libSwiftScan that can be used for cache query and cache
replay. This enables swift-driver or build system to query the cache and
replay the compilation results without invocation swift-frontend for
better scheduling.
This commit is contained in:
Steven Wu
2023-10-26 10:48:05 -07:00
parent 30cfa3deb2
commit 034c15ce54
13 changed files with 1437 additions and 245 deletions

View File

@@ -369,10 +369,21 @@ int SwiftCacheToolInvocation::validateOutputs() {
report_fatal_error(DB.takeError());
auto &CAS = *DB->first;
auto &Cache = *DB->second;
PrintingDiagnosticConsumer PDC;
Instance.getDiags().addConsumer(PDC);
auto lookupFailed = [&](StringRef Key) {
llvm::errs() << "failed to find output for cache key " << Key << "\n";
return true;
};
auto lookupError = [&](llvm::Error Err, StringRef Key) {
llvm::errs() << "failed to find output for cache key " << Key << ": "
<< toString(std::move(Err)) << "\n";
return true;
};
auto validateCacheKeysFromFile = [&](const std::string &Path) {
auto Keys = readOutputEntriesFromFile(Path);
if (!Keys) {
@@ -391,28 +402,30 @@ int SwiftCacheToolInvocation::validateOutputs() {
return true;
}
auto Ref = CAS.getReference(*ID);
if (!Ref) {
llvm::errs() << "failed to find output for cache key " << Key
<< "\n";
return true;
}
cas::CachedResultLoader Loader(*DB->first, *DB->second, *Ref);
auto Result = Loader.replay(
if (!Ref)
return lookupFailed(*Key);
auto KeyID = CAS.getID(*Ref);
auto Lookup = Cache.get(KeyID);
if (!Lookup)
return lookupError(Lookup.takeError(), *Key);
if (!*Lookup)
return lookupFailed(*Key);
auto OutputRef = CAS.getReference(**Lookup);
if (!OutputRef)
return lookupFailed(*Key);
cas::CachedResultLoader Loader(CAS, *OutputRef);
if (auto Err = Loader.replay(
[&](file_types::ID Kind, ObjectRef Ref) -> llvm::Error {
auto Proxy = CAS.getProxy(Ref);
if (!Proxy)
return Proxy.takeError();
return llvm::Error::success();
});
if (!Result) {
})) {
llvm::errs() << "failed to find output for cache key " << *Key
<< ": " << toString(Result.takeError()) << "\n";
return true;
}
if (!*Result) {
llvm::errs() << "failed to load output for cache key " << *Key
<< "\n";
<< ": " << toString(std::move(Err)) << "\n";
return true;
}
continue;