Skip to content

Commit 0d0b234

Browse files
authored
Fix SIMD field access (rust-lang#2660)
Change rust-lang#2633 was incomplete and it doesn't work in the context of generic functions. This PR fixes that.
1 parent 973ce3e commit 0d0b234

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl<'tcx> GotocCtx<'tcx> {
340340
field: FieldIdx,
341341
field_ty: Ty<'tcx>,
342342
) -> Expr {
343+
let field_ty = self.monomorphize(field_ty);
343344
if matches!(field_ty.kind(), ty::Array { .. }) {
344345
// Array based
345346
assert_eq!(field.index(), 0);

tests/kani/SIMD/generic_access.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! Ensure we can use a generic function to access field of `repr_simd`.
5+
#![feature(portable_simd, repr_simd)]
6+
7+
use std::simd::SimdElement;
8+
9+
mod array_based {
10+
use super::*;
11+
12+
#[repr(simd)]
13+
struct CustomSimd<T: SimdElement, const LANES: usize>([T; LANES]);
14+
15+
fn check_fields<T: SimdElement + PartialEq, const LANES: usize>(
16+
simd: CustomSimd<T, LANES>,
17+
expected: [T; LANES],
18+
) {
19+
assert_eq!(simd.0, expected);
20+
}
21+
22+
#[kani::proof]
23+
fn check_field_access() {
24+
let data: [u8; 16] = kani::any();
25+
let vec = CustomSimd(data.clone());
26+
check_fields(vec, data);
27+
}
28+
}
29+
30+
mod fields_based {
31+
use super::*;
32+
33+
#[repr(simd)]
34+
struct CustomSimd<T: SimdElement>(T, T);
35+
36+
fn check_fields<T: SimdElement + PartialEq, const LANES: usize>(
37+
simd: CustomSimd<T>,
38+
expected: [T; LANES],
39+
) {
40+
assert_eq!(simd.0, expected[0]);
41+
assert_eq!(simd.1, expected[1])
42+
}
43+
44+
#[kani::proof]
45+
fn check_field_access() {
46+
let data: [u8; 16] = kani::any();
47+
let vec = CustomSimd(data[0], data[1]);
48+
check_fields(vec, data);
49+
}
50+
}

0 commit comments

Comments
 (0)