Skip to content

Commit

Permalink
Always run intrinsics lowering pass
Browse files Browse the repository at this point in the history
Move intrinsics lowering pass from the optimization phase (where it
would not run if -Zmir-opt-level=0), to the drop lowering phase where it
runs unconditionally.

The implementation of those intrinsics in code generation and
interpreter is unnecessary. Remove it.
  • Loading branch information
tmiasko committed Dec 15, 2020
1 parent 1f7762b commit 1b74a82
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
16 changes: 1 addition & 15 deletions compiler/rustc_codegen_ssa/src/mir/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
return;
}

sym::unreachable => {
return;
}
sym::va_start => bx.va_start(args[0].immediate()),
sym::va_end => bx.va_end(args[0].immediate()),
sym::size_of_val => {
Expand All @@ -106,8 +103,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
bx.const_usize(bx.layout_of(tp_ty).align.abi.bytes())
}
}
sym::size_of
| sym::pref_align_of
sym::pref_align_of
| sym::min_align_of
| sym::needs_drop
| sym::type_id
Expand All @@ -119,10 +115,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
.unwrap();
OperandRef::from_const(bx, value, ret_ty).immediate_or_packed_pair(bx)
}
// Effectively no-op
sym::forget => {
return;
}
sym::offset => {
let ptr = args[0].immediate();
let offset = args[1].immediate();
Expand Down Expand Up @@ -218,9 +210,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
sym::add_with_overflow
| sym::sub_with_overflow
| sym::mul_with_overflow
| sym::wrapping_add
| sym::wrapping_sub
| sym::wrapping_mul
| sym::unchecked_div
| sym::unchecked_rem
| sym::unchecked_shl
Expand Down Expand Up @@ -254,9 +243,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {

return;
}
sym::wrapping_add => bx.add(args[0].immediate(), args[1].immediate()),
sym::wrapping_sub => bx.sub(args[0].immediate(), args[1].immediate()),
sym::wrapping_mul => bx.mul(args[0].immediate(), args[1].immediate()),
sym::exact_div => {
if signed {
bx.exactsdiv(args[0].immediate(), args[1].immediate())
Expand Down
31 changes: 8 additions & 23 deletions compiler/rustc_mir/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ crate fn eval_nullary_intrinsic<'tcx>(
ConstValue::Slice { data: alloc, start: 0, end: alloc.len() }
}
sym::needs_drop => ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env)),
sym::size_of | sym::min_align_of | sym::pref_align_of => {
sym::min_align_of | sym::pref_align_of => {
let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
let n = match name {
sym::pref_align_of => layout.align.pref.bytes(),
sym::min_align_of => layout.align.abi.bytes(),
sym::size_of => layout.size.bytes(),
_ => bug!(),
};
ConstValue::from_machine_usize(n, &tcx)
Expand Down Expand Up @@ -125,7 +124,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let (dest, ret) = match ret {
None => match intrinsic_name {
sym::transmute => throw_ub_format!("transmuting to uninhabited type"),
sym::unreachable => throw_ub!(Unreachable),
sym::abort => M::abort(self, "the program aborted execution".to_owned())?,
// Unsupported diverging intrinsic.
_ => return Ok(false),
Expand Down Expand Up @@ -160,13 +158,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
sym::min_align_of
| sym::pref_align_of
| sym::needs_drop
| sym::size_of
| sym::type_id
| sym::type_name
| sym::variant_count => {
let gid = GlobalId { instance, promoted: None };
let ty = match intrinsic_name {
sym::min_align_of | sym::pref_align_of | sym::size_of | sym::variant_count => {
sym::min_align_of | sym::pref_align_of | sym::variant_count => {
self.tcx.types.usize
}
sym::needs_drop => self.tcx.types.bool,
Expand Down Expand Up @@ -212,28 +209,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let out_val = numeric_intrinsic(intrinsic_name, bits, kind)?;
self.write_scalar(out_val, dest)?;
}
sym::wrapping_add
| sym::wrapping_sub
| sym::wrapping_mul
| sym::add_with_overflow
| sym::sub_with_overflow
| sym::mul_with_overflow => {
sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
let lhs = self.read_immediate(args[0])?;
let rhs = self.read_immediate(args[1])?;
let (bin_op, ignore_overflow) = match intrinsic_name {
sym::wrapping_add => (BinOp::Add, true),
sym::wrapping_sub => (BinOp::Sub, true),
sym::wrapping_mul => (BinOp::Mul, true),
sym::add_with_overflow => (BinOp::Add, false),
sym::sub_with_overflow => (BinOp::Sub, false),
sym::mul_with_overflow => (BinOp::Mul, false),
let bin_op = match intrinsic_name {
sym::add_with_overflow => BinOp::Add,
sym::sub_with_overflow => BinOp::Sub,
sym::mul_with_overflow => BinOp::Mul,
_ => bug!("Already checked for int ops"),
};
if ignore_overflow {
self.binop_ignore_overflow(bin_op, lhs, rhs, dest)?;
} else {
self.binop_with_overflow(bin_op, lhs, rhs, dest)?;
}
self.binop_with_overflow(bin_op, lhs, rhs, dest)?;
}
sym::saturating_add | sym::saturating_sub => {
let l = self.read_immediate(args[0])?;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
// `AddRetag` needs to run after `ElaborateDrops`. Otherwise it should run fairly late,
// but before optimizations begin.
&add_retag::AddRetag,
&lower_intrinsics::LowerIntrinsics,
&simplify::SimplifyCfg::new("elaborate-drops"),
// `Deaggregator` is conceptually part of MIR building, some backends rely on it happening
// and it can help optimizations.
Expand Down Expand Up @@ -391,7 +392,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

// The main optimizations that we do on MIR.
let optimizations: &[&dyn MirPass<'tcx>] = &[
&lower_intrinsics::LowerIntrinsics,
&remove_unneeded_drops::RemoveUnneededDrops,
&match_branches::MatchBranchSimplification,
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
Expand Down
8 changes: 8 additions & 0 deletions src/test/mir-opt/lower_intrinsics.forget.LowerIntrinsics.diff
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@
StorageDead(_3); // scope 1 at $DIR/lower_intrinsics.rs:19:40: 19:41
StorageDead(_2); // scope 0 at $DIR/lower_intrinsics.rs:19:43: 19:44
_0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:18:24: 20:2
goto -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:20:1: 20:2
}

bb2: {
return; // scope 0 at $DIR/lower_intrinsics.rs:20:2: 20:2
}

bb3 (cleanup): {
resume; // scope 0 at $DIR/lower_intrinsics.rs:18:1: 20:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
StorageDead(_1); // scope 0 at $DIR/lower_intrinsics.rs:59:1: 59:2
return; // scope 0 at $DIR/lower_intrinsics.rs:59:2: 59:2
}

bb2 (cleanup): {
resume; // scope 0 at $DIR/lower_intrinsics.rs:55:1: 59:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
bb1: {
return; // scope 0 at $DIR/lower_intrinsics.rs:15:2: 15:2
}

bb2 (cleanup): {
resume; // scope 0 at $DIR/lower_intrinsics.rs:13:1: 15:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn() -> ! {std::intrinsics::unreachable}, val: Value(Scalar(<ZST>)) }
+ unreachable; // scope 1 at $DIR/lower_intrinsics.rs:24:14: 24:45
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/lower_intrinsics.rs:23:1: 25:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,9 @@
StorageDead(_3); // scope 0 at $DIR/lower_intrinsics.rs:10:1: 10:2
return; // scope 0 at $DIR/lower_intrinsics.rs:10:2: 10:2
}

bb4 (cleanup): {
resume; // scope 0 at $DIR/lower_intrinsics.rs:6:1: 10:2
}
}

0 comments on commit 1b74a82

Please sign in to comment.