Skip to content

Commit 4822b43

Browse files
[StaticDataLayout] Sort records before printing them in text format (#172592)
This change proposes to sort records before printing to make it more readable and easier to compare.
1 parent 5a54d0f commit 4822b43

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,12 @@ memprof::AllMemProfData IndexedMemProfReader::getAllMemProfData() const {
15541554
}
15551555
// Populate the data access profiles for yaml output.
15561556
if (DataAccessProfileData != nullptr) {
1557+
AllMemProfData.YamlifiedDataAccessProfiles.Records.reserve(
1558+
DataAccessProfileData->getRecords().size());
1559+
AllMemProfData.YamlifiedDataAccessProfiles.KnownColdSymbols.reserve(
1560+
DataAccessProfileData->getKnownColdSymbols().size());
1561+
AllMemProfData.YamlifiedDataAccessProfiles.KnownColdStrHashes.reserve(
1562+
DataAccessProfileData->getKnownColdHashes().size());
15571563
for (const auto &[SymHandleRef, RecordRef] :
15581564
DataAccessProfileData->getRecords())
15591565
AllMemProfData.YamlifiedDataAccessProfiles.Records.push_back(
@@ -1565,6 +1571,19 @@ memprof::AllMemProfData IndexedMemProfReader::getAllMemProfData() const {
15651571
for (uint64_t Hash : DataAccessProfileData->getKnownColdHashes())
15661572
AllMemProfData.YamlifiedDataAccessProfiles.KnownColdStrHashes.push_back(
15671573
Hash);
1574+
llvm::stable_sort(AllMemProfData.YamlifiedDataAccessProfiles.Records,
1575+
[](const llvm::memprof::DataAccessProfRecord &lhs,
1576+
const llvm::memprof::DataAccessProfRecord &rhs) {
1577+
return lhs.AccessCount > rhs.AccessCount;
1578+
});
1579+
llvm::stable_sort(
1580+
AllMemProfData.YamlifiedDataAccessProfiles.KnownColdSymbols,
1581+
[](const std::string &lhs, const std::string &rhs) {
1582+
return lhs < rhs;
1583+
});
1584+
llvm::stable_sort(
1585+
AllMemProfData.YamlifiedDataAccessProfiles.KnownColdStrHashes,
1586+
[](const uint64_t &lhs, const uint64_t &rhs) { return lhs < rhs; });
15681587
}
15691588
return AllMemProfData;
15701589
}

llvm/test/tools/llvm-profdata/memprof-yaml.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ HeapProfileRecords:
7676
DataAccessProfiles:
7777
SampledRecords:
7878
- Symbol: abcde
79-
AccessCount: 100
79+
AccessCount: 202
8080
Locations:
8181
- FileName: file2.h
8282
Line: 123
@@ -88,8 +88,8 @@ DataAccessProfiles:
8888
- FileName: file.cpp
8989
Line: 233
9090
KnownColdSymbols:
91-
- foo
9291
- bar
92+
- foo
9393
KnownColdStrHashes: [ 999, 1001 ]
9494
...
9595
;--- memprof-in-v3.yaml
@@ -233,7 +233,7 @@ HeapProfileRecords:
233233
DataAccessProfiles:
234234
SampledRecords:
235235
- Symbol: abcde
236-
AccessCount: 100
236+
AccessCount: 202
237237
Locations:
238238
- FileName: file2.h
239239
Line: 123
@@ -245,7 +245,7 @@ DataAccessProfiles:
245245
- FileName: file.cpp
246246
Line: 233
247247
KnownColdSymbols:
248-
- foo
249248
- bar
249+
- foo
250250
KnownColdStrHashes: [ 999, 1001 ]
251251
...

0 commit comments

Comments
 (0)