Skip to content

Commit 3cf762b

Browse files
[Transforms] Migrate to a new version of getValueProfDataFromInst (#96380)
1 parent 10c894c commit 3cf762b

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -788,25 +788,23 @@ SampleProfileLoader::findFunctionSamples(const Instruction &Inst) const {
788788
/// NOMORE_ICP_MAGICNUM count values in the value profile of \p Inst, we
789789
/// cannot promote for \p Inst anymore.
790790
static bool doesHistoryAllowICP(const Instruction &Inst, StringRef Candidate) {
791-
uint32_t NumVals = 0;
792791
uint64_t TotalCount = 0;
793-
auto ValueData =
794-
getValueProfDataFromInst(Inst, IPVK_IndirectCallTarget, MaxNumPromotions,
795-
NumVals, TotalCount, true);
792+
auto ValueData = getValueProfDataFromInst(Inst, IPVK_IndirectCallTarget,
793+
MaxNumPromotions, TotalCount, true);
796794
// No valid value profile so no promoted targets have been recorded
797795
// before. Ok to do ICP.
798-
if (!ValueData)
796+
if (ValueData.empty())
799797
return true;
800798

801799
unsigned NumPromoted = 0;
802-
for (uint32_t I = 0; I < NumVals; I++) {
803-
if (ValueData[I].Count != NOMORE_ICP_MAGICNUM)
800+
for (const auto &V : ValueData) {
801+
if (V.Count != NOMORE_ICP_MAGICNUM)
804802
continue;
805803

806804
// If the promotion candidate has NOMORE_ICP_MAGICNUM count in the
807805
// metadata, it means the candidate has been promoted for this
808806
// indirect call.
809-
if (ValueData[I].Value == Function::getGUID(Candidate))
807+
if (V.Value == Function::getGUID(Candidate))
810808
return false;
811809
NumPromoted++;
812810
// If already have MaxNumPromotions promotion, don't do it anymore.
@@ -832,11 +830,10 @@ updateIDTMetaData(Instruction &Inst,
832830
// `MaxNumPromotions` inside it.
833831
if (MaxNumPromotions == 0)
834832
return;
835-
uint32_t NumVals = 0;
836833
// OldSum is the existing total count in the value profile data.
837834
uint64_t OldSum = 0;
838-
auto ValueData = getValueProfDataFromInst(
839-
Inst, IPVK_IndirectCallTarget, MaxNumPromotions, NumVals, OldSum, true);
835+
auto ValueData = getValueProfDataFromInst(Inst, IPVK_IndirectCallTarget,
836+
MaxNumPromotions, OldSum, true);
840837

841838
DenseMap<uint64_t, uint64_t> ValueCountMap;
842839
if (Sum == 0) {
@@ -845,10 +842,8 @@ updateIDTMetaData(Instruction &Inst,
845842
"If sum is 0, assume only one element in CallTargets "
846843
"with count being NOMORE_ICP_MAGICNUM");
847844
// Initialize ValueCountMap with existing value profile data.
848-
if (ValueData) {
849-
for (uint32_t I = 0; I < NumVals; I++)
850-
ValueCountMap[ValueData[I].Value] = ValueData[I].Count;
851-
}
845+
for (const auto &V : ValueData)
846+
ValueCountMap[V.Value] = V.Count;
852847
auto Pair =
853848
ValueCountMap.try_emplace(CallTargets[0].Value, CallTargets[0].Count);
854849
// If the target already exists in value profile, decrease the total
@@ -861,11 +856,9 @@ updateIDTMetaData(Instruction &Inst,
861856
} else {
862857
// Initialize ValueCountMap with existing NOMORE_ICP_MAGICNUM
863858
// counts in the value profile.
864-
if (ValueData) {
865-
for (uint32_t I = 0; I < NumVals; I++) {
866-
if (ValueData[I].Count == NOMORE_ICP_MAGICNUM)
867-
ValueCountMap[ValueData[I].Value] = ValueData[I].Count;
868-
}
859+
for (const auto &V : ValueData) {
860+
if (V.Count == NOMORE_ICP_MAGICNUM)
861+
ValueCountMap[V.Value] = V.Count;
869862
}
870863

871864
for (const auto &Data : CallTargets) {

llvm/lib/Transforms/Instrumentation/CGProfile.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,11 @@ static bool runCGProfilePass(Module &M, FunctionAnalysisManager &FAM,
7979
if (!CB)
8080
continue;
8181
if (CB->isIndirectCall()) {
82-
uint32_t ActualNumValueData;
8382
uint64_t TotalC;
84-
auto ValueData = getValueProfDataFromInst(
85-
*CB, IPVK_IndirectCallTarget, 8, ActualNumValueData, TotalC);
86-
if (!ValueData)
87-
continue;
88-
for (const auto &VD : ArrayRef<InstrProfValueData>(
89-
ValueData.get(), ActualNumValueData)) {
83+
auto ValueData =
84+
getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8, TotalC);
85+
for (const auto &VD : ValueData)
9086
UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
91-
}
9287
continue;
9388
}
9489
UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);

llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,11 @@ bool MemOPSizeOpt::perform(MemOp MO) {
247247
if (!MemOPOptMemcmpBcmp && (MO.isMemcmp(TLI) || MO.isBcmp(TLI)))
248248
return false;
249249

250-
uint32_t NumVals = INSTR_PROF_NUM_BUCKETS;
251250
uint32_t MaxNumVals = INSTR_PROF_NUM_BUCKETS;
252251
uint64_t TotalCount;
253-
auto ValueDataArray = getValueProfDataFromInst(
254-
*MO.I, IPVK_MemOPSize, MaxNumVals, NumVals, TotalCount);
255-
if (!ValueDataArray)
252+
auto VDs =
253+
getValueProfDataFromInst(*MO.I, IPVK_MemOPSize, MaxNumVals, TotalCount);
254+
if (VDs.empty())
256255
return false;
257256

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

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

406404
LLVM_DEBUG(dbgs() << "\n\n== Basic Block After==\n");

0 commit comments

Comments
 (0)