Buffer initialization does not get optimized out

I have the following snippet (minimization of a similar problem encountered in real code):

const N: usize = 32;

fn read(block: &[u8; 8 * N]) -> [u64; N] {
    let (chunks, tail) = block.as_chunks();
    assert!(tail.is_empty());

    let mut block = [0u64; N];
    for i in 0..N {
        block[i] = u64::from_be_bytes(chunks[i]);
    }
    block
}

It gets compiled into the following assembly: Compiler Explorer (RISC-V is used for simplicity, the same issue is present on x86 as well)

As can be clearly seen, the compiler keeps initialization of the buffer (the memset part) despite it being fully overwritten shortly after. Is there a reason for it? On the first glance, it looks like a trivial optimization for the compiler. Interestingly, this memset persists even after the function gets inlined, so it's not some ABI peculiarity.

Note that I know that we can write the function using core::array::from_fn. In my code it can not be used, since next buffer values depend on previously computed buffer values. Also, using it still produces suboptimal code: Compiler Explorer (note the memcpy part)

Looks fine in x64-v3 at least: No memcpy, no alloca https://rust.godbolt.org/z/1MxznsxYd

Increasing N still results in memcpy: Compiler Explorer (granted, it's a 2 KiB buffer, but still) Simply enabling AVX2 produces the same result, so the difference is likely to be more about being able to use YMM registers and to fully unroll the loop.

No, it's known at compile time to the compiler. The bound checks get properly eliminated as can be easily seen in the generated assembly. Using let chunk = block[8 * i..][..8].try_into().unwrap(); produces the same result.

How many compilers have you wrote or worked with? You are asking compiler to notice that two regions of memory are overwritten and that new values don't depend on old values… pretty demanding task. Not impossible, but somewhere near “the bleeding edge” of how compilers work.

Compiler work perfectly fine with values that may be lifted to registers, if values are too big for that then it's entirelly different kettle of fish.

Yes. You are converting bleeding edge, ultra-hard, something that's still “state of the art” into something that was explored for decades. Resutls are radically different.

It's chicken-end-the-egg problem: before Rust LLVM had little incentive to base in-memory optimizations on the unique references, because C and C++ don't have them, in practice (yes, restrict exists, no it's not a replacement) — and Rust team is not yet established enough to explore that on Rust side, they are busy with many other things.

Bounds checks are easy. They don't touch memory.

It's just a tricky optimization. The compiler has to prove that you always write all the elements, and never use the previous ones. But I guess it should be doable for simple in-order loops like this.

There is a ticket for a similar example: [DSE] Stores not eliminated if they are overwritten in a loop.

Optimizes well on -C opt-level=3. If you could make an example with dependent initialization, though, I'd appreciate it; I do have a sketch of an idea (instead of referencing previous values in the destination array, recalculate whatever would be written there, and mix with some optimization hints).

The memset comes back when you increase N so the loop doesn't get unrolled.