Skip to content

Commit 81960b2

Browse files
committed
Create mapped places upon seeing them in the body.
1 parent d7c5937 commit 81960b2

11 files changed

+153
-198
lines changed

compiler/rustc_mir_dataflow/src/value_analysis.rs

+121-121
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'tcx> MirPass<'tcx> for DataflowConstProp {
6666
}
6767

6868
struct ConstAnalysis<'a, 'tcx> {
69-
map: Map,
69+
map: Map<'tcx>,
7070
tcx: TyCtxt<'tcx>,
7171
local_decls: &'a LocalDecls<'tcx>,
7272
ecx: InterpCx<'tcx, DummyMachine>,
@@ -78,7 +78,7 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
7878

7979
const NAME: &'static str = "ConstAnalysis";
8080

81-
fn map(&self) -> &Map {
81+
fn map(&self) -> &Map<'tcx> {
8282
&self.map
8383
}
8484

@@ -335,7 +335,7 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
335335
}
336336

337337
impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
338-
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map) -> Self {
338+
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map<'tcx>) -> Self {
339339
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
340340
Self {
341341
map,
@@ -565,12 +565,13 @@ impl<'tcx, 'locals> Collector<'tcx, 'locals> {
565565
Self { patch: Patch::new(tcx), local_decls }
566566
}
567567

568+
#[instrument(level = "trace", skip(self, ecx, map), ret)]
568569
fn try_make_constant(
569570
&self,
570571
ecx: &mut InterpCx<'tcx, DummyMachine>,
571572
place: Place<'tcx>,
572573
state: &State<FlatSet<Scalar>>,
573-
map: &Map,
574+
map: &Map<'tcx>,
574575
) -> Option<Const<'tcx>> {
575576
let ty = place.ty(self.local_decls, self.patch.tcx).ty;
576577
let layout = ecx.layout_of(ty).ok()?;
@@ -603,10 +604,11 @@ impl<'tcx, 'locals> Collector<'tcx, 'locals> {
603604
}
604605
}
605606

607+
#[instrument(level = "trace", skip(map), ret)]
606608
fn propagatable_scalar(
607609
place: PlaceIndex,
608610
state: &State<FlatSet<Scalar>>,
609-
map: &Map,
611+
map: &Map<'_>,
610612
) -> Option<Scalar> {
611613
if let FlatSet::Elem(value) = state.get_idx(place, map)
612614
&& value.try_to_scalar_int().is_ok()
@@ -618,14 +620,14 @@ fn propagatable_scalar(
618620
}
619621
}
620622

621-
#[instrument(level = "trace", skip(ecx, state, map))]
623+
#[instrument(level = "trace", skip(ecx, state, map), ret)]
622624
fn try_write_constant<'tcx>(
623625
ecx: &mut InterpCx<'tcx, DummyMachine>,
624626
dest: &PlaceTy<'tcx>,
625627
place: PlaceIndex,
626628
ty: Ty<'tcx>,
627629
state: &State<FlatSet<Scalar>>,
628-
map: &Map,
630+
map: &Map<'tcx>,
629631
) -> InterpResult<'tcx> {
630632
let layout = ecx.layout_of(ty)?;
631633

@@ -724,6 +726,7 @@ impl<'mir, 'tcx>
724726
{
725727
type FlowState = State<FlatSet<Scalar>>;
726728

729+
#[instrument(level = "trace", skip(self, results, statement))]
727730
fn visit_statement_before_primary_effect(
728731
&mut self,
729732
results: &mut Results<'tcx, ValueAnalysisWrapper<ConstAnalysis<'_, 'tcx>>>,
@@ -745,6 +748,7 @@ impl<'mir, 'tcx>
745748
}
746749
}
747750

