Skip to content

Miscompilation: loop counter off by one after a vectorized loop at -O #164295

Open

Description

@zxt5

Compiler Explorer: https://godbolt.org/z/v6aeMarE7

small.rs

#[inline(never)]
fn f(buf: &[u8], x: u64) -> (u64, usize) {
    let mut at = 1;
    let mut acc = 0u64;
    loop {
        acc ^= x;
        let more = at < buf.len();
        at += more as usize;
        if !more {
            break;
        }
    }
    (acc, buf[at..].len())
}

fn main() {
    let buf = vec![0u8; std::hint::black_box(4)];
    println!("{:?}", f(&buf, 1));
}

Reproduce:

Correct:

$ rustc small.rs && ./small

(0, 0)

Miscompile:

$ rustc -O small.rs && ./small

thread 'main' (4184111) panicked at small.rs:13:14:
range start index 5 out of range for slice of length 4
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Meta

rustc --version --verbose:

rustc 1.101.0-nightly (91e426a05 2026-10-13)
binary: rustc
commit-hash: 91e426a0518d5bb3672330c5238ccda6fd26fb54
commit-date: 2026-10-13
host: x86_64-unknown-linux-gnu
release: 1.101.0-nightly
LLVM version: 23.1.3

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 14, 2026
  2. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    I-miscompileIssue: Correct Rust code lowers to incorrect machine code
    on Oct 14, 2026
  3. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 14, 2026
  4. added
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Oct 14, 2026
  5. saethlin commented on Oct 18, 2026

    @saethlin
    Member

    Bisects to #158734 (LLVM 23).

  6. removed
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Oct 18, 2026
  7. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 18, 2026
  8. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 27, 2026
  9. rustbot commented on Oct 27, 2026

    @rustbot
    Collaborator

    Assigning P-high (discussion on Zulip)

    Should it be (or was it already?) reported upstream?

    cc @rust-lang/wg-llvm (in case you were not notified by the A-LLVM labelling)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions