LLVM 22.0.0git
ObjectStore.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
10#include "llvm/ADT/DenseSet.h"
11#include "llvm/Support/Debug.h"
12#include "llvm/Support/Errc.h"
15#include <deque>
16
17using namespace llvm;
18using namespace llvm::cas;
19
20void CASContext::anchor() {}
21void ObjectStore::anchor() {}
22
27
28std::string CASID::toString() const {
29 std::string S;
30 raw_string_ostream(S) << *this;
31 return S;
32}
33
35 uint64_t InternalRef, std::optional<CASID> ID) {
36 OS << Kind << "=" << InternalRef;
37 if (ID)
38 OS << "[" << *ID << "]";
39}
40
41void ReferenceBase::print(raw_ostream &OS, const ObjectHandle &This) const {
42 assert(this == &This);
43 printReferenceBase(OS, "object-handle", InternalRef, std::nullopt);
44}
45
46void ReferenceBase::print(raw_ostream &OS, const ObjectRef &This) const {
47 assert(this == &This);
48
49 std::optional<CASID> ID;
50#if LLVM_ENABLE_ABI_BREAKING_CHECKS
51 if (CAS)
52 ID = CAS->getID(This);
53#endif
54 printReferenceBase(OS, "object-ref", InternalRef, ID);
55}
56
58 std::optional<ObjectHandle> Handle;
59 if (Error E = loadIfExists(Ref).moveInto(Handle))
60 return std::move(E);
61 if (!Handle)
63 "missing object '" + getID(Ref).toString() + "'");
64 return *Handle;
65}
66
67std::unique_ptr<MemoryBuffer>
69 bool RequiresNullTerminator) {
71 toStringRef(getData(Node, RequiresNullTerminator)), Name,
72 RequiresNullTerminator);
73}
74
76 SmallVectorImpl<ObjectRef> &Refs) const {
78 Refs.push_back(Ref);
79 return Error::success();
80 }));
81}
82
84 std::optional<ObjectRef> Ref = getReference(ID);
85 if (!Ref)
87
88 return getProxy(*Ref);
89}
90
92 std::optional<ObjectHandle> H;
93 if (Error E = load(Ref).moveInto(H))
94 return std::move(E);
95
96 return ObjectProxy::load(*this, Ref, *H);
97}
98
101 std::optional<ObjectHandle> H;
102 if (Error E = loadIfExists(Ref).moveInto(H))
103 return std::move(E);
104 if (!H)
105 return std::nullopt;
106 return ObjectProxy::load(*this, Ref, *H);
107}
108
110 return createStringError(std::make_error_code(std::errc::invalid_argument),
111 "unknown object '" + ID.toString() + "'");
112}
113
121
124 std::optional<sys::fs::file_status> Status) {
125 // TODO: For the on-disk CAS implementation use cloning to store it as a
126 // standalone file if the file-system supports it and the file is large.
127 uint64_t Size = Status ? Status->getSize() : -1;
128 auto Buffer = MemoryBuffer::getOpenFile(FD, /*Filename=*/"", Size);
129 if (!Buffer)
130 return errorCodeToError(Buffer.getError());
131
132 return store({}, arrayRefFromStringRef<char>((*Buffer)->getBuffer()));
133}
134
136 SmallDenseSet<ObjectRef> ValidatedRefs;
137 SmallVector<ObjectRef, 16> RefsToValidate;
138 RefsToValidate.push_back(Root);
139
140 while (!RefsToValidate.empty()) {
141 ObjectRef Ref = RefsToValidate.pop_back_val();
142 auto [I, Inserted] = ValidatedRefs.insert(Ref);
143 if (!Inserted)
144 continue; // already validated.
145 if (Error E = validateObject(getID(Ref)))
146 return E;
148 if (!Obj)
149 return Obj.takeError();
150 if (Error E = forEachRef(*Obj, [&RefsToValidate](ObjectRef R) -> Error {
151 RefsToValidate.push_back(R);
152 return Error::success();
153 }))
154 return E;
155 }
156 return Error::success();
157}
158
161 // Copy the full CAS tree from upstream with depth-first ordering to ensure
162 // all the child nodes are available in downstream CAS before inserting
163 // current object. This uses a similar algorithm as
164 // `OnDiskGraphDB::importFullTree` but doesn't assume the upstream CAS schema
165 // so it can be used to import from any other ObjectStore reguardless of the
166 // CAS schema.
167
168 // There is no work to do if importing from self.
169 if (this == &Upstream)
170 return Other;
171
172 /// Keeps track of the state of visitation for current node and all of its
173 /// parents. Upstream Cursor holds information only from upstream CAS.
174 struct UpstreamCursor {
177 size_t RefsCount;
178 std::deque<ObjectRef> Refs;
179 };
181 /// PrimaryNodeStack holds the ObjectRef of the current CAS, with nodes either
182 /// just stored in the CAS or nodes already exists in the current CAS.
183 SmallVector<ObjectRef, 128> PrimaryRefStack;
184 /// A map from upstream ObjectRef to current ObjectRef.
186
187 auto enqueueNode = [&](ObjectRef Ref, ObjectHandle Node) {
188 unsigned NumRefs = Upstream.getNumRefs(Node);
189 std::deque<ObjectRef> Refs;
190 for (unsigned I = 0; I < NumRefs; ++I)
191 Refs.push_back(Upstream.readRef(Node, I));
192
193 CursorStack.push_back({Ref, Node, NumRefs, std::move(Refs)});
194 };
195
196 auto UpstreamHandle = Upstream.load(Other);
197 if (!UpstreamHandle)
198 return UpstreamHandle.takeError();
199 enqueueNode(Other, *UpstreamHandle);
200
201 while (!CursorStack.empty()) {
202 UpstreamCursor &Cur = CursorStack.back();
203 if (Cur.Refs.empty()) {
204 // Copy the node data into the primary store.
205 // The bottom of \p PrimaryRefStack contains the ObjectRef for the
206 // current node.
207 assert(PrimaryRefStack.size() >= Cur.RefsCount);
208 auto Refs = ArrayRef(PrimaryRefStack)
209 .slice(PrimaryRefStack.size() - Cur.RefsCount);
210 auto NewNode = store(Refs, Upstream.getData(Cur.Node));
211 if (!NewNode)
212 return NewNode.takeError();
213
214 // Remove the current node and its IDs from the stack.
215 PrimaryRefStack.truncate(PrimaryRefStack.size() - Cur.RefsCount);
216 CursorStack.pop_back();
217
218 PrimaryRefStack.push_back(*NewNode);
219 CreatedObjects.try_emplace(Cur.Ref, *NewNode);
220 continue;
221 }
222
223 // Check if the node exists already.
224 auto CurrentID = Cur.Refs.front();
225 Cur.Refs.pop_front();
226 auto Ref = CreatedObjects.find(CurrentID);
227 if (Ref != CreatedObjects.end()) {
228 // If exists already, just need to enqueue the primary node.
229 PrimaryRefStack.push_back(Ref->second);
230 continue;
231 }
232
233 // Load child.
234 auto PrimaryID = Upstream.load(CurrentID);
235 if (LLVM_UNLIKELY(!PrimaryID))
236 return PrimaryID.takeError();
237
238 enqueueNode(CurrentID, *PrimaryID);
239 }
240
241 assert(PrimaryRefStack.size() == 1);
242 return PrimaryRefStack.front();
243}
244
245std::unique_ptr<MemoryBuffer>
247 bool RequiresNullTerminator) const {
248 return CAS->getMemoryBuffer(H, Name, RequiresNullTerminator);
249}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Mark last scratch load
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#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 the DenseSet and SmallDenseSet classes.
#define I(x, y, z)
Definition MD5.cpp:58
#define H(x, y, z)
Definition MD5.cpp:57
static void printReferenceBase(raw_ostream &OS, StringRef Kind, uint64_t InternalRef, std::optional< CASID > ID)
This file contains the declaration of the ObjectStore class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:248
iterator end()
Definition DenseMap.h:81
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
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:291
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void truncate(size_type N)
Like resize, but requires that N is less than size().
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Unique identifier for a CAS object.
Definition CASID.h:58
void dump() const
void print(raw_ostream &OS) const
Print CASID.
Definition CASID.h:68
std::string toString() const
Return a printable string for CASID.
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.
LLVM_DUMP_METHOD void dump() const
static ObjectProxy load(ObjectStore &CAS, ObjectRef Ref, ObjectHandle Node)
std::unique_ptr< MemoryBuffer > getMemoryBuffer(StringRef Name="", bool RequiresNullTerminator=true) const
Reference to an object in an ObjectStore instance.
void print(raw_ostream &OS) const
Print internal ref and/or CASID. Only suitable for debugging.
LLVM_DUMP_METHOD void dump() const
Expected< ObjectHandle > load(ObjectRef Ref)
Like loadIfExists but returns an error if the object is missing.
Expected< ObjectProxy > createProxy(ArrayRef< ObjectRef > Refs, StringRef Data)
Helper functions to store object and returns a ObjectProxy.
virtual void print(raw_ostream &) const
Print the ObjectStore internals for debugging purpose.
virtual Error validateObject(const CASID &ID)=0
Validate the underlying object referred by CASID.
Expected< ObjectRef > importObject(ObjectStore &Upstream, ObjectRef Other)
Import object from another CAS.
Expected< std::optional< ObjectProxy > > getProxyIfExists(ObjectRef Ref)
virtual Expected< ObjectRef > store(ArrayRef< ObjectRef > Refs, ArrayRef< char > Data)=0
Store object into ObjectStore.
virtual ArrayRef< char > getData(ObjectHandle Node, bool RequiresNullTerminator=false) const =0
virtual CASID getID(ObjectRef Ref) const =0
Get an ID for Ref.
static Error createUnknownObjectError(const CASID &ID)
virtual Expected< std::optional< ObjectHandle > > loadIfExists(ObjectRef Ref)=0
Load the object referenced by Ref.
Error validateTree(ObjectRef Ref)
Validate the whole node tree.
virtual ObjectRef readRef(ObjectHandle Node, size_t I) const =0
ObjectStore(const CASContext &Context)
virtual Expected< ObjectRef > storeFromOpenFileImpl(sys::fs::file_t FD, std::optional< sys::fs::file_status > Status)
Get ObjectRef from open file.
virtual void readRefs(ObjectHandle Node, SmallVectorImpl< ObjectRef > &Refs) const
Read all the refs from object in a SmallVector.
virtual size_t getNumRefs(ObjectHandle Node) const =0
std::unique_ptr< MemoryBuffer > getMemoryBuffer(ObjectHandle Node, StringRef Name="", bool RequiresNullTerminator=true)
Get a lifetime-extended MemoryBuffer pointing at Data.
virtual std::optional< ObjectRef > getReference(const CASID &ID) const =0
Get an existing reference to the object called ID.
Expected< ObjectProxy > getProxy(const CASID &ID)
Create ObjectProxy from CASID. If the object doesn't exist, get an error.
virtual Error forEachRef(ObjectHandle Node, function_ref< Error(ObjectRef)> Callback) const =0
Methods for handling objects.
void print(raw_ostream &OS, const ObjectHandle &This) const
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
@ invalid_argument
Definition Errc.h:56
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ Other
Any other memory.
Definition ModRef.h:68
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:111
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
StringRef toStringRef(bool B)
Construct a string ref from a boolean.