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
This commit is contained in:
Kavon Farvardin
2024-11-01 11:50:22 -07:00
parent 2b01714fba
commit 19e593bc07
6 changed files with 45 additions and 3 deletions

View File

@@ -504,6 +504,9 @@ public:
/// or the empty string.
std::string UseSampleProfile = "";
/// Controls whether DWARF discriminators are added to the IR.
unsigned DebugInfoForProfiling : 1;
/// List of backend command-line options for -embed-bitcode.
std::vector<uint8_t> CmdArgs;
@@ -588,7 +591,8 @@ public:
ColocateTypeDescriptors(true), UseRelativeProtocolWitnessTables(false),
UseFragileResilientProtocolWitnesses(false), EnableHotColdSplit(false),
EmitAsyncFramePushPopMetadata(false), EmitYieldOnce2AsYieldOnce(true),
AsyncFramePointerAll(false), UseProfilingMarkerThunks(false), CmdArgs(),
AsyncFramePointerAll(false), UseProfilingMarkerThunks(false),
DebugInfoForProfiling(false), CmdArgs(),
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
TypeInfoFilter(TypeInfoDumpFilter::All),
PlatformCCallingConvention(llvm::CallingConv::C), UseCASBackend(false),

View File

@@ -1477,6 +1477,10 @@ def profile_generate : Flag<["-"], "profile-generate">,
Flags<[FrontendOption, NoInteractiveOption]>,
HelpText<"Generate instrumented code to collect execution counts">;
def debug_info_for_profiling : Flag<["-"], "debug-info-for-profiling">,
Flags<[FrontendOption, NoInteractiveOption]>,
HelpText<"Emit extra debug info (DWARF discriminators) to make sampling-based profiling more accurate">;
def profile_use : CommaJoined<["-"], "profile-use=">,
Flags<[FrontendOption, NoInteractiveOption, ArgumentIsPath]>,
MetaVarName<"<profdata>">,

View File

@@ -3180,6 +3180,8 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
const Arg *ProfileSampleUse = Args.getLastArg(OPT_profile_sample_use);
Opts.UseSampleProfile = ProfileSampleUse ? ProfileSampleUse->getValue() : "";
Opts.DebugInfoForProfiling |= Args.hasArg(OPT_debug_info_for_profiling);
Opts.PrintInlineTree |= Args.hasArg(OPT_print_llvm_inline_tree);
// Always producing all outputs when caching is enabled.
Opts.AlwaysCompile |= Args.hasArg(OPT_always_compile_output_files) ||

View File

@@ -212,7 +212,22 @@ static void populatePGOOptions(std::optional<PGOOptions> &Out,
/*Action=*/ PGOOptions::SampleUse,
/*CSPGOAction=*/ PGOOptions::NoCSAction,
/*ColdType=*/ PGOOptions::ColdFuncOpt::Default,
/*DebugInfoForProfiling=*/ false
/*DebugInfoForProfiling=*/ Opts.DebugInfoForProfiling
);
return;
}
if (Opts.DebugInfoForProfiling) {
Out = PGOOptions(
/*ProfileFile=*/ "",
/*CSProfileGenFile=*/ "",
/*ProfileRemappingFile=*/ "",
/*MemoryProfile=*/ "",
/*FS=*/ nullptr,
/*Action=*/ PGOOptions::NoAction,
/*CSPGOAction=*/ PGOOptions::NoCSAction,
/*ColdType=*/ PGOOptions::ColdFuncOpt::Default,
/*DebugInfoForProfiling=*/ true
);
return;
}

View File

@@ -839,6 +839,7 @@ private:
TheCU->getProducer(), true, StringRef(), 0,
RemappedASTFile, llvm::DICompileUnit::FullDebug,
Signature);
// NOTE: not setting DebugInfoForProfiling here
DIB.finalize();
}
}
@@ -2504,7 +2505,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
? llvm::DICompileUnit::FullDebug
: llvm::DICompileUnit::LineTablesOnly,
/* DWOId */ 0, /* SplitDebugInlining */ true,
/* DebugInfoForProfiling */ false,
/* DebugInfoForProfiling */ Opts.DebugInfoForProfiling,
llvm::DICompileUnit::DebugNameTableKind::Default,
/* RangesBaseAddress */ false, DebugPrefixMap.remapPath(Sysroot), SDK);

View File

@@ -0,0 +1,16 @@
// RUN: %target-swift-frontend %s -emit-ir -g -debug-info-for-profiling -o - | %FileCheck %s
// CHECK: distinct !DICompileUnit(language: DW_LANG_Swift
// CHECK-SAME: debugInfoForProfiling: true
// CHECK: !DILexicalBlockFile({{.*}} discriminator: 2)
public func lobster(_ n: Int) -> Bool {
guard n > 0 else { fatalError("too cold!") }
if n > 100 {
print("warm but ok")
}
return n < 120
}