Skip to content

Commit b878245

Browse files
committed
[DebugInfo][Docs] Improve code formatting in instruction referencing doc
This adds code blocks and inline code formatting to improve the readability of the instruction referencing doc. Reviewed By: Orlando Differential Revision: https://reviews.llvm.org/D126767
1 parent f4baf63 commit b878245

File tree

1 file changed

+66
-52
lines changed

1 file changed

+66
-52
lines changed

llvm/docs/InstrRefDebugInfo.md

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ instead in instruction referencing mode, LLVM refers to the machine instruction
2222
and operand position that the value is defined in. Consider the LLVM IR way of
2323
referring to instruction values:
2424

25-
%2 = add i32 %0, %1
26-
call void @llvm.dbg.value(metadata i32 %2,
25+
```llvm
26+
%2 = add i32 %0, %1
27+
call void @llvm.dbg.value(metadata i32 %2,
28+
```
2729

2830
In LLVM IR, the IR Value is synonymous with the instruction that computes the
2931
value, to the extent that in memory a Value is a pointer to the computing
@@ -32,70 +34,76 @@ codegen backend of LLVM, after instruction selection. Consider the X86 assembly
3234
below and instruction referencing debug info, corresponding to the earlier
3335
LLVM IR:
3436

35-
%2:gr32 = ADD32rr %0, %1, implicit-def $eflags, debug-instr-number 1
36-
DBG_INSTR_REF 1, 0, !123, !456, debug-location !789
37+
```text
38+
%2:gr32 = ADD32rr %0, %1, implicit-def $eflags, debug-instr-number 1
39+
DBG_INSTR_REF 1, 0, !123, !456, debug-location !789
40+
```
3741

38-
While the function remains in SSA form, virtual register %2 is sufficient to
42+
While the function remains in SSA form, virtual register `%2` is sufficient to
3943
identify the value computed by the instruction -- however the function
4044
eventually leaves SSA form, and register optimisations will obscure which
4145
register the desired value is in. Instead, a more consistent way of identifying
42-
the instruction's value is to refer to the MachineOperand where the value is
43-
defined: independently of which register is defined by that MachineOperand. In
44-
the code above, the DBG_INSTR_REF instruction refers to instruction number one,
45-
operand zero, while the ADD32rr has a debug-instr-number attribute attached
46-
indicating that it is instruction number one.
46+
the instruction's value is to refer to the `MachineOperand` where the value is
47+
defined: independently of which register is defined by that `MachineOperand`. In
48+
the code above, the `DBG_INSTR_REF` instruction refers to instruction number
49+
one, operand zero, while the `ADD32rr` has a `debug-instr-number` attribute
50+
attached indicating that it is instruction number one.
4751

4852
De-coupling variable locations from registers avoids difficulties involving
4953
register allocation and optimisation, but requires additional instrumentation
5054
when the instructions are optimised instead. Optimisations that replace
5155
instructions with optimised versions that compute the same value must either
5256
preserve the instruction number, or record a substitution from the old
5357
instruction / operand number pair to the new instruction / operand pair -- see
54-
MachineFunction::substituteDebugValuesForInst. If debug info maintenance is not
55-
performed, or an instruction is eliminated as dead code, the variable location
56-
is safely dropped and marked "optimised out". The exception is instructions
57-
that are mutated rather than replaced, which always need debug info
58+
`MachineFunction::substituteDebugValuesForInst`. If debug info maintenance is
59+
not performed, or an instruction is eliminated as dead code, the variable
60+
location is safely dropped and marked "optimised out". The exception is
61+
instructions that are mutated rather than replaced, which always need debug info
5862
maintenance.
5963

6064
# Register allocator considerations
6165

6266
When the register allocator runs, debugging instructions do not directly refer
6367
to any virtual registers, and thus there is no need for expensive location
64-
maintenance during regalloc (i.e., LiveDebugVariables). Debug instructions are
68+
maintenance during regalloc (i.e. `LiveDebugVariables`). Debug instructions are
6569
unlinked from the function, then linked back in after register allocation
6670
completes.
6771

68-
The exception is PHI instructions: these become implicit definitions at control
69-
flow merges once regalloc finishes, and any debug numbers attached to PHI
70-
instructions are lost. To circumvent this, debug numbers of PHIs are recorded
71-
at the start of register allocation (phi-node-elimination), then DBG_PHI
72-
instructions are inserted after regalloc finishes. This requires some
72+
The exception is `PHI` instructions: these become implicit definitions at
73+
control flow merges once regalloc finishes, and any debug numbers attached to
74+
`PHI` instructions are lost. To circumvent this, debug numbers of `PHI`s are
75+
recorded at the start of register allocation (`phi-node-elimination`), then
76+
`DBG_PHI` instructions are inserted after regalloc finishes. This requires some
7377
maintenance of which register a variable is located in during regalloc, but at
7478
single positions (block entry points) rather than ranges of instructions.
7579

7680
An example, before regalloc:
7781

78-
bb.2:
79-
%2 = PHI %1, %bb.0, %2, %bb.1, debug-instr-number 1
82+
```text
83+
bb.2:
84+
%2 = PHI %1, %bb.0, %2, %bb.1, debug-instr-number 1
85+
```
8086

8187
After:
8288

83-
bb.2:
84-
DBG_PHI $rax, 1
89+
```text
90+
bb.2:
91+
DBG_PHI $rax, 1
92+
```
8593

86-
# LiveDebugValues
94+
# `LiveDebugValues`
8795

8896
After optimisations and code layout complete, information about variable
8997
values must be translated into variable locations, i.e. registers and stack
90-
slots. This is performed in the [LiveDebugValues pass][LiveDebugValues], where
98+
slots. This is performed in the [LiveDebugValues pass][`LiveDebugValues`], where
9199
the debug instructions and machine code are separated out into two independent
92100
functions:
93101
* One that assigns values to variable names,
94102
* One that assigns values to machine registers and stack slots.
95103

96-
LLVM's existing SSA tools are used to place PHIs for each function, between
104+
LLVM's existing SSA tools are used to place `PHI`s for each function, between
97105
variable values and the values contained in machine locations, with value
98-
propagation eliminating any un-necessary PHIs. The two can then be joined up
106+
propagation eliminating any unnecessary `PHI`s. The two can then be joined up
99107
to map variables to values, then values to locations, for each instruction in
100108
the function.
101109

@@ -107,36 +115,38 @@ for the full time that they are resident in the machine.
107115

108116
Instruction referencing will work on any target, but likely with poor coverage.
109117
Supporting instruction referencing well requires:
110-
* Target hooks to be implemented to allow LiveDebugValues to follow values through the machine,
111-
* Target-specific optimisations to be instrumented, to preserve instruction numbers.
118+
* Target hooks to be implemented to allow `LiveDebugValues` to follow values
119+
through the machine,
120+
* Target-specific optimisations to be instrumented, to preserve instruction
121+
numbers.
112122

113123
## Target hooks
114124

115-
TargetInstrInfo::isCopyInstrImpl must be implemented to recognise any
116-
instructions that are copy-like -- LiveDebugValues uses this to identify when
125+
`TargetInstrInfo::isCopyInstrImpl` must be implemented to recognise any
126+
instructions that are copy-like -- `LiveDebugValues` uses this to identify when
117127
values move between registers.
118128

119-
TargetInstrInfo::isLoadFromStackSlotPostFE and
120-
TargetInstrInfo::isStoreToStackSlotPostFE are needed to identify spill and
129+
`TargetInstrInfo::isLoadFromStackSlotPostFE` and
130+
`TargetInstrInfo::isStoreToStackSlotPostFE` are needed to identify spill and
121131
restore instructions. Each should return the destination or source register
122-
respectively. LiveDebugValues will track the movement of a value from / to
132+
respectively. `LiveDebugValues` will track the movement of a value from / to
123133
the stack slot. In addition, any instruction that writes to a stack spill
124-
should have a MachineMemoryOperand attached, so that LiveDebugValues can
134+
should have a `MachineMemoryOperand` attached, so that `LiveDebugValues` can
125135
recognise that a slot has been clobbered.
126136

127137
## Target-specific optimisation instrumentation
128138

129-
Optimisations come in two flavours: those that mutate a MachineInstr to make
139+
Optimisations come in two flavours: those that mutate a `MachineInstr` to make
130140
it do something different, and those that create a new instruction to replace
131141
the operation of the old.
132142

133143
The former _must_ be instrumented -- the relevant question is whether any
134144
register def in any operand will produce a different value, as a result of the
135-
mutation. If the answer is yes, then there is a risk that a DBG_INSTR_REF
145+
mutation. If the answer is yes, then there is a risk that a `DBG_INSTR_REF`
136146
instruction referring to that operand will end up assigning the different
137147
value to a variable, presenting the debugging developer with an unexpected
138-
variable value. In such scenarios, call MachineInstr::dropDebugNumber() on the
139-
mutated instruction to erase its instruction number. Any DBG_INSTR_REF
148+
variable value. In such scenarios, call `MachineInstr::dropDebugNumber()` on the
149+
mutated instruction to erase its instruction number. Any `DBG_INSTR_REF`
140150
referring to it will produce an empty variable location instead, that appears
141151
as "optimised out" in the debugger.
142152

@@ -145,36 +155,40 @@ an instruction number substitution: a mapping from the old instruction number /
145155
operand pair to new instruction number / operand pair. Consider if we replace
146156
a three-address add instruction with a two-address add:
147157

148-
%2:gr32 = ADD32rr %0, %1, debug-instr-number 1
158+
```text
159+
%2:gr32 = ADD32rr %0, %1, debug-instr-number 1
160+
```
149161

150162
becomes
151163

152-
%2:gr32 = ADD32rr %0(tied-def 0), %1, debug-instr-number 2
164+
```text
165+
%2:gr32 = ADD32rr %0(tied-def 0), %1, debug-instr-number 2
166+
```
153167

154168
With a substitution from "instruction number 1 operand 0" to "instruction number
155-
2 operand 0" recorded in the MachineFunction. In LiveDebugValues, DBG_INSTR_REFs
156-
will be mapped through the substitution table to find the most recent
157-
instruction number / operand number of the value it refers to.
169+
2 operand 0" recorded in the `MachineFunction`. In `LiveDebugValues`,
170+
`DBG_INSTR_REF`s will be mapped through the substitution table to find the most
171+
recent instruction number / operand number of the value it refers to.
158172

159-
Use MachineFunction::substituteDebugValuesForInst to automatically produce
173+
Use `MachineFunction::substituteDebugValuesForInst` to automatically produce
160174
substitutions between an old and new instruction. It assumes that any operand
161175
that is a def in the old instruction is a def in the new instruction at the
162176
same operand position. This works most of the time, for example in the example
163177
above.
164178

165179
If operand numbers do not line up between the old and new instruction, use
166-
MachineInstr::getDebugInstrNum to acquire the instruction number for the new
167-
instruction, and MachineFunction::makeDebugValueSubstitution to record the
180+
`MachineInstr::getDebugInstrNum` to acquire the instruction number for the new
181+
instruction, and `MachineFunction::makeDebugValueSubstitution` to record the
168182
mapping between register definitions in the old and new instructions. If some
169183
values computed by the old instruction are no longer computed by the new
170-
instruction, record no substitution -- LiveDebugValues will safely drop the
184+
instruction, record no substitution -- `LiveDebugValues` will safely drop the
171185
now unavailable variable value.
172186

173-
Should your target clone instructions, much the same as the TailDuplicator
187+
Should your target clone instructions, much the same as the `TailDuplicator`
174188
optimisation pass, do not attempt to preserve the instruction numbers or
175-
record any substitutions. MachineFunction::CloneMachineInstr should drop the
189+
record any substitutions. `MachineFunction::CloneMachineInstr` should drop the
176190
instruction number of any cloned instruction, to avoid duplicate numbers
177-
appearing to LiveDebugValues. Dealing with duplicated instructions is a
191+
appearing to `LiveDebugValues`. Dealing with duplicated instructions is a
178192
natural extension to instruction referencing that's currently unimplemented.
179193

180194
[LiveDebugValues]: SourceLevelDebugging.html#livedebugvalues-expansion-of-variable-locations

0 commit comments

Comments
 (0)