Skip to content

Commit e2bf960

Browse files
committed
sccs info
1 parent 960ebaf commit e2bf960

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

compiler/rustc_borrowck/src/constraints/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) mod graph;
1717
/// constraints of the form `R1: R2`. Each constraint is identified by
1818
/// a unique `OutlivesConstraintIndex` and you can index into the set
1919
/// (`constraint_set[i]`) to access the constraint details.
20-
#[derive(Clone, Default)]
20+
#[derive(Clone, Debug, Default)]
2121
pub(crate) struct OutlivesConstraintSet<'tcx> {
2222
outlives: IndexVec<OutlivesConstraintIndex, OutlivesConstraint<'tcx>>,
2323
}

compiler/rustc_borrowck/src/nll.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
263263
}
264264

265265
let mut regioncx = RegionInferenceContext::new(
266+
infcx,
266267
var_origins,
267268
universal_regions,
268269
placeholder_indices,

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::{
3434
},
3535
type_check::{free_region_relations::UniversalRegionRelations, Locations},
3636
universal_regions::UniversalRegions,
37+
BorrowckInferCtxt,
3738
};
3839

3940
mod dump_mir;
@@ -243,6 +244,59 @@ pub enum ExtraConstraintInfo {
243244
PlaceholderFromPredicate(Span),
244245
}
245246

247+
#[cfg(debug_assertions)]
248+
#[instrument(skip(infcx, sccs), level = "debug")]
249+
fn sccs_info<'cx, 'tcx>(
250+
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
251+
sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
252+
) {
253+
use crate::renumber::RegionCtxt;
254+
255+
let var_to_origin = infcx.reg_var_to_origin.borrow();
256+
let num_components = sccs.scc_data.ranges.len();
257+
let mut components = vec![FxHashSet::default(); num_components];
258+
259+
for (reg_var_idx, scc_idx) in sccs.scc_indices.iter().enumerate() {
260+
let reg_var = ty::RegionVid::from_usize(reg_var_idx);
261+
let origin = var_to_origin.get(&reg_var).unwrap_or_else(|| &RegionCtxt::Unknown);
262+
components[scc_idx.as_usize()].insert(*origin);
263+
}
264+
265+
debug!(
266+
"strongly connected components: {:#?}",
267+
components
268+
.iter()
269+
.enumerate()
270+
.map(|(idx, origin)| { (ConstraintSccIndex::from_usize(idx), origin) })
271+
.collect::<Vec<_>>()
272+
);
273+
274+
// Now let's calculate the best representative for each component
275+
let components_representatives = components
276+
.into_iter()
277+
.enumerate()
278+
.map(|(scc_idx, region_ctxts)| {
279+
let repr = region_ctxts
280+
.into_iter()
281+
.max_by(|x, y| x._preference_value().cmp(&y._preference_value()))
282+
.unwrap();
283+
284+
(ConstraintSccIndex::from_usize(scc_idx), repr)
285+
})
286+
.collect::<FxHashMap<_, _>>();
287+
288+
let mut scc_node_to_edges = FxHashMap::default();
289+
for (scc_idx, repr) in components_representatives.iter() {
290+
let edges_range = sccs.scc_data.ranges[*scc_idx].clone();
291+
let edges = &sccs.scc_data.all_successors[edges_range];
292+
let edge_representatives =
293+
edges.iter().map(|scc_idx| components_representatives[scc_idx]).collect::<Vec<_>>();
294+
scc_node_to_edges.insert((scc_idx, repr), edge_representatives);
295+
}
296+
297+
debug!("SCC edges {:#?}", scc_node_to_edges);
298+
}
299+
246300
impl<'tcx> RegionInferenceContext<'tcx> {
247301
/// Creates a new region inference context with a total of
248302
/// `num_region_variables` valid inference variables; the first N
@@ -251,7 +305,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
251305
///
252306
/// The `outlives_constraints` and `type_tests` are an initial set
253307
/// of constraints produced by the MIR type check.
254-
pub(crate) fn new(
308+
pub(crate) fn new<'cx>(
309+
_infcx: &BorrowckInferCtxt<'cx, 'tcx>,
255310
var_infos: VarInfos,
256311
universal_regions: Rc<UniversalRegions<'tcx>>,
257312
placeholder_indices: Rc<PlaceholderIndices>,
@@ -263,6 +318,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
263318
liveness_constraints: LivenessValues<RegionVid>,
264319
elements: &Rc<RegionValueElements>,
265320
) -> Self {
321+
debug!("universal_regions: {:#?}", universal_regions);
322+
debug!("outlives constraints: {:#?}", outlives_constraints);
323+
debug!("placeholder_indices: {:#?}", placeholder_indices);
324+
debug!("type tests: {:#?}", type_tests);
325+
266326
// Create a RegionDefinition for each inference variable.
267327
let definitions: IndexVec<_, _> = var_infos
268328
.iter()
@@ -274,6 +334,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
274334
let fr_static = universal_regions.fr_static;
275335
let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static));
276336

337+
#[cfg(debug_assertions)]
338+
{
339+
sccs_info(_infcx, constraint_sccs.clone());
340+
}
341+
277342
let mut scc_values =
278343
RegionValues::new(elements, universal_regions.len(), &placeholder_indices);
279344

compiler/rustc_borrowck/src/region_infer/values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<N: Idx> LivenessValues<N> {
181181
/// Maps from `ty::PlaceholderRegion` values that are used in the rest of
182182
/// rustc to the internal `PlaceholderIndex` values that are used in
183183
/// NLL.
184-
#[derive(Default)]
184+
#[derive(Debug, Default)]
185185
pub(crate) struct PlaceholderIndices {
186186
indices: FxIndexSet<ty::PlaceholderRegion>,
187187
}

compiler/rustc_data_structures/src/graph/scc/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ mod tests;
2121
pub struct Sccs<N: Idx, S: Idx> {
2222
/// For each node, what is the SCC index of the SCC to which it
2323
/// belongs.
24-
scc_indices: IndexVec<N, S>,
24+
pub scc_indices: IndexVec<N, S>,
2525

2626
/// Data about each SCC.
27-
scc_data: SccData<S>,
27+
pub scc_data: SccData<S>,
2828
}
2929

30-
struct SccData<S: Idx> {
30+
pub struct SccData<S: Idx> {
3131
/// For each SCC, the range of `all_successors` where its
3232
/// successors can be found.
33-
ranges: IndexVec<S, Range<usize>>,
33+
pub ranges: IndexVec<S, Range<usize>>,
3434

3535
/// Contains the successors for all the Sccs, concatenated. The
3636
/// range of indices corresponding to a given SCC is found in its
3737
/// SccData.
38-
all_successors: Vec<S>,
38+
pub all_successors: Vec<S>,
3939
}
4040

4141
impl<N: Idx, S: Idx + Ord> Sccs<N, S> {

0 commit comments

Comments
 (0)