Skip to content

portable-simd's swizzle_dyn miscompiles on x86-64 #119904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maghoff opened this issue Jan 12, 2024 · 4 comments · Fixed by #121269
Closed

portable-simd's swizzle_dyn miscompiles on x86-64 #119904

maghoff opened this issue Jan 12, 2024 · 4 comments · Fixed by #121269
Labels
A-SIMD Area: SIMD (Single Instruction Multiple Data) C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness PG-portable-simd Project group: Portable SIMD (https://github.com/rust-lang/project-portable-simd) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@maghoff
Copy link

maghoff commented Jan 12, 2024

I tried this code:

#![feature(portable_simd)]

use std::simd::prelude::*;

static TABLE: [u8; 32] = *b"abcdefghijklmnopqrstuvwxyz012345";

pub fn x(input_indices: [u8; 4]) -> [u8; 4] {
    let mut indices = [0u8; 32];
    indices[0..4].copy_from_slice(&input_indices);
    let indices = u8x32::from_array(indices);
    let table = u8x32::from_array(TABLE);

    let buf = table.swizzle_dyn(indices);

    buf.to_array()[0..4].try_into().unwrap()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(&x([2, 14, 14, 11]), b"cool");
    }
}

I expected to see this happen: The test should pass.

Instead, this happened: The test fails when run with RUSTFLAGS="-C target-cpu=icelake-client" cargo +nightly test -Zbuild-std --target x86_64-unknown-linux-gnu. But it passes with, say, target-cpu=x86-64, which points to this being a problem with the code generated for this specific cpu.

In my analysis, the failure happens because swizzle_dyn ends up calling the _mm256_permutexvar_epi8 intrinsic with the arguments in reverse order. According to the docs, the first parameter is the index vector, but in swizzle_dyn (via transize), the index vector is passed as the second argument. Oops!

This analysis is corroborated by the fact that I can make it work by calling said intrinsic directly :)

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (3cdd004e5 2023-12-29)
binary: rustc
commit-hash: 3cdd004e55c869faa2b7b25efd3becf50346e7d6
commit-date: 2023-12-29
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

@maghoff maghoff added the C-bug Category: This is a bug. label Jan 12, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 12, 2024
@Noratrieb
Copy link
Member

cc @workingjubilee @calebzulawski

@Noratrieb Noratrieb added I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness A-SIMD Area: SIMD (Single Instruction Multiple Data) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. PG-portable-simd Project group: Portable SIMD (https://github.com/rust-lang/project-portable-simd) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 12, 2024
@calebzulawski
Copy link
Member

@workingjubilee looks like an easy enough fix but I'm curious why our tests didn't catch it

@maghoff
Copy link
Author

maghoff commented Feb 14, 2024

I'm sorry to nag, but it's now been one month since this was deemed "an easy enough fix" 😄 It'd be helpful for me if this were fixed. I deeply appreciate the value of knowing your tests are solid, but perhaps the test aspect could be looked into even after patching the bug?

I can see several ways of fixing this, but I feel like the choice of how to fix this is a matter of taste in the specific code base, so I hesitate to submit a PR, and I don't think that'd save anybody much work anyway.

Thanks for all your hard work! 💐

@Noratrieb
Copy link
Member

Do send in a PR if you have a fix, even if you're not sure whether it's the best one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-SIMD Area: SIMD (Single Instruction Multiple Data) C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness PG-portable-simd Project group: Portable SIMD (https://github.com/rust-lang/project-portable-simd) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants