1414#include < unordered_set>
1515
1616namespace llvm {
17+ class MCDecodedPseudoProbeInlineTree ;
18+
1719namespace bolt {
1820
1921class YAMLProfileReader : public ProfileReaderBase {
@@ -43,6 +45,9 @@ class YAMLProfileReader : public ProfileReaderBase {
4345 using ProfileLookupMap =
4446 DenseMap<uint32_t , yaml::bolt::BinaryFunctionProfile *>;
4547
48+ using GUIDInlineTreeMap =
49+ std::unordered_map<uint64_t , const MCDecodedPseudoProbeInlineTree *>;
50+
4651 // / A class for matching binary functions in functions in the YAML profile.
4752 // / First, a call graph is constructed for both profiled and binary functions.
4853 // / Then functions are hashed based on the names of their callee/caller
@@ -96,6 +101,61 @@ class YAMLProfileReader : public ProfileReaderBase {
96101 YamlBFAdjacencyMap;
97102 };
98103
104+ // A class for matching inline tree nodes between profile and binary.
105+ // Provides the mapping from profile inline tree node id to a
106+ // corresponding binary MCDecodedPseudoProbeInlineTree node.
107+ //
108+ // The whole mapping process is the following:
109+ //
110+ // (profile) (binary)
111+ // | blocks ^
112+ // v |
113+ // yaml::bolt::BinaryBasicBlockProfile ~= FlowBlock
114+ // ||| probes ^ (majority vote)
115+ // v ||| BBPseudoProbeToBlock
116+ // yaml::bolt::PseudoProbeInfo MCDecodedPseudoProbe
117+ // | InlineTreeIndex ^
118+ // v | probe id
119+ // [ profile node id (uint32_t) -> MCDecodedPseudoProbeInlineTree *]
120+ // InlineTreeNodeMapTy
121+ class InlineTreeNodeMapTy {
122+ DenseMap<uint32_t , const MCDecodedPseudoProbeInlineTree *> Map;
123+
124+ void mapInlineTreeNode (uint32_t ProfileNodeIdx,
125+ const MCDecodedPseudoProbeInlineTree *BinaryNode) {
126+ auto Res = Map.try_emplace (ProfileNodeIdx, BinaryNode);
127+ assert (Res.second &&
128+ " Duplicate mapping from profile node index to binary inline tree" );
129+ (void )Res;
130+ }
131+
132+ public:
133+ // / Returns matched InlineTree * for a given profile inline_tree_id.
134+ const MCDecodedPseudoProbeInlineTree *
135+ getInlineTreeNode (uint32_t ProfileInlineTreeNodeId) const {
136+ auto It = Map.find (ProfileInlineTreeNodeId);
137+ if (It == Map.end ())
138+ return nullptr ;
139+ return It->second ;
140+ }
141+
142+ // Match up \p YamlInlineTree with binary inline tree rooted at \p Root.
143+ // Return the number of matched nodes.
144+ //
145+ // This function populates the mapping from profile inline tree node id to a
146+ // corresponding binary MCDecodedPseudoProbeInlineTree node.
147+ size_t matchInlineTrees (
148+ const MCPseudoProbeDecoder &Decoder,
149+ const std::vector<yaml::bolt::InlineTreeNode> &YamlInlineTree,
150+ const MCDecodedPseudoProbeInlineTree *Root);
151+ };
152+
153+ // Partial probe matching specification: matched inline tree and corresponding
154+ // BinaryFunctionProfile
155+ using ProbeMatchSpec =
156+ std::pair<InlineTreeNodeMapTy,
157+ std::reference_wrapper<yaml::bolt::BinaryFunctionProfile>>;
158+
99159private:
100160 // / Adjustments for basic samples profiles (without LBR).
101161 bool NormalizeByInsnCount{false };
@@ -129,6 +189,13 @@ class YAMLProfileReader : public ProfileReaderBase {
129189 // / BinaryFunction pointers indexed by YamlBP functions.
130190 std::vector<BinaryFunction *> ProfileBFs;
131191
192+ // Pseudo probe function GUID to inline tree node
193+ GUIDInlineTreeMap TopLevelGUIDToInlineTree;
194+
195+ // Mapping from a binary function to its partial match specification
196+ // (YAML profile and its inline tree mapping to binary).
197+ DenseMap<BinaryFunction *, std::vector<ProbeMatchSpec>> BFToProbeMatchSpecs;
198+
132199 // / Populate \p Function profile with the one supplied in YAML format.
133200 bool parseFunctionProfile (BinaryFunction &Function,
134201 const yaml::bolt::BinaryFunctionProfile &YamlBF);
@@ -139,7 +206,8 @@ class YAMLProfileReader : public ProfileReaderBase {
139206
140207 // / Infer function profile from stale data (collected on older binaries).
141208 bool inferStaleProfile (BinaryFunction &Function,
142- const yaml::bolt::BinaryFunctionProfile &YamlBF);
209+ const yaml::bolt::BinaryFunctionProfile &YamlBF,
210+ const ArrayRef<ProbeMatchSpec> ProbeMatchSpecs);
143211
144212 // / Initialize maps for profile matching.
145213 void buildNameMaps (BinaryContext &BC);
@@ -156,6 +224,10 @@ class YAMLProfileReader : public ProfileReaderBase {
156224 // / Matches functions using the call graph.
157225 size_t matchWithCallGraph (BinaryContext &BC);
158226
227+ // / Matches functions using the call graph.
228+ // / Populates BF->partial probe match spec map.
229+ size_t matchWithPseudoProbes (BinaryContext &BC);
230+
159231 // / Matches functions with similarly named profiled functions.
160232 size_t matchWithNameSimilarity (BinaryContext &BC);
161233
0 commit comments