Skip to main content

rustc_codegen_llvm/
intrinsic.rs

1use std::cmp::Ordering;
2use std::ffi::c_uint;
3use std::{assert_matches, iter, ptr};
4
5use rustc_abi::{
6    AddressSpace, Align, BackendRepr, CVariadicStatus, Float, HasDataLayout, Integer,
7    NumScalableVectors, Primitive, Size, WrappingRange,
8};
9use rustc_codegen_ssa::RetagInfo;
10use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh, wants_wasm_eh};
11use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
12use rustc_codegen_ssa::errors::{ExpectedPointerMutability, InvalidMonomorphization};
13use rustc_codegen_ssa::mir::IntrinsicResult;
14use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
15use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
16use rustc_codegen_ssa::traits::*;
17use rustc_hir as hir;
18use rustc_hir::def_id::LOCAL_CRATE;
19use rustc_hir::find_attr;
20use rustc_middle::mir::BinOp;
21use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, LayoutOf};
22use rustc_middle::ty::offload_meta::OffloadMetadata;
23use rustc_middle::ty::{self, GenericArgsRef, Instance, SimdAlign, Ty, TyCtxt, TypingEnv};
24use rustc_middle::{bug, span_bug};
25use rustc_session::config::CrateType;
26use rustc_session::errors::feature_err;
27use rustc_session::lint::builtin::DEPRECATED_LLVM_INTRINSIC;
28use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
29use rustc_symbol_mangling::{mangle_internal_symbol, symbol_name_for_instance_in_crate};
30use rustc_target::callconv::PassMode;
31use rustc_target::spec::{Arch, Os};
32use tracing::debug;
33
34use crate::abi::FnAbiLlvmExt;
35use crate::builder::Builder;
36use crate::builder::autodiff::{adjust_activity_to_abi, generate_enzyme_call};
37use crate::builder::gpu_offload::{
38    OffloadKernelDims, gen_call_handling, gen_define_handling, register_offload,
39};
40use crate::context::CodegenCx;
41use crate::declare::declare_raw_fn;
42use crate::errors::{
43    AutoDiffWithoutEnable, AutoDiffWithoutLto, IntrinsicSignatureMismatch, IntrinsicWrongArch,
44    OffloadWithoutEnable, OffloadWithoutFatLTO, UnknownIntrinsic,
45};
46use crate::llvm::{self, Type, Value};
47use crate::type_of::LayoutLlvmExt;
48use crate::va_arg::emit_va_arg;
49
50fn call_simple_intrinsic<'ll, 'tcx>(
51    bx: &mut Builder<'_, 'll, 'tcx>,
52    name: Symbol,
53    args: &[OperandRef<'tcx, &'ll Value>],
54) -> Option<&'ll Value> {
55    let (base_name, type_params): (&'static str, &[&'ll Type]) = match name {
56        sym::sqrtf16 => ("llvm.sqrt", &[bx.type_f16()]),
57        sym::sqrtf32 => ("llvm.sqrt", &[bx.type_f32()]),
58        sym::sqrtf64 => ("llvm.sqrt", &[bx.type_f64()]),
59        sym::sqrtf128 => ("llvm.sqrt", &[bx.type_f128()]),
60
61        sym::powif16 => ("llvm.powi", &[bx.type_f16(), bx.type_i32()]),
62        sym::powif32 => ("llvm.powi", &[bx.type_f32(), bx.type_i32()]),
63        sym::powif64 => ("llvm.powi", &[bx.type_f64(), bx.type_i32()]),
64        sym::powif128 => ("llvm.powi", &[bx.type_f128(), bx.type_i32()]),
65
66        sym::sinf16 => ("llvm.sin", &[bx.type_f16()]),
67        sym::sinf32 => ("llvm.sin", &[bx.type_f32()]),
68        sym::sinf64 => ("llvm.sin", &[bx.type_f64()]),
69        sym::sinf128 => ("llvm.sin", &[bx.type_f128()]),
70
71        sym::cosf16 => ("llvm.cos", &[bx.type_f16()]),
72        sym::cosf32 => ("llvm.cos", &[bx.type_f32()]),
73        sym::cosf64 => ("llvm.cos", &[bx.type_f64()]),
74        sym::cosf128 => ("llvm.cos", &[bx.type_f128()]),
75
76        sym::powf16 => ("llvm.pow", &[bx.type_f16()]),
77        sym::powf32 => ("llvm.pow", &[bx.type_f32()]),
78        sym::powf64 => ("llvm.pow", &[bx.type_f64()]),
79        sym::powf128 => ("llvm.pow", &[bx.type_f128()]),
80
81        sym::expf16 => ("llvm.exp", &[bx.type_f16()]),
82        sym::expf32 => ("llvm.exp", &[bx.type_f32()]),
83        sym::expf64 => ("llvm.exp", &[bx.type_f64()]),
84        sym::expf128 => ("llvm.exp", &[bx.type_f128()]),
85
86        sym::exp2f16 => ("llvm.exp2", &[bx.type_f16()]),
87        sym::exp2f32 => ("llvm.exp2", &[bx.type_f32()]),
88        sym::exp2f64 => ("llvm.exp2", &[bx.type_f64()]),
89        sym::exp2f128 => ("llvm.exp2", &[bx.type_f128()]),
90
91        sym::logf16 => ("llvm.log", &[bx.type_f16()]),
92        sym::logf32 => ("llvm.log", &[bx.type_f32()]),
93        sym::logf64 => ("llvm.log", &[bx.type_f64()]),
94        sym::logf128 => ("llvm.log", &[bx.type_f128()]),
95
96        sym::log10f16 => ("llvm.log10", &[bx.type_f16()]),
97        sym::log10f32 => ("llvm.log10", &[bx.type_f32()]),
98        sym::log10f64 => ("llvm.log10", &[bx.type_f64()]),
99        sym::log10f128 => ("llvm.log10", &[bx.type_f128()]),
100
101        sym::log2f16 => ("llvm.log2", &[bx.type_f16()]),
102        sym::log2f32 => ("llvm.log2", &[bx.type_f32()]),
103        sym::log2f64 => ("llvm.log2", &[bx.type_f64()]),
104        sym::log2f128 => ("llvm.log2", &[bx.type_f128()]),
105
106        sym::fmaf16 => ("llvm.fma", &[bx.type_f16()]),
107        sym::fmaf32 => ("llvm.fma", &[bx.type_f32()]),
108        sym::fmaf64 => ("llvm.fma", &[bx.type_f64()]),
109        sym::fmaf128 => ("llvm.fma", &[bx.type_f128()]),
110
111        sym::fmuladdf16 => ("llvm.fmuladd", &[bx.type_f16()]),
112        sym::fmuladdf32 => ("llvm.fmuladd", &[bx.type_f32()]),
113        sym::fmuladdf64 => ("llvm.fmuladd", &[bx.type_f64()]),
114        sym::fmuladdf128 => ("llvm.fmuladd", &[bx.type_f128()]),
115
116        // FIXME: LLVM currently mis-compile those intrinsics, re-enable them
117        // when llvm/llvm-project#{139380,139381,140445} are fixed.
118        //sym::minimumf16 => ("llvm.minimum", &[bx.type_f16()]),
119        //sym::minimumf32 => ("llvm.minimum", &[bx.type_f32()]),
120        //sym::minimumf64 => ("llvm.minimum", &[bx.type_f64()]),
121        //sym::minimumf128 => ("llvm.minimum", &[cx.type_f128()]),
122        //
123        // FIXME: LLVM currently mis-compile those intrinsics, re-enable them
124        // when llvm/llvm-project#{139380,139381,140445} are fixed.
125        //sym::maximumf16 => ("llvm.maximum", &[bx.type_f16()]),
126        //sym::maximumf32 => ("llvm.maximum", &[bx.type_f32()]),
127        //sym::maximumf64 => ("llvm.maximum", &[bx.type_f64()]),
128        //sym::maximumf128 => ("llvm.maximum", &[cx.type_f128()]),
129        //
130        sym::copysignf16 => ("llvm.copysign", &[bx.type_f16()]),
131        sym::copysignf32 => ("llvm.copysign", &[bx.type_f32()]),
132        sym::copysignf64 => ("llvm.copysign", &[bx.type_f64()]),
133        sym::copysignf128 => ("llvm.copysign", &[bx.type_f128()]),
134
135        sym::floorf16 => ("llvm.floor", &[bx.type_f16()]),
136        sym::floorf32 => ("llvm.floor", &[bx.type_f32()]),
137        sym::floorf64 => ("llvm.floor", &[bx.type_f64()]),
138        sym::floorf128 => ("llvm.floor", &[bx.type_f128()]),
139
140        sym::ceilf16 => ("llvm.ceil", &[bx.type_f16()]),
141        sym::ceilf32 => ("llvm.ceil", &[bx.type_f32()]),
142        sym::ceilf64 => ("llvm.ceil", &[bx.type_f64()]),
143        sym::ceilf128 => ("llvm.ceil", &[bx.type_f128()]),
144
145        sym::truncf16 => ("llvm.trunc", &[bx.type_f16()]),
146        sym::truncf32 => ("llvm.trunc", &[bx.type_f32()]),
147        sym::truncf64 => ("llvm.trunc", &[bx.type_f64()]),
148        sym::truncf128 => ("llvm.trunc", &[bx.type_f128()]),
149
150        // We could use any of `rint`, `nearbyint`, or `roundeven`
151        // for this -- they are all identical in semantics when
152        // assuming the default FP environment.
153        // `rint` is what we used for $forever.
154        sym::round_ties_even_f16 => ("llvm.rint", &[bx.type_f16()]),
155        sym::round_ties_even_f32 => ("llvm.rint", &[bx.type_f32()]),
156        sym::round_ties_even_f64 => ("llvm.rint", &[bx.type_f64()]),
157        sym::round_ties_even_f128 => ("llvm.rint", &[bx.type_f128()]),
158
159        sym::roundf16 => ("llvm.round", &[bx.type_f16()]),
160        sym::roundf32 => ("llvm.round", &[bx.type_f32()]),
161        sym::roundf64 => ("llvm.round", &[bx.type_f64()]),
162        sym::roundf128 => ("llvm.round", &[bx.type_f128()]),
163
164        _ => return None,
165    };
166    Some(bx.call_intrinsic(
167        base_name,
168        type_params,
169        &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
170    ))
171}
172
173impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
174    fn codegen_intrinsic_call(
175        &mut self,
176        instance: ty::Instance<'tcx>,
177        args: &[OperandRef<'tcx, &'ll Value>],
178        result_layout: ty::layout::TyAndLayout<'tcx>,
179        result_place: Option<PlaceValue<&'ll Value>>,
180        span: Span,
181    ) -> IntrinsicResult<'tcx, &'ll Value> {
182        let tcx = self.tcx;
183        let llvm_version = crate::llvm_util::get_version();
184
185        let name = tcx.item_name(instance.def_id());
186        let fn_args = instance.args;
187
188        let simple = call_simple_intrinsic(self, name, args);
189        let llval = match name {
190            _ if simple.is_some() => simple.unwrap(),
191            sym::minimum_number_nsz_f16
192            | sym::minimum_number_nsz_f32
193            | sym::minimum_number_nsz_f64
194            | sym::minimum_number_nsz_f128
195            | sym::maximum_number_nsz_f16
196            | sym::maximum_number_nsz_f32
197            | sym::maximum_number_nsz_f64
198            | sym::maximum_number_nsz_f128
199                // Need at least LLVM 22 for `min/maximumnum` to not crash LLVM.
200                if llvm_version >= (22, 0, 0) =>
201            {
202                let intrinsic_name = if name.as_str().starts_with("min") {
203                    "llvm.minimumnum"
204                } else {
205                    "llvm.maximumnum"
206                };
207                let call = self.call_intrinsic(
208                    intrinsic_name,
209                    &[args[0].layout.immediate_llvm_type(self.cx)],
210                    &[args[0].immediate(), args[1].immediate()],
211                );
212                // `nsz` on minimumnum/maximumnum is special: its only effect is to make
213                // signed-zero ordering non-deterministic.
214                unsafe { llvm::LLVMRustSetNoSignedZeros(call) };
215                call
216            }
217            sym::ptr_mask => {
218                let ptr = args[0].immediate();
219                self.call_intrinsic(
220                    "llvm.ptrmask",
221                    &[self.val_ty(ptr), self.type_isize()],
222                    &[ptr, args[1].immediate()],
223                )
224            }
225            sym::autodiff => {
226                let result = PlaceRef {
227                    val: result_place.unwrap(),
228                    layout: result_layout,
229                };
230                codegen_autodiff(self, tcx, instance, args, result);
231                return IntrinsicResult::WroteIntoPlace;
232            }
233            sym::offload => {
234                if tcx.sess.opts.unstable_opts.offload.is_empty() {
235                    let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutEnable);
236                }
237
238                if tcx.sess.lto() != rustc_session::config::Lto::Fat {
239                    let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutFatLTO);
240                }
241
242                codegen_offload(self, tcx, instance, args);
243                // offload *has* a return type, but somehow works without mentioning the place
244                return IntrinsicResult::WroteIntoPlace;
245            }
246            sym::is_val_statically_known => {
247                if let OperandValue::Immediate(imm) = args[0].val {
248                    self.call_intrinsic(
249                        "llvm.is.constant",
250                        &[args[0].layout.immediate_llvm_type(self.cx)],
251                        &[imm],
252                    )
253                } else {
254                    self.const_bool(false)
255                }
256            }
257            sym::select_unpredictable => {
258                let cond = args[0].immediate();
259                match (&args[1].layout, &args[2].layout) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(args[1].layout, args[2].layout);
260                let select = |bx: &mut Self, true_val, false_val| {
261                    let result = bx.select(cond, true_val, false_val);
262                    bx.set_unpredictable(&result);
263                    result
264                };
265                match (args[1].val, args[2].val) {
266                    (OperandValue::Ref(true_val), OperandValue::Ref(false_val)) => {
267                        if !true_val.llextra.is_none() {
    ::core::panicking::panic("assertion failed: true_val.llextra.is_none()")
};assert!(true_val.llextra.is_none());
268                        if !false_val.llextra.is_none() {
    ::core::panicking::panic("assertion failed: false_val.llextra.is_none()")
};assert!(false_val.llextra.is_none());
269                        match (&true_val.align, &false_val.align) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(true_val.align, false_val.align);
270                        let ptr = select(self, true_val.llval, false_val.llval);
271                        let selected =
272                            OperandValue::Ref(PlaceValue::new_sized(ptr, true_val.align));
273                        let result = PlaceRef {
274                            val: result_place.unwrap(),
275                            layout: result_layout,
276                        };
277                        selected.store(self, result);
278                        return IntrinsicResult::WroteIntoPlace;
279                    }
280                    (OperandValue::Immediate(_), OperandValue::Immediate(_))
281                    | (OperandValue::Pair(_, _), OperandValue::Pair(_, _)) => {
282                        let true_val = args[1].immediate_or_packed_pair(self);
283                        let false_val = args[2].immediate_or_packed_pair(self);
284                        select(self, true_val, false_val)
285                    }
286                    (OperandValue::ZeroSized, OperandValue::ZeroSized) => return IntrinsicResult::Operand(OperandValue::ZeroSized),
287                    _ => ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("Incompatible OperandValue for select_unpredictable"))span_bug!(span, "Incompatible OperandValue for select_unpredictable"),
288                }
289            }
290            sym::catch_unwind => {
291                let result = PlaceRef {
292                    val: result_place.unwrap(),
293                    layout: result_layout,
294                };
295                catch_unwind_intrinsic(
296                    self,
297                    args[0].immediate(),
298                    args[1].immediate(),
299                    args[2].immediate(),
300                    result,
301                );
302                return IntrinsicResult::WroteIntoPlace;
303            }
304            sym::breakpoint => self.call_intrinsic("llvm.debugtrap", &[], &[]),
305            sym::va_arg => {
306                let target = &self.cx.tcx.sess.target;
307                let stability = target.supports_c_variadic_definitions();
308                if let CVariadicStatus::Unstable { feature } = stability
309                    && !self.tcx.features().enabled(feature)
310                {
311                    let msg =
312                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("C-variadic function definitions on this target are unstable"))
    })format!("C-variadic function definitions on this target are unstable");
313                    feature_err(&*self.sess(), feature, span, msg).emit();
314                }
315
316                let BackendRepr::Scalar(scalar) = result_layout.backend_repr else {
317                    ::rustc_middle::util::bug::bug_fmt(format_args!("the va_arg intrinsic does not support non-scalar types"))bug!("the va_arg intrinsic does not support non-scalar types")
318                };
319
320                // We reject types that would never be passed as varargs in C because
321                // they get promoted to a larger type, specifically integers smaller than
322                // c_int and float type smaller than c_double.
323                match scalar.primitive() {
324                    Primitive::Pointer(_) => {
325                        // Pointers are always OK.
326                    }
327                    Primitive::Int(Integer::I128, _) => {
328                        // FIXME: maybe we should support these? At least on 32-bit powerpc
329                        // the logic in LLVM does not handle i128 correctly though.
330                        ::rustc_middle::util::bug::bug_fmt(format_args!("the va_arg intrinsic does not support `i128`/`u128`"))bug!("the va_arg intrinsic does not support `i128`/`u128`")
331                    }
332                    Primitive::Int(..) => {
333                        let int_width = self.cx().size_of(result_layout.ty).bits();
334                        let target_c_int_width = self.cx().sess().target.options.c_int_width;
335                        if int_width < u64::from(target_c_int_width) {
336                            // Smaller integer types are automatically promototed and `va_arg`
337                            // should not be called on them.
338                            ::rustc_middle::util::bug::bug_fmt(format_args!("va_arg got i{0} but needs at least c_int (an i{1})",
        int_width, target_c_int_width));bug!(
339                                "va_arg got i{} but needs at least c_int (an i{})",
340                                int_width,
341                                target_c_int_width
342                            );
343                        }
344                    }
345                    Primitive::Float(Float::F16) => {
346                        ::rustc_middle::util::bug::bug_fmt(format_args!("the va_arg intrinsic does not support `f16`"))bug!("the va_arg intrinsic does not support `f16`")
347                    }
348                    Primitive::Float(Float::F32) => {
349                        // c_double is actually f32 on avr.
350                        if self.cx().sess().target.arch != Arch::Avr {
351                            ::rustc_middle::util::bug::bug_fmt(format_args!("the va_arg intrinsic does not support `f32` on this target"))bug!("the va_arg intrinsic does not support `f32` on this target")
352                        }
353                    }
354                    Primitive::Float(Float::F64) => {
355                        // 64-bit floats are always OK.
356                    }
357                    Primitive::Float(Float::F128) => {
358                        // FIXME(f128) figure out whether we should support this.
359                        ::rustc_middle::util::bug::bug_fmt(format_args!("the va_arg intrinsic does not support `f128`"))bug!("the va_arg intrinsic does not support `f128`")
360                    }
361                }
362
363                emit_va_arg(self, args[0], result_layout.ty)
364            }
365
366            sym::volatile_load | sym::unaligned_volatile_load => {
367                let result = PlaceRef {
368                    val: result_place.unwrap(),
369                    layout: result_layout,
370                };
371
372                let ptr = args[0].immediate();
373                let load = self.volatile_load(result_layout.llvm_type(self), ptr);
374                let align = if name == sym::unaligned_volatile_load {
375                    1
376                } else {
377                    result_layout.align.bytes() as u32
378                };
379                unsafe {
380                    llvm::LLVMSetAlignment(load, align);
381                }
382                if !result_layout.is_zst() {
383                    self.store_to_place(load, result.val);
384                }
385                return IntrinsicResult::WroteIntoPlace;
386            }
387            sym::volatile_store => {
388                let dst = args[0].deref(self.cx());
389                args[1].val.volatile_store(self, dst);
390                return IntrinsicResult::Operand(OperandValue::ZeroSized);
391            }
392            sym::unaligned_volatile_store => {
393                let dst = args[0].deref(self.cx());
394                args[1].val.unaligned_volatile_store(self, dst);
395                return IntrinsicResult::Operand(OperandValue::ZeroSized);
396            }
397            sym::prefetch_read_data
398            | sym::prefetch_write_data
399            | sym::prefetch_read_instruction
400            | sym::prefetch_write_instruction => {
401                let (rw, cache_type) = match name {
402                    sym::prefetch_read_data => (0, 1),
403                    sym::prefetch_write_data => (1, 1),
404                    sym::prefetch_read_instruction => (0, 0),
405                    sym::prefetch_write_instruction => (1, 0),
406                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
407                };
408                let ptr = args[0].immediate();
409                let locality = fn_args.const_at(1).to_leaf().to_i32();
410                self.call_intrinsic(
411                    "llvm.prefetch",
412                    &[self.val_ty(ptr)],
413                    &[
414                        ptr,
415                        self.const_i32(rw),
416                        self.const_i32(locality),
417                        self.const_i32(cache_type),
418                    ],
419                );
420                return IntrinsicResult::Operand(OperandValue::ZeroSized);
421            }
422            sym::carrying_mul_add => {
423                let (size, signed) = fn_args.type_at(0).int_size_and_signed(self.tcx);
424
425                let wide_llty = self.type_ix(size.bits() * 2);
426                let args = args.as_array().unwrap();
427                let [a, b, c, d] = args.map(|a| self.intcast(a.immediate(), wide_llty, signed));
428
429                let wide = if signed {
430                    let prod = self.unchecked_smul(a, b);
431                    let acc = self.unchecked_sadd(prod, c);
432                    self.unchecked_sadd(acc, d)
433                } else {
434                    let prod = self.unchecked_umul(a, b);
435                    let acc = self.unchecked_uadd(prod, c);
436                    self.unchecked_uadd(acc, d)
437                };
438
439                let narrow_llty = self.type_ix(size.bits());
440                let low = self.trunc(wide, narrow_llty);
441                let bits_const = self.const_uint(wide_llty, size.bits());
442                // No need for ashr when signed; LLVM changes it to lshr anyway.
443                let high = self.lshr(wide, bits_const);
444                // FIXME: could be `trunc nuw`, even for signed.
445                let high = self.trunc(high, narrow_llty);
446
447                let pair_llty = self.type_struct(&[narrow_llty, narrow_llty], false);
448                let pair = self.const_poison(pair_llty);
449                let pair = self.insert_value(pair, low, 0);
450                let pair = self.insert_value(pair, high, 1);
451                pair
452            }
453
454            // FIXME move into the branch below when LLVM 22 is the lowest version we support.
455            sym::carryless_mul if llvm_version >= (22, 0, 0) => {
456                let ty = args[0].layout.ty;
457                if !ty.is_integral() {
458                    let err = tcx.dcx().emit_err(InvalidMonomorphization::BasicIntegerType {
459                        span,
460                        name,
461                        ty,
462                    });
463                    return IntrinsicResult::Err(err);
464                }
465                let (size, _) = ty.int_size_and_signed(self.tcx);
466                let width = size.bits();
467                let llty = self.type_ix(width);
468
469                let lhs = args[0].immediate();
470                let rhs = args[1].immediate();
471                self.call_intrinsic("llvm.clmul", &[llty], &[lhs, rhs])
472            }
473
474            sym::ctlz
475            | sym::ctlz_nonzero
476            | sym::cttz
477            | sym::cttz_nonzero
478            | sym::ctpop
479            | sym::bswap
480            | sym::bitreverse
481            | sym::saturating_add
482            | sym::saturating_sub
483            | sym::unchecked_funnel_shl
484            | sym::unchecked_funnel_shr => {
485                let ty = args[0].layout.ty;
486                if !ty.is_integral() {
487                    let err = tcx.dcx().emit_err(InvalidMonomorphization::BasicIntegerType {
488                        span,
489                        name,
490                        ty,
491                    });
492                    return IntrinsicResult::Err(err);
493                }
494                let (size, signed) = ty.int_size_and_signed(self.tcx);
495                let width = size.bits();
496                let llty = self.type_ix(width);
497                match name {
498                    sym::ctlz | sym::ctlz_nonzero | sym::cttz | sym::cttz_nonzero => {
499                        let y =
500                            self.const_bool(name == sym::ctlz_nonzero || name == sym::cttz_nonzero);
501                        let llvm_name = if name == sym::ctlz || name == sym::ctlz_nonzero {
502                            "llvm.ctlz"
503                        } else {
504                            "llvm.cttz"
505                        };
506                        let ret =
507                            self.call_intrinsic(llvm_name, &[llty], &[args[0].immediate(), y]);
508                        self.intcast(ret, result_layout.llvm_type(self), false)
509                    }
510                    sym::ctpop => {
511                        let ret =
512                            self.call_intrinsic("llvm.ctpop", &[llty], &[args[0].immediate()]);
513                        self.intcast(ret, result_layout.llvm_type(self), false)
514                    }
515                    sym::bswap => {
516                        if width == 8 {
517                            args[0].immediate() // byte swap a u8/i8 is just a no-op
518                        } else {
519                            self.call_intrinsic("llvm.bswap", &[llty], &[args[0].immediate()])
520                        }
521                    }
522                    sym::bitreverse => {
523                        self.call_intrinsic("llvm.bitreverse", &[llty], &[args[0].immediate()])
524                    }
525                    sym::unchecked_funnel_shl | sym::unchecked_funnel_shr => {
526                        let is_left = name == sym::unchecked_funnel_shl;
527                        let lhs = args[0].immediate();
528                        let rhs = args[1].immediate();
529                        let raw_shift = args[2].immediate();
530                        let llvm_name = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("llvm.fsh{0}",
                if is_left { 'l' } else { 'r' }))
    })format!("llvm.fsh{}", if is_left { 'l' } else { 'r' });
