Skip to content

Commit df4379b

Browse files
committed
Auto merge of rust-lang#116510 - scottmcm:no-1-simd-v2, r=compiler-errors
Copy 1-element arrays as scalars, not vectors For `[T; 1]` it's silly to copy as `<1 x T>` when we can just copy as `T`. Inspired by rust-lang#101210 (comment), which pointed out that `Option<[u8; 1]>` was codegenning worse than `Option<u8>`. (I'm not sure *why* LLVM doesn't optimize out `<1 x u8>`, but might as well just not emit it in the first place in this codepath.) --- I think I bit off too much in rust-lang#116479; let me try just the scalar case first. r? `@ghost`
2 parents 672fad9 + f5cdd3e commit df4379b

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

compiler/rustc_codegen_llvm/src/type_of.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,12 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
397397
// extracts all the individual values.
398398

399399
let ety = element.llvm_type(cx);
400-
return Some(cx.type_vector(ety, *count));
400+
if *count == 1 {
401+
// Emitting `<1 x T>` would be silly; just use the scalar.
402+
return Some(ety);
403+
} else {
404+
return Some(cx.type_vector(ety, *count));
405+
}
401406
}
402407

403408
// FIXME: The above only handled integer arrays; surely more things
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// assembly-output: emit-asm
2+
// compile-flags: --crate-type=lib -O -C llvm-args=-x86-asm-syntax=intel
3+
// only-x86_64
4+
// ignore-sgx
5+
// ignore-macos (manipulates rsp too)
6+
7+
// Depending on various codegen choices, this might end up copying
8+
// a `<2 x i8>`, an `i16`, or two `i8`s.
9+
// Regardless of those choices, make sure the instructions use (2-byte) words.
10+
11+
// CHECK-LABEL: array_copy_2_elements:
12+
#[no_mangle]
13+
pub fn array_copy_2_elements(a: &[u8; 2], p: &mut [u8; 2]) {
14+
// CHECK-NOT: byte
15+
// CHECK-NOT: mov
16+
// CHECK: mov{{.+}}, word ptr
17+
// CHECK-NEXT: mov word ptr
18+
// CHECK-NEXT: ret
19+
*p = *a;
20+
}

tests/codegen/array-codegen.rs

+22
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,25 @@ pub fn array_copy(a: &[u8; 4], p: &mut [u8; 4]) {
3232
// CHECK: store <4 x i8> %[[TEMP2]], ptr %p, align 1
3333
*p = *a;
3434
}
35+
36+
// CHECK-LABEL: @array_copy_1_element
37+
#[no_mangle]
38+
pub fn array_copy_1_element(a: &[u8; 1], p: &mut [u8; 1]) {
39+
// CHECK: %[[LOCAL:.+]] = alloca [1 x i8], align 1
40+
// CHECK: %[[TEMP1:.+]] = load i8, ptr %a, align 1
41+
// CHECK: store i8 %[[TEMP1]], ptr %[[LOCAL]], align 1
42+
// CHECK: %[[TEMP2:.+]] = load i8, ptr %[[LOCAL]], align 1
43+
// CHECK: store i8 %[[TEMP2]], ptr %p, align 1
44+
*p = *a;
45+
}
46+
47+
// CHECK-LABEL: @array_copy_2_elements
48+
#[no_mangle]
49+
pub fn array_copy_2_elements(a: &[u8; 2], p: &mut [u8; 2]) {
50+
// CHECK: %[[LOCAL:.+]] = alloca [2 x i8], align 1
51+
// CHECK: %[[TEMP1:.+]] = load <2 x i8>, ptr %a, align 1
52+
// CHECK: store <2 x i8> %[[TEMP1]], ptr %[[LOCAL]], align 1
53+
// CHECK: %[[TEMP2:.+]] = load <2 x i8>, ptr %[[LOCAL]], align 1
54+
// CHECK: store <2 x i8> %[[TEMP2]], ptr %p, align 1
55+
*p = *a;
56+
}

tests/codegen/array-optimized.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// CHECK-LABEL: @array_copy_1_element
6+
#[no_mangle]
7+
pub fn array_copy_1_element(a: &[u8; 1], p: &mut [u8; 1]) {
8+
// CHECK-NOT: alloca
9+
// CHECK: %[[TEMP:.+]] = load i8, ptr %a, align 1
10+
// CHECK: store i8 %[[TEMP]], ptr %p, align 1
11+
// CHECK: ret
12+
*p = *a;
13+
}
14+
15+
// CHECK-LABEL: @array_copy_2_elements
16+
#[no_mangle]
17+
pub fn array_copy_2_elements(a: &[u8; 2], p: &mut [u8; 2]) {
18+
// CHECK-NOT: alloca
19+
// CHECK: %[[TEMP:.+]] = load <2 x i8>, ptr %a, align 1
20+
// CHECK: store <2 x i8> %[[TEMP]], ptr %p, align 1
21+
// CHECK: ret
22+
*p = *a;
23+
}
24+
25+
// CHECK-LABEL: @array_copy_4_elements
26+
#[no_mangle]
27+
pub fn array_copy_4_elements(a: &[u8; 4], p: &mut [u8; 4]) {
28+
// CHECK-NOT: alloca
29+
// CHECK: %[[TEMP:.+]] = load <4 x i8>, ptr %a, align 1
30+
// CHECK: store <4 x i8> %[[TEMP]], ptr %p, align 1
31+
// CHECK: ret
32+
*p = *a;
33+
}

0 commit comments

Comments
 (0)