Egon Elbre

ixdiff

One of the problems, when making changes to the Go compiler, is determining whether the optimization was any good. Of course, the best approach is always benchmarking, but if you need to compare a large corpus it becomes prohibitively slow. However, you can get pretty good estimates just by comparing how the assembly instructions have changed.

The usual objdump and awk/diff combination works for small scale, but if you need to dump a 100MB binary, then it becomes really slow. That’s why I ended up conjuring ixdiff. I already had some experience with building lensm so I knew it’s relatively easy with packages from golang.org/x/arch and the standard library.

To install the tool:

1
go install github.com/loov/ixdiff@latest

Global stats #

So what does the tool do? In summary it compares and diffs binaries at the instruction level. It’s easier to show than to explain all the details.

Here’s comparing TypeScript Go binaries before and after a compiler optimization (or rather a deoptimization):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ixdiff tsgo.v0 tsgo.v3
functions: 5044 identical, 232 changed (+17229 relocations), 5 added, 8 removed
total text size delta: +6000 bytes
total spill delta: +181 registers moved
total stack traffic delta: +204 8-byte slots

instruction delta by opcode:
    +362 MOV
    +244 LDR
     +93 BL
    ...

package delta:
       bytes    insts   spills    slots  changed  added  removed  package
       +2784     +699     +102     +118       15      0        2  syscall
        +992     +247      +31      +34        2      2        0  fmt
    ...

top 100 by size delta:
       bytes    insts   spills    slots state     function
       +1216     +304      +48      +56 changed   syscall.forkExec
        +608     +152      +19      +22 added     fmt.wrapErrorf
    ...

This comparison gives the main statistics. It shows how much the binary size changed, ignoring alignment padding, and how the count of each instruction changed. Similarly it gives individual stats for each package and the functions that changed the most.

Seeing the most changed funcs can spot significant regressions immediately, without trying to dig further.

Add --json, if you want to consume the output programmatically.

Function diffs #

To dig further you can also compare each func individually:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ ixdiff --fn net/url.parseHost tsgo.v0 tsgo.v3
--- net/url.parseHost (1888 bytes)
+++ net/url.parseHost (1904 bytes)
@@ -10046b65c +10046c8ac @@
 10046b65c: ADD  $240, RSP, R2
 10046b660: ORR  $1, ZR, R3
 10046b664: MOVD R3, R4
+10046c8b8: ORR  $14, ZR, R5
 10046b668: CALL fmt.errorf(SB)
 10046b66c: CBNZ R0, L22

Or alternatively, if you have a large screen:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ ixdiff --side-by-side --fn net/url.parseHost tsgo.v0 tsgo.v3
--- net/url.parseHost (1888 bytes)
+++ net/url.parseHost (1904 bytes)
@@ -10046b65c +10046c8ac @@
10046b65c: ADD  $240, RSP, R2    10046c8ac: ADD  $240, RSP, R2
10046b660: ORR  $1, ZR, R3       10046c8b0: ORR  $1, ZR, R3
10046b664: MOVD R3, R4           10046c8b4: MOVD R3, R4
                               > 10046c8b8: ORR  $14, ZR, R5
10046b668: CALL fmt.errorf(SB)   10046c8bc: CALL fmt.errorf(SB)
10046b66c: CBNZ R0, L22          10046c8c0: CBNZ R0, L22

Detecting spills #

If you have spent a lot of time optimizing the Go compiler, you may wonder where the “spill” number comes from, since the binary does not contain that information. Spills are quite an important metric for how everything optimizes, effectively, it’s somewhat inversely related to the amount of work that can be done in registers.

Initially I was considering using -gcflags='-d=ssa/regalloc/debug=2', which would give the precise answer. The problem is that recompiling everything is adding a lot of work. Most of the spill information is in the binary, so we can just guess.

Let’s take a small function where values need to survive a call:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//go:noinline
func log(x int) {}

//go:noinline
func sum(a, b, c int) int {
	x := a * b
	y := b * c
	log(x)
	return x + y
}

Go’s register ABI passes a, b and c in R0, R1 and R2, but every register is clobbered by the call to log. So the compiler has to put them somewhere safe, which means the stack. Here’s the arm64 disassembly with the stack accesses marked:

 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