531
532                        // llvm expects shift to be the same type as the values, but rust
533                        // always uses `u32`.
534                        let raw_shift = self.intcast(raw_shift, self.val_ty(lhs), false);
535
536                        self.call_intrinsic(llvm_name, &[llty], &[lhs, rhs, raw_shift])
537                    }
538                    sym::saturating_add | sym::saturating_sub => {
539                        let is_add = name == sym::saturating_add;
540                        let lhs = args[0].immediate();
541                        let rhs = args[1].immediate();
542                        let llvm_name = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("llvm.{0}{1}.sat",
                if signed { 's' } else { 'u' },
                if is_add { "add" } else { "sub" }))
    })format!(
543                            "llvm.{}{}.sat",
544                            if signed { 's' } else { 'u' },
545                            if is_add { "add" } else { "sub" },
546                        );
547                        self.call_intrinsic(llvm_name, &[llty], &[lhs, rhs])
548                    }
549                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
550                }
551            }
552
553            sym::fabs => {
554                let ty = args[0].layout.ty;
555                let ty::Float(f) = ty.kind() else {
556                    ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("the `fabs` intrinsic requires a floating-point argument, got {0:?}",
        ty));span_bug!(span, "the `fabs` intrinsic requires a floating-point argument, got {:?}", ty);
557                };
558                let llty = self.type_float_from_ty(*f);
559                let llvm_name = "llvm.fabs";
560                self.call_intrinsic(
561                    llvm_name,
562                    &[llty],
563                    &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
564                )
565            }
566
567            sym::raw_eq => {
568                use BackendRepr::*;
569                let tp_ty = fn_args.type_at(0);
570                let layout = self.layout_of(tp_ty).layout;
571                let use_integer_compare = match layout.backend_repr() {
572                    Scalar(_) | ScalarPair(_, _) => true,
573                    SimdVector { .. } => false,
574                    SimdScalableVector { .. } => {
575                        let err = tcx.dcx().emit_err(InvalidMonomorphization::NonScalableType {
576                            span,
577                            name: sym::raw_eq,
578                            ty: tp_ty,
579                        });
580                        return IntrinsicResult::Err(err);
581                    }
582                    Memory { .. } => {
583                        // For rusty ABIs, small aggregates are actually passed
584                        // as `RegKind::Integer` (see `FnAbi::adjust_for_abi`),
585                        // so we re-use that same threshold here.
586                        layout.size() <= self.data_layout().pointer_size() * 2
587                    }
588                };
589
590                let a = args[0].immediate();
591                let b = args[1].immediate();
592                if layout.size().bytes() == 0 {
593                    self.const_bool(true)
594                } else if use_integer_compare {
595                    let integer_ty = self.type_ix(layout.size().bits());
596                    let a_val = self.load(integer_ty, a, layout.align().abi);
597                    let b_val = self.load(integer_ty, b, layout.align().abi);
598                    self.icmp(IntPredicate::IntEQ, a_val, b_val)
599                } else {
600                    let n = self.const_usize(layout.size().bytes());
601                    let cmp = self.call_intrinsic("memcmp", &[], &[a, b, n]);
602                    self.icmp(IntPredicate::IntEQ, cmp, self.const_int(self.type_int(), 0))
603                }
604            }
605
606            sym::compare_bytes => {
607                // Here we assume that the `memcmp` provided by the target is a NOP for size 0.
608                let cmp = self.call_intrinsic(
609                    "memcmp",
610                    &[],
611                    &[args[0].immediate(), args[1].immediate(), args[2].immediate()],
612                );
613                // Some targets have `memcmp` returning `i16`, but the intrinsic is always `i32`.
614                self.sext(cmp, self.type_ix(32))
615            }
616
617            sym::black_box => {
618                let result = PlaceRef {
619                    val: result_place.unwrap(),
620                    layout: result_layout,
621                };
622                args[0].val.store(self, result);
623                let result_val_span = [result.val.llval];
624                // We need to "use" the argument in some way LLVM can't introspect, and on
625                // targets that support it we can typically leverage inline assembly to do
626                // this. LLVM's interpretation of inline assembly is that it's, well, a black
627                // box. This isn't the greatest implementation since it probably deoptimizes
628                // more than we want, but it's so far good enough.
629                //
630                // For zero-sized types, the location pointed to by the result may be
631                // uninitialized. Do not "use" the result in this case; instead just clobber
632                // the memory.
633                let (constraint, inputs): (&str, &[_]) = if result.layout.is_zst() {
634                    ("~{memory}", &[])
635                } else {
636                    ("r,~{memory}", &result_val_span)
637                };
638                crate::asm::inline_asm_call(
639                    self,
640                    "",
641                    constraint,
642                    inputs,
643                    self.type_void(),
644                    &[],
645                    true,
646                    false,
647                    llvm::AsmDialect::Att,
648                    &[span],
649                    false,
650                    None,
651                    None,
652                )
653                .unwrap_or_else(|| ::rustc_middle::util::bug::bug_fmt(format_args!("failed to generate inline asm call for `black_box`"))bug!("failed to generate inline asm call for `black_box`"));
654
655                // We have copied the value to `result` already.
656                return IntrinsicResult::WroteIntoPlace;
657            }
658
659            sym::gpu_launch_sized_workgroup_mem => {
660                // Generate an anonymous global per call, with these properties:
661                // 1. The global is in the address space for workgroup memory
662                // 2. It is an `external` global
663                // 3. It is correctly aligned for the pointee `T`
664                // All instances of extern addrspace(gpu_workgroup) globals are merged in the LLVM backend.
665                // The name is irrelevant.
666                // See https://docs.nvidia.com/cuda/cuda-c-programming-guide/#shared
667                let name = if llvm_version < (23, 0, 0) && tcx.sess.target.arch == Arch::Nvptx64 {
668                    // The auto-assigned name for extern shared globals in the nvptx backend does
669                    // not compile in ptxas. Workaround this issue by assigning a name.
670                    // Fixed in LLVM 23.
671                    "gpu_launch_sized_workgroup_mem"
672                } else {
673                    ""
674                };
675                let global = self.declare_global_in_addrspace(
676                    name,
677                    self.type_array(self.type_i8(), 0),
678                    AddressSpace::GPU_WORKGROUP,
679                );
680                let ty::RawPtr(inner_ty, _) = result_layout.ty.kind() else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
681                // The alignment of the global is used to specify the *minimum* alignment that
682                // must be obeyed by the GPU runtime.
683                // When multiple of these global variables are used by a kernel, the maximum alignment is taken.
684                // See https://github.com/llvm/llvm-project/blob/a271d07488a85ce677674bbe8101b10efff58c95/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp#L821
685                let alignment = self.align_of(*inner_ty).bytes() as u32;
686                unsafe {
687                    // FIXME Workaround the above issue by taking maximum alignment if the global existed
688                    if tcx.sess.target.arch == Arch::Nvptx64 {
689                        if alignment > llvm::LLVMGetAlignment(global) {
690                            llvm::LLVMSetAlignment(global, alignment);
691                        }
692                    } else {
693                        llvm::LLVMSetAlignment(global, alignment);
694                    }
695                }
696                self.cx().const_pointercast(global, self.type_ptr())
697            }
698
699            sym::amdgpu_dispatch_ptr => {
700                let val = self.call_intrinsic("llvm.amdgcn.dispatch.ptr", &[], &[]);
701                // Relying on `LLVMBuildPointerCast` to produce an addrspacecast
702                self.pointercast(val, self.type_ptr())
703            }
704
705            sym::sve_tuple_create2 => {
706                {
    match self.layout_of(fn_args.type_at(0)).backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(1), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(1), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
707                    self.layout_of(fn_args.type_at(0)).backend_repr,
708                    BackendRepr::SimdScalableVector {
709                        number_of_vectors: NumScalableVectors(1),
710                        ..
711                    }
712                );
713                let tuple_ty = self.layout_of(fn_args.type_at(1));
714                {
    match tuple_ty.backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(2), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(2), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
715                    tuple_ty.backend_repr,
716                    BackendRepr::SimdScalableVector {
717                        number_of_vectors: NumScalableVectors(2),
718                        ..
719                    }
720                );
721                let ret = self.const_poison(self.backend_type(tuple_ty));
722                let ret = self.insert_value(ret, args[0].immediate(), 0);
723                self.insert_value(ret, args[1].immediate(), 1)
724            }
725
726            sym::sve_tuple_create3 => {
727                {
    match self.layout_of(fn_args.type_at(0)).backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(1), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(1), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
728                    self.layout_of(fn_args.type_at(0)).backend_repr,
729                    BackendRepr::SimdScalableVector {
730                        number_of_vectors: NumScalableVectors(1),
731                        ..
732                    }
733                );
734                let tuple_ty = self.layout_of(fn_args.type_at(1));
735                {
    match tuple_ty.backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(3), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(3), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
736                    tuple_ty.backend_repr,
737                    BackendRepr::SimdScalableVector {
738                        number_of_vectors: NumScalableVectors(3),
739                        ..
740                    }
741                );
742                let ret = self.const_poison(self.backend_type(tuple_ty));
743                let ret = self.insert_value(ret, args[0].immediate(), 0);
744                let ret = self.insert_value(ret, args[1].immediate(), 1);
745                self.insert_value(ret, args[2].immediate(), 2)
746            }
747
748            sym::sve_tuple_create4 => {
749                {
    match self.layout_of(fn_args.type_at(0)).backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(1), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(1), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
750                    self.layout_of(fn_args.type_at(0)).backend_repr,
751                    BackendRepr::SimdScalableVector {
752                        number_of_vectors: NumScalableVectors(1),
753                        ..
754                    }
755                );
756                let tuple_ty = self.layout_of(fn_args.type_at(1));
757                {
    match tuple_ty.backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(4), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(4), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
758                    tuple_ty.backend_repr,
759                    BackendRepr::SimdScalableVector {
760                        number_of_vectors: NumScalableVectors(4),
761                        ..
762                    }
763                );
764                let ret = self.const_poison(self.backend_type(tuple_ty));
765                let ret = self.insert_value(ret, args[0].immediate(), 0);
766                let ret = self.insert_value(ret, args[1].immediate(), 1);
767                let ret = self.insert_value(ret, args[2].immediate(), 2);
768                self.insert_value(ret, args[3].immediate(), 3)
769            }
770
771            sym::sve_tuple_get => {
772                {
    match self.layout_of(fn_args.type_at(0)).backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(2 | 3 | 4 | 5 | 6 | 7 | 8),
            .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(2 | 3 | 4 | 5 | 6 | 7 | 8), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
773                    self.layout_of(fn_args.type_at(0)).backend_repr,
774                    BackendRepr::SimdScalableVector {
775                        number_of_vectors: NumScalableVectors(2 | 3 | 4 | 5 | 6 | 7 | 8),
776                        ..
777                    }
778                );
779                {
    match self.layout_of(fn_args.type_at(1)).backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(1), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(1), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
780                    self.layout_of(fn_args.type_at(1)).backend_repr,
781                    BackendRepr::SimdScalableVector {
782                        number_of_vectors: NumScalableVectors(1),
783                        ..
784                    }
785                );
786                self.extract_value(
787                    args[0].immediate(),
788                    fn_args.const_at(2).to_leaf().to_i32() as u64,
789                )
790            }
791
792            sym::sve_tuple_set => {
793                {
    match self.layout_of(fn_args.type_at(0)).backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(2 | 3 | 4 | 5 | 6 | 7 | 8),
            .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(2 | 3 | 4 | 5 | 6 | 7 | 8), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
794                    self.layout_of(fn_args.type_at(0)).backend_repr,
795                    BackendRepr::SimdScalableVector {
796                        number_of_vectors: NumScalableVectors(2 | 3 | 4 | 5 | 6 | 7 | 8),
797                        ..
798                    }
799                );
800                {
    match self.layout_of(fn_args.type_at(1)).backend_repr {
        BackendRepr::SimdScalableVector {
            number_of_vectors: NumScalableVectors(1), .. } => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::SimdScalableVector\n{ number_of_vectors: NumScalableVectors(1), .. }",
                ::core::option::Option::None);
        }
    }
};assert_matches!(
801                    self.layout_of(fn_args.type_at(1)).backend_repr,
802                    BackendRepr::SimdScalableVector {
803                        number_of_vectors: NumScalableVectors(1),
804                        ..
805                    }
806                );
807                self.insert_value(
808                    args[0].immediate(),
809                    args[1].immediate(),
810                    fn_args.const_at(2).to_leaf().to_i32() as u64,
811                )
812            }
813
814            _ if name.as_str().starts_with("simd_") => {
815                // Unpack non-power-of-2 #[repr(packed, simd)] arguments.
816                // This gives them the expected layout of a regular #[repr(simd)] vector.
817                let mut loaded_args = Vec::new();
818                for arg in args {
819                    loaded_args.push(
820                        // #[repr(packed, simd)] vectors are passed like arrays (as references,
821                        // with reduced alignment and no padding) rather than as immediates.
822                        // We can use a vector load to fix the layout and turn the argument
823                        // into an immediate.
824                        if arg.layout.ty.is_simd()
825                            && let OperandValue::Ref(place) = arg.val
826                        {
827                            let (size, elem_ty) = arg.layout.ty.simd_size_and_type(self.tcx());
828                            let elem_ll_ty = match elem_ty.kind() {
829                                ty::Float(f) => self.type_float_from_ty(*f),
830                                ty::Int(i) => self.type_int_from_ty(*i),
831                                ty::Uint(u) => self.type_uint_from_ty(*u),
832                                ty::RawPtr(_, _) => self.type_ptr(),
833                                _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
834                            };
835                            let loaded =
836                                self.load_from_place(self.type_vector(elem_ll_ty, size), place);
837                            OperandRef::from_immediate_or_packed_pair(self, loaded, arg.layout)
838                        } else {
839                            *arg
840                        },
841                    );
842                }
843
844                let llret_ty = if result_layout.ty.is_simd()
845                    && let BackendRepr::Memory { .. } = result_layout.backend_repr
846                {
847                    let (size, elem_ty) = result_layout.ty.simd_size_and_type(self.tcx());
848                    let elem_ll_ty = match elem_ty.kind() {
849                        ty::Float(f) => self.type_float_from_ty(*f),
850                        ty::Int(i) => self.type_int_from_ty(*i),
851                        ty::Uint(u) => self.type_uint_from_ty(*u),
852                        ty::RawPtr(_, _) => self.type_ptr(),
853                        _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
854                    };
855                    self.type_vector(elem_ll_ty, size)
856                } else {
857                    result_layout.llvm_type(self)
858                };
859
860                match generic_simd_intrinsic(
861                    self,
862                    name,
863                    fn_args,
864                    &loaded_args,
865                    result_layout.ty,
866                    llret_ty,
867                    span,
868                ) {
869                    Ok(llval) => llval,
870                    // If there was an error, just skip this invocation... we'll abort compilation
871                    // anyway, but we can keep codegen'ing to find more errors.
872                    Err(err) => return IntrinsicResult::Err(err),
873                }
874            }
875
876            sym::return_address => {
877                match self.sess().target.arch {
878                    // Expand this list as needed
879                    | Arch::Wasm32
880                    | Arch::Wasm64 => {
881                        let ty = self.type_ptr();
882                        self.const_null(ty)
883                    }
884                    _ => {
885                        let ty = self.type_ix(32);
886                        let val = self.const_int(ty, 0);
887
888                        let type_params: &[&'ll Type] = if llvm_version < (23, 0, 0) {
889                            &[]
890                        } else {
891                            &[self.type_ptr()]
892                        };
893
894                        self.call_intrinsic("llvm.returnaddress", type_params, &[val])
895                    }
896                }
897            }
898
899            _ => {
900                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/intrinsic.rs:900",
                        "rustc_codegen_llvm::intrinsic", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/intrinsic.rs"),
                        ::tracing_core::__macro_support::Option::Some(900u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::intrinsic"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("unknown intrinsic \'{0}\' -- falling back to default body",
                                                    name) as &dyn Value))])
            });
    } else { ; }
};debug!("unknown intrinsic '{}' -- falling back to default body", name);
901                // Call the fallback body instead of generating the intrinsic code
902                let fallback = ty::Instance::new_raw(instance.def_id(), instance.args);
903                return IntrinsicResult::Fallback(fallback);
904            }
905        };
906
907        if let BackendRepr::Memory { .. } = result_layout.backend_repr {
908            // We have an llvm immediate, but that's not what cg_ssa expects,
909            // so write it into the place (that always exists for memory)
910            if !result_layout.is_zst() {
911                self.store_to_place(llval, result_place.unwrap());
912            }
913            IntrinsicResult::WroteIntoPlace
914        } else {
915            IntrinsicResult::Operand(
916                OperandRef::from_immediate_or_packed_pair(self, llval, result_layout).val,
917            )
918        }
919    }
920
921    fn codegen_llvm_intrinsic_call(
922        &mut self,
923        instance: ty::Instance<'tcx>,
924        args: &[OperandRef<'tcx, Self::Value>],
925        _is_cleanup: bool,
926    ) -> Self::Value {
927        let tcx = self.tcx();
928
929        let fn_ty = instance.ty(tcx, self.typing_env());
930        let fn_sig = match *fn_ty.kind() {
931            ty::FnDef(def_id, args) => tcx.instantiate_bound_regions_with_erased(
932                tcx.fn_sig(def_id).instantiate(tcx, args).skip_norm_wip(),
933            ),
934            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
935        };
936        if !!fn_sig.c_variadic() {
    ::core::panicking::panic("assertion failed: !fn_sig.c_variadic()")
};assert!(!fn_sig.c_variadic());
937
938        let ret_layout = self.layout_of(fn_sig.output());
939        let llreturn_ty = if ret_layout.is_zst() {
940            self.type_void()
941        } else {
942            ret_layout.immediate_llvm_type(self)
943        };
944
945        let mut llargument_tys = Vec::with_capacity(fn_sig.inputs().len());
946        for &arg in fn_sig.inputs() {
947            let arg_layout = self.layout_of(arg);
948            if arg_layout.is_zst() {
949                continue;
950            }
951            llargument_tys.push(arg_layout.immediate_llvm_type(self));
952        }
953
954        let fn_ptr = if let Some(&llfn) = self.intrinsic_instances.borrow().get(&instance) {
955            llfn
956        } else {
957            let sym = tcx.symbol_name(instance).name;
958
959            let llfn = if let Some(llfn) = self.get_declared_value(sym) {
960                llfn
961            } else {
962                intrinsic_fn(self, sym, llreturn_ty, llargument_tys, instance)
963            };
964
965            self.intrinsic_instances.borrow_mut().insert(instance, llfn);
966
967            llfn
968        };
969        let fn_ty = self.get_type_of_global(fn_ptr);
970
971        let mut llargs = ::alloc::vec::Vec::new()vec![];
972
973        for arg in args {
974            match arg.val {
975                OperandValue::ZeroSized => {}
976                OperandValue::Immediate(a) => llargs.push(a),
977                OperandValue::Pair(a, b) => {
978                    llargs.push(a);
979                    llargs.push(b);
980                }
981                OperandValue::Ref(op_place_val) => {
982                    let mut llval = op_place_val.llval;
983                    // We can't use `PlaceRef::load` here because the argument
984                    // may have a type we don't treat as immediate, but the ABI
985                    // used for this call is passing it by-value. In that case,
986                    // the load would just produce `OperandValue::Ref` instead
987                    // of the `OperandValue::Immediate` we need for the call.
988                    llval = self.load(self.backend_type(arg.layout), llval, op_place_val.align);
989                    if let BackendRepr::Scalar(scalar) = arg.layout.backend_repr {
990                        if scalar.is_bool() {
991                            self.range_metadata(llval, WrappingRange { start: 0, end: 1 });
992                        }
993                        // We store bools as `i8` so we need to truncate to `i1`.
994                        llval = self.to_immediate_scalar(llval, scalar);
995                    }
996                    llargs.push(llval);
997                }
998            }
999        }
1000
1001        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/intrinsic.rs:1001",
                        "rustc_codegen_llvm::intrinsic", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/intrinsic.rs"),
                        ::tracing_core::__macro_support::Option::Some(1001u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::intrinsic"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("call intrinsic {0:?} with args ({1:?})",
                                                    instance, llargs) as &dyn Value))])
            });
    } else { ; }
};debug!("call intrinsic {:?} with args ({:?})", instance, llargs);
1002
1003        for (dest_ty, arg) in iter::zip(self.func_params_types(fn_ty), &mut llargs) {
1004            let src_ty = self.val_ty(arg);
1005            if !can_autocast(self, src_ty, dest_ty) {
    {
        ::core::panicking::panic_fmt(format_args!("Cannot match `{0:?}` (expected) with {1:?} (found) in `{2:?}",
                dest_ty, src_ty, fn_ptr));
    }
};assert!(
1006                can_autocast(self, src_ty, dest_ty),
1007                "Cannot match `{dest_ty:?}` (expected) with {src_ty:?} (found) in `{fn_ptr:?}"
1008            );
1009
1010            *arg = autocast(self, arg, src_ty, dest_ty);
1011        }
1012
1013        let llret = unsafe {
1014            llvm::LLVMBuildCallWithOperandBundles(
1015                self.llbuilder,
1016                fn_ty,
1017                fn_ptr,
1018                llargs.as_ptr(),
1019                llargs.len() as c_uint,
1020                ptr::dangling(),
1021                0,
1022                c"".as_ptr(),
1023            )
1024        };
1025
1026        let src_ty = self.val_ty(llret);
1027        let dest_ty = llreturn_ty;
1028        if !can_autocast(self, dest_ty, src_ty) {
    {
        ::core::panicking::panic_fmt(format_args!("Cannot match `{0:?}` (expected) with `{1:?}` (found) in `{2:?}`",
                src_ty, dest_ty, fn_ptr));
    }
};assert!(
1029            can_autocast(self, dest_ty, src_ty),
1030            "Cannot match `{src_ty:?}` (expected) with `{dest_ty:?}` (found) in `{fn_ptr:?}`"
1031        );
1032
1033        autocast(self, llret, src_ty, dest_ty)
1034    }
1035
1036    fn abort(&mut self) {
1037        self.call_intrinsic("llvm.trap", &[], &[]);
1038    }
1039
1040    fn assume(&mut self, val: Self::Value) {
1041        if self.cx.sess().opts.optimize != rustc_session::config::OptLevel::No {
1042            self.call_intrinsic("llvm.assume", &[], &[val]);
1043        }
1044    }
1045
1046    fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value {
1047        if self.cx.sess().opts.optimize != rustc_session::config::OptLevel::No {
1048            self.call_intrinsic(
1049                "llvm.expect",
1050                &[self.type_i1()],
1051                &[cond, self.const_bool(expected)],
1052            )
1053        } else {
1054            cond
1055        }
1056    }
1057
1058    fn type_checked_load(
1059        &mut self,
1060        llvtable: &'ll Value,
1061        vtable_byte_offset: u64,
1062        typeid: &[u8],
1063    ) -> Self::Value {
1064        let typeid = self.create_metadata(typeid);
1065        let typeid = self.get_metadata_value(typeid);
1066        let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
1067        let type_checked_load = self.call_intrinsic(
1068            "llvm.type.checked.load",
1069            &[],
1070            &[llvtable, vtable_byte_offset, typeid],
1071        );
1072        self.extract_value(type_checked_load, 0)
1073    }
1074
1075    fn va_start(&mut self, va_list: &'ll Value) {
1076        self.call_intrinsic("llvm.va_start", &[self.val_ty(va_list)], &[va_list]);
1077    }
1078
1079    fn va_end(&mut self, va_list: &'ll Value) {
1080        self.call_intrinsic("llvm.va_end", &[self.val_ty(va_list)], &[va_list]);
1081    }
1082
1083    fn retag_reg(&mut self, ptr: Self::Value, info: &RetagInfo<Self::Value>) -> Self::Value {
1084        codegen_retag_inner(self, "__rust_retag_reg", ptr, info)
1085    }
1086
1087    fn retag_mem(&mut self, ptr: Self::Value, info: &RetagInfo<Self::Value>) {
1088        codegen_retag_inner(self, "__rust_retag_mem", ptr, info);
1089    }
1090}
1091
1092fn llvm_arch_for(rust_arch: &Arch) -> Option<&'static str> {
1093    Some(match rust_arch {
1094        Arch::AArch64 | Arch::Arm64EC => "aarch64",
1095        Arch::AmdGpu => "amdgcn",
1096        Arch::Arm => "arm",
1097        Arch::Bpf => "bpf",
1098        Arch::Hexagon => "hexagon",
1099        Arch::LoongArch32 | Arch::LoongArch64 => "loongarch",
1100        Arch::Mips | Arch::Mips32r6 | Arch::Mips64 | Arch::Mips64r6 => "mips",
1101        Arch::Nvptx64 => "nvvm",
1102        Arch::PowerPC | Arch::PowerPC64 => "ppc",
1103        Arch::RiscV32 | Arch::RiscV64 => "riscv",
1104        Arch::S390x => "s390",
1105        Arch::SpirV => "spv",
1106        Arch::Wasm32 | Arch::Wasm64 => "wasm",
1107        Arch::X86 | Arch::X86_64 => "x86",
1108        _ => return None, // fallback for unknown archs
1109    })
1110}
1111
1112fn can_autocast<'ll>(cx: &CodegenCx<'ll, '_>, rust_ty: &'ll Type, llvm_ty: &'ll Type) -> bool {
1113    if rust_ty == llvm_ty {
1114        return true;
1115    }
1116
1117    match cx.type_kind(llvm_ty) {
1118        // Some LLVM intrinsics return **non-packed** structs, but they can't be mimicked from Rust
1119        // due to auto field-alignment in non-packed structs (packed structs are represented in LLVM
1120        // as, well, packed structs, so they won't match with those either)
1121        TypeKind::Struct if cx.type_kind(rust_ty) == TypeKind::Struct => {
1122            let rust_element_tys = cx.struct_element_types(rust_ty);
1123            let llvm_element_tys = cx.struct_element_types(llvm_ty);
1124
1125            if rust_element_tys.len() != llvm_element_tys.len() {
1126                return false;
1127            }
1128
1129            iter::zip(rust_element_tys, llvm_element_tys).all(
1130                |(rust_element_ty, llvm_element_ty)| {
1131                    can_autocast(cx, rust_element_ty, llvm_element_ty)
1132                },
1133            )
1134        }
1135        TypeKind::Vector => {
1136            let llvm_element_ty = cx.element_type(llvm_ty);
1137            let element_count = cx.vector_length(llvm_ty) as u64;
1138
1139            if llvm_element_ty == cx.type_bf16() {
1140                rust_ty == cx.type_vector(cx.type_i16(), element_count)
1141            } else if llvm_element_ty == cx.type_i1() {
1142                let int_width = element_count.next_power_of_two().max(8);
1143                rust_ty == cx.type_ix(int_width)
1144            } else {
1145                false
1146            }
1147        }
1148        TypeKind::BFloat => rust_ty == cx.type_i16(),
1149        TypeKind::X86_AMX if cx.type_kind(rust_ty) == TypeKind::Vector => {
1150            let element_ty = cx.element_type(rust_ty);
1151            let element_count = cx.vector_length(rust_ty) as u64;
1152
1153            let element_size_bits = match cx.type_kind(element_ty) {
1154                TypeKind::Half => 16,
1155                TypeKind::Float => 32,
1156                TypeKind::Double => 64,
1157                TypeKind::FP128 => 128,
1158                TypeKind::Integer => cx.int_width(element_ty),
1159                TypeKind::Pointer => cx.int_width(cx.isize_ty),
1160                _ => ::rustc_middle::util::bug::bug_fmt(format_args!("Vector element type `{0:?}` not one of integer, float or pointer",
        element_ty))bug!(
1161                    "Vector element type `{element_ty:?}` not one of integer, float or pointer"
1162                ),
1163            };
1164
1165            element_size_bits * element_count == 8192
1166        }
1167        _ => false,
1168    }
1169}
1170
1171fn autocast<'ll>(
1172    bx: &mut Builder<'_, 'll, '_>,
1173    val: &'ll Value,
1174    src_ty: &'ll Type,
1175    dest_ty: &'ll Type,
1176) -> &'ll Value {
1177    if src_ty == dest_ty {
1178        return val;
1179    }
1180    match (bx.type_kind(src_ty), bx.type_kind(dest_ty)) {
1181        // re-pack structs
1182        (TypeKind::Struct, TypeKind::Struct) => {
1183            let mut ret = bx.const_poison(dest_ty);
1184            for (idx, (src_element_ty, dest_element_ty)) in
1185                iter::zip(bx.struct_element_types(src_ty), bx.struct_element_types(dest_ty))
1186                    .enumerate()
1187            {
1188                let elt = bx.extract_value(val, idx as u64);
1189                let casted_elt = autocast(bx, elt, src_element_ty, dest_element_ty);
1190                ret = bx.insert_value(ret, casted_elt, idx as u64);
1191            }
1192            ret
1193        }
1194        // cast from the i1xN vector type to the primitive type
1195        (TypeKind::Vector, TypeKind::Integer) if bx.element_type(src_ty) == bx.type_i1() => {
1196            let vector_length = bx.vector_length(src_ty) as u64;
1197            let int_width = vector_length.next_power_of_two().max(8);
1198
1199            let val = if vector_length == int_width {
1200                val
1201            } else {
1202                // zero-extends vector
1203                let shuffle_indices = match vector_length {
1204                    0 => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("zero length vectors are not allowed")));
}unreachable!("zero length vectors are not allowed"),
1205                    1 => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [0, 1, 1, 1, 1, 1, 1, 1]))vec![0, 1, 1, 1, 1, 1, 1, 1],
1206                    2 => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [0, 1, 2, 2, 2, 2, 2, 2]))vec![0, 1, 2, 2, 2, 2, 2, 2],
1207                    3 => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [0, 1, 2, 3, 3, 3, 3, 3]))vec![0, 1, 2, 3, 3, 3, 3, 3],
1208                    4.. => (0..int_width as i32).collect(),
1209                };
1210                let shuffle_mask =
1211                    shuffle_indices.into_iter().map(|i| bx.const_i32(i)).collect::<Vec<_>>();
1212                bx.shuffle_vector(val, bx.const_null(src_ty), bx.const_vector(&shuffle_mask))
1213            };
1214            bx.bitcast(val, dest_ty)
1215        }
1216        // cast from the primitive type to the i1xN vector type
1217        (TypeKind::Integer, TypeKind::Vector) if bx.element_type(dest_ty) == bx.type_i1() => {
1218            let vector_length = bx.vector_length(dest_ty) as u64;
1219            let int_width = vector_length.next_power_of_two().max(8);
1220
1221            let intermediate_ty = bx.type_vector(bx.type_i1(), int_width);
1222            let intermediate = bx.bitcast(val, intermediate_ty);
1223
1224            if vector_length == int_width {
1225                intermediate
1226            } else {
1227                let shuffle_mask: Vec<_> =
1228                    (0..vector_length).map(|i| bx.const_i32(i as i32)).collect();
1229                bx.shuffle_vector(
1230                    intermediate,
1231                    bx.const_poison(intermediate_ty),
1232                    bx.const_vector(&shuffle_mask),
1233                )
1234            }
1235        }
1236        (TypeKind::Vector, TypeKind::X86_AMX) => {
1237            bx.call_intrinsic("llvm.x86.cast.vector.to.tile", &[src_ty], &[val])
1238        }
1239        (TypeKind::X86_AMX, TypeKind::Vector) => {
1240            bx.call_intrinsic("llvm.x86.cast.tile.to.vector", &[dest_ty], &[val])
1241        }
1242        _ => bx.bitcast(val, dest_ty), // for `bf16(xN)` <-> `u16(xN)`
1243    }
1244}
1245
1246fn intrinsic_fn<'ll, 'tcx>(
1247    bx: &Builder<'_, 'll, 'tcx>,
1248    name: &str,
1249    rust_return_ty: &'ll Type,
1250    rust_argument_tys: Vec<&'ll Type>,
1251    instance: ty::Instance<'tcx>,
1252) -> &'ll Value {
1253    let tcx = bx.tcx;
1254
1255    let rust_fn_ty = bx.type_func(&rust_argument_tys, rust_return_ty);
1256
1257    let intrinsic = llvm::Intrinsic::lookup(name.as_bytes());
1258
1259    if let Some(intrinsic) = intrinsic
1260        && intrinsic.is_target_specific()
1261    {
1262        let (llvm_arch, _) = name[5..].split_once('.').unwrap();
1263        let rust_arch = &tcx.sess.target.arch;
1264
1265        if let Some(correct_llvm_arch) = llvm_arch_for(rust_arch)
1266            && llvm_arch != correct_llvm_arch
1267        {
1268            tcx.dcx().emit_fatal(IntrinsicWrongArch {
1269                name,
1270                target_arch: rust_arch.desc(),
1271                span: tcx.def_span(instance.def_id()),
1272            });
1273        }
1274    }
1275
1276    if let Some(intrinsic) = intrinsic
1277        && !intrinsic.is_overloaded()
1278    {
1279        // FIXME: also do this for overloaded intrinsics
1280        let llfn = intrinsic.get_declaration(bx.llmod, &[]);
1281        let llvm_fn_ty = bx.get_type_of_global(llfn);
1282
1283        let llvm_return_ty = bx.get_return_type(llvm_fn_ty);
1284        let llvm_argument_tys = bx.func_params_types(llvm_fn_ty);
1285        let llvm_is_variadic = bx.func_is_variadic(llvm_fn_ty);
1286
1287        let is_correct_signature = !llvm_is_variadic
1288            && rust_argument_tys.len() == llvm_argument_tys.len()
1289            && iter::once((rust_return_ty, llvm_return_ty))
1290                .chain(iter::zip(rust_argument_tys, llvm_argument_tys))
1291                .all(|(rust_ty, llvm_ty)| can_autocast(bx, rust_ty, llvm_ty));
1292
1293        if !is_correct_signature {
1294            tcx.dcx().emit_fatal(IntrinsicSignatureMismatch {
1295                name,
1296                llvm_fn_ty: &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", llvm_fn_ty))
    })format!("{llvm_fn_ty:?}"),
1297                rust_fn_ty: &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", rust_fn_ty))
    })format!("{rust_fn_ty:?}"),
