Skip to content

Commit 16d3e18

Browse files
committed
Auto merge of rust-lang#111447 - scottmcm:remove-more-assumes, r=thomcc
Remove useless `assume`s from `slice::iter(_mut)` You were right in rust-lang#111395 (comment), r? `@the8472` LLVM already removes these assumes while optimizing, as can be seen in <https://rust.godbolt.org/z/KTfWKbdEM>.
2 parents 9850584 + c50a2e1 commit 16d3e18

File tree

4 files changed

+300
-63
lines changed

4 files changed

+300
-63
lines changed

library/core/src/slice/iter.rs

-4
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ impl<'a, T> Iter<'a, T> {
9090
let ptr = slice.as_ptr();
9191
// SAFETY: Similar to `IterMut::new`.
9292
unsafe {
93-
assume(!ptr.is_null());
94-
9593
let end = if T::IS_ZST { invalid(slice.len()) } else { ptr.add(slice.len()) };
9694

9795
Self { ptr: NonNull::new_unchecked(ptr as *mut T), end, _marker: PhantomData }
@@ -228,8 +226,6 @@ impl<'a, T> IterMut<'a, T> {
228226
// See the `next_unchecked!` and `is_empty!` macros as well as the
229227
// `post_inc_start` method for more information.
230228
unsafe {
231-
assume(!ptr.is_null());
232-
233229
let end = if T::IS_ZST { invalid_mut(slice.len()) } else { ptr.add(slice.len()) };
234230

235231
Self { ptr: NonNull::new_unchecked(ptr), end, _marker: PhantomData }

tests/codegen/slice-iter-nonnull.rs

+35
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,38 @@ pub fn slice_iter_next_back<'a>(it: &mut std::slice::Iter<'a, u32>) -> Option<&'
4040

4141
it.next_back()
4242
}
43+
44+
// The slice iterator `new` methods used to `assume` that the pointer is non-null,
45+
// but passing slices already requires that, to the extent that LLVM actually
46+
// removed the `call @llvm.assume` anyway. These tests just demonstrate that the
47+
// attribute is there, and confirms adding the assume back doesn't do anything.
48+
49+
// CHECK-LABEL: @slice_iter_new
50+
// CHECK-SAME: (ptr noalias noundef nonnull {{.+}} %slice.0, {{.+}} noundef %slice.1)
51+
#[no_mangle]
52+
pub fn slice_iter_new(slice: &[u32]) -> std::slice::Iter<'_, u32> {
53+
// CHECK-NOT: slice
54+
// CHECK: %[[END:.+]] = getelementptr inbounds i32{{.+}} %slice.0{{.+}} %slice.1
55+
// CHECK-NOT: slice
56+
// CHECK: insertvalue {{.+}} ptr %slice.0, 0
57+
// CHECK-NOT: slice
58+
// CHECK: insertvalue {{.+}} ptr %[[END]], 1
59+
// CHECK-NOT: slice
60+
// CHECK: }
61+
slice.iter()
62+
}
63+
64+
// CHECK-LABEL: @slice_iter_mut_new
65+
// CHECK-SAME: (ptr noalias noundef nonnull {{.+}} %slice.0, {{.+}} noundef %slice.1)
66+
#[no_mangle]
67+
pub fn slice_iter_mut_new(slice: &mut [u32]) -> std::slice::IterMut<'_, u32> {
68+
// CHECK-NOT: slice
69+
// CHECK: %[[END:.+]] = getelementptr inbounds i32{{.+}} %slice.0{{.+}} %slice.1
70+
// CHECK-NOT: slice
71+
// CHECK: insertvalue {{.+}} ptr %slice.0, 0
72+
// CHECK-NOT: slice
73+
// CHECK: insertvalue {{.+}} ptr %[[END]], 1
74+
// CHECK-NOT: slice
75+
// CHECK: }
76+
slice.iter_mut()
77+
}

tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir

+139-39
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,120 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
66
let mut _0: (); // return place in scope 0 at $DIR/slice_iter.rs:+0:60: +0:60
77
let mut _3: std::slice::Iter<'_, T>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
88
let mut _4: std::slice::Iter<'_, T>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
9-
let mut _5: std::slice::Iter<'_, T>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
10-
let _6: (); // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
11-
let mut _7: std::option::Option<&T>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
12-
let mut _8: &mut std::slice::Iter<'_, T>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
13-
let mut _9: isize; // in scope 0 at $DIR/slice_iter.rs:+1:5: +3:6
14-
let mut _11: &impl Fn(&T); // in scope 0 at $DIR/slice_iter.rs:+2:9: +2:10
15-
let mut _12: (&T,); // in scope 0 at $DIR/slice_iter.rs:+2:9: +2:13
9+
let _5: (); // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
10+
let mut _6: std::option::Option<&T>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
11+
let mut _7: &mut std::slice::Iter<'_, T>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
12+
let mut _8: isize; // in scope 0 at $DIR/slice_iter.rs:+1:5: +3:6
13+
let mut _10: &impl Fn(&T); // in scope 0 at $DIR/slice_iter.rs:+2:9: +2:10
14+
let mut _11: (&T,); // in scope 0 at $DIR/slice_iter.rs:+2:9: +2:13
1615
scope 1 {
17-
debug iter => _5; // in scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
18-
let _10: &T; // in scope 1 at $DIR/slice_iter.rs:+1:9: +1:10
16+
debug iter => _4; // in scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
17+
let _9: &T; // in scope 1 at $DIR/slice_iter.rs:+1:9: +1:10
1918
scope 2 {
20-
debug x => _10; // in scope 2 at $DIR/slice_iter.rs:+1:9: +1:10
19+
debug x => _9; // in scope 2 at $DIR/slice_iter.rs:+1:9: +1:10
2120
}
2221
}
2322
scope 3 (inlined core::slice::<impl [T]>::iter) { // at $DIR/slice_iter.rs:28:20: 28:26
2423
debug self => _1; // in scope 3 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
24+
scope 4 (inlined std::slice::Iter::<'_, T>::new) { // at $SRC_DIR/core/src/slice/mod.rs:LL:COL
25+
debug slice => _1; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
26+
let _12: *const T; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
27+
let mut _14: bool; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
28+
let mut _15: usize; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
29+
let mut _16: usize; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
30+
let mut _17: std::ptr::NonNull<T>; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
31+
let mut _18: *mut T; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
32+
let mut _19: *const T; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
33+
scope 5 {
34+
debug ptr => _12; // in scope 5 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
35+
scope 6 {
36+
let _13: *const T; // in scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
37+
scope 7 {
38+
debug end => _13; // in scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
39+
scope 13 (inlined NonNull::<T>::new_unchecked) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL
40+
debug ptr => _18; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
41+
let mut _21: *const T; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
42+
let mut _22: *mut T; // in scope 13 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
43+
scope 14 {
44+
scope 15 (inlined NonNull::<T>::new_unchecked::runtime::<T>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
45+
debug ptr => _22; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
46+
scope 16 (inlined ptr::mut_ptr::<impl *mut T>::is_null) { // at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
47+
debug self => _22; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
48+
let mut _23: *mut u8; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
49+
scope 17 {
50+
scope 18 (inlined ptr::mut_ptr::<impl *mut T>::is_null::runtime_impl) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
51+
debug ptr => _23; // in scope 18 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
52+
scope 19 (inlined ptr::mut_ptr::<impl *mut u8>::addr) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
53+
debug self => _23; // in scope 19 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
54+
scope 20 {
55+
scope 21 (inlined ptr::mut_ptr::<impl *mut u8>::cast::<()>) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
56+
debug self => _23; // in scope 21 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
scope 9 (inlined invalid::<T>) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL
68+
debug addr => _15; // in scope 9 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
69+
scope 10 {
70+
}
71+
}
72+
scope 11 (inlined ptr::const_ptr::<impl *const T>::add) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL
73+
debug self => _12; // in scope 11 at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
74+
debug count => _16; // in scope 11 at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
75+
scope 12 {
76+
}
77+
}
78+
}
79+
}
80+
scope 8 (inlined core::slice::<impl [T]>::as_ptr) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL
81+
debug self => _1; // in scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
82+
let mut _20: *const [T]; // in scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
83+
}
84+
}
2585
}
26-
scope 4 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) { // at $DIR/slice_iter.rs:28:14: 28:26
27-
debug self => _4; // in scope 4 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
86+
scope 22 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) { // at $DIR/slice_iter.rs:28:14: 28:26
87+
debug self => _3; // in scope 22 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
2888
}
2989

3090
bb0: {
31-
StorageLive(_3); // scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
32-
StorageLive(_4); // scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
33-
_4 = std::slice::Iter::<'_, T>::new(_1) -> [return: bb10, unwind: bb8]; // scope 3 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
34-
// mir::Constant
35-
// + span: $SRC_DIR/core/src/slice/mod.rs:LL:COL
36-
// + user_ty: UserType(0)
37-
// + literal: Const { ty: fn(&[T]) -> std::slice::Iter<'_, T> {std::slice::Iter::<'_, T>::new}, val: Value(<ZST>) }
91+
StorageLive(_12); // scope 3 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
92+
StorageLive(_20); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
93+
_20 = &raw const (*_1); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
94+
_12 = move _20 as *const T (PtrToPtr); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
95+
StorageDead(_20); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
96+
StorageLive(_13); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
97+
StorageLive(_14); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
98+
_14 = const _; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
99+
switchInt(move _14) -> [0: bb11, otherwise: bb10]; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
38100
}
39101

40102
bb1: {
41-
StorageLive(_7); // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
42-
_8 = &mut _5; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
43-
_7 = <std::slice::Iter<'_, T> as Iterator>::next(_8) -> [return: bb2, unwind: bb8]; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
103+
StorageLive(_6); // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
104+
_7 = &mut _4; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
105+
_6 = <std::slice::Iter<'_, T> as Iterator>::next(_7) -> [return: bb2, unwind: bb8]; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
44106
// mir::Constant
45107
// + span: $DIR/slice_iter.rs:28:14: 28:26
46108
// + literal: Const { ty: for<'a> fn(&'a mut std::slice::Iter<'_, T>) -> Option<<std::slice::Iter<'_, T> as Iterator>::Item> {<std::slice::Iter<'_, T> as Iterator>::next}, val: Value(<ZST>) }
47109
}
48110

49111
bb2: {
50-
_9 = discriminant(_7); // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
51-
switchInt(move _9) -> [0: bb5, 1: bb3, otherwise: bb4]; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
112+
_8 = discriminant(_6); // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
113+
switchInt(move _8) -> [0: bb5, 1: bb3, otherwise: bb4]; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:26
52114
}
53115

54116
bb3: {
55-
_10 = ((_7 as Some).0: &T); // scope 1 at $DIR/slice_iter.rs:+1:9: +1:10
56-
StorageLive(_11); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:10
57-
_11 = &_2; // scope 2 at $DIR/slice_iter.rs:+2:9: +2:10
58-
StorageLive(_12); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:13
59-
_12 = (_10,); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:13
60-
_6 = <impl Fn(&T) as Fn<(&T,)>>::call(move _11, move _12) -> [return: bb6, unwind: bb8]; // scope 2 at $DIR/slice_iter.rs:+2:9: +2:13
117+
_9 = ((_6 as Some).0: &T); // scope 1 at $DIR/slice_iter.rs:+1:9: +1:10
118+
StorageLive(_10); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:10
119+
_10 = &_2; // scope 2 at $DIR/slice_iter.rs:+2:9: +2:10
120+
StorageLive(_11); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:13
121+
_11 = (_9,); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:13
122+
_5 = <impl Fn(&T) as Fn<(&T,)>>::call(move _10, move _11) -> [return: bb6, unwind: bb8]; // scope 2 at $DIR/slice_iter.rs:+2:9: +2:13
61123
// mir::Constant
62124
// + span: $DIR/slice_iter.rs:29:9: 29:10
63125
// + literal: Const { ty: for<'a> extern "rust-call" fn(&'a impl Fn(&T), (&T,)) -> <impl Fn(&T) as FnOnce<(&T,)>>::Output {<impl Fn(&T) as Fn<(&T,)>>::call}, val: Value(<ZST>) }
@@ -68,16 +130,15 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
68130
}
69131

70132
bb5: {
71-
StorageDead(_7); // scope 1 at $DIR/slice_iter.rs:+3:5: +3:6
72-
StorageDead(_5); // scope 0 at $DIR/slice_iter.rs:+3:5: +3:6
73-
StorageDead(_3); // scope 0 at $DIR/slice_iter.rs:+3:5: +3:6
133+
StorageDead(_6); // scope 1 at $DIR/slice_iter.rs:+3:5: +3:6
134+
StorageDead(_4); // scope 0 at $DIR/slice_iter.rs:+3:5: +3:6
74135
drop(_2) -> bb7; // scope 0 at $DIR/slice_iter.rs:+4:1: +4:2
75136
}
76137

77138
bb6: {
78-
StorageDead(_12); // scope 2 at $DIR/slice_iter.rs:+2:12: +2:13
79139
StorageDead(_11); // scope 2 at $DIR/slice_iter.rs:+2:12: +2:13
80-
StorageDead(_7); // scope 1 at $DIR/slice_iter.rs:+3:5: +3:6
140+
StorageDead(_10); // scope 2 at $DIR/slice_iter.rs:+2:12: +2:13
141+
StorageDead(_6); // scope 1 at $DIR/slice_iter.rs:+3:5: +3:6
81142
goto -> bb1; // scope 1 at $DIR/slice_iter.rs:+1:5: +3:6
82143
}
83144

@@ -94,10 +155,49 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
94155
}
95156

96157
bb10: {
97-
_3 = move _4; // scope 4 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
98-
StorageDead(_4); // scope 0 at $DIR/slice_iter.rs:+1:25: +1:26
99-
StorageLive(_5); // scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
100-
_5 = move _3; // scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
158+
StorageLive(_15); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
159+
_15 = Len((*_1)); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
160+
_13 = _15 as *const T (Transmute); // scope 10 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
161+
StorageDead(_15); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
162+
goto -> bb12; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
163+
}
164+
165+
bb11: {
166+
StorageLive(_16); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
167+
_16 = Len((*_1)); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
168+
_13 = Offset(_12, _16); // scope 12 at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
169+
StorageDead(_16); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
170+
goto -> bb12; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
171+
}
172+
173+
bb12: {
174+
StorageDead(_14); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
175+
StorageLive(_17); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
176+
StorageLive(_18); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
177+
_18 = _12 as *mut T (PtrToPtr); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
178+
StorageLive(_21); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
179+
StorageLive(_22); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
180+
StorageLive(_23); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
181+
_21 = _18 as *const T (Pointer(MutToConstPointer)); // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
182+
_17 = NonNull::<T> { pointer: _21 }; // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
183+
StorageDead(_23); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
184+
StorageDead(_22); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
185+
StorageDead(_21); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
186+
StorageDead(_18); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
187+
StorageLive(_19); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
188+
_19 = _13; // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
189+
_3 = std::slice::Iter::<'_, T> { ptr: move _17, end: move _19, _marker: const ZeroSized: PhantomData<&T> }; // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
190+
// mir::Constant
191+
// + span: no-location
192+
// + literal: Const { ty: PhantomData<&T>, val: Value(<ZST>) }
193+
// adt
194+
// + user_ty: UserType(1)
195+
StorageDead(_19); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
196+
StorageDead(_17); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
197+
StorageDead(_13); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL
198+
StorageDead(_12); // scope 3 at $SRC_DIR/core/src/slice/mod.rs:LL:COL
199+
StorageLive(_4); // scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
200+
_4 = move _3; // scope 0 at $DIR/slice_iter.rs:+1:14: +1:26
101201
goto -> bb1; // scope 1 at $DIR/slice_iter.rs:+1:5: +3:6
102202
}
103203
}

0 commit comments

Comments
 (0)