1=======================================================
2How to Update Debug Info: A Guide for LLVM Pass Authors
3=======================================================
4
5.. contents::
6   :local:
7
8Introduction
9============
10
11Certain kinds of code transformations can inadvertently result in a loss of
12debug info, or worse, make debug info misrepresent the state of a program.
13
14This document specifies how to correctly update debug info in various kinds of
15code transformations, and offers suggestions for how to create targeted debug
16info tests for arbitrary transformations.
17
18For more on the philosophy behind LLVM debugging information, see
19:doc:`SourceLevelDebugging`.
20
21Rules for updating debug locations
22==================================
23
24.. _WhenToPreserveLocation:
25
26When to preserve an instruction location
27----------------------------------------
28
29A transformation should preserve the debug location of an instruction if the
30instruction either remains in its basic block, or if its basic block is folded
31into a predecessor that branches unconditionally. The APIs to use are
32``IRBuilder``, or ``Instruction::setDebugLoc``.
33
34The purpose of this rule is to ensure that common block-local optimizations
35preserve the ability to set breakpoints on source locations corresponding to
36the instructions they touch. Debugging, crash logs, and SamplePGO accuracy
37would be severely impacted if that ability were lost.
38
39Examples of transformations that should follow this rule include:
40
41* Instruction scheduling. Block-local instruction reordering should not drop
42  source locations, even though this may lead to jumpy single-stepping
43  behavior.
44
45* Simple jump threading. For example, if block ``B1`` unconditionally jumps to
46  ``B2``, *and* is its unique predecessor, instructions from ``B2`` can be
47  hoisted into ``B1``. Source locations from ``B2`` should be preserved.
48
49* Peephole optimizations that replace or expand an instruction, like ``(add X
50  X) => (shl X 1)``. The location of the ``shl`` instruction should be the same
51  as the location of the ``add`` instruction.
52
53* Tail duplication. For example, if blocks ``B1`` and ``B2`` both
54  unconditionally branch to ``B3`` and ``B3`` can be folded into its
55  predecessors, source locations from ``B3`` should be preserved.
56
57Examples of transformations for which this rule *does not* apply include:
58
59* LICM. E.g., if an instruction is moved from the loop body to the preheader,
60  the rule for :ref:`dropping locations<WhenToDropLocation>` applies.
61
62In addition to the rule above, a transformation should also preserve the debug
63location of an instruction that is moved between basic blocks, if the
64destination block already contains an instruction with an identical debug
65location.
66
67Examples of transformations that should follow this rule include:
68
69* Moving instructions between basic blocks. For example, if instruction ``I1``
70  in ``BB1`` is moved before ``I2`` in ``BB2``, the source location of ``I1``
71  can be preserved if it has the same source location as ``I2``.
72
73.. _WhenToMergeLocation:
74
75When to merge instruction locations
76-----------------------------------
77
78A transformation should merge instruction locations if it replaces multiple
79instructions with a single merged instruction, *and* that merged instruction
80does not correspond to any of the original instructions' locations. The API to
81use is ``Instruction::applyMergedLocation``.
82
83The purpose of this rule is to ensure that a) the single merged instruction
84has a location with an accurate scope attached, and b) to prevent misleading
85single-stepping (or breakpoint) behavior. Often, merged instructions are memory
86accesses which can trap: having an accurate scope attached greatly assists in
87crash triage by identifying the (possibly inlined) function where the bad
88memory access occurred. This rule is also meant to assist SamplePGO by banning
89scenarios in which a sample of a block containing a merged instruction is
90misattributed to a block containing one of the instructions-to-be-merged.
91
92Examples of transformations that should follow this rule include:
93
94* Merging identical loads/stores which occur on both sides of a CFG diamond
95  (see the ``MergedLoadStoreMotion`` pass).
96
97* Merging identical loop-invariant stores (see the LICM utility
98  ``llvm::promoteLoopAccessesToScalars``).
99
100* Peephole optimizations which combine multiple instructions together, like
101  ``(add (mul A B) C) => llvm.fma.f32(A, B, C)``.  Note that the location of
102  the ``fma`` does not exactly correspond to the locations of either the
103  ``mul`` or the ``add`` instructions.
104
105Examples of transformations for which this rule *does not* apply include:
106
107* Block-local peepholes which delete redundant instructions, like
108  ``(sext (zext i8 %x to i16) to i32) => (zext i8 %x to i32)``. The inner
109  ``zext`` is modified but remains in its block, so the rule for
110  :ref:`preserving locations<WhenToPreserveLocation>` should apply.
111
112* Converting an if-then-else CFG diamond into a ``select``. Preserving the
113  debug locations of speculated instructions can make it seem like a condition
114  is true when it's not (or vice versa), which leads to a confusing
115  single-stepping experience. The rule for
116  :ref:`dropping locations<WhenToDropLocation>` should apply here.
117
118* Hoisting identical instructions which appear in several successor blocks into
119  a predecessor block (see ``BranchFolder::HoistCommonCodeInSuccs``). In this
120  case there is no single merged instruction. The rule for
121  :ref:`dropping locations<WhenToDropLocation>` applies.
122
123.. _WhenToDropLocation:
124
125When to drop an instruction location
126------------------------------------
127
128A transformation should drop debug locations if the rules for
129:ref:`preserving<WhenToPreserveLocation>` and
130:ref:`merging<WhenToMergeLocation>` debug locations do not apply. The API to
131use is ``Instruction::dropLocation()``.
132
133The purpose of this rule is to prevent erratic or misleading single-stepping
134behavior in situations in which an instruction has no clear, unambiguous
135relationship to a source location.
136
137To handle an instruction without a location, the DWARF generator
138defaults to allowing the last-set location after a label to cascade forward, or
139to setting a line 0 location with viable scope information if no previous
140location is available.
141
142See the discussion in the section about
143:ref:`merging locations<WhenToMergeLocation>` for examples of when the rule for
144dropping locations applies.
145
146Rules for updating debug values
147===============================
148
149Deleting an IR-level Instruction
150--------------------------------
151
152When an ``Instruction`` is deleted, its debug uses change to ``undef``. This is
153a loss of debug info: the value of one or more source variables becomes
154unavailable, starting with the ``llvm.dbg.value(undef, ...)``. When there is no
155way to reconstitute the value of the lost instruction, this is the best
156possible outcome. However, it's often possible to do better:
157
158* If the dying instruction can be RAUW'd, do so. The
159  ``Value::replaceAllUsesWith`` API transparently updates debug uses of the
160  dying instruction to point to the replacement value.
161
162* If the dying instruction cannot be RAUW'd, call ``llvm::salvageDebugInfo`` on
163  it. This makes a best-effort attempt to rewrite debug uses of the dying
164  instruction by describing its effect as a ``DIExpression``.
165
166* If one of the **operands** of a dying instruction would become trivially
167  dead, use ``llvm::replaceAllDbgUsesWith`` to rewrite the debug uses of that
168  operand. Consider the following example function:
169
170.. code-block:: llvm
171
172  define i16 @foo(i16 %a) {
173    %b = sext i16 %a to i32
174    %c = and i32 %b, 15
175    call void @llvm.dbg.value(metadata i32 %c, ...)
176    %d = trunc i32 %c to i16
177    ret i16 %d
178  }
179
180Now, here's what happens after the unnecessary truncation instruction ``%d`` is
181replaced with a simplified instruction:
182
183.. code-block:: llvm
184
185  define i16 @foo(i16 %a) {
186    call void @llvm.dbg.value(metadata i32 undef, ...)
187    %simplified = and i16 %a, 15
188    ret i16 %simplified
189  }
190
191Note that after deleting ``%d``, all uses of its operand ``%c`` become
192trivially dead. The debug use which used to point to ``%c`` is now ``undef``,
193and debug info is needlessly lost.
194
195To solve this problem, do:
196
197.. code-block:: cpp
198
199  llvm::replaceAllDbgUsesWith(%c, theSimplifiedAndInstruction, ...)
200
201This results in better debug info because the debug use of ``%c`` is preserved:
202
203.. code-block:: llvm
204
205  define i16 @foo(i16 %a) {
206    %simplified = and i16 %a, 15
207    call void @llvm.dbg.value(metadata i16 %simplified, ...)
208    ret i16 %simplified
209  }
210
211You may have noticed that ``%simplified`` is narrower than ``%c``: this is not
212a problem, because ``llvm::replaceAllDbgUsesWith`` takes care of inserting the
213necessary conversion operations into the DIExpressions of updated debug uses.
214
215Deleting a MIR-level MachineInstr
216---------------------------------
217
218TODO
219
220How to automatically convert tests into debug info tests
221========================================================
222
223.. _IRDebugify:
224
225Mutation testing for IR-level transformations
226---------------------------------------------
227
228An IR test case for a transformation can, in many cases, be automatically
229mutated to test debug info handling within that transformation. This is a
230simple way to test for proper debug info handling.
231
232The ``debugify`` utility pass
233^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
234
235The ``debugify`` testing utility is just a pair of passes: ``debugify`` and
236``check-debugify``.
237
238The first applies synthetic debug information to every instruction of the
239module, and the second checks that this DI is still available after an
240optimization has occurred, reporting any errors/warnings while doing so.
241
242The instructions are assigned sequentially increasing line locations, and are
243immediately used by debug value intrinsics everywhere possible.
244
245For example, here is a module before:
246
247.. code-block:: llvm
248
249   define void @f(i32* %x) {
250   entry:
251     %x.addr = alloca i32*, align 8
252     store i32* %x, i32** %x.addr, align 8
253     %0 = load i32*, i32** %x.addr, align 8
254     store i32 10, i32* %0, align 4
255     ret void
256   }
257
258and after running ``opt -debugify``:
259
260.. code-block:: llvm
261
262   define void @f(i32* %x) !dbg !6 {
263   entry:
264     %x.addr = alloca i32*, align 8, !dbg !12
265     call void @llvm.dbg.value(metadata i32** %x.addr, metadata !9, metadata !DIExpression()), !dbg !12
266     store i32* %x, i32** %x.addr, align 8, !dbg !13
267     %0 = load i32*, i32** %x.addr, align 8, !dbg !14
268     call void @llvm.dbg.value(metadata i32* %0, metadata !11, metadata !DIExpression()), !dbg !14
269     store i32 10, i32* %0, align 4, !dbg !15
270     ret void, !dbg !16
271   }
272
273   !llvm.dbg.cu = !{!0}
274   !llvm.debugify = !{!3, !4}
275   !llvm.module.flags = !{!5}
276
277   !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
278   !1 = !DIFile(filename: "debugify-sample.ll", directory: "/")
279   !2 = !{}
280   !3 = !{i32 5}
281   !4 = !{i32 2}
282   !5 = !{i32 2, !"Debug Info Version", i32 3}
283   !6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8)
284   !7 = !DISubroutineType(types: !2)
285   !8 = !{!9, !11}
286   !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
287   !10 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned)
288   !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 3, type: !10)
289   !12 = !DILocation(line: 1, column: 1, scope: !6)
290   !13 = !DILocation(line: 2, column: 1, scope: !6)
291   !14 = !DILocation(line: 3, column: 1, scope: !6)
292   !15 = !DILocation(line: 4, column: 1, scope: !6)
293   !16 = !DILocation(line: 5, column: 1, scope: !6)
294
295Using ``debugify``
296^^^^^^^^^^^^^^^^^^
297
298A simple way to use ``debugify`` is as follows:
299
300.. code-block:: bash
301
302  $ opt -debugify -pass-to-test -check-debugify sample.ll
303
304This will inject synthetic DI to ``sample.ll`` run the ``pass-to-test`` and
305then check for missing DI. The ``-check-debugify`` step can of course be
306omitted in favor of more customizable FileCheck directives.
307
308Some other ways to run debugify are available:
309
310.. code-block:: bash
311
312   # Same as the above example.
313   $ opt -enable-debugify -pass-to-test sample.ll
314
315   # Suppresses verbose debugify output.
316   $ opt -enable-debugify -debugify-quiet -pass-to-test sample.ll
317
318   # Prepend -debugify before and append -check-debugify -strip after
319   # each pass on the pipeline (similar to -verify-each).
320   $ opt -debugify-each -O2 sample.ll
321
322In order for ``check-debugify`` to work, the DI must be coming from
323``debugify``. Thus, modules with existing DI will be skipped.
324
325``debugify`` can be used to test a backend, e.g:
326
327.. code-block:: bash
328
329   $ opt -debugify < sample.ll | llc -o -
330
331There is also a MIR-level debugify pass that can be run before each backend
332pass, see:
333:ref:`Mutation testing for MIR-level transformations<MIRDebugify>`.
334
335``debugify`` in regression tests
336^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
337
338The output of the ``debugify`` pass must be stable enough to use in regression
339tests. Changes to this pass are not allowed to break existing tests.
340
341.. note::
342
343   Regression tests must be robust. Avoid hardcoding line/variable numbers in
344   check lines. In cases where this can't be avoided (say, if a test wouldn't
345   be precise enough), moving the test to its own file is preferred.
346
347.. _MIRDebugify:
348
349Test original debug info preservation in optimizations
350------------------------------------------------------
351
352In addition to automatically generating debug info, the checks provided by
353the ``debugify`` utility pass can also be used to test the preservation of
354pre-existing debug info metadata. It could be run as follows:
355
356.. code-block:: bash
357
358  # Run the pass by checking original Debug Info preservation.
359  $ opt -verify-debuginfo-preserve -pass-to-test sample.ll
360
361  # Check the preservation of original Debug Info after each pass.
362  $ opt -verify-each-debuginfo-preserve -O2 sample.ll
363
364Mutation testing for MIR-level transformations
365----------------------------------------------
366
367A variant of the ``debugify`` utility described in
368:ref:`Mutation testing for IR-level transformations<IRDebugify>` can be used
369for MIR-level transformations as well: much like the IR-level pass,
370``mir-debugify`` inserts sequentially increasing line locations to each
371``MachineInstr`` in a ``Module``. And the MIR-level ``mir-check-debugify`` is
372similar to IR-level ``check-debugify`` pass.
373
374For example, here is a snippet before:
375
376.. code-block:: llvm
377
378  name:            test
379  body:             |
380    bb.1 (%ir-block.0):
381      %0:_(s32) = IMPLICIT_DEF
382      %1:_(s32) = IMPLICIT_DEF
383      %2:_(s32) = G_CONSTANT i32 2
384      %3:_(s32) = G_ADD %0, %2
385      %4:_(s32) = G_SUB %3, %1
386
387and after running ``llc -run-pass=mir-debugify``:
388
389.. code-block:: llvm
390
391  name:            test
392  body:             |
393    bb.0 (%ir-block.0):
394      %0:_(s32) = IMPLICIT_DEF debug-location !12
395      DBG_VALUE %0(s32), $noreg, !9, !DIExpression(), debug-location !12
396      %1:_(s32) = IMPLICIT_DEF debug-location !13
397      DBG_VALUE %1(s32), $noreg, !11, !DIExpression(), debug-location !13
398      %2:_(s32) = G_CONSTANT i32 2, debug-location !14
399      DBG_VALUE %2(s32), $noreg, !9, !DIExpression(), debug-location !14
400      %3:_(s32) = G_ADD %0, %2, debug-location !DILocation(line: 4, column: 1, scope: !6)
401      DBG_VALUE %3(s32), $noreg, !9, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !6)
402      %4:_(s32) = G_SUB %3, %1, debug-location !DILocation(line: 5, column: 1, scope: !6)
403      DBG_VALUE %4(s32), $noreg, !9, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !6)
404
405By default, ``mir-debugify`` inserts ``DBG_VALUE`` instructions **everywhere**
406it is legal to do so.  In particular, every (non-PHI) machine instruction that
407defines a register must be followed by a ``DBG_VALUE`` use of that def.  If
408an instruction does not define a register, but can be followed by a debug inst,
409MIRDebugify inserts a ``DBG_VALUE`` that references a constant.  Insertion of
410``DBG_VALUE``'s can be disabled by setting ``-debugify-level=locations``.
411
412To run MIRDebugify once, simply insert ``mir-debugify`` into your ``llc``
413invocation, like:
414
415.. code-block:: bash
416
417  # Before some other pass.
418  $ llc -run-pass=mir-debugify,other-pass ...
419
420  # After some other pass.
421  $ llc -run-pass=other-pass,mir-debugify ...
422
423To run MIRDebugify before each pass in a pipeline, use
424``-debugify-and-strip-all-safe``. This can be combined with ``-start-before``
425and ``-start-after``. For example:
426
427.. code-block:: bash
428
429  $ llc -debugify-and-strip-all-safe -run-pass=... <other llc args>
430  $ llc -debugify-and-strip-all-safe -O1 <other llc args>
431
432If you want to check it after each pass in a pipeline, use
433``-debugify-check-and-strip-all-safe``. This can also be combined with
434``-start-before`` and ``-start-after``. For example:
435
436.. code-block:: bash
437
438  $ llc -debugify-check-and-strip-all-safe -run-pass=... <other llc args>
439  $ llc -debugify-check-and-strip-all-safe -O1 <other llc args>
440
441To check all debug info from a test, use ``mir-check-debugify``, like:
442
443.. code-block:: bash
444
445  $ llc -run-pass=mir-debugify,other-pass,mir-check-debugify
446
447To strip out all debug info from a test, use ``mir-strip-debug``, like:
448
449.. code-block:: bash
450
451  $ llc -run-pass=mir-debugify,other-pass,mir-strip-debug
452
453It can be useful to combine ``mir-debugify``, ``mir-check-debugify`` and/or
454``mir-strip-debug`` to identify backend transformations which break in
455the presence of debug info. For example, to run the AArch64 backend tests
456with all normal passes "sandwiched" in between MIRDebugify and
457MIRStripDebugify mutation passes, run:
458
459.. code-block:: bash
460
461  $ llvm-lit test/CodeGen/AArch64 -Dllc="llc -debugify-and-strip-all-safe"
462
463Using LostDebugLocObserver
464--------------------------
465
466TODO
467