Index: llvm/include/llvm/CodeGen/AccelTable.h =================================================================== --- llvm/include/llvm/CodeGen/AccelTable.h +++ llvm/include/llvm/CodeGen/AccelTable.h @@ -122,8 +122,8 @@ return order() < Other.order(); } - // Subclasses should implement: - // static uint32_t hash(StringRef Name); + // Subclasses should implement: + // static uint32_t hash(StringRef Name); #ifndef NDEBUG virtual void print(raw_ostream &OS) const = 0; @@ -287,6 +287,23 @@ const DwarfDebug &DD, ArrayRef> CUs); +class AccelTableEmitterInfo { +public: + virtual ~AccelTableEmitterInfo() = default; + virtual MCSymbol* getLabelForCU(unsigned Idx) const = 0; + virtual unsigned getNumberOfCUs() const = 0; + virtual unsigned getUnqiueIDForUnitDie(const DIE* UnitDie) const = 0; +}; + +void emitDWARF5AccelTable(AsmPrinter *Asm, + AccelTable &Contents, + const DwarfDebug &DD, + ArrayRef> CUs); + +void emitDWARF5AccelTable( + AsmPrinter *Asm, AccelTable &Contents, + std::unique_ptr EmitterInfo); + /// Accelerator table data implementation for simple Apple accelerator tables /// with just a DIE reference. class AppleAccelTableOffsetData : public AppleAccelTableData { Index: llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp +++ llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp @@ -209,8 +209,7 @@ Header Header; DenseMap> Abbreviations; - const DwarfDebug ⅅ - ArrayRef> CompUnits; + std::unique_ptr EmitterInfo; MCSymbol *ContributionStart = Asm->createTempSymbol("names_start"); MCSymbol *ContributionEnd = Asm->createTempSymbol("names_end"); MCSymbol *AbbrevStart = Asm->createTempSymbol("names_abbrev_start"); @@ -230,14 +229,47 @@ void emitData() const; public: - Dwarf5AccelTableEmitter( - AsmPrinter *Asm, const AccelTableBase &Contents, const DwarfDebug &DD, - ArrayRef> CompUnits); + Dwarf5AccelTableEmitter(AsmPrinter *Asm, const AccelTableBase &Contents, + std::unique_ptr EmitterInfo); void emit() const; }; + +class DefaultAccelTableEmitterInfo final : public AccelTableEmitterInfo { +private: + const DwarfDebug ⅅ + ArrayRef> CompUnits; + +public: + DefaultAccelTableEmitterInfo(const DwarfDebug &DD, ArrayRef> CompUnits); + MCSymbol *getLabelForCU(unsigned Idx) const override; + unsigned getNumberOfCUs() const override; + unsigned getUnqiueIDForUnitDie(const DIE *UnitDie) const override; +}; } // namespace +DefaultAccelTableEmitterInfo::DefaultAccelTableEmitterInfo( + const DwarfDebug &DD, ArrayRef> CompUnits) + : DD(DD), CompUnits(CompUnits) {} + +MCSymbol *DefaultAccelTableEmitterInfo::getLabelForCU(unsigned Idx) const { + assert(Idx < CompUnits.size()); + const auto &CU = CompUnits[Idx]; + assert(Idx == CU->getUniqueID()); + const DwarfCompileUnit *MainCU = + DD.useSplitDwarf() ? CU->getSkeleton() : CU.get(); + return MainCU->getLabelBegin(); +} + +unsigned DefaultAccelTableEmitterInfo::getNumberOfCUs() const { + return CompUnits.size(); +} + +unsigned +DefaultAccelTableEmitterInfo::getUnqiueIDForUnitDie(const DIE *UnitDie) const { + return DD.lookupCU(UnitDie)->getUniqueID(); +} + void AccelTableEmitter::emitHashes() const { uint64_t PrevHash = std::numeric_limits::max(); unsigned BucketIdx = 0; @@ -403,8 +435,8 @@ SmallVector Dwarf5AccelTableEmitter::getUniformAttributes() const { SmallVector UA; - if (CompUnits.size() > 1) { - size_t LargestCUIndex = CompUnits.size() - 1; + if (EmitterInfo->getNumberOfCUs() > 1) { + size_t LargestCUIndex = EmitterInfo->getNumberOfCUs() - 1; dwarf::Form Form = DIEInteger::BestForm(/*IsSigned*/ false, LargestCUIndex); UA.push_back({dwarf::DW_IDX_compile_unit, Form}); } @@ -413,6 +445,12 @@ } void Dwarf5AccelTableEmitter::emitCUList() const { + for (unsigned Idx = 0, CUs = EmitterInfo->getNumberOfCUs(); Idx < CUs; + ++Idx) { + Asm->OutStreamer->AddComment("Compilation unit " + Twine(Idx)); + Asm->emitDwarfSymbolReference(EmitterInfo->getLabelForCU(Idx)); + } +#if 0 for (const auto &CU : enumerate(CompUnits)) { assert(CU.index() == CU.value()->getUniqueID()); Asm->OutStreamer->AddComment("Compilation unit " + Twine(CU.index())); @@ -420,6 +458,7 @@ DD.useSplitDwarf() ? CU.value()->getSkeleton() : CU.value().get(); Asm->emitDwarfSymbolReference(MainCU->getLabelBegin()); } +#endif } void Dwarf5AccelTableEmitter::emitBuckets() const { @@ -474,7 +513,7 @@ switch (AttrEnc.Index) { case dwarf::DW_IDX_compile_unit: { const DIE *CUDie = Entry.getDie().getUnitDie(); - DIEInteger ID(DD.lookupCU(CUDie)->getUniqueID()); + DIEInteger ID(EmitterInfo->getUnqiueIDForUnitDie(CUDie)); ID.EmitValue(Asm, AttrEnc.Form); break; } @@ -503,12 +542,12 @@ } Dwarf5AccelTableEmitter::Dwarf5AccelTableEmitter( - AsmPrinter *Asm, const AccelTableBase &Contents, const DwarfDebug &DD, - ArrayRef> CompUnits) + AsmPrinter *Asm, const AccelTableBase &Contents, + std::unique_ptr EmitterInfo) : AccelTableEmitter(Asm, Contents, false), - Header(CompUnits.size(), Contents.getBucketCount(), + Header(EmitterInfo->getNumberOfCUs(), Contents.getBucketCount(), Contents.getUniqueNameCount()), - DD(DD), CompUnits(CompUnits) { + EmitterInfo(std::move(EmitterInfo)) { DenseSet UniqueTags = getUniqueTags(); SmallVector UniformAttributes = getUniformAttributes(); @@ -540,8 +579,15 @@ void llvm::emitDWARF5AccelTable( AsmPrinter *Asm, AccelTable &Contents, const DwarfDebug &DD, ArrayRef> CUs) { + auto EmitterInfo = llvm::make_unique(DD, CUs); + emitDWARF5AccelTable(Asm, Contents, std::move(EmitterInfo)); +} + +void llvm::emitDWARF5AccelTable( + AsmPrinter *Asm, AccelTable &Contents, + std::unique_ptr EmitterInfo) { Contents.finalize(Asm, "names"); - Dwarf5AccelTableEmitter(Asm, Contents, DD, CUs).emit(); + Dwarf5AccelTableEmitter(Asm, Contents, std::move(EmitterInfo)).emit(); } void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const {