@@ -136,11 +136,13 @@ class IndirectCallPromoter {
136
136
const CallBase &CB, const ArrayRef<InstrProfValueData> &ValueDataRef,
137
137
uint64_t TotalCount, uint32_t NumCandidates);
138
138
139
- // Promote a list of targets for one indirect-call callsite. Return
140
- // the number of promotions.
141
- uint32_t tryToPromote (CallBase &CB,
142
- const std::vector<PromotionCandidate> &Candidates,
143
- uint64_t &TotalCount);
139
+ // Promote a list of targets for one indirect-call callsite by comparing
140
+ // indirect callee with functions. Returns true if there are IR
141
+ // transformations and false otherwise.
142
+ bool tryToPromoteWithFuncCmp (
143
+ CallBase &CB, const std::vector<PromotionCandidate> &Candidates,
144
+ uint64_t TotalCount, ArrayRef<InstrProfValueData> ICallProfDataRef,
145
+ uint32_t NumCandidates);
144
146
145
147
public:
146
148
IndirectCallPromoter (Function &Func, InstrProfSymtab *Symtab, bool SamplePGO,
@@ -273,9 +275,10 @@ CallBase &llvm::pgo::promoteIndirectCall(CallBase &CB, Function *DirectCallee,
273
275
}
274
276
275
277
// Promote indirect-call to conditional direct-call for one callsite.
276
- uint32_t IndirectCallPromoter::tryToPromote (
278
+ bool IndirectCallPromoter::tryToPromoteWithFuncCmp (
277
279
CallBase &CB, const std::vector<PromotionCandidate> &Candidates,
278
- uint64_t &TotalCount) {
280
+ uint64_t TotalCount, ArrayRef<InstrProfValueData> ICallProfDataRef,
281
+ uint32_t NumCandidates) {
279
282
uint32_t NumPromoted = 0 ;
280
283
281
284
for (const auto &C : Candidates) {
@@ -287,7 +290,22 @@ uint32_t IndirectCallPromoter::tryToPromote(
287
290
NumOfPGOICallPromotion++;
288
291
NumPromoted++;
289
292
}
290
- return NumPromoted;
293
+
294
+ if (NumPromoted == 0 )
295
+ return false ;
296
+
297
+ // Adjust the MD.prof metadata. First delete the old one.
298
+ CB.setMetadata (LLVMContext::MD_prof, nullptr );
299
+
300
+ assert (NumPromoted <= ICallProfDataRef.size () &&
301
+ " Number of promoted functions should not be greater than the number "
302
+ " of values in profile metadata" );
303
+ // Annotate the remaining value profiles if counter is not zero.
304
+ if (TotalCount != 0 )
305
+ annotateValueSite (*F.getParent (), CB, ICallProfDataRef.slice (NumPromoted),
306
+ TotalCount, IPVK_IndirectCallTarget, NumCandidates);
307
+
308
+ return true ;
291
309
}
292
310
293
311
// Traverse all the indirect-call callsite and get the value profile
@@ -305,19 +323,8 @@ bool IndirectCallPromoter::processFunction(ProfileSummaryInfo *PSI) {
305
323
continue ;
306
324
auto PromotionCandidates = getPromotionCandidatesForCallSite (
307
325
*CB, ICallProfDataRef, TotalCount, NumCandidates);
308
- uint32_t NumPromoted = tryToPromote (*CB, PromotionCandidates, TotalCount);
309
- if (NumPromoted == 0 )
310
- continue ;
311
-
312
- Changed = true ;
313
- // Adjust the MD.prof metadata. First delete the old one.
314
- CB->setMetadata (LLVMContext::MD_prof, nullptr );
315
- // If all promoted, we don't need the MD.prof metadata.
316
- if (TotalCount == 0 || NumPromoted == NumVals)
317
- continue ;
318
- // Otherwise we need update with the un-promoted records back.
319
- annotateValueSite (*F.getParent (), *CB, ICallProfDataRef.slice (NumPromoted),
320
- TotalCount, IPVK_IndirectCallTarget, NumCandidates);
326
+ Changed |= tryToPromoteWithFuncCmp (*CB, PromotionCandidates, TotalCount,
327
+ ICallProfDataRef, NumCandidates);
321
328
}
322
329
return Changed;
323
330
}
0 commit comments