1298                span: tcx.def_span(instance.def_id()),
1299            });
1300        }
1301
1302        return llfn;
1303    }
1304
1305    // Function addresses in Rust are never significant, allowing functions to be merged.
1306    let llfn = declare_raw_fn(
1307        bx,
1308        name,
1309        llvm::CCallConv,
1310        llvm::UnnamedAddr::Global,
1311        llvm::Visibility::Default,
1312        rust_fn_ty,
1313    );
1314
1315    if intrinsic.is_none() {
1316        let mut new_llfn = None;
1317        let can_upgrade = unsafe { llvm::LLVMRustUpgradeIntrinsicFunction(llfn, &mut new_llfn) };
1318
1319        if !can_upgrade {
1320            // This is either plain wrong, or this can be caused by incompatible LLVM versions
1321            tcx.dcx().emit_fatal(UnknownIntrinsic { name, span: tcx.def_span(instance.def_id()) });
1322        } else if let Some(def_id) = instance.def_id().as_local() {
1323            // we can emit diagnostics only for local crates
1324            let hir_id = tcx.local_def_id_to_hir_id(def_id);
1325
1326            // not all intrinsics are upgraded to some other intrinsics, most are upgraded to instruction sequences
1327            let msg = if let Some(new_llfn) = new_llfn {
1328                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("using deprecated intrinsic `{1}`, `{0}` can be used instead",
                str::from_utf8(&llvm::get_value_name(new_llfn)).unwrap(),
                name))
    })format!(
1329                    "using deprecated intrinsic `{name}`, `{}` can be used instead",
1330                    str::from_utf8(&llvm::get_value_name(new_llfn)).unwrap()
1331                )
1332            } else {
1333                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("using deprecated intrinsic `{0}`",
                name))
    })format!("using deprecated intrinsic `{name}`")
