Skip to content

Commit 993d39c

Browse files
authored
Merge pull request #6382 from akyrtzi/pr/stable/cas-plugin
[stable][CAS/plugin] Introduce a CAS plugin interface
2 parents da68a15 + c6a91f3 commit 993d39c

24 files changed

+1411
-79
lines changed

clang/include/clang/Basic/DiagnosticCASKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ let Component = "CAS" in {
1111
def err_builtin_cas_cannot_be_initialized : Error<
1212
"CAS cannot be initialized from '%0' on disk (check -fcas-path): %1">,
1313
DefaultFatal;
14+
def err_plugin_cas_cannot_be_initialized : Error<
15+
"plugin CAS cannot be initialized from '%0': %1">,
16+
DefaultFatal;
1417
def err_builtin_actioncache_cannot_be_initialized : Error<
1518
"ActionCache cannot be initialized from '%0' on disk (check -fcas-path)">,
1619
DefaultFatal;

clang/include/clang/CAS/CASOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/ADT/SmallVector.h"
1818
#include <string>
19+
#include <vector>
1920

2021
namespace llvm {
2122
namespace cas {
@@ -56,6 +57,10 @@ class CASConfiguration {
5657
/// system cache.
5758
std::string CASPath;
5859

60+
std::string PluginPath;
61+
/// Each entry is a (<option-name>, <value>) pair.
62+
std::vector<std::pair<std::string, std::string>> PluginOptions;
63+
5964
friend bool operator==(const CASConfiguration &LHS,
6065
const CASConfiguration &RHS) {
6166
return LHS.CASPath == RHS.CASPath;

clang/include/clang/Driver/Options.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6591,6 +6591,16 @@ def fcas_path : Separate<["-"], "fcas-path">,
65916591
" '-fcas-path=auto' chooses a path in the user's system cache.">,
65926592
MarshallingInfoString<CASOpts<"CASPath">>;
65936593

6594+
def fcas_plugin_path : Separate<["-"], "fcas-plugin-path">,
6595+
Group<f_Group>, MetaVarName<"<pathname>">,
6596+
HelpText<"Path to a shared library implementing the LLVM CAS plugin API.">,
6597+
MarshallingInfoString<CASOpts<"PluginPath">>;
6598+
6599+
def fcas_plugin_option : Separate<["-"], "fcas-plugin-option">,
6600+
Group<f_Group>, MetaVarName<"<name>=<value>">,
6601+
HelpText<"Option to pass to the CAS plugin.">,
6602+
MarshallingInfoStringPairVector<CASOpts<"PluginOptions">>;
6603+
65946604
// FIXME: Add to driver once it's supported by -fdepscan.
65956605
def fcas_fs : Separate<["-"], "fcas-fs">,
65966606
Group<f_Group>, MetaVarName<"<tree>">,

clang/lib/CAS/CASOptions.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ void CASOptions::freezeConfig(DiagnosticsEngine &Diags) {
5959
// API and is observable.
6060
CurrentConfig.CASPath =
6161
Cache.CAS->getContext().getHashSchemaIdentifier().str();
62+
CurrentConfig.PluginPath.clear();
63+
CurrentConfig.PluginOptions.clear();
6264
}
6365
}
6466

@@ -82,6 +84,20 @@ void CASOptions::initCache(DiagnosticsEngine &Diags) const {
8284

8385
Cache.Config = CurrentConfig;
8486
StringRef CASPath = Cache.Config.CASPath;
87+
88+
if (!PluginPath.empty()) {
89+
std::pair<std::unique_ptr<ObjectStore>, std::unique_ptr<ActionCache>> DBs;
90+
if (llvm::Error E =
91+
createPluginCASDatabases(PluginPath, CASPath, PluginOptions)
92+
.moveInto(DBs)) {
93+
Diags.Report(diag::err_plugin_cas_cannot_be_initialized)
94+
<< PluginPath << toString(std::move(E));
95+
return;
96+
}
97+
std::tie(Cache.CAS, Cache.AC) = std::move(DBs);
98+
return;
99+
}
100+
85101
if (CASPath.empty()) {
86102
Cache.CAS = llvm::cas::createInMemoryCAS();
87103
Cache.AC = llvm::cas::createInMemoryActionCache();

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,31 @@ static void denormalizeStringVector(SmallVectorImpl<const char *> &Args,
403403
}
404404
}
405405

406+
using StringPair = std::pair<std::string, std::string>;
407+
408+
static std::optional<std::vector<std::pair<std::string, std::string>>>
409+
normalizeStringPairVector(OptSpecifier Opt, int, const ArgList &Args,
410+
DiagnosticsEngine &) {
411+
std::vector<std::pair<std::string, std::string>> Pairs;
412+
for (StringRef Arg : Args.getAllArgValues(Opt)) {
413+
auto [L, R] = Arg.split('=');
414+
Pairs.emplace_back(std::string(L), std::string(R));
415+
}
416+
return Pairs;
417+
}
418+
419+
static void denormalizeStringPairVector(
420+
SmallVectorImpl<const char *> &Args, const char *Spelling,
421+
CompilerInvocation::StringAllocator SA, Option::OptionClass OptClass,
422+
unsigned TableIndex,
423+
const std::vector<std::pair<std::string, std::string>> &Values) {
424+
std::vector<std::string> Joined;
425+
for (const auto &Pair : Values) {
426+
Joined.push_back(Pair.first + "=" + Pair.second);
427+
}
428+
denormalizeStringVector(Args, Spelling, SA, OptClass, TableIndex, Joined);
429+
}
430+
406431
static Optional<std::string> normalizeTriple(OptSpecifier Opt, int TableIndex,
407432
const ArgList &Args,
408433
DiagnosticsEngine &Diags) {

clang/test/CAS/driver-cache-launcher.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
// SPECIFIC-DAEMON-NOT: "-fdepscan-share-identifier"
3939
// SPECIFIC-DAEMON: "-cc1depscan" "-fdepscan=daemon" "-fdepscan-daemon=[[PREFIX]]/scand"
4040

41+
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas LLVM_CACHE_PLUGIN_PATH=%t/plugin LLVM_CACHE_PLUGIN_OPTIONS=some-opt=value \
42+
// RUN: %clang-cache %clang -c %s -o %t.o -### 2>&1 | FileCheck %s -check-prefix=PLUGIN -DPREFIX=%t
43+
// PLUGIN: "-fcas-path" "[[PREFIX]]/cas"
44+
// PLUGIN: "-fcas-plugin-path" "[[PREFIX]]/plugin"
45+
// PLUGIN: "-fcas-plugin-options" "some-opt=value"
46+
4147
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas LLVM_CACHE_REMOTE_SERVICE_SOCKET_PATH=%t/ccremote %clang-cache %clang -c %s -o %t.o -### 2>&1 | FileCheck %s -check-prefix=REMOTE -DPREFIX=%t
4248
// REMOTE: "-fcompilation-caching-service-path" "[[PREFIX]]/ccremote"
4349

clang/test/CAS/plugin-cas.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// REQUIRES: ondisk_cas
2+
3+
// RUN: rm -rf %t && mkdir -p %t
4+
5+
// RUN: %clang -cc1depscan -o %t/t.rsp -fdepscan=inline -cc1-args \
6+
// RUN: -cc1 %s -fcas-path %t/cas \
7+
// RUN: -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
8+
// RUN: -fcas-plugin-option first-prefix=myfirst- -fcas-plugin-option second-prefix=mysecond-
9+
// RUN: %clang @%t/t.rsp -emit-obj -o %t/t1.o -Rcompile-job-cache 2>&1 | FileCheck %s --check-prefix=CACHE-MISS
10+
// RUN: %clang @%t/t.rsp -emit-obj -o %t/t2.o -Rcompile-job-cache 2>&1 | FileCheck %s --check-prefix=CACHE-HIT
11+
// RUN: diff %t/t1.o %t/t2.o
12+
13+
// CACHE-MISS: remark: compile job cache miss for 'myfirst-mysecond-
14+
// CACHE-MISS: warning: some warning
15+
16+
// CACHE-HIT: remark: compile job cache hit for 'myfirst-mysecond-
17+
// CACHE-HIT: warning: some warning
18+
19+
// RUN: not %clang -cc1depscan -o %t/t.rsp -fdepscan=inline -cc1-args \
20+
// RUN: -cc1 %s -fcas-path %t/cas \
21+
// RUN: -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
22+
// RUN: -fcas-plugin-option no-such-option=2 2>&1 | FileCheck %s --check-prefix=FAIL-PLUGIN-OPT
23+
// FAIL-PLUGIN-OPT: fatal error: plugin CAS cannot be initialized {{.*}}: unknown option: no-such-option
24+
25+
// RUN: not %clang -cc1depscan -o %t/t.rsp -fdepscan=inline -cc1-args \
26+
// RUN: -cc1 %s -fcas-path %t/cas -fcas-plugin-path %t/non-existent 2>&1 | FileCheck %s --check-prefix=NOTEXISTENT
27+
// NOTEXISTENT: fatal error: plugin CAS cannot be initialized
28+
29+
#warning some warning
30+
void test() {}

clang/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ if( NOT CLANG_BUILT_STANDALONE )
110110
list(APPEND CLANG_TEST_DEPS
111111
llvm-config
112112
FileCheck count not
113+
CASPluginTest
113114
llc
114115
llvm-ar
115116
llvm-as

clang/tools/driver/CacheLauncherMode.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,24 @@ static void addCommonArgs(bool ForDriver, SmallVectorImpl<const char *> &Args,
9191
if (!llvm::sys::Process::GetEnv("CLANG_CACHE_USE_CASFS_DEPSCAN")) {
9292
Args.push_back("-fdepscan-include-tree");
9393
}
94-
if (auto CASPath = llvm::sys::Process::GetEnv("LLVM_CACHE_CAS_PATH")) {
95-
if (ForDriver) {
96-
Args.append(
97-
{"-Xclang", "-fcas-path", "-Xclang", Saver.save(*CASPath).data()});
98-
} else {
99-
Args.append({"-fcas-path", Saver.save(*CASPath).data()});
94+
auto addCC1Args = [&](ArrayRef<const char *> NewArgs) {
95+
for (const char *Arg : NewArgs) {
96+
if (ForDriver) {
97+
Args.append({"-Xclang", Arg});
98+
} else {
99+
Args.push_back(Arg);
100+
}
100101
}
102+
};
103+
if (auto CASPath = llvm::sys::Process::GetEnv("LLVM_CACHE_CAS_PATH")) {
104+
addCC1Args({"-fcas-path", Saver.save(*CASPath).data()});
105+
}
106+
if (auto PluginPath = llvm::sys::Process::GetEnv("LLVM_CACHE_PLUGIN_PATH")) {
107+
addCC1Args({"-fcas-plugin-path", Saver.save(*PluginPath).data()});
108+
}
109+
if (auto PluginOpts =
110+
llvm::sys::Process::GetEnv("LLVM_CACHE_PLUGIN_OPTIONS")) {
111+
addCC1Args({"-fcas-plugin-options", Saver.save(*PluginOpts).data()});
101112
}
102113
}
103114

0 commit comments

Comments
 (0)