Skip to content

Commit 08e210c

Browse files
[NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update into one helper function (#80762)
* The motivation is to move indirect callee profile update inside the function-based speculative indirect-call promotion, so that there are fewer diffs the vtable-based transformation and profile update is implemented in a follow-up patch. * The Parent patch is #79381
1 parent aa80f3e commit 08e210c

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ void annotateValueSite(Module &M, Instruction &Inst,
277277
uint32_t MaxMDCount = 3);
278278

279279
/// Same as the above interface but using an ArrayRef, as well as \p Sum.
280+
/// This function will not annotate !prof metadata on the instruction if the
281+
/// referenced array is empty.
280282
void annotateValueSite(Module &M, Instruction &Inst,
281283
ArrayRef<InstrProfValueData> VDs, uint64_t Sum,
282284
InstrProfValueKind ValueKind, uint32_t MaxMDCount);
@@ -465,7 +467,6 @@ class InstrProfSymtab {
465467
StringSet<> VTableNames;
466468
// A map from MD5 keys to function name strings.
467469
std::vector<std::pair<uint64_t, StringRef>> MD5NameMap;
468-
469470
// A map from MD5 keys to function define. We only populate this map
470471
// when build the Symtab from a Module.
471472
std::vector<std::pair<uint64_t, Function *>> MD5FuncMap;

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,8 @@ void annotateValueSite(Module &M, Instruction &Inst,
12461246
ArrayRef<InstrProfValueData> VDs,
12471247
uint64_t Sum, InstrProfValueKind ValueKind,
12481248
uint32_t MaxMDCount) {
1249+
if (VDs.empty())
1250+
return;
12491251
LLVMContext &Ctx = M.getContext();
12501252
MDBuilder MDHelper(Ctx);
12511253
SmallVector<Metadata *, 3> Vals;

llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,13 @@ class IndirectCallPromoter {
136136
const CallBase &CB, const ArrayRef<InstrProfValueData> &ValueDataRef,
137137
uint64_t TotalCount, uint32_t NumCandidates);
138138

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);
144146

145147
public:
146148
IndirectCallPromoter(Function &Func, InstrProfSymtab *Symtab, bool SamplePGO,
@@ -273,9 +275,10 @@ CallBase &llvm::pgo::promoteIndirectCall(CallBase &CB, Function *DirectCallee,
273275
}
274276

275277
// Promote indirect-call to conditional direct-call for one callsite.
276-
uint32_t IndirectCallPromoter::tryToPromote(
278+
bool IndirectCallPromoter::tryToPromoteWithFuncCmp(
277279
CallBase &CB, const std::vector<PromotionCandidate> &Candidates,
278-
uint64_t &TotalCount) {
280+
uint64_t TotalCount, ArrayRef<InstrProfValueData> ICallProfDataRef,
281+
uint32_t NumCandidates) {
279282
uint32_t NumPromoted = 0;
280283

281284
for (const auto &C : Candidates) {
@@ -287,7 +290,22 @@ uint32_t IndirectCallPromoter::tryToPromote(
287290
NumOfPGOICallPromotion++;
288291
NumPromoted++;
289292
}
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;
291309
}
292310

293311
// Traverse all the indirect-call callsite and get the value profile
@@ -305,19 +323,8 @@ bool IndirectCallPromoter::processFunction(ProfileSummaryInfo *PSI) {
305323
continue;
306324
auto PromotionCandidates = getPromotionCandidatesForCallSite(
307325
*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);
321328
}
322329
return Changed;
323330
}

0 commit comments

Comments
 (0)