Skip to content

Commit 09af193

Browse files
[CAS] Improve swift cas options
Using the same CASOption from clang to communicate CAS configurations so it is easier to exchange CAS configuration between them.
1 parent c5018f3 commit 09af193

16 files changed

+289
-40
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ REMARK(replay_output, none, "replay output file '%0': key '%1'", (StringRef, Str
494494
REMARK(output_cache_miss, none, "cache miss output file '%0': key '%1'", (StringRef, StringRef))
495495

496496
// CAS related diagnostics
497-
ERROR(error_create_cas, none, "failed to create CAS '%0' (%1)", (StringRef, StringRef))
498497
ERROR(error_invalid_cas_id, none, "invalid CASID '%0' (%1)", (StringRef, StringRef))
499498
ERROR(error_cas, none, "CAS error encountered: %0", (StringRef))
500499

include/swift/AST/ModuleDependencies.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct CommonSwiftTextualModuleDependencyDetails {
152152
const std::vector<std::string> extraPCMArgs;
153153

154154
/// Bridging header file, if there is one.
155-
Optional<std::string> bridgingHeaderFile;
155+
llvm::Optional<std::string> bridgingHeaderFile;
156156

157157
/// Source files on which the bridging header depends.
158158
std::vector<std::string> bridgingSourceFiles;

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Basic/LLVM.h"
2424
#include "swift/Basic/Version.h"
2525
#include "swift/Config.h"
26+
#include "clang/CAS/CASOptions.h"
2627
#include "llvm/ADT/ArrayRef.h"
2728
#include "llvm/ADT/Hashing.h"
2829
#include "llvm/ADT/SmallString.h"
@@ -820,7 +821,7 @@ namespace swift {
820821
std::string Optimization;
821822

822823
/// clang CASOptions.
823-
std::string CASPath;
824+
llvm::Optional<clang::CASOptions> CASOpts;
824825

825826
/// Cache key for imported bridging header.
826827
std::string BridgingHeaderPCHCacheKey;

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/Hashing.h"
2222
#include "llvm/ADT/Optional.h"
2323
#include "llvm/ADT/StringMap.h"
24+
#include "clang/CAS/CASOptions.h"
2425

2526
#include <string>
2627
#include <vector>
@@ -125,8 +126,8 @@ class FrontendOptions {
125126
/// Use CAS.
126127
bool EnableCAS = false;
127128

128-
/// The CAS Path.
129-
std::string CASPath;
129+
/// CASOptions
130+
clang::CASOptions CASOpts;
130131

131132
/// CASFS Root.
132133
std::vector<std::string> CASFSRootIDs;

include/swift/Option/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,14 @@ def cas_path: Separate<["-"], "cas-path">,
18161816
Flags<[FrontendOption, NewDriverOnlyOption]>,
18171817
HelpText<"Path to CAS">, MetaVarName<"<path>">;
18181818

1819+
def cas_plugin_path: Separate<["-"], "cas-plugin-path">,
1820+
Flags<[FrontendOption, NewDriverOnlyOption]>,
1821+
HelpText<"Path to CAS Plugin">, MetaVarName<"<path>">;
1822+
1823+
def cas_plugin_option: Separate<["-"], "cas-plugin-option">,
1824+
Flags<[FrontendOption, NewDriverOnlyOption]>,
1825+
HelpText<"Option pass to CAS Plugin">, MetaVarName<"<name>=<option>">;
1826+
18191827
def allow_unstable_cache_key_for_testing: Flag<["-"], "allow-unstable-cache-key-for-testing">,
18201828
Flags<[FrontendOption, HelpHidden, NoDriverOption]>,
18211829
HelpText<"Allow compilation caching with unstable inputs for testing purpose">;

lib/AST/ModuleDependencies.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,12 @@ void SwiftDependencyScanningService::setupCachingDependencyScanningService(
454454
auto CachingFS =
455455
llvm::cas::createCachingOnDiskFileSystem(Instance.getObjectStore());
456456
if (!CachingFS) {
457-
Instance.getDiags().diagnose(SourceLoc(), diag::error_create_cas,
458-
"CachingOnDiskFS",
457+
Instance.getDiags().diagnose(SourceLoc(), diag::error_cas,
459458
toString(CachingFS.takeError()));
460459
return;
461460
}
462461
CacheFS = std::move(*CachingFS);
463462

464-
clang::CASOptions CASOpts;
465-
CASOpts.CASPath = Instance.getInvocation().getFrontendOptions().CASPath;
466-
CASOpts.ensurePersistentCAS();
467-
468463
UseClangIncludeTree =
469464
Instance.getInvocation().getClangImporterOptions().UseClangIncludeTree;
470465
const clang::tooling::dependencies::ScanningOutputFormat ClangScanningFormat =
@@ -474,8 +469,9 @@ void SwiftDependencyScanningService::setupCachingDependencyScanningService(
474469

475470
ClangScanningService.emplace(
476471
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,
477-
ClangScanningFormat, CASOpts, Instance.getSharedCASInstance(),
478-
Instance.getSharedCacheInstance(),
472+
ClangScanningFormat,
473+
Instance.getInvocation().getFrontendOptions().CASOpts,
474+
Instance.getSharedCASInstance(), Instance.getSharedCacheInstance(),
479475
UseClangIncludeTree ? nullptr : CacheFS,
480476
/* ReuseFileManager */ false, /* OptimizeArgs */ false);
481477
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,13 +709,28 @@ importer::getNormalInvocationArguments(
709709
llvm::sys::path::get_separator() +
710710
"apinotes").str());
711711

712-
if (!importerOpts.CASPath.empty()) {
713-
invocationArgStrs.push_back("-Xclang");
714-
invocationArgStrs.push_back("-fcas-path");
715-
invocationArgStrs.push_back("-Xclang");
716-
invocationArgStrs.push_back(importerOpts.CASPath);
712+
if (importerOpts.CASOpts) {
717713
invocationArgStrs.push_back("-Xclang");
718714
invocationArgStrs.push_back("-fno-pch-timestamp");
715+
if (!importerOpts.CASOpts->CASPath.empty()) {
716+
invocationArgStrs.push_back("-Xclang");
717+
invocationArgStrs.push_back("-fcas-path");
718+
invocationArgStrs.push_back("-Xclang");
719+
invocationArgStrs.push_back(importerOpts.CASOpts->CASPath);
720+
}
721+
if (!importerOpts.CASOpts->PluginPath.empty()) {
722+
invocationArgStrs.push_back("-Xclang");
723+
invocationArgStrs.push_back("-fcas-plugin-path");
724+
invocationArgStrs.push_back("-Xclang");
725+
invocationArgStrs.push_back(importerOpts.CASOpts->PluginPath);
726+
for (auto Opt : importerOpts.CASOpts->PluginOptions) {
727+
invocationArgStrs.push_back("-Xclang");
728+
invocationArgStrs.push_back("-fcas-plugin-option");
729+
invocationArgStrs.push_back("-Xclang");
730+
invocationArgStrs.push_back(
731+
(llvm::Twine(Opt.first) + "=" + Opt.second).str());
732+
}
733+
}
719734
}
720735
}
721736

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,21 @@ void ClangImporter::recordModuleDependencies(
238238
std::string IncludeTree =
239239
clangModuleDep.IncludeTreeID ? *clangModuleDep.IncludeTreeID : "";
240240

241-
if (!RootID.empty() || !IncludeTree.empty()) {
241+
if (ctx.ClangImporterOpts.CASOpts) {
242242
swiftArgs.push_back("-enable-cas");
243-
swiftArgs.push_back("-cas-path");
244-
swiftArgs.push_back(ctx.ClangImporterOpts.CASPath);
243+
if (!ctx.ClangImporterOpts.CASOpts->CASPath.empty()) {
244+
swiftArgs.push_back("-cas-path");
245+
swiftArgs.push_back(ctx.ClangImporterOpts.CASOpts->CASPath);
246+
}
247+
if (!ctx.ClangImporterOpts.CASOpts->PluginPath.empty()) {
248+
swiftArgs.push_back("-cas-plugin-path");
249+
swiftArgs.push_back(ctx.ClangImporterOpts.CASOpts->PluginPath);
250+
for (auto Opt : ctx.ClangImporterOpts.CASOpts->PluginOptions) {
251+
swiftArgs.push_back("-cas-plugin-option");
252+
swiftArgs.push_back(
253+
(llvm::Twine(Opt.first) + "=" + Opt.second).str());
254+
}
255+
}
245256
}
246257

247258
if (!RootID.empty()) {
@@ -334,10 +345,20 @@ void ClangImporter::recordBridgingHeaderOptions(
334345

335346
llvm::for_each(clangArgs, addClangArg);
336347

337-
if (!ctx.ClangImporterOpts.CASPath.empty()) {
348+
if (ctx.ClangImporterOpts.CASOpts) {
338349
swiftArgs.push_back("-enable-cas");
339-
swiftArgs.push_back("-cas-path");
340-
swiftArgs.push_back(ctx.ClangImporterOpts.CASPath);
350+
if (!ctx.ClangImporterOpts.CASOpts->CASPath.empty()) {
351+
swiftArgs.push_back("-cas-path");
352+
swiftArgs.push_back(ctx.ClangImporterOpts.CASOpts->CASPath);
353+
}
354+
if (!ctx.ClangImporterOpts.CASOpts->PluginPath.empty()) {
355+
swiftArgs.push_back("-cas-plugin-path");
356+
swiftArgs.push_back(ctx.ClangImporterOpts.CASOpts->PluginPath);
357+
for (auto Opt : ctx.ClangImporterOpts.CASOpts->PluginOptions) {
358+
swiftArgs.push_back("-cas-plugin-option");
359+
swiftArgs.push_back((llvm::Twine(Opt.first) + "=" + Opt.second).str());
360+
}
361+
}
341362
}
342363

343364
if (auto Tree = deps.IncludeTreeID) {

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,16 @@ bool ArgsToFrontendOptionsConverter::convert(
352352
}
353353

354354
Opts.EnableCAS = Args.hasArg(OPT_enable_cas);
355-
Opts.CASPath =
355+
Opts.CASOpts.CASPath =
356356
Args.getLastArgValue(OPT_cas_path, llvm::cas::getDefaultOnDiskCASPath());
357+
Opts.CASOpts.PluginPath = Args.getLastArgValue(OPT_cas_plugin_path);
358+
for (StringRef Opt : Args.getAllArgValues(OPT_cas_plugin_option)) {
359+
StringRef Name, Value;
360+
std::tie(Name, Value) = Opt.split('=');
361+
Opts.CASOpts.PluginOptions.emplace_back(std::string(Name),
362+
std::string(Value));
363+
}
364+
357365
Opts.CASFSRootIDs = Args.getAllArgValues(OPT_cas_fs);
358366
Opts.ClangIncludeTrees = Args.getAllArgValues(OPT_clang_include_tree_root);
359367

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
15061506
// Forward the FrontendOptions to clang importer option so it can be
15071507
// accessed when creating clang module compilation invocation.
15081508
if (FrontendOpts.EnableCAS)
1509-
Opts.CASPath = FrontendOpts.CASPath;
1509+
Opts.CASOpts = FrontendOpts.CASOpts;
15101510

15111511
return false;
15121512
}

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,13 @@ bool CompilerInstance::setupCASIfNeeded(ArrayRef<const char *> Args) {
406406
if (!Opts.EnableCAS)
407407
return false;
408408

409-
auto MaybeCache = llvm::cas::createOnDiskUnifiedCASDatabases(Opts.CASPath);
410-
if (!MaybeCache) {
411-
Diagnostics.diagnose(SourceLoc(), diag::error_create_cas, Opts.CASPath,
412-
toString(MaybeCache.takeError()));
409+
auto MaybeDB= Opts.CASOpts.getOrCreateDatabases();
410+
if (!MaybeDB) {
411+
Diagnostics.diagnose(SourceLoc(), diag::error_cas,
412+
toString(MaybeDB.takeError()));
413413
return true;
414414
}
415-
CAS = std::move(MaybeCache->first);
416-
ResultCache = std::move(MaybeCache->second);
415+
std::tie(CAS, ResultCache) = *MaybeDB;
417416

418417
// create baseline key.
419418
auto BaseKey = createCompileJobBaseCacheKey(*CAS, Args);

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "llvm/ADT/Hashing.h"
4040
#include "llvm/ADT/STLExtras.h"
4141
#include "llvm/ADT/StringExtras.h"
42+
#include "llvm/ADT/Twine.h"
4243
#include "llvm/CAS/ActionCache.h"
4344
#include "llvm/CAS/ObjectStore.h"
4445
#include "llvm/Support/CommandLine.h"
@@ -1600,12 +1601,23 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
16001601
GenericArgs.push_back(clangImporterOpts.BuildSessionFilePath);
16011602
}
16021603

1603-
if (!clangImporterOpts.CASPath.empty()) {
1604-
genericSubInvocation.getClangImporterOptions().CASPath =
1605-
clangImporterOpts.CASPath;
1604+
if (clangImporterOpts.CASOpts) {
1605+
genericSubInvocation.getClangImporterOptions().CASOpts =
1606+
clangImporterOpts.CASOpts;
16061607
GenericArgs.push_back("-enable-cas");
1607-
GenericArgs.push_back("-cas-path");
1608-
GenericArgs.push_back(clangImporterOpts.CASPath);
1608+
if (!clangImporterOpts.CASOpts->CASPath.empty()) {
1609+
GenericArgs.push_back("-cas-path");
1610+
GenericArgs.push_back(clangImporterOpts.CASOpts->CASPath);
1611+
}
1612+
if (!clangImporterOpts.CASOpts->PluginPath.empty()) {
1613+
GenericArgs.push_back("-cas-plugin-path");
1614+
GenericArgs.push_back(clangImporterOpts.CASOpts->PluginPath);
1615+
for (auto Opt : clangImporterOpts.CASOpts->PluginOptions) {
1616+
GenericArgs.push_back("-cas-plugin-option");
1617+
std::string pair = (llvm::Twine(Opt.first) + "=" + Opt.second).str();
1618+
GenericArgs.push_back(ArgSaver.save(pair));
1619+
}
1620+
}
16091621
}
16101622

16111623
if (clangImporterOpts.UseClangIncludeTree) {
@@ -1702,7 +1714,7 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
17021714
// required by sourcekitd.
17031715
subClangImporterOpts.DetailedPreprocessingRecord =
17041716
clangImporterOpts.DetailedPreprocessingRecord;
1705-
subClangImporterOpts.CASPath = clangImporterOpts.CASPath;
1717+
subClangImporterOpts.CASOpts = clangImporterOpts.CASOpts;
17061718

17071719
// If the compiler has been asked to be strict with ensuring downstream dependencies
17081720
// get the parent invocation's context, or this is an Explicit build, inherit the

0 commit comments

Comments
 (0)