Skip to content

Commit 9607e9d

Browse files
Merge pull request #71497 from cachemeifyoucan/eng/PR-122423965
[Caching] Use subInvocation to verify interface
2 parents b23bc82 + ebc90ff commit 9607e9d

36 files changed

+294
-220
lines changed

include/swift/AST/ASTContext.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/AST/Type.h"
2828
#include "swift/AST/TypeAlignments.h"
2929
#include "swift/AST/Types.h"
30+
#include "swift/Basic/CASOptions.h"
3031
#include "swift/Basic/LangOptions.h"
3132
#include "swift/Basic/Located.h"
3233
#include "swift/Basic/Malloc.h"
@@ -239,10 +240,9 @@ class ASTContext final {
239240
LangOptions &langOpts, TypeCheckerOptions &typecheckOpts,
240241
SILOptions &silOpts, SearchPathOptions &SearchPathOpts,
241242
ClangImporterOptions &ClangImporterOpts,
242-
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
243+
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts, CASOptions &casOpts,
243244
SourceManager &SourceMgr, DiagnosticEngine &Diags,
244-
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutBackend = nullptr
245-
);
245+
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutBackend = nullptr);
246246

247247
public:
248248
// Members that should only be used by ASTContext.cpp.
@@ -257,10 +257,9 @@ class ASTContext final {
257257
get(LangOptions &langOpts, TypeCheckerOptions &typecheckOpts,
258258
SILOptions &silOpts, SearchPathOptions &SearchPathOpts,
259259
ClangImporterOptions &ClangImporterOpts,
260-
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
260+
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts, CASOptions &casOpts,
261261
SourceManager &SourceMgr, DiagnosticEngine &Diags,
262-
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutBackend = nullptr
263-
);
262+
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutBackend = nullptr);
264263
~ASTContext();
265264

