From 56b45b104a2ab2dbc4ab8e9643c90092894b579e Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Wed, 24 Jul 2024 11:29:22 -0700 Subject: [PATCH 1/8] Comment Created using spr 1.3.4 --- bolt/include/bolt/Profile/YAMLProfileReader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bolt/include/bolt/Profile/YAMLProfileReader.h b/bolt/include/bolt/Profile/YAMLProfileReader.h index 6c00f82302fb9..bc09751fcae75 100644 --- a/bolt/include/bolt/Profile/YAMLProfileReader.h +++ b/bolt/include/bolt/Profile/YAMLProfileReader.h @@ -108,7 +108,7 @@ class YAMLProfileReader : public ProfileReaderBase { std::vector YamlProfileToFunction; using FunctionSet = std::unordered_set; - /// To keep track of functions that have a matched profile before the profilez + /// To keep track of functions that have a matched profile before the profile /// is attributed. FunctionSet ProfiledFunctions; From b851ca65c2bf2a9569315d62722b60a04c8102ee Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Wed, 24 Jul 2024 11:39:48 -0700 Subject: [PATCH 2/8] Was accessing wrong YamlBF Hash, fixed Created using spr 1.3.4 --- bolt/lib/Profile/YAMLProfileReader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp index 68af95a1cd043..f5ac0b8e2c56a 100644 --- a/bolt/lib/Profile/YAMLProfileReader.cpp +++ b/bolt/lib/Profile/YAMLProfileReader.cpp @@ -614,7 +614,7 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) { uint64_t MatchedWithPseudoProbes = 0; for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) { - auto It = PseudoProbeDescHashToBF.find(YamlBF.Hash); + auto It = PseudoProbeDescHashToBF.find(YamlBF.PseudoProbeDescHash); if (It == PseudoProbeDescHashToBF.end()) continue; BinaryFunction *BF = It->second; From 39ba7175c9224c3584db7f5f8ca8fbed14da41e5 Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Wed, 24 Jul 2024 11:49:54 -0700 Subject: [PATCH 3/8] Changed ordering of matching Created using spr 1.3.4 --- bolt/lib/Profile/YAMLProfileReader.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp index f5ac0b8e2c56a..75ec4465856a1 100644 --- a/bolt/lib/Profile/YAMLProfileReader.cpp +++ b/bolt/lib/Profile/YAMLProfileReader.cpp @@ -770,8 +770,8 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) { const size_t MatchedWithHash = matchWithHash(BC); const size_t MatchedWithLTOCommonName = matchWithLTOCommonName(); const size_t MatchedWithCallGraph = matchWithCallGraph(BC); - const size_t MatchedWithNameSimilarity = matchWithNameSimilarity(BC); const size_t MatchedWithPseudoProbes = matchWithPseudoProbes(BC); + const size_t MatchedWithNameSimilarity = matchWithNameSimilarity(BC); for (auto [YamlBF, BF] : llvm::zip_equal(YamlBP.Functions, ProfileBFs)) if (!YamlBF.Used && BF && !ProfiledFunctions.count(BF)) @@ -792,10 +792,10 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) { << " functions with matching LTO common names\n"; outs() << "BOLT-INFO: matched " << MatchedWithCallGraph << " functions with call graph\n"; - outs() << "BOLT-INFO: matched " << MatchedWithNameSimilarity - << " functions with similar names\n"; outs() << "BOLT-INFO: matched " << MatchedWithPseudoProbes << " functions with pseudo probes\n"; + outs() << "BOLT-INFO: matched " << MatchedWithNameSimilarity + << " functions with similar names\n"; } // Set for parseFunctionProfile(). From 11af7f19953da7c5ad4eb263be3d38a70b2518e0 Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Wed, 24 Jul 2024 12:56:56 -0700 Subject: [PATCH 4/8] Added check for YamlBF.Used in pseudo probe function matching Created using spr 1.3.4 --- bolt/lib/Profile/YAMLProfileReader.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp index 75ec4465856a1..8dfdf1fb30eb3 100644 --- a/bolt/lib/Profile/YAMLProfileReader.cpp +++ b/bolt/lib/Profile/YAMLProfileReader.cpp @@ -614,6 +614,8 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) { uint64_t MatchedWithPseudoProbes = 0; for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) { + if (YamlBF.Used) + continue; auto It = PseudoProbeDescHashToBF.find(YamlBF.PseudoProbeDescHash); if (It == PseudoProbeDescHashToBF.end()) continue; From ad4d98fc4bf3f16d119ddbc5abad11b93641bf99 Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Wed, 11 Sep 2024 15:49:03 -0700 Subject: [PATCH 5/8] Debug logging Created using spr 1.3.4 --- bolt/lib/Profile/YAMLProfileReader.cpp | 31 +++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp index 9aa5eb8391ab4..ecb786f367951 100644 --- a/bolt/lib/Profile/YAMLProfileReader.cpp +++ b/bolt/lib/Profile/YAMLProfileReader.cpp @@ -20,6 +20,9 @@ #include "llvm/Support/CommandLine.h" #include +#undef DEBUG_TYPE +#define DEBUG_TYPE "bolt-prof" + using namespace llvm; namespace opts { @@ -666,24 +669,35 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) { // Get unused profile functions GUID. for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) { - if (YamlBF.Used || YamlBF.InlineTree.empty()) + if (YamlBF.Used) + continue; + LLVM_DEBUG(dbgs() << "Attempting to match " << YamlBF.Name + << " using pseudo probes: "); + if (YamlBF.InlineTree.empty()) { + LLVM_DEBUG(dbgs() << "no probe information\n"); continue; + } uint64_t GUIDIdx = YamlBF.InlineTree.front().GUIDIndex; assert(GUIDIdx - 1 < YamlPD.GUID.size()); uint64_t YamlGUID = YamlPD.GUID[GUIDIdx - 1]; // Look up corresponding GUID in binary. auto It = GUID2FuncDescMap.find(YamlGUID); - if (It == GUID2FuncDescMap.end()) + if (It == GUID2FuncDescMap.end()) { + LLVM_DEBUG(dbgs() << "no function with GUID=" << YamlGUID + << " in the binary\n"); continue; + } // Check if checksums match between profile and binary. assert(GUIDIdx - 1 < YamlPD.GUIDHash.size()); uint64_t HashIdx = YamlPD.GUIDHash[GUIDIdx - 1]; assert(HashIdx < YamlPD.Hash.size()); uint64_t YamlHash = YamlPD.Hash[HashIdx]; - if (YamlHash != It->FuncHash) + if (YamlHash != It->FuncHash) { + LLVM_DEBUG(dbgs() << "hash mismatch\n"); continue; + } // Look for binary inline trees with the best match to YAML inline tree. auto Range = llvm::make_range(std::equal_range( @@ -707,6 +721,8 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) { BestNodes.push_back(Node); } + LLVM_DEBUG(dbgs() << BestNodes.size() << " candidates: "); + // Find binary functions containing best matching binary inline tree nodes. DenseMap BFProbeCountMap; for (const MCDecodedPseudoProbeInlineTree *Node : BestNodes) { @@ -714,10 +730,6 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) { const MCDecodedPseudoProbeInlineTree *Root = Node; while (!Root->Parent->isRoot()) Root = (const MCDecodedPseudoProbeInlineTree *)Root->Parent; - // Get containing non-profiled binary function. - auto GUIDToBFIt = GUIDToBF.find(Root->Guid); - if (GUIDToBFIt == GUIDToBF.end()) - continue; // Compute the number of probes belonging to functions. for (const MCDecodedPseudoProbe &Probe : Node->getProbes()) { uint64_t Address = Probe.getAddress(); @@ -732,10 +744,13 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) { llvm::sort(BFProbeCountVec); for (BinaryFunction *BF : llvm::make_second_range(BFProbeCountVec)) { // Check if BF already has profile attached, bail for now. - if (ProfiledFunctions.count(BF)) + if (ProfiledFunctions.count(BF)) { + LLVM_DEBUG(dbgs() << *BF << " is already profiled, "); continue; + } matchProfileToFunction(YamlBF, *BF); ++MatchedWithPseudoProbes; + LLVM_DEBUG(dbgs() << "matched to " << *BF << '\n'); break; } } From 15643b7da8102e313a28f0f14dd4e607f7a81323 Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Fri, 8 Nov 2024 13:15:53 -0800 Subject: [PATCH 6/8] clang-format Created using spr 1.3.4 --- bolt/lib/Profile/YAMLProfileReader.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp index 07bc890926652..2c0c3cc1376b7 100644 --- a/bolt/lib/Profile/YAMLProfileReader.cpp +++ b/bolt/lib/Profile/YAMLProfileReader.cpp @@ -51,9 +51,10 @@ llvm::cl::opt cl::desc("Match functions with call graph"), cl::Hidden, cl::cat(BoltOptCategory)); -llvm::cl::opt MatchWithPseudoProbes( - "match-with-pseudo-probes", cl::desc("Match functions with pseudo probes"), - cl::Hidden, cl::cat(BoltOptCategory)); +llvm::cl::opt + MatchWithPseudoProbes("match-with-pseudo-probes", + cl::desc("Match functions with pseudo probes"), + cl::Hidden, cl::cat(BoltOptCategory)); llvm::cl::opt ProfileUseDFS("profile-use-dfs", cl::desc("use DFS order for YAML profile"), @@ -809,12 +810,11 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) { const auto ProbeIt = Node->getProbes().begin(); const auto *Probe = (ProbeIt != Node->getProbes().end()) ? &*ProbeIt : nullptr; - LLVM_DEBUG(dbgs() - << MatchedNodes << "/" << YamlBF.InlineTree.size() - << " match with " << *BF << " at " - << (Probe ? Probe->getInlineContextStr(GUID2FuncDescMap) - : "(none)") - << '\n'); + LLVM_DEBUG(dbgs() << MatchedNodes << "/" << YamlBF.InlineTree.size() + << " match with " << *BF << " at " + << (Probe ? Probe->getInlineContextStr(GUID2FuncDescMap) + : "(none)") + << '\n'); } MatchedWithPseudoProbes += !!Matched; YamlBF.Used |= !!Matched; From 57f46da05bfb216584ada918e8d197a0a5cd3ad2 Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Tue, 28 Oct 2025 22:14:52 -0700 Subject: [PATCH 7/8] clang-format Created using spr 1.3.4 --- bolt/lib/Profile/StaleProfileMatching.cpp | 13 +++++++------ bolt/lib/Profile/YAMLProfileReader.cpp | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bolt/lib/Profile/StaleProfileMatching.cpp b/bolt/lib/Profile/StaleProfileMatching.cpp index b24d5228dcd23..344c8f366ad3a 100644 --- a/bolt/lib/Profile/StaleProfileMatching.cpp +++ b/bolt/lib/Profile/StaleProfileMatching.cpp @@ -473,12 +473,13 @@ createFlowFunction(const BinaryFunction::BasicBlockOrderType &BlockOrder) { /// of the basic blocks in the binary, the count is "matched" to the block. /// Similarly, if both the source and the target of a count in the profile are /// matched to a jump in the binary, the count is recorded in CFG. -size_t matchWeights( - BinaryContext &BC, const BinaryFunction::BasicBlockOrderType &BlockOrder, - const yaml::bolt::BinaryFunctionProfile &YamlBF, FlowFunction &Func, - HashFunction HashFunction, YAMLProfileReader::ProfileLookupMap &IdToYamlBF, - const BinaryFunction &BF, - const YAMLProfileReader::ProbeMatchSpec &ProbeMatchSpecs); +size_t matchWeights(BinaryContext &BC, + const BinaryFunction::BasicBlockOrderType &BlockOrder, + const yaml::bolt::BinaryFunctionProfile &YamlBF, + FlowFunction &Func, HashFunction HashFunction, + YAMLProfileReader::ProfileLookupMap &IdToYamlBF, + const BinaryFunction &BF, + const YAMLProfileReader::ProbeMatchSpec &ProbeMatchSpecs); std::pair> initMatcher(BinaryContext &BC, diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp index 4a32d46708a51..78cea1f71a66d 100644 --- a/bolt/lib/Profile/YAMLProfileReader.cpp +++ b/bolt/lib/Profile/YAMLProfileReader.cpp @@ -369,7 +369,7 @@ bool YAMLProfileReader::parseFunctionProfile( // Probe inline tree: move InlineTreeIndex into InlineTreeNodes. static void decodeYamlInlineTree(const yaml::bolt::ProfilePseudoProbeDesc &YamlPD, - yaml::bolt::BinaryFunctionProfile &YamlBF) { + yaml::bolt::BinaryFunctionProfile &YamlBF) { uint32_t ParentId = 0; uint32_t PrevGUIDIdx = 0; for (yaml::bolt::InlineTreeNode &InlineTreeNode : YamlBF.InlineTree) { From 5dd720eaa3e1fbced4d6876980bb56a909753a2b Mon Sep 17 00:00:00 2001 From: shawbyoung Date: Tue, 28 Oct 2025 22:48:08 -0700 Subject: [PATCH 8/8] Avoid structured binding Created using spr 1.3.4 --- bolt/lib/Profile/StaleProfileMatching.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bolt/lib/Profile/StaleProfileMatching.cpp b/bolt/lib/Profile/StaleProfileMatching.cpp index 344c8f366ad3a..3d3d9384acc54 100644 --- a/bolt/lib/Profile/StaleProfileMatching.cpp +++ b/bolt/lib/Profile/StaleProfileMatching.cpp @@ -707,7 +707,8 @@ size_t matchWeights(BinaryContext &BC, // Block index => matched block + its block profile ProfileBlockMatchMap MatchedBlocks; for (const auto &[RootIdx, NodeMapScale] : StartIdxNodeMap) { - const auto &[NodeMap, Scale] = NodeMapScale; + ArrayRef NodeMap = NodeMapScale.first; + const float Scale = NodeMapScale.second; for (const BinaryBasicBlockProfile &BB : YamlBF->Blocks) { auto matchProbe = [&](uint32_t Node, uint64_t Id) { if (const FlowBlock *Block = getBlock(NodeMap[Node], Id)) {