Skip to content

Commit 7d831d0

Browse files
committed
Auto merge of #48052 - eddyb:deggregate, r=<try>
rustc_mir: handle all aggregate kinds in, and always run, the deaggregator. This helps with removing`Rvalue::Aggregate` from the MIR, and with enabling more optimizations. r? @nikomatsakis
2 parents 4f93357 + 5cc4b94 commit 7d831d0

File tree

10 files changed

+109
-109
lines changed

10 files changed

+109
-109
lines changed

src/librustc_mir/transform/deaggregator.rs

Lines changed: 79 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,116 +21,103 @@ impl MirPass for Deaggregator {
2121
tcx: TyCtxt<'a, 'tcx, 'tcx>,
2222
source: MirSource,
2323
mir: &mut Mir<'tcx>) {
24-
let node_path = tcx.item_path_str(source.def_id);
25-
debug!("running on: {:?}", node_path);
26-
// we only run when mir_opt_level > 2
27-
if tcx.sess.opts.debugging_opts.mir_opt_level <= 2 {
28-
return;
29-
}
30-
3124
// Don't run on constant MIR, because trans might not be able to
3225
// evaluate the modified MIR.
3326
// FIXME(eddyb) Remove check after miri is merged.
3427
let id = tcx.hir.as_local_node_id(source.def_id).unwrap();
3528
match (tcx.hir.body_owner_kind(id), source.promoted) {
36-
(hir::BodyOwnerKind::Fn, None) => {},
37-
_ => return
29+
(_, Some(_)) |
30+
(hir::BodyOwnerKind::Const, _) |
31+
(hir::BodyOwnerKind::Static(_), _) => return,
32+
33+
(hir::BodyOwnerKind::Fn, _) => {
34+
if tcx.is_const_fn(source.def_id) {
35+
// Don't run on const functions, as, again, trans might not be able to evaluate
36+
// the optimized IR.
37+
return
38+
}
39+
}
3840
}
39-
// In fact, we might not want to trigger in other cases.
40-
// Ex: when we could use SROA. See issue #35259
4141

42-
for bb in mir.basic_blocks_mut() {
43-
let mut curr: usize = 0;
44-
while let Some(idx) = get_aggregate_statement_index(curr, &bb.statements) {
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);
42+
let can_deaggregate = |statement: &Statement| {
43+
if let StatementKind::Assign(_, ref rhs) = statement.kind {
44+
if let Rvalue::Aggregate(..) = *rhs {
45+
return true;
46+
}
47+
}
48+
49+
false
50+
};
51+
52+
let (basic_blocks, local_decls) = mir.basic_blocks_and_local_decls_mut();
53+
for bb in basic_blocks {
54+
let mut start = 0;
55+
while let Some(i) = bb.statements[start..].iter().position(&can_deaggregate) {
56+
let i = start + i;
57+
58+
// FIXME(eddyb) this is probably more expensive than it should be.
59+
// Ideally we'd move the block's statements all at once.
60+
let suffix_stmts = bb.statements.split_off(i + 1);
4961
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),
62+
let source_info = orig_stmt.source_info;
63+
let (mut lhs, kind, operands) = match orig_stmt.kind {
64+
StatementKind::Assign(lhs, Rvalue::Aggregate(kind, operands))
65+
=> (lhs, kind, operands),
66+
_ => bug!()
5767
};
58-
let (adt_def, variant, substs) = match **agg_kind {
59-
AggregateKind::Adt(adt_def, variant, substs, None)
60-
=> (adt_def, variant, substs),
61-
_ => span_bug!(src_info.span, "expected struct, not {:?}", rhs),
68+
69+
let mut set_discriminant = None;
70+
let active_field_index = match *kind {
71+
AggregateKind::Adt(adt_def, variant_index, _, active_field_index) => {
72+
if adt_def.is_enum() {
73+
set_discriminant = Some(Statement {
74+
kind: StatementKind::SetDiscriminant {
75+
place: lhs.clone(),
76+
variant_index,
77+
},
78+
source_info,
79+
});
80+
lhs = lhs.downcast(adt_def, variant_index);
81+
}
82+
active_field_index
83+
}
84+
_ => None
6285
};
63-
let n = bb.statements.len();
64-
bb.statements.reserve(n + operands.len() + suffix_stmts.len());
65-
for (i, op) in operands.iter().enumerate() {
66-
let ref variant_def = adt_def.variants[variant];
67-
let ty = variant_def.fields[i].ty(tcx, substs);
68-
let rhs = Rvalue::Use(op.clone());
6986

70-
let lhs_cast = if adt_def.is_enum() {
71-
Place::Projection(Box::new(PlaceProjection {
72-
base: lhs.clone(),
73-
elem: ProjectionElem::Downcast(adt_def, variant),
74-
}))
75-
} else {
76-
lhs.clone()
77-
};
87+
let new_total_count = bb.statements.len() +
88+
operands.len() +
89+
(set_discriminant.is_some() as usize) +
90+
suffix_stmts.len();
91+
bb.statements.reserve(new_total_count);
7892

79-
let lhs_proj = Place::Projection(Box::new(PlaceProjection {
80-
base: lhs_cast,
81-
elem: ProjectionElem::Field(Field::new(i), ty),
82-
}));
83-
let new_statement = Statement {
84-
source_info: src_info,
85-
kind: StatementKind::Assign(lhs_proj, rhs),
93+
for (j, op) in operands.into_iter().enumerate() {
94+
let lhs_field = if let AggregateKind::Array(_) = *kind {
95+
// FIXME(eddyb) `offset` should be u64.
96+
let offset = j as u32;
97+
assert_eq!(offset as usize, j);
98+
lhs.clone().elem(ProjectionElem::ConstantIndex {
99+
offset,
100+
// FIXME(eddyb) `min_length` doesn't appear to be used.
101+
min_length: offset + 1,
102+
from_end: false
103+
})
104+
} else {
105+
let ty = op.ty(local_decls, tcx);
106+
let field = Field::new(active_field_index.unwrap_or(j));
107+
lhs.clone().field(field, ty)
86108
};
87-
debug!("inserting: {:?} @ {:?}", new_statement, idx + i);
88-
bb.statements.push(new_statement);
109+
bb.statements.push(Statement {
110+
source_info,
111+
kind: StatementKind::Assign(lhs_field, Rvalue::Use(op)),
112+
});
89113
}
90114

91-
// if the aggregate was an enum, we need to set the discriminant
92-
if adt_def.is_enum() {
93-
let set_discriminant = Statement {
94-
kind: StatementKind::SetDiscriminant {
95-
place: lhs.clone(),
96-
variant_index: variant,
97-
},
98-
source_info: src_info,
99-
};
100-
bb.statements.push(set_discriminant);
101-
};
115+
// If the aggregate was an enum, we need to set the discriminant.
116+
bb.statements.extend(set_discriminant);
102117

103-
curr = bb.statements.len();
118+
start = bb.statements.len();
104119
bb.statements.extend(suffix_stmts);
105120
}
106121
}
107122
}
108123
}
109-
110-
fn get_aggregate_statement_index<'a, 'tcx, 'b>(start: usize,
111-
statements: &Vec<Statement<'tcx>>)
112-
-> Option<usize> {
113-
for i in start..statements.len() {
114-
let ref statement = statements[i];
115-
let rhs = match statement.kind {
116-
StatementKind::Assign(_, ref rhs) => rhs,
117-
_ => continue,
118-
};
119-
let (kind, operands) = match rhs {
120-
&Rvalue::Aggregate(ref kind, ref operands) => (kind, operands),
121-
_ => continue,
122-
};
123-
let (adt_def, variant) = match **kind {
124-
AggregateKind::Adt(adt_def, variant, _, None) => (adt_def, variant),
125-
_ => continue,
126-
};
127-
if operands.len() == 0 {
128-
// don't deaggregate ()
129-
continue;
130-
}
131-
debug!("getting variant {:?}", variant);
132-
debug!("for adt_def {:?}", adt_def);
133-
return Some(i);
134-
};
135-
None
136-
}

src/librustc_mir/transform/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
255255

256256
// Optimizations begin.
257257
inline::Inline,
258+
259+
// Lowering generator control-flow and variables
260+
// has to happen before we do anything else to them.
261+
generator::StateTransform,
262+
258263
instcombine::InstCombine,
259264
deaggregator::Deaggregator,
260265
copy_prop::CopyPropagation,

src/librustc_mir/transform/simplify.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use rustc_data_structures::indexed_vec::{Idx, IndexVec};
4242
use rustc::ty::TyCtxt;
4343
use rustc::mir::*;
4444
use rustc::mir::visit::{MutVisitor, Visitor, PlaceContext};
45+
use rustc::session::config::FullDebugInfo;
4546
use std::borrow::Cow;
4647
use transform::{MirPass, MirSource};
4748

@@ -281,16 +282,24 @@ pub struct SimplifyLocals;
281282

282283
impl MirPass for SimplifyLocals {
283284
fn run_pass<'a, 'tcx>(&self,
284-
_: TyCtxt<'a, 'tcx, 'tcx>,
285+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
285286
_: MirSource,
286287
mir: &mut Mir<'tcx>) {
287288
let mut marker = DeclMarker { locals: BitVector::new(mir.local_decls.len()) };
288289
marker.visit_mir(mir);
289290
// Return pointer and arguments are always live
290-
marker.locals.insert(0);
291-
for idx in mir.args_iter() {
292-
marker.locals.insert(idx.index());
291+
marker.locals.insert(RETURN_PLACE.index());
292+
for arg in mir.args_iter() {
293+
marker.locals.insert(arg.index());
293294
}
295+
296+
// We may need to keep dead user variables live for debuginfo.
297+
if tcx.sess.opts.debuginfo == FullDebugInfo {
298+
for local in mir.vars_iter() {
299+
marker.locals.insert(local.index());
300+
}
301+
}
302+
294303
let map = make_local_map(&mut mir.local_decls, marker.locals);
295304
// Update references to all vars and tmps now
296305
LocalUpdater { map: map }.visit_mir(mir);

src/test/codegen/lifetime_start_end.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ pub fn test() {
2828
// CHECK: [[S_b:%[0-9]+]] = bitcast %"core::option::Option<i32>"** %b to i8*
2929
// CHECK: call void @llvm.lifetime.start{{.*}}(i{{[0-9 ]+}}, i8* [[S_b]])
3030

31-
// CHECK: [[S__5:%[0-9]+]] = bitcast %"core::option::Option<i32>"* %_5 to i8*
32-
// CHECK: call void @llvm.lifetime.start{{.*}}(i{{[0-9 ]+}}, i8* [[S__5]])
31+
// CHECK: [[S__4:%[0-9]+]] = bitcast %"core::option::Option<i32>"* %_4 to i8*
32+
// CHECK: call void @llvm.lifetime.start{{.*}}(i{{[0-9 ]+}}, i8* [[S__4]])
3333

3434
// CHECK: [[E_b:%[0-9]+]] = bitcast %"core::option::Option<i32>"** %b to i8*
3535
// CHECK: call void @llvm.lifetime.end{{.*}}(i{{[0-9 ]+}}, i8* [[E_b]])
3636

37-
// CHECK: [[E__5:%[0-9]+]] = bitcast %"core::option::Option<i32>"* %_5 to i8*
38-
// CHECK: call void @llvm.lifetime.end{{.*}}(i{{[0-9 ]+}}, i8* [[E__5]])
37+
// CHECK: [[E__4:%[0-9]+]] = bitcast %"core::option::Option<i32>"* %_4 to i8*
38+
// CHECK: call void @llvm.lifetime.end{{.*}}(i{{[0-9 ]+}}, i8* [[E__4]])
3939
}
4040

4141
let c = 1;

src/test/codegen/match.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum E {
1919

2020
// CHECK-LABEL: @exhaustive_match
2121
#[no_mangle]
22-
pub fn exhaustive_match(e: E) {
22+
pub fn exhaustive_match(e: E, unit: ()) {
2323
// CHECK: switch{{.*}}, label %[[OTHERWISE:[a-zA-Z0-9_]+]] [
2424
// CHECK-NEXT: i[[TY:[0-9]+]] [[DISCR:[0-9]+]], label %[[A:[a-zA-Z0-9_]+]]
2525
// CHECK-NEXT: i[[TY:[0-9]+]] [[DISCR:[0-9]+]], label %[[B:[a-zA-Z0-9_]+]]
@@ -31,7 +31,7 @@ pub fn exhaustive_match(e: E) {
3131
// CHECK: [[OTHERWISE]]:
3232
// CHECK-NEXT: unreachable
3333
match e {
34-
E::A => (),
35-
E::B => (),
34+
E::A => unit,
35+
E::B => unit,
3636
}
3737
}

src/test/incremental/hashes/closure_expressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn change_parameter_pattern() {
6464
}
6565

6666
#[cfg(not(cfail1))]
67-
#[rustc_clean(cfg="cfail2", except="HirBody, MirValidated, MirOptimized, TypeckTables")]
67+
#[rustc_clean(cfg="cfail2", except="HirBody, MirValidated, TypeckTables")]
6868
#[rustc_clean(cfg="cfail3")]
6969
pub fn change_parameter_pattern() {
7070
let _ = |&x: &u32| x;

src/test/incremental/issue-38222.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#![feature(rustc_attrs)]
1919

2020

21-
#![rustc_partition_translated(module="issue_38222-mod1", cfg="rpass2")]
21+
#![rustc_partition_reused(module="issue_38222-mod1", cfg="rpass2")]
2222

2323
// If trans had added a dependency edge to the Krate dep-node, nothing would
2424
// be re-used, so checking that this module was re-used is sufficient.

src/test/mir-opt/copy_propagation_arg.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ fn main() {
7878
// bb1: {
7979
// StorageDead(_3);
8080
// _1 = const 5u8;
81-
// _0 = ();
8281
// return;
8382
// }
8483
// END rustc.bar.CopyPropagation.before.mir
@@ -100,7 +99,6 @@ fn main() {
10099
// _2 = _1;
101100
// _1 = move _2;
102101
// StorageDead(_2);
103-
// _0 = ();
104102
// return;
105103
// }
106104
// END rustc.baz.CopyPropagation.before.mir

src/test/mir-opt/deaggregator_test_multiple.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ fn main() {
5252
// ((_4 as A).0: i32) = move _5;
5353
// discriminant(_4) = 0;
5454
// ...
55-
// _0 = [move _2, move _4];
55+
// _0[0 of 1] = move _2;
56+
// _0[1 of 2] = move _4;
5657
// ...
5758
// return;
5859
// }

src/test/ui/print_type_sizes/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn f1<T:Copy>(x: T) {
7272
fn start(_: isize, _: *const *const u8) -> isize {
7373
let _b: Pair<u8> = Pair::new(0, 0);
7474
let _s: Pair<SevenBytes> = Pair::new(SevenBytes::new(), SevenBytes::new());
75-
let _z: ZeroSized = ZeroSized;
75+
let ref _z: ZeroSized = ZeroSized;
7676
f1::<SevenBytes>(SevenBytes::new());
7777
0
7878
}

0 commit comments

Comments
 (0)