266265
/// Optional table of counters to report, nullptr when not collecting.
@@ -287,6 +286,9 @@ class ASTContext final {
287286
/// The symbol graph generation options used by this AST context.
288287
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts;
289288

289+
/// The CAS options used by this AST context.
290+
const CASOptions &CASOpts;
291+
290292
/// The source manager object.
291293
SourceManager &SourceMgr;
292294

include/swift/Basic/CASOptions.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===--- CASOptions.h - CAS & caching options -------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the CASOptions class, which provides various
14+
// CAS and caching flags.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_BASIC_CASOPTIONS_H
19+
#define SWIFT_BASIC_CASOPTIONS_H
20+
21+
#include "clang/CAS/CASOptions.h"
22+
23+
namespace swift {
24+
25+
class CASOptions final {
26+
public:
27+
/// Enable compiler caching.
28+
bool EnableCaching = false;
29+
30+
/// Enable compiler caching remarks.
31+
bool EnableCachingRemarks = false;
32+
33+
/// Skip replaying outputs from cache.
34+
bool CacheSkipReplay = false;
35+
36+
/// CASOptions
37+
clang::CASOptions CASOpts;
38+
39+
/// CASFS Root.
40+
std::vector<std::string> CASFSRootIDs;
41+
42+
/// Clang Include Trees.
43+
std::vector<std::string> ClangIncludeTrees;
44+
45+
/// CacheKey for input file.
46+
std::string InputFileKey;
47+
48+
/// Cache key for imported bridging header.
49+
std::string BridgingHeaderPCHCacheKey;
50+
51+
/// Get the CAS configuration flags.
52+
void enumerateCASConfigurationFlags(
53+
llvm::function_ref<void(llvm::StringRef)> Callback) const;
54+
55+
/// Check to see if a CASFileSystem is required.
56+
bool requireCASFS() const {
57+
return EnableCaching &&
58+
(!CASFSRootIDs.empty() || !ClangIncludeTrees.empty() ||
59+
!InputFileKey.empty() || !BridgingHeaderPCHCacheKey.empty());
60+
}
61+
};
62+
63+
} // namespace swift
64+
65+
#endif // SWIFT_BASIC_CASOPTIONS_H

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -884,12 +884,6 @@ namespace swift {
884884
/// import, but it can affect Clang's IR generation of static functions.
885885
std::string Optimization;
886886

887-
/// clang CASOptions.
888-
llvm::Optional<clang::CASOptions> CASOpts;
889-
890-
/// Cache key for imported bridging header.
891-
std::string BridgingHeaderPCHCacheKey;
892-
893887
/// Disable validating the persistent PCH.
894888
bool PCHDisableValidation = false;
895889

include/swift/Frontend/Frontend.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/SILOptions.h"
2727
#include "swift/AST/SearchPathOptions.h"
2828
#include "swift/AST/SourceFile.h"
29+
#include "swift/Basic/CASOptions.h"
2930
#include "swift/Basic/DiagnosticOptions.h"
3031
#include "swift/Basic/LangOptions.h"
3132
#include "swift/Basic/SourceManager.h"
@@ -102,6 +103,7 @@ class CompilerInvocation {
102103
IRGenOptions IRGenOpts;
103104
TBDGenOptions TBDGenOpts;
104105
ModuleInterfaceOptions ModuleInterfaceOpts;
106+
CASOptions CASOpts;
105107
llvm::MemoryBuffer *IDEInspectionTargetBuffer = nullptr;
106108

107109
/// The offset that IDEInspection wants to further examine in offset of bytes
@@ -166,7 +168,7 @@ class CompilerInvocation {
166168
}
167169

168170
bool requiresCAS() const {
169-
return FrontendOpts.EnableCaching || FrontendOpts.UseCASBackend;
171+
return CASOpts.EnableCaching || IRGenOpts.UseCASBackend;
170172
}
171173

172174
void setClangModuleCachePath(StringRef Path) {
@@ -274,6 +276,9 @@ class CompilerInvocation {
274276
FrontendOptions &getFrontendOptions() { return FrontendOpts; }
275277
const FrontendOptions &getFrontendOptions() const { return FrontendOpts; }
276278

279+
CASOptions &getCASOptions() { return CASOpts; }
280+
const CASOptions &getCASOptions() const { return CASOpts; }
281+
277282
TBDGenOptions &getTBDGenOptions() { return TBDGenOpts; }
278283
const TBDGenOptions &getTBDGenOptions() const { return TBDGenOpts; }
279284

include/swift/Frontend/FrontendOptions.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -129,33 +129,6 @@ class FrontendOptions {
129129
/// The module for which we should verify all of the generic signatures.
130130
std::string VerifyGenericSignaturesInModule;
131131

132-
/// Enable compiler caching.
133-
bool EnableCaching = false;
134-
135-
/// Enable compiler caching remarks.
136-
bool EnableCachingRemarks = false;
137-
138-
/// Skip replaying outputs from cache.
139-
bool CacheSkipReplay = false;
140-
141-
/// CASOptions
142-
clang::CASOptions CASOpts;
143-
144-
/// CASFS Root.
145-
std::vector<std::string> CASFSRootIDs;
146-
147-
/// Clang Include Trees.
148-
std::vector<std::string> ClangIncludeTrees;
149-
150-
/// CacheKey for input file.
151-
std::string InputFileKey;
152-
153-
/// Enable using the LLVM MCCAS backend for object file output.
154-
bool UseCASBackend = false;
155-
156-
/// The output mode for the CAS Backend.
157-
llvm::CASBackendMode CASObjMode;
158-
159132
/// Emit a .casid file next to the object file if CAS Backend is used.
160133
bool EmitCASIDFile = false;
161134

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,12 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
596596
static bool buildSwiftModuleFromSwiftInterface(
597597
SourceManager &SourceMgr, DiagnosticEngine &Diags,
598598
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
599-
const ClangImporterOptions &ClangOpts, StringRef CacheDir,
600-
StringRef PrebuiltCacheDir, StringRef BackupInterfaceDir,
601-
StringRef ModuleName, StringRef InPath,
599+
const ClangImporterOptions &ClangOpts, const CASOptions &CASOpts,
600+
StringRef CacheDir, StringRef PrebuiltCacheDir,
601+
StringRef BackupInterfaceDir, StringRef ModuleName, StringRef InPath,
602602
StringRef OutPath, StringRef ABIOutputPath,
603-
bool SerializeDependencyHashes,
604-
bool TrackSystemDependencies, ModuleInterfaceLoaderOptions Opts,
603+
bool SerializeDependencyHashes, bool TrackSystemDependencies,
604+
ModuleInterfaceLoaderOptions Opts,
605605
RequireOSSAModules_t RequireOSSAModules,
606606
RequireNoncopyableGenerics_t RequireNCGenerics,
607607
bool silenceInterfaceDiagnostics);
@@ -657,6 +657,7 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
657657
inheritOptionsForBuildingInterface(const SearchPathOptions &SearchPathOpts,
658658
const LangOptions &LangOpts,
659659
const ClangImporterOptions &clangImporterOpts,
660+
const CASOptions &casOpts,
660661
bool suppressRemarks,
661662
RequireOSSAModules_t requireOSSAModules,
662663
RequireNoncopyableGenerics_t requireNCGenerics);
@@ -668,12 +669,11 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
668669
InterfaceSubContextDelegateImpl(
669670
SourceManager &SM, DiagnosticEngine *Diags,
670671
const SearchPathOptions &searchPathOpts, const LangOptions &langOpts,
671-
const ClangImporterOptions &clangImporterOpts,
672+
const ClangImporterOptions &clangImporterOpts, const CASOptions &casOpts,
672673
ModuleInterfaceLoaderOptions LoaderOpts, bool buildModuleCacheDirIfAbsent,
673674
StringRef moduleCachePath, StringRef prebuiltCachePath,
674-
StringRef backupModuleInterfaceDir,
675-
bool serializeDependencyHashes, bool trackSystemDependencies,
676-
RequireOSSAModules_t requireOSSAModules,
675+
StringRef backupModuleInterfaceDir, bool serializeDependencyHashes,
676+
bool trackSystemDependencies, RequireOSSAModules_t requireOSSAModules,
677677
RequireNoncopyableGenerics_t requireNCGenerics);
678678

679679
template<typename ...ArgTypes>

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ ASTContext *ASTContext::get(
677677
LangOptions &langOpts, TypeCheckerOptions &typecheckOpts,
678678
SILOptions &silOpts, SearchPathOptions &SearchPathOpts,
679679
ClangImporterOptions &ClangImporterOpts,
680-
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
680+
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts, CASOptions &casOpts,
681681
SourceManager &SourceMgr, DiagnosticEngine &Diags,
682682
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutputBackend) {
683683
// If more than two data structures are concatentated, then the aggregate
@@ -692,23 +692,22 @@ ASTContext *ASTContext::get(
692692
new (impl) Implementation();
693693
return new (mem)
694694
ASTContext(langOpts, typecheckOpts, silOpts, SearchPathOpts,
695-
ClangImporterOpts, SymbolGraphOpts, SourceMgr, Diags,
695+
ClangImporterOpts, SymbolGraphOpts, casOpts, SourceMgr, Diags,
696696
std::move(OutputBackend));
697697
}
698698

699699
ASTContext::ASTContext(
700700
LangOptions &langOpts, TypeCheckerOptions &typecheckOpts,
701701
SILOptions &silOpts, SearchPathOptions &SearchPathOpts,
702702
ClangImporterOptions &ClangImporterOpts,
703-
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts,
703+
symbolgraphgen::SymbolGraphOptions &SymbolGraphOpts, CASOptions &casOpts,
704704
SourceManager &SourceMgr, DiagnosticEngine &Diags,
705-
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutBackend
706-
)
705+
llvm::IntrusiveRefCntPtr<llvm::vfs::OutputBackend> OutBackend)
707706
: LangOpts(langOpts), TypeCheckerOpts(typecheckOpts), SILOpts(silOpts),
708707
SearchPathOpts(SearchPathOpts), ClangImporterOpts(ClangImporterOpts),
709-
SymbolGraphOpts(SymbolGraphOpts), SourceMgr(SourceMgr), Diags(Diags),
710-
OutputBackend(std::move(OutBackend)), evaluator(Diags, langOpts),
711-
TheBuiltinModule(createBuiltinModule(*this)),
708+
SymbolGraphOpts(SymbolGraphOpts), CASOpts(casOpts), SourceMgr(SourceMgr),
709+
Diags(Diags), OutputBackend(std::move(OutBackend)),
710+
evaluator(Diags, langOpts), TheBuiltinModule(createBuiltinModule(*this)),
712711
StdlibModuleName(getIdentifier(STDLIB_NAME)),
713712
SwiftShimsModuleName(getIdentifier(SWIFT_SHIMS_NAME)),
714713
TheErrorType(new (*this, AllocationArena::Permanent) ErrorType(

lib/AST/ModuleDependencies.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,12 @@ SwiftDependencyTracker::createTreeFromDependencies() {
480480

481481
bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
482482
CompilerInstance &Instance) {
483-
if (!Instance.getInvocation().getFrontendOptions().EnableCaching)
483+
if (!Instance.getInvocation().getCASOptions().EnableCaching)
484484
return false;
485485

486486
if (CASOpts) {
487487
// If CASOption matches, the service is initialized already.
488-
if (*CASOpts == Instance.getInvocation().getFrontendOptions().CASOpts)
488+
if (*CASOpts == Instance.getInvocation().getCASOptions().CASOpts)
489489
return false;
490490

491491
// CASOption mismatch, return error.
@@ -496,7 +496,7 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
496496
}
497497

498498
// Setup CAS.
499-
CASOpts = Instance.getInvocation().getFrontendOptions().CASOpts;
499+
CASOpts = Instance.getInvocation().getCASOptions().CASOpts;
500500
CAS = Instance.getSharedCASInstance();
501501

502502
// Add SDKSetting file.
@@ -552,7 +552,7 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
552552
ClangScanningService.emplace(
553553
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,
554554
ClangScanningFormat,
555-
Instance.getInvocation().getFrontendOptions().CASOpts,
555+
Instance.getInvocation().getCASOptions().CASOpts,
556556
Instance.getSharedCASInstance(), Instance.getSharedCacheInstance(),
557557
UseClangIncludeTree ? nullptr : CacheFS);
558558

lib/Basic/CASOptions.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- CASOptions.cpp - CAS & caching options ---------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the CASOptions class, which provides various
14+
// CAS and caching flags.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#include "swift/Basic/CASOptions.h"
19+
20+
using namespace swift;
21+
22+
void CASOptions::enumerateCASConfigurationFlags(
23+
llvm::function_ref<void(llvm::StringRef)> Callback) const {
24+
if (EnableCaching) {
25+
Callback("-cache-compile-job");
26+
if (!CASOpts.CASPath.empty()) {
27+
Callback("-cas-path");
28+
Callback(CASOpts.CASPath);
29+
}
30+
if (!CASOpts.PluginPath.empty()) {
31+
Callback("-cas-plugin-path");
32+
Callback(CASOpts.PluginPath);
33+
for (auto Opt : CASOpts.PluginOptions) {
34+
Callback("-cas-plugin-option");
35+
Callback((llvm::Twine(Opt.first) + "=" + Opt.second).str());
36+
}
37+
}
38+
}
39+
}

lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ add_swift_host_library(swiftBasic STATIC
4545
BasicBridging.cpp
4646
BasicSourceInfo.cpp
4747
Cache.cpp
48+
CASOptions.cpp
4849
ClusteredBitVector.cpp
4950
DiverseStack.cpp
5051
Edit.cpp

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -718,21 +718,22 @@ importer::getNormalInvocationArguments(
718718
llvm::sys::path::get_separator() +
719719
"apinotes").str());
720720

721-
if (importerOpts.CASOpts) {
721+
auto CASOpts = ctx.CASOpts;
722+
if (CASOpts.EnableCaching) {
722723
invocationArgStrs.push_back("-Xclang");
723724
invocationArgStrs.push_back("-fno-pch-timestamp");
724-
if (!importerOpts.CASOpts->CASPath.empty()) {
725+
if (!CASOpts.CASOpts.CASPath.empty()) {
725726
invocationArgStrs.push_back("-Xclang");
726727
invocationArgStrs.push_back("-fcas-path");
727728
invocationArgStrs.push_back("-Xclang");
728-
invocationArgStrs.push_back(importerOpts.CASOpts->CASPath);
729+
invocationArgStrs.push_back(CASOpts.CASOpts.CASPath);
729730
}
730-
if (!importerOpts.CASOpts->PluginPath.empty()) {
731+
if (!CASOpts.CASOpts.PluginPath.empty()) {
731732
invocationArgStrs.push_back("-Xclang");
732733
invocationArgStrs.push_back("-fcas-plugin-path");
733734
invocationArgStrs.push_back("-Xclang");
734-
invocationArgStrs.push_back(importerOpts.CASOpts->PluginPath);
735-
for (auto Opt : importerOpts.CASOpts->PluginOptions) {
735+
invocationArgStrs.push_back(CASOpts.CASOpts.PluginPath);
736+
for (auto Opt : CASOpts.CASOpts.PluginOptions) {
736737
invocationArgStrs.push_back("-Xclang");
737738
invocationArgStrs.push_back("-fcas-plugin-option");
738739
invocationArgStrs.push_back("-Xclang");

0 commit comments

Comments
 (0)