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 if intrinsic.as_str().starts_with("simd_") {
222 self::simd::codegen_simd_intrinsic_call(
228 target.expect("target for simd intrinsic"),
231 } else if codegen_float_intrinsic_call(fx, intrinsic, args, destination) {
232 let ret_block = fx.get_block(target.expect("target for float intrinsic"));
233 fx.bcx.ins().jump(ret_block, &[]);
235 codegen_regular_intrinsic_call(
248 fn codegen_float_intrinsic_call<'tcx>(
249 fx: &mut FunctionCx<'_, '_, 'tcx>,
251 args: &[mir::Operand<'tcx>],
254 let (name, arg_count, ty) = match intrinsic {
255 sym::expf32 => ("expf", 1, fx.tcx.types.f32),
256 sym::expf64 => ("exp", 1, fx.tcx.types.f64),
257 sym::exp2f32 => ("exp2f", 1, fx.tcx.types.f32),
258 sym::exp2f64 => ("exp2", 1, fx.tcx.types.f64),
259 sym::sqrtf32 => ("sqrtf", 1, fx.tcx.types.f32),
260 sym::sqrtf64 => ("sqrt", 1, fx.tcx.types.f64),
261 sym::powif32 => ("__powisf2", 2, fx.tcx.types.f32), // compiler-builtins
262 sym::powif64 => ("__powidf2", 2, fx.tcx.types.f64), // compiler-builtins
263 sym::powf32 => ("powf", 2, fx.tcx.types.f32),
264 sym::powf64 => ("pow", 2, fx.tcx.types.f64),
265 sym::logf32 => ("logf", 1, fx.tcx.types.f32),
266 sym::logf64 => ("log", 1, fx.tcx.types.f64),
267 sym::log2f32 => ("log2f", 1, fx.tcx.types.f32),
268 sym::log2f64 => ("log2", 1, fx.tcx.types.f64),
269 sym::log10f32 => ("log10f", 1, fx.tcx.types.f32),
270 sym::log10f64 => ("log10", 1, fx.tcx.types.f64),
271 sym::fabsf32 => ("fabsf", 1, fx.tcx.types.f32),
272 sym::fabsf64 => ("fabs", 1, fx.tcx.types.f64),
273 sym::fmaf32 => ("fmaf", 3, fx.tcx.types.f32),
274 sym::fmaf64 => ("fma", 3, fx.tcx.types.f64),
275 sym::copysignf32 => ("copysignf", 2, fx.tcx.types.f32),
276 sym::copysignf64 => ("copysign", 2, fx.tcx.types.f64),
277 sym::floorf32 => ("floorf", 1, fx.tcx.types.f32),
278 sym::floorf64 => ("floor", 1, fx.tcx.types.f64),
279 sym::ceilf32 => ("ceilf", 1, fx.tcx.types.f32),
280 sym::ceilf64 => ("ceil", 1, fx.tcx.types.f64),
281 sym::truncf32 => ("truncf", 1, fx.tcx.types.f32),
282 sym::truncf64 => ("trunc", 1, fx.tcx.types.f64),
283 sym::roundf32 => ("roundf", 1, fx.tcx.types.f32),
284 sym::roundf64 => ("round", 1, fx.tcx.types.f64),
285 sym::sinf32 => ("sinf", 1, fx.tcx.types.f32),
286 sym::sinf64 => ("sin", 1, fx.tcx.types.f64),
287 sym::cosf32 => ("cosf", 1, fx.tcx.types.f32),
288 sym::cosf64 => ("cos", 1, fx.tcx.types.f64),
292 if args.len() != arg_count {
293 bug!("wrong number of args for intrinsic {:?}", intrinsic);
297 let args = match args {
299 a = [codegen_operand(fx, x)];
303 b = [codegen_operand(fx, x), codegen_operand(fx, y)];
307 c = [codegen_operand(fx, x), codegen_operand(fx, y), codegen_operand(fx, z)];
313 let layout = fx.layout_of(ty);
314 let res = match intrinsic {
315 sym::fmaf32 | sym::fmaf64 => {
316 let a = args[0].load_scalar(fx);
317 let b = args[1].load_scalar(fx);
318 let c = args[2].load_scalar(fx);
319 CValue::by_val(fx.bcx.ins().fma(a, b, c), layout)
321 sym::copysignf32 | sym::copysignf64 => {
322 let a = args[0].load_scalar(fx);
323 let b = args[1].load_scalar(fx);
324 CValue::by_val(fx.bcx.ins().fcopysign(a, b), layout)
334 let a = args[0].load_scalar(fx);
336 let val = match intrinsic {
337 sym::fabsf32 | sym::fabsf64 => fx.bcx.ins().fabs(a),
338 sym::floorf32 | sym::floorf64 => fx.bcx.ins().floor(a),
339 sym::ceilf32 | sym::ceilf64 => fx.bcx.ins().ceil(a),
340 sym::truncf32 | sym::truncf64 => fx.bcx.ins().trunc(a),
344 CValue::by_val(val, layout)
346 // These intrinsics aren't supported natively by Cranelift.
347 // Lower them to a libcall.
348 _ => fx.easy_call(name, &args, ty),
351 ret.write_cvalue(fx, res);
356 fn codegen_regular_intrinsic_call<'tcx>(
357 fx: &mut FunctionCx<'_, '_, 'tcx>,
358 instance: Instance<'tcx>,
360 substs: SubstsRef<'tcx>,
361 args: &[mir::Operand<'tcx>],
363 destination: Option<BasicBlock>,
364 source_info: mir::SourceInfo,
366 let usize_layout = fx.layout_of(fx.tcx.types.usize);
370 fx.bcx.ins().trap(TrapCode::User(0));
373 sym::likely | sym::unlikely => {
374 intrinsic_args!(fx, args => (a); intrinsic);
376 ret.write_cvalue(fx, a);
379 intrinsic_args!(fx, args => (); intrinsic);
381 fx.bcx.ins().debugtrap();
383 sym::copy | sym::copy_nonoverlapping => {
384 intrinsic_args!(fx, args => (src, dst, count); intrinsic);
385 let src = src.load_scalar(fx);
386 let dst = dst.load_scalar(fx);
387 let count = count.load_scalar(fx);
389 let elem_ty = substs.type_at(0);
390 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
391 assert_eq!(args.len(), 3);
393 if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
395 if intrinsic == sym::copy_nonoverlapping {
396 // FIXME emit_small_memcpy
397 fx.bcx.call_memcpy(fx.target_config, dst, src, byte_amount);
399 // FIXME emit_small_memmove
400 fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount);
403 sym::volatile_copy_memory | sym::volatile_copy_nonoverlapping_memory => {
404 // NOTE: the volatile variants have src and dst swapped
405 intrinsic_args!(fx, args => (dst, src, count); intrinsic);
406 let dst = dst.load_scalar(fx);
407 let src = src.load_scalar(fx);
408 let count = count.load_scalar(fx);
410 let elem_ty = substs.type_at(0);
411 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
412 assert_eq!(args.len(), 3);
414 if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
416 // FIXME make the copy actually volatile when using emit_small_mem{cpy,move}
417 if intrinsic == sym::volatile_copy_nonoverlapping_memory {
418 // FIXME emit_small_memcpy
419 fx.bcx.call_memcpy(fx.target_config, dst, src, byte_amount);
421 // FIXME emit_small_memmove
422 fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount);
425 sym::size_of_val => {
426 intrinsic_args!(fx, args => (ptr); intrinsic);
428 let layout = fx.layout_of(substs.type_at(0));
429 // Note: Can't use is_unsized here as truly unsized types need to take the fixed size
431 let size = if let Abi::ScalarPair(_, _) = ptr.layout().abi {
432 let (_ptr, info) = ptr.load_scalar_pair(fx);
433 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
436 fx.bcx.ins().iconst(fx.pointer_type, layout.size.bytes() as i64)
438 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
440 sym::min_align_of_val => {
441 intrinsic_args!(fx, args => (ptr); intrinsic);
443 let layout = fx.layout_of(substs.type_at(0));
444 // Note: Can't use is_unsized here as truly unsized types need to take the fixed size
446 let align = if let Abi::ScalarPair(_, _) = ptr.layout().abi {
447 let (_ptr, info) = ptr.load_scalar_pair(fx);
448 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
451 fx.bcx.ins().iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
453 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
456 sym::vtable_size => {
457 intrinsic_args!(fx, args => (vtable); intrinsic);
458 let vtable = vtable.load_scalar(fx);
460 let size = crate::vtable::size_of_obj(fx, vtable);
461 ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
464 sym::vtable_align => {
465 intrinsic_args!(fx, args => (vtable); intrinsic);
466 let vtable = vtable.load_scalar(fx);
468 let align = crate::vtable::min_align_of_obj(fx, vtable);
469 ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
479 | sym::unchecked_shr => {
480 intrinsic_args!(fx, args => (x, y); intrinsic);
482 // FIXME trap on overflow
483 let bin_op = match intrinsic {
484 sym::unchecked_add => BinOp::Add,
485 sym::unchecked_sub => BinOp::Sub,
486 sym::unchecked_mul => BinOp::Mul,
487 sym::unchecked_div | sym::exact_div => BinOp::Div,
488 sym::unchecked_rem => BinOp::Rem,
489 sym::unchecked_shl => BinOp::Shl,
490 sym::unchecked_shr => BinOp::Shr,
493 let res = crate::num::codegen_int_binop(fx, bin_op, x, y);
494 ret.write_cvalue(fx, res);
496 sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
497 intrinsic_args!(fx, args => (x, y); intrinsic);
499 assert_eq!(x.layout().ty, y.layout().ty);
500 let bin_op = match intrinsic {
501 sym::add_with_overflow => BinOp::Add,
502 sym::sub_with_overflow => BinOp::Sub,
503 sym::mul_with_overflow => BinOp::Mul,
507 let res = crate::num::codegen_checked_int_binop(fx, bin_op, x, y);
508 ret.write_cvalue(fx, res);
510 sym::saturating_add | sym::saturating_sub => {
511 intrinsic_args!(fx, args => (lhs, rhs); intrinsic);
513 assert_eq!(lhs.layout().ty, rhs.layout().ty);
514 let bin_op = match intrinsic {
515 sym::saturating_add => BinOp::Add,
516 sym::saturating_sub => BinOp::Sub,
520 let res = crate::num::codegen_saturating_int_binop(fx, bin_op, lhs, rhs);
521 ret.write_cvalue(fx, res);
523 sym::rotate_left => {
524 intrinsic_args!(fx, args => (x, y); intrinsic);
525 let y = y.load_scalar(fx);
527 let layout = x.layout();
528 let x = x.load_scalar(fx);
529 let res = fx.bcx.ins().rotl(x, y);
530 ret.write_cvalue(fx, CValue::by_val(res, layout));
532 sym::rotate_right => {
533 intrinsic_args!(fx, args => (x, y); intrinsic);
534 let y = y.load_scalar(fx);
536 let layout = x.layout();
537 let x = x.load_scalar(fx);
538 let res = fx.bcx.ins().rotr(x, y);
539 ret.write_cvalue(fx, CValue::by_val(res, layout));
542 // The only difference between offset and arith_offset is regarding UB. Because Cranelift
543 // doesn't have UB both are codegen'ed the same way
544 sym::offset | sym::arith_offset => {
545 intrinsic_args!(fx, args => (base, offset); intrinsic);
546 let offset = offset.load_scalar(fx);
548 let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
549 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
550 let ptr_diff = if pointee_size != 1 {
551 fx.bcx.ins().imul_imm(offset, pointee_size as i64)
555 let base_val = base.load_scalar(fx);
556 let res = fx.bcx.ins().iadd(base_val, ptr_diff);
557 ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
561 intrinsic_args!(fx, args => (ptr, mask); intrinsic);
562 let ptr = ptr.load_scalar(fx);
563 let mask = mask.load_scalar(fx);
564 fx.bcx.ins().band(ptr, mask);
568 intrinsic_args!(fx, args => (from); intrinsic);
570 if ret.layout().abi.is_uninhabited() {
571 crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", source_info);
575 ret.write_cvalue_transmute(fx, from);
577 sym::write_bytes | sym::volatile_set_memory => {
578 intrinsic_args!(fx, args => (dst, val, count); intrinsic);
579 let val = val.load_scalar(fx);
580 let count = count.load_scalar(fx);
582 let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
583 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
584 let count = if pointee_size != 1 {
585 fx.bcx.ins().imul_imm(count, pointee_size as i64)
589 let dst_ptr = dst.load_scalar(fx);
590 // FIXME make the memset actually volatile when switching to emit_small_memset
591 // FIXME use emit_small_memset
592 fx.bcx.call_memset(fx.target_config, dst_ptr, val, count);
594 sym::ctlz | sym::ctlz_nonzero => {
595 intrinsic_args!(fx, args => (arg); intrinsic);
596 let val = arg.load_scalar(fx);
598 // FIXME trap on `ctlz_nonzero` with zero arg.
599 let res = fx.bcx.ins().clz(val);
600 let res = CValue::by_val(res, arg.layout());
601 ret.write_cvalue(fx, res);
603 sym::cttz | sym::cttz_nonzero => {
604 intrinsic_args!(fx, args => (arg); intrinsic);
605 let val = arg.load_scalar(fx);
607 // FIXME trap on `cttz_nonzero` with zero arg.
608 let res = fx.bcx.ins().ctz(val);
609 let res = CValue::by_val(res, arg.layout());
610 ret.write_cvalue(fx, res);
613 intrinsic_args!(fx, args => (arg); intrinsic);
614 let val = arg.load_scalar(fx);
616 let res = fx.bcx.ins().popcnt(val);
617 let res = CValue::by_val(res, arg.layout());
618 ret.write_cvalue(fx, res);
621 intrinsic_args!(fx, args => (arg); intrinsic);
622 let val = arg.load_scalar(fx);
624 let res = fx.bcx.ins().bitrev(val);
625 let res = CValue::by_val(res, arg.layout());
626 ret.write_cvalue(fx, res);
629 intrinsic_args!(fx, args => (arg); intrinsic);
630 let val = arg.load_scalar(fx);
632 let res = if fx.bcx.func.dfg.value_type(val) == types::I8 {
635 fx.bcx.ins().bswap(val)
637 let res = CValue::by_val(res, arg.layout());
638 ret.write_cvalue(fx, res);
640 sym::assert_inhabited | sym::assert_zero_valid | sym::assert_mem_uninitialized_valid => {
641 intrinsic_args!(fx, args => (); intrinsic);
643 let layout = fx.layout_of(substs.type_at(0));
644 if layout.abi.is_uninhabited() {
645 with_no_trimmed_paths!({
646 crate::base::codegen_panic_nounwind(
648 &format!("attempted to instantiate uninhabited type `{}`", layout.ty),
655 if intrinsic == sym::assert_zero_valid
656 && !fx.tcx.permits_zero_init(fx.param_env().and(layout))
658 with_no_trimmed_paths!({
659 crate::base::codegen_panic_nounwind(
662 "attempted to zero-initialize type `{}`, which is invalid",
671 if intrinsic == sym::assert_mem_uninitialized_valid
672 && !fx.tcx.permits_uninit_init(fx.param_env().and(layout))
674 with_no_trimmed_paths!({
675 crate::base::codegen_panic_nounwind(
678 "attempted to leave type `{}` uninitialized, which is invalid",
688 sym::volatile_load | sym::unaligned_volatile_load => {
689 intrinsic_args!(fx, args => (ptr); intrinsic);
691 // Cranelift treats loads as volatile by default
692 // FIXME correctly handle unaligned_volatile_load
693 let inner_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
694 let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
695 ret.write_cvalue(fx, val);
697 sym::volatile_store | sym::unaligned_volatile_store => {
698 intrinsic_args!(fx, args => (ptr, val); intrinsic);
699 let ptr = ptr.load_scalar(fx);
701 // Cranelift treats stores as volatile by default
702 // FIXME correctly handle unaligned_volatile_store
703 let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
704 dest.write_cvalue(fx, val);
711 | sym::variant_count => {
712 intrinsic_args!(fx, args => (); intrinsic);
715 fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
716 let val = crate::constant::codegen_const_value(fx, const_val, ret.layout().ty);
717 ret.write_cvalue(fx, val);
720 sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
721 intrinsic_args!(fx, args => (ptr, base); intrinsic);
722 let ptr = ptr.load_scalar(fx);
723 let base = base.load_scalar(fx);
724 let ty = substs.type_at(0);
726 let pointee_size: u64 = fx.layout_of(ty).size.bytes();
727 let diff_bytes = fx.bcx.ins().isub(ptr, base);
728 // FIXME this can be an exact division.
729 let val = if intrinsic == sym::ptr_offset_from_unsigned {
730 let usize_layout = fx.layout_of(fx.tcx.types.usize);
731 // Because diff_bytes ULE isize::MAX, this would be fine as signed,
732 // but unsigned is slightly easier to codegen, so might as well.
733 CValue::by_val(fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64), usize_layout)
735 let isize_layout = fx.layout_of(fx.tcx.types.isize);
736 CValue::by_val(fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64), isize_layout)
738 ret.write_cvalue(fx, val);
741 sym::ptr_guaranteed_cmp => {
742 intrinsic_args!(fx, args => (a, b); intrinsic);
744 let val = crate::num::codegen_ptr_binop(fx, BinOp::Eq, a, b).load_scalar(fx);
745 ret.write_cvalue(fx, CValue::by_val(val, fx.layout_of(fx.tcx.types.u8)));
748 sym::caller_location => {
749 intrinsic_args!(fx, args => (); intrinsic);
751 let caller_location = fx.get_caller_location(source_info);
752 ret.write_cvalue(fx, caller_location);
755 _ if intrinsic.as_str().starts_with("atomic_fence") => {
756 intrinsic_args!(fx, args => (); intrinsic);
758 fx.bcx.ins().fence();
760 _ if intrinsic.as_str().starts_with("atomic_singlethreadfence") => {
761 intrinsic_args!(fx, args => (); intrinsic);
763 // FIXME use a compiler fence once Cranelift supports it
764 fx.bcx.ins().fence();
766 _ if intrinsic.as_str().starts_with("atomic_load") => {
767 intrinsic_args!(fx, args => (ptr); intrinsic);
768 let ptr = ptr.load_scalar(fx);
770 let ty = substs.type_at(0);
772 ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
773 // FIXME implement 128bit atomics
774 if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
775 // special case for compiler-builtins to avoid having to patch it
776 crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
781 .span_fatal(source_info.span, "128bit atomics not yet supported");
784 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
786 report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
790 let clif_ty = fx.clif_type(ty).unwrap();
792 let val = fx.bcx.ins().atomic_load(clif_ty, MemFlags::trusted(), ptr);
794 let val = CValue::by_val(val, fx.layout_of(ty));
795 ret.write_cvalue(fx, val);
797 _ if intrinsic.as_str().starts_with("atomic_store") => {
798 intrinsic_args!(fx, args => (ptr, val); intrinsic);
799 let ptr = ptr.load_scalar(fx);
801 let ty = substs.type_at(0);
803 ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
804 // FIXME implement 128bit atomics
805 if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
806 // special case for compiler-builtins to avoid having to patch it
807 crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
812 .span_fatal(source_info.span, "128bit atomics not yet supported");
815 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
817 report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
822 let val = val.load_scalar(fx);
824 fx.bcx.ins().atomic_store(MemFlags::trusted(), val, ptr);
826 _ if intrinsic.as_str().starts_with("atomic_xchg") => {
827 intrinsic_args!(fx, args => (ptr, new); intrinsic);
828 let ptr = ptr.load_scalar(fx);
830 let layout = new.layout();
831 match layout.ty.kind() {
832 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
834 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
838 let ty = fx.clif_type(layout.ty).unwrap();
840 let new = new.load_scalar(fx);
842 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xchg, ptr, new);
844 let old = CValue::by_val(old, layout);
845 ret.write_cvalue(fx, old);
847 _ if intrinsic.as_str().starts_with("atomic_cxchg") => {
848 // both atomic_cxchg_* and atomic_cxchgweak_*
849 intrinsic_args!(fx, args => (ptr, test_old, new); intrinsic);
850 let ptr = ptr.load_scalar(fx);
852 let layout = new.layout();
853 match layout.ty.kind() {
854 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
856 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
861 let test_old = test_old.load_scalar(fx);
862 let new = new.load_scalar(fx);
864 let old = fx.bcx.ins().atomic_cas(MemFlags::trusted(), ptr, test_old, new);
865 let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
867 let ret_val = CValue::by_val_pair(old, is_eq, ret.layout());
868 ret.write_cvalue(fx, ret_val)
871 _ if intrinsic.as_str().starts_with("atomic_xadd") => {
872 intrinsic_args!(fx, args => (ptr, amount); intrinsic);
873 let ptr = ptr.load_scalar(fx);
875 let layout = amount.layout();
876 match layout.ty.kind() {
877 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
879 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
883 let ty = fx.clif_type(layout.ty).unwrap();
885 let amount = amount.load_scalar(fx);
888 fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Add, ptr, amount);
890 let old = CValue::by_val(old, layout);
891 ret.write_cvalue(fx, old);
893 _ if intrinsic.as_str().starts_with("atomic_xsub") => {
894 intrinsic_args!(fx, args => (ptr, amount); intrinsic);
895 let ptr = ptr.load_scalar(fx);
897 let layout = amount.layout();
898 match layout.ty.kind() {
899 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
901 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
905 let ty = fx.clif_type(layout.ty).unwrap();
907 let amount = amount.load_scalar(fx);
910 fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Sub, ptr, amount);
912 let old = CValue::by_val(old, layout);
913 ret.write_cvalue(fx, old);
915 _ if intrinsic.as_str().starts_with("atomic_and") => {
916 intrinsic_args!(fx, args => (ptr, src); intrinsic);
917 let ptr = ptr.load_scalar(fx);
919 let layout = src.layout();
920 match layout.ty.kind() {
921 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
923 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
927 let ty = fx.clif_type(layout.ty).unwrap();
929 let src = src.load_scalar(fx);
931 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::And, ptr, src);
933 let old = CValue::by_val(old, layout);
934 ret.write_cvalue(fx, old);
936 _ if intrinsic.as_str().starts_with("atomic_or") => {
937 intrinsic_args!(fx, args => (ptr, src); intrinsic);
938 let ptr = ptr.load_scalar(fx);
940 let layout = src.layout();
941 match layout.ty.kind() {
942 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
944 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
948 let ty = fx.clif_type(layout.ty).unwrap();
950 let src = src.load_scalar(fx);
952 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Or, ptr, src);
954 let old = CValue::by_val(old, layout);
955 ret.write_cvalue(fx, old);
957 _ if intrinsic.as_str().starts_with("atomic_xor") => {
958 intrinsic_args!(fx, args => (ptr, src); intrinsic);
959 let ptr = ptr.load_scalar(fx);
961 let layout = src.layout();
962 match layout.ty.kind() {
963 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
965 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
969 let ty = fx.clif_type(layout.ty).unwrap();
971 let src = src.load_scalar(fx);
973 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xor, ptr, src);
975 let old = CValue::by_val(old, layout);
976 ret.write_cvalue(fx, old);
978 _ if intrinsic.as_str().starts_with("atomic_nand") => {
979 intrinsic_args!(fx, args => (ptr, src); intrinsic);
980 let ptr = ptr.load_scalar(fx);
982 let layout = src.layout();
983 match layout.ty.kind() {
984 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
986 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
990 let ty = fx.clif_type(layout.ty).unwrap();
992 let src = src.load_scalar(fx);
994 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Nand, ptr, src);
996 let old = CValue::by_val(old, layout);
997 ret.write_cvalue(fx, old);
999 _ if intrinsic.as_str().starts_with("atomic_max") => {
1000 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1001 let ptr = ptr.load_scalar(fx);
1003 let layout = src.layout();
1004 match layout.ty.kind() {
1005 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1007 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1011 let ty = fx.clif_type(layout.ty).unwrap();
1013 let src = src.load_scalar(fx);
1015 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smax, ptr, src);
1017 let old = CValue::by_val(old, layout);
1018 ret.write_cvalue(fx, old);
1020 _ if intrinsic.as_str().starts_with("atomic_umax") => {
1021 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1022 let ptr = ptr.load_scalar(fx);
1024 let layout = src.layout();
1025 match layout.ty.kind() {
1026 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1028 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1032 let ty = fx.clif_type(layout.ty).unwrap();
1034 let src = src.load_scalar(fx);
1036 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umax, ptr, src);
1038 let old = CValue::by_val(old, layout);
1039 ret.write_cvalue(fx, old);
1041 _ if intrinsic.as_str().starts_with("atomic_min") => {
1042 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1043 let ptr = ptr.load_scalar(fx);
1045 let layout = src.layout();
1046 match layout.ty.kind() {
1047 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1049 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1053 let ty = fx.clif_type(layout.ty).unwrap();
1055 let src = src.load_scalar(fx);
1057 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smin, ptr, src);
1059 let old = CValue::by_val(old, layout);
1060 ret.write_cvalue(fx, old);
1062 _ if intrinsic.as_str().starts_with("atomic_umin") => {
1063 intrinsic_args!(fx, args => (ptr, src); intrinsic);
1064 let ptr = ptr.load_scalar(fx);
1066 let layout = src.layout();
1067 match layout.ty.kind() {
1068 ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1070 report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1074 let ty = fx.clif_type(layout.ty).unwrap();
1076 let src = src.load_scalar(fx);
1078 let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umin, ptr, src);
1080 let old = CValue::by_val(old, layout);
1081 ret.write_cvalue(fx, old);
1085 intrinsic_args!(fx, args => (a, b); intrinsic);
1086 let a = a.load_scalar(fx);
1087 let b = b.load_scalar(fx);
1089 let val = crate::num::codegen_float_min(fx, a, b);
1090 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1091 ret.write_cvalue(fx, val);
1094 intrinsic_args!(fx, args => (a, b); intrinsic);
1095 let a = a.load_scalar(fx);
1096 let b = b.load_scalar(fx);
1098 let val = crate::num::codegen_float_min(fx, a, b);
1099 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1100 ret.write_cvalue(fx, val);
1103 intrinsic_args!(fx, args => (a, b); intrinsic);
1104 let a = a.load_scalar(fx);
1105 let b = b.load_scalar(fx);
1107 let val = crate::num::codegen_float_max(fx, a, b);
1108 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
1109 ret.write_cvalue(fx, val);
1112 intrinsic_args!(fx, args => (a, b); intrinsic);
1113 let a = a.load_scalar(fx);
1114 let b = b.load_scalar(fx);
1116 let val = crate::num::codegen_float_max(fx, a, b);
1117 let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
1118 ret.write_cvalue(fx, val);
1122 intrinsic_args!(fx, args => (f, data, catch_fn); intrinsic);
1123 let f = f.load_scalar(fx);
1124 let data = data.load_scalar(fx);
1125 let _catch_fn = catch_fn.load_scalar(fx);
1127 // FIXME once unwinding is supported, change this to actually catch panics
1128 let f_sig = fx.bcx.func.import_signature(Signature {
1129 call_conv: fx.target_config.default_call_conv,
1130 params: vec![AbiParam::new(pointer_ty(fx.tcx))],
1134 fx.bcx.ins().call_indirect(f_sig, f, &[data]);
1136 let layout = ret.layout();
1137 let ret_val = CValue::const_val(fx, layout, ty::ScalarInt::null(layout.size));
1138 ret.write_cvalue(fx, ret_val);
1141 sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
1142 intrinsic_args!(fx, args => (x, y); intrinsic);
1144 let res = crate::num::codegen_float_binop(
1147 sym::fadd_fast => BinOp::Add,
1148 sym::fsub_fast => BinOp::Sub,
1149 sym::fmul_fast => BinOp::Mul,
1150 sym::fdiv_fast => BinOp::Div,
1151 sym::frem_fast => BinOp::Rem,
1152 _ => unreachable!(),
1157 ret.write_cvalue(fx, res);
1159 sym::float_to_int_unchecked => {
1160 intrinsic_args!(fx, args => (f); intrinsic);
1161 let f = f.load_scalar(fx);
1163 let res = crate::cast::clif_int_or_float_cast(
1167 fx.clif_type(ret.layout().ty).unwrap(),
1168 type_sign(ret.layout().ty),
1170 ret.write_cvalue(fx, CValue::by_val(res, ret.layout()));
1174 intrinsic_args!(fx, args => (lhs_ref, rhs_ref); intrinsic);
1175 let lhs_ref = lhs_ref.load_scalar(fx);
1176 let rhs_ref = rhs_ref.load_scalar(fx);
1178 let size = fx.layout_of(substs.type_at(0)).layout.size();
1179 // FIXME add and use emit_small_memcmp
1180 let is_eq_value = if size == Size::ZERO {
1181 // No bytes means they're trivially equal
1182 fx.bcx.ins().iconst(types::I8, 1)
1183 } else if let Some(clty) = size.bits().try_into().ok().and_then(Type::int) {
1184 // Can't use `trusted` for these loads; they could be unaligned.
1185 let mut flags = MemFlags::new();
1187 let lhs_val = fx.bcx.ins().load(clty, flags, lhs_ref, 0);
1188 let rhs_val = fx.bcx.ins().load(clty, flags, rhs_ref, 0);
1189 fx.bcx.ins().icmp(IntCC::Equal, lhs_val, rhs_val)
1191 // Just call `memcmp` (like slices do in core) when the
1192 // size is too large or it's not a power-of-two.
1193 let signed_bytes = i64::try_from(size.bytes()).unwrap();
1194 let bytes_val = fx.bcx.ins().iconst(fx.pointer_type, signed_bytes);
1195 let params = vec![AbiParam::new(fx.pointer_type); 3];
1196 let returns = vec![AbiParam::new(types::I32)];
1197 let args = &[lhs_ref, rhs_ref, bytes_val];
1198 let cmp = fx.lib_call("memcmp", params, returns, args)[0];
1199 fx.bcx.ins().icmp_imm(IntCC::Equal, cmp, 0)
1201 ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout()));
1204 sym::const_allocate => {
1205 intrinsic_args!(fx, args => (_size, _align); intrinsic);
1207 // returns a null pointer at runtime.
1208 let null = fx.bcx.ins().iconst(fx.pointer_type, 0);
1209 ret.write_cvalue(fx, CValue::by_val(null, ret.layout()));
1212 sym::const_deallocate => {
1213 intrinsic_args!(fx, args => (_ptr, _size, _align); intrinsic);
1218 intrinsic_args!(fx, args => (a); intrinsic);
1220 // FIXME implement black_box semantics
1221 ret.write_cvalue(fx, a);
1224 // FIXME implement variadics in cranelift
1225 sym::va_copy | sym::va_arg | sym::va_end => {
1226 fx.tcx.sess.span_fatal(
1228 "Defining variadic functions is not yet supported by Cranelift",
1235 .span_fatal(source_info.span, &format!("unsupported intrinsic {}", intrinsic));
1239 let ret_block = fx.get_block(destination.unwrap());
1240 fx.bcx.ins().jump(ret_block, &[]);