Skip to content

Regression: Inlined loops not fully evaluating with const-eable dependencies #101082

Closed
@sno2

Description

@sno2

I tried this code:

pub fn arr() -> usize {
  let values = [23, 16, 54, 3, 60, 9];
  let mut acc = 0;
  for item in values { 
    acc += item;
  }
  acc
}

I expected to see this happen: The loop should be inlined because values is const-known. For example, here is the generated assembly on Rust 1.63.0 (Godbolt) and Rust Beta (Godbolt):

example::arr:
        mov     eax, 165
        ret

Instead, this happened: On nightly, the loop is inlined. However, the entire function is not computed despite the stable and beta Rust inlining it. Here is the generated assembly on Nightly (Godbolt):

.LCPI0_0:
        .quad   23
        .quad   16
.LCPI0_1:
        .quad   54
        .quad   3
.LCPI0_2:
        .quad   60
        .quad   9
example::arr:
        sub     rsp, 72
        movaps  xmm0, xmmword ptr [rip + .LCPI0_0]
        movaps  xmmword ptr [rsp], xmm0
        movaps  xmm0, xmmword ptr [rip + .LCPI0_1]
        movaps  xmmword ptr [rsp + 16], xmm0
        movaps  xmm0, xmmword ptr [rip + .LCPI0_2]
        movaps  xmmword ptr [rsp + 32], xmm0
        mov     qword ptr [rsp + 56], 6
        xor     ecx, ecx
        xor     edx, edx
        jmp     .LBB0_1
.LBB0_3:
        lea     rdi, [rcx + 1]
        mov     qword ptr [rsp + 48], rdi
        mov     rdx, qword ptr [rsp + 8*rcx]
        mov     esi, 1
        mov     rcx, rdi
        add     rdx, rax
        test    rsi, rsi
        je      .LBB0_5
.LBB0_1:
        mov     rax, rdx
        cmp     rcx, 5
        jbe     .LBB0_3
        xor     esi, esi
        add     rdx, rax
        test    rsi, rsi
        jne     .LBB0_1
.LBB0_5:
        add     rsp, 72
        ret

Meta

n/a - disclosed at usages

Keywords: const inline loop, const loop, const loop not evaluated

Relevant discord thread: https://discord.com/channels/273534239310479360/592856094527848449/1013050670942793808

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions