19#include "llvm/Config/llvm-config.h"
23#define DEBUG_TYPE "cas-action-caches"
33template <
size_t Size>
class CacheEntry {
35 CacheEntry() =
default;
37 CacheEntry(
const CacheEntry &Entry) {
llvm::copy(Entry.Value,
Value.data()); }
41 std::array<uint8_t, Size>
Value;
45class InMemoryActionCache final :
public ActionCache {
51 bool CanBeDistributed)
final;
53 bool CanBeDistributed)
const final;
55 Error validate()
const final {
60 using DataT = CacheEntry<
sizeof(HashType)>;
70 bool CanBeDistributed)
final;
72 bool CanBeDistributed)
const final;
76 Error validate()
const final;
79 static StringRef getHashName() {
return "BLAKE3"; }
81 OnDiskActionCache(std::unique_ptr<ondisk::OnDiskKeyValueDB> DB);
83 std::unique_ptr<ondisk::OnDiskKeyValueDB> DB;
84 using DataT = CacheEntry<
sizeof(HashType)>;
89class UnifiedOnDiskActionCache final :
public ActionCache {
92 bool CanBeDistributed)
final;
94 bool CanBeDistributed)
const final;
96 UnifiedOnDiskActionCache(std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB);
98 Error validate()
const final;
101 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB;
109 std::string Existing =
114 "cache poisoned for '" +
Key +
"' (new='" +
115 Output.
toString() +
"' vs. existing '" +
128Error InMemoryActionCache::putImpl(ArrayRef<uint8_t>
Key,
const CASID &Result,
130 DataT Expected(
Result.getHash());
131 const InMemoryCacheT::value_type &Cached = *Cache.
insertLazy(
132 Key, [&](
auto ValueConstructor) { ValueConstructor.emplace(Expected); });
134 const DataT &Observed = Cached.Data;
135 if (Expected.getValue() == Observed.getValue())
145 return std::make_unique<InMemoryActionCache>();
150OnDiskActionCache::OnDiskActionCache(
151 std::unique_ptr<ondisk::OnDiskKeyValueDB> DB)
156OnDiskActionCache::create(
StringRef AbsPath) {
157 std::shared_ptr<ondisk::OnDiskCASLogger>
Logger;
163 std::unique_ptr<ondisk::OnDiskKeyValueDB>
DB;
165 AbsPath, getHashName(),
sizeof(HashType), getHashName(),
166 sizeof(DataT),
nullptr, std::move(
Logger))
169 return std::unique_ptr<OnDiskActionCache>(
170 new OnDiskActionCache(std::move(DB)));
176 std::optional<ArrayRef<char>> Val;
186 auto ResultHash =
Result.getHash();
200Error OnDiskActionCache::validate()
const {
203 return DB->validate(
nullptr);
206UnifiedOnDiskActionCache::UnifiedOnDiskActionCache(
207 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB)
208 : ActionCache(builtin::BuiltinCASContext::getDefaultContext()),
209 UniDB(std::
move(UniDB)) {}
214 std::optional<ArrayRef<char>> Val;
215 if (
Error E = UniDB->getKeyValueDB().get(
Key).moveInto(Val))
227 auto Expected = UniDB->getGraphDB().getReference(
Result.getHash());
232 std::optional<ArrayRef<char>> Observed;
233 if (
Error E = UniDB->getKeyValueDB().put(
Key,
Value).moveInto(Observed))
241 Key,
getContext(), Result, UniDB->getGraphDB().getDigest(ObservedID));
244Error UnifiedOnDiskActionCache::validate()
const {
247 auto formatError = [&](
Twine Msg) {
254 if (
Error E = UniDB->getGraphDB().validateObjectID(
ID))
258 return UniDB->getKeyValueDB().validate(ValidateRef);
263#if LLVM_ENABLE_ONDISK_CAS
264 return OnDiskActionCache::create(Path);
270std::unique_ptr<ActionCache>
272 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB) {
273 return std::make_unique<UnifiedOnDiskActionCache>(std::move(UniDB));
This file contains the declaration of the ActionCache class, which is the base class for ActionCache ...
static Error createResultCachePoisonedError(ArrayRef< uint8_t > KeyHash, const CASContext &Context, CASID Output, ArrayRef< uint8_t > ExistingOutput)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
This file declares interface for OnDiskCASLogger, an interface that can be used to log CAS events to ...
This declares OnDiskKeyValueDB, a key value storage database of fixed size key and value.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
A class that wraps the BLAKE3 algorithm.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringRef - Represent a constant reference to a string, i.e.
Lock-free thread-safe hash-mapped trie.
pointer find(ArrayRef< uint8_t > Hash)
pointer insertLazy(const_pointer Hint, ArrayRef< uint8_t > Hash, function_ref< void(LazyValueConstructor)> OnConstruct)
Insert with a hint.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM Value Representation.
A cache from a key (that describes an action) to the result of performing that action.
Context for CAS identifiers.
Unique identifier for a CAS object.
LLVM_ABI std::string toString() const
Return a printable string for CASID.
static CASID create(const CASContext *Context, StringRef Hash)
Create CASID from CASContext and raw hash bytes.
static const BuiltinCASContext & getDefaultContext()
static LLVM_ABI Expected< std::unique_ptr< OnDiskCASLogger > > openIfEnabled(const Twine &Path)
Create or append to a log file inside the given CAS directory Path if logging is enabled by the envir...
static LLVM_ABI_FOR_TEST Expected< std::unique_ptr< OnDiskKeyValueDB > > open(StringRef Path, StringRef HashName, unsigned KeySize, StringRef ValueName, size_t ValueSize, UnifiedOnDiskCache *UnifiedCache=nullptr, std::shared_ptr< OnDiskCASLogger > Logger=nullptr)
Open the on-disk store from a directory.
static LLVM_ABI_FOR_TEST ValueBytes getValueFromObjectID(ObjectID ID)
static LLVM_ABI_FOR_TEST ObjectID getObjectIDFromValue(ArrayRef< char > Value)
Helper function to convert the value stored in KeyValueDB and ObjectID.
LLVM_ABI sandboxir::Value * getValue(llvm::Value *V) const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
std::unique_ptr< ActionCache > createActionCacheFromUnifiedOnDiskCache(std::shared_ptr< ondisk::UnifiedOnDiskCache > UniDB)
LLVM_ABI Expected< std::unique_ptr< ActionCache > > createOnDiskActionCache(StringRef Path)
Create an action cache on disk.
LLVM_ABI std::unique_ptr< ActionCache > createInMemoryActionCache()
Create an action cache in memory.
Context & getContext() const
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
OutputIt copy(R &&Range, OutputIt Out)
void toHex(ArrayRef< uint8_t > Input, bool LowerCase, SmallVectorImpl< char > &Output)
Convert buffer Input to its hexadecimal representation. The returned string is double the size of Inp...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Implement std::hash so that hash_code can be used in STL containers.