Skip to content

Pass ByRef values at fixed stack offset for extern "C" #1068

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

Merged
merged 1 commit into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
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
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cargo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ fi
cmd=$1
shift

cargo +${TOOLCHAIN} $cmd --target $TARGET_TRIPLE $@
cargo +${TOOLCHAIN} $cmd $@
22 changes: 13 additions & 9 deletions src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod returning;
use rustc_target::spec::abi::Abi;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;

use cranelift_codegen::ir::AbiParam;
use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};

use self::pass_mode::*;
use crate::prelude::*;
Expand Down Expand Up @@ -123,7 +123,11 @@ fn clif_sig_from_fn_sig<'tcx>(
if abi != Abi::Rust && abi != Abi::RustCall && abi != Abi::RustIntrinsic {
match pass_mode {
PassMode::NoPass | PassMode::ByVal(_) => {}
PassMode::ByValPair(_, _) | PassMode::ByRef { sized: _ } => {
PassMode::ByRef { size: Some(size) } => {
let purpose = ArgumentPurpose::StructArgument(u32::try_from(size.bytes()).expect("struct too big to pass on stack"));
return EmptySinglePair::Single(AbiParam::special(pointer_ty(tcx), purpose)).into_iter();
}
PassMode::ByValPair(_, _) | PassMode::ByRef { size: None } => {
tcx.sess.span_warn(
span,
&format!(
Expand All @@ -137,34 +141,34 @@ fn clif_sig_from_fn_sig<'tcx>(
}
}
}
pass_mode.get_param_ty(tcx).into_iter()
pass_mode.get_param_ty(tcx).map(AbiParam::new).into_iter()
})
.flatten();

let (mut params, returns): (Vec<_>, Vec<_>) = match get_pass_mode(
tcx,
tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(),
) {
PassMode::NoPass => (inputs.map(AbiParam::new).collect(), vec![]),
PassMode::NoPass => (inputs.collect(), vec![]),
PassMode::ByVal(ret_ty) => (
inputs.map(AbiParam::new).collect(),
inputs.collect(),
vec![AbiParam::new(ret_ty)],
),
PassMode::ByValPair(ret_ty_a, ret_ty_b) => (
inputs.map(AbiParam::new).collect(),
inputs.collect(),
vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)],
),
PassMode::ByRef { sized: true } => {
PassMode::ByRef { size: Some(_) } => {
(
Some(pointer_ty(tcx)) // First param is place to put return val
.into_iter()
.map(|ty| AbiParam::special(ty, ArgumentPurpose::StructReturn))
.chain(inputs)
.map(AbiParam::new)
.collect(),
vec![],
)
}
PassMode::ByRef { sized: false } => todo!(),
PassMode::ByRef { size: None } => todo!(),
};

if requires_caller_location {
Expand Down
19 changes: 10 additions & 9 deletions src/abi/pass_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(super) enum PassMode {
NoPass,
ByVal(Type),
ByValPair(Type, Type),
ByRef { sized: bool },
ByRef { size: Option<Size> },
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -70,8 +70,8 @@ impl PassMode {
PassMode::NoPass => Empty,
PassMode::ByVal(clif_type) => Single(clif_type),
PassMode::ByValPair(a, b) => Pair(a, b),
PassMode::ByRef { sized: true } => Single(pointer_ty(tcx)),
PassMode::ByRef { sized: false } => Pair(pointer_ty(tcx), pointer_ty(tcx)),
PassMode::ByRef { size: Some(_) } => Single(pointer_ty(tcx)),
PassMode::ByRef { size: None } => Pair(pointer_ty(tcx), pointer_ty(tcx)),
}
}
}
Expand All @@ -93,16 +93,17 @@ pub(super) fn get_pass_mode<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>)
// Returning (i128, i128) by-val-pair would take 4 regs, while only 3 are
// available on x86_64. Cranelift gets confused when too many return params
// are used.
PassMode::ByRef { sized: true }
PassMode::ByRef { size: Some(layout.size) }
} else {
PassMode::ByValPair(a, b)
}
}

// FIXME implement Vector Abi in a cg_llvm compatible way
Abi::Vector { .. } => PassMode::ByRef { sized: true },
Abi::Vector { .. } => PassMode::ByRef { size: Some(layout.size) },

