rust-lang / rust-lang/rust

Noop loop is only optimized away when the range is half-open

Open
#123,845 6 comments 5 reactions 1 assignee View on GitHub

@veera-sivarajan is already working on this.

Since Dec 2, 2024.

A-codegen A-LLVM C-bug C-optimization I-heavy I-slow S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

#[no_mangle]
pub fn f() {
    for _ in 0..=100 {
        ()
    }
}

#[no_mangle]
pub fn g() {
    for _ in 0..100 {
        ()
    }
}

with -O gives

f:
        xor     ecx, ecx
        mov     eax, 100
.LBB0_1:
        lea     edx, [rcx + 1]
        cmp     ecx, 100
        mov     ecx, edx
        cmovge  ecx, eax
        jge     .LBB0_3
        cmp     ecx, 101
        jl      .LBB0_1
.LBB0_3:
        ret

g:
        ret
Meta

Exists in both 1.77 and nightly.

Backtrace

<backtrace>

@rustbot label +A-codegen +A-LLVM +C-optimization +I-slow +S-has-mcve +I-heavy +T-compiler -needs-triage

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.