Skip to content

Commit 08f49a4

Browse files
committed
[DebugInfo] Fix wrong scope being used in AllocBoxToStack
(cherry picked from commit f6232a9)
1 parent abbba09 commit 08f49a4

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

docs/DebuggingTheCompiler.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ If one builds swift using ninja and wants to dump the SIL of the
260260
stdlib using some of the SIL dumping options from the previous
261261
section, one can use the following one-liner:
262262

263-
ninja -t commands | grep swiftc | grep Swift.o | grep " -c "
263+
ninja -t commands | grep swiftc | grep 'Swift\.o'
264264

265265
This should give one a single command line that one can use for
266266
Swift.o, perfect for applying the previous sections options to.

docs/HowToUpdateDebugInfo.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ merge drop and copy locations, since all the same considerations apply. Helpers
1616
like `SILBuilderWithScope` make it easy to copy source locations when expanding
1717
SIL instructions.
1818

19+
> [!Warning]
20+
> Don't use `SILBuilderWithScope` when replacing a single instruction of type
21+
> `AllocStackInst` or `DebugValueInst`. These meta instructions are skipped,
22+
> so the wrong scope will be inferred.
23+
1924
## Variables
2025

2126
Each `debug_value` (and variable-carrying instruction) defines an update point
@@ -254,16 +259,34 @@ debug_value %1 : $Int, var, name "pair", type $Pair, expr op_fragment:#Pair.a //
254259
```
255260

256261
## Rules of thumb
257-
- Optimization passes may never drop a variable entirely. If a variable is
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.
260-
- A `debug_value` must always describe a correct value for that source variable
261-
at that source location. If a value is only correct on some paths through that
262-
instruction, it must be replaced with `undef`. Debug info never speculates.
263-
- When a SIL instruction is deleted, call salvageDebugInfo(). It will try to
264-
capture the effect of the deleted instruction in a debug expression, so the
265-
location can be preserved. You can also use an `InstructionDeleter` which will
266-
automatically call `salvageDebugInfo`.
262+
263+
### Correctness
264+
A `debug_value` must always describe a correct value for that source variable
265+
at that source location. If a value is only correct on some paths through that
266+
instruction, it must be replaced with `undef`. Debug info never speculates.
267+
268+
### Don't drop debug info
269+
270+
Optimization passes may never drop a variable entirely. If a variable is
271+
entirely optimized away, an `undef` debug value should still be kept. The only
272+
exception is when the variable is in an unreachable function or scope, where it
273+
can be removed with the rest of the instructions.
274+
275+
### Instruction Deletion
276+
277+
When a SIL instruction is deleted, call `salvageDebugInfo`. It will try to
278+
capture the effect of the deleted instruction in a debug expression, so the
279+
location can be preserved.
280+
281+
Alternatively, you can use an `InstructionDeleter`, which will automatically
282+
call `salvageDebugInfo`.
283+
284+
If the debug info cannot be salvaged by `salvageDebugInfo`, and the pass has a
285+
special knowledge of the value, the pass can directly replace the debug value
286+
with the known value.
287+
288+
If an instruction is being replaced by another, use `replaceAllUsesWith`. It
289+
will also update debug values to use the new instruction.
267290

268291
> [!Tip]
269292
> To detect when a pass drops a variable, you can use the

lib/SIL/IR/SILInstruction.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,9 +1475,8 @@ bool SILInstruction::mayTrap() const {
14751475
}
14761476

14771477
bool SILInstruction::isMetaInstruction() const {
1478-
// Every instruction that implements getVarInfo() should be in this list.
1478+
// Every instruction that doesn't generate code should be in this list.
14791479
switch (getKind()) {
1480-
case SILInstructionKind::AllocBoxInst:
14811480
case SILInstructionKind::AllocStackInst:
14821481
case SILInstructionKind::DebugValueInst:
14831482
return true;

0 commit comments

Comments
 (0)