LLVM 22.0.0git
ActionCache.h
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
10/// This file contains the declaration of the ActionCache class, which is the
11/// base class for ActionCache implementations.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CAS_ACTIONCACHE_H
16#define LLVM_CAS_ACTIONCACHE_H
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/CAS/CASID.h"
21#include "llvm/Support/Error.h"
22
23namespace llvm::cas {
24
25class ObjectStore;
26class CASID;
27class ObjectProxy;
28
29/// A key for caching an operation.
30/// It is implemented as a bag of bytes and provides a convenient constructor
31/// for CAS types.
32class CacheKey {
33public:
34 StringRef getKey() const { return Key; }
35
36 CacheKey(const CASID &ID);
37 CacheKey(const ObjectProxy &Proxy);
38 CacheKey(const ObjectStore &CAS, const ObjectRef &Ref);
39
40private:
41 std::string Key;
42};
43
44/// A cache from a key (that describes an action) to the result of performing
45/// that action.
46///
47/// Actions are expected to be pure. Storing mappings from one action to
48/// multiple results will result in error (cache poisoning).
50 virtual void anchor();
51
52public:
53 /// Get a previously computed result for \p ActionKey.
54 ///
55 /// \param CanBeDistributed is a hint to the underlying implementation that if
56 /// it is true, the lookup is profitable to be done on a distributed caching
57 /// level, not just locally. The implementation is free to ignore this flag.
59 bool CanBeDistributed = false) const {
60 return getImpl(arrayRefFromStringRef(ActionKey.getKey()), CanBeDistributed);
61 }
62
63 /// Cache \p Result for the \p ActionKey computation.
64 ///
65 /// \param CanBeDistributed is a hint to the underlying implementation that if
66 /// it is true, the association is profitable to be done on a distributed
67 /// caching level, not just locally. The implementation is free to ignore this
68 /// flag.
69 Error put(const CacheKey &ActionKey, const CASID &Result,
70 bool CanBeDistributed = false) {
71 assert(Result.getContext().getHashSchemaIdentifier() ==
72 getContext().getHashSchemaIdentifier() &&
73 "Hash schema mismatch");
74 return putImpl(arrayRefFromStringRef(ActionKey.getKey()), Result,
75 CanBeDistributed);
76 }
77
78 virtual ~ActionCache() = default;
79
80protected:
81 // Implementation detail for \p get method.
83 getImpl(ArrayRef<uint8_t> ResolvedKey, bool CanBeDistributed) const = 0;
84
85 // Implementation details for \p put method.
86 virtual Error putImpl(ArrayRef<uint8_t> ResolvedKey, const CASID &Result,
87 bool CanBeDistributed) = 0;
88
89 ActionCache(const CASContext &Context) : Context(Context) {}
90
91 const CASContext &getContext() const { return Context; }
92
93private:
94 const CASContext &Context;
95};
96
97/// Create an action cache in memory.
98std::unique_ptr<ActionCache> createInMemoryActionCache();
99
100} // end namespace llvm::cas
101
102#endif // LLVM_CAS_ACTIONCACHE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A cache from a key (that describes an action) to the result of performing that action.
Definition: ActionCache.h:49
Error put(const CacheKey &ActionKey, const CASID &Result, bool CanBeDistributed=false)
Cache Result for the ActionKey computation.
Definition: ActionCache.h:69
Expected< std::optional< CASID > > get(const CacheKey &ActionKey, bool CanBeDistributed=false) const
Get a previously computed result for ActionKey.
Definition: ActionCache.h:58
ActionCache(const CASContext &Context)
Definition: ActionCache.h:89
virtual Expected< std::optional< CASID > > getImpl(ArrayRef< uint8_t > ResolvedKey, bool CanBeDistributed) const =0
const CASContext & getContext() const
Definition: ActionCache.h:91
virtual Error putImpl(ArrayRef< uint8_t > ResolvedKey, const CASID &Result, bool CanBeDistributed)=0
virtual ~ActionCache()=default
Context for CAS identifiers.
Definition: CASID.h:28
Unique identifier for a CAS object.
Definition: CASID.h:58
A key for caching an operation.
Definition: ActionCache.h:32
StringRef getKey() const
Definition: ActionCache.h:34
Reference to an abstract hierarchical node, with data and references.
Definition: ObjectStore.h:236
Reference to an object in an ObjectStore instance.
Definition: CASReference.h:108
Content-addressable storage for objects.
Definition: ObjectStore.h:85
std::unique_ptr< ActionCache > createInMemoryActionCache()
Create an action cache in memory.
@ Ref
The access may reference the value stored in memory.