rustc_codegen_ssa/mir/
constant.rs1use 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 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 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 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 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 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 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 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 let llty = bx.backend_type(bx.layout_of(ty));
107 bx.const_undef(llty)
108 });
109 (val, ty)
110 }
111}