Skip to content

Commit 3144073

Browse files
[ProfileData] Use std::vector for ValueData (NFC) (llvm#95194)
This patch changes the type of ValueData to std::vector<InstrProfValueData> so that, in a follow-up patch, we can teach getValueForSite to return ArrayRef<InstrProfValueData>. Currently, a typical traversal over the value data looks like: uint32_t NV = Func.getNumValueDataForSite(VK, I); std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, I); for (uint32_t V = 0; V < NV; V++) Do something with VD[V].Value and/or VD[V].Count; Note that we need to call getNumValueDataForSite and getValueForSite separately. If getValueForSite returns ArrayRef<InstrProfValueData> in the future, then we'll be able to do something like: for (const auto &V : Func.getValueForSite(VK, I)) Do something with V.Value and/or V.Count; If ArrayRef<InstrProfValueData> directly points to ValueData, then getValueForSite won't need to allocate memory with std::make_unique. Now, switching to std::vector requires us to update several places: - sortByTargetValues switches to llvm::sort because we don't need to worry about sort stability. - sortByCount retains sort stability because std::list::sort also performs stable sort. - merge builds another array and move it back to ValueData to avoid a potential quadratic behavior with std::vector::insert into the middle of a vector.
1 parent af0d712 commit 3144073

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ struct OverlapFuncFilters {
795795

796796
struct InstrProfValueSiteRecord {
797797
/// Value profiling data pairs at a given value site.
798-
std::list<InstrProfValueData> ValueData;
798+
std::vector<InstrProfValueData> ValueData;
799799

800800
InstrProfValueSiteRecord() = default;
801801
template <class InputIterator>
@@ -804,10 +804,10 @@ struct InstrProfValueSiteRecord {
804804

805805
/// Sort ValueData ascending by Value
806806
void sortByTargetValues() {
807-
ValueData.sort(
808-
[](const InstrProfValueData &left, const InstrProfValueData &right) {
809-
return left.Value < right.Value;
810-
});
807+
llvm::sort(ValueData,
808+
[](const InstrProfValueData &L, const InstrProfValueData &R) {
809+
return L.Value < R.Value;
810+
});
811811
}
812812
/// Sort ValueData Descending by Count
813813
inline void sortByCount();
@@ -1093,9 +1093,9 @@ void InstrProfRecord::reserveSites(uint32_t ValueKind, uint32_t NumValueSites) {
10931093
#include "llvm/ProfileData/InstrProfData.inc"
10941094

10951095
void InstrProfValueSiteRecord::sortByCount() {
1096-
ValueData.sort(
1097-
[](const InstrProfValueData &left, const InstrProfValueData &right) {
1098-
return left.Count > right.Count;
1096+
llvm::stable_sort(
1097+
ValueData, [](const InstrProfValueData &L, const InstrProfValueData &R) {
1098+
return L.Count > R.Count;
10991099
});
11001100
// Now truncate
11011101
size_t max_s = INSTR_PROF_MAX_NUM_VAL_PER_SITE;

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,19 +847,26 @@ void InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
847847
Input.sortByTargetValues();
848848
auto I = ValueData.begin();
849849
auto IE = ValueData.end();
850+
std::vector<InstrProfValueData> Merged;
851+
Merged.reserve(std::max(ValueData.size(), Input.ValueData.size()));
850852
for (const InstrProfValueData &J : Input.ValueData) {
851-
while (I != IE && I->Value < J.Value)
853+
while (I != IE && I->Value < J.Value) {
854+
Merged.push_back(*I);
852855
++I;
856+
}
853857
if (I != IE && I->Value == J.Value) {
854858
bool Overflowed;
855859
I->Count = SaturatingMultiplyAdd(J.Count, Weight, I->Count, &Overflowed);
856860
if (Overflowed)
857861
Warn(instrprof_error::counter_overflow);
862+
Merged.push_back(*I);
858863
++I;
859864
continue;
860865
}
861-
ValueData.insert(I, J);
866+
Merged.push_back(J);
862867
}
868+
Merged.insert(Merged.end(), I, IE);
869+
ValueData = std::move(Merged);
863870
}
864871

865872
void InstrProfValueSiteRecord::scale(uint64_t N, uint64_t D,

0 commit comments

Comments
 (0)