Skip to content

Commit bcb064a

Browse files
committed
Auto merge of rust-lang#107406 - cjgillot:eliminate-witnesses, r=compiler-errors
Only compute mir_generator_witnesses query in drop_tracking_mir mode. Attempt to fix the perf regression in rust-lang#101692 r? `@ghost`
2 parents 9f82651 + 4db4860 commit bcb064a

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14151415
if encode_opt {
14161416
record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id));
14171417

1418-
if let DefKind::Generator = self.tcx.def_kind(def_id) {
1418+
if let DefKind::Generator = self.tcx.def_kind(def_id) && tcx.sess.opts.unstable_opts.drop_tracking_mir {
14191419
record!(self.tables.mir_generator_witnesses[def_id.to_def_id()] <- tcx.mir_generator_witnesses(def_id));
14201420
}
14211421
}

compiler/rustc_mir_transform/src/generator.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,7 @@ pub(crate) fn mir_generator_witnesses<'tcx>(
13901390
tcx: TyCtxt<'tcx>,
13911391
def_id: DefId,
13921392
) -> GeneratorLayout<'tcx> {
1393+
assert!(tcx.sess.opts.unstable_opts.drop_tracking_mir);
13931394
let def_id = def_id.expect_local();
13941395

13951396
let (body, _) = tcx.mir_promoted(ty::WithOptConstParam::unknown(def_id));
@@ -1400,15 +1401,8 @@ pub(crate) fn mir_generator_witnesses<'tcx>(
14001401
let gen_ty = body.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty;
14011402

14021403
// Get the interior types and substs which typeck computed
1403-
let (upvars, interior, movable) = match *gen_ty.kind() {
1404-
ty::Generator(_, substs, movability) => {
1405-
let substs = substs.as_generator();
1406-
(
1407-
substs.upvar_tys().collect::<Vec<_>>(),
1408-
substs.witness(),
1409-
movability == hir::Movability::Movable,
1410-
)
1411-
}
1404+
let movable = match *gen_ty.kind() {
1405+
ty::Generator(_, _, movability) => movability == hir::Movability::Movable,
14121406
_ => span_bug!(body.span, "unexpected generator type {}", gen_ty),
14131407
};
14141408

@@ -1422,11 +1416,7 @@ pub(crate) fn mir_generator_witnesses<'tcx>(
14221416
// `storage_liveness` tells us which locals have live storage at suspension points
14231417
let (_, generator_layout, _) = compute_layout(tcx, liveness_info, body);
14241418

1425-
if tcx.sess.opts.unstable_opts.drop_tracking_mir {
1426-
check_suspend_tys(tcx, &generator_layout, &body);
1427-
} else {
1428-
sanitize_witness(tcx, body, interior, upvars, &generator_layout);
1429-
}
1419+
check_suspend_tys(tcx, &generator_layout, &body);
14301420

14311421
generator_layout
14321422
}
@@ -1444,10 +1434,15 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
14441434
let gen_ty = body.local_decls.raw[1].ty;
14451435

14461436
// Get the discriminant type and substs which typeck computed
1447-
let (discr_ty, movable) = match *gen_ty.kind() {
1437+
let (discr_ty, upvars, interior, movable) = match *gen_ty.kind() {
14481438
ty::Generator(_, substs, movability) => {
14491439
let substs = substs.as_generator();
1450-
(substs.discr_ty(tcx), movability == hir::Movability::Movable)
1440+
(
1441+
substs.discr_ty(tcx),
1442+
substs.upvar_tys().collect::<Vec<_>>(),
1443+
substs.witness(),
1444+
movability == hir::Movability::Movable,
1445+
)
14511446
}
14521447
_ => {
14531448
tcx.sess
@@ -1524,6 +1519,12 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
15241519
// `storage_liveness` tells us which locals have live storage at suspension points
15251520
let (remap, layout, storage_liveness) = compute_layout(tcx, liveness_info, body);
15261521

1522+
if tcx.sess.opts.unstable_opts.validate_mir
1523+
&& !tcx.sess.opts.unstable_opts.drop_tracking_mir
1524+
{
1525+
sanitize_witness(tcx, body, interior, upvars, &layout);
1526+
}
1527+
15271528
let can_return = can_return(tcx, body, tcx.param_env(body.source.def_id()));
15281529

15291530
// Run the transformation which converts Places from Local to generator struct

compiler/rustc_mir_transform/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fn mir_drops_elaborated_and_const_checked(
426426
return tcx.mir_drops_elaborated_and_const_checked(def);
427427
}
428428

429-
if tcx.generator_kind(def.did).is_some() {
429+
if tcx.generator_kind(def.did).is_some() && tcx.sess.opts.unstable_opts.drop_tracking_mir {
430430
tcx.ensure().mir_generator_witnesses(def.did);
431431
}
432432
let mir_borrowck = tcx.mir_borrowck_opt_const_arg(def);

0 commit comments

Comments
 (0)