Skip to content

Commit

Permalink
Return values up to 128 bits in registers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-schievink committed Sep 26, 2020
1 parent 6f9a8a7 commit 4c5acc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,7 @@ where
can_unwind: fn_can_unwind(cx.tcx().sess.panic_strategy(), codegen_fn_attr_flags, conv),
};
fn_abi.adjust_for_abi(cx, sig.abi);
debug!("FnAbi::new_internal = {:?}", fn_abi);
fn_abi
}

Expand All @@ -2748,7 +2749,7 @@ where
|| abi == SpecAbi::RustIntrinsic
|| abi == SpecAbi::PlatformIntrinsic
{
let fixup = |arg: &mut ArgAbi<'tcx, Ty<'tcx>>| {
let fixup = |arg: &mut ArgAbi<'tcx, Ty<'tcx>>, is_ret: bool| {
if arg.is_ignore() {
return;
}
Expand Down Expand Up @@ -2786,8 +2787,11 @@ where
_ => return,
}

let max_by_val_size =
if is_ret { call::max_ret_by_val(cx) } else { Pointer.size(cx) };
let size = arg.layout.size;
if arg.layout.is_unsized() || size > Pointer.size(cx) {

if arg.layout.is_unsized() || size > max_by_val_size {
arg.make_indirect();
} else {
// We want to pass small aggregates as immediates, but using
Expand All @@ -2796,9 +2800,9 @@ where
arg.cast_to(Reg { kind: RegKind::Integer, size });
}
};
fixup(&mut self.ret);
fixup(&mut self.ret, true);
for arg in &mut self.args {
fixup(arg);
fixup(arg, false);
}
if let PassMode::Indirect(ref mut attrs, _) = self.ret.mode {
attrs.set(ArgAttribute::StructRet);
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_target/src/abi/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,15 @@ impl<'a, Ty> FnAbi<'a, Ty> {
Ok(())
}
}

/// Returns the maximum size of return values to be passed by value in the Rust ABI.
///
/// Return values beyond this size will use an implicit out-pointer instead.
pub fn max_ret_by_val<C: HasTargetSpec + HasDataLayout>(spec: &C) -> Size {
match spec.target_spec().arch.as_str() {
// System-V will pass return values up to 128 bits in RAX/RDX.
"x86_64" => Size::from_bits(128),

_ => spec.data_layout().pointer_size,
}
}

0 comments on commit 4c5acc4

Please sign in to comment.