Skip to content

Commit 6b6c1ff

Browse files
committed
Auto merge of #96596 - scottmcm:limited-calloc, r=Mark-Simulacrum
Tweak the vec-calloc runtime check to only apply to shortish-arrays r? `@Mark-Simulacrum` `@nbdd0121` pointed out in #95362 (comment) that LLVM currently doesn't constant-fold the `IsZero` check for long arrays, so that seems like a reasonable justification for limiting it. It appears that it's based on length, not byte size, (https://godbolt.org/z/4s48Y81dP), so that's what I used in the PR. Maybe it's a ["the number of inlining shall be three"](https://youtu.be/s4wnuiCwTGU?t=320) sort of situation. Certainly there's more that could be done here -- that generated code that checks long arrays byte-by-byte is highly suboptimal, for example -- but this is an easy, low-risk tweak.
2 parents 905fd73 + 2830dbd commit 6b6c1ff

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

library/alloc/src/vec/is_zero.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ unsafe impl<T> IsZero for *mut T {
5252
unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
5353
#[inline]
5454
fn is_zero(&self) -> bool {
55-
self.iter().all(IsZero::is_zero)
55+
// Because this is generated as a runtime check, it's not obvious that
56+
// it's worth doing if the array is really long. The threshold here
57+
// is largely arbitrary, but was picked because as of 2022-05-01 LLVM
58+
// can const-fold the check in `vec![[0; 32]; n]` but not in
59+
// `vec![[0; 64]; n]`: https://godbolt.org/z/WTzjzfs5b
60+
// Feel free to tweak if you have better evidence.
61+
62+
N <= 32 && self.iter().all(IsZero::is_zero)
5663
}
5764
}
5865

src/test/codegen/vec-calloc.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// compile-flags: -O
2+
// only-x86_64
3+
// ignore-debug
4+
5+
#![crate_type = "lib"]
6+
7+
// CHECK-LABEL: @vec_zero_scalar
8+
#[no_mangle]
9+
pub fn vec_zero_scalar(n: usize) -> Vec<i32> {
10+
// CHECK-NOT: __rust_alloc(
11+
// CHECK: __rust_alloc_zeroed(
12+
// CHECK-NOT: __rust_alloc(
13+
vec![0; n]
14+
}
15+
16+
// CHECK-LABEL: @vec_zero_rgb48
17+
#[no_mangle]
18+
pub fn vec_zero_rgb48(n: usize) -> Vec<[u16; 3]> {
19+
// CHECK-NOT: __rust_alloc(
20+
// CHECK: __rust_alloc_zeroed(
21+
// CHECK-NOT: __rust_alloc(
22+
vec![[0, 0, 0]; n]
23+
}
24+
25+
// CHECK-LABEL: @vec_zero_array_32
26+
#[no_mangle]
27+
pub fn vec_zero_array_32(n: usize) -> Vec<[i64; 32]> {
28+
// CHECK-NOT: __rust_alloc(
29+
// CHECK: __rust_alloc_zeroed(
30+
// CHECK-NOT: __rust_alloc(
31+
vec![[0_i64; 32]; n]
32+
}

0 commit comments

Comments
 (0)