Skip to content

Commit 1e9668e

Browse files
committed
add -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 da65ed4 commit 1e9668e

File tree

7 files changed

+32
-14
lines changed

7 files changed

+32
-14
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
@@ -1472,6 +1472,10 @@ def profile_generate : Flag<["-"], "profile-generate">,
14721472
Flags<[FrontendOption, NoInteractiveOption]>,
14731473
HelpText<"Generate instrumented code to collect execution counts">;
14741474

1475+
def debug_info_for_profiling : Flag<["-"], "debug-info-for-profiling">,
1476+
Flags<[FrontendOption, NoInteractiveOption]>,
1477+
HelpText<"Emit extra debug info to make sample profile more accurate">;
1478+
14751479
def profile_use : CommaJoined<["-"], "profile-use=">,
14761480
Flags<[FrontendOption, NoInteractiveOption, ArgumentIsPath]>,
14771481
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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,21 @@ static void populatePGOOptions(std::optional<PGOOptions> &Out,
211211
/*FS=*/ llvm::vfs::getRealFileSystem(), // TODO: is this fine?
212212
/*Action=*/ PGOOptions::SampleUse,
213213
/*CSPGOAction=*/ PGOOptions::NoCSAction,
214-
/*DebugInfoForProfiling=*/ false
214+
/*DebugInfoForProfiling=*/ Opts.DebugInfoForProfiling
215+
);
216+
return;
217+
}
218+
219+
if (Opts.DebugInfoForProfiling) {
220+
Out = PGOOptions(
221+
/*ProfileFile=*/ "",
222+
/*CSProfileGenFile=*/ "",
223+
/*ProfileRemappingFile=*/ "",
224+
/*MemoryProfile=*/ "",
225+
/*FS=*/ nullptr,
226+
/*Action=*/ PGOOptions::NoAction,
227+
/*CSPGOAction=*/ PGOOptions::NoCSAction,
228+
/*DebugInfoForProfiling=*/ true
215229
);
216230
return;
217231
}
@@ -239,14 +253,15 @@ void swift::performLLVMOptimizations(const IRGenOptions &Opts,
239253
PTO.LoopVectorization = true;
240254
PTO.SLPVectorization = true;
241255
PTO.MergeFunctions = true;
242-
populatePGOOptions(PGOOpt, Opts);
243256
// Splitting trades code size to enhance memory locality, avoid in -Osize.
244257
DoHotColdSplit = Opts.EnableHotColdSplit && !Opts.optimizeForSize();
245258
level = llvm::OptimizationLevel::Os;
246259
} else {
247260
level = llvm::OptimizationLevel::O0;
248261
}
249262

263+
populatePGOOptions(PGOOpt, Opts);
264+
250265
LoopAnalysisManager LAM;
251266
FunctionAnalysisManager FAM;
252267
CGSCCAnalysisManager CGAM;

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

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,9 +1939,7 @@ IRGenSILFunction::IRGenSILFunction(IRGenModule &IGM, SILFunction *f)
19391939
// LLVM doesn't have an attribute for -O
19401940
}
19411941

1942-
const bool optimizing = IGM.IRGen.Opts.shouldOptimize() &&
1943-
!IGM.IRGen.Opts.DisableLLVMOptzns;
1944-
if (optimizing && !IGM.IRGen.Opts.UseSampleProfile.empty()) {
1942+
if (!IGM.IRGen.Opts.UseSampleProfile.empty()) {
19451943
// This attribute helps in LTO situations: https://reviews.llvm.org/D79959
19461944
CurFn->addFnAttr("use-sample-profile");
19471945
}

test/Profiler/samplepgo.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@
1919

2020
// ----------------------------------------
2121
// Now, test cases where there there should not be any profile metadata,
22-
// such as when no optimizations are requested, or no data is provided
23-
24-
// RUN: %target-swift-frontend %t/program.swift -module-name test -emit-ir \
25-
// RUN: -profile-sample-use=%t/profile.txt -o %t/no-optimizations.ll
26-
27-
// RUN: %FileCheck --check-prefix CHECK-NODATA %s < %t/no-optimizations.ll
28-
22+
// such as when no data is provided
2923

3024
// RUN: %target-swift-frontend %t/program.swift -module-name test -emit-ir \
3125
// RUN: -O -o %t/no-data.ll

0 commit comments

Comments
 (0)