Debian Codegraph @

gcc/testsuite/rust/compile/issue-1981.rs

raw
1 #![feature(intrinsics)]
2
3 #[lang = "sized"]
4 pub trait Sized {}
5
6 mod intrinsics {
7 extern "rust-intrinsic" {
8 pub fn offset<T>(ptr: *const T, count: isize) -> *const T;
9 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
10 pub fn move_val_init<T>(dst: *mut T, src: T);
11 pub fn uninit<T>() -> T;
12 }
13 }
14
15 mod ptr {
16 #[lang = "const_ptr"]
17 impl<T> *const T {
18 pub unsafe fn offset(self, count: isize) -> *const T {
19 crate::intrinsics::offset(self, count)
20 }
21 }
22
23 #[lang = "mut_ptr"]
24 impl<T> *mut T {
25 pub unsafe fn offset(self, count: isize) -> *mut T {
26 crate::intrinsics::offset(self, count) as *mut T
27 }
28 }
29
30 pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
31 let x = x as *mut u8;
32 let y = y as *mut u8;
33 let len = crate::mem::size_of::<T>() * count;
34 swap_nonoverlapping_bytes(x, y, len)
35 }
36
37 pub unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) {
38 // For types smaller than the block optimization below,
39 // just swap directly to avoid pessimizing codegen.
40 if crate::mem::size_of::<T>() < 32 {
41 let z = read(x);
42 crate::intrinsics::copy_nonoverlapping(y, x, 1);
43 write(y, z);
44 } else {
45 swap_nonoverlapping(x, y, 1);
46 }
47 }
48
49 pub unsafe fn write<T>(dst: *mut T, src: T) {
50 crate::intrinsics::move_val_init(&mut *dst, src)
51 }
52
53 pub unsafe fn read<T>(src: *const T) -> T {
54 let mut tmp: T = crate::mem::uninitialized();
55 crate::intrinsics::copy_nonoverlapping(src, &mut tmp, 1);
56 tmp
57 }
58
59 unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
60 struct Block(u64, u64, u64, u64);
61 struct UnalignedBlock(u64, u64, u64, u64);
62
63 let block_size = crate::mem::size_of::<Block>();
64
65 // Loop through x & y, copying them `Block` at a time
66 // The optimizer should unroll the loop fully for most types
67 // N.B. We can't use a for loop as the `range` impl calls `mem::swap` recursively
68 let mut i = 0;
69 while i + block_size <= len {
70 // Create some uninitialized memory as scratch space
71 // Declaring `t` here avoids aligning the stack when this loop is unused
72 let mut t: Block = crate::mem::uninitialized();
73 let t = &mut t as *mut _ as *mut u8;
74 let x = x.offset(i as isize);
75 let y = y.offset(i as isize);
76
77 // Swap a block of bytes of x & y, using t as a temporary buffer
78 // This should be optimized into efficient SIMD operations where available
79 crate::intrinsics::copy_nonoverlapping(x, t, block_size);
80 crate::intrinsics::copy_nonoverlapping(y, x, block_size);
81 crate::intrinsics::copy_nonoverlapping(t, y, block_size);
82 i += block_size;
83 }
84
85 if i < len {
86 // Swap any remaining bytes
87 let mut t: UnalignedBlock = crate::mem::uninitialized();
88 let rem = len - i;
89
90 let t = &mut t as *mut _ as *mut u8;
91 let x = x.offset(i as isize);
92 let y = y.offset(i as isize);
93
94 crate::intrinsics::copy_nonoverlapping(x, t, rem);
95 crate::intrinsics::copy_nonoverlapping(y, x, rem);
96 crate::intrinsics::copy_nonoverlapping(t, y, rem);
97 }
98 }
99 }
100
101 mod mem {
102 extern "rust-intrinsic" {
103 pub fn transmute<T, U>(_: T) -> U;
104 pub fn size_of<T>() -> usize;
105 }
106
107 pub fn swap<T>(x: &mut T, y: &mut T) {
108 unsafe {
109 crate::ptr::swap_nonoverlapping_one(x, y);
110 }
111 }
112
113 pub fn replace<T>(dest: &mut T, mut src: T) -> T {
114 swap(dest, &mut src);
115 src
116 }
117
118 pub unsafe fn uninitialized<T>() -> T {
119 crate::intrinsics::uninit()
120 }
121 }
122
123 trait Step {
124 fn replace_zero(&mut self) -> Self;
125 }
126
127 impl Step for i32 {
128 fn replace_zero(&mut self) -> Self {
129 crate::mem::replace(self, 0)
130 }
131 }
132
133 fn main() -> i32 {
134 let a = 123;
135 a.replace_zero();
136 a
137 }