Skip to content

Commit 051dd67

Browse files
committed
Remove duplication.
1 parent 3483e77 commit 051dd67

10 files changed

+541
-1406
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// MIR for `filter_mapped` after PreCodegen
2+
3+
fn filter_mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> Option<U>) -> () {
4+
debug iter => _1; // in scope 0 at $DIR/loops.rs:+0:28: +0:32
5+
debug f => _2; // in scope 0 at $DIR/loops.rs:+0:59: +0:60
6+
let mut _0: (); // return place in scope 0 at $DIR/loops.rs:+0:87: +0:87
7+
let mut _3: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32
8+
let mut _4: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32
9+
let mut _5: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32
10+
let mut _6: &mut std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32
11+
let mut _9: std::option::Option<U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32
12+
let mut _10: isize; // in scope 0 at $DIR/loops.rs:+1:5: +3:6
13+
let _12: (); // in scope 0 at $DIR/loops.rs:+1:14: +1:32
14+
scope 1 {
15+
debug iter => _5; // in scope 1 at $DIR/loops.rs:+1:14: +1:32
16+
let _11: U; // in scope 1 at $DIR/loops.rs:+1:9: +1:10
17+
scope 2 {
18+
debug x => _11; // in scope 2 at $DIR/loops.rs:+1:9: +1:10
19+
}
20+
scope 4 (inlined <FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> as Iterator>::next) { // at $DIR/loops.rs:19:14: 19:32
21+
debug self => _6; // in scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
22+
let mut _7: &mut impl Iterator<Item = T>; // in scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
23+
let mut _8: &mut impl Fn(T) -> Option<U>; // in scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
24+
}
25+
}
26+
scope 3 (inlined <FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> as IntoIterator>::into_iter) { // at $DIR/loops.rs:19:14: 19:32
27+
debug self => _3; // in scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
28+
}
29+
30+
bb0: {
31+
StorageLive(_4); // scope 0 at $DIR/loops.rs:+1:14: +1:32
32+
StorageLive(_3); // scope 0 at $DIR/loops.rs:+1:14: +1:32
33+
_3 = <impl Iterator<Item = T> as Iterator>::filter_map::<U, impl Fn(T) -> Option<U>>(move _1, move _2) -> bb1; // scope 0 at $DIR/loops.rs:+1:14: +1:32
34+
// mir::Constant
35+
// + span: $DIR/loops.rs:19:19: 19:29
36+
// + literal: Const { ty: fn(impl Iterator<Item = T>, impl Fn(T) -> Option<U>) -> FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> {<impl Iterator<Item = T> as Iterator>::filter_map::<U, impl Fn(T) -> Option<U>>}, val: Value(<ZST>) }
37+
}
38+
39+
bb1: {
40+
_4 = move _3; // scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
41+
StorageDead(_3); // scope 0 at $DIR/loops.rs:+1:31: +1:32
42+
StorageLive(_5); // scope 0 at $DIR/loops.rs:+1:14: +1:32
43+
_5 = move _4; // scope 0 at $DIR/loops.rs:+1:14: +1:32
44+
goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6
45+
}
46+
47+
bb2: {
48+
StorageLive(_9); // scope 1 at $DIR/loops.rs:+1:14: +1:32
49+
_6 = &mut _5; // scope 1 at $DIR/loops.rs:+1:14: +1:32
50+
StorageLive(_7); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
51+
_7 = &mut ((*_6).0: impl Iterator<Item = T>); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
52+
StorageLive(_8); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
53+
_8 = &mut ((*_6).1: impl Fn(T) -> Option<U>); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
54+
_9 = <impl Iterator<Item = T> as Iterator>::find_map::<U, &mut impl Fn(T) -> Option<U>>(move _7, move _8) -> [return: bb3, unwind: bb9]; // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
55+
// mir::Constant
56+
// + span: $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
57+
// + literal: Const { ty: for<'a> fn(&'a mut impl Iterator<Item = T>, &mut impl Fn(T) -> Option<U>) -> Option<U> {<impl Iterator<Item = T> as Iterator>::find_map::<U, &mut impl Fn(T) -> Option<U>>}, val: Value(<ZST>) }
58+
}
59+
60+
bb3: {
61+
StorageDead(_8); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
62+
StorageDead(_7); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL
63+
_10 = discriminant(_9); // scope 1 at $DIR/loops.rs:+1:14: +1:32
64+
switchInt(move _10) -> [0: bb4, 1: bb6, otherwise: bb8]; // scope 1 at $DIR/loops.rs:+1:14: +1:32
65+
}
66+
67+
bb4: {
68+
StorageDead(_9); // scope 1 at $DIR/loops.rs:+3:5: +3:6
69+
drop(_5) -> bb5; // scope 0 at $DIR/loops.rs:+3:5: +3:6
70+
}
71+
72+
bb5: {
73+
StorageDead(_5); // scope 0 at $DIR/loops.rs:+3:5: +3:6
74+
StorageDead(_4); // scope 0 at $DIR/loops.rs:+3:5: +3:6
75+
return; // scope 0 at $DIR/loops.rs:+4:2: +4:2
76+
}
77+
78+
bb6: {
79+
_11 = move ((_9 as Some).0: U); // scope 1 at $DIR/loops.rs:+1:9: +1:10
80+
_12 = opaque::<U>(move _11) -> [return: bb7, unwind: bb9]; // scope 2 at $DIR/loops.rs:+2:9: +2:18
81+
// mir::Constant
82+
// + span: $DIR/loops.rs:20:9: 20:15
83+
// + literal: Const { ty: fn(U) {opaque::<U>}, val: Value(<ZST>) }
84+
}
85+
86+
bb7: {
87+
StorageDead(_9); // scope 1 at $DIR/loops.rs:+3:5: +3:6
88+
goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6
89+
}
90+
91+
bb8: {
92+
unreachable; // scope 1 at $DIR/loops.rs:+1:14: +1:32
93+
}
94+
95+
bb9 (cleanup): {
96+
drop(_5) -> [return: bb10, unwind terminate]; // scope 0 at $DIR/loops.rs:+3:5: +3:6
97+
}
98+
99+
bb10 (cleanup): {
100+
resume; // scope 0 at $DIR/loops.rs:+0:1: +4:2
101+
}
102+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// MIR for `mapped` after PreCodegen
2+
3+
fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
4+
debug iter => _1; // in scope 0 at $DIR/loops.rs:+0:21: +0:25
5+
debug f => _2; // in scope 0 at $DIR/loops.rs:+0:52: +0:53
6+
let mut _0: (); // return place in scope 0 at $DIR/loops.rs:+0:72: +0:72
7+
let mut _3: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25
8+
let mut _4: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25
9+
let mut _5: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25
10+
let mut _6: &mut std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25
11+
let mut _7: std::option::Option<U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25
12+
let mut _8: isize; // in scope 0 at $DIR/loops.rs:+1:5: +3:6
13+
let _10: (); // in scope 0 at $DIR/loops.rs:+1:14: +1:25
14+
scope 1 {
15+
debug iter => _5; // in scope 1 at $DIR/loops.rs:+1:14: +1:25
16+
let _9: U; // in scope 1 at $DIR/loops.rs:+1:9: +1:10
17+
scope 2 {
18+
debug x => _9; // in scope 2 at $DIR/loops.rs:+1:9: +1:10
19+
}
20+
}
21+
scope 3 (inlined <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as IntoIterator>::into_iter) { // at $DIR/loops.rs:13:14: 13:25
22+
debug self => _3; // in scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
23+
}
24+
25+
bb0: {
26+
StorageLive(_4); // scope 0 at $DIR/loops.rs:+1:14: +1:25
27+
StorageLive(_3); // scope 0 at $DIR/loops.rs:+1:14: +1:25
28+
_3 = <impl Iterator<Item = T> as Iterator>::map::<U, impl Fn(T) -> U>(move _1, move _2) -> bb1; // scope 0 at $DIR/loops.rs:+1:14: +1:25
29+
// mir::Constant
30+
// + span: $DIR/loops.rs:13:19: 13:22
31+
// + literal: Const { ty: fn(impl Iterator<Item = T>, impl Fn(T) -> U) -> Map<impl Iterator<Item = T>, impl Fn(T) -> U> {<impl Iterator<Item = T> as Iterator>::map::<U, impl Fn(T) -> U>}, val: Value(<ZST>) }
32+
}
33+
34+
bb1: {
35+
_4 = move _3; // scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
36+
StorageDead(_3); // scope 0 at $DIR/loops.rs:+1:24: +1:25
37+
StorageLive(_5); // scope 0 at $DIR/loops.rs:+1:14: +1:25
38+
_5 = move _4; // scope 0 at $DIR/loops.rs:+1:14: +1:25
39+
goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6
40+
}
41+
42+
bb2: {
43+
StorageLive(_7); // scope 1 at $DIR/loops.rs:+1:14: +1:25
44+
_6 = &mut _5; // scope 1 at $DIR/loops.rs:+1:14: +1:25
45+
_7 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(_6) -> [return: bb3, unwind: bb9]; // scope 1 at $DIR/loops.rs:+1:14: +1:25
46+
// mir::Constant
47+
// + span: $DIR/loops.rs:13:14: 13:25
48+
// + literal: Const { ty: for<'a> fn(&'a mut Map<impl Iterator<Item = T>, impl Fn(T) -> U>) -> Option<<Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::Item> {<Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next}, val: Value(<ZST>) }
49+
}
50+
51+
bb3: {
52+
_8 = discriminant(_7); // scope 1 at $DIR/loops.rs:+1:14: +1:25
53+
switchInt(move _8) -> [0: bb4, 1: bb6, otherwise: bb8]; // scope 1 at $DIR/loops.rs:+1:14: +1:25
54+
}
55+
56+
bb4: {
57+
StorageDead(_7); // scope 1 at $DIR/loops.rs:+3:5: +3:6
58+
drop(_5) -> bb5; // scope 0 at $DIR/loops.rs:+3:5: +3:6
59+
}
60+
61+
bb5: {
62+
StorageDead(_5); // scope 0 at $DIR/loops.rs:+3:5: +3:6
63+
StorageDead(_4); // scope 0 at $DIR/loops.rs:+3:5: +3:6
64+
return; // scope 0 at $DIR/loops.rs:+4:2: +4:2
65+
}
66+
67+
bb6: {
68+
_9 = move ((_7 as Some).0: U); // scope 1 at $DIR/loops.rs:+1:9: +1:10
69+
_10 = opaque::<U>(move _9) -> [return: bb7, unwind: bb9]; // scope 2 at $DIR/loops.rs:+2:9: +2:18
70+
// mir::Constant
71+
// + span: $DIR/loops.rs:14:9: 14:15
72+
// + literal: Const { ty: fn(U) {opaque::<U>}, val: Value(<ZST>) }
73+
}
74+
75+
bb7: {
76+
StorageDead(_7); // scope 1 at $DIR/loops.rs:+3:5: +3:6
77+
goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6
78+
}
79+
80+
bb8: {
81+
unreachable; // scope 1 at $DIR/loops.rs:+1:14: +1:25
82+
}
83+
84+
bb9 (cleanup): {
85+
drop(_5) -> [return: bb10, unwind terminate]; // scope 0 at $DIR/loops.rs:+3:5: +3:6
86+
}
87+
88+
bb10 (cleanup): {
89+
resume; // scope 0 at $DIR/loops.rs:+0:1: +4:2
90+
}
91+
}

