1 use gccjit::{RValue, Type};
2 use rustc_codegen_ssa::base::compare_simd_types;
3 use rustc_codegen_ssa::common::{TypeKind, span_invalid_monomorphization_error};
4 use rustc_codegen_ssa::mir::operand::OperandRef;
5 use rustc_codegen_ssa::traits::{BaseTypeMethods, BuilderMethods};
7 use rustc_middle::span_bug;
8 use rustc_middle::ty::layout::HasTyCtxt;
9 use rustc_middle::ty::{self, Ty};
10 use rustc_span::{Span, Symbol, sym};
12 use crate::builder::Builder;
14 pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, name: Symbol, callee_ty: Ty<'tcx>, args: &[OperandRef<'tcx, RValue<'gcc>>], ret_ty: Ty<'tcx>, llret_ty: Type<'gcc>, span: Span) -> Result<RValue<'gcc>, ()> {
15 // macros for error handling:
16 macro_rules! emit_error {
20 ($msg: tt, $($fmt: tt)*) => {
21 span_invalid_monomorphization_error(
23 &format!(concat!("invalid monomorphization of `{}` intrinsic: ", $msg),
28 macro_rules! return_error {
31 emit_error!($($fmt)*);
37 macro_rules! require {
38 ($cond: expr, $($fmt: tt)*) => {
40 return_error!($($fmt)*);
45 macro_rules! require_simd {
46 ($ty: expr, $position: expr) => {
47 require!($ty.is_simd(), "expected SIMD {} type, found non-SIMD `{}`", $position, $ty)
53 tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), callee_ty.fn_sig(tcx));
54 let arg_tys = sig.inputs();
55 let name_str = name.as_str();
57 // every intrinsic below takes a SIMD vector as its first argument
58 require_simd!(arg_tys[0], "input");
59 let in_ty = arg_tys[0];
61 let comparison = match name {
62 sym::simd_eq => Some(hir::BinOpKind::Eq),
63 sym::simd_ne => Some(hir::BinOpKind::Ne),
64 sym::simd_lt => Some(hir::BinOpKind::Lt),
65 sym::simd_le => Some(hir::BinOpKind::Le),
66 sym::simd_gt => Some(hir::BinOpKind::Gt),
67 sym::simd_ge => Some(hir::BinOpKind::Ge),
71 let (in_len, in_elem) = arg_tys[0].simd_size_and_type(bx.tcx());
72 if let Some(cmp_op) = comparison {
73 require_simd!(ret_ty, "return");
75 let (out_len, out_ty) = ret_ty.simd_size_and_type(bx.tcx());
78 "expected return type with length {} (same as input type `{}`), \
79 found `{}` with length {}",
86 bx.type_kind(bx.element_type(llret_ty)) == TypeKind::Integer,
87 "expected return type with integer elements, found `{}` with non-integer `{}`",
92 return Ok(compare_simd_types(
102 if let Some(stripped) = name_str.strip_prefix("simd_shuffle") {
103 let n: u64 = stripped.parse().unwrap_or_else(|_| {
104 span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?")
107 require_simd!(ret_ty, "return");
109 let (out_len, out_ty) = ret_ty.simd_size_and_type(bx.tcx());
112 "expected return type of length {}, found `{}` with length {}",
119 "expected return element type `{}` (element of input `{}`), \
120 found `{}` with element type `{}`",
127 let vector = args[2].immediate();
129 return Ok(bx.shuffle_vector(
136 macro_rules! arith_binary {
137 ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
138 $(if name == sym::$name {
139 match in_elem.kind() {
140 $($(ty::$p(_))|* => {
141 return Ok(bx.$call(args[0].immediate(), args[1].immediate()))
146 "unsupported operation on `{}` with element `{}`",
154 simd_add: Uint, Int => add, Float => fadd;
155 simd_sub: Uint, Int => sub, Float => fsub;
156 simd_mul: Uint, Int => mul, Float => fmul;
157 simd_div: Uint => udiv, Int => sdiv, Float => fdiv;
158 simd_rem: Uint => urem, Int => srem, Float => frem;
159 simd_shl: Uint, Int => shl;
160 simd_shr: Uint => lshr, Int => ashr;
161 simd_and: Uint, Int => and;
162 simd_or: Uint, Int => or; // FIXME(antoyo): calling `or` might not work on vectors.
163 simd_xor: Uint, Int => xor;
166 unimplemented!("simd {}", name);