Repository navigation
Miscompilation under target-cpu >= haswell #63791
Description
This example fails when compiled with target-cpu "haswell" or more recent:
[dependencies]
aes-soft = "=0.3.3"use aes_soft::{block_cipher_trait::BlockCipher, Aes128};
fn main() {
let plain = [127, 0, 0, 1, 174, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let key = [0; 16];
let encrypted = [222, 157, 168, 71, 195, 237, 77, 237, 182, 194, 17, 235, 182, 214, 204, 80];
let output = encrypt(plain, key);
assert_eq!(output, encrypted);
println!("success");
}
fn encrypt(input: [u8; 16], key: [u8; 16]) -> [u8; 16] {
let key = key.into();
let mut block = input.into();
let cipher = Aes128::new(&key);
cipher.encrypt_block(&mut block);
block.into()
}> RUSTFLAGS='-C target-cpu=ivybridge' cargo run --release
success
> RUSTFLAGS='-C target-cpu=haswell' cargo run --release
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `[103, 175, 2, 16, 66, 180, 192, 20, 55, 121, 111, 21, 82, 184, 106, 59]`,
right: `[222, 157, 168, 71, 195, 237, 77, 237, 182, 194, 17, 235, 182, 214, 204, 80]`', src/main.rs:9:5
> RUSTFLAGS='-C target-cpu=broadwell' cargo run --release
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `[103, 175, 2, 16, 66, 180, 192, 20, 55, 121, 111, 21, 82, 184, 106, 59]`,
right: `[222, 157, 168, 71, 195, 237, 77, 237, 182, 194, 17, 235, 182, 214, 204, 80]`', src/main.rs:9:5
> RUSTFLAGS='-C target-cpu=skylake' cargo run --release
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `[103, 175, 2, 16, 66, 180, 192, 20, 55, 121, 111, 21, 82, 184, 106, 59]`,
right: `[222, 157, 168, 71, 195, 237, 77, 237, 182, 194, 17, 235, 182, 214, 204, 80]`', src/main.rs:9:5
I bumped into this when compiling with target-cpu=native and assumed it was related to #54688, but after minimising the testcase I don't think it is. My next guess is an llvm bug but I thought I'd make an issue here in case anyone else bumps into it or wants to help investigate.
I've created a self-contained godbolt: https://rust.godbolt.org/z/ChdU6w
The two uses of unsafe I believe are sound. I think I've ruled out target-features being the cause, because this succeeds:
RUSTFLAGS='-C target-cpu=ivybridge -C target-feature=-slow-unaligned-mem-32,+avx2,+bmi,+bmi2,+ermsb,+fma,+invpcid,+lzcnt,+movbe,+false-deps-lzcnt-tzcnt' cargo run --release
Which should enable identical features to haswell given https://github.com/llvm-mirror/llvm/blob/release_90/lib/Target/X86/X86.td#L542-L568.
This occurs on nightly and stables back to 1.34.0, though 1.33.0 and prior seem to work correctly.
Tested on Broadwell (Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz) and Skylake (Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz).
Same issue filed against aes_soft crate: RustCrypto/block-ciphers#51
Activity
gnzlbg commentedon Aug 24, 2019
It would help tremendously if you could reduce that example further.
mati865 commentedon Aug 24, 2019
You can use C-Reduce to achieve that.
alecmocatta commentedon Aug 24, 2019
I'll look into using bugpoint and -opt-bisect-limit to narrow this down. Any other suggestions welcome.
@mati865 Thanks for the suggestion, I didn't know C-Reduce worked for Rust! My suspicion is in llvm's optimization passes, i.e. LLVM IR -> assembly, so I'll give llvm's bugpoint a go first.
bjorn3 commentedon Aug 24, 2019
It works for pretty much any language. It just does changes like removing a line or changing it a bit, so it often generates invalid C as well. Only a few passes assume C, but those will just not reduce the test case for Rust. See https://embed.cs.utah.edu/creduce/pldi12_talk.pdf
Aaron1011 commentedon Aug 24, 2019
Unforunately, C-Reduce proved to be too clever when I ran it: it replaced:
with
While this is technically a 'minimization', it's not a very useful one.
JakubValtar commentedon Aug 24, 2019
Not sure if it helps, but
git bisectrevealed df0466d to be the first bad commit.Aaron1011 commentedon Aug 24, 2019
THe problem appears to be related to the
deconstructfunction insideun_bit_slice_4x4_with_u16. For some reason, the high-order bit ina(computed aspb(x7, bit + 12, 31)) seems to be getting left off.The earliest point at which this occurs appears to be the call
un_bit_slice_4x4_with_u16(Bs8State(61439, 65535, 0, 4369, 273, 65535, 61439, 4369)). The correct result is(2868640763, 1667457891, 1667457891, 1667457891), but with-C target-cpu=haswell, it gets miscompiled somehow to return(721157115, 1667457891, 1667457891, 1667457891). Note that2868640763 = 721157115 | (1 << 31).Unfortunately, the problem disappeared when I made a standalone project with
un_bit_slice_4x4_with_u16andBs8State.JakubValtar commentedon Aug 25, 2019
I decided to give it a try and managed to simplify the code:
Assembly output from 1.33.0 and 1.34.0 is identical, except for this block of AVX instructions:
Looks like some of them got incorrectly optimized away. I'm not familiar with how LLVM vectorizes and optimizes things, so sadly this is as far as I can get on my own.
Edit: Just to confirm, running with
-opt-bisect-limitpoints toSLP Vectorizer on function (deconstruct).gnzlbg commentedon Aug 27, 2019
I managed to produce a smaller but slightly different reduction. I'm not sure if it also triggers the exact same issue reported here, but a similar asm diff is observed, see the diff in https://gcc.godbolt.org/z/KZVz_O:
nikomatsakis commentedon Aug 29, 2019
Check-in from the compiler triage meeting:
Summarizing, the previous comments, it seems we have minimized the example and we have bisected to a commit that upgrades LLVM.
This looks likely to be an LLVM bug. I'm going to cc @nikic and @nagisa in the hopes that one of them can help in pushing this to resolution.
I'm marking it as P-high.
nikic commentedon Sep 1, 2019
The IR for the functions is the same, so this is an issue in the backend, not the SLP vectorizer.
It looks like LLVM correctly determines that one of the vpsrld's is dead based on demanded elements and replaces the packus with a pshufb. A little later we see:
Which doesn't look right... pshuflw -28 is a no-op operation, but the previous pshufb is not. This combine is performed by matchUnaryPermuteShuffle(), but I haven't looked further than that yet.
nikic commentedon Sep 7, 2019
LLVM bug: https://bugs.llvm.org/show_bug.cgi?id=43230
nikic commentedon Sep 9, 2019
Fixed & backported to LLVM 9.
Rollup merge of rust-lang#64317 - nikic:bump-llvm-3, r=alexcrichton