Skip to content

Commit ca09f22

Browse files
committed
Add a context parameter to JoinSemiLattice
1 parent c1699a7 commit ca09f22

File tree

10 files changed

+133
-100
lines changed

10 files changed

+133
-100
lines changed

compiler/rustc_const_eval/src/transform/check_consts/resolver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ impl<C> DebugWithContext<C> for State {
307307
}
308308

309309
impl JoinSemiLattice for State {
310-
fn join(&mut self, other: &Self) -> bool {
311-
self.qualif.join(&other.qualif) || self.borrow.join(&other.borrow)
310+
fn join(&mut self, _: &(), other: &Self) -> bool {
311+
self.qualif.union(&other.qualif) || self.borrow.union(&other.borrow)
312312
}
313313
}
314314

compiler/rustc_mir_dataflow/src/framework/direction.rs

+46-35
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::ops::RangeInclusive;
44

55
use super::visitor::{ResultsVisitable, ResultsVisitor};
66
use super::{
7-
Analysis, CallReturnPlaces, Effect, EffectIndex, GenKillAnalysis, GenKillSet, SwitchIntTarget,
7+
Analysis, AnalysisDomain, CallReturnPlaces, Effect, EffectIndex, GenKillAnalysis, GenKillSet,
8+
SwitchIntTarget, WithJoinCtxt,
89
};
910

1011
pub trait Direction {
@@ -55,9 +56,9 @@ pub trait Direction {
5556
body: &mir::Body<'tcx>,
5657
exit_state: &mut A::Domain,
5758
block: (BasicBlock, &'_ mir::BasicBlockData<'tcx>),
58-
propagate: impl FnMut(BasicBlock, &A::Domain),
59+
propagate: impl FnMut(&A::JoinCtxt, BasicBlock, &A::Domain),
5960
) where
60-
A: Analysis<'tcx>;
61+
A: Analysis<'tcx> + WithJoinCtxt<'tcx>;
6162
}
6263

6364
/// Dataflow that runs from the exit of a block (the terminator), to its entry (the first statement).
@@ -221,9 +222,9 @@ impl Direction for Backward {
221222
body: &mir::Body<'tcx>,
222223
exit_state: &mut A::Domain,
223224
(bb, _bb_data): (BasicBlock, &'_ mir::BasicBlockData<'tcx>),
224-
mut propagate: impl FnMut(BasicBlock, &A::Domain),
225+
mut propagate: impl FnMut(&A::JoinCtxt, BasicBlock, &A::Domain),
225226
) where
226-
A: Analysis<'tcx>,
227+
A: Analysis<'tcx> + WithJoinCtxt<'tcx>,
227228
{
228229
for pred in body.basic_blocks.predecessors()[bb].iter().copied() {
229230
match body[pred].terminator().kind {
@@ -237,7 +238,7 @@ impl Direction for Backward {
237238
pred,
238239
CallReturnPlaces::Call(destination),
239240
);
240-
propagate(pred, &tmp);
241+
propagate(analysis.join_ctxt(), pred, &tmp);
241242
}
242243

243244
mir::TerminatorKind::InlineAsm {
@@ -249,13 +250,13 @@ impl Direction for Backward {
249250
pred,
250251
CallReturnPlaces::InlineAsm(operands),
251252
);
252-
propagate(pred, &tmp);
253+
propagate(analysis.join_ctxt(), pred, &tmp);
253254
}
254255

255256
mir::TerminatorKind::Yield { resume, resume_arg, .. } if resume == bb => {
256257
let mut tmp = exit_state.clone();
257258
analysis.apply_yield_resume_effect(&mut tmp, resume, resume_arg);
258-
propagate(pred, &tmp);
259+
propagate(analysis.join_ctxt(), pred, &tmp);
259260
}
260261

261262
mir::TerminatorKind::SwitchInt { targets: _, ref discr } => {
@@ -271,11 +272,11 @@ impl Direction for Backward {
271272
analysis.apply_switch_int_edge_effects(pred, discr, &mut applier);
272273

273274
if !applier.effects_applied {
274-
propagate(pred, exit_state)
275+
propagate(analysis.join_ctxt(), pred, exit_state)
275276
}
276277
}
277278

278-
_ => propagate(pred, exit_state),
279+
_ => propagate(analysis.join_ctxt(), pred, exit_state),
279280
}
280281
}
281282
}
@@ -290,12 +291,17 @@ struct BackwardSwitchIntEdgeEffectsApplier<'a, 'tcx, D, F> {
290291
effects_applied: bool,
291292
}
292293

293-
impl<D, F> super::SwitchIntEdgeEffects<D> for BackwardSwitchIntEdgeEffectsApplier<'_, '_, D, F>
294+
impl<'tcx, A, F> super::SwitchIntEdgeEffects<'tcx, A>
295+
for BackwardSwitchIntEdgeEffectsApplier<'_, '_, A::Domain, F>
294296
where
295-
D: Clone,
296-
F: FnMut(BasicBlock, &D),
297+
A: AnalysisDomain<'tcx>,
298+
F: FnMut(&A::JoinCtxt, BasicBlock, &A::Domain),
297299
{
298-
fn apply(&mut self, mut apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget)) {
300+
fn apply(
301+
&mut self,
302+
ctxt: &A::JoinCtxt,
303+
mut apply_edge_effect: impl FnMut(&mut A::Domain, SwitchIntTarget),
304+
) {
299305
assert!(!self.effects_applied);
300306

301307
let values = &self.body.basic_blocks.switch_sources()[&(self.bb, self.pred)];
@@ -305,7 +311,7 @@ where
305311
for target in targets {
306312
let tmp = opt_clone_from_or_clone(&mut tmp, self.exit_state);
307313
apply_edge_effect(tmp, target);
308-
(self.propagate)(self.pred, tmp);
314+
(self.propagate)(ctxt, self.pred, tmp);
309315
}
310316

311317
self.effects_applied = true;
@@ -468,43 +474,43 @@ impl Direction for Forward {
468474
_body: &mir::Body<'tcx>,
469475
exit_state: &mut A::Domain,
470476
(bb, bb_data): (BasicBlock, &'_ mir::BasicBlockData<'tcx>),
471-
mut propagate: impl FnMut(BasicBlock, &A::Domain),
477+
mut propagate: impl FnMut(&A::JoinCtxt, BasicBlock, &A::Domain),
472478
) where
473-
A: Analysis<'tcx>,
479+
A: Analysis<'tcx> + WithJoinCtxt<'tcx>,
474480
{
475481
use mir::TerminatorKind::*;
476482
match bb_data.terminator().kind {
477483
Return | Resume | Terminate | GeneratorDrop | Unreachable => {}
478484

479-
Goto { target } => propagate(target, exit_state),
485+
Goto { target } => propagate(analysis.join_ctxt(), target, exit_state),
480486

481487
Assert { target, unwind, expected: _, msg: _, cond: _ }
482488
| Drop { target, unwind, place: _, replace: _ }
483489
| FalseUnwind { real_target: target, unwind } => {
484490
if let UnwindAction::Cleanup(unwind) = unwind {
485-
propagate(unwind, exit_state);
491+
propagate(analysis.join_ctxt(), unwind, exit_state);
486492
}
487493

488-
propagate(target, exit_state);
494+
propagate(analysis.join_ctxt(), target, exit_state);
489495
}
490496

491497
FalseEdge { real_target, imaginary_target } => {
492-
propagate(real_target, exit_state);
493-
propagate(imaginary_target, exit_state);
498+
propagate(analysis.join_ctxt(), real_target, exit_state);
499+
propagate(analysis.join_ctxt(), imaginary_target, exit_state);
494500
}
495501

496502
Yield { resume: target, drop, resume_arg, value: _ } => {
497503
if let Some(drop) = drop {
498-
propagate(drop, exit_state);
504+
propagate(analysis.join_ctxt(), drop, exit_state);
499505
}
500506

501507
analysis.apply_yield_resume_effect(exit_state, target, resume_arg);
502-
propagate(target, exit_state);
508+
propagate(analysis.join_ctxt(), target, exit_state);
503509
}
504510

505511
Call { unwind, destination, target, func: _, args: _, call_source: _, fn_span: _ } => {
506512
if let UnwindAction::Cleanup(unwind) = unwind {
507-
propagate(unwind, exit_state);
513+
propagate(analysis.join_ctxt(), unwind, exit_state);
508514
}
509515

510516
if let Some(target) = target {
@@ -515,7 +521,7 @@ impl Direction for Forward {
515521
bb,
516522
CallReturnPlaces::Call(destination),
517523
);
518-
propagate(target, exit_state);
524+
propagate(analysis.join_ctxt(), target, exit_state);
519525
}
520526
}
521527

@@ -528,7 +534,7 @@ impl Direction for Forward {
528534
unwind,
529535
} => {
530536
if let UnwindAction::Cleanup(unwind) = unwind {
531-
propagate(unwind, exit_state);
537+
propagate(analysis.join_ctxt(), unwind, exit_state);
532538
}
533539

534540
if let Some(target) = destination {
@@ -539,7 +545,7 @@ impl Direction for Forward {
539545
bb,
540546
CallReturnPlaces::InlineAsm(operands),
541547
);
542-
propagate(target, exit_state);
548+
propagate(analysis.join_ctxt(), target, exit_state);
543549
}
544550
}
545551

@@ -562,7 +568,7 @@ impl Direction for Forward {
562568

563569
if !effects_applied {
564570
for target in targets.all_targets() {
565-
propagate(*target, exit_state);
571+
propagate(analysis.join_ctxt(), *target, exit_state);
566572
}
567573
}
568574
}
@@ -578,26 +584,31 @@ struct ForwardSwitchIntEdgeEffectsApplier<'a, D, F> {
578584
effects_applied: bool,
579585
}
580586

581-
impl<D, F> super::SwitchIntEdgeEffects<D> for ForwardSwitchIntEdgeEffectsApplier<'_, D, F>
587+
impl<'tcx, A, F> super::SwitchIntEdgeEffects<'tcx, A>
588+
for ForwardSwitchIntEdgeEffectsApplier<'_, A::Domain, F>
582589
where
583-
D: Clone,
584-
F: FnMut(BasicBlock, &D),
590+
A: AnalysisDomain<'tcx>,
591+
F: FnMut(&A::JoinCtxt, BasicBlock, &A::Domain),
585592
{
586-
fn apply(&mut self, mut apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget)) {
593+
fn apply(
594+
&mut self,
595+
ctxt: &A::JoinCtxt,
596+
mut apply_edge_effect: impl FnMut(&mut A::Domain, SwitchIntTarget),
597+
) {
587598
assert!(!self.effects_applied);
588599

589600
let mut tmp = None;
590601
for (value, target) in self.targets.iter() {
591602
let tmp = opt_clone_from_or_clone(&mut tmp, self.exit_state);
592603
apply_edge_effect(tmp, SwitchIntTarget { value: Some(value), target });
593-
(self.propagate)(target, tmp);
604+
(self.propagate)(ctxt, target, tmp);
594605
}
595606

596607
// Once we get to the final, "otherwise" branch, there is no need to preserve `exit_state`,
597608
// so pass it directly to `apply_edge_effect` to save a clone of the dataflow state.
598609
let otherwise = self.targets.otherwise();
599610
apply_edge_effect(self.exit_state, SwitchIntTarget { value: None, target: otherwise });
600-
(self.propagate)(otherwise, self.exit_state);
611+
(self.propagate)(ctxt, otherwise, self.exit_state);
601612

602613
self.effects_applied = true;
603614
}

compiler/rustc_mir_dataflow/src/framework/engine.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use super::graphviz;
2626
use super::{
2727
visit_results, Analysis, AnalysisDomain, CloneAnalysis, Direction, GenKill, GenKillAnalysis,
2828
GenKillSet, JoinSemiLattice, ResultsClonedCursor, ResultsCursor, ResultsRefCursor,
29-
ResultsVisitor,
29+
ResultsVisitor, WithJoinCtxt,
3030
};
3131

3232
pub type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as AnalysisDomain<'tcx>>::Domain>;
@@ -149,8 +149,8 @@ where
149149

150150
impl<'a, 'tcx, A, D, T> Engine<'a, 'tcx, A>
151151
where
152-
A: GenKillAnalysis<'tcx, Idx = T, Domain = D>,
153-
D: Clone + JoinSemiLattice + GenKill<T> + BitSetExt<T>,
152+
A: GenKillAnalysis<'tcx, Idx = T, Domain = D> + WithJoinCtxt<'tcx>,
153+
D: Clone + JoinSemiLattice<A::JoinCtxt> + GenKill<T> + BitSetExt<T>,
154154
T: Idx,
155155
{
156156
/// Creates a new `Engine` to solve a gen-kill dataflow problem.
@@ -183,8 +183,8 @@ where
183183

184184
impl<'a, 'tcx, A, D> Engine<'a, 'tcx, A>
185185
where
186-
A: Analysis<'tcx, Domain = D>,
187-
D: Clone + JoinSemiLattice,
186+
A: Analysis<'tcx, Domain = D> + WithJoinCtxt<'tcx>,
187+
D: Clone + JoinSemiLattice<A::JoinCtxt>,
188188
{
189189
/// Creates a new `Engine` to solve a dataflow problem with an arbitrary transfer
190190
/// function.
@@ -276,8 +276,8 @@ where
276276
body,
277277
&mut state,
278278
(bb, bb_data),
279-
|target: BasicBlock, state: &A::Domain| {
280-
let set_changed = entry_sets[target].join(state);
279+
|ctxt: &A::JoinCtxt, target: BasicBlock, state: &A::Domain| {
280+
let set_changed = entry_sets[target].join(ctxt, state);
281281
if set_changed {
282282
dirty_queue.insert(target);
283283
}

0 commit comments

Comments
 (0)