Skip to content

Commit 52c9e02

Browse files
committed
Address comments
1 parent 2bd4d3b commit 52c9e02

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

llvm/include/llvm/ProfileData/InstrProfWriter.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,20 @@ class InstrProfWriter {
7878
// Whether to serialize the full schema.
7979
bool MemProfFullSchema;
8080

81+
// Whether to generated random memprof hotness for testing.
82+
bool MemprofGenerateRandomHotness;
83+
8184
public:
85+
// For memprof testing, random hotness can be assigned to the contexts if
86+
// MemprofGenerateRandomHotness is enabled. The random seed can be either
87+
// provided by MemprofGenerateRandomHotnessSeed, or if that is 0, one will be
88+
// generated in the writer using the current time.
8289
InstrProfWriter(
8390
bool Sparse = false, uint64_t TemporalProfTraceReservoirSize = 0,
8491
uint64_t MaxTemporalProfTraceLength = 0, bool WritePrevVersion = false,
8592
memprof::IndexedVersion MemProfVersionRequested = memprof::Version0,
86-
bool MemProfFullSchema = false);
93+
bool MemProfFullSchema = false, bool MemprofGenerateRandomHotness = false,
94+
unsigned MemprofGenerateRandomHotnessSeed = 0);
8795
~InstrProfWriter();
8896

8997
StringMap<ProfilingData> &getProfileData() { return FunctionData; }

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@
3737

3838
using namespace llvm;
3939

40-
static cl::opt<bool>
41-
MemprofGenerateRandomHotness("memprof-random-hotness", cl::init(false),
42-
cl::Hidden,
43-
cl::desc("Generate random hotness values"));
44-
static cl::opt<unsigned> MemprofGenerateRandomHotnessSeed(
45-
"memprof-random-hotness-seed", cl::init(0), cl::Hidden,
46-
cl::desc("Random hotness seed to use (0 to generate new seed)"));
47-
4840
// A struct to define how the data stream should be patched. For Indexed
4941
// profiling, only uint64_t data type is needed.
5042
struct PatchItem {
@@ -193,13 +185,16 @@ class InstrProfRecordWriterTrait {
193185
InstrProfWriter::InstrProfWriter(
194186
bool Sparse, uint64_t TemporalProfTraceReservoirSize,
195187
uint64_t MaxTemporalProfTraceLength, bool WritePrevVersion,
196-
memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema)
188+
memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema,
189+
bool MemprofGenerateRandomHotness,
190+
unsigned MemprofGenerateRandomHotnessSeed)
197191
: Sparse(Sparse), MaxTemporalProfTraceLength(MaxTemporalProfTraceLength),
198192
TemporalProfTraceReservoirSize(TemporalProfTraceReservoirSize),
199193
InfoObj(new InstrProfRecordWriterTrait()),
200194
WritePrevVersion(WritePrevVersion),
201195
MemProfVersionRequested(MemProfVersionRequested),
202-
MemProfFullSchema(MemProfFullSchema) {
196+
MemProfFullSchema(MemProfFullSchema),
197+
MemprofGenerateRandomHotness(MemprofGenerateRandomHotness) {
203198
// Set up the random number seed if requested.
204199
if (MemprofGenerateRandomHotness) {
205200
unsigned seed = MemprofGenerateRandomHotnessSeed
@@ -297,19 +292,16 @@ void InstrProfWriter::addMemProfRecord(
297292
// See the logic in getAllocType() in Analysis/MemoryProfileInfo.cpp.
298293
if (MemprofGenerateRandomHotness) {
299294
for (auto &Alloc : NewRecord.AllocSites) {
300-
uint64_t NewTLAD = 0;
295+
// To get a not cold context, set the lifetime access density to the
296+
// maximum value and the lifetime to 0.
297+
uint64_t NewTLAD = std::numeric_limits<uint64_t>::max();
301298
uint64_t NewTL = 0;
302299
bool IsCold = std::rand() % 2;
303300
if (IsCold) {
304301
// To get a cold context, set the lifetime access density to 0 and the
305302
// lifetime to the maximum value.
306303
NewTLAD = 0;
307304
NewTL = std::numeric_limits<uint64_t>::max();
308-
} else {
309-
// To get a not cold context, set the lifetime access density to the
310-
// maximum value and the lifetime to 0.
311-
NewTLAD = std::numeric_limits<uint64_t>::max();
312-
NewTL = 0;
313305
}
314306
Alloc.Info.setTotalLifetimeAccessDensity(NewTLAD);
315307
Alloc.Info.setTotalLifetime(NewTL);

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,15 @@ cl::opt<bool> MemProfFullSchema(
342342
"memprof-full-schema", cl::Hidden, cl::sub(MergeSubcommand),
343343
cl::desc("Use the full schema for serialization"), cl::init(false));
344344

345+
static cl::opt<bool>
346+
MemprofGenerateRandomHotness("memprof-random-hotness", cl::init(false),
347+
cl::Hidden, cl::sub(MergeSubcommand),
348+
cl::desc("Generate random hotness values"));
349+
static cl::opt<unsigned> MemprofGenerateRandomHotnessSeed(
350+
"memprof-random-hotness-seed", cl::init(0), cl::Hidden,
351+
cl::sub(MergeSubcommand),
352+
cl::desc("Random hotness seed to use (0 to generate new seed)"));
353+
345354
// Options specific to overlap subcommand.
346355
cl::opt<std::string> BaseFilename(cl::Positional, cl::Required,
347356
cl::desc("<base profile file>"),
@@ -641,7 +650,8 @@ struct WriterContext {
641650
SmallSet<instrprof_error, 4> &WriterErrorCodes,
642651
uint64_t ReservoirSize = 0, uint64_t MaxTraceLength = 0)
643652
: Writer(IsSparse, ReservoirSize, MaxTraceLength, DoWritePrevVersion,
644-
MemProfVersionRequested, MemProfFullSchema),
653+
MemProfVersionRequested, MemProfFullSchema,
654+
MemprofGenerateRandomHotness, MemprofGenerateRandomHotnessSeed),
645655
ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
646656
};
647657

0 commit comments

Comments
 (0)