Skip to content

Commit

Permalink
[CodeView] Micro-optimizations to speed up type merging.
Browse files Browse the repository at this point in the history
Based on a profile, a couple of hot spots were identified in the
main type merging loop.  The code was simplified, a few loops
were re-arranged, and some outlined functions were inlined.  This
speeds up type merging by a decent amount, shaving around 3-4 seconds
off of a 40 second link in my test case.

Differential Revision: https://reviews.llvm.org/D42559

llvm-svn: 323790
  • Loading branch information
Zachary Turner committed Jan 30, 2018
1 parent 43a68f1 commit 07d8037
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 118 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/DebugInfo/CodeView/CVRecord.h
Expand Up @@ -70,7 +70,7 @@ Error forEachCodeViewRecord(ArrayRef<uint8_t> StreamBuffer, Func F) {
const RecordPrefix *Prefix =
reinterpret_cast<const RecordPrefix *>(StreamBuffer.data());

uint16_t RealLen = Prefix->RecordLen + 2;
size_t RealLen = Prefix->RecordLen + 2;
if (StreamBuffer.size() < RealLen)
return make_error<CodeViewError>(cv_error_code::corrupt_record);

Expand Down
17 changes: 15 additions & 2 deletions llvm/include/llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h
Expand Up @@ -69,9 +69,22 @@ class GlobalTypeTableBuilder : public TypeCollection {
ArrayRef<ArrayRef<uint8_t>> records() const;
ArrayRef<GloballyHashedType> hashes() const;

using CreateRecord = llvm::function_ref<ArrayRef<uint8_t>()>;
template <typename CreateFunc>
TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize,
CreateFunc Create) {
auto Result = HashedRecords.try_emplace(Hash, nextTypeIndex());

if (LLVM_UNLIKELY(Result.second)) {
uint8_t *Stable = RecordStorage.Allocate<uint8_t>(RecordSize);
MutableArrayRef<uint8_t> Data(Stable, RecordSize);
SeenRecords.push_back(Create(Data));
SeenHashes.push_back(Hash);
}

// Update the caller's copy of Record to point a stable copy.
return Result.first->second;
}

TypeIndex insertRecordAs(GloballyHashedType Hash, CreateRecord Create);
TypeIndex insertRecordBytes(ArrayRef<uint8_t> Data);
TypeIndex insertRecord(ContinuationRecordBuilder &Builder);

Expand Down
37 changes: 12 additions & 25 deletions llvm/lib/DebugInfo/CodeView/GlobalTypeTableBuilder.cpp
Expand Up @@ -55,9 +55,12 @@ Optional<TypeIndex> GlobalTypeTableBuilder::getNext(TypeIndex Prev) {
CVType GlobalTypeTableBuilder::getType(TypeIndex Index) {
CVType Type;
Type.RecordData = SeenRecords[Index.toArrayIndex()];
const RecordPrefix *P =
reinterpret_cast<const RecordPrefix *>(Type.RecordData.data());
Type.Type = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
if (!Type.RecordData.empty()) {
assert(Type.RecordData.size() >= sizeof(RecordPrefix));
const RecordPrefix *P =
reinterpret_cast<const RecordPrefix *>(Type.RecordData.data());
Type.Type = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
}
return Type;
}

Expand Down Expand Up @@ -89,31 +92,15 @@ void GlobalTypeTableBuilder::reset() {
SeenRecords.clear();
}

static inline ArrayRef<uint8_t> stabilize(BumpPtrAllocator &Alloc,
ArrayRef<uint8_t> Data) {
uint8_t *Stable = Alloc.Allocate<uint8_t>(Data.size());
memcpy(Stable, Data.data(), Data.size());
return makeArrayRef(Stable, Data.size());
}

TypeIndex GlobalTypeTableBuilder::insertRecordAs(GloballyHashedType Hash,
CreateRecord Create) {
auto Result = HashedRecords.try_emplace(Hash, nextTypeIndex());

if (Result.second) {
ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Create());
SeenRecords.push_back(RecordData);
SeenHashes.push_back(Hash);
}

// Update the caller's copy of Record to point a stable copy.
return Result.first->second;
}

TypeIndex GlobalTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> Record) {
GloballyHashedType GHT =
GloballyHashedType::hashType(Record, SeenHashes, SeenHashes);
return insertRecordAs(GHT, [Record]() { return Record; });
return insertRecordAs(GHT, Record.size(),
[Record](MutableArrayRef<uint8_t> Data) {
assert(Data.size() == Record.size());
::memcpy(Data.data(), Record.data(), Record.size());
return Data;
});
}

TypeIndex
Expand Down
169 changes: 79 additions & 90 deletions llvm/lib/DebugInfo/CodeView/TypeStreamMerger.cpp
Expand Up @@ -20,6 +20,11 @@
using namespace llvm;
using namespace llvm::codeview;

static inline size_t slotForIndex(TypeIndex Idx) {
assert(!Idx.isSimple() && "simple type indices have no slots");
return Idx.getIndex() - TypeIndex::FirstNonSimpleIndex;
}

