Skip to content

Commit 7a5f454

Browse files
committed
[clang][cas] Ignore the -fdepfile-entry= dependency option when calculating the cache key
`-fdepfile-entry=` is a dependency option that can be treated as "transparent", not affecting the cached compilation. The passed-in filenames are added at the point where the dependency file is generated and are not read from the file system. rdar://99683427 (cherry picked from commit dde9a72)
1 parent fecabcd commit 7a5f454

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

clang/include/clang/Frontend/CASDependencyCollector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CASDependencyCollector : public DependencyFileGenerator {
2727
/// \param Callback Callback that receives the resulting dependencies on
2828
/// completion, or \c None if an error occurred.
2929
CASDependencyCollector(
30-
const DependencyOutputOptions &Opts, cas::ObjectStore &CAS,
30+
DependencyOutputOptions Opts, cas::ObjectStore &CAS,
3131
std::function<void(Optional<cas::ObjectRef>)> Callback);
3232

3333
/// Replay the given result, which should have been created by a
@@ -51,4 +51,4 @@ class CASDependencyCollector : public DependencyFileGenerator {
5151

5252
} // namespace clang
5353

54-
#endif // LLVM_CLANG_FRONTEND_CASDEPENDENCYCOLLECTOR_H
54+
#endif // LLVM_CLANG_FRONTEND_CASDEPENDENCYCOLLECTOR_H

clang/lib/Frontend/CASDependencyCollector.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,19 @@
1515
using namespace clang;
1616
using namespace clang::cas;
1717

18+
/// \returns \p DependencyOutputOptions but with \p ExtraDeps cleared out.
19+
///
20+
/// This is useful to avoid recording these filenames in the CAS.
21+
static DependencyOutputOptions dropExtraDeps(DependencyOutputOptions Opts) {
22+
Opts.ExtraDeps.clear();
23+
return Opts;
24+
}
25+
1826
CASDependencyCollector::CASDependencyCollector(
19-
const DependencyOutputOptions &Opts, cas::ObjectStore &CAS,
27+
DependencyOutputOptions Opts, cas::ObjectStore &CAS,
2028
std::function<void(Optional<cas::ObjectRef>)> Callback)
21-
: DependencyFileGenerator(Opts, llvm::vfs::makeNullOutputBackend()),
29+
: DependencyFileGenerator(dropExtraDeps(std::move(Opts)),
30+
llvm::vfs::makeNullOutputBackend()),
2231
CAS(CAS), Callback(std::move(Callback)) {}
2332

2433
llvm::Error CASDependencyCollector::replay(const DependencyOutputOptions &Opts,
@@ -29,6 +38,15 @@ llvm::Error CASDependencyCollector::replay(const DependencyOutputOptions &Opts,
2938
return Refs.takeError();
3039

3140
CASDependencyCollector DC(Opts, CAS, nullptr);
41+
42+
// Add the filenames from DependencyOutputOptions::ExtraDeps. These are kept
43+
// out of the compilation cache key since they can be passed-in and added at
44+
// the point where the dependency file is generated, without needing to affect
45+
// the cached compilation.
46+
for (const std::pair<std::string, ExtraDepKind> &Dep : Opts.ExtraDeps) {
47+
DC.addDependency(Dep.first);
48+
}
49+
3250
auto Err = Refs->forEachReference([&](ObjectRef Ref) -> llvm::Error {
3351
auto PathHandle = CAS.getProxy(Ref);
3452
if (!PathHandle)

clang/lib/Frontend/CompileJobCacheKey.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ clang::createCompileJobCacheKey(ObjectStore &CAS, DiagnosticsEngine &Diags,
7979
if (!DepOpts.Targets.empty())
8080
DepOpts.Targets = {"-"};
8181
DepOpts.UsePhonyTargets = false;
82+
// These are added in when the dependency file is generated, but they don't
83+
// affect the actual compilation.
84+
DepOpts.ExtraDeps.clear();
8285

8386
// Generate a new command-line in case Invocation has been canonicalized.
8487
llvm::BumpPtrAllocator Alloc;

clang/test/CAS/fcache-compile-job-dependency-file.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
// RUN: %clang -cc1 -triple x86_64-apple-macos11 \
3232
// RUN: -fcas-path %t/cas -faction-cache-path %t/cache -fcas-fs @%t/casid -fcache-compile-job \
3333
// RUN: -Rcompile-job-cache %t/main.c -emit-obj -o %t/output.o -isystem %t/sys \
34-
// RUN: -dependency-file %t/deps3.d -MT other1 -MT other2 -MP 2>&1 \
34+
// RUN: -dependency-file %t/deps3.d -MT other1 -MT other2 -MP -fdepfile-entry=extra-depfile.json 2>&1 \
3535
// RUN: | FileCheck %s --check-prefix=CACHE-HIT
3636

3737
// RUN: FileCheck %s --input-file=%t/deps3.d --check-prefix=DEPS_OTHER
3838
// DEPS_OTHER: other1 other2:
39+
// DEPS_OTHER: extra-depfile.json
3940
// DEPS_OTHER: main.c
4041
// DEPS_OTHER: my_header.h
4142
// DEPS_OTHER-NOT: sys.h
@@ -56,6 +57,29 @@
5657
// DEPS_SYS: my_header.h
5758
// DEPS_SYS: sys.h
5859

60+
// Using another cache path to avoid reusing artifacts.
61+
62+
// RUN: %clang -cc1 -triple x86_64-apple-macos11 \
63+
// RUN: -fcas-path %t/cas -faction-cache-path %t/cache2 -fcas-fs @%t/casid -fcache-compile-job \
64+
// RUN: -Rcompile-job-cache %t/main.c -emit-obj -o %t/output.o -isystem %t/sys \
65+
// RUN: -dependency-file %t/deps-depfile1.d -MT deps -fdepfile-entry=extra-depfile.json -fdepfile-entry=%t/main.c 2>&1 \
66+
// RUN: | FileCheck %s --check-prefix=CACHE-MISS
67+
68+
// RUN: FileCheck %s --input-file=%t/deps-depfile1.d --check-prefix=DEPS_DEPFILE1
69+
// DEPS_DEPFILE1: deps:
70+
// DEPS_DEPFILE1: extra-depfile.json
71+
// DEPS_DEPFILE1: main.c
72+
73+
// RUN: %clang -cc1 -triple x86_64-apple-macos11 \
74+
// RUN: -fcas-path %t/cas -faction-cache-path %t/cache2 -fcas-fs @%t/casid -fcache-compile-job \
75+
// RUN: -Rcompile-job-cache %t/main.c -emit-obj -o %t/output.o -isystem %t/sys \
76+
// RUN: -dependency-file %t/deps-depfile2.d -MT deps 2>&1 \
77+
// RUN: | FileCheck %s --check-prefix=CACHE-HIT
78+
79+
// RUN: FileCheck %s --input-file=%t/deps-depfile2.d --check-prefix=DEPS_DEPFILE2
80+
// DEPS_DEPFILE2-NOT: extra-depfile.json
81+
// DEPS_DEPFILE2: main.c
82+
5983
//--- main.c
6084
#include "my_header.h"
6185
#include <sys.h>

0 commit comments

Comments
 (0)