rust-lang / rust-lang/rust

Wrong codegen for simd_select_bitmask on big-endian targets

Open
#127,205 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-LLVM A-SIMD C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Given the testcase

#![feature(repr_simd, intrinsics)]

extern "rust-intrinsic" {
    fn simd_bitmask<T, U>(v: T) -> U;
    fn simd_select_bitmask<T, U>(m: T, a: U, b: U) -> U;
}

#[repr(simd, packed)]
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct i32x10([i32; 10]);
impl i32x10 {
    fn splat(x: i32) -> Self {
        Self([x; 10])
    }
}

pub fn main() {
    // Non-power-of-2 multi-byte mask.
    unsafe {
        let mask = i32x10([-1, -1, 0, -1, 0, 0, -1, 0, -1, 0]);
        let mask_bytes =
            if cfg!(target_endian = "little") { [0b01001011, 0b01] } else { [0b11, 0b01001010] };

        let selected2 = simd_select_bitmask::<[u8; 2], _>(
            mask_bytes,
            i32x10::splat(-1), // yes
            i32x10::splat(0),  // no
        );

        assert_eq!(selected2, mask);
    }
}

on a big-endian target, this passes with optimizations but fails without. LLVM optimizations correctly implement the semantics of the IR this generates (so the resulting program is trivial), but without optimizations all the IR reaches the backend and somewhere in the LLVM machine backend, things go wrong.

See here for some analysis.

Cc @uweigand @nikic

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 reproducing the inline Rust testcase on a big-endian target with and without optimizations, then read the analysis linked from PR #126171. Trace the generated IR into the LLVM machine backend and identify why unoptimized code selects the wrong lanes; done means the testcase passes in both modes.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.