Skip to content

Fix AArch64 switch/table jumps (#25733)

Sven Tennie requested to merge wip/supersven/fix-aarch64-switch-table-jumps into master

This fixes the issue described in: #25733 (closed)

It looks like switch table jumps should not deallocate stack slots, at least X86 does not do it. (RISCV-64 NCG probably suffers from the same issue. But, I haven't tested that, yet.)

In the failing code, the jump was rendered as:

# Jump table for switch
        lsl x17, x17, #3 // CmmMachOp (MO_UU_Conv W32 W64) [CmmLoad (CmmMachOp (MO_Add W64) [CmmMachOp (MO_RelaxedRead W64) [CmmMachOp (MO_And W64) [CmmReg (CmmGlobal (GlobalRegUse {globalRegUse_reg = VanillaReg 1, globalRegUse_type = CmmType GcPtrCat W64})),CmmLit (CmmInt (-8) W64)]],CmmLit (CmmInt (-4) W64)]) (CmmType BitsCat W32) NaturallyAligned]
        add x17, x17, x15
        ldr x17, [ x17, 0 ]
        add x17, x17, x15
        add sp, sp, #3504 // Dealloc More Stack
        br x17

With this fix it becomes:

# Jump table for switch
        lsl x17, x17, #3 // CmmMachOp (MO_UU_Conv W32 W64) [CmmLoad (CmmMachOp (MO_Add W64) [CmmMachOp (MO_RelaxedRead W64) [CmmMachOp (MO_And W64) [CmmReg (CmmGlobal (GlobalRegUse {globalRegUse_reg = VanillaReg 1, globalRegUse_type = CmmType GcPtrCat W64})),CmmLit (CmmInt (-8) W64)]],CmmLit (CmmInt (-4) W64)]) (CmmType BitsCat W32) NaturallyAligned]
        add x17, x17, x15
        ldr x17, [ x17, 0 ]
        add x17, x17, x15
        br x17

I'm not completely certain, but the fix seems to be reasonable and fixes the reproducer. In my (now more informed) opinion, this change should be right. Please also read the comments below (they are my ad-hoc reasoning and writing them helped me to understand the issue better.)

Edited by Sven Tennie

Merge request reports

Loading