Skip to content

Fix x86 argument passing ABI for small integers - #167002

Open
nikic wants to merge 1 commit into
rust-lang:mainfrom
nikic:x86-psabi
Open

nikic wants to merge 1 commit into
rust-lang:mainfrom
nikic:x86-psabi

Conversation

@nikic

@nikic nikic commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

This mirrors llvm/llvm-project#236450 to Rust.

For non-Darwin, non-Win64 x86, the platform psABI does not require extension for small integer arguments (except bool to 8 bits). However, Clang prior to version 24 extended such arguments and assumed the extension to be present in the callee.

Clang 24 switches to an asymmetric scheme, where the argument is extended at the call-site, but no extension assumed in the callee. This makes it compatible with both old Clang and the platform psABI. This PR implements the same in Rust.

The Clang change comes with an -msmall-int-abi-compat flag to control the extension behavior. In Rust, we only support the maximally-compatible behavior. This only affects the C ABI, not the Rust ABI, so I don't think there is a need to expose more optimized ABI flavors here.

@rustbot

rustbot commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

cc @bjorn3

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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 Dec 3, 2026
@rustbot

rustbot commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin 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.

r? me
r=me with nits

View changes since this review

Comment on lines +154 to +156
/// Sometimes an ABI requires small integers to be extended to a full or partial register. This
/// struct defines if this extension should be zero-extension or sign-extension when necessary.
/// When it is not necessary to extend the argument, this information is ignored.

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.

Not sure I understand what this is trying to say with "information is ignored"; shouldn't it just not be present when not necessary?

/// Whether the receiver should assume that the incoming value is extended. If false, the
/// value is only extended by the sender, without an assumption on the receiver side.
/// Currently only supported for arguments.
pub with_assumption: bool,

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.

Could you maybe link to the reason this is needed?

Comment on lines 317 to 323
} else {
// The x86_64 psABI requires bools to be zero-extended to 8 bits, with high
// bits unspecified. zeroext i1 has this behavior in the LLVM X86 backend.
if arg.layout.backend_repr.is_bool() {
arg.extend_integer_width_to(8);
}
}

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.

Suggested change
} else {
// The x86_64 psABI requires bools to be zero-extended to 8 bits, with high
// bits unspecified. zeroext i1 has this behavior in the LLVM X86 backend.
if arg.layout.backend_repr.is_bool() {
arg.extend_integer_width_to(8);
}
}
} else if arg.layout.backend_repr.is_bool() {
// The x86_64 psABI requires bools to be zero-extended to 8 bits, with high
// bits unspecified. zeroext i1 has this behavior in the LLVM X86 backend.
arg.extend_integer_width_to(8);
}

@rustbot rustbot assigned WaffleLapkin and unassigned oli-obk Dec 4, 2026
@WaffleLapkin WaffleLapkin added the relnotes Marks issues that should be documented in the release notes of the next release. label Dec 4, 2026
@WaffleLapkin WaffleLapkin 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 Dec 4, 2026
@rust-log-analyzer

This comment has been minimized.

This mirrors llvm/llvm-project#236450 to
Rust.

For non-Darwin, non-Win64 x86, the platform psABI does not require
extension for small integer arguments (except bool to 8 bits).
However, Clang prior to version 24 extended such arguments and
assumed the extension to be present in the callee.

Clang 24 switches to an asymmetric scheme, where the argument is
extended at the call-site, but no extension assumed in the callee.
This makes it compatible with both old Clang and the platform
psABI. This PR implements the same in Rust.

The Clang change comes with an `-msmall-int-abi-compat` flag to
control the extension behavior. In Rust, we only support the
maximally-compatible behavior. This only affects the C ABI, not
the Rust ABI, so I don't think there is a need to expose more
optimized ABI flavors here.

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

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. relnotes Marks issues that should be documented in the release notes of the next release. 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