Skip to main content

rustc_codegen_ssa/mir/
constant.rs

1use rustc_abi::BackendRepr;
2use rustc_middle::mir::interpret::ErrorHandled;
3use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv};
4use rustc_middle::ty::{self, Ty};
5use rustc_middle::{bug, mir, span_bug};
6
7use super::FunctionCx;
8use crate::errors;
9use crate::mir::operand::OperandRef;
10use crate::traits::*;
11
12impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13    pub(crate) fn eval_mir_constant_to_operand(
14        &self,
15        bx: &mut Bx,
16        constant: &mir::ConstOperand<'tcx>,
17    ) -> OperandRef<'tcx, Bx::Value> {
18        let val = self.eval_mir_constant(constant);
19        let ty = self.monomorphize(constant.ty());
20        OperandRef::from_const(bx, val, ty)
21    }
22
23    pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue {
24        // `MirUsedCollector` visited all required_consts before codegen began, so if we got here
25        // there can be no more constants that fail to evaluate.
26        self.monomorphize(constant.const_)
27            .eval(self.cx.tcx(), self.cx.typing_env(), constant.span)
28            .expect("erroneous constant missed by mono item collection")
29    }
30
31    /// This is a convenience helper for `immediate_const_vector`. It has the precondition
32    /// that the given `constant` is an `Const::Unevaluated` and must be convertible to
33    /// a `ValTree`. If you want a more general version of this, talk to `wg-const-eval` on zulip.
34    ///
35    /// Note that this function is cursed, since usually MIR consts should not be evaluated to
36    /// valtrees!
37    fn eval_unevaluated_mir_constant_to_valtree(
38        &self,
39        constant: &mir::ConstOperand<'tcx>,
40    ) -> Result<Result<ty::ValTree<'tcx>, Ty<'tcx>>, ErrorHandled> {
41        let tcx = self.cx.tcx();
42        let uv = match self.monomorphize(constant.const_) {
43            mir::Const::Unevaluated(uv, _) => uv.shrink(tcx),
44            mir::Const::Ty(_, c) => match c.kind() {
45                // A constant that came from a const generic but was then used as an argument to
46                // old-style simd_shuffle (passing as argument instead of as a generic param).
47                ty::ConstKind::Value(cv) => return Ok(Ok(cv.valtree)),
48                other => ::rustc_middle::util::bug::span_bug_fmt(constant.span,
    format_args!("{0:#?}", other))span_bug!(constant.span, "{other:#?}"),
49            },
50            // We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
51            // a constant and write that value back into `Operand`s. This could happen, but is
52            // unlikely. Also: all users of `simd_shuffle` are on unstable and already need to take
53            // a lot of care around intrinsics. For an issue to happen here, it would require a
54            // macro expanding to a `simd_shuffle` call without wrapping the constant argument in a
55            // `const {}` block, but the user pass through arbitrary expressions.
56            // FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a
57            // real const generic, and get rid of this entire function.
58            other => ::rustc_middle::util::bug::span_bug_fmt(constant.span,
    format_args!("{0:#?}", other))span_bug!(constant.span, "{other:#?}"),
59        };
60        let uv = self.monomorphize(uv);
61        tcx.const_eval_resolve_for_typeck(self.cx.typing_env(), uv, constant.span)
62    }
63
64    /// process constant containing SIMD shuffle indices & constant vectors
65    pub fn immediate_const_vector(
66        &mut self,
67        bx: &Bx,
68        constant: &mir::ConstOperand<'tcx>,
69    ) -> (Bx::Value, Ty<'tcx>) {
70        let ty = self.monomorphize(constant.ty());
71        if !ty.is_simd() {
    ::core::panicking::panic("assertion failed: ty.is_simd()")
};assert!(ty.is_simd());
72        let field_ty = ty.simd_size_and_type(bx.tcx()).1;
73
74        let val = self
75            .eval_unevaluated_mir_constant_to_valtree(constant)
76            .ok()
77            .map(|x| x.ok())
78            .flatten()
79            .map(|val| {
80                // A SIMD type has a single field, which is an array.
81                let fields = val.to_branch();
82                match (&fields.len(), &1) {
    (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!(fields.len(), 1);
83                let array = fields[0].to_branch();
84                // Iterate over the array elements to obtain the values in the vector.
85                let values: Vec<_> = array
86                    .iter()
87                    .map(|field| {
88                        let Some(prim) = field.try_to_scalar() else {
89                            ::rustc_middle::util::bug::bug_fmt(format_args!("field is not a scalar {0:?}",
        field))bug!("field is not a scalar {:?}", field)
90                        };
91                        let layout = bx.layout_of(field_ty);
92                        let BackendRepr::Scalar(scalar) = layout.backend_repr else {
93                            ::rustc_middle::util::bug::bug_fmt(format_args!("from_const: invalid ByVal layout: {0:#?}",
        layout));bug!("from_const: invalid ByVal layout: {:#?}", layout);
94                        };
95                        bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
96                    })
97                    .collect();
98                bx.const_vector(&values)
99            })
100            .unwrap_or_else(|| {
101                bx.tcx().dcx().emit_err(errors::ShuffleIndicesEvaluation { span: constant.span });
102                // We've errored, so we don't have to produce working code.
103                let llty = bx.backend_type(bx.layout_of(ty));
104                bx.const_undef(llty)
105            });
106        (val, ty)
107    }
108}