Skip to content

Commit 19e593b

Browse files
committed
PGO: new -debug-info-for-profiling frontend flag
This achieves the same as clang's `-fdebug-info-for-profiling`, which emits DWARF discriminators to aid in narrowing-down which basic block corresponds to a particular instruction address. This is particularly useful for sampling-based profiling. rdar://135443278
1 parent 2b01714 commit 19e593b

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ class IRGenOptions {
504504
/// or the empty string.
505505
std::string UseSampleProfile = "";
506506

507+
/// Controls whether DWARF discriminators are added to the IR.
508+
unsigned DebugInfoForProfiling : 1;
509+
507510
/// List of backend command-line options for -embed-bitcode.
508511
std::vector<uint8_t> CmdArgs;
509512

@@ -588,7 +591,8 @@ class IRGenOptions {
588591
ColocateTypeDescriptors(true), UseRelativeProtocolWitnessTables(false),
589592
UseFragileResilientProtocolWitnesses(false), EnableHotColdSplit(false),
590593
EmitAsyncFramePushPopMetadata(false), EmitYieldOnce2AsYieldOnce(true),
591-
AsyncFramePointerAll(false), UseProfilingMarkerThunks(false), CmdArgs(),
594+
AsyncFramePointerAll(false), UseProfilingMarkerThunks(false),
595+
DebugInfoForProfiling(false), CmdArgs(),
592596
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
593597
TypeInfoFilter(TypeInfoDumpFilter::All),
594598
PlatformCCallingConvention(llvm::CallingConv::C), UseCASBackend(false),

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,10 @@ def profile_generate : Flag<["-"], "profile-generate">,
14771477
Flags<[FrontendOption, NoInteractiveOption]>,
14781478
HelpText<"Generate instrumented code to collect execution counts">;
14791479

1480+
def debug_info_for_profiling : Flag<["-"], "debug-info-for-profiling">,
1481+
Flags<[FrontendOption, NoInteractiveOption]>,
1482+
HelpText<"Emit extra debug info (DWARF discriminators) to make sampling-based profiling more accurate">;
1483+
14801484
def profile_use : CommaJoined<["-"], "profile-use=">,
14811485
Flags<[FrontendOption, NoInteractiveOption, ArgumentIsPath]>,
14821486
MetaVarName<"<profdata>">,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,6 +3180,8 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
31803180
const Arg *ProfileSampleUse = Args.getLastArg(OPT_profile_sample_use);
31813181
Opts.UseSampleProfile = ProfileSampleUse ? ProfileSampleUse->getValue() : "";
31823182

3183+
Opts.DebugInfoForProfiling |= Args.hasArg(OPT_debug_info_for_profiling);
3184+
31833185
Opts.PrintInlineTree |= Args.hasArg(OPT_print_llvm_inline_tree);
31843186
// Always producing all outputs when caching is enabled.
31853187
Opts.AlwaysCompile |= Args.hasArg(OPT_always_compile_output_files) ||

lib/IRGen/IRGen.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,22 @@ static void populatePGOOptions(std::optional<PGOOptions> &Out,
212212
/*Action=*/ PGOOptions::SampleUse,
213213
/*CSPGOAction=*/ PGOOptions::NoCSAction,
214214
/*ColdType=*/ PGOOptions::ColdFuncOpt::Default,
215-
/*DebugInfoForProfiling=*/ false
215+
/*DebugInfoForProfiling=*/ Opts.DebugInfoForProfiling
216+
);
217+
return;
218+
}
219+
220+
if (Opts.DebugInfoForProfiling) {
221+
Out = PGOOptions(
222+
/*ProfileFile=*/ "",
223+
/*CSProfileGenFile=*/ "",
224+
/*ProfileRemappingFile=*/ "",
225+
/*MemoryProfile=*/ "",
226+
/*FS=*/ nullptr,
227+
/*Action=*/ PGOOptions::NoAction,
228+
/*CSPGOAction=*/ PGOOptions::NoCSAction,
229+
/*ColdType=*/ PGOOptions::ColdFuncOpt::Default,
230+
/*DebugInfoForProfiling=*/ true
216231
);
217232
return;
218233
}

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
839839
TheCU->getProducer(), true, StringRef(), 0,
840840
RemappedASTFile, llvm::DICompileUnit::FullDebug,
841841
Signature);
842+
// NOTE: not setting DebugInfoForProfiling here
842843
DIB.finalize();
843844
}
844845
}
@@ -2504,7 +2505,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
25042505
? llvm::DICompileUnit::FullDebug
25052506
: llvm::DICompileUnit::LineTablesOnly,
25062507
/* DWOId */ 0, /* SplitDebugInlining */ true,
2507-
/* DebugInfoForProfiling */ false,
2508+
/* DebugInfoForProfiling */ Opts.DebugInfoForProfiling,
25082509
llvm::DICompileUnit::DebugNameTableKind::Default,
25092510
/* RangesBaseAddress */ false, DebugPrefixMap.remapPath(Sysroot), SDK);
25102511

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swift-frontend %s -emit-ir -g -debug-info-for-profiling -o - | %FileCheck %s
2+
3+
// CHECK: distinct !DICompileUnit(language: DW_LANG_Swift
4+
// CHECK-SAME: debugInfoForProfiling: true
5+
6+
// CHECK: !DILexicalBlockFile({{.*}} discriminator: 2)
7+
8+
public func lobster(_ n: Int) -> Bool {
9+
guard n > 0 else { fatalError("too cold!") }
10+
11+
if n > 100 {
12+
print("warm but ok")
13+
}
14+
15+
return n < 120
16+
}

0 commit comments

Comments
 (0)