Skip to main content

miri/shims/
aarch64.rs

1use rustc_abi::CanonAbi;
2use rustc_middle::mir::BinOp;
3use rustc_middle::ty::Ty;
4use rustc_span::Symbol;
5use rustc_target::callconv::FnAbi;
6
7use crate::*;
8
9impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
10pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11    fn emulate_aarch64_intrinsic(
12        &mut self,
13        link_name: Symbol,
14        abi: &FnAbi<'tcx, Ty<'tcx>>,
15        args: &[OpTy<'tcx>],
16        dest: &MPlaceTy<'tcx>,
17    ) -> InterpResult<'tcx, EmulateItemResult> {
18        let this = self.eval_context_mut();
19        // Prefix should have already been checked.
20        let unprefixed_name = link_name.as_str().strip_prefix("llvm.aarch64.").unwrap();
21        match unprefixed_name {
22            // Used to implement the vpmaxq_u8 function.
23            // Computes the maximum of adjacent pairs; the first half of the output is produced from the
24            // `left` input, the second half of the output from the `right` input.
25            // https://developer.arm.com/architectures/instruction-sets/intrinsics/vpmaxq_u8
26            "neon.umaxp.v16i8" => {
27                let [left, right] =
28                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
29
30                let (left, left_len) = this.project_to_simd(left)?;
31                let (right, right_len) = this.project_to_simd(right)?;
32                let (dest, lane_count) = this.project_to_simd(dest)?;
33                assert_eq!(left_len, right_len);
34                assert_eq!(lane_count, left_len);
35
36                for lane_idx in 0..lane_count {
37                    let src = if lane_idx < (lane_count / 2) { &left } else { &right };
38                    let src_idx = lane_idx.strict_rem(lane_count / 2);
39
40                    let lhs_lane =
41                        this.read_immediate(&this.project_index(src, src_idx.strict_mul(2))?)?;
42                    let rhs_lane = this.read_immediate(
43                        &this.project_index(src, src_idx.strict_mul(2).strict_add(1))?,
44                    )?;
45
46                    // Compute `if lhs > rhs { lhs } else { rhs }`, i.e., `max`.
47                    let res_lane = if this
48                        .binary_op(BinOp::Gt, &lhs_lane, &rhs_lane)?
49                        .to_scalar()
50                        .to_bool()?
51                    {
52                        lhs_lane
53                    } else {
54                        rhs_lane
55                    };
56
57                    let dest = this.project_index(&dest, lane_idx)?;
58                    this.write_immediate(*res_lane, &dest)?;
59                }
60            }
61            // Vector table lookup: each index selects a byte from the 16-byte table, out-of-range -> 0.
62            // Used to implement vtbl1_u8 function.
63            // LLVM does not have a portable shuffle that takes non-const indices
64            // so we need to implement this ourselves.
65            // https://developer.arm.com/architectures/instruction-sets/intrinsics/vtbl1_u8
66            "neon.tbl1.v16i8" => {
67                let [table, indices] =
68                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
69
70                let (table, table_len) = this.project_to_simd(table)?;
71                let (indices, idx_len) = this.project_to_simd(indices)?;
72                let (dest, dest_len) = this.project_to_simd(dest)?;
73                assert_eq!(table_len, 16);
74                assert_eq!(idx_len, dest_len);
75
76                for i in 0..dest_len {
77                    let idx = this.read_immediate(&this.project_index(&indices, i)?)?;
78                    let idx_u = idx.to_scalar().to_u8()?;
79                    let val = if u64::from(idx_u) < table_len {
80                        let t = this.read_immediate(&this.project_index(&table, idx_u.into())?)?;
81                        t.to_scalar()
82                    } else {
83                        Scalar::from_u8(0)
84                    };
85                    this.write_scalar(val, &this.project_index(&dest, i)?)?;
86                }
87            }
88            _ => return interp_ok(EmulateItemResult::NotSupported),
89        }
90        interp_ok(EmulateItemResult::NeedsReturn)
91    }
92}