Skip to content

Commit acdfec6

Browse files
committed
Move mir const to valtree conversion to its own method.
1 parent 168de14 commit acdfec6

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -863,22 +863,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
863863
// promotes any complex rvalues to constants.
864864
if i == 2 && intrinsic.as_str().starts_with("simd_shuffle") {
865865
if let mir::Operand::Constant(constant) = arg {
866-
let ct = self.monomorphize(constant.literal);
867-
let uv = match ct {
868-
mir::ConstantKind::Unevaluated(uv, _) => uv.shrink(),
869-
other => span_bug!(constant.span, "{other:#?}"),
870-
};
871-
let c = self.cx.tcx().const_eval_resolve_for_typeck(
872-
ty::ParamEnv::reveal_all(),
873-
uv,
874-
Some(constant.span),
875-
);
876-
let (llval, ty) = self.simd_shuffle_indices(
877-
&bx,
878-
constant.span,
879-
self.monomorphize(constant.ty()),
880-
c,
881-
);
866+
let (llval, ty) = self.simd_shuffle_indices(&bx, constant);
882867
return OperandRef {
883868
val: Immediate(llval),
884869
layout: bx.layout_of(ty),

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_middle::mir;
55
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
66
use rustc_middle::ty::layout::HasTyCtxt;
77
use rustc_middle::ty::{self, Ty};
8-
use rustc_span::source_map::Span;
98
use rustc_target::abi::Abi;
109

1110
use super::FunctionCx;
@@ -59,15 +58,34 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
5958
})
6059
}
6160

61+
/// This is a convenience helper for `simd_shuffle_indices`. It has the precondition
62+
/// that the given `constant` is an `ConstantKind::Unevaluated` and must be convertible to
63+
/// a `ValTree`. If you want a more general version of this, talk to `wg-const-eval` on zulip.
64+
pub fn eval_unevaluated_mir_constant_to_valtree(
65+
&self,
66+
constant: &mir::Constant<'tcx>,
67+
) -> Result<Option<ty::ValTree<'tcx>>, ErrorHandled> {
68+
let uv = match constant.literal {
69+
mir::ConstantKind::Unevaluated(uv, _) => uv.shrink(),
70+
other => span_bug!(constant.span, "{other:#?}"),
71+
};
72+
let uv = self.monomorphize(uv);
73+
self.cx.tcx().const_eval_resolve_for_typeck(
74+
ty::ParamEnv::reveal_all(),
75+
uv,
76+
Some(constant.span),
77+
)
78+
}
79+
6280
/// process constant containing SIMD shuffle indices
6381
pub fn simd_shuffle_indices(
6482
&mut self,
6583
bx: &Bx,
66-
span: Span,
67-
ty: Ty<'tcx>,
68-
constant: Result<Option<ty::ValTree<'tcx>>, ErrorHandled>,
84+
constant: &mir::Constant<'tcx>,
6985
) -> (Bx::Value, Ty<'tcx>) {
70-
let val = constant
86+
let ty = self.monomorphize(constant.ty());
87+
let val = self
88+
.eval_unevaluated_mir_constant_to_valtree(constant)
7189
.ok()
7290
.flatten()
7391
.map(|val| {
@@ -90,9 +108,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
90108
bx.const_struct(&values, false)
91109
})
92110
.unwrap_or_else(|| {
93-
bx.tcx().sess.emit_err(errors::ShuffleIndicesEvaluation { span });
111+
bx.tcx().sess.emit_err(errors::ShuffleIndicesEvaluation { span: constant.span });
94112
// We've errored, so we don't have to produce working code.
95-
let ty = self.monomorphize(ty);
96113
let llty = bx.backend_type(bx.layout_of(ty));
97114
bx.const_undef(llty)
98115
});

0 commit comments

Comments
 (0)