diff --git a/bolt/src/BinaryFunction.cpp b/bolt/src/BinaryFunction.cpp index a8fb19598624..a6a9c7efa8d9 100644 --- a/bolt/src/BinaryFunction.cpp +++ b/bolt/src/BinaryFunction.cpp @@ -3203,7 +3203,7 @@ MCSymbol *BinaryFunction::addEntryPoint(const BinaryBasicBlock &BB) { return EntrySymbol; } -const MCSymbol *BinaryFunction::getSymbolForEntryID(uint64_t EntryID) const { +MCSymbol *BinaryFunction::getSymbolForEntryID(uint64_t EntryID) { if (EntryID == 0) return getSymbol(); diff --git a/bolt/src/BinaryFunction.h b/bolt/src/BinaryFunction.h index af3e028fc633..80beb1a2b778 100644 --- a/bolt/src/BinaryFunction.h +++ b/bolt/src/BinaryFunction.h @@ -72,20 +72,17 @@ enum IndirectCallPromotionType : char { /// Information on a single indirect call to a particular callee. struct IndirectCallProfile { - bool IsFunction; + MCSymbol *Symbol; uint32_t Offset; - StringRef Name; uint64_t Count; uint64_t Mispreds; - IndirectCallProfile(bool IsFunction, StringRef Name, uint64_t Count, + IndirectCallProfile(MCSymbol *Symbol, uint64_t Count, uint64_t Mispreds, uint32_t Offset = 0) - : IsFunction(IsFunction), Offset(Offset), Name(Name), Count(Count), - Mispreds(Mispreds) {} + : Symbol(Symbol), Offset(Offset), Count(Count), Mispreds(Mispreds) {} bool operator==(const IndirectCallProfile &Other) const { - return IsFunction == Other.IsFunction && - Name == Other.Name && + return Symbol == Other.Symbol && Offset == Other.Offset; } }; @@ -102,8 +99,8 @@ inline raw_ostream &operator<<(raw_ostream &OS, uint64_t TotalCount = 0; uint64_t TotalMispreds = 0; for (auto &CSP : ICSP) { - SS << Sep << "{ " << (CSP.IsFunction ? CSP.Name : "") << ": " - << CSP.Count << " (" << CSP.Mispreds << " misses) }"; + SS << Sep << "{ " << (CSP.Symbol ? CSP.Symbol->getName() : "") + << ": " << CSP.Count << " (" << CSP.Mispreds << " misses) }"; Sep = ",\n "; TotalCount += CSP.Count; TotalMispreds += CSP.Mispreds; @@ -1127,7 +1124,10 @@ public: /// Return MC symbol corresponding to an enumerated entry for multiple-entry /// functions. - const MCSymbol *getSymbolForEntryID(uint64_t EntryNum) const; + MCSymbol *getSymbolForEntryID(uint64_t EntryNum); + const MCSymbol *getSymbolForEntryID(uint64_t EntryNum) const { + return const_cast(this)->getSymbolForEntryID(EntryNum); + } MCSymbol *getColdSymbol() { if (ColdSymbol) diff --git a/bolt/src/BinaryFunctionProfile.cpp b/bolt/src/BinaryFunctionProfile.cpp index fbc656fb0165..db86f789e3e5 100644 --- a/bolt/src/BinaryFunctionProfile.cpp +++ b/bolt/src/BinaryFunctionProfile.cpp @@ -540,8 +540,13 @@ void BinaryFunction::convertBranchData() { IndirectCallSiteProfile &CSP = BC.MIB->getOrCreateAnnotationAs( *Instr, "CallProfile"); - CSP.emplace_back(BI.To.IsSymbol, BI.To.Name, BI.Branches, - BI.Mispreds); + MCSymbol *CalleeSymbol{nullptr}; + if (BI.To.IsSymbol) { + if (auto *BD = BC.getBinaryDataByName(BI.To.Name)) { + CalleeSymbol = BD->getSymbol(); + } + } + CSP.emplace_back(CalleeSymbol, BI.Branches, BI.Mispreds); } else if (BC.MIB->getConditionalTailCall(*Instr)) { setOrUpdateAnnotation("CTCTakenCount", BI.Branches); setOrUpdateAnnotation("CTCMispredCount", BI.Mispreds); diff --git a/bolt/src/Passes/BinaryFunctionCallGraph.cpp b/bolt/src/Passes/BinaryFunctionCallGraph.cpp index c5db238fc465..7a551580df12 100644 --- a/bolt/src/Passes/BinaryFunctionCallGraph.cpp +++ b/bolt/src/Passes/BinaryFunctionCallGraph.cpp @@ -178,11 +178,8 @@ BinaryFunctionCallGraph buildCallGraph(BinaryContext &BC, const auto &ICSP = BC.MIB->getAnnotationAs(Inst, "CallProfile"); for (const auto &CSI : ICSP) { - if (!CSI.IsFunction) - continue; - if (auto *DstBD = BC.getBinaryDataByName(CSI.Name)) { - Counts.push_back(std::make_pair(DstBD->getSymbol(), CSI.Count)); - } + if (CSI.Symbol) + Counts.push_back(std::make_pair(CSI.Symbol, CSI.Count)); } } else { const auto Count = BB->getExecutionCount(); @@ -204,11 +201,7 @@ BinaryFunctionCallGraph buildCallGraph(BinaryContext &BC, for (const auto &CSI : Function->getAllCallSites()) { ++TotalCallsites; - if (!CSI.IsFunction) - continue; - - auto *DstBD = BC.getBinaryDataByName(CSI.Name); - if (!DstBD) + if (!CSI.Symbol) continue; // The computed offset may exceed the hot part of the function; hence, @@ -217,7 +210,7 @@ BinaryFunctionCallGraph buildCallGraph(BinaryContext &BC, if (Offset > Size) Offset = Size; - if (!recordCall(DstBD->getSymbol(), CSI.Count)) { + if (!recordCall(CSI.Symbol, CSI.Count)) { ++NotProcessed; } } diff --git a/bolt/src/Passes/IndirectCallPromotion.cpp b/bolt/src/Passes/IndirectCallPromotion.cpp index e6267254b66f..ac984a2e1347 100644 --- a/bolt/src/Passes/IndirectCallPromotion.cpp +++ b/bolt/src/Passes/IndirectCallPromotion.cpp @@ -204,11 +204,9 @@ IndirectCallPromotion::Callsite::Callsite(BinaryFunction &BF, To(ICP.Offset), Mispreds(ICP.Mispreds), Branches(ICP.Count) { - if (ICP.IsFunction) { - if (auto *BD = BF.getBinaryContext().getBinaryDataByName(ICP.Name)) { - To.Sym = BD->getSymbol(); - To.Addr = 0; - } + if (ICP.Symbol) { + To.Sym = ICP.Symbol; + To.Addr = 0; } } @@ -1301,8 +1299,8 @@ void IndirectCallPromotion::runOnFunctions(BinaryContext &BC) { for (const auto &BInfo : getCallTargets(BB, Inst)) { NumCalls += BInfo.Branches; } - - IndirectCalls.push_back(std::make_tuple(NumCalls, &Inst, &Function)); + IndirectCalls.push_back( + std::make_tuple(NumCalls, &Inst, &Function)); TotalIndirectCalls += NumCalls; } } diff --git a/bolt/src/ProfileReader.cpp b/bolt/src/ProfileReader.cpp index ad4b143ba197..e4cbd17bc4a0 100644 --- a/bolt/src/ProfileReader.cpp +++ b/bolt/src/ProfileReader.cpp @@ -122,13 +122,12 @@ ProfileReader::parseFunctionProfile(BinaryFunction &BF, auto *Callee = YamlCSI.DestId < YamlProfileToFunction.size() ? YamlProfileToFunction[YamlCSI.DestId] : nullptr; bool IsFunction = Callee ? true : false; - const MCSymbol *CalleeSymbol = nullptr; + MCSymbol *CalleeSymbol = nullptr; if (IsFunction) { CalleeSymbol = Callee->getSymbolForEntryID(YamlCSI.EntryDiscriminator); } - StringRef Name = CalleeSymbol ? CalleeSymbol->getName() : ""; BF.getAllCallSites().emplace_back( - IsFunction, Name, YamlCSI.Count, YamlCSI.Mispreds, YamlCSI.Offset); + CalleeSymbol, YamlCSI.Count, YamlCSI.Mispreds, YamlCSI.Offset); if (YamlCSI.Offset >= BB.getOriginalSize()) { if (opts::Verbosity >= 2) @@ -170,7 +169,7 @@ ProfileReader::parseFunctionProfile(BinaryFunction &BF, IndirectCallSiteProfile &CSP = BC.MIB->getOrCreateAnnotationAs( *Instr, "CallProfile"); - CSP.emplace_back(IsFunction, Name, YamlCSI.Count, YamlCSI.Mispreds); + CSP.emplace_back(CalleeSymbol, YamlCSI.Count, YamlCSI.Mispreds); } else if (BC.MIB->getConditionalTailCall(*Instr)) { setAnnotation("CTCTakenCount", YamlCSI.Count); setAnnotation("CTCMispredCount", YamlCSI.Mispreds); diff --git a/bolt/src/ProfileWriter.cpp b/bolt/src/ProfileWriter.cpp index 29d9ea25aa25..19c95c77e675 100644 --- a/bolt/src/ProfileWriter.cpp +++ b/bolt/src/ProfileWriter.cpp @@ -81,14 +81,10 @@ convert(const BinaryFunction &BF, yaml::bolt::BinaryFunctionProfile &YamlBF) { for (auto &CSP : ICSP.get()) { CSI.DestId = 0; // designated for unknown functions CSI.EntryDiscriminator = 0; - if (CSP.IsFunction) { - const auto *CalleeBD = BC.getBinaryDataByName(CSP.Name); - if (CalleeBD) { - const auto *Callee = - BC.getFunctionForSymbol(CalleeBD->getSymbol()); - if (Callee) { - CSI.DestId = Callee->getFunctionNumber(); - } + if (CSP.Symbol) { + const auto *Callee = BC.getFunctionForSymbol(CSP.Symbol); + if (Callee) { + CSI.DestId = Callee->getFunctionNumber(); } } CSI.Count = CSP.Count;