Skip to content

Commit 5dfc84c

Browse files
committed
Auto merge of #44480 - Zoxc:gen-liveness, r=arielb1
Analyse storage liveness and preserve it during generator transformation This uses a dataflow analysis on `StorageLive` and `StorageDead` statements to infer where the storage of locals are live. The result of this analysis is intersected with the regular liveness analysis such that a local is can only be live when its storage is. This fixes #44184. If the storage of a local is live across a suspension point, we'll insert a `StorageLive` statement for it after the suspension point so storage liveness is preserved. This fixes #44179. r? @arielb1
2 parents 2b6bc58 + 0e8e659 commit 5dfc84c

File tree

7 files changed

+354
-174
lines changed

7 files changed

+354
-174
lines changed

src/librustc_data_structures/bitslice.rs

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ pub trait BitwiseOperator {
132132
fn join(&self, pred1: usize, pred2: usize) -> usize;
133133
}
134134

135+
pub struct Intersect;
136+
impl BitwiseOperator for Intersect {
137+
#[inline]
138+
fn join(&self, a: usize, b: usize) -> usize { a & b }
139+
}
135140
pub struct Union;
136141
impl BitwiseOperator for Union {
137142
#[inline]

src/librustc_data_structures/indexed_set.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::mem;
1515
use std::ops::{Deref, DerefMut, Range};
1616
use std::slice;
1717
use bitslice::{BitSlice, Word};
18-
use bitslice::{bitwise, Union, Subtract};
18+
use bitslice::{bitwise, Union, Subtract, Intersect};
1919
use indexed_vec::Idx;
2020

2121
/// Represents a set (or packed family of sets), of some element type
@@ -164,6 +164,10 @@ impl<T: Idx> IdxSet<T> {
164164
bitwise(self.words_mut(), other.words(), &Subtract)
165165
}
166166

167+
pub fn intersect(&mut self, other: &IdxSet<T>) -> bool {
168+
bitwise(self.words_mut(), other.words(), &Intersect)
169+
}
170+
167171
pub fn iter(&self) -> Iter<T> {
168172
Iter {
169173
cur: None,

src/librustc_mir/dataflow/impls/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ use super::drop_flag_effects_for_function_entry;
2727
use super::drop_flag_effects_for_location;
2828
use super::on_lookup_result_bits;
2929

30+
mod storage_liveness;
31+
32+
pub use self::storage_liveness::*;
33+
3034
#[allow(dead_code)]
3135
pub(super) mod borrows;
3236

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub use super::*;
12+
13+
use rustc::mir::*;
14+
use dataflow::BitDenotation;
15+
16+
#[derive(Copy, Clone)]
17+
pub struct MaybeStorageLive<'a, 'tcx: 'a> {
18+
mir: &'a Mir<'tcx>,
19+
}
20+
21+
impl<'a, 'tcx: 'a> MaybeStorageLive<'a, 'tcx> {
22+
pub fn new(mir: &'a Mir<'tcx>)
23+
-> Self {
24+
MaybeStorageLive { mir: mir }
25+
}
26+
27+
pub fn mir(&self) -> &Mir<'tcx> {
28+
self.mir
29+
}
30+
}
31+
32+
impl<'a, 'tcx> BitDenotation for MaybeStorageLive<'a, 'tcx> {
33+
type Idx = Local;
34+
fn name() -> &'static str { "maybe_storage_live" }
35+
fn bits_per_block(&self) -> usize {
36+
self.mir.local_decls.len()
37+
}
38+
39+
fn start_block_effect(&self, _sets: &mut BlockSets<Local>) {
40+
// Nothing is live on function entry
41+
}
42+
43+
fn statement_effect(&self,
44+
sets: &mut BlockSets<Local>,
45+
loc: Location) {
46+
let stmt = &self.mir[loc.block].statements[loc.statement_index];
47+
48+
match stmt.kind {
49+
StatementKind::StorageLive(l) => sets.gen(&l),
50+
StatementKind::StorageDead(l) => sets.kill(&l),
51+
_ => (),
52+
}
53+
}
54+
55+
fn terminator_effect(&self,
56+
_sets: &mut BlockSets<Local>,
57+
_loc: Location) {
58+
// Terminators have no effect
59+
}
60+
61+
fn propagate_call_return(&self,
62+
_in_out: &mut IdxSet<Local>,
63+
_call_bb: mir::BasicBlock,
64+
_dest_bb: mir::BasicBlock,
65+
_dest_lval: &mir::Lvalue) {
66+
// Nothing to do when a call returns successfully
67+
}
68+
}
69+
70+
impl<'a, 'tcx> BitwiseOperator for MaybeStorageLive<'a, 'tcx> {
71+
#[inline]
72+
fn join(&self, pred1: usize, pred2: usize) -> usize {
73+
pred1 | pred2 // "maybe" means we union effects of both preds
74+
}
75+
}
76+
77+
impl<'a, 'tcx> DataflowOperator for MaybeStorageLive<'a, 'tcx> {
78+
#[inline]
79+
fn bottom_value() -> bool {
80+
false // bottom = dead
81+
}
82+
}

src/librustc_mir/dataflow/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::mem;
2424
use std::path::PathBuf;
2525
use std::usize;
2626

27+
pub use self::impls::{MaybeStorageLive};
2728
pub use self::impls::{MaybeInitializedLvals, MaybeUninitializedLvals};
2829
pub use self::impls::{DefinitelyInitializedLvals};
2930
pub use self::impls::borrows::{Borrows, BorrowData, BorrowIndex};
@@ -351,6 +352,29 @@ pub trait DataflowResultsConsumer<'a, 'tcx: 'a> {
351352
flow_state: &mut Self::FlowState);
352353
}
353354

355+
pub fn state_for_location<T: BitDenotation>(loc: Location,
356+
analysis: &T,
357+
result: &DataflowResults<T>)
358+
-> IdxSetBuf<T::Idx> {
359+
let mut entry = result.sets().on_entry_set_for(loc.block.index()).to_owned();
360+
361+
{
362+
let mut sets = BlockSets {
363+
on_entry: &mut entry.clone(),
364+
kill_set: &mut entry.clone(),
365+
gen_set: &mut entry,
366+
};
367+
368+
for stmt in 0..loc.statement_index {
369+
let mut stmt_loc = loc;
370+
stmt_loc.statement_index = stmt;
371+
analysis.statement_effect(&mut sets, stmt_loc);
372+
}
373+
}
374+
375+
entry
376+
}
377+
354378
pub struct DataflowAnalysis<'a, 'tcx: 'a, O> where O: BitDenotation
355379
{
356380
flow_state: DataflowState<O>,

0 commit comments

Comments
 (0)