Skip to content

Commit be70402

Browse files
Merge pull request #77347 from cachemeifyoucan/eng/PR-cache-print-key
[CAS] Add an utility action to swift-cache-tool to print cache key
2 parents 1bdffc3 + 05e50da commit be70402

File tree

4 files changed

+108
-4
lines changed

4 files changed

+108
-4
lines changed

include/swift/Frontend/CompileJobCacheKey.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/ArrayRef.h"
2424
#include "llvm/CAS/ObjectStore.h"
2525
#include "llvm/Support/Error.h"
26+
#include "llvm/Support/raw_ostream.h"
2627

2728
namespace swift {
2829

@@ -43,6 +44,11 @@ llvm::Expected<llvm::cas::ObjectRef>
4344
createCompileJobCacheKeyForOutput(llvm::cas::ObjectStore &CAS,
4445
llvm::cas::ObjectRef BaseKey,
4546
unsigned InputIndex);
47+
48+
/// Print the CompileJobKey for debugging purpose.
49+
llvm::Error printCompileJobCacheKey(llvm::cas::ObjectStore &CAS,
50+
llvm::cas::ObjectRef Key,
51+
llvm::raw_ostream &os);
4652
} // namespace swift
4753

4854
#endif

lib/DriverTool/swift_cache_tool_main.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ enum class SwiftCacheToolAction {
4848
ValidateOutputs,
4949
RenderDiags,
5050
PrintIncludeTreeList,
51+
PrintCompileCacheKey,
5152
};
5253

