Skip to content

Commit 9a3573b

Browse files
[CacheKey] Fix filelist CAS option
When using a file list inputs to the swift-frontend invocation, the name of the file is not significant, only the content of the file. Improve cache key computation so only the content of the input file will affect cache key, not the file name.
1 parent 1b70ba1 commit 9a3573b

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

lib/Frontend/CompileJobCacheKey.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "llvm/ADT/STLExtras.h"
2121
#include "llvm/CAS/HierarchicalTreeBuilder.h"
2222
#include "llvm/CAS/ObjectStore.h"
23+
#include "llvm/Support/Error.h"
24+
#include "llvm/Support/MemoryBuffer.h"
2325

2426
using namespace swift;
2527

@@ -30,29 +32,45 @@ llvm::Expected<llvm::cas::ObjectRef> swift::createCompileJobBaseCacheKey(
3032

3133
// TODO: Improve this list.
3234
static const std::vector<std::string> removeArgAndNext = {
33-
"-o", "-supplementary-output-file-map", "-serialize-diagnostics-path",
34-
"-num-threads", "-cas-path"};
35+
"-o",
36+
"-output-filelist",
37+
"-supplementary-output-file-map",
38+
"-index-unit-output-path",
39+
"-index-unit-output-path-filelist",
40+
"-serialize-diagnostics-path",
41+
"-num-threads",
42+
"-cas-path"};
3543

3644
// Don't count the `-frontend` in the first location since only frontend
3745
// invocation can have a cache key.
3846
if (Args.size() > 1 && StringRef(Args.front()) == "-frontend")
3947
Args = Args.drop_front();
4048

41-
bool SkipNext = false;
42-
for (StringRef Arg : Args) {
43-
if (SkipNext) {
44-
SkipNext = false;
45-
continue;
46-
}
49+
for (unsigned I = 0, IE =Args.size(); I < IE; ++I) {
50+
StringRef Arg = Args[I];
4751
if (llvm::is_contained(removeArgAndNext, Arg)) {
48-
SkipNext = true;
52+
++I;
4953
continue;
5054
}
5155
// FIXME: Use a heuristic to remove all the flags that affect output paths.
5256
// Those should not affect compile cache key.
5357
if (Arg.startswith("-emit-")) {
5458
if (Arg.endswith("-path"))
55-
SkipNext = true;
59+
++I;
60+
continue;
61+
}
62+
// Handle -file-list option. Need to drop the option but adds the file
63+
// content instead.
64+
// FIXME: will be nice if the same list of files gets the same key no matter
65+
// going through command-line or filelist.
66+
if (Arg == "-filelist" || Arg == "-primary-filelist") {
67+
auto FileList = llvm::MemoryBuffer::getFile(Args[++I]);
68+
if (!FileList)
69+
return llvm::errorCodeToError(FileList.getError());
70+
CommandLine.append(Arg);
71+
CommandLine.push_back(0);
72+
CommandLine.append((*FileList)->getBuffer());
73+
CommandLine.push_back(0);
5674
continue;
5775
}
5876
CommandLine.append(Arg);

test/CAS/cache_key_compute.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
// RUN: diff %t1.casid %t3.casid
2626
// RUN: not diff %t1.casid %t4.casid
2727

28+
/// Check filelist option.
29+
// RUN: echo "%s" > %t/filelist-1
30+
// RUN: echo "%s" > %t/filelist-2
31+
// RUN: cp %s %t/temp.swift
32+
// RUN: echo "%t/temp.swift" > %t/filelist-3
33+
// RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-base-key -- \
34+
// RUN: %target-swift-frontend -enable-cas -filelist %t/filelist-1 -c -allow-unstable-cache-key-for-testing > %t5.casid
35+
// RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-base-key -- \
36+
// RUN: %target-swift-frontend -enable-cas -filelist %t/filelist-2 -c -allow-unstable-cache-key-for-testing > %t6.casid
37+
// RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-base-key -- \
38+
// RUN: %target-swift-frontend -enable-cas -filelist %t/filelist-3 -c -allow-unstable-cache-key-for-testing > %t7.casid
39+
// RUN: diff %t5.casid %t6.casid
40+
// RUN: not diff %t5.casid %t7.casid
41+
2842
/// Test output keys.
2943
// RUN: %cache-tool -cas-path %t/cas -cache-tool-action print-output-keys -- \
3044
// RUN: %target-swift-frontend -enable-cas %s -emit-module -c -emit-dependencies \

0 commit comments

Comments
 (0)