Skip to content

Commit fd97012

Browse files
committed
Add CtxSplitLayout support for text mode (for manual inspection only)
Fixed wordings refering to this flag, Previously it is described as "samples with/out context", which confuses with CSSPGO. Now they are called "samples/functions with/out inlined functions"
1 parent 551f8c1 commit fd97012

File tree

6 files changed

+61
-18
lines changed

6 files changed

+61
-18
lines changed

llvm/docs/CommandGuide/llvm-profdata.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ OPTIONS
162162
coverage for the optimized target. This option can only be used with
163163
sample-based profile in extbinary format.
164164

165-
.. option:: --split_layout=[true|false]
165+
.. option:: --split-layout=[true|false]
166166

167167
Split the profile data section to two with one containing sample profiles with
168168
inlined functions and the other not. This option can only be used with

llvm/include/llvm/ProfileData/SampleProfWriter.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ namespace sampleprof {
2828

2929
enum SectionLayout {
3030
DefaultLayout,
31-
// The layout splits profile with context information from profile without
32-
// context information. When Thinlto is enabled, ThinLTO postlink phase only
33-
// has to load profile with context information and can skip the other part.
31+
// The layout splits profile with inlined functions from profile without
32+
// inlined functions. When Thinlto is enabled, ThinLTO postlink phase only
33+
// has to load profile with inlined functions and can skip the other part.
3434
CtxSplitLayout,
3535
NumOfLayout,
3636
};
@@ -128,7 +128,7 @@ class SampleProfileWriter {
128128
virtual void setToCompressAllSections() {}
129129
virtual void setUseMD5() {}
130130
virtual void setPartialProfile() {}
131-
virtual void resetSecLayout(SectionLayout SL) {}
131+
virtual void setUseCtxSplitLayout() {}
132132

133133
protected:
134134
SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
@@ -176,12 +176,22 @@ class SampleProfileWriterText : public SampleProfileWriter {
176176
return sampleprof_error::success;
177177
}
178178

179+
void setUseCtxSplitLayout() override {
180+
MarkFlatProfiles = true;
181+
}
182+
179183
private:
180184
/// Indent level to use when writing.
181185
///
182186
/// This is used when printing inlined callees.
183187
unsigned Indent = 0;
184188

189+
/// If set, writes metadata "!Flat" to functions without inlined functions.
190+
/// This flag is for manual inspection only, it has no effect for the profile
191+
/// reader because a text sample profile is read sequentially and functions
192+
/// cannot be skipped.
193+
bool MarkFlatProfiles = false;
194+
185195
friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
186196
SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
187197
SampleProfileFormat Format);
@@ -242,11 +252,11 @@ const std::array<SmallVector<SecHdrTableEntry, 8>, NumOfLayout>
242252
// CtxSplitLayout
243253
SmallVector<SecHdrTableEntry, 8>({{SecProfSummary, 0, 0, 0, 0},
244254
{SecNameTable, 0, 0, 0, 0},
245-
// profile with context
255+
// profile with inlined functions
246256
// for next two sections
247257
{SecFuncOffsetTable, 0, 0, 0, 0},
248258
{SecLBRProfile, 0, 0, 0, 0},
249-
// profile without context
259+
// profile without inlined functions
250260
// for next two sections
251261
{SecFuncOffsetTable, 0, 0, 0, 0},
252262
{SecLBRProfile, 0, 0, 0, 0},
@@ -283,7 +293,11 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
283293
ProfSymList = PSL;
284294
};
285295

286-
void resetSecLayout(SectionLayout SL) override {
296+
void setUseCtxSplitLayout() override {
297+
resetSecLayout(SectionLayout::CtxSplitLayout);
298+
}
299+
300+
void resetSecLayout(SectionLayout SL) {
287301
verifySecLayout(SL);
288302
#ifndef NDEBUG
289303
// Make sure resetSecLayout is called before any flag setting.

llvm/lib/ProfileData/SampleProfReader.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ static bool ParseLine(const StringRef &Input, LineType &LineTy, uint32_t &Depth,
223223

224224
if (Input[Depth] == '!') {
225225
LineTy = LineType::Metadata;
226+
// This metadata is only for manual inspection only. We already created a
227+
// FunctionSamples and put it in the profile map, so there is no point
228+
// to skip profiles even they have no use for ThinLTO.
229+
if (Input == StringRef(" !Flat"))
230+
return true;
226231
return parseMetadata(Input.substr(Depth), FunctionHash, Attributes);
227232
}
228233

@@ -1026,7 +1031,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
10261031
if (!Entry.Size)
10271032
continue;
10281033

1029-
// Skip sections without context when SkipFlatProf is true.
1034+
// Skip sections without inlined functions when SkipFlatProf is true.
10301035
if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
10311036
continue;
10321037

llvm/lib/ProfileData/SampleProfWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,9 @@ std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
624624
LineCount++;
625625
}
626626

627+
if (Indent == 0 && MarkFlatProfiles && S.getCallsiteSamples().size() == 0)
628+
OS << " !Flat\n";
629+
627630
return sampleprof_error::success;
628631
}
629632

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
RUN: llvm-profdata merge --sample --extbinary --split_layout %p/Inputs/sample-profile.proftext -o %t-output
1+
RUN: llvm-profdata merge --sample --extbinary --split-layout %p/Inputs/sample-profile.proftext -o %t-output
22
RUN: diff %t-output %p/Inputs/split-layout.profdata
3+
4+
RUN: llvm-profdata merge --sample --text --split-layout %t-output | FileCheck %s
5+
CHECK: main:184019:0
6+
CHECK-NEXT: 4: 534
7+
CHECK-NEXT: 4.2: 534
8+
CHECK-NEXT: 5: 1075
9+
CHECK-NEXT: 5.1: 1075
10+
CHECK-NEXT: 6: 2080
11+
CHECK-NEXT: 7: 534
12+
CHECK-NEXT: 9: 2064 _Z3bari:1471 _Z3fooi:631
13+
CHECK-NEXT: 10: inline1:1000
14+
CHECK-NEXT: 1: 1000
15+
CHECK-NEXT: 10: inline2:2000
16+
CHECK-NEXT: 1: 2000
17+
CHECK-NEXT: _Z3bari:20301:1437
18+
CHECK-NEXT: 1: 1437
19+
CHECK-NEXT: !Flat
20+
CHECK-NEXT: _Z3fooi:7711:610
21+
CHECK-NEXT: 1: 610
22+
CHECK-NEXT: !Flat

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ cl::opt<bool> GenPartialProfile(
208208
cl::sub(MergeSubcommand),
209209
cl::desc("Generate a partial profile (only meaningful for -extbinary)"));
210210
cl::opt<bool> SplitLayout(
211-
"split_layout", cl::init(false), cl::Hidden,
211+
"split-layout", cl::init(false), cl::Hidden,
212212
cl::sub(MergeSubcommand),
213213
cl::desc("Split the profile to two sections with one containing sample "
214-
"profiles with inlined functions and the another not (only "
214+
"profiles with inlined functions and the other without (only "
215215
"meaningful for -extbinary)"));
216216
cl::opt<std::string> SupplInstrWithSample(
217217
"supplement-instr-with-sample", cl::init(""), cl::Hidden,
@@ -1473,6 +1473,13 @@ static void handleExtBinaryWriter(sampleprof::SampleProfileWriter &Writer,
14731473
sampleprof::ProfileSymbolList &WriterList,
14741474
bool CompressAllSections, bool UseMD5,
14751475
bool GenPartialProfile) {
1476+
if (SplitLayout) {
1477+
if (OutputFormat == PF_Binary)
1478+
warn("-split-layout is ignored. Specify -extbinary to enable it");
1479+
else
1480+
Writer.setUseCtxSplitLayout();
1481+
}
1482+
14761483
populateProfileSymbolList(Buffer, WriterList);
14771484
if (WriterList.size() > 0 && OutputFormat != PF_Ext_Binary)
14781485
warn("Profile Symbol list is not empty but the output format is not "
@@ -1498,12 +1505,6 @@ static void handleExtBinaryWriter(sampleprof::SampleProfileWriter &Writer,
14981505
else
14991506
Writer.setPartialProfile();
15001507
}
1501-
if (SplitLayout) {
1502-
if (OutputFormat != PF_Ext_Binary)
1503-
warn("-split-layout is ignored. Specify -extbinary to enable it");
1504-
else
1505-
Writer.resetSecLayout(SectionLayout::CtxSplitLayout);
1506-
}
15071508
}
15081509

15091510
static void mergeSampleProfile(const WeightedFileVector &Inputs,

0 commit comments

Comments
 (0)