Skip to content

Commit 585f3ee

Browse files
committed
Auto merge of #107851 - cjgillot:sroa-const, r=oli-obk
Put deaggregated statements after original constant. Fixes #107818
2 parents 2b3f260 + 221ea30 commit 585f3ee

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

compiler/rustc_mir_transform/src/sroa.rs

+2
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ impl<'tcx, 'll> MutVisitor<'tcx> for ReplacementVisitor<'tcx, 'll> {
318318
// ConstProp will pick up the pieces and replace them by actual constants.
319319
StatementKind::Assign(box (place, Rvalue::Use(Operand::Constant(_)))) => {
320320
if let Some(final_locals) = self.replacements.place_fragments(place) {
321+
// Put the deaggregated statements *after* the original one.
322+
let location = location.successor_within_block();
321323
for (field, ty, new_local) in final_locals {
322324
let rplace = self.tcx.mk_place_field(place, field, ty);
323325
let rvalue = Rvalue::Use(Operand::Move(rplace));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `constant` before ScalarReplacementOfAggregates
2+
+ // MIR for `constant` after ScalarReplacementOfAggregates
3+
4+
fn constant() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/sroa.rs:+0:15: +0:15
6+
let _1: (usize, u8); // in scope 0 at $DIR/sroa.rs:+2:9: +2:10
7+
+ let _4: usize; // in scope 0 at $DIR/sroa.rs:+2:9: +2:10
8+
+ let _5: u8; // in scope 0 at $DIR/sroa.rs:+2:9: +2:10
9+
scope 1 {
10+
- debug y => _1; // in scope 1 at $DIR/sroa.rs:+2:9: +2:10
11+
+ debug y => (usize, u8){ .0 => _4, .1 => _5, }; // in scope 1 at $DIR/sroa.rs:+2:9: +2:10
12+
let _2: usize; // in scope 1 at $DIR/sroa.rs:+3:9: +3:10
13+
scope 2 {
14+
debug t => _2; // in scope 2 at $DIR/sroa.rs:+3:9: +3:10
15+
let _3: u8; // in scope 2 at $DIR/sroa.rs:+4:9: +4:10
16+
scope 3 {
17+
debug u => _3; // in scope 3 at $DIR/sroa.rs:+4:9: +4:10
18+
}
19+
}
20+
}
21+
22+
bb0: {
23+
- StorageLive(_1); // scope 0 at $DIR/sroa.rs:+2:9: +2:10
24+
+ StorageLive(_4); // scope 0 at $DIR/sroa.rs:+2:9: +2:10
25+
+ StorageLive(_5); // scope 0 at $DIR/sroa.rs:+2:9: +2:10
26+
+ nop; // scope 0 at $DIR/sroa.rs:+2:9: +2:10
27+
_1 = const _; // scope 0 at $DIR/sroa.rs:+2:13: +2:14
28+
+ _4 = move (_1.0: usize); // scope 1 at $DIR/sroa.rs:+3:9: +3:10
29+
+ _5 = move (_1.1: u8); // scope 1 at $DIR/sroa.rs:+3:9: +3:10
30+
StorageLive(_2); // scope 1 at $DIR/sroa.rs:+3:9: +3:10
31+
- _2 = (_1.0: usize); // scope 1 at $DIR/sroa.rs:+3:13: +3:16
32+
+ _2 = _4; // scope 1 at $DIR/sroa.rs:+3:13: +3:16
33+
StorageLive(_3); // scope 2 at $DIR/sroa.rs:+4:9: +4:10
34+
- _3 = (_1.1: u8); // scope 2 at $DIR/sroa.rs:+4:13: +4:16
35+
+ _3 = _5; // scope 2 at $DIR/sroa.rs:+4:13: +4:16
36+
_0 = const (); // scope 0 at $DIR/sroa.rs:+0:15: +5:2
37+
StorageDead(_3); // scope 2 at $DIR/sroa.rs:+5:1: +5:2
38+
StorageDead(_2); // scope 1 at $DIR/sroa.rs:+5:1: +5:2
39+
- StorageDead(_1); // scope 0 at $DIR/sroa.rs:+5:1: +5:2
40+
+ StorageDead(_4); // scope 0 at $DIR/sroa.rs:+5:1: +5:2
41+
+ StorageDead(_5); // scope 0 at $DIR/sroa.rs:+5:1: +5:2
42+
+ nop; // scope 0 at $DIR/sroa.rs:+5:1: +5:2
43+
return; // scope 0 at $DIR/sroa.rs:+5:2: +5:2
44+
}
45+
}
46+

tests/mir-opt/sroa.rs

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ fn ref_copies(x: &Foo) {
8787
let u = y.c;
8888
}
8989

90+
fn constant() {
91+
const U: (usize, u8) = (5, 9);
92+
let y = U;
93+
let t = y.0;
94+
let u = y.1;
95+
}
96+
9097
fn main() {
9198
dropping();
9299
enums(5);
@@ -96,6 +103,7 @@ fn main() {
96103
escaping();
97104
copies(Foo { a: 5, b: (), c: "a", d: Some(-4) });
98105
ref_copies(&Foo { a: 5, b: (), c: "a", d: Some(-4) });
106+
constant();
99107
}
100108

101109
// EMIT_MIR sroa.dropping.ScalarReplacementOfAggregates.diff
@@ -106,3 +114,4 @@ fn main() {
106114
// EMIT_MIR sroa.escaping.ScalarReplacementOfAggregates.diff
107115
// EMIT_MIR sroa.copies.ScalarReplacementOfAggregates.diff
108116
// EMIT_MIR sroa.ref_copies.ScalarReplacementOfAggregates.diff
117+
// EMIT_MIR sroa.constant.ScalarReplacementOfAggregates.diff

0 commit comments

Comments
 (0)