Skip to content

[Transforms] Migrate to a new version of getValueProfDataFromInst #96380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions llvm/lib/Transforms/IPO/SampleProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,25 +788,23 @@ SampleProfileLoader::findFunctionSamples(const Instruction &Inst) const {
/// NOMORE_ICP_MAGICNUM count values in the value profile of \p Inst, we
/// cannot promote for \p Inst anymore.
static bool doesHistoryAllowICP(const Instruction &Inst, StringRef Candidate) {
uint32_t NumVals = 0;
uint64_t TotalCount = 0;
auto ValueData =
getValueProfDataFromInst(Inst, IPVK_IndirectCallTarget, MaxNumPromotions,
NumVals, TotalCount, true);
auto ValueData = getValueProfDataFromInst(Inst, IPVK_IndirectCallTarget,
MaxNumPromotions, TotalCount, true);
// No valid value profile so no promoted targets have been recorded
// before. Ok to do ICP.
if (!ValueData)
if (ValueData.empty())
return true;

unsigned NumPromoted = 0;
for (uint32_t I = 0; I < NumVals; I++) {
if (ValueData[I].Count != NOMORE_ICP_MAGICNUM)
for (const auto &V : ValueData) {
if (V.Count != NOMORE_ICP_MAGICNUM)
continue;

// If the promotion candidate has NOMORE_ICP_MAGICNUM count in the
// metadata, it means the candidate has been promoted for this
// indirect call.
if (ValueData[I].Value == Function::getGUID(Candidate))
if (V.Value == Function::getGUID(Candidate))
return false;
NumPromoted++;
// If already have MaxNumPromotions promotion, don't do it anymore.
Expand All @@ -832,11 +830,10 @@ updateIDTMetaData(Instruction &Inst,
// `MaxNumPromotions` inside it.
if (MaxNumPromotions == 0)
return;
uint32_t NumVals = 0;
// OldSum is the existing total count in the value profile data.
uint64_t OldSum = 0;
auto ValueData = getValueProfDataFromInst(
Inst, IPVK_IndirectCallTarget, MaxNumPromotions, NumVals, OldSum, true);
auto ValueData = getValueProfDataFromInst(Inst, IPVK_IndirectCallTarget,
MaxNumPromotions, OldSum, true);

DenseMap<uint64_t, uint64_t> ValueCountMap;
if (Sum == 0) {
Expand All @@ -845,10 +842,8 @@ updateIDTMetaData(Instruction &Inst,
"If sum is 0, assume only one element in CallTargets "
"with count being NOMORE_ICP_MAGICNUM");
// Initialize ValueCountMap with existing value profile data.
if (ValueData) {
for (uint32_t I = 0; I < NumVals; I++)
ValueCountMap[ValueData[I].Value] = ValueData[I].Count;
}
for (const auto &V : ValueData)
ValueCountMap[V.Value] = V.Count;
auto Pair =
ValueCountMap.try_emplace(CallTargets[0].Value, CallTargets[0].Count);
// If the target already exists in value profile, decrease the total
Expand All @@ -861,11 +856,9 @@ updateIDTMetaData(Instruction &Inst,
} else {
// Initialize ValueCountMap with existing NOMORE_ICP_MAGICNUM
// counts in the value profile.
if (ValueData) {
for (uint32_t I = 0; I < NumVals; I++) {
if (ValueData[I].Count == NOMORE_ICP_MAGICNUM)
ValueCountMap[ValueData[I].Value] = ValueData[I].Count;
}
for (const auto &V : ValueData) {
if (V.Count == NOMORE_ICP_MAGICNUM)
ValueCountMap[V.Value] = V.Count;
}

for (const auto &Data : CallTargets) {
Expand Down
11 changes: 3 additions & 8 deletions llvm/lib/Transforms/Instrumentation/CGProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,11 @@ static bool runCGProfilePass(Module &M, FunctionAnalysisManager &FAM,
if (!CB)
continue;
if (CB->isIndirectCall()) {
uint32_t ActualNumValueData;
uint64_t TotalC;
auto ValueData = getValueProfDataFromInst(
*CB, IPVK_IndirectCallTarget, 8, ActualNumValueData, TotalC);
if (!ValueData)
continue;
for (const auto &VD : ArrayRef<InstrProfValueData>(
ValueData.get(), ActualNumValueData)) {
auto ValueData =
getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8, TotalC);
for (const auto &VD : ValueData)
UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
}
continue;
}
UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,11 @@ bool MemOPSizeOpt::perform(MemOp MO) {
if (!MemOPOptMemcmpBcmp && (MO.isMemcmp(TLI) || MO.isBcmp(TLI)))
return false;

uint32_t NumVals = INSTR_PROF_NUM_BUCKETS;
uint32_t MaxNumVals = INSTR_PROF_NUM_BUCKETS;
uint64_t TotalCount;
auto ValueDataArray = getValueProfDataFromInst(
*MO.I, IPVK_MemOPSize, MaxNumVals, NumVals, TotalCount);
if (!ValueDataArray)
auto VDs =
getValueProfDataFromInst(*MO.I, IPVK_MemOPSize, MaxNumVals, TotalCount);
if (VDs.empty())
return false;

uint64_t ActualCount = TotalCount;
Expand All @@ -264,7 +263,6 @@ bool MemOPSizeOpt::perform(MemOp MO) {
ActualCount = *BBEdgeCount;
}

ArrayRef<InstrProfValueData> VDs(ValueDataArray.get(), NumVals);
LLVM_DEBUG(dbgs() << "Read one memory intrinsic profile with count "
<< ActualCount << "\n");
LLVM_DEBUG(
Expand Down Expand Up @@ -397,10 +395,10 @@ bool MemOPSizeOpt::perform(MemOp MO) {
// Clear the value profile data.
MO.I->setMetadata(LLVMContext::MD_prof, nullptr);
// If all promoted, we don't need the MD.prof metadata.
if (SavedRemainCount > 0 || Version != NumVals) {
if (SavedRemainCount > 0 || Version != VDs.size()) {
// Otherwise we need update with the un-promoted records back.
annotateValueSite(*Func.getParent(), *MO.I, RemainingVDs, SavedRemainCount,
IPVK_MemOPSize, NumVals);
IPVK_MemOPSize, VDs.size());
}

LLVM_DEBUG(dbgs() << "\n\n== Basic Block After==\n");
Expand Down
Loading