1334            };
1335
1336            tcx.emit_node_lint(
1337                DEPRECATED_LLVM_INTRINSIC,
1338                hir_id,
1339                rustc_errors::DiagDecorator(|d| {
1340                    d.primary_message(msg).span(tcx.hir_span(hir_id));
1341                }),
1342            );
1343        }
1344    }
1345
1346    llfn
1347}
1348
1349fn catch_unwind_intrinsic<'ll, 'tcx>(
1350    bx: &mut Builder<'_, 'll, 'tcx>,
1351    try_func: &'ll Value,
1352    data: &'ll Value,
1353    catch_func: &'ll Value,
1354    dest: PlaceRef<'tcx, &'ll Value>,
1355) {
1356    if !bx.sess().panic_strategy().unwinds() {
1357        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
1358        bx.call(try_func_ty, None, None, try_func, &[data], None, None);
1359        // Return 0 unconditionally from the intrinsic call;
1360        // we can never unwind.
1361        OperandValue::Immediate(bx.const_i32(0)).store(bx, dest);
1362    } else if wants_msvc_seh(bx.sess()) {
1363        codegen_msvc_try(bx, try_func, data, catch_func, dest);
1364    } else if wants_wasm_eh(bx.sess()) {
1365        codegen_wasm_try(bx, try_func, data, catch_func, dest);
1366    } else if bx.sess().target.os == Os::Emscripten {
1367        codegen_emcc_try(bx, try_func, data, catch_func, dest);
1368    } else {
1369        codegen_gnu_try(bx, try_func, data, catch_func, dest);
1370    }
1371}
1372
1373// MSVC's definition of the `rust_try` function.
1374//
1375// This implementation uses the new exception handling instructions in LLVM
1376// which have support in LLVM for SEH on MSVC targets. Although these
1377// instructions are meant to work for all targets, as of the time of this
1378// writing, however, LLVM does not recommend the usage of these new instructions
1379// as the old ones are still more optimized.
1380fn codegen_msvc_try<'ll, 'tcx>(
1381    bx: &mut Builder<'_, 'll, 'tcx>,
1382    try_func: &'ll Value,
1383    data: &'ll Value,
1384    catch_func: &'ll Value,
1385    dest: PlaceRef<'tcx, &'ll Value>,
1386) {
1387    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
1388        bx.set_personality_fn(bx.eh_personality());
1389
1390        let normal = bx.append_sibling_block("normal");
1391        let catchswitch = bx.append_sibling_block("catchswitch");
1392        let catchpad_rust = bx.append_sibling_block("catchpad_rust");
1393        let catchpad_foreign = bx.append_sibling_block("catchpad_foreign");
1394        let caught = bx.append_sibling_block("caught");
1395
1396        let try_func = llvm::get_param(bx.llfn(), 0);
1397        let data = llvm::get_param(bx.llfn(), 1);
1398        let catch_func = llvm::get_param(bx.llfn(), 2);
1399
1400        // We're generating an IR snippet that looks like:
1401        //
1402        //   declare i32 @rust_try(%try_func, %data, %catch_func) {
1403        //      %slot = alloca i8*
1404        //      invoke %try_func(%data) to label %normal unwind label %catchswitch
1405        //
1406        //   normal:
1407        //      ret i32 0
1408        //
1409        //   catchswitch:
1410        //      %cs = catchswitch within none [%catchpad_rust, %catchpad_foreign] unwind to caller
1411        //
1412        //   catchpad_rust:
1413        //      %tok = catchpad within %cs [%type_descriptor, 8, %slot]
1414        //      %ptr = load %slot
1415        //      call %catch_func(%data, %ptr)
1416        //      catchret from %tok to label %caught
1417        //
1418        //   catchpad_foreign:
1419        //      %tok = catchpad within %cs [null, 64, null]
1420        //      call %catch_func(%data, null)
1421        //      catchret from %tok to label %caught
1422        //
1423        //   caught:
1424        //      ret i32 1
1425        //   }
1426        //
1427        // This structure follows the basic usage of throw/try/catch in LLVM.
1428        // For example, compile this C++ snippet to see what LLVM generates:
1429        //
1430        //      struct rust_panic {
1431        //          rust_panic(const rust_panic&);
1432        //          ~rust_panic();
1433        //
1434        //          void* x[2];
1435        //      };
1436        //
1437        //      int __rust_try(
1438        //          void (*try_func)(void*),
1439        //          void *data,
1440        //          void (*catch_func)(void*, void*) noexcept
1441        //      ) {
1442        //          try {
1443        //              try_func(data);
1444        //              return 0;
1445        //          } catch(rust_panic& a) {
1446        //              catch_func(data, &a);
1447        //              return 1;
1448        //          } catch(...) {
1449        //              catch_func(data, NULL);
1450        //              return 1;
1451        //          }
1452        //      }
1453        //
1454        // More information can be found in libstd's seh.rs implementation.
1455        let ptr_size = bx.tcx().data_layout.pointer_size();
1456        let ptr_align = bx.tcx().data_layout.pointer_align().abi;
1457        let slot = bx.alloca(ptr_size, ptr_align);
1458        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
1459        bx.invoke(try_func_ty, None, None, try_func, &[data], normal, catchswitch, None, None);
1460
1461        bx.switch_to_block(normal);
1462        bx.ret(bx.const_i32(0));
1463
1464        bx.switch_to_block(catchswitch);
1465        let cs = bx.catch_switch(None, None, &[catchpad_rust, catchpad_foreign]);
1466
1467        // We can't use the TypeDescriptor defined in libpanic_unwind because it
1468        // might be in another DLL and the SEH encoding only supports specifying
1469        // a TypeDescriptor from the current module.
1470        //
1471        // However this isn't an issue since the MSVC runtime uses string
1472        // comparison on the type name to match TypeDescriptors rather than
1473        // pointer equality.
1474        //
1475        // So instead we generate a new TypeDescriptor in each module that uses
1476        // `try` and let the linker merge duplicate definitions in the same
1477        // module.
1478        //
1479        // When modifying, make sure that the type_name string exactly matches
1480        // the one used in library/panic_unwind/src/seh.rs.
1481        let type_info_vtable = bx.declare_global("??_7type_info@@6B@", bx.type_ptr());
1482        let type_name = bx.const_bytes(b"rust_panic\0");
1483        let type_info =
1484            bx.const_struct(&[type_info_vtable, bx.const_null(bx.type_ptr()), type_name], false);
1485        let tydesc = bx.declare_global(
1486            &mangle_internal_symbol(bx.tcx, "__rust_panic_type_info"),
1487            bx.val_ty(type_info),
1488        );
1489
1490        llvm::set_linkage(tydesc, llvm::Linkage::LinkOnceODRLinkage);
1491        if bx.cx.tcx.sess.target.supports_comdat() {
1492            llvm::SetUniqueComdat(bx.llmod, tydesc);
1493        }
1494        llvm::set_initializer(tydesc, type_info);
1495
1496        // The flag value of 8 indicates that we are catching the exception by
1497        // reference instead of by value. We can't use catch by value because
1498        // that requires copying the exception object, which we don't support
1499        // since our exception object effectively contains a Box.
1500        //
1501        // Source: MicrosoftCXXABI::getAddrOfCXXCatchHandlerType in clang
1502        bx.switch_to_block(catchpad_rust);
1503        let flags = bx.const_i32(8);
1504        let funclet = bx.catch_pad(cs, &[tydesc, flags, slot]);
1505        let ptr = bx.load(bx.type_ptr(), slot, ptr_align);
1506        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
1507        bx.call(catch_ty, None, None, catch_func, &[data, ptr], Some(&funclet), None);
1508        bx.catch_ret(&funclet, caught);
1509
1510        // The flag value of 64 indicates a "catch-all".
1511        bx.switch_to_block(catchpad_foreign);
1512        let flags = bx.const_i32(64);
1513        let null = bx.const_null(bx.type_ptr());
1514        let funclet = bx.catch_pad(cs, &[null, flags, null]);
1515        bx.call(catch_ty, None, None, catch_func, &[data, null], Some(&funclet), None);
1516        bx.catch_ret(&funclet, caught);
1517
1518        bx.switch_to_block(caught);
1519        bx.ret(bx.const_i32(1));
1520    });
1521
1522    // Note that no invoke is used here because by definition this function
1523    // can't panic (that's what it's catching).
1524    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
1525    OperandValue::Immediate(ret).store(bx, dest);
1526}
1527
1528// WASM's definition of the `rust_try` function.
1529fn codegen_wasm_try<'ll, 'tcx>(
1530    bx: &mut Builder<'_, 'll, 'tcx>,
1531    try_func: &'ll Value,
1532    data: &'ll Value,
1533    catch_func: &'ll Value,
1534    dest: PlaceRef<'tcx, &'ll Value>,
1535) {
1536    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
1537        bx.set_personality_fn(bx.eh_personality());
1538
1539        let normal = bx.append_sibling_block("normal");
1540        let catchswitch = bx.append_sibling_block("catchswitch");
1541        let catchpad = bx.append_sibling_block("catchpad");
1542        let caught = bx.append_sibling_block("caught");
1543
1544        let try_func = llvm::get_param(bx.llfn(), 0);
1545        let data = llvm::get_param(bx.llfn(), 1);
1546        let catch_func = llvm::get_param(bx.llfn(), 2);
1547
1548        // We're generating an IR snippet that looks like:
1549        //
1550        //   declare i32 @rust_try(%try_func, %data, %catch_func) {
1551        //      %slot = alloca i8*
1552        //      invoke %try_func(%data) to label %normal unwind label %catchswitch
1553        //
1554        //   normal:
1555        //      ret i32 0
1556        //
1557        //   catchswitch:
1558        //      %cs = catchswitch within none [%catchpad] unwind to caller
1559        //
1560        //   catchpad:
1561        //      %tok = catchpad within %cs [null]
1562        //      %ptr = call @llvm.wasm.get.exception(token %tok)
1563        //      %sel = call @llvm.wasm.get.ehselector(token %tok)
1564        //      call %catch_func(%data, %ptr)
1565        //      catchret from %tok to label %caught
1566        //
1567        //   caught:
1568        //      ret i32 1
1569        //   }
1570        //
1571        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
1572        bx.invoke(try_func_ty, None, None, try_func, &[data], normal, catchswitch, None, None);
1573
1574        bx.switch_to_block(normal);
1575        bx.ret(bx.const_i32(0));
1576
1577        bx.switch_to_block(catchswitch);
1578        let cs = bx.catch_switch(None, None, &[catchpad]);
1579
1580        bx.switch_to_block(catchpad);
1581        let null = bx.const_null(bx.type_ptr());
1582        let funclet = bx.catch_pad(cs, &[null]);
1583
1584        let ptr = bx.call_intrinsic("llvm.wasm.get.exception", &[], &[funclet.cleanuppad()]);
1585        let _sel = bx.call_intrinsic("llvm.wasm.get.ehselector", &[], &[funclet.cleanuppad()]);
1586
1587        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
1588        bx.call(catch_ty, None, None, catch_func, &[data, ptr], Some(&funclet), None);
1589        bx.catch_ret(&funclet, caught);
1590
1591        bx.switch_to_block(caught);
1592        bx.ret(bx.const_i32(1));
1593    });
1594
1595    // Note that no invoke is used here because by definition this function
1596    // can't panic (that's what it's catching).
1597    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
1598    OperandValue::Immediate(ret).store(bx, dest);
1599}
1600
1601// Definition of the standard `try` function for Rust using the GNU-like model
1602// of exceptions (e.g., the normal semantics of LLVM's `landingpad` and `invoke`
1603// instructions).
1604//
1605// This codegen is a little surprising because we always call a shim
1606// function instead of inlining the call to `invoke` manually here. This is done
1607// because in LLVM we're only allowed to have one personality per function
1608// definition. The call to the `try` intrinsic is being inlined into the
1609// function calling it, and that function may already have other personality
1610// functions in play. By calling a shim we're guaranteed that our shim will have
1611// the right personality function.
1612fn codegen_gnu_try<'ll, 'tcx>(
1613    bx: &mut Builder<'_, 'll, 'tcx>,
1614    try_func: &'ll Value,
1615    data: &'ll Value,
1616    catch_func: &'ll Value,
1617    dest: PlaceRef<'tcx, &'ll Value>,
1618) {
1619    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
1620        // Codegens the shims described above:
1621        //
1622        //   bx:
1623        //      invoke %try_func(%data) normal %normal unwind %catch
1624        //
1625        //   normal:
1626        //      ret 0
1627        //
1628        //   catch:
1629        //      (%ptr, _) = landingpad
1630        //      call %catch_func(%data, %ptr)
1631        //      ret 1
1632        let then = bx.append_sibling_block("then");
1633        let catch = bx.append_sibling_block("catch");
1634
1635        let try_func = llvm::get_param(bx.llfn(), 0);
1636        let data = llvm::get_param(bx.llfn(), 1);
1637        let catch_func = llvm::get_param(bx.llfn(), 2);
1638        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
1639        bx.invoke(try_func_ty, None, None, try_func, &[data], then, catch, None, None);
1640
1641        bx.switch_to_block(then);
1642        bx.ret(bx.const_i32(0));
1643
1644        // Type indicator for the exception being thrown.
1645        //
1646        // The first value in this tuple is a pointer to the exception object
1647        // being thrown. The second value is a "selector" indicating which of
1648        // the landing pad clauses the exception's type had been matched to.
1649        // rust_try ignores the selector.
1650        bx.switch_to_block(catch);
1651        let lpad_ty = bx.type_struct(&[bx.type_ptr(), bx.type_i32()], false);
1652        let vals = bx.landing_pad(lpad_ty, bx.eh_personality(), 1);
1653        let tydesc = bx.const_null(bx.type_ptr());
1654        bx.add_clause(vals, tydesc);
1655        let ptr = bx.extract_value(vals, 0);
1656        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
1657        bx.call(catch_ty, None, None, catch_func, &[data, ptr], None, None);
1658        bx.ret(bx.const_i32(1));
1659    });
1660
1661    // Note that no invoke is used here because by definition this function
1662    // can't panic (that's what it's catching).
1663    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
1664    OperandValue::Immediate(ret).store(bx, dest);
1665}
1666
1667// Variant of codegen_gnu_try used for emscripten where Rust panics are
1668// implemented using C++ exceptions. Here we use exceptions of a specific type
1669// (`struct rust_panic`) to represent Rust panics.
1670fn codegen_emcc_try<'ll, 'tcx>(
1671    bx: &mut Builder<'_, 'll, 'tcx>,
1672    try_func: &'ll Value,
1673    data: &'ll Value,
1674    catch_func: &'ll Value,
1675    dest: PlaceRef<'tcx, &'ll Value>,
1676) {
1677    let (llty, llfn) = get_rust_try_fn(bx, &mut |mut bx| {
1678        // Codegens the shims described above:
1679        //
1680        //   bx:
1681        //      invoke %try_func(%data) normal %normal unwind %catch
1682        //
1683        //   normal:
1684        //      ret 0
1685        //
1686        //   catch:
1687        //      (%ptr, %selector) = landingpad
1688        //      %rust_typeid = @llvm.eh.typeid.for(@_ZTI10rust_panic)
1689        //      %is_rust_panic = %selector == %rust_typeid
1690        //      %catch_data = alloca { i8*, i8 }
1691        //      %catch_data[0] = %ptr
1692        //      %catch_data[1] = %is_rust_panic
1693        //      call %catch_func(%data, %catch_data)
1694        //      ret 1
1695        let then = bx.append_sibling_block("then");
1696        let catch = bx.append_sibling_block("catch");
1697
1698        let try_func = llvm::get_param(bx.llfn(), 0);
1699        let data = llvm::get_param(bx.llfn(), 1);
1700        let catch_func = llvm::get_param(bx.llfn(), 2);
1701        let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
1702        bx.invoke(try_func_ty, None, None, try_func, &[data], then, catch, None, None);
1703
1704        bx.switch_to_block(then);
1705        bx.ret(bx.const_i32(0));
1706
1707        // Type indicator for the exception being thrown.
1708        //
1709        // The first value in this tuple is a pointer to the exception object
1710        // being thrown. The second value is a "selector" indicating which of
1711        // the landing pad clauses the exception's type had been matched to.
1712        bx.switch_to_block(catch);
1713        let tydesc = bx.eh_catch_typeinfo();
1714        let lpad_ty = bx.type_struct(&[bx.type_ptr(), bx.type_i32()], false);
1715        let vals = bx.landing_pad(lpad_ty, bx.eh_personality(), 2);
1716        bx.add_clause(vals, tydesc);
1717        bx.add_clause(vals, bx.const_null(bx.type_ptr()));
1718        let ptr = bx.extract_value(vals, 0);
1719        let selector = bx.extract_value(vals, 1);
1720
1721        // Check if the typeid we got is the one for a Rust panic.
1722        let rust_typeid = bx.call_intrinsic("llvm.eh.typeid.for", &[bx.val_ty(tydesc)], &[tydesc]);
1723        let is_rust_panic = bx.icmp(IntPredicate::IntEQ, selector, rust_typeid);
1724        let is_rust_panic = bx.zext(is_rust_panic, bx.type_bool());
1725
1726        // We need to pass two values to catch_func (ptr and is_rust_panic), so
1727        // create an alloca and pass a pointer to that.
1728        let ptr_size = bx.tcx().data_layout.pointer_size();
1729        let ptr_align = bx.tcx().data_layout.pointer_align().abi;
1730        let i8_align = bx.tcx().data_layout.i8_align;
1731        // Required in order for there to be no padding between the fields.
1732        if !(i8_align <= ptr_align) {
    ::core::panicking::panic("assertion failed: i8_align <= ptr_align")
};assert!(i8_align <= ptr_align);
1733        let catch_data = bx.alloca(2 * ptr_size, ptr_align);
1734        bx.store(ptr, catch_data, ptr_align);
1735        let catch_data_1 = bx.inbounds_ptradd(catch_data, bx.const_usize(ptr_size.bytes()));
1736        bx.store(is_rust_panic, catch_data_1, i8_align);
1737
1738        let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
1739        bx.call(catch_ty, None, None, catch_func, &[data, catch_data], None, None);
1740        bx.ret(bx.const_i32(1));
1741    });
1742
1743    // Note that no invoke is used here because by definition this function
1744    // can't panic (that's what it's catching).
1745    let ret = bx.call(llty, None, None, llfn, &[try_func, data, catch_func], None, None);
1746    OperandValue::Immediate(ret).store(bx, dest);
1747}
1748
1749// Helper function to give a Block to a closure to codegen a shim function.
1750// This is currently primarily used for the `try` intrinsic functions above.
1751fn gen_fn<'a, 'll, 'tcx>(
1752    cx: &'a CodegenCx<'ll, 'tcx>,
1753    name: &str,
1754    rust_fn_sig: ty::PolyFnSig<'tcx>,
1755    codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>),
1756) -> (&'ll Type, &'ll Value) {
1757    let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty());
1758    let llty = fn_abi.llvm_type(cx);
1759    let llfn = cx.declare_fn(name, fn_abi, None);
1760    cx.set_frame_pointer_type(llfn);
1761    cx.apply_target_cpu_attr(llfn);
1762    // FIXME(eddyb) find a nicer way to do this.
1763    llvm::set_linkage(llfn, llvm::Linkage::InternalLinkage);
1764    let llbb = Builder::append_block(cx, llfn, "entry-block");
1765    let bx = Builder::build(cx, llbb);
1766    codegen(bx);
1767    (llty, llfn)
1768}
1769
1770// Helper function used to get a handle to the `__rust_try` function used to
1771// catch exceptions.
1772//
1773// This function is only generated once and is then cached.
1774fn get_rust_try_fn<'a, 'll, 'tcx>(
1775    cx: &'a CodegenCx<'ll, 'tcx>,
1776    codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>),
1777) -> (&'ll Type, &'ll Value) {
1778    if let Some(llfn) = cx.rust_try_fn.get() {
1779        return llfn;
1780    }
1781
1782    // Define the type up front for the signature of the rust_try function.
1783    let tcx = cx.tcx;
1784    let i8p = Ty::new_mut_ptr(tcx, tcx.types.i8);
1785    // `unsafe fn(*mut i8) -> ()`
1786    let try_fn_ty = Ty::new_fn_ptr(
1787        tcx,
1788        ty::Binder::dummy(tcx.mk_fn_sig_rust_abi([i8p], tcx.types.unit, hir::Safety::Unsafe)),
1789    );
1790    // `unsafe fn(*mut i8, *mut i8) -> ()`
1791    let catch_fn_ty = Ty::new_fn_ptr(
1792        tcx,
1793        ty::Binder::dummy(tcx.mk_fn_sig_rust_abi([i8p, i8p], tcx.types.unit, hir::Safety::Unsafe)),
1794    );
1795    // `unsafe fn(unsafe fn(*mut i8) -> (), *mut i8, unsafe fn(*mut i8, *mut i8) -> ()) -> i32`
1796    let rust_fn_sig = ty::Binder::dummy(cx.tcx.mk_fn_sig_rust_abi(
1797        [try_fn_ty, i8p, catch_fn_ty],
1798        tcx.types.i32,
1799        hir::Safety::Unsafe,
1800    ));
1801    let rust_try = gen_fn(cx, "__rust_try", rust_fn_sig, codegen);
1802    cx.rust_try_fn.set(Some(rust_try));
1803    rust_try
1804}
1805
1806fn codegen_retag_inner<'ll, 'tcx>(
1807    bx: &mut Builder<'_, 'll, 'tcx>,
1808    name: &'static str,
1809    ptr: &'ll Value,
1810    info: &RetagInfo<&'ll Value>,
1811) -> &'ll Value {
1812    let size = bx.const_usize(info.size.bytes());
1813    let perms = bx.const_u8(info.flags.bits());
1814
1815    bx.call_intrinsic(
1816        name,
1817        // Retag intrinsics have special handling within `CodegenCx::declare_intrinsic`
1818        // to ensure that each form has the correct return type.
1819        &[bx.type_ptr(), bx.val_ty(size), bx.type_i8(), bx.type_ptr(), bx.type_ptr()],
1820        &[ptr, size, perms, info.im_layout, info.pin_layout],
1821    )
1822}
1823
1824fn codegen_autodiff<'ll, 'tcx>(
1825    bx: &mut Builder<'_, 'll, 'tcx>,
1826    tcx: TyCtxt<'tcx>,
1827    instance: ty::Instance<'tcx>,
1828    args: &[OperandRef<'tcx, &'ll Value>],
1829    result: PlaceRef<'tcx, &'ll Value>,
1830) {
1831    if !tcx.sess.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable) {
1832        let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutEnable);
1833    }
1834
1835    let ct = tcx.crate_types();
1836    let lto = tcx.sess.lto();
1837    if ct.len() == 1 && ct.contains(&CrateType::Executable) {
1838        if lto != rustc_session::config::Lto::Fat {
1839            let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
1840        }
1841    } else {
1842        if lto != rustc_session::config::Lto::Fat && !tcx.sess.opts.cg.linker_plugin_lto.enabled() {
1843            let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
1844        }
1845    }
1846
1847    let fn_args = instance.args;
1848    let callee_ty = instance.ty(tcx, bx.typing_env());
1849
1850    let sig = callee_ty.fn_sig(tcx).skip_binder();
1851
1852    let ret_ty = sig.output();
1853    let llret_ty = bx.layout_of(ret_ty).llvm_type(bx);
1854
1855    let source_fn_ptr_ty = fn_args.into_type_list(tcx)[0];
1856    let fn_to_diff = args[0].immediate();
1857
1858    let (diff_id, diff_args) = match fn_args.into_type_list(tcx)[1].kind() {
1859        ty::FnDef(def_id, diff_args) => (def_id, diff_args),
1860        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("invalid args"))bug!("invalid args"),
1861    };
1862
1863    let fn_diff = match Instance::try_resolve(tcx, bx.cx.typing_env(), *diff_id, diff_args) {
1864        Ok(Some(instance)) => instance,
1865        Ok(None) => ::rustc_middle::util::bug::bug_fmt(format_args!("could not resolve ({0:?}, {1:?}) to a specific autodiff instance",
        diff_id, diff_args))bug!(
1866            "could not resolve ({:?}, {:?}) to a specific autodiff instance",
1867            diff_id,
1868            diff_args
1869        ),
1870        Err(_) => {
1871            // An error has already been emitted
1872            return;
1873        }
1874    };
1875
1876    let val_arr = get_args_from_tuple(bx, args[2], fn_diff);
1877    let diff_symbol = symbol_name_for_instance_in_crate(tcx, fn_diff.clone(), LOCAL_CRATE);
1878
1879    let Some(Some(mut diff_attrs)) =
1880        {
    {
        'done:
            {
            for i in
                ::rustc_hir::attrs::HasAttrs::get_attrs(fn_diff.def_id(),
                    &tcx) {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(RustcAutodiff(attr)) => {
                        break 'done Some(attr.clone());
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }
}find_attr!(tcx, fn_diff.def_id(), RustcAutodiff(attr) => attr.clone())
1881    else {
1882        ::rustc_middle::util::bug::bug_fmt(format_args!("could not find autodiff attrs"))bug!("could not find autodiff attrs")
1883    };
1884
1885    adjust_activity_to_abi(
1886        tcx,
1887        source_fn_ptr_ty,
1888        TypingEnv::fully_monomorphized(),
1889        &mut diff_attrs.input_activity,
1890    );
1891
1892    let fnc_tree = rustc_middle::ty::fnc_typetrees(tcx, source_fn_ptr_ty);
1893
1894    // Build body
1895    generate_enzyme_call(
1896        bx,
1897        bx.cx,
1898        fn_to_diff,
1899        &diff_symbol,
1900        llret_ty,
1901        &val_arr,
1902        &diff_attrs,
1903        result,
1904        fnc_tree,
1905    );
1906}
1907
1908// Generates the LLVM code to offload a Rust function to a target device (e.g., GPU).
1909// For each kernel call, it generates the necessary globals (including metadata such as
1910// size and pass mode), manages memory mapping to and from the device, handles all
1911// data transfers, and launches the kernel on the target device.
1912fn codegen_offload<'ll, 'tcx>(
1913    bx: &mut Builder<'_, 'll, 'tcx>,
1914    tcx: TyCtxt<'tcx>,
1915    instance: ty::Instance<'tcx>,
1916    args: &[OperandRef<'tcx, &'ll Value>],
1917) {
1918    let cx = bx.cx;
1919    let fn_args = instance.args;
1920
1921    let (target_id, target_args) = match fn_args.into_type_list(tcx)[0].kind() {
1922        ty::FnDef(def_id, params) => (def_id, params),
1923        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("invalid offload intrinsic arg"))bug!("invalid offload intrinsic arg"),
1924    };
1925
1926    let fn_target = match Instance::try_resolve(tcx, cx.typing_env(), *target_id, target_args) {
1927        Ok(Some(instance)) => instance,
1928        Ok(None) => ::rustc_middle::util::bug::bug_fmt(format_args!("could not resolve ({0:?}, {1:?}) to a specific offload instance",
        target_id, target_args))bug!(
1929            "could not resolve ({:?}, {:?}) to a specific offload instance",
1930            target_id,
1931            target_args
1932        ),
1933        Err(_) => {
1934            // An error has already been emitted
1935            return;
1936        }
1937    };
1938
1939    let offload_dims = OffloadKernelDims::from_operands(bx, &args[1], &args[2]);
1940    let args = get_args_from_tuple(bx, args[3], fn_target);
1941    let target_symbol = symbol_name_for_instance_in_crate(tcx, fn_target, LOCAL_CRATE);
1942
1943    let sig = tcx.fn_sig(fn_target.def_id()).skip_binder();
1944    let sig = tcx.instantiate_bound_regions_with_erased(sig);
1945    let inputs = sig.inputs();
1946
1947    let fn_abi = cx.fn_abi_of_instance(fn_target, ty::List::empty());
1948
1949    let mut metadata = Vec::new();
1950    let mut types = Vec::new();
1951
1952    for (i, arg_abi) in fn_abi.args.iter().enumerate() {
1953        let ty = inputs[i];
1954        let decomposed = OffloadMetadata::handle_abi(cx, tcx, ty, arg_abi);
1955
1956        for (meta, entry_ty) in decomposed {
1957            metadata.push(meta);
1958            types.push(bx.cx.layout_of(entry_ty).llvm_type(bx.cx));
1959        }
1960    }
1961
1962    let offload_globals_ref = cx.offload_globals.borrow();
1963    let offload_globals = match offload_globals_ref.as_ref() {
1964        Some(globals) => globals,
1965        None => {
1966            // Offload is not initialized, cannot continue
1967            return;
1968        }
1969    };
1970    register_offload(cx);
1971    let offload_data = gen_define_handling(&cx, &metadata, target_symbol, offload_globals);
1972    gen_call_handling(bx, &offload_data, &args, &types, &metadata, offload_globals, &offload_dims);
1973}
1974
1975fn get_args_from_tuple<'ll, 'tcx>(
1976    bx: &mut Builder<'_, 'll, 'tcx>,
1977    tuple_op: OperandRef<'tcx, &'ll Value>,
1978    fn_instance: Instance<'tcx>,
1979) -> Vec<&'ll Value> {
1980    let cx = bx.cx;
1981    let fn_abi = cx.fn_abi_of_instance(fn_instance, ty::List::empty());
1982
1983    match tuple_op.val {
1984        OperandValue::Immediate(val) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [val]))vec![val],
1985        OperandValue::Pair(v1, v2) => ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [v1, v2]))vec![v1, v2],
1986        OperandValue::Ref(ptr) => {
1987            let tuple_place = PlaceRef { val: ptr, layout: tuple_op.layout };
1988
1989            let mut result = Vec::with_capacity(fn_abi.args.len());
1990            let mut tuple_index = 0;
1991
1992            for arg in &fn_abi.args {
1993                match arg.mode {
1994                    PassMode::Ignore => {}
1995                    PassMode::Direct(_) | PassMode::Cast { .. } => {
1996                        let field = tuple_place.project_field(bx, tuple_index);
1997                        let llvm_ty = field.layout.llvm_type(bx.cx);
1998                        let val = bx.load(llvm_ty, field.val.llval, field.val.align);
1999                        result.push(val);
2000                        tuple_index += 1;
2001                    }
2002                    PassMode::Pair(_, _) => {
2003                        let field = tuple_place.project_field(bx, tuple_index);
2004                        let llvm_ty = field.layout.llvm_type(bx.cx);
2005                        let pair_val = bx.load(llvm_ty, field.val.llval, field.val.align);
2006                        result.push(bx.extract_value(pair_val, 0));
2007                        result.push(bx.extract_value(pair_val, 1));
2008                        tuple_index += 1;
2009                    }
2010                    PassMode::Indirect { .. } => {
2011                        let field = tuple_place.project_field(bx, tuple_index);
2012                        result.push(field.val.llval);
2013                        tuple_index += 1;
2014                    }
2015                }
2016            }
2017
2018            result
2019        }
2020
2021        OperandValue::ZeroSized => ::alloc::vec::Vec::new()vec![],
2022    }
2023}
2024
2025fn generic_simd_intrinsic<'ll, 'tcx>(
2026    bx: &mut Builder<'_, 'll, 'tcx>,
2027    name: Symbol,
2028    fn_args: GenericArgsRef<'tcx>,
2029    args: &[OperandRef<'tcx, &'ll Value>],
2030    ret_ty: Ty<'tcx>,
2031    llret_ty: &'ll Type,
2032    span: Span,
2033) -> Result<&'ll Value, ErrorGuaranteed> {
2034    macro_rules! return_error {
2035        ($diag: expr) => {{
2036            let err = bx.sess().dcx().emit_err($diag);
2037            return Err(err);
2038        }};
2039    }
2040
2041    macro_rules! require {
2042        ($cond: expr, $diag: expr) => {
2043            if !$cond {
2044                return_error!($diag);
2045            }
2046        };
2047    }
2048
2049    macro_rules! require_simd {
2050        ($ty: expr, $variant:ident) => {{
2051            require!($ty.is_simd(), InvalidMonomorphization::$variant { span, name, ty: $ty });
2052            $ty.simd_size_and_type(bx.tcx())
2053        }};
2054    }
2055
2056    macro_rules! require_simd_or_scalable {
2057        ($ty: expr, $variant:ident) => {{
2058            require!(
2059                $ty.is_simd() || $ty.is_scalable_vector(),
2060                InvalidMonomorphization::$variant { span, name, ty: $ty }
2061            );
2062            if $ty.is_simd() {
2063                let (len, ty) = $ty.simd_size_and_type(bx.tcx());
2064                (len, ty, None)
2065            } else {
2066                let (count, ty, num_vecs) =
2067                    $ty.scalable_vector_parts(bx.tcx()).expect("`is_scalable_vector` was wrong");
2068                (count as u64, ty, Some(num_vecs))
2069            }
2070        }};
2071    }
2072
2073    /// Returns the bitwidth of the `$ty` argument if it is an `Int` or `Uint` type.
2074    macro_rules! require_int_or_uint_ty {
2075        ($ty: expr, $diag: expr) => {
2076            match $ty {
2077                ty::Int(i) => {
2078                    i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
2079                }
2080                ty::Uint(i) => {
2081                    i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
2082                }
2083                _ => {
2084                    return_error!($diag);
2085                }
2086            }
2087        };
2088    }
2089
2090    let llvm_version = crate::llvm_util::get_version();
2091
2092    /// Converts a vector mask, where each element has a bit width equal to the data elements it is used with,
2093    /// down to an i1 based mask that can be used by llvm intrinsics.
2094    ///
2095    /// The rust simd semantics are that each element should either consist of all ones or all zeroes,
2096    /// but this information is not available to llvm. Truncating the vector effectively uses the lowest bit,
2097    /// but codegen for several targets is better if we consider the highest bit by shifting.
2098    ///
2099    /// For x86 SSE/AVX targets this is beneficial since most instructions with mask parameters only consider the highest bit.
2100    /// So even though on llvm level we have an additional shift, in the final assembly there is no shift or truncate and
2101    /// instead the mask can be used as is.
2102    ///
2103    /// For aarch64 and other targets there is a benefit because a mask from the sign bit can be more
2104    /// efficiently converted to an all ones / all zeroes mask by comparing whether each element is negative.
2105    fn vector_mask_to_bitmask<'a, 'll, 'tcx>(
2106        bx: &mut Builder<'a, 'll, 'tcx>,
2107        i_xn: &'ll Value,
2108        in_elem_bitwidth: u64,
2109        in_len: u64,
2110    ) -> &'ll Value {
2111        // Shift the MSB to the right by "in_elem_bitwidth - 1" into the first bit position.
2112        let shift_idx = bx.cx.const_int(bx.type_ix(in_elem_bitwidth), (in_elem_bitwidth - 1) as _);
2113        let shift_indices = ::alloc::vec::from_elem(shift_idx, in_len as _)vec![shift_idx; in_len as _];
2114        let i_xn_msb = bx.lshr(i_xn, bx.const_vector(shift_indices.as_slice()));
2115        // Truncate vector to an <i1 x N>
2116        bx.trunc(i_xn_msb, bx.type_vector(bx.type_i1(), in_len))
2117    }
2118
2119    // Sanity-check: all vector arguments must be immediates.
2120    if truecfg!(debug_assertions) {
2121        for arg in args {
2122            if arg.layout.ty.is_simd() {
2123                {
    match arg.val {
        OperandValue::Immediate(_) => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "OperandValue::Immediate(_)", ::core::option::Option::None);
        }
    }
};assert_matches!(arg.val, OperandValue::Immediate(_));
2124            }
2125        }
2126    }
2127
2128    if name == sym::simd_select_bitmask {
2129        let (len, _) = {
    if !args[1].layout.ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdArgument {
                        span,
                        name,
                        ty: args[1].layout.ty,
                    });
            return Err(err);
        };
    };
    args[1].layout.ty.simd_size_and_type(bx.tcx())
}require_simd!(args[1].layout.ty, SimdArgument);
2130
2131        let expected_int_bits = len.max(8).next_power_of_two();
2132        let expected_bytes = len.div_ceil(8);
2133
2134        let mask_ty = args[0].layout.ty;
2135        let mask = match mask_ty.kind() {
2136            ty::Int(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
2137            ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
2138            ty::Array(elem, len)
2139                if #[allow(non_exhaustive_omitted_patterns)] match elem.kind() {
    ty::Uint(ty::UintTy::U8) => true,
    _ => false,
}matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
2140                    && len
2141                        .try_to_target_usize(bx.tcx)
2142                        .expect("expected monomorphic const in codegen")
2143                        == expected_bytes =>
2144            {
2145                let place = PlaceRef::alloca(bx, args[0].layout);
2146                args[0].val.store(bx, place);
2147                let int_ty = bx.type_ix(expected_bytes * 8);
2148                bx.load(int_ty, place.val.llval, Align::ONE)
2149            }
2150            _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::InvalidBitmask {
                span,
                name,
                mask_ty,
                expected_int_bits,
                expected_bytes,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::InvalidBitmask {
2151                span,
2152                name,
2153                mask_ty,
2154                expected_int_bits,
2155                expected_bytes
2156            }),
2157        };
2158
2159        let i1 = bx.type_i1();
2160        let im = bx.type_ix(len);
2161        let i1xn = bx.type_vector(i1, len);
2162        let m_im = bx.trunc(mask, im);
2163        let m_i1s = bx.bitcast(m_im, i1xn);
2164        return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
2165    }
2166
2167    if name == sym::simd_splat {
2168        let (out_len, out_ty) = {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
}require_simd!(ret_ty, SimdReturn);
2169
2170        if !(args[0].layout.ty == out_ty) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedVectorElementType {
                    span,
                    name,
                    expected_element: out_ty,
                    vector_type: ret_ty,
                });
        return Err(err);
    };
};require!(
2171            args[0].layout.ty == out_ty,
2172            InvalidMonomorphization::ExpectedVectorElementType {
2173                span,
2174                name,
2175                expected_element: out_ty,
2176                vector_type: ret_ty,
2177            }
2178        );
2179
2180        // `insertelement <N x elem> poison, elem %x, i32 0`
2181        let poison_vec = bx.const_poison(llret_ty);
2182        let idx0 = bx.const_i32(0);
2183        let v0 = bx.insert_element(poison_vec, args[0].immediate(), idx0);
2184
2185        // `shufflevector <N x elem> v0, <N x elem> poison, <N x i32> zeroinitializer`
2186        // The masks is all zeros, so this splats lane 0 (which has our element in it).
2187        let mask_ty = bx.type_vector(bx.type_i32(), out_len);
2188        let splat = bx.shuffle_vector(v0, poison_vec, bx.const_null(mask_ty));
2189
2190        return Ok(splat);
2191    }
2192
2193    let supports_scalable = match name {
2194        sym::simd_cast | sym::simd_select => true,
2195        _ => false,
2196    };
2197
2198    // Every intrinsic below takes a SIMD vector as its first argument. Some intrinsics also accept
2199    // scalable vectors. `require_simd_or_scalable` is used regardless as it'll do the right thing
2200    // for non-scalable vectors, and an additional check to prohibit scalable vectors for those
2201    // intrinsics that do not support them is added.
2202    if !supports_scalable {
2203        let _ = {
    if !args[0].layout.ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdInput {
                        span,
                        name,
                        ty: args[0].layout.ty,
                    });
            return Err(err);
        };
    };
    args[0].layout.ty.simd_size_and_type(bx.tcx())
}require_simd!(args[0].layout.ty, SimdInput);
2204    }
2205    let (in_len, in_elem, in_num_vecs) = {
    if !(args[0].layout.ty.is_simd() ||
                args[0].layout.ty.is_scalable_vector()) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdInput {
                        span,
                        name,
                        ty: args[0].layout.ty,
                    });
            return Err(err);
        };
    };
    if args[0].layout.ty.is_simd() {
        let (len, ty) = args[0].layout.ty.simd_size_and_type(bx.tcx());
        (len, ty, None)
    } else {
        let (count, ty, num_vecs) =
            args[0].layout.ty.scalable_vector_parts(bx.tcx()).expect("`is_scalable_vector` was wrong");
        (count as u64, ty, Some(num_vecs))
    }
}require_simd_or_scalable!(args[0].layout.ty, SimdInput);
2206    let in_ty = args[0].layout.ty;
2207
2208    let comparison = match name {
2209        sym::simd_eq => Some(BinOp::Eq),
2210        sym::simd_ne => Some(BinOp::Ne),
2211        sym::simd_lt => Some(BinOp::Lt),
2212        sym::simd_le => Some(BinOp::Le),
2213        sym::simd_gt => Some(BinOp::Gt),
2214        sym::simd_ge => Some(BinOp::Ge),
2215        _ => None,
2216    };
2217
2218    if let Some(cmp_op) = comparison {
2219        let (out_len, out_ty) = {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
}require_simd!(ret_ty, SimdReturn);
2220
2221        if !(in_len == out_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnLengthInputType {
                    span,
                    name,
                    in_len,
                    in_ty,
                    ret_ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
2222            in_len == out_len,
2223            InvalidMonomorphization::ReturnLengthInputType {
2224                span,
2225                name,
2226                in_len,
2227                in_ty,
2228                ret_ty,
2229                out_len
2230            }
2231        );
2232        if !(bx.type_kind(bx.element_type(llret_ty)) == TypeKind::Integer) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnIntegerType {
                    span,
                    name,
                    ret_ty,
                    out_ty,
                });
        return Err(err);
    };
};require!(
2233            bx.type_kind(bx.element_type(llret_ty)) == TypeKind::Integer,
2234            InvalidMonomorphization::ReturnIntegerType { span, name, ret_ty, out_ty }
2235        );
2236
2237        return Ok(compare_simd_types(
2238            bx,
2239            args[0].immediate(),
2240            args[1].immediate(),
2241            in_elem,
2242            llret_ty,
2243            cmp_op,
2244        ));
2245    }
2246
2247    if name == sym::simd_shuffle_const_generic {
2248        let idx = fn_args[2].expect_const().to_branch();
2249        let n = idx.len() as u64;
2250
2251        let (out_len, out_ty) = {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
}require_simd!(ret_ty, SimdReturn);
2252        if !(out_len == n) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnLength {
                    span,
                    name,
                    in_len: n,
                    ret_ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
2253            out_len == n,
2254            InvalidMonomorphization::ReturnLength { span, name, in_len: n, ret_ty, out_len }
2255        );
2256        if !(in_elem == out_ty) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnElement {
                    span,
                    name,
                    in_elem,
                    in_ty,
                    ret_ty,
                    out_ty,
                });
        return Err(err);
    };
};require!(
2257            in_elem == out_ty,
2258            InvalidMonomorphization::ReturnElement { span, name, in_elem, in_ty, ret_ty, out_ty }
2259        );
2260
2261        let total_len = in_len * 2;
2262
2263        let indices: Option<Vec<_>> = idx
2264            .iter()
2265            .enumerate()
2266            .map(|(arg_idx, val)| {
2267                let idx = val.to_leaf().to_i32();
2268                if idx >= i32::try_from(total_len).unwrap() {
2269                    bx.sess().dcx().emit_err(InvalidMonomorphization::SimdIndexOutOfBounds {
2270                        span,
2271                        name,
2272                        arg_idx: arg_idx as u64,
2273                        total_len: total_len.into(),
2274                    });
2275                    None
2276                } else {
2277                    Some(bx.const_i32(idx))
2278                }
2279            })
2280            .collect();
2281        let Some(indices) = indices else {
2282            return Ok(bx.const_null(llret_ty));
2283        };
2284
2285        return Ok(bx.shuffle_vector(
2286            args[0].immediate(),
2287            args[1].immediate(),
2288            bx.const_vector(&indices),
2289        ));
2290    }
2291
2292    if name == sym::simd_shuffle {
2293        // Make sure this is actually a SIMD vector.
2294        let idx_ty = args[2].layout.ty;
2295        let n: u64 = if idx_ty.is_simd()
2296            && #[allow(non_exhaustive_omitted_patterns)] match idx_ty.simd_size_and_type(bx.cx.tcx).1.kind()
    {
    ty::Uint(ty::UintTy::U32) => true,
    _ => false,
}matches!(idx_ty.simd_size_and_type(bx.cx.tcx).1.kind(), ty::Uint(ty::UintTy::U32))
2297        {
2298            idx_ty.simd_size_and_type(bx.cx.tcx).0
2299        } else {
2300            {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::SimdShuffle {
                span,
                name,
                ty: idx_ty,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::SimdShuffle { span, name, ty: idx_ty })
2301        };
2302
2303        let (out_len, out_ty) = {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
}require_simd!(ret_ty, SimdReturn);
2304        if !(out_len == n) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnLength {
                    span,
                    name,
                    in_len: n,
                    ret_ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
2305            out_len == n,
2306            InvalidMonomorphization::ReturnLength { span, name, in_len: n, ret_ty, out_len }
2307        );
2308        if !(in_elem == out_ty) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnElement {
                    span,
                    name,
                    in_elem,
                    in_ty,
                    ret_ty,
                    out_ty,
                });
        return Err(err);
    };
};require!(
2309            in_elem == out_ty,
2310            InvalidMonomorphization::ReturnElement { span, name, in_elem, in_ty, ret_ty, out_ty }
2311        );
2312
2313        let total_len = u128::from(in_len) * 2;
2314
2315        // Check that the indices are in-bounds.
2316        let indices = args[2].immediate();
2317        for i in 0..n {
2318            let val = bx.const_get_elt(indices, i as u64);
2319            let idx = bx
2320                .const_to_opt_u128(val, true)
2321                .unwrap_or_else(|| ::rustc_middle::util::bug::bug_fmt(format_args!("typeck should have already ensured that these are const"))bug!("typeck should have already ensured that these are const"));
2322            if idx >= total_len {
2323                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::SimdIndexOutOfBounds {
                span,
                name,
                arg_idx: i,
                total_len,
            });
    return Err(err);
};return_error!(InvalidMonomorphization::SimdIndexOutOfBounds {
2324                    span,
2325                    name,
2326                    arg_idx: i,
2327                    total_len,
2328                });
2329            }
2330        }
2331
2332        return Ok(bx.shuffle_vector(args[0].immediate(), args[1].immediate(), indices));
2333    }
2334
2335    if name == sym::simd_insert || name == sym::simd_insert_dyn {
2336        if !(in_elem == args[2].layout.ty) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::InsertedType {
                    span,
                    name,
                    in_elem,
                    in_ty,
                    out_ty: args[2].layout.ty,
                });
        return Err(err);
    };
};require!(
2337            in_elem == args[2].layout.ty,
2338            InvalidMonomorphization::InsertedType {
2339                span,
2340                name,
2341                in_elem,
2342                in_ty,
2343                out_ty: args[2].layout.ty
2344            }
2345        );
2346
2347        let index_imm = if name == sym::simd_insert {
2348            let idx = bx
2349                .const_to_opt_u128(args[1].immediate(), false)
2350                .expect("typeck should have ensure that this is a const");
2351            if idx >= in_len.into() {
2352                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::SimdIndexOutOfBounds {
                span,
                name,
                arg_idx: 1,
                total_len: in_len.into(),
            });
    return Err(err);
};return_error!(InvalidMonomorphization::SimdIndexOutOfBounds {
2353                    span,
2354                    name,
2355                    arg_idx: 1,
2356                    total_len: in_len.into(),
2357                });
2358            }
2359            bx.const_i32(idx as i32)
2360        } else {
2361            args[1].immediate()
2362        };
2363
2364        return Ok(bx.insert_element(args[0].immediate(), args[2].immediate(), index_imm));
2365    }
2366    if name == sym::simd_extract || name == sym::simd_extract_dyn {
2367        if !(ret_ty == in_elem) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                    span,
                    name,
                    in_elem,
                    in_ty,
                    ret_ty,
                });
        return Err(err);
    };
};require!(
2368            ret_ty == in_elem,
2369            InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
2370        );
2371        let index_imm = if name == sym::simd_extract {
2372            let idx = bx
2373                .const_to_opt_u128(args[1].immediate(), false)
2374                .expect("typeck should have ensure that this is a const");
2375            if idx >= in_len.into() {
2376                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::SimdIndexOutOfBounds {
                span,
                name,
                arg_idx: 1,
                total_len: in_len.into(),
            });
    return Err(err);
};return_error!(InvalidMonomorphization::SimdIndexOutOfBounds {
2377                    span,
2378                    name,
2379                    arg_idx: 1,
2380                    total_len: in_len.into(),
2381                });
2382            }
2383            bx.const_i32(idx as i32)
2384        } else {
2385            args[1].immediate()
2386        };
2387
2388        return Ok(bx.extract_element(args[0].immediate(), index_imm));
2389    }
2390
2391    if name == sym::simd_select {
2392        let m_elem_ty = in_elem;
2393        let m_len = in_len;
2394        let (v_len, _, _) = {
    if !(args[1].layout.ty.is_simd() ||
                args[1].layout.ty.is_scalable_vector()) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdArgument {
                        span,
                        name,
                        ty: args[1].layout.ty,
                    });
            return Err(err);
        };
    };
    if args[1].layout.ty.is_simd() {
        let (len, ty) = args[1].layout.ty.simd_size_and_type(bx.tcx());
        (len, ty, None)
    } else {
        let (count, ty, num_vecs) =
            args[1].layout.ty.scalable_vector_parts(bx.tcx()).expect("`is_scalable_vector` was wrong");
        (count as u64, ty, Some(num_vecs))
    }
}require_simd_or_scalable!(args[1].layout.ty, SimdArgument);
2395        if !(m_len == v_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::MismatchedLengths {
                    span,
                    name,
                    m_len,
                    v_len,
                });
        return Err(err);
    };
};require!(
2396            m_len == v_len,
2397            InvalidMonomorphization::MismatchedLengths { span, name, m_len, v_len }
2398        );
2399
2400        let m_i1s = if args[1].layout.ty.is_scalable_vector() {
2401            match m_elem_ty.kind() {
2402                ty::Bool => {}
2403                _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::MaskWrongElementType {
                span,
                name,
                ty: m_elem_ty,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::MaskWrongElementType {
2404                    span,
2405                    name,
2406                    ty: m_elem_ty
2407                }),
2408            };
2409            let i1 = bx.type_i1();
2410            let i1xn = bx.type_scalable_vector(i1, m_len as u64);
2411            bx.trunc(args[0].immediate(), i1xn)
2412        } else {
2413            let in_elem_bitwidth = match m_elem_ty.kind() {
    ty::Int(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    ty::Uint(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    _ => {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::MaskWrongElementType {
                        span,
                        name,
                        ty: m_elem_ty,
                    });
            return Err(err);
        };
    }
}require_int_or_uint_ty!(
2414                m_elem_ty.kind(),
2415                InvalidMonomorphization::MaskWrongElementType { span, name, ty: m_elem_ty }
2416            );
2417            vector_mask_to_bitmask(bx, args[0].immediate(), in_elem_bitwidth, m_len)
2418        };
2419
2420        return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
2421    }
2422
2423    if name == sym::simd_bitmask {
2424        // The `fn simd_bitmask(vector) -> unsigned integer` intrinsic takes a vector mask and
2425        // returns one bit for each lane (which must all be `0` or `!0`) in the form of either:
2426        // * an unsigned integer
2427        // * an array of `u8`
2428        // If the vector has less than 8 lanes, a u8 is returned with zeroed trailing bits.
2429        //
2430        // The bit order of the result depends on the byte endianness, LSB-first for little
2431        // endian and MSB-first for big endian.
2432        let expected_int_bits = in_len.max(8).next_power_of_two();
2433        let expected_bytes = in_len.div_ceil(8);
2434
2435        // Integer vector <i{in_bitwidth} x in_len>:
2436        let in_elem_bitwidth = match in_elem.kind() {
    ty::Int(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    ty::Uint(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    _ => {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::MaskWrongElementType {
                        span,
                        name,
                        ty: in_elem,
                    });
            return Err(err);
        };
    }
}require_int_or_uint_ty!(
2437            in_elem.kind(),
2438            InvalidMonomorphization::MaskWrongElementType { span, name, ty: in_elem }
2439        );
2440
2441        let i1xn = vector_mask_to_bitmask(bx, args[0].immediate(), in_elem_bitwidth, in_len);
2442        // Bitcast <i1 x N> to iN:
2443        let i_ = bx.bitcast(i1xn, bx.type_ix(in_len));
2444
2445        match ret_ty.kind() {
2446            ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => {
2447                // Zero-extend iN to the bitmask type:
2448                return Ok(bx.zext(i_, bx.type_ix(expected_int_bits)));
2449            }
2450            ty::Array(elem, len)
2451                if #[allow(non_exhaustive_omitted_patterns)] match elem.kind() {
    ty::Uint(ty::UintTy::U8) => true,
    _ => false,
}matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
2452                    && len
2453                        .try_to_target_usize(bx.tcx)
2454                        .expect("expected monomorphic const in codegen")
2455                        == expected_bytes =>
2456            {
2457                // Zero-extend iN to the array length:
2458                let ze = bx.zext(i_, bx.type_ix(expected_bytes * 8));
2459
2460                // Convert the integer to a byte array
2461                let ptr = bx.alloca(Size::from_bytes(expected_bytes), Align::ONE);
2462                bx.store(ze, ptr, Align::ONE);
2463                let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
2464                return Ok(bx.load(array_ty, ptr, Align::ONE));
2465            }
2466            _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::CannotReturn {
                span,
                name,
                ret_ty,
                expected_int_bits,
                expected_bytes,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::CannotReturn {
2467                span,
2468                name,
2469                ret_ty,
2470                expected_int_bits,
2471                expected_bytes
2472            }),
2473        }
2474    }
2475
2476    fn simd_simple_float_intrinsic<'ll, 'tcx>(
2477        name: Symbol,
2478        in_elem: Ty<'_>,
2479        in_ty: Ty<'_>,
2480        in_len: u64,
2481        bx: &mut Builder<'_, 'll, 'tcx>,
2482        span: Span,
2483        args: &[OperandRef<'tcx, &'ll Value>],
2484    ) -> Result<&'ll Value, ErrorGuaranteed> {
2485        macro_rules! return_error {
2486            ($diag: expr) => {{
2487                let err = bx.sess().dcx().emit_err($diag);
2488                return Err(err);
2489            }};
2490        }
2491
2492        let ty::Float(f) = in_elem.kind() else {
2493            {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::BasicFloatType {
                span,
                name,
                ty: in_ty,
            });
    return Err(err);
};return_error!(InvalidMonomorphization::BasicFloatType { span, name, ty: in_ty });
2494        };
2495        let elem_ty = bx.cx.type_float_from_ty(*f);
2496
2497        let vec_ty = bx.type_vector(elem_ty, in_len);
2498
2499        let intr_name = match name {
2500            sym::simd_ceil => "llvm.ceil",
2501            sym::simd_fabs => "llvm.fabs",
2502            sym::simd_fcos => "llvm.cos",
2503            sym::simd_fexp2 => "llvm.exp2",
2504            sym::simd_fexp => "llvm.exp",
2505            sym::simd_flog10 => "llvm.log10",
2506            sym::simd_flog2 => "llvm.log2",
2507            sym::simd_flog => "llvm.log",
2508            sym::simd_floor => "llvm.floor",
2509            sym::simd_fma => "llvm.fma",
2510            sym::simd_relaxed_fma => "llvm.fmuladd",
2511            sym::simd_fsin => "llvm.sin",
2512            sym::simd_fsqrt => "llvm.sqrt",
2513            sym::simd_round => "llvm.round",
2514            sym::simd_round_ties_even => "llvm.rint",
2515            sym::simd_trunc => "llvm.trunc",
2516            _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::UnrecognizedIntrinsic {
                span,
                name,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::UnrecognizedIntrinsic { span, name }),
2517        };
2518        Ok(bx.call_intrinsic(
2519            intr_name,
2520            &[vec_ty],
2521            &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
2522        ))
2523    }
2524
2525    if #[allow(non_exhaustive_omitted_patterns)] match name {
    sym::simd_ceil | sym::simd_fabs | sym::simd_fcos | sym::simd_fexp2 |
        sym::simd_fexp | sym::simd_flog10 | sym::simd_flog2 | sym::simd_flog |
        sym::simd_floor | sym::simd_fma | sym::simd_fsin | sym::simd_fsqrt |
        sym::simd_relaxed_fma | sym::simd_round | sym::simd_round_ties_even |
        sym::simd_trunc => true,
    _ => false,
}std::matches!(
2526        name,
2527        sym::simd_ceil
2528            | sym::simd_fabs
2529            | sym::simd_fcos
2530            | sym::simd_fexp2
2531            | sym::simd_fexp
2532            | sym::simd_flog10
2533            | sym::simd_flog2
2534            | sym::simd_flog
2535            | sym::simd_floor
2536            | sym::simd_fma
2537            | sym::simd_fsin
2538            | sym::simd_fsqrt
2539            | sym::simd_relaxed_fma
2540            | sym::simd_round
2541            | sym::simd_round_ties_even
2542            | sym::simd_trunc
2543    ) {
2544        return simd_simple_float_intrinsic(name, in_elem, in_ty, in_len, bx, span, args);
2545    }
2546
2547    fn llvm_vector_ty<'ll>(cx: &CodegenCx<'ll, '_>, elem_ty: Ty<'_>, vec_len: u64) -> &'ll Type {
2548        let elem_ty = match *elem_ty.kind() {
2549            ty::Int(v) => cx.type_int_from_ty(v),
2550            ty::Uint(v) => cx.type_uint_from_ty(v),
2551            ty::Float(v) => cx.type_float_from_ty(v),
2552            ty::RawPtr(_, _) => cx.type_ptr(),
2553            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
2554        };
2555        cx.type_vector(elem_ty, vec_len)
2556    }
2557
2558    if name == sym::simd_gather {
2559        // simd_gather(values: <N x T>, pointers: <N x *_ T>,
2560        //             mask: <N x i{M}>) -> <N x T>
2561        // * N: number of elements in the input vectors
2562        // * T: type of the element to load
2563        // * M: any integer width is supported, will be truncated to i1
2564
2565        // All types must be simd vector types
2566
2567        // The second argument must be a simd vector with an element type that's a pointer
2568        // to the element type of the first argument
2569        let (_, element_ty0) = {
    if !in_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdFirst {
                        span,
                        name,
                        ty: in_ty,
                    });
            return Err(err);
        };
    };
    in_ty.simd_size_and_type(bx.tcx())
}require_simd!(in_ty, SimdFirst);
2570        let (out_len, element_ty1) = {
    if !args[1].layout.ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdSecond {
                        span,
                        name,
                        ty: args[1].layout.ty,
                    });
            return Err(err);
        };
    };
    args[1].layout.ty.simd_size_and_type(bx.tcx())
}require_simd!(args[1].layout.ty, SimdSecond);
2571        // The element type of the third argument must be a signed integer type of any width:
2572        let (out_len2, element_ty2) = {
    if !args[2].layout.ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdThird {
                        span,
                        name,
                        ty: args[2].layout.ty,
                    });
            return Err(err);
        };
    };
    args[2].layout.ty.simd_size_and_type(bx.tcx())
}require_simd!(args[2].layout.ty, SimdThird);
2573        {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
};require_simd!(ret_ty, SimdReturn);
2574
2575        // Of the same length:
2576        if !(in_len == out_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::SecondArgumentLength {
                    span,
                    name,
                    in_len,
                    in_ty,
                    arg_ty: args[1].layout.ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
2577            in_len == out_len,
2578            InvalidMonomorphization::SecondArgumentLength {
2579                span,
2580                name,
2581                in_len,
2582                in_ty,
2583                arg_ty: args[1].layout.ty,
2584                out_len
2585            }
2586        );
2587        if !(in_len == out_len2) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ThirdArgumentLength {
                    span,
                    name,
                    in_len,
                    in_ty,
                    arg_ty: args[2].layout.ty,
                    out_len: out_len2,
                });
        return Err(err);
    };
};require!(
2588            in_len == out_len2,
2589            InvalidMonomorphization::ThirdArgumentLength {
2590                span,
2591                name,
2592                in_len,
2593                in_ty,
2594                arg_ty: args[2].layout.ty,
2595                out_len: out_len2
2596            }
2597        );
2598
2599        // The return type must match the first argument type
2600        if !(ret_ty == in_ty) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedReturnType {
                    span,
                    name,
                    in_ty,
                    ret_ty,
                });
        return Err(err);
    };
};require!(
2601            ret_ty == in_ty,
2602            InvalidMonomorphization::ExpectedReturnType { span, name, in_ty, ret_ty }
2603        );
2604
2605        if !#[allow(non_exhaustive_omitted_patterns)] match *element_ty1.kind() {
            ty::RawPtr(p_ty, _) if
                p_ty == in_elem && p_ty.kind() == element_ty0.kind() => true,
            _ => false,
        } {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedElementType {
                    span,
                    name,
                    expected_element: element_ty1,
                    second_arg: args[1].layout.ty,
                    in_elem,
                    in_ty,
                    mutability: ExpectedPointerMutability::Not,
                });
        return Err(err);
    };
};require!(
2606            matches!(
2607                *element_ty1.kind(),
2608                ty::RawPtr(p_ty, _) if p_ty == in_elem && p_ty.kind() == element_ty0.kind()
2609            ),
2610            InvalidMonomorphization::ExpectedElementType {
2611                span,
2612                name,
2613                expected_element: element_ty1,
2614                second_arg: args[1].layout.ty,
2615                in_elem,
2616                in_ty,
2617                mutability: ExpectedPointerMutability::Not,
2618            }
2619        );
2620
2621        let mask_elem_bitwidth = match element_ty2.kind() {
    ty::Int(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    ty::Uint(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    _ => {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::MaskWrongElementType {
                        span,
                        name,
                        ty: element_ty2,
                    });
            return Err(err);
        };
    }
}require_int_or_uint_ty!(
2622            element_ty2.kind(),
2623            InvalidMonomorphization::MaskWrongElementType { span, name, ty: element_ty2 }
2624        );
2625
2626        // Alignment of T, must be a constant integer value:
2627        let alignment = bx.align_of(in_elem).bytes();
2628
2629        // Truncate the mask vector to a vector of i1s:
2630        let mask = vector_mask_to_bitmask(bx, args[2].immediate(), mask_elem_bitwidth, in_len);
2631
2632        // Type of the vector of pointers:
2633        let llvm_pointer_vec_ty = llvm_vector_ty(bx, element_ty1, in_len);
2634
2635        // Type of the vector of elements:
2636        let llvm_elem_vec_ty = llvm_vector_ty(bx, element_ty0, in_len);
2637
2638        let args: &[&'ll Value] = if llvm_version < (22, 0, 0) {
2639            let alignment = bx.const_i32(alignment as i32);
2640            &[args[1].immediate(), alignment, mask, args[0].immediate()]
2641        } else {
2642            &[args[1].immediate(), mask, args[0].immediate()]
2643        };
2644
2645        let call =
2646            bx.call_intrinsic("llvm.masked.gather", &[llvm_elem_vec_ty, llvm_pointer_vec_ty], args);
2647        if llvm_version >= (22, 0, 0) {
2648            crate::attributes::apply_to_callsite(
2649                call,
2650                crate::llvm::AttributePlace::Argument(0),
2651                &[crate::llvm::CreateAlignmentAttr(bx.llcx, alignment)],
2652            )
2653        }
2654        return Ok(call);
2655    }
2656
2657    fn llvm_alignment<'ll, 'tcx>(
2658        bx: &mut Builder<'_, 'll, 'tcx>,
2659        alignment: SimdAlign,
2660        vector_ty: Ty<'tcx>,
2661        element_ty: Ty<'tcx>,
2662    ) -> u64 {
2663        match alignment {
2664            SimdAlign::Unaligned => 1,
2665            SimdAlign::Element => bx.align_of(element_ty).bytes(),
2666            SimdAlign::Vector => bx.align_of(vector_ty).bytes(),
2667        }
2668    }
2669
2670    if name == sym::simd_masked_load {
2671        // simd_masked_load<_, _, _, const ALIGN: SimdAlign>(mask: <N x i{M}>, pointer: *_ T, values: <N x T>) -> <N x T>
2672        // * N: number of elements in the input vectors
2673        // * T: type of the element to load
2674        // * M: any integer width is supported, will be truncated to i1
2675        // Loads contiguous elements from memory behind `pointer`, but only for
2676        // those lanes whose `mask` bit is enabled.
2677        // The memory addresses corresponding to the “off” lanes are not accessed.
2678
2679        let alignment = fn_args[3].expect_const().to_branch()[0].to_leaf().to_simd_alignment();
2680
2681        // The element type of the "mask" argument must be a signed integer type of any width
2682        let mask_ty = in_ty;
2683        let (mask_len, mask_elem) = (in_len, in_elem);
2684
2685        // The second argument must be a pointer matching the element type
2686        let pointer_ty = args[1].layout.ty;
2687
2688        // The last argument is a passthrough vector providing values for disabled lanes
2689        let values_ty = args[2].layout.ty;
2690        let (values_len, values_elem) = {
    if !values_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdThird {
                        span,
                        name,
                        ty: values_ty,
                    });
            return Err(err);
        };
    };
    values_ty.simd_size_and_type(bx.tcx())
}require_simd!(values_ty, SimdThird);
2691
2692        {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
};require_simd!(ret_ty, SimdReturn);
2693
2694        // Of the same length:
2695        if !(values_len == mask_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ThirdArgumentLength {
                    span,
                    name,
                    in_len: mask_len,
                    in_ty: mask_ty,
                    arg_ty: values_ty,
                    out_len: values_len,
                });
        return Err(err);
    };
};require!(
2696            values_len == mask_len,
2697            InvalidMonomorphization::ThirdArgumentLength {
2698                span,
2699                name,
2700                in_len: mask_len,
2701                in_ty: mask_ty,
2702                arg_ty: values_ty,
2703                out_len: values_len
2704            }
2705        );
2706
2707        // The return type must match the last argument type
2708        if !(ret_ty == values_ty) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedReturnType {
                    span,
                    name,
                    in_ty: values_ty,
                    ret_ty,
                });
        return Err(err);
    };
};require!(
2709            ret_ty == values_ty,
2710            InvalidMonomorphization::ExpectedReturnType { span, name, in_ty: values_ty, ret_ty }
2711        );
2712
2713        if !#[allow(non_exhaustive_omitted_patterns)] match *pointer_ty.kind() {
            ty::RawPtr(p_ty, _) if
                p_ty == values_elem && p_ty.kind() == values_elem.kind() =>
                true,
            _ => false,
        } {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedElementType {
                    span,
                    name,
                    expected_element: values_elem,
                    second_arg: pointer_ty,
                    in_elem: values_elem,
                    in_ty: values_ty,
                    mutability: ExpectedPointerMutability::Not,
                });
        return Err(err);
    };
};require!(
2714            matches!(
2715                *pointer_ty.kind(),
2716                ty::RawPtr(p_ty, _) if p_ty == values_elem && p_ty.kind() == values_elem.kind()
2717            ),
2718            InvalidMonomorphization::ExpectedElementType {
2719                span,
2720                name,
2721                expected_element: values_elem,
2722                second_arg: pointer_ty,
2723                in_elem: values_elem,
2724                in_ty: values_ty,
2725                mutability: ExpectedPointerMutability::Not,
2726            }
2727        );
2728
2729        let m_elem_bitwidth = match mask_elem.kind() {
    ty::Int(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    ty::Uint(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    _ => {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::MaskWrongElementType {
                        span,
                        name,
                        ty: mask_elem,
                    });
            return Err(err);
        };
    }
}require_int_or_uint_ty!(
2730            mask_elem.kind(),
2731            InvalidMonomorphization::MaskWrongElementType { span, name, ty: mask_elem }
2732        );
2733
2734        let mask = vector_mask_to_bitmask(bx, args[0].immediate(), m_elem_bitwidth, mask_len);
2735
2736        // Alignment of T, must be a constant integer value:
2737        let alignment = llvm_alignment(bx, alignment, values_ty, values_elem);
2738
2739        let llvm_pointer = bx.type_ptr();
2740
2741        // Type of the vector of elements:
2742        let llvm_elem_vec_ty = llvm_vector_ty(bx, values_elem, values_len);
2743
2744        let args: &[&'ll Value] = if llvm_version < (22, 0, 0) {
2745            let alignment = bx.const_i32(alignment as i32);
2746
2747            &[args[1].immediate(), alignment, mask, args[2].immediate()]
2748        } else {
2749            &[args[1].immediate(), mask, args[2].immediate()]
2750        };
2751
2752        let call = bx.call_intrinsic("llvm.masked.load", &[llvm_elem_vec_ty, llvm_pointer], args);
2753        if llvm_version >= (22, 0, 0) {
2754            crate::attributes::apply_to_callsite(
2755                call,
2756                crate::llvm::AttributePlace::Argument(0),
2757                &[crate::llvm::CreateAlignmentAttr(bx.llcx, alignment)],
2758            )
2759        }
2760        return Ok(call);
2761    }
2762
2763    if name == sym::simd_masked_store {
2764        // simd_masked_store<_, _, _, const ALIGN: SimdAlign>(mask: <N x i{M}>, pointer: *mut T, values: <N x T>) -> ()
2765        // * N: number of elements in the input vectors
2766        // * T: type of the element to load
2767        // * M: any integer width is supported, will be truncated to i1
2768        // Stores contiguous elements to memory behind `pointer`, but only for
2769        // those lanes whose `mask` bit is enabled.
2770        // The memory addresses corresponding to the “off” lanes are not accessed.
2771
2772        let alignment = fn_args[3].expect_const().to_branch()[0].to_leaf().to_simd_alignment();
2773
2774        // The element type of the "mask" argument must be a signed integer type of any width
2775        let mask_ty = in_ty;
2776        let (mask_len, mask_elem) = (in_len, in_elem);
2777
2778        // The second argument must be a pointer matching the element type
2779        let pointer_ty = args[1].layout.ty;
2780
2781        // The last argument specifies the values to store to memory
2782        let values_ty = args[2].layout.ty;
2783        let (values_len, values_elem) = {
    if !values_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdThird {
                        span,
                        name,
                        ty: values_ty,
                    });
            return Err(err);
        };
    };
    values_ty.simd_size_and_type(bx.tcx())
}require_simd!(values_ty, SimdThird);
2784
2785        // Of the same length:
2786        if !(values_len == mask_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ThirdArgumentLength {
                    span,
                    name,
                    in_len: mask_len,
                    in_ty: mask_ty,
                    arg_ty: values_ty,
                    out_len: values_len,
                });
        return Err(err);
    };
};require!(
2787            values_len == mask_len,
2788            InvalidMonomorphization::ThirdArgumentLength {
2789                span,
2790                name,
2791                in_len: mask_len,
2792                in_ty: mask_ty,
2793                arg_ty: values_ty,
2794                out_len: values_len
2795            }
2796        );
2797
2798        // The second argument must be a mutable pointer type matching the element type
2799        if !#[allow(non_exhaustive_omitted_patterns)] match *pointer_ty.kind() {
            ty::RawPtr(p_ty, p_mutbl) if
                p_ty == values_elem && p_ty.kind() == values_elem.kind() &&
                    p_mutbl.is_mut() => true,
            _ => false,
        } {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedElementType {
                    span,
                    name,
                    expected_element: values_elem,
                    second_arg: pointer_ty,
                    in_elem: values_elem,
                    in_ty: values_ty,
                    mutability: ExpectedPointerMutability::Mut,
                });
        return Err(err);
    };
};require!(
2800            matches!(
2801                *pointer_ty.kind(),
2802                ty::RawPtr(p_ty, p_mutbl)
2803                    if p_ty == values_elem && p_ty.kind() == values_elem.kind() && p_mutbl.is_mut()
2804            ),
2805            InvalidMonomorphization::ExpectedElementType {
2806                span,
2807                name,
2808                expected_element: values_elem,
2809                second_arg: pointer_ty,
2810                in_elem: values_elem,
2811                in_ty: values_ty,
2812                mutability: ExpectedPointerMutability::Mut,
2813            }
2814        );
2815
2816        let m_elem_bitwidth = match mask_elem.kind() {
    ty::Int(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    ty::Uint(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    _ => {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::MaskWrongElementType {
                        span,
                        name,
                        ty: mask_elem,
                    });
            return Err(err);
        };
    }
}require_int_or_uint_ty!(
2817            mask_elem.kind(),
2818            InvalidMonomorphization::MaskWrongElementType { span, name, ty: mask_elem }
2819        );
2820
2821        let mask = vector_mask_to_bitmask(bx, args[0].immediate(), m_elem_bitwidth, mask_len);
2822
2823        // Alignment of T, must be a constant integer value:
2824        let alignment = llvm_alignment(bx, alignment, values_ty, values_elem);
2825
2826        let llvm_pointer = bx.type_ptr();
2827
2828        // Type of the vector of elements:
2829        let llvm_elem_vec_ty = llvm_vector_ty(bx, values_elem, values_len);
2830
2831        let args: &[&'ll Value] = if llvm_version < (22, 0, 0) {
2832            let alignment = bx.const_i32(alignment as i32);
2833            &[args[2].immediate(), args[1].immediate(), alignment, mask]
2834        } else {
2835            &[args[2].immediate(), args[1].immediate(), mask]
2836        };
2837
2838        let call = bx.call_intrinsic("llvm.masked.store", &[llvm_elem_vec_ty, llvm_pointer], args);
2839        if llvm_version >= (22, 0, 0) {
2840            crate::attributes::apply_to_callsite(
2841                call,
2842                crate::llvm::AttributePlace::Argument(1),
2843                &[crate::llvm::CreateAlignmentAttr(bx.llcx, alignment)],
2844            )
2845        }
2846        return Ok(call);
2847    }
2848
2849    if name == sym::simd_scatter {
2850        // simd_scatter(values: <N x T>, pointers: <N x *mut T>,
2851        //             mask: <N x i{M}>) -> ()
2852        // * N: number of elements in the input vectors
2853        // * T: type of the element to load
2854        // * M: any integer width is supported, will be truncated to i1
2855
2856        // All types must be simd vector types
2857        // The second argument must be a simd vector with an element type that's a pointer
2858        // to the element type of the first argument
2859        let (_, element_ty0) = {
    if !in_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdFirst {
                        span,
                        name,
                        ty: in_ty,
                    });
            return Err(err);
        };
    };
    in_ty.simd_size_and_type(bx.tcx())
}require_simd!(in_ty, SimdFirst);
2860        let (element_len1, element_ty1) = {
    if !args[1].layout.ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdSecond {
                        span,
                        name,
                        ty: args[1].layout.ty,
                    });
            return Err(err);
        };
    };
    args[1].layout.ty.simd_size_and_type(bx.tcx())
}require_simd!(args[1].layout.ty, SimdSecond);
2861        let (element_len2, element_ty2) = {
    if !args[2].layout.ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdThird {
                        span,
                        name,
                        ty: args[2].layout.ty,
                    });
            return Err(err);
        };
    };
    args[2].layout.ty.simd_size_and_type(bx.tcx())
}require_simd!(args[2].layout.ty, SimdThird);
2862
2863        // Of the same length:
2864        if !(in_len == element_len1) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::SecondArgumentLength {
                    span,
                    name,
                    in_len,
                    in_ty,
                    arg_ty: args[1].layout.ty,
                    out_len: element_len1,
                });
        return Err(err);
    };
};require!(
2865            in_len == element_len1,
2866            InvalidMonomorphization::SecondArgumentLength {
2867                span,
2868                name,
2869                in_len,
2870                in_ty,
2871                arg_ty: args[1].layout.ty,
2872                out_len: element_len1
2873            }
2874        );
2875        if !(in_len == element_len2) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ThirdArgumentLength {
                    span,
                    name,
                    in_len,
                    in_ty,
                    arg_ty: args[2].layout.ty,
                    out_len: element_len2,
                });
        return Err(err);
    };
};require!(
2876            in_len == element_len2,
2877            InvalidMonomorphization::ThirdArgumentLength {
2878                span,
2879                name,
2880                in_len,
2881                in_ty,
2882                arg_ty: args[2].layout.ty,
2883                out_len: element_len2
2884            }
2885        );
2886
2887        if !#[allow(non_exhaustive_omitted_patterns)] match *element_ty1.kind() {
            ty::RawPtr(p_ty, p_mutbl) if
                p_ty == in_elem && p_mutbl.is_mut() &&
                    p_ty.kind() == element_ty0.kind() => true,
            _ => false,
        } {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedElementType {
                    span,
                    name,
                    expected_element: element_ty1,
                    second_arg: args[1].layout.ty,
                    in_elem,
                    in_ty,
                    mutability: ExpectedPointerMutability::Mut,
                });
        return Err(err);
    };
};require!(
2888            matches!(
2889                *element_ty1.kind(),
2890                ty::RawPtr(p_ty, p_mutbl)
2891                    if p_ty == in_elem && p_mutbl.is_mut() && p_ty.kind() == element_ty0.kind()
2892            ),
2893            InvalidMonomorphization::ExpectedElementType {
2894                span,
2895                name,
2896                expected_element: element_ty1,
2897                second_arg: args[1].layout.ty,
2898                in_elem,
2899                in_ty,
2900                mutability: ExpectedPointerMutability::Mut,
2901            }
2902        );
2903
2904        // The element type of the third argument must be an integer type of any width:
2905        let mask_elem_bitwidth = match element_ty2.kind() {
    ty::Int(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    ty::Uint(i) => {
        i.bit_width().unwrap_or_else(||
                bx.data_layout().pointer_size().bits())
    }
    _ => {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::MaskWrongElementType {
                        span,
                        name,
                        ty: element_ty2,
                    });
            return Err(err);
        };
    }
}require_int_or_uint_ty!(
2906            element_ty2.kind(),
2907            InvalidMonomorphization::MaskWrongElementType { span, name, ty: element_ty2 }
2908        );
2909
2910        // Alignment of T, must be a constant integer value:
2911        let alignment = bx.align_of(in_elem).bytes();
2912
2913        // Truncate the mask vector to a vector of i1s:
2914        let mask = vector_mask_to_bitmask(bx, args[2].immediate(), mask_elem_bitwidth, in_len);
2915
2916        // Type of the vector of pointers:
2917        let llvm_pointer_vec_ty = llvm_vector_ty(bx, element_ty1, in_len);
2918
2919        // Type of the vector of elements:
2920        let llvm_elem_vec_ty = llvm_vector_ty(bx, element_ty0, in_len);
2921        let args: &[&'ll Value] = if llvm_version < (22, 0, 0) {
2922            let alignment = bx.const_i32(alignment as i32);
2923            &[args[0].immediate(), args[1].immediate(), alignment, mask]
2924        } else {
2925            &[args[0].immediate(), args[1].immediate(), mask]
2926        };
2927        let call = bx.call_intrinsic(
2928            "llvm.masked.scatter",
2929            &[llvm_elem_vec_ty, llvm_pointer_vec_ty],
2930            args,
2931        );
2932        if llvm_version >= (22, 0, 0) {
2933            crate::attributes::apply_to_callsite(
2934                call,
2935                crate::llvm::AttributePlace::Argument(1),
2936                &[crate::llvm::CreateAlignmentAttr(bx.llcx, alignment)],
2937            )
2938        }
2939        return Ok(call);
2940    }
2941
2942    macro_rules! arith_red {
2943        ($name:ident : $integer_reduce:ident, $float_reduce:ident, $ordered:expr, $op:ident,
2944         $identity:expr) => {
2945            if name == sym::$name {
2946                require!(
2947                    ret_ty == in_elem,
2948                    InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
2949                );
2950                return match in_elem.kind() {
2951                    ty::Int(_) | ty::Uint(_) => {
2952                        let r = bx.$integer_reduce(args[0].immediate());
2953                        if $ordered {
2954                            // if overflow occurs, the result is the
2955                            // mathematical result modulo 2^n:
2956                            Ok(bx.$op(args[1].immediate(), r))
2957                        } else {
2958                            Ok(bx.$integer_reduce(args[0].immediate()))
2959                        }
2960                    }
2961                    ty::Float(f) => {
2962                        let acc = if $ordered {
2963                            // ordered arithmetic reductions take an accumulator
2964                            args[1].immediate()
2965                        } else {
2966                            // unordered arithmetic reductions use the identity accumulator
2967                            match f.bit_width() {
2968                                32 => bx.const_real(bx.type_f32(), $identity),
2969                                64 => bx.const_real(bx.type_f64(), $identity),
2970                                v => return_error!(
2971                                    InvalidMonomorphization::UnsupportedSymbolOfSize {
2972                                        span,
2973                                        name,
2974                                        symbol: sym::$name,
2975                                        in_ty,
2976                                        in_elem,
2977                                        size: v,
2978                                        ret_ty
2979                                    }
2980                                ),
2981                            }
2982                        };
2983                        Ok(bx.$float_reduce(acc, args[0].immediate()))
2984                    }
2985                    _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
2986                        span,
2987                        name,
2988                        symbol: sym::$name,
2989                        in_ty,
2990                        in_elem,
2991                        ret_ty
2992                    }),
2993                };
2994            }
2995        };
2996    }
2997
2998    if name == sym::simd_reduce_add_ordered {
    if !(ret_ty == in_elem) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                        span,
                        name,
                        in_elem,
                        in_ty,
                        ret_ty,
                    });
            return Err(err);
        };
    };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_add(args[0].immediate());
                if true {
                    Ok(bx.add(args[1].immediate(), r))
                } else { Ok(bx.vector_reduce_add(args[0].immediate())) }
            }
            ty::Float(f) => {
                let acc =
                    if true {
                        args[1].immediate()
                    } else {
                        match f.bit_width() {
                            32 => bx.const_real(bx.type_f32(), -0.0),
                            64 => bx.const_real(bx.type_f64(), -0.0),
                            v => {
                                let err =
                                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbolOfSize {
                                            span,
                                            name,
                                            symbol: sym::simd_reduce_add_ordered,
                                            in_ty,
                                            in_elem,
                                            size: v,
                                            ret_ty,
                                        });
                                return Err(err);
                            }
                        }
                    };
                Ok(bx.vector_reduce_fadd(acc, args[0].immediate()))
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_add_ordered,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};arith_red!(simd_reduce_add_ordered: vector_reduce_add, vector_reduce_fadd, true, add, -0.0);
2999    if name == sym::simd_reduce_mul_ordered {
    if !(ret_ty == in_elem) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                        span,
                        name,
                        in_elem,
                        in_ty,
                        ret_ty,
                    });
            return Err(err);
        };
    };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_mul(args[0].immediate());
                if true {
                    Ok(bx.mul(args[1].immediate(), r))
                } else { Ok(bx.vector_reduce_mul(args[0].immediate())) }
            }
            ty::Float(f) => {
                let acc =
                    if true {
                        args[1].immediate()
                    } else {
                        match f.bit_width() {
                            32 => bx.const_real(bx.type_f32(), 1.0),
                            64 => bx.const_real(bx.type_f64(), 1.0),
                            v => {
                                let err =
                                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbolOfSize {
                                            span,
                                            name,
                                            symbol: sym::simd_reduce_mul_ordered,
                                            in_ty,
                                            in_elem,
                                            size: v,
                                            ret_ty,
                                        });
                                return Err(err);
                            }
                        }
                    };
                Ok(bx.vector_reduce_fmul(acc, args[0].immediate()))
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_mul_ordered,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};arith_red!(simd_reduce_mul_ordered: vector_reduce_mul, vector_reduce_fmul, true, mul, 1.0);
3000    if name == sym::simd_reduce_add_unordered {
    if !(ret_ty == in_elem) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                        span,
                        name,
                        in_elem,
                        in_ty,
                        ret_ty,
                    });
            return Err(err);
        };
    };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_add(args[0].immediate());
                if false {
                    Ok(bx.add(args[1].immediate(), r))
                } else { Ok(bx.vector_reduce_add(args[0].immediate())) }
            }
            ty::Float(f) => {
                let acc =
                    if false {
                        args[1].immediate()
                    } else {
                        match f.bit_width() {
                            32 => bx.const_real(bx.type_f32(), -0.0),
                            64 => bx.const_real(bx.type_f64(), -0.0),
                            v => {
                                let err =
                                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbolOfSize {
                                            span,
                                            name,
                                            symbol: sym::simd_reduce_add_unordered,
                                            in_ty,
                                            in_elem,
                                            size: v,
                                            ret_ty,
                                        });
                                return Err(err);
                            }
                        }
                    };
                Ok(bx.vector_reduce_fadd_reassoc(acc, args[0].immediate()))
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_add_unordered,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};arith_red!(
3001        simd_reduce_add_unordered: vector_reduce_add,
3002        vector_reduce_fadd_reassoc,
3003        false,
3004        add,
3005        -0.0
3006    );
3007    if name == sym::simd_reduce_mul_unordered {
    if !(ret_ty == in_elem) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                        span,
                        name,
                        in_elem,
                        in_ty,
                        ret_ty,
                    });
            return Err(err);
        };
    };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_mul(args[0].immediate());
                if false {
                    Ok(bx.mul(args[1].immediate(), r))
                } else { Ok(bx.vector_reduce_mul(args[0].immediate())) }
            }
            ty::Float(f) => {
                let acc =
                    if false {
                        args[1].immediate()
                    } else {
                        match f.bit_width() {
                            32 => bx.const_real(bx.type_f32(), 1.0),
                            64 => bx.const_real(bx.type_f64(), 1.0),
                            v => {
                                let err =
                                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbolOfSize {
                                            span,
                                            name,
                                            symbol: sym::simd_reduce_mul_unordered,
                                            in_ty,
                                            in_elem,
                                            size: v,
                                            ret_ty,
                                        });
                                return Err(err);
                            }
                        }
                    };
                Ok(bx.vector_reduce_fmul_reassoc(acc, args[0].immediate()))
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_mul_unordered,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};arith_red!(
3008        simd_reduce_mul_unordered: vector_reduce_mul,
3009        vector_reduce_fmul_reassoc,
3010        false,
3011        mul,
3012        1.0
3013    );
3014
3015    macro_rules! minmax_red {
3016        ($name:ident: $int_red:ident) => {
3017            if name == sym::$name {
3018                require!(
3019                    ret_ty == in_elem,
3020                    InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
3021                );
3022                return match in_elem.kind() {
3023                    ty::Int(_i) => Ok(bx.$int_red(args[0].immediate(), true)),
3024                    ty::Uint(_u) => Ok(bx.$int_red(args[0].immediate(), false)),
3025                    _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
3026                        span,
3027                        name,
3028                        symbol: sym::$name,
3029                        in_ty,
3030                        in_elem,
3031                        ret_ty
3032                    }),
3033                };
3034            }
3035        };
3036    }
3037
3038    // Currently no support for float due to <https://github.com/llvm/llvm-project/issues/185827>.
3039    if name == sym::simd_reduce_min {
    if !(ret_ty == in_elem) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                        span,
                        name,
                        in_elem,
                        in_ty,
                        ret_ty,
                    });
            return Err(err);
        };
    };
    return match in_elem.kind() {
            ty::Int(_i) =>
                Ok(bx.vector_reduce_min(args[0].immediate(), true)),
            ty::Uint(_u) =>
                Ok(bx.vector_reduce_min(args[0].immediate(), false)),
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_min,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};minmax_red!(simd_reduce_min: vector_reduce_min);
3040    if name == sym::simd_reduce_max {
    if !(ret_ty == in_elem) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                        span,
                        name,
                        in_elem,
                        in_ty,
                        ret_ty,
                    });
            return Err(err);
        };
    };
    return match in_elem.kind() {
            ty::Int(_i) =>
                Ok(bx.vector_reduce_max(args[0].immediate(), true)),
            ty::Uint(_u) =>
                Ok(bx.vector_reduce_max(args[0].immediate(), false)),
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_max,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};minmax_red!(simd_reduce_max: vector_reduce_max);
3041
3042    macro_rules! bitwise_red {
3043        ($name:ident : $red:ident, $boolean:expr) => {
3044            if name == sym::$name {
3045                let input = if !$boolean {
3046                    require!(
3047                        ret_ty == in_elem,
3048                        InvalidMonomorphization::ReturnType { span, name, in_elem, in_ty, ret_ty }
3049                    );
3050                    args[0].immediate()
3051                } else {
3052                    let bitwidth = match in_elem.kind() {
3053                        ty::Int(i) => {
3054                            i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
3055                        }
3056                        ty::Uint(i) => {
3057                            i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size().bits())
3058                        }
3059                        _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
3060                            span,
3061                            name,
3062                            symbol: sym::$name,
3063                            in_ty,
3064                            in_elem,
3065                            ret_ty
3066                        }),
3067                    };
3068
3069                    vector_mask_to_bitmask(bx, args[0].immediate(), bitwidth, in_len as _)
3070                };
3071                return match in_elem.kind() {
3072                    ty::Int(_) | ty::Uint(_) => {
3073                        let r = bx.$red(input);
3074                        Ok(r)
3075                    }
3076                    _ => return_error!(InvalidMonomorphization::UnsupportedSymbol {
3077                        span,
3078                        name,
3079                        symbol: sym::$name,
3080                        in_ty,
3081                        in_elem,
3082                        ret_ty
3083                    }),
3084                };
3085            }
3086        };
3087    }
3088
3089    if name == sym::simd_reduce_and {
    let input =
        if !false {
            if !(ret_ty == in_elem) {
                {
                    let err =
                        bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                                span,
                                name,
                                in_elem,
                                in_ty,
                                ret_ty,
                            });
                    return Err(err);
                };
            };
            args[0].immediate()
        } else {
            let bitwidth =
                match in_elem.kind() {
                    ty::Int(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    ty::Uint(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    _ => {
                        let err =
                            bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                                    span,
                                    name,
                                    symbol: sym::simd_reduce_and,
                                    in_ty,
                                    in_elem,
                                    ret_ty,
                                });
                        return Err(err);
                    }
                };
            vector_mask_to_bitmask(bx, args[0].immediate(), bitwidth,
                in_len as _)
        };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_and(input);
                Ok(r)
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_and,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};bitwise_red!(simd_reduce_and: vector_reduce_and, false);
3090    if name == sym::simd_reduce_or {
    let input =
        if !false {
            if !(ret_ty == in_elem) {
                {
                    let err =
                        bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                                span,
                                name,
                                in_elem,
                                in_ty,
                                ret_ty,
                            });
                    return Err(err);
                };
            };
            args[0].immediate()
        } else {
            let bitwidth =
                match in_elem.kind() {
                    ty::Int(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    ty::Uint(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    _ => {
                        let err =
                            bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                                    span,
                                    name,
                                    symbol: sym::simd_reduce_or,
                                    in_ty,
                                    in_elem,
                                    ret_ty,
                                });
                        return Err(err);
                    }
                };
            vector_mask_to_bitmask(bx, args[0].immediate(), bitwidth,
                in_len as _)
        };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_or(input);
                Ok(r)
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_or,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};bitwise_red!(simd_reduce_or: vector_reduce_or, false);
3091    if name == sym::simd_reduce_xor {
    let input =
        if !false {
            if !(ret_ty == in_elem) {
                {
                    let err =
                        bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                                span,
                                name,
                                in_elem,
                                in_ty,
                                ret_ty,
                            });
                    return Err(err);
                };
            };
            args[0].immediate()
        } else {
            let bitwidth =
                match in_elem.kind() {
                    ty::Int(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    ty::Uint(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    _ => {
                        let err =
                            bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                                    span,
                                    name,
                                    symbol: sym::simd_reduce_xor,
                                    in_ty,
                                    in_elem,
                                    ret_ty,
                                });
                        return Err(err);
                    }
                };
            vector_mask_to_bitmask(bx, args[0].immediate(), bitwidth,
                in_len as _)
        };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_xor(input);
                Ok(r)
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_xor,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};bitwise_red!(simd_reduce_xor: vector_reduce_xor, false);
3092    if name == sym::simd_reduce_all {
    let input =
        if !true {
            if !(ret_ty == in_elem) {
                {
                    let err =
                        bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                                span,
                                name,
                                in_elem,
                                in_ty,
                                ret_ty,
                            });
                    return Err(err);
                };
            };
            args[0].immediate()
        } else {
            let bitwidth =
                match in_elem.kind() {
                    ty::Int(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    ty::Uint(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    _ => {
                        let err =
                            bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                                    span,
                                    name,
                                    symbol: sym::simd_reduce_all,
                                    in_ty,
                                    in_elem,
                                    ret_ty,
                                });
                        return Err(err);
                    }
                };
            vector_mask_to_bitmask(bx, args[0].immediate(), bitwidth,
                in_len as _)
        };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_and(input);
                Ok(r)
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_all,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};bitwise_red!(simd_reduce_all: vector_reduce_and, true);
3093    if name == sym::simd_reduce_any {
    let input =
        if !true {
            if !(ret_ty == in_elem) {
                {
                    let err =
                        bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnType {
                                span,
                                name,
                                in_elem,
                                in_ty,
                                ret_ty,
                            });
                    return Err(err);
                };
            };
            args[0].immediate()
        } else {
            let bitwidth =
                match in_elem.kind() {
                    ty::Int(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    ty::Uint(i) => {
                        i.bit_width().unwrap_or_else(||
                                bx.data_layout().pointer_size().bits())
                    }
                    _ => {
                        let err =
                            bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                                    span,
                                    name,
                                    symbol: sym::simd_reduce_any,
                                    in_ty,
                                    in_elem,
                                    ret_ty,
                                });
                        return Err(err);
                    }
                };
            vector_mask_to_bitmask(bx, args[0].immediate(), bitwidth,
                in_len as _)
        };
    return match in_elem.kind() {
            ty::Int(_) | ty::Uint(_) => {
                let r = bx.vector_reduce_or(input);
                Ok(r)
            }
            _ => {
                let err =
                    bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedSymbol {
                            span,
                            name,
                            symbol: sym::simd_reduce_any,
                            in_ty,
                            in_elem,
                            ret_ty,
                        });
                return Err(err);
            }
        };
};bitwise_red!(simd_reduce_any: vector_reduce_or, true);
3094
3095    if name == sym::simd_cast_ptr {
3096        let (out_len, out_elem) = {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
}require_simd!(ret_ty, SimdReturn);
3097        if !(in_len == out_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnLengthInputType {
                    span,
                    name,
                    in_len,
                    in_ty,
                    ret_ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
3098            in_len == out_len,
3099            InvalidMonomorphization::ReturnLengthInputType {
3100                span,
3101                name,
3102                in_len,
3103                in_ty,
3104                ret_ty,
3105                out_len
3106            }
3107        );
3108
3109        match in_elem.kind() {
3110            ty::RawPtr(p_ty, _) => {
3111                let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| {
3112                    bx.tcx.normalize_erasing_regions(bx.typing_env(), ty)
3113                });
3114                if !metadata.is_unit() {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::CastWidePointer {
                    span,
                    name,
                    ty: in_elem,
                });
        return Err(err);
    };
};require!(
3115                    metadata.is_unit(),
3116                    InvalidMonomorphization::CastWidePointer { span, name, ty: in_elem }
3117                );
3118            }
3119            _ => {
3120                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedPointer {
                span,
                name,
                ty: in_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: in_elem })
