From bf35e6ab2aae9b3d529d0659248ebb1af1a330d6 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Tue, 23 May 2017 15:50:37 +0000 Subject: [PATCH] Revert "Make TypeSerializer's StringMap use the same allocator." This reverts commit e34ccb7b57da25cc89ded913d8638a2906d1110a. This is causing failures on the ASAN bots. llvm-svn: 303640 --- .../llvm/DebugInfo/CodeView/TypeSerializer.h | 15 ++++---- .../llvm/DebugInfo/CodeView/TypeTableBuilder.h | 4 +- .../llvm/DebugInfo/CodeView/TypeTableCollection.h | 4 +- llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp | 43 ++++++++++++++++------ .../lib/DebugInfo/CodeView/TypeTableCollection.cpp | 2 +- llvm/tools/llvm-pdbdump/llvm-pdbdump.cpp | 4 +- 6 files changed, 46 insertions(+), 26 deletions(-) diff --git a/llvm/include/llvm/DebugInfo/CodeView/TypeSerializer.h b/llvm/include/llvm/DebugInfo/CodeView/TypeSerializer.h index f265670..6dad982 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/TypeSerializer.h +++ b/llvm/include/llvm/DebugInfo/CodeView/TypeSerializer.h @@ -45,13 +45,12 @@ class TypeSerializer : public TypeVisitorCallbacks { } }; - typedef SmallVector, 2> MutableRecordList; - typedef SmallVector, 2> RecordList; + typedef SmallVector, 2> RecordList; static constexpr uint8_t ContinuationLength = 8; BumpPtrAllocator &RecordStorage; RecordSegment CurrentSegment; - MutableRecordList FieldListSegments; + RecordList FieldListSegments; TypeIndex LastTypeIndex; Optional TypeKind; @@ -62,7 +61,7 @@ class TypeSerializer : public TypeVisitorCallbacks { TypeRecordMapping Mapping; RecordList SeenRecords; - StringMap HashedRecords; + StringMap HashedRecords; bool isInFieldList() const; TypeIndex calcNextTypeIndex() const; @@ -70,7 +69,9 @@ class TypeSerializer : public TypeVisitorCallbacks { MutableArrayRef getCurrentSubRecordData(); MutableArrayRef getCurrentRecordData(); Error writeRecordPrefix(TypeLeafKind Kind); - TypeIndex insertRecordBytesPrivate(ArrayRef &Record); + TypeIndex insertRecordBytesPrivate(MutableArrayRef Record); + TypeIndex insertRecordBytesWithCopy(CVType &Record, + MutableArrayRef Data); Expected> addPadding(MutableArrayRef Record); @@ -78,9 +79,9 @@ class TypeSerializer : public TypeVisitorCallbacks { public: explicit TypeSerializer(BumpPtrAllocator &Storage); - ArrayRef> records() const; + ArrayRef> records() const; TypeIndex getLastTypeIndex() const; - TypeIndex insertRecordBytes(ArrayRef Record); + TypeIndex insertRecordBytes(MutableArrayRef Record); Expected visitTypeEndGetIndex(CVType &Record); Error visitTypeBegin(CVType &Record) override; diff --git a/llvm/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h b/llvm/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h index 9cb43b4..102bee4 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h +++ b/llvm/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h @@ -64,7 +64,7 @@ public: return *ExpectedIndex; } - TypeIndex writeSerializedRecord(ArrayRef Record) { + TypeIndex writeSerializedRecord(MutableArrayRef Record) { return Serializer.insertRecordBytes(Record); } @@ -77,7 +77,7 @@ public: } } - ArrayRef> records() const { + ArrayRef> records() const { return Serializer.records(); } }; diff --git a/llvm/include/llvm/DebugInfo/CodeView/TypeTableCollection.h b/llvm/include/llvm/DebugInfo/CodeView/TypeTableCollection.h index 42b62ba..7de562a 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/TypeTableCollection.h +++ b/llvm/include/llvm/DebugInfo/CodeView/TypeTableCollection.h @@ -18,7 +18,7 @@ namespace codeview { class TypeTableCollection : public TypeCollection { public: - explicit TypeTableCollection(ArrayRef> Records); + explicit TypeTableCollection(ArrayRef> Records); Optional getFirst() override; Optional getNext(TypeIndex Prev) override; @@ -33,7 +33,7 @@ private: bool hasCapacityFor(TypeIndex Index) const; void ensureTypeExists(TypeIndex Index); - ArrayRef> Records; + ArrayRef> Records; TypeDatabase Database; }; } diff --git a/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp b/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp index f986560..3b061e6 100644 --- a/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp @@ -52,26 +52,45 @@ Error TypeSerializer::writeRecordPrefix(TypeLeafKind Kind) { } TypeIndex -TypeSerializer::insertRecordBytesPrivate(ArrayRef &Record) { +TypeSerializer::insertRecordBytesPrivate(MutableArrayRef Record) { assert(Record.size() % 4 == 0 && "Record is not aligned to 4 bytes!"); StringRef S(reinterpret_cast(Record.data()), Record.size()); TypeIndex NextTypeIndex = calcNextTypeIndex(); auto Result = HashedRecords.try_emplace(S, NextTypeIndex); - - StringRef NewData = Result.first->getKey(); - Record = ArrayRef(NewData.bytes_begin(), NewData.bytes_end()); - if (Result.second) { - // If this triggered an insert into the map, store the bytes. LastTypeIndex = NextTypeIndex; SeenRecords.push_back(Record); } - return Result.first->getValue(); } +TypeIndex +TypeSerializer::insertRecordBytesWithCopy(CVType &Record, + MutableArrayRef Data) { + assert(Data.size() % 4 == 0 && "Record is not aligned to 4 bytes!"); + + StringRef S(reinterpret_cast(Data.data()), Data.size()); + + // Do a two state lookup / insert so that we don't have to allocate unless + // we're going + // to do an insert. This is a big memory savings. + auto Iter = HashedRecords.find(S); + if (Iter != HashedRecords.end()) + return Iter->second; + + LastTypeIndex = calcNextTypeIndex(); + uint8_t *Copy = RecordStorage.Allocate(Data.size()); + ::memcpy(Copy, Data.data(), Data.size()); + Data = MutableArrayRef(Copy, Data.size()); + S = StringRef(reinterpret_cast(Data.data()), Data.size()); + HashedRecords.insert(std::make_pair(S, LastTypeIndex)); + SeenRecords.push_back(Data); + Record.RecordData = Data; + return LastTypeIndex; +} + Expected> TypeSerializer::addPadding(MutableArrayRef Record) { uint32_t Align = Record.size() % 4; @@ -93,19 +112,19 @@ TypeSerializer::TypeSerializer(BumpPtrAllocator &Storage) : RecordStorage(Storage), LastTypeIndex(), RecordBuffer(MaxRecordLength * 2), Stream(RecordBuffer, llvm::support::little), Writer(Stream), - Mapping(Writer), HashedRecords(Storage) { + Mapping(Writer) { // RecordBuffer needs to be able to hold enough data so that if we are 1 // byte short of MaxRecordLen, and then we try to write MaxRecordLen bytes, // we won't overflow. } -ArrayRef> TypeSerializer::records() const { +ArrayRef> TypeSerializer::records() const { return SeenRecords; } TypeIndex TypeSerializer::getLastTypeIndex() const { return LastTypeIndex; } -TypeIndex TypeSerializer::insertRecordBytes(ArrayRef Record) { +TypeIndex TypeSerializer::insertRecordBytes(MutableArrayRef Record) { assert(!TypeKind.hasValue() && "Already in a type mapping!"); assert(Writer.getOffset() == 0 && "Stream has data already!"); @@ -144,8 +163,8 @@ Expected TypeSerializer::visitTypeEndGetIndex(CVType &Record) { Prefix->RecordLen = ThisRecordData.size() - sizeof(uint16_t); Record.Type = *TypeKind; - Record.RecordData = ThisRecordData; - TypeIndex InsertedTypeIndex = insertRecordBytesPrivate(Record.RecordData); + TypeIndex InsertedTypeIndex = + insertRecordBytesWithCopy(Record, ThisRecordData); // Write out each additional segment in reverse order, and update each // record's continuation index to point to the previous one. diff --git a/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp b/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp index 4adecbc..a18710d 100644 --- a/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp @@ -25,7 +25,7 @@ static void error(Error &&EC) { } TypeTableCollection::TypeTableCollection( - ArrayRef> Records) + ArrayRef> Records) : Records(Records), Database(Records.size()) {} Optional TypeTableCollection::getFirst() { diff --git a/llvm/tools/llvm-pdbdump/llvm-pdbdump.cpp b/llvm/tools/llvm-pdbdump/llvm-pdbdump.cpp index 3394d32..6b6a0ef 100644 --- a/llvm/tools/llvm-pdbdump/llvm-pdbdump.cpp +++ b/llvm/tools/llvm-pdbdump/llvm-pdbdump.cpp @@ -878,11 +878,11 @@ static void mergePdbs() { auto &DestTpi = Builder.getTpiBuilder(); auto &DestIpi = Builder.getIpiBuilder(); MergedTpi.ForEachRecord( - [&DestTpi](TypeIndex TI, ArrayRef Data) { + [&DestTpi](TypeIndex TI, MutableArrayRef Data) { DestTpi.addTypeRecord(Data, None); }); MergedIpi.ForEachRecord( - [&DestIpi](TypeIndex TI, ArrayRef Data) { + [&DestIpi](TypeIndex TI, MutableArrayRef Data) { DestIpi.addTypeRecord(Data, None); }); -- 2.7.4