Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 18 pull requests #76945

Closed
wants to merge 56 commits into from
Closed
Changes from 6 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
f8376b5
shim: monomorphic `FnPtrShim`s during construction
davidtwco Aug 9, 2020
a06edda
Fix segfault if pthread_getattr_np fails
tavianator Sep 9, 2020
a684153
Only call pthread_attr_destroy() after getattr_np() succeeds on all l…
tavianator Sep 9, 2020
8f27e3c
Make some methods of `Pin` unstable const
CDirkx Sep 12, 2020
e5447a2
Fix #76432
simonvandel Sep 13, 2020
9c5d0c1
MIR pass to remove unneeded drops on types not needing drop
simonvandel Sep 13, 2020
9d47ecf
Suggestion from review
simonvandel Sep 14, 2020
176956c
Test and fix Sync & Send traits of BTreeMap artefacts
ssomers Sep 14, 2020
21b0c12
Extract some intrinsics out of rustc_codegen_llvm
khyperia Sep 15, 2020
eede953
Only get ImplKind::Impl once
tesuji Sep 10, 2020
94dae60
simplfy condition in ItemLowerer::with_trait_impl_ref()
matthiaskrgr Sep 16, 2020
b7c8bea
Fix wording in mir doc
pickfire Sep 17, 2020
f4a7149
Don't compile regex at every function call.
hbina Sep 17, 2020
b479139
Remove intrinsics::arith_offset use from libarena
est31 Sep 17, 2020
4fe6ca3
Replace const_generics feature gate with min_const_generics
est31 Sep 17, 2020
ebdea01
Remove redundant #![feature(...)] 's from compiler/
est31 Sep 17, 2020
5f58e00
fix array_windows docs
lcnr Sep 17, 2020
2793672
use `array_windows` instead of `windows` in the compiler
lcnr Sep 17, 2020
f193593
array pattern
lcnr Sep 17, 2020
764d307
docs `array` -> `slice`
lcnr Sep 17, 2020
1dd3df6
black_box: silence unused_mut warning when building with cfg(miri)
RalfJung Sep 17, 2020
1ccfcb6
Set BUILD_TRIPLE via build script
Mark-Simulacrum Sep 6, 2020
ccdb48a
Remove support for different src directory
Mark-Simulacrum Sep 7, 2020
f0392c2
Discover Rust toolchain without Python
Mark-Simulacrum Sep 7, 2020
aac7d9e
Provide bootstrap tools with RUSTC in environment
Mark-Simulacrum Sep 10, 2020
afed561
Specify output directory for bootstrap tests
Mark-Simulacrum Sep 10, 2020
4382436
Make invalid integer operation messages consistent
varkor Sep 15, 2020
c9c5068
`char` not char
varkor Sep 15, 2020
5ec9e96
Bless mir-opt tests
varkor Sep 15, 2020
e2e000a
Bless mir-opt 32-bit tests
varkor Sep 16, 2020
e3c6e46
Make some methods of `Pin<&mut T>` unstable const
CDirkx Sep 18, 2020
673935f
Get LocalDefId from source instead of passing in
simonvandel Sep 19, 2020
30dd6cf
The optimization should also apply for DropAndReplace
simonvandel Sep 19, 2020
804f673
cleanup cfg after optimization
simonvandel Sep 19, 2020
4d1ef03
cleanup promotion const_kind checks
RalfJung Sep 6, 2020
7febd5a
fix doc comment
RalfJung Sep 7, 2020
7b99c8e
never promote non-const operations; revert STATIC promotion change
RalfJung Sep 9, 2020
9216eb8
fix some comments
RalfJung Sep 9, 2020
7dafad3
Rollup merge of #75346 - davidtwco:issue-69925-polymorphic-instancede…
Dylan-DPC Sep 19, 2020
bbccaed
Rollup merge of #76411 - RalfJung:promote-in-const-fn, r=ecstatic-morse
Dylan-DPC Sep 19, 2020
6a6a5b6
Rollup merge of #76521 - tavianator:fix-pthread-getattr-destroy, r=Am…
Dylan-DPC Sep 19, 2020
6b21e93
Rollup merge of #76544 - Mark-Simulacrum:less-python, r=alexcrichton
Dylan-DPC Sep 19, 2020
aa81536
Rollup merge of #76655 - CDirkx:const-pin, r=ecstatic-morse
Dylan-DPC Sep 19, 2020
8549f2e
Rollup merge of #76659 - simonvandel:76432, r=oli-obk
Dylan-DPC Sep 19, 2020
2b56019
Rollup merge of #76673 - simonvandel:remove-unneeded-drops, r=oli-obk
Dylan-DPC Sep 19, 2020
12da3b0
Rollup merge of #76722 - ssomers:btree_send_sync, r=Mark-Simulacrum
Dylan-DPC Sep 19, 2020
678268e
Rollup merge of #76754 - varkor:diagnostic-cleanup-ii, r=ecstatic-morse
Dylan-DPC Sep 19, 2020
64efb80
Rollup merge of #76766 - khyperia:generic_intrinsics, r=eddyb
Dylan-DPC Sep 19, 2020
a2e1b15
Rollup merge of #76783 - lzutao:rd_impl_kind, r=GuillaumeGomez
Dylan-DPC Sep 19, 2020
52891e0
Rollup merge of #76809 - matthiaskrgr:simplify_cond, r=varkor
Dylan-DPC Sep 19, 2020
92077f1
Rollup merge of #76815 - pickfire:patch-6, r=jonas-schievink
Dylan-DPC Sep 19, 2020
c377533
Rollup merge of #76818 - hbina:dont_compile_regex_all_the_time, r=ecs…
Dylan-DPC Sep 19, 2020
ea11a23
Rollup merge of #76821 - est31:remove_redundant_nightly_features, r=o…
Dylan-DPC Sep 19, 2020
d0d8e7d
Rollup merge of #76823 - RalfJung:black-box-warn, r=joshtriplett
Dylan-DPC Sep 19, 2020
6403603
Rollup merge of #76825 - lcnr:array-windows-apply, r=varkor
Dylan-DPC Sep 19, 2020
68301a0
Rollup merge of #76827 - lcnr:array_windows-docs, r=jonas-schievink
Dylan-DPC Sep 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_mir/src/transform/mod.rs
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ pub mod nrvo;
pub mod promote_consts;
pub mod qualify_min_const_fn;
pub mod remove_noop_landing_pads;
pub mod remove_unneeded_drops;
pub mod required_consts;
pub mod rustc_peek;
pub mod simplify;
@@ -459,6 +460,7 @@ fn run_optimization_passes<'tcx>(

