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 14 pull requests #76987

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b1375cd
Deny unsafe op in unsafe fns without the unsafe keyword, first part f…
poliorcetics Jul 10, 2020
3a22b21
Finished documenting all unsafe op inside unsafe fn
poliorcetics Jul 10, 2020
ee289d2
Improve some SAFETY comments following suggestions
poliorcetics Aug 29, 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
4675a31
Use intra-doc links in core/src/iter when possible
poliorcetics Sep 18, 2020
bffd211
Finish moving to intra doc links for std::sync
poliorcetics Sep 18, 2020
982ec0d
Fix broken link
poliorcetics Sep 18, 2020
b534d9f
Fix broken link
poliorcetics Sep 18, 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
924cd13
Added benchmarks for BinaryHeap
SkiFire13 Sep 5, 2020
af1e363
Set sift=true only when PeekMut yields a mutable reference
SkiFire13 Aug 26, 2020
ca15e9d
Fix time complexity in BinaryHeap::peek_mut docs
SkiFire13 Sep 19, 2020
2a00dda
miri: correctly deal with `ConstKind::Bound`
lcnr Sep 10, 2020
6734230
do not ICE on `ty::Bound` in Layout::compute
lcnr Sep 11, 2020
65b3419
update stderr file
lcnr Sep 20, 2020
cebbd9f
Use as_nanos in bench.rs and base.rs
est31 Sep 20, 2020
43193dc
Use as_secs_f64 in profiling.rs
est31 Sep 20, 2020
4bc0e55
Replace write_fmt with write!
est31 Sep 20, 2020
0e56b52
Fix accordingly to review
poliorcetics Sep 20, 2020
08b85a6
use iter:: before free functions
poliorcetics Sep 20, 2020
8169989
Add non-`unsafe` `.get_mut()` for `UnsafeCell`
danielhenrymantilla Sep 19, 2020
5886c38
Replace unneeded `unsafe` calls to `.get()` with calls to `.get_mut()`
danielhenrymantilla Sep 19, 2020
aaddcdb
Fix nits
poliorcetics Sep 20, 2020
c9c8fb8
Add sample defaults for config.toml
jyn514 Sep 12, 2020
8e10905
Add a changelog for x.py
jyn514 Sep 12, 2020
4cb73e3
Rollup merge of #74225 - poliorcetics:std-thread-unsafe-op-in-unsafe-…
Dylan-DPC Sep 21, 2020
ac0e987
Rollup merge of #75974 - SkiFire13:peekmut-opt-sift, r=LukasKalbertodt
Dylan-DPC Sep 21, 2020
6dd9926
Rollup merge of #76521 - tavianator:fix-pthread-getattr-destroy, r=Am…
Dylan-DPC Sep 21, 2020
55e26f4
Rollup merge of #76581 - lcnr:bound-too-generic, r=eddyb
Dylan-DPC Sep 21, 2020
f20fa17
Rollup merge of #76626 - jyn514:x.py-changelog, r=Mark-Simulacrum
Dylan-DPC Sep 21, 2020
ee3743d
Rollup merge of #76628 - jyn514:default-config-files, r=Mark-Simulacrum
Dylan-DPC Sep 21, 2020
71cdf25
Rollup merge of #76655 - CDirkx:const-pin, r=ecstatic-morse
Dylan-DPC Sep 21, 2020
5f4afbb
Rollup merge of #76659 - simonvandel:76432, r=oli-obk
Dylan-DPC Sep 21, 2020
62ddc9b
Rollup merge of #76673 - simonvandel:remove-unneeded-drops, r=oli-obk
Dylan-DPC Sep 21, 2020
b7dc6d5
Rollup merge of #76867 - poliorcetics:intra-doc-core-iter, r=jyn514
Dylan-DPC Sep 21, 2020
72a86bf
Rollup merge of #76868 - poliorcetics:intra-doc-std-sync, r=jyn514
Dylan-DPC Sep 21, 2020
4289998
Rollup merge of #76936 - danielhenrymantilla:unsafecell_get_mut, r=Ra…
Dylan-DPC Sep 21, 2020
bfb1d0e
Rollup merge of #76958 - est31:ns, r=oli-obk
Dylan-DPC Sep 21, 2020
4098d2a
Rollup merge of #76959 - est31:write, r=oli-obk
Dylan-DPC Sep 21, 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
Expand Up @@ -38,6 +38,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;
Expand Down Expand Up @@ -461,6 +462,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,
Expand Down
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]);
}