Skip to content

callconv: mips64: match gcc for "alignment" of 16-byte-aligned ZSTs - #164783

Open
chenx97 wants to merge 1 commit into
rust-lang:mainfrom
chenx97:mips64-zst
Open

chenx97 wants to merge 1 commit into
rust-lang:mainfrom
chenx97:mips64-zst

Conversation

@chenx97

@chenx97 chenx97 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

While ZSTs themselves are ignored, the paddings are not, so we use an indirection pointer to fill the argument list when padding is needed.

Fix validated with abi-cafe.

r? folkertdev

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 23, 2026
@rust-log-analyzer

This comment has been minimized.

@folkertdev

Copy link
Copy Markdown
Contributor

Wow, that's kinda hacky, but does actually seem to work...

I tried using a Cast with the padding and an empty CastTarget, that does not work. I think using Direct would break invariants.

@beetrees do you have better ideas for how to handle this? The proposal here is kind of weird but with a solid comment maybe it's good enough? We do use ignore_to_indirect in various places already, but here we'd use it not for the ABI of a ZST but just as padding.

@beetrees

beetrees commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

The alternative here would be to keep track of whether (one of) the previous ZST argument(s) was an aligned ZST that needs padding and add the required padding to the next non-ZST argument.

@folkertdev

Copy link
Copy Markdown
Contributor

The alternative here would be to keep track of whether (one of) the previous ZST argument(s) was an aligned ZST and add the required padding to the next non-ZST argument.

Played around with this, and it has an extremely subtle edge case of an over-aligned ZST before ... which would misalign the variable argument list https://godbolt.org/z/dMbjod3ss.

That makes for a good test case regardless.

@beetrees

Copy link
Copy Markdown
Contributor

Would using Cast with a non-empty cast target work? That way the padding would be undef rather than a pointer.

@folkertdev

Copy link
Copy Markdown
Contributor

It could perhaps but currently it hits the bottom bug! here

ZeroSized => match arg.mode {
PassMode::Indirect { mode, .. } => {
if mode == IndirectMode::AmdgpuKernelArg {
bug!("{op:?} passed as amdgpu kernel argument with abi {arg:?}");
}
if mode == IndirectMode::OnStack {
// It doesn't seem like any target can have `byval` ZSTs, so this assert
// is here to replace a would-be untested codepath.
bug!("ZST {op:?} passed on stack with abi {arg:?}");
}
// Though `extern "Rust"` doesn't pass ZSTs, some ABIs pass
// a pointer for `repr(C)` structs even when empty, so get
// one from an `alloca` (which can be left uninitialized).
let scratch = PlaceRef::alloca(bx, arg.layout);
(scratch.val.llval, scratch.val.align, true)
}
_ => bug!("ZST {op:?} wasn't ignored, but was passed with abi {arg:?}"),
},

This is also where a cast to an empty CastTarget fails by the way, so in theory we could allow that too. It just would be new behavior.

@folkertdev folkertdev left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you extend the comment here with this second padding use?

ZeroSized => match arg.mode {
PassMode::Indirect { mode, .. } => {
if mode == IndirectMode::AmdgpuKernelArg {
bug!("{op:?} passed as amdgpu kernel argument with abi {arg:?}");
}
if mode == IndirectMode::OnStack {
// It doesn't seem like any target can have `byval` ZSTs, so this assert
// is here to replace a would-be untested codepath.
bug!("ZST {op:?} passed on stack with abi {arg:?}");
}
// Though `extern "Rust"` doesn't pass ZSTs, some ABIs pass
// a pointer for `repr(C)` structs even when empty, so get
// one from an `alloca` (which can be left uninitialized).
let scratch = PlaceRef::alloca(bx, arg.layout);
(scratch.val.llval, scratch.val.align, true)
}
_ => bug!("ZST {op:?} wasn't ignored, but was passed with abi {arg:?}"),
},

View changes since this review

Comment on lines +184 to +185
// Use pointer indirection as padding for 16-byte-aligned ZSTs.
arg.make_indirect_from_ignore();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Use pointer indirection as padding for 16-byte-aligned ZSTs.
arg.make_indirect_from_ignore();
// Use pointer indirection as padding for over-aligned ZSTs.
//
// ZSTs are not passed (they have no data), but may be over-aligned, and when they
// are aligned to 16 or more, that still matters for register alignment. So, add padding
// to have the ZST argument start in an even-numbered register.
//
// This is (ab)using a mechanism that some other calling conventions use where
// ZSTs are passed as pointers (to uninitialized memory). This is the least
// awkward way to fit padding on a ZST into our logic.
arg.make_indirect_from_ignore();

Let's expand this a bit, the above is a suggestion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add a c-variadic test, the padding parameter should show up before the ... like in https://godbolt.org/z/5d4WfheW6. I'd add the 8-aligned version too to make it easier to compare.

Overall it might be good to add some 8-aligned negative tests.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 27, 2026
@rustbot

rustbot commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@chenx97

chenx97 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 23, 2026
@chenx97

chenx97 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Can you extend the comment here with this second padding use?

ZeroSized => match arg.mode {
PassMode::Indirect { mode, .. } => {
if mode == IndirectMode::AmdgpuKernelArg {
bug!("{op:?} passed as amdgpu kernel argument with abi {arg:?}");
}
if mode == IndirectMode::OnStack {
// It doesn't seem like any target can have `byval` ZSTs, so this assert
// is here to replace a would-be untested codepath.
bug!("ZST {op:?} passed on stack with abi {arg:?}");
}
// Though `extern "Rust"` doesn't pass ZSTs, some ABIs pass
// a pointer for `repr(C)` structs even when empty, so get
// one from an `alloca` (which can be left uninitialized).
let scratch = PlaceRef::alloca(bx, arg.layout);
(scratch.val.llval, scratch.val.align, true)
}
_ => bug!("ZST {op:?} wasn't ignored, but was passed with abi {arg:?}"),
},

View changes since this review

Oof, I forgot to address this request.

@chenx97
chenx97 marked this pull request as draft November 23, 2026 17:06
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 23, 2026
While ZSTs themselves are ignored, the paddings are not, so we use
an indirection pointer to fill the argument list when a padding is
needed.
@chenx97
chenx97 marked this pull request as ready for review November 24, 2026 11:16
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 24, 2026

@folkertdev folkertdev left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 32358e5 has been approved by folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
callconv: mips64: match gcc for "alignment" of 16-byte-aligned ZSTs

While ZSTs themselves are ignored, the paddings are not, so we use an indirection pointer to fill the argument list when padding is needed.

Fix validated with abi-cafe.

r? folkertdev
@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 24, 2026
@rust-bors

rust-bors Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #166507) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This pull request was unapproved.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants