rustc_codegen_llvm/
intrinsic.rs

1use std::assert_matches::assert_matches;
2use std::cmp::Ordering;
3
4use rustc_abi::{Align, BackendRepr, ExternAbi, Float, HasDataLayout, Primitive, Size};
5use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh, wants_wasm_eh};
6use rustc_codegen_ssa::codegen_attrs::autodiff_attrs;
7use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
8use rustc_codegen_ssa::errors::{ExpectedPointerMutability, InvalidMonomorphization};
9use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
10use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
11use rustc_codegen_ssa::traits::*;
12use rustc_hir::def_id::LOCAL_CRATE;
13use rustc_hir::{self as hir};
14use rustc_middle::mir::BinOp;
15use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, LayoutOf};
16use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TyCtxt, TypingEnv};
17use rustc_middle::{bug, span_bug};
18use rustc_span::{Span, Symbol, sym};
19use rustc_symbol_mangling::{mangle_internal_symbol, symbol_name_for_instance_in_crate};
20use rustc_target::callconv::PassMode;
21use tracing::debug;
22
23use crate::abi::FnAbiLlvmExt;
24use crate::builder::Builder;
25use crate::builder::autodiff::{adjust_activity_to_abi, generate_enzyme_call};
26use crate::context::CodegenCx;
27use crate::errors::AutoDiffWithoutEnable;
28use crate::llvm::{self, Metadata, Type, Value};
29use crate::type_of::LayoutLlvmExt;
30use crate::va_arg::emit_va_arg;
31
32fn call_simple_intrinsic<'ll, 'tcx>(
33    bx: &mut Builder<'_, 'll, 'tcx>,
34    name: Symbol,
35    args: &[OperandRef<'tcx, &'ll Value>],
36) -> Option<&'ll Value> {
37    let (base_name, type_params): (&'static str, &[&'ll Type]) = match name {
38        sym::sqrtf16 => ("llvm.sqrt", &[bx.type_f16()]),
39        sym::sqrtf32 => ("llvm.sqrt", &[bx.type_f32()]),
40        sym::sqrtf64 => ("llvm.sqrt", &[bx.type_f64()]),
41        sym::sqrtf128 => ("llvm.sqrt", &[bx.type_f128()]),
42
43        sym::powif16 => ("llvm.powi", &[bx.type_f16(), bx.type_i32()]),
44        sym::powif32 => ("llvm.powi", &[bx.type_f32(), bx.type_i32()]),
45        sym::powif64 => ("llvm.powi", &[bx.type_f64(), bx.type_i32()]),
46        sym::powif128 => ("llvm.powi", &[bx.type_f128(), bx.type_i32()]),
47
48        sym::sinf16 => ("llvm.sin", &[bx.type_f16()]),
49        sym::sinf32 => ("llvm.sin", &[bx.type_f32()]),
50        sym::sinf64 => ("llvm.sin", &[bx.type_f64()]),
51        sym::sinf128 => ("llvm.sin", &[bx.type_f128()]),
52
53        sym::cosf16 => ("llvm.cos", &[bx.type_f16()]),
54        sym::cosf32 => ("llvm.cos", &[bx.type_f32()]),
55        sym::cosf64 => ("llvm.cos", &[bx.type_f64()]),
56        sym::cosf128 => ("llvm.cos", &[bx.type_f128()]),
57
58        sym::powf16 => ("llvm.pow", &[bx.type_f16()]),
59        sym::powf32 => ("llvm.pow", &[bx.type_f32()]),
60        sym::powf64 => ("llvm.pow", &[bx.type_f64()]),
61        sym::powf128 => ("llvm.pow", &[bx.type_f128()]),
62
63        sym::expf16 => ("llvm.exp", &[bx.type_f16()]),
64        sym::expf32 => ("llvm.exp", &[bx.type_f32()]),
65        sym::expf64 => ("llvm.exp", &[bx.type_f64()]),
66        sym::expf128 => ("llvm.exp", &[bx.type_f128()]),
67
68        sym::exp2f16 => ("llvm.exp2", &[bx.type_f16()]),
69        sym::exp2f32 => ("llvm.exp2", &[bx.type_f32()]),
70        sym::exp2f64 => ("llvm.exp2", &[bx.type_f64()]),
71        sym::exp2f128 => ("llvm.exp2", &[bx.type_f128()]),
72
73        sym::logf16 => ("llvm.log", &[bx.type_f16()]),
74        sym::logf32 => ("llvm.log", &[bx.type_f32()]),
75        sym::logf64 => ("llvm.log", &[bx.type_f64()]),
76        sym::logf128 => ("llvm.log", &[bx.type_f128()]),
77
78        sym::log10f16 => ("llvm.log10", &[bx.type_f16()]),
79        sym::log10f32 => ("llvm.log10", &[bx.type_f32()]),
80        sym::log10f64 => ("llvm.log10", &[bx.type_f64()]),
81        sym::log10f128 => ("llvm.log10", &[bx.type_f128()]),
82
83        sym::log2f16 => ("llvm.log2", &[bx.type_f16()]),
84        sym::log2f32 => ("llvm.log2", &[bx.type_f32()]),
85        sym::log2f64 => ("llvm.log2", &[bx.type_f64()]),
86        sym::log2f128 => ("llvm.log2", &[bx.type_f128()]),
87
88        sym::fmaf16 => ("llvm.fma", &[bx.type_f16()]),
89        sym::fmaf32 => ("llvm.fma", &[bx.type_f32()]),
90        sym::fmaf64 => ("llvm.fma", &[bx.type_f64()]),
91        sym::fmaf128 => ("llvm.fma", &[bx.type_f128()]),
92
93        sym::fmuladdf16 => ("llvm.fmuladd", &[bx.type_f16()]),
94        sym::fmuladdf32 => ("llvm.fmuladd", &[bx.type_f32()]),
95        sym::fmuladdf64 => ("llvm.fmuladd", &[bx.type_f64()]),
96        sym::fmuladdf128 => ("llvm.fmuladd", &[bx.type_f128()]),
97
98        sym::fabsf16 => ("llvm.fabs", &[bx.type_f16()]),
99        sym::fabsf32 => ("llvm.fabs", &[bx.type_f32()]),
100        sym::fabsf64 => ("llvm.fabs", &[bx.type_f64()]),
101        sym::fabsf128 => ("llvm.fabs", &[bx.type_f128()]),
102
103        sym::minnumf16 => ("llvm.minnum", &[bx.type_f16()]),
104        sym::minnumf32 => ("llvm.minnum", &[bx.type_f32()]),
105        sym::minnumf64 => ("llvm.minnum", &[bx.type_f64()]),
106        sym::minnumf128 => ("llvm.minnum", &[bx.type_f128()]),
107
108        // FIXME: LLVM currently mis-compile those intrinsics, re-enable them
109        // when llvm/llvm-project#{139380,139381,140445} are fixed.
110        //sym::minimumf16 => ("llvm.minimum", &[bx.type_f16()]),
111        //sym::minimumf32 => ("llvm.minimum", &[bx.type_f32()]),
112        //sym::minimumf64 => ("llvm.minimum", &[bx.type_f64()]),
113        //sym::minimumf128 => ("llvm.minimum", &[cx.type_f128()]),
114        //
115        sym::maxnumf16 => ("llvm.maxnum", &[bx.type_f16()]),
116        sym::maxnumf32 => ("llvm.maxnum", &[bx.type_f32()]),
117        sym::maxnumf64 => ("llvm.maxnum", &[bx.type_f64()]),
118        sym::maxnumf128 => ("llvm.maxnum", &[bx.type_f128()]),
119
120        // FIXME: LLVM currently mis-compile those intrinsics, re-enable them
121        // when llvm/llvm-project#{139380,139381,140445} are fixed.
122        //sym::maximumf16 => ("llvm.maximum", &[bx.type_f16()]),
123        //sym::maximumf32 => ("llvm.maximum", &[bx.type_f32()]),
124        //sym::maximumf64 => ("llvm.maximum", &[bx.type_f64()]),
125        //sym::maximumf128 => ("llvm.maximum", &[cx.type_f128()]),
126        //
127        sym::copysignf16 => ("llvm.copysign", &[bx.type_f16()]),
128        sym::copysignf32 => ("llvm.copysign", &[bx.type_f32()]),
129        sym::copysignf64 => ("llvm.copysign", &[bx.type_f64()]),
130        sym::copysignf128 => ("llvm.copysign", &[bx.type_f128()]),
131
132        sym::floorf16 => ("llvm.floor", &[bx.type_f16()]),
133        sym::floorf32 => ("llvm.floor", &[bx.type_f32()]),
134        sym::floorf64 => ("llvm.floor", &[bx.type_f64()]),
135        sym::floorf128 => ("llvm.floor", &[bx.type_f128()]),
136
137        sym::ceilf16 => ("llvm.ceil", &[bx.type_f16()]),
138        sym::ceilf32 => ("llvm.ceil", &[bx.type_f32()]),
139        sym::ceilf64 => ("llvm.ceil", &[bx.type_f64()]),
140        sym::ceilf128 => ("llvm.ceil", &[bx.type_f128()]),
141
142        sym::truncf16 => ("llvm.trunc", &[bx.type_f16()]),
143        sym::truncf32 => ("llvm.trunc", &[bx.type_f32()]),
144        sym::truncf64 => ("llvm.trunc", &[bx.type_f64()]),
145        sym::truncf128 => ("llvm.trunc", &[bx.type_f128()]),
146
147        // We could use any of `rint`, `nearbyint`, or `roundeven`
148        // for this -- they are all identical in semantics when
149        // assuming the default FP environment.
150        // `rint` is what we used for $forever.
151        sym::round_ties_even_f16 => ("llvm.rint", &[bx.type_f16()]),
152        sym::round_ties_even_f32 => ("llvm.rint", &[bx.type_f32()]),
153        sym::round_ties_even_f64 => ("llvm.rint", &[bx.type_f64()]),
154        sym::round_ties_even_f128 => ("llvm.rint", &[bx.type_f128()]),
155
156        sym::roundf16 => ("llvm.round", &[bx.type_f16()]),
157        sym::roundf32 => ("llvm.round", &[bx.type_f32()]),
158        sym::roundf64 => ("llvm.round", &[bx.type_f64()]),
159        sym::roundf128 => ("llvm.round", &[bx.type_f128()]),
160
161        _ => return None,
162    };
163    Some(bx.call_intrinsic(
164        base_name,
165        type_params,
166        &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
167    ))
168}
169
170impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
171    fn codegen_intrinsic_call(
172        &mut self,
173        instance: ty::Instance<'tcx>,
174        args: &[OperandRef<'tcx, &'ll Value>],
175        result: PlaceRef<'tcx, &'ll Value>,
176        span: Span,
177    ) -> Result<(), ty::Instance<'tcx>> {
178        let tcx = self.tcx;
179
180        let name = tcx.item_name(instance.def_id());
181        let fn_args = instance.args;
182
183        let simple = call_simple_intrinsic(self, name, args);
184        let llval = match name {
185            _ if simple.is_some() => simple.unwrap(),
186            sym::ptr_mask => {
187                let ptr = args[0].immediate();
188                self.call_intrinsic(
189                    "llvm.ptrmask",
190                    &[self.val_ty(ptr), self.type_isize()],
191                    &[ptr, args[1].immediate()],
192                )
193            }
194            sym::autodiff => {
195                codegen_autodiff(self, tcx, instance, args, result);
196                return Ok(());
197            }
198            sym::is_val_statically_known => {
199                if let OperandValue::Immediate(imm) = args[0].val {
200                    self.call_intrinsic(
201                        "llvm.is.constant",
202                        &[args[0].layout.immediate_llvm_type(self.cx)],
203                        &[imm],
204                    )
205                } else {
206                    self.const_bool(false)
207                }
208            }
209            sym::select_unpredictable => {
210                let cond = args[0].immediate();
211                assert_eq!(args[1].layout, args[2].layout);
212                let select = |bx: &mut Self, true_val, false_val| {
213                    let result = bx.select(cond, true_val, false_val);
214                    bx.set_unpredictable(&result);
215                    result
216                };
217                match (args[1].val, args[2].val) {
218                    (OperandValue::Ref(true_val), OperandValue::Ref(false_val)) => {
219                        assert!(true_val.llextra.is_none());
220                        assert!(false_val.llextra.is_none());
221                        assert_eq!(true_val.align, false_val.align);
222                        let ptr = select(self, true_val.llval, false_val.llval);
223                        let selected =
224                            OperandValue::Ref(PlaceValue::new_sized(ptr, true_val.align));
225                        selected.store(self, result);
226                        return Ok(());
227                    }
228                    (OperandValue::Immediate(_), OperandValue::Immediate(_))
229                    | (OperandValue::Pair(_, _), OperandValue::Pair(_, _)) => {
230                        let true_val = args[1].immediate_or_packed_pair(self);
231                        let false_val = args[2].immediate_or_packed_pair(self);
232                        select(self, true_val, false_val)
233                    }
234                    (OperandValue::ZeroSized, OperandValue::ZeroSized) => return Ok(()),
235                    _ => span_bug!(span, "Incompatible OperandValue for select_unpredictable"),
236                }
237            }
238            sym::catch_unwind => {
239                catch_unwind_intrinsic(
240                    self,
241                    args[0].immediate(),
242                    args[1].immediate(),
243                    args[2].immediate(),
244                    result,
245                );
246                return Ok(());
247            }
248            sym::breakpoint => self.call_intrinsic("llvm.debugtrap", &[], &[]),
249            sym::va_copy => {
250                let dest = args[0].immediate();
251                self.call_intrinsic(
252                    "llvm.va_copy",
253                    &[self.val_ty(dest)],
254                    &[dest, args[1].immediate()],
255                )
256            }
257            sym::va_arg => {
258                match result.layout.backend_repr {
259                    BackendRepr::Scalar(scalar) => {
260                        match scalar.primitive() {
261                            Primitive::Int(..) => {
262                                if self.cx().size_of(result.layout.ty).bytes() < 4 {
263                                    // `va_arg` should not be called on an integer type
264                                    // less than 4 bytes in length. If it is, promote
265                                    // the integer to an `i32` and truncate the result
266                                    // back to the smaller type.
267                                    let promoted_result = emit_va_arg(self, args[0], tcx.types.i32);
268                                    self.trunc(promoted_result, result.layout.llvm_type(self))
269                                } else {
270                                    emit_va_arg(self, args[0], result.layout.ty)
271                                }
272                            }
273                            Primitive::Float(Float::F16) => {
274                                bug!("the va_arg intrinsic does not work with `f16`")
275                            }
276                            Primitive::Float(Float::F64) | Primitive::Pointer(_) => {
277                                emit_va_arg(self, args[0], result.layout.ty)
278                            }
279                            // `va_arg` should never be used with the return type f32.
280                            Primitive::Float(Float::F32) => {
281                                bug!("the va_arg intrinsic does not work with `f32`")
282                            }
283                            Primitive::Float(Float::F128) => {
284                                bug!("the va_arg intrinsic does not work with `f128`")
285                            }
286                        }
287                    }
288                    _ => bug!("the va_arg intrinsic does not work with non-scalar types"),
289                }
290            }
291
292            sym::volatile_load | sym::unaligned_volatile_load => {
293                let ptr = args[0].immediate();
294                let load = self.volatile_load(result.layout.llvm_type(self), ptr);
295                let align = if name == sym::unaligned_volatile_load {
296                    1
297                } else {
298                    result.layout.align.bytes() as u32
299                };
300                unsafe {
301                    llvm::LLVMSetAlignment(load, align);
302                }
303                if !result.layout.is_zst() {
304                    self.store_to_place(load, result.val);
305                }
306                return Ok(());
307            }
308            sym::volatile_store => {
309                let dst = args[0].deref(self.cx());
310                args[1].val.volatile_store(self, dst);
311                return Ok(());
312            }
313            sym::unaligned_volatile_store => {
314                let dst = args[0].deref(self.cx());
315                args[1].val.unaligned_volatile_store(self, dst);
316                return Ok(());
317            }
318            sym::prefetch_read_data
319            | sym::prefetch_write_data
320            | sym::prefetch_read_instruction
321            | sym::prefetch_write_instruction => {
322                let (rw, cache_type) = match name {
323                    sym::prefetch_read_data => (0, 1),
324                    sym::prefetch_write_data => (1, 1),
325                    sym::prefetch_read_instruction => (0, 0),
326                    sym::prefetch_write_instruction => (1, 0),
327                    _ => bug!(),
328                };
329                let ptr = args[0].immediate();
330                let locality = fn_args.const_at(1).to_value().valtree.unwrap_leaf().to_i32();
331                self.call_intrinsic(
332                    "llvm.prefetch",
333                    &[self.val_ty(ptr)],
334                    &[
335                        ptr,
336                        self.const_i32(rw),
337                        self.const_i32(locality),
338                        self.const_i32(cache_type),
339                    ],
340                )
341            }
342            sym::carrying_mul_add => {
343                let (size, signed) = fn_args.type_at(0).int_size_and_signed(self.tcx);
344
345                let wide_llty = self.type_ix(size.bits() * 2);
346                let args = args.as_array().unwrap();
347                let [a, b, c, d] = args.map(|a| self.intcast(a.immediate(), wide_llty, signed));
348
349                let wide = if signed {
350                    let prod = self.unchecked_smul(a, b);
351                    let acc = self.unchecked_sadd(prod, c);
352                    self.unchecked_sadd(acc, d)
353                } else {
354                    let prod = self.unchecked_umul(a, b);
355                    let acc = self.unchecked_uadd(prod, c);
356                    self.unchecked_uadd(acc, d)
357                };
358
359                let narrow_llty = self.type_ix(size.bits());
360                let low = self.trunc(wide, narrow_llty);
361                let bits_const = self.const_uint(wide_llty, size.bits());
362                // No need for ashr when signed; LLVM changes it to lshr anyway.
363                let high = self.lshr(wide, bits_const);
364                // FIXME: could be `trunc nuw`, even for signed.
365                let high = self.trunc(high, narrow_llty);
366
367                let pair_llty = self.type_struct(&[narrow_llty, narrow_llty], false);
368                let pair = self.const_poison(pair_llty);
369                let pair = self.insert_value(pair, low, 0);
370                let pair = self.insert_value(pair, high, 1);
371                pair
372            }
373            sym::ctlz
374            | sym::ctlz_nonzero
375            | sym::cttz
376            | sym::cttz_nonzero
377            | sym::ctpop
378            | sym::bswap
379            | sym::bitreverse
380            | sym::rotate_left
381            | sym::rotate_right
382            | sym::saturating_add
383            | sym::saturating_sub
384            | sym::unchecked_funnel_shl
385            | sym::unchecked_funnel_shr => {
386                let ty = args[0].layout.ty;
387                if !ty.is_integral() {
388                    tcx.dcx().emit_err(InvalidMonomorphization::BasicIntegerType {
389                        span,
390                        name,
391                        ty,
392                    });
393                    return Ok(());
394                }
395                let (size, signed) = ty.int_size_and_signed(self.tcx);
396                let width = size.bits();
397                let llty = self.type_ix(width);
398                match name {
399                    sym::ctlz | sym::ctlz_nonzero | sym::cttz | sym::cttz_nonzero => {
400                        let y =
401                            self.const_bool(name == sym::ctlz_nonzero || name == sym::cttz_nonzero);
402                        let llvm_name = if name == sym::ctlz || name == sym::ctlz_nonzero {
403                            "llvm.ctlz"
404                        } else {
405                            "llvm.cttz"
406                        };
407                        let ret =
408                            self.call_intrinsic(llvm_name, &[llty], &[args[0].immediate(), y]);
409                        self.intcast(ret, result.layout.llvm_type(self), false)
410                    }
411                    sym::ctpop => {
412                        let ret =
413                            self.call_intrinsic("llvm.ctpop", &[llty], &[args[0].immediate()]);
414                        self.intcast(ret, result.layout.llvm_type(self), false)
415                    }
416                    sym::bswap => {
417                        if width == 8 {
418                            args[0].immediate() // byte swap a u8/i8 is just a no-op
419                        } else {
420                            self.call_intrinsic("llvm.bswap", &[llty], &[args[0].immediate()])
421                        }
422                    }
423                    sym::bitreverse => {
424                        self.call_intrinsic("llvm.bitreverse", &[llty], &[args[0].immediate()])
425                    }
426                    sym::rotate_left
427                    | sym::rotate_right
428                    | sym::unchecked_funnel_shl
429                    | sym::unchecked_funnel_shr => {
430                        let is_left = name == sym::rotate_left || name == sym::unchecked_funnel_shl;
431                        let lhs = args[0].immediate();
432                        let (rhs, raw_shift) =
433                            if name == sym::rotate_left || name == sym::rotate_right {
434                                // rotate = funnel shift with first two args the same
435                                (lhs, args[1].immediate())
436                            } else {
437                                (args[1].immediate(), args[2].immediate())
438                            };
439                        let llvm_name = format!("llvm.fsh{}", if is_left { 'l' } else { 'r' });
440
441                        // llvm expects shift to be the same type as the values, but rust
442                        // always uses `u32`.
443                        let raw_shift = self.intcast(raw_shift, self.val_ty(lhs), false);
444
445                        self.call_intrinsic(llvm_name, &[llty], &[lhs, rhs, raw_shift])
446                    }
447                    sym::saturating_add | sym::saturating_sub => {
448                        let is_add = name == sym::saturating_add;
449                        let lhs = args[0].immediate();
450                        let rhs = args[1].immediate();
451                        let llvm_name = format!(
452                            "llvm.{}{}.sat",
453                            if signed { 's' } else { 'u' },
454                            if is_add { "add" } else { "sub" },
455                        );
456                        self.call_intrinsic(llvm_name, &[llty], &[lhs, rhs])
457                    }
458                    _ => bug!(),
459                }
460            }
461
462            sym::raw_eq => {
463                use BackendRepr::*;
464                let tp_ty = fn_args.type_at(0);
465                let layout = self.layout_of(tp_ty).layout;
466                let use_integer_compare = match layout.backend_repr() {
467                    Scalar(_) | ScalarPair(_, _) => true,
468                    SimdVector { .. } => false,
469                    Memory { .. } => {
470                        // For rusty ABIs, small aggregates are actually passed
471                        // as `RegKind::Integer` (see `FnAbi::adjust_for_abi`),
472                        // so we re-use that same threshold here.
473                        layout.size() <= self.data_layout().pointer_size() * 2
474                    }
475                };
476
477                let a = args[0].immediate();
478                let b = args[1].immediate();
479                if layout.size().bytes() == 0 {
480                    self.const_bool(true)
481                } else if use_integer_compare {
482                    let integer_ty = self.type_ix(layout.size().bits());
483                    let a_val = self.load(integer_ty, a, layout.align().abi);
484                    let b_val = self.load(integer_ty, b, layout.align().abi);
485                    self.icmp(IntPredicate::IntEQ, a_val, b_val)
486                } else {
487                    let n = self.const_usize(layout.size().bytes());
488                    let cmp = self.call_intrinsic("memcmp", &[], &[a, b, n]);
489                    self.icmp(IntPredicate::IntEQ, cmp, self.const_int(self.type_int(), 0))
490                }
491            }
492
493            sym::compare_bytes => {
494                // Here we assume that the `memcmp` provided by the target is a NOP for size 0.
495                let cmp = self.call_intrinsic(
496                    "memcmp",
497                    &[],
498                    &[args[0].immediate(), args[1].immediate(), args[2].immediate()],
499                );
500                // Some targets have `memcmp` returning `i16`, but the intrinsic is always `i32`.
501                self.sext(cmp, self.type_ix(32))
502            }
503
504            sym::black_box => {
505                args[0].val.store(self, result);
506                let result_val_span = [result.val.llval];
507                // We need to "use" the argument in some way LLVM can't introspect, and on
508                // targets that support it we can typically leverage inline assembly to do
509                // this. LLVM's interpretation of inline assembly is that it's, well, a black
510                // box. This isn't the greatest implementation since it probably deoptimizes
511                // more than we want, but it's so far good enough.
512                //
513                // For zero-sized types, the location pointed to by the result may be
514                // uninitialized. Do not "use" the result in this case; instead just clobber
515                // the memory.
516                let (constraint, inputs): (&str, &[_]) = if result.layout.is_zst() {
517                    ("~{memory}", &[])
518                } else {
519                    ("r,~{memory}", &result_val_span)
520                };
521                crate::asm::inline_asm_call(
522                    self,
523                    "",
524                    constraint,
525                    inputs,
526                    self.type_void(),
527                    &[],
528                    true,
529                    false,
530                    llvm::AsmDialect::Att,
531                    &[span],
532                    false,
533                    None,
534                    None,
535                )
536                .unwrap_or_else(|| bug!("failed to generate inline asm call for `black_box`"));
537
538                // We have copied the value to `result` already.
539                return Ok(());
540            }
541
542            _ if name.as_str().starts_with("simd_") => {
543                // Unpack non-power-of-2 #[repr(packed, simd)] arguments.
544                // This gives them the expected layout of a regular #[repr(simd)] vector.
545                let mut loaded_args = Vec::new();
546                for arg in args {
547                    loaded_args.push(
548                        // #[repr(packed, simd)] vectors are passed like arrays (as references,
549                        // with reduced alignment and no padding) rather than as immediates.
550                        // We can use a vector load to fix the layout and turn the argument
551                        // into an immediate.
552                        if arg.layout.ty.is_simd()
553                            && let OperandValue::Ref(place) = arg.val
554                        {
555                            let (size, elem_ty) = arg.layout.ty.simd_size_and_type(self.tcx());
556                            let elem_ll_ty = match elem_ty.kind() {
557                                ty::Float(f) => self.type_float_from_ty(*f),
558                                ty::Int(i) => self.type_int_from_ty(*i),
559                                ty::Uint(u) => self.type_uint_from_ty(*u),
560                                ty::RawPtr(_, _) => self.type_ptr(),
561                                _ => unreachable!(),
562                            };
563                            let loaded =
564                                self.load_from_place(self.type_vector(elem_ll_ty, size), place);
565                            OperandRef::from_immediate_or_packed_pair(self, loaded, arg.layout)
566                        } else {
567                            *arg
568                        },
569                    );
570                }
571
572                let llret_ty = if result.layout.ty.is_simd()
573                    && let BackendRepr::Memory { .. } = result.layout.backend_repr
574                {
575                    let (size, elem_ty) = result.layout.ty.simd_size_and_type(self.tcx());
576                    let elem_ll_ty = match elem_ty.kind() {
577                        ty::Float(f) => self.type_float_from_ty(*f),
578                        ty::Int(i) => self.type_int_from_ty(*i),
579                        ty::Uint(u) => self.type_uint_from_ty(*u),
580                        ty::RawPtr(_, _) => self.type_ptr(),
581                        _ => unreachable!(),
582                    };
583                    self.type_vector(elem_ll_ty, size)
584                } else {
585                    result.layout.llvm_type(self)
586                };
587
588                match generic_simd_intrinsic(
589                    self,
590                    name,
591                    fn_args,
592                    &loaded_args,
593                    result.layout.ty,
594                    llret_ty,
595                    span,
596                ) {
597                    Ok(llval) => llval,
598                    // If there was an error, just skip this invocation... we'll abort compilation
599                    // anyway, but we can keep codegen'ing to find more errors.
600                    Err(()) => return Ok(()),
601                }
602            }
603
604            _ => {
605                debug!("unknown intrinsic '{}' -- falling back to default body", name);
606                // Call the fallback body instead of generating the intrinsic code
607                return Err(ty::Instance::new_raw(instance.def_id(), instance.args));
608            }
609        };
610
611        if result.layout.ty.is_bool() {
612            let val = self.from_immediate(llval);
613            self.store_to_place(val, result.val);
614        } else if !result.layout.ty.is_unit() {
615            self.store_to_place(llval, result.val);
616        }
617        Ok(())
618    }
619
620    fn abort(&mut self) {
621        self.call_intrinsic("llvm.trap", &[], &[]);
622    }
623
624    fn assume(&mut self, val: Self::Value) {
625        if self.cx.sess().opts.optimize != rustc_session::config::OptLevel::No {
626            self.call_intrinsic("llvm.assume", &[], &[val]);
627        }
628    }
629
630    fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value {
631        if self.cx.sess().opts.optimize != rustc_session::config::OptLevel::No {
632            self.call_intrinsic(
633                "llvm.expect",
634                &[self.type_i1()],
635                &[cond, self.const_bool(expected)],
636            )
637        } else {
638            cond
639        }
640    }
641
642    fn type_checked_load(
643        &mut self,
644        llvtable: &'ll Value,
645        vtable_byte_offset: u64,
646        typeid: &'ll Metadata,
647    ) -> Self::Value {
648        let typeid = self.get_metadata_value(typeid);
649        let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
650        let type_checked_load = self.call_intrinsic(
651            "llvm.type.checked.load",
652            &[],
653            &[llvtable, vtable_byte_offset, typeid],
654        );
655        self.extract_value(type_checked_load, 0)
656    }
657
658    fn va_start(&mut self, va_list: &'ll Value) -> &'ll Value {
659        self.call_intrinsic("llvm.va_start", &[self.val_ty(va_list)], &[va_list])
660    }
661
662    fn va_end(&mut self, va_list: &'ll Value) -> &'ll Value {
663        self.call_intrinsic("llvm.va_end", &[self.val_ty(va_list)], &[va_list])
664    }
665}
666
667fn catch_unwind_intrinsic<'ll, 'tcx>(
668    bx: &mut Builder<'_, 'll, 'tcx>,
669    try_func: &'ll Value,
670    data: &'ll Value,
671    catch_func: &'ll Value,
672    dest: PlaceRef<'tcx, &'ll Value>,
673) {
674    if !bx.sess().panic_strategy().unwinds() {
675        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
676        bx.call(try_func_ty, None, None, try_func, &[data], None, None);
677        // Return 0 unconditionally from the intrinsic call;
678        // we can never unwind.
679        OperandValue::Immediate(bx.const_i32(0)).store(bx, dest);
680    } else if wants_msvc_seh(bx.sess()) {
681        codegen_msvc_try(bx, try_func, data, catch_func, dest);
682    } else if wants_wasm_eh(bx.sess()) {
683        codegen_wasm_try(bx, try_func, data, catch_func, dest);
684    } else if bx.sess().target.os == "emscripten" {
685        codegen_emcc_try(bx, try_func, data, catch_func, dest);
686    } else {
687        codegen_gnu_try(bx, try_func, data, catch_func, dest);
688    }
689}
690
691// MSVC's definition of the `rust_try` function.
692//
693// This implementation uses the new exception handling instructions in LLVM
694// which have support in LLVM for SEH on MSVC targets. Although these
695// instructions are meant to work for all targets, as of the time of this
696// writing, however, LLVM does not recommend the usage of these new instructions
697// as the old ones are still more optimized.
698fn codegen_msvc_try<'ll, 'tcx>(
699    bx: &mut Builder<'_, 'll, 'tcx>,
700    try_func: &'ll Value,
701    data: &'ll Value,
702    catch_func: &'ll Value,
703    dest: PlaceRef<'tcx, &'ll Value>,
704) {
705    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
706        bx.set_personality_fn(bx.eh_personality());
707
708        let normal = bx.append_sibling_block("normal");
709        let catchswitch = bx.append_sibling_block("catchswitch");
710        let catchpad_rust = bx.append_sibling_block("catchpad_rust");
711        let catchpad_foreign = bx.append_sibling_block("catchpad_foreign");
712        let caught = bx.append_sibling_block("caught");
713
714        let try_func = llvm::get_param(bx.llfn(), 0);
715        let data = llvm::get_param(bx.llfn(), 1);
716        let catch_func = llvm::get_param(bx.llfn(), 2);
717
718        // We're generating an IR snippet that looks like:
719        //
720        //   declare i32 @rust_try(%try_func, %data, %catch_func) {
721        //      %slot = alloca i8*
722        //      invoke %try_func(%data) to label %normal unwind label %catchswitch
723        //
724        //   normal:
725        //      ret i32 0
726        //
727        //   catchswitch:
728        //      %cs = catchswitch within none [%catchpad_rust, %catchpad_foreign] unwind to caller
729        //
730        //   catchpad_rust:
731        //      %tok = catchpad within %cs [%type_descriptor, 8, %slot]
732        //      %ptr = load %slot
733        //      call %catch_func(%data, %ptr)
734        //      catchret from %tok to label %caught
735        //
736        //   catchpad_foreign:
737        //      %tok = catchpad within %cs [null, 64, null]
738        //      call %catch_func(%data, null)
739        //      catchret from %tok to label %caught
740        //
741        //   caught:
742        //      ret i32 1
743        //   }
744        //
745        // This structure follows the basic usage of throw/try/catch in LLVM.
746        // For example, compile this C++ snippet to see what LLVM generates:
747        //
748        //      struct rust_panic {
749        //          rust_panic(const rust_panic&);
750        //          ~rust_panic();
751        //
752        //          void* x[2];
753        //      };
754        //
755        //      int __rust_try(
756        //          void (*try_func)(void*),
757        //          void *data,
758        //          void (*catch_func)(void*, void*) noexcept
759        //      ) {
760        //          try {
761        //              try_func(data);
762        //              return 0;
763        //          } catch(rust_panic& a) {
764        //              catch_func(data, &a);
765        //              return 1;
766        //          } catch(...) {
767        //              catch_func(data, NULL);
768        //              return 1;
769        //          }
770        //      }
771        //
772        // More information can be found in libstd's seh.rs implementation.
773        let ptr_size = bx.tcx().data_layout.pointer_size();
774        let ptr_align = bx.tcx().data_layout.pointer_align().abi;
775        let slot = bx.alloca(ptr_size, ptr_align);
776        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
777        bx.invoke(try_func_ty, None, None, try_func, &[data], normal, catchswitch, None, None);
778
779        bx.switch_to_block(normal);
780        bx.ret(bx.const_i32(0));
781
782        bx.switch_to_block(catchswitch);
783        let cs = bx.catch_switch(None, None, &[catchpad_rust, catchpad_foreign]);
784
785        // We can't use the TypeDescriptor defined in libpanic_unwind because it
786        // might be in another DLL and the SEH encoding only supports specifying
787        // a TypeDescriptor from the current module.
788        //
789        // However this isn't an issue since the MSVC runtime uses string
790        // comparison on the type name to match TypeDescriptors rather than
791        // pointer equality.
792        //
793        // So instead we generate a new TypeDescriptor in each module that uses
794        // `try` and let the linker merge duplicate definitions in the same
795        // module.
796        //
797        // When modifying, make sure that the type_name string exactly matches
798        // the one used in library/panic_unwind/src/seh.rs.
799        let type_info_vtable = bx.declare_global("??_7type_info@@6B@", bx.type_ptr());
800        let type_name = bx.const_bytes(b"rust_panic\0");
801        let type_info =
802            bx.const_struct(&[type_info_vtable, bx.const_null(bx.type_ptr()), type_name], false);
803        let tydesc = bx.declare_global(
804            &mangle_internal_symbol(bx.tcx, "__rust_panic_type_info"),
805            bx.val_ty(type_info),
806        );
807
808        llvm::set_linkage(tydesc, llvm::Linkage::LinkOnceODRLinkage);
809        if bx.cx.tcx.sess.target.supports_comdat() {
810            llvm::SetUniqueComdat(bx.llmod, tydesc);
811        }
812        llvm::set_initializer(tydesc, type_info);
813
814        // The flag value of 8 indicates that we are catching the exception by
815        // reference instead of by value. We can't use catch by value because
816        // that requires copying the exception object, which we don't support
817        // since our exception object effectively contains a Box.
818        //
819        // Source: MicrosoftCXXABI::getAddrOfCXXCatchHandlerType in clang
820        bx.switch_to_block(catchpad_rust);
821        let flags = bx.const_i32(8);
822        let funclet = bx.catch_pad(cs, &[tydesc, flags, slot]);
823        let ptr = bx.load(bx.type_ptr(), slot, ptr_align);
824        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
825        bx.call(catch_ty, None, None, catch_func, &[data, ptr], Some(&funclet), None);
826        bx.catch_ret(&funclet, caught);
827
828        // The flag value of 64 indicates a "catch-all".
829        bx.switch_to_block(catchpad_foreign);
830        let flags = bx.const_i32(64);
831        let null = bx.const_null(bx.type_ptr());
832        let funclet = bx.catch_pad(cs, &[null, flags, null]);
833        bx.call(catch_ty, None, None, catch_func, &[data, null], Some(&funclet), None);
834        bx.catch_ret(&funclet, caught);
835
836        bx.switch_to_block(caught);
837        bx.ret(bx.const_i32(1));
838    });
839
840    // Note that no invoke is used here because by definition this function
841    // can't panic (that's what it's catching).
842    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
843    OperandValue::Immediate(ret).store(bx, dest);
844}
845
846// WASM's definition of the `rust_try` function.
847fn codegen_wasm_try<'ll, 'tcx>(
848    bx: &mut Builder<'_, 'll, 'tcx>,
849    try_func: &'ll Value,
850    data: &'ll Value,
851    catch_func: &'ll Value,
852    dest: PlaceRef<'tcx, &'ll Value>,
853) {
854    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
855        bx.set_personality_fn(bx.eh_personality());
856
857        let normal = bx.append_sibling_block("normal");
858        let catchswitch = bx.append_sibling_block("catchswitch");
859        let catchpad = bx.append_sibling_block("catchpad");
860        let caught = bx.append_sibling_block("caught");
861
862        let try_func = llvm::get_param(bx.llfn(), 0);
863        let data = llvm::get_param(bx.llfn(), 1);
864        let catch_func = llvm::get_param(bx.llfn(), 2);
865
866        // We're generating an IR snippet that looks like:
867        //
868        //   declare i32 @rust_try(%try_func, %data, %catch_func) {
869        //      %slot = alloca i8*
870        //      invoke %try_func(%data) to label %normal unwind label %catchswitch
871        //
872        //   normal:
873        //      ret i32 0
874        //
875        //   catchswitch:
876        //      %cs = catchswitch within none [%catchpad] unwind to caller
877        //
878        //   catchpad:
879        //      %tok = catchpad within %cs [null]
880        //      %ptr = call @llvm.wasm.get.exception(token %tok)
881        //      %sel = call @llvm.wasm.get.ehselector(token %tok)
882        //      call %catch_func(%data, %ptr)
883        //      catchret from %tok to label %caught
884        //
885        //   caught:
886        //      ret i32 1
887        //   }
888        //
889        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
890        bx.invoke(try_func_ty, None, None, try_func, &[data], normal, catchswitch, None, None);
891
892        bx.switch_to_block(normal);
893        bx.ret(bx.const_i32(0));
894
895        bx.switch_to_block(catchswitch);
896        let cs = bx.catch_switch(None, None, &[catchpad]);
897
898        bx.switch_to_block(catchpad);
899        let null = bx.const_null(bx.type_ptr());
900        let funclet = bx.catch_pad(cs, &[null]);
901
902        let ptr = bx.call_intrinsic("llvm.wasm.get.exception", &[], &[funclet.cleanuppad()]);
903        let _sel = bx.call_intrinsic("llvm.wasm.get.ehselector", &[], &[funclet.cleanuppad()]);
904
905        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
906        bx.call(catch_ty, None, None, catch_func, &[data, ptr], Some(&funclet), None);
907        bx.catch_ret(&funclet, caught);
908
909        bx.switch_to_block(caught);
910        bx.ret(bx.const_i32(1));
911    });
912
913    // Note that no invoke is used here because by definition this function
914    // can't panic (that's what it's catching).
915    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
916    OperandValue::Immediate(ret).store(bx, dest);
917}
918
919// Definition of the standard `try` function for Rust using the GNU-like model
920// of exceptions (e.g., the normal semantics of LLVM's `landingpad` and `invoke`
921// instructions).
922//
923// This codegen is a little surprising because we always call a shim
924// function instead of inlining the call to `invoke` manually here. This is done
925// because in LLVM we're only allowed to have one personality per function
926// definition. The call to the `try` intrinsic is being inlined into the
927// function calling it, and that function may already have other personality
928// functions in play. By calling a shim we're guaranteed that our shim will have
929// the right personality function.
930fn codegen_gnu_try<'ll, 'tcx>(
931    bx: &mut Builder<'_, 'll, 'tcx>,
932    try_func: &'ll Value,
933    data: &'ll Value,
934    catch_func: &'ll Value,
935    dest: PlaceRef<'tcx, &'ll Value>,
936) {
937    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
938        // Codegens the shims described above:
939        //
940        //   bx:
941        //      invoke %try_func(%data) normal %normal unwind %catch
942        //
943        //   normal:
944        //      ret 0
945        //
946        //   catch:
947        //      (%ptr, _) = landingpad
948        //      call %catch_func(%data, %ptr)
949        //      ret 1
950        let then = bx.append_sibling_block("then");
951        let catch = bx.append_sibling_block("catch");
952
953        let try_func = llvm::get_param(bx.llfn(), 0);
954        let data = llvm::get_param(bx.llfn(), 1);
955        let catch_func = llvm::get_param(bx.llfn(), 2);
956        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
957        bx.invoke(try_func_ty, None, None, try_func, &[data], then, catch, None, None);
958
959        bx.switch_to_block(then);
960        bx.ret(bx.const_i32(0));
961
962        // Type indicator for the exception being thrown.
963        //
964        // The first value in this tuple is a pointer to the exception object
965        // being thrown. The second value is a "selector" indicating which of
966        // the landing pad clauses the exception's type had been matched to.
967        // rust_try ignores the selector.
968        bx.switch_to_block(catch);
969        let lpad_ty = bx.type_struct(&[bx.type_ptr(), bx.type_i32()], false);
970        let vals = bx.landing_pad(lpad_ty, bx.eh_personality(), 1);
971        let tydesc = bx.const_null(bx.type_ptr());
972        bx.add_clause(vals, tydesc);
973        let ptr = bx.extract_value(vals, 0);
974        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
975        bx.call(catch_ty, None, None, catch_func, &[data, ptr], None, None);
976        bx.ret(bx.const_i32(1));
977    });
978
979    // Note that no invoke is used here because by definition this function
980    // can't panic (that's what it's catching).
981    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
982    OperandValue::Immediate(ret).store(bx, dest);
983}
984
985// Variant of codegen_gnu_try used for emscripten where Rust panics are
986// implemented using C++ exceptions. Here we use exceptions of a specific type
987// (`struct rust_panic`) to represent Rust panics.
988fn codegen_emcc_try<'ll, 'tcx>(
989    bx: &mut Builder<'_, 'll, 'tcx>,
990    try_func: &'ll Value,
991    data: &'ll Value,
992    catch_func: &'ll Value,
993    dest: PlaceRef<'tcx, &'ll Value>,
994) {
995    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
996        // Codegens the shims described above:
997        //
998        //   bx:
999        //      invoke %try_func(%data) normal %normal unwind %catch
1000        //
1001        //   normal:
1002        //      ret 0
1003        //
1004        //   catch:
1005        //      (%ptr, %selector) = landingpad
1006        //      %rust_typeid = @llvm.eh.typeid.for(@_ZTI10rust_panic)
1007        //      %is_rust_panic = %selector == %rust_typeid
1008        //      %catch_data = alloca { i8*, i8 }
1009        //      %catch_data[0] = %ptr
1010        //      %catch_data[1] = %is_rust_panic
1011        //      call %catch_func(%data, %catch_data)
1012        //      ret 1
1013        let then = bx.append_sibling_block("then");
1014        let catch = bx.append_sibling_block("catch");
1015
1016        let try_func = llvm::get_param(bx.llfn(), 0);
1017        let data = llvm::get_param(bx.llfn(), 1);
1018        let catch_func = llvm::get_param(bx.llfn(), 2);
1019        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
1020        bx.invoke(try_func_ty, None, None, try_func, &[data], then, catch, None, None);
1021
1022        bx.switch_to_block(then);
1023        bx.ret(bx.const_i32(0));
1024
1025        // Type indicator for the exception being thrown.
1026        //
1027        // The first value in this tuple is a pointer to the exception object
1028        // being thrown. The second value is a "selector" indicating which of
1029        // the landing pad clauses the exception's type had been matched to.
1030        bx.switch_to_block(catch);
1031        let tydesc = bx.eh_catch_typeinfo();
1032        let lpad_ty = bx.type_struct(&[bx.type_ptr(), bx.type_i32()], false);
1033        let vals = bx.landing_pad(lpad_ty, bx.eh_personality(), 2);
1034        bx.add_clause(vals, tydesc);
1035        bx.add_clause(vals, bx.const_null(bx.type_ptr()));
1036        let ptr = bx.extract_value(vals, 0);
1037        let selector = bx.extract_value(vals, 1);
1038
1039        // Check if the typeid we got is the one for a Rust panic.
1040        let rust_typeid = bx.call_intrinsic("llvm.eh.typeid.for", &[bx.val_ty(tydesc)], &[tydesc]);
1041        let is_rust_panic = bx.icmp(IntPredicate::IntEQ, selector, rust_typeid);
1042        let is_rust_panic = bx.zext(is_rust_panic, bx.type_bool());
1043
1044        // We need to pass two values to catch_func (ptr and is_rust_panic), so
1045        // create an alloca and pass a pointer to that.
1046        let ptr_size = bx.tcx().data_layout.pointer_size();
1047        let ptr_align = bx.tcx().data_layout.pointer_align().abi;
1048        let i8_align = bx.tcx().data_layout.i8_align;
1049        // Required in order for there to be no padding between the fields.
1050        assert!(i8_align <= ptr_align);
1051        let catch_data = bx.alloca(2 * ptr_size, ptr_align);
1052        bx.store(ptr, catch_data, ptr_align);
1053        let catch_data_1 = bx.inbounds_ptradd(catch_data, bx.const_usize(ptr_size.bytes()));
1054        bx.store(is_rust_panic, catch_data_1, i8_align);
1055
1056        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
1057        bx.call(catch_ty, None, None, catch_func, &[data, catch_data], None, None);
1058        bx.ret(bx.const_i32(1));
1059    });
1060
1061    // Note that no invoke is used here because by definition this function
1062    // can't panic (that's what it's catching).
1063    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
1064    OperandValue::Immediate(ret).store(bx, dest);
1065}
1066
1067// Helper function to give a Block to a closure to codegen a shim function.
1068// This is currently primarily used for the `try` intrinsic functions above.
1069fn gen_fn<'a, 'll, 'tcx>(
1070    cx: &'a CodegenCx<'ll, 'tcx>,
1071    name: &str,
1072    rust_fn_sig: ty::PolyFnSig<'tcx>,
1073    codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>),
1074) -> (&'ll Type, &'ll Value) {
1075    let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty());
1076    let llty = fn_abi.llvm_type(cx);
1077    let llfn = cx.declare_fn(name, fn_abi, None);
1078    cx.set_frame_pointer_type(llfn);
1079    cx.apply_target_cpu_attr(llfn);
1080    // FIXME(eddyb) find a nicer way to do this.
1081    llvm::set_linkage(llfn, llvm::Linkage::InternalLinkage);
1082    let llbb = Builder::append_block(cx, llfn, "entry-block");
1083    let bx = Builder::build(cx, llbb);
1084    codegen(bx);
1085    (llty, llfn)
1086}
1087
1088// Helper function used to get a handle to the `__rust_try` function used to
1089// catch exceptions.
1090//
1091// This function is only generated once and is then cached.
1092fn get_rust_try_fn<'a, 'll, 'tcx>(
1093    cx: &'a CodegenCx<'ll, 'tcx>,
1094    codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>),
1095) -> (&'ll Type, &'ll Value) {
1096    if let Some(llfn) = cx.rust_try_fn.get() {
1097        return llfn;
1098    }
1099
1100    // Define the type up front for the signature of the rust_try function.
1101    let tcx = cx.tcx;
1102    let i8p = Ty::new_mut_ptr(tcx, tcx.types.i8);
1103    // `unsafe fn(*mut i8) -> ()`
1104    let try_fn_ty = Ty::new_fn_ptr(
1105        tcx,
1106        ty::Binder::dummy(tcx.mk_fn_sig(
1107            [i8p],
1108            tcx.types.unit,
1109            false,
1110            hir::Safety::Unsafe,
1111            ExternAbi::Rust,
1112        )),
1113    );
1114    // `unsafe fn(*mut i8, *mut i8) -> ()`
1115    let catch_fn_ty = Ty::new_fn_ptr(
1116        tcx,
1117        ty::Binder::dummy(tcx.mk_fn_sig(
1118            [i8p, i8p],
1119            tcx.types.unit,
1120            false,
1121            hir::Safety::Unsafe,
1122            ExternAbi::Rust,
1123        )),
1124    );
1125    // `unsafe fn(unsafe fn(*mut i8) -> (), *mut i8, unsafe fn(*mut i8, *mut i8) -> ()) -> i32`
1126    let rust_fn_sig = ty::Binder::dummy(cx.tcx.mk_fn_sig(
1127        [try_fn_ty, i8p, catch_fn_ty],
1128        tcx.types.i32,
1129        false,
1130        hir::Safety::Unsafe,
1131        ExternAbi::Rust,
1132    ));
1133    let rust_try = gen_fn(cx, "__rust_try", rust_fn_sig, codegen);
1134    cx.rust_try_fn.set(Some(rust_try));
1135    rust_try
1136}
1137
1138fn codegen_autodiff<'ll, 'tcx>(
1139    bx: &mut Builder<'_, 'll, 'tcx>,
1140    tcx: TyCtxt<'tcx>,
1141    instance: ty::Instance<'tcx>,
1142    args: &[OperandRef<'tcx, &'ll Value>],
1143    result: PlaceRef<'tcx, &'ll Value>,
1144) {
1145    if !tcx.sess.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable) {
1146        let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutEnable);
1147    }
1148
1149    let fn_args = instance.args;
1150    let callee_ty = instance.ty(tcx, bx.typing_env());
1151
1152    let sig = callee_ty.fn_sig(tcx).skip_binder();
1153
1154    let ret_ty = sig.output();
1155    let llret_ty = bx.layout_of(ret_ty).llvm_type(bx);
1156
1157    // Get source, diff, and attrs
1158    let (source_id, source_args) = match fn_args.into_type_list(tcx)[0].kind() {
1159        ty::FnDef(def_id, source_params) => (def_id, source_params),
1160        _ => bug!("invalid autodiff intrinsic args"),
1161    };
1162
1163    let fn_source = match Instance::try_resolve(tcx, bx.cx.typing_env(), *source_id, source_args) {
1164        Ok(Some(instance)) => instance,
1165        Ok(None) => bug!(
1166            "could not resolve ({:?}, {:?}) to a specific autodiff instance",
1167            source_id,
1168            source_args
1169        ),
1170        Err(_) => {
1171            // An error has already been emitted
1172            return;
1173        }
1174    };
1175
1176    let source_symbol = symbol_name_for_instance_in_crate(tcx, fn_source.clone(), LOCAL_CRATE);
1177    let Some(fn_to_diff) = bx.cx.get_function(&source_symbol) else {
1178        bug!("could not find source function")
1179    };
1180
1181    let (diff_id, diff_args) = match fn_args.into_type_list(tcx)[1].kind() {
1182        ty::FnDef(def_id, diff_args) => (def_id, diff_args),
1183        _ => bug!("invalid args"),
1184    };
1185
1186    let fn_diff = match Instance::try_resolve(tcx, bx.cx.typing_env(), *diff_id, diff_args) {
1187        Ok(Some(instance)) => instance,
1188        Ok(None) => bug!(
1189            "could not resolve ({:?}, {:?}) to a specific autodiff instance",
1190            diff_id,
1191            diff_args
1192        ),
1193        Err(_) => {
1194            // An error has already been emitted
1195            return;
1196        }
1197    };
1198
1199    let val_arr = get_args_from_tuple(bx, args[2], fn_diff);
1200    let diff_symbol = symbol_name_for_instance_in_crate(tcx, fn_diff.clone(), LOCAL_CRATE);
1201
1202    let Some(mut diff_attrs) = autodiff_attrs(tcx, fn_diff.def_id()) else {
1203        bug!("could not find autodiff attrs")
1204    };
1205
1206    adjust_activity_to_abi(
1207        tcx,
1208        fn_source,
1209        TypingEnv::fully_monomorphized(),
1210        &mut diff_attrs.input_activity,
1211    );
1212
1213    let fnc_tree =
1214        rustc_middle::ty::fnc_typetrees(tcx, fn_source.ty(tcx, TypingEnv::fully_monomorphized()));
1215
1216    // Build body
1217    generate_enzyme_call(
1218        bx,
1219        bx.cx,
1220        fn_to_diff,
1221        &diff_symbol,
1222        llret_ty,
1223        &val_arr,
1224        diff_attrs.clone(),
1225        result,
1226        fnc_tree,
1227    );
1228}
1229
1230fn get_args_from_tuple<'ll, 'tcx>(
1231    bx: &mut Builder<'_, 'll, 'tcx>,
1232    tuple_op: OperandRef<'tcx, &'ll Value>,
1233    fn_instance: Instance<'tcx>,
1234) -> Vec<&'ll Value> {
1235    let cx = bx.cx;
1236    let fn_abi = cx.fn_abi_of_instance(fn_instance, ty::List::empty());
1237
1238    match tuple_op.val {
1239        OperandValue::Immediate(val) => vec![val],
1240        OperandValue::Pair(v1, v2) => vec![v1, v2],
1241        OperandValue::Ref(ptr) => {
1242            let tuple_place = PlaceRef { val: ptr, layout: tuple_op.layout };
1243
1244            let mut result = Vec::with_capacity(fn_abi.args.len());
1245            let mut tuple_index = 0;
1246
1247            for arg in &fn_abi.args {
1248                match arg.mode {
1249                    PassMode::Ignore => {}
1250                    PassMode::Direct(_) | PassMode::Cast { .. } => {
1251                        let field = tuple_place.project_field(bx, tuple_index);
1252                        let llvm_ty = field.layout.llvm_type(bx.cx);
1253                        let val = bx.load(llvm_ty, field.val.llval, field.val.align);
1254                        result.push(val);
1255                        tuple_index += 1;
1256                    }
1257                    PassMode::Pair(_, _) => {
1258                        let field = tuple_place.project_field(bx, tuple_index);
1259                        let llvm_ty = field.layout.llvm_type(bx.cx);
1260                        let pair_val = bx.load(llvm_ty, field.val.llval, field.val.align);
1261                        result.push(bx.extract_value(pair_val, 0));
1262                        result.push(bx.extract_value(pair_val, 1));
1263                        tuple_index += 1;
1264                    }
1265                    PassMode::Indirect { .. } => {
1266                        let field = tuple_place.project_field(bx, tuple_index);
1267                        result.push(field.val.llval);
1268                        tuple_index += 1;
1269                    }
1270                }
1271            }
1272
1273            result
1274        }
1275
1276        OperandValue::ZeroSized => vec![],
1277    }
1278}
1279
1280fn generic_simd_intrinsic<'ll, 'tcx>(
1281    bx: &mut Builder<'_, 'll, 'tcx>,
1282    name: Symbol,
1283    fn_args: GenericArgsRef<'tcx>,
1284    args: &[OperandRef<'tcx, &'ll Value>],
1285    ret_ty: Ty<'tcx>,
1286    llret_ty: &'ll Type,
1287    span: Span,
1288) -> Result<&'ll Value, ()> {
1289    macro_rules! return_error {
1290        ($diag: expr) => {{
1291            bx.sess().dcx().emit_err($diag);
1292            return Err(());
1293        }};
1294    }
1295
1296    macro_rules! require {
1297        ($cond: expr, $diag: expr) => {
1298            if !$cond {
1299                return_error!($diag);
1300            }
1301        };
1302    }
1303
1304    macro_rules! require_simd {
1305        ($ty: expr, $variant:ident) => {{
1306            require!($ty.is_simd(), InvalidMonomorphization::$variant { span, name, ty: $ty });
1307            $ty.simd_size_and_type(bx.tcx())
1308        }};
1309    }
1310
1311    /// Returns the bitwidth of the `$ty` argument if it is an `Int` or `Uint` type.
1312    macro_rules! require_int_or_uint_ty {
1313        ($ty: expr, $diag: expr) => {
1314            match $ty {
1315                ty::Int(i) => {
1316                    i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
1317                }
1318                ty::Uint(i) => {
1319                    i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
1320                }
1321                _ => {
1322                    return_error!($diag);
1323                }
1324            }
1325        };
1326    }
1327
1328    /// Converts a vector mask, where each element has a bit width equal to the data elements it is used with,
1329    /// down to an i1 based mask that can be used by llvm intrinsics.
1330    ///
1331    /// The rust simd semantics are that each element should either consist of all ones or all zeroes,
1332    /// but this information is not available to llvm. Truncating the vector effectively uses the lowest bit,
1333    /// but codegen for several targets is better if we consider the highest bit by shifting.
1334    ///
1335    /// For x86 SSE/AVX targets this is beneficial since most instructions with mask parameters only consider the highest bit.
1336    /// So even though on llvm level we have an additional shift, in the final assembly there is no shift or truncate and
1337    /// instead the mask can be used as is.
1338    ///
1339    /// For aarch64 and other targets there is a benefit because a mask from the sign bit can be more
1340    /// efficiently converted to an all ones / all zeroes mask by comparing whether each element is negative.
1341    fn vector_mask_to_bitmask<'a, 'll, 'tcx>(
1342        bx: &mut Builder<'a, 'll, 'tcx>,
1343        i_xn: &'ll Value,
1344        in_elem_bitwidth: u64,
1345        in_len: u64,
1346    ) -> &'ll Value {
1347        // Shift the MSB to the right by "in_elem_bitwidth - 1" into the first bit position.
1348        let shift_idx = bx.cx.const_int(bx.type_ix(in_elem_bitwidth), (in_elem_bitwidth - 1) as _);
1349        let shift_indices = vec![shift_idx; in_len as _];
1350        let i_xn_msb = bx.lshr(i_xn, bx.const_vector(shift_indices.as_slice()));
1351        // Truncate vector to an <i1 x N>
1352        bx.trunc(i_xn_msb, bx.type_vector(bx.type_i1(), in_len))
1353    }
1354
1355    // Sanity-check: all vector arguments must be immediates.
1356    if cfg!(debug_assertions) {
1357        for arg in args {
1358            if arg.layout.ty.is_simd() {
1359                assert_matches!(arg.val, OperandValue::Immediate(_));
1360            }
1361        }
1362    }
1363
1364    if name == sym::simd_select_bitmask {
1365        let (len, _) = require_simd!(args[1].layout.ty, SimdArgument);
1366
1367        let expected_int_bits = len.max(8).next_power_of_two();
1368        let expected_bytes = len.div_ceil(8);
1369
1370        let mask_ty = args[0].layout.ty;
1371        let mask = match mask_ty.kind() {
1372            ty::Int(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
1373            ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
1374            ty::Array(elem, len)
1375                if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
1376                    && len
1377                        .try_to_target_usize(bx.tcx)
1378                        .expect("expected monomorphic const in codegen")
1379                        == expected_bytes =>
1380            {
1381                let place = PlaceRef::alloca(bx, args[0].layout);
1382                args[0].val.store(bx, place);
1383                let int_ty = bx.type_ix(expected_bytes * 8);
1384                bx.load(int_ty, place.val.llval, Align::ONE)
1385            }
1386            _ => return_error!(InvalidMonomorphization::InvalidBitmask {
1387                span,
1388                name,
1389                mask_ty,
1390                expected_int_bits,
1391                expected_bytes
1392            }),
1393        };
1394
1395        let i1 = bx.type_i1();
1396        let im = bx.type_ix(len);
1397        let i1xn = bx.type_vector(i1, len);
1398        let m_im = bx.trunc(mask, im);
1399        let m_i1s = bx.bitcast(m_im, i1xn);
1400        return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
1401    }
1402
1403    // every intrinsic below takes a SIMD vector as its first argument
1404    let (in_len, in_elem) = require_simd!(args[0].layout.ty, SimdInput);
1405    let in_ty = args[0].layout.ty;
1406
1407    let comparison = match name {
1408        sym::simd_eq => Some(BinOp::Eq),
1409        sym::simd_ne => Some(BinOp::Ne),
1410        sym::simd_lt => Some(BinOp::Lt),
1411        sym::simd_le => Some(BinOp::Le),
1412        sym::simd_gt => Some(BinOp::Gt),
1413        sym::simd_ge => Some(BinOp::Ge),
1414        _ => None,
1415    };
1416
1417    if let Some(cmp_op) = comparison {
1418        let (out_len, out_ty) = require_simd!(ret_ty, SimdReturn);
1419
1420        require!(
1421            in_len == out_len,
1422            InvalidMonomorphization::ReturnLengthInputType {
1423                span,
1424                name,
1425                in_len,
1426                in_ty,
1427                ret_ty,
1428                out_len
1429            }
1430        );
1431        require!(
1432            bx.type_kind(bx.element_type(llret_ty)) == TypeKind::Integer,
1433            InvalidMonomorphization::ReturnIntegerType { span, name, ret_ty, out_ty }
1434        );
1435
1436        return Ok(compare_simd_types(
1437            bx,
1438            args[0].immediate(),
1439            args[1].immediate(),
1440            in_elem,
1441            llret_ty,
1442            cmp_op,
1443        ));
1444    }
1445
1446    if name == sym::simd_shuffle_const_generic {
1447        let idx = fn_args[2].expect_const().to_value().valtree.unwrap_branch();
1448        let n = idx.len() as u64;
1449
1450        let (out_len, out_ty) = require_simd!(ret_ty, SimdReturn);
1451        require!(
1452            out_len == n,
1453            InvalidMonomorphization::ReturnLength { span, name, in_len: n, ret_ty, out_len }
1454        );
1455        require!(
1456            in_elem == out_ty,
1457            InvalidMonomorphization::ReturnElement { span, name, in_elem, in_ty, ret_ty, out_ty }
1458        );
1459
1460        let total_len = in_len * 2;
1461
1462        let indices: Option<Vec<_>> = idx
1463            .iter()
1464            .enumerate()
1465            .map(|(arg_idx, val)| {
1466                let idx = val.unwrap_leaf().to_i32();
1467                if idx >= i32::try_from(total_len).unwrap() {
1468                    bx.sess().dcx().emit_err(InvalidMonomorphization::SimdIndexOutOfBounds {
1469                        span,
1470                        name,
1471                        arg_idx: arg_idx as u64,
1472                        total_len: total_len.into(),
1473                    });
1474                    None
1475                } else {
1476                    Some(bx.const_i32(idx))
1477                }
1478            })
1479            .collect();
1480        let Some(indices) = indices else {
1481            return Ok(bx.const_null(llret_ty));
1482        };
1483
1484        return Ok(bx.shuffle_vector(
1485            args[0].immediate(),
1486            args[1].immediate(),
1487            bx.const_vector(&indices),
1488        ));
1489    }
1490
1491    if name == sym::simd_shuffle {
1492        // Make sure this is actually a SIMD vector.
1493        let idx_ty = args[2].layout.ty;
1494        let n: u64 = if idx_ty.is_simd()
1495            && matches!(idx_ty.simd_size_and_type(bx.cx.tcx).1.kind(), ty::Uint(ty::UintTy::U32))
1496        {
1497            idx_ty.simd_size_and_type(bx.cx.tcx).0
1498        } else {
1499            return_error!(InvalidMonomorphization::SimdShuffle { span, name, ty: idx_ty })
1500        };
1501
1502        let (out_len, out_ty) = require_simd!(ret_ty, SimdReturn);
1503        require!(
1504            out_len == n,
1505            InvalidMonomorphization::ReturnLength { span, name, in_len: n, ret_ty, out_len }
1506        );
1507        require!(
1508            in_elem == out_ty,
1509            InvalidMonomorphization::ReturnElement { span, name, in_elem, in_ty, ret_ty, out_ty }
1510        );
1511
1512        let total_len = u128::from(in_len) * 2;
1513
1514        // Check that the indices are in-bounds.
1515        let indices = args[2].immediate();
1516        for i in 0..n {
1517            let val = bx.const_get_elt(indices, i as u64);
1518            let idx = bx
1519                .const_to_opt_u128(val, true)
1520                .unwrap_or_else(|| bug!("typeck should have already ensured that these are const"));
1521            if idx >= total_len {
1522                return_error!(InvalidMonomorphization::SimdIndexOutOfBounds {
1523                    span,
1524                    name,
1525                    arg_idx: i,
1526                    total_len,
1527                });
1528            }
1529        }
1530
1531        return Ok(bx.shuffle_vector(args[0].immediate(), args[1].immediate(), indices));
1532    }
1533
1534    if name == sym::simd_insert || name == sym::simd_insert_dyn {
1535        require!(
1536            in_elem == args[2].layout.ty,
1537            InvalidMonomorphization::InsertedType {
1538                span,
1539                name,
1540                in_elem,
1541                in_ty,
1542                out_ty: args[2].layout.ty
1543            }
1544        );
1545
1546        let index_imm = if name == sym::simd_insert {
1547            let idx = bx
1548                .const_to_opt_u128(args[1].immediate(), false)
1549                .expect("typeck should have ensure that this is a const");
1550            if idx >= in_len.into() {
1551                return_error!(InvalidMonomorphization::SimdIndexOutOfBounds {
1552                    span,
1553                    name,
1554                    arg_idx: 1,
1555                    total_len: in_len.into(),
1556                });
1557            }
1558            bx.const_i32(idx as i32)
1559        } else {
1560            args[1].immediate()
1561        };
1562
1563        return Ok(bx.insert_element(args[0].immediate(), args[2].immediate(), index_imm));
1564    }
1565    if name == sym::simd_extract || name == sym::simd_extract_dyn {
1566        require!(
1567            ret_ty == in_elem,
1568            InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
1569        );
1570        let index_imm = if name == sym::simd_extract {
1571            let idx = bx
1572                .const_to_opt_u128(args[1].immediate(), false)
1573                .expect("typeck should have ensure that this is a const");
1574            if idx >= in_len.into() {
1575                return_error!(InvalidMonomorphization::SimdIndexOutOfBounds {
1576                    span,
1577                    name,
1578                    arg_idx: 1,
1579                    total_len: in_len.into(),
1580                });
1581            }
1582            bx.const_i32(idx as i32)
1583        } else {
1584            args[1].immediate()
1585        };
1586
1587        return Ok(bx.extract_element(args[0].immediate(), index_imm));
1588    }
1589
1590    if name == sym::simd_select {
1591        let m_elem_ty = in_elem;
1592        let m_len = in_len;
1593        let (v_len, _) = require_simd!(args[1].layout.ty, SimdArgument);
1594        require!(
1595            m_len == v_len,
1596            InvalidMonomorphization::MismatchedLengths { span, name, m_len, v_len }
1597        );
1598        let in_elem_bitwidth = require_int_or_uint_ty!(
1599            m_elem_ty.kind(),
1600            InvalidMonomorphization::MaskWrongElementType { span, name, ty: m_elem_ty }
1601        );
1602        let m_i1s = vector_mask_to_bitmask(bx, args[0].immediate(), in_elem_bitwidth, m_len);
1603        return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
1604    }
1605
1606    if name == sym::simd_bitmask {
1607        // The `fn simd_bitmask(vector) -> unsigned integer` intrinsic takes a vector mask and
1608        // returns one bit for each lane (which must all be `0` or `!0`) in the form of either:
1609        // * an unsigned integer
1610        // * an array of `u8`
1611        // If the vector has less than 8 lanes, a u8 is returned with zeroed trailing bits.
1612        //
1613        // The bit order of the result depends on the byte endianness, LSB-first for little
1614        // endian and MSB-first for big endian.
1615        let expected_int_bits = in_len.max(8).next_power_of_two();
1616        let expected_bytes = in_len.div_ceil(8);
1617
1618        // Integer vector <i{in_bitwidth} x in_len>:
1619        let in_elem_bitwidth = require_int_or_uint_ty!(
1620            in_elem.kind(),
1621            InvalidMonomorphization::MaskWrongElementType { span, name, ty: in_elem }
1622        );
1623
1624        let i1xn = vector_mask_to_bitmask(bx, args[0].immediate(), in_elem_bitwidth, in_len);
1625        // Bitcast <i1 x N> to iN:
1626        let i_ = bx.bitcast(i1xn, bx.type_ix(in_len));
1627
1628        match ret_ty.kind() {
1629            ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => {
1630                // Zero-extend iN to the bitmask type:
1631                return Ok(bx.zext(i_, bx.type_ix(expected_int_bits)));
1632            }
1633            ty::Array(elem, len)
1634                if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
1635                    && len
1636                        .try_to_target_usize(bx.tcx)
1637                        .expect("expected monomorphic const in codegen")
1638                        == expected_bytes =>
1639            {
1640                // Zero-extend iN to the array length:
1641                let ze = bx.zext(i_, bx.type_ix(expected_bytes * 8));
1642
1643                // Convert the integer to a byte array
1644                let ptr = bx.alloca(Size::from_bytes(expected_bytes), Align::ONE);
1645                bx.store(ze, ptr, Align::ONE);
1646                let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
1647                return Ok(bx.load(array_ty, ptr, Align::ONE));
1648            }
1649            _ => return_error!(InvalidMonomorphization::CannotReturn {
1650                span,
1651                name,
1652                ret_ty,
1653                expected_int_bits,
1654                expected_bytes
1655            }),
1656        }
1657    }
1658
1659    fn simd_simple_float_intrinsic<'ll, 'tcx>(
1660        name: Symbol,
1661        in_elem: Ty<'_>,
1662        in_ty: Ty<'_>,
1663        in_len: u64,
1664        bx: &mut Builder<'_, 'll, 'tcx>,
1665        span: Span,
1666        args: &[OperandRef<'tcx, &'ll Value>],
1667    ) -> Result<&'ll Value, ()> {
1668        macro_rules! return_error {
1669            ($diag: expr) => {{
1670                bx.sess().dcx().emit_err($diag);
1671                return Err(());
1672            }};
1673        }
1674
1675        let elem_ty = if let ty::Float(f) = in_elem.kind() {
1676            bx.cx.type_float_from_ty(*f)
1677        } else {
1678            return_error!(InvalidMonomorphization::FloatingPointType { span, name, in_ty });
1679        };
1680
1681        let vec_ty = bx.type_vector(elem_ty, in_len);
1682
1683        let intr_name = match name {
1684            sym::simd_ceil => "llvm.ceil",
1685            sym::simd_fabs => "llvm.fabs",
1686            sym::simd_fcos => "llvm.cos",
1687            sym::simd_fexp2 => "llvm.exp2",
1688            sym::simd_fexp => "llvm.exp",
1689            sym::simd_flog10 => "llvm.log10",
1690            sym::simd_flog2 => "llvm.log2",
1691            sym::simd_flog => "llvm.log",
1692            sym::simd_floor => "llvm.floor",
1693            sym::simd_fma => "llvm.fma",
1694            sym::simd_relaxed_fma => "llvm.fmuladd",
1695            sym::simd_fsin => "llvm.sin",
1696            sym::simd_fsqrt => "llvm.sqrt",
1697            sym::simd_round => "llvm.round",
1698            sym::simd_round_ties_even => "llvm.rint",
1699            sym::simd_trunc => "llvm.trunc",
1700            _ => return_error!(InvalidMonomorphization::UnrecognizedIntrinsic { span, name }),
1701        };
1702        Ok(bx.call_intrinsic(
1703            intr_name,
1704            &[vec_ty],
1705            &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
1706        ))
1707    }
1708
1709    if std::matches!(
1710        name,
1711        sym::simd_ceil
1712            | sym::simd_fabs
1713            | sym::simd_fcos
1714            | sym::simd_fexp2
1715            | sym::simd_fexp
1716            | sym::simd_flog10
1717            | sym::simd_flog2
1718            | sym::simd_flog
1719            | sym::simd_floor
1720            | sym::simd_fma
1721            | sym::simd_fsin
1722            | sym::simd_fsqrt
1723            | sym::simd_relaxed_fma
1724            | sym::simd_round
1725            | sym::simd_round_ties_even
1726            | sym::simd_trunc
1727    ) {
1728        return simd_simple_float_intrinsic(name, in_elem, in_ty, in_len, bx, span, args);
1729    }
1730
1731    fn llvm_vector_ty<'ll>(cx: &CodegenCx<'ll, '_>, elem_ty: Ty<'_>, vec_len: u64) -> &'ll Type {
1732        let elem_ty = match *elem_ty.kind() {
1733            ty::Int(v) => cx.type_int_from_ty(v),
1734            ty::Uint(v) => cx.type_uint_from_ty(v),
1735            ty::Float(v) => cx.type_float_from_ty(v),
1736            ty::RawPtr(_, _) => cx.type_ptr(),
1737            _ => unreachable!(),
1738        };
1739        cx.type_vector(elem_ty, vec_len)
1740    }
1741
1742    if name == sym::simd_gather {
1743        // simd_gather(values: <N x T>, pointers: <N x *_ T>,
1744        //             mask: <N x i{M}>) -> <N x T>
1745        // * N: number of elements in the input vectors
1746        // * T: type of the element to load
1747        // * M: any integer width is supported, will be truncated to i1
1748
1749        // All types must be simd vector types
1750
1751        // The second argument must be a simd vector with an element type that's a pointer
1752        // to the element type of the first argument
1753        let (_, element_ty0) = require_simd!(in_ty, SimdFirst);
1754        let (out_len, element_ty1) = require_simd!(args[1].layout.ty, SimdSecond);
1755        // The element type of the third argument must be a signed integer type of any width:
1756        let (out_len2, element_ty2) = require_simd!(args[2].layout.ty, SimdThird);
1757        require_simd!(ret_ty, SimdReturn);
1758
1759        // Of the same length:
1760        require!(
1761            in_len == out_len,
1762            InvalidMonomorphization::SecondArgumentLength {
1763                span,
1764                name,
1765                in_len,
1766                in_ty,
1767                arg_ty: args[1].layout.ty,
1768                out_len
1769            }
1770        );
1771        require!(
1772            in_len == out_len2,
1773            InvalidMonomorphization::ThirdArgumentLength {
1774                span,
1775                name,
1776                in_len,
1777                in_ty,
1778                arg_ty: args[2].layout.ty,
1779                out_len: out_len2
1780            }
1781        );
1782
1783        // The return type must match the first argument type
1784        require!(
1785            ret_ty == in_ty,
1786            InvalidMonomorphization::ExpectedReturnType { span, name, in_ty, ret_ty }
1787        );
1788
1789        require!(
1790            matches!(
1791                *element_ty1.kind(),
1792                ty::RawPtr(p_ty, _) if p_ty == in_elem && p_ty.kind() == element_ty0.kind()
1793            ),
1794            InvalidMonomorphization::ExpectedElementType {
1795                span,
1796                name,
1797                expected_element: element_ty1,
1798                second_arg: args[1].layout.ty,
1799                in_elem,
1800                in_ty,
1801                mutability: ExpectedPointerMutability::Not,
1802            }
1803        );
1804
1805        let mask_elem_bitwidth = require_int_or_uint_ty!(
1806            element_ty2.kind(),
1807            InvalidMonomorphization::MaskWrongElementType { span, name, ty: element_ty2 }
1808        );
1809
1810        // Alignment of T, must be a constant integer value:
1811        let alignment = bx.const_i32(bx.align_of(in_elem).bytes() as i32);
1812
1813        // Truncate the mask vector to a vector of i1s:
1814        let mask = vector_mask_to_bitmask(bx, args[2].immediate(), mask_elem_bitwidth, in_len);
1815
1816        // Type of the vector of pointers:
1817        let llvm_pointer_vec_ty = llvm_vector_ty(bx, element_ty1, in_len);
1818
1819        // Type of the vector of elements:
1820        let llvm_elem_vec_ty = llvm_vector_ty(bx, element_ty0, in_len);
1821
1822        return Ok(bx.call_intrinsic(
1823            "llvm.masked.gather",
1824            &[llvm_elem_vec_ty, llvm_pointer_vec_ty],
1825            &[args[1].immediate(), alignment, mask, args[0].immediate()],
1826        ));
1827    }
1828
1829    if name == sym::simd_masked_load {
1830        // simd_masked_load(mask: <N x i{M}>, pointer: *_ T, values: <N x T>) -> <N x T>
1831        // * N: number of elements in the input vectors
1832        // * T: type of the element to load
1833        // * M: any integer width is supported, will be truncated to i1
1834        // Loads contiguous elements from memory behind `pointer`, but only for
1835        // those lanes whose `mask` bit is enabled.
1836        // The memory addresses corresponding to the “off” lanes are not accessed.
1837
1838        // The element type of the "mask" argument must be a signed integer type of any width
1839        let mask_ty = in_ty;
1840        let (mask_len, mask_elem) = (in_len, in_elem);
1841
1842        // The second argument must be a pointer matching the element type
1843        let pointer_ty = args[1].layout.ty;
1844
1845        // The last argument is a passthrough vector providing values for disabled lanes
1846        let values_ty = args[2].layout.ty;
1847        let (values_len, values_elem) = require_simd!(values_ty, SimdThird);
1848
1849        require_simd!(ret_ty, SimdReturn);
1850
1851        // Of the same length:
1852        require!(
1853            values_len == mask_len,
1854            InvalidMonomorphization::ThirdArgumentLength {
1855                span,
1856                name,
1857                in_len: mask_len,
1858                in_ty: mask_ty,
1859                arg_ty: values_ty,
1860                out_len: values_len
1861            }
1862        );
1863
1864        // The return type must match the last argument type
1865        require!(
1866            ret_ty == values_ty,
1867            InvalidMonomorphization::ExpectedReturnType { span, name, in_ty: values_ty, ret_ty }
1868        );
1869
1870        require!(
1871            matches!(
1872                *pointer_ty.kind(),
1873                ty::RawPtr(p_ty, _) if p_ty == values_elem && p_ty.kind() == values_elem.kind()
1874            ),
1875            InvalidMonomorphization::ExpectedElementType {
1876                span,
1877                name,
1878                expected_element: values_elem,
1879                second_arg: pointer_ty,
1880                in_elem: values_elem,
1881                in_ty: values_ty,
1882                mutability: ExpectedPointerMutability::Not,
1883            }
1884        );
1885
1886        let m_elem_bitwidth = require_int_or_uint_ty!(
1887            mask_elem.kind(),
1888            InvalidMonomorphization::MaskWrongElementType { span, name, ty: mask_elem }
1889        );
1890
1891        let mask = vector_mask_to_bitmask(bx, args[0].immediate(), m_elem_bitwidth, mask_len);
1892
1893        // Alignment of T, must be a constant integer value:
1894        let alignment = bx.const_i32(bx.align_of(values_elem).bytes() as i32);
1895
1896        let llvm_pointer = bx.type_ptr();
1897
1898        // Type of the vector of elements:
1899        let llvm_elem_vec_ty = llvm_vector_ty(bx, values_elem, values_len);
1900
1901        return Ok(bx.call_intrinsic(
1902            "llvm.masked.load",
1903            &[llvm_elem_vec_ty, llvm_pointer],
1904            &[args[1].immediate(), alignment, mask, args[2].immediate()],
1905        ));
1906    }
1907
1908    if name == sym::simd_masked_store {
1909        // simd_masked_store(mask: <N x i{M}>, pointer: *mut T, values: <N x T>) -> ()
1910        // * N: number of elements in the input vectors
1911        // * T: type of the element to load
1912        // * M: any integer width is supported, will be truncated to i1
1913        // Stores contiguous elements to memory behind `pointer`, but only for
1914        // those lanes whose `mask` bit is enabled.
1915        // The memory addresses corresponding to the “off” lanes are not accessed.
1916
1917        // The element type of the "mask" argument must be a signed integer type of any width
1918        let mask_ty = in_ty;
1919        let (mask_len, mask_elem) = (in_len, in_elem);
1920
1921        // The second argument must be a pointer matching the element type
1922        let pointer_ty = args[1].layout.ty;
1923
1924        // The last argument specifies the values to store to memory
1925        let values_ty = args[2].layout.ty;
1926        let (values_len, values_elem) = require_simd!(values_ty, SimdThird);
1927
1928        // Of the same length:
1929        require!(
1930            values_len == mask_len,
1931            InvalidMonomorphization::ThirdArgumentLength {
1932                span,
1933                name,
1934                in_len: mask_len,
1935                in_ty: mask_ty,
1936                arg_ty: values_ty,
1937                out_len: values_len
1938            }
1939        );
1940
1941        // The second argument must be a mutable pointer type matching the element type
1942        require!(
1943            matches!(
1944                *pointer_ty.kind(),
1945                ty::RawPtr(p_ty, p_mutbl)
1946                    if p_ty == values_elem && p_ty.kind() == values_elem.kind() && p_mutbl.is_mut()
1947            ),
1948            InvalidMonomorphization::ExpectedElementType {
1949                span,
1950                name,
1951                expected_element: values_elem,
1952                second_arg: pointer_ty,
1953                in_elem: values_elem,
1954                in_ty: values_ty,
1955                mutability: ExpectedPointerMutability::Mut,
1956            }
1957        );
1958
1959        let m_elem_bitwidth = require_int_or_uint_ty!(
1960            mask_elem.kind(),
1961            InvalidMonomorphization::MaskWrongElementType { span, name, ty: mask_elem }
1962        );
1963
1964        let mask = vector_mask_to_bitmask(bx, args[0].immediate(), m_elem_bitwidth, mask_len);
1965
1966        // Alignment of T, must be a constant integer value:
1967        let alignment = bx.const_i32(bx.align_of(values_elem).bytes() as i32);
1968
1969        let llvm_pointer = bx.type_ptr();
1970
1971        // Type of the vector of elements:
1972        let llvm_elem_vec_ty = llvm_vector_ty(bx, values_elem, values_len);
1973
1974        return Ok(bx.call_intrinsic(
1975            "llvm.masked.store",
1976            &[llvm_elem_vec_ty, llvm_pointer],
1977            &[args[2].immediate(), args[1].immediate(), alignment, mask],
1978        ));
1979    }
1980
1981    if name == sym::simd_scatter {
1982        // simd_scatter(values: <N x T>, pointers: <N x *mut T>,
1983        //             mask: <N x i{M}>) -> ()
1984        // * N: number of elements in the input vectors
1985        // * T: type of the element to load
1986        // * M: any integer width is supported, will be truncated to i1
1987
1988        // All types must be simd vector types
1989        // The second argument must be a simd vector with an element type that's a pointer
1990        // to the element type of the first argument
1991        let (_, element_ty0) = require_simd!(in_ty, SimdFirst);
1992        let (element_len1, element_ty1) = require_simd!(args[1].layout.ty, SimdSecond);
1993        let (element_len2, element_ty2) = require_simd!(args[2].layout.ty, SimdThird);
1994
1995        // Of the same length:
1996        require!(
1997            in_len == element_len1,
1998            InvalidMonomorphization::SecondArgumentLength {
1999                span,
2000                name,
2001                in_len,
2002                in_ty,
2003                arg_ty: args[1].layout.ty,
2004                out_len: element_len1
2005            }
2006        );
2007        require!(
2008            in_len == element_len2,
2009            InvalidMonomorphization::ThirdArgumentLength {
2010                span,
2011                name,
2012                in_len,
2013                in_ty,
2014                arg_ty: args[2].layout.ty,
2015                out_len: element_len2
2016            }
2017        );
2018
2019        require!(
2020            matches!(
2021                *element_ty1.kind(),
2022                ty::RawPtr(p_ty, p_mutbl)
2023                    if p_ty == in_elem && p_mutbl.is_mut() && p_ty.kind() == element_ty0.kind()
2024            ),
2025            InvalidMonomorphization::ExpectedElementType {
2026                span,
2027                name,
2028                expected_element: element_ty1,
2029                second_arg: args[1].layout.ty,
2030                in_elem,
2031                in_ty,
2032                mutability: ExpectedPointerMutability::Mut,
2033            }
2034        );
2035
2036        // The element type of the third argument must be an integer type of any width:
2037        let mask_elem_bitwidth = require_int_or_uint_ty!(
2038            element_ty2.kind(),
2039            InvalidMonomorphization::MaskWrongElementType { span, name, ty: element_ty2 }
2040        );
2041
2042        // Alignment of T, must be a constant integer value:
2043        let alignment = bx.const_i32(bx.align_of(in_elem).bytes() as i32);
2044
2045        // Truncate the mask vector to a vector of i1s:
2046        let mask = vector_mask_to_bitmask(bx, args[2].immediate(), mask_elem_bitwidth, in_len);
2047
2048        // Type of the vector of pointers:
2049        let llvm_pointer_vec_ty = llvm_vector_ty(bx, element_ty1, in_len);
2050
2051        // Type of the vector of elements:
2052        let llvm_elem_vec_ty = llvm_vector_ty(bx, element_ty0, in_len);
2053
2054        return Ok(bx.call_intrinsic(
2055            "llvm.masked.scatter",
2056            &[llvm_elem_vec_ty, llvm_pointer_vec_ty],
2057            &[args[0].immediate(), args[1].immediate(), alignment, mask],
2058        ));
2059    }
2060
2061    macro_rules! arith_red {
2062        ($name:ident : $integer_reduce:ident, $float_reduce:ident, $ordered:expr, $op:ident,
2063         $identity:expr) => {
2064            if name == sym::$name {
2065                require!(
2066                    ret_ty == in_elem,
2067                    InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
2068                );
2069                return match in_elem.kind() {
2070                    ty::Int(_) | ty::Uint(_) => {
2071                        let r = bx.$integer_reduce(args[0].immediate());
2072                        if $ordered {
2073                            // if overflow occurs, the result is the
2074                            // mathematical result modulo 2^n:
2075                            Ok(bx.$op(args[1].immediate(), r))
2076                        } else {
2077                            Ok(bx.$integer_reduce(args[0].immediate()))
2078                        }
2079                    }
2080                    ty::Float(f) => {
2081                        let acc = if $ordered {
2082                            // ordered arithmetic reductions take an accumulator
2083                            args[1].immediate()
2084                        } else {
2085                            // unordered arithmetic reductions use the identity accumulator
2086                            match f.bit_width() {
2087                                32 => bx.const_real(bx.type_f32(), $identity),
2088                                64 => bx.const_real(bx.type_f64(), $identity),
2089                                v => return_error!(
2090                                    InvalidMonomorphization::UnsupportedSymbolOfSize {
2091                                        span,
2092                                        name,
2093                                        symbol: sym::$name,
2094                                        in_ty,
2095                                        in_elem,
2096                                        size: v,
2097                                        ret_ty
2098                                    }
2099                                ),
2100                            }
2101                        };
2102                        Ok(bx.$float_reduce(acc, args[0].immediate()))
2103                    }
2104                    _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
2105                        span,
2106                        name,
2107                        symbol: sym::$name,
2108                        in_ty,
2109                        in_elem,
2110                        ret_ty
2111                    }),
2112                };
2113            }
2114        };
2115    }
2116
2117    arith_red!(simd_reduce_add_ordered: vector_reduce_add, vector_reduce_fadd, true, add, -0.0);
2118    arith_red!(simd_reduce_mul_ordered: vector_reduce_mul, vector_reduce_fmul, true, mul, 1.0);
2119    arith_red!(
2120        simd_reduce_add_unordered: vector_reduce_add,
2121        vector_reduce_fadd_reassoc,
2122        false,
2123        add,
2124        -0.0
2125    );
2126    arith_red!(
2127        simd_reduce_mul_unordered: vector_reduce_mul,
2128        vector_reduce_fmul_reassoc,
2129        false,
2130        mul,
2131        1.0
2132    );
2133
2134    macro_rules! minmax_red {
2135        ($name:ident: $int_red:ident, $float_red:ident) => {
2136            if name == sym::$name {
2137                require!(
2138                    ret_ty == in_elem,
2139                    InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
2140                );
2141                return match in_elem.kind() {
2142                    ty::Int(_i) => Ok(bx.$int_red(args[0].immediate(), true)),
2143                    ty::Uint(_u) => Ok(bx.$int_red(args[0].immediate(), false)),
2144                    ty::Float(_f) => Ok(bx.$float_red(args[0].immediate())),
2145                    _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
2146                        span,
2147                        name,
2148                        symbol: sym::$name,
2149                        in_ty,
2150                        in_elem,
2151                        ret_ty
2152                    }),
2153                };
2154            }
2155        };
2156    }
2157
2158    minmax_red!(simd_reduce_min: vector_reduce_min, vector_reduce_fmin);
2159    minmax_red!(simd_reduce_max: vector_reduce_max, vector_reduce_fmax);
2160
2161    macro_rules! bitwise_red {
2162        ($name:ident : $red:ident, $boolean:expr) => {
2163            if name == sym::$name {
2164                let input = if !$boolean {
2165                    require!(
2166                        ret_ty == in_elem,
2167                        InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
2168                    );
2169                    args[0].immediate()
2170                } else {
2171                    let bitwidth = match in_elem.kind() {
2172                        ty::Int(i) => {
2173                            i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
2174                        }
2175                        ty::Uint(i) => {
2176                            i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
2177                        }
2178                        _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
2179                            span,
2180                            name,
2181                            symbol: sym::$name,
2182                            in_ty,
2183                            in_elem,
2184                            ret_ty
2185                        }),
2186                    };
2187
2188                    vector_mask_to_bitmask(bx, args[0].immediate(), bitwidth, in_len as _)
2189                };
2190                return match in_elem.kind() {
2191                    ty::Int(_) | ty::Uint(_) => {
2192                        let r = bx.$red(input);
2193                        Ok(if !$boolean { r } else { bx.zext(r, bx.type_bool()) })
2194                    }
2195                    _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
2196                        span,
2197                        name,
2198                        symbol: sym::$name,
2199                        in_ty,
2200                        in_elem,
2201                        ret_ty
2202                    }),
2203                };
2204            }
2205        };
2206    }
2207
2208    bitwise_red!(simd_reduce_and: vector_reduce_and, false);
2209    bitwise_red!(simd_reduce_or: vector_reduce_or, false);
2210    bitwise_red!(simd_reduce_xor: vector_reduce_xor, false);
2211    bitwise_red!(simd_reduce_all: vector_reduce_and, true);
2212    bitwise_red!(simd_reduce_any: vector_reduce_or, true);
2213
2214    if name == sym::simd_cast_ptr {
2215        let (out_len, out_elem) = require_simd!(ret_ty, SimdReturn);
2216        require!(
2217            in_len == out_len,
2218            InvalidMonomorphization::ReturnLengthInputType {
2219                span,
2220                name,
2221                in_len,
2222                in_ty,
2223                ret_ty,
2224                out_len
2225            }
2226        );
2227
2228        match in_elem.kind() {
2229            ty::RawPtr(p_ty, _) => {
2230                let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| {
2231                    bx.tcx.normalize_erasing_regions(bx.typing_env(), ty)
2232                });
2233                require!(
2234                    metadata.is_unit(),
2235                    InvalidMonomorphization::CastWidePointer { span, name, ty: in_elem }
2236                );
2237            }
2238            _ => {
2239                return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: in_elem })
2240            }
2241        }
2242        match out_elem.kind() {
2243            ty::RawPtr(p_ty, _) => {
2244                let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| {
2245                    bx.tcx.normalize_erasing_regions(bx.typing_env(), ty)
2246                });
2247                require!(
2248                    metadata.is_unit(),
2249                    InvalidMonomorphization::CastWidePointer { span, name, ty: out_elem }
2250                );
2251            }
2252            _ => {
2253                return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: out_elem })
2254            }
2255        }
2256
2257        return Ok(args[0].immediate());
2258    }
2259
2260    if name == sym::simd_expose_provenance {
2261        let (out_len, out_elem) = require_simd!(ret_ty, SimdReturn);
2262        require!(
2263            in_len == out_len,
2264            InvalidMonomorphization::ReturnLengthInputType {
2265                span,
2266                name,
2267                in_len,
2268                in_ty,
2269                ret_ty,
2270                out_len
2271            }
2272        );
2273
2274        match in_elem.kind() {
2275            ty::RawPtr(_, _) => {}
2276            _ => {
2277                return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: in_elem })
2278            }
2279        }
2280        match out_elem.kind() {
2281            ty::Uint(ty::UintTy::Usize) => {}
2282            _ => return_error!(InvalidMonomorphization::ExpectedUsize { span, name, ty: out_elem }),
2283        }
2284
2285        return Ok(bx.ptrtoint(args[0].immediate(), llret_ty));
2286    }
2287
2288    if name == sym::simd_with_exposed_provenance {
2289        let (out_len, out_elem) = require_simd!(ret_ty, SimdReturn);
2290        require!(
2291            in_len == out_len,
2292            InvalidMonomorphization::ReturnLengthInputType {
2293                span,
2294                name,
2295                in_len,
2296                in_ty,
2297                ret_ty,
2298                out_len
2299            }
2300        );
2301
2302        match in_elem.kind() {
2303            ty::Uint(ty::UintTy::Usize) => {}
2304            _ => return_error!(InvalidMonomorphization::ExpectedUsize { span, name, ty: in_elem }),
2305        }
2306        match out_elem.kind() {
2307            ty::RawPtr(_, _) => {}
2308            _ => {
2309                return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: out_elem })
2310            }
2311        }
2312
2313        return Ok(bx.inttoptr(args[0].immediate(), llret_ty));
2314    }
2315
2316    if name == sym::simd_cast || name == sym::simd_as {
2317        let (out_len, out_elem) = require_simd!(ret_ty, SimdReturn);
2318        require!(
2319            in_len == out_len,
2320            InvalidMonomorphization::ReturnLengthInputType {
2321                span,
2322                name,
2323                in_len,
2324                in_ty,
2325                ret_ty,
2326                out_len
2327            }
2328        );
2329        // casting cares about nominal type, not just structural type
2330        if in_elem == out_elem {
2331            return Ok(args[0].immediate());
2332        }
2333
2334        #[derive(Copy, Clone)]
2335        enum Sign {
2336            Unsigned,
2337            Signed,
2338        }
2339        use Sign::*;
2340
2341        enum Style {
2342            Float,
2343            Int(Sign),
2344            Unsupported,
2345        }
2346
2347        let (in_style, in_width) = match in_elem.kind() {
2348            // vectors of pointer-sized integers should've been
2349            // disallowed before here, so this unwrap is safe.
2350            ty::Int(i) => (
2351                Style::Int(Signed),
2352                i.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
2353            ),
2354            ty::Uint(u) => (
2355                Style::Int(Unsigned),
2356                u.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
2357            ),
2358            ty::Float(f) => (Style::Float, f.bit_width()),
2359            _ => (Style::Unsupported, 0),
2360        };
2361        let (out_style, out_width) = match out_elem.kind() {
2362            ty::Int(i) => (
2363                Style::Int(Signed),
2364                i.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
2365            ),
2366            ty::Uint(u) => (
2367                Style::Int(Unsigned),
2368                u.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
2369            ),
2370            ty::Float(f) => (Style::Float, f.bit_width()),
2371            _ => (Style::Unsupported, 0),
2372        };
2373
2374        match (in_style, out_style) {
2375            (Style::Int(sign), Style::Int(_)) => {
2376                return Ok(match in_width.cmp(&out_width) {
2377                    Ordering::Greater => bx.trunc(args[0].immediate(), llret_ty),
2378                    Ordering::Equal => args[0].immediate(),
2379                    Ordering::Less => match sign {
2380                        Sign::Signed => bx.sext(args[0].immediate(), llret_ty),
2381                        Sign::Unsigned => bx.zext(args[0].immediate(), llret_ty),
2382                    },
2383                });
2384            }
2385            (Style::Int(Sign::Signed), Style::Float) => {
2386                return Ok(bx.sitofp(args[0].immediate(), llret_ty));
2387            }
2388            (Style::Int(Sign::Unsigned), Style::Float) => {
2389                return Ok(bx.uitofp(args[0].immediate(), llret_ty));
2390            }
2391            (Style::Float, Style::Int(sign)) => {
2392                return Ok(match (sign, name == sym::simd_as) {
2393                    (Sign::Unsigned, false) => bx.fptoui(args[0].immediate(), llret_ty),
2394                    (Sign::Signed, false) => bx.fptosi(args[0].immediate(), llret_ty),
2395                    (_, true) => bx.cast_float_to_int(
2396                        matches!(sign, Sign::Signed),
2397                        args[0].immediate(),
2398                        llret_ty,
2399                    ),
2400                });
2401            }
2402            (Style::Float, Style::Float) => {
2403                return Ok(match in_width.cmp(&out_width) {
2404                    Ordering::Greater => bx.fptrunc(args[0].immediate(), llret_ty),
2405                    Ordering::Equal => args[0].immediate(),
2406                    Ordering::Less => bx.fpext(args[0].immediate(), llret_ty),
2407                });
2408            }
2409            _ => { /* Unsupported. Fallthrough. */ }
2410        }
2411        return_error!(InvalidMonomorphization::UnsupportedCast {
2412            span,
2413            name,
2414            in_ty,
2415            in_elem,
2416            ret_ty,
2417            out_elem
2418        });
2419    }
2420    macro_rules! arith_binary {
2421        ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
2422            $(if name == sym::$name {
2423                match in_elem.kind() {
2424                    $($(ty::$p(_))|* => {
2425                        return Ok(bx.$call(args[0].immediate(), args[1].immediate()))
2426                    })*
2427                    _ => {},
2428                }
2429                return_error!(
2430                    InvalidMonomorphization::UnsupportedOperation { span, name, in_ty, in_elem }
2431                );
2432            })*
2433        }
2434    }
2435    arith_binary! {
2436        simd_add: Uint, Int => add, Float => fadd;
2437        simd_sub: Uint, Int => sub, Float => fsub;
2438        simd_mul: Uint, Int => mul, Float => fmul;
2439        simd_div: Uint => udiv, Int => sdiv, Float => fdiv;
2440        simd_rem: Uint => urem, Int => srem, Float => frem;
2441        simd_shl: Uint, Int => shl;
2442        simd_shr: Uint => lshr, Int => ashr;
2443        simd_and: Uint, Int => and;
2444        simd_or: Uint, Int => or;
2445        simd_xor: Uint, Int => xor;
2446        simd_fmax: Float => maxnum;
2447        simd_fmin: Float => minnum;
2448
2449    }
2450    macro_rules! arith_unary {
2451        ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
2452            $(if name == sym::$name {
2453                match in_elem.kind() {
2454                    $($(ty::$p(_))|* => {
2455                        return Ok(bx.$call(args[0].immediate()))
2456                    })*
2457                    _ => {},
2458                }
2459                return_error!(
2460                    InvalidMonomorphization::UnsupportedOperation { span, name, in_ty, in_elem }
2461                );
2462            })*
2463        }
2464    }
2465    arith_unary! {
2466        simd_neg: Int => neg, Float => fneg;
2467    }
2468
2469    // Unary integer intrinsics
2470    if matches!(
2471        name,
2472        sym::simd_bswap
2473            | sym::simd_bitreverse
2474            | sym::simd_ctlz
2475            | sym::simd_ctpop
2476            | sym::simd_cttz
2477            | sym::simd_funnel_shl
2478            | sym::simd_funnel_shr
2479    ) {
2480        let vec_ty = bx.cx.type_vector(
2481            match *in_elem.kind() {
2482                ty::Int(i) => bx.cx.type_int_from_ty(i),
2483                ty::Uint(i) => bx.cx.type_uint_from_ty(i),
2484                _ => return_error!(InvalidMonomorphization::UnsupportedOperation {
2485                    span,
2486                    name,
2487                    in_ty,
2488                    in_elem
2489                }),
2490            },
2491            in_len as u64,
2492        );
2493        let llvm_intrinsic = match name {
2494            sym::simd_bswap => "llvm.bswap",
2495            sym::simd_bitreverse => "llvm.bitreverse",
2496            sym::simd_ctlz => "llvm.ctlz",
2497            sym::simd_ctpop => "llvm.ctpop",
2498            sym::simd_cttz => "llvm.cttz",
2499            sym::simd_funnel_shl => "llvm.fshl",
2500            sym::simd_funnel_shr => "llvm.fshr",
2501            _ => unreachable!(),
2502        };
2503        let int_size = in_elem.int_size_and_signed(bx.tcx()).0.bits();
2504
2505        return match name {
2506            // byte swap is no-op for i8/u8
2507            sym::simd_bswap if int_size == 8 => Ok(args[0].immediate()),
2508            sym::simd_ctlz | sym::simd_cttz => {
2509                // for the (int, i1 immediate) pair, the second arg adds `(0, true) => poison`
2510                let dont_poison_on_zero = bx.const_int(bx.type_i1(), 0);
2511                Ok(bx.call_intrinsic(
2512                    llvm_intrinsic,
2513                    &[vec_ty],
2514                    &[args[0].immediate(), dont_poison_on_zero],
2515                ))
2516            }
2517            sym::simd_bswap | sym::simd_bitreverse | sym::simd_ctpop => {
2518                // simple unary argument cases
2519                Ok(bx.call_intrinsic(llvm_intrinsic, &[vec_ty], &[args[0].immediate()]))
2520            }
2521            sym::simd_funnel_shl | sym::simd_funnel_shr => Ok(bx.call_intrinsic(
2522                llvm_intrinsic,
2523                &[vec_ty],
2524                &[args[0].immediate(), args[1].immediate(), args[2].immediate()],
2525            )),
2526            _ => unreachable!(),
2527        };
2528    }
2529
2530    if name == sym::simd_arith_offset {
2531        // This also checks that the first operand is a ptr type.
2532        let pointee = in_elem.builtin_deref(true).unwrap_or_else(|| {
2533            span_bug!(span, "must be called with a vector of pointer types as first argument")
2534        });
2535        let layout = bx.layout_of(pointee);
2536        let ptrs = args[0].immediate();
2537        // The second argument must be a ptr-sized integer.
2538        // (We don't care about the signedness, this is wrapping anyway.)
2539        let (_offsets_len, offsets_elem) = args[1].layout.ty.simd_size_and_type(bx.tcx());
2540        if !matches!(offsets_elem.kind(), ty::Int(ty::IntTy::Isize) | ty::Uint(ty::UintTy::Usize)) {
2541            span_bug!(
2542                span,
2543                "must be called with a vector of pointer-sized integers as second argument"
2544            );
2545        }
2546        let offsets = args[1].immediate();
2547
2548        return Ok(bx.gep(bx.backend_type(layout), ptrs, &[offsets]));
2549    }
2550
2551    if name == sym::simd_saturating_add || name == sym::simd_saturating_sub {
2552        let lhs = args[0].immediate();
2553        let rhs = args[1].immediate();
2554        let is_add = name == sym::simd_saturating_add;
2555        let (signed, elem_ty) = match *in_elem.kind() {
2556            ty::Int(i) => (true, bx.cx.type_int_from_ty(i)),
2557            ty::Uint(i) => (false, bx.cx.type_uint_from_ty(i)),
2558            _ => {
2559                return_error!(InvalidMonomorphization::ExpectedVectorElementType {
2560                    span,
2561                    name,
2562                    expected_element: args[0].layout.ty.simd_size_and_type(bx.tcx()).1,
2563                    vector_type: args[0].layout.ty
2564                });
2565            }
2566        };
2567        let llvm_intrinsic = format!(
2568            "llvm.{}{}.sat",
2569            if signed { 's' } else { 'u' },
2570            if is_add { "add" } else { "sub" },
2571        );
2572        let vec_ty = bx.cx.type_vector(elem_ty, in_len as u64);
2573
2574        return Ok(bx.call_intrinsic(llvm_intrinsic, &[vec_ty], &[lhs, rhs]));
2575    }
2576
2577    span_bug!(span, "unknown SIMD intrinsic");
2578}