Skip to content

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

Closed
@maghoff

Description

@maghoff

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-SIMDArea: SIMD (Single Instruction Multiple Data)C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessPG-portable-simdProject group: Portable SIMD (https://github.com/rust-lang/project-portable-simd)T-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