Skip to content

Commit edbbc5c

Browse files
teresajohnsonsvkeerthy
authored andcommitted
[MemProf] Summary section cleanup (NFC) (#142003)
Address post-commit review comments from PR141805. Misc cleanup but the biggest changes are moving some common utilities to new MemProfCommon files to reduce unnecessary includes.
1 parent 609546b commit edbbc5c

File tree

12 files changed

+135
-96
lines changed

12 files changed

+135
-96
lines changed

llvm/include/llvm/ProfileData/IndexedMemProfData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ Error writeMemProf(
9191
ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
9292
memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema,
9393
std::unique_ptr<memprof::DataAccessProfData> DataAccessProfileData,
94-
memprof::MemProfSummary *MemProfSum);
94+
std::unique_ptr<memprof::MemProfSummary> MemProfSum);
9595
} // namespace llvm
9696
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- MemProfCommon.h - MemProf common utilities ---------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains MemProf common utilities.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_PROFILEDATA_MEMPROFCOMMON_H
14+
#define LLVM_PROFILEDATA_MEMPROFCOMMON_H
15+
16+
#include "llvm/IR/ModuleSummaryIndex.h"
17+
18+
namespace llvm {
19+
namespace memprof {
20+
21+
struct Frame;
22+
23+
/// Return the allocation type for a given set of memory profile values.
24+
AllocationType getAllocType(uint64_t TotalLifetimeAccessDensity,
25+
uint64_t AllocCount, uint64_t TotalLifetime);
26+
27+
/// Helper to generate a single hash id for a given callstack, used for emitting
28+
/// matching statistics and useful for uniquing such statistics across modules.
29+
/// Also used to dedup contexts when computing the summary.
30+
uint64_t computeFullStackId(ArrayRef<Frame> CallStack);
31+
32+
} // namespace memprof
33+
} // namespace llvm
34+
35+
#endif // LLVM_PROFILEDATA_MEMPROFCOMMON_H

llvm/include/llvm/ProfileData/MemProfSummary.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,18 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file contains MemProf summary support and related interfaces.
9+
// This file contains MemProf summary support.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#ifndef LLVM_PROFILEDATA_MEMPROFSUMMARY_H
1414
#define LLVM_PROFILEDATA_MEMPROFSUMMARY_H
1515

16-
#include "llvm/IR/ModuleSummaryIndex.h"
1716
#include "llvm/ProfileData/InstrProf.h"
18-
#include "llvm/ProfileData/MemProf.h"
1917

