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