5 use rustc_apfloat::{Float, Round};
6 use rustc_middle::ty::layout::{HasParamEnv, IntegerExt, LayoutOf};
7 use rustc_middle::{mir, mir::BinOp, ty, ty::FloatTy};
8 use rustc_target::abi::{Align, Integer};
11 use helpers::{bool_to_simd_element, check_arg_count, simd_element_to_bool};
14 MirOp(mir::BinOp, bool),
19 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
20 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
23 instance: ty::Instance<'tcx>,
24 args: &[OpTy<'tcx, Tag>],
25 ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
26 _unwind: StackPopUnwind,
27 ) -> InterpResult<'tcx> {
28 let this = self.eval_context_mut();
30 if this.emulate_intrinsic(instance, args, ret)? {
34 // All supported intrinsics have a return place.
35 let intrinsic_name = this.tcx.item_name(instance.def_id());
36 let intrinsic_name = intrinsic_name.as_str();
37 let (dest, ret) = match ret {
38 None => throw_unsup_format!("unimplemented (diverging) intrinsic: {}", intrinsic_name),
42 // Then handle terminating intrinsics.
43 match intrinsic_name {
44 // Miri overwriting CTFE intrinsics.
45 "ptr_guaranteed_eq" => {
46 let &[ref left, ref right] = check_arg_count(args)?;
47 let left = this.read_immediate(left)?;
48 let right = this.read_immediate(right)?;
49 this.binop_ignore_overflow(mir::BinOp::Eq, &left, &right, dest)?;
51 "ptr_guaranteed_ne" => {
52 let &[ref left, ref right] = check_arg_count(args)?;
53 let left = this.read_immediate(left)?;
54 let right = this.read_immediate(right)?;
55 this.binop_ignore_overflow(mir::BinOp::Ne, &left, &right, dest)?;
58 // For now, for compatibility with the run-time implementation of this, we just return null.
59 // See <https://github.com/rust-lang/rust/issues/93935>.
60 this.write_null(dest)?;
62 "const_deallocate" => {
66 // Raw memory accesses
68 let &[ref place] = check_arg_count(args)?;
69 let place = this.deref_operand(place)?;
70 this.copy_op(&place.into(), dest)?;
73 let &[ref place, ref dest] = check_arg_count(args)?;
74 let place = this.deref_operand(place)?;
75 this.copy_op(dest, &place.into())?;
78 "write_bytes" | "volatile_set_memory" => {
79 let &[ref ptr, ref val_byte, ref count] = check_arg_count(args)?;
80 let ty = instance.substs.type_at(0);
81 let ty_layout = this.layout_of(ty)?;
82 let val_byte = this.read_scalar(val_byte)?.to_u8()?;
83 let ptr = this.read_pointer(ptr)?;
84 let count = this.read_scalar(count)?.to_machine_usize(this)?;
85 let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
86 err_ub_format!("overflow computing total size of `{}`", intrinsic_name)
89 .write_bytes(ptr, iter::repeat(val_byte).take(byte_count.bytes() as usize))?;
92 // Floating-point operations
94 let &[ref f] = check_arg_count(args)?;
95 let f = this.read_scalar(f)?.to_f32()?;
96 // Can be implemented in soft-floats.
97 this.write_scalar(Scalar::from_f32(f.abs()), dest)?;
100 let &[ref f] = check_arg_count(args)?;
101 let f = this.read_scalar(f)?.to_f64()?;
102 // Can be implemented in soft-floats.
103 this.write_scalar(Scalar::from_f64(f.abs()), dest)?;
119 let &[ref f] = check_arg_count(args)?;
120 // FIXME: Using host floats.
121 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
122 let f = match intrinsic_name {
125 "sqrtf32" => f.sqrt(),
127 "exp2f32" => f.exp2(),
129 "log10f32" => f.log10(),
130 "log2f32" => f.log2(),
131 "floorf32" => f.floor(),
132 "ceilf32" => f.ceil(),
133 "truncf32" => f.trunc(),
134 "roundf32" => f.round(),
137 this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
154 let &[ref f] = check_arg_count(args)?;
155 // FIXME: Using host floats.
156 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
157 let f = match intrinsic_name {
160 "sqrtf64" => f.sqrt(),
162 "exp2f64" => f.exp2(),
164 "log10f64" => f.log10(),
165 "log2f64" => f.log2(),
166 "floorf64" => f.floor(),
167 "ceilf64" => f.ceil(),
168 "truncf64" => f.trunc(),
169 "roundf64" => f.round(),
172 this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
182 let &[ref a, ref b] = check_arg_count(args)?;
183 let a = this.read_immediate(a)?;
184 let b = this.read_immediate(b)?;
185 let op = match intrinsic_name {
186 "fadd_fast" => mir::BinOp::Add,
187 "fsub_fast" => mir::BinOp::Sub,
188 "fmul_fast" => mir::BinOp::Mul,
189 "fdiv_fast" => mir::BinOp::Div,
190 "frem_fast" => mir::BinOp::Rem,
193 let float_finite = |x: ImmTy<'tcx, _>| -> InterpResult<'tcx, bool> {
194 Ok(match x.layout.ty.kind() {
195 ty::Float(FloatTy::F32) => x.to_scalar()?.to_f32()?.is_finite(),
196 ty::Float(FloatTy::F64) => x.to_scalar()?.to_f64()?.is_finite(),
198 "`{}` called with non-float input type {:?}",
204 match (float_finite(a)?, float_finite(b)?) {
205 (false, false) => throw_ub_format!(
206 "`{}` intrinsic called with non-finite value as both parameters",
209 (false, _) => throw_ub_format!(
210 "`{}` intrinsic called with non-finite value as first parameter",
213 (_, false) => throw_ub_format!(
214 "`{}` intrinsic called with non-finite value as second parameter",
219 this.binop_ignore_overflow(op, &a, &b, dest)?;
227 let &[ref a, ref b] = check_arg_count(args)?;
228 let a = this.read_scalar(a)?.to_f32()?;
229 let b = this.read_scalar(b)?.to_f32()?;
230 let res = match intrinsic_name {
231 "minnumf32" => a.min(b),
232 "maxnumf32" => a.max(b),
233 "copysignf32" => a.copy_sign(b),
236 this.write_scalar(Scalar::from_f32(res), dest)?;
244 let &[ref a, ref b] = check_arg_count(args)?;
245 let a = this.read_scalar(a)?.to_f64()?;
246 let b = this.read_scalar(b)?.to_f64()?;
247 let res = match intrinsic_name {
248 "minnumf64" => a.min(b),
249 "maxnumf64" => a.max(b),
250 "copysignf64" => a.copy_sign(b),
253 this.write_scalar(Scalar::from_f64(res), dest)?;
257 let &[ref f, ref f2] = check_arg_count(args)?;
258 // FIXME: Using host floats.
259 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
260 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
261 this.write_scalar(Scalar::from_u32(f.powf(f2).to_bits()), dest)?;
265 let &[ref f, ref f2] = check_arg_count(args)?;
266 // FIXME: Using host floats.
267 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
268 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
269 this.write_scalar(Scalar::from_u64(f.powf(f2).to_bits()), dest)?;
273 let &[ref a, ref b, ref c] = check_arg_count(args)?;
274 let a = this.read_scalar(a)?.to_f32()?;
275 let b = this.read_scalar(b)?.to_f32()?;
276 let c = this.read_scalar(c)?.to_f32()?;
277 let res = a.mul_add(b, c).value;
278 this.write_scalar(Scalar::from_f32(res), dest)?;
282 let &[ref a, ref b, ref c] = check_arg_count(args)?;
283 let a = this.read_scalar(a)?.to_f64()?;
284 let b = this.read_scalar(b)?.to_f64()?;
285 let c = this.read_scalar(c)?.to_f64()?;
286 let res = a.mul_add(b, c).value;
287 this.write_scalar(Scalar::from_f64(res), dest)?;
291 let &[ref f, ref i] = check_arg_count(args)?;
292 // FIXME: Using host floats.
293 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
294 let i = this.read_scalar(i)?.to_i32()?;
295 this.write_scalar(Scalar::from_u32(f.powi(i).to_bits()), dest)?;
299 let &[ref f, ref i] = check_arg_count(args)?;
300 // FIXME: Using host floats.
301 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
302 let i = this.read_scalar(i)?.to_i32()?;
303 this.write_scalar(Scalar::from_u64(f.powi(i).to_bits()), dest)?;
306 "float_to_int_unchecked" => {
307 let &[ref val] = check_arg_count(args)?;
308 let val = this.read_immediate(val)?;
310 let res = match val.layout.ty.kind() {
311 ty::Float(FloatTy::F32) =>
312 this.float_to_int_unchecked(val.to_scalar()?.to_f32()?, dest.layout.ty)?,
313 ty::Float(FloatTy::F64) =>
314 this.float_to_int_unchecked(val.to_scalar()?.to_f64()?, dest.layout.ty)?,
317 "`float_to_int_unchecked` called with non-float input type {:?}",
322 this.write_scalar(res, dest)?;
333 let &[ref op] = check_arg_count(args)?;
334 let (op, op_len) = this.operand_to_simd(op)?;
335 let (dest, dest_len) = this.place_to_simd(dest)?;
337 assert_eq!(dest_len, op_len);
339 #[derive(Copy, Clone)]
346 #[derive(Copy, Clone)]
352 let which = match intrinsic_name {
353 "simd_neg" => Op::MirOp(mir::UnOp::Neg),
354 "simd_fabs" => Op::Abs,
355 "simd_ceil" => Op::HostOp(HostFloatOp::Ceil),
356 "simd_floor" => Op::HostOp(HostFloatOp::Floor),
357 "simd_round" => Op::HostOp(HostFloatOp::Round),
358 "simd_trunc" => Op::HostOp(HostFloatOp::Trunc),
362 for i in 0..dest_len {
363 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
364 let dest = this.mplace_index(&dest, i)?;
365 let val = match which {
366 Op::MirOp(mir_op) => this.unary_op(mir_op, &op)?.to_scalar()?,
368 // Works for f32 and f64.
369 let ty::Float(float_ty) = op.layout.ty.kind() else {
370 bug!("{} operand is not a float", intrinsic_name)
372 let op = op.to_scalar()?;
374 FloatTy::F32 => Scalar::from_f32(op.to_f32()?.abs()),
375 FloatTy::F64 => Scalar::from_f64(op.to_f64()?.abs()),
378 Op::HostOp(host_op) => {
379 let ty::Float(float_ty) = op.layout.ty.kind() else {
380 bug!("{} operand is not a float", intrinsic_name)
382 // FIXME using host floats
385 let f = f32::from_bits(op.to_scalar()?.to_u32()?);
386 let res = match host_op {
387 HostFloatOp::Ceil => f.ceil(),
388 HostFloatOp::Floor => f.floor(),
389 HostFloatOp::Round => f.round(),
390 HostFloatOp::Trunc => f.trunc(),
392 Scalar::from_u32(res.to_bits())
395 let f = f64::from_bits(op.to_scalar()?.to_u64()?);
396 let res = match host_op {
397 HostFloatOp::Ceil => f.ceil(),
398 HostFloatOp::Floor => f.floor(),
399 HostFloatOp::Round => f.round(),
400 HostFloatOp::Trunc => f.trunc(),
402 Scalar::from_u64(res.to_bits())
408 this.write_scalar(val, &dest.into())?;
430 | "simd_saturating_add"
431 | "simd_saturating_sub" => {
434 let &[ref left, ref right] = check_arg_count(args)?;
435 let (left, left_len) = this.operand_to_simd(left)?;
436 let (right, right_len) = this.operand_to_simd(right)?;
437 let (dest, dest_len) = this.place_to_simd(dest)?;
439 assert_eq!(dest_len, left_len);
440 assert_eq!(dest_len, right_len);
448 let which = match intrinsic_name {
449 "simd_add" => Op::MirOp(BinOp::Add),
450 "simd_sub" => Op::MirOp(BinOp::Sub),
451 "simd_mul" => Op::MirOp(BinOp::Mul),
452 "simd_div" => Op::MirOp(BinOp::Div),
453 "simd_rem" => Op::MirOp(BinOp::Rem),
454 "simd_shl" => Op::MirOp(BinOp::Shl),
455 "simd_shr" => Op::MirOp(BinOp::Shr),
456 "simd_and" => Op::MirOp(BinOp::BitAnd),
457 "simd_or" => Op::MirOp(BinOp::BitOr),
458 "simd_xor" => Op::MirOp(BinOp::BitXor),
459 "simd_eq" => Op::MirOp(BinOp::Eq),
460 "simd_ne" => Op::MirOp(BinOp::Ne),
461 "simd_lt" => Op::MirOp(BinOp::Lt),
462 "simd_le" => Op::MirOp(BinOp::Le),
463 "simd_gt" => Op::MirOp(BinOp::Gt),
464 "simd_ge" => Op::MirOp(BinOp::Ge),
465 "simd_fmax" => Op::FMax,
466 "simd_fmin" => Op::FMin,
467 "simd_saturating_add" => Op::SaturatingOp(BinOp::Add),
468 "simd_saturating_sub" => Op::SaturatingOp(BinOp::Sub),
472 for i in 0..dest_len {
473 let left = this.read_immediate(&this.mplace_index(&left, i)?.into())?;
474 let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
475 let dest = this.mplace_index(&dest, i)?;
476 let val = match which {
477 Op::MirOp(mir_op) => {
478 let (val, overflowed, ty) = this.overflowing_binary_op(mir_op, &left, &right)?;
479 if matches!(mir_op, BinOp::Shl | BinOp::Shr) {
480 // Shifts have extra UB as SIMD operations that the MIR binop does not have.
481 // See <https://github.com/rust-lang/rust/issues/91237>.
483 let r_val = right.to_scalar()?.to_bits(right.layout.size)?;
484 throw_ub_format!("overflowing shift by {} in `{}` in SIMD lane {}", r_val, intrinsic_name, i);
487 if matches!(mir_op, BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge) {
488 // Special handling for boolean-returning operations
489 assert_eq!(ty, this.tcx.types.bool);
490 let val = val.to_bool().unwrap();
491 bool_to_simd_element(val, dest.layout.size)
493 assert_ne!(ty, this.tcx.types.bool);
494 assert_eq!(ty, dest.layout.ty);
499 fmax_op(&left, &right)?
502 fmin_op(&left, &right)?
504 Op::SaturatingOp(mir_op) => {
505 this.saturating_arith(mir_op, &left, &right)?
508 this.write_scalar(val, &dest.into())?;
518 | "simd_reduce_min" => {
521 let &[ref op] = check_arg_count(args)?;
522 let (op, op_len) = this.operand_to_simd(op)?;
525 |b| ImmTy::from_scalar(Scalar::from_bool(b), this.machine.layouts.bool);
533 let which = match intrinsic_name {
534 "simd_reduce_and" => Op::MirOp(BinOp::BitAnd),
535 "simd_reduce_or" => Op::MirOp(BinOp::BitOr),
536 "simd_reduce_xor" => Op::MirOp(BinOp::BitXor),
537 "simd_reduce_any" => Op::MirOpBool(BinOp::BitOr),
538 "simd_reduce_all" => Op::MirOpBool(BinOp::BitAnd),
539 "simd_reduce_max" => Op::Max,
540 "simd_reduce_min" => Op::Min,
544 // Initialize with first lane, then proceed with the rest.
545 let mut res = this.read_immediate(&this.mplace_index(&op, 0)?.into())?;
546 if matches!(which, Op::MirOpBool(_)) {
547 // Convert to `bool` scalar.
548 res = imm_from_bool(simd_element_to_bool(res)?);
551 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
553 Op::MirOp(mir_op) => {
554 this.binary_op(mir_op, &res, &op)?
556 Op::MirOpBool(mir_op) => {
557 let op = imm_from_bool(simd_element_to_bool(op)?);
558 this.binary_op(mir_op, &res, &op)?
561 if matches!(res.layout.ty.kind(), ty::Float(_)) {
562 ImmTy::from_scalar(fmax_op(&res, &op)?, res.layout)
564 // Just boring integers, so NaNs to worry about
565 if this.binary_op(BinOp::Ge, &res, &op)?.to_scalar()?.to_bool()? {
573 if matches!(res.layout.ty.kind(), ty::Float(_)) {
574 ImmTy::from_scalar(fmin_op(&res, &op)?, res.layout)
576 // Just boring integers, so NaNs to worry about
577 if this.binary_op(BinOp::Le, &res, &op)?.to_scalar()?.to_bool()? {
586 this.write_immediate(*res, dest)?;
589 | "simd_reduce_add_ordered"
590 | "simd_reduce_mul_ordered" => {
593 let &[ref op, ref init] = check_arg_count(args)?;
594 let (op, op_len) = this.operand_to_simd(op)?;
595 let init = this.read_immediate(init)?;
597 let mir_op = match intrinsic_name {
598 "simd_reduce_add_ordered" => BinOp::Add,
599 "simd_reduce_mul_ordered" => BinOp::Mul,
605 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
606 res = this.binary_op(mir_op, &res, &op)?;
608 this.write_immediate(*res, dest)?;
611 let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
612 let (mask, mask_len) = this.operand_to_simd(mask)?;
613 let (yes, yes_len) = this.operand_to_simd(yes)?;
614 let (no, no_len) = this.operand_to_simd(no)?;
615 let (dest, dest_len) = this.place_to_simd(dest)?;
617 assert_eq!(dest_len, mask_len);
618 assert_eq!(dest_len, yes_len);
619 assert_eq!(dest_len, no_len);
621 for i in 0..dest_len {
622 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
623 let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
624 let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
625 let dest = this.mplace_index(&dest, i)?;
627 let val = if simd_element_to_bool(mask)? { yes } else { no };
628 this.write_immediate(*val, &dest.into())?;
632 "simd_cast" | "simd_as" => {
633 let &[ref op] = check_arg_count(args)?;
634 let (op, op_len) = this.operand_to_simd(op)?;
635 let (dest, dest_len) = this.place_to_simd(dest)?;
637 assert_eq!(dest_len, op_len);
639 let safe_cast = intrinsic_name == "simd_as";
641 for i in 0..dest_len {
642 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
643 let dest = this.mplace_index(&dest, i)?;
645 let val = match (op.layout.ty.kind(), dest.layout.ty.kind()) {
646 // Int-to-(int|float): always safe
647 (ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_)) =>
648 this.misc_cast(&op, dest.layout.ty)?,
649 // Float-to-float: always safe
650 (ty::Float(_), ty::Float(_)) =>
651 this.misc_cast(&op, dest.layout.ty)?,
652 // Float-to-int in safe mode
653 (ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
654 this.misc_cast(&op, dest.layout.ty)?,
655 // Float-to-int in unchecked mode
656 (ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
657 this.float_to_int_unchecked(op.to_scalar()?.to_f32()?, dest.layout.ty)?.into(),
658 (ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
659 this.float_to_int_unchecked(op.to_scalar()?.to_f64()?, dest.layout.ty)?.into(),
662 "Unsupported SIMD cast from element type {} to {}",
667 this.write_immediate(val, &dest.into())?;
671 let &[ref left, ref right, ref index] = check_arg_count(args)?;
672 let (left, left_len) = this.operand_to_simd(left)?;
673 let (right, right_len) = this.operand_to_simd(right)?;
674 let (dest, dest_len) = this.place_to_simd(dest)?;
676 // `index` is an array, not a SIMD type
677 let ty::Array(_, index_len) = index.layout.ty.kind() else {
678 bug!("simd_shuffle index argument has non-array type {}", index.layout.ty)
680 let index_len = index_len.eval_usize(*this.tcx, this.param_env());
682 assert_eq!(left_len, right_len);
683 assert_eq!(index_len, dest_len);
685 for i in 0..dest_len {
686 let src_index: u64 = this
687 .read_immediate(&this.operand_index(&index, i)?.into())?
691 let dest = this.mplace_index(&dest, i)?;
693 let val = if src_index < left_len {
694 this.read_immediate(&this.mplace_index(&left, src_index)?.into())?
695 } else if src_index < left_len.checked_add(right_len).unwrap() {
697 &this.mplace_index(&right, src_index - left_len)?.into(),
701 "simd_shuffle index {} is out of bounds for 2 vectors of size {}",
706 this.write_immediate(*val, &dest.into())?;
710 let &[ref passthru, ref ptrs, ref mask] = check_arg_count(args)?;
711 let (passthru, passthru_len) = this.operand_to_simd(passthru)?;
712 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
713 let (mask, mask_len) = this.operand_to_simd(mask)?;
714 let (dest, dest_len) = this.place_to_simd(dest)?;
716 assert_eq!(dest_len, passthru_len);
717 assert_eq!(dest_len, ptrs_len);
718 assert_eq!(dest_len, mask_len);
720 for i in 0..dest_len {
721 let passthru = this.read_immediate(&this.mplace_index(&passthru, i)?.into())?;
722 let ptr = this.read_immediate(&this.mplace_index(&ptrs, i)?.into())?;
723 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
724 let dest = this.mplace_index(&dest, i)?;
726 let val = if simd_element_to_bool(mask)? {
727 let place = this.deref_operand(&ptr.into())?;
728 this.read_immediate(&place.into())?
732 this.write_immediate(*val, &dest.into())?;
736 let &[ref value, ref ptrs, ref mask] = check_arg_count(args)?;
737 let (value, value_len) = this.operand_to_simd(value)?;
738 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
739 let (mask, mask_len) = this.operand_to_simd(mask)?;
741 assert_eq!(ptrs_len, value_len);
742 assert_eq!(ptrs_len, mask_len);
744 for i in 0..ptrs_len {
745 let value = this.read_immediate(&this.mplace_index(&value, i)?.into())?;
746 let ptr = this.read_immediate(&this.mplace_index(&ptrs, i)?.into())?;
747 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
749 if simd_element_to_bool(mask)? {
750 let place = this.deref_operand(&ptr.into())?;
751 this.write_immediate(*value, &place.into())?;
757 "atomic_load" => this.atomic_load(args, dest, AtomicReadOp::SeqCst)?,
758 "atomic_load_relaxed" => this.atomic_load(args, dest, AtomicReadOp::Relaxed)?,
759 "atomic_load_acq" => this.atomic_load(args, dest, AtomicReadOp::Acquire)?,
761 "atomic_store" => this.atomic_store(args, AtomicWriteOp::SeqCst)?,
762 "atomic_store_relaxed" => this.atomic_store(args, AtomicWriteOp::Relaxed)?,
763 "atomic_store_rel" => this.atomic_store(args, AtomicWriteOp::Release)?,
765 "atomic_fence_acq" => this.atomic_fence(args, AtomicFenceOp::Acquire)?,
766 "atomic_fence_rel" => this.atomic_fence(args, AtomicFenceOp::Release)?,
767 "atomic_fence_acqrel" => this.atomic_fence(args, AtomicFenceOp::AcqRel)?,
768 "atomic_fence" => this.atomic_fence(args, AtomicFenceOp::SeqCst)?,
770 "atomic_singlethreadfence_acq" => this.compiler_fence(args, AtomicFenceOp::Acquire)?,
771 "atomic_singlethreadfence_rel" => this.compiler_fence(args, AtomicFenceOp::Release)?,
772 "atomic_singlethreadfence_acqrel" =>
773 this.compiler_fence(args, AtomicFenceOp::AcqRel)?,
774 "atomic_singlethreadfence" => this.compiler_fence(args, AtomicFenceOp::SeqCst)?,
776 "atomic_xchg" => this.atomic_exchange(args, dest, AtomicRwOp::SeqCst)?,
777 "atomic_xchg_acq" => this.atomic_exchange(args, dest, AtomicRwOp::Acquire)?,
778 "atomic_xchg_rel" => this.atomic_exchange(args, dest, AtomicRwOp::Release)?,
779 "atomic_xchg_acqrel" => this.atomic_exchange(args, dest, AtomicRwOp::AcqRel)?,
780 "atomic_xchg_relaxed" => this.atomic_exchange(args, dest, AtomicRwOp::Relaxed)?,
784 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
786 "atomic_cxchg_acq" =>
787 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
789 "atomic_cxchg_rel" =>
790 this.atomic_compare_exchange(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
792 "atomic_cxchg_acqrel" =>
793 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
795 "atomic_cxchg_relaxed" =>
796 this.atomic_compare_exchange(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
798 "atomic_cxchg_acq_failrelaxed" =>
799 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
801 "atomic_cxchg_acqrel_failrelaxed" =>
802 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
804 "atomic_cxchg_failrelaxed" =>
805 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
807 "atomic_cxchg_failacq" =>
808 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
811 "atomic_cxchgweak" =>
812 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
814 "atomic_cxchgweak_acq" =>
815 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
817 "atomic_cxchgweak_rel" =>
818 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
820 "atomic_cxchgweak_acqrel" =>
821 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
823 "atomic_cxchgweak_relaxed" =>
824 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
826 "atomic_cxchgweak_acq_failrelaxed" =>
827 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
829 "atomic_cxchgweak_acqrel_failrelaxed" =>
830 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
832 "atomic_cxchgweak_failrelaxed" =>
833 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
835 "atomic_cxchgweak_failacq" =>
836 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
840 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::SeqCst)?,
843 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Acquire)?,
846 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Release)?,
848 "atomic_or_acqrel" =>
849 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::AcqRel)?,
851 "atomic_or_relaxed" =>
852 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Relaxed)?,
855 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::SeqCst)?,
858 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Acquire)?,
861 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Release)?,
863 "atomic_xor_acqrel" =>
864 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::AcqRel)?,
866 "atomic_xor_relaxed" =>
867 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Relaxed)?,
870 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::SeqCst)?,
873 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Acquire)?,
876 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Release)?,
878 "atomic_and_acqrel" =>
879 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::AcqRel)?,
881 "atomic_and_relaxed" =>
882 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Relaxed)?,
885 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::SeqCst)?,
888 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Acquire)?,
891 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Release)?,
893 "atomic_nand_acqrel" =>
894 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::AcqRel)?,
896 "atomic_nand_relaxed" =>
897 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Relaxed)?,
900 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::SeqCst)?,
903 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Acquire)?,
906 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Release)?,
908 "atomic_xadd_acqrel" =>
909 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::AcqRel)?,
911 "atomic_xadd_relaxed" =>
912 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Relaxed)?,
915 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::SeqCst)?,
918 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Acquire)?,
921 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Release)?,
923 "atomic_xsub_acqrel" =>
924 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::AcqRel)?,
926 "atomic_xsub_relaxed" =>
927 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Relaxed)?,
928 "atomic_min" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
929 "atomic_min_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
930 "atomic_min_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
931 "atomic_min_acqrel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
932 "atomic_min_relaxed" =>
933 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
934 "atomic_max" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
935 "atomic_max_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
936 "atomic_max_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
937 "atomic_max_acqrel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
938 "atomic_max_relaxed" =>
939 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
940 "atomic_umin" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
941 "atomic_umin_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
942 "atomic_umin_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
943 "atomic_umin_acqrel" =>
944 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
945 "atomic_umin_relaxed" =>
946 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
947 "atomic_umax" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
948 "atomic_umax_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
949 "atomic_umax_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
950 "atomic_umax_acqrel" =>
951 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
952 "atomic_umax_relaxed" =>
953 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
957 let &[ref num, ref denom] = check_arg_count(args)?;
958 this.exact_div(&this.read_immediate(num)?, &this.read_immediate(denom)?, dest)?;
961 "try" => return this.handle_try(args, dest, ret),
964 let &[] = check_arg_count(args)?;
965 // normally this would raise a SIGTRAP, which aborts if no debugger is connected
966 throw_machine_stop!(TerminationInfo::Abort("Trace/breakpoint trap".to_string()))
969 name => throw_unsup_format!("unimplemented intrinsic: {}", name),
972 trace!("{:?}", this.dump_place(**dest));
973 this.go_to_block(ret);
979 args: &[OpTy<'tcx, Tag>],
980 dest: &PlaceTy<'tcx, Tag>,
981 atomic: AtomicReadOp,
982 ) -> InterpResult<'tcx> {
983 let this = self.eval_context_mut();
985 let &[ref place] = check_arg_count(args)?;
986 let place = this.deref_operand(place)?;
988 // make sure it fits into a scalar; otherwise it cannot be atomic
989 let val = this.read_scalar_atomic(&place, atomic)?;
991 // Check alignment requirements. Atomics must always be aligned to their size,
992 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
994 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
995 this.memory.check_ptr_access_align(
999 CheckInAllocMsg::MemoryAccessTest,
1001 // Perform regular access.
1002 this.write_scalar(val, dest)?;
1008 args: &[OpTy<'tcx, Tag>],
1009 atomic: AtomicWriteOp,
1010 ) -> InterpResult<'tcx> {
1011 let this = self.eval_context_mut();
1013 let &[ref place, ref val] = check_arg_count(args)?;
1014 let place = this.deref_operand(place)?;
1015 let val = this.read_scalar(val)?; // make sure it fits into a scalar; otherwise it cannot be atomic
1017 // Check alignment requirements. Atomics must always be aligned to their size,
1018 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1020 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1021 this.memory.check_ptr_access_align(
1025 CheckInAllocMsg::MemoryAccessTest,
1028 // Perform atomic store
1029 this.write_scalar_atomic(val, &place, atomic)?;
1035 args: &[OpTy<'tcx, Tag>],
1036 atomic: AtomicFenceOp,
1037 ) -> InterpResult<'tcx> {
1038 let &[] = check_arg_count(args)?;
1040 //FIXME: compiler fences are currently ignored
1046 args: &[OpTy<'tcx, Tag>],
1047 atomic: AtomicFenceOp,
1048 ) -> InterpResult<'tcx> {
1049 let this = self.eval_context_mut();
1050 let &[] = check_arg_count(args)?;
1051 this.validate_atomic_fence(atomic)?;
1057 args: &[OpTy<'tcx, Tag>],
1058 dest: &PlaceTy<'tcx, Tag>,
1059 atomic_op: AtomicOp,
1061 ) -> InterpResult<'tcx> {
1062 let this = self.eval_context_mut();
1064 let &[ref place, ref rhs] = check_arg_count(args)?;
1065 let place = this.deref_operand(place)?;
1067 if !place.layout.ty.is_integral() {
1068 bug!("Atomic arithmetic operations only work on integer types");
1070 let rhs = this.read_immediate(rhs)?;
1072 // Check alignment requirements. Atomics must always be aligned to their size,
1073 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1075 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1076 this.memory.check_ptr_access_align(
1080 CheckInAllocMsg::MemoryAccessTest,
1085 let old = this.atomic_min_max_scalar(&place, rhs, true, atomic)?;
1086 this.write_immediate(*old, &dest)?; // old value is returned
1090 let old = this.atomic_min_max_scalar(&place, rhs, false, atomic)?;
1091 this.write_immediate(*old, &dest)?; // old value is returned
1094 AtomicOp::MirOp(op, neg) => {
1095 let old = this.atomic_op_immediate(&place, &rhs, op, neg, atomic)?;
1096 this.write_immediate(*old, dest)?; // old value is returned
1104 args: &[OpTy<'tcx, Tag>],
1105 dest: &PlaceTy<'tcx, Tag>,
1107 ) -> InterpResult<'tcx> {
1108 let this = self.eval_context_mut();
1110 let &[ref place, ref new] = check_arg_count(args)?;
1111 let place = this.deref_operand(place)?;
1112 let new = this.read_scalar(new)?;
1114 // Check alignment requirements. Atomics must always be aligned to their size,
1115 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1117 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1118 this.memory.check_ptr_access_align(
1122 CheckInAllocMsg::MemoryAccessTest,
1125 let old = this.atomic_exchange_scalar(&place, new, atomic)?;
1126 this.write_scalar(old, dest)?; // old value is returned
1130 fn atomic_compare_exchange_impl(
1132 args: &[OpTy<'tcx, Tag>],
1133 dest: &PlaceTy<'tcx, Tag>,
1134 success: AtomicRwOp,
1136 can_fail_spuriously: bool,
1137 ) -> InterpResult<'tcx> {
1138 let this = self.eval_context_mut();
1140 let &[ref place, ref expect_old, ref new] = check_arg_count(args)?;
1141 let place = this.deref_operand(place)?;
1142 let expect_old = this.read_immediate(expect_old)?; // read as immediate for the sake of `binary_op()`
1143 let new = this.read_scalar(new)?;
1145 // Check alignment requirements. Atomics must always be aligned to their size,
1146 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1148 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1149 this.memory.check_ptr_access_align(
1153 CheckInAllocMsg::MemoryAccessTest,
1156 let old = this.atomic_compare_exchange_scalar(
1162 can_fail_spuriously,
1165 // Return old value.
1166 this.write_immediate(old, dest)?;
1170 fn atomic_compare_exchange(
1172 args: &[OpTy<'tcx, Tag>],
1173 dest: &PlaceTy<'tcx, Tag>,
1174 success: AtomicRwOp,
1176 ) -> InterpResult<'tcx> {
1177 self.atomic_compare_exchange_impl(args, dest, success, fail, false)
1180 fn atomic_compare_exchange_weak(
1182 args: &[OpTy<'tcx, Tag>],
1183 dest: &PlaceTy<'tcx, Tag>,
1184 success: AtomicRwOp,
1186 ) -> InterpResult<'tcx> {
1187 self.atomic_compare_exchange_impl(args, dest, success, fail, true)
1190 fn float_to_int_unchecked<F>(
1193 dest_ty: ty::Ty<'tcx>,
1194 ) -> InterpResult<'tcx, Scalar<Tag>>
1196 F: Float + Into<Scalar<Tag>>,
1198 let this = self.eval_context_ref();
1200 // Step 1: cut off the fractional part of `f`. The result of this is
1201 // guaranteed to be precisely representable in IEEE floats.
1202 let f = f.round_to_integral(Round::TowardZero).value;
1204 // Step 2: Cast the truncated float to the target integer type and see if we lose any information in this step.
1205 Ok(match dest_ty.kind() {
1208 let size = Integer::from_uint_ty(this, *t).size();
1209 let res = f.to_u128(size.bits_usize());
1210 if res.status.is_empty() {
1211 // No status flags means there was no further rounding or other loss of precision.
1212 Scalar::from_uint(res.value, size)
1214 // `f` was not representable in this integer type.
1216 "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1224 let size = Integer::from_int_ty(this, *t).size();
1225 let res = f.to_i128(size.bits_usize());
1226 if res.status.is_empty() {
1227 // No status flags means there was no further rounding or other loss of precision.
1228 Scalar::from_int(res.value, size)
1230 // `f` was not representable in this integer type.
1232 "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1239 _ => bug!("`float_to_int_unchecked` called with non-int output type {:?}", dest_ty),
1245 left: &ImmTy<'tcx, Tag>,
1246 right: &ImmTy<'tcx, Tag>,
1247 ) -> InterpResult<'tcx, Scalar<Tag>> {
1248 assert_eq!(left.layout.ty, right.layout.ty);
1249 let ty::Float(float_ty) = left.layout.ty.kind() else {
1250 bug!("fmax operand is not a float")
1252 let left = left.to_scalar()?;
1253 let right = right.to_scalar()?;
1255 FloatTy::F32 => Scalar::from_f32(left.to_f32()?.max(right.to_f32()?)),
1256 FloatTy::F64 => Scalar::from_f64(left.to_f64()?.max(right.to_f64()?)),
1261 left: &ImmTy<'tcx, Tag>,
1262 right: &ImmTy<'tcx, Tag>,
1263 ) -> InterpResult<'tcx, Scalar<Tag>> {
1264 assert_eq!(left.layout.ty, right.layout.ty);
1265 let ty::Float(float_ty) = left.layout.ty.kind() else {
1266 bug!("fmin operand is not a float")
1268 let left = left.to_scalar()?;
1269 let right = right.to_scalar()?;
1271 FloatTy::F32 => Scalar::from_f32(left.to_f32()?.min(right.to_f32()?)),
1272 FloatTy::F64 => Scalar::from_f64(left.to_f64()?.min(right.to_f64()?)),