Skip to content

Commit 7e81fd6

Browse files
committed
[docs] Document the lost variables statistics option
1 parent 59d0bd4 commit 7e81fd6

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

docs/DebuggingTheCompiler.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ with the proper attributes to ensure they'll be available in the debugger. In
302302
particular, if you see `SWIFT_DEBUG_DUMP` in a class declaration, that class
303303
has a `dump()` method you can call.
304304

305+
### Pass statistics
306+
307+
There are options to output a lot of different statistics, including about
308+
SIL passes. More information is available in
309+
[Compiler Performance](CompilerPerformance.md) for the unified statistics, and
310+
[Optimizer Counter Analysis](OptimizerCountersAnalysis.md) for pass counters.
311+
305312
## Debugging and Profiling on SIL level
306313

307314
### SIL source level profiling

docs/HowToUpdateDebugInfo.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,18 @@ debug_value %1 : $Int, var, name "pair", type $Pair, expr op_fragment:#Pair.a //
255255

256256
## Rules of thumb
257257
- Optimization passes may never drop a variable entirely. If a variable is
258-
entirely optimized away, an `undef` debug value should still be kept.
258+
entirely optimized away, an `undef` debug value should still be kept. The only
259+
exception is an unreachable function or scope, which is entirely removed.
259260
- A `debug_value` must always describe a correct value for that source variable
260261
at that source location. If a value is only correct on some paths through that
261262
instruction, it must be replaced with `undef`. Debug info never speculates.
262263
- When a SIL instruction is deleted, call salvageDebugInfo(). It will try to
263264
capture the effect of the deleted instruction in a debug expression, so the
264265
location can be preserved. You can also use an `InstructionDeleter` which will
265266
automatically call `salvageDebugInfo`.
267+
268+
> [!Tip]
269+
> To detect when a pass drops a variable, you can use the
270+
> `-Xllvm -sil-stats-lost-variables` to print when a variable is lost by a pass.
271+
> More information about this option is available in
272+
> [Optimizer Counter Analysis](OptimizerCountersAnalysis.md)

docs/OptimizerCountersAnalysis.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ The following statistics can be recorded:
5454

5555
* For SILFunctions: the number of SIL basic blocks for each SILFunction, the
5656
number of SIL instructions, the number of SILInstructions of a specific
57-
kind (e.g. a number of alloc_ref instructions)
57+
kind (e.g. a number of alloc_ref instructions), the number of debug
58+
variables
5859

5960
* For SILModules: the number of SIL basic blocks in the SILModule, the number
6061
of SIL instructions, the number of SILFunctions, the number of
@@ -118,6 +119,16 @@ e.g. `-Xllvm -sil-stats-only-instructions=alloc_ref,alloc_stack`. If you need to
118119
collect stats about all kinds of SIL instructions, you can use this syntax:
119120
`-Xllvm -sil-stats-only-instructions=all`.
120121

122+
### Debug variable level counters
123+
A different type of counter is the lost debug variables counter. It is enabled
124+
by using the `-Xllvm -sil-stats-lost-variables` command-line option. It only
125+
tracks statistics about lost variables in SILFunctions. It is not enabled by
126+
any other command-line option, but can be combined with the others. It is not
127+
compatible with thresholds, it always counts lost variables. Note that it does
128+
not track the number of debug variables: it counts the number of debug variables
129+
that were present, but aren't anymore. If a variable changes location or scope,
130+
which is not allowed, it will be counted as lost.
131+
121132
## Configuring which counters changes should be recorded
122133

123134
The user has a possibility to configure a number of thresholds, which control
@@ -181,9 +192,9 @@ And for counter stats it looks like this:
181192
* `function_history` corresponds to the verbose mode of function
182193
counters collection, when changes to the SILFunction counters are logged
183194
unconditionally, without any on-line filtering.
184-
* `CounterName` is typically one of `block`, `inst`, `function`, `memory`,
185-
or `inst_instruction_name` if you collect counters for specific kinds of SIL
186-
instructions.
195+
* `CounterName` is typically one of `block`, `inst`, `function`, `memory`,
196+
`lostvars`, or `inst_instruction_name` if you collect counters for specific
197+
kinds of SIL instructions.
187198
* `Symbol` is e.g. the name of a function
188199
* `StageName` is the name of the current optimizer pipeline stage
189200
* `TransformName` is the name of the current optimizer transformation/pass
@@ -192,6 +203,14 @@ And for counter stats it looks like this:
192203
want to reproduce the result later using
193204
`-Xllvm -sil-opt-pass-count -Xllvm TransformPassNumber`
194205

206+
## Extract Lost Variables per Pass
207+
208+
For lost variables, there is a script to output a CSV with only the amount of
209+
lost variables per pass. You can then easily open the resulting CSV in Numbers
210+
to make graphs.
211+
212+
`utils/process-stats-lost-variables csv_file_with_counters > csv_aggregate`
213+
195214
## Storing the produced statistics into a database
196215

197216
To store the set of produced counters into a database, you can use the
@@ -345,4 +364,3 @@ from Counters C where C.counter = 'inst' and C.kind = 'module'
345364
group by Stage
346365
order by sum(C.Delta);
347366
```
348-

0 commit comments

Comments
 (0)