Skip to content

Commit cfe87ec

Browse files
[Caching] Add fast swift instance setup for cache replay
Add a fast path to create swift CompilerInstance when it is only used to replay output when there is a cache hit. The normal `setup` function is very expensive to call, especially in cache mode to setup inputs, and it needs to be called once per input file from libSwiftScan API due to the current caching granularity. The fast path will only construct the part that is needed for output replay, including the CAS, the output backend and caching diagnostic processor. rdar://127062609 (cherry picked from commit facfe45)
1 parent 6eb92fb commit cfe87ec

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,10 @@ class CompilerInstance {
691691
bool setup(const CompilerInvocation &Invocation, std::string &Error,
692692
ArrayRef<const char *> Args = {});
693693

694+
/// The fast setup function for cache replay.
695+
bool setupForReplay(const CompilerInvocation &Invocation, std::string &Error,
696+
ArrayRef<const char *> Args = {});
697+
694698
const CompilerInvocation &getInvocation() const { return Invocation; }
695699

696700
/// If a IDE inspection buffer has been set, returns the corresponding source

lib/Frontend/Frontend.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,23 @@ bool CompilerInstance::setup(const CompilerInvocation &Invoke,
562562
return false;
563563
}
564564

565+
bool CompilerInstance::setupForReplay(const CompilerInvocation &Invoke,
566+
std::string &Error,
567+
ArrayRef<const char *> Args) {
568+
// This is the fast path for setup an instance for replay but cannot run
569+
// regular compilation.
570+
Invocation = Invoke;
571+
572+
if (setupCASIfNeeded(Args)) {
573+
Error = "Setting up CAS failed";
574+
return true;
575+
}
576+
577+
setupOutputBackend();
578+
setupCachingDiagnosticsProcessorIfNeeded();
579+
return false;
580+
}
581+
565582
bool CompilerInstance::setUpVirtualFileSystemOverlays() {
566583
if (Invocation.getCASOptions().requireCASFS()) {
567584
const auto &Opts = getInvocation().getCASOptions();

tools/libSwiftScan/SwiftCaching.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ static llvm::Error replayCompilation(SwiftScanReplayInstance &Instance,
932932
Invocation.getDiagnosticOptions().EmitMacroExpansionFiles);
933933

934934
std::string InstanceSetupError;
935-
if (Inst.setup(Instance.Invocation, InstanceSetupError, Instance.Args))
935+
if (Inst.setupForReplay(Instance.Invocation, InstanceSetupError,
936+
Instance.Args))
936937
return createStringError(inconvertibleErrorCode(), InstanceSetupError);
937938

938939
auto *CDP = Inst.getCachingDiagnosticsProcessor();

0 commit comments

Comments
 (0)