Skip to content

[CAS] Protect the CASOutputBackend from multi-threaded updates #82859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions lib/Frontend/CASOutputBackends.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "llvm/CAS/HierarchicalTreeBuilder.h"
#include "llvm/CAS/ObjectStore.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Mutex.h"
#include <optional>

#define DEBUG_TYPE "swift-cas-backend"
Expand Down Expand Up @@ -112,6 +113,9 @@ class SwiftCASOutputBackend::Implementation {
Error storeImpl(StringRef Path, StringRef Bytes, unsigned InputIndex,
file_types::ID OutputKind);

// Return true if all the outputs are produced for the given index.
bool addProducedOutput(unsigned InputIndex, file_types::ID OutputKind,
ObjectRef BytesRef);
Error finalizeCacheKeysFor(unsigned InputIndex);

private:
Expand All @@ -123,6 +127,9 @@ class SwiftCASOutputBackend::Implementation {
const FrontendOptions &Opts;
FrontendOptions::ActionType Action;

// Lock for updating output file status.
llvm::sys::SmartMutex<true> OutputLock;

// Map from output path to the input index and output kind.
StringMap<std::pair<unsigned, file_types::ID>> OutputToInputMap;

Expand Down Expand Up @@ -197,8 +204,10 @@ Error SwiftCASOutputBackend::storeMCCASObjectID(StringRef OutputFilename,
return createStringError("Invalid CASID: " + ID.toString() +
". No associated ObjectRef found!");

Impl.OutputRefs[InputIndex].insert({file_types::TY_Object, *MCRef});
return Impl.finalizeCacheKeysFor(InputIndex);
if (Impl.addProducedOutput(InputIndex, file_types::TY_Object, *MCRef))
return Impl.finalizeCacheKeysFor(InputIndex);

return Error::success();
}

void SwiftCASOutputBackend::Implementation::initBackend(
Expand Down Expand Up @@ -249,23 +258,29 @@ Error SwiftCASOutputBackend::Implementation::storeImpl(
<< "\' for input \'" << InputIndex << "\': \'"
<< CAS.getID(*BytesRef).toString() << "\'\n";);

OutputRefs[InputIndex].insert({OutputKind, *BytesRef});
if (addProducedOutput(InputIndex, OutputKind, *BytesRef))
return finalizeCacheKeysFor(InputIndex);
return Error::success();
}

bool SwiftCASOutputBackend::Implementation::addProducedOutput(
unsigned InputIndex, file_types::ID OutputKind,
ObjectRef BytesRef) {
llvm::sys::SmartScopedLock<true> LockOutput(OutputLock);
auto &ProducedOutputs = OutputRefs[InputIndex];
ProducedOutputs.insert({OutputKind, BytesRef});

return finalizeCacheKeysFor(InputIndex);
return llvm::all_of(OutputToInputMap, [&](auto &E) {
return (E.second.first != InputIndex ||
ProducedOutputs.count(E.second.second));
});
}

Error SwiftCASOutputBackend::Implementation::finalizeCacheKeysFor(
unsigned InputIndex) {
auto ProducedOutputs = OutputRefs[InputIndex];
const auto &ProducedOutputs = OutputRefs[InputIndex];
assert(!ProducedOutputs.empty() && "Expect outputs for this input");

// If not all outputs for the input are emitted, return.
if (!llvm::all_of(OutputToInputMap, [&](auto &E) {
return (E.second.first != InputIndex ||
ProducedOutputs.count(E.second.second));
}))
return Error::success();

std::vector<std::pair<file_types::ID, ObjectRef>> OutputsForInput;
llvm::for_each(ProducedOutputs, [&OutputsForInput](auto E) {
OutputsForInput.emplace_back(E.first, E.second);
Expand Down