1
Fork 0

Fix 128bit checked math intrinsic calls

This commit is contained in:
bjorn3 2021-01-30 11:02:24 +01:00
parent e8f48e4bae
commit 139a6d12de
3 changed files with 52 additions and 41 deletions

View file

@ -102,13 +102,13 @@ impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
pub(crate) fn lib_call( pub(crate) fn lib_call(
&mut self, &mut self,
name: &str, name: &str,
input_tys: Vec<types::Type>, params: Vec<AbiParam>,
output_tys: Vec<types::Type>, returns: Vec<AbiParam>,
args: &[Value], args: &[Value],
) -> &[Value] { ) -> &[Value] {
let sig = Signature { let sig = Signature {
params: input_tys.iter().cloned().map(AbiParam::new).collect(), params,
returns: output_tys.iter().cloned().map(AbiParam::new).collect(), returns,
call_conv: CallConv::triple_default(self.triple()), call_conv: CallConv::triple_default(self.triple()),
}; };
let func_id = self let func_id = self
@ -140,16 +140,18 @@ impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
.iter() .iter()
.map(|arg| { .map(|arg| {
( (
self.clif_type(arg.layout().ty).unwrap(), AbiParam::new(self.clif_type(arg.layout().ty).unwrap()),
arg.load_scalar(self), arg.load_scalar(self),
) )
}) })
.unzip(); .unzip();
let return_layout = self.layout_of(return_ty); let return_layout = self.layout_of(return_ty);
let return_tys = if let ty::Tuple(tup) = return_ty.kind() { let return_tys = if let ty::Tuple(tup) = return_ty.kind() {
tup.types().map(|ty| self.clif_type(ty).unwrap()).collect() tup.types()
.map(|ty| AbiParam::new(self.clif_type(ty).unwrap()))
.collect()
} else { } else {
vec![self.clif_type(return_ty).unwrap()] vec![AbiParam::new(self.clif_type(return_ty).unwrap())]
}; };
let ret_vals = self.lib_call(name, input_tys, return_tys, &args); let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
match *ret_vals { match *ret_vals {
@ -208,7 +210,8 @@ pub(crate) fn codegen_fn_prelude<'tcx>(
.block_params(start_block) .block_params(start_block)
.to_vec() .to_vec()
.into_iter(); .into_iter();
let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, &mut block_params_iter); let ret_place =
self::returning::codegen_return_param(fx, &ssa_analyzed, &mut block_params_iter);
assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE); assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE);
// None means pass_mode == NoPass // None means pass_mode == NoPass
@ -241,14 +244,16 @@ pub(crate) fn codegen_fn_prelude<'tcx>(
let mut params = Vec::new(); let mut params = Vec::new();
for (i, _arg_ty) in tupled_arg_tys.types().enumerate() { for (i, _arg_ty) in tupled_arg_tys.types().enumerate() {
let arg_abi = arg_abis_iter.next().unwrap(); let arg_abi = arg_abis_iter.next().unwrap();
let param = cvalue_for_param(fx, Some(local), Some(i), arg_abi, &mut block_params_iter); let param =
cvalue_for_param(fx, Some(local), Some(i), arg_abi, &mut block_params_iter);
params.push(param); params.push(param);
} }
(local, ArgKind::Spread(params), arg_ty) (local, ArgKind::Spread(params), arg_ty)
} else { } else {
let arg_abi = arg_abis_iter.next().unwrap(); let arg_abi = arg_abis_iter.next().unwrap();
let param = cvalue_for_param(fx, Some(local), None, arg_abi, &mut block_params_iter); let param =
cvalue_for_param(fx, Some(local), None, arg_abi, &mut block_params_iter);
(local, ArgKind::Normal(param), arg_ty) (local, ArgKind::Normal(param), arg_ty)
} }
}) })

View file

@ -1060,7 +1060,11 @@ pub(crate) fn codegen_panic_inner<'tcx>(
fx.lib_call( fx.lib_call(
&*symbol_name, &*symbol_name,
vec![fx.pointer_type, fx.pointer_type, fx.pointer_type], vec![
AbiParam::new(fx.pointer_type),
AbiParam::new(fx.pointer_type),
AbiParam::new(fx.pointer_type),
],
vec![], vec![],
args, args,
); );

View file

@ -1,5 +1,7 @@
//! Replaces 128-bit operators with lang item calls where necessary //! Replaces 128-bit operators with lang item calls where necessary
use cranelift_codegen::ir::ArgumentPurpose;
use crate::prelude::*; use crate::prelude::*;
pub(crate) fn maybe_codegen<'tcx>( pub(crate) fn maybe_codegen<'tcx>(
@ -24,41 +26,41 @@ pub(crate) fn maybe_codegen<'tcx>(
None None
} }
BinOp::Add | BinOp::Sub if !checked => None, BinOp::Add | BinOp::Sub if !checked => None,
BinOp::Add => { BinOp::Mul if !checked => {
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); let val_ty = if is_signed {
return Some(if is_signed { fx.tcx.types.i128
fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty)
} else { } else {
fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty) fx.tcx.types.u128
}); };
Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty))
} }
BinOp::Sub => { BinOp::Add | BinOp::Sub | BinOp::Mul => {
assert!(checked);
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
return Some(if is_signed { let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty) let param_types = vec![
} else { AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn),
fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty) AbiParam::new(types::I128),
}); AbiParam::new(types::I128),
];
let args = [
out_place.to_ptr().get_addr(fx),
lhs.load_scalar(fx),
rhs.load_scalar(fx),
];
let name = match (bin_op, is_signed) {
(BinOp::Add, false) => "__rust_u128_addo",
(BinOp::Add, true) => "__rust_i128_addo",
(BinOp::Sub, false) => "__rust_u128_subo",
(BinOp::Sub, true) => "__rust_i128_subo",
(BinOp::Mul, false) => "__rust_u128_mulo",
(BinOp::Mul, true) => "__rust_i128_mulo",
_ => unreachable!(),
};
fx.lib_call(name, param_types, vec![], &args);
Some(out_place.to_cvalue(fx))
} }
BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"), BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
BinOp::Mul => {
let res = if checked {
let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter());
if is_signed {
fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty)
} else {
fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty)
}
} else {
let val_ty = if is_signed {
fx.tcx.types.i128
} else {
fx.tcx.types.u128
};
fx.easy_call("__multi3", &[lhs, rhs], val_ty)
};
Some(res)
}
BinOp::Div => { BinOp::Div => {
assert!(!checked); assert!(!checked);
if is_signed { if is_signed {