Skip to content

[ProfileData] Add a variant of getValueProfDataFromInst #95993

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
7 changes: 7 additions & 0 deletions llvm/include/llvm/ProfileData/InstrProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
uint32_t MaxNumValueData, uint32_t &ActualNumValueData,
uint64_t &TotalC, bool GetNoICPValue = false);

/// Extract the value profile data from \p Inst and returns them if \p Inst is
/// annotated with value profile data. Returns an empty vector otherwise.
SmallVector<InstrProfValueData, 4>
getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
uint32_t MaxNumValueData, uint64_t &TotalC,
bool GetNoICPValue = false);

inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; }

/// Return the PGOFuncName meta data associated with a function.
Expand Down
17 changes: 6 additions & 11 deletions llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,15 @@ static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,

const Instruction *I = dyn_cast<Instruction>(CurUser);
if (I) {
uint32_t ActualNumValueData = 0;
uint64_t TotalCount = 0;
// MaxNumVTableAnnotations is the maximum number of vtables annotated on
// the instruction.
auto ValueDataArray =
getValueProfDataFromInst(*I, IPVK_VTableTarget, MaxNumVTableAnnotations,
ActualNumValueData, TotalCount);

if (ValueDataArray.get()) {
for (uint32_t j = 0; j < ActualNumValueData; j++) {
RefEdges.insert(Index.getOrInsertValueInfo(/* VTableGUID = */
ValueDataArray[j].Value));
}
}
auto ValueDataArray = getValueProfDataFromInst(
*I, IPVK_VTableTarget, MaxNumVTableAnnotations, TotalCount);

for (const auto &V : ValueDataArray)
RefEdges.insert(Index.getOrInsertValueInfo(/* VTableGUID = */
V.Value));
}
return HasBlockAddress;
}
Expand Down
39 changes: 39 additions & 0 deletions llvm/lib/ProfileData/InstrProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,45 @@ getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
return ValueDataArray;
}

SmallVector<InstrProfValueData, 4>
getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
uint32_t MaxNumValueData, uint64_t &TotalC,
bool GetNoICPValue) {
// Four inline elements seem to work well in practice. With MaxNumValueData,
// this array won't grow very big anyway.
SmallVector<InstrProfValueData, 4> ValueData;
MDNode *MD = mayHaveValueProfileOfKind(Inst, ValueKind);
if (!MD)
return ValueData;
const unsigned NOps = MD->getNumOperands();
// Get total count
ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
if (!TotalCInt)
return ValueData;
TotalC = TotalCInt->getZExtValue();

ValueData.reserve((NOps - 3) / 2);
for (unsigned I = 3; I < NOps; I += 2) {
if (ValueData.size() >= MaxNumValueData)
break;
ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
ConstantInt *Count =
mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
if (!Value || !Count) {
ValueData.clear();
return ValueData;
}
uint64_t CntValue = Count->getZExtValue();
if (!GetNoICPValue && (CntValue == NOMORE_ICP_MAGICNUM))
continue;
InstrProfValueData V;
V.Value = Value->getZExtValue();
V.Count = CntValue;
ValueData.push_back(V);
}
return ValueData;
}

MDNode *getPGOFuncNameMetadata(const Function &F) {
return F.getMetadata(getPGOFuncNameMetadataName());
}
Expand Down
Loading