$ go tool objdump -s main.sum spill.v1
MOVD 16(R28), R16
CMP R16, RSP
BLS 17(PC)
MOVD.W R30, -32(RSP)       // save link register
MOVD R29, -8(RSP)          // save frame pointer
SUB $8, RSP, R29
MOVD R0, 40(RSP)           // spill a
MOVD R1, 48(RSP)           // spill b
MOVD R2, 56(RSP)           // spill c
MUL R0, R1, R0
CALL main.log(SB)
MOVD 40(RSP), R1           // reload a
MOVD 56(RSP), R2           // reload c
ADD R2, R1, R1
MOVD 48(RSP), R2           // reload b
MUL R2, R1, R0             // (a + c) * b
MOVD -8(RSP), R29          // restore frame pointer
MOVD.P 32(RSP), R30        // restore link register
RET
STP (R0, R1), 8(RSP)       // spill a, b for morestack
MOVD R2, 24(RSP)           // spill c for morestack
MOVD R30, R3
CALL runtime.morestack_noctxt.abi0(SB)
LDP 8(RSP), (R0, R1)       // reload a, b
MOVD 24(RSP), R2           // reload c
JMP main.sum(SB)

Since a spill means storing something on the stack, it’s easy to spot. Effectively, we can treat any move to/from stack as a spill/reload. The compiler sometimes computes a stack address into a scratch register first, e.g. ADD $4096, RSP, R27 followed by MOVD R0, 8(R27), so ixdiff tracks those registers too.

Similarly, there are other things that write/read the stack, which are not spills/reloads. For example frame pointer and link register saves. Most of that noise cancels out, when comparing binaries… we mostly care about the delta anyways.

1
2
3
4
5
6
$ ixdiff spill.v0 spill.v1
functions: 0 identical, 1 changed (+0 relocations), 0 added, 0 removed
total text size delta: +96 bytes
total spill delta: +16 registers moved
total stack traffic delta: +16 8-byte slots
...

In this example, the noise doesn’t cancel out. Without the call, sum is a leaf function with no frame at all: ADD R2, R0, R2; MUL R2, R1, R0; RET. So all 16 show up in the delta, even though only 6 of them are real spills and reloads; the rest are the frame setup and the morestack path.

Ignoring slot layout #

Another related problem is slot layout. Different optimizations may inadvertently change where registers are spilled on the stack – i.e. slots. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//go:noinline
func fill(buf *[2]int) {}

//go:noinline
func sum(a, b, c int) int {
	var buf [2]int
	fill(&buf)
	x := a * b
	y := b * c
	log(x)
	return x + y
}

The frame grows and every slot moves along with it. The diff ends up being mostly offsets:

 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
$ ixdiff --side-by-side --fn main.sum slot.v0 slot.v1
--- main.sum (112 bytes)
+++ main.sum (128 bytes)
@@ -100074570 +100074580 @@
100074570: MOVD   16(R28), R16       100074580: MOVD   16(R28), R16
100074574: CMP    R16, RSP           100074584: CMP    R16, RSP
100074578: BLS    L2                 100074588: BLS    L2
10007457c: MOVD.W R30, -32(RSP)    | 10007458c: MOVD.W R30, -48(RSP)
100074580: MOVD   R29, -8(RSP)       100074590: MOVD   R29, -8(RSP)
100074584: SUB    $8, RSP, R29       100074594: SUB    $8, RSP, R29
100074588: MOVD   R0, 40(RSP)      | 100074598: MOVD   R0, 56(RSP)
10007458c: MOVD   R1, 48(RSP)      | 10007459c: MOVD   R1, 64(RSP)
100074590: MOVD   R2, 56(RSP)      | 1000745a0: MOVD   R2, 72(RSP)
100074594: MUL    R0, R1, R0       | 1000745a4: STP    (ZR, ZR), 24(RSP)
100074598: CALL   main.log(SB)     | 1000745a8: ADD    $24, RSP, R0
10007459c: MOVD   40(RSP), R1      | 1000745ac: CALL   main.fill(SB)
                                   > 1000745b0: MOVD   64(RSP), R1
1000745a0: MOVD   56(RSP), R2        1000745b4: MOVD   56(RSP), R2
                                   > 1000745b8: MUL    R2, R1, R0
                                   > 1000745bc: CALL   main.log(SB)
                                   > 1000745c0: MOVD   56(RSP), R1
                                   > 1000745c4: MOVD   72(RSP), R2
1000745a4: ADD    R2, R1, R1         1000745c8: ADD    R2, R1, R1
1000745a8: MOVD   48(RSP), R2      | 1000745cc: MOVD   64(RSP), R2
1000745ac: MUL    R2, R1, R0         1000745d0: MUL    R2, R1, R0
1000745b0: MOVD   -8(RSP), R29       1000745d4: MOVD   -8(RSP), R29
1000745b4: MOVD.P 32(RSP), R30     | 1000745d8: MOVD.P 48(RSP), R30
1000745b8: RET                       1000745dc: RET
...

