1b429a0feSVedant Kumar======================================================= 2b429a0feSVedant KumarHow to Update Debug Info: A Guide for LLVM Pass Authors 3b429a0feSVedant Kumar======================================================= 4b429a0feSVedant Kumar 5b429a0feSVedant Kumar.. contents:: 6b429a0feSVedant Kumar :local: 7b429a0feSVedant Kumar 8b429a0feSVedant KumarIntroduction 9b429a0feSVedant Kumar============ 10b429a0feSVedant Kumar 11b429a0feSVedant KumarCertain kinds of code transformations can inadvertently result in a loss of 12b429a0feSVedant Kumardebug info, or worse, make debug info misrepresent the state of a program. 13b429a0feSVedant Kumar 14b429a0feSVedant KumarThis document specifies how to correctly update debug info in various kinds of 15b429a0feSVedant Kumarcode transformations, and offers suggestions for how to create targeted debug 16b429a0feSVedant Kumarinfo tests for arbitrary transformations. 17b429a0feSVedant Kumar 18b429a0feSVedant KumarFor more on the philosophy behind LLVM debugging information, see 19b429a0feSVedant Kumar:doc:`SourceLevelDebugging`. 20b429a0feSVedant Kumar 21b4459b59SVedant KumarRules for updating debug locations 22b4459b59SVedant Kumar================================== 23b429a0feSVedant Kumar 24b4459b59SVedant Kumar.. _WhenToPreserveLocation: 25b4459b59SVedant Kumar 26b4459b59SVedant KumarWhen to preserve an instruction location 27b4459b59SVedant Kumar---------------------------------------- 28b4459b59SVedant Kumar 29b4459b59SVedant KumarA transformation should preserve the debug location of an instruction if the 30b4459b59SVedant Kumarinstruction either remains in its basic block, or if its basic block is folded 31b4459b59SVedant Kumarinto a predecessor that branches unconditionally. The APIs to use are 32b4459b59SVedant Kumar``IRBuilder``, or ``Instruction::setDebugLoc``. 33b4459b59SVedant Kumar 34b4459b59SVedant KumarThe purpose of this rule is to ensure that common block-local optimizations 35b4459b59SVedant Kumarpreserve the ability to set breakpoints on source locations corresponding to 36b4459b59SVedant Kumarthe instructions they touch. Debugging, crash logs, and SamplePGO accuracy 37b4459b59SVedant Kumarwould be severely impacted if that ability were lost. 38b4459b59SVedant Kumar 39b4459b59SVedant KumarExamples of transformations that should follow this rule include: 40b4459b59SVedant Kumar 41b4459b59SVedant Kumar* Instruction scheduling. Block-local instruction reordering should not drop 42b4459b59SVedant Kumar source locations, even though this may lead to jumpy single-stepping 43b4459b59SVedant Kumar behavior. 44b4459b59SVedant Kumar 45b4459b59SVedant Kumar* Simple jump threading. For example, if block ``B1`` unconditionally jumps to 46b4459b59SVedant Kumar ``B2``, *and* is its unique predecessor, instructions from ``B2`` can be 47b4459b59SVedant Kumar hoisted into ``B1``. Source locations from ``B2`` should be preserved. 48b4459b59SVedant Kumar 49b4459b59SVedant Kumar* Peephole optimizations that replace or expand an instruction, like ``(add X 50b4459b59SVedant Kumar X) => (shl X 1)``. The location of the ``shl`` instruction should be the same 51b4459b59SVedant Kumar as the location of the ``add`` instruction. 52b4459b59SVedant Kumar 53b4459b59SVedant Kumar* Tail duplication. For example, if blocks ``B1`` and ``B2`` both 54b4459b59SVedant Kumar unconditionally branch to ``B3`` and ``B3`` can be folded into its 55b4459b59SVedant Kumar predecessors, source locations from ``B3`` should be preserved. 56b4459b59SVedant Kumar 57b4459b59SVedant KumarExamples of transformations for which this rule *does not* apply include: 58b4459b59SVedant Kumar 59b4459b59SVedant Kumar* LICM. E.g., if an instruction is moved from the loop body to the preheader, 60b4459b59SVedant Kumar the rule for :ref:`dropping locations<WhenToDropLocation>` applies. 61b4459b59SVedant Kumar 62d98fc62aSFlorian HahnIn addition to the rule above, a transformation should also preserve the debug 63d98fc62aSFlorian Hahnlocation of an instruction that is moved between basic blocks, if the 64d98fc62aSFlorian Hahndestination block already contains an instruction with an identical debug 65d98fc62aSFlorian Hahnlocation. 66d98fc62aSFlorian Hahn 67d98fc62aSFlorian HahnExamples of transformations that should follow this rule include: 68d98fc62aSFlorian Hahn 69d98fc62aSFlorian Hahn* Moving instructions between basic blocks. For example, if instruction ``I1`` 70d98fc62aSFlorian Hahn in ``BB1`` is moved before ``I2`` in ``BB2``, the source location of ``I1`` 71d98fc62aSFlorian Hahn can be preserved if it has the same source location as ``I2``. 72d98fc62aSFlorian Hahn 73b4459b59SVedant Kumar.. _WhenToMergeLocation: 74b4459b59SVedant Kumar 75b4459b59SVedant KumarWhen to merge instruction locations 76b4459b59SVedant Kumar----------------------------------- 77b4459b59SVedant Kumar 78b4459b59SVedant KumarA transformation should merge instruction locations if it replaces multiple 79b4459b59SVedant Kumarinstructions with a single merged instruction, *and* that merged instruction 80b4459b59SVedant Kumardoes not correspond to any of the original instructions' locations. The API to 81b4459b59SVedant Kumaruse is ``Instruction::applyMergedLocation``. 82b4459b59SVedant Kumar 83b4459b59SVedant KumarThe purpose of this rule is to ensure that a) the single merged instruction 84b4459b59SVedant Kumarhas a location with an accurate scope attached, and b) to prevent misleading 85b4459b59SVedant Kumarsingle-stepping (or breakpoint) behavior. Often, merged instructions are memory 86b4459b59SVedant Kumaraccesses which can trap: having an accurate scope attached greatly assists in 87b4459b59SVedant Kumarcrash triage by identifying the (possibly inlined) function where the bad 88b4459b59SVedant Kumarmemory access occurred. This rule is also meant to assist SamplePGO by banning 89b4459b59SVedant Kumarscenarios in which a sample of a block containing a merged instruction is 90b4459b59SVedant Kumarmisattributed to a block containing one of the instructions-to-be-merged. 91b4459b59SVedant Kumar 92b4459b59SVedant KumarExamples of transformations that should follow this rule include: 93b4459b59SVedant Kumar 94b4459b59SVedant Kumar* Merging identical loads/stores which occur on both sides of a CFG diamond 95b4459b59SVedant Kumar (see the ``MergedLoadStoreMotion`` pass). 96b4459b59SVedant Kumar 97b4459b59SVedant Kumar* Merging identical loop-invariant stores (see the LICM utility 98b4459b59SVedant Kumar ``llvm::promoteLoopAccessesToScalars``). 99b4459b59SVedant Kumar 100b4459b59SVedant Kumar* Peephole optimizations which combine multiple instructions together, like 101b4459b59SVedant Kumar ``(add (mul A B) C) => llvm.fma.f32(A, B, C)``. Note that the location of 102b4459b59SVedant Kumar the ``fma`` does not exactly correspond to the locations of either the 103b4459b59SVedant Kumar ``mul`` or the ``add`` instructions. 104b4459b59SVedant Kumar 105b4459b59SVedant KumarExamples of transformations for which this rule *does not* apply include: 106b4459b59SVedant Kumar 107b4459b59SVedant Kumar* Block-local peepholes which delete redundant instructions, like 108b4459b59SVedant Kumar ``(sext (zext i8 %x to i16) to i32) => (zext i8 %x to i32)``. The inner 109b4459b59SVedant Kumar ``zext`` is modified but remains in its block, so the rule for 110b4459b59SVedant Kumar :ref:`preserving locations<WhenToPreserveLocation>` should apply. 111b4459b59SVedant Kumar 112b4459b59SVedant Kumar* Converting an if-then-else CFG diamond into a ``select``. Preserving the 113b4459b59SVedant Kumar debug locations of speculated instructions can make it seem like a condition 114b4459b59SVedant Kumar is true when it's not (or vice versa), which leads to a confusing 115b4459b59SVedant Kumar single-stepping experience. The rule for 116b4459b59SVedant Kumar :ref:`dropping locations<WhenToDropLocation>` should apply here. 117b4459b59SVedant Kumar 118b4459b59SVedant Kumar* Hoisting identical instructions which appear in several successor blocks into 119b4459b59SVedant Kumar a predecessor block (see ``BranchFolder::HoistCommonCodeInSuccs``). In this 120b4459b59SVedant Kumar case there is no single merged instruction. The rule for 121b4459b59SVedant Kumar :ref:`dropping locations<WhenToDropLocation>` applies. 122b4459b59SVedant Kumar 123b4459b59SVedant Kumar.. _WhenToDropLocation: 124b4459b59SVedant Kumar 125b4459b59SVedant KumarWhen to drop an instruction location 126b4459b59SVedant Kumar------------------------------------ 127b4459b59SVedant Kumar 128b4459b59SVedant KumarA transformation should drop debug locations if the rules for 129b4459b59SVedant Kumar:ref:`preserving<WhenToPreserveLocation>` and 130b4459b59SVedant Kumar:ref:`merging<WhenToMergeLocation>` debug locations do not apply. The API to 131f71849c7SVedant Kumaruse is ``Instruction::dropLocation()``. 132b4459b59SVedant Kumar 133b4459b59SVedant KumarThe purpose of this rule is to prevent erratic or misleading single-stepping 134b4459b59SVedant Kumarbehavior in situations in which an instruction has no clear, unambiguous 135b4459b59SVedant Kumarrelationship to a source location. 136b4459b59SVedant Kumar 137b4459b59SVedant KumarTo handle an instruction without a location, the DWARF generator 138b4459b59SVedant Kumardefaults to allowing the last-set location after a label to cascade forward, or 139b4459b59SVedant Kumarto setting a line 0 location with viable scope information if no previous 140b4459b59SVedant Kumarlocation is available. 141b4459b59SVedant Kumar 142b4459b59SVedant KumarSee the discussion in the section about 143b4459b59SVedant Kumar:ref:`merging locations<WhenToMergeLocation>` for examples of when the rule for 144b4459b59SVedant Kumardropping locations applies. 145b4459b59SVedant Kumar 146b4459b59SVedant KumarRules for updating debug values 147b4459b59SVedant Kumar=============================== 148b4459b59SVedant Kumar 149b4459b59SVedant KumarDeleting an IR-level Instruction 150b4459b59SVedant Kumar-------------------------------- 151b429a0feSVedant Kumar 152b429a0feSVedant KumarWhen an ``Instruction`` is deleted, its debug uses change to ``undef``. This is 153d65cdb49SVedant Kumara loss of debug info: the value of one or more source variables becomes 154b429a0feSVedant Kumarunavailable, starting with the ``llvm.dbg.value(undef, ...)``. When there is no 155b429a0feSVedant Kumarway to reconstitute the value of the lost instruction, this is the best 156b429a0feSVedant Kumarpossible outcome. However, it's often possible to do better: 157b429a0feSVedant Kumar 158b429a0feSVedant Kumar* If the dying instruction can be RAUW'd, do so. The 159b429a0feSVedant Kumar ``Value::replaceAllUsesWith`` API transparently updates debug uses of the 160b429a0feSVedant Kumar dying instruction to point to the replacement value. 161b429a0feSVedant Kumar 16224660ea1SVedant Kumar* If the dying instruction cannot be RAUW'd, call ``llvm::salvageDebugInfo`` on 16324660ea1SVedant Kumar it. This makes a best-effort attempt to rewrite debug uses of the dying 16424660ea1SVedant Kumar instruction by describing its effect as a ``DIExpression``. 165b429a0feSVedant Kumar 166b429a0feSVedant Kumar* If one of the **operands** of a dying instruction would become trivially 167b429a0feSVedant Kumar dead, use ``llvm::replaceAllDbgUsesWith`` to rewrite the debug uses of that 168b429a0feSVedant Kumar operand. Consider the following example function: 169b429a0feSVedant Kumar 170b429a0feSVedant Kumar.. code-block:: llvm 171b429a0feSVedant Kumar 172b429a0feSVedant Kumar define i16 @foo(i16 %a) { 173b429a0feSVedant Kumar %b = sext i16 %a to i32 174b429a0feSVedant Kumar %c = and i32 %b, 15 175b429a0feSVedant Kumar call void @llvm.dbg.value(metadata i32 %c, ...) 176b429a0feSVedant Kumar %d = trunc i32 %c to i16 177b429a0feSVedant Kumar ret i16 %d 178b429a0feSVedant Kumar } 179b429a0feSVedant Kumar 180b429a0feSVedant KumarNow, here's what happens after the unnecessary truncation instruction ``%d`` is 181b429a0feSVedant Kumarreplaced with a simplified instruction: 182b429a0feSVedant Kumar 183b429a0feSVedant Kumar.. code-block:: llvm 184b429a0feSVedant Kumar 185b429a0feSVedant Kumar define i16 @foo(i16 %a) { 186b429a0feSVedant Kumar call void @llvm.dbg.value(metadata i32 undef, ...) 187b429a0feSVedant Kumar %simplified = and i16 %a, 15 188b429a0feSVedant Kumar ret i16 %simplified 189b429a0feSVedant Kumar } 190b429a0feSVedant Kumar 191b429a0feSVedant KumarNote that after deleting ``%d``, all uses of its operand ``%c`` become 192b429a0feSVedant Kumartrivially dead. The debug use which used to point to ``%c`` is now ``undef``, 193b429a0feSVedant Kumarand debug info is needlessly lost. 194b429a0feSVedant Kumar 195b429a0feSVedant KumarTo solve this problem, do: 196b429a0feSVedant Kumar 197b429a0feSVedant Kumar.. code-block:: cpp 198b429a0feSVedant Kumar 199b429a0feSVedant Kumar llvm::replaceAllDbgUsesWith(%c, theSimplifiedAndInstruction, ...) 200b429a0feSVedant Kumar 201b429a0feSVedant KumarThis results in better debug info because the debug use of ``%c`` is preserved: 202b429a0feSVedant Kumar 203b429a0feSVedant Kumar.. code-block:: llvm 204b429a0feSVedant Kumar 205b429a0feSVedant Kumar define i16 @foo(i16 %a) { 206b429a0feSVedant Kumar %simplified = and i16 %a, 15 207b429a0feSVedant Kumar call void @llvm.dbg.value(metadata i16 %simplified, ...) 208b429a0feSVedant Kumar ret i16 %simplified 209b429a0feSVedant Kumar } 210b429a0feSVedant Kumar 211b429a0feSVedant KumarYou may have noticed that ``%simplified`` is narrower than ``%c``: this is not 212b429a0feSVedant Kumara problem, because ``llvm::replaceAllDbgUsesWith`` takes care of inserting the 213b429a0feSVedant Kumarnecessary conversion operations into the DIExpressions of updated debug uses. 214b429a0feSVedant Kumar 21524660ea1SVedant KumarDeleting a MIR-level MachineInstr 21624660ea1SVedant Kumar--------------------------------- 217b429a0feSVedant Kumar 218b429a0feSVedant KumarTODO 219b429a0feSVedant Kumar 22024660ea1SVedant KumarHow to automatically convert tests into debug info tests 22124660ea1SVedant Kumar======================================================== 222b429a0feSVedant Kumar 22324660ea1SVedant Kumar.. _IRDebugify: 224b429a0feSVedant Kumar 225b429a0feSVedant KumarMutation testing for IR-level transformations 226b429a0feSVedant Kumar--------------------------------------------- 227b429a0feSVedant Kumar 228b429a0feSVedant KumarAn IR test case for a transformation can, in many cases, be automatically 229b429a0feSVedant Kumarmutated to test debug info handling within that transformation. This is a 230b429a0feSVedant Kumarsimple way to test for proper debug info handling. 231b429a0feSVedant Kumar 2321a2b3536SDjordje TodorovicThe ``debugify`` utility pass 2331a2b3536SDjordje Todorovic^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 234b429a0feSVedant Kumar 235b429a0feSVedant KumarThe ``debugify`` testing utility is just a pair of passes: ``debugify`` and 236b429a0feSVedant Kumar``check-debugify``. 237b429a0feSVedant Kumar 238b429a0feSVedant KumarThe first applies synthetic debug information to every instruction of the 239b429a0feSVedant Kumarmodule, and the second checks that this DI is still available after an 240b429a0feSVedant Kumaroptimization has occurred, reporting any errors/warnings while doing so. 241b429a0feSVedant Kumar 242b429a0feSVedant KumarThe instructions are assigned sequentially increasing line locations, and are 243b429a0feSVedant Kumarimmediately used by debug value intrinsics everywhere possible. 244b429a0feSVedant Kumar 245b429a0feSVedant KumarFor example, here is a module before: 246b429a0feSVedant Kumar 247b429a0feSVedant Kumar.. code-block:: llvm 248b429a0feSVedant Kumar 249b429a0feSVedant Kumar define void @f(i32* %x) { 250b429a0feSVedant Kumar entry: 251b429a0feSVedant Kumar %x.addr = alloca i32*, align 8 252b429a0feSVedant Kumar store i32* %x, i32** %x.addr, align 8 253b429a0feSVedant Kumar %0 = load i32*, i32** %x.addr, align 8 254b429a0feSVedant Kumar store i32 10, i32* %0, align 4 255b429a0feSVedant Kumar ret void 256b429a0feSVedant Kumar } 257b429a0feSVedant Kumar 258b429a0feSVedant Kumarand after running ``opt -debugify``: 259b429a0feSVedant Kumar 260b429a0feSVedant Kumar.. code-block:: llvm 261b429a0feSVedant Kumar 262b429a0feSVedant Kumar define void @f(i32* %x) !dbg !6 { 263b429a0feSVedant Kumar entry: 264b429a0feSVedant Kumar %x.addr = alloca i32*, align 8, !dbg !12 265b429a0feSVedant Kumar call void @llvm.dbg.value(metadata i32** %x.addr, metadata !9, metadata !DIExpression()), !dbg !12 266b429a0feSVedant Kumar store i32* %x, i32** %x.addr, align 8, !dbg !13 267b429a0feSVedant Kumar %0 = load i32*, i32** %x.addr, align 8, !dbg !14 268b429a0feSVedant Kumar call void @llvm.dbg.value(metadata i32* %0, metadata !11, metadata !DIExpression()), !dbg !14 269b429a0feSVedant Kumar store i32 10, i32* %0, align 4, !dbg !15 270b429a0feSVedant Kumar ret void, !dbg !16 271b429a0feSVedant Kumar } 272b429a0feSVedant Kumar 273b429a0feSVedant Kumar !llvm.dbg.cu = !{!0} 274b429a0feSVedant Kumar !llvm.debugify = !{!3, !4} 275b429a0feSVedant Kumar !llvm.module.flags = !{!5} 276b429a0feSVedant Kumar 277b429a0feSVedant Kumar !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) 278b429a0feSVedant Kumar !1 = !DIFile(filename: "debugify-sample.ll", directory: "/") 279b429a0feSVedant Kumar !2 = !{} 280b429a0feSVedant Kumar !3 = !{i32 5} 281b429a0feSVedant Kumar !4 = !{i32 2} 282b429a0feSVedant Kumar !5 = !{i32 2, !"Debug Info Version", i32 3} 283b429a0feSVedant Kumar !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) 284b429a0feSVedant Kumar !7 = !DISubroutineType(types: !2) 285b429a0feSVedant Kumar !8 = !{!9, !11} 286b429a0feSVedant Kumar !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10) 287b429a0feSVedant Kumar !10 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned) 288b429a0feSVedant Kumar !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 3, type: !10) 289b429a0feSVedant Kumar !12 = !DILocation(line: 1, column: 1, scope: !6) 290b429a0feSVedant Kumar !13 = !DILocation(line: 2, column: 1, scope: !6) 291b429a0feSVedant Kumar !14 = !DILocation(line: 3, column: 1, scope: !6) 292b429a0feSVedant Kumar !15 = !DILocation(line: 4, column: 1, scope: !6) 293b429a0feSVedant Kumar !16 = !DILocation(line: 5, column: 1, scope: !6) 294b429a0feSVedant Kumar 295b429a0feSVedant KumarUsing ``debugify`` 296b429a0feSVedant Kumar^^^^^^^^^^^^^^^^^^ 297b429a0feSVedant Kumar 298b429a0feSVedant KumarA simple way to use ``debugify`` is as follows: 299b429a0feSVedant Kumar 300b429a0feSVedant Kumar.. code-block:: bash 301b429a0feSVedant Kumar 302b429a0feSVedant Kumar $ opt -debugify -pass-to-test -check-debugify sample.ll 303b429a0feSVedant Kumar 304b429a0feSVedant KumarThis will inject synthetic DI to ``sample.ll`` run the ``pass-to-test`` and 305b429a0feSVedant Kumarthen check for missing DI. The ``-check-debugify`` step can of course be 306b429a0feSVedant Kumaromitted in favor of more customizable FileCheck directives. 307b429a0feSVedant Kumar 308b429a0feSVedant KumarSome other ways to run debugify are available: 309b429a0feSVedant Kumar 310b429a0feSVedant Kumar.. code-block:: bash 311b429a0feSVedant Kumar 312b429a0feSVedant Kumar # Same as the above example. 313b429a0feSVedant Kumar $ opt -enable-debugify -pass-to-test sample.ll 314b429a0feSVedant Kumar 315b429a0feSVedant Kumar # Suppresses verbose debugify output. 316b429a0feSVedant Kumar $ opt -enable-debugify -debugify-quiet -pass-to-test sample.ll 317b429a0feSVedant Kumar 318b429a0feSVedant Kumar # Prepend -debugify before and append -check-debugify -strip after 319b429a0feSVedant Kumar # each pass on the pipeline (similar to -verify-each). 320b429a0feSVedant Kumar $ opt -debugify-each -O2 sample.ll 321b429a0feSVedant Kumar 322b429a0feSVedant KumarIn order for ``check-debugify`` to work, the DI must be coming from 323b429a0feSVedant Kumar``debugify``. Thus, modules with existing DI will be skipped. 324b429a0feSVedant Kumar 325b429a0feSVedant Kumar``debugify`` can be used to test a backend, e.g: 326b429a0feSVedant Kumar 327b429a0feSVedant Kumar.. code-block:: bash 328b429a0feSVedant Kumar 329b429a0feSVedant Kumar $ opt -debugify < sample.ll | llc -o - 330b429a0feSVedant Kumar 331b429a0feSVedant KumarThere is also a MIR-level debugify pass that can be run before each backend 332b429a0feSVedant Kumarpass, see: 33324660ea1SVedant Kumar:ref:`Mutation testing for MIR-level transformations<MIRDebugify>`. 334b429a0feSVedant Kumar 335b429a0feSVedant Kumar``debugify`` in regression tests 336b429a0feSVedant Kumar^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 337b429a0feSVedant Kumar 338b429a0feSVedant KumarThe output of the ``debugify`` pass must be stable enough to use in regression 339b429a0feSVedant Kumartests. Changes to this pass are not allowed to break existing tests. 340b429a0feSVedant Kumar 341b429a0feSVedant Kumar.. note:: 342b429a0feSVedant Kumar 343b429a0feSVedant Kumar Regression tests must be robust. Avoid hardcoding line/variable numbers in 344b429a0feSVedant Kumar check lines. In cases where this can't be avoided (say, if a test wouldn't 345b429a0feSVedant Kumar be precise enough), moving the test to its own file is preferred. 346b429a0feSVedant Kumar 34724660ea1SVedant Kumar.. _MIRDebugify: 348b429a0feSVedant Kumar 3491a2b3536SDjordje TodorovicTest original debug info preservation in optimizations 3501a2b3536SDjordje Todorovic------------------------------------------------------ 3511a2b3536SDjordje Todorovic 3521a2b3536SDjordje TodorovicIn addition to automatically generating debug info, the checks provided by 3531a2b3536SDjordje Todorovicthe ``debugify`` utility pass can also be used to test the preservation of 3541a2b3536SDjordje Todorovicpre-existing debug info metadata. It could be run as follows: 3551a2b3536SDjordje Todorovic 3561a2b3536SDjordje Todorovic.. code-block:: bash 3571a2b3536SDjordje Todorovic 3581a2b3536SDjordje Todorovic # Run the pass by checking original Debug Info preservation. 3591a2b3536SDjordje Todorovic $ opt -verify-debuginfo-preserve -pass-to-test sample.ll 3601a2b3536SDjordje Todorovic 3611a2b3536SDjordje Todorovic # Check the preservation of original Debug Info after each pass. 3621a2b3536SDjordje Todorovic $ opt -verify-each-debuginfo-preserve -O2 sample.ll 3631a2b3536SDjordje Todorovic 364*c5600aefSNikola TesicLimit number of observed functions to speed up the analysis: 365*c5600aefSNikola Tesic 366*c5600aefSNikola Tesic.. code-block:: bash 367*c5600aefSNikola Tesic 368*c5600aefSNikola Tesic # Test up to 100 functions (per compile unit) per pass. 369*c5600aefSNikola Tesic $ opt -verify-each-debuginfo-preserve -O2 -debugify-func-limit=100 sample.ll 370*c5600aefSNikola Tesic 371*c5600aefSNikola TesicPlease do note that running ``-verify-each-debuginfo-preserve`` on big projects 372*c5600aefSNikola Tesiccould be heavily time consuming. Therefore, we suggest using 373*c5600aefSNikola Tesic``-debugify-func-limit`` with a suitable limit number to prevent extremely long 374*c5600aefSNikola Tesicbuilds. 375*c5600aefSNikola Tesic 3769f41c03fSDjordje TodorovicFurthermore, there is a way to export the issues that have been found into 3779f41c03fSDjordje Todorovica JSON file as follows: 3789f41c03fSDjordje Todorovic 3799f41c03fSDjordje Todorovic.. code-block:: bash 3809f41c03fSDjordje Todorovic 3819f41c03fSDjordje Todorovic $ opt -verify-debuginfo-preserve -verify-di-preserve-export=sample.json -pass-to-test sample.ll 3829f41c03fSDjordje Todorovic 3839f41c03fSDjordje Todorovicand then use the ``llvm/utils/llvm-original-di-preservation.py`` script 3849f41c03fSDjordje Todorovicto generate an HTML page with the issues reported in a more human readable form 3859f41c03fSDjordje Todorovicas follows: 3869f41c03fSDjordje Todorovic 3879f41c03fSDjordje Todorovic.. code-block:: bash 3889f41c03fSDjordje Todorovic 3899f41c03fSDjordje Todorovic $ llvm-original-di-preservation.py sample.json sample.html 3909f41c03fSDjordje Todorovic 3918420a533SDjordje TodorovicTesting of original debug info preservation can be invoked from front-end level 3928420a533SDjordje Todorovicas follows: 3938420a533SDjordje Todorovic 3948420a533SDjordje Todorovic.. code-block:: bash 3958420a533SDjordje Todorovic 3968420a533SDjordje Todorovic # Test each pass. 3978420a533SDjordje Todorovic $ clang -Xclang -fverify-debuginfo-preserve -g -O2 sample.c 3988420a533SDjordje Todorovic 3998420a533SDjordje Todorovic # Test each pass and export the issues report into the JSON file. 4008420a533SDjordje Todorovic $ clang -Xclang -fverify-debuginfo-preserve -Xclang -fverify-debuginfo-preserve-export=sample.json -g -O2 sample.c 4018420a533SDjordje Todorovic 402b9076d11SDjordje TodorovicPlease do note that there are some known false positives, for source locations 403b9076d11SDjordje Todorovicand debug intrinsic checking, so that will be addressed as a future work. 404b9076d11SDjordje Todorovic 405b429a0feSVedant KumarMutation testing for MIR-level transformations 406b429a0feSVedant Kumar---------------------------------------------- 407b429a0feSVedant Kumar 40824660ea1SVedant KumarA variant of the ``debugify`` utility described in 40924660ea1SVedant Kumar:ref:`Mutation testing for IR-level transformations<IRDebugify>` can be used 41024660ea1SVedant Kumarfor MIR-level transformations as well: much like the IR-level pass, 41124660ea1SVedant Kumar``mir-debugify`` inserts sequentially increasing line locations to each 41239584ae5SXiang1 Zhang``MachineInstr`` in a ``Module``. And the MIR-level ``mir-check-debugify`` is 41339584ae5SXiang1 Zhangsimilar to IR-level ``check-debugify`` pass. 414b429a0feSVedant Kumar 415b429a0feSVedant KumarFor example, here is a snippet before: 416b429a0feSVedant Kumar 417b429a0feSVedant Kumar.. code-block:: llvm 418b429a0feSVedant Kumar 419b429a0feSVedant Kumar name: test 420b429a0feSVedant Kumar body: | 421b429a0feSVedant Kumar bb.1 (%ir-block.0): 422b429a0feSVedant Kumar %0:_(s32) = IMPLICIT_DEF 423b429a0feSVedant Kumar %1:_(s32) = IMPLICIT_DEF 424b429a0feSVedant Kumar %2:_(s32) = G_CONSTANT i32 2 425b429a0feSVedant Kumar %3:_(s32) = G_ADD %0, %2 426b429a0feSVedant Kumar %4:_(s32) = G_SUB %3, %1 427b429a0feSVedant Kumar 428b429a0feSVedant Kumarand after running ``llc -run-pass=mir-debugify``: 429b429a0feSVedant Kumar 430b429a0feSVedant Kumar.. code-block:: llvm 431b429a0feSVedant Kumar 432b429a0feSVedant Kumar name: test 433b429a0feSVedant Kumar body: | 434b429a0feSVedant Kumar bb.0 (%ir-block.0): 435b429a0feSVedant Kumar %0:_(s32) = IMPLICIT_DEF debug-location !12 436b429a0feSVedant Kumar DBG_VALUE %0(s32), $noreg, !9, !DIExpression(), debug-location !12 437b429a0feSVedant Kumar %1:_(s32) = IMPLICIT_DEF debug-location !13 438b429a0feSVedant Kumar DBG_VALUE %1(s32), $noreg, !11, !DIExpression(), debug-location !13 439b429a0feSVedant Kumar %2:_(s32) = G_CONSTANT i32 2, debug-location !14 440b429a0feSVedant Kumar DBG_VALUE %2(s32), $noreg, !9, !DIExpression(), debug-location !14 441b429a0feSVedant Kumar %3:_(s32) = G_ADD %0, %2, debug-location !DILocation(line: 4, column: 1, scope: !6) 442b429a0feSVedant Kumar DBG_VALUE %3(s32), $noreg, !9, !DIExpression(), debug-location !DILocation(line: 4, column: 1, scope: !6) 443b429a0feSVedant Kumar %4:_(s32) = G_SUB %3, %1, debug-location !DILocation(line: 5, column: 1, scope: !6) 444b429a0feSVedant Kumar DBG_VALUE %4(s32), $noreg, !9, !DIExpression(), debug-location !DILocation(line: 5, column: 1, scope: !6) 445b429a0feSVedant Kumar 446b429a0feSVedant KumarBy default, ``mir-debugify`` inserts ``DBG_VALUE`` instructions **everywhere** 447b429a0feSVedant Kumarit is legal to do so. In particular, every (non-PHI) machine instruction that 44824660ea1SVedant Kumardefines a register must be followed by a ``DBG_VALUE`` use of that def. If 449b429a0feSVedant Kumaran instruction does not define a register, but can be followed by a debug inst, 450b429a0feSVedant KumarMIRDebugify inserts a ``DBG_VALUE`` that references a constant. Insertion of 451b429a0feSVedant Kumar``DBG_VALUE``'s can be disabled by setting ``-debugify-level=locations``. 452b429a0feSVedant Kumar 453b429a0feSVedant KumarTo run MIRDebugify once, simply insert ``mir-debugify`` into your ``llc`` 454b429a0feSVedant Kumarinvocation, like: 455b429a0feSVedant Kumar 456b429a0feSVedant Kumar.. code-block:: bash 457b429a0feSVedant Kumar 458b429a0feSVedant Kumar # Before some other pass. 459b429a0feSVedant Kumar $ llc -run-pass=mir-debugify,other-pass ... 460b429a0feSVedant Kumar 461b429a0feSVedant Kumar # After some other pass. 462b429a0feSVedant Kumar $ llc -run-pass=other-pass,mir-debugify ... 463b429a0feSVedant Kumar 464b429a0feSVedant KumarTo run MIRDebugify before each pass in a pipeline, use 465b429a0feSVedant Kumar``-debugify-and-strip-all-safe``. This can be combined with ``-start-before`` 466b429a0feSVedant Kumarand ``-start-after``. For example: 467b429a0feSVedant Kumar 468b429a0feSVedant Kumar.. code-block:: bash 469b429a0feSVedant Kumar 470b429a0feSVedant Kumar $ llc -debugify-and-strip-all-safe -run-pass=... <other llc args> 471b429a0feSVedant Kumar $ llc -debugify-and-strip-all-safe -O1 <other llc args> 472b429a0feSVedant Kumar 47339584ae5SXiang1 ZhangIf you want to check it after each pass in a pipeline, use 47439584ae5SXiang1 Zhang``-debugify-check-and-strip-all-safe``. This can also be combined with 47539584ae5SXiang1 Zhang``-start-before`` and ``-start-after``. For example: 47639584ae5SXiang1 Zhang 47739584ae5SXiang1 Zhang.. code-block:: bash 47839584ae5SXiang1 Zhang 47939584ae5SXiang1 Zhang $ llc -debugify-check-and-strip-all-safe -run-pass=... <other llc args> 48039584ae5SXiang1 Zhang $ llc -debugify-check-and-strip-all-safe -O1 <other llc args> 48139584ae5SXiang1 Zhang 48239584ae5SXiang1 ZhangTo check all debug info from a test, use ``mir-check-debugify``, like: 48339584ae5SXiang1 Zhang 48439584ae5SXiang1 Zhang.. code-block:: bash 48539584ae5SXiang1 Zhang 48639584ae5SXiang1 Zhang $ llc -run-pass=mir-debugify,other-pass,mir-check-debugify 48739584ae5SXiang1 Zhang 488b429a0feSVedant KumarTo strip out all debug info from a test, use ``mir-strip-debug``, like: 489b429a0feSVedant Kumar 490b429a0feSVedant Kumar.. code-block:: bash 491b429a0feSVedant Kumar 492b429a0feSVedant Kumar $ llc -run-pass=mir-debugify,other-pass,mir-strip-debug 493b429a0feSVedant Kumar 49439584ae5SXiang1 ZhangIt can be useful to combine ``mir-debugify``, ``mir-check-debugify`` and/or 49539584ae5SXiang1 Zhang``mir-strip-debug`` to identify backend transformations which break in 49639584ae5SXiang1 Zhangthe presence of debug info. For example, to run the AArch64 backend tests 49739584ae5SXiang1 Zhangwith all normal passes "sandwiched" in between MIRDebugify and 49839584ae5SXiang1 ZhangMIRStripDebugify mutation passes, run: 499b429a0feSVedant Kumar 500b429a0feSVedant Kumar.. code-block:: bash 501b429a0feSVedant Kumar 502b429a0feSVedant Kumar $ llvm-lit test/CodeGen/AArch64 -Dllc="llc -debugify-and-strip-all-safe" 503b429a0feSVedant Kumar 50424660ea1SVedant KumarUsing LostDebugLocObserver 50524660ea1SVedant Kumar-------------------------- 506b429a0feSVedant Kumar 507b429a0feSVedant KumarTODO 508