// The main optimizations that we do on MIR.
let optimizations: &[&dyn MirPass<'tcx>] = &[
&remove_unneeded_drops::RemoveUnneededDrops,
&match_branches::MatchBranchSimplification,
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
&instcombine::InstCombine,
59 changes: 59 additions & 0 deletions compiler/rustc_mir/src/transform/remove_unneeded_drops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! This pass replaces a drop of a type that does not need dropping, with a goto

use crate::transform::{MirPass, MirSource};
use rustc_hir::def_id::LocalDefId;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;

use super::simplify::simplify_cfg;

pub struct RemoveUnneededDrops;

impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
trace!("Running RemoveUnneededDrops on {:?}", source);
let mut opt_finder = RemoveUnneededDropsOptimizationFinder {
tcx,
body,
optimizations: vec![],
def_id: source.def_id().expect_local(),
};
opt_finder.visit_body(body);
let should_simplify = !opt_finder.optimizations.is_empty();
for (loc, target) in opt_finder.optimizations {
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
terminator.kind = TerminatorKind::Goto { target };
}

// if we applied optimizations, we potentially have some cfg to cleanup to
// make it easier for further passes
if should_simplify {
simplify_cfg(body);
}
}
}

impl<'a, 'tcx> Visitor<'tcx> for RemoveUnneededDropsOptimizationFinder<'a, 'tcx> {
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
match terminator.kind {
TerminatorKind::Drop { place, target, .. }
| TerminatorKind::DropAndReplace { place, target, .. } => {
let ty = place.ty(self.body, self.tcx);
let needs_drop = ty.ty.needs_drop(self.tcx, self.tcx.param_env(self.def_id));
if !needs_drop {
self.optimizations.push((location, target));
}
}
_ => {}
}
self.super_terminator(terminator, location);
}
}
pub struct RemoveUnneededDropsOptimizationFinder<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
body: &'a Body<'tcx>,
optimizations: Vec<(Location, BasicBlock)>,
def_id: LocalDefId,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- // MIR for `cannot_opt_generic` before RemoveUnneededDrops
+ // MIR for `cannot_opt_generic` after RemoveUnneededDrops

