Skip to content

Commit

Permalink
rustc_mir: run the lower_intrinsics pass on MIR shims.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Oct 9, 2021
1 parent c82cf47 commit fddc321
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_mir_transform/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::iter;

use crate::util::expand_aggregate;
use crate::{
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, remove_noop_landing_pads,
run_passes, simplify,
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, lower_intrinsics,
remove_noop_landing_pads, run_passes, simplify,
};
use rustc_middle::mir::patch::MirPatch;
use rustc_mir_dataflow::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle};
Expand Down Expand Up @@ -82,6 +82,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
&[&[
&add_moves_for_packed_drops::AddMovesForPackedDrops,
&remove_noop_landing_pads::RemoveNoopLandingPads,
&lower_intrinsics::LowerIntrinsics,
&simplify::SimplifyCfg::new("make_shim"),
&add_call_guards::CriticalCallEdges,
&abort_unwinding_calls::AbortUnwindingCalls,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- // MIR for `discriminant_value` before LowerIntrinsics
+ // MIR for `discriminant_value` after LowerIntrinsics

fn discriminant_value(_1: &T) -> <T as DiscriminantKind>::Discriminant {
let mut _0: <T as std::marker::DiscriminantKind>::Discriminant; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL

bb0: {
- _0 = discriminant_value::<T>(move _1) -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // mir::Constant
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r T) -> <T as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<T>}, val: Value(Scalar(<ZST>)) }
+ _0 = discriminant((*_1)); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb1: {
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb2 (cleanup): {
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- // MIR for `std::intrinsics::forget` before LowerIntrinsics
+ // MIR for `std::intrinsics::forget` after LowerIntrinsics

fn std::intrinsics::forget(_1: T) -> () {
let mut _0: (); // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL

bb0: {
- _0 = std::intrinsics::forget::<T>(move _1) -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // mir::Constant
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // + literal: Const { ty: extern "rust-intrinsic" fn(T) {std::intrinsics::forget::<T>}, val: Value(Scalar(<ZST>)) }
+ _0 = const (); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb1: {
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb2 (cleanup): {
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- // MIR for `std::intrinsics::size_of` before LowerIntrinsics
+ // MIR for `std::intrinsics::size_of` after LowerIntrinsics

fn std::intrinsics::size_of() -> usize {
let mut _0: usize; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL

bb0: {
- _0 = std::intrinsics::size_of::<T>() -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // mir::Constant
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // + literal: Const { ty: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>}, val: Value(Scalar(<ZST>)) }
+ _0 = SizeOf(T); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb1: {
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb2 (cleanup): {
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
- // MIR for `std::intrinsics::unreachable` before LowerIntrinsics
+ // MIR for `std::intrinsics::unreachable` after LowerIntrinsics

fn std::intrinsics::unreachable() -> ! {
let mut _0: !; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL

bb0: {
- _0 = std::intrinsics::unreachable() -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // mir::Constant
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn() -> ! {std::intrinsics::unreachable}, val: Value(Scalar(<ZST>)) }
+ unreachable; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb1: {
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb2 (cleanup): {
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- // MIR for `wrapping_add` before LowerIntrinsics
+ // MIR for `wrapping_add` after LowerIntrinsics

fn wrapping_add(_1: T, _2: T) -> T {
let mut _0: T; // return place in scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL

bb0: {
- _0 = wrapping_add::<T>(move _1, move _2) -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // mir::Constant
- // + span: $SRC_DIR/core/src/intrinsics.rs:LL:COL
- // + literal: Const { ty: extern "rust-intrinsic" fn(T, T) -> T {std::intrinsics::wrapping_add::<T>}, val: Value(Scalar(<ZST>)) }
+ _0 = Add(move _1, move _2); // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
+ goto -> bb1; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb1: {
return; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}

bb2 (cleanup): {
resume; // scope 0 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
}
}

17 changes: 17 additions & 0 deletions src/test/mir-opt/lower_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@ pub fn discriminant<T>(t: T) {
core::intrinsics::discriminant_value(&());
core::intrinsics::discriminant_value(&E::B);
}

// Check that the MIR shims used for reifying intrinsics to `fn` pointers,
// also go through the lowering pass.
pub fn reify_intrinsics() -> impl Copy {
(
// EMIT_MIR core.intrinsics-#1-wrapping_add.LowerIntrinsics.diff
core::intrinsics::wrapping_add::<u32> as unsafe fn(_, _) -> _,
// EMIT_MIR core.intrinsics-#1-size_of.LowerIntrinsics.diff
core::intrinsics::size_of::<u8> as unsafe fn() -> _,
// EMIT_MIR core.intrinsics-#1-unreachable.LowerIntrinsics.diff
core::intrinsics::unreachable as unsafe fn() -> !,
// EMIT_MIR core.intrinsics-#1-forget.LowerIntrinsics.diff
core::intrinsics::forget::<E> as unsafe fn(_),
// EMIT_MIR core.intrinsics-#1-discriminant_value.LowerIntrinsics.diff
core::intrinsics::discriminant_value::<E> as unsafe fn(_) -> _,
)
}

0 comments on commit fddc321

Please sign in to comment.