Skip to content

Commit b0ae923

Browse files
[ProfileData] Add a variant of getValueProfDataFromInst (#95993)
This patch adds a variant of getValueProfDataFromInst that returns std::vector<InstrProfValueData> instead of std::unique<InstrProfValueData[]>. The new return type carries the length with it, so we can drop out parameter ActualNumValueData. Also, the caller can directly feed the return value into a range-based for loop as shown in the patch. I'm planning to migrate other callers of getValueProfDataFromInst to the new variant in follow-up patches.
1 parent 7500646 commit b0ae923

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
294294
uint32_t MaxNumValueData, uint32_t &ActualNumValueData,
295295
uint64_t &TotalC, bool GetNoICPValue = false);
296296

297+
/// Extract the value profile data from \p Inst and returns them if \p Inst is
298+
/// annotated with value profile data. Returns an empty vector otherwise.
299+
SmallVector<InstrProfValueData, 4>
300+
getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
301+
uint32_t MaxNumValueData, uint64_t &TotalC,
302+
bool GetNoICPValue = false);
303+
297304
inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; }
298305

299306
/// Return the PGOFuncName meta data associated with a function.

llvm/lib/Analysis/ModuleSummaryAnalysis.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,15 @@ static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
143143

144144
const Instruction *I = dyn_cast<Instruction>(CurUser);
145145
if (I) {
146-
uint32_t ActualNumValueData = 0;
147146
uint64_t TotalCount = 0;
148147
// MaxNumVTableAnnotations is the maximum number of vtables annotated on
149148
// the instruction.
150-
auto ValueDataArray =
151-
getValueProfDataFromInst(*I, IPVK_VTableTarget, MaxNumVTableAnnotations,
152-
ActualNumValueData, TotalCount);
153-
154-
if (ValueDataArray.get()) {
155-
for (uint32_t j = 0; j < ActualNumValueData; j++) {
156-
RefEdges.insert(Index.getOrInsertValueInfo(/* VTableGUID = */
157-
ValueDataArray[j].Value));
158-
}
159-
}
149+
auto ValueDataArray = getValueProfDataFromInst(
150+
*I, IPVK_VTableTarget, MaxNumVTableAnnotations, TotalCount);
151+
152+
for (const auto &V : ValueDataArray)
153+
RefEdges.insert(Index.getOrInsertValueInfo(/* VTableGUID = */
154+
V.Value));
160155
}
161156
return HasBlockAddress;
162157
}

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,45 @@ getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
13821382
return ValueDataArray;
13831383
}
13841384

1385+
SmallVector<InstrProfValueData, 4>
1386+
getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
1387+
uint32_t MaxNumValueData, uint64_t &TotalC,
1388+
bool GetNoICPValue) {
1389+
// Four inline elements seem to work well in practice. With MaxNumValueData,
1390+
// this array won't grow very big anyway.
1391+
SmallVector<InstrProfValueData, 4> ValueData;
1392+
MDNode *MD = mayHaveValueProfileOfKind(Inst, ValueKind);
1393+
if (!MD)
1394+
return ValueData;
1395+
const unsigned NOps = MD->getNumOperands();
1396+
// Get total count
1397+
ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
1398+
if (!TotalCInt)
1399+
return ValueData;
1400+
TotalC = TotalCInt->getZExtValue();
1401+
1402+
ValueData.reserve((NOps - 3) / 2);
1403+
for (unsigned I = 3; I < NOps; I += 2) {
1404+
if (ValueData.size() >= MaxNumValueData)
1405+
break;
1406+
ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
1407+
ConstantInt *Count =
1408+
mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
1409+
if (!Value || !Count) {
1410+
ValueData.clear();
1411+
return ValueData;
1412+
}
1413+
uint64_t CntValue = Count->getZExtValue();
1414+
if (!GetNoICPValue && (CntValue == NOMORE_ICP_MAGICNUM))
1415+
continue;
1416+
InstrProfValueData V;
1417+
V.Value = Value->getZExtValue();
1418+
V.Count = CntValue;
1419+
ValueData.push_back(V);
1420+
}
1421+
return ValueData;
1422+
}
1423+
13851424
MDNode *getPGOFuncNameMetadata(const Function &F) {
13861425
return F.getMetadata(getPGOFuncNameMetadataName());
13871426
}

0 commit comments

Comments
 (0)