Skip to content

Commit 6b1dd82

Browse files
committed
Prepare mir::Constant for ty::Const only supporting valtrees
1 parent 1b1f6ea commit 6b1dd82

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/constant.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
88
use rustc_middle::mir::interpret::{
99
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Pointer, Scalar,
1010
};
11-
use rustc_middle::ty::{Const, ConstKind};
11+
use rustc_middle::ty::ConstKind;
1212

1313
use cranelift_codegen::ir::GlobalValueData;
1414
use cranelift_module::*;
@@ -39,7 +39,10 @@ impl ConstantCx {
3939
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
4040
let mut all_constants_ok = true;
4141
for constant in &fx.mir.required_consts {
42-
let const_ = fx.monomorphize(constant.literal);
42+
let const_ = match fx.monomorphize(constant.literal) {
43+
ConstantSource::Ty(ct) => ct,
44+
ConstantSource::Val(..) => continue,
45+
};
4346
match const_.val {
4447
ConstKind::Value(_) => {}
4548
ConstKind::Unevaluated(def, ref substs, promoted) => {
@@ -113,19 +116,17 @@ pub(crate) fn codegen_constant<'tcx>(
113116
fx: &mut FunctionCx<'_, '_, 'tcx>,
114117
constant: &Constant<'tcx>,
115118
) -> CValue<'tcx> {
116-
let const_ = fx.monomorphize(constant.literal);
119+
let const_ = match fx.monomorphize(constant.literal) {
120+
ConstantSource::Ty(ct) => ct,
121+
ConstantSource::Val(val, ty) => return codegen_const_value(fx, val, ty),
122+
};
117123
let const_val = match const_.val {
118124
ConstKind::Value(const_val) => const_val,
119125
ConstKind::Unevaluated(def, ref substs, promoted) if fx.tcx.is_static(def.did) => {
120126
assert!(substs.is_empty());
121127
assert!(promoted.is_none());
122128

123-
return codegen_static_ref(
124-
fx,
125-
def.did,
126-
fx.layout_of(fx.monomorphize(&constant.literal.ty)),
127-
)
128-
.to_cvalue(fx);
129+
return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty)).to_cvalue(fx);
129130
}
130131
ConstKind::Unevaluated(def, ref substs, promoted) => {
131132
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) {
@@ -422,11 +423,14 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
422423
pub(crate) fn mir_operand_get_const_val<'tcx>(
423424
fx: &FunctionCx<'_, '_, 'tcx>,
424425
operand: &Operand<'tcx>,
425-
) -> Option<&'tcx Const<'tcx>> {
426+
) -> Option<ConstValue<'tcx>> {
426427
match operand {
427428
Operand::Copy(_) | Operand::Move(_) => None,
428-
Operand::Constant(const_) => {
429-
Some(fx.monomorphize(const_.literal).eval(fx.tcx, ParamEnv::reveal_all()))
430-
}
429+
Operand::Constant(const_) => match const_.literal {
430+
ConstantSource::Ty(const_) => {
431+
fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).val.try_to_value()
432+
}
433+
ConstantSource::Val(val, _) => Some(val),
434+
},
431435
}
432436
}

src/intrinsics/llvm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
5353
};
5454
llvm.x86.sse2.cmp.ps | llvm.x86.sse2.cmp.pd, (c x, c y, o kind) {
5555
let kind_const = crate::constant::mir_operand_get_const_val(fx, kind).expect("llvm.x86.sse2.cmp.* kind not const");
56-
let flt_cc = match kind_const.val.try_to_bits(Size::from_bytes(1)).unwrap_or_else(|| panic!("kind not scalar: {:?}", kind_const)) {
56+
let flt_cc = match kind_const.try_to_bits(Size::from_bytes(1)).unwrap_or_else(|| panic!("kind not scalar: {:?}", kind_const)) {
5757
0 => FloatCC::Equal,
5858
1 => FloatCC::LessThan,
5959
2 => FloatCC::LessThanOrEqual,
@@ -84,7 +84,7 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
8484
llvm.x86.sse2.psrli.d, (c a, o imm8) {
8585
let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8).expect("llvm.x86.sse2.psrli.d imm8 not const");
8686
simd_for_each_lane(fx, a, ret, |fx, _lane_layout, res_lane_layout, lane| {
87-
let res_lane = match imm8.val.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) {
87+
let res_lane = match imm8.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) {
8888
imm8 if imm8 < 32 => fx.bcx.ins().ushr_imm(lane, i64::from(imm8 as u8)),
8989
_ => fx.bcx.ins().iconst(types::I32, 0),
9090
};
@@ -94,7 +94,7 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
9494
llvm.x86.sse2.pslli.d, (c a, o imm8) {
9595
let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8).expect("llvm.x86.sse2.psrli.d imm8 not const");
9696
simd_for_each_lane(fx, a, ret, |fx, _lane_layout, res_lane_layout, lane| {
97-
let res_lane = match imm8.val.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) {
97+
let res_lane = match imm8.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) {
9898
imm8 if imm8 < 32 => fx.bcx.ins().ishl_imm(lane, i64::from(imm8 as u8)),
9999
_ => fx.bcx.ins().iconst(types::I32, 0),
100100
};

src/intrinsics/simd.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
8585
use rustc_middle::mir::interpret::*;
8686
let idx_const = crate::constant::mir_operand_get_const_val(fx, idx).expect("simd_shuffle* idx not const");
8787

88-
let idx_bytes = match idx_const.val {
89-
ty::ConstKind::Value(ConstValue::ByRef { alloc, offset }) => {
88+
let idx_bytes = match idx_const {
89+
ConstValue::ByRef { alloc, offset } => {
9090
let ptr = Pointer::new(AllocId(0 /* dummy */), offset);
9191
let size = Size::from_bytes(4 * u64::from(ret_lane_count) /* size_of([u32; ret_lane_count]) */);
9292
alloc.get_bytes(fx, ptr, size).unwrap()
@@ -130,7 +130,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
130130
);
131131
};
132132

133-
let idx = idx_const.val.try_to_bits(Size::from_bytes(4 /* u32*/)).unwrap_or_else(|| panic!("kind not scalar: {:?}", idx_const));
133+
let idx = idx_const.try_to_bits(Size::from_bytes(4 /* u32*/)).unwrap_or_else(|| panic!("kind not scalar: {:?}", idx_const));
134134
let (lane_count, _lane_ty) = base.layout().ty.simd_size_and_type(fx.tcx);
135135
if idx >= lane_count.into() {
136136
fx.tcx.sess.span_fatal(fx.mir.span, &format!("[simd_insert] idx {} >= lane_count {}", idx, lane_count));
@@ -159,7 +159,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
159159
return;
160160
};
161161

162-
let idx = idx_const.val.try_to_bits(Size::from_bytes(4 /* u32*/)).unwrap_or_else(|| panic!("kind not scalar: {:?}", idx_const));
162+
let idx = idx_const.try_to_bits(Size::from_bytes(4 /* u32*/)).unwrap_or_else(|| panic!("kind not scalar: {:?}", idx_const));
163163
let (lane_count, _lane_ty) = v.layout().ty.simd_size_and_type(fx.tcx);
164164
if idx >= lane_count.into() {
165165
fx.tcx.sess.span_fatal(fx.mir.span, &format!("[simd_extract] idx {} >= lane_count {}", idx, lane_count));

0 commit comments

Comments
 (0)