5354
struct OutputEntry {
@@ -146,12 +147,15 @@ class SwiftCacheToolInvocation {
146147
.Case("render-diags", SwiftCacheToolAction::RenderDiags)
147148
.Case("print-include-tree-list",
148149
SwiftCacheToolAction::PrintIncludeTreeList)
150+
.Case("print-compile-cache-key",
151+
SwiftCacheToolAction::PrintCompileCacheKey)
149152
.Default(SwiftCacheToolAction::Invalid);
150153

151154
if (ActionKind == SwiftCacheToolAction::Invalid) {
152155
llvm::errs()
153156
<< "Invalid option specified for -cache-tool-action: "
154-
<< "print-base-key|print-output-keys|validate-outputs|render-diags\n";
157+
<< "print-base-key|print-output-keys|validate-outputs|render-diags|"
158+
<< "print-include-tree-list|print-compile-cache-key\n";
155159
return 1;
156160
}
157161

@@ -170,6 +174,8 @@ class SwiftCacheToolInvocation {
170174
return renderDiags();
171175
case SwiftCacheToolAction::PrintIncludeTreeList:
172176
return printIncludeTreeList();
177+
case SwiftCacheToolAction::PrintCompileCacheKey:
178+
return printCompileCacheKey();
173179
case SwiftCacheToolAction::Invalid:
174180
return 0; // No action. Probably just print help. Return.
175181
}
@@ -271,6 +277,7 @@ class SwiftCacheToolInvocation {
271277
int validateOutputs();
272278
int renderDiags();
273279
int printIncludeTreeList();
280+
int printCompileCacheKey();
274281
};
275282

276283
} // end anonymous namespace
@@ -522,6 +529,38 @@ int SwiftCacheToolInvocation::printIncludeTreeList() {
522529
return 0;
523530
}
524531

532+
int SwiftCacheToolInvocation::printCompileCacheKey() {
533+
auto error = [](llvm::Error err) {
534+
llvm::errs() << "cannot print cache key: " << llvm::toString(std::move(err))
535+
<< "\n";
536+
return 1;
537+
};
538+
if (Inputs.size() != 1) {
539+
llvm::errs() << "expect 1 CASID as input\n";
540+
return 1;
541+
}
542+
auto DB = CASOpts.getOrCreateDatabases();
543+
if (!DB) {
544+
return error(DB.takeError());
545+
}
546+
auto CAS = DB->first;
547+
auto &input = Inputs.front();
548+
auto ID = CAS->parseID(input);
549+
if (!ID)
550+
return error(ID.takeError());
551+
552+
auto Ref = CAS->getReference(*ID);
553+
if (!Ref) {
554+
llvm::errs() << "CAS object not found: " << input << "\n";
555+
return 1;
556+
}
557+
558+
if (auto err = swift::printCompileJobCacheKey(*CAS, *Ref, llvm::outs()))
559+
return error(std::move(err));
560+
561+
return 0;
562+
}
563+
525564
int swift_cache_tool_main(ArrayRef<const char *> Args, const char *Argv0,
526565
void *MainAddr) {
527566
INITIALIZE_LLVM();

lib/Frontend/CompileJobCacheKey.cpp

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Option/Options.h"
18-
#include <swift/Frontend/CompileJobCacheKey.h>
19-
#include <swift/Basic/Version.h>
20-
#include <llvm/ADT/SmallString.h>
2118
#include "llvm/ADT/STLExtras.h"
2219
#include "llvm/CAS/HierarchicalTreeBuilder.h"
2320
#include "llvm/CAS/ObjectStore.h"
21+
#include "llvm/CAS/TreeEntry.h"
22+
#include "llvm/CAS/TreeSchema.h"
2423
#include "llvm/Option/ArgList.h"
2524
#include "llvm/Option/OptTable.h"
25+
#include "llvm/Support/Endian.h"
2626
#include "llvm/Support/EndianStream.h"
2727
#include "llvm/Support/Error.h"
2828
#include "llvm/Support/MemoryBuffer.h"
29+
#include "llvm/Support/raw_ostream.h"
30+
#include <llvm/ADT/SmallString.h>
31+
#include <swift/Basic/Version.h>
32+
#include <swift/Frontend/CompileJobCacheKey.h>
2933

3034
using namespace swift;
3135

@@ -99,3 +103,52 @@ swift::createCompileJobCacheKeyForOutput(llvm::cas::ObjectStore &CAS,
99103

100104
return CAS.storeFromString({BaseKey}, OS.str());
101105
}
106+
107+
llvm::Error swift::printCompileJobCacheKey(llvm::cas::ObjectStore &CAS,
108+
llvm::cas::ObjectRef Key,
109+
llvm::raw_ostream &OS) {
110+
auto Proxy = CAS.getProxy(Key);
111+
if (!Proxy)
112+
return Proxy.takeError();
113+
114+
if (Proxy->getData().size() != sizeof(uint32_t))
115+
return llvm::createStringError("incorrect size for cache key node");
116+
if (Proxy->getNumReferences() != 1)
117+
return llvm::createStringError("incorrect child number for cache key node");
118+
119+
uint32_t InputIndex = llvm::support::endian::read<uint32_t>(
120+
Proxy->getData().data(), llvm::endianness::little);
121+
122+
auto Base = Proxy->getReference(0);
123+
llvm::cas::TreeSchema Schema(CAS);
124+
auto Tree = Schema.load(Base);
125+
if (!Tree)
126+
return Tree.takeError();
127+
128+
std::string BaseStr;
129+
llvm::raw_string_ostream BaseOS(BaseStr);
130+
auto Err = Tree->forEachEntry(
131+
[&](const llvm::cas::NamedTreeEntry &Entry) -> llvm::Error {
132+
auto Ref = Entry.getRef();
133+
auto DataProxy = CAS.getProxy(Ref);
134+
if (!DataProxy)
135+
return DataProxy.takeError();
136+
137+
BaseOS.indent(2) << Entry.getName() << "\n";
138+
StringRef Line, Remain = DataProxy->getData();
139+
while (!Remain.empty()) {
140+
std::tie(Line, Remain) = Remain.split(0);
141+
BaseOS.indent(4) << Line << "\n";
142+
}
143+
return llvm::Error::success();
144+
});
145+
if (Err)
146+
return Err;
147+
148+
llvm::outs() << "Cache Key " << CAS.getID(Key).toString() << "\n";
149+
llvm::outs() << "Swift Compiler Invocation Info:\n";
150+
llvm::outs() << BaseStr;
151+
llvm::outs() << "Input index: " << InputIndex << "\n";
152+
153+
return llvm::Error::success();
154+
}

test/CAS/cas-explicit-module-map.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib @%t/MyApp.cmd > %t/keys.json
3232
// RUN: %{python} %S/Inputs/ExtractOutputKey.py %t/keys.json %t/Test.swift > %t/key
3333

34+
// RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-compile-cache-key @%t/key | %FileCheck %s --check-prefix=CACHE-KEY
35+
// CACHE-KEY: Cache Key llvmcas://
36+
// CACHE-KEY-NEXT: Swift Compiler Invocation Info:
37+
// CACHE-KEY-NEXT: command-line
38+
// CACHE-KEY: Input index: 0
39+
3440
// RUN: %target-swift-frontend -typecheck-module-from-interface %t/Foo.swiftinterface -disable-implicit-swift-modules \
3541
// RUN: -module-cache-path %t.module-cache -explicit-swift-module-map-file @%t/map.casid \
3642
// RUN: -cache-compile-job -cas-path %t/cas -swift-version 5 -enable-library-evolution \

0 commit comments

Comments
 (0)