rust-lang / rust-lang/rust

LLVM miscompile with -Copt-level=2 and -Ctarget-cpu=znver2

Open
#155,952 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-LLVM A-rustlantis C-bug I-miscompile O-x86_64 P-medium
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

#![allow(internal_features)]
#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;
#[custom_mir(dialect = "runtime", phase = "initial")]
fn fn2() -> u8 {
    mir!{
        let _1: (u16, u8, [u32; 3]);
        let _2: *mut [u32; 3];
        let _3: (u16, u8, [u32; 3]);
        let _4: u16;
        {
            _1.0 = 0;
            _1.1 = 0;
            _1.2 = [0, 0, 0];
            _2 = core::ptr::addr_of_mut!(_1.2);
            _3 = _1;
            _4 = _3.0;
            _1 = (0, 0, _3.2);
            _1 = _3;
            *_2 = _3.2;
            _1 = (0, _3.1, _3.2);
            Call(RET = dump_var(_4, Move(_3), Move(_1)), ReturnTo(bb52), UnwindUnreachable())
        }
        bb52 = {
            Return()
        }
    }
}
#[inline(never)]
fn dump_var(val0: u16, val1: (u16, u8, [u32; 3]), val2: (u16, u8, [u32; 3])) -> u8 {
    let _val0 = std::hint::black_box(val0);
    let _val1 = std::hint::black_box(val1);
    val2.1
}
fn main() {
    println!("{}", fn2());
}

Compile with

-Zmir-opt-level=0 -Copt-level=2 -Ctarget-cpu=znver2

And you'll get different output run-to-run. Miri reports no UB with Tree Borrows.

I'm sure this is incompletely reduced, but I am tired so I am filing this here for someone to reduce away the Rust layer around the bad LLVM IR pattern.

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.

Research direction

Start by compiling the supplied Rust reproducer with -Zmir-opt-level=0, -Copt-level=2, and -Ctarget-cpu=znver2, then compare its run-to-run output and Miri result. Reduce the custom MIR and Rust layer while preserving the nondeterministic behavior, and inspect the resulting LLVM IR for the miscompile pattern. Done means a smaller reproducer or a clearly isolated bad LLVM IR pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.