2022-11-06 10:09:24 -05:00
|
|
|
use crate::errors;
|
2018-10-03 13:49:57 +02:00
|
|
|
use crate::mir::operand::OperandRef;
|
|
|
|
|
use crate::traits::*;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir;
|
2023-09-16 09:36:22 +02:00
|
|
|
use rustc_middle::mir::interpret::ErrorHandled;
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_middle::ty::layout::HasTyCtxt;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2024-04-29 13:56:41 +10:00
|
|
|
use rustc_middle::{bug, span_bug};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::Abi;
|
2015-11-16 19:57:57 +02:00
|
|
|
|
2018-01-16 10:16:38 +01:00
|
|
|
use super::FunctionCx;
|
2016-05-27 14:40:05 +03:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2019-11-11 12:53:31 +01:00
|
|
|
pub fn eval_mir_constant_to_operand(
|
2020-05-30 15:02:32 -04:00
|
|
|
&self,
|
2019-11-11 12:53:31 +01:00
|
|
|
bx: &mut Bx,
|
2023-09-20 20:51:14 +02:00
|
|
|
constant: &mir::ConstOperand<'tcx>,
|
2023-09-11 09:52:45 +02:00
|
|
|
) -> OperandRef<'tcx, Bx::Value> {
|
|
|
|
|
let val = self.eval_mir_constant(constant);
|
2021-03-08 14:14:11 +00:00
|
|
|
let ty = self.monomorphize(constant.ty());
|
2023-09-11 09:52:45 +02:00
|
|
|
OperandRef::from_const(bx, val, ty)
|
2019-11-11 12:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-20 20:51:14 +02:00
|
|
|
pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue<'tcx> {
|
2024-03-10 12:39:53 +01:00
|
|
|
// `MirUsedCollector` visited all required_consts before codegen began, so if we got here
|
|
|
|
|
// there can be no more constants that fail to evaluate.
|
2023-09-20 20:51:14 +02:00
|
|
|
self.monomorphize(constant.const_)
|
2024-03-14 09:10:28 +00:00
|
|
|
.eval(self.cx.tcx(), ty::ParamEnv::reveal_all(), constant.span)
|
2024-03-10 12:39:53 +01:00
|
|
|
.expect("erroneous constant missed by mono item collection")
|
2017-01-02 11:00:42 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-20 08:14:51 +00:00
|
|
|
/// This is a convenience helper for `simd_shuffle_indices`. It has the precondition
|
2023-09-20 20:51:14 +02:00
|
|
|
/// that the given `constant` is an `Const::Unevaluated` and must be convertible to
|
2023-06-20 08:14:51 +00:00
|
|
|
/// a `ValTree`. If you want a more general version of this, talk to `wg-const-eval` on zulip.
|
2023-09-12 23:28:25 +02:00
|
|
|
///
|
|
|
|
|
/// Note that this function is cursed, since usually MIR consts should not be evaluated to valtrees!
|
2023-06-20 08:14:51 +00:00
|
|
|
pub fn eval_unevaluated_mir_constant_to_valtree(
|
|
|
|
|
&self,
|
2023-09-20 20:51:14 +02:00
|
|
|
constant: &mir::ConstOperand<'tcx>,
|
2023-06-20 08:14:51 +00:00
|
|
|
) -> Result<Option<ty::ValTree<'tcx>>, ErrorHandled> {
|
2023-09-20 20:51:14 +02:00
|
|
|
let uv = match self.monomorphize(constant.const_) {
|
|
|
|
|
mir::Const::Unevaluated(uv, _) => uv.shrink(),
|
2024-06-03 04:46:24 +01:00
|
|
|
mir::Const::Ty(_, c) => match c.kind() {
|
2023-07-10 09:33:34 +00:00
|
|
|
// A constant that came from a const generic but was then used as an argument to old-style
|
|
|
|
|
// simd_shuffle (passing as argument instead of as a generic param).
|
2024-06-03 02:34:35 +01:00
|
|
|
rustc_type_ir::ConstKind::Value(_, valtree) => return Ok(Some(valtree)),
|
2023-07-10 09:33:34 +00:00
|
|
|
other => span_bug!(constant.span, "{other:#?}"),
|
|
|
|
|
},
|
2023-09-20 20:51:14 +02:00
|
|
|
// We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
|
2023-07-10 09:33:34 +00:00
|
|
|
// a constant and write that value back into `Operand`s. This could happen, but is unlikely.
|
|
|
|
|
// Also: all users of `simd_shuffle` are on unstable and already need to take a lot of care
|
|
|
|
|
// around intrinsics. For an issue to happen here, it would require a macro expanding to a
|
|
|
|
|
// `simd_shuffle` call without wrapping the constant argument in a `const {}` block, but
|
|
|
|
|
// the user pass through arbitrary expressions.
|
|
|
|
|
// FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a real
|
2023-09-12 23:28:25 +02:00
|
|
|
// const generic, and get rid of this entire function.
|
2023-06-20 08:14:51 +00:00
|
|
|
other => span_bug!(constant.span, "{other:#?}"),
|
|
|
|
|
};
|
|
|
|
|
let uv = self.monomorphize(uv);
|
2024-03-14 09:10:28 +00:00
|
|
|
self.cx.tcx().const_eval_resolve_for_typeck(ty::ParamEnv::reveal_all(), uv, constant.span)
|
2023-06-20 08:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-26 13:45:41 +01:00
|
|
|
/// process constant containing SIMD shuffle indices
|
|
|
|
|
pub fn simd_shuffle_indices(
|
|
|
|
|
&mut self,
|
2018-09-20 15:47:22 +02:00
|
|
|
bx: &Bx,
|
2023-09-20 20:51:14 +02:00
|
|
|
constant: &mir::ConstOperand<'tcx>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> (Bx::Value, Ty<'tcx>) {
|
2023-06-20 08:14:51 +00:00
|
|
|
let ty = self.monomorphize(constant.ty());
|
|
|
|
|
let val = self
|
|
|
|
|
.eval_unevaluated_mir_constant_to_valtree(constant)
|
2023-06-16 16:02:11 +00:00
|
|
|
.ok()
|
|
|
|
|
.flatten()
|
2020-02-15 12:57:46 +13:00
|
|
|
.map(|val| {
|
|
|
|
|
let field_ty = ty.builtin_index().unwrap();
|
2023-06-16 16:02:11 +00:00
|
|
|
let values: Vec<_> = val
|
|
|
|
|
.unwrap_branch()
|
2020-06-09 15:57:08 +02:00
|
|
|
.iter()
|
2019-02-16 20:51:43 +01:00
|
|
|
.map(|field| {
|
2022-02-16 10:56:01 +01:00
|
|
|
if let Some(prim) = field.try_to_scalar() {
|
2018-11-27 19:00:25 +01: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);
|
2018-04-26 09:18:19 +02:00
|
|
|
};
|
2018-11-27 19:00:25 +01:00
|
|
|
bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
|
2018-04-26 09:18:19 +02:00
|
|
|
} else {
|
|
|
|
|
bug!("simd shuffle field {:?}", field)
|
2018-01-29 09:58:28 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2023-06-16 16:02:11 +00:00
|
|
|
bx.const_struct(&values, false)
|
2018-01-16 09:31:48 +01:00
|
|
|
})
|
2023-06-16 16:02:11 +00:00
|
|
|
.unwrap_or_else(|| {
|
2023-12-18 22:21:37 +11:00
|
|
|
bx.tcx().dcx().emit_err(errors::ShuffleIndicesEvaluation { span: constant.span });
|
2018-01-16 09:31:48 +01:00
|
|
|
// We've errored, so we don't have to produce working code.
|
2018-11-27 19:00:25 +01:00
|
|
|
let llty = bx.backend_type(bx.layout_of(ty));
|
2023-06-16 16:02:11 +00:00
|
|
|
bx.const_undef(llty)
|
|
|
|
|
});
|
|
|
|
|
(val, ty)
|
2017-01-02 11:00:42 -07:00
|
|
|
}
|
|
|
|
|
}
|