Skip to content

Commit 0531bcb

Browse files
committed
erase coroutine shim dereftemps
1 parent d93c1f9 commit 0531bcb

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode, Obliga
9393
use tracing::{debug, instrument, trace};
9494

9595
use crate::deref_separator::deref_finder;
96+
use crate::erase_deref_temps::deref_temp_eraser;
9697
use crate::{abort_unwinding_calls, errors, pass_manager as pm, simplify};
9798

9899
pub(super) struct StateTransform;
@@ -1622,27 +1623,30 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
16221623
let mut drop_shim =
16231624
create_coroutine_drop_shim_async(tcx, &transform, body, drop_clean, can_unwind);
16241625
// Run derefer to fix Derefs that are not in the first place
1625-
deref_finder(tcx, &mut drop_shim);
1626+
deref_finder(tcx, &mut drop_shim, false);
1627+
deref_temp_eraser(tcx, &mut drop_shim);
16261628
body.coroutine.as_mut().unwrap().coroutine_drop_async = Some(drop_shim);
16271629
} else {
16281630
// If coroutine has no async drops, generating sync drop shim
16291631
let mut drop_shim =
16301632
create_coroutine_drop_shim(tcx, &transform, coroutine_ty, body, drop_clean);
16311633
// Run derefer to fix Derefs that are not in the first place
1632-
deref_finder(tcx, &mut drop_shim);
1634+
deref_finder(tcx, &mut drop_shim, false);
1635+
deref_temp_eraser(tcx, &mut drop_shim);
16331636
body.coroutine.as_mut().unwrap().coroutine_drop = Some(drop_shim);
16341637

16351638
// For coroutine with sync drop, generating async proxy for `future_drop_poll` call
16361639
let mut proxy_shim = create_coroutine_drop_shim_proxy_async(tcx, body);
1637-
deref_finder(tcx, &mut proxy_shim);
1640+
deref_finder(tcx, &mut proxy_shim, false);
1641+
deref_temp_eraser(tcx, &mut proxy_shim);
16381642
body.coroutine.as_mut().unwrap().coroutine_drop_proxy_async = Some(proxy_shim);
16391643
}
16401644

16411645
// Create the Coroutine::resume / Future::poll function
16421646
create_coroutine_resume_function(tcx, transform, body, can_return, can_unwind);
16431647

16441648
// Run derefer to fix Derefs that are not in the first place
1645-
deref_finder(tcx, body);
1649+
deref_finder(tcx, body, true);
16461650
}
16471651

16481652
fn is_required(&self) -> bool {

compiler/rustc_mir_transform/src/deref_separator.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ use rustc_middle::ty::TyCtxt;
66
use crate::patch::MirPatch;
77

88
pub(super) struct Derefer;
9+
pub(super) struct DereferErased;
910

1011
struct DerefChecker<'a, 'tcx> {
1112
tcx: TyCtxt<'tcx>,
1213
patcher: MirPatch<'tcx>,
1314
local_decls: &'a LocalDecls<'tcx>,
15+
add_deref_metadata: bool,
1416
}
1517

1618
impl<'a, 'tcx> MutVisitor<'tcx> for DerefChecker<'a, 'tcx> {
@@ -39,7 +41,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for DerefChecker<'a, 'tcx> {
3941
let temp = self.patcher.new_local_with_info(
4042
ty,
4143
self.local_decls[p_ref.local].source_info.span,
42-
LocalInfo::DerefTemp,
44+
if self.add_deref_metadata {
45+
LocalInfo::DerefTemp
46+
} else {
47+
LocalInfo::Boring
48+
},
4349
);
4450

4551
// We are adding current p_ref's projections to our
@@ -50,7 +56,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for DerefChecker<'a, 'tcx> {
5056
self.patcher.add_assign(
5157
loc,
5258
Place::from(temp),
53-
Rvalue::CopyForDeref(deref_place),
59+
if self.add_deref_metadata {
60+
Rvalue::CopyForDeref(deref_place)
61+
} else {
62+
Rvalue::Use(Operand::Copy(deref_place))
63+
},
5464
);
5565
place_local = temp;
5666
last_len = p_ref.projection.len();
@@ -67,9 +77,14 @@ impl<'a, 'tcx> MutVisitor<'tcx> for DerefChecker<'a, 'tcx> {
6777
}
6878
}
6979

70-
pub(super) fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
80+
pub(super) fn deref_finder<'tcx>(
81+
tcx: TyCtxt<'tcx>,
82+
body: &mut Body<'tcx>,
83+
add_deref_metadata: bool,
84+
) {
7185
let patch = MirPatch::new(body);
72-
let mut checker = DerefChecker { tcx, patcher: patch, local_decls: &body.local_decls };
86+
let mut checker =
87+
DerefChecker { tcx, patcher: patch, local_decls: &body.local_decls, add_deref_metadata };
7388

7489
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
7590
checker.visit_basic_block_data(bb, data);
@@ -80,7 +95,17 @@ pub(super) fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8095

8196
impl<'tcx> crate::MirPass<'tcx> for Derefer {
8297
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
83-
deref_finder(tcx, body);
98+
deref_finder(tcx, body, true);
99+
}
100+
101+
fn is_required(&self) -> bool {
102+
true
103+
}
104+
}
105+
106+
impl<'tcx> crate::MirPass<'tcx> for DereferErased {
107+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
108+
deref_finder(tcx, body, false);
84109
}
85110

