diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFSection.h @@ -15,6 +15,7 @@ struct DWARFSection { StringRef Data; + int Index; }; struct SectionName { diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h @@ -121,6 +121,7 @@ const DWARFUnitIndex::Entry *)> Parser; int NumInfoUnits = -1; + int NumCompileUnits = -1; public: using UnitVector = SmallVectorImpl>; @@ -154,11 +155,14 @@ unsigned getNumInfoUnits() const { return NumInfoUnits == -1 ? size() : NumInfoUnits; } + unsigned getNumCompileUnits() const { + return NumCompileUnits == -1 ? size() : NumCompileUnits; + } /// Returns number of units from all .debug_types[.dwo] sections. - unsigned getNumTypesUnits() const { return size() - NumInfoUnits; } + unsigned getNumTypesUnits() const { return size() - NumCompileUnits; } /// Indicate that parsing .debug_info[.dwo] is done, and remaining units /// will be from .debug_types[.dwo]. - void finishedInfoUnits() { NumInfoUnits = size(); } + void finishedInfoUnits(); private: void addUnitsImpl(DWARFContext &Context, const DWARFObject &Obj, diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -379,8 +379,17 @@ DObj->getAbbrevDWOSection())) getDebugAbbrevDWO()->dump(OS); - auto dumpDebugInfo = [&](const char *Name, unit_iterator_range Units) { + auto dumpDebugInfo = [&](const char *Name, + unit_iterator_range UnsortedUnits) { OS << '\n' << Name << " contents:\n"; + SmallVector Units; + Units.reserve(llvm::size(UnsortedUnits)); + for (const auto &U : UnsortedUnits) + Units.push_back(U.get()); + llvm::stable_sort(Units, [](const DWARFUnit *LHS, const DWARFUnit *RHS) { + return std::make_tuple(LHS->getInfoSection().Index, LHS->getOffset()) < + std::make_tuple(RHS->getInfoSection().Index, RHS->getOffset()); + }); if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugInfo]) for (const auto &U : Units) U->getDIEForOffset(DumpOffset.getValue()) @@ -1720,6 +1729,7 @@ // there are multiple, comdat grouped, of these sections. DWARFSectionMap &S = (*Sections)[Section]; S.Data = Data; + S.Index = Section.getIndex(); } if (RelocatedSection == Obj.section_end()) diff --git a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp @@ -170,6 +170,23 @@ return NewCU; } +void DWARFUnitVector::finishedInfoUnits() { + NumInfoUnits = size(); + llvm::stable_sort(*this, [](const std::unique_ptr &LHS, + const std::unique_ptr &RHS) { + bool LTU = LHS->isTypeUnit(); + bool RTU = RHS->isTypeUnit(); + if (LTU == RTU) + return false; + if (!LTU && RTU) + return true; + return false; + }); + auto I = llvm::find_if(*this, [](const std::unique_ptr &U) { + return U->isTypeUnit(); + }); + NumCompileUnits = I - begin(); +} DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section, const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA, const DWARFSection *RS, const DWARFSection *LocSection,