Skip to content

Commit d93036a

Browse files
committed
Auto merge of #44249 - pnkfelix:debugflag-emit-end-regions, r=arielb1
Debugflag: -Z emit-end-regions Skip EndRegion emission by default. Use `-Z emit-end-regions` to reenable it. The main intent is to fix cases where `EndRegion` emission is believed to be causing excess peak memory pressure. It may also be a welcome change to people inspecting the MIR output who find the EndRegions to be a distraction. (In later follow-up PR's I will put in safe-guards against using the current mir-borrowck without enabling `EndRegion` emission. But I wanted this PR to be minimal, in part because we may wish to backport it to the beta channel if we find that it reduces peak memory usage significantly.)
2 parents d7d75ef + f2892ad commit d93036a

15 files changed

+39
-26
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
919919
"when debug-printing compiler state, do not include spans"), // o/w tests have closure@path
920920
identify_regions: bool = (false, parse_bool, [UNTRACKED],
921921
"make unnamed regions display as '# (where # is some non-ident unique id)"),
922+
emit_end_regions: bool = (false, parse_bool, [UNTRACKED],
923+
"emit EndRegion as part of MIR; enable transforms that solely process EndRegion"),
922924
borrowck_mir: bool = (false, parse_bool, [UNTRACKED],
923925
"implicitly treat functions as if they have `#[rustc_mir_borrowck]` attribute"),
924926
time_passes: bool = (false, parse_bool, [UNTRACKED],

src/librustc/session/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ impl Session {
410410
pub fn print_llvm_passes(&self) -> bool {
411411
self.opts.debugging_opts.print_llvm_passes
412412
}
413+
pub fn emit_end_regions(&self) -> bool {
414+
self.opts.debugging_opts.emit_end_regions ||
415+
(self.opts.debugging_opts.mir_emit_validate > 0)
416+
}
413417
pub fn lto(&self) -> bool {
414418
self.opts.cg.lto
415419
}

src/librustc_mir/build/cfg.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use build::CFG;
1717
use rustc::middle::region;
1818
use rustc::mir::*;
19+
use rustc::ty;
1920

2021
impl<'tcx> CFG<'tcx> {
2122
pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
@@ -44,14 +45,17 @@ impl<'tcx> CFG<'tcx> {
4445
self.block_data_mut(block).statements.push(statement);
4546
}
4647

47-
pub fn push_end_region(&mut self,
48-
block: BasicBlock,
49-
source_info: SourceInfo,
50-
region_scope: region::Scope) {
51-
self.push(block, Statement {
52-
source_info,
53-
kind: StatementKind::EndRegion(region_scope),
54-
});
48+
pub fn push_end_region<'a, 'gcx:'a+'tcx>(&mut self,
49+
tcx: ty::TyCtxt<'a, 'gcx, 'tcx>,
50+
block: BasicBlock,
51+
source_info: SourceInfo,
52+
region_scope: region::Scope) {
53+
if tcx.sess.emit_end_regions() {
54+
self.push(block, Statement {
55+
source_info,
56+
kind: StatementKind::EndRegion(region_scope),
57+
});
58+
}
5559
}
5660

5761
pub fn push_assign(&mut self,

src/librustc_mir/build/scope.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ should go to.
8989

9090
use build::{BlockAnd, BlockAndExtension, Builder, CFG};
9191
use rustc::middle::region;
92-
use rustc::ty::Ty;
92+
use rustc::ty::{Ty, TyCtxt};
9393
use rustc::mir::*;
9494
use rustc::mir::transform::MirSource;
9595
use syntax_pos::{Span};
@@ -359,7 +359,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
359359
self.arg_count,
360360
false));
361361

362-
self.cfg.push_end_region(block, region_scope.1, scope.region_scope);
362+
self.cfg.push_end_region(self.hir.tcx(), block, region_scope.1, scope.region_scope);
363363
block.unit()
364364
}
365365

@@ -414,7 +414,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
414414
false));
415415

416416
// End all regions for scopes out of which we are breaking.
417-
self.cfg.push_end_region(block, region_scope.1, scope.region_scope);
417+
self.cfg.push_end_region(self.hir.tcx(), block, region_scope.1, scope.region_scope);
418418
}
419419
}
420420
let scope = &self.scopes[len - scope_count];
@@ -463,7 +463,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
463463
true));
464464

