Skip to content

Miscompilation with mir-opt-level=2 #77002

Closed
@leonardo-m

Description

@leonardo-m

This little matrix processing code shows miscompilation if compiled with:

rustc -Z mir-opt-level=2 test.rs

But it compiles correctly with:

rustc -Z mir-opt-level=1 test.rs

With:

rustc 1.48.0-nightly 1fd5b9d516c035a898dcb437b2f982bea5d4bc88 2020-09-20 x86_64-pc-windows-gnu
fn main() {
    const MOD: i64 = 100_000_000;
    const N: u64 = 1_000_000_000_000;

    const SIZE: usize = 4;
    const SIZE64: u64 = SIZE as _;
    type Mat = [[i64; SIZE]; SIZE];

    fn matrix_prod(a: &Mat, b: &Mat, n: usize, m: usize, p: usize) -> Mat {
        let mut res: Mat = Mat::default();
        for i in 0 .. n {
            for j in 0 .. p {
                for k in 0 .. m {
                    res[i][j] = (res[i][j] + a[i][k] * b[k][j] % MOD) % MOD;
                }
            }
        }
        res
    }

    fn matrix_pow(mut a: Mat, s: usize, mut n: u64) -> Mat {
        let mut res: Mat = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];

        while n != 0 {
            if (n & 1) != 0 {
                res = matrix_prod(&a, &res, s, s, s);
            }
            n >>= 1;
            a = matrix_prod(&a, &a, s, s, s);
        }
        res
    }

    const G1: Mat = [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, -2, 2, 2]];
    let g2 = matrix_pow(G1, SIZE, N - SIZE64);
    println!("{:?}", g2);
}

Metadata

Metadata

Labels

A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions