Skip to content

Stack allocation: enforce a per-architecture minimal stack-pointer alignment - #1546

Merged
vbgl merged 2 commits into
jasmin-lang:mainfrom
clebreto:sp-min-align
Aug 28, 2026
Merged

Stack allocation: enforce a per-architecture minimal stack-pointer alignment#1546
vbgl merged 2 commits into
jasmin-lang:mainfrom
clebreto:sp-min-align

Conversation

@clebreto

@clebreto clebreto commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Split out of #1541, as suggested in review: the mechanism is independent of the ARMv8-A backend.

  • First commit: add an sp_min_align architecture parameter bounding the alignment the stack pointer keeps across calls; stack allocation raises the frame alignment of every function that actually uses the stack up to it (frame sizes are rounded up to the frame alignment, so this bounds the SP alignment itself). All architectures declare U8, so no generated code changes.
  • Second commit: set it to U32 on arm-m4. Armv7-M guarantees that stack pointer values are at least 4-byte aligned — writes to SP force bits [1:0] to zero (Arm v7-M Architecture Reference Manual, B1.5.7) — so a frame that is only 1- or 2-byte aligned lets the export prologue write a non-4-byte-aligned SP (BIC r12, r12, #1 before MOV sp, r12), which the hardware silently rounds, shifting every subsequent SP-relative access. The new tests/success/arm-m4/unaligned_stackalign.jazz asserts the computed alignment with #[stackalign=u32] and fails without this commit; the instack function of tests/success/common/unaligned.jazz moves to per-architecture copies since the expected alignment is now architecture-dependent.

No command-line override: the per-architecture default is the only value with a real use case.

The ARMv8-A backend (#1541) relies on this mechanism with a 16-byte floor (AArch64 SP alignment checking); once this lands, the corresponding hunks disappear from that PR.

Comment thread compiler/src/stackAlloc.ml Outdated
Add an sp_min_align architecture parameter bounding the alignment the
stack pointer keeps across calls: stack allocation raises the frame
alignment of every function that actually uses the stack up to it
(frame sizes are rounded up to the frame alignment, so this bounds the
stack-pointer alignment itself). Functions with no stack footprint keep
their alignment, so export functions with no stack use are unaffected.

All architectures declare U8 here (no constraint), so this commit does
not change the generated code.
Armv7-M guarantees that stack pointer values are at least 4-byte
aligned: writes to SP force bits [1:0] to zero (Arm v7-M Architecture
Reference Manual, B1.5.7). With no minimal alignment, a function whose
frame is only 1- or 2-byte aligned lets the export prologue write a
non-4-byte-aligned SP (BIC #1 on the aligned copy), which the hardware
silently rounds, shifting every subsequent SP-relative access. Set
sp_min_align to U32 for arm-m4 so such frames are padded to 4 bytes.

The instack function of tests/success/common/unaligned.jazz asserted
the computed alignment with #[stackalign=u16], which is now
architecture-dependent; move it to per-architecture copies
(unaligned_stackalign.jazz): u16 on x86-64 and risc-v, and u32 on
arm-m4, failing before this change.

@vbgl vbgl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Why is this PR marked as “draft”?

@clebreto

Copy link
Copy Markdown
Contributor Author

LGTM. Why is this PR marked as “draft”?
I wanted to have a last look at it with the github diffs, sorry. I remove the draft now.

@clebreto
clebreto marked this pull request as ready for review August 28, 2026 12:20
@vbgl
vbgl merged commit 6ec26dd into jasmin-lang:main Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants