Files
rust/compiler/rustc_codegen_ssa/src/mir/constant.rs

91 lines
3.3 KiB
Rust
Raw Normal View History

2019-12-22 17:42:04 -05:00
use crate::mir::operand::OperandRef;
use crate::traits::*;
2020-03-29 16:41:09 +02:00
use rustc_middle::mir;
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
use rustc_middle::ty::layout::HasTyCtxt;
2020-03-29 16:41:09 +02:00
use rustc_middle::ty::{self, Ty};
use rustc_span::source_map::Span;
use rustc_target::abi::Abi;
use super::FunctionCx;
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
pub fn eval_mir_constant_to_operand(
&self,
bx: &mut Bx,
constant: &mir::Constant<'tcx>,
) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
let val = self.eval_mir_constant(constant)?;
let ty = self.monomorphize(constant.ty());
Ok(OperandRef::from_const(bx, val, ty))
}
2019-03-14 10:19:31 +01:00
pub fn eval_mir_constant(
&self,
2019-03-14 10:19:31 +01:00
constant: &mir::Constant<'tcx>,
) -> Result<ConstValue<'tcx>, ErrorHandled> {
let ct = self.monomorphize(constant.literal);
let ct = match ct {
2021-03-15 11:23:44 +00:00
mir::ConstantKind::Ty(ct) => ct,
mir::ConstantKind::Val(val, _) => return Ok(val),
};
match ct.val() {
2021-03-13 16:31:38 +01:00
ty::ConstKind::Unevaluated(ct) => self
.cx
.tcx()
2021-03-13 16:31:38 +01:00
.const_eval_resolve(ty::ParamEnv::reveal_all(), ct, None)
.map_err(|err| {
self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
err
}),
ty::ConstKind::Value(value) => Ok(value),
2020-03-22 11:53:13 +01:00
err => span_bug!(
constant.span,
"encountered bad ConstKind after monomorphizing: {:?}",
2020-03-22 11:53:13 +01:00
err
),
2017-01-02 11:00:42 -07:00
}
}
/// process constant containing SIMD shuffle indices
pub fn simd_shuffle_indices(
&mut self,
bx: &Bx,
span: Span,
ty: Ty<'tcx>,
constant: Result<ConstValue<'tcx>, ErrorHandled>,
) -> (Bx::Value, Ty<'tcx>) {
constant
.map(|val| {
let field_ty = ty.builtin_index().unwrap();
2020-02-16 09:59:01 +13:00
let c = ty::Const::from_value(bx.tcx(), val, ty);
2020-05-24 19:38:54 +05:30
let values: Vec<_> = bx
.tcx()
.destructure_const(ty::ParamEnv::reveal_all().and(c))
2020-05-24 19:38:54 +05:30
.fields
.iter()
2019-12-22 17:42:04 -05:00
.map(|field| {
if let Some(prim) = field.val().try_to_scalar() {
2019-12-22 17:42:04 -05:00
let layout = bx.layout_of(field_ty);
2022-02-19 00:48:49 +01:00
let Abi::Scalar(scalar) = layout.abi else {
bug!("from_const: invalid ByVal layout: {:#?}", layout);
2019-12-22 17:42:04 -05:00
};
bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
} else {
bug!("simd shuffle field {:?}", field)
}
})
.collect();
let llval = bx.const_struct(&values, false);
(llval, c.ty())
2018-01-16 09:31:48 +01:00
})
.unwrap_or_else(|_| {
2019-12-22 17:42:04 -05:00
bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time");
2018-01-16 09:31:48 +01:00
// We've errored, so we don't have to produce working code.
2020-10-24 02:21:18 +02:00
let ty = self.monomorphize(ty);
let llty = bx.backend_type(bx.layout_of(ty));
(bx.const_undef(llty), ty)
2018-01-16 09:31:48 +01:00
})
2017-01-02 11:00:42 -07:00
}
}