Rather than try to figure out what is stored in each stack slot, I figured there’s a hacky way to work around it. Just ignore the stack offsets when doing the diff. This is the --mask-sp flag:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ ixdiff --side-by-side --mask-sp --fn main.sum slot.v0 slot.v1
--- main.sum (112 bytes)
+++ main.sum (128 bytes)
@@ -100074588 +100074598 @@
100074588: MOVD R0, <sp>(RSP)   100074598: MOVD R0, <sp>(RSP)
10007458c: MOVD R1, <sp>(RSP)   10007459c: MOVD R1, <sp>(RSP)
100074590: MOVD R2, <sp>(RSP)   1000745a0: MOVD R2, <sp>(RSP)
100074594: MUL  R0, R1, R0    | 1000745a4: STP  (ZR, ZR), <sp>(RSP)
                              > 1000745a8: ADD  $24, RSP, R0
                              > 1000745ac: CALL main.fill(SB)
                              > 1000745b0: MOVD <sp>(RSP), R1
                              > 1000745b4: MOVD <sp>(RSP), R2
                              > 1000745b8: MUL  R2, R1, R0
100074598: CALL main.log(SB)    1000745bc: CALL main.log(SB)
10007459c: MOVD <sp>(RSP), R1   1000745c0: MOVD <sp>(RSP), R1
1000745a0: MOVD <sp>(RSP), R2   1000745c4: MOVD <sp>(RSP), R2
...

Now we can much more clearly see what happened between the funcs. However, it also ends up hiding genuine slot changes, which might be relevant. I’m still wondering whether there’s a better way to handle this.

C, Rust, Wasm and friends #

ixdiff isn’t limited to Go binaries. It can disassemble ELF, Mach-O and PE binaries for most common architectures (amd64, arm64, riscv64, …). Here’s the same sum example written in C:

1
2
3
4
5
6
7
8
__attribute__((noinline)) long sum(long a, long b, long c) {
	long x = a * b;
	long y = b * c;
#ifdef WITHLOG
	logx(x);
#endif
	return x + y;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$ ixdiff --side-by-side --fn sum c.v0 c.v1
--- sum (12 bytes)
+++ sum (60 bytes)
@@ -10000032c +10000032c @@
10000032c: ADD   R0, R2, R8 | 10000032c: STP.W (R22, R21), -48(RSP)
100000330: MUL   R1, R8, R0 | 100000330: STP   (R20, R19), 16(RSP)
                            > 100000334: STP   (R29, R30), 32(RSP)
                            > 100000338: ADD   $32, RSP, R29
                            > 10000033c: MOVD  R2, R19
                            > 100000340: MOVD  R1, R20
                            > 100000344: MOVD  R0, R21
                            > 100000348: MUL   R0, R1, R0
                            > 10000034c: CALL  logx(SB)
                            > 100000350: ADD   R21, R19, R8
                            > 100000354: MUL   R20, R8, R0
                            > 100000358: LDP   32(RSP), (R29, R30)
                            > 10000035c: LDP   16(RSP), (R20, R19)
                            > 100000360: LDP.P 48(RSP), (R22, R21)
100000334: RET                100000364: RET

The output uses Go assembly syntax, since that’s what golang.org/x/arch uses. Rust, compiled with rustc -O, works as well:

1
2
3
4
5
6
$ ixdiff rs.v0 rs.v1
functions: 341 identical, 2 changed (+270 relocations), 1 added, 0 removed
total text size delta: +64 bytes
total spill delta: +13 registers moved
total stack traffic delta: +13 8-byte slots
...

PS: Some of the heuristics for spills/reloads might be wrong for them.

Finally Wasm, here the Go example built with GOOS=wasip1 GOARCH=wasm:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ ixdiff --side-by-side --fn main.sum wasm.v0 wasm.v1
--- main.sum (60 bytes)
+++ main.sum (178 bytes)
@@ -14e00000 +14e10000 @@
...
                                     > 14e10000: local.set 1
                                     > 14e10000: br_if    2
                                     > 14e10001: end
                                     > 14e10001: local.get 1
14e00000: i64.extend_i32_u             14e10001: i64.extend_i32_u
14e00000: i64.const 32               | 14e10001: i64.const 40
14e00000: i64.add                      14e10001: i64.add
14e00000: i32.wrap_i64                 14e10001: i32.wrap_i64
14e00000: local.get 1                  14e10001: local.get 1
14e00000: i64.load offset=16 align=8   14e10001: i64.load offset=16 align=8
14e00000: local.get 1                  14e10001: local.get 1
14e00000: i64.load offset=8 align=8  | 14e10001: i64.load offset=32 align=8
                                     > 14e10001: i64.add
14e00000: local.get 1                  14e10001: local.get 1
...

The diffing works, but the spill heuristic doesn’t, because Wasm has no stack pointer register.

Conclusion #

ixdiff won’t replace benchmarking, but it’s a quick way to see what a compiler change actually did across a large codebase, before you spend hours running benchmarks.

If you have any feature requests or ideas for better heuristics, feel free to create an issue in github.com/loov/ixdiff.