]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mir/constant.rs
Rollup merge of #101717 - Pointerbender:unsafecell-memory-layout, r=Amanieu
[rust.git] / compiler / rustc_codegen_ssa / src / mir / constant.rs
1 use crate::mir::operand::OperandRef;
2 use crate::traits::*;
3 use rustc_middle::mir;
4 use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
5 use rustc_middle::ty::layout::HasTyCtxt;
6 use rustc_middle::ty::{self, Ty};
7 use rustc_span::source_map::Span;
8 use rustc_target::abi::Abi;
9
10 use super::FunctionCx;
11
12 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13     pub fn eval_mir_constant_to_operand(
14         &self,
15         bx: &mut Bx,
16         constant: &mir::Constant<'tcx>,
17     ) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
18         let val = self.eval_mir_constant(constant)?;
19         let ty = self.monomorphize(constant.ty());
20         Ok(OperandRef::from_const(bx, val, ty))
21     }
22
23     pub fn eval_mir_constant(
24         &self,
25         constant: &mir::Constant<'tcx>,
26     ) -> Result<ConstValue<'tcx>, ErrorHandled> {
27         let ct = self.monomorphize(constant.literal);
28         let uv = match ct {
29             mir::ConstantKind::Ty(ct) => match ct.kind() {
30                 ty::ConstKind::Unevaluated(uv) => uv.expand(),
31                 ty::ConstKind::Value(val) => {
32                     return Ok(self.cx.tcx().valtree_to_const_val((ct.ty(), val)));
33                 }
34                 err => span_bug!(
35                     constant.span,
36                     "encountered bad ConstKind after monomorphizing: {:?}",
37                     err
38                 ),
39             },
40             mir::ConstantKind::Unevaluated(uv, _) => uv,
41             mir::ConstantKind::Val(val, _) => return Ok(val),
42         };
43
44         self.cx.tcx().const_eval_resolve(ty::ParamEnv::reveal_all(), uv, None).map_err(|err| {
45             self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
46             err
47         })
48     }
49
50     /// process constant containing SIMD shuffle indices
51     pub fn simd_shuffle_indices(
52         &mut self,
53         bx: &Bx,
54         span: Span,
55         ty: Ty<'tcx>,
56         constant: Result<ConstValue<'tcx>, ErrorHandled>,
57     ) -> (Bx::Value, Ty<'tcx>) {
58         constant
59             .map(|val| {
60                 let field_ty = ty.builtin_index().unwrap();
61                 let c = mir::ConstantKind::from_value(val, ty);
62                 let values: Vec<_> = bx
63                     .tcx()
64                     .destructure_mir_constant(ty::ParamEnv::reveal_all(), c)
65                     .fields
66                     .iter()
67                     .map(|field| {
68                         if let Some(prim) = field.try_to_scalar() {
69                             let layout = bx.layout_of(field_ty);
70                             let Abi::Scalar(scalar) = layout.abi else {
71                                 bug!("from_const: invalid ByVal layout: {:#?}", layout);
72                             };
73                             bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
74                         } else {
75                             bug!("simd shuffle field {:?}", field)
76                         }
77                     })
78                     .collect();
79                 let llval = bx.const_struct(&values, false);
80                 (llval, c.ty())
81             })
82             .unwrap_or_else(|_| {
83                 bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time");
84                 // We've errored, so we don't have to produce working code.
85                 let ty = self.monomorphize(ty);
86                 let llty = bx.backend_type(bx.layout_of(ty));
87                 (bx.const_undef(llty), ty)
88             })
89     }
90 }