namespace {

/// Implementation of CodeView type stream merging.
Expand Down Expand Up @@ -94,8 +99,22 @@ class TypeStreamMerger {

void addMapping(TypeIndex Idx);

bool remapTypeIndex(TypeIndex &Idx);
bool remapItemIndex(TypeIndex &Idx);
inline bool remapTypeIndex(TypeIndex &Idx) {
// If we're mapping a pure index stream, then IndexMap only contains
// mappings from OldIdStream -> NewIdStream, in which case we will need to
// use the special mapping from OldTypeStream -> NewTypeStream which was
// computed externally. Regardless, we use this special map if and only if
// we are doing an id-only mapping.
if (!hasTypeStream())
return remapIndex(Idx, TypeLookup);

assert(TypeLookup.empty());
return remapIndex(Idx, IndexMap);
}
inline bool remapItemIndex(TypeIndex &Idx) {
assert(hasIdStream());
return remapIndex(Idx, IndexMap);
}

bool hasTypeStream() const {
return (UseGlobalHashes) ? (!!DestGlobalTypeStream) : (!!DestTypeStream);
Expand All @@ -105,17 +124,34 @@ class TypeStreamMerger {
return (UseGlobalHashes) ? (!!DestGlobalIdStream) : (!!DestIdStream);
}

ArrayRef<uint8_t> serializeRemapped(const RemappedType &Record);
ArrayRef<uint8_t> remapIndices(const CVType &OriginalType,
MutableArrayRef<uint8_t> Storage);

inline bool remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map) {
if (LLVM_LIKELY(remapIndexSimple(Idx, Map)))
return true;

return remapIndexFallback(Idx, Map);
}

bool remapIndices(RemappedType &Record, ArrayRef<TiReference> Refs);
inline bool remapIndexSimple(TypeIndex &Idx, ArrayRef<TypeIndex> Map) const {
// Simple types are unchanged.
if (Idx.isSimple())
return true;

bool remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map);
// Check if this type index refers to a record we've already translated
// successfully. If it refers to a type later in the stream or a record we
// had to defer, defer it until later pass.
unsigned MapPos = slotForIndex(Idx);
if (LLVM_UNLIKELY(MapPos >= Map.size() || Map[MapPos] == Untranslated))
return false;

size_t slotForIndex(TypeIndex Idx) const {
assert(!Idx.isSimple() && "simple type indices have no slots");
return Idx.getIndex() - TypeIndex::FirstNonSimpleIndex;
Idx = Map[MapPos];
return true;
}

bool remapIndexFallback(TypeIndex &Idx, ArrayRef<TypeIndex> Map);

Error errorCorruptRecord() const {
return llvm::make_error<CodeViewError>(cv_error_code::corrupt_record);
}
Expand Down Expand Up @@ -153,27 +189,6 @@ class TypeStreamMerger {

} // end anonymous namespace

ArrayRef<uint8_t>
TypeStreamMerger::serializeRemapped(const RemappedType &Record) {
TypeIndex TI;
ArrayRef<uint8_t> OriginalData = Record.OriginalRecord.RecordData;
if (Record.Mappings.empty())
return OriginalData;

// At least one type index was remapped. We copy the full record bytes,
// re-write each type index, then return that.
RemapStorage.resize(OriginalData.size());
::memcpy(&RemapStorage[0], OriginalData.data(), OriginalData.size());
uint8_t *ContentBegin = RemapStorage.data() + sizeof(RecordPrefix);
for (const auto &M : Record.Mappings) {
// First 4 bytes of every record are the record prefix, but the mapping
// offset is relative to the content which starts after.
*(TypeIndex *)(ContentBegin + M.first) = M.second;
}
auto RemapRef = makeArrayRef(RemapStorage);
return RemapRef;
}

const TypeIndex TypeStreamMerger::Untranslated(SimpleTypeKind::NotTranslated);

static bool isIdRecord(TypeLeafKind K) {
Expand Down Expand Up @@ -202,19 +217,9 @@ void TypeStreamMerger::addMapping(TypeIndex Idx) {
}
}

bool TypeStreamMerger::remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map) {
// Simple types are unchanged.
if (Idx.isSimple())
return true;

// Check if this type index refers to a record we've already translated
// successfully. If it refers to a type later in the stream or a record we
// had to defer, defer it until later pass.
unsigned MapPos = slotForIndex(Idx);
if (MapPos < Map.size() && Map[MapPos] != Untranslated) {
Idx = Map[MapPos];
return true;
}
bool TypeStreamMerger::remapIndexFallback(TypeIndex &Idx,
ArrayRef<TypeIndex> Map) {
size_t MapPos = slotForIndex(Idx);

// If this is the second pass and this index isn't in the map, then it points
// outside the current type stream, and this is a corrupt record.
Expand All @@ -232,24 +237,6 @@ bool TypeStreamMerger::remapIndex(TypeIndex &Idx, ArrayRef<TypeIndex> Map) {
return false;
}

bool TypeStreamMerger::remapTypeIndex(TypeIndex &Idx) {
// If we're mapping a pure index stream, then IndexMap only contains mappings
// from OldIdStream -> NewIdStream, in which case we will need to use the
// special mapping from OldTypeStream -> NewTypeStream which was computed
// externally. Regardless, we use this special map if and only if we are
// doing an id-only mapping.
if (!hasTypeStream())
return remapIndex(Idx, TypeLookup);

assert(TypeLookup.empty());
return remapIndex(Idx, IndexMap);
}

bool TypeStreamMerger::remapItemIndex(TypeIndex &Idx) {
assert(hasIdStream());
return remapIndex(Idx, IndexMap);
}

// Local hashing entry points
Error TypeStreamMerger::mergeTypeRecords(MergingTypeTableBuilder &Dest,
const CVTypeArray &Types) {
Expand Down Expand Up @@ -355,28 +342,25 @@ Error TypeStreamMerger::remapAllTypes(const CVTypeArray &Types) {
}

Error TypeStreamMerger::remapType(const CVType &Type) {
auto DoSerialize = [this, Type]() -> ArrayRef<uint8_t> {
RemappedType R(Type);
SmallVector<TiReference, 32> Refs;
discoverTypeIndices(Type.RecordData, Refs);
if (!remapIndices(R, Refs))
return {};
return serializeRemapped(R);
auto DoSerialize =
[this, Type](MutableArrayRef<uint8_t> Storage) -> ArrayRef<uint8_t> {
return remapIndices(Type, Storage);
};

TypeIndex DestIdx = Untranslated;
if (UseGlobalHashes) {
if (LLVM_LIKELY(UseGlobalHashes)) {
GlobalTypeTableBuilder &Dest =
isIdRecord(Type.kind()) ? *DestGlobalIdStream : *DestGlobalTypeStream;
GloballyHashedType H = GlobalHashes[CurIndex.toArrayIndex()];
DestIdx = Dest.insertRecordAs(H, DoSerialize);
DestIdx = Dest.insertRecordAs(H, Type.RecordData.size(), DoSerialize);
} else {
MergingTypeTableBuilder &Dest =
isIdRecord(Type.kind()) ? *DestIdStream : *DestTypeStream;

auto Data = DoSerialize();
if (!Data.empty())
DestIdx = Dest.insertRecordBytes(Data);
RemapStorage.resize(Type.RecordData.size());
ArrayRef<uint8_t> Result = DoSerialize(RemapStorage);
if (!Result.empty())
DestIdx = Dest.insertRecordBytes(Result);
}
addMapping(DestIdx);

Expand All @@ -386,27 +370,32 @@ Error TypeStreamMerger::remapType(const CVType &Type) {
return Error::success();
}

bool TypeStreamMerger::remapIndices(RemappedType &Record,
ArrayRef<TiReference> Refs) {
ArrayRef<uint8_t> OriginalData = Record.OriginalRecord.content();
bool Success = true;
ArrayRef<uint8_t>
TypeStreamMerger::remapIndices(const CVType &OriginalType,
MutableArrayRef<uint8_t> Storage) {
SmallVector<TiReference, 4> Refs;
discoverTypeIndices(OriginalType.RecordData, Refs);
if (Refs.empty())
return OriginalType.RecordData;

::memcpy(Storage.data(), OriginalType.RecordData.data(),
OriginalType.RecordData.size());

uint8_t *DestContent = Storage.data() + sizeof(RecordPrefix);

for (auto &Ref : Refs) {
uint32_t Offset = Ref.Offset;
ArrayRef<uint8_t> Bytes = OriginalData.slice(Ref.Offset, sizeof(TypeIndex));
ArrayRef<TypeIndex> TIs(reinterpret_cast<const TypeIndex *>(Bytes.data()),
Ref.Count);
for (auto TI : TIs) {
TypeIndex NewTI = TI;
bool ThisSuccess = (Ref.Kind == TiRefKind::IndexRef)
? remapItemIndex(NewTI)
: remapTypeIndex(NewTI);
if (ThisSuccess && NewTI != TI)
Record.Mappings.emplace_back(Offset, NewTI);
Offset += sizeof(TypeIndex);
Success &= ThisSuccess;
TypeIndex *DestTIs =
reinterpret_cast<TypeIndex *>(DestContent + Ref.Offset);

for (size_t I = 0; I < Ref.Count; ++I) {
TypeIndex &TI = DestTIs[I];
bool Success = (Ref.Kind == TiRefKind::IndexRef) ? remapItemIndex(TI)
: remapTypeIndex(TI);
if (LLVM_UNLIKELY(!Success))
return {};
}
}
return Success;
return Storage;
}

Error llvm::codeview::mergeTypeRecords(MergingTypeTableBuilder &Dest,
Expand Down

0 comments on commit 07d8037

Please sign in to comment.