Skip to content

Commit 3db197a

Browse files
committed
Auto merge of #38265 - bluss:mir-deaggregator-loop, r=nagisa
Reinstate while loop in deaggregator pass A previous commit must have removed the `while let` loop here by mistake; for each basic block, it should find and deaggregate multiple statements in their index order, and the `curr` index tracks the progress through the block. This fixes both the case of deaggregating statements in separate basic blocks (preserving `curr` could prevent that) as well as multiple times in the same block (missing loop prevented that). The loop was lost in commit bda46c2.
2 parents 6d5ec58 + fbc3f11 commit 3db197a

File tree

3 files changed

+163
-59
lines changed

3 files changed

+163
-59
lines changed

src/librustc_mir/transform/deaggregator.rs

+58-59
Original file line numberDiff line numberDiff line change
@@ -36,71 +36,70 @@ impl<'tcx> MirPass<'tcx> for Deaggregator {
3636
// In fact, we might not want to trigger in other cases.
3737
// Ex: when we could use SROA. See issue #35259
3838

39-
let mut curr: usize = 0;
4039
for bb in mir.basic_blocks_mut() {
41-
let idx = match get_aggregate_statement_index(curr, &bb.statements) {
42-
Some(idx) => idx,
43-
None => continue,
44-
};
45-
// do the replacement
46-
debug!("removing statement {:?}", idx);
47-
let src_info = bb.statements[idx].source_info;
48-
let suffix_stmts = bb.statements.split_off(idx+1);
49-
let orig_stmt = bb.statements.pop().unwrap();
50-
let (lhs, rhs) = match orig_stmt.kind {
51-
StatementKind::Assign(ref lhs, ref rhs) => (lhs, rhs),
52-
_ => span_bug!(src_info.span, "expected assign, not {:?}", orig_stmt),
53-
};
54-
let (agg_kind, operands) = match rhs {
55-
&Rvalue::Aggregate(ref agg_kind, ref operands) => (agg_kind, operands),
56-
_ => span_bug!(src_info.span, "expected aggregate, not {:?}", rhs),
57-
};
58-
let (adt_def, variant, substs) = match agg_kind {
59-
&AggregateKind::Adt(adt_def, variant, substs, None) => (adt_def, variant, substs),
60-
_ => span_bug!(src_info.span, "expected struct, not {:?}", rhs),
61-
};
62-
let n = bb.statements.len();
63-
bb.statements.reserve(n + operands.len() + suffix_stmts.len());
64-
for (i, op) in operands.iter().enumerate() {
65-
let ref variant_def = adt_def.variants[variant];
66-
let ty = variant_def.fields[i].ty(tcx, substs);
67-
let rhs = Rvalue::Use(op.clone());
68-
69-
let lhs_cast = if adt_def.variants.len() > 1 {
70-
Lvalue::Projection(Box::new(LvalueProjection {
71-
base: lhs.clone(),
72-
elem: ProjectionElem::Downcast(adt_def, variant),
73-
}))
74-
} else {
75-
lhs.clone()
40+
let mut curr: usize = 0;
41+
while let Some(idx) = get_aggregate_statement_index(curr, &bb.statements) {
42+
// do the replacement
43+
debug!("removing statement {:?}", idx);
44+
let src_info = bb.statements[idx].source_info;
45+
let suffix_stmts = bb.statements.split_off(idx+1);
46+
let orig_stmt = bb.statements.pop().unwrap();
47+
let (lhs, rhs) = match orig_stmt.kind {
48+
StatementKind::Assign(ref lhs, ref rhs) => (lhs, rhs),
49+
_ => span_bug!(src_info.span, "expected assign, not {:?}", orig_stmt),
7650
};
77-
78-
let lhs_proj = Lvalue::Projection(Box::new(LvalueProjection {
79-
base: lhs_cast,
80-
elem: ProjectionElem::Field(Field::new(i), ty),
81-
}));
82-
let new_statement = Statement {
83-
source_info: src_info,
84-
kind: StatementKind::Assign(lhs_proj, rhs),
51+
let (agg_kind, operands) = match rhs {
52+
&Rvalue::Aggregate(ref agg_kind, ref operands) => (agg_kind, operands),
53+
_ => span_bug!(src_info.span, "expected aggregate, not {:?}", rhs),
8554
};
86-
debug!("inserting: {:?} @ {:?}", new_statement, idx + i);
87-
bb.statements.push(new_statement);
88-
}
55+
let (adt_def, variant, substs) = match agg_kind {
56+
&AggregateKind::Adt(adt_def, variant, substs, None)
57+
=> (adt_def, variant, substs),
58+
_ => span_bug!(src_info.span, "expected struct, not {:?}", rhs),
59+
};
60+
let n = bb.statements.len();
61+
bb.statements.reserve(n + operands.len() + suffix_stmts.len());
62+
for (i, op) in operands.iter().enumerate() {
63+
let ref variant_def = adt_def.variants[variant];
64+
let ty = variant_def.fields[i].ty(tcx, substs);
65+
let rhs = Rvalue::Use(op.clone());
8966

90-
// if the aggregate was an enum, we need to set the discriminant
91-
if adt_def.variants.len() > 1 {
92-
let set_discriminant = Statement {
93-
kind: StatementKind::SetDiscriminant {
94-
lvalue: lhs.clone(),
95-
variant_index: variant,
96-
},
97-
source_info: src_info,
67+
let lhs_cast = if adt_def.variants.len() > 1 {
68+
Lvalue::Projection(Box::new(LvalueProjection {
69+
base: lhs.clone(),
70+
elem: ProjectionElem::Downcast(adt_def, variant),
71+
}))
72+
} else {
73+
lhs.clone()
74+
};
75+
76+
let lhs_proj = Lvalue::Projection(Box::new(LvalueProjection {
77+
base: lhs_cast,
78+
elem: ProjectionElem::Field(Field::new(i), ty),
79+
}));
80+
let new_statement = Statement {
81+
source_info: src_info,
82+
kind: StatementKind::Assign(lhs_proj, rhs),
83+
};
84+
debug!("inserting: {:?} @ {:?}", new_statement, idx + i);
85+
bb.statements.push(new_statement);
86+
}
87+
88+
// if the aggregate was an enum, we need to set the discriminant
89+
if adt_def.variants.len() > 1 {
90+
let set_discriminant = Statement {
91+
kind: StatementKind::SetDiscriminant {
92+
lvalue: lhs.clone(),
93+
variant_index: variant,
94+
},
95+
source_info: src_info,
96+
};
97+
bb.statements.push(set_discriminant);
9898
};
99-
bb.statements.push(set_discriminant);
100-
};
10199

102-
curr = bb.statements.len();
103-
bb.statements.extend(suffix_stmts);
100+
curr = bb.statements.len();
101+
bb.statements.extend(suffix_stmts);
102+
}
104103
}
105104
}
106105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2016 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+
// Test that deaggregate fires in more than one basic block
12+
13+
enum Foo {
14+
A(i32),
15+
B(i32),
16+
}
17+
18+
fn test1(x: bool, y: i32) -> Foo {
19+
if x {
20+
Foo::A(y)
21+
} else {
22+
Foo::B(y)
23+
}
24+
}
25+
26+
fn main() {}
27+
28+
// END RUST SOURCE
29+
// START rustc.node12.Deaggregator.before.mir
30+
// bb1: {
31+
// _6 = _4;
32+
// _0 = Foo::A(_6,);
33+
// goto -> bb3;
34+
// }
35+
//
36+
// bb2: {
37+
// _7 = _4;
38+
// _0 = Foo::B(_7,);
39+
// goto -> bb3;
40+
// }
41+
// END rustc.node12.Deaggregator.before.mir
42+
// START rustc.node12.Deaggregator.after.mir
43+
// bb1: {
44+
// _6 = _4;
45+
// ((_0 as A).0: i32) = _6;
46+
// discriminant(_0) = 0;
47+
// goto -> bb3;
48+
// }
49+
//
50+
// bb2: {
51+
// _7 = _4;
52+
// ((_0 as B).0: i32) = _7;
53+
// discriminant(_0) = 1;
54+
// goto -> bb3;
55+
// }
56+
// END rustc.node12.Deaggregator.after.mir
57+
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2016 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+
// Test that deaggregate fires more than once per block
12+
13+
enum Foo {
14+
A(i32),
15+
B,
16+
}
17+
18+
fn test(x: i32) -> [Foo; 2] {
19+
[Foo::A(x), Foo::A(x)]
20+
}
21+
22+
fn main() { }
23+
24+
// END RUST SOURCE
25+
// START rustc.node10.Deaggregator.before.mir
26+
// bb0: {
27+
// _2 = _1;
28+
// _4 = _2;
29+
// _3 = Foo::A(_4,);
30+
// _6 = _2;
31+
// _5 = Foo::A(_6,);
32+
// _0 = [_3, _5];
33+
// return;
34+
// }
35+
// END rustc.node10.Deaggregator.before.mir
36+
// START rustc.node10.Deaggregator.after.mir
37+
// bb0: {
38+
// _2 = _1;
39+
// _4 = _2;
40+
// ((_3 as A).0: i32) = _4;
41+
// discriminant(_3) = 0;
42+
// _6 = _2;
43+
// ((_5 as A).0: i32) = _6;
44+
// discriminant(_5) = 0;
45+
// _0 = [_3, _5];
46+
// return;
47+
// }
48+
// END rustc.node10.Deaggregator.after.mir

0 commit comments

Comments
 (0)