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 one or more new instructions, *and* the new instruction(s) 80produce the output of more than one of the original instructions. The API to use 81is ``Instruction::applyMergedLocation``. For each new instruction I, its new 82location should be a merge of the locations of all instructions whose output is 83produced by I. Typically, this includes any instruction being RAUWed by a new 84instruction, and excludes any instruction that only produces an intermediate 85value used by the RAUWed instruction. 86 87The purpose of this rule is to ensure that a) the single merged instruction 88has a location with an accurate scope attached, and b) to prevent misleading 89single-stepping (or breakpoint) behavior. Often, merged instructions are memory 90accesses which can trap: having an accurate scope attached greatly assists in 91crash triage by identifying the (possibly inlined) function where the bad 92memory access occurred. This rule is also meant to assist SamplePGO by banning 93scenarios in which a sample of a block containing a merged instruction is 94misattributed to a block containing one of the instructions-to-be-merged. 95 96Examples of transformations that should follow this rule include: 97 98* Hoisting identical instructions from all successors of a conditional branch 99 or sinking those from all paths to a postdominating block. For example, 100 merging identical loads/stores which occur on both sides of a CFG diamond 101 (see the ``MergedLoadStoreMotion`` pass). For each group of identical 102 instructions being hoisted/sunk, the merge of all their locations should be 103 applied to the merged instruction. 104 105* Merging identical loop-invariant stores (see the LICM utility 106 ``llvm::promoteLoopAccessesToScalars``). 107 108* Scalar instructions being combined into a vector instruction, like 109 ``(add A1, B1), (add A2, B2) => (add (A1, A2), (B1, B2))``. As the new vector 110 ``add`` computes the result of both original ``add`` instructions 111 simultaneously, it should use a merge of the two locations. Similarly, if 112 prior optimizations have already produced vectors ``(A1, A2)`` and 113 ``(B2, B1)``, then we might create a ``(shufflevector (1, 0), (B2, B1))`` 114 instruction to produce ``(B1, B2)`` for the vector ``add``; in this case we've 115 created two instructions to replace the original ``adds``, so both new 116 instructions should use the merged location. 117 118Examples of transformations for which this rule *does not* apply include: 119 120* Block-local peepholes which delete redundant instructions, like 121 ``(sext (zext i8 %x to i16) to i32) => (zext i8 %x to i32)``. The inner 122 ``zext`` is modified but remains in its block, so the rule for 123 :ref:`preserving locations<WhenToPreserveLocation>` should apply. 124 125* Peephole optimizations which combine multiple instructions together, like 126 ``(add (mul A B) C) => llvm.fma.f32(A, B, C)``. Note that the result of the 127 ``mul`` no longer appears in the program, while the result of the ``add`` is 128 now produced by the ``fma``, so the ``add``'s location should be used. 129 130* Converting an if-then-else CFG diamond into a ``select``. Preserving the 131 debug locations of speculated instructions can make it seem like a condition 132 is true when it's not (or vice versa), which leads to a confusing 133 single-stepping experience. The rule for 134 :ref:`dropping locations<WhenToDropLocation>` should apply here. 135 136* Hoisting/sinking that would make a location reachable when it previously 137 wasn't. Consider hoisting two identical instructions with the same location 138 from first two cases of a switch that has three cases. Merging their 139 locations would make the location from the first two cases reachable when the 140 third case is taken. The rule for 141 :ref:`dropping locations<WhenToDropLocation>` applies. 142 143.. _WhenToDropLocation: 144 145When to drop an instruction location 146------------------------------------ 147 148A transformation should drop debug locations if the rules for 149:ref:`preserving<WhenToPreserveLocation>` and 150:ref:`merging<WhenToMergeLocation>` debug locations do not apply. The API to 151use is ``Instruction::dropLocation()``. 152 153The purpose of this rule is to prevent erratic or misleading single-stepping 154behavior in situations in which an instruction has no clear, unambiguous 155relationship to a source location. 156 157To handle an instruction without a location, the DWARF generator 158defaults to allowing the last-set location after a label to cascade forward, or 159to setting a line 0 location with viable scope information if no previous 160location is available. 161 162See the discussion in the section about 163:ref:`merging locations<WhenToMergeLocation>` for examples of when the rule for 164dropping locations applies. 165 166Rules for updating debug values 167=============================== 168 169Deleting an IR-level Instruction 170-------------------------------- 171 172When an ``Instruction`` is deleted, its debug uses change to ``undef``. This is 173a loss of debug info: the value of one or more source variables becomes 174unavailable, starting with the ``#dbg_value(undef, ...)``. When there is no 175way to reconstitute the value of the lost instruction, this is the best 176possible outcome. However, it's often possible to do better: 177 178* If the dying instruction can be RAUW'd, do so. The 179 ``Value::replaceAllUsesWith`` API transparently updates debug uses of the 180 dying instruction to point to the replacement value. 181 182* If the dying instruction cannot be RAUW'd, call ``llvm::salvageDebugInfo`` on 183 it. This makes a best-effort attempt to rewrite debug uses of the dying 184 instruction by describing its effect as a ``DIExpression``. 185 186* If one of the **operands** of a dying instruction would become trivially 187 dead, use ``llvm::replaceAllDbgUsesWith`` to rewrite the debug uses of that 188 operand. Consider the following example function: 189 190.. code-block:: llvm 191 192 define i16 @foo(i16 %a) { 193 %b = sext i16 %a to i32 194 %c = and i32 %b, 15 195 #dbg_value(i32 %c, ...) 196 %d = trunc i32 %c to i16 197 ret i16 %d 198 } 199 200Now, here's what happens after the unnecessary truncation instruction ``%d`` is 201replaced with a simplified instruction: 202 203.. code-block:: llvm 204 205 define i16 @foo(i16 %a) { 206 #dbg_value(i32 undef, ...) 207 %simplified = and i16 %a, 15 208 ret i16 %simplified 209 } 210 211Note that after deleting ``%d``, all uses of its operand ``%c`` become 212trivially dead. The debug use which used to point to ``%c`` is now ``undef``, 213and debug info is needlessly lost. 214 215To solve this problem, do: 216 217.. code-block:: cpp 218 219 llvm::replaceAllDbgUsesWith(%c, theSimplifiedAndInstruction, ...) 220 221This results in better debug info because the debug use of ``%c`` is preserved: 222 223.. code-block:: llvm 224 225 define i16 @foo(i16 %a) { 226 %simplified = and i16 %a, 15 227 #dbg_value(i16 %simplified, ...) 228 ret i16 %simplified 229 } 230 231You may have noticed that ``%simplified`` is narrower than ``%c``: this is not 232a problem, because ``llvm::replaceAllDbgUsesWith`` takes care of inserting the 233necessary conversion operations into the DIExpressions of updated debug uses. 234 235Deleting a MIR-level MachineInstr 236--------------------------------- 237 238TODO 239 240Rules for updating ``DIAssignID`` Attachments 241============================================= 242 243``DIAssignID`` metadata attachments are used by Assignment Tracking, which is 244currently an experimental debug mode. 245 246See :doc:`AssignmentTracking` for how to update them and for more info on 247Assignment Tracking. 248 249How to automatically convert tests into debug info tests 250======================================================== 251 252.. _IRDebugify: 253 254Mutation testing for IR-level transformations 255--------------------------------------------- 256 257An IR test case for a transformation can, in many cases, be automatically 258mutated to test debug info handling within that transformation. This is a 259simple way to test for proper debug info handling. 260 261The ``debugify`` utility pass 262^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 263 264The ``debugify`` testing utility is just a pair of passes: ``debugify`` and 265``check-debugify``. 266 267The first applies synthetic debug information to every instruction of the 268module, and the second checks that this DI is still available after an 269optimization has occurred, reporting any errors/warnings while doing so. 270 271The instructions are assigned sequentially increasing line locations, and are 272immediately used by debug value records everywhere possible. 273 274For example, here is a module before: 275 276.. code-block:: llvm 277 278 define void @f(i32* %x) { 279 entry: 280 %x.addr = alloca i32*, align 8 281 store i32* %x, i32** %x.addr, align 8 282 %0 = load i32*, i32** %x.addr, align 8 283 store i32 10, i32* %0, align 4 284 ret void 285 } 286 287and after running ``opt -debugify``: 288 289.. code-block:: llvm 290 291 define void @f(i32* %x) !dbg !6 { 292 entry: 293 %x.addr = alloca i32*, align 8, !dbg !12 294 #dbg_value(i32** %x.addr, !9, !DIExpression(), !12) 295 store i32* %x, i32** %x.addr, align 8, !dbg !13 296 %0 = load i32*, i32** %x.addr, align 8, !dbg !14 297 #dbg_value(i32* %0, !11, !DIExpression(), !14) 298 store i32 10, i32* %0, align 4, !dbg !15 299 ret void, !dbg !16 300 } 301 302 !llvm.dbg.cu = !{!0} 303 !llvm.debugify = !{!3, !4} 304 !llvm.module.flags = !{!5} 305 306 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) 307 !1 = !DIFile(filename: "debugify-sample.ll", directory: "/") 308 !2 = !{} 309 !3 = !{i32 5} 310 !4 = !{i32 2} 311 !5 = !{i32 2, !"Debug Info Version", i32 3} 312 !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) 313 !7 = !DISubroutineType(types: !2) 314 !8 = !{!9, !11} 315 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10) 316 !10 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned) 317 !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 3, type: !10) 318 !12 = !DILocation(line: 1, column: 1, scope: !6) 319 !13 = !DILocation(line: 2, column: 1, scope: !6) 320 !14 = !DILocation(line: 3, column: 1, scope: !6) 321 !15 = !DILocation(line: 4, column: 1, scope: !6) 322 !16 = !DILocation(line: 5, column: 1, scope: !6) 323 324Using ``debugify`` 325^^^^^^^^^^^^^^^^^^ 326 327A simple way to use ``debugify`` is as follows: 328 329.. code-block:: bash 330 331 $ opt -debugify -pass-to-test -check-debugify sample.ll 332 333This will inject synthetic DI to ``sample.ll`` run the ``pass-to-test`` and 334then check for missing DI. The ``-check-debugify`` step can of course be 335omitted in favor of more customizable FileCheck directives. 336 337Some other ways to run debugify are available: 338 339.. code-block:: bash 340 341 # Same as the above example. 342 $ opt -enable-debugify -pass-to-test sample.ll 343 344 # Suppresses verbose debugify output. 345 $ opt -enable-debugify -debugify-quiet -pass-to-test sample.ll 346 347 # Prepend -debugify before and append -check-debugify -strip after 348 # each pass on the pipeline (similar to -verify-each). 349 $ opt -debugify-each -O2 sample.ll 350 351In order for ``check-debugify`` to work, the DI must be coming from 352``debugify``. Thus, modules with existing DI will be skipped. 353 354``debugify`` can be used to test a backend, e.g: 355 356.. code-block:: bash 357 358 $ opt -debugify < sample.ll | llc -o - 359 360There is also a MIR-level debugify pass that can be run before each backend 361pass, see: 362:ref:`Mutation testing for MIR-level transformations<MIRDebugify>`. 363 364``debugify`` in regression tests 365^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 366 367The output of the ``debugify`` pass must be stable enough to use in regression 368tests. Changes to this pass are not allowed to break existing tests. 369 370.. note:: 371 372 Regression tests must be robust. Avoid hardcoding line/variable numbers in 373 check lines. In cases where this can't be avoided (say, if a test wouldn't 374 be precise enough), moving the test to its own file is preferred. 375 376.. _MIRDebugify: 377 378Test original debug info preservation in optimizations 379------------------------------------------------------ 380 381In addition to automatically generating debug info, the checks provided by 382the ``debugify`` utility pass can also be used to test the preservation of 383pre-existing debug info metadata. It could be run as follows: 384 385.. code-block:: bash 386 387 # Run the pass by checking original Debug Info preservation. 388 $ opt -verify-debuginfo-preserve -pass-to-test sample.ll 389 390 # Check the preservation of original Debug Info after each pass. 391 $ opt -verify-each-debuginfo-preserve -O2 sample.ll 392 393Limit number of observed functions to speed up the analysis: 394 395.. code-block:: bash 396 397 # Test up to 100 functions (per compile unit) per pass. 398 $ opt -verify-each-debuginfo-preserve -O2 -debugify-func-limit=100 sample.ll 399 400Please do note that running ``-verify-each-debuginfo-preserve`` on big projects 401could be heavily time consuming. Therefore, we suggest using 402``-debugify-func-limit`` with a suitable limit number to prevent extremely long 403builds. 404 405Furthermore, there is a way to export the issues that have been found into 406a JSON file as follows: 407 408.. code-block:: bash 409 410 $ opt -verify-debuginfo-preserve -verify-di-preserve-export=sample.json -pass-to-test sample.ll 411 412and then use the ``llvm/utils/llvm-original-di-preservation.py`` script 413to generate an HTML page with the issues reported in a more human readable form 414as follows: 415 416.. code-block:: bash 417 418 $ llvm-original-di-preservation.py sample.json sample.html 419 420Testing of original debug info preservation can be invoked from front-end level 421as follows: 422 423.. code-block:: bash 424 425 # Test each pass. 426 $ clang -Xclang -fverify-debuginfo-preserve -g -O2 sample.c 427 428 # Test each pass and export the issues report into the JSON file. 429 $ clang -Xclang -fverify-debuginfo-preserve -Xclang -fverify-debuginfo-preserve-export=sample.json -g -O2 sample.c 430 431Please do note that there are some known false positives, for source locations 432and debug record checking, so that will be addressed as a future work. 433 434Mutation testing for MIR-level transformations 435---------------------------------------------- 436 437A variant of the ``debugify`` utility described in 438:ref:`Mutation testing for IR-level transformations<IRDebugify>` can be used 439for MIR-level transformations as well: much like the IR-level pass, 440``mir-debugify`` inserts sequentially increasing line locations to each 441``MachineInstr`` in a ``Module``. And the MIR-level ``mir-check-debugify`` is 442similar to IR-level ``check-debugify`` pass. 443 444For example, here is a snippet before: 445 446.. code-block:: llvm 447 448 name: test 449 body: | 450 bb.1 (%ir-block.0): 451 %0:_(s32) = IMPLICIT_DEF 452 %1:_(s32) = IMPLICIT_DEF 453 %2:_(s32) = G_CONSTANT i32 2 454 %3:_(s32) = G_ADD %0, %2 455 %4:_(s32) = G_SUB %3, %1 456 457and after running ``llc -run-pass=mir-debugify``: 458 459.. code-block:: llvm 460 461 name: test 462 body: | 463 bb.0 (%ir-block.0): 464 %0:_(s32) = IMPLICIT_DEF debug-location !12 465 DBG_VALUE %0(s32), $noreg, !9, !DIExpression(), debug-location !12 466 %1:_(s32) = IMPLICIT_DEF debug-location !13 467 DBG_VALUE %1(s32), $noreg, !11, !DIExpression(), debug-location !13 468 %2:_(s32) = G_CONSTANT i32 2, debug-location !14 469 DBG_VALUE %2(s32), $noreg, !9, !DIExpression(), debug-location !14 470 %3:_(s32) = G_ADD %0, %2, debug-location !DILocation(line: 4, column: 1, scope: !6) 471 DBG_VALUE %3(s32), $noreg, !9, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !6) 472 %4:_(s32) = G_SUB %3, %1, debug-location !DILocation(line: 5, column: 1, scope: !6) 473 DBG_VALUE %4(s32), $noreg, !9, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !6) 474 475By default, ``mir-debugify`` inserts ``DBG_VALUE`` instructions **everywhere** 476it is legal to do so. In particular, every (non-PHI) machine instruction that 477defines a register must be followed by a ``DBG_VALUE`` use of that def. If 478an instruction does not define a register, but can be followed by a debug inst, 479MIRDebugify inserts a ``DBG_VALUE`` that references a constant. Insertion of 480``DBG_VALUE``'s can be disabled by setting ``-debugify-level=locations``. 481 482To run MIRDebugify once, simply insert ``mir-debugify`` into your ``llc`` 483invocation, like: 484 485.. code-block:: bash 486 487 # Before some other pass. 488 $ llc -run-pass=mir-debugify,other-pass ... 489 490 # After some other pass. 491 $ llc -run-pass=other-pass,mir-debugify ... 492 493To run MIRDebugify before each pass in a pipeline, use 494``-debugify-and-strip-all-safe``. This can be combined with ``-start-before`` 495and ``-start-after``. For example: 496 497.. code-block:: bash 498 499 $ llc -debugify-and-strip-all-safe -run-pass=... <other llc args> 500 $ llc -debugify-and-strip-all-safe -O1 <other llc args> 501 502If you want to check it after each pass in a pipeline, use 503``-debugify-check-and-strip-all-safe``. This can also be combined with 504``-start-before`` and ``-start-after``. For example: 505 506.. code-block:: bash 507 508 $ llc -debugify-check-and-strip-all-safe -run-pass=... <other llc args> 509 $ llc -debugify-check-and-strip-all-safe -O1 <other llc args> 510 511To check all debug info from a test, use ``mir-check-debugify``, like: 512 513.. code-block:: bash 514 515 $ llc -run-pass=mir-debugify,other-pass,mir-check-debugify 516 517To strip out all debug info from a test, use ``mir-strip-debug``, like: 518 519.. code-block:: bash 520 521 $ llc -run-pass=mir-debugify,other-pass,mir-strip-debug 522 523It can be useful to combine ``mir-debugify``, ``mir-check-debugify`` and/or 524``mir-strip-debug`` to identify backend transformations which break in 525the presence of debug info. For example, to run the AArch64 backend tests 526with all normal passes "sandwiched" in between MIRDebugify and 527MIRStripDebugify mutation passes, run: 528 529.. code-block:: bash 530 531 $ llvm-lit test/CodeGen/AArch64 -Dllc="llc -debugify-and-strip-all-safe" 532 533Using LostDebugLocObserver 534-------------------------- 535 536TODO 537