[llvm] [CodeLayout] Size-aware machine block placement (PR #109711)

Zhaoxuan Jiang via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 11 22:29:16 PDT 2024


nocchijiang wrote:

I tested this patch on a Swift codebase and noticed that it tries to move statically-known code block (0% probability) right after its (only) successor, instead of keeping it at the end of the function. I'm not sure if it is intended, but it looks counter-intuitive, brings no code size benefit, and may even hurt compression ratio.

To add more background to it, Swift compiler tends to generate a lot of runtime failure traps at the end of a function that looks like a series of `brk #1` instructions (arm64). These traps are made of a non-mergeable basic block with 0% branch probability. The compiler emits a check to certain condition and a jump to the trap if the condition does not hold.

Here is an example that I extracted from a bad case:

Before MBP:

```llvm
bb.10 (%ir-block.40):
; predecessors: %bb.9
  successors: %bb.13(0x00000800), %bb.11(0x7ffff800); %bb.13(0.00%), %bb.11(100.00%)

  BL @objc_retainAutoreleasedReturnValue, <regmask $fp $lr $wzr $xzr $b8 $b9 $b10 $b11 $b12 $b13 $b14 $b15 $d8 $d9 $d10 $d11 $d12 $d13 $d14 $d15 $h8 $h9 $h10 $h11 $h12 $h13 $h14 $h15 $s8 $s9 $s10 $s11 $s12 and 55 more...>, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $sp, implicit-def $x0
  CBZX $x0, %bb.13

bb.11 (%ir-block.47):
; predecessors: %bb.10
  successors: %bb.12(0x80000000); %bb.12(100.00%)
  liveins: $x0:0x0000000000000008

  BL @objc_release, <regmask $fp $lr $wzr $xzr $b8 $b9 $b10 $b11 $b12 $b13 $b14 $b15 $d8 $d9 $d10 $d11 $d12 $d13 $d14 $d15 $h8 $h9 $h10 $h11 $h12 $h13 $h14 $h15 $s8 $s9 $s10 $s11 $s12 and 55 more...>, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $sp

bb.12 (%ir-block.49):
; predecessors: %bb.8, %bb.9, %bb.11

  RET undef $lr

bb.13 (%ir-block.50):
; predecessors: %bb.10

  INLINEASM &"" [sideeffect] [attdialect], $0:[imm], 0
  BRK 1
```

After MBP:

```llvm
bb.10 (%ir-block.40):
; predecessors: %bb.9
  successors: %bb.13(0x00000800), %bb.11(0x7ffff800); %bb.13(0.00%), %bb.11(100.00%)

  BL @objc_retainAutoreleasedReturnValue, <regmask $fp $lr $wzr $xzr $b8 $b9 $b10 $b11 $b12 $b13 $b14 $b15 $d8 $d9 $d10 $d11 $d12 $d13 $d14 $d15 $h8 $h9 $h10 $h11 $h12 $h13 $h14 $h15 $s8 $s9 $s10 $s11 $s12 and 55 more...>, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $sp, implicit-def $x0
  CBZNX $x0, %bb.11

bb.13 (%ir-block.50):
; predecessors: %bb.10

  INLINEASM &"" [sideeffect] [attdialect], $0:[imm], 0
  BRK 1

bb.11 (%ir-block.47):
; predecessors: %bb.10
  successors: %bb.12(0x80000000); %bb.12(100.00%)
  liveins: $x0:0x0000000000000008

  BL @objc_release, <regmask $fp $lr $wzr $xzr $b8 $b9 $b10 $b11 $b12 $b13 $b14 $b15 $d8 $d9 $d10 $d11 $d12 $d13 $d14 $d15 $h8 $h9 $h10 $h11 $h12 $h13 $h14 $h15 $s8 $s9 $s10 $s11 $s12 and 55 more...>, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $sp

bb.12 (%ir-block.49):
; predecessors: %bb.8, %bb.9, %bb.11

  RET undef $lr
```

https://github.com/llvm/llvm-project/pull/109711


More information about the llvm-commits mailing list