Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying stack slot alignments #6716

Open
bjorn3 opened this issue Jul 12, 2023 · 3 comments · Fixed by #8635
Open

Support specifying stack slot alignments #6716

bjorn3 opened this issue Jul 12, 2023 · 3 comments · Fixed by #8635

Comments

@bjorn3
Copy link
Contributor

bjorn3 commented Jul 12, 2023

Feature

See title.

Benefit

Correct alignment are necessary to avoid crashes on some architectures and code may depend on correct alignment. In addition rustc checks that the right alignment is used when dereferencing a raw pointer in debug mode. This breaks rayon which allocates a stack value with cacheline alignment and then takes a raw pointer which it later dereferences. See rust-lang/rustc_codegen_cranelift#1381.

Implementation

Sort stackslots by alignment for better packing and then add padding as necessary and if the alignment of a stack slot exceeds the ABI stack alignment realign the stack at runtime.

Alternatives

Doing dynamic alignment at runtime in the cranelift ir producer. This is slower and has higher stack usage overhead.

@bjorn3
Copy link
Contributor Author

bjorn3 commented Jul 12, 2023

Also necessary to fix rust-lang/rustc_codegen_cranelift#1258.

cfallin added a commit to cfallin/wasmtime that referenced this issue May 16, 2024
Fixes bytecodealliance#6716.

Currently, stack slots on the stack are aligned only to a machine-word
boundary. This is insufficient for some use-cases: for example, storing
SIMD data or structs that require a larger alignment.

This PR adds a parameter to the `StackSlotData` to specify alignment,
and the associated logic to the CLIF parser and printer. It updates the
shared ABI code to compute the stackslot layout taking the alignment
into account. In order to ensure the alignment is always a power of two,
it is stored as a shift amount (log2 of actual alignment) in the IR.
cfallin added a commit to cfallin/wasmtime that referenced this issue May 16, 2024
Fixes bytecodealliance#6716.

Currently, stack slots on the stack are aligned only to a machine-word
boundary. This is insufficient for some use-cases: for example, storing
SIMD data or structs that require a larger alignment.

This PR adds a parameter to the `StackSlotData` to specify alignment,
and the associated logic to the CLIF parser and printer. It updates the
shared ABI code to compute the stackslot layout taking the alignment
into account. In order to ensure the alignment is always a power of two,
it is stored as a shift amount (log2 of actual alignment) in the IR.
github-merge-queue bot pushed a commit that referenced this issue May 16, 2024
* Cranelift: add alignment parameter to stack slots.

Fixes #6716.

Currently, stack slots on the stack are aligned only to a machine-word
boundary. This is insufficient for some use-cases: for example, storing
SIMD data or structs that require a larger alignment.

This PR adds a parameter to the `StackSlotData` to specify alignment,
and the associated logic to the CLIF parser and printer. It updates the
shared ABI code to compute the stackslot layout taking the alignment
into account. In order to ensure the alignment is always a power of two,
it is stored as a shift amount (log2 of actual alignment) in the IR.

* Apply suggestions from code review

Co-authored-by: Trevor Elliott <[email protected]>

* Update filetest.

* Update alignment to ValRaw vector.

* Fix printer test.

* cargo-fmt from suggestion update.

---------

Co-authored-by: Trevor Elliott <[email protected]>
@bjorn3
Copy link
Contributor Author

bjorn3 commented May 16, 2024

Would you mind reopening this? It hasn't been fully resolved yet: https://github.com/bytecodealliance/wasmtime/pull/8635/files#r1603954465

@cfallin
Copy link
Member

cfallin commented May 16, 2024

Sure; to summarize here, the issue is that alignment is now correct with respect to the start of stack frame, but start of stack frame is only aligned as per ABI (e.g. 16 bytes on x86-64 and aarch64) so we need to dynamically align SP (it must be dynamic, we can't know statically anything more than the ABI guarantee).

@cfallin cfallin reopened this May 16, 2024
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 a pull request may close this issue.

2 participants