| //! Codegen of intrinsics. This includes functions marked with the `#[rustc_intrinsic]` attribute | |
| //! and LLVM intrinsics that have symbol names starting with `llvm.`. | |
| macro_rules! intrinsic_args { | |
| ($fx:expr, $args:expr => ($($arg:tt),*); $intrinsic:expr) => { | |
| #[allow(unused_parens, clippy::unused_unit)] | |
| let ($($arg),*) = if let [$($arg),*] = $args { | |
| ($(codegen_operand($fx, &($arg).node)),*) | |
| } else { | |
| $crate::intrinsics::bug_on_incorrect_arg_count($intrinsic); | |
| }; | |
| } | |
| } | |
| mod llvm; | |
| mod llvm_aarch64; | |
| mod llvm_x86; | |
| mod simd; | |
| use cranelift_codegen::ir::{ | |
| AtomicRmwOp, BlockArg, ExceptionTableData, ExceptionTableItem, ExceptionTag, | |
| }; | |
| use rustc_middle::ty; | |
| use rustc_middle::ty::GenericArgs; | |
| use rustc_middle::ty::layout::{HasTypingEnv as _, ValidityRequirement}; | |
| use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; | |
| use rustc_span::{Spanned, Symbol, sym}; | |
| use rustc_target::spec::PanicStrategy; | |
| pub(crate) use self::llvm::codegen_llvm_intrinsic_call; | |
| use crate::cast::clif_intcast; | |
| use crate::codegen_f16_f128; | |
| use crate::debuginfo::EXCEPTION_HANDLER_CATCH; | |
| use crate::prelude::*; | |
| fn bug_on_incorrect_arg_count(intrinsic: impl std::fmt::Display) -> ! { | |
| bug!("wrong number of args for intrinsic {}", intrinsic); | |
| } | |
| fn report_atomic_type_validation_error<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| intrinsic: Symbol, | |
| span: Span, | |
| ty: Ty<'tcx>, | |
| ) { | |
| fx.tcx.dcx().span_err( | |
| span, | |
| format!( | |
| "`{}` intrinsic: expected basic integer or raw pointer type, found `{:?}`", | |
| intrinsic, ty | |
| ), | |
| ); | |
| // Prevent verifier error | |
| fx.bcx.ins().trap(TrapCode::user(1 /* unreachable */).unwrap()); | |
| } | |
| pub(crate) fn clif_vector_type<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>) -> Type { | |
| let (element, count) = match layout.backend_repr { | |
| BackendRepr::SimdVector { element, count } => (element, count), | |
| _ => unreachable!(), | |
| }; | |
| scalar_to_clif_type(tcx, element).by(count.as_u32()).unwrap() | |
| } | |
| fn simd_for_each_lane<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| val: CValue<'tcx>, | |
| ret: CPlace<'tcx>, | |
| f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value) -> Value, | |
| ) { | |
| let layout = val.layout(); | |
| let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx); | |
| let lane_layout = fx.layout_of(lane_ty); | |
| let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx); | |
| let ret_lane_layout = fx.layout_of(ret_lane_ty); | |
| assert_eq!(lane_count, ret_lane_count); | |
| for lane_idx in 0..lane_count { | |
| let lane = val.value_lane(fx, lane_idx).load_scalar(fx); | |
| let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, lane); | |
| let res_lane = CValue::by_val(res_lane, ret_lane_layout); | |
| ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); | |
| } | |
| } | |
| fn simd_pair_for_each_lane_typed<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| x: CValue<'tcx>, | |
| y: CValue<'tcx>, | |
| ret: CPlace<'tcx>, | |
| f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, CValue<'tcx>, CValue<'tcx>) -> CValue<'tcx>, | |
| ) { | |
| assert_eq!(x.layout(), y.layout()); | |
| let layout = x.layout(); | |
| let (lane_count, _lane_ty) = layout.ty.simd_size_and_type(fx.tcx); | |
| let (ret_lane_count, _ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx); | |
| assert_eq!(lane_count, ret_lane_count); | |
| for lane_idx in 0..lane_count { | |
| let x_lane = x.value_lane(fx, lane_idx); | |
| let y_lane = y.value_lane(fx, lane_idx); | |
| let res_lane = f(fx, x_lane, y_lane); | |
| ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); | |
| } | |
| } | |
| fn simd_pair_for_each_lane<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| x: CValue<'tcx>, | |
| y: CValue<'tcx>, | |
| ret: CPlace<'tcx>, | |
| f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value, Value) -> Value, | |
| ) { | |
| assert_eq!(x.layout(), y.layout()); | |
| let layout = x.layout(); | |
| let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx); | |
| let lane_layout = fx.layout_of(lane_ty); | |
| let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx); | |
| let ret_lane_layout = fx.layout_of(ret_lane_ty); | |
| assert_eq!(lane_count, ret_lane_count); | |
| for lane_idx in 0..lane_count { | |
| let x_lane = x.value_lane(fx, lane_idx).load_scalar(fx); | |
| let y_lane = y.value_lane(fx, lane_idx).load_scalar(fx); | |
| let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, x_lane, y_lane); | |
| let res_lane = CValue::by_val(res_lane, ret_lane_layout); | |
| ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); | |
| } | |
| } | |
| fn simd_horizontal_pair_for_each_lane<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| x: CValue<'tcx>, | |
| y: CValue<'tcx>, | |
| ret: CPlace<'tcx>, | |
| f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value, Value) -> Value, | |
| ) { | |
| assert_eq!(x.layout(), y.layout()); | |
| let layout = x.layout(); | |
| let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx); | |
| let lane_layout = fx.layout_of(lane_ty); | |
| let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx); | |
| let ret_lane_layout = fx.layout_of(ret_lane_ty); | |
| assert_eq!(lane_count, ret_lane_count); | |
| for lane_idx in 0..lane_count { | |
| let src = if lane_idx < (lane_count / 2) { x } else { y }; | |
| let src_idx = lane_idx % (lane_count / 2); | |
| let lhs_lane = src.value_lane(fx, src_idx * 2).load_scalar(fx); | |
| let rhs_lane = src.value_lane(fx, src_idx * 2 + 1).load_scalar(fx); | |
| let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, lhs_lane, rhs_lane); | |
| let res_lane = CValue::by_val(res_lane, ret_lane_layout); | |
| ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); | |
| } | |
| } | |
| fn simd_trio_for_each_lane<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| x: CValue<'tcx>, | |
| y: CValue<'tcx>, | |
| z: CValue<'tcx>, | |
| ret: CPlace<'tcx>, | |
| f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value, Value, Value) -> Value, | |
| ) { | |
| assert_eq!(x.layout(), y.layout()); | |
| let layout = x.layout(); | |
| let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx); | |
| let lane_layout = fx.layout_of(lane_ty); | |
| let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx); | |
| let ret_lane_layout = fx.layout_of(ret_lane_ty); | |
| assert_eq!(lane_count, ret_lane_count); | |
| for lane_idx in 0..lane_count { | |
| let x_lane = x.value_lane(fx, lane_idx).load_scalar(fx); | |
| let y_lane = y.value_lane(fx, lane_idx).load_scalar(fx); | |
| let z_lane = z.value_lane(fx, lane_idx).load_scalar(fx); | |
| let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, x_lane, y_lane, z_lane); | |
| let res_lane = CValue::by_val(res_lane, ret_lane_layout); | |
| ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); | |
| } | |
| } | |
| fn simd_reduce<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| val: CValue<'tcx>, | |
| acc: Option<Value>, | |
| ret: CPlace<'tcx>, | |
| f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Value, Value) -> Value, | |
| ) { | |
| let (lane_count, lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx); | |
| let lane_layout = fx.layout_of(lane_ty); | |
| assert_eq!(lane_layout, ret.layout()); | |
| let (mut res_val, start_lane) = | |
| if let Some(acc) = acc { (acc, 0) } else { (val.value_lane(fx, 0).load_scalar(fx), 1) }; | |
| for lane_idx in start_lane..lane_count { | |
| let lane = val.value_lane(fx, lane_idx).load_scalar(fx); | |
| res_val = f(fx, lane_layout.ty, res_val, lane); | |
| } | |
| let res = CValue::by_val(res_val, lane_layout); | |
| ret.write_cvalue(fx, res); | |
| } | |
| // FIXME move all uses to `simd_reduce` | |
| fn simd_reduce_bool<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| val: CValue<'tcx>, | |
| ret: CPlace<'tcx>, | |
| f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Value, Value) -> Value, | |
| ) { | |
| let (lane_count, _lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx); | |
| assert!(ret.layout().ty.is_bool()); | |
| let res_val = val.value_lane(fx, 0).load_scalar(fx); | |
| let mut res_val = fx.bcx.ins().band_imm_u(res_val, 1); // mask to boolean | |
| for lane_idx in 1..lane_count { | |
| let lane = val.value_lane(fx, lane_idx).load_scalar(fx); | |
| let lane = fx.bcx.ins().band_imm_u(lane, 1); // mask to boolean | |
| res_val = f(fx, res_val, lane); | |
| } | |
| let res_val = if fx.bcx.func.dfg.value_type(res_val) != types::I8 { | |
| fx.bcx.ins().ireduce(types::I8, res_val) | |
| } else { | |
| res_val | |
| }; | |
| let res = CValue::by_val(res_val, ret.layout()); | |
| ret.write_cvalue(fx, res); | |
| } | |
| fn bool_to_zero_or_max_uint<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| ty: Ty<'tcx>, | |
| val: Value, | |
| ) -> Value { | |
| let ty = fx.clif_type(ty).unwrap(); | |
| let int_ty = match ty { | |
| types::F16 => types::I16, | |
| types::F32 => types::I32, | |
| types::F64 => types::I64, | |
| types::F128 => types::I128, | |
| ty => ty, | |
| }; | |
| let mut res = fx.bcx.ins().bmask(int_ty, val); | |
| if ty.is_float() { | |
| res = codegen_bitcast(fx, ty, res); | |
| } | |
| res | |
| } | |
| pub(crate) fn codegen_intrinsic_call<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| instance: Instance<'tcx>, | |
| args: &[Spanned<mir::Operand<'tcx>>], | |
| destination: CPlace<'tcx>, | |
| target: Option<BasicBlock>, | |
| source_info: mir::SourceInfo, | |
| ) -> Result<(), Instance<'tcx>> { | |
| // When `-Zforce-intrinsic-fallback` is enabled, always use the fallback body if it exists, | |
| if fx.tcx.sess.opts.unstable_opts.force_intrinsic_fallback | |
| && let Some(def) = fx.tcx.intrinsic(instance.def_id()) | |
| && !def.must_be_overridden | |
| { | |
| return Err(Instance::new_raw(instance.def_id(), instance.args)); | |
| } | |
| let intrinsic = fx.tcx.item_name(instance.def_id()); | |
| let instance_args = instance.args; | |
| if intrinsic.as_str().starts_with("simd_") { | |
| self::simd::codegen_simd_intrinsic_call( | |
| fx, | |
| intrinsic, | |
| instance_args, | |
| args, | |
| destination, | |
| target.expect("target for simd intrinsic"), | |
| source_info.span, | |
| ); | |
| } else { | |
| codegen_regular_intrinsic_call( | |
| fx, | |
| instance, | |
| intrinsic, | |
| instance_args, | |
| args, | |
| destination, | |
| target, | |
| source_info, | |
| )?; | |
| } | |
| Ok(()) | |
| } | |
| /// How a float intrinsic is lowered: either directly to Cranelift instructions, or to a call to an | |
| /// external (libm/compiler-builtins) function. This is unrelated to the Rust fallback bodies of | |
| /// intrinsics. | |
| // FIXME: the `Libcall` cases could probably all use the Rust fallback bodies instead, which would | |
| // allow removing this enum. | |
| enum FloatIntrinsicLowering { | |
| Libcall(&'static str), | |
| Codegen(Value), | |
| } | |
| fn codegen_regular_intrinsic_call<'tcx>( | |
| fx: &mut FunctionCx<'_, '_, 'tcx>, | |
| instance: Instance<'tcx>, | |
| intrinsic: Symbol, | |
| generic_args: GenericArgs<'tcx>, | |
| args: &[Spanned<mir::Operand<'tcx>>], | |
| ret: CPlace<'tcx>, | |
| destination: Option<BasicBlock>, | |
| source_info: mir::SourceInfo, | |
| ) -> Result<(), Instance<'tcx>> { | |
| assert_eq!(generic_args, instance.args); | |
| let usize_layout = fx.layout_of(fx.tcx.types.usize); | |
| match intrinsic { | |
| sym::abort_immediate => { | |
| fx.bcx.set_cold_block(fx.bcx.current_block().unwrap()); | |
| fx.bcx.ins().trap(TrapCode::user(2).unwrap()); | |
| return Ok(()); | |
| } | |
| sym::breakpoint | sym::resumable_breakpoint => { | |
| intrinsic_args!(fx, args => (); intrinsic); | |
| fx.bcx.ins().debugtrap(); | |
| } | |
| sym::copy => { | |
| intrinsic_args!(fx, args => (src, dst, count); intrinsic); | |
| let src = src.load_scalar(fx); | |
| let dst = dst.load_scalar(fx); | |
| let count = count.load_scalar(fx); | |
| let elem_ty = generic_args.type_at(0); | |
| let elem_size: u64 = fx.layout_of(elem_ty).size.bytes(); | |
| assert_eq!(args.len(), 3); | |
| let byte_amount = if elem_size != 1 { | |
| fx.bcx.ins().imul_imm_u(count, elem_size as i64) | |
| } else { | |
| count | |
| }; | |
| // FIXME emit_small_memmove | |
| fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount); | |
| } | |
| sym::volatile_copy_memory | sym::volatile_copy_nonoverlapping_memory => { | |
| // NOTE: the volatile variants have src and dst swapped | |
| intrinsic_args!(fx, args => (dst, src, count); intrinsic); | |
| let dst = dst.load_scalar(fx); | |
| let src = src.load_scalar(fx); | |
| let count = count.load_scalar(fx); | |
| let elem_ty = generic_args.type_at(0); | |
| let elem_size: u64 = fx.layout_of(elem_ty).size.bytes(); | |
| assert_eq!(args.len(), 3); | |
| let byte_amount = if elem_size != 1 { | |
| fx.bcx.ins().imul_imm_u(count, elem_size as i64) | |
| } else { | |
| count | |
| }; | |
| // FIXME make the copy actually volatile when using emit_small_mem{cpy,move} | |
| if intrinsic == sym::volatile_copy_nonoverlapping_memory { | |
| // FIXME emit_small_memcpy | |
| fx.bcx.call_memcpy(fx.target_config, dst, src, byte_amount); | |
| } else { | |
| // FIXME emit_small_memmove | |
| fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount); | |
| } | |
| } | |
| sym::size_of_val => { | |
| intrinsic_args!(fx, args => (ptr); intrinsic); | |
| let layout = fx.layout_of(generic_args.type_at(0)); | |
| // Note: Can't use is_unsized here as truly unsized types need to take the fixed size | |
| // branch | |
| let meta = if let BackendRepr::ScalarPair { .. } = ptr.layout().backend_repr { | |
| Some(ptr.load_scalar_pair(fx).1) | |
| } else { | |
| None | |
| }; | |
| let (size, _align) = crate::unsize::size_and_align_of(fx, layout, meta); | |
| ret.write_cvalue(fx, CValue::by_val(size, usize_layout)); | |
| } | |
| sym::align_of_val => { | |
| intrinsic_args!(fx, args => (ptr); intrinsic); | |
| let layout = fx.layout_of(generic_args.type_at(0)); | |
| // Note: Can't use is_unsized here as truly unsized types need to take the fixed size | |
| // branch | |
| let meta = if let BackendRepr::ScalarPair { .. } = ptr.layout().backend_repr { | |
| Some(ptr.load_scalar_pair(fx).1) | |
| } else { | |
| None | |
| }; | |
| let (_size, align) = crate::unsize::size_and_align_of(fx, layout, meta); | |
| ret.write_cvalue(fx, CValue::by_val(align, usize_layout)); | |
| } | |
| sym::vtable_size => { | |
| intrinsic_args!(fx, args => (vtable); intrinsic); | |
| let vtable = vtable.load_scalar(fx); | |
| let size = crate::vtable::size_of_obj(fx, vtable); | |
| ret.write_cvalue(fx, CValue::by_val(size, usize_layout)); | |
| } | |
| sym::vtable_align => { | |
| intrinsic_args!(fx, args => (vtable); intrinsic); | |
| let vtable = vtable.load_scalar(fx); | |
| let align = crate::vtable::align_of_obj(fx, vtable); | |
| ret.write_cvalue(fx, CValue::by_val(align, usize_layout)); | |
| } | |
| sym::exact_div => { | |
| intrinsic_args!(fx, args => (x, y); intrinsic); | |
| // FIXME trap on inexact | |
| let res = crate::num::codegen_int_binop(fx, BinOp::Div, x, y); | |
| ret.write_cvalue(fx, res); | |
| } | |
| // FIXME: remove the guard here once `umin.i128` and friends are supported | |
| // cc https://github.com/bytecodealliance/wasmtime/issues/13790 | |
| sym::integer_max | sym::integer_min if ret.layout().size <= Size::from_bits(64) => { | |
| intrinsic_args!(fx, args => (lhs, rhs); intrinsic); | |
| assert_eq!(lhs.layout().ty, rhs.layout().ty); | |
| let signed = type_sign(lhs.layout().ty); | |
| let lhs = lhs.load_scalar(fx); | |
| let rhs = rhs.load_scalar(fx); | |
| let res = match (intrinsic, signed) { | |
| (sym::integer_max, false) => fx.bcx.ins().umax(lhs, rhs), | |
| (sym::integer_max, true) => fx.bcx.ins().smax(lhs, rhs), | |
| (sym::integer_min, false) => fx.bcx.ins().umin(lhs, rhs), | |
| (sym::integer_min, true) => fx.bcx.ins().smin(lhs, rhs), | |
| _ => unreachable!(), | |
| }; | |
| let res = CValue::by_val(res, ret.layout()); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::saturating_add | sym::saturating_sub => { | |
| intrinsic_args!(fx, args => (lhs, rhs); intrinsic); | |
| assert_eq!(lhs.layout().ty, rhs.layout().ty); | |
| let bin_op = match intrinsic { | |
| sym::saturating_add => BinOp::Add, | |
| sym::saturating_sub => BinOp::Sub, | |
| _ => unreachable!(), | |
| }; | |
| let res = crate::num::codegen_saturating_int_binop(fx, bin_op, lhs, rhs); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::rotate_left => { | |
| intrinsic_args!(fx, args => (x, y); intrinsic); | |
| let y = y.load_scalar(fx); | |
| let layout = x.layout(); | |
| let x = x.load_scalar(fx); | |
| let res = fx.bcx.ins().rotl(x, y); | |
| ret.write_cvalue(fx, CValue::by_val(res, layout)); | |
| } | |
| sym::rotate_right => { | |
| intrinsic_args!(fx, args => (x, y); intrinsic); | |
| let y = y.load_scalar(fx); | |
| let layout = x.layout(); | |
| let x = x.load_scalar(fx); | |
| let res = fx.bcx.ins().rotr(x, y); | |
| ret.write_cvalue(fx, CValue::by_val(res, layout)); | |
| } | |
| // The only difference between offset and arith_offset is regarding UB. Because Cranelift | |
| // doesn't have UB both are codegen'ed the same way | |
| sym::arith_offset => { | |
| intrinsic_args!(fx, args => (base, offset); intrinsic); | |
| let offset = offset.load_scalar(fx); | |
| let pointee_ty = base.layout().ty.builtin_deref(true).unwrap(); | |
| let pointee_size = fx.layout_of(pointee_ty).size.bytes(); | |
| let ptr_diff = if pointee_size != 1 { | |
| fx.bcx.ins().imul_imm_u(offset, pointee_size as i64) | |
| } else { | |
| offset | |
| }; | |
| let base_val = base.load_scalar(fx); | |
| let res = fx.bcx.ins().iadd(base_val, ptr_diff); | |
| ret.write_cvalue(fx, CValue::by_val(res, base.layout())); | |
| } | |
| sym::ptr_mask => { | |
| intrinsic_args!(fx, args => (ptr, mask); intrinsic); | |
| let ptr_layout = ptr.layout(); | |
| let ptr = ptr.load_scalar(fx); | |
| let mask = mask.load_scalar(fx); | |
| let res = fx.bcx.ins().band(ptr, mask); | |
| ret.write_cvalue(fx, CValue::by_val(res, ptr_layout)); | |
| } | |
| sym::write_bytes | sym::volatile_set_memory => { | |
| intrinsic_args!(fx, args => (dst, val, count); intrinsic); | |
| let val = val.load_scalar(fx); | |
| let count = count.load_scalar(fx); | |
| let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap(); | |
| let pointee_size = fx.layout_of(pointee_ty).size.bytes(); | |
| let count = if pointee_size != 1 { | |
| fx.bcx.ins().imul_imm_u(count, pointee_size as i64) | |
| } else { | |
| count | |
| }; | |
| let dst_ptr = dst.load_scalar(fx); | |
| // FIXME make the memset actually volatile when switching to emit_small_memset | |
| // FIXME use emit_small_memset | |
| fx.bcx.call_memset(fx.target_config, dst_ptr, val, count); | |
| } | |
| sym::ctlz | sym::ctlz_nonzero => { | |
| intrinsic_args!(fx, args => (arg); intrinsic); | |
| let val = arg.load_scalar(fx); | |
| // FIXME trap on `ctlz_nonzero` with zero arg. | |
| let res = fx.bcx.ins().clz(val); | |
| let res = clif_intcast(fx, res, types::I32, false); | |
| let res = CValue::by_val(res, ret.layout()); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::cttz | sym::cttz_nonzero => { | |
| intrinsic_args!(fx, args => (arg); intrinsic); | |
| let val = arg.load_scalar(fx); | |
| // FIXME trap on `cttz_nonzero` with zero arg. | |
| let res = fx.bcx.ins().ctz(val); | |
| let res = clif_intcast(fx, res, types::I32, false); | |
| let res = CValue::by_val(res, ret.layout()); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::ctpop => { | |
| intrinsic_args!(fx, args => (arg); intrinsic); | |
| let val = arg.load_scalar(fx); | |
| let res = fx.bcx.ins().popcnt(val); | |
| let res = clif_intcast(fx, res, types::I32, false); | |
| let res = CValue::by_val(res, ret.layout()); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::bitreverse => { | |
| intrinsic_args!(fx, args => (arg); intrinsic); | |
| let val = arg.load_scalar(fx); | |
| let res = fx.bcx.ins().bitrev(val); | |
| let res = CValue::by_val(res, arg.layout()); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::bswap => { | |
| intrinsic_args!(fx, args => (arg); intrinsic); | |
| let val = arg.load_scalar(fx); | |
| let res = if fx.bcx.func.dfg.value_type(val) == types::I8 { | |
| val | |
| } else { | |
| fx.bcx.ins().bswap(val) | |
| }; | |
| let res = CValue::by_val(res, arg.layout()); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::assert_inhabited | sym::assert_zero_valid | sym::assert_mem_uninitialized_valid => { | |
| intrinsic_args!(fx, args => (); intrinsic); | |
| let ty = generic_args.type_at(0); | |
| let requirement = ValidityRequirement::from_intrinsic(intrinsic); | |
| if let Some(requirement) = requirement { | |
| let do_panic = !fx | |
| .tcx | |
| .check_validity_requirement((requirement, fx.typing_env().as_query_input(ty))) | |
| .expect("expect to have layout during codegen"); | |
| if do_panic { | |
| let layout = fx.layout_of(ty); | |
| let msg_str = with_no_visible_paths!({ | |
| with_no_trimmed_paths!({ | |
| if layout.is_uninhabited() { | |
| // Use this error even for the other intrinsics as it is more precise. | |
| format!("attempted to instantiate uninhabited type `{}`", ty) | |
| } else if intrinsic == sym::assert_zero_valid { | |
| format!( | |
| "attempted to zero-initialize type `{}`, which is invalid", | |
| ty | |
| ) | |
| } else { | |
| format!( | |
| "attempted to leave type `{}` uninitialized, which is invalid", | |
| ty | |
| ) | |
| } | |
| }) | |
| }); | |
| crate::base::codegen_panic_nounwind(fx, &msg_str, source_info.span); | |
| return Ok(()); | |
| } | |
| } | |
| } | |
| sym::volatile_load | sym::unaligned_volatile_load => { | |
| intrinsic_args!(fx, args => (ptr); intrinsic); | |
| // Cranelift treats loads as volatile by default | |
| // FIXME correctly handle unaligned_volatile_load | |
| let inner_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap()); | |
| let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout); | |
| ret.write_cvalue(fx, val); | |
| } | |
| sym::volatile_store | sym::unaligned_volatile_store | sym::nontemporal_store => { | |
| intrinsic_args!(fx, args => (ptr, val); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| // Cranelift treats stores as volatile by default | |
| // FIXME correctly handle unaligned_volatile_store | |
| // FIXME actually do nontemporal stores if requested (but do not just emit MOVNT on x86; | |
| // see the LLVM backend for details) | |
| let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout()); | |
| dest.write_cvalue(fx, val); | |
| } | |
| sym::ptr_offset_from | sym::ptr_offset_from_unsigned => { | |
| intrinsic_args!(fx, args => (ptr, base); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let base = base.load_scalar(fx); | |
| let ty = generic_args.type_at(0); | |
| let pointee_size: u64 = fx.layout_of(ty).size.bytes(); | |
| let diff_bytes = fx.bcx.ins().isub(ptr, base); | |
| // FIXME this can be an exact division. | |
| let val = if intrinsic == sym::ptr_offset_from_unsigned { | |
| let usize_layout = fx.layout_of(fx.tcx.types.usize); | |
| // Because diff_bytes ULE isize::MAX, this would be fine as signed, | |
| // but unsigned is slightly easier to codegen, so might as well. | |
| CValue::by_val( | |
| fx.bcx.ins().udiv_imm_u(diff_bytes, pointee_size as i64), | |
| usize_layout, | |
| ) | |
| } else { | |
| let isize_layout = fx.layout_of(fx.tcx.types.isize); | |
| CValue::by_val( | |
| fx.bcx.ins().sdiv_imm_u(diff_bytes, pointee_size as i64), | |
| isize_layout, | |
| ) | |
| }; | |
| ret.write_cvalue(fx, val); | |
| } | |
| sym::caller_location => { | |
| intrinsic_args!(fx, args => (); intrinsic); | |
| let caller_location = fx.get_caller_location(source_info); | |
| ret.write_cvalue(fx, caller_location); | |
| } | |
| _ if intrinsic.as_str().starts_with("atomic_fence") => { | |
| intrinsic_args!(fx, args => (); intrinsic); | |
| fx.bcx.ins().fence(); | |
| } | |
| _ if intrinsic.as_str().starts_with("atomic_singlethreadfence") => { | |
| intrinsic_args!(fx, args => (); intrinsic); | |
| // FIXME use a compiler fence once Cranelift supports it | |
| fx.bcx.ins().fence(); | |
| } | |
| sym::atomic_load => { | |
| intrinsic_args!(fx, args => (ptr); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| // FIXME: this ignores the atomic ordering and the volatile flag. | |
| let ty = generic_args.type_at(0); | |
| match ty.kind() { | |
| ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => { | |
| // FIXME implement 128bit atomics | |
| if fx.tcx.is_compiler_builtins(LOCAL_CRATE) { | |
| // special case for compiler-builtins to avoid having to patch it | |
| crate::base::codegen_panic_nounwind( | |
| fx, | |
| "128bit atomics not yet supported", | |
| source_info.span, | |
| ); | |
| return Ok(()); | |
| } else { | |
| fx.tcx | |
| .dcx() | |
| .span_fatal(source_info.span, "128bit atomics not yet supported"); | |
| } | |
| } | |
| ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty); | |
| return Ok(()); | |
| } | |
| } | |
| let clif_ty = fx.clif_type(ty).unwrap(); | |
| let val = fx.bcx.ins().atomic_load(clif_ty, MemFlagsData::trusted(), ptr); | |
| let val = CValue::by_val(val, fx.layout_of(ty)); | |
| ret.write_cvalue(fx, val); | |
| } | |
| sym::atomic_store => { | |
| intrinsic_args!(fx, args => (ptr, val); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| // FIXME: this ignores the atomic ordering and the volatile flag. | |
| let ty = generic_args.type_at(0); | |
| match ty.kind() { | |
| ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => { | |
| // FIXME implement 128bit atomics | |
| if fx.tcx.is_compiler_builtins(LOCAL_CRATE) { | |
| // special case for compiler-builtins to avoid having to patch it | |
| crate::base::codegen_panic_nounwind( | |
| fx, | |
| "128bit atomics not yet supported", | |
| source_info.span, | |
| ); | |
| return Ok(()); | |
| } else { | |
| fx.tcx | |
| .dcx() | |
| .span_fatal(source_info.span, "128bit atomics not yet supported"); | |
| } | |
| } | |
| ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty); | |
| return Ok(()); | |
| } | |
| } | |
| let val = val.load_scalar(fx); | |
| fx.bcx.ins().atomic_store(MemFlagsData::trusted(), val, ptr); | |
| } | |
| sym::atomic_xchg => { | |
| intrinsic_args!(fx, args => (ptr, new); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = new.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let new = new.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Xchg, ptr, new); | |
| let old = CValue::by_val(old, layout); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_cxchg | sym::atomic_cxchgweak => { | |
| intrinsic_args!(fx, args => (ptr, test_old, new); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = new.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let test_old = test_old.load_scalar(fx); | |
| let new = new.load_scalar(fx); | |
| let old = fx.bcx.ins().atomic_cas(MemFlagsData::trusted(), ptr, test_old, new); | |
| let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old); | |
| let ret_val = CValue::by_val_pair(old, is_eq, ret.layout()); | |
| ret.write_cvalue(fx, ret_val) | |
| } | |
| sym::atomic_xadd => { | |
| intrinsic_args!(fx, args => (ptr, amount); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = amount.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let amount = amount.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Add, ptr, amount); | |
| let old = CValue::by_val(old, ret.layout()); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_xsub => { | |
| intrinsic_args!(fx, args => (ptr, amount); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = amount.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let amount = amount.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Sub, ptr, amount); | |
| let old = CValue::by_val(old, ret.layout()); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_and => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::And, ptr, src); | |
| let old = CValue::by_val(old, ret.layout()); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_or => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Or, ptr, src); | |
| let old = CValue::by_val(old, ret.layout()); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_xor => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Xor, ptr, src); | |
| let old = CValue::by_val(old, ret.layout()); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_nand => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) | ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Nand, ptr, src); | |
| let old = CValue::by_val(old, ret.layout()); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_max => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Smax, ptr, src); | |
| let old = CValue::by_val(old, layout); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_umax => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Umax, ptr, src); | |
| let old = CValue::by_val(old, layout); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_min => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Int(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Smin, ptr, src); | |
| let old = CValue::by_val(old, layout); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::atomic_umin => { | |
| intrinsic_args!(fx, args => (ptr, src); intrinsic); | |
| let ptr = ptr.load_scalar(fx); | |
| let layout = src.layout(); | |
| match layout.ty.kind() { | |
| ty::Uint(_) => {} | |
| _ => { | |
| report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); | |
| return Ok(()); | |
| } | |
| } | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let src = src.load_scalar(fx); | |
| let old = | |
| fx.bcx.ins().atomic_rmw(ty, MemFlagsData::trusted(), AtomicRmwOp::Umin, ptr, src); | |
| let old = CValue::by_val(old, layout); | |
| ret.write_cvalue(fx, old); | |
| } | |
| sym::fma | sym::fmuladd => { | |
| intrinsic_args!(fx, args => (arg1, arg2, arg3); intrinsic); | |
| let layout = arg1.layout(); | |
| let ty::Float(float_ty) = layout.ty.kind() else { | |
| span_bug!( | |
| source_info.span, | |
| "expected a float type for {:?} intrinsic, but got: {:?}", | |
| intrinsic, | |
| layout.ty | |
| ); | |
| }; | |
| use FloatIntrinsicLowering::*; | |
| use FloatTy::*; | |
| let x = arg1.load_scalar(fx); | |
| let y = arg2.load_scalar(fx); | |
| let z = arg3.load_scalar(fx); | |
| let res = match (intrinsic, float_ty) { | |
| // FIXME: calling `fma` from libc without FMA target feature uses expensive | |
| // sofware emulation, use cranelift intrinsic analogous to llvm.fmuladd.*. | |
| (sym::fma | sym::fmuladd, F32 | F64) => Codegen(fx.bcx.ins().fma(x, y, z)), | |
| (sym::fma, F128) => Libcall("fmaf128"), | |
| (sym::fmuladd, F128) | (_, F16) => { | |
| // We use the intrinsic fallback bodies for the rest | |
| return Err(Instance::new_raw(instance.def_id(), instance.args)); | |
| } | |
| _ => unreachable!(), | |
| }; | |
| let val = match res { | |
| Codegen(val) => val, | |
| Libcall(name) => { | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let arg = AbiParam::new(ty); | |
| fx.lib_call(name, vec![arg, arg, arg], vec![arg], &[x, y, z])[0] | |
| } | |
| }; | |
| let val = CValue::by_val(val, layout); | |
| ret.write_cvalue(fx, val); | |
| } | |
| sym::powf => { | |
| intrinsic_args!(fx, args => (arg1, arg2); intrinsic); | |
| let layout = arg1.layout(); | |
| let ty::Float(float_ty) = layout.ty.kind() else { | |
| span_bug!( | |
| source_info.span, | |
| "expected a float type for {:?} intrinsic, but got: {:?}", | |
| intrinsic, | |
| layout.ty | |
| ); | |
| }; | |
| use FloatIntrinsicLowering::*; | |
| use FloatTy::*; | |
| let x = arg1.load_scalar(fx); | |
| let y = arg2.load_scalar(fx); | |
| let res = match (intrinsic, float_ty) { | |
| (sym::powf, F32) => Libcall("powf"), | |
| (sym::powf, F64) => Libcall("pow"), | |
| (sym::powf, F128) => Libcall("powf128"), | |
| (_, F16) => { | |
| // We use the intrinsic fallback bodies for the rest | |
| return Err(Instance::new_raw(instance.def_id(), instance.args)); | |
| } | |
| _ => unreachable!(), | |
| }; | |
| let val = match res { | |
| Codegen(_) => span_bug!(source_info.span, "`powf` is always lowered to a libcall"), | |
| Libcall(name) => { | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| let arg = AbiParam::new(ty); | |
| fx.lib_call(name, vec![arg, arg], vec![arg], &[x, y])[0] | |
| } | |
| }; | |
| let val = CValue::by_val(val, layout); | |
| ret.write_cvalue(fx, val); | |
| } | |
| sym::copysign | |
| | sym::minimum | |
| | sym::maximum | |
| | sym::minimum_number_nsz | |
| | sym::maximum_number_nsz => { | |
| intrinsic_args!(fx, args => (arg1, arg2); intrinsic); | |
| let layout = arg1.layout(); | |
| let ty::Float(float_ty) = layout.ty.kind() else { | |
| span_bug!( | |
| source_info.span, | |
| "expected a float type for {:?} intrinsic, but got: {:?}", | |
| intrinsic, | |
| layout.ty | |
| ); | |
| }; | |
| use FloatIntrinsicLowering::*; | |
| use FloatTy::*; | |
| let x = arg1.load_scalar(fx); | |
| let y = arg2.load_scalar(fx); | |
| let res = match (intrinsic, float_ty) { | |
| (sym::copysign, F32 | F64) => Codegen(fx.bcx.ins().fcopysign(x, y)), | |
| (sym::minimum, F32 | F64) => Codegen(fx.bcx.ins().fmin(x, y)), | |
| (sym::maximum, F32 | F64) => Codegen(fx.bcx.ins().fmax(x, y)), | |
| // FIXME(bytecodealliance/wasmtime#8312): Use `fmin`/`fmax` directly for `f16` and | |
| // `f128` once the lowerings have been implemented in Cranelift. | |
| (sym::minimum, F128) => Codegen(codegen_f16_f128::fmin_f128(fx, x, y)), | |
| (sym::maximum, F128) => Codegen(codegen_f16_f128::fmax_f128(fx, x, y)), | |
| (sym::minimum, F16) => { | |
| Codegen(codegen_f16_f128::maybe_with_f16_to_f32_pair(fx, x, y, |fx, x, y| { | |
| fx.bcx.ins().fmin(x, y) | |
| })) | |
| } | |
| (sym::maximum, F16) => { | |
| Codegen(codegen_f16_f128::maybe_with_f16_to_f32_pair(fx, x, y, |fx, x, y| { | |
| fx.bcx.ins().fmax(x, y) | |
| })) | |
| } | |
| (sym::minimum_number_nsz, _) => Codegen(crate::num::codegen_float_min(fx, x, y)), | |
| (sym::maximum_number_nsz, _) => Codegen(crate::num::codegen_float_max(fx, x, y)), | |
| (sym::copysign, F128) | (_, F16) => { | |
| // We use the intrinsic fallback bodies for the rest | |
| return Err(Instance::new_raw(instance.def_id(), instance.args)); | |
| } | |
| _ => unreachable!(), | |
| }; | |
| let val = match res { | |
| Codegen(val) => val, | |
| Libcall(_) => { | |
| span_bug!(source_info.span, "no libcall lowering for {:?}", intrinsic) | |
| } | |
| }; | |
| let val = CValue::by_val(val, layout); | |
| ret.write_cvalue(fx, val); | |
| } | |
| sym::fabs | |
| | sym::sqrt | |
| | sym::floor | |
| | sym::ceil | |
| | sym::trunc | |
| | sym::round | |
| | sym::round_ties_even | |
| | sym::exp | |
| | sym::exp2 | |
| | sym::log | |
| | sym::log2 | |
| | sym::log10 | |
| | sym::sin | |
| | sym::cos => { | |
| intrinsic_args!(fx, args => (arg); intrinsic); | |
| let layout = arg.layout(); | |
| let ty::Float(float_ty) = layout.ty.kind() else { | |
| span_bug!( | |
| source_info.span, | |
| "expected a float type for {:?} intrinsic, but got: {:?}", | |
| intrinsic, | |
| layout.ty | |
| ); | |
| }; | |
| use FloatIntrinsicLowering::*; | |
| use FloatTy::*; | |
| let x = arg.load_scalar(fx); | |
| let res = match (intrinsic, float_ty) { | |
| (sym::fabs, F32 | F64) => Codegen(fx.bcx.ins().fabs(x)), | |
| (sym::sqrt, F32 | F64) => Codegen(fx.bcx.ins().sqrt(x)), | |
| (sym::sqrt, F128) => Libcall("sqrtf128"), | |
| (sym::floor, F32 | F64) => Codegen(fx.bcx.ins().floor(x)), | |
| (sym::floor, F128) => Libcall("floorf128"), | |
| (sym::ceil, F32 | F64) => Codegen(fx.bcx.ins().ceil(x)), | |
| (sym::ceil, F128) => Libcall("ceilf128"), | |
| (sym::trunc, F32 | F64) => Codegen(fx.bcx.ins().trunc(x)), | |
| (sym::trunc, F128) => Libcall("truncf128"), | |
| (sym::round_ties_even, F32 | F64) => Codegen(fx.bcx.ins().nearest(x)), | |
| (sym::round_ties_even, F128) => Libcall("rintf128"), | |
| (sym::round, F32) => Libcall("roundf"), | |
| (sym::round, F64) => Libcall("round"), | |
| (sym::round, F128) => Libcall("roundf128"), | |
| (sym::exp, F32) => Libcall("expf"), | |
| (sym::exp, F64) => Libcall("exp"), | |
| (sym::exp, F128) => Libcall("expf128"), | |
| (sym::exp2, F32) => Libcall("exp2f"), | |
| (sym::exp2, F64) => Libcall("exp2"), | |
| (sym::exp2, F128) => Libcall("exp2f128"), | |
| (sym::log, F32) => Libcall("logf"), | |
| (sym::log, F64) => Libcall("log"), | |
| (sym::log, F128) => Libcall("logf128"), | |
| (sym::log2, F32) => Libcall("log2f"), | |
| (sym::log2, F64) => Libcall("log2"), | |
| (sym::log2, F128) => Libcall("log2f128"), | |
| (sym::log10, F32) => Libcall("log10f"), | |
| (sym::log10, F64) => Libcall("log10"), | |
| (sym::log10, F128) => Libcall("log10f128"), | |
| (sym::sin, F32) => Libcall("sinf"), | |
| (sym::sin, F64) => Libcall("sin"), | |
| (sym::sin, F128) => Libcall("sinf128"), | |
| (sym::cos, F32) => Libcall("cosf"), | |
| (sym::cos, F64) => Libcall("cos"), | |
| (sym::cos, F128) => Libcall("cosf128"), | |
| (sym::fabs, F128) | (_, F16) => { | |
| // FIXME(bytecodealliance/wasmtime#8312): Use the native operations once | |
| // Cranelift backend lowerings for `f16` are implemented. | |
| // We use the intrinsic fallback bodies for the rest | |
| return Err(Instance::new_raw(instance.def_id(), instance.args)); | |
| } | |
| _ => unreachable!(), | |
| }; | |
| let val = match res { | |
| Codegen(val) => val, | |
| Libcall(name) => { | |
| let ty = fx.clif_type(layout.ty).unwrap(); | |
| fx.lib_call(name, vec![AbiParam::new(ty)], vec![AbiParam::new(ty)], &[x])[0] | |
| } | |
| }; | |
| let val = CValue::by_val(val, layout); | |
| ret.write_cvalue(fx, val); | |
| } | |
| sym::catch_unwind => { | |
| let ret_block = fx.get_block(destination.unwrap()); | |
| intrinsic_args!(fx, args => (f, data, catch_fn); intrinsic); | |
| let f = f.load_scalar(fx); | |
| let data = data.load_scalar(fx); | |
| let catch_fn = catch_fn.load_scalar(fx); | |
| let f_sig = fx.bcx.func.import_signature(Signature { | |
| call_conv: fx.target_config.default_call_conv, | |
| params: vec![AbiParam::new(pointer_ty(fx.tcx))], | |
| returns: vec![], | |
| }); | |
| if cfg!(not(feature = "unwinding")) | |
| || fx.tcx.sess.panic_strategy() == PanicStrategy::Abort | |
| { | |
| fx.bcx.ins().call_indirect(f_sig, f, &[data]); | |
| let layout = fx.layout_of(fx.tcx.types.bool); | |
| let ret_val = CValue::by_val(fx.bcx.ins().iconst(types::I8, 0), layout); | |
| ret.write_cvalue(fx, ret_val); | |
| fx.bcx.ins().jump(ret_block, &[]); | |
| } else { | |
| let catch_fn_sig = fx.bcx.func.import_signature(Signature { | |
| call_conv: fx.target_config.default_call_conv, | |
| params: vec![ | |
| AbiParam::new(pointer_ty(fx.tcx)), | |
| AbiParam::new(pointer_ty(fx.tcx)), | |
| ], | |
| returns: vec![], | |
| }); | |
| let fallthrough_block = fx.bcx.create_block(); | |
| let fallthrough_block_call = fx.bcx.func.dfg.block_call(fallthrough_block, &[]); | |
| let catch_block = fx.bcx.create_block(); | |
| let catch_block_call = | |
| fx.bcx.func.dfg.block_call(catch_block, &[BlockArg::TryCallExn(0)]); | |
| let exception_table = | |
| fx.bcx.func.dfg.exception_tables.push(ExceptionTableData::new( | |
| f_sig, | |
| fallthrough_block_call, | |
| [ExceptionTableItem::Tag( | |
| ExceptionTag::with_number(EXCEPTION_HANDLER_CATCH).unwrap(), | |
| catch_block_call, | |
| )], | |
| )); | |
| fx.bcx.ins().try_call_indirect(f, &[data], exception_table); | |
| fx.bcx.seal_block(fallthrough_block); | |
| fx.bcx.switch_to_block(fallthrough_block); | |
| let layout = fx.layout_of(fx.tcx.types.bool); | |
| let ret_val = CValue::by_val(fx.bcx.ins().iconst(types::I8, 0), layout); | |
| ret.write_cvalue(fx, ret_val); | |
| fx.bcx.ins().jump(ret_block, &[]); | |
| fx.bcx.seal_block(catch_block); | |
| fx.bcx.switch_to_block(catch_block); | |
| fx.bcx.set_cold_block(catch_block); | |
| let exception = fx.bcx.append_block_param(catch_block, pointer_ty(fx.tcx)); | |
| fx.bcx.ins().call_indirect(catch_fn_sig, catch_fn, &[data, exception]); | |
| let layout = fx.layout_of(fx.tcx.types.bool); | |
| let ret_val = CValue::by_val(fx.bcx.ins().iconst(types::I8, 1), layout); | |
| ret.write_cvalue(fx, ret_val); | |
| fx.bcx.ins().jump(ret_block, &[]); | |
| } | |
| return Ok(()); | |
| } | |
| sym::fadd_fast | |
| | sym::fsub_fast | |
| | sym::fmul_fast | |
| | sym::fdiv_fast | |
| | sym::frem_fast | |
| | sym::fadd_algebraic | |
| | sym::fsub_algebraic | |
| | sym::fmul_algebraic | |
| | sym::fdiv_algebraic | |
| | sym::frem_algebraic => { | |
| intrinsic_args!(fx, args => (x, y); intrinsic); | |
| let res = crate::num::codegen_float_binop( | |
| fx, | |
| match intrinsic { | |
| sym::fadd_fast | sym::fadd_algebraic => BinOp::Add, | |
| sym::fsub_fast | sym::fsub_algebraic => BinOp::Sub, | |
| sym::fmul_fast | sym::fmul_algebraic => BinOp::Mul, | |
| sym::fdiv_fast | sym::fdiv_algebraic => BinOp::Div, | |
| sym::frem_fast | sym::frem_algebraic => BinOp::Rem, | |
| _ => unreachable!(), | |
| }, | |
| x, | |
| y, | |
| ); | |
| ret.write_cvalue(fx, res); | |
| } | |
| sym::float_to_int_unchecked => { | |
| intrinsic_args!(fx, args => (f); intrinsic); | |
| let f = f.load_scalar(fx); | |
| let res = crate::cast::clif_int_or_float_cast( | |
| fx, | |
| f, | |
| false, | |
| fx.clif_type(ret.layout().ty).unwrap(), | |
| type_sign(ret.layout().ty), | |
| ); | |
| ret.write_cvalue(fx, CValue::by_val(res, ret.layout())); | |
| } | |
| sym::raw_eq => { | |
| intrinsic_args!(fx, args => (lhs_ref, rhs_ref); intrinsic); | |
| let lhs_ref = lhs_ref.load_scalar(fx); | |
| let rhs_ref = rhs_ref.load_scalar(fx); | |
| let size = fx.layout_of(generic_args.type_at(0)).layout.size(); | |
| // FIXME add and use emit_small_memcmp | |
| let is_eq_value = if size == Size::ZERO { | |
| // No bytes means they're trivially equal | |
| fx.bcx.ins().iconst(types::I8, 1) | |
| } else if let Some(clty) = size.bits().try_into().ok().and_then(Type::int) { | |
| // Can't use `trusted` for these loads; they could be unaligned. | |
| let flags = MemFlagsData::new().with_notrap(); | |
| let lhs_val = fx.bcx.ins().load(clty, flags, lhs_ref, 0); | |
| let rhs_val = fx.bcx.ins().load(clty, flags, rhs_ref, 0); | |
| fx.bcx.ins().icmp(IntCC::Equal, lhs_val, rhs_val) | |
| } else { | |
| // Just call `memcmp` (like slices do in core) when the | |
| // size is too large or it's not a power-of-two. | |
| let signed_bytes = i64::try_from(size.bytes()).unwrap(); | |
| let bytes_val = fx.bcx.ins().iconst(fx.pointer_type, signed_bytes); | |
| let params = vec![AbiParam::new(fx.pointer_type); 3]; | |
| let returns = vec![AbiParam::new(types::I32)]; | |
| let args = &[lhs_ref, rhs_ref, bytes_val]; | |
| let cmp = fx.lib_call("memcmp", params, returns, args)[0]; | |
| fx.bcx.ins().icmp_imm_u(IntCC::Equal, cmp, 0) | |
| }; | |
| ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout())); | |
| } | |
| sym::compare_bytes => { | |
| intrinsic_args!(fx, args => (lhs_ptr, rhs_ptr, bytes_val); intrinsic); | |
| let lhs_ptr = lhs_ptr.load_scalar(fx); | |
| let rhs_ptr = rhs_ptr.load_scalar(fx); | |
| let bytes_val = bytes_val.load_scalar(fx); | |
| let params = vec![AbiParam::new(fx.pointer_type); 3]; | |
| let returns = vec![AbiParam::new(types::I32)]; | |
| let args = &[lhs_ptr, rhs_ptr, bytes_val]; | |
| // Here we assume that the `memcmp` provided by the target is a NOP for size 0. | |
| let cmp = fx.lib_call("memcmp", params, returns, args)[0]; | |
| ret.write_cvalue(fx, CValue::by_val(cmp, ret.layout())); | |
| } | |
| sym::black_box => { | |
| intrinsic_args!(fx, args => (a); intrinsic); | |
| // FIXME implement black_box semantics | |
| ret.write_cvalue(fx, a); | |
| } | |
| // FIXME implement variadics in cranelift | |
| sym::va_arg => { | |
| fx.tcx.dcx().span_fatal( | |
| source_info.span, | |
| "Defining variadic functions is not yet supported by Cranelift", | |
| ); | |
| } | |
| sym::cold_path => { | |
| fx.bcx.set_cold_block(fx.bcx.current_block().unwrap()); | |
| } | |
| sym::return_address => { | |
| let val = fx.bcx.ins().get_return_address(fx.pointer_type); | |
| let val = CValue::by_val(val, ret.layout()); | |
| ret.write_cvalue(fx, val); | |
| } | |
| // Unimplemented intrinsics must have a fallback body. The fallback body is obtained | |
| // by converting the `InstanceKind::Intrinsic` to an `InstanceKind::Item`. | |
| _ => { | |
| let intrinsic = fx.tcx.intrinsic(instance.def_id()).unwrap(); | |
| if intrinsic.must_be_overridden { | |
| span_bug!( | |
| source_info.span, | |
| "intrinsic {} must be overridden by codegen_cranelift, but isn't", | |
| intrinsic.name, | |
| ); | |
| } | |
| return Err(Instance::new_raw(instance.def_id(), instance.args)); | |
| } | |
| } | |
| let ret_block = fx.get_block(destination.unwrap()); | |
| fx.bcx.ins().jump(ret_block, &[]); | |
| Ok(()) | |
| } |