Skip to content

Miscompilation in simple rust program, since rust 1.99.0(nightly-2026-08-06), difference between none and opt-level=1 optimization #164667

Open

Description

@qarmin

I tried this code:

use std::hint::black_box as a;
fn main() {
    let b: i8 = a(0);
    let a = a(1);
    let a = (-2i128).rotate_right(1).checked_mul(a).unwrap_or(0);
    print!("{}", a);
    let a = (b as u128).abs_diff(b as u16 as u128);
    print!("{}", a)
}

Compiled with

rustc reduced.rs

it prints

1701411834604692317316873037158841057270

but compiled with

rustc reduced.rs -C opt-level=1

it prints

1701411834604692317316873037158841057271

Meta

rustc --version --verbose:

rustc 1.99.1 (58cf8f9c3 2026-10-13)
binary: rustc
commit-hash: 58cf8f9c35b355199727627d07ae34a4e2846d67
commit-date: 2026-10-13
host: x86_64-unknown-linux-gnu
release: 1.99.1
LLVM version: 23.1.1

Problem was found by a rust fuzzer (created by an LLM) - https://github.com/qarmin/rs-compiler-fuzzer

channel reproducible
stable (rustc 1.99.1 (58cf8f9 2026-10-13)) yes
beta (rustc 1.100.0-beta.7 (fd8e211 2026-10-17)) yes
nightly (rustc 1.101.0-nightly (07e526d 2026-10-19)) yes
stable 1.98.0 (rustc 1.98.0 (88d9e12 2026-08-18)) no
stable 1.99.0 (rustc 1.99.0 (b940084 2026-09-28)) yes
nightly-2026-08-05 (rustc 1.99.0-nightly (1ed2df6 2026-08-04)) no
nightly-2026-08-06 (rustc 1.99.0-nightly (7608eb7 2026-08-05)) yes

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 21, 2026
  2. changed the title [-]Miscompilation in simple rust program, since rust 1.99.0(nightly-2026-08-06), with and without `opt-level=1` optimization[/-] [+]Miscompilation in simple rust program, since rust 1.99.0(nightly-2026-08-06), difference between none and `opt-level=1` optimization[/+] on Oct 21, 2026
  3. added
    T-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.
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
    I-miscompileIssue: Correct Rust code lowers to incorrect machine code
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 21, 2026
  4. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 21, 2026
  5. saethlin commented on Oct 21, 2026

    @saethlin
    Member

    Checking a smattering of stable/beta/nightly versions is not informative, I know our default issue template asks you to check stable/beta/nightly but if you are capable of doing so (you're running a fuzzer, so clearly you are capable) bisecting is more useful. In this case, bisection points to #158734, the LLVM 23 upgrade.

    In addition, please check perhaps as part of your differential testing, if a miscompile is caused by LLVM or by a MIR optimization (this is often as easy a checking if the bug reproduces with -Zmir-opt-level=0).

  6. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    and removed
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Oct 21, 2026
  7. D3athSkulll commented on Oct 21, 2026

    @D3athSkulll

    Hey @qarmin @saethlin , I am interested in taking this issue. Have investigated it further from my end. On reproducing the miscompilation for -C opt-level=1 . The LLVM pass number 1036 is creating issue specifically stack-coloring pass. On generating the machine instructions I see a incorrect stack slot reuse which involves value with address being passed through a black box.
    I would prefer trying lifetime analysis here and bug persists with -Zmir-opt-level=0, which argues against MIR optimization as the cause.

  8. apiraino commented on Oct 21, 2026

    @apiraino
    Contributor

    if the issue lies in the LLVM upgrade, as the bisection seems to demonstrate, wouldn't be more helpful to file an issue upstream? I know that we try to stay close to LLVM.

    (what is a "LLVM pass number 1036"?)

  9. D3athSkulll commented on Oct 21, 2026

    @D3athSkulll

    LLVM pass number 1036 is the position of the faulty pass in the LLVM verbose execution log, in the reproducer which is triggering our miscompilation. The pass name is “Merge disjoint stack slots (stack-coloring)” which analyses stack slot lifetimes and merges the slots whose live ranges do not overlap to allow memory reuse.

  10. saethlin commented on Oct 21, 2026

    @saethlin
    Member

    You are having this conversation on the wrong issue tracker, with the wrong people, and in any case I would advise against a first-time contributor trying to fix a miscompile unless you have previous experience working on compiler optimizations.

  11. maxdexh commented on Oct 21, 2026

    @maxdexh
    Member

    You are quite welcome to file an issue on the LLVM side though ^^

  12. D3athSkulll commented on Oct 22, 2026

    @D3athSkulll

    12. You are quite welcome to file an issue on the LLVM side though ^^

    Hey @qarmin did you file an issue on LLVM side in the repository? Or would you have me file one as I do have some info to file and try decoding it further

  13. theemathas commented on Oct 22, 2026

    @theemathas
    Contributor

    @D3athSkulll I believe they didn't.

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 codeI-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueS-waiting-on-LLVMStatus: the compiler-dragon is eepy, can someone get it some tea?T-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