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<'tcx> {
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 uv = match self.monomorphize(constant.const_) {
42            mir::Const::Unevaluated(uv, _) => uv.shrink(),
43            mir::Const::Ty(_, c) => match c.kind() {
44                // A constant that came from a const generic but was then used as an argument to
45                // old-style simd_shuffle (passing as argument instead of as a generic param).
46                rustc_type_ir::ConstKind::Value(cv) => return Ok(Ok(cv.valtree)),
47                other => span_bug!(constant.span, "{other:#?}"),
48            },
49            // We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
50            // a constant and write that value back into `Operand`s. This could happen, but is
51            // unlikely. Also: all users of `simd_shuffle` are on unstable and already need to take
52            // a lot of care around intrinsics. For an issue to happen here, it would require a
53            // macro expanding to a `simd_shuffle` call without wrapping the constant argument in a
54            // `const {}` block, but the user pass through arbitrary expressions.
55            // FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a
56            // real const generic, and get rid of this entire function.
57            other => span_bug!(constant.span, "{other:#?}"),
58        };
59        let uv = self.monomorphize(uv);
60        self.cx.tcx().const_eval_resolve_for_typeck(self.cx.typing_env(), uv, constant.span)
61    }
62
63    /// process constant containing SIMD shuffle indices & constant vectors
64    pub fn immediate_const_vector(
65        &mut self,
66        bx: &Bx,
67        constant: &mir::ConstOperand<'tcx>,
68    ) -> (Bx::Value, Ty<'tcx>) {
69        let ty = self.monomorphize(constant.ty());
70        assert!(ty.is_simd());
71        let field_ty = ty.simd_size_and_type(bx.tcx()).1;
72
73        let val = self
74            .eval_unevaluated_mir_constant_to_valtree(constant)
75            .ok()
76            .map(|x| x.ok())
77            .flatten()
78            .map(|val| {
79                // A SIMD type has a single field, which is an array.
80                let fields = val.unwrap_branch();
81                assert_eq!(fields.len(), 1);
82                let array = fields[0].unwrap_branch();
83                // Iterate over the array elements to obtain the values in the vector.
84                let values: Vec<_> = array
85                    .iter()
86                    .map(|field| {
87                        if let Some(prim) = field.try_to_scalar() {
88                            let layout = bx.layout_of(field_ty);
89                            let BackendRepr::Scalar(scalar) = layout.backend_repr else {
90                                bug!("from_const: invalid ByVal layout: {:#?}", layout);
91                            };
92                            bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
93                        } else {
94                            bug!("field is not a scalar {:?}", field)
95                        }
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}