Skip to content

[JIT] Push APX callee-saved registers in ascending order so Unwind V3 can use WOD_PUSH_CONSECUTIVE_2 #136724

Open

Description

@Ruihan-Yin

Summary

On AMD64 with APX, the JIT saves callee-saved registers in pairs with PUSH2 (opt-in under EnableApxPP2). Unwind Information V3 has a one-byte WOD for a PUSH2 of consecutive registers, WOD_PUSH_CONSECUTIVE_2. Every other register pair needs the two-byte WOD_PUSH2. The JIT can't use the one-byte form today because of the order it pushes in. This issue tracks changing that order and adding a test for the encoding.

This should be done after the JIT emits V3 unwind information. See "Why wait" below.

Background

WOD_PUSH_CONSECUTIVE_2 describes push2 reg, reg+1: Register is pushed first, then Register + 1. The encoder is unwindPush2V3 in src/coreclr/jit/unwindamd64.cpp, added in #135443. A TODO-Unwind-V3 there notes that this branch is unreachable.

The reason is that genPushCalleeSavedRegistersFromMaskAPX (src/coreclr/jit/codegenxarch.cpp) pushes registers from the highest to the lowest. So every PUSH2 it emits has reg1 > reg2, for example push2 r15, r14, and reg2 == REG_NEXT(reg1) never holds.

Proposal

Push the callee-saved registers in ascending order in genPushCalleeSavedRegistersFromMaskAPX, and pop in the matching order in genPopCalleeSavedRegistersFromMaskAPX. Pairs such as rsi/rdi, r12/r13 and r14/r15 then become push2 rsi, rdi and so on, and get the one-byte WOD. This saves one byte per consecutive pair in the prolog's unwind information and one in each epilog's.

This path only runs with APX and EnableApxPP2, and only for the main function: funclets and OSR methods don't use it.

Things to check

  • Callee-saved area layout. The comment above the pairing loop says registers are pushed backwards "so we match the order we will pop them in the epilog and all the other code that expects it to be in this order". Changing the order changes where each register is saved, so the code that relies on this order needs an audit.
  • APX vs. non-APX layout. genPushCalleeSavedRegistersFromMask still pushes in descending order. After this change the two paths would save registers in different places. Any code that assumes one layout must handle both.
  • Pairing and alignment. The current code pushes one register first when needed to align the stack for PUSH2, and pairs the rest. The ascending version must keep the alignment, and should pair registers so that consecutive pairs are actually formed.
  • Unix. The same code path emits CFI on Unix. The CFI describes each push separately, so it stays correct, but the layout change applies there too.

Why wait

Today nothing calls the V3 encoders, and no V3 data reaches the VM or a JIT dump. If the order changed now, the one-byte WOD would still be unused and nothing could test it. Once V3 is emitted, the change can come with tests:

  • A DEBUG check in unwindPush2V3 that the encoded byte is correct, like the existing checks for WOD_PUSH2. Any Checked-JIT run that compiles an APX method with a consecutive callee-saved pair would then exercise it.
  • A JIT test that checks the emitted WOD bytes, for example in the V3 unwind dump. This would also catch a prolog/epilog layout mismatch from the reorder.

Related

Activity

  1. added
    untriaged
    on Oct 30, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 30, 2026
  3. dotnet-policy-service commented on Oct 30, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. removed
    untriaged
    on Nov 2, 2026
  5. added this to the 12.0.0 milestone on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions