Skip to content

Commit

Permalink
Fix SIMD field access (#2660)
Browse files Browse the repository at this point in the history
Change #2633 was incomplete and it doesn't work in the context of generic functions. This PR fixes that.
  • Loading branch information
celinval authored Aug 4, 2023
1 parent 973ce3e commit 0d0b234
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ impl<'tcx> GotocCtx<'tcx> {
field: FieldIdx,
field_ty: Ty<'tcx>,
) -> Expr {
let field_ty = self.monomorphize(field_ty);
if matches!(field_ty.kind(), ty::Array { .. }) {
// Array based
assert_eq!(field.index(), 0);
Expand Down
50 changes: 50 additions & 0 deletions tests/kani/SIMD/generic_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Ensure we can use a generic function to access field of `repr_simd`.
#![feature(portable_simd, repr_simd)]

use std::simd::SimdElement;

mod array_based {
use super::*;

#[repr(simd)]
struct CustomSimd<T: SimdElement, const LANES: usize>([T; LANES]);

fn check_fields<T: SimdElement + PartialEq, const LANES: usize>(
simd: CustomSimd<T, LANES>,
expected: [T; LANES],
) {
assert_eq!(simd.0, expected);
}

#[kani::proof]
fn check_field_access() {
let data: [u8; 16] = kani::any();
let vec = CustomSimd(data.clone());
check_fields(vec, data);
}
}

mod fields_based {
use super::*;

#[repr(simd)]
struct CustomSimd<T: SimdElement>(T, T);

fn check_fields<T: SimdElement + PartialEq, const LANES: usize>(
simd: CustomSimd<T>,
expected: [T; LANES],
) {
assert_eq!(simd.0, expected[0]);
assert_eq!(simd.1, expected[1])
}

#[kani::proof]
fn check_field_access() {
let data: [u8; 16] = kani::any();
let vec = CustomSimd(data[0], data[1]);
check_fields(vec, data);
}
}

0 comments on commit 0d0b234

Please sign in to comment.