Skip to content

Commit dae1e84

Browse files
Merge pull request #73597 from cachemeifyoucan/eng/PR-typecheck-caching-support
[Caching] Mark `-typecheck` action as caching supported
2 parents 4789cc7 + 431f097 commit dae1e84

File tree

7 files changed

+34
-17
lines changed

7 files changed

+34
-17
lines changed

include/swift/Basic/CASOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class CASOptions final {
4949
/// Cache key for imported bridging header.
5050
std::string BridgingHeaderPCHCacheKey;
5151

52+
/// Has immutable file system input.
53+
bool HasImmutableFileSystem = false;
54+
5255
/// Get the CAS configuration flags.
5356
void enumerateCASConfigurationFlags(
5457
llvm::function_ref<void(llvm::StringRef)> Callback) const;

lib/Frontend/CASOutputBackends.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ void SwiftCASOutputBackend::Implementation::initBackend(
149149
// any commands write output to `-`.
150150
file_types::ID mainOutputType = InputsAndOutputs.getPrincipalOutputType();
151151
auto addInput = [&](const InputFile &Input, unsigned Index) {
152-
if (!Input.outputFilename().empty())
152+
// Ignore the outputFilename for typecheck action since it is not producing
153+
// an output file for that.
154+
if (!Input.outputFilename().empty() &&
155+
Action != FrontendOptions::ActionType::Typecheck)
153156
OutputToInputMap.insert(
154157
{Input.outputFilename(), {Index, mainOutputType}});
155158
Input.getPrimarySpecificPaths()

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,8 @@ static bool ParseCASArgs(CASOptions &Opts, ArgList &Args,
571571
if (const Arg*A = Args.getLastArg(OPT_bridging_header_pch_key))
572572
Opts.BridgingHeaderPCHCacheKey = A->getValue();
573573

574-
if (Opts.EnableCaching && Opts.CASFSRootIDs.empty() &&
575-
Opts.ClangIncludeTrees.empty() &&
576-
FrontendOptions::supportCompilationCaching(
577-
FrontendOpts.RequestedAction)) {
578-
Diags.diagnose(SourceLoc(), diag::error_caching_no_cas_fs);
579-
return true;
580-
}
574+
if (!Opts.CASFSRootIDs.empty() || !Opts.ClangIncludeTrees.empty())
575+
Opts.HasImmutableFileSystem = true;
581576

582577
return false;
583578
}

lib/Frontend/Frontend.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,19 @@ bool CompilerInstance::setupForReplay(const CompilerInvocation &Invoke,
580580
}
581581

582582
bool CompilerInstance::setUpVirtualFileSystemOverlays() {
583+
const auto &CASOpts = getInvocation().getCASOptions();
584+
if (CASOpts.EnableCaching && !CASOpts.HasImmutableFileSystem &&
585+
FrontendOptions::supportCompilationCaching(
586+
Invocation.getFrontendOptions().RequestedAction)) {
587+
Diagnostics.diagnose(SourceLoc(), diag::error_caching_no_cas_fs);
588+
return true;
589+
}
590+
583591
if (Invocation.getCASOptions().requireCASFS()) {
584-
const auto &Opts = getInvocation().getCASOptions();
585-
if (!Opts.CASFSRootIDs.empty() || !Opts.ClangIncludeTrees.empty()) {
592+
if (!CASOpts.CASFSRootIDs.empty() || !CASOpts.ClangIncludeTrees.empty()) {
586593
// Set up CASFS as BaseFS.
587-
auto FS =
588-
createCASFileSystem(*CAS, Opts.CASFSRootIDs, Opts.ClangIncludeTrees);
594+
auto FS = createCASFileSystem(*CAS, CASOpts.CASFSRootIDs,
595+
CASOpts.ClangIncludeTrees);
589596
if (!FS) {
590597
Diagnostics.diagnose(SourceLoc(), diag::error_cas,
591598
toString(FS.takeError()));
@@ -599,10 +606,10 @@ bool CompilerInstance::setUpVirtualFileSystemOverlays() {
599606
new llvm::vfs::InMemoryFileSystem();
600607
const auto &ClangOpts = getInvocation().getClangImporterOptions();
601608

602-
if (!Opts.BridgingHeaderPCHCacheKey.empty()) {
609+
if (!CASOpts.BridgingHeaderPCHCacheKey.empty()) {
603610
if (auto loadedBuffer = loadCachedCompileResultFromCacheKey(
604611
getObjectStore(), getActionCache(), Diagnostics,
605-
Opts.BridgingHeaderPCHCacheKey, file_types::ID::TY_PCH,
612+
CASOpts.BridgingHeaderPCHCacheKey, file_types::ID::TY_PCH,
606613
ClangOpts.BridgingHeader))
607614
MemFS->addFile(Invocation.getClangImporterOptions().BridgingHeader, 0,
608615
std::move(loadedBuffer));
@@ -611,7 +618,7 @@ bool CompilerInstance::setUpVirtualFileSystemOverlays() {
611618
SourceLoc(), diag::error_load_input_from_cas,
612619
Invocation.getClangImporterOptions().BridgingHeader);
613620
}
614-
if (!Opts.InputFileKey.empty()) {
621+
if (!CASOpts.InputFileKey.empty()) {
615622
if (Invocation.getFrontendOptions()
616623
.InputsAndOutputs.getAllInputs()
617624
.size() != 1)
@@ -624,7 +631,7 @@ bool CompilerInstance::setUpVirtualFileSystemOverlays() {
624631
llvm::sys::path::filename(InputPath));
625632
if (auto loadedBuffer = loadCachedCompileResultFromCacheKey(
626633
getObjectStore(), getActionCache(), Diagnostics,
627-
Opts.InputFileKey, Type, InputPath))
634+
CASOpts.InputFileKey, Type, InputPath))
628635
MemFS->addFile(InputPath, 0, std::move(loadedBuffer));
629636
else
630637
Diagnostics.diagnose(SourceLoc(), diag::error_load_input_from_cas,

lib/Frontend/FrontendOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ bool FrontendOptions::supportCompilationCaching(ActionType action) {
230230
case ActionType::EmitImportedModules:
231231
case ActionType::ScanDependencies:
232232
case ActionType::ResolveImports:
233-
case ActionType::Typecheck:
234233
case ActionType::DumpAST:
235234
case ActionType::PrintAST:
236235
case ActionType::PrintASTDecl:
@@ -240,6 +239,7 @@ bool FrontendOptions::supportCompilationCaching(ActionType action) {
240239
case ActionType::Immediate:
241240
case ActionType::DumpTypeInfo:
242241
return false;
242+
case ActionType::Typecheck:
243243
case ActionType::TypecheckModuleFromInterface:
244244
case ActionType::CompileModuleFromInterface:
245245
case ActionType::EmitPCH:

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,8 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
17651765
if (casOpts.EnableCaching) {
17661766
genericSubInvocation.getCASOptions().EnableCaching = casOpts.EnableCaching;
17671767
genericSubInvocation.getCASOptions().CASOpts = casOpts.CASOpts;
1768+
genericSubInvocation.getCASOptions().HasImmutableFileSystem =
1769+
casOpts.HasImmutableFileSystem;
17681770
casOpts.enumerateCASConfigurationFlags(
17691771
[&](StringRef Arg) { GenericArgs.push_back(ArgSaver.save(Arg)); });
17701772
// ClangIncludeTree is default on when caching is enabled.

test/CAS/cached_diagnostics.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ let _ : MyEnum? = .none // expected-warning {{assuming you mean 'Optional<MyEnum
5858

5959
/// Verify the serialized diags have the right magic at the top.
6060
// CHECK-SERIALIZED: DIA
61+
62+
// RUN: %target-swift-frontend -c -cache-compile-job -module-name Test -O -cas-path %t/cas @%t/MyApp.cmd %s \
63+
// RUN: -typecheck -serialize-diagnostics -serialize-diagnostics-path %t/test.diag -Rcache-compile-job 2>&1 | %FileCheck %s -check-prefix CACHE-MISS
64+
// RUN: %target-swift-frontend -c -cache-compile-job -module-name Test -O -cas-path %t/cas @%t/MyApp.cmd %s \
65+
// RUN: -typecheck -serialize-diagnostics -serialize-diagnostics-path %t/test.diag -Rcache-compile-job 2>&1 | %FileCheck %s -check-prefix CACHE-HIT
66+
// CACHE-MISS: remark: cache miss
67+
// CACHE-HIT: remark: replay output file '<cached-diagnostics>'

0 commit comments

Comments
 (0)