28 using namespace llvm::codeview;
41 struct HashedTypePtr {
42 HashedTypePtr() =
default;
43 HashedTypePtr(HashedType *
Ptr) : Ptr(Ptr) {}
45 HashedType *
Ptr =
nullptr;
53 static inline HashedTypePtr
getEmptyKey() {
return HashedTypePtr(
nullptr); }
56 return HashedTypePtr(reinterpret_cast<HashedType *>(1));
60 assert(Val.Ptr != getEmptyKey().
Ptr && Val.Ptr != getTombstoneKey().
Ptr);
64 static bool isEqual(HashedTypePtr LHSP, HashedTypePtr RHSP) {
65 HashedType *LHS = LHSP.Ptr;
66 HashedType *RHS = RHSP.Ptr;
67 if (RHS == getEmptyKey().
Ptr || RHS == getTombstoneKey().
Ptr)
69 if (LHS->Hash != RHS->Hash || LHS->Size != RHS->Size)
71 return ::memcmp(LHS->Data, RHS->Data, LHS->Size) == 0;
109 assert(Record.
size() < UINT32_MAX &&
"Record too big");
110 assert(Record.
size() % 4 == 0 &&
"Record is not aligned to 4 bytes!");
115 auto Result = HashedRecords.
insert(HashedTypePtr(&TempHashedType));
116 HashedType *&Hashed = Result.first->Ptr;
121 Hashed = KeyStorage.
Allocate<HashedType>();
122 *Hashed = TempHashedType;
124 uint8_t *Stable = RecordStorage.
Allocate<uint8_t>(Record.
size());
125 memcpy(Stable, Record.
data(), Record.
size());
126 Hashed->Data = Stable;
132 return Hashed->Index;
135 TypeIndex TypeSerializer::nextTypeIndex()
const {
139 bool TypeSerializer::isInFieldList()
const {
145 return getCurrentRecordData().drop_front(CurrentSegment.length());
156 if (
auto EC = Writer.writeObject(Prefix))
167 int PaddingBytes = 4 -
Align;
168 int N = PaddingBytes;
169 while (PaddingBytes > 0) {
170 uint8_t Pad =
static_cast<uint8_t
>(LF_PAD0 + PaddingBytes);
171 if (
auto EC = Writer.writeInteger(Pad))
172 return std::move(
EC);
180 Stream(RecordBuffer, support::
little), Writer(Stream),
186 Hasher = llvm::make_unique<TypeHasher>(Storage);
199 CurrentSegment = RecordSegment();
200 FieldListSegments.
clear();
211 TypeIndex ActualTI = Hasher->getOrCreateRecord(Record, nextTypeIndex());
212 if (nextTypeIndex() == ActualTI)
213 SeenRecords.push_back(Record);
218 uint8_t *Stable = RecordStorage.
Allocate<uint8_t>(Record.
size());
219 memcpy(Stable, Record.
data(), Record.
size());
221 SeenRecords.push_back(Record);
242 ::memcpy(&RemapStorage[0], OriginalData.
data(), OriginalData.
size());
244 for (
const auto &M : Record.
Mappings) {
247 *(
TypeIndex *)(ContentBegin + M.first) = M.second;
257 if (
auto EC = writeRecordPrefix(Record.
kind()))
260 TypeKind = Record.
kind();
270 return std::move(
EC);
274 auto ThisRecordData = getCurrentRecordData();
275 auto ExpectedData = addPadding(ThisRecordData);
277 return ExpectedData.takeError();
278 ThisRecordData = *ExpectedData;
281 reinterpret_cast<RecordPrefix *
>(ThisRecordData.data());
282 Prefix->
RecordLen = ThisRecordData.size() -
sizeof(uint16_t);
284 Record.
Type = *TypeKind;
295 for (
auto X :
reverse(FieldListSegments)) {
296 auto CIBytes =
X.take_back(
sizeof(
uint32_t));
299 assert(*CI == 0xB0C0B0C0 &&
"Invalid TypeIndex placeholder");
304 FieldListSegments.
clear();
305 CurrentSegment.SubRecords.clear();
307 return InsertedTypeIndex;
313 return ExpectedIndex.takeError();
318 assert(isInFieldList() &&
"Not in a field list!");
320 MemberKind = Record.
Kind;
337 SubRecord LastSubRecord = CurrentSegment.SubRecords.back();
338 uint32_t CopySize = CurrentSegment.length() - LastSubRecord.Size;
340 auto LeftOverData = Data.
drop_front(CopySize);
341 assert(LastSubRecord.Size == LeftOverData.size());
345 uint16_t LengthWithSize = CopySize + ContinuationLength;
348 Prefix->
RecordLen = LengthWithSize -
sizeof(uint16_t);
350 uint8_t *SegmentBytes = RecordStorage.
Allocate<uint8_t>(LengthWithSize);
356 if (
auto EC = CW.
writeEnum(TypeLeafKind::LF_INDEX))
362 FieldListSegments.
push_back(SavedSegment);
367 if (
auto EC = writeRecordPrefix(TypeLeafKind::LF_FIELDLIST))
375 LeftOverData.size());
379 CurrentSegment.SubRecords.clear();
380 CurrentSegment.SubRecords.push_back(LastSubRecord);
385 Record.
Data = getCurrentSubRecordData();
void push_back(const T &Elt)
MutableArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
TypeIndex getOrCreateRecord(ArrayRef< uint8_t > &Record, TypeIndex TI)
Takes the bytes of type record, inserts them into the hash table, saves them, and returns a pointer t...
TypeLeafKind
Duplicate copy of the above enum, but using the official CV names.
Error visitMemberBegin(CVMemberRecord &Record) override
TypeIndex insertRecord(const RemappedType &Record)
Error visitTypeEnd(CVType &Record) override
Private implementation so that we don't leak our DenseMap instantiations to users.
uint32_t getOffset() const
TypeHasher(BumpPtrAllocator &RecordStorage)
Error visitMemberEnd(CVMemberRecord &Record) override
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
SmallVector< std::pair< uint32_t, TypeIndex >, 8 > Mappings
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
CVRecord< Kind > OriginalRecord
static bool isEqual(HashedTypePtr LHSP, HashedTypePtr RHSP)
Tagged union holding either a T or a Error.
Error visitTypeEnd(CVType &Record) override
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Error visitTypeBegin(CVType &Record) override
Paired begin/end actions for all types.
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
size_t size() const
size - Get the array size.
static TypeIndex fromArrayIndex(uint32_t Index)
uint32_t getIndex() const
Error visitTypeBegin(CVType &Record) override
Paired begin/end actions for all types.
Allocate memory in an ever growing pool, as if by bump-pointer.
static HashedTypePtr getTombstoneKey()
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
std::pair< iterator, bool > insert(const ValueT &V)
static HashedTypePtr getEmptyKey()
Provides write only access to a subclass of WritableBinaryStream.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
Error writeInteger(T Value)
Write the the integer Value to the underlying stream in the specified endianness. ...
static ErrorSuccess success()
Create a success value.
static unsigned getHashValue(HashedTypePtr Val)
~TypeSerializer() override
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Error writeEnum(T Num)
Similar to writeInteger.
void setOffset(uint32_t Off)
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
ArrayRef< uint8_t > RecordData
pointer data()
Return a pointer to the vector's buffer, even if empty().
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Error visitMemberEnd(CVMemberRecord &Record) override
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MutableArrayRef< uint8_t > data() const
Lightweight error class with error context and mandatory checking.
ArrayRef< ArrayRef< uint8_t > > records() const
std::string Hash(const Unit &U)
TypeIndex insertRecordBytes(ArrayRef< uint8_t > &Record)
Error visitMemberBegin(CVMemberRecord &Record) override
Expected< TypeIndex > visitTypeEndGetIndex(CVType &Record)
TypeSerializer(BumpPtrAllocator &Storage, bool Hash=true)