Skip to content

riscv64: Split clobber save/restore SP adjustment for compressed instructions#13797

Open
rajeshgangam wants to merge 1 commit into
bytecodealliance:mainfrom
rajeshgangam:clobber-save-restore
Open

riscv64: Split clobber save/restore SP adjustment for compressed instructions#13797
rajeshgangam wants to merge 1 commit into
bytecodealliance:mainfrom
rajeshgangam:clobber-save-restore

Conversation

@rajeshgangam

@rajeshgangam rajeshgangam commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • When the Zca extension is enabled, split the SP adjustment in function prologues/epilogues so callee-saved stores use small SP-relative offsets that fit in c.sdsp's 504-byte encoding range
  • All callee-save stores/loads compress from 4-byte sd/ld to 2-byte c.sdsp/c.ldsp, saving ~28 bytes per function with 8 callee-saves
  • Without Zca, the original combined SP decrement is preserved — zero behavior change on existing tests
  • Adds isa_flags parameter to the ABIMachineSpec::gen_clobber_save and gen_clobber_restore trait methods so implementations can make ISA-specific decisions

Before (main, has_c enabled, 1KB frame + 8 callee-saves):

  addi sp, sp, -0x440           ; 4B  combined (clobber+frame)
  sd s1, 0x438(sp)              ; 4B  offset > 504, can't compress
  sd s2, 0x430(sp)              ; 4B
  ...

After (with Zca):

  c.addi16sp sp, -0x40          ; 2B  clobber only
  c.sdsp s1, 0x38(sp)           ; 2B  offset ≤ 504, compressed
  c.sdsp s2, 0x30(sp)           ; 2B
  ...
  addi sp, sp, -0x400           ; 4B  remaining frame

Fixes #7190.

Test plan

  • All 235 riscv64 compile filetests pass
  • All 116 aarch64, 181 x64, 101 s390x, 49 pulley filetests pass (no regressions from trait change)
  • New c-clobber-save-restore.clif test with 3 cases: 100KB frame, 512B boundary, max callee-saves
  • Existing 9 riscv64 test files have zero diff against main (non-Zca path unchanged)

@rajeshgangam rajeshgangam requested review from a team as code owners July 1, 2026 17:34
@rajeshgangam rajeshgangam requested review from alexcrichton and removed request for a team July 1, 2026 17:34
…ructions

When the Zca extension is enabled, split the SP adjustment in function
prologues/epilogues so callee-saved stores use small SP-relative offsets
that fit in c.sdsp's 504-byte encoding range. This allows all callee-save
stores/loads to compress from 4-byte sd/ld to 2-byte c.sdsp/c.ldsp.

Without Zca, the original combined SP decrement is preserved to avoid
adding an extra instruction with no compression benefit.

Also adds isa_flags parameter to the ABIMachineSpec gen_clobber_save and
gen_clobber_restore trait methods so implementations can make ISA-specific
decisions.

Fixes bytecodealliance#7190.
@rajeshgangam rajeshgangam force-pushed the clobber-save-restore branch from d447f39 to f3d500e Compare July 1, 2026 17:39
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:x64 Issues related to x64 codegen labels Jul 1, 2026
@fitzgen

fitzgen commented Jul 2, 2026

Copy link
Copy Markdown
Member

Hi, can you confirm that you are following our AI policy and have fully reviewed and understood the code here, and are willing to take responsibility for the code in the way that only a human can? Thanks.

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:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

riscv64: Improve Clobber Save/Restore Sequence

2 participants