Skip to content

Commit 68096cc

Browse files
authored
Rollup merge of #64532 - ecstatic-morse:dataflow-cursor-get, r=tmandry
Replace `state_for_location` with `DataflowResultsCursor` These are two different ways of getting the same data from the result of a dataflow analysis. However, `state_for_location` goes quadratic if you try to call it for every statement in the body.
2 parents 05d93a7 + 73c7a68 commit 68096cc

File tree

2 files changed

+17
-48
lines changed

2 files changed

+17
-48
lines changed

src/librustc_mir/dataflow/mod.rs

+2-26
Original file line numberDiff line numberDiff line change
@@ -453,34 +453,10 @@ where
453453
{
454454
self.flow_state.each_gen_bit(f)
455455
}
456-
}
457-
458-
pub fn state_for_location<'tcx, T: BitDenotation<'tcx>>(loc: Location,
459-
analysis: &T,
460-
result: &DataflowResults<'tcx, T>,
461-
body: &Body<'tcx>)
462-
-> BitSet<T::Idx> {
463-
let mut trans = GenKill::from_elem(HybridBitSet::new_empty(analysis.bits_per_block()));
464456

465-
for stmt in 0..loc.statement_index {
466-
let mut stmt_loc = loc;
467-
stmt_loc.statement_index = stmt;
468-
analysis.before_statement_effect(&mut trans, stmt_loc);
469-
analysis.statement_effect(&mut trans, stmt_loc);
457+
pub fn get(&self) -> &BitSet<BD::Idx> {
458+
self.flow_state.as_dense()
470459
}
471-
472-
// Apply the pre-statement effect of the statement we're evaluating.
473-
if loc.statement_index == body[loc.block].statements.len() {
474-
analysis.before_terminator_effect(&mut trans, loc);
475-
} else {
476-
analysis.before_statement_effect(&mut trans, loc);
477-
}
478-
479-
// Apply the transfer function for all preceding statements to the fixpoint
480-
// at the start of the block.
481-
let mut state = result.sets().entry_set_for(loc.block.index()).to_owned();
482-
trans.apply(&mut state);
483-
state
484460
}
485461

486462
pub struct DataflowAnalysis<'a, 'tcx, O>

src/librustc_mir/transform/generator.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::transform::{MirPass, MirSource};
6767
use crate::transform::simplify;
6868
use crate::transform::no_landing_pads::no_landing_pads;
6969
use crate::dataflow::{DataflowResults, DataflowResultsConsumer, FlowAtLocation};
70-
use crate::dataflow::{do_dataflow, DebugFormatted, state_for_location};
70+
use crate::dataflow::{do_dataflow, DebugFormatted, DataflowResultsCursor};
7171
use crate::dataflow::{MaybeStorageLive, HaveBeenBorrowedLocals, RequiresStorage};
7272
use crate::util::dump_mir;
7373
use crate::util::liveness;
@@ -436,9 +436,10 @@ fn locals_live_across_suspend_points(
436436
// Calculate when MIR locals have live storage. This gives us an upper bound of their
437437
// lifetimes.
438438
let storage_live_analysis = MaybeStorageLive::new(body);
439-
let storage_live =
439+
let storage_live_results =
440440
do_dataflow(tcx, body, def_id, &[], &dead_unwinds, storage_live_analysis,
441441
|bd, p| DebugFormatted::new(&bd.body().local_decls[p]));
442+
let mut storage_live_cursor = DataflowResultsCursor::new(&storage_live_results, body);
442443

443444
// Find the MIR locals which do not use StorageLive/StorageDead statements.
444445
// The storage of these locals are always live.
@@ -448,17 +449,18 @@ fn locals_live_across_suspend_points(
448449
// Calculate the MIR locals which have been previously
449450
// borrowed (even if they are still active).
450451
let borrowed_locals_analysis = HaveBeenBorrowedLocals::new(body);
451-
let borrowed_locals_result =
452+
let borrowed_locals_results =
452453
do_dataflow(tcx, body, def_id, &[], &dead_unwinds, borrowed_locals_analysis,
453454
|bd, p| DebugFormatted::new(&bd.body().local_decls[p]));
455+
let mut borrowed_locals_cursor = DataflowResultsCursor::new(&borrowed_locals_results, body);
454456

455457
// Calculate the MIR locals that we actually need to keep storage around
456458
// for.
457-
let requires_storage_analysis = RequiresStorage::new(body, &borrowed_locals_result);
458-
let requires_storage =
459+
let requires_storage_analysis = RequiresStorage::new(body, &borrowed_locals_results);
460+
let requires_storage_results =
459461
do_dataflow(tcx, body, def_id, &[], &dead_unwinds, requires_storage_analysis,
460462
|bd, p| DebugFormatted::new(&bd.body().local_decls[p]));
461-
let requires_storage_analysis = RequiresStorage::new(body, &borrowed_locals_result);
463+
let mut requires_storage_cursor = DataflowResultsCursor::new(&requires_storage_results, body);
462464

463465
// Calculate the liveness of MIR locals ignoring borrows.
464466
let mut live_locals = liveness::LiveVarSet::new_empty(body.local_decls.len());
@@ -484,10 +486,6 @@ fn locals_live_across_suspend_points(
484486
};
485487

486488
if !movable {
487-
let borrowed_locals = state_for_location(loc,
488-
&borrowed_locals_analysis,
489-
&borrowed_locals_result,
490-
body);
491489
// The `liveness` variable contains the liveness of MIR locals ignoring borrows.
492490
// This is correct for movable generators since borrows cannot live across
493491
// suspension points. However for immovable generators we need to account for
@@ -498,22 +496,19 @@ fn locals_live_across_suspend_points(
498496
// If a borrow is converted to a raw reference, we must also assume that it lives
499497
// forever. Note that the final liveness is still bounded by the storage liveness
500498
// of the local, which happens using the `intersect` operation below.
501-
liveness.outs[block].union(&borrowed_locals);
499+
borrowed_locals_cursor.seek(loc);
500+
liveness.outs[block].union(borrowed_locals_cursor.get());
502501
}
503502

504-
let storage_liveness = state_for_location(loc,
505-
&storage_live_analysis,
506-
&storage_live,
507-
body);
503+
storage_live_cursor.seek(loc);
504+
let storage_liveness = storage_live_cursor.get();
508505

509506
// Store the storage liveness for later use so we can restore the state
510507
// after a suspension point
511508
storage_liveness_map.insert(block, storage_liveness.clone());
512509

513-
let mut storage_required = state_for_location(loc,
514-
&requires_storage_analysis,
515-
&requires_storage,
516-
body);
510+
requires_storage_cursor.seek(loc);
511+
let mut storage_required = requires_storage_cursor.get().clone();
517512

518513
// Mark locals without storage statements as always requiring storage
519514
storage_required.union(&ignored.0);
@@ -549,8 +544,7 @@ fn locals_live_across_suspend_points(
549544
body,
550545
&live_locals,
551546
&ignored,
552-
requires_storage,
553-
requires_storage_analysis);
547+
requires_storage_results);
554548

555549
LivenessInfo {
556550
live_locals,
@@ -588,7 +582,6 @@ fn compute_storage_conflicts(
588582
stored_locals: &liveness::LiveVarSet,
589583
ignored: &StorageIgnored,
590584
requires_storage: DataflowResults<'tcx, RequiresStorage<'mir, 'tcx>>,
591-
_requires_storage_analysis: RequiresStorage<'mir, 'tcx>,
592585
) -> BitMatrix<GeneratorSavedLocal, GeneratorSavedLocal> {
593586
assert_eq!(body.local_decls.len(), ignored.0.domain_size());
594587
assert_eq!(body.local_decls.len(), stored_locals.domain_size());

0 commit comments

Comments
 (0)