3121            }
3122        }
3123        match out_elem.kind() {
3124            ty::RawPtr(p_ty, _) => {
3125                let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| {
3126                    bx.tcx.normalize_erasing_regions(bx.typing_env(), ty)
3127                });
3128                if !metadata.is_unit() {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::CastWidePointer {
                    span,
                    name,
                    ty: out_elem,
                });
        return Err(err);
    };
};require!(
3129                    metadata.is_unit(),
3130                    InvalidMonomorphization::CastWidePointer { span, name, ty: out_elem }
3131                );
3132            }
3133            _ => {
3134                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedPointer {
                span,
                name,
                ty: out_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: out_elem })
3135            }
3136        }
3137
3138        return Ok(args[0].immediate());
3139    }
3140
3141    if name == sym::simd_expose_provenance {
3142        let (out_len, out_elem) = {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
}require_simd!(ret_ty, SimdReturn);
3143        if !(in_len == out_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnLengthInputType {
                    span,
                    name,
                    in_len,
                    in_ty,
                    ret_ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
3144            in_len == out_len,
3145            InvalidMonomorphization::ReturnLengthInputType {
3146                span,
3147                name,
3148                in_len,
3149                in_ty,
3150                ret_ty,
3151                out_len
3152            }
3153        );
3154
3155        match in_elem.kind() {
3156            ty::RawPtr(_, _) => {}
3157            _ => {
3158                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedPointer {
                span,
                name,
                ty: in_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: in_elem })
3159            }
3160        }
3161        match out_elem.kind() {
3162            ty::Uint(ty::UintTy::Usize) => {}
3163            _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedUsize {
                span,
                name,
                ty: out_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::ExpectedUsize { span, name, ty: out_elem }),
3164        }
3165
3166        return Ok(bx.ptrtoint(args[0].immediate(), llret_ty));
3167    }
3168
3169    if name == sym::simd_with_exposed_provenance {
3170        let (out_len, out_elem) = {
    if !ret_ty.is_simd() {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    ret_ty.simd_size_and_type(bx.tcx())
}require_simd!(ret_ty, SimdReturn);
3171        if !(in_len == out_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnLengthInputType {
                    span,
                    name,
                    in_len,
                    in_ty,
                    ret_ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
3172            in_len == out_len,
3173            InvalidMonomorphization::ReturnLengthInputType {
3174                span,
3175                name,
3176                in_len,
3177                in_ty,
3178                ret_ty,
3179                out_len
3180            }
3181        );
3182
3183        match in_elem.kind() {
3184            ty::Uint(ty::UintTy::Usize) => {}
3185            _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedUsize {
                span,
                name,
                ty: in_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::ExpectedUsize { span, name, ty: in_elem }),
3186        }
3187        match out_elem.kind() {
3188            ty::RawPtr(_, _) => {}
3189            _ => {
3190                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedPointer {
                span,
                name,
                ty: out_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::ExpectedPointer { span, name, ty: out_elem })
3191            }
3192        }
3193
3194        return Ok(bx.inttoptr(args[0].immediate(), llret_ty));
3195    }
3196
3197    if name == sym::simd_cast || name == sym::simd_as {
3198        let (out_len, out_elem, out_num_vecs) = {
    if !(ret_ty.is_simd() || ret_ty.is_scalable_vector()) {
        {
            let err =
                bx.sess().dcx().emit_err(InvalidMonomorphization::SimdReturn {
                        span,
                        name,
                        ty: ret_ty,
                    });
            return Err(err);
        };
    };
    if ret_ty.is_simd() {
        let (len, ty) = ret_ty.simd_size_and_type(bx.tcx());
        (len, ty, None)
    } else {
        let (count, ty, num_vecs) =
            ret_ty.scalable_vector_parts(bx.tcx()).expect("`is_scalable_vector` was wrong");
        (count as u64, ty, Some(num_vecs))
    }
}require_simd_or_scalable!(ret_ty, SimdReturn);
3199        if !(in_len == out_len) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnLengthInputType {
                    span,
                    name,
                    in_len,
                    in_ty,
                    ret_ty,
                    out_len,
                });
        return Err(err);
    };
};require!(
3200            in_len == out_len,
3201            InvalidMonomorphization::ReturnLengthInputType {
3202                span,
3203                name,
3204                in_len,
3205                in_ty,
3206                ret_ty,
3207                out_len
3208            }
3209        );
3210        if !(in_num_vecs == out_num_vecs) {
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::ReturnNumVecsInputType {
                    span,
                    name,
                    in_num_vecs: in_num_vecs.unwrap_or(NumScalableVectors(1)),
                    in_ty,
                    ret_ty,
                    out_num_vecs: out_num_vecs.unwrap_or(NumScalableVectors(1)),
                });
        return Err(err);
    };
};require!(
3211            in_num_vecs == out_num_vecs,
3212            InvalidMonomorphization::ReturnNumVecsInputType {
3213                span,
3214                name,
3215                in_num_vecs: in_num_vecs.unwrap_or(NumScalableVectors(1)),
3216                in_ty,
3217                ret_ty,
3218                out_num_vecs: out_num_vecs.unwrap_or(NumScalableVectors(1))
3219            }
3220        );
3221
3222        // Casting cares about nominal type, not just structural type
3223        if in_elem == out_elem {
3224            return Ok(args[0].immediate());
3225        }
3226
3227        #[derive(#[automatically_derived]
impl ::core::marker::Copy for Sign { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Sign {
    #[inline]
    fn clone(&self) -> Sign { *self }
}Clone)]
3228        enum Sign {
3229            Unsigned,
3230            Signed,
3231        }
3232        use Sign::*;
3233
3234        enum Style {
3235            Float,
3236            Int(Sign),
3237            Unsupported,
3238        }
3239
3240        let (in_style, in_width) = match in_elem.kind() {
3241            // vectors of pointer-sized integers should've been
3242            // disallowed before here, so this unwrap is safe.
3243            ty::Int(i) => (
3244                Style::Int(Signed),
3245                i.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
3246            ),
3247            ty::Uint(u) => (
3248                Style::Int(Unsigned),
3249                u.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
3250            ),
3251            ty::Float(f) => (Style::Float, f.bit_width()),
3252            _ => (Style::Unsupported, 0),
3253        };
3254        let (out_style, out_width) = match out_elem.kind() {
3255            ty::Int(i) => (
3256                Style::Int(Signed),
3257                i.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
3258            ),
3259            ty::Uint(u) => (
3260                Style::Int(Unsigned),
3261                u.normalize(bx.tcx().sess.target.pointer_width).bit_width().unwrap(),
3262            ),
3263            ty::Float(f) => (Style::Float, f.bit_width()),
3264            _ => (Style::Unsupported, 0),
3265        };
3266
3267        match (in_style, out_style) {
3268            (Style::Int(sign), Style::Int(_)) => {
3269                return Ok(match in_width.cmp(&out_width) {
3270                    Ordering::Greater => bx.trunc(args[0].immediate(), llret_ty),
3271                    Ordering::Equal => args[0].immediate(),
3272                    Ordering::Less => match sign {
3273                        Sign::Signed => bx.sext(args[0].immediate(), llret_ty),
3274                        Sign::Unsigned => bx.zext(args[0].immediate(), llret_ty),
3275                    },
3276                });
3277            }
3278            (Style::Int(Sign::Signed), Style::Float) => {
3279                return Ok(bx.sitofp(args[0].immediate(), llret_ty));
3280            }
3281            (Style::Int(Sign::Unsigned), Style::Float) => {
3282                return Ok(bx.uitofp(args[0].immediate(), llret_ty));
3283            }
3284            (Style::Float, Style::Int(sign)) => {
3285                return Ok(match (sign, name == sym::simd_as) {
3286                    (Sign::Unsigned, false) => bx.fptoui(args[0].immediate(), llret_ty),
3287                    (Sign::Signed, false) => bx.fptosi(args[0].immediate(), llret_ty),
3288                    (_, true) => bx.cast_float_to_int(
3289                        #[allow(non_exhaustive_omitted_patterns)] match sign {
    Sign::Signed => true,
    _ => false,
}matches!(sign, Sign::Signed),
3290                        args[0].immediate(),
3291                        llret_ty,
3292                    ),
3293                });
3294            }
3295            (Style::Float, Style::Float) => {
3296                return Ok(match in_width.cmp(&out_width) {
3297                    Ordering::Greater => bx.fptrunc(args[0].immediate(), llret_ty),
3298                    Ordering::Equal => args[0].immediate(),
3299                    Ordering::Less => bx.fpext(args[0].immediate(), llret_ty),
3300                });
3301            }
3302            _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedCast {
                span,
                name,
                in_ty,
                in_elem,
                ret_ty,
                out_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::UnsupportedCast {
3303                span,
3304                name,
3305                in_ty,
3306                in_elem,
3307                ret_ty,
3308                out_elem
3309            }),
3310        }
3311    }
3312    macro_rules! arith_binary {
3313        ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
3314            $(if name == sym::$name {
3315                match in_elem.kind() {
3316                    $($(ty::$p(_))|* => {
3317                        return Ok(bx.$call(args[0].immediate(), args[1].immediate()))
3318                    })*
3319                    _ => {},
3320                }
3321                return_error!(
3322                    InvalidMonomorphization::UnsupportedOperation { span, name, in_ty, in_elem }
3323                );
3324            })*
3325        }
3326    }
3327    if name == sym::simd_minimum_number_nsz {
    match in_elem.kind() {
        ty::Float(_) => {
            return Ok(bx.minimum_number_nsz(args[0].immediate(),
                        args[1].immediate()))
        }
        _ => {}
    }
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedOperation {
                    span,
                    name,
                    in_ty,
                    in_elem,
                });
        return Err(err);
    };
}arith_binary! {
3328        simd_add: Uint, Int => add, Float => fadd;
3329        simd_sub: Uint, Int => sub, Float => fsub;
3330        simd_mul: Uint, Int => mul, Float => fmul;
3331        simd_div: Uint => udiv, Int => sdiv, Float => fdiv;
3332        simd_rem: Uint => urem, Int => srem, Float => frem;
3333        simd_shl: Uint, Int => shl;
3334        simd_shr: Uint => lshr, Int => ashr;
3335        simd_and: Uint, Int => and;
3336        simd_or: Uint, Int => or;
3337        simd_xor: Uint, Int => xor;
3338        simd_maximum_number_nsz: Float => maximum_number_nsz;
3339        simd_minimum_number_nsz: Float => minimum_number_nsz;
3340
3341    }
3342    macro_rules! arith_unary {
3343        ($($name: ident: $($($p: ident),* => $call: ident),*;)*) => {
3344            $(if name == sym::$name {
3345                match in_elem.kind() {
3346                    $($(ty::$p(_))|* => {
3347                        return Ok(bx.$call(args[0].immediate()))
3348                    })*
3349                    _ => {},
3350                }
3351                return_error!(
3352                    InvalidMonomorphization::UnsupportedOperation { span, name, in_ty, in_elem }
3353                );
3354            })*
3355        }
3356    }
3357    if name == sym::simd_neg {
    match in_elem.kind() {
        ty::Int(_) => { return Ok(bx.neg(args[0].immediate())) }
        ty::Float(_) => { return Ok(bx.fneg(args[0].immediate())) }
        _ => {}
    }
    {
        let err =
            bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedOperation {
                    span,
                    name,
                    in_ty,
                    in_elem,
                });
        return Err(err);
    };
}arith_unary! {
3358        simd_neg: Int => neg, Float => fneg;
3359    }
3360
3361    // Unary integer intrinsics
3362    if #[allow(non_exhaustive_omitted_patterns)] match name {
    sym::simd_bswap | sym::simd_bitreverse | sym::simd_ctlz | sym::simd_ctpop
        | sym::simd_cttz | sym::simd_carryless_mul | sym::simd_funnel_shl |
        sym::simd_funnel_shr => true,
    _ => false,
}matches!(
3363        name,
3364        sym::simd_bswap
3365            | sym::simd_bitreverse
3366            | sym::simd_ctlz
3367            | sym::simd_ctpop
3368            | sym::simd_cttz
3369            | sym::simd_carryless_mul
3370            | sym::simd_funnel_shl
3371            | sym::simd_funnel_shr
3372    ) {
3373        let vec_ty = bx.cx.type_vector(
3374            match *in_elem.kind() {
3375                ty::Int(i) => bx.cx.type_int_from_ty(i),
3376                ty::Uint(i) => bx.cx.type_uint_from_ty(i),
3377                _ => {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::UnsupportedOperation {
                span,
                name,
                in_ty,
                in_elem,
            });
    return Err(err);
}return_error!(InvalidMonomorphization::UnsupportedOperation {
3378                    span,
3379                    name,
3380                    in_ty,
3381                    in_elem
3382                }),
3383            },
3384            in_len as u64,
3385        );
3386        let llvm_intrinsic = match name {
3387            sym::simd_bswap => "llvm.bswap",
3388            sym::simd_bitreverse => "llvm.bitreverse",
3389            sym::simd_ctlz => "llvm.ctlz",
3390            sym::simd_ctpop => "llvm.ctpop",
3391            sym::simd_cttz => "llvm.cttz",
3392            sym::simd_funnel_shl => "llvm.fshl",
3393            sym::simd_funnel_shr => "llvm.fshr",
3394            sym::simd_carryless_mul => "llvm.clmul",
3395            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3396        };
3397        let int_size = in_elem.int_size_and_signed(bx.tcx()).0.bits();
3398
3399        return match name {
3400            // byte swap is no-op for i8/u8
3401            sym::simd_bswap if int_size == 8 => Ok(args[0].immediate()),
3402            sym::simd_ctlz | sym::simd_cttz => {
3403                // for the (int, i1 immediate) pair, the second arg adds `(0, true) => poison`
3404                let dont_poison_on_zero = bx.const_int(bx.type_i1(), 0);
3405                Ok(bx.call_intrinsic(
3406                    llvm_intrinsic,
3407                    &[vec_ty],
3408                    &[args[0].immediate(), dont_poison_on_zero],
3409                ))
3410            }
3411            sym::simd_bswap | sym::simd_bitreverse | sym::simd_ctpop => {
3412                // simple unary argument cases
3413                Ok(bx.call_intrinsic(llvm_intrinsic, &[vec_ty], &[args[0].immediate()]))
3414            }
3415            sym::simd_funnel_shl | sym::simd_funnel_shr => Ok(bx.call_intrinsic(
3416                llvm_intrinsic,
3417                &[vec_ty],
3418                &[args[0].immediate(), args[1].immediate(), args[2].immediate()],
3419            )),
3420            sym::simd_carryless_mul => {
3421                if crate::llvm_util::get_version() >= (22, 0, 0) {
3422                    Ok(bx.call_intrinsic(
3423                        llvm_intrinsic,
3424                        &[vec_ty],
3425                        &[args[0].immediate(), args[1].immediate()],
3426                    ))
3427                } else {
3428                    ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("`simd_carryless_mul` needs LLVM 22 or higher"));span_bug!(span, "`simd_carryless_mul` needs LLVM 22 or higher");
3429                }
3430            }
3431            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3432        };
3433    }
3434
3435    if name == sym::simd_arith_offset {
3436        // This also checks that the first operand is a ptr type.
3437        let pointee = in_elem.builtin_deref(true).unwrap_or_else(|| {
3438            ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("must be called with a vector of pointer types as first argument"))span_bug!(span, "must be called with a vector of pointer types as first argument")
3439        });
3440        let layout = bx.layout_of(pointee);
3441        let ptrs = args[0].immediate();
3442        // The second argument must be a ptr-sized integer.
3443        // (We don't care about the signedness, this is wrapping anyway.)
3444        let (_offsets_len, offsets_elem) = args[1].layout.ty.simd_size_and_type(bx.tcx());
3445        if !#[allow(non_exhaustive_omitted_patterns)] match offsets_elem.kind() {
    ty::Int(ty::IntTy::Isize) | ty::Uint(ty::UintTy::Usize) => true,
    _ => false,
}matches!(offsets_elem.kind(), ty::Int(ty::IntTy::Isize) | ty::Uint(ty::UintTy::Usize)) {
3446            ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("must be called with a vector of pointer-sized integers as second argument"));span_bug!(
3447                span,
3448                "must be called with a vector of pointer-sized integers as second argument"
3449            );
3450        }
3451        let offsets = args[1].immediate();
3452
3453        return Ok(bx.gep(bx.backend_type(layout), ptrs, &[offsets]));
3454    }
3455
3456    if name == sym::simd_saturating_add || name == sym::simd_saturating_sub {
3457        let lhs = args[0].immediate();
3458        let rhs = args[1].immediate();
3459        let is_add = name == sym::simd_saturating_add;
3460        let (signed, elem_ty) = match *in_elem.kind() {
3461            ty::Int(i) => (true, bx.cx.type_int_from_ty(i)),
3462            ty::Uint(i) => (false, bx.cx.type_uint_from_ty(i)),
3463            _ => {
3464                {
    let err =
        bx.sess().dcx().emit_err(InvalidMonomorphization::ExpectedVectorElementType {
                span,
                name,
                expected_element: args[0].layout.ty.simd_size_and_type(bx.tcx()).1,
                vector_type: args[0].layout.ty,
            });
    return Err(err);
};return_error!(InvalidMonomorphization::ExpectedVectorElementType {
3465                    span,
3466                    name,
3467                    expected_element: args[0].layout.ty.simd_size_and_type(bx.tcx()).1,
3468                    vector_type: args[0].layout.ty
3469                });
3470            }
3471        };
3472        let llvm_intrinsic = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("llvm.{0}{1}.sat",
                if signed { 's' } else { 'u' },
                if is_add { "add" } else { "sub" }))
    })format!(
3473            "llvm.{}{}.sat",
3474            if signed { 's' } else { 'u' },
3475            if is_add { "add" } else { "sub" },
3476        );
3477        let vec_ty = bx.cx.type_vector(elem_ty, in_len as u64);
3478
3479        return Ok(bx.call_intrinsic(llvm_intrinsic, &[vec_ty], &[lhs, rhs]));
3480    }
3481
3482    ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("unknown SIMD intrinsic"));span_bug!(span, "unknown SIMD intrinsic");
3483}