1 //! Codegen of intrinsics. This includes `extern "rust-intrinsic"`, `extern "platform-intrinsic"`
2 //! and LLVM intrinsics that have symbol names starting with `llvm.`.
4 macro_rules! intrinsic_args {
5 ($fx:expr, $args:expr => ($($arg:tt),*); $intrinsic:expr) => {
6 #[allow(unused_parens)]
7 let ($($arg),*) = if let [$($arg),*] = $args {
8 ($(codegen_operand($fx, $arg)),*)
10 $crate::intrinsics::bug_on_incorrect_arg_count($intrinsic);
21 pub(crate) use cpuid::codegen_cpuid_call;
22 pub(crate) use llvm::codegen_llvm_intrinsic_call;
24 use rustc_middle::ty::layout::HasParamEnv;
25 use rustc_middle::ty::print::with_no_trimmed_paths;
26 use rustc_middle::ty::subst::SubstsRef;
27 use rustc_span::symbol::{kw, sym, Symbol};
29 use crate::prelude::*;
30 use cranelift_codegen::ir::AtomicRmwOp;
32 fn bug_on_incorrect_arg_count(intrinsic: impl std::fmt::Display) -> ! {
33 bug!("wrong number of args for intrinsic {}", intrinsic);
36 fn report_atomic_type_validation_error<'tcx>(
37 fx: &mut FunctionCx<'_, '_, 'tcx>,
45 "`{}` intrinsic: expected basic integer or raw pointer type, found `{:?}`",
49 // Prevent verifier error
50 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
53 pub(crate) fn clif_vector_type<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>) -> Option<Type> {
54 let (element, count) = match layout.abi {
55 Abi::Vector { element, count } => (element, count),
59 match scalar_to_clif_type(tcx, element).by(u32::try_from(count).unwrap()) {
60 // Cranelift currently only implements icmp for 128bit vectors.
61 Some(vector_ty) if vector_ty.bits() == 128 => Some(vector_ty),
66 fn simd_for_each_lane<'tcx>(
67 fx: &mut FunctionCx<'_, '_, 'tcx>,
70 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value) -> Value,
72 let layout = val.layout();
74 let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
75 let lane_layout = fx.layout_of(lane_ty);
76 let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
77 let ret_lane_layout = fx.layout_of(ret_lane_ty);
78 assert_eq!(lane_count, ret_lane_count);
80 for lane_idx in 0..lane_count {
81 let lane = val.value_lane(fx, lane_idx).load_scalar(fx);
83 let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, lane);
84 let res_lane = CValue::by_val(res_lane, ret_lane_layout);
86 ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane);
90 fn simd_pair_for_each_lane_typed<'tcx>(
91 fx: &mut FunctionCx<'_, '_, 'tcx>,
95 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, CValue<'tcx>, CValue<'tcx>) -> CValue<'tcx>,
97 assert_eq!(x.layout(), y.layout());
98 let layout = x.layout();
100 let (lane_count, _lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
101 let (ret_lane_count, _ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
102 assert_eq!(lane_count, ret_lane_count);
104 for lane_idx in 0..lane_count {
105 let x_lane = x.value_lane(fx, lane_idx);
106 let y_lane = y.value_lane(fx, lane_idx);
108 let res_lane = f(fx, x_lane, y_lane);
110 ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane);
114 fn simd_pair_for_each_lane<'tcx>(
115 fx: &mut FunctionCx<'_, '_, 'tcx>,
119 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value, Value) -> Value,
121 assert_eq!(x.layout(), y.layout());
122 let layout = x.layout();
124 let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
125 let lane_layout = fx.layout_of(lane_ty);
126 let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
127 let ret_lane_layout = fx.layout_of(ret_lane_ty);
128 assert_eq!(lane_count, ret_lane_count);
130 for lane_idx in 0..lane_count {
131 let x_lane = x.value_lane(fx, lane_idx).load_scalar(fx);
132 let y_lane = y.value_lane(fx, lane_idx).load_scalar(fx);
134 let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, x_lane, y_lane);
135 let res_lane = CValue::by_val(res_lane, ret_lane_layout);
137 ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane);
141 fn simd_reduce<'tcx>(
142 fx: &mut FunctionCx<'_, '_, 'tcx>,
146 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Value, Value) -> Value,
148 let (lane_count, lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx);
149 let lane_layout = fx.layout_of(lane_ty);
150 assert_eq!(lane_layout, ret.layout());
152 let (mut res_val, start_lane) =
153 if let Some(acc) = acc { (acc, 0) } else { (val.value_lane(fx, 0).load_scalar(fx), 1) };
154 for lane_idx in start_lane..lane_count {
155 let lane = val.value_lane(fx, lane_idx).load_scalar(fx);
156 res_val = f(fx, lane_layout.ty, res_val, lane);
158 let res = CValue::by_val(res_val, lane_layout);
159 ret.write_cvalue(fx, res);
162 // FIXME move all uses to `simd_reduce`
163 fn simd_reduce_bool<'tcx>(
164 fx: &mut FunctionCx<'_, '_, 'tcx>,
167 f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Value, Value) -> Value,
169 let (lane_count, _lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx);
170 assert!(ret.layout().ty.is_bool());
172 let res_val = val.value_lane(fx, 0).load_scalar(fx);
173 let mut res_val = fx.bcx.ins().band_imm(res_val, 1); // mask to boolean
174 for lane_idx in 1..lane_count {
175 let lane = val.value_lane(fx, lane_idx).load_scalar(fx);
176 let lane = fx.bcx.ins().band_imm(lane, 1); // mask to boolean
177 res_val = f(fx, res_val, lane);
179 let res_val = if fx.bcx.func.dfg.value_type(res_val) != types::I8 {
180 fx.bcx.ins().ireduce(types::I8, res_val)
184 let res = CValue::by_val(res_val, ret.layout());
185 ret.write_cvalue(fx, res);
188 fn bool_to_zero_or_max_uint<'tcx>(
189 fx: &mut FunctionCx<'_, '_, 'tcx>,
193 let ty = fx.clif_type(ty).unwrap();
195 let int_ty = match ty {
196 types::F32 => types::I32,
197 types::F64 => types::I64,
201 let mut res = fx.bcx.ins().bmask(int_ty, val);
204 res = codegen_bitcast(fx, ty, res);
210 pub(crate) fn codegen_intrinsic_call<'tcx>(
211 fx: &mut FunctionCx<'_, '_, 'tcx>,
212 instance: Instance<'tcx>,
213 args: &[mir::Operand<'tcx>],
214 destination: CPlace<'tcx>,
215 target: Option<BasicBlock>,
216 source_info: mir::SourceInfo,
218 let intrinsic = fx.tcx.item_name(instance.def_id());
219 let substs = instance.substs;
221 let target = if let Some(target) = target {
224 // Insert non returning intrinsics here
227 fx.bcx.ins().trap(TrapCode::User(0));
230 crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", source_info);
232 _ => unimplemented!("unsupported intrinsic {}", intrinsic),
237 if intrinsic.as_str().starts_with("simd_") {
238 self::simd::codegen_simd_intrinsic_call(
247 } else if codegen_float_intrinsic_call(fx, intrinsic, args, destination) {
248 let ret_block = fx.get_block(target);
249 fx.bcx.ins().jump(ret_block, &[]);
251 codegen_regular_intrinsic_call(
264 fn codegen_float_intrinsic_call<'tcx>(
265 fx: &mut FunctionCx<'_, '_, 'tcx>,
267 args: &[mir::Operand<'tcx>],
270 let (name, arg_count, ty) = match intrinsic {
271 sym::expf32 => ("expf", 1, fx.tcx.types.f32),
272 sym::expf64 => ("exp", 1, fx.tcx.types.f64),
273 sym::exp2f32 => ("exp2f", 1, fx.tcx.types.f32),
274 sym::exp2f64 => ("exp2", 1, fx.tcx.types.f64),
275 sym::sqrtf32 => ("sqrtf", 1, fx.tcx.types.f32),
276 sym::sqrtf64 => ("sqrt", 1, fx.tcx.types.f64),
277 sym::powif32 => ("__powisf2", 2, fx.tcx.types.f32), // compiler-builtins
278 sym::powif64 => ("__powidf2", 2, fx.tcx.types.f64), // compiler-builtins
279 sym::powf32 => ("powf", 2, fx.tcx.types.f32),
280 sym::powf64 => ("pow", 2, fx.tcx.types.f64),
281 sym::logf32 => ("logf", 1, fx.tcx.types.f32),
282 sym::logf64 => ("log", 1, fx.tcx.types.f64),
283 sym::log2f32 => ("log2f", 1, fx.tcx.types.f32),
284 sym::log2f64 => ("log2", 1, fx.tcx.types.f64),
285 sym::log10f32 => ("log10f", 1, fx.tcx.types.f32),
286 sym::log10f64 => ("log10", 1, fx.tcx.types.f64),
287 sym::fabsf32 => ("fabsf", 1, fx.tcx.types.f32),
288 sym::fabsf64 => ("fabs", 1, fx.tcx.types.f64),
289 sym::fmaf32 => ("fmaf", 3, fx.tcx.types.f32),
290 sym::fmaf64 => ("fma", 3, fx.tcx.types.f64),
291 sym::copysignf32 => ("copysignf", 2, fx.tcx.types.f32),
292 sym::copysignf64 => ("copysign", 2, fx.tcx.types.f64),
293 sym::floorf32 => ("floorf", 1, fx.tcx.types.f32),
294 sym::floorf64 => ("floor", 1, fx.tcx.types.f64),
295 sym::ceilf32 => ("ceilf", 1, fx.tcx.types.f32),
296 sym::ceilf64 => ("ceil", 1, fx.tcx.types.f64),
297 sym::truncf32 => ("truncf", 1, fx.tcx.types.f32),
298 sym::truncf64 => ("trunc", 1, fx.tcx.types.f64),
299 sym::roundf32 => ("roundf", 1, fx.tcx.types.f32),
300 sym::roundf64 => ("round", 1, fx.tcx.types.f64),
301 sym::sinf32 => ("sinf", 1, fx.tcx.types.f32),
302 sym::sinf64 => ("sin", 1, fx.tcx.types.f64),
303 sym::cosf32 => ("cosf", 1, fx.tcx.types.f32),
304 sym::cosf64 => ("cos", 1, fx.tcx.types.f64),
308 if args.len() != arg_count {
309 bug!("wrong number of args for intrinsic {:?}", intrinsic);
313 let args = match args {
315 a = [codegen_operand(fx, x)];
319 b = [codegen_operand(fx, x), codegen_operand(fx, y)];
323 c = [codegen_operand(fx, x), codegen_operand(fx, y), codegen_operand(fx, z)];
329 let layout = fx.layout_of(ty);
330 let res = match intrinsic {
331 sym::fmaf32 | sym::fmaf64 => {
332 let a = args[0].load_scalar(fx);
333 let b = args[1].load_scalar(fx);
334 let c = args[2].load_scalar(fx);
335 CValue::by_val(fx.bcx.ins().fma(a, b, c), layout)
337 sym::copysignf32 | sym::copysignf64 => {
338 let a = args[0].load_scalar(fx);
339 let b = args[1].load_scalar(fx);
340 CValue::by_val(fx.bcx.ins().fcopysign(a, b), layout)
350 let a = args[0].load_scalar(fx);
352 let val = match intrinsic {
353 sym::fabsf32 | sym::fabsf64 => fx.bcx.ins().fabs(a),
354 sym::floorf32 | sym::floorf64 => fx.bcx.ins().floor(a),
355 sym::ceilf32 | sym::ceilf64 => fx.bcx.ins().ceil(a),
356 sym::truncf32 | sym::truncf64 => fx.bcx.ins().trunc(a),
360 CValue::by_val(val, layout)
362 // These intrinsics aren't supported natively by Cranelift.
363 // Lower them to a libcall.
364 _ => fx.easy_call(name, &args, ty),
367 ret.write_cvalue(fx, res);
372 fn codegen_regular_intrinsic_call<'tcx>(
373 fx: &mut FunctionCx<'_, '_, 'tcx>,
374 instance: Instance<'tcx>,
376 substs: SubstsRef<'tcx>,
377 args: &[mir::Operand<'tcx>],
379 destination: Option<BasicBlock>,
380 source_info: mir::SourceInfo,
382 let usize_layout = fx.layout_of(fx.tcx.types.usize);
385 sym::likely | sym::unlikely => {
386 intrinsic_args!(fx, args => (a); intrinsic);
388 ret.write_cvalue(fx, a);
391 intrinsic_args!(fx, args => (); intrinsic);
393 fx.bcx.ins().debugtrap();
395 sym::copy | sym::copy_nonoverlapping => {
396 intrinsic_args!(fx, args => (src, dst, count); intrinsic);
397 let src = src.load_scalar(fx);
398 let dst = dst.load_scalar(fx);
399 let count = count.load_scalar(fx);
401 let elem_ty = substs.type_at(0);
402 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
403 assert_eq!(args.len(), 3);
405 if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
407 if intrinsic == sym::copy_nonoverlapping {
408 // FIXME emit_small_memcpy
409 fx.bcx.call_memcpy(fx.target_config, dst, src, byte_amount);
411 // FIXME emit_small_memmove
412 fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount);
415 sym::volatile_copy_memory | sym::volatile_copy_nonoverlapping_memory => {
416 // NOTE: the volatile variants have src and dst swapped
417 intrinsic_args!(fx, args => (dst, src, count); intrinsic);
418 let dst = dst.load_scalar(fx);
419 let src = src.load_scalar(fx);
420 let count = count.load_scalar(fx);
422 let elem_ty = substs.type_at(0);
423 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
424 assert_eq!(args.len(), 3);
426 if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
428 // FIXME make the copy actually volatile when using emit_small_mem{cpy,move}
429 if intrinsic == sym::volatile_copy_nonoverlapping_memory {
430 // FIXME emit_small_memcpy
431 fx.bcx.call_memcpy(fx.target_config, dst, src, byte_amount);
433 // FIXME emit_small_memmove
434 fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount);
437 sym::size_of_val => {
438 intrinsic_args!(fx, args => (ptr); intrinsic);
440 let layout = fx.layout_of(substs.type_at(0));
441 // Note: Can't use is_unsized here as truly unsized types need to take the fixed size
443 let size = if let Abi::ScalarPair(_, _) = ptr.layout().abi {
444 let (_ptr, info) = ptr.load_scalar_pair(fx);
445 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
448 fx.bcx.ins().iconst(fx.pointer_type, layout.size.bytes() as i64)
450 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
452 sym::min_align_of_val => {
453 intrinsic_args!(fx, args => (ptr); intrinsic);
455 let layout = fx.layout_of(substs.type_at(0));
456 // Note: Can't use is_unsized here as truly unsized types need to take the fixed size
458 let align = if let Abi::ScalarPair(_, _) = ptr.layout().abi {
459 let (_ptr, info) = ptr.load_scalar_pair(fx);
460 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
463 fx.bcx.ins().iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
465 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
468 sym::vtable_size => {
469 intrinsic_args!(fx, args => (vtable); intrinsic);
470 let vtable = vtable.load_scalar(fx);
472 let size = crate::vtable::size_of_obj(fx, vtable);
473 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
476 sym::vtable_align => {
477 intrinsic_args!(fx, args => (vtable); intrinsic);
478 let vtable = vtable.load_scalar(fx);
480 let align = crate::vtable::min_align_of_obj(fx, vtable);
481 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
491 | sym::unchecked_shr => {
492 intrinsic_args!(fx, args => (x, y); intrinsic);
494 // FIXME trap on overflow
495 let bin_op = match intrinsic {
496 sym::unchecked_add => BinOp::Add,
497 sym::unchecked_sub => BinOp::Sub,
498 sym::unchecked_mul => BinOp::Mul,
499 sym::unchecked_div | sym::exact_div => BinOp::Div,
500 sym::unchecked_rem => BinOp::Rem,
501 sym::unchecked_shl => BinOp::Shl,
502 sym::unchecked_shr => BinOp::Shr,
505 let res = crate::num::codegen_int_binop(fx, bin_op, x, y);
506 ret.write_cvalue(fx, res);
508 sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
509 intrinsic_args!(fx, args => (x, y); intrinsic);
511 assert_eq!(x.layout().ty, y.layout().ty);
512 let bin_op = match intrinsic {
513 sym::add_with_overflow => BinOp::Add,
514 sym::sub_with_overflow => BinOp::Sub,
515 sym::mul_with_overflow => BinOp::Mul,
519 let res = crate::num::codegen_checked_int_binop(fx, bin_op, x, y);
520 ret.write_cvalue(fx, res);
522 sym::saturating_add | sym::saturating_sub => {
523 intrinsic_args!(fx, args => (lhs, rhs); intrinsic);
525 assert_eq!(lhs.layout().ty, rhs.layout().ty);
526 let bin_op = match intrinsic {
527 sym::saturating_add => BinOp::Add,
528 sym::saturating_sub => BinOp::Sub,
532 let res = crate::num::codegen_saturating_int_binop(fx, bin_op, lhs, rhs);
533 ret.write_cvalue(fx, res);
535 sym::rotate_left => {
536 intrinsic_args!(fx, args => (x, y); intrinsic);
537 let y = y.load_scalar(fx);
539 let layout = x.layout();
540 let x = x.load_scalar(fx);
541 let res = fx.bcx.ins().rotl(x, y);
542 ret.write_cvalue(fx, CValue::by_val(res, layout));
544 sym::rotate_right => {
545 intrinsic_args!(fx, args => (x, y); intrinsic);
546 let y = y.load_scalar(fx);
548 let layout = x.layout();
549 let x = x.load_scalar(fx);
550 let res = fx.bcx.ins().rotr(x, y);
551 ret.write_cvalue(fx, CValue::by_val(res, layout));
554 // The only difference between offset and arith_offset is regarding UB. Because Cranelift
555 // doesn't have UB both are codegen'ed the same way
556 sym::offset | sym::arith_offset => {
557 intrinsic_args!(fx, args => (base, offset); intrinsic);
558 let offset = offset.load_scalar(fx);
560 let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
561 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
562 let ptr_diff = if pointee_size != 1 {
563 fx.bcx.ins().imul_imm(offset, pointee_size as i64)
567 let base_val = base.load_scalar(fx);
568 let res = fx.bcx.ins().iadd(base_val, ptr_diff);
569 ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
573 intrinsic_args!(fx, args => (ptr, mask); intrinsic);
574 let ptr = ptr.load_scalar(fx);
575 let mask = mask.load_scalar(fx);
576 fx.bcx.ins().band(ptr, mask);
580 intrinsic_args!(fx, args => (from); intrinsic);
582 ret.write_cvalue_transmute(fx, from);
584 sym::write_bytes | sym::volatile_set_memory => {
585 intrinsic_args!(fx, args => (dst, val, count); intrinsic);
586 let val = val.load_scalar(fx);
587 let count = count.load_scalar(fx);
589 let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
590 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
591 let count = if pointee_size != 1 {
592 fx.bcx.ins().imul_imm(count, pointee_size as i64)
596 let dst_ptr = dst.load_scalar(fx);
597 // FIXME make the memset actually volatile when switching to emit_small_memset
598 // FIXME use emit_small_memset
599 fx.bcx.call_memset(fx.target_config, dst_ptr, val, count);
601 sym::ctlz | sym::ctlz_nonzero => {
602 intrinsic_args!(fx, args => (arg); intrinsic);
603 let val = arg.load_scalar(fx);
605 // FIXME trap on `ctlz_nonzero` with zero arg.
606 let res = fx.bcx.ins().clz(val);
607 let res = CValue::by_val(res, arg.layout());
608 ret.write_cvalue(fx, res);
610 sym::cttz | sym::cttz_nonzero => {
611 intrinsic_args!(fx, args => (arg); intrinsic);
612 let val = arg.load_scalar(fx);
614 // FIXME trap on `cttz_nonzero` with zero arg.
615 let res = fx.bcx.ins().ctz(val);
616 let res = CValue::by_val(res, arg.layout());
617 ret.write_cvalue(fx, res);
620 intrinsic_args!(fx, args => (arg); intrinsic);
621 let val = arg.load_scalar(fx);
623 let res = fx.bcx.ins().popcnt(val);
624 let res = CValue::by_val(res, arg.layout());
625 ret.write_cvalue(fx, res);
628 intrinsic_args!(fx, args => (arg); intrinsic);
629 let val = arg.load_scalar(fx);
631 let res = fx.bcx.ins().bitrev(val);
632 let res = CValue::by_val(res, arg.layout());
633 ret.write_cvalue(fx, res);
636 intrinsic_args!(fx, args => (arg); intrinsic);
637 let val = arg.load_scalar(fx);
639 let res = if fx.bcx.func.dfg.value_type(val) == types::I8 {
642 fx.bcx.ins().bswap(val)
644 let res = CValue::by_val(res, arg.layout());
645 ret.write_cvalue(fx, res);
647 sym::assert_inhabited | sym::assert_zero_valid | sym::assert_mem_uninitialized_valid => {
648 intrinsic_args!(fx, args => (); intrinsic);
650 let layout = fx.layout_of(substs.type_at(0));
651 if layout.abi.is_uninhabited() {
652 with_no_trimmed_paths!({
653 crate::base::codegen_panic_nounwind(
655 &format!("attempted to instantiate uninhabited type `{}`", layout.ty),
662 if intrinsic == sym::assert_zero_valid
663 && !fx.tcx.permits_zero_init(fx.param_env().and(layout))
665 with_no_trimmed_paths!({
666 crate::base::codegen_panic_nounwind(
669 "attempted to zero-initialize type `{}`, which is invalid",
678 if intrinsic == sym::assert_mem_uninitialized_valid
679 && !fx.tcx.permits_uninit_init(fx.param_env().and(layout))
681 with_no_trimmed_paths!({
682 crate::base::codegen_panic_nounwind(
685 "attempted to leave type `{}` uninitialized, which is invalid",
695 sym::volatile_load | sym::unaligned_volatile_load => {
696 intrinsic_args!(fx, args => (ptr); intrinsic);
698 // Cranelift treats loads as volatile by default
699 // FIXME correctly handle unaligned_volatile_load
700 let inner_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
701 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
702 ret.write_cvalue(fx, val);
704 sym::volatile_store | sym::unaligned_volatile_store => {
705 intrinsic_args!(fx, args => (ptr, val); intrinsic);
706 let ptr = ptr.load_scalar(fx);
708 // Cranelift treats stores as volatile by default
709 // FIXME correctly handle unaligned_volatile_store
710 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
711 dest.write_cvalue(fx, val);
718 | sym::variant_count => {
719 intrinsic_args!(fx, args => (); intrinsic);
722 fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
723 let val = crate::constant::codegen_const_value(fx, const_val, ret.layout().ty);
724 ret.write_cvalue(fx, val);
727 sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
728 intrinsic_args!(fx, args => (ptr, base); intrinsic);
729 let ptr = ptr.load_scalar(fx);
730 let base = base.load_scalar(fx);
731 let ty = substs.type_at(0);
733 let pointee_size: u64 = fx.layout_of(ty).size.bytes();
734 let diff_bytes = fx.bcx.ins().isub(ptr, base);
735 // FIXME this can be an exact division.
736 let val = if intrinsic == sym::ptr_offset_from_unsigned {
737 let usize_layout = fx.layout_of(fx.tcx.types.usize);
738 // Because diff_bytes ULE isize::MAX, this would be fine as signed,
739 // but unsigned is slightly easier to codegen, so might as well.
740 CValue::by_val(fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64), usize_layout)
742 let isize_layout = fx.layout_of(fx.tcx.types.isize);
743 CValue::by_val(fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64), isize_layout)
745 ret.write_cvalue(fx, val);
748 sym::ptr_guaranteed_cmp => {
749 intrinsic_args!(fx, args => (a, b); intrinsic);
751 let val = crate::num::codegen_ptr_binop(fx, BinOp::Eq, a, b).load_scalar(fx);
752 ret.write_cvalue(fx, CValue::by_val(val, fx.layout_of(fx.tcx.types.u8)));
755 sym::caller_location => {
756 intrinsic_args!(fx, args => (); intrinsic);
758 let caller_location = fx.get_caller_location(source_info);
759 ret.write_cvalue(fx, caller_location);
762 _ if intrinsic.as_str().starts_with("atomic_fence") => {
763 intrinsic_args!(fx, args => (); intrinsic);
765 fx.bcx.ins().fence();
767 _ if intrinsic.as_str().starts_with("atomic_singlethreadfence") => {
768 intrinsic_args!(fx, args => (); intrinsic);
770 // FIXME use a compiler fence once Cranelift supports it
771 fx.bcx.ins().fence();
773 _ if intrinsic.as_str().starts_with("atomic_load") => {
774 intrinsic_args!(fx, args => (ptr); intrinsic);
775 let ptr = ptr.load_scalar(fx);
777 let ty = substs.type_at(0);
779 ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
780 // FIXME implement 128bit atomics
781 if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
782 // special case for compiler-builtins to avoid having to patch it
783 crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
788 .span_fatal(source_info.span, "128bit atomics not yet supported");
791 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
793 report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
797 let clif_ty = fx.clif_type(ty).unwrap();
799 let val = fx.bcx.ins().atomic_load(clif_ty, MemFlags::trusted(), ptr);
801 let val = CValue::by_val(val, fx.layout_of(ty));
802 ret.write_cvalue(fx, val);
804 _ if intrinsic.as_str().starts_with("atomic_store") => {
805 intrinsic_args!(fx, args => (ptr, val); intrinsic);
806 let ptr = ptr.load_scalar(fx);
808 let ty = substs.type_at(0);
810 ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
811 // FIXME implement 128bit atomics
812 if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
813 // special case for compiler-builtins to avoid having to patch it
814 crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
819 .span_fatal(source_info.span, "128bit atomics not yet supported");
822 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
824 report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
829 let val = val.load_scalar(fx);
831 fx.bcx.ins().atomic_store(MemFlags::trusted(), val, ptr);
833 _ if intrinsic.as_str().starts_with("atomic_xchg") => {
834 intrinsic_args!(fx, args => (ptr, new); intrinsic);
835 let ptr = ptr.load_scalar(fx);
837 let layout = new.layout();
838 match layout.ty.kind() {
839 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
841 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
845 let ty = fx.clif_type(layout.ty).unwrap();
847 let new = new.load_scalar(fx);
849 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xchg, ptr, new);
851 let old = CValue::by_val(old, layout);
852 ret.write_cvalue(fx, old);
854 _ if intrinsic.as_str().starts_with("atomic_cxchg") => {
855 // both atomic_cxchg_* and atomic_cxchgweak_*
856 intrinsic_args!(fx, args => (ptr, test_old, new); intrinsic);
857 let ptr = ptr.load_scalar(fx);
859 let layout = new.layout();
860 match layout.ty.kind() {
861 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
863 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
868 let test_old = test_old.load_scalar(fx);
869 let new = new.load_scalar(fx);
871 let old = fx.bcx.ins().atomic_cas(MemFlags::trusted(), ptr, test_old, new);
872 let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
874 let ret_val = CValue::by_val_pair(old, is_eq, ret.layout());
875 ret.write_cvalue(fx, ret_val)
878 _ if intrinsic.as_str().starts_with("atomic_xadd") => {
879 intrinsic_args!(fx, args => (ptr, amount); intrinsic);
880 let ptr = ptr.load_scalar(fx);
882 let layout = amount.layout();
883 match layout.ty.kind() {
884 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
886 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
890 let ty = fx.clif_type(layout.ty).unwrap();
892 let amount = amount.load_scalar(fx);
895 fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Add, ptr, amount);
897 let old = CValue::by_val(old, layout);
898 ret.write_cvalue(fx, old);
900 _ if intrinsic.as_str().starts_with("atomic_xsub") => {
901 intrinsic_args!(fx, args => (ptr, amount); intrinsic);
902 let ptr = ptr.load_scalar(fx);
904 let layout = amount.layout();
905 match layout.ty.kind() {
906 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
908 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
912 let ty = fx.clif_type(layout.ty).unwrap();
914 let amount = amount.load_scalar(fx);
917 fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Sub, ptr, amount);
919 let old = CValue::by_val(old, layout);
920 ret.write_cvalue(fx, old);
922 _ if intrinsic.as_str().starts_with("atomic_and") => {
923 intrinsic_args!(fx, args => (ptr, src); intrinsic);
924 let ptr = ptr.load_scalar(fx);
926 let layout = src.layout();
927 match layout.ty.kind() {
928 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
930 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
934 let ty = fx.clif_type(layout.ty).unwrap();
936 let src = src.load_scalar(fx);
938 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::And, ptr, src);
940 let old = CValue::by_val(old, layout);
941 ret.write_cvalue(fx, old);
943 _ if intrinsic.as_str().starts_with("atomic_or") => {
944 intrinsic_args!(fx, args => (ptr, src); intrinsic);
945 let ptr = ptr.load_scalar(fx);
947 let layout = src.layout();
948 match layout.ty.kind() {
949 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
951 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
955 let ty = fx.clif_type(layout.ty).unwrap();
957 let src = src.load_scalar(fx);
959 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Or, ptr, src);
961 let old = CValue::by_val(old, layout);
962 ret.write_cvalue(fx, old);
964 _ if intrinsic.as_str().starts_with("atomic_xor") => {
965 intrinsic_args!(fx, args => (ptr, src); intrinsic);
966 let ptr = ptr.load_scalar(fx);
968 let layout = src.layout();
969 match layout.ty.kind() {
970 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
972 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
976 let ty = fx.clif_type(layout.ty).unwrap();
978 let src = src.load_scalar(fx);
980 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xor, ptr, src);
982 let old = CValue::by_val(old, layout);
983 ret.write_cvalue(fx, old);
985 _ if intrinsic.as_str().starts_with("atomic_nand") => {
986 intrinsic_args!(fx, args => (ptr, src); intrinsic);
987 let ptr = ptr.load_scalar(fx);
989 let layout = src.layout();
990 match layout.ty.kind() {
991 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
993 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
997 let ty = fx.clif_type(layout.ty).unwrap();
999 let src = src.load_scalar(fx);
1001 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Nand, ptr, src);
1003 let old = CValue::by_val(old, layout);
1004 ret.write_cvalue(fx, old);
1006 _ if intrinsic.as_str().starts_with("atomic_max") => {
1007 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1008 let ptr = ptr.load_scalar(fx);
1010 let layout = src.layout();
1011 match layout.ty.kind() {
1012 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1014 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1018 let ty = fx.clif_type(layout.ty).unwrap();
1020 let src = src.load_scalar(fx);
1022 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smax, ptr, src);
1024 let old = CValue::by_val(old, layout);
1025 ret.write_cvalue(fx, old);
1027 _ if intrinsic.as_str().starts_with("atomic_umax") => {
1028 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1029 let ptr = ptr.load_scalar(fx);
1031 let layout = src.layout();
1032 match layout.ty.kind() {
1033 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1035 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1039 let ty = fx.clif_type(layout.ty).unwrap();
1041 let src = src.load_scalar(fx);
1043 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umax, ptr, src);
1045 let old = CValue::by_val(old, layout);
1046 ret.write_cvalue(fx, old);
1048 _ if intrinsic.as_str().starts_with("atomic_min") => {
1049 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1050 let ptr = ptr.load_scalar(fx);
1052 let layout = src.layout();
1053 match layout.ty.kind() {
1054 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1056 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1060 let ty = fx.clif_type(layout.ty).unwrap();
1062 let src = src.load_scalar(fx);
1064 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smin, ptr, src);
1066 let old = CValue::by_val(old, layout);
1067 ret.write_cvalue(fx, old);
1069 _ if intrinsic.as_str().starts_with("atomic_umin") => {
1070 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1071 let ptr = ptr.load_scalar(fx);
1073 let layout = src.layout();
1074 match layout.ty.kind() {
1075 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1077 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1081 let ty = fx.clif_type(layout.ty).unwrap();
1083 let src = src.load_scalar(fx);
1085 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umin, ptr, src);
1087 let old = CValue::by_val(old, layout);
1088 ret.write_cvalue(fx, old);
1092 intrinsic_args!(fx, args => (a, b); intrinsic);
1093 let a = a.load_scalar(fx);
1094 let b = b.load_scalar(fx);
1096 let val = crate::num::codegen_float_min(fx, a, b);
1097 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1098 ret.write_cvalue(fx, val);
1101 intrinsic_args!(fx, args => (a, b); intrinsic);
1102 let a = a.load_scalar(fx);
1103 let b = b.load_scalar(fx);
1105 let val = crate::num::codegen_float_min(fx, a, b);
1106 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1107 ret.write_cvalue(fx, val);
1110 intrinsic_args!(fx, args => (a, b); intrinsic);
1111 let a = a.load_scalar(fx);
1112 let b = b.load_scalar(fx);
1114 let val = crate::num::codegen_float_max(fx, a, b);
1115 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1116 ret.write_cvalue(fx, val);
1119 intrinsic_args!(fx, args => (a, b); intrinsic);
1120 let a = a.load_scalar(fx);
1121 let b = b.load_scalar(fx);
1123 let val = crate::num::codegen_float_max(fx, a, b);
1124 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1125 ret.write_cvalue(fx, val);
1129 intrinsic_args!(fx, args => (f, data, catch_fn); intrinsic);
1130 let f = f.load_scalar(fx);
1131 let data = data.load_scalar(fx);
1132 let _catch_fn = catch_fn.load_scalar(fx);
1134 // FIXME once unwinding is supported, change this to actually catch panics
1135 let f_sig = fx.bcx.func.import_signature(Signature {
1136 call_conv: fx.target_config.default_call_conv,
1137 params: vec![AbiParam::new(pointer_ty(fx.tcx))],
1141 fx.bcx.ins().call_indirect(f_sig, f, &[data]);
1143 let layout = ret.layout();
1144 let ret_val = CValue::const_val(fx, layout, ty::ScalarInt::null(layout.size));
1145 ret.write_cvalue(fx, ret_val);
1148 sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
1149 intrinsic_args!(fx, args => (x, y); intrinsic);
1151 let res = crate::num::codegen_float_binop(
1154 sym::fadd_fast => BinOp::Add,
1155 sym::fsub_fast => BinOp::Sub,
1156 sym::fmul_fast => BinOp::Mul,
1157 sym::fdiv_fast => BinOp::Div,
1158 sym::frem_fast => BinOp::Rem,
1159 _ => unreachable!(),
1164 ret.write_cvalue(fx, res);
1166 sym::float_to_int_unchecked => {
1167 intrinsic_args!(fx, args => (f); intrinsic);
1168 let f = f.load_scalar(fx);
1170 let res = crate::cast::clif_int_or_float_cast(
1174 fx.clif_type(ret.layout().ty).unwrap(),
1175 type_sign(ret.layout().ty),
1177 ret.write_cvalue(fx, CValue::by_val(res, ret.layout()));
1181 intrinsic_args!(fx, args => (lhs_ref, rhs_ref); intrinsic);
1182 let lhs_ref = lhs_ref.load_scalar(fx);
1183 let rhs_ref = rhs_ref.load_scalar(fx);
1185 let size = fx.layout_of(substs.type_at(0)).layout.size();
1186 // FIXME add and use emit_small_memcmp
1187 let is_eq_value = if size == Size::ZERO {
1188 // No bytes means they're trivially equal
1189 fx.bcx.ins().iconst(types::I8, 1)
1190 } else if let Some(clty) = size.bits().try_into().ok().and_then(Type::int) {
1191 // Can't use `trusted` for these loads; they could be unaligned.
1192 let mut flags = MemFlags::new();
1194 let lhs_val = fx.bcx.ins().load(clty, flags, lhs_ref, 0);
1195 let rhs_val = fx.bcx.ins().load(clty, flags, rhs_ref, 0);
1196 fx.bcx.ins().icmp(IntCC::Equal, lhs_val, rhs_val)
1198 // Just call `memcmp` (like slices do in core) when the
1199 // size is too large or it's not a power-of-two.
1200 let signed_bytes = i64::try_from(size.bytes()).unwrap();
1201 let bytes_val = fx.bcx.ins().iconst(fx.pointer_type, signed_bytes);
1202 let params = vec![AbiParam::new(fx.pointer_type); 3];
1203 let returns = vec![AbiParam::new(types::I32)];
1204 let args = &[lhs_ref, rhs_ref, bytes_val];
1205 let cmp = fx.lib_call("memcmp", params, returns, args)[0];
1206 fx.bcx.ins().icmp_imm(IntCC::Equal, cmp, 0)
1208 ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout()));
1211 sym::const_allocate => {
1212 intrinsic_args!(fx, args => (_size, _align); intrinsic);
1214 // returns a null pointer at runtime.
1215 let null = fx.bcx.ins().iconst(fx.pointer_type, 0);
1216 ret.write_cvalue(fx, CValue::by_val(null, ret.layout()));
1219 sym::const_deallocate => {
1220 intrinsic_args!(fx, args => (_ptr, _size, _align); intrinsic);
1225 intrinsic_args!(fx, args => (a); intrinsic);
1227 // FIXME implement black_box semantics
1228 ret.write_cvalue(fx, a);
1231 // FIXME implement variadics in cranelift
1232 sym::va_copy | sym::va_arg | sym::va_end => {
1233 fx.tcx.sess.span_fatal(
1235 "Defining variadic functions is not yet supported by Cranelift",
1242 .span_fatal(source_info.span, &format!("unsupported intrinsic {}", intrinsic));
1246 let ret_block = fx.get_block(destination.unwrap());
1247 fx.bcx.ins().jump(ret_block, &[]);