Skip to content

Commit 3071410

Browse files
[CAS] Teach swift-cache-tool about plugin-cas
1 parent 02c26e7 commit 3071410

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

lib/DriverTool/SwiftCacheToolOptions.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ def cache_tool_action: JoinedOrSeparate<["-"], "cache-tool-action">,
1515

1616
def cas_path: Separate<["-"], "cas-path">,
1717
HelpText<"Path to CAS">, MetaVarName<"<path>">;
18+
19+
def cas_plugin_path: Separate<["-"], "cas-plugin-path">,
20+
HelpText<"Path to CAS Plugin">, MetaVarName<"<path>">;
21+
22+
def cas_plugin_option: Separate<["-"], "cas-plugin-option">,
23+
HelpText<"Option pass to CAS Plugin">, MetaVarName<"<name>=<option>">;

lib/DriverTool/swift_cache_tool_main.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
#include "swift/Frontend/Frontend.h"
2424
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2525
#include "swift/Parse/ParseVersion.h"
26+
#include "clang/CAS/CASOptions.h"
2627
#include "llvm/CAS/ActionCache.h"
2728
#include "llvm/CAS/BuiltinUnifiedCASDatabases.h"
2829
#include "llvm/CAS/ObjectStore.h"
2930
#include "llvm/Option/OptTable.h"
31+
#include "llvm/Support/Allocator.h"
3032
#include "llvm/Support/Error.h"
3133
#include "llvm/Support/JSON.h"
3234
#include "llvm/Support/MemoryBuffer.h"
35+
#include "llvm/Support/StringSaver.h"
3336
#include <memory>
3437

