Skip to content

Commit c3364a2

Browse files
perf: Don't track specific live points for promoteds
We don't query this information out of the promoted (it's basically a single "unit" regardless of the complexity within it) and this saves on re-initializing the SparseIntervalMatrix's backing IndexVec with mostly empty rows for all of the leading regions in the function. Typical promoteds will only contain a few regions that need up be uplifted, while the parent function can have thousands. For a simple function repeating println!("Hello world"); 50,000 times this reduces compile times from 90 to 15 seconds in debug mode. The previous implementations re-initialization led to an overall roughly n^2 runtime as each promoted initialized slots for ~n regions, now we scale closer to linearly (5000 hello worlds takes 1.1 seconds).
1 parent 88189a7 commit c3364a2

File tree

2 files changed

+87
-30
lines changed

2 files changed

+87
-30
lines changed

compiler/rustc_borrowck/src/region_infer/values.rs

+72-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![deny(rustc::untranslatable_diagnostic)]
22
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use rustc_data_structures::fx::FxHashSet;
34
use rustc_data_structures::fx::FxIndexSet;
45
use rustc_index::bit_set::SparseBitMatrix;
56
use rustc_index::interval::IntervalSet;
@@ -41,8 +42,15 @@ pub(crate) struct LivenessValues {
4142
/// The map from locations to points.
4243
elements: Rc<DenseLocationMap>,
4344

45+
/// Which regions are live. This is exclusive with the fine-grained tracking in `points`, and
46+
/// currently only used for validating promoteds (which don't care about more precise tracking).
47+
live_regions: Option<FxHashSet<RegionVid>>,
48+
4449
/// For each region: the points where it is live.
45-
points: SparseIntervalMatrix<RegionVid, PointIndex>,
50+
///
51+
/// This is not initialized for promoteds, because we don't care *where* within a promoted a
52+
/// region is live, only that it is.
53+
points: Option<SparseIntervalMatrix<RegionVid, PointIndex>>,
4654

4755
/// When using `-Zpolonius=next`, for each point: the loans flowing into the live regions at
4856
/// that point.
@@ -71,24 +79,52 @@ impl LiveLoans {
7179

7280
impl LivenessValues {
7381
/// Create an empty map of regions to locations where they're live.
74-
pub(crate) fn new(elements: Rc<DenseLocationMap>) -> Self {
82+
pub(crate) fn with_specific_points(elements: Rc<DenseLocationMap>) -> Self {
7583
LivenessValues {
76-
points: SparseIntervalMatrix::new(elements.num_points()),
84+
live_regions: None,
85+
points: Some(SparseIntervalMatrix::new(elements.num_points())),
86+
elements,
87+
loans: None,
88+
}
89+
}
90+
91+
/// Create an empty map of regions to locations where they're live.
92+
///
93+
/// Unlike `with_specific_points`, does not track exact locations where something is live, only
94+
/// which regions are live.
95+
pub(crate) fn without_specific_points(elements: Rc<DenseLocationMap>) -> Self {
96+
LivenessValues {
97+
live_regions: Some(Default::default()),
98+
points: None,
7799
elements,
78100
loans: None,
79101
}
80102
}
81103

82104
/// Iterate through each region that has a value in this set.
83-
pub(crate) fn regions(&self) -> impl Iterator<Item = RegionVid> {
84-
self.points.rows()
105+
pub(crate) fn regions(&self) -> impl Iterator<Item = RegionVid> + '_ {
106+
self.points.as_ref().expect("use with_specific_points").rows()
107+
}
108+
109+
/// Iterate through each region that has a value in this set.
110+
// We are passing query instability implications to the caller.
111+
#[rustc_lint_query_instability]
112+
#[allow(rustc::potential_query_instability)]
113+
pub(crate) fn live_regions_unordered(&self) -> impl Iterator<Item = RegionVid> + '_ {
114+
self.live_regions.as_ref().unwrap().iter().copied()
85115
}
86116

87117
/// Records `region` as being live at the given `location`.
88118
pub(crate) fn add_location(&mut self, region: RegionVid, location: Location) {
89-
debug!("LivenessValues::add_location(region={:?}, location={:?})", region, location);
90119
let point = self.elements.point_from_location(location);
91-
self.points.insert(region, point);
120+
debug!("LivenessValues::add_location(region={:?}, location={:?})", region, location);
121+
if let Some(points) = &mut self.points {
122+
points.insert(region, point);
123+
} else {
124+
if self.elements.point_in_range(point) {
125+
self.live_regions.as_mut().unwrap().insert(region);
126+
}
127+
}
92128

93129
// When available, record the loans flowing into this region as live at the given point.
94130
if let Some(loans) = self.loans.as_mut() {
@@ -101,7 +137,13 @@ impl LivenessValues {
101137
/// Records `region` as being live at all the given `points`.
102138
pub(crate) fn add_points(&mut self, region: RegionVid, points: &IntervalSet<PointIndex>) {
103139
debug!("LivenessValues::add_points(region={:?}, points={:?})", region, points);
104-
self.points.union_row(region, points);
140+
if let Some(this) = &mut self.points {
141+
this.union_row(region, points);
142+
} else {
143+
if points.iter().any(|point| self.elements.point_in_range(point)) {
144+
self.live_regions.as_mut().unwrap().insert(region);
145+
}
146+
}
105147

106148
// When available, record the loans flowing into this region as live at the given points.
107149
if let Some(loans) = self.loans.as_mut() {
@@ -117,23 +159,33 @@ impl LivenessValues {
117159

118160
/// Records `region` as being live at all the control-flow points.
119161
pub(crate) fn add_all_points(&mut self, region: RegionVid) {
120-
self.points.insert_all_into_row(region);
162+
if let Some(points) = &mut self.points {
163+
points.insert_all_into_row(region);
164+
} else {
165+
self.live_regions.as_mut().unwrap().insert(region);
166+
}
121167
}
122168

123169
/// Returns whether `region` is marked live at the given `location`.
124170
pub(crate) fn is_live_at(&self, region: RegionVid, location: Location) -> bool {
125171
let point = self.elements.point_from_location(location);
126-
self.points.row(region).is_some_and(|r| r.contains(point))
127-
}
128-
129-
/// Returns whether `region` is marked live at any location.
130-
pub(crate) fn is_live_anywhere(&self, region: RegionVid) -> bool {
131-
self.live_points(region).next().is_some()
172+
if let Some(points) = &self.points {
173+
points.row(region).is_some_and(|r| r.contains(point))
174+
} else {
175+
unreachable!(
176+
"Should be using LivenessValues::with_specific_points to ask whether live at a location"
177+
)
178+
}
132179
}
133180

134181
/// Returns an iterator of all the points where `region` is live.
135182
fn live_points(&self, region: RegionVid) -> impl Iterator<Item = PointIndex> + '_ {
136-
self.points
183+
let Some(points) = &self.points else {
184+
unreachable!(
185+
"Should be using LivenessValues::with_specific_points to ask whether live at a location"
186+
)
187+
};
188+
points
137189
.row(region)
138190
.into_iter()
139191
.flat_map(|set| set.iter())
@@ -288,7 +340,10 @@ impl<N: Idx> RegionValues<N> {
288340
/// elements for the region `from` from `values` and add them to
289341
/// the region `to` in `self`.
290342
pub(crate) fn merge_liveness(&mut self, to: N, from: RegionVid, values: &LivenessValues) {
291-
if let Some(set) = values.points.row(from) {
343+
let Some(value_points) = &values.points else {
344+
panic!("LivenessValues must track specific points for use in merge_liveness");
345+
};
346+
if let Some(set) = value_points.row(from) {
292347
self.points.union_row(to, set);
293348
}
294349
}

compiler/rustc_borrowck/src/type_check/mod.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
141141
let mut constraints = MirTypeckRegionConstraints {
142142
placeholder_indices: PlaceholderIndices::default(),
143143
placeholder_index_to_region: IndexVec::default(),
144-
liveness_constraints: LivenessValues::new(elements.clone()),
144+
liveness_constraints: LivenessValues::with_specific_points(elements.clone()),
145145
outlives_constraints: OutlivesConstraintSet::default(),
146146
member_constraints: MemberConstraintSet::default(),
147147
type_tests: Vec::default(),
@@ -555,7 +555,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
555555
let all_facts = &mut None;
556556
let mut constraints = Default::default();
557557
let mut liveness_constraints =
558-
LivenessValues::new(Rc::new(DenseLocationMap::new(promoted_body)));
558+
LivenessValues::without_specific_points(Rc::new(DenseLocationMap::new(promoted_body)));
559559
// Don't try to add borrow_region facts for the promoted MIR
560560

561561
let mut swap_constraints = |this: &mut Self| {
@@ -594,17 +594,19 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
594594
}
595595
self.cx.borrowck_context.constraints.outlives_constraints.push(constraint)
596596
}
597-
for region in liveness_constraints.regions() {
598-
// If the region is live at at least one location in the promoted MIR,
599-
// then add a liveness constraint to the main MIR for this region
600-
// at the location provided as an argument to this method
601-
if liveness_constraints.is_live_anywhere(region) {
602-
self.cx
603-
.borrowck_context
604-
.constraints
605-
.liveness_constraints
606-
.add_location(region, location);
607-
}
597+
// If the region is live at at least one location in the promoted MIR,
598+
// then add a liveness constraint to the main MIR for this region
599+
// at the location provided as an argument to this method
600+
//
601+
// add_location doesn't care about ordering so not a problem for the live regions to be
602+
// unordered.
603+
#[allow(rustc::potential_query_instability)]
604+
for region in liveness_constraints.live_regions_unordered() {
605+
self.cx
606+
.borrowck_context
607+
.constraints
608+
.liveness_constraints
609+
.add_location(region, location);
608610
}
609611
}
610612

0 commit comments

Comments
 (0)