Skip to content

[ProfileData] Compute sum in annotateValueSite (NFC) #95199

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
merged 1 commit into from
Jun 12, 2024
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: 10 additions & 23 deletions llvm/include/llvm/ProfileData/InstrProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -869,18 +869,14 @@ struct InstrProfRecord {
inline uint32_t getNumValueDataForSite(uint32_t ValueKind,
uint32_t Site) const;

/// Return the array of profiled values at \p Site. If \p TotalC
/// is not null, the total count of all target values at this site
/// will be stored in \c *TotalC.
/// Return the array of profiled values at \p Site.
inline std::unique_ptr<InstrProfValueData[]>
getValueForSite(uint32_t ValueKind, uint32_t Site,
uint64_t *TotalC = nullptr) const;
getValueForSite(uint32_t ValueKind, uint32_t Site) const;

/// Get the target value/counts of kind \p ValueKind collected at site
/// \p Site and store the result in array \p Dest. Return the total
/// counts of all target values at this site.
inline uint64_t getValueForSite(InstrProfValueData Dest[], uint32_t ValueKind,
uint32_t Site) const;
/// \p Site and store the result in array \p Dest.
inline void getValueForSite(InstrProfValueData Dest[], uint32_t ValueKind,
uint32_t Site) const;

/// Reserve space for NumValueSites sites.
inline void reserveSites(uint32_t ValueKind, uint32_t NumValueSites);
Expand Down Expand Up @@ -1065,34 +1061,25 @@ uint32_t InstrProfRecord::getNumValueDataForSite(uint32_t ValueKind,
}

std::unique_ptr<InstrProfValueData[]>
InstrProfRecord::getValueForSite(uint32_t ValueKind, uint32_t Site,
uint64_t *TotalC) const {
uint64_t Dummy = 0;
uint64_t &TotalCount = (TotalC == nullptr ? Dummy : *TotalC);
InstrProfRecord::getValueForSite(uint32_t ValueKind, uint32_t Site) const {
uint32_t N = getNumValueDataForSite(ValueKind, Site);
if (N == 0) {
TotalCount = 0;
if (N == 0)
return std::unique_ptr<InstrProfValueData[]>(nullptr);
}

auto VD = std::make_unique<InstrProfValueData[]>(N);
TotalCount = getValueForSite(VD.get(), ValueKind, Site);
getValueForSite(VD.get(), ValueKind, Site);

return VD;
}

uint64_t InstrProfRecord::getValueForSite(InstrProfValueData Dest[],
uint32_t ValueKind,
uint32_t Site) const {
void InstrProfRecord::getValueForSite(InstrProfValueData Dest[],
uint32_t ValueKind, uint32_t Site) const {
uint32_t I = 0;
uint64_t TotalCount = 0;
for (auto V : getValueSitesForKind(ValueKind)[Site].ValueData) {
Dest[I].Value = V.Value;
Dest[I].Count = V.Count;
TotalCount = SaturatingAdd(TotalCount, V.Count);
I++;
}
return TotalCount;
}

void InstrProfRecord::reserveSites(uint32_t ValueKind, uint32_t NumValueSites) {
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/ProfileData/InstrProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1271,11 +1271,13 @@ void annotateValueSite(Module &M, Instruction &Inst,
if (!NV)
return;

uint64_t Sum = 0;
std::unique_ptr<InstrProfValueData[]> VD =
InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
InstrProfR.getValueForSite(ValueKind, SiteIdx);

ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
uint64_t Sum = 0;
for (const InstrProfValueData &V : VDs)
Sum = SaturatingAdd(Sum, V.Count);
annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
}

Expand Down
12 changes: 3 additions & 9 deletions llvm/unittests/ProfileData/InstrProfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,11 @@ TEST_P(InstrProfReaderWriterTest, icall_and_vtable_data_read_write) {

// First indirect site.
{
uint64_t TotalC;
auto VD = R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC);
auto VD = R->getValueForSite(IPVK_IndirectCallTarget, 0);

EXPECT_EQ(VD[0].Count, 3U * getProfWeight());
EXPECT_EQ(VD[1].Count, 2U * getProfWeight());
EXPECT_EQ(VD[2].Count, 1U * getProfWeight());
EXPECT_EQ(TotalC, 6U * getProfWeight());

EXPECT_STREQ((const char *)VD[0].Value, "callee3");
EXPECT_STREQ((const char *)VD[1].Value, "callee2");
Expand All @@ -882,13 +880,11 @@ TEST_P(InstrProfReaderWriterTest, icall_and_vtable_data_read_write) {

// First vtable site.
{
uint64_t TotalC;
auto VD = R->getValueForSite(IPVK_VTableTarget, 0, &TotalC);
auto VD = R->getValueForSite(IPVK_VTableTarget, 0);

EXPECT_EQ(VD[0].Count, 3U * getProfWeight());
EXPECT_EQ(VD[1].Count, 2U * getProfWeight());
EXPECT_EQ(VD[2].Count, 1U * getProfWeight());
EXPECT_EQ(TotalC, 6U * getProfWeight());

EXPECT_EQ(VD[0].Value, getCalleeAddress(vtable3));
EXPECT_EQ(VD[1].Value, getCalleeAddress(vtable2));
Expand All @@ -897,12 +893,10 @@ TEST_P(InstrProfReaderWriterTest, icall_and_vtable_data_read_write) {

// Second vtable site.
{
uint64_t TotalC;
auto VD = R->getValueForSite(IPVK_VTableTarget, 1, &TotalC);
auto VD = R->getValueForSite(IPVK_VTableTarget, 1);

EXPECT_EQ(VD[0].Count, 2U * getProfWeight());
EXPECT_EQ(VD[1].Count, 1U * getProfWeight());
EXPECT_EQ(TotalC, 3U * getProfWeight());

EXPECT_EQ(VD[0].Value, getCalleeAddress(vtable2));
EXPECT_EQ(VD[1].Value, getCalleeAddress(vtable1));
Expand Down
Loading