&Abi::Aggregate { sized } => PassMode::ByRef { sized },
Abi::Aggregate { sized: true } => PassMode::ByRef { size: Some(layout.size) },
Abi::Aggregate { sized: false } => PassMode::ByRef { size: None },
}
}
}
Expand All @@ -118,7 +119,7 @@ pub(super) fn adjust_arg_for_abi<'tcx>(
let (a, b) = arg.load_scalar_pair(fx);
Pair(a, b)
}
PassMode::ByRef { sized: _ } => {
PassMode::ByRef { size: _ } => {
match arg.force_stack(fx) {
(ptr, None) => Single(ptr.get_addr(fx)),
(ptr, Some(meta)) => Pair(ptr.get_addr(fx), meta),
Expand Down Expand Up @@ -164,8 +165,8 @@ pub(super) fn cvalue_for_param<'tcx>(
let (a, b) = block_params.assert_pair();
Some(CValue::by_val_pair(a, b, layout))
}
PassMode::ByRef { sized: true } => Some(CValue::by_ref(Pointer::new(block_params.assert_single()), layout)),
PassMode::ByRef { sized: false } => {
PassMode::ByRef { size: Some(_) } => Some(CValue::by_ref(Pointer::new(block_params.assert_single()), layout)),
PassMode::ByRef { size: None } => {
let (ptr, meta) = block_params.assert_pair();
Some(CValue::by_ref_unsized(Pointer::new(ptr), meta, layout))
}
Expand Down
20 changes: 10 additions & 10 deletions src/abi/returning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) fn can_return_to_ssa_var<'tcx>(tcx: TyCtxt<'tcx>, dest_layout: TyAndL
match get_pass_mode(tcx, dest_layout) {
PassMode::NoPass | PassMode::ByVal(_) => true,
// FIXME Make it possible to return ByValPair and ByRef to an ssa var.
PassMode::ByValPair(_, _) | PassMode::ByRef { sized: _ } => false
PassMode::ByValPair(_, _) | PassMode::ByRef { size: _ } => false
}
}

Expand All @@ -33,14 +33,14 @@ pub(super) fn codegen_return_param(

Empty
}
PassMode::ByRef { sized: true } => {
PassMode::ByRef { size: Some(_) } => {
let ret_param = fx.bcx.append_block_param(start_block, fx.pointer_type);
fx.local_map
.insert(RETURN_PLACE, CPlace::for_ptr(Pointer::new(ret_param), ret_layout));

Single(ret_param)
}
PassMode::ByRef { sized: false } => todo!(),
PassMode::ByRef { size: None } => todo!(),
};

#[cfg(not(debug_assertions))]
Expand Down Expand Up @@ -69,11 +69,11 @@ pub(super) fn codegen_with_call_return_arg<'tcx, B: Backend, T>(
let output_pass_mode = get_pass_mode(fx.tcx, ret_layout);
let return_ptr = match output_pass_mode {
PassMode::NoPass => None,
PassMode::ByRef { sized: true } => match ret_place {
PassMode::ByRef { size: Some(_)} => match ret_place {
Some(ret_place) => Some(ret_place.to_ptr().get_addr(fx)),
None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)),
None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)), // FIXME allocate temp stack slot
},
PassMode::ByRef { sized: false } => todo!(),
PassMode::ByRef { size: None } => todo!(),
PassMode::ByVal(_) | PassMode::ByValPair(_, _) => None,
};

Expand All @@ -94,19 +94,19 @@ pub(super) fn codegen_with_call_return_arg<'tcx, B: Backend, T>(
ret_place.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_layout));
}
}
PassMode::ByRef { sized: true } => {}
PassMode::ByRef { sized: false } => todo!(),
PassMode::ByRef { size: Some(_) } => {}
PassMode::ByRef { size: None } => todo!(),
}

(call_inst, meta)
}

pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, impl Backend>) {
match get_pass_mode(fx.tcx, return_layout(fx)) {
PassMode::NoPass | PassMode::ByRef { sized: true } => {
PassMode::NoPass | PassMode::ByRef { size: Some(_) } => {
fx.bcx.ins().return_(&[]);
}
PassMode::ByRef { sized: false } => todo!(),
PassMode::ByRef { size: None } => todo!(),
PassMode::ByVal(_) => {
let place = fx.get_local_place(RETURN_PLACE);
let ret_val = place.to_cvalue(fx).load_scalar(fx);
Expand Down
1 change: 1 addition & 0 deletions src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ impl<'tcx> DebugContext<'tcx> {
use cranelift_codegen::ir::ArgumentPurpose;
let base_name = match param.purpose {
ArgumentPurpose::Normal => "arg",
ArgumentPurpose::StructArgument(_) => "struct_arg",
ArgumentPurpose::StructReturn => "sret",
ArgumentPurpose::Link | ArgumentPurpose::FramePointer | ArgumentPurpose::CalleeSaved => continue,
ArgumentPurpose::VMContext | ArgumentPurpose::SignatureId | ArgumentPurpose::StackLimit => unreachable!(),
Expand Down
6 changes: 3 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ $RUSTC example/mod_bench.rs --crate-type bin --target $TARGET_TRIPLE
pushd simple-raytracer
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
echo "[BENCH COMPILE] ebobby/simple-raytracer"
hyperfine --runs ${RUN_RUNS:-10} --warmup 1 --prepare "rm -r target/*/debug || true" \
"RUSTFLAGS='' cargo build --target $TARGET_TRIPLE" \
hyperfine --runs ${RUN_RUNS:-10} --warmup 1 --prepare "cargo clean" \
"RUSTFLAGS='' cargo build" \
"../cargo.sh build"

echo "[BENCH RUN] ebobby/simple-raytracer"
cp ./target/*/debug/main ./raytracer_cg_clif
cp ./target/debug/main ./raytracer_cg_clif
hyperfine --runs ${RUN_RUNS:-10} ./raytracer_cg_llvm ./raytracer_cg_clif
else
echo "[BENCH COMPILE] ebobby/simple-raytracer (skipped)"
Expand Down