465465
// End all regions for scopes out of which we are breaking.
466-
self.cfg.push_end_region(block, src_info, scope.region_scope);
466+
self.cfg.push_end_region(self.hir.tcx(), block, src_info, scope.region_scope);
467467
}
468468

469469
self.cfg.terminate(block, src_info, TerminatorKind::GeneratorDrop);
@@ -694,7 +694,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
694694
};
695695

696696
for scope in scopes.iter_mut() {
697-
target = build_diverge_scope(cfg, scope.region_scope_span,
697+
target = build_diverge_scope(self.hir.tcx(), cfg, scope.region_scope_span,
698698
scope, target, generator_drop);
699699
}
700700
Some(target)
@@ -831,7 +831,8 @@ fn build_scope_drops<'tcx>(cfg: &mut CFG<'tcx>,
831831
block.unit()
832832
}
833833

834-
fn build_diverge_scope<'a, 'gcx, 'tcx>(cfg: &mut CFG<'tcx>,
834+
fn build_diverge_scope<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
835+
cfg: &mut CFG<'tcx>,
835836
span: Span,
836837
scope: &mut Scope<'tcx>,
837838
mut target: BasicBlock,
@@ -893,7 +894,7 @@ fn build_diverge_scope<'a, 'gcx, 'tcx>(cfg: &mut CFG<'tcx>,
893894
// becomes trivial goto after pass that removes all EndRegions.)
894895
{
895896
let block = cfg.start_new_cleanup_block();
896-
cfg.push_end_region(block, source_info(span), scope.region_scope);
897+
cfg.push_end_region(tcx, block, source_info(span), scope.region_scope);
897898
cfg.terminate(block, source_info(span), TerminatorKind::Goto { target: target });
898899
target = block
899900
}

src/librustc_mir/transform/clean_end_regions.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ struct DeleteTrivialEndRegions<'a> {
3939

4040
impl MirPass for CleanEndRegions {
4141
fn run_pass<'a, 'tcx>(&self,
42-
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
42+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
4343
_source: MirSource,
4444
mir: &mut Mir<'tcx>) {
45+
if !tcx.sess.emit_end_regions() { return; }
46+
4547
let mut gather = GatherBorrowedRegions {
4648
seen_regions: FxHashSet()
4749
};

src/test/mir-opt/end_region_1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions
11+
// compile-flags: -Z identify_regions -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// This is just about the simplest program that exhibits an EndRegion.

src/test/mir-opt/end_region_2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions
11+
// compile-flags: -Z identify_regions -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// We will EndRegion for borrows in a loop that occur before break but

src/test/mir-opt/end_region_3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions
11+
// compile-flags: -Z identify_regions -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// Binding the borrow's subject outside the loop does not increase the

src/test/mir-opt/end_region_4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions
11+
// compile-flags: -Z identify_regions -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// Unwinding should EndRegion for in-scope borrows: Direct borrows.

src/test/mir-opt/end_region_5.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions -Z span_free_formats
11+
// compile-flags: -Z identify_regions -Z span_free_formats -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// Unwinding should EndRegion for in-scope borrows: Borrowing via by-ref closure.

src/test/mir-opt/end_region_6.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions -Z span_free_formats
11+
// compile-flags: -Z identify_regions -Z span_free_formats -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// Unwinding should EndRegion for in-scope borrows: 2nd borrow within by-ref closure.

src/test/mir-opt/end_region_7.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions -Z span_free_formats
11+
// compile-flags: -Z identify_regions -Z span_free_formats -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// Unwinding should EndRegion for in-scope borrows: Borrow of moved data.

src/test/mir-opt/end_region_8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions -Z span_free_formats
11+
// compile-flags: -Z identify_regions -Z span_free_formats -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// Unwinding should EndRegion for in-scope borrows: Move of borrow into closure.

src/test/mir-opt/end_region_9.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions -Z span_free_formats
11+
// compile-flags: -Z identify_regions -Z span_free_formats -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// This test models a scenario that arielb1 found during review.

src/test/mir-opt/issue-43457.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z identify_regions -Z span_free_formats
11+
// compile-flags: -Z identify_regions -Z span_free_formats -Z emit-end-regions
1212
// ignore-tidy-linelength
1313

1414
// Regression test for #43457: an `EndRegion` was missing from output

0 commit comments

Comments
 (0)