Skip to main content

rustc_codegen_ssa/mir/
constant.rs

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