|
| 1 | +// Copyright Kani Contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | +//! Test for projection mismatch fix with array-based SIMD types. |
| 4 | +//! This addresses the issue described in https://github.com/model-checking/kani/issues/2264 |
| 5 | +//! where array-based SIMD types would cause projection mismatches during type conversions. |
| 6 | +
|
| 7 | +#![feature(repr_simd)] |
| 8 | + |
| 9 | +#[derive(Copy)] |
| 10 | +#[repr(simd)] |
| 11 | +struct V<T>([T; 2]); |
| 12 | + |
| 13 | +impl<T: Copy> Clone for V<T> { |
| 14 | + fn clone(&self) -> Self { |
| 15 | + *self |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Copy)] |
| 20 | +#[repr(simd)] |
| 21 | +struct VF32([f32; 2]); |
| 22 | + |
| 23 | +impl Clone for VF32 { |
| 24 | + fn clone(&self) -> Self { |
| 25 | + *self |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Copy)] |
| 30 | +#[repr(simd)] |
| 31 | +struct VU32([u32; 2]); |
| 32 | + |
| 33 | +impl Clone for VU32 { |
| 34 | + fn clone(&self) -> Self { |
| 35 | + *self |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Test transmute between SIMD types with same representation size |
| 40 | +// This should work with the projection fix for array-based SIMD types |
| 41 | +fn test_simd_transmute_same_size() { |
| 42 | + let v_f32 = VF32([1.0f32, 2.0f32]); |
| 43 | + let v_u32: VU32 = unsafe { std::mem::transmute(v_f32) }; |
| 44 | + |
| 45 | + // Verify the transmute worked by checking bit patterns |
| 46 | + let f32_bits = 1.0f32.to_bits(); |
| 47 | + let u32_val = unsafe { std::mem::transmute::<VU32, [u32; 2]>(v_u32) }; |
| 48 | + assert_eq!(u32_val[0], f32_bits); |
| 49 | +} |
| 50 | + |
| 51 | +// Test field access on array-based SIMD (this was part of the original issue) |
| 52 | +fn test_simd_field_access() { |
| 53 | + let v = V::<u32>([u32::MIN, u32::MAX]); |
| 54 | + |
| 55 | + // This should work without projection mismatch errors |
| 56 | + unsafe { |
| 57 | + let arr: [u32; 2] = std::mem::transmute(v); |
| 58 | + assert_eq!(arr[0], u32::MIN); |
| 59 | + assert_eq!(arr[1], u32::MAX); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[kani::proof] |
| 64 | +fn verify_simd_transmute_same_size() { |
| 65 | + test_simd_transmute_same_size(); |
| 66 | +} |
| 67 | + |
| 68 | +#[kani::proof] |
| 69 | +fn verify_simd_field_access() { |
| 70 | + test_simd_field_access(); |
| 71 | +} |
| 72 | + |
| 73 | +#[kani::proof] |
| 74 | +fn verify_simd_clone() { |
| 75 | + let v = V::<i32>([42, -42]); |
| 76 | + let v2 = v.clone(); |
| 77 | + |
| 78 | + unsafe { |
| 79 | + let arr1: [i32; 2] = std::mem::transmute(v); |
| 80 | + let arr2: [i32; 2] = std::mem::transmute(v2); |
| 81 | + assert_eq!(arr1[0], arr2[0]); |
| 82 | + assert_eq!(arr1[1], arr2[1]); |
| 83 | + } |
| 84 | +} |
0 commit comments