LLVM 22.0.0git
ActionCaches.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file This file implements the underlying ActionCache implementations.
10///
11//===----------------------------------------------------------------------===//
12
13#include "BuiltinCAS.h"
16#include "llvm/Support/BLAKE3.h"
17
18#define DEBUG_TYPE "cas-action-caches"
19
20using namespace llvm;
21using namespace llvm::cas;
22
23namespace {
24
25using HasherT = BLAKE3;
26using HashType = decltype(HasherT::hash(std::declval<ArrayRef<uint8_t> &>()));
27
28template <size_t Size> class CacheEntry {
29public:
30 CacheEntry() = default;
31 CacheEntry(ArrayRef<uint8_t> Hash) { llvm::copy(Hash, Value.data()); }
32 CacheEntry(const CacheEntry &Entry) { llvm::copy(Entry.Value, Value.data()); }
33 ArrayRef<uint8_t> getValue() const { return Value; }
34
35private:
36 std::array<uint8_t, Size> Value;
37};
38
39/// Builtin InMemory ActionCache that stores the mapping in memory.
40class InMemoryActionCache final : public ActionCache {
41public:
42 InMemoryActionCache()
44
45 Error putImpl(ArrayRef<uint8_t> ActionKey, const CASID &Result,
46 bool CanBeDistributed) final;
48 bool CanBeDistributed) const final;
49
50private:
51 using DataT = CacheEntry<sizeof(HashType)>;
52 using InMemoryCacheT = ThreadSafeTrieRawHashMap<DataT, sizeof(HashType)>;
53
54 InMemoryCacheT Cache;
55};
56} // end namespace
57
59 const CASContext &Context,
60 CASID Output,
61 ArrayRef<uint8_t> ExistingOutput) {
62 std::string Existing =
63 CASID::create(&Context, toStringRef(ExistingOutput)).toString();
65 toHex(KeyHash, /*LowerCase=*/true, Key);
66 return createStringError(std::make_error_code(std::errc::invalid_argument),
67 "cache poisoned for '" + Key + "' (new='" +
68 Output.toString() + "' vs. existing '" +
69 Existing + "')");
70}
71
73InMemoryActionCache::getImpl(ArrayRef<uint8_t> Key,
74 bool /*CanBeDistributed*/) const {
75 auto Result = Cache.find(Key);
76 if (!Result)
77 return std::nullopt;
78 return CASID::create(&getContext(), toStringRef(Result->Data.getValue()));
79}
80
81Error InMemoryActionCache::putImpl(ArrayRef<uint8_t> Key, const CASID &Result,
82 bool /*CanBeDistributed*/) {
83 DataT Expected(Result.getHash());
84 const InMemoryCacheT::value_type &Cached = *Cache.insertLazy(
85 Key, [&](auto ValueConstructor) { ValueConstructor.emplace(Expected); });
86
87 const DataT &Observed = Cached.Data;
88 if (Expected.getValue() == Observed.getValue())
89 return Error::success();
90
92 Observed.getValue());
93}
94
95namespace llvm::cas {
96
97std::unique_ptr<ActionCache> createInMemoryActionCache() {
98 return std::make_unique<InMemoryActionCache>();
99}
100
101} // namespace llvm::cas
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)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
A class that wraps the BLAKE3 algorithm.
Definition BLAKE3.h:38
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
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.
LLVM Value Representation.
Definition Value.h:75
A cache from a key (that describes an action) to the result of performing that action.
Definition ActionCache.h:49
Context for CAS identifiers.
Definition CASID.h:28
Unique identifier for a CAS object.
Definition CASID.h:58
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.
Definition CASID.h:117
static const BuiltinCASContext & getDefaultContext()
LLVM_ABI sandboxir::Value * getValue(llvm::Value *V) const
Definition Context.cpp:629
std::unique_ptr< ActionCache > createInMemoryActionCache()
Create an action cache in memory.
Context & getContext() const
Definition BasicBlock.h:99
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1837
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...
StringRef toStringRef(bool B)
Construct a string ref from a boolean.