File: array-map.rs

package info (click to toggle)
rustc-web 1.70.0%2Bdfsg1-7~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,517,036 kB
  • sloc: xml: 147,962; javascript: 10,210; sh: 8,590; python: 8,220; ansic: 5,901; cpp: 4,635; makefile: 4,006; asm: 2,856
file content (49 lines) | stat: -rw-r--r-- 1,697 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// compile-flags: -C opt-level=3 -C target-cpu=x86-64-v3
// no-system-llvm
// only-x86_64
// ignore-debug (the extra assertions get in the way)

#![crate_type = "lib"]
#![feature(array_zip)]

// CHECK-LABEL: @short_integer_map
#[no_mangle]
pub fn short_integer_map(x: [u32; 8]) -> [u32; 8] {
    // CHECK: load <8 x i32>
    // CHECK: shl <8 x i32>
    // CHECK: or <8 x i32>
    // CHECK: store <8 x i32>
    x.map(|x| 2 * x + 1)
}

// CHECK-LABEL: @short_integer_zip_map
#[no_mangle]
pub fn short_integer_zip_map(x: [u32; 8], y: [u32; 8]) -> [u32; 8] {
    // CHECK: %[[A:.+]] = load <8 x i32>
    // CHECK: %[[B:.+]] = load <8 x i32>
    // CHECK: sub <8 x i32> %[[A]], %[[B]]
    // CHECK: store <8 x i32>
    x.zip(y).map(|(x, y)| x - y)
}

// This test is checking that LLVM can SRoA away a bunch of the overhead,
// like fully moving the iterators to registers.  Notably, previous implementations
// of `map` ended up `alloca`ing the whole `array::IntoIterator`, meaning both a
// hard-to-eliminate `memcpy` and that the iteration counts needed to be written
// out to stack every iteration, even for infallible operations on `Copy` types.
//
// This is still imperfect, as there's more copies than would be ideal,
// but hopefully work like #103830 will improve that in future,
// and update this test to be stricter.
//
// CHECK-LABEL: @long_integer_map
#[no_mangle]
pub fn long_integer_map(x: [u32; 512]) -> [u32; 512] {
    // CHECK: start:
    // CHECK-NEXT: alloca [512 x i32]
    // CHECK-NEXT: alloca %"core::mem::manually_drop::ManuallyDrop<[u32; 512]>"
    // CHECK-NOT: alloca
    // CHECK: mul <{{[0-9]+}} x i32>
    // CHECK: add <{{[0-9]+}} x i32>
    x.map(|x| 13 * x + 7)
}