Skip to content

Commit 104f97e

Browse files
committed
Move CTFE handling of nondiverging intrinsics to intrinsics.rs
1 parent b741351 commit 104f97e

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def_id::DefId;
88
use rustc_middle::mir::{
99
self,
1010
interpret::{ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar},
11-
BinOp,
11+
BinOp, NonDivergingIntrinsic,
1212
};
1313
use rustc_middle::ty;
1414
use rustc_middle::ty::layout::LayoutOf as _;
@@ -530,6 +530,32 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
530530
Ok(true)
531531
}
532532

533+
pub(super) fn emulate_nondiverging_intrinsic(
534+
&mut self,
535+
intrinsic: &NonDivergingIntrinsic<'tcx>,
536+
) -> InterpResult<'tcx> {
537+
match intrinsic {
538+
NonDivergingIntrinsic::Assume(op) => {
539+
let op = self.eval_operand(op, None)?;
540+
let cond = self.read_scalar(&op)?.to_bool()?;
541+
if !cond {
542+
throw_ub_format!("`assume` called with `false`");
543+
}
544+
Ok(())
545+
}
546+
NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
547+
count,
548+
src,
549+
dst,
550+
}) => {
551+
let src = self.eval_operand(src, None)?;
552+
let dst = self.eval_operand(dst, None)?;
553+
let count = self.eval_operand(count, None)?;
554+
self.copy_intrinsic(&src, &dst, &count, /* nonoverlapping */ true)
555+
}
556+
}
557+
}
558+
533559
pub fn exact_div(
534560
&mut self,
535561
a: &ImmTy<'tcx, M::Provenance>,

compiler/rustc_const_eval/src/interpret/step.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//!
33
//! The main entry point is the `step` method.
44
5+
use rustc_middle::mir;
56
use rustc_middle::mir::interpret::{InterpResult, Scalar};
6-
use rustc_middle::mir::{self, NonDivergingIntrinsic};
77
use rustc_middle::ty::layout::LayoutOf;
88

99
use super::{InterpCx, Machine};
@@ -114,23 +114,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
114114
M::retag(self, *kind, &dest)?;
115115
}
116116

117-
Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
118-
let op = self.eval_operand(op, None)?;
119-
let cond = self.read_scalar(&op)?.to_bool()?;
120-
if !cond {
121-
throw_ub_format!("`assume` called with `false`");
122-
}
123-
}
124-
Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
125-
ref count,
126-
ref src,
127-
ref dst,
128-
})) => {
129-
let src = self.eval_operand(src, None)?;
130-
let dst = self.eval_operand(dst, None)?;
131-
let count = self.eval_operand(count, None)?;
132-
self.copy_intrinsic(&src, &dst, &count, /* nonoverlapping */ true)?;
133-
}
117+
Intrinsic(box ref intrinsic) => self.emulate_nondiverging_intrinsic(intrinsic)?,
134118

135119
// Statements we do not track.
136120
AscribeUserType(..) => {}

0 commit comments

Comments
 (0)