Re:The Death of Optimizing Compilers
by kibwen on Saturday March 14, 2015 @11:37PM
(#9205273)
It's not a priority for Rust to hoist subscript checks in loops, because Rust uses iterators pervasively and iterators are guaranteed to check the bounds of the array no more than once.
[ Parent
| on HN ]
LLVM (used by rustc) is perfectly capable of hoisting and eliminating subscript checks, although I believe rustc doesn't necessarily drive LLVM in the optimal way, yet: https://github.com/rust-lang/rust/issues/22987
E.g. this has no bounds checks, and is in fact vectorised:
pub fn bad_sum(x: &[i32]) -> i32 {
let x = x; // *
let mut sum = 0;
for i in 0..x.len() {
sum += x[i]
}
sum
}
The line marked * is needed because of https://github.com/rust-lang/rust/issues/22924 which is caused by https://llvm.org/bugs/show_bug.cgi?id=22786 .Write a matrix multiply. Or a linear equation solver.
The bounds checks for matrix multiplication over fixed length arrays are completely optimized out by LLVM, and LLVM will also optimize the bounds checks for non-fixed-length arrays in many cases. The slice length is an SSA value, so it's subject to constant propagation and SCCP.