2018
namespace llvm {
2119
namespace memprof {
2220

23-
/// Return the allocation type for a given set of memory profile values.
24-
AllocationType getAllocType(uint64_t TotalLifetimeAccessDensity,
25-
uint64_t AllocCount, uint64_t TotalLifetime);
26-
27-
/// Helper to generate a single hash id for a given callstack, used for emitting
28-
/// matching statistics and useful for uniquing such statistics across modules.
29-
/// Also used to dedup contexts when computing the summary.
30-
uint64_t computeFullStackId(ArrayRef<Frame> CallStack);
31-
3221
class MemProfSummary {
3322
private:
3423
/// The number of summary fields below, which is used to enable some forwards

llvm/include/llvm/ProfileData/MemProfSummaryBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class MemProfSummaryBuilder {
2626
// instances of the same allocations.
2727
DenseSet<uint64_t> Contexts;
2828

29+
// Helper called by the public raw and indexed profile addRecord interfaces.
2930
void addRecord(uint64_t, const PortableMemInfoBlock &);
3031

31-
protected:
3232
uint64_t MaxColdTotalSize = 0;
3333
uint64_t MaxWarmTotalSize = 0;
3434
uint64_t MaxHotTotalSize = 0;

llvm/lib/ProfileData/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_llvm_component_library(LLVMProfileData
88
InstrProfWriter.cpp
99
ItaniumManglingCanonicalizer.cpp
1010
MemProf.cpp
11+
MemProfCommon.cpp
1112
MemProfReader.cpp
1213
MemProfRadixTree.cpp
1314
MemProfSummary.cpp

llvm/lib/ProfileData/IndexedMemProfData.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static Error writeMemProfRadixTreeBased(
222222
memprof::IndexedVersion Version, bool MemProfFullSchema,
223223
std::unique_ptr<memprof::DataAccessProfData> DataAccessProfileData =
224224
nullptr,
225-
memprof::MemProfSummary *MemProfSum = nullptr) {
225+
std::unique_ptr<memprof::MemProfSummary> MemProfSum = nullptr) {
226226
assert((Version == memprof::Version3 || Version == memprof::Version4) &&
227227
"Unsupported version for radix tree format");
228228

@@ -303,26 +303,27 @@ static Error writeMemProfV4(
303303
ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
304304
bool MemProfFullSchema,
305305
std::unique_ptr<memprof::DataAccessProfData> DataAccessProfileData,
306-
memprof::MemProfSummary *MemProfSum) {
306+
std::unique_ptr<memprof::MemProfSummary> MemProfSum) {
307307
return writeMemProfRadixTreeBased(
308308
OS, MemProfData, memprof::Version4, MemProfFullSchema,
309-
std::move(DataAccessProfileData), MemProfSum);
309+
std::move(DataAccessProfileData), std::move(MemProfSum));
310310
}
311311

312312
// Write out the MemProf data in a requested version.
313313
Error writeMemProf(
314314
ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
315315
memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema,
316316
std::unique_ptr<memprof::DataAccessProfData> DataAccessProfileData,
317-
memprof::MemProfSummary *MemProfSum) {
317+
std::unique_ptr<memprof::MemProfSummary> MemProfSum) {
318318
switch (MemProfVersionRequested) {
319319
case memprof::Version2:
320320
return writeMemProfV2(OS, MemProfData, MemProfFullSchema);
321321
case memprof::Version3:
322322
return writeMemProfV3(OS, MemProfData, MemProfFullSchema);
323323
case memprof::Version4:
324324
return writeMemProfV4(OS, MemProfData, MemProfFullSchema,
325-
std::move(DataAccessProfileData), MemProfSum);
325+
std::move(DataAccessProfileData),
326+
std::move(MemProfSum));
326327
}
327328

328329
return make_error<InstrProfError>(

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,9 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
618618
if (static_cast<bool>(ProfileKind & InstrProfKind::MemProf)) {
619619
MemProfSectionStart = OS.tell();
620620

621-
// Get the finalized MemProf summary that was built when adding records.
622-
auto MemProfSum = MemProfSumBuilder.getSummary();
623-
624621
if (auto E = writeMemProf(
625622
OS, MemProfData, MemProfVersionRequested, MemProfFullSchema,
626-
std::move(DataAccessProfileData), MemProfSum.get()))
623+
std::move(DataAccessProfileData), MemProfSumBuilder.getSummary()))
627624
return E;
628625
}
629626

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//=-- MemProfCommon.cpp - MemProf common utilities ---------------=//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains MemProf common utilities.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/ProfileData/MemProfCommon.h"
14+
#include "llvm/ProfileData/MemProf.h"
15+
#include "llvm/Support/BLAKE3.h"
16+
#include "llvm/Support/CommandLine.h"
17+
#include "llvm/Support/HashBuilder.h"
18+
19+
using namespace llvm;
20+
using namespace llvm::memprof;
21+
22+
// Upper bound on lifetime access density (accesses per byte per lifetime sec)
23+
// for marking an allocation cold.
24+
cl::opt<float> MemProfLifetimeAccessDensityColdThreshold(
25+
"memprof-lifetime-access-density-cold-threshold", cl::init(0.05),
26+
cl::Hidden,
27+
cl::desc("The threshold the lifetime access density (accesses per byte per "
28+
"lifetime sec) must be under to consider an allocation cold"));
29+
30+
// Lower bound on lifetime to mark an allocation cold (in addition to accesses
31+
// per byte per sec above). This is to avoid pessimizing short lived objects.
32+
cl::opt<unsigned> MemProfAveLifetimeColdThreshold(
33+
"memprof-ave-lifetime-cold-threshold", cl::init(200), cl::Hidden,
34+
cl::desc("The average lifetime (s) for an allocation to be considered "
35+
"cold"));
36+
37+
// Lower bound on average lifetime accesses density (total life time access
38+
// density / alloc count) for marking an allocation hot.
39+
cl::opt<unsigned> MemProfMinAveLifetimeAccessDensityHotThreshold(
40+
"memprof-min-ave-lifetime-access-density-hot-threshold", cl::init(1000),
41+
cl::Hidden,
42+
cl::desc("The minimum TotalLifetimeAccessDensity / AllocCount for an "
43+
"allocation to be considered hot"));
44+
45+
cl::opt<bool>
46+
MemProfUseHotHints("memprof-use-hot-hints", cl::init(false), cl::Hidden,
47+
cl::desc("Enable use of hot hints (only supported for "
48+
"unambigously hot allocations)"));
49+
50+
AllocationType llvm::memprof::getAllocType(uint64_t TotalLifetimeAccessDensity,
51+
uint64_t AllocCount,
52+
uint64_t TotalLifetime) {
53+
// The access densities are multiplied by 100 to hold 2 decimal places of
54+
// precision, so need to divide by 100.
55+
if (((float)TotalLifetimeAccessDensity) / AllocCount / 100 <
56+
MemProfLifetimeAccessDensityColdThreshold
57+
// Lifetime is expected to be in ms, so convert the threshold to ms.
58+
&& ((float)TotalLifetime) / AllocCount >=
59+
MemProfAveLifetimeColdThreshold * 1000)
60+
return AllocationType::Cold;
61+
62+
// The access densities are multiplied by 100 to hold 2 decimal places of
63+
// precision, so need to divide by 100.
64+
if (MemProfUseHotHints &&
65+
((float)TotalLifetimeAccessDensity) / AllocCount / 100 >
66+
MemProfMinAveLifetimeAccessDensityHotThreshold)
67+
return AllocationType::Hot;
68+
69+
return AllocationType::NotCold;
70+
}
71+
72+
uint64_t llvm::memprof::computeFullStackId(ArrayRef<Frame> CallStack) {
73+
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
74+
HashBuilder;
75+
for (auto &F : CallStack)
76+
HashBuilder.add(F.Function, F.LineOffset, F.Column);
77+
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
78+
uint64_t Id;
79+
std::memcpy(&Id, Hash.data(), sizeof(Hash));
80+
return Id;
81+
}

llvm/lib/ProfileData/MemProfSummary.cpp

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file contains MemProf summary support and related interfaces.
9+
// This file contains MemProf summary support.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/ProfileData/MemProfSummary.h"
14-
#include "llvm/Support/BLAKE3.h"
15-
#include "llvm/Support/CommandLine.h"
16-
#include "llvm/Support/HashBuilder.h"
1714

1815
using namespace llvm;
1916
using namespace llvm::memprof;
2017

21-
// Upper bound on lifetime access density (accesses per byte per lifetime sec)
22-
// for marking an allocation cold.
23-
cl::opt<float> MemProfLifetimeAccessDensityColdThreshold(
24-
"memprof-lifetime-access-density-cold-threshold", cl::init(0.05),
25-
cl::Hidden,
26-
cl::desc("The threshold the lifetime access density (accesses per byte per "
27-
"lifetime sec) must be under to consider an allocation cold"));
28-
29-
// Lower bound on lifetime to mark an allocation cold (in addition to accesses
30-
// per byte per sec above). This is to avoid pessimizing short lived objects.
31-
cl::opt<unsigned> MemProfAveLifetimeColdThreshold(
32-
"memprof-ave-lifetime-cold-threshold", cl::init(200), cl::Hidden,
33-
cl::desc("The average lifetime (s) for an allocation to be considered "
34-
"cold"));
35-
36-
// Lower bound on average lifetime accesses density (total life time access
37-
// density / alloc count) for marking an allocation hot.
38-
cl::opt<unsigned> MemProfMinAveLifetimeAccessDensityHotThreshold(
39-
"memprof-min-ave-lifetime-access-density-hot-threshold", cl::init(1000),
40-
cl::Hidden,
41-
cl::desc("The minimum TotalLifetimeAccessDensity / AllocCount for an "
42-
"allocation to be considered hot"));
43-
44-
cl::opt<bool>
45-
MemProfUseHotHints("memprof-use-hot-hints", cl::init(false), cl::Hidden,
46-
cl::desc("Enable use of hot hints (only supported for "
47-
"unambigously hot allocations)"));
48-
49-
AllocationType llvm::memprof::getAllocType(uint64_t TotalLifetimeAccessDensity,
50-
uint64_t AllocCount,
51-
uint64_t TotalLifetime) {
52-
// The access densities are multiplied by 100 to hold 2 decimal places of
53-
// precision, so need to divide by 100.
54-
if (((float)TotalLifetimeAccessDensity) / AllocCount / 100 <
55-
MemProfLifetimeAccessDensityColdThreshold
56-
// Lifetime is expected to be in ms, so convert the threshold to ms.
57-
&& ((float)TotalLifetime) / AllocCount >=
58-
MemProfAveLifetimeColdThreshold * 1000)
59-
return AllocationType::Cold;
60-
61-
// The access densities are multiplied by 100 to hold 2 decimal places of
62-
// precision, so need to divide by 100.
63-
if (MemProfUseHotHints &&
64-
((float)TotalLifetimeAccessDensity) / AllocCount / 100 >
65-
MemProfMinAveLifetimeAccessDensityHotThreshold)
66-
return AllocationType::Hot;
67-
68-
return AllocationType::NotCold;
69-
}
70-
71-
uint64_t llvm::memprof::computeFullStackId(ArrayRef<Frame> CallStack) {
72-
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
73-
HashBuilder;
74-
for (auto &F : CallStack)
75-
HashBuilder.add(F.Function, F.LineOffset, F.Column);
76-
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
77-
uint64_t Id;
78-
std::memcpy(&Id, Hash.data(), sizeof(Hash));
79-
return Id;
80-
}
81-
8218
void MemProfSummary::printSummaryYaml(raw_ostream &OS) const {
8319
// For now emit as YAML comments, since they aren't read on input.
8420
OS << "---\n";

llvm/lib/ProfileData/MemProfSummaryBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/ProfileData/MemProfSummaryBuilder.h"
14+
#include "llvm/ProfileData/MemProfCommon.h"
1415

1516
using namespace llvm;
1617
using namespace llvm::memprof;

0 commit comments

Comments
 (0)