tests/mir-opt/pre-codegen/loops.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,15 @@ pub fn int_range(start: usize, end: usize) {
99
}
1010
}
1111

12-
pub fn vec_range(mut v: Vec<impl Sized>) {
13-
for i in 0..v.len() {
14-
let x = &mut v[i];
15-
opaque((i, x))
16-
}
17-
for i in 0..v.len() {
18-
let x = &v[i];
19-
opaque((i, x))
20-
}
21-
}
22-
23-
pub fn vec_iter(mut v: Vec<impl Sized>) {
24-
for x in v.iter_mut() {
25-
opaque(x)
26-
}
27-
for x in v.iter() {
12+
pub fn mapped<T, U>(iter: impl Iterator<Item = T>, f: impl Fn(T) -> U) {
13+
for x in iter.map(f) {
2814
opaque(x)
2915
}
3016
}
3117

32-
pub fn vec_iter_enumerate(mut v: Vec<impl Sized>) {
33-
for (i, x) in v.iter_mut().enumerate() {
34-
opaque((i, x))
35-
}
36-
for (i, x) in v.iter().enumerate() {
37-
opaque((i, x))
18+
pub fn filter_mapped<T, U>(iter: impl Iterator<Item = T>, f: impl Fn(T) -> Option<U>) {
19+
for x in iter.filter_map(f) {
20+
opaque(x)
3821
}
3922
}
4023

@@ -48,7 +31,6 @@ pub fn vec_move(mut v: Vec<impl Sized>) {
4831
fn opaque(_: impl Sized) {}
4932

5033
// EMIT_MIR loops.int_range.PreCodegen.after.mir
51-
// EMIT_MIR loops.vec_range.PreCodegen.after.mir
52-
// EMIT_MIR loops.vec_iter.PreCodegen.after.mir
53-
// EMIT_MIR loops.vec_iter_enumerate.PreCodegen.after.mir
34+
// EMIT_MIR loops.mapped.PreCodegen.after.mir
35+
// EMIT_MIR loops.filter_mapped.PreCodegen.after.mir
5436
// EMIT_MIR loops.vec_move.PreCodegen.after.mir

0 commit comments

Comments
 (0)