LLVM 22.0.0git
CASReference.h
Go to the documentation of this file.
1//===- llvm/CAS/CASReference.h ----------------------------------*- C++ -*-===//
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#ifndef LLVM_CAS_CASREFERENCE_H
10#define LLVM_CAS_CASREFERENCE_H
11
12#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15
16namespace llvm {
17
18class raw_ostream;
19
20namespace cas {
21
22class ObjectStore;
23class ObjectHandle;
24class ObjectRef;
25
26/// Base class for references to things in \a ObjectStore.
28protected:
31 static constexpr uint64_t getDenseMapEmptyRef() { return -1ULL; }
32 static constexpr uint64_t getDenseMapTombstoneRef() { return -2ULL; }
33
34public:
35 /// Get an internal reference.
36 uint64_t getInternalRef(const ObjectStore &ExpectedCAS) const {
37#if LLVM_ENABLE_ABI_BREAKING_CHECKS
38 assert(CAS == &ExpectedCAS && "Extracting reference for the wrong CAS");
39#endif
40 return InternalRef;
41 }
42
43 /// Helper functions for DenseMapInfo.
44 unsigned getDenseMapHash() const {
45 return static_cast<unsigned>(llvm::hash_value(InternalRef));
46 }
47 bool isDenseMapEmpty() const { return InternalRef == getDenseMapEmptyRef(); }
48 bool isDenseMapTombstone() const {
49 return InternalRef == getDenseMapTombstoneRef();
50 }
51 bool isDenseMapSentinel() const {
53 }
54
55protected:
56 void print(raw_ostream &OS, const ObjectHandle &This) const;
57 void print(raw_ostream &OS, const ObjectRef &This) const;
58
60#if LLVM_ENABLE_ABI_BREAKING_CHECKS
61 assert(
62 (isDenseMapSentinel() || RHS.isDenseMapSentinel() || CAS == RHS.CAS) &&
63 "Cannot compare across CAS instances");
64#endif
65 return InternalRef == RHS.InternalRef;
66 }
67
68protected:
69 friend class ObjectStore;
70 ReferenceBase(const ObjectStore *CAS, uint64_t InternalRef, bool IsHandle)
71 : InternalRef(InternalRef) {
72#if LLVM_ENABLE_ABI_BREAKING_CHECKS
73 this->CAS = CAS;
74#endif
75 assert(InternalRef != getDenseMapEmptyRef() && "Reserved for DenseMapInfo");
76 assert(InternalRef != getDenseMapTombstoneRef() &&
77 "Reserved for DenseMapInfo");
78 }
80 : InternalRef(getDenseMapEmptyRef()) {}
82 : InternalRef(getDenseMapTombstoneRef()) {}
83
84private:
85 uint64_t InternalRef;
86
87#if LLVM_ENABLE_ABI_BREAKING_CHECKS
88 const ObjectStore *CAS = nullptr;
89#endif
90};
91
92/// Reference to an object in an \a ObjectStore instance.
93///
94/// If you have an ObjectRef, you know the object exists, and you can point at
95/// it from new nodes with \a ObjectStore::store(), but you don't know anything
96/// about it. "Loading" the object is a separate step that may not have
97/// happened yet, and which can fail (due to filesystem corruption) or
98/// introduce latency (if downloading from a remote store).
99///
100/// \a ObjectStore::store() takes a list of these, and these are returned by \a
101/// ObjectStore::forEachRef() and \a ObjectStore::readRef(), which are accessors
102/// for nodes, and \a ObjectStore::getReference().
103///
104/// \a ObjectStore::load() will load the referenced object, and returns \a
105/// ObjectHandle, a variant that knows what kind of entity it is. \a
106/// ObjectStore::getReferenceKind() can expect the type of reference without
107/// asking for unloaded objects to be loaded.
108class ObjectRef : public ReferenceBase {
109 struct DenseMapTag {};
110
111public:
112 friend bool operator==(const ObjectRef &LHS, const ObjectRef &RHS) {
113 return LHS.hasSameInternalRef(RHS);
114 }
115 friend bool operator!=(const ObjectRef &LHS, const ObjectRef &RHS) {
116 return !(LHS == RHS);
117 }
118
120 return ObjectRef(DenseMapEmptyTag{});
121 }
124 }
125
126 /// Print internal ref and/or CASID. Only suitable for debugging.
127 void print(raw_ostream &OS) const { return ReferenceBase::print(OS, *this); }
128
129 LLVM_DUMP_METHOD void dump() const;
130
131private:
132 friend class ObjectStore;
133 friend class ReferenceBase;
135 ObjectRef(const ObjectStore &CAS, uint64_t InternalRef)
136 : ReferenceBase(&CAS, InternalRef, /*IsHandle=*/false) {
137 assert(InternalRef != -1ULL && "Reserved for DenseMapInfo");
138 assert(InternalRef != -2ULL && "Reserved for DenseMapInfo");
139 }
140 explicit ObjectRef(DenseMapEmptyTag T) : ReferenceBase(T) {}
141 explicit ObjectRef(DenseMapTombstoneTag T) : ReferenceBase(T) {}
142 explicit ObjectRef(ReferenceBase) = delete;
143};
144
145/// Handle to a loaded object in a \a ObjectStore instance.
146///
147/// ObjectHandle encapulates a *loaded* object in the CAS. You need one
148/// of these to inspect the content of an object: to look at its stored
149/// data and references.
151public:
152 friend bool operator==(const ObjectHandle &LHS, const ObjectHandle &RHS) {
153 return LHS.hasSameInternalRef(RHS);
154 }
155 friend bool operator!=(const ObjectHandle &LHS, const ObjectHandle &RHS) {
156 return !(LHS == RHS);
157 }
158
159 /// Print internal ref and/or CASID. Only suitable for debugging.
160 void print(raw_ostream &OS) const { return ReferenceBase::print(OS, *this); }
161
162 LLVM_DUMP_METHOD void dump() const;
163
164private:
165 friend class ObjectStore;
166 friend class ReferenceBase;
168 explicit ObjectHandle(ReferenceBase) = delete;
169 ObjectHandle(const ObjectStore &CAS, uint64_t InternalRef)
170 : ReferenceBase(&CAS, InternalRef, /*IsHandle=*/true) {}
171};
172
173} // namespace cas
174
175template <> struct DenseMapInfo<cas::ObjectRef> {
178 }
179
182 }
183
184 static unsigned getHashValue(cas::ObjectRef Ref) {
185 return Ref.getDenseMapHash();
186 }
187
189 return LHS == RHS;
190 }
191};
192
193} // namespace llvm
194
195#endif // LLVM_CAS_CASREFERENCE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
This file defines DenseMapInfo traits for DenseMap.
raw_pwrite_stream & OS
Value * RHS
Value * LHS
Handle to a loaded object in a ObjectStore instance.
Definition: CASReference.h:150
void print(raw_ostream &OS) const
Print internal ref and/or CASID. Only suitable for debugging.
Definition: CASReference.h:160
friend bool operator!=(const ObjectHandle &LHS, const ObjectHandle &RHS)
Definition: CASReference.h:155
LLVM_DUMP_METHOD void dump() const
Definition: ObjectStore.cpp:25
friend bool operator==(const ObjectHandle &LHS, const ObjectHandle &RHS)
Definition: CASReference.h:152
Reference to an object in an ObjectStore instance.
Definition: CASReference.h:108
void print(raw_ostream &OS) const
Print internal ref and/or CASID. Only suitable for debugging.
Definition: CASReference.h:127
friend bool operator==(const ObjectRef &LHS, const ObjectRef &RHS)
Definition: CASReference.h:112
friend class ReferenceBase
Definition: CASReference.h:133
friend bool operator!=(const ObjectRef &LHS, const ObjectRef &RHS)
Definition: CASReference.h:115
static ObjectRef getDenseMapEmptyKey()
Definition: CASReference.h:119
static ObjectRef getDenseMapTombstoneKey()
Definition: CASReference.h:122
LLVM_DUMP_METHOD void dump() const
Definition: ObjectStore.cpp:24
Content-addressable storage for objects.
Definition: ObjectStore.h:85
Base class for references to things in ObjectStore.
Definition: CASReference.h:27
static constexpr uint64_t getDenseMapTombstoneRef()
Definition: CASReference.h:32
uint64_t getInternalRef(const ObjectStore &ExpectedCAS) const
Get an internal reference.
Definition: CASReference.h:36
ReferenceBase(DenseMapEmptyTag)
Definition: CASReference.h:79
static constexpr uint64_t getDenseMapEmptyRef()
Definition: CASReference.h:31
bool isDenseMapTombstone() const
Definition: CASReference.h:48
bool isDenseMapEmpty() const
Definition: CASReference.h:47
void print(raw_ostream &OS, const ObjectHandle &This) const
Definition: ObjectStore.cpp:40
bool isDenseMapSentinel() const
Definition: CASReference.h:51
ReferenceBase(const ObjectStore *CAS, uint64_t InternalRef, bool IsHandle)
Definition: CASReference.h:70
ReferenceBase(DenseMapTombstoneTag)
Definition: CASReference.h:81
bool hasSameInternalRef(const ReferenceBase &RHS) const
Definition: CASReference.h:59
unsigned getDenseMapHash() const
Helper functions for DenseMapInfo.
Definition: CASReference.h:44
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:137
@ Ref
The access may reference the value stored in memory.
static bool isEqual(cas::ObjectRef LHS, cas::ObjectRef RHS)
Definition: CASReference.h:188
static cas::ObjectRef getTombstoneKey()
Definition: CASReference.h:180
static unsigned getHashValue(cas::ObjectRef Ref)
Definition: CASReference.h:184
static cas::ObjectRef getEmptyKey()
Definition: CASReference.h:176
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54