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

Make Rust ABI return types up to two pointers in registers #124373

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_session::config::OptLevel;
use rustc_span::def_id::DefId;
use rustc_target::abi::call::{
ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, Conv, FnAbi, PassMode, Reg, RegKind,
RiscvInterruptKind,
RiscvInterruptKind, Uniform,
};
use rustc_target::abi::*;
use rustc_target::spec::abi::Abi as SpecAbi;
Expand Down Expand Up @@ -779,11 +779,23 @@ fn fn_abi_adjust_for_abi<'tcx>(
assert!(is_indirect_not_on_stack, "{:?}", arg);

let size = arg.layout.size;
if !arg.layout.is_unsized() && size <= Pointer(AddressSpace::DATA).size(cx) {
// We want to pass small aggregates as immediates, but using
// an LLVM aggregate type for this leads to bad optimizations,
// so we pick an appropriately sized integer type instead.
arg.cast_to(Reg { kind: RegKind::Integer, size });
if !arg.layout.is_unsized() {
let data_pointer_size = Pointer(AddressSpace::DATA).size(cx);
if size <= data_pointer_size {
// We want to pass small aggregates as immediates, but using
// an LLVM aggregate type for this leads to bad optimizations,
// so we pick an appropriately sized integer type instead.
arg.cast_to(Reg { kind: RegKind::Integer, size });
} else if size == data_pointer_size * 2 && size.bytes() % 2 == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

There's no comment or anything explaining this subtlety -- please add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, my original idea on how this should look got shot down by CI, so I'll look into this some more and add comments to the final version.

Copy link
Member

Choose a reason for hiding this comment

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

Right, so it would be useful to understand why the original assumption you made is not compatible with codegen and explain it here. It also may suggest a deeper bug in your implementation, though it's not certain unless you look into it.

// Aggregates like `[usize; 2]` or (on 64-bit arch) `[u128; 1]`
// can be passed as a scalar pair.
let part_size = Size::from_bytes(size.bytes() / 2);
arg.cast_to(Uniform {
unit: Reg { kind: RegKind::Integer, size: part_size },
total: size,
is_consecutive: false,
});
}
}

// If we deduced that this parameter was read-only, add that to the attribute list now.
Expand Down
Loading