@@ -16,6 +16,11 @@ merge drop and copy locations, since all the same considerations apply. Helpers
16
16
like ` SILBuilderWithScope ` make it easy to copy source locations when expanding
17
17
SIL instructions.
18
18
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
+
19
24
## Variables
20
25
21
26
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 //
254
259
```
255
260
256
261
## 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.
267
290
268
291
> [ !Tip]
269
292
> To detect when a pass drops a variable, you can use the
0 commit comments