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

Activity

changed the title [-]Regression: Inlined loops not fully evaluating `const`-eable dependencies[/-] [+]Regression: Inlined loops not fully evaluating with `const`-eable dependencies[/+] on Aug 27, 2022
Rageking8

Rageking8 commented on Aug 27, 2022

@Rageking8
Contributor

@rustbot label regression-from-stable-to-nightly

added
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Aug 27, 2022
steffahn

steffahn commented on Aug 27, 2022

@steffahn
Member

@rustbot label I-slow

added
I-slowIssue: Problems and improvements with respect to performance of generated code.
on Aug 27, 2022
apiraino

apiraino commented on Aug 31, 2022

@apiraino
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-high t-compiler

added
P-highHigh priority
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
and removed
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Aug 31, 2022
added
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
on Aug 31, 2022
self-assigned this
on Sep 1, 2022
nikic

nikic commented on Sep 1, 2022

@nikic
Contributor

Partial fix: llvm/llvm-project@43e7d9a This at least allows the loop to be unrolled, though it only gets fully constant folded in the backend.

16 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @pnkfelix@nikic@steffahn@apiraino@sno2

    Issue actions

      Regression: Inlined loops not fully evaluating with `const`-eable dependencies · Issue #101082 · rust-lang/rust