Skip to content

ICE due to generating LLVM bitcast vec -> array #105439

New issue

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

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

Already on GitHub? # to your account

Closed
JakobDegen opened this issue Dec 8, 2022 · 1 comment · Fixed by #105583
Closed

ICE due to generating LLVM bitcast vec -> array #105439

JakobDegen opened this issue Dec 8, 2022 · 1 comment · Fixed by #105583
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-SIMD Area: SIMD (Single Instruction Multiple Data) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ PG-portable-simd Project group: Portable SIMD (https://github.com/rust-lang/project-portable-simd) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JakobDegen
Copy link
Contributor

Found here: #105344 (comment)

Code:

#![feature(portable_simd)]
fn main() {
    use core::simd::Simd;
    let a = Simd::from_array([0, 4, 1, 5]);
    let b = Simd::from_array([2, 6, 3, 7]);
    let (x, y) = a.deinterleave(b);
    assert_eq!(x.to_array(), [0, 1, 2, 3]);
    assert_eq!(y.to_array(), [4, 5, 6, 7]);
}

Invocation:

$ rustc +nightly --version
rustc 1.67.0-nightly (e1d819583 2022-12-05)
binary: rustc
commit-hash: e1d819583f0bf13b016b119c1c2c43e6d3979450
commit-date: 2022-12-05
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Compiler returned: 0
$ rustc +nightly --edition 2021 -C opt-level=3 -Zmir-opt-level=3 -C no-prepopulate-passes

Causes an LLVM ICE:

Invalid bitcast
  %27 = bitcast <4 x i32> %22 to [4 x i32], !dbg !268
Invalid bitcast
  %31 = bitcast <4 x i32> %26 to [4 x i32], !dbg !280
in function _ZN7example4main17h82e1f57aed922fc8E
LLVM ERROR: Broken function found, compilation aborted!
Compiler returned: 101

This reproduces easily on Godbolt.

The invalid bitcast is introduced by codgen for the MIR statement _1 = (_25.0: [i32; 4]); on line 151.

Local _25 has type std::simd::Simd<i32, 4>. I believe the bitcast is introduced here, but I don't think this path is even intended to handle Simd types. I'm not exactly sure what the bug here is either, ie I have no idea how this was supposed to be codegened.

@rustbot label +A-codegen +A-llvm +I-ICE +T-compiler

@rustbot rustbot added A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 8, 2022
@Nugine
Copy link
Contributor

Nugine commented Dec 8, 2022

Reproduction

#![feature(portable_simd)]

extern crate core;

use core::simd::*;

pub fn check(a: i32x4) -> bool {
    (a + a).to_array() == [0, 0, 0, 0]
}

--edition 2021 -C opt-level=3 -C no-prepopulate-passes
#![feature(repr_simd)]
#![feature(platform_intrinsics)]

extern crate core;

#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
#[repr(simd)]
pub struct i32x4([i32; 4]);

extern "platform-intrinsic" {
    pub(crate) fn simd_add<T>(x: T, y: T) -> T;
}

#[inline(always)]
fn to_array(x: i32x4) -> [i32; 4] {
    x.0
}

pub fn check(a: i32x4) -> bool {
    let b: i32x4 = unsafe { simd_add(a, a) };
    to_array(b) == [0; 4]
}

@workingjubilee workingjubilee added A-SIMD Area: SIMD (Single Instruction Multiple Data) PG-portable-simd Project group: Portable SIMD (https://github.com/rust-lang/project-portable-simd) labels Mar 11, 2023
@bors bors closed this as completed in 5859771 May 7, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-SIMD Area: SIMD (Single Instruction Multiple Data) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ PG-portable-simd Project group: Portable SIMD (https://github.com/rust-lang/project-portable-simd) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants