@@ -143,7 +143,6 @@ void PseudoProbeRewriter::parsePseudoProbe() {
143143 if (!ProbeDecoder.buildAddress2ProbeMap (
144144 reinterpret_cast <const uint8_t *>(Contents.data ()), Contents.size (),
145145 GuidFilter, FuncStartAddrs)) {
146- ProbeDecoder.getAddress2ProbesMap ().clear ();
147146 errs () << " BOLT-WARNING: fail in building Address2ProbeMap\n " ;
148147 return ;
149148 }
@@ -156,7 +155,8 @@ void PseudoProbeRewriter::parsePseudoProbe() {
156155 ProbeDecoder.printProbesForAllAddresses (outs ());
157156 }
158157
159- for (const auto &[GUID, FuncDesc] : ProbeDecoder.getGUID2FuncDescMap ()) {
158+ for (const auto &FuncDesc : ProbeDecoder.getGUID2FuncDescMap ()) {
159+ uint64_t GUID = FuncDesc.FuncGUID ;
160160 if (!FuncStartAddrs.contains (GUID))
161161 continue ;
162162 BinaryFunction *BF = BC.getBinaryFunctionAtAddress (FuncStartAddrs[GUID]);
@@ -174,59 +174,50 @@ void PseudoProbeRewriter::updatePseudoProbes() {
174174 AddressProbesMap &Address2ProbesMap = ProbeDecoder.getAddress2ProbesMap ();
175175 const GUIDProbeFunctionMap &GUID2Func = ProbeDecoder.getGUID2FuncDescMap ();
176176
177- for (auto &AP : Address2ProbesMap) {
178- BinaryFunction *F = BC.getBinaryFunctionContainingAddress (AP.first );
177+ for (MCDecodedPseudoProbe &Probe : Address2ProbesMap) {
178+ uint64_t Address = Probe.getAddress ();
179+ BinaryFunction *F = BC.getBinaryFunctionContainingAddress (Address);
179180 // If F is removed, eliminate all probes inside it from inline tree
180181 // Setting probes' addresses as INT64_MAX means elimination
181182 if (!F) {
182- for (MCDecodedPseudoProbe &Probe : AP.second )
183- Probe.setAddress (INT64_MAX);
183+ Probe.setAddress (INT64_MAX);
184184 continue ;
185185 }
186186 // If F is not emitted, the function will remain in the same address as its
187187 // input
188188 if (!F->isEmitted ())
189189 continue ;
190190
191- uint64_t Offset = AP. first - F->getAddress ();
191+ uint64_t Offset = Address - F->getAddress ();
192192 const BinaryBasicBlock *BB = F->getBasicBlockContainingOffset (Offset);
193193 uint64_t BlkOutputAddress = BB->getOutputAddressRange ().first ;
194194 // Check if block output address is defined.
195195 // If not, such block is removed from binary. Then remove the probes from
196196 // inline tree
197197 if (BlkOutputAddress == 0 ) {
198- for (MCDecodedPseudoProbe &Probe : AP.second )
199- Probe.setAddress (INT64_MAX);
198+ Probe.setAddress (INT64_MAX);
200199 continue ;
201200 }
202201
203- unsigned ProbeTrack = AP.second .size ();
204- std::list<MCDecodedPseudoProbe>::iterator Probe = AP.second .begin ();
205- while (ProbeTrack != 0 ) {
206- if (Probe->isBlock ()) {
207- Probe->setAddress (BlkOutputAddress);
208- } else if (Probe->isCall ()) {
209- // A call probe may be duplicated due to ICP
210- // Go through output of InputOffsetToAddressMap to collect all related
211- // probes
212- auto CallOutputAddresses = BC.getIOAddressMap ().lookupAll (AP.first );
213- auto CallOutputAddress = CallOutputAddresses.first ;
214- if (CallOutputAddress == CallOutputAddresses.second ) {
215- Probe->setAddress (INT64_MAX);
216- } else {
217- Probe->setAddress (CallOutputAddress->second );
218- CallOutputAddress = std::next (CallOutputAddress);
219- }
220-
221- while (CallOutputAddress != CallOutputAddresses.second ) {
222- AP.second .push_back (*Probe);
223- AP.second .back ().setAddress (CallOutputAddress->second );
224- Probe->getInlineTreeNode ()->addProbes (&(AP.second .back ()));
225- CallOutputAddress = std::next (CallOutputAddress);
226- }
202+ if (Probe.isBlock ()) {
203+ Probe.setAddress (BlkOutputAddress);
204+ } else if (Probe.isCall ()) {
205+ // A call probe may be duplicated due to ICP
206+ // Go through output of InputOffsetToAddressMap to collect all related
207+ // probes
208+ auto CallOutputAddresses = BC.getIOAddressMap ().lookupAll (Address);
209+ auto CallOutputAddress = CallOutputAddresses.first ;
210+ if (CallOutputAddress == CallOutputAddresses.second ) {
211+ Probe.setAddress (INT64_MAX);
212+ } else {
213+ Probe.setAddress (CallOutputAddress->second );
214+ CallOutputAddress = std::next (CallOutputAddress);
215+ }
216+
217+ while (CallOutputAddress != CallOutputAddresses.second ) {
218+ ProbeDecoder.addInjectedProbe (Probe, CallOutputAddress->second );
219+ CallOutputAddress = std::next (CallOutputAddress);
227220 }
228- Probe = std::next (Probe);
229- ProbeTrack--;
230221 }
231222 }
232223
@@ -242,22 +233,16 @@ void PseudoProbeRewriter::updatePseudoProbes() {
242233 BinaryBlock.getName ();
243234
244235 // scan all addresses -> correlate probe to block when print out
245- std::vector<uint64_t > Addresses;
246- for (auto &Entry : Address2ProbesMap)
247- Addresses.push_back (Entry.first );
248- llvm::sort (Addresses);
249- for (uint64_t Key : Addresses) {
250- for (MCDecodedPseudoProbe &Probe : Address2ProbesMap[Key]) {
251- if (Probe.getAddress () == INT64_MAX)
252- outs () << " Deleted Probe: " ;
253- else
254- outs () << " Address: " << format_hex (Probe.getAddress (), 8 ) << " " ;
255- Probe.print (outs (), GUID2Func, true );
256- // print block name only if the probe is block type and undeleted.
257- if (Probe.isBlock () && Probe.getAddress () != INT64_MAX)
258- outs () << format_hex (Probe.getAddress (), 8 ) << " Probe is in "
259- << Addr2BlockNames[Probe.getAddress ()] << " \n " ;
260- }
236+ for (MCDecodedPseudoProbe &Probe : Address2ProbesMap) {
237+ if (Probe.getAddress () == INT64_MAX)
238+ outs () << " Deleted Probe: " ;
239+ else
240+ outs () << " Address: " << format_hex (Probe.getAddress (), 8 ) << " " ;
241+ Probe.print (outs (), GUID2Func, true );
242+ // print block name only if the probe is block type and undeleted.
243+ if (Probe.isBlock () && Probe.getAddress () != INT64_MAX)
244+ outs () << format_hex (Probe.getAddress (), 8 ) << " Probe is in "
245+ << Addr2BlockNames[Probe.getAddress ()] << " \n " ;
261246 }
262247 outs () << " =======================================\n " ;
263248 }
@@ -333,7 +318,7 @@ void PseudoProbeRewriter::encodePseudoProbes() {
333318 ProbeDecoder.getDummyInlineRoot ();
334319 for (auto Child = Root.getChildren ().begin ();
335320 Child != Root.getChildren ().end (); ++Child)
336- Inlinees[Child->first ] = Child-> second . get () ;
321+ Inlinees[Child->getInlineSite () ] = &* Child;
337322
338323 for (auto Inlinee : Inlinees)
339324 // INT64_MAX is "placeholder" of unused callsite index field in the pair
@@ -359,25 +344,37 @@ void PseudoProbeRewriter::encodePseudoProbes() {
359344 EmitInt (Cur->Guid , 8 );
360345 // Emit number of probes in this node
361346 uint64_t Deleted = 0 ;
362- for (MCDecodedPseudoProbe *&Probe : Cur->getProbes ())
347+ for (MCDecodedPseudoProbe *&Probe :
348+ llvm::make_pointer_range (Cur->getProbes ()))
363349 if (Probe->getAddress () == INT64_MAX)
364350 Deleted++;
365351 LLVM_DEBUG (dbgs () << " Deleted Probes:" << Deleted << " \n " );
366- uint64_t ProbesSize = Cur->getProbes ().size () - Deleted;
352+ size_t InjectedProbes = ProbeDecoder.getNumInjectedProbes (Cur);
353+ uint64_t ProbesSize = Cur->getProbes ().size () - Deleted + InjectedProbes;
367354 EmitULEB128IntValue (ProbesSize);
368355 // Emit number of direct inlinees
369356 EmitULEB128IntValue (Cur->getChildren ().size ());
370357 // Emit probes in this group
371- for (MCDecodedPseudoProbe *&Probe : Cur->getProbes ()) {
358+ for (MCDecodedPseudoProbe *&Probe :
359+ llvm::make_pointer_range (Cur->getProbes ())) {
372360 if (Probe->getAddress () == INT64_MAX)
373361 continue ;
374362 EmitDecodedPseudoProbe (Probe);
375363 LastProbe = Probe;
376364 }
365+ if (InjectedProbes) {
366+ for (MCDecodedPseudoProbe *&Probe :
367+ llvm::make_pointer_range (ProbeDecoder.getInjectedProbes (Cur))) {
368+ if (Probe->getAddress () == INT64_MAX)
369+ continue ;
370+ EmitDecodedPseudoProbe (Probe);
371+ LastProbe = Probe;
372+ }
373+ }
377374
378375 for (auto Child = Cur->getChildren ().begin ();
379376 Child != Cur->getChildren ().end (); ++Child)
380- Inlinees[Child->first ] = Child-> second . get () ;
377+ Inlinees[Child->getInlineSite () ] = &* Child;
381378 for (const auto &Inlinee : Inlinees) {
382379 assert (Cur->Guid != 0 && " non root tree node must have nonzero Guid" );
383380 NextNodes.push_back ({std::get<1 >(Inlinee.first ), Inlinee.second });
0 commit comments