Skip to content

Commit d1db2f2

Browse files
committed
Move PointIndex to mir_dataflow.
1 parent 26089ba commit d1db2f2

15 files changed

+211
-197
lines changed

compiler/rustc_borrowck/src/constraint_generation.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ use rustc_middle::mir::{
1010
use rustc_middle::ty::visit::TypeVisitable;
1111
use rustc_middle::ty::GenericArgsRef;
1212
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
13+
use rustc_mir_dataflow::points::LivenessValues;
1314

14-
use crate::{
15-
borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, places_conflict,
16-
region_infer::values::LivenessValues,
17-
};
15+
use crate::{borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, places_conflict};
1816

1917
pub(super) fn generate_constraints<'tcx>(
2018
infcx: &InferCtxt<'tcx>,

compiler/rustc_borrowck/src/nll.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_middle::mir::{
1111
START_BLOCK,
1212
};
1313
use rustc_middle::ty::{self, OpaqueHiddenType, TyCtxt};
14+
use rustc_mir_dataflow::points::DenseLocationMap;
1415
use rustc_span::symbol::sym;
1516
use std::env;
1617
use std::io;
@@ -32,7 +33,7 @@ use crate::{
3233
facts::{AllFacts, AllFactsExt, RustcFacts},
3334
invalidation,
3435
location::LocationTable,
35-
region_infer::{values::RegionValueElements, RegionInferenceContext},
36+
region_infer::RegionInferenceContext,
3637
renumber,
3738
type_check::{self, MirTypeckRegionConstraints, MirTypeckResults},
3839
universal_regions::UniversalRegions,
@@ -177,7 +178,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
177178

178179
let universal_regions = Rc::new(universal_regions);
179180

180-
let elements = &Rc::new(RegionValueElements::new(&body));
181+
let elements = &Rc::new(DenseLocationMap::new(&body));
181182

182183
// Run the MIR type-checker.
183184
let MirTypeckResults { constraints, universal_region_relations, opaque_type_values } =

compiler/rustc_borrowck/src/region_infer/dump_mir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
6767
with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
6868
) -> io::Result<()> {
6969
for region in self.definitions.indices() {
70-
let value = self.liveness_constraints.region_value_str(region);
70+
let value = self.region_value_str(region);
7171
if value != "{}" {
7272
with_msg(&format!("{region:?} live at {value}"))?;
7373
}

compiler/rustc_borrowck/src/region_infer/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::mir::{
1919
use rustc_middle::traits::ObligationCause;
2020
use rustc_middle::traits::ObligationCauseCode;
2121
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
22+
use rustc_mir_dataflow::points::{DenseLocationMap, LivenessValues};
2223
use rustc_span::Span;
2324

2425
use crate::{
@@ -29,10 +30,7 @@ use crate::{
2930
member_constraints::{MemberConstraintSet, NllMemberConstraintIndex},
3031
nll::PoloniusOutput,
3132
region_infer::reverse_sccs::ReverseSccGraph,
32-
region_infer::values::{
33-
LivenessValues, PlaceholderIndices, RegionElement, RegionValueElements, RegionValues,
34-
ToElementIndex,
35-
},
33+
region_infer::values::{PlaceholderIndices, RegionElement, RegionValues, ToElementIndex},
3634
type_check::{free_region_relations::UniversalRegionRelations, Locations},
3735
universal_regions::UniversalRegions,
3836
BorrowckInferCtxt,
@@ -329,7 +327,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
329327
universe_causes: FxIndexMap<ty::UniverseIndex, UniverseInfo<'tcx>>,
330328
type_tests: Vec<TypeTest<'tcx>>,
331329
liveness_constraints: LivenessValues<RegionVid>,
332-
elements: &Rc<RegionValueElements>,
330+
elements: &Rc<DenseLocationMap>,
333331
) -> Self {
334332
debug!("universal_regions: {:#?}", universal_regions);
335333
debug!("outlives constraints: {:#?}", outlives_constraints);
@@ -1963,7 +1961,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
19631961
trace!(universe = ?self.scc_universes[self.constraint_sccs.scc(fr1)]);
19641962
self.find_constraint_paths_between_regions(fr1, |r| {
19651963
// First look for some `r` such that `fr1: r` and `r` is live at `elem`
1966-
trace!(?r, liveness_constraints=?self.liveness_constraints.region_value_str(r));
1964+
trace!(?r, liveness_constraints=?self.region_value_str(r));
19671965
self.liveness_constraints.contains(r, elem)
19681966
})
19691967
.or_else(|| {

compiler/rustc_borrowck/src/region_infer/values.rs

+7-153
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,14 @@
22
#![deny(rustc::diagnostic_outside_of_impl)]
33
use rustc_data_structures::fx::FxIndexSet;
44
use rustc_index::bit_set::SparseBitMatrix;
5-
use rustc_index::interval::IntervalSet;
65
use rustc_index::interval::SparseIntervalMatrix;
76
use rustc_index::Idx;
8-
use rustc_index::IndexVec;
9-
use rustc_middle::mir::{BasicBlock, Body, Location};
7+
use rustc_middle::mir::{BasicBlock, Location};
108
use rustc_middle::ty::{self, RegionVid};
9+
use rustc_mir_dataflow::points::{DenseLocationMap, LivenessValues, PointIndex};
1110
use std::fmt::Debug;
1211
use std::rc::Rc;
1312

14-
/// Maps between a `Location` and a `PointIndex` (and vice versa).
15-
pub(crate) struct RegionValueElements {
16-
/// For each basic block, how many points are contained within?
17-
statements_before_block: IndexVec<BasicBlock, usize>,
18-
19-
/// Map backward from each point to the basic block that it
20-
/// belongs to.
21-
basic_blocks: IndexVec<PointIndex, BasicBlock>,
22-
23-
num_points: usize,
24-
}
25-
26-
impl RegionValueElements {
27-
pub(crate) fn new(body: &Body<'_>) -> Self {
28-
let mut num_points = 0;
29-
let statements_before_block: IndexVec<BasicBlock, usize> = body
30-
.basic_blocks
31-
.iter()
32-
.map(|block_data| {
33-
let v = num_points;
34-
num_points += block_data.statements.len() + 1;
35-
v
36-
})
37-
.collect();
38-
debug!("RegionValueElements: statements_before_block={:#?}", statements_before_block);
39-
debug!("RegionValueElements: num_points={:#?}", num_points);
40-
41-
let mut basic_blocks = IndexVec::with_capacity(num_points);
42-
for (bb, bb_data) in body.basic_blocks.iter_enumerated() {
43-
basic_blocks.extend((0..=bb_data.statements.len()).map(|_| bb));
44-
}
45-
46-
Self { statements_before_block, basic_blocks, num_points }
47-
}
48-
49-
/// Total number of point indices
50-
pub(crate) fn num_points(&self) -> usize {
51-
self.num_points
52-
}
53-
54-
/// Converts a `Location` into a `PointIndex`. O(1).
55-
pub(crate) fn point_from_location(&self, location: Location) -> PointIndex {
56-
let Location { block, statement_index } = location;
57-
let start_index = self.statements_before_block[block];
58-
PointIndex::new(start_index + statement_index)
59-
}
60-
61-
/// Converts a `Location` into a `PointIndex`. O(1).
62-
pub(crate) fn entry_point(&self, block: BasicBlock) -> PointIndex {
63-
let start_index = self.statements_before_block[block];
64-
PointIndex::new(start_index)
65-
}
66-
67-
/// Return the PointIndex for the block start of this index.
68-
pub(crate) fn to_block_start(&self, index: PointIndex) -> PointIndex {
69-
PointIndex::new(self.statements_before_block[self.basic_blocks[index]])
70-
}
71-
72-
/// Converts a `PointIndex` back to a location. O(1).
73-
pub(crate) fn to_location(&self, index: PointIndex) -> Location {
74-
assert!(index.index() < self.num_points);
75-
let block = self.basic_blocks[index];
76-
let start_index = self.statements_before_block[block];
77-
let statement_index = index.index() - start_index;
78-
Location { block, statement_index }
79-
}
80-
81-
/// Sometimes we get point-indices back from bitsets that may be
82-
/// out of range (because they round up to the nearest 2^N number
83-
/// of bits). Use this function to filter such points out if you
84-
/// like.
85-
pub(crate) fn point_in_range(&self, index: PointIndex) -> bool {
86-
index.index() < self.num_points
87-
}
88-
}
89-
90-
rustc_index::newtype_index! {
91-
/// A single integer representing a `Location` in the MIR control-flow
92-
/// graph. Constructed efficiently from `RegionValueElements`.
93-
#[debug_format = "PointIndex({})"]
94-
pub struct PointIndex {}
95-
}
96-
9713
rustc_index::newtype_index! {
9814
/// A single integer representing a `ty::Placeholder`.
9915
#[debug_format = "PlaceholderIndex({})"]
@@ -116,68 +32,6 @@ pub(crate) enum RegionElement {
11632
PlaceholderRegion(ty::PlaceholderRegion),
11733
}
11834

119-
/// When we initially compute liveness, we use an interval matrix storing
120-
/// liveness ranges for each region-vid.
121-
pub(crate) struct LivenessValues<N: Idx> {
122-
elements: Rc<RegionValueElements>,
123-
points: SparseIntervalMatrix<N, PointIndex>,
124-
}
125-
126-
impl<N: Idx> LivenessValues<N> {
127-
/// Creates a new set of "region values" that tracks causal information.
128-
/// Each of the regions in num_region_variables will be initialized with an
129-
/// empty set of points and no causal information.
130-
pub(crate) fn new(elements: Rc<RegionValueElements>) -> Self {
131-
Self { points: SparseIntervalMatrix::new(elements.num_points), elements }
132-
}
133-
134-
/// Iterate through each region that has a value in this set.
135-
pub(crate) fn rows(&self) -> impl Iterator<Item = N> {
136-
self.points.rows()
137-
}
138-
139-
/// Adds the given element to the value for the given region. Returns whether
140-
/// the element is newly added (i.e., was not already present).
141-
pub(crate) fn add_element(&mut self, row: N, location: Location) -> bool {
142-
debug!("LivenessValues::add(r={:?}, location={:?})", row, location);
143-
let index = self.elements.point_from_location(location);
144-
self.points.insert(row, index)
145-
}
146-
147-
/// Adds all the elements in the given bit array into the given
148-
/// region. Returns whether any of them are newly added.
149-
pub(crate) fn add_elements(&mut self, row: N, locations: &IntervalSet<PointIndex>) -> bool {
150-
debug!("LivenessValues::add_elements(row={:?}, locations={:?})", row, locations);
151-
self.points.union_row(row, locations)
152-
}
153-
154-
/// Adds all the control-flow points to the values for `r`.
155-
pub(crate) fn add_all_points(&mut self, row: N) {
156-
self.points.insert_all_into_row(row);
157-
}
158-
159-
/// Returns `true` if the region `r` contains the given element.
160-
pub(crate) fn contains(&self, row: N, location: Location) -> bool {
161-
let index = self.elements.point_from_location(location);
162-
self.points.row(row).is_some_and(|r| r.contains(index))
163-
}
164-
165-
/// Returns an iterator of all the elements contained by the region `r`
166-
pub(crate) fn get_elements(&self, row: N) -> impl Iterator<Item = Location> + '_ {
167-
self.points
168-
.row(row)
169-
.into_iter()
170-
.flat_map(|set| set.iter())
171-
.take_while(move |&p| self.elements.point_in_range(p))
172-
.map(move |p| self.elements.to_location(p))
173-
}
174-
175-
/// Returns a "pretty" string value of the region. Meant for debugging.
176-
pub(crate) fn region_value_str(&self, r: N) -> String {
177-
region_value_str(self.get_elements(r).map(RegionElement::Location))
178-
}
179-
}
180-
18135
/// Maps from `ty::PlaceholderRegion` values that are used in the rest of
18236
/// rustc to the internal `PlaceholderIndex` values that are used in
18337
/// NLL.
@@ -229,7 +83,7 @@ impl PlaceholderIndices {
22983
/// it would also contain various points from within the function.
23084
#[derive(Clone)]
23185
pub(crate) struct RegionValues<N: Idx> {
232-
elements: Rc<RegionValueElements>,
86+
elements: Rc<DenseLocationMap>,
23387
placeholder_indices: Rc<PlaceholderIndices>,
23488
points: SparseIntervalMatrix<N, PointIndex>,
23589
free_regions: SparseBitMatrix<N, RegionVid>,
@@ -244,14 +98,14 @@ impl<N: Idx> RegionValues<N> {
24498
/// Each of the regions in num_region_variables will be initialized with an
24599
/// empty set of points and no causal information.
246100
pub(crate) fn new(
247-
elements: &Rc<RegionValueElements>,
101+
elements: &Rc<DenseLocationMap>,
248102
num_universal_regions: usize,
249103
placeholder_indices: &Rc<PlaceholderIndices>,
250104
) -> Self {
251105
let num_placeholders = placeholder_indices.len();
252106
Self {
253107
elements: elements.clone(),
254-
points: SparseIntervalMatrix::new(elements.num_points),
108+
points: SparseIntervalMatrix::new(elements.num_points()),
255109
placeholder_indices: placeholder_indices.clone(),
256110
free_regions: SparseBitMatrix::new(num_universal_regions),
257111
placeholders: SparseBitMatrix::new(num_placeholders),
@@ -303,7 +157,7 @@ impl<N: Idx> RegionValues<N> {
303157
/// elements for the region `from` from `values` and add them to
304158
/// the region `to` in `self`.
305159
pub(crate) fn merge_liveness<M: Idx>(&mut self, to: N, from: M, values: &LivenessValues<M>) {
306-
if let Some(set) = values.points.row(from) {
160+
if let Some(set) = values.get_intervals(from) {
307161
self.points.union_row(to, set);
308162
}
309163
}
@@ -416,7 +270,7 @@ impl ToElementIndex for ty::PlaceholderRegion {
416270
}
417271

418272
pub(crate) fn location_set_str(
419-
elements: &RegionValueElements,
273+
elements: &DenseLocationMap,
420274
points: impl IntoIterator<Item = PointIndex>,
421275
) -> String {
422276
region_value_str(

compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use rustc_data_structures::vec_linked_list as vll;
22
use rustc_index::IndexVec;
33
use rustc_middle::mir::visit::{PlaceContext, Visitor};
44
use rustc_middle::mir::{Body, Local, Location};
5+
use rustc_mir_dataflow::points::{DenseLocationMap, PointIndex};
56

67
use crate::def_use::{self, DefUse};
7-
use crate::region_infer::values::{PointIndex, RegionValueElements};
88

99
/// A map that cross references each local with the locations where it
1010
/// is defined (assigned), used, or dropped. Used during liveness
@@ -60,7 +60,7 @@ impl vll::LinkElem for Appearance {
6060
impl LocalUseMap {
6161
pub(crate) fn build(
6262
live_locals: &[Local],
63-
elements: &RegionValueElements,
63+
elements: &DenseLocationMap,
6464
body: &Body<'_>,
6565
) -> Self {
6666
let nones = IndexVec::from_elem(None, &body.local_decls);
@@ -103,7 +103,7 @@ impl LocalUseMap {
103103

104104
struct LocalUseMapBuild<'me> {
105105
local_use_map: &'me mut LocalUseMap,
106-
elements: &'me RegionValueElements,
106+
elements: &'me DenseLocationMap,
107107

108108
// Vector used in `visit_local` to signal which `Local`s do we need
109109
// def/use/drop information on, constructed from `live_locals` (that
@@ -144,7 +144,7 @@ impl LocalUseMapBuild<'_> {
144144
}
145145

146146
fn insert(
147-
elements: &RegionValueElements,
147+
elements: &DenseLocationMap,
148148
first_appearance: &mut Option<AppearanceIndex>,
149149
appearances: &mut IndexVec<AppearanceIndex, Appearance>,
150150
location: Location,

compiler/rustc_borrowck/src/type_check/liveness/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use rustc_middle::mir::{Body, Local};
44
use rustc_middle::ty::{RegionVid, TyCtxt};
55
use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
66
use rustc_mir_dataflow::move_paths::MoveData;
7+
use rustc_mir_dataflow::points::DenseLocationMap;
78
use rustc_mir_dataflow::ResultsCursor;
89
use std::rc::Rc;
910

1011
use crate::{
1112
constraints::OutlivesConstraintSet,
1213
facts::{AllFacts, AllFactsExt},
1314
location::LocationTable,
14-
region_infer::values::RegionValueElements,
1515
universal_regions::UniversalRegions,
1616
};
1717

@@ -32,7 +32,7 @@ mod trace;
3232
pub(super) fn generate<'mir, 'tcx>(
3333
typeck: &mut TypeChecker<'_, 'tcx>,
3434
body: &Body<'tcx>,
35-
elements: &Rc<RegionValueElements>,
35+
elements: &Rc<DenseLocationMap>,
3636
flow_inits: &mut ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>,
3737
move_data: &MoveData<'tcx>,
3838
location_table: &LocationTable,

0 commit comments

Comments
 (0)