Skip to content

Commit 00fa3fb

Browse files
[ProfileData] Compute sum in annotateValueSite (NFC) (#95199)
getValueForSite computes the total count -- the total number of times a given value site is visited. The problem is that, excluding tests, annotateValueSite is the only place that needs the total count. This patch moves the total count computation to annotateValueSite.
1 parent 760ad23 commit 00fa3fb

File tree

3 files changed

+17
-34
lines changed

3 files changed

+17
-34
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -869,18 +869,14 @@ struct InstrProfRecord {
869869
inline uint32_t getNumValueDataForSite(uint32_t ValueKind,
870870
uint32_t Site) const;
871871

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

879876
/// Get the target value/counts of kind \p ValueKind collected at site
880-
/// \p Site and store the result in array \p Dest. Return the total
881-
/// counts of all target values at this site.
882-
inline uint64_t getValueForSite(InstrProfValueData Dest[], uint32_t ValueKind,
883-
uint32_t Site) const;
877+
/// \p Site and store the result in array \p Dest.
878+
inline void getValueForSite(InstrProfValueData Dest[], uint32_t ValueKind,
879+
uint32_t Site) const;
884880

885881
/// Reserve space for NumValueSites sites.
886882
inline void reserveSites(uint32_t ValueKind, uint32_t NumValueSites);
@@ -1065,34 +1061,25 @@ uint32_t InstrProfRecord::getNumValueDataForSite(uint32_t ValueKind,
10651061
}
10661062

10671063
std::unique_ptr<InstrProfValueData[]>
1068-
InstrProfRecord::getValueForSite(uint32_t ValueKind, uint32_t Site,
1069-
uint64_t *TotalC) const {
1070-
uint64_t Dummy = 0;
1071-
uint64_t &TotalCount = (TotalC == nullptr ? Dummy : *TotalC);
1064+
InstrProfRecord::getValueForSite(uint32_t ValueKind, uint32_t Site) const {
10721065
uint32_t N = getNumValueDataForSite(ValueKind, Site);
1073-
if (N == 0) {
1074-
TotalCount = 0;
1066+
if (N == 0)
10751067
return std::unique_ptr<InstrProfValueData[]>(nullptr);
1076-
}
10771068

10781069
auto VD = std::make_unique<InstrProfValueData[]>(N);
1079-
TotalCount = getValueForSite(VD.get(), ValueKind, Site);
1070+
getValueForSite(VD.get(), ValueKind, Site);
10801071

10811072
return VD;
10821073
}
10831074

1084-
uint64_t InstrProfRecord::getValueForSite(InstrProfValueData Dest[],
1085-
uint32_t ValueKind,
1086-
uint32_t Site) const {
1075+
void InstrProfRecord::getValueForSite(InstrProfValueData Dest[],
1076+
uint32_t ValueKind, uint32_t Site) const {
10871077
uint32_t I = 0;
1088-
uint64_t TotalCount = 0;
10891078
for (auto V : getValueSitesForKind(ValueKind)[Site].ValueData) {
10901079
Dest[I].Value = V.Value;
10911080
Dest[I].Count = V.Count;
1092-
TotalCount = SaturatingAdd(TotalCount, V.Count);
10931081
I++;
10941082
}
1095-
return TotalCount;
10961083
}
10971084

10981085
void InstrProfRecord::reserveSites(uint32_t ValueKind, uint32_t NumValueSites) {

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,11 +1271,13 @@ void annotateValueSite(Module &M, Instruction &Inst,
12711271
if (!NV)
12721272
return;
12731273

1274-
uint64_t Sum = 0;
12751274
std::unique_ptr<InstrProfValueData[]> VD =
1276-
InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
1275+
InstrProfR.getValueForSite(ValueKind, SiteIdx);
12771276

12781277
ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
1278+
uint64_t Sum = 0;
1279+
for (const InstrProfValueData &V : VDs)
1280+
Sum = SaturatingAdd(Sum, V.Count);
12791281
annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
12801282
}
12811283

llvm/unittests/ProfileData/InstrProfTest.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,13 +867,11 @@ TEST_P(InstrProfReaderWriterTest, icall_and_vtable_data_read_write) {
867867

868868
// First indirect site.
869869
{
870-
uint64_t TotalC;
871-
auto VD = R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC);
870+
auto VD = R->getValueForSite(IPVK_IndirectCallTarget, 0);
872871

873872
EXPECT_EQ(VD[0].Count, 3U * getProfWeight());
874873
EXPECT_EQ(VD[1].Count, 2U * getProfWeight());
875874
EXPECT_EQ(VD[2].Count, 1U * getProfWeight());
876-
EXPECT_EQ(TotalC, 6U * getProfWeight());
877875

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

883881
// First vtable site.
884882
{
885-
uint64_t TotalC;
886-
auto VD = R->getValueForSite(IPVK_VTableTarget, 0, &TotalC);
883+
auto VD = R->getValueForSite(IPVK_VTableTarget, 0);
887884

888885
EXPECT_EQ(VD[0].Count, 3U * getProfWeight());
889886
EXPECT_EQ(VD[1].Count, 2U * getProfWeight());
890887
EXPECT_EQ(VD[2].Count, 1U * getProfWeight());
891-
EXPECT_EQ(TotalC, 6U * getProfWeight());
892888

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

898894
// Second vtable site.
899895
{
900-
uint64_t TotalC;
901-
auto VD = R->getValueForSite(IPVK_VTableTarget, 1, &TotalC);
896+
auto VD = R->getValueForSite(IPVK_VTableTarget, 1);
902897

903898
EXPECT_EQ(VD[0].Count, 2U * getProfWeight());
904899
EXPECT_EQ(VD[1].Count, 1U * getProfWeight());
905-
EXPECT_EQ(TotalC, 3U * getProfWeight());
906900

907901
EXPECT_EQ(VD[0].Value, getCalleeAddress(vtable2));
908902
EXPECT_EQ(VD[1].Value, getCalleeAddress(vtable1));

0 commit comments

Comments
 (0)