LLVM 23.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:
29 static constexpr uint64_t getDenseMapEmptyRef() { return -1ULL; }
30 static constexpr uint64_t getDenseMapTombstoneRef() { return -2ULL; }
31
32public:
33 /// Get an internal reference.
34 uint64_t getInternalRef(const ObjectStore &ExpectedCAS) const {
35#if LLVM_ENABLE_ABI_BREAKING_CHECKS
36 assert(CAS == &ExpectedCAS && "Extracting reference for the wrong CAS");
37#endif
38 return InternalRef;
39 }
40
41 /// Helper functions for DenseMapInfo.
42 unsigned getDenseMapHash() const {
43 return static_cast<unsigned>(llvm::hash_value(InternalRef));
44 }
45 bool isDenseMapEmpty() const { return InternalRef == getDenseMapEmptyRef(); }
46 bool isDenseMapTombstone() const {
47 return InternalRef == getDenseMapTombstoneRef();
48 }
49 bool isDenseMapSentinel() const {
51 }
52
53protected:
54 void print(raw_ostream &OS, const ObjectHandle &This) const;
55 void print(raw_ostream &OS, const ObjectRef &This) const;
56
58#if LLVM_ENABLE_ABI_BREAKING_CHECKS
59 assert(
60 (isDenseMapSentinel() || RHS.isDenseMapSentinel() || CAS == RHS.CAS) &&
61 "Cannot compare across CAS instances");
62#endif
63 return InternalRef == RHS.InternalRef;
64 }
65
66protected:
67 friend class ObjectStore;
68 ReferenceBase(const ObjectStore *CAS, uint64_t InternalRef, bool IsHandle)
69 : InternalRef(InternalRef) {
70#if LLVM_ENABLE_ABI_BREAKING_CHECKS
71 this->CAS = CAS;
72#endif
73 assert(InternalRef != getDenseMapEmptyRef() && "Reserved for DenseMapInfo");
74 assert(InternalRef != getDenseMapTombstoneRef() &&
75 "Reserved for DenseMapInfo");
76 }
77
78private:
79 uint64_t InternalRef;
80
81#if LLVM_ENABLE_ABI_BREAKING_CHECKS
82 const ObjectStore *CAS = nullptr;
83#endif
84};
85
86/// Reference to an object in an \a ObjectStore instance.
87///
88/// If you have an ObjectRef, you know the object exists, and you can point at
89/// it from new nodes with \a ObjectStore::store(), but you don't know anything
90/// about it. "Loading" the object is a separate step that may not have
91/// happened yet, and which can fail (due to filesystem corruption) or
92/// introduce latency (if downloading from a remote store).
93///
94/// \a ObjectStore::store() takes a list of these, and these are returned by \a
95/// ObjectStore::forEachRef() and \a ObjectStore::readRef(), which are accessors
96/// for nodes, and \a ObjectStore::getReference().
97///
98/// \a ObjectStore::load() will load the referenced object, and returns \a
99/// ObjectHandle, a variant that knows what kind of entity it is. \a
100/// ObjectStore::getReferenceKind() can expect the type of reference without
101/// asking for unloaded objects to be loaded.
102class ObjectRef : public ReferenceBase {
103public:
104 friend bool operator==(const ObjectRef &LHS, const ObjectRef &RHS) {
105 return LHS.hasSameInternalRef(RHS);
106 }
107 friend bool operator!=(const ObjectRef &LHS, const ObjectRef &RHS) {
108 return !(LHS == RHS);
109 }
110
111 /// Print internal ref and/or CASID. Only suitable for debugging.
112 void print(raw_ostream &OS) const { return ReferenceBase::print(OS, *this); }
113
114 LLVM_DUMP_METHOD void dump() const;
115
116private:
117 friend class ObjectStore;
118 friend class ReferenceBase;
120 ObjectRef(const ObjectStore &CAS, uint64_t InternalRef)
121 : ReferenceBase(&CAS, InternalRef, /*IsHandle=*/false) {
122 assert(InternalRef != -1ULL && "Reserved for DenseMapInfo");
123 assert(InternalRef != -2ULL && "Reserved for DenseMapInfo");
124 }
125 explicit ObjectRef(ReferenceBase) = delete;
126};
127
128/// Handle to a loaded object in a \a ObjectStore instance.
129///
130/// ObjectHandle encapulates a *loaded* object in the CAS. You need one
131/// of these to inspect the content of an object: to look at its stored
132/// data and references.
133class ObjectHandle : public ReferenceBase {
134public:
135 friend bool operator==(const ObjectHandle &LHS, const ObjectHandle &RHS) {
136 return LHS.hasSameInternalRef(RHS);
137 }
138 friend bool operator!=(const ObjectHandle &LHS, const ObjectHandle &RHS) {
139 return !(LHS == RHS);
140 }
141
142 /// Print internal ref and/or CASID. Only suitable for debugging.
143 void print(raw_ostream &OS) const { return ReferenceBase::print(OS, *this); }
144
145 LLVM_DUMP_METHOD void dump() const;
146
147private:
148 friend class ObjectStore;
149 friend class ReferenceBase;
151 explicit ObjectHandle(ReferenceBase) = delete;
152 ObjectHandle(const ObjectStore &CAS, uint64_t InternalRef)
153 : ReferenceBase(&CAS, InternalRef, /*IsHandle=*/true) {}
154};
155
156} // namespace cas
157
158template <> struct DenseMapInfo<cas::ObjectRef> {
159 static unsigned getHashValue(cas::ObjectRef Ref) {
160 return Ref.getDenseMapHash();
161 }
162
164 return LHS == RHS;
165 }
166};
167
168} // namespace llvm
169
170#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:661
This file defines DenseMapInfo traits for DenseMap.
Value * RHS
Value * LHS
Handle to a loaded object in a ObjectStore instance.
void print(raw_ostream &OS) const
Print internal ref and/or CASID. Only suitable for debugging.
friend bool operator!=(const ObjectHandle &LHS, const ObjectHandle &RHS)
LLVM_DUMP_METHOD void dump() const
friend bool operator==(const ObjectHandle &LHS, const ObjectHandle &RHS)
Reference to an object in an ObjectStore instance.
void print(raw_ostream &OS) const
Print internal ref and/or CASID. Only suitable for debugging.
friend bool operator==(const ObjectRef &LHS, const ObjectRef &RHS)
friend class ReferenceBase
friend bool operator!=(const ObjectRef &LHS, const ObjectRef &RHS)
LLVM_DUMP_METHOD void dump() const
friend class ObjectStore
Content-addressable storage for objects.
Definition ObjectStore.h:90
Base class for references to things in ObjectStore.
static constexpr uint64_t getDenseMapTombstoneRef()
uint64_t getInternalRef(const ObjectStore &ExpectedCAS) const
Get an internal reference.
static constexpr uint64_t getDenseMapEmptyRef()
bool isDenseMapTombstone() const
bool isDenseMapEmpty() const
void print(raw_ostream &OS, const ObjectHandle &This) const
bool isDenseMapSentinel() const
ReferenceBase(const ObjectStore *CAS, uint64_t InternalRef, bool IsHandle)
bool hasSameInternalRef(const ReferenceBase &RHS) const
unsigned getDenseMapHash() const
Helper functions for DenseMapInfo.
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.
hash_code hash_value(const FixedPointSemantics &Val)
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
static bool isEqual(cas::ObjectRef LHS, cas::ObjectRef RHS)
static unsigned getHashValue(cas::ObjectRef Ref)
An information struct used to provide DenseMap with the various necessary components for a given valu...