3538
using namespace swift;
@@ -87,7 +90,7 @@ class SwiftCacheToolInvocation {
8790
CompilerInvocation Invocation;
8891
PrintingDiagnosticConsumer PDC;
8992
std::string MainExecutablePath;
90-
std::string CASPath;
93+
clang::CASOptions CASOpts;
9194
std::vector<std::string> Inputs;
9295
std::vector<std::string> FrontendArgs;
9396
SwiftCacheToolAction ActionKind = SwiftCacheToolAction::Invalid;
@@ -124,8 +127,19 @@ class SwiftCacheToolInvocation {
124127
return 0;
125128
}
126129

127-
CASPath =
128-
ParsedArgs.getLastArgValue(OPT_cas_path, getDefaultOnDiskCASPath());
130+
if (const Arg* PluginPath = ParsedArgs.getLastArg(OPT_cas_plugin_path))
131+
CASOpts.PluginPath = PluginPath->getValue();
132+
if (const Arg* OnDiskPath = ParsedArgs.getLastArg(OPT_cas_path))
133+
CASOpts.CASPath = OnDiskPath->getValue();
134+
for (StringRef Opt : ParsedArgs.getAllArgValues(OPT_cas_plugin_option)) {
135+
StringRef Name, Value;
136+
std::tie(Name, Value) = Opt.split('=');
137+
CASOpts.PluginOptions.emplace_back(std::string(Name), std::string(Value));
138+
}
139+
140+
// Fallback to default path if not set.
141+
if (CASOpts.CASPath.empty() && CASOpts.PluginPath.empty())
142+
CASOpts.CASPath = getDefaultOnDiskCASPath();
129143

130144
Inputs = ParsedArgs.getAllArgValues(OPT_INPUT);
131145
FrontendArgs = ParsedArgs.getAllArgValues(OPT__DASH_DASH);
@@ -186,8 +200,21 @@ class SwiftCacheToolInvocation {
186200

187201
// Make sure CASPath is the same between invocation and cache-tool by
188202
// appending the cas-path since the option doesn't affect cache key.
189-
Args.emplace_back("-cas-path");
190-
Args.emplace_back(CASPath.c_str());
203+
if (!CASOpts.CASPath.empty()) {
204+
Args.emplace_back("-cas-path");
205+
Args.emplace_back(CASOpts.CASPath.c_str());
206+
}
207+
if (!CASOpts.PluginPath.empty()) {
208+
Args.emplace_back("-cas-plugin-path");
209+
Args.emplace_back(CASOpts.PluginPath.c_str());
210+
}
211+
llvm::BumpPtrAllocator Alloc;
212+
llvm::StringSaver Saver(Alloc);
213+
for (const auto& Opt: CASOpts.PluginOptions) {
214+
StringRef OptValue = Saver.save(Opt.first + "=" + Opt.second);
215+
Args.emplace_back("-cas-plugin-option");
216+
Args.emplace_back(OptValue.str().c_str());
217+
}
191218

192219
if (Invocation.parseArgs(Args, Instance.getDiags(),
193220
&configurationFileBuffers, workingDirectory,
@@ -336,7 +363,7 @@ readOutputEntriesFromFile(StringRef Path) {
336363
}
337364

338365
int SwiftCacheToolInvocation::validateOutputs() {
339-
auto DB = llvm::cas::createOnDiskUnifiedCASDatabases(CASPath);
366+
auto DB = CASOpts.getOrCreateDatabases();
340367
if (!DB)
341368
report_fatal_error(DB.takeError());
342369

lib/Frontend/CompileJobCacheKey.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ llvm::Expected<llvm::cas::ObjectRef> swift::createCompileJobBaseCacheKey(
3939
"-index-unit-output-path-filelist",
4040
"-serialize-diagnostics-path",
4141
"-num-threads",
42-
"-cas-path"};
42+
"-cas-path",
43+
"-cas-plugin-path"};
4344

4445
// Don't count the `-frontend` in the first location since only frontend
4546
// invocation can have a cache key.

test/CAS/cache_key_compute.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,38 @@
4444
// RUN: %target-swift-frontend -cache-compile-job %s -emit-module -c -emit-dependencies \
4545
// RUN: -emit-tbd -emit-tbd-path %t/test.tbd -o %t/test.o -allow-unstable-cache-key-for-testing | %FileCheck %s
4646

47+
/// Test plugin CAS.
48+
// RUN: %cache-tool -cas-path %t/cas -cas-plugin-path %llvm_libs_dir/libCASPluginTest%llvm_plugin_ext \
49+
// RUN: -cas-plugin-option first-prefix=myfirst- -cache-tool-action print-output-keys -- \
50+
// RUN: %target-swift-frontend -cache-compile-job %s -emit-module -c -emit-dependencies \
51+
// RUN: -emit-tbd -emit-tbd-path %t/test.tbd -o %t/test.o -allow-unstable-cache-key-for-testing | %FileCheck %s --check-prefix=CHECK --check-prefix=PLUGIN
52+
4753
// CHECK: test.o
4854
// CHECK-NEXT: "OutputKind": "object"
4955
// CHECK-NEXT: "Input"
5056
// CHECK-NEXT: "CacheKey"
57+
// PLUGIN-SAME: myfirst-llvmcas://
5158

5259
// CHECK: test.swiftmodule
5360
// CHECK-NEXT: "OutputKind": "swiftmodule"
5461
// CHECK-NEXT: "Input"
5562
// CHECK-NEXT: "CacheKey"
63+
// PLUGIN-SAME: myfirst-llvmcas://
5664

5765
// CHECK: test.d
5866
// CHECK-NEXT: "OutputKind": "dependencies"
5967
// CHECK-NEXT: "Input"
6068
// CHECK-NEXT: "CacheKey"
69+
// PLUGIN-SAME: myfirst-llvmcas://
6170

6271
// CHECK: test.tbd
6372
// CHECK-NEXT: "OutputKind": "tbd"
6473
// CHECK-NEXT: "Input"
6574
// CHECK-NEXT: "CacheKey"
75+
// PLUGIN-SAME: myfirst-llvmcas://
6676

6777
// CHECK: <cached-diagnostics>
6878
// CHECK-NEXT: "OutputKind": "cached-diagnostics"
6979
// CHECK-NEXT: "Input": "<cached-diagnostics>"
7080
// CHECK-NEXT: "CacheKey"
81+
// PLUGIN-SAME: myfirst-llvmcas://

0 commit comments

Comments
 (0)