Skip to content

Commit 4ec8ee8

Browse files
authored
Merge pull request #38167 from mshockwave/dev-sil-deserialize
[SIL][Frontend] Simplify debug info generation flow for SIL files
2 parents f58bf1f + d00a6cc commit 4ec8ee8

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;

lib/SILOptimizer/UtilityPasses/SILDebugInfoGenerator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#define DEBUG_TYPE "gsil-gen"
13+
#define DEBUG_TYPE "sil-based-debuginfo-gen"
1414
#include "swift/AST/SILOptions.h"
1515
#include "swift/SIL/SILPrintContext.h"
1616
#include "swift/SIL/SILModule.h"
@@ -25,12 +25,12 @@ namespace {
2525
/// A pass for generating debug info on SIL level.
2626
///
2727
/// This pass is only enabled if SILOptions::SILOutputFileNameForDebugging is
28-
/// set (i.e. if the -gsil command line option is specified).
28+
/// set (i.e. if the -sil-based-debuginfo frontend option is specified).
2929
/// The pass writes all SIL functions into one or multiple output files,
3030
/// depending on the size of the SIL. The names of the output files are derived
3131
/// from the main output file.
3232
///
33-
/// output file name = <main-output-filename>.gsil_<n>.sil
33+
/// output file name = <main-output-filename>.sil_dbg_<n>.sil
3434
///
3535
/// Where <n> is a consecutive number. The files are stored in the same
3636
/// same directory as the main output file.
@@ -104,7 +104,7 @@ class SILDebugInfoGenerator : public SILModuleTransform {
104104

105105
std::string FileName;
106106
llvm::raw_string_ostream NameOS(FileName);
107-
NameOS << FileBaseName << ".gsil_" << FileIdx++ << ".sil";
107+
NameOS << FileBaseName << ".sil_dbg_" << FileIdx++ << ".sil";
108108
NameOS.flush();
109109

110110
char *FileNameBuf = (char *)M->allocate(FileName.size() + 1, 1);

stdlib/public/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ endif()
1919
list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "-Xfrontend" "-verify-syntax-tree")
2020

2121
if(SWIFT_STDLIB_SIL_DEBUGGING)
22-
list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "-Xfrontend" "-gsil")
22+
list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "-Xfrontend" "-sil-based-debuginfo")
2323
endif()
2424

2525
# Build the runtime with -Wall to catch, e.g., uninitialized variables

test/DebugInfo/gsil.swift renamed to test/DebugInfo/sil_based_dbg.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend %s -O -gsil -Xllvm -sil-print-debuginfo -emit-ir -o %t/out.ir
2+
// RUN: %target-swift-frontend %s -O -sil-based-debuginfo -Xllvm -sil-print-debuginfo -emit-ir -o %t/out.ir
33
// RUN: %FileCheck %s < %t/out.ir
4-
// RUN: %FileCheck %s --check-prefix=CHECK_OUT_SIL < %t/out.ir.gsil_0.sil
4+
// RUN: %FileCheck %s --check-prefix=CHECK_OUT_SIL < %t/out.ir.sil_dbg_0.sil
55

66
// Second test: check that we don't crash with multi-threaded IRGen
7-
// RUN: %target-swift-frontend -c %s %S/Inputs/testclass.swift -wmo -O -num-threads 1 -gsil -o %t/gsil.o -o %t/testclass.o
7+
// RUN: %target-swift-frontend -c %s %S/Inputs/testclass.swift -wmo -O -num-threads 1 -sil-based-debuginfo -o %t/sil_based_dbg.o -o %t/testclass.o
88

9-
// CHECK: !DIFile(filename: "{{.+}}gsil.swift", directory: "{{.+}}")
10-
// CHECK: [[F:![0-9]+]] = !DIFile(filename: "{{.+}}out.ir.gsil_0.sil",
9+
// CHECK: !DIFile(filename: "{{.+}}sil_based_dbg.swift", directory: "{{.+}}")
10+
// CHECK: [[F:![0-9]+]] = !DIFile(filename: "{{.+}}out.ir.sil_dbg_0.sil",
1111
// CHECK: !DISubprogram(linkageName: "$s3out6testityyF", scope: !{{[0-9]+}}, file: [[F]], line: {{[1-9][0-9]+}},
1212

1313
// CHECK_OUT_SIL: sil @$s3out6testityyF : $@convention(thin) () -> () {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Just a simple test to see whether debug info is printed to SIL
2+
// if any of the '-g<kind>' (except '-gnone') is given.
3+
4+
// RUN: %target-swift-frontend -emit-sil -gnone %s | %FileCheck --check-prefix=CHECK-GNONE %s
5+
// RUN: %target-swift-frontend -emit-sil -gline-tables-only %s | %FileCheck %s
6+
// RUN: %target-swift-frontend -emit-sil -g %s | %FileCheck %s
7+
8+
func foo(x: Int, y: Int) -> Int {
9+
// CHECK: struct_extract
10+
// CHECK-SAME: loc "{{.*}}.swift":{{[0-9]+}}:{{[0-9]+}}, scope {{[0-9]+}}
11+
// CHECK-GNONE-NOT: loc "{{.*}}.swift":{{[0-9]+}}:{{[0-9]+}}, scope {{[0-9]+}}
12+
x + y
13+
}

test/SILOptimizer/outliner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public func testOutlining() {
136136
// CHECK: %15 = function_ref @$sSS10FoundationE36_unconditionallyBridgeFromObjectiveCySSSo8NSStringCSgFZ : $@convention(method) (@guaranteed Optional<NSString>, @thin String.Type) -> @owned String
137137
// CHECK: %16 = metatype $@thin String.Type
138138
// CHECK: %17 = apply %15(%9, %16) : $@convention(method) (@guaranteed Optional<NSString>, @thin String.Type) -> @owned String
139-
// CHECK: release_value %9 : $Optional<NSString> // id: %18
139+
// CHECK: release_value %9 : $Optional<NSString>
140140
// CHECK: %19 = enum $Optional<String>, #Optional.some!enumelt, %17 : $String
141141
// CHECK: br bb3(%19 : $Optional<String>)
142142
//

utils/build-script-impl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ KNOWN_SETTINGS=(
142142

143143
## Build ...
144144
build-llvm "1" "set to 1 to build LLVM and Clang"
145-
build-sil-debugging-stdlib "0" "set to 1 to build the Swift standard library with -gsil to enable debugging and profiling on SIL level"
145+
build-sil-debugging-stdlib "0" "set to 1 to build the Swift standard library with -sil-based-debuginfo to enable debugging and profiling on SIL level"
146146
build-swift-dynamic-sdk-overlay "" "set to 1 to build dynamic variants of the Swift SDK overlay"
147147
build-swift-dynamic-stdlib "" "set to 1 to build dynamic variants of the Swift standard library"
148148
build-swift-examples "1" "set to 1 to build examples"

0 commit comments

Comments
 (0)