From 7af4bb16417deeb1d01e7dbbbb2272f1f46753c6 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Sat, 2 May 2020 16:31:43 -0700 Subject: [PATCH] [PDB] Remove unique_ptr wrapper around C13 line table subsections This accounts for a large portion of the memory allocations in LLD. This DebugSubsectionRecordBuilder object can be stored directly in C13Builders, it mostly wraps other subsections. Remove the container kind field from the object. It is always the same for all elements in the vector, and we can pass it in during writing. --- .../CodeView/DebugSubsectionRecord.h | 23 +++++++------------ .../PDB/Native/DbiModuleDescriptorBuilder.h | 3 +-- .../CodeView/DebugSubsectionRecord.cpp | 22 ++++++++---------- .../PDB/Native/DbiModuleDescriptorBuilder.cpp | 10 ++++---- llvm/lib/ObjectYAML/COFFEmitter.cpp | 4 ++-- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/llvm/include/llvm/DebugInfo/CodeView/DebugSubsectionRecord.h b/llvm/include/llvm/DebugInfo/CodeView/DebugSubsectionRecord.h index bcb379f00d68..e915d8a5830c 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/DebugSubsectionRecord.h +++ b/llvm/include/llvm/DebugInfo/CodeView/DebugSubsectionRecord.h @@ -35,44 +35,38 @@ struct DebugSubsectionHeader { class DebugSubsectionRecord { public: DebugSubsectionRecord(); - DebugSubsectionRecord(DebugSubsectionKind Kind, BinaryStreamRef Data, - CodeViewContainer Container); + DebugSubsectionRecord(DebugSubsectionKind Kind, BinaryStreamRef Data); - static Error initialize(BinaryStreamRef Stream, DebugSubsectionRecord &Info, - CodeViewContainer Container); + static Error initialize(BinaryStreamRef Stream, DebugSubsectionRecord &Info); uint32_t getRecordLength() const; DebugSubsectionKind kind() const; BinaryStreamRef getRecordData() const; private: - CodeViewContainer Container = CodeViewContainer::ObjectFile; DebugSubsectionKind Kind = DebugSubsectionKind::None; BinaryStreamRef Data; }; class DebugSubsectionRecordBuilder { public: - DebugSubsectionRecordBuilder(std::shared_ptr Subsection, - CodeViewContainer Container); + DebugSubsectionRecordBuilder(std::shared_ptr Subsection); /// Use this to copy existing subsections directly from source to destination. /// For example, line table subsections in an object file only need to be /// relocated before being copied into the PDB. - DebugSubsectionRecordBuilder(const DebugSubsectionRecord &Contents, - CodeViewContainer Container); + DebugSubsectionRecordBuilder(const DebugSubsectionRecord &Contents); - uint32_t calculateSerializedLength(); - Error commit(BinaryStreamWriter &Writer) const; + uint32_t calculateSerializedLength() const; + Error commit(BinaryStreamWriter &Writer, CodeViewContainer Container) const; private: /// The subsection to build. Will be null if Contents is non-empty. std::shared_ptr Subsection; /// The bytes of the subsection. Only non-empty if Subsection is null. + /// FIXME: Reduce the size of this. DebugSubsectionRecord Contents; - - CodeViewContainer Container; }; } // end namespace codeview @@ -83,8 +77,7 @@ template <> struct VarStreamArrayExtractor { // FIXME: We need to pass the container type through to this function. In // practice this isn't super important since the subsection header describes // its length and we can just skip it. It's more important when writing. - if (auto EC = codeview::DebugSubsectionRecord::initialize( - Stream, Info, codeview::CodeViewContainer::Pdb)) + if (auto EC = codeview::DebugSubsectionRecord::initialize(Stream, Info)) return EC; Length = alignTo(Info.getRecordLength(), 4); return Error::success(); diff --git a/llvm/include/llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h b/llvm/include/llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h index 4f5d28bbd05a..beaaef0c5a6c 100644 --- a/llvm/include/llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h +++ b/llvm/include/llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h @@ -93,8 +93,7 @@ private: std::vector SourceFiles; std::vector> Symbols; - std::vector> - C13Builders; + std::vector C13Builders; ModuleInfoHeader Layout; }; diff --git a/llvm/lib/DebugInfo/CodeView/DebugSubsectionRecord.cpp b/llvm/lib/DebugInfo/CodeView/DebugSubsectionRecord.cpp index 0f704f286ee9..3c8a30101450 100644 --- a/llvm/lib/DebugInfo/CodeView/DebugSubsectionRecord.cpp +++ b/llvm/lib/DebugInfo/CodeView/DebugSubsectionRecord.cpp @@ -23,13 +23,11 @@ using namespace llvm::codeview; DebugSubsectionRecord::DebugSubsectionRecord() = default; DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind, - BinaryStreamRef Data, - CodeViewContainer Container) - : Container(Container), Kind(Kind), Data(Data) {} + BinaryStreamRef Data) + : Kind(Kind), Data(Data) {} Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream, - DebugSubsectionRecord &Info, - CodeViewContainer Container) { + DebugSubsectionRecord &Info) { const DebugSubsectionHeader *Header; BinaryStreamReader Reader(Stream); if (auto EC = Reader.readObject(Header)) @@ -39,7 +37,6 @@ Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream, static_cast(uint32_t(Header->Kind)); if (auto EC = Reader.readStreamRef(Info.Data, Header->Length)) return EC; - Info.Container = Container; Info.Kind = Kind; return Error::success(); } @@ -53,14 +50,14 @@ DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; } BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; } DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder( - std::shared_ptr Subsection, CodeViewContainer Container) - : Subsection(std::move(Subsection)), Container(Container) {} + std::shared_ptr Subsection) + : Subsection(std::move(Subsection)) {} DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder( - const DebugSubsectionRecord &Contents, CodeViewContainer Container) - : Contents(Contents), Container(Container) {} + const DebugSubsectionRecord &Contents) + : Contents(Contents) {} -uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() { +uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() const { uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize() : Contents.getRecordData().getLength(); // The length of the entire subsection is always padded to 4 bytes, @@ -68,7 +65,8 @@ uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() { return sizeof(DebugSubsectionHeader) + alignTo(DataSize, 4); } -Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const { +Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer, + CodeViewContainer Container) const { assert(Writer.getOffset() % alignOf(Container) == 0 && "Debug Subsection not properly aligned"); diff --git a/llvm/lib/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.cpp b/llvm/lib/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.cpp index 06bbbdefe214..9b243006c5f8 100644 --- a/llvm/lib/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.cpp +++ b/llvm/lib/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.cpp @@ -90,7 +90,7 @@ uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const { uint32_t Result = 0; for (const auto &Builder : C13Builders) { assert(Builder && "Empty C13 Fragment Builder!"); - Result += Builder->calculateSerializedLength(); + Result += Builder.calculateSerializedLength(); } return Result; } @@ -164,7 +164,7 @@ Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter, // TODO: Write C11 Line data for (const auto &Builder : C13Builders) { assert(Builder && "Empty C13 Fragment Builder!"); - if (auto EC = Builder->commit(SymbolWriter)) + if (auto EC = Builder.commit(SymbolWriter, CodeViewContainer::Pdb)) return EC; } @@ -180,12 +180,10 @@ Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter, void DbiModuleDescriptorBuilder::addDebugSubsection( std::shared_ptr Subsection) { assert(Subsection); - C13Builders.push_back(std::make_unique( - std::move(Subsection), CodeViewContainer::Pdb)); + C13Builders.push_back(DebugSubsectionRecordBuilder(std::move(Subsection))); } void DbiModuleDescriptorBuilder::addDebugSubsection( const DebugSubsectionRecord &SubsectionContents) { - C13Builders.push_back(std::make_unique( - SubsectionContents, CodeViewContainer::Pdb)); + C13Builders.push_back(DebugSubsectionRecordBuilder(SubsectionContents)); } diff --git a/llvm/lib/ObjectYAML/COFFEmitter.cpp b/llvm/lib/ObjectYAML/COFFEmitter.cpp index ec3ec55011f9..734e1be4b2d5 100644 --- a/llvm/lib/ObjectYAML/COFFEmitter.cpp +++ b/llvm/lib/ObjectYAML/COFFEmitter.cpp @@ -187,7 +187,7 @@ toDebugS(ArrayRef Subsections, std::vector Builders; uint32_t Size = sizeof(uint32_t); for (auto &SS : CVSS) { - DebugSubsectionRecordBuilder B(SS, CodeViewContainer::ObjectFile); + DebugSubsectionRecordBuilder B(SS); Size += B.calculateSerializedLength(); Builders.push_back(std::move(B)); } @@ -197,7 +197,7 @@ toDebugS(ArrayRef Subsections, Err(Writer.writeInteger(COFF::DEBUG_SECTION_MAGIC)); for (const auto &B : Builders) { - Err(B.commit(Writer)); + Err(B.commit(Writer, CodeViewContainer::ObjectFile)); } return {Output}; }