fn cannot_opt_generic(_1: T) -> () {
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:19:26: 19:27
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:19:32: 19:32
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:20:5: 20:12
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:20:10: 20:11
scope 1 {
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:20:5: 20:12
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:20:10: 20:11
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:20:10: 20:11
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/remove_unneeded_drops.rs:19:1: 21:2
}

bb2: {
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:20:11: 20:12
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:20:12: 20:13
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:19:32: 21:2
return; // scope 0 at $DIR/remove_unneeded_drops.rs:21:2: 21:2
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- // MIR for `dont_opt` before RemoveUnneededDrops
+ // MIR for `dont_opt` after RemoveUnneededDrops

fn dont_opt(_1: Vec<bool>) -> () {
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:7:13: 7:14
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:7:27: 7:27
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:8:5: 8:12
let mut _3: std::vec::Vec<bool>; // in scope 0 at $DIR/remove_unneeded_drops.rs:8:10: 8:11
scope 1 {
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:8:5: 8:12
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:8:10: 8:11
_3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:8:10: 8:11
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/remove_unneeded_drops.rs:7:1: 9:2
}

bb2: {
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:8:11: 8:12
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:8:12: 8:13
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:7:27: 9:2
return; // scope 0 at $DIR/remove_unneeded_drops.rs:9:2: 9:2
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- // MIR for `opt` before RemoveUnneededDrops
+ // MIR for `opt` after RemoveUnneededDrops

fn opt(_1: bool) -> () {
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:2:8: 2:9
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:2:17: 2:17
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:3:5: 3:12
let mut _3: bool; // in scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
scope 1 {
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:3:5: 3:12
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:3:10: 3:11
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
- }
-
- bb1: {
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:3:11: 3:12
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:3:12: 3:13
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:2:17: 4:2
return; // scope 0 at $DIR/remove_unneeded_drops.rs:4:2: 4:2
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- // MIR for `opt_generic_copy` before RemoveUnneededDrops
+ // MIR for `opt_generic_copy` after RemoveUnneededDrops

fn opt_generic_copy(_1: T) -> () {
debug x => _1; // in scope 0 at $DIR/remove_unneeded_drops.rs:12:30: 12:31
let mut _0: (); // return place in scope 0 at $DIR/remove_unneeded_drops.rs:12:36: 12:36
let _2: (); // in scope 0 at $DIR/remove_unneeded_drops.rs:13:5: 13:12
let mut _3: T; // in scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
scope 1 {
debug _x => _3; // in scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:13:5: 13:12
StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
_3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:13:10: 13:11
_2 = const (); // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
- drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
- }
-
- bb1: {
StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:13:11: 13:12
StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:13:12: 13:13
_0 = const (); // scope 0 at $DIR/remove_unneeded_drops.rs:12:36: 14:2
return; // scope 0 at $DIR/remove_unneeded_drops.rs:14:2: 14:2
}
}

28 changes: 28 additions & 0 deletions src/test/mir-opt/remove_unneeded_drops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// EMIT_MIR remove_unneeded_drops.opt.RemoveUnneededDrops.diff
fn opt(x: bool) {
drop(x);
}

// EMIT_MIR remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff
fn dont_opt(x: Vec<bool>) {
drop(x);
}

// EMIT_MIR remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff
fn opt_generic_copy<T: Copy>(x: T) {
drop(x);
}

// EMIT_MIR remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff
// since the pass is not running on monomorphisized code,
// we can't (but probably should) optimize this
fn cannot_opt_generic<T>(x: T) {
drop(x);
}

fn main() {
opt(true);
opt_generic_copy(42);
cannot_opt_generic(42);
dont_opt(vec![true]);
}