Skip to content

Commit 343e911

Browse files
committed
Revert "Revert "Modify the localCache API to require an explicit commit on CachedFile… (llvm#136121)""
This reverts commit cf261c6 and reinstates commit c3f815b with changes to the comgr caching code to use the new commit API.
1 parent 84d2cbd commit 343e911

File tree

11 files changed

+226
-17
lines changed

11 files changed

+226
-17
lines changed

amd/comgr/src/comgr-cache.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ void saveCommandOutput(CachedCommandAdaptor &C, AddStreamFn &AddStream,
110110

111111
CachedFileStream *CFS = FileOrErr->get();
112112
serializeCacheEntry(*CFS->OS, *Buffer, CapturedLogS);
113+
ErrorHandler(CFS->commit(), "when commiting file stream");
113114
}
114115

115116
bool readEntryFromCache(CachedCommandAdaptor &C, MemoryBuffer &CachedBuffer,

lldb/source/Core/DataFileCache.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ bool DataFileCache::SetCachedData(llvm::StringRef key,
132132
if (file_or_err) {
133133
llvm::CachedFileStream *cfs = file_or_err->get();
134134
cfs->OS->write((const char *)data.data(), data.size());
135+
if (llvm::Error err = cfs->commit()) {
136+
Log *log = GetLog(LLDBLog::Modules);
137+
LLDB_LOG_ERROR(log, std::move(err),
138+
"failed to commit to the cache for key: {0}");
139+
}
135140
return true;
136141
} else {
137142
Log *log = GetLog(LLDBLog::Modules);

llvm/include/llvm/Support/Caching.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,32 @@ class MemoryBuffer;
2424
/// This class wraps an output stream for a file. Most clients should just be
2525
/// able to return an instance of this base class from the stream callback, but
2626
/// if a client needs to perform some action after the stream is written to,
27-
/// that can be done by deriving from this class and overriding the destructor.
27+
/// that can be done by deriving from this class and overriding the destructor
28+
/// or the commit() method.
2829
class CachedFileStream {
2930
public:
3031
CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS,
3132
std::string OSPath = "")
3233
: OS(std::move(OS)), ObjectPathName(OSPath) {}
34+
35+
/// Must be called exactly once after the writes to OS have been completed
36+
/// but before the CachedFileStream object is destroyed.
37+
virtual Error commit() {
38+
if (Committed)
39+
return createStringError(make_error_code(std::errc::invalid_argument),
40+
Twine("CacheStream already committed."));
41+
Committed = true;
42+
43+
return Error::success();
44+
}
45+
46+
bool Committed = false;
3347
std::unique_ptr<raw_pwrite_stream> OS;
3448
std::string ObjectPathName;
35-
virtual ~CachedFileStream() = default;
49+
virtual ~CachedFileStream() {
50+
if (!Committed)
51+
report_fatal_error("CachedFileStream was not committed.\n");
52+
}
3653
};
3754

3855
/// This type defines the callback to add a file that is generated on the fly.

llvm/lib/CGData/CodeGenData.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ void saveModuleForTwoRounds(const Module &TheModule, unsigned Task,
233233

234234
WriteBitcodeToFile(TheModule, *Stream->OS,
235235
/*ShouldPreserveUseListOrder=*/true);
236+
237+
if (Error Err = Stream->commit())
238+
report_fatal_error(std::move(Err));
236239
}
237240

238241
std::unique_ptr<Module> loadModuleForTwoRounds(BitcodeModule &OrigModule,

llvm/lib/Debuginfod/Debuginfod.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ class StreamedHTTPResponseHandler : public HTTPResponseHandler {
188188
public:
189189
StreamedHTTPResponseHandler(CreateStreamFn CreateStream, HTTPClient &Client)
190190
: CreateStream(CreateStream), Client(Client) {}
191+
192+
/// Must be called exactly once after the writes have been completed
193+
/// but before the StreamedHTTPResponseHandler object is destroyed.
194+
Error commit();
195+
191196
virtual ~StreamedHTTPResponseHandler() = default;
192197

193198
Error handleBodyChunk(StringRef BodyChunk) override;
@@ -210,6 +215,12 @@ Error StreamedHTTPResponseHandler::handleBodyChunk(StringRef BodyChunk) {
210215
return Error::success();
211216
}
212217

218+
Error StreamedHTTPResponseHandler::commit() {
219+
if (FileStream)
220+
return FileStream->commit();
221+
return Error::success();
222+
}
223+
213224
// An over-accepting simplification of the HTTP RFC 7230 spec.
214225
static bool isHeader(StringRef S) {
215226
StringRef Name;
@@ -298,6 +309,8 @@ Expected<std::string> getCachedOrDownloadArtifact(
298309
Error Err = Client.perform(Request, Handler);
299310
if (Err)
300311
return std::move(Err);
312+
if ((Err = Handler.commit()))
313+
return std::move(Err);
301314

302315
unsigned Code = Client.responseCode();
303316
if (Code && Code != 200)

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ static void codegen(const CodegenConfig &Conf, TargetMachine *TM,
482482

483483
if (DwoOut)
484484
DwoOut->keep();
485+
486+
if (Error Err = Stream->commit())
487+
report_fatal_error(std::move(Err));
485488
}
486489

487490
static void splitCodeGen(const CodegenConfig &CodegenC, TargetMachine *TM,

llvm/lib/Support/Caching.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
8989
AddBuffer(std::move(AddBuffer)), TempFile(std::move(TempFile)),
9090
ModuleName(ModuleName), Task(Task) {}
9191

92-
~CacheStream() {
93-
// TODO: Manually commit rather than using non-trivial destructor,
94-
// allowing to replace report_fatal_errors with a return Error.
92+
Error commit() override {
93+
Error E = CachedFileStream::commit();
94+
if (E)
95+
return E;
9596

9697
// Make sure the stream is closed before committing it.
9798
OS.reset();
@@ -101,10 +102,12 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
101102
MemoryBuffer::getOpenFile(
102103
sys::fs::convertFDToNativeFile(TempFile.FD), ObjectPathName,
103104
/*FileSize=*/-1, /*RequiresNullTerminator=*/false);
104-
if (!MBOrErr)
105-
report_fatal_error(Twine("Failed to open new cache file ") +
106-
TempFile.TmpName + ": " +
107-
MBOrErr.getError().message() + "\n");
105+
if (!MBOrErr) {
106+
std::error_code EC = MBOrErr.getError();
107+
return createStringError(EC, Twine("Failed to open new cache file ") +
108+
TempFile.TmpName + ": " +
109+
EC.message() + "\n");
110+
}
108111

109112
// On POSIX systems, this will atomically replace the destination if
110113
// it already exists. We try to emulate this on Windows, but this may
@@ -115,11 +118,14 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
115118
// AddBuffer a copy of the bytes we wrote in that case. We do this
116119
// instead of just using the existing file, because the pruner might
117120
// delete the file before we get a chance to use it.
118-
Error E = TempFile.keep(ObjectPathName);
121+
E = TempFile.keep(ObjectPathName);
119122
E = handleErrors(std::move(E), [&](const ECError &E) -> Error {
120123
std::error_code EC = E.convertToErrorCode();
121124
if (EC != errc::permission_denied)
122-
return errorCodeToError(EC);
125+
return createStringError(
126+
EC, Twine("Failed to rename temporary file ") +
127+
TempFile.TmpName + " to " + ObjectPathName + ": " +
128+
EC.message() + "\n");
123129

124130
auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(),
125131
ObjectPathName);
@@ -132,11 +138,10 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
132138
});
133139

134140
if (E)
135-
report_fatal_error(Twine("Failed to rename temporary file ") +
136-
TempFile.TmpName + " to " + ObjectPathName + ": " +
137-
toString(std::move(E)) + "\n");
141+
return E;
138142

139143
AddBuffer(Task, ModuleName, std::move(*MBOrErr));
144+
return Error::success();
140145
}
141146
};
142147

llvm/tools/gold/gold-plugin.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,9 +1117,11 @@ static std::vector<std::pair<SmallString<128>, bool>> runLTO() {
11171117
std::make_unique<llvm::raw_fd_ostream>(FD, true));
11181118
};
11191119

1120-
auto AddBuffer = [&](size_t Task, const Twine &moduleName,
1120+
auto AddBuffer = [&](size_t Task, const Twine &ModuleName,
11211121
std::unique_ptr<MemoryBuffer> MB) {
1122-
*AddStream(Task, moduleName)->OS << MB->getBuffer();
1122+
auto Stream = AddStream(Task, ModuleName);
1123+
*Stream->OS << MB->getBuffer();
1124+
check(Stream->commit(), "Failed to commit cache");
11231125
};
11241126

11251127
FileCache Cache;

llvm/tools/llvm-lto2/llvm-lto2.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ static int run(int argc, char **argv) {
443443

444444
auto AddBuffer = [&](size_t Task, const Twine &ModuleName,
445445
std::unique_ptr<MemoryBuffer> MB) {
446-
*AddStream(Task, ModuleName)->OS << MB->getBuffer();
446+
auto Stream = AddStream(Task, ModuleName);
447+
*Stream->OS << MB->getBuffer();
448+
check(Stream->commit(), "Failed to commit cache");
447449
};
448450

449451
FileCache Cache;

llvm/unittests/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_llvm_unittest(SupportTests
1818
BranchProbabilityTest.cpp
1919
CachePruningTest.cpp
2020
CrashRecoveryTest.cpp
21+
Caching.cpp
2122
Casting.cpp
2223
CheckedArithmeticTest.cpp
2324
Chrono.cpp

0 commit comments

Comments
 (0)