LLVM 23.0.0git
NamedValuesSchema.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/Support/Endian.h"
13
14using namespace llvm;
15using namespace llvm::cas;
16
18constexpr StringLiteral NamedValuesSchema::SchemaName;
19
20void NamedValuesSchema::anchor() {}
21
23 // Load the first ref to check its content.
24 if (Node.getNumReferences() < 1)
25 return false;
26
27 auto FirstRef = Node.getReference(0);
28 return FirstRef == *NamedValuesKindRef;
29}
30
31NamedValuesSchema::NamedValuesSchema(cas::ObjectStore &CAS, Error &E)
33 ErrorAsOutParameter EAOP(E);
34 auto Kind = CAS.storeFromString({}, SchemaName);
35 if (!Kind) {
36 E = Kind.takeError();
37 return;
38 }
39 NamedValuesKindRef = *Kind;
40}
41
44 NamedValuesSchema S(CAS, E);
45 if (E)
46 return std::move(E);
47 return S;
48}
49
50size_t NamedValuesSchema::getNumEntries(NamedValuesProxy Values) const {
51 return Values.getNumReferences() - 1;
52}
53
54Error NamedValuesSchema::forEachEntry(
56 function_ref<Error(const NamedValuesEntry &)> Callback) const {
57 for (size_t I = 0, IE = getNumEntries(Values); I != IE; ++I)
58 if (Error E = Callback(loadEntry(Values, I)))
59 return E;
60
61 return Error::success();
62}
63
64NamedValuesEntry NamedValuesSchema::loadEntry(NamedValuesProxy Values,
65 size_t I) const {
66 StringRef Name = Values.getName(I);
67 auto ObjectRef = Values.getReference(I + 1);
68
69 return {Name, ObjectRef};
70}
71
72std::optional<size_t> NamedValuesSchema::lookupEntry(NamedValuesProxy Values,
73 StringRef Name) const {
74 size_t NumNames = getNumEntries(Values);
75 if (!NumNames)
76 return std::nullopt;
77
78 // Start with a binary search, if there are enough entries.
79 // FIXME: MaxLinearSearchSize is a heuristic and not optimized.
80 const size_t MaxLinearSearchSize = 4;
81 size_t Last = NumNames;
82 size_t First = 0;
83 while (Last - First > MaxLinearSearchSize) {
84 auto I = First + (Last - First) / 2;
85 StringRef NameI = Values.getName(I);
86 switch (Name.compare(NameI)) {
87 case 0:
88 return I;
89 case -1:
90 Last = I;
91 break;
92 case 1:
93 First = I + 1;
94 break;
95 }
96 }
97
98 // Use a linear search for small list.
99 for (; First != Last; ++First)
100 if (Name == Values.getName(First))
101 return First;
102
103 return std::nullopt;
104}
105
107 auto Node = CAS.getProxy(Object);
108 if (!Node)
109 return Node.takeError();
110
111 return load(*Node);
112}
113
115 if (!isNode(Object))
117 "object does not conform to NamedValuesSchema");
118
119 return NamedValuesProxy(*this, Object);
120}
121
124 // ScratchPad for output.
127 Refs.push_back(*NamedValuesKindRef);
128
129 // Ensure a stable order for entries and ignore name collisions.
130 SmallVector<NamedValuesEntry> Sorted(Entries);
131 llvm::stable_sort(Sorted);
132
133 if (llvm::unique(Sorted) != Sorted.end())
134 return createStringError("entry names are not unique");
135
138 // Encode the entries in the Data. The layout of the named values schema
139 // object is:
140 // * Name offset table: The offset of in the data blob for where to find the
141 // string. It has N + 1 entries and you can find the name of n-th entry at
142 // offset[n] -> offset[n+1]. Each offset is encoded as little-endian
143 // uint32_t.
144 // * Object: ObjectRef for each entry is at n + 1 refs for the object (with
145 // the first one being the named value kind ID).
146
147 // Write Name.
148 // The start of the string table index.
149 uint32_t StrIdx = sizeof(uint32_t) * (Sorted.size() + 1);
150 for (auto &Entry : Sorted) {
151 Writer.write(StrIdx);
152 StrIdx += Entry.Name.size();
153
154 // Append refs.
155 Refs.push_back(Entry.Ref);
156 }
157 // Write the end index for the last string.
158 Writer.write(StrIdx);
159
160 // Write names in the end of the block.
161 for (auto &Entry : Sorted)
162 OS << Entry.Name;
163
164 auto Proxy = CAS.createProxy(Refs, Data);
165 if (!Proxy)
166 return Proxy.takeError();
167
168 return NamedValuesProxy(*this, *Proxy);
169}
170
172 StringSaver Saver(Alloc);
173 Nodes.emplace_back(Saver.save(Name), Ref);
174}
175
177 auto Schema = NamedValuesSchema::create(CAS);
178 if (!Schema)
179 return Schema.takeError();
180 return Schema->construct(Nodes);
181}
182
184 uint32_t StartIdx =
186 uint32_t EndIdx =
187 support::endian::read32le(getData().data() + sizeof(uint32_t) * (I + 1));
188
189 return StringRef(getData().data() + StartIdx, EndIdx - StartIdx);
190}
AMDGPU Mark last scratch load
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for the NamedValuesSchema, a schema to represent an array of name...
static Split data
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Helper for Errors used as out-parameters.
Definition Error.h:1160
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
Inheritance utility for extensible RTTI.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:888
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
StringRef save(const char *S)
Definition StringSaver.h:31
A proxy for a loaded CAS Object in NamedValuesSchema.
LLVM_ABI StringRef getName(size_t I) const
Get the name of an entry by index.
LLVM_ABI void add(StringRef Name, ObjectRef Ref)
Add an entry to the builder.
LLVM_ABI Expected< NamedValuesProxy > build()
Build the node from added entries.
A schema for representing an array of named nodes in a CAS.
Expected< NamedValuesProxy > load(ObjectRef Object) const
Load NamedValuesProxy from an ObjectRef.
static Expected< NamedValuesSchema > create(ObjectStore &CAS)
Create a NamedValuesSchema.
Expected< NamedValuesProxy > construct(ArrayRef< NamedValuesEntry > Entries)
Construct a NamedValuesSchema CAS object with the given entries.
bool isNode(const ObjectProxy &Node) const final
Check if a proxy represents a valid node.
Reference to an abstract hierarchical node, with data and references.
StringRef getData() const
Get the content of the node. Valid as long as the CAS is valid.
Reference to an object in an ObjectStore instance.
Content-addressable storage for objects.
Definition ObjectStore.h:90
Expected< ObjectProxy > createProxy(ArrayRef< ObjectRef > Refs, StringRef Data)
Helper functions to store object and returns a ObjectProxy.
Expected< ObjectRef > storeFromString(ArrayRef< ObjectRef > Refs, StringRef String)
Store object from StringRef.
Expected< ObjectProxy > getProxy(const CASID &ID)
Create ObjectProxy from CASID. If the object doesn't exist, get an error.
An efficient, type-erasing, non-owning reference to a callable.
A raw_ostream that writes to an SmallVector or SmallString.
uint32_t read32le(const void *P)
Definition Endian.h:432
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
Definition STLExtras.h:2116
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2134
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
Represents an entry in NamedValuesSchema.
Adapter to write values to a stream in a particular byte order.
void write(ArrayRef< value_type > Val)