86111
fn is_required(&self) -> bool {

compiler/rustc_mir_transform/src/elaborate_drops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateDrops {
8787
.elaborate()
8888
};
8989
elaborate_patch.apply(body);
90-
deref_finder(tcx, body);
90+
deref_finder(tcx, body, true);
9191
}
9292

9393
fn is_required(&self) -> bool {

compiler/rustc_mir_transform/src/erase_deref_temps.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ impl<'tcx> MutVisitor<'tcx> for EraseDerefTempsVisitor<'tcx> {
2828
}
2929
}
3030

31+
pub(super) fn deref_temp_eraser<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
32+
EraseDerefTempsVisitor { tcx }.visit_body_preserves_cfg(body);
33+
}
34+
3135
pub(super) struct EraseDerefTemps;
3236

3337
impl<'tcx> crate::MirPass<'tcx> for EraseDerefTemps {
3438
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
35-
EraseDerefTempsVisitor { tcx }.visit_body_preserves_cfg(body);
39+
deref_temp_eraser(tcx, body);
3640
}
3741

3842
fn is_required(&self) -> bool {

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'tcx> crate::MirPass<'tcx> for Inline {
6464
if inline::<NormalInliner<'tcx>>(tcx, body) {
6565
debug!("running simplify cfg on {:?}", body.source);
6666
simplify_cfg(tcx, body);
67-
deref_finder(tcx, body);
67+
deref_finder(tcx, body, false);
6868
}
6969
}
7070

@@ -100,7 +100,7 @@ impl<'tcx> crate::MirPass<'tcx> for ForceInline {
100100
if inline::<ForceInliner<'tcx>>(tcx, body) {
101101
debug!("running simplify cfg on {:?}", body.source);
102102
simplify_cfg(tcx, body);
103-
deref_finder(tcx, body);
103+
deref_finder(tcx, body, false);
104104
}
105105
}
106106
}

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ declare_passes! {
133133
Initial,
134134
Final
135135
};
136-
mod deref_separator : Derefer;
136+
mod deref_separator : Derefer, DereferErased;
137137
mod dest_prop : DestinationPropagation;
138138
pub mod dump_mir : Marker;
139139
mod early_otherwise_branch : EarlyOtherwiseBranch;

compiler/rustc_mir_transform/src/shim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
232232
&[
233233
&mentioned_items::MentionedItems,
234234
&add_moves_for_packed_drops::AddMovesForPackedDrops,
235-
&deref_separator::Derefer,
235+
&deref_separator::DereferErased,
236236
&remove_noop_landing_pads::RemoveNoopLandingPads,
237237
&simplify::SimplifyCfg::MakeShim,
238238
&instsimplify::InstSimplify::BeforeInline,

0 commit comments

Comments
 (0)