18#include "llvm/Config/llvm-config.h"
22#define DEBUG_TYPE "cas-action-caches"
32template <
size_t Size>
class CacheEntry {
34 CacheEntry() =
default;
36 CacheEntry(
const CacheEntry &Entry) {
llvm::copy(Entry.Value,
Value.data()); }
40 std::array<uint8_t, Size>
Value;
44class InMemoryActionCache final :
public ActionCache {
50 bool CanBeDistributed)
final;
52 bool CanBeDistributed)
const final;
54 Error validate()
const final {
59 using DataT = CacheEntry<
sizeof(HashType)>;
69 bool CanBeDistributed)
final;
71 bool CanBeDistributed)
const final;
75 Error validate()
const final;
78 static StringRef getHashName() {
return "BLAKE3"; }
80 OnDiskActionCache(std::unique_ptr<ondisk::OnDiskKeyValueDB> DB);
82 std::unique_ptr<ondisk::OnDiskKeyValueDB> DB;
83 using DataT = CacheEntry<
sizeof(HashType)>;
88class UnifiedOnDiskActionCache final :
public ActionCache {
91 bool CanBeDistributed)
final;
93 bool CanBeDistributed)
const final;
95 UnifiedOnDiskActionCache(std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB);
97 Error validate()
const final;
100 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB;
108 std::string Existing =
113 "cache poisoned for '" +
Key +
"' (new='" +
114 Output.
toString() +
"' vs. existing '" +
127Error InMemoryActionCache::putImpl(ArrayRef<uint8_t>
Key,
const CASID &Result,
129 DataT Expected(
Result.getHash());
130 const InMemoryCacheT::value_type &Cached = *Cache.
insertLazy(
131 Key, [&](
auto ValueConstructor) { ValueConstructor.emplace(Expected); });
133 const DataT &Observed = Cached.Data;
134 if (Expected.getValue() == Observed.getValue())
144 return std::make_unique<InMemoryActionCache>();
149OnDiskActionCache::OnDiskActionCache(
150 std::unique_ptr<ondisk::OnDiskKeyValueDB> DB)
155OnDiskActionCache::create(
StringRef AbsPath) {
156 std::unique_ptr<ondisk::OnDiskKeyValueDB>
DB;
158 sizeof(HashType), getHashName(),
162 return std::unique_ptr<OnDiskActionCache>(
163 new OnDiskActionCache(std::move(DB)));
169 std::optional<ArrayRef<char>> Val;
179 auto ResultHash =
Result.getHash();
193Error OnDiskActionCache::validate()
const {
196 return DB->validate(
nullptr);
199UnifiedOnDiskActionCache::UnifiedOnDiskActionCache(
200 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB)
201 : ActionCache(builtin::BuiltinCASContext::getDefaultContext()),
202 UniDB(std::
move(UniDB)) {}
207 std::optional<ArrayRef<char>> Val;
208 if (
Error E = UniDB->getKeyValueDB().get(
Key).moveInto(Val))
220 auto Expected = UniDB->getGraphDB().getReference(
Result.getHash());
225 std::optional<ArrayRef<char>> Observed;
226 if (
Error E = UniDB->getKeyValueDB().put(
Key,
Value).moveInto(Observed))
234 Key,
getContext(), Result, UniDB->getGraphDB().getDigest(ObservedID));
237Error UnifiedOnDiskActionCache::validate()
const {
240 auto formatError = [&](
Twine Msg) {
247 if (
ID.getOpaqueData() == 0)
248 return formatError(
"zero is not a valid ref");
251 return UniDB->getKeyValueDB().validate(ValidateRef);
256#if LLVM_ENABLE_ONDISK_CAS
257 return OnDiskActionCache::create(Path);
263std::unique_ptr<ActionCache>
265 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB) {
266 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 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.
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.
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 Expected< std::unique_ptr< OnDiskKeyValueDB > > open(StringRef Path, StringRef HashName, unsigned KeySize, StringRef ValueName, size_t ValueSize, UnifiedOnDiskCache *UnifiedCache=nullptr)
Open the on-disk store from a directory.
static ValueBytes getValueFromObjectID(ObjectID ID)
static 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)
Expected< std::unique_ptr< ActionCache > > createOnDiskActionCache(StringRef Path)
Create an action cache on disk.
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 >
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.