Skip to content

Commit e63b992

Browse files
committed
Resolve PR comments
1 parent 9fdf5b5 commit e63b992

File tree

6 files changed

+81
-131
lines changed

6 files changed

+81
-131
lines changed

src/librustc_metadata/decoder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,11 @@ impl<'a, 'tcx> CrateMetadata {
924924
}
925925
}
926926

927-
pub fn maybe_get_promoted_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) ->
928-
Option<IndexVec<Promoted, Body<'tcx>>> {
927+
pub fn maybe_get_promoted_mir(
928+
&self,
929+
tcx: TyCtxt<'tcx>,
930+
id: DefIndex,
931+
) -> Option<IndexVec<Promoted, Body<'tcx>>> {
929932
match self.is_proc_macro(id) {
930933
true => None,
931934
false => self.entry(id).promoted_mir.map(|promoted| promoted.decode((self, tcx)),)

src/librustc_metadata/encoder.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,10 @@ impl EncodeContext<'tcx> {
10601060
}
10611061
}
10621062

1063-
fn encode_promoted_mir(&mut self, def_id: DefId) ->
1064-
Option<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>> {
1063+
fn encode_promoted_mir(
1064+
&mut self,
1065+
def_id: DefId,
1066+
) -> Option<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>> {
10651067
debug!("EncodeContext::encode_promoted_mir({:?})", def_id);
10661068
if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) {
10671069
let promoted = self.tcx.promoted_mir(def_id);

src/librustc_mir/borrow_check/nll/renumber.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ pub fn renumber_mir<'tcx>(
1616
debug!("renumber_mir: body.arg_count={:?}", body.arg_count);
1717

1818
let mut visitor = NLLVisitor { infcx };
19-
visitor.visit_promoted(promoted);
19+
20+
for body in promoted.iter_mut() {
21+
visitor.visit_body(body);
22+
}
23+
2024
visitor.visit_body(body);
2125
}
2226

@@ -47,13 +51,6 @@ impl<'a, 'tcx> NLLVisitor<'a, 'tcx> {
4751
{
4852
renumber_regions(self.infcx, value)
4953
}
50-
51-
fn visit_promoted(&mut self, promoted: &mut IndexVec<Promoted, Body<'tcx>>) {
52-
debug!("visiting promoted mir");
53-
for body in promoted.iter_mut() {
54-
self.visit_body(body);
55-
}
56-
}
5754
}
5855

5956
impl<'a, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'tcx> {

src/librustc_mir/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx
112112
};
113113
debug!("make_shim({:?}) = untransformed {:?}", instance, result);
114114

115-
run_passes(tcx, &mut result, instance, MirPhase::Const, &[
115+
run_passes(tcx, &mut result, instance, None, MirPhase::Const, &[
116116
&add_moves_for_packed_drops::AddMovesForPackedDrops,
117117
&no_landing_pads::NoLandingPads,
118118
&remove_noop_landing_pads::RemoveNoopLandingPads,

src/librustc_mir/transform/mod.rs

+55-98
Original file line numberDiff line numberDiff line change
@@ -149,49 +149,46 @@ pub fn run_passes(
149149
tcx: TyCtxt<'tcx>,
150150
body: &mut Body<'tcx>,
151151
instance: InstanceDef<'tcx>,
152+
promoted: Option<Promoted>,
152153
mir_phase: MirPhase,
153154
passes: &[&dyn MirPass<'tcx>],
154155
) {
155156
let phase_index = mir_phase.phase_index();
156157

157-
let run_passes = |body: &mut Body<'tcx>, promoted| {
158-
if body.phase >= mir_phase {
159-
return;
160-
}
158+
if body.phase >= mir_phase {
159+
return;
160+
}
161161

162-
let source = MirSource {
163-
instance,
164-
promoted,
165-
};
166-
let mut index = 0;
167-
let mut run_pass = |pass: &dyn MirPass<'tcx>| {
168-
let run_hooks = |body: &_, index, is_after| {
169-
dump_mir::on_mir_pass(tcx, &format_args!("{:03}-{:03}", phase_index, index),
170-
&pass.name(), source, body, is_after);
171-
};
172-
run_hooks(body, index, false);
173-
pass.run_pass(tcx, source, body);
174-
run_hooks(body, index, true);
175-
176-
index += 1;
162+
let source = MirSource {
163+
instance,
164+
promoted,
165+
};
166+
let mut index = 0;
167+
let mut run_pass = |pass: &dyn MirPass<'tcx>| {
168+
let run_hooks = |body: &_, index, is_after| {
169+
dump_mir::on_mir_pass(tcx, &format_args!("{:03}-{:03}", phase_index, index),
170+
&pass.name(), source, body, is_after);
177171
};
172+
run_hooks(body, index, false);
173+
pass.run_pass(tcx, source, body);
174+
run_hooks(body, index, true);
178175

179-
for pass in passes {
180-
run_pass(*pass);
181-
}
182-
183-
body.phase = mir_phase;
176+
index += 1;
184177
};
185178

186-
run_passes(body, None);
179+
for pass in passes {
180+
run_pass(*pass);
181+
}
182+
183+
body.phase = mir_phase;
187184
}
188185

189186
fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> &Steal<Body<'_>> {
190187
// Unsafety check uses the raw mir, so make sure it is run
191188
let _ = tcx.unsafety_check_result(def_id);
192189

193190
let mut body = tcx.mir_built(def_id).steal();
194-
run_passes(tcx, &mut body, InstanceDef::Item(def_id), MirPhase::Const, &[
191+
run_passes(tcx, &mut body, InstanceDef::Item(def_id), None, MirPhase::Const, &[
195192
// What we need to do constant evaluation.
196193
&simplify::SimplifyCfg::new("initial"),
197194
&rustc_peek::SanityCheck,
@@ -213,7 +210,7 @@ fn mir_validated(
213210

214211
let mut body = tcx.mir_const(def_id).steal();
215212
let qualify_and_promote_pass = qualify_consts::QualifyAndPromoteConstants::default();
216-
run_passes(tcx, &mut body, InstanceDef::Item(def_id), MirPhase::Validated, &[
213+
run_passes(tcx, &mut body, InstanceDef::Item(def_id), None, MirPhase::Validated, &[
217214
// What we need to run borrowck etc.
218215
&qualify_and_promote_pass,
219216
&simplify::SimplifyCfg::new("qualify-consts"),
@@ -222,26 +219,13 @@ fn mir_validated(
222219
(tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
223220
}
224221

225-
fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &Body<'_> {
226-
if tcx.is_constructor(def_id) {
227-
// There's no reason to run all of the MIR passes on constructors when
228-
// we can just output the MIR we want directly. This also saves const
229-
// qualification and borrow checking the trouble of special casing
230-
// constructors.
231-
return shim::build_adt_ctor(tcx, def_id);
232-
}
233-
234-
// (Mir-)Borrowck uses `mir_validated`, so we have to force it to
235-
// execute before we can steal.
236-
tcx.ensure().mir_borrowck(def_id);
237-
238-
if tcx.use_ast_borrowck() {
239-
tcx.ensure().borrowck(def_id);
240-
}
241-
242-
let (body, _) = tcx.mir_validated(def_id);
243-
let mut body = body.steal();
244-
run_passes(tcx, &mut body, InstanceDef::Item(def_id), MirPhase::Optimized, &[
222+
fn run_optimization_passes<'tcx>(
223+
tcx: TyCtxt<'tcx>,
224+
body: &mut Body<'tcx>,
225+
def_id: DefId,
226+
promoted: Option<Promoted>,
227+
) {
228+
run_passes(tcx, body, InstanceDef::Item(def_id), promoted, MirPhase::Optimized, &[
245229
// Remove all things only needed by analysis
246230
&no_landing_pads::NoLandingPads,
247231
&simplify_branches::SimplifyBranches::new("initial"),
@@ -292,6 +276,28 @@ fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &Body<'_> {
292276
&add_call_guards::CriticalCallEdges,
293277
&dump_mir::Marker("PreCodegen"),
294278
]);
279+
}
280+
281+
fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &Body<'_> {
282+
if tcx.is_constructor(def_id) {
283+
// There's no reason to run all of the MIR passes on constructors when
284+
// we can just output the MIR we want directly. This also saves const
285+
// qualification and borrow checking the trouble of special casing
286+
// constructors.
287+
return shim::build_adt_ctor(tcx, def_id);
288+
}
289+
290+
// (Mir-)Borrowck uses `mir_validated`, so we have to force it to
291+
// execute before we can steal.
292+
tcx.ensure().mir_borrowck(def_id);
293+
294+
if tcx.use_ast_borrowck() {
295+
tcx.ensure().borrowck(def_id);
296+
}
297+
298+
let (body, _) = tcx.mir_validated(def_id);
299+
let mut body = body.steal();
300+
run_optimization_passes(tcx, &mut body, def_id, None);
295301
tcx.arena.alloc(body)
296302
}
297303

@@ -304,57 +310,8 @@ fn promoted_mir<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx IndexVec<Promot
304310
let (_, promoted) = tcx.mir_validated(def_id);
305311
let mut promoted = promoted.steal();
306312

307-
for mut body in promoted.iter_mut() {
308-
run_passes(tcx, &mut body, InstanceDef::Item(def_id), MirPhase::Optimized, &[
309-
// Remove all things only needed by analysis
310-
&no_landing_pads::NoLandingPads,
311-
&simplify_branches::SimplifyBranches::new("initial"),
312-
&remove_noop_landing_pads::RemoveNoopLandingPads,
313-
&cleanup_post_borrowck::CleanupNonCodegenStatements,
314-
315-
&simplify::SimplifyCfg::new("early-opt"),
316-
317-
// These next passes must be executed together
318-
&add_call_guards::CriticalCallEdges,
319-
&elaborate_drops::ElaborateDrops,
320-
&no_landing_pads::NoLandingPads,
321-
// AddMovesForPackedDrops needs to run after drop
322-
// elaboration.
323-
&add_moves_for_packed_drops::AddMovesForPackedDrops,
324-
// AddRetag needs to run after ElaborateDrops, and it needs
325-
// an AllCallEdges pass right before it. Otherwise it should
326-
// run fairly late, but before optimizations begin.
327-
&add_call_guards::AllCallEdges,
328-
&add_retag::AddRetag,
329-
330-
&simplify::SimplifyCfg::new("elaborate-drops"),
331-
332-
// No lifetime analysis based on borrowing can be done from here on out.
333-
334-
// From here on out, regions are gone.
335-
&erase_regions::EraseRegions,
336-
337-
// Optimizations begin.
338-
&uniform_array_move_out::RestoreSubsliceArrayMoveOut,
339-
&inline::Inline,
340-
341-
// Lowering generator control-flow and variables
342-
// has to happen before we do anything else to them.
343-
&generator::StateTransform,
344-
345-
&instcombine::InstCombine,
346-
&const_prop::ConstProp,
347-
&simplify_branches::SimplifyBranches::new("after-const-prop"),
348-
&deaggregator::Deaggregator,
349-
&copy_prop::CopyPropagation,
350-
&simplify_branches::SimplifyBranches::new("after-copy-prop"),
351-
&remove_noop_landing_pads::RemoveNoopLandingPads,
352-
&simplify::SimplifyCfg::new("final"),
353-
&simplify::SimplifyLocals,
354-
355-
&add_call_guards::CriticalCallEdges,
356-
&dump_mir::Marker("PreCodegen"),
357-
]);
313+
for (p, mut body) in promoted.iter_enumerated_mut() {
314+
run_optimization_passes(tcx, &mut body, def_id, Some(p));
358315
}
359316

360317
tcx.intern_promoted(promoted)

src/librustc_mir/transform/promote_consts.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,17 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
304304
let mut operand = {
305305
let promoted = &mut self.promoted;
306306
let promoted_id = Promoted::new(next_promoted_id);
307-
let mut promoted_place = |ty, substs, span| {
307+
let tcx = self.tcx;
308+
let mut promoted_place = |ty, span| {
308309
promoted.span = span;
309310
promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
310311
Place {
311312
base: PlaceBase::Static(box Static {
312-
kind: StaticKind::Promoted(promoted_id, substs),
313+
kind:
314+
StaticKind::Promoted(
315+
promoted_id,
316+
InternalSubsts::identity_for_item(tcx, def_id),
317+
),
313318
ty,
314319
def_id,
315320
}),
@@ -329,11 +334,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
329334
Operand::Move(Place {
330335
base: mem::replace(
331336
&mut place.base,
332-
promoted_place(
333-
ty,
334-
InternalSubsts::identity_for_item(self.tcx, def_id),
335-
span,
336-
).base
337+
promoted_place(ty, span).base
337338
),
338339
projection: None,
339340
})
@@ -349,13 +350,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
349350
let span = statement.source_info.span;
350351
mem::replace(
351352
operand,
352-
Operand::Copy(
353-
promoted_place(
354-
ty,
355-
InternalSubsts::identity_for_item(self.tcx, def_id),
356-
span,
357-
)
358-
)
353+
Operand::Copy(promoted_place(ty, span))
359354
)
360355
}
361356
_ => bug!()
@@ -367,12 +362,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
367362
TerminatorKind::Call { ref mut args, .. } => {
368363
let ty = args[index].ty(local_decls, self.tcx);
369364
let span = terminator.source_info.span;
370-
let operand =
371-
Operand::Copy(
372-
promoted_place(
373-
ty,
374-
InternalSubsts::identity_for_item(self.tcx, def_id),
375-
span));
365+
let operand = Operand::Copy(promoted_place(ty, span));
376366
mem::replace(&mut args[index], operand)
377367
}
378368
// We expected a `TerminatorKind::Call` for which we'd like to promote an
@@ -472,6 +462,7 @@ pub fn promote_candidates<'tcx>(
472462
keep_original: false
473463
};
474464

465+
//FIXME(oli-obk): having a `maybe_push()` method on `IndexVec` might be nice
475466
if let Some(promoted) = promoter.promote_candidate(def_id, candidate, promotions.len()) {
476467
promotions.push(promoted);
477468
}

0 commit comments

Comments
 (0)