[llvm] [ProfileData] Remove SampleSorter (NFC) (PR #215941)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 12 21:09:43 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-pgo
Author: Kazu Hirata (kazutakahirata)
<details>
<summary>Changes</summary>
This patch removes SampleSorter in favor of direct iteration over
BodySamples and CallsiteSamples.
Originally, BodySamples and CallsiteSamples used DenseMap. Because
DenseMap does not guarantee iteration order, SampleSorter was
introduced to sort samples by LineLocation on demand when printing or
writing text profiles.
In December 2015, commit 10cf124bb920 switched BodySamples and
CallsiteSamples from DenseMap to std::map to reduce memory usage, but
SampleSorter was retained. Since std::map is already ordered by
LineLocation, SampleSorter is redundant.
Assisted-by: Antigravity
---
Full diff: https://github.com/llvm/llvm-project/pull/215941.diff
3 Files Affected:
- (modified) llvm/include/llvm/ProfileData/SampleProf.h (-23)
- (modified) llvm/lib/ProfileData/SampleProf.cpp (+3-9)
- (modified) llvm/lib/ProfileData/SampleProfWriter.cpp (+2-9)
``````````diff
diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h
index 4e484a401fd0a..f35ad8c0ad060 100644
--- a/llvm/include/llvm/ProfileData/SampleProf.h
+++ b/llvm/include/llvm/ProfileData/SampleProf.h
@@ -1535,29 +1535,6 @@ LLVM_ABI void
sortFuncProfiles(const SampleProfileMap &ProfileMap,
std::vector<NameFunctionSamples> &SortedProfiles);
-/// Sort a LocationT->SampleT map by LocationT.
-///
-/// It produces a sorted list of <LocationT, SampleT> records by ascending
-/// order of LocationT.
-template <class LocationT, class SampleT> class SampleSorter {
-public:
- using SamplesWithLoc = std::pair<const LocationT, SampleT>;
- using SamplesWithLocList = SmallVector<const SamplesWithLoc *, 20>;
-
- SampleSorter(const std::map<LocationT, SampleT> &Samples) {
- for (const auto &I : Samples)
- V.push_back(&I);
- llvm::stable_sort(V, [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
- return A->first < B->first;
- });
- }
-
- const SamplesWithLocList &get() const { return V; }
-
-private:
- SamplesWithLocList V;
-};
-
/// SampleContextTrimmer impelements helper functions to trim, merge cold
/// context profiles. It also supports context profile canonicalization to make
/// sure ProfileMap's key is consistent with FunctionSample's name/context.
diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp
index d560c30f17617..22c7f8365ec38 100644
--- a/llvm/lib/ProfileData/SampleProf.cpp
+++ b/llvm/lib/ProfileData/SampleProf.cpp
@@ -220,11 +220,9 @@ void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
OS.indent(Indent);
if (!BodySamples.empty()) {
OS << "Samples collected in the function's body {\n";
- SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
- for (const auto &SI : SortedBodySamples.get()) {
+ for (const auto &[Loc, Record] : BodySamples) {
OS.indent(Indent + 2);
- const auto &Loc = SI->first;
- OS << SI->first << ": " << SI->second;
+ OS << Loc << ": " << Record;
if (const TypeCountMap *TypeCountMap =
this->findCallsiteTypeSamplesAt(Loc)) {
OS.indent(Indent + 2);
@@ -240,11 +238,7 @@ void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
OS.indent(Indent);
if (!CallsiteSamples.empty()) {
OS << "Samples collected in inlined callsites {\n";
- SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
- CallsiteSamples);
- for (const auto *Element : SortedCallsiteSamples.get()) {
- // Element is a pointer to a pair of LineLocation and FunctionSamplesMap.
- const auto &[Loc, FunctionSampleMap] = *Element;
+ for (const auto &[Loc, FunctionSampleMap] : CallsiteSamples) {
for (const FunctionSamples &FuncSample :
llvm::make_second_range(FunctionSampleMap)) {
OS.indent(Indent + 2);
diff --git a/llvm/lib/ProfileData/SampleProfWriter.cpp b/llvm/lib/ProfileData/SampleProfWriter.cpp
index 5c6ca653b0cd9..72403b80d10e9 100644
--- a/llvm/lib/ProfileData/SampleProfWriter.cpp
+++ b/llvm/lib/ProfileData/SampleProfWriter.cpp
@@ -806,10 +806,7 @@ std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
OS << "\n";
LineCount++;
- SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
- for (const auto &I : SortedSamples.get()) {
- LineLocation Loc = I->first;
- const SampleRecord &Sample = I->second;
+ for (const auto &[Loc, Sample] : S.getBodySamples()) {
OS.indent(Indent + 1);
Loc.print(OS);
OS << ": " << Sample.getSamples();
@@ -833,12 +830,8 @@ std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
}
}
- SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
- S.getCallsiteSamples());
Indent += 1;
- for (const auto *Element : SortedCallsiteSamples.get()) {
- // Element is a pointer to a pair of LineLocation and FunctionSamplesMap.
- const auto &[Loc, FunctionSamplesMap] = *Element;
+ for (const auto &[Loc, FunctionSamplesMap] : S.getCallsiteSamples()) {
for (const FunctionSamples &CalleeSamples :
make_second_range(FunctionSamplesMap)) {
OS.indent(Indent);
``````````
</details>
https://github.com/llvm/llvm-project/pull/215941
More information about the llvm-commits
mailing list