Skip to content

feat(MachO): Cranelift: align stack prologue/epilogue with compact unwind format#13586

Merged
cfallin merged 5 commits into
bytecodealliance:mainfrom
marxin:macho-eh-main
Jun 8, 2026
Merged

feat(MachO): Cranelift: align stack prologue/epilogue with compact unwind format#13586
cfallin merged 5 commits into
bytecodealliance:mainfrom
marxin:macho-eh-main

Conversation

@marxin
Copy link
Copy Markdown
Contributor

@marxin marxin commented Jun 8, 2026

This change is motivated by a compatibility difference between LLVM and the Cranelift compiler. The PR enables Cranelift to emit proper compact unwind information for the Mach-O AArch64 platform, based on UnwindInst and FinalizedMachCallSite.

The change has two parts:

  1. Unwind instructions only describe which pairs of callee-save registers are saved and restored. The canonical stack frame layout is still expected: nonvolatile registers must be placed near the top of the frame, immediately below the return address.
  2. The unwind encoding only supports register pairs emitted with stp; it cannot encode individual registers. This results in slightly larger stack frames, but the increase should hopefully be manageable.

Collected statistics for a larger WASM module (total function count: ~37K):

function count: diff: newly spilled registers
9828 diff=+0: newly spilled=0
1958 diff=+0: newly spilled=10
3457 diff=+0: newly spilled=2
3828 diff=+0: newly spilled=4
1038 diff=+0: newly spilled=6
 552 diff=+0: newly spilled=8
 639 diff=+1: newly spilled=10
 882 diff=+1: newly spilled=2
6639 diff=+1: newly spilled=4
2393 diff=+1: newly spilled=6
1072 diff=+1: newly spilled=8
 359 diff=+2: newly spilled=10
2229 diff=+2: newly spilled=4
1062 diff=+2: newly spilled=6
 523 diff=+2: newly spilled=8
 118 diff=+3: newly spilled=10
 505 diff=+3: newly spilled=6
 226 diff=+3: newly spilled=8
  54 diff=+4: newly spilled=10
  68 diff=+4: newly spilled=8
   7 diff=+5: newly spilled=10

Pair-register encoding constant defined in LLVM:
https://github.com/llvm/llvm-project/blob/main/libunwind/include/mach-o/compact_unwind_encoding.h?utm_source=chatgpt.com#L292-L300

@marxin marxin requested a review from a team as a code owner June 8, 2026 08:50
@marxin marxin requested review from alexcrichton and removed request for a team June 8, 2026 08:50
Comment thread cranelift/codegen/src/isa/aarch64/abi.rs Outdated
@marxin marxin requested a review from a team as a code owner June 8, 2026 10:14
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:meta Everything related to the meta-language. wasmtime:api Related to the API of the `wasmtime` crate itself labels Jun 8, 2026
Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable to me -- thanks a bunch for the contribution!

In addition to the nit below, could you add a compile filetest showing the assembly (including cases where the logic adds a new clobber) as well?

Comment thread cranelift/codegen/src/isa/aarch64/abi.rs Outdated
@alexcrichton alexcrichton requested a review from cfallin June 8, 2026 18:35
@cfallin cfallin enabled auto-merge June 8, 2026 18:35
@alexcrichton alexcrichton removed their request for review June 8, 2026 18:35
@marxin
Copy link
Copy Markdown
Contributor Author

marxin commented Jun 8, 2026

In addition to the nit below, could you add a compile filetest showing the assembly (including cases where the logic adds a new clobber) as well?

Included a test-case, where switching set enable_compact_unwind_abi=true to false leads to the following diff:

    --- expected
    +++ actual
    @@ -1,8 +1,8 @@
     VCode:
       stp fp, lr, [sp, #-16]!
       mov fp, sp
    -  stp x20, x19, [sp, #-16]!
    -  stp x22, x21, [sp, #-16]!
    +  str x21, [sp, #-16]!
    +  stp x19, x20, [sp, #-16]!
     block0:
       mov x19, x2
       mov x20, x1
    @@ -13,8 +13,8 @@
       add x6, x0, x1
       mov x2, x19
       add x0, x6, x2
    -  ldp x22, x21, [sp], #16
    -  ldp x20, x19, [sp], #16
    +  ldp x19, x20, [sp], #16
    +  ldr x21, [sp], #16
       ldp fp, lr, [sp], #16
       ret

@cfallin cfallin added this pull request to the merge queue Jun 8, 2026
Merged via the queue into bytecodealliance:main with commit 51b7ea5 Jun 8, 2026
51 checks passed
@marxin marxin deleted the macho-eh-main branch June 8, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:meta Everything related to the meta-language. cranelift Issues related to the Cranelift code generator wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants