Skip to content

Commit d00a6cc

Browse files
committed
[SIL][Frontend] Simplify debug info generation flow for SIL files
- If any of the `-g<kind>` flag is given -- except `-gnone`, debug info will be printed into every generated SIL files. - The `-gsil` is deprecated in favor of `-sil-based-debuginfo`. The SILDebugInfoGenerator Pass now generates intermediate SIL file with name "<output file>.sil_dbg_<n>.sil". Other functionalities of that Pass remain the same.
1 parent 340c855 commit d00a6cc

File tree

16 files changed

+67
-26
lines changed

16 files changed

+67
-26
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ option(SWIFT_RUNTIME_CLOBBER_FREED_OBJECTS
374374
"${SWIFT_RUNTIME_CLOBBER_FREED_OBJECTS_default}")
375375

376376
option(SWIFT_STDLIB_SIL_DEBUGGING
377-
"Compile the Swift standard library with -gsil to enable debugging and profiling on SIL level"
377+
"Compile the Swift standard library with -sil-based-debuginfo to enable debugging and profiling on SIL level"
378378
FALSE)
379379

380380
option(SWIFT_CHECK_INCREMENTAL_COMPILATION

docs/DebuggingTheCompiler.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ benefit of all Swift developers.
2525
- [Getting CommandLine for swift stdlib from Ninja to enable dumping stdlib SIL](#getting-commandline-for-swift-stdlib-from-ninja-to-enable-dumping-stdlib-sil)
2626
- [Dumping the SIL and other Data in LLDB](#dumping-the-sil-and-other-data-in-lldb)
2727
- [Debugging and Profiling on SIL level](#debugging-and-profiling-on-sil-level)
28-
- [SIL source level profiling using -gsil](#sil-source-level-profiling-using--gsil)
28+
- [SIL source level profiling using -sil-based-debuginfo](#sil-source-level-profiling)
2929
- [ViewCFG: Regex based CFG Printer for SIL/LLVM-IR](#viewcfg-regex-based-cfg-printer-for-silllvm-ir)
3030
- [Debugging the Compiler using advanced LLDB Breakpoints](#debugging-the-compiler-using-advanced-lldb-breakpoints)
3131
- [Debugging the Compiler using LLDB Scripts](#debugging-the-compiler-using-lldb-scripts)
@@ -101,6 +101,12 @@ swiftc -emit-sil -Onone file.swift
101101
swiftc -emit-sil -O file.swift
102102
```
103103

104+
* **Debug info in SIL** To print debug info from `file.swift` in SIL:
105+
106+
```sh
107+
swiftc -g -emit-sil -O file.swift
108+
```
109+
104110
* **IRGen** To print the LLVM IR after IR generation:
105111

106112
```sh
@@ -299,12 +305,13 @@ has a `dump()` method you can call.
299305

300306
## Debugging and Profiling on SIL level
301307

302-
### SIL source level profiling using -gsil
308+
### SIL source level profiling
303309

304310
The compiler provides a way to debug and profile on SIL level. To enable SIL
305-
debugging add the front-end option -gsil together with -g. Example:
311+
debugging add the front-end option -sil-based-debuginfo together with -g.
312+
Example:
306313

307-
swiftc -g -Xfrontend -gsil -O test.swift -o a.out
314+
swiftc -g -Xfrontend -sil-based-debuginfo -O test.swift -o a.out
308315

309316
This writes the SIL after optimizations into a file and generates debug info
310317
for it. In the debugger and profiler you can then see the SIL code instead of

include/swift/AST/SILOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ class SILOptions {
145145
/// Don't generate code using partial_apply in SIL generation.
146146
bool DisableSILPartialApply = false;
147147

148-
/// The name of the SIL outputfile if compiled with SIL debugging (-gsil).
148+
/// Print debug information into the SIL file
149+
bool PrintDebugInfo = false;
150+
151+
/// The name of the SIL outputfile if compiled with SIL debugging
152+
/// (-sil-based-debuginfo).
149153
std::string SILOutputFileNameForDebugging;
150154

151155
/// If set to true, compile with the SIL Ownership Model enabled.

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,11 @@ def print_inst_counts : Flag<["-"], "print-inst-counts">,
645645
HelpText<"Before IRGen, count all the various SIL instructions. Must be used "
646646
"in conjunction with -print-stats.">;
647647

648-
def debug_on_sil : Flag<["-"], "gsil">,
648+
def debug_on_sil : Flag<["-"], "sil-based-debuginfo">,
649649
HelpText<"Write the SIL into a file and generate debug-info to debug on SIL "
650650
" level.">;
651+
def legacy_gsil : Flag<["-"], "gsil">,
652+
HelpText<"Deprecated, use '-sil-based-debuginfo' instead">;
651653

652654
def print_llvm_inline_tree : Flag<["-"], "print-llvm-inline-tree">,
653655
HelpText<"Print the LLVM inline tree.">;

include/swift/SIL/SILFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,8 @@ class SILFunction
838838
DeclCtxt = (DS ? DebugScope->Loc.getAsDeclContext() : nullptr);
839839
}
840840

841-
/// Initialize the debug scope for debug info on SIL level (-gsil).
841+
/// Initialize the debug scope for debug info on SIL level
842+
/// (-sil-based-debuginfo).
842843
void setSILDebugScope(const SILDebugScope *DS) {
843844
DebugScope = DS;
844845
}

include/swift/SIL/SILLocation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class SILLocation {
5151
/// Describes a position in a source file by explicitly storing the file name,
5252
/// line and column.
5353
///
54-
/// This is used for parsed locations from a SIL file, for "-gsil" (see
55-
/// SILDebugInfoGenerator) and for the "compiler-generated" singleton location.
54+
/// This is used for parsed locations from a SIL file, for
55+
/// "-sil-based-debuginfo" (see SILDebugInfoGenerator) and for the
56+
/// "compiler-generated" singleton location.
5657
/// In future we might also use it for de-serialized locations from a
5758
/// swiftmodule file.
5859
struct FilenameAndLocation : public SILAllocated<FilenameAndLocation> {

include/swift/SIL/SILPrintContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class SILPrintContext {
7878

7979
/// Constructor based on SILOptions.
8080
///
81-
/// DebugInfo will be set according to the -sil-print-debuginfo option.
81+
/// DebugInfo will be set according to SILOptions::PrintDebugInfo or
82+
/// the -sil-print-debuginfo option.
8283
SILPrintContext(llvm::raw_ostream &OS, const SILOptions &Opts);
8384

8485
SILPrintContext(llvm::raw_ostream &OS, bool Verbose, bool SortedSIL,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Support/LineIterator.h"
2828
#include "llvm/Support/Path.h"
2929
#include "llvm/Support/Process.h"
30+
#include "llvm/Support/WithColor.h"
3031

3132
using namespace swift;
3233
using namespace llvm::opt;
@@ -1360,6 +1361,16 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
13601361
if (const Arg *A = Args.getLastArg(OPT_save_optimization_record_path))
13611362
Opts.OptRecordFile = A->getValue();
13621363

1364+
// If any of the '-g<kind>', except '-gnone', is given,
1365+
// tell the SILPrinter to print debug info as well
1366+
if (const Arg *A = Args.getLastArg(OPT_g_Group)) {
1367+
if (!A->getOption().matches(options::OPT_gnone))
1368+
Opts.PrintDebugInfo = true;
1369+
}
1370+
1371+
if (Args.hasArg(OPT_legacy_gsil))
1372+
llvm::WithColor::warning() << "'-gsil' is deprecated, "
1373+
<< "use '-sil-based-debuginfo' instead\n";
13631374
if (Args.hasArg(OPT_debug_on_sil)) {
13641375
// Derive the name of the SIL file for debugging from
13651376
// the regular outputfile.

lib/SIL/IR/SILPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3637,7 +3637,8 @@ SILPrintContext::SILPrintContext(llvm::raw_ostream &OS, bool Verbose,
36373637

36383638
SILPrintContext::SILPrintContext(llvm::raw_ostream &OS, const SILOptions &Opts)
36393639
: OutStream(OS), Verbose(Opts.EmitVerboseSIL),
3640-
SortedSIL(Opts.EmitSortedSIL), DebugInfo(SILPrintDebugInfo),
3640+
SortedSIL(Opts.EmitSortedSIL),
3641+
DebugInfo(Opts.PrintDebugInfo || SILPrintDebugInfo),
36413642
PrintFullConvention(Opts.PrintFullConvention) {}
36423643

36433644
SILPrintContext::SILPrintContext(llvm::raw_ostream &OS, bool Verbose,

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ SILPassPipelinePlan::getPerformancePassPipeline(const SILOptions &Options) {
832832

833833
addLastChanceOptPassPipeline(P);
834834

835-
// Has only an effect if the -gsil option is specified.
835+
// Has only an effect if the -sil-based-debuginfo option is specified.
836836
addSILDebugInfoGeneratorPipeline(P);
837837

838838
// Call the CFG viewer.
@@ -883,7 +883,7 @@ SILPassPipelinePlan::getOnonePassPipeline(const SILOptions &Options) {
883883
// Create pre-specializations.
884884
P.addOnonePrespecializations();
885885

886-
// Has only an effect if the -gsil option is specified.
886+
// Has only an effect if the -sil-based-debuginfo option is specified.
887887
P.addSILDebugInfoGenerator();
888888

889889
return P;

0 commit comments

Comments
 (0)