Repository navigation
Expand file tree
/
Copy pathblocks.go
More file actions
223 lines (208 loc) · 6.93 KB
/
Copy pathblocks.go
File metadata and controls
223 lines (208 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"fmt"
"io"
"strings"
"github.com/loov/ixdiff/internal/fndiff"
"github.com/loov/ixdiff/internal/norm"
"github.com/loov/ixdiff/ixdiff"
)
// A block is a run of instructions entered only at its first line: it
// starts at a branch target or after an unconditional control
// transfer.
type block struct {
lines []string
addrs []uint64
}
// content returns the block's comparison key.
func (b block) content() string { return strings.Join(b.lines, "\n") }
// unconditional lists the rendered mnemonics after which control never
// falls through, ending a block. It keys on the first word of the
// rendered text rather than the raw decoder Op, because GoSyntax
// normalizes returns and jumps to the same Go assembler names on every
// architecture (riscv64's raw Op is JAL/JALR for calls, jumps, and
// returns alike; arm's return is a raw LDR into PC rendered as RET).
var unconditional = map[string]bool{
// JMP and RET are the GoSyntax renderings on amd64, 386, arm64,
// riscv64, loong64, ppc64, and s390x.
"JMP": true, "RET": true,
"B": true, // arm unconditional branch (conditionals render as B.EQ etc.)
"BR": true, // ppc64 and s390x unconditional branch, arm64 indirect
// Traps and inter-function padding.
"UD2": true, "INT": true, "UDF": true, "UNIMP": true,
"BREAK": true, "EBREAK": true,
// wasm.
"return": true, "br": true, "br_table": true, "unreachable": true,
}
// splitBlocks cuts rendered lines into basic blocks. leaders marks
// instruction indices that start a block (branch targets) and ends
// marks instructions after which a block ends (any branch or
// unconditional transfer).
func splitBlocks(lines []string, addrs []uint64, leaders, ends map[int]bool) []block {
var blocks []block
var cur block
flush := func() {
if len(cur.lines) > 0 {
blocks = append(blocks, cur)
cur = block{}
}
}
for i, line := range lines {
if leaders[i] {
flush()
}
cur.lines = append(cur.lines, line)
cur.addrs = append(cur.addrs, addrs[i])
if ends[i] {
flush()
}
}
flush()
return blocks
}
// blockEnds marks every instruction that terminates a basic block: an
// intra-function branch (it has a label target), or an unconditional
// transfer.
func blockEnds(nl []norm.Line, insts []norm.Inst) map[int]bool {
ends := map[int]bool{}
for i, l := range nl {
op, _, _ := strings.Cut(insts[i].Text, " ")
if l.Target >= 0 || unconditional[op] {
ends[i] = true
}
}
return ends
}
// blockMove describes an identical block found at a different position.
type blockMove struct {
oldAddr, newAddr uint64
insts int
}
// matchBlocks pairs identical blocks across the two sides in order.
// It returns notes for blocks that moved, and the concatenated
// leftovers of each side for a conventional diff.
func matchBlocks(old, new []block) (moves []blockMove, restOld, restNew block) {
byContent := map[string][]int{}
for i, b := range old {
key := b.content()
byContent[key] = append(byContent[key], i)
}
matched := make([]int, len(new)) // new index -> old index, -1 unmatched
usedOld := make([]bool, len(old))
for i, b := range new {
matched[i] = -1
key := b.content()
for _, oi := range byContent[key] {
if !usedOld[oi] {
usedOld[oi] = true
matched[i] = oi
break
}
}
}
// The longest run of matched pairs in ascending old order stays in
// place; every other matched pair moved. A greedy scan would
// instead blame every block after one that moved to the front.
// The run is searched from the end so ties keep the blocks earliest
// in new order: of two swapped blocks, the later one moved.
var pairedNew, pairedOld []int
for i, oi := range matched {
if oi < 0 {
restNew.lines = append(restNew.lines, new[i].lines...)
restNew.addrs = append(restNew.addrs, new[i].addrs...)
continue
}
pairedNew = append(pairedNew, i)
pairedOld = append(pairedOld, oi)
}
n := len(pairedOld)
reversed := make([]int, n)
for k, oi := range pairedOld {
reversed[n-1-k] = -oi
}
inPlace := make([]bool, n)
for _, k := range fndiff.LongestIncreasing(reversed) {
inPlace[n-1-k] = true
}
for k, i := range pairedNew {
if !inPlace[k] {
moves = append(moves, blockMove{
oldAddr: old[pairedOld[k]].addrs[0],
newAddr: new[i].addrs[0],
insts: len(new[i].lines),
})
}
}
for i, b := range old {
if !usedOld[i] {
restOld.lines = append(restOld.lines, b.lines...)
restOld.addrs = append(restOld.addrs, b.addrs...)
}
}
return moves, restOld, restNew
}
// writeFuncBlocks prints a block-matched diff of one changed function:
// identical blocks that only moved become one-line notes, and only the
// genuinely unmatched instructions are diffed. Useful when the
// compiler reordered basic blocks (e.g. PGO), where a linear diff
// drowns in relocation of unchanged code.
//
// A block that both moved and changed shows as delete plus
// insert; fuzzy block matching would pair those, add it if PGO
// comparisons need it. Measured on a real PGO pair (ixdiff built with
// and without a profile of itself, 2026-07): moved-and-changed blocks
// reduced diff lines by under 1% in 3 of 34 changed functions, so
// fuzzy matching stays unimplemented.
func (c *cmdDiff) writeFuncBlocks(w io.Writer, p ixdiff.Pair) error {
old, new := c.objOld, c.objNew
oldName := p.Name
if p.RenamedFrom != "" {
oldName = p.RenamedFrom
}
oldF, newF := old.Func(oldName), new.Func(p.Name)
opts := norm.Options{MaskSP: c.maskSP, Arch: old.Arch}
oldInsts, err := old.Disassemble(oldF)
if err != nil {
return fmt.Errorf("disassembling old %s: %w", p.Name, err)
}
newInsts, err := new.Disassemble(newF)
if err != nil {
return fmt.Errorf("disassembling new %s: %w", p.Name, err)
}
oldOpts, newOpts := opts, opts
oldOpts.IsAddr, newOpts.IsAddr = old.Contains, new.Contains
oldOpts.DataSym, newOpts.DataSym = old.DataSym, new.DataSym
oldNL := norm.NormalizeLines(oldF.Name, oldInsts, oldOpts)
newNL := norm.NormalizeLines(newF.Name, newInsts, newOpts)
oldLines, newLines := fndiff.AlignLabels(oldNL, newNL)
oldBlocks := splitBlocks(oldLines, norm.Addrs(oldInsts), targetSet(oldNL), blockEnds(oldNL, oldInsts))
newBlocks := splitBlocks(newLines, norm.Addrs(newInsts), targetSet(newNL), blockEnds(newNL, newInsts))
moves, restOld, restNew := matchBlocks(oldBlocks, newBlocks)
writeDiffHeader(w, p)
for _, m := range moves {
fmt.Fprintf(w, "block of %d unchanged insts moved: %x -> %x\n", m.insts, m.oldAddr, m.newAddr)
}
if len(restOld.lines) == 0 && len(restNew.lines) == 0 {
if len(moves) == 0 {
fmt.Fprintf(w, "all blocks match\n")
}
return nil
}
rest := resolveLines(fndiff.Diff(restOld.lines, restNew.lines), restOld.addrs, restNew.addrs)
if c.sideBy {
writeHunksSide(w, rest, c.pal)
} else {
writeHunks(w, rest, c.pal)
}
return nil
}
// targetSet collects the branch-target instruction indices of lines.
func targetSet(lines []norm.Line) map[int]bool {
targets := map[int]bool{0: true}
for _, l := range lines {
if l.Target >= 0 {
targets[l.Target] = true
}
}
return targets
}