751+
#[instrument(level = "trace", skip(self, results, statement))]
748752
fn visit_statement_after_primary_effect(
749753
&mut self,
750754
results: &mut Results<'tcx, ValueAnalysisWrapper<ConstAnalysis<'_, 'tcx>>>,
@@ -839,7 +843,7 @@ struct OperandCollector<'tcx, 'map, 'locals, 'a> {
839843
state: &'a State<FlatSet<Scalar>>,
840844
visitor: &'a mut Collector<'tcx, 'locals>,
841845
ecx: &'map mut InterpCx<'tcx, DummyMachine>,
842-
map: &'map Map,
846+
map: &'map Map<'tcx>,
843847
}
844848

845849
impl<'tcx> Visitor<'tcx> for OperandCollector<'tcx, '_, '_, '_> {

compiler/rustc_mir_transform/src/jump_threading.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct TOFinder<'tcx, 'a> {
157157
param_env: ty::ParamEnv<'tcx>,
158158
ecx: InterpCx<'tcx, DummyMachine>,
159159
body: &'a Body<'tcx>,
160-
map: &'a Map,
160+
map: &'a Map<'tcx>,
161161
loop_headers: &'a BitSet<BasicBlock>,
162162
/// We use an arena to avoid cloning the slices when cloning `state`.
163163
arena: &'a DroplessArena,

tests/mir-opt/dataflow-const-prop/repr_transparent.main.DataflowConstProp.diff

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
StorageDead(_5);
3333
StorageDead(_4);
3434
- _2 = I32(move _3);
35-
+ _2 = const I32(0_i32);
35+
+ _2 = I32(const 0_i32);
3636
StorageDead(_3);
3737
_0 = const ();
3838
StorageDead(_2);
@@ -42,10 +42,6 @@
4242
+ }
4343
+
4444
+ ALLOC0 (size: 4, align: 4) {
45-
+ 00 00 00 00 │ ....
46-
+ }
47-
+
48-
+ ALLOC1 (size: 4, align: 4) {
4945
+ 00 00 00 00 │ ....
5046
}
5147

tests/mir-opt/dataflow-const-prop/repr_transparent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fn main() {
1515
// CHECK: [[x]] = const I32(0_i32);
1616
let x = I32(0);
1717

18-
// CHECK: [[y]] = const I32(0_i32);
18+
// CHECK: [[y]] = I32(const 0_i32);
1919
let y = I32(x.0 + x.0);
2020
}

tests/mir-opt/dataflow-const-prop/struct.main.DataflowConstProp.32bit.diff

+5-24
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@
106106
- _7 = (_10.0: f32);
107107
+ _7 = const 4f32;
108108
StorageLive(_8);
109-
- _8 = (_10.1: std::option::Option<S>);
110-
+ _8 = const Option::<S>::Some(S(1_i32));
109+
_8 = (_10.1: std::option::Option<S>);
111110
StorageLive(_9);
112111
_9 = (_10.2: &[f32]);
113112
StorageDead(_10);
@@ -157,8 +156,7 @@
157156
+ _23 = const 82f32;
158157
StorageLive(_24);
159158
_37 = deref_copy (*_26);
160-
- _24 = ((*_37).1: std::option::Option<S>);
161-
+ _24 = const Option::<S>::Some(S(35_i32));
159+
_24 = ((*_37).1: std::option::Option<S>);
162160
StorageLive(_25);
163161
_38 = deref_copy (*_26);
164162
_25 = ((*_38).2: &[f32]);
@@ -168,12 +166,11 @@
168166
- _28 = _23;
169167
+ _28 = const 82f32;
170168
StorageLive(_29);
171-
- _29 = _24;
172-
+ _29 = const Option::<S>::Some(S(35_i32));
169+
_29 = _24;
173170
StorageLive(_30);
174171
_30 = _25;
175172
- _27 = BigStruct(move _28, move _29, move _30);
176-
+ _27 = BigStruct(const 82f32, const Option::<S>::Some(S(35_i32)), move _30);
173+
+ _27 = BigStruct(const 82f32, move _29, move _30);
177174
StorageDead(_30);
178175
StorageDead(_29);
179176
StorageDead(_28);
@@ -199,23 +196,7 @@
199196
}
200197
+ }
201198
+
202-
+ ALLOC6 (size: 8, align: 4) {
203-
+ 01 00 00 00 23 00 00 00 │ ....#...
204-
+ }
205-
+
206-
+ ALLOC7 (size: 8, align: 4) {
207-
+ 01 00 00 00 23 00 00 00 │ ....#...
208-
+ }
209-
+
210-
+ ALLOC8 (size: 8, align: 4) {
211-
+ 01 00 00 00 23 00 00 00 │ ....#...
212-
+ }
213-
+
214-
+ ALLOC9 (size: 8, align: 4) {
215-
+ 01 00 00 00 01 00 00 00 │ ........
216-
+ }
217-
+
218-
+ ALLOC10 (size: 4, align: 4) {
199+
+ ALLOC6 (size: 4, align: 4) {
219200
+ 01 00 00 00 │ ....
220201
}
221202

tests/mir-opt/dataflow-const-prop/struct.main.DataflowConstProp.64bit.diff

+5-24
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@
106106
- _7 = (_10.0: f32);
107107
+ _7 = const 4f32;
108108
StorageLive(_8);
109-
- _8 = (_10.1: std::option::Option<S>);
110-
+ _8 = const Option::<S>::Some(S(1_i32));
109+
_8 = (_10.1: std::option::Option<S>);
111110
StorageLive(_9);
112111
_9 = (_10.2: &[f32]);
113112
StorageDead(_10);
@@ -157,8 +156,7 @@
157156
+ _23 = const 82f32;
158157
StorageLive(_24);
159158
_37 = deref_copy (*_26);
160-
- _24 = ((*_37).1: std::option::Option<S>);
161-
+ _24 = const Option::<S>::Some(S(35_i32));
159+
_24 = ((*_37).1: std::option::Option<S>);
162160
StorageLive(_25);
163161
_38 = deref_copy (*_26);
164162
_25 = ((*_38).2: &[f32]);
@@ -168,12 +166,11 @@
168166
- _28 = _23;
169167
+ _28 = const 82f32;
170168
StorageLive(_29);
171-
- _29 = _24;
172-
+ _29 = const Option::<S>::Some(S(35_i32));
169+
_29 = _24;
173170
StorageLive(_30);
174171
_30 = _25;
175172
- _27 = BigStruct(move _28, move _29, move _30);
176-
+ _27 = BigStruct(const 82f32, const Option::<S>::Some(S(35_i32)), move _30);
173+
+ _27 = BigStruct(const 82f32, move _29, move _30);
177174
StorageDead(_30);
178175
StorageDead(_29);
179176
StorageDead(_28);
@@ -199,23 +196,7 @@
199196
}
200197
+ }
201198
+
202-
+ ALLOC6 (size: 8, align: 4) {
203-
+ 01 00 00 00 23 00 00 00 │ ....#...
204-
+ }
205-
+
206-
+ ALLOC7 (size: 8, align: 4) {
207-
+ 01 00 00 00 23 00 00 00 │ ....#...
208-
+ }
209-
+
210-
+ ALLOC8 (size: 8, align: 4) {
211-
+ 01 00 00 00 23 00 00 00 │ ....#...
212-
+ }
213-
+
214-
+ ALLOC9 (size: 8, align: 4) {
215-
+ 01 00 00 00 01 00 00 00 │ ........
216-
+ }
217-
+
218-
+ ALLOC10 (size: 4, align: 4) {
199+
+ ALLOC6 (size: 4, align: 4) {
219200
+ 01 00 00 00 │ ....
220201
}
221202

tests/mir-opt/dataflow-const-prop/struct.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn main() {
4545
const SMALL_VAL: SmallStruct = SmallStruct(4., Some(S(1)), &[]);
4646

4747
// CHECK: [[a1]] = const 4f32;
48-
// CHECK: [[b1]] = const Option::<S>::Some(S(1_i32));
48+
// CHECK: [[b1]] = ({{_.*}}.1: std::option::Option<S>);
4949
// CHECK: [[c1]] = ({{_.*}}.2: &[f32]);
5050
let SmallStruct(a1, b1, c1) = SMALL_VAL;
5151

@@ -68,12 +68,12 @@ fn main() {
6868

6969
static BIG_STAT: &BigStruct = &BigStruct(82., Some(S(35)), &[45., 72.]);
7070
// CHECK: [[a4]] = const 82f32;
71-
// CHECK: [[b4]] = const Option::<S>::Some(S(35_i32));
71+
// CHECK: [[b4]] = ((*{{_.*}}).1: std::option::Option<S>);
7272
// CHECK: [[c4]] = ((*{{_.*}}).2: &[f32]);
7373
let BigStruct(a4, b4, c4) = *BIG_STAT;
7474

7575
// We arbitrarily limit the size of synthetized values to 4 pointers.
7676
// `BigStruct` can be read, but we will keep a MIR aggregate for this.
77-
// CHECK: [[bs]] = BigStruct(const 82f32, const Option::<S>::Some(S(35_i32)), move {{_.*}});
77+
// CHECK: [[bs]] = BigStruct(const 82f32, move {{.*}}, move {{_.*}});
7878
let bs = BigStruct(a4, b4, c4);
7979
}

tests/mir-opt/dataflow-const-prop/tuple.main.DataflowConstProp.32bit.diff

+1-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
- _14 = _6;
8282
- _11 = (move _12, move _13, move _14);
8383
+ _14 = const 11_i32;
84-
+ _11 = (const 6_i32, const (2_i32, 3_i32), const 11_i32);
84+
+ _11 = (const 6_i32, move _13, const 11_i32);
8585
StorageDead(_14);
8686
StorageDead(_13);
8787
StorageDead(_12);
@@ -103,10 +103,6 @@
103103
+ }
104104
+
105105
+ ALLOC2 (size: 8, align: 4) {
106-
+ 02 00 00 00 03 00 00 00 │ ........
107-
+ }
108-
+
109-
+ ALLOC3 (size: 8, align: 4) {
110106
+ 01 00 00 00 02 00 00 00 │ ........
111107
}
112108

tests/mir-opt/dataflow-const-prop/tuple.main.DataflowConstProp.64bit.diff

+1-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
- _14 = _6;
8282
- _11 = (move _12, move _13, move _14);
8383
+ _14 = const 11_i32;
84-
+ _11 = (const 6_i32, const (2_i32, 3_i32), const 11_i32);
84+
+ _11 = (const 6_i32, move _13, const 11_i32);
8585
StorageDead(_14);
8686
StorageDead(_13);
8787
StorageDead(_12);
@@ -103,10 +103,6 @@
103103
+ }
104104
+
105105
+ ALLOC2 (size: 8, align: 4) {
106-
+ 02 00 00 00 03 00 00 00 │ ........
107-
+ }
108-
+
109-
+ ALLOC3 (size: 8, align: 4) {
110106
+ 01 00 00 00 02 00 00 00 │ ........
111107
}
112108

tests/mir-opt/dataflow-const-prop/tuple.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn main() {
2222
// CHECK: [[c]] = const 11_i32;
2323
let c = a.0 + a.1 + b;
2424

25-
// CHECK: [[d]] = (const 6_i32, const (2_i32, 3_i32), const 11_i32);
25+
// CHECK: [[a2:_.*]] = const (2_i32, 3_i32);
26+
// CHECK: [[d]] = (const 6_i32, move [[a2]], const 11_i32);
2627
let d = (b, a, c);
2728
}

0 commit comments

Comments
 (0)