Skip to content

Commit e7e7cc0

Browse files
Swift MCCAS object files are 0 bytes on cache miss
When clang caching and MCCAS are enabled, on a cache miss, the object file is not written to in MachOCASWriter, instead the replay code is rerun and the object file is serialized and written out. However, in swift, the replay code isn't run on a cache miss at all, and it expects the object writer to write to the object file. This patch makes it so that on a cache miss, the MachOCASWriter always writes to the raw_ostream buffer for the object file and doesn't serialize the object file in the replay code. (cherry picked from commit ce65bfa)
1 parent de75787 commit e7e7cc0

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

clang/lib/Frontend/CompileJobCache.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ Error ObjectStoreCachingOutputs::finishComputedResult(
724724
std::optional<int> ObjectStoreCachingOutputs::replayCachedResult(
725725
const llvm::cas::CASID &ResultCacheKey, llvm::cas::ObjectRef ResultID,
726726
bool JustComputedResult) {
727-
if (JustComputedResult && !ComputedJobNeedsReplay)
727+
if (JustComputedResult && !WriteOutputAsCASID)
728728
return std::nullopt;
729729

730730
// FIXME: Stop calling report_fatal_error().
@@ -745,10 +745,7 @@ std::optional<int> ObjectStoreCachingOutputs::replayCachedResult(
745745
Expected<std::optional<int>> ObjectStoreCachingOutputs::replayCachedResult(
746746
const llvm::cas::CASID &ResultCacheKey,
747747
clang::cas::CompileJobCacheResult &Result, bool JustComputedResult) {
748-
// FIXME: The correct fix for MCCAS replay is that you have an official CASID
749-
// file output going all the way down into ObjectWriter, we can remove this
750-
// callback and special case.
751-
if (JustComputedResult && !ComputedJobNeedsReplay)
748+
if (JustComputedResult && !WriteOutputAsCASID)
752749
return std::nullopt;
753750

754751
llvm::cas::ObjectStore &CAS = Result.getCAS();

clang/test/CAS/cas-backend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: rm -rf %t && mkdir -p %t
22
// RUN: llvm-cas --cas %t/cas --ingest %s > %t/casid
33
//
4-
// RUN: %clang -cc1 -triple x86_64-apple-macos11 -fcas-backend \
4+
// RUN: %clang -cc1 -fcas-emit-casid-file -triple x86_64-apple-macos11 -fcas-backend \
55
// RUN: -fcas-path %t/cas -fcas-fs @%t/casid -fcache-compile-job \
66
// RUN: -Rcompile-job-cache %s -emit-obj -o %t/output.o \
77
// RUN: -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb \
@@ -11,7 +11,7 @@
1111
// RUN: ls %t/output.o && rm %t/output.o
1212
// RUN: ls %t/deps.d && mv %t/deps.d %t/deps.d.orig
1313
//
14-
// RUN: CLANG_CAS_BACKEND_SAVE_CASID_FILE=1 %clang -cc1 \
14+
// RUN: %clang -cc1 -fcas-emit-casid-file\
1515
// RUN: -triple x86_64-apple-macos11 -fcas-backend \
1616
// RUN: -fcas-path %t/cas -fcas-fs @%t/casid -fcache-compile-job \
1717
// RUN: -Rcompile-job-cache %s -emit-obj -o %t/output.o \

clang/test/CAS/casid-output-test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
3+
// Check if -fcasid-output works on a cache miss with file based caching
4+
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas CLANG_CACHE_DISABLE_MCCAS=1 %clang-cache %clang -target x86_64-apple-macos11 -Xclang -fcasid-output -g -c %s -o %t/test.o
5+
// RUN: cat %t/test.o | FileCheck %s
6+
// RUN: rm -rf %t/test.o
7+
// Check if -fcasid-output works on a cache hit with file based caching
8+
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas CLANG_CACHE_DISABLE_MCCAS=1 %clang-cache %clang -target x86_64-apple-macos11 -Xclang -fcasid-output -g -c %s -o %t/test.o
9+
// RUN: cat %t/test.o | FileCheck %s
10+
// RUN: rm -rf %t/test.o
11+
// RUN: rm -rf %t/cas
12+
13+
// Check if -fcasid-output works on a cache miss with MCCAS
14+
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas %clang-cache %clang -target x86_64-apple-macos11 -Xclang -fcasid-output -g -c %s -o %t/test.o
15+
// RUN: cat %t/test.o | FileCheck %s
16+
// RUN: rm -rf %t/test.o
17+
18+
// Check if -fcasid-output works on a cache hit with MCCAS
19+
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas %clang-cache %clang -target x86_64-apple-macos11 -Xclang -fcasid-output -g -c %s -o %t/test.o
20+
// RUN: cat %t/test.o | FileCheck %s
21+
22+
// CHECK: llvmcas://{{[a-f0-9]+}}
23+
24+
25+
void foo() {}

llvm/lib/MC/MachOCASWriter.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ uint64_t MachOCASWriter::writeObject(MCAssembler &Asm) {
7676
inconvertibleErrorCode(),
7777
"CASBackend output round-trip verification error");
7878

79+
OS << ObjectBuffer;
7980
return Error::success();
8081
};
8182

@@ -85,11 +86,6 @@ uint64_t MachOCASWriter::writeObject(MCAssembler &Asm) {
8586
// If there is a callback, then just hand off the result through callback.
8687
if (ResultCallBack) {
8788
cantFail((*ResultCallBack)(CASObj.getID()));
88-
if (Mode == CASBackendMode::Verify) {
89-
if (auto E = VerifyObject())
90-
report_fatal_error(std::move(E));
91-
}
92-
return 0;
9389
}
9490

9591
switch (Mode) {
@@ -103,17 +99,8 @@ uint64_t MachOCASWriter::writeObject(MCAssembler &Asm) {
10399
break;
104100
}
105101
case CASBackendMode::Verify: {
106-
SmallString<512> ObjectBuffer;
107-
raw_svector_ostream ObjectOS(ObjectBuffer);
108-
auto E = SerializeObjectFile(CASObj, CAS, ObjectOS);
109-
if (E)
102+
if (auto E = VerifyObject())
110103
report_fatal_error(std::move(E));
111-
112-
if (!ObjectBuffer.equals(InternalBuffer))
113-
report_fatal_error("CASBackend output round-trip verification error");
114-
115-
OS << ObjectBuffer;
116-
break;
117104
}
118105
}
119106

0 commit comments

Comments
 (0)