Skip to content

Commit fb3d1d0

Browse files
committed
add inline attribute and codegen test
1 parent 311c3b7 commit fb3d1d0

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Diff for: library/core/src/slice/rotate.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type BufType = [usize; 32];
1010
/// # Safety
1111
///
1212
/// The specified range must be valid for reading and writing.
13+
#[inline]
1314
pub(super) unsafe fn ptr_rotate<T>(left: usize, mid: *mut T, right: usize) {
1415
if T::IS_ZST {
1516
return;
@@ -43,6 +44,7 @@ pub(super) unsafe fn ptr_rotate<T>(left: usize, mid: *mut T, right: usize) {
4344
/// # Safety
4445
///
4546
/// The specified range must be valid for reading and writing.
47+
#[inline]
4648
unsafe fn ptr_rotate_memmove<T>(left: usize, mid: *mut T, right: usize) {
4749
// The `[T; 0]` here is to ensure this is appropriately aligned for T
4850
let mut rawarray = MaybeUninit::<(BufType, [T; 0])>::uninit();
@@ -114,6 +116,7 @@ unsafe fn ptr_rotate_memmove<T>(left: usize, mid: *mut T, right: usize) {
114116
/// # Safety
115117
///
116118
/// The specified range must be valid for reading and writing.
119+
#[inline]
117120
unsafe fn ptr_rotate_gcd<T>(left: usize, mid: *mut T, right: usize) {
118121
// Algorithm 2
119122
// Microbenchmarks indicate that the average performance for random shifts is better all
@@ -218,6 +221,7 @@ unsafe fn ptr_rotate_gcd<T>(left: usize, mid: *mut T, right: usize) {
218221
/// # Safety
219222
///
220223
/// The specified range must be valid for reading and writing.
224+
#[inline]
221225
unsafe fn ptr_rotate_swap<T>(mut left: usize, mut mid: *mut T, mut right: usize) {
222226
loop {
223227
if left >= right {

Diff for: tests/codegen/lib-optimizations/slice_rotate.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// Ensure that the simple case of rotating by a constant 1 optimizes to the obvious thing
6+
7+
// CHECK-LABEL: @rotate_left_by_one
8+
#[no_mangle]
9+
pub fn rotate_left_by_one(slice: &mut [i32]) {
10+
// CHECK-NOT: phi
11+
// CHECK-NOT: call
12+
// CHECK-NOT: load
13+
// CHECK-NOT: store
14+
// CHECK-NOT: getelementptr
15+
// CHECK: %[[END:.+]] = getelementptr
16+
// CHECK-NEXT: %[[DIM:.+]] = getelementptr
17+
// CHECK-NEXT: %[[LAST:.+]] = load
18+
// CHECK-NEXT: %[[FIRST:.+]] = shl
19+
// CHECK-NEXT: call void @llvm.memmove
20+
// CHECK-NEXT: store i32 %[[LAST]], ptr %[[DIM:.+]]
21+
// CHECK-NOT: phi
22+
// CHECK-NOT: call
23+
// CHECK-NOT: load
24+
// CHECK-NOT: store
25+
// CHECK-NOT: getelementptr
26+
// CHECK: ret void
27+
if !slice.is_empty() {
28+
slice.rotate_left(1);
29+
}
30+
}

0 commit comments

Comments
 (0)