rust-lang / rust-lang/rust

Nested arrays are completely loop-unrolled into a huge amount of instructions (feat. 153 MB binary)

Open
#143,673 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-LLVM C-optimization I-heavy
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#[unsafe(no_mangle)]
pub fn foo(f: fn([[[[i32; 16]; 16]; 16]; 16])) {
    let x = [[[[1i32; 16]; 16]; 16]; 16];
    f(x);
}

I expected this to compile into a loop of some kind. But when I compiled this with -Copt-level=3, I got a huuuuge completely-unrolled loop:

Godbolt link

Part of the generated assembly
.LCPI0_0:
        .long   1
        .long   1
        .long   1
        .long   1
foo:
        mov     r11, rsp
        sub     r11, 262144
.LBB0_1:
        sub     rsp, 4096
        mov     qword ptr [rsp], 0
        cmp     rsp, r11
        jne     .LBB0_1
        sub     rsp, 8
        mov     rax, rdi
        movaps  xmm0, xmmword ptr [rip + .LCPI0_0]
        movaps  xmmword ptr [rsp], xmm0
        movaps  xmmword ptr [rsp + 16], xmm0
        movaps  xmmword ptr [rsp + 32], xmm0
        movaps  xmmword ptr [rsp + 48], xmm0
        movaps  xmmword ptr [rsp + 64], xmm0
        movaps  xmmword ptr [rsp + 80], xmm0
        movaps  xmmword ptr [rsp + 96], xmm0
; A large amount of assembly code omitted for brevity
        movaps  xmmword ptr [rsp + 15936], xmm0
        movaps  xmmword ptr [rsp + 15952], xmm0
        movaps  xmmword ptr [rsp + 15984], xmm0
        movaps  xmmword ptr [rsp + 16000], xmm0
; A large amount of assembly code omitted for brevity
        movaps  xmmword ptr [rsp + 262048], xmm0
        movaps  xmmword ptr [rsp + 262064], xmm0
        movaps  xmmword ptr [rsp + 262080], xmm0
        movaps  xmmword ptr [rsp + 262096], xmm0
        movaps  xmmword ptr [rsp + 262112], xmm0
        movaps  xmmword ptr [rsp + 262128], xmm0
        movaps  xmmword ptr [rsp + 15968], xmm0
        mov     rdi, rsp
        call    rax
        add     rsp, 262152
        ret

I also tried compiling the following code locally with cargo build --release (since godbolt has a compilation time limit), and I got a 153 MB rlib.

#[unsafe(no_mangle)]
pub fn foo(f: fn([[[[[[[[[[i32; 16]; 16]; 16]; 16]; 16]; 16]; 16]; 16]; 16]; 16])) {
    let x = [[[[[[[[[[1i32; 16]; 16]; 16]; 16]; 16]; 16]; 16]; 16]; 16]; 16];
    f(x);
}
Meta

rustc version on godbolt:

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5
Internal compiler ID: r1880

rustc version locally:

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: aarch64-apple-darwin
release: 1.88.0
LLVM version: 20.1.5

@rustbot labels +I-heavy

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Compile the Rust reproducer with -Copt-level=3 and cargo build --release, then compare the generated assembly and rlib size with the expected loop-like behavior. Trace the compiler's handling of nested array initialization and calls, and confirm the fix avoids pathological unrolling without changing the function's behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.