From 105adf2cd9588b4839fbdc7287bb76a962fdb8ca Mon Sep 17 00:00:00 2001 From: Micah Weston Date: Tue, 12 Dec 2023 10:23:16 -0500 Subject: [SHT_LLVM_BB_ADDR_MAP] Implements PGOAnalysisMap in Object and ObjectYAML with tests. Reviewed in PR (#71750). A part of [RFC - PGO Accuracy Metrics: Emitting and Evaluating Branch and Block Analysis](https://discourse.llvm.org/t/rfc-pgo-accuracy-metrics-emitting-and-evaluating-branch-and-block-analysis/73902). This PR adds the PGOAnalysisMap data structure and implements encoding and decoding through Object and ObjectYAML along with associated tests. When emitted into the bb-addr-map section, each function is followed by the associated pgo-analysis-map for that function. The emitting of each analysis in the map is controlled by a bit in the bb-addr-map feature byte. All existing bb-addr-map code can ignore the pgo-analysis-map if the caller does not request the data. --- llvm/lib/Object/ELFObjectFile.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'llvm/lib/Object/ELFObjectFile.cpp') diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp index 25dbcbd..3c86b0f 100644 --- a/llvm/lib/Object/ELFObjectFile.cpp +++ b/llvm/lib/Object/ELFObjectFile.cpp @@ -716,10 +716,13 @@ std::vector ELFObjectFileBase::getPltEntries() const { template Expected> static readBBAddrMapImpl( - const ELFFile &EF, std::optional TextSectionIndex) { + const ELFFile &EF, std::optional TextSectionIndex, + std::vector *PGOAnalyses) { using Elf_Shdr = typename ELFT::Shdr; bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL; std::vector BBAddrMaps; + if (PGOAnalyses) + PGOAnalyses->clear(); const auto &Sections = cantFail(EF.sections()); auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected { @@ -748,10 +751,13 @@ Expected> static readBBAddrMapImpl( return createError("unable to get relocation section for " + describe(EF, *Sec)); Expected> BBAddrMapOrErr = - EF.decodeBBAddrMap(*Sec, RelocSec); - if (!BBAddrMapOrErr) + EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses); + if (!BBAddrMapOrErr) { + if (PGOAnalyses) + PGOAnalyses->clear(); return createError("unable to read " + describe(EF, *Sec) + ": " + toString(BBAddrMapOrErr.takeError())); + } std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(), std::back_inserter(BBAddrMaps)); } @@ -828,13 +834,14 @@ ELFObjectFileBase::readDynsymVersions() const { } Expected> ELFObjectFileBase::readBBAddrMap( - std::optional TextSectionIndex) const { + std::optional TextSectionIndex, + std::vector *PGOAnalyses) const { if (const auto *Obj = dyn_cast(this)) - return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex); + return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses); if (const auto *Obj = dyn_cast(this)) - return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex); + return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses); if (const auto *Obj = dyn_cast(this)) - return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex); + return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses); return readBBAddrMapImpl(cast(this)->getELFFile(), - TextSectionIndex); + TextSectionIndex, PGOAnalyses); } -- cgit v1.1