summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_codegen_cranelift/src/constant.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:25 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:25 +0000
commit5363f350887b1e5b5dd21a86f88c8af9d7fea6da (patch)
tree35ca005eb6e0e9a1ba3bb5dbc033209ad445dc17 /compiler/rustc_codegen_cranelift/src/constant.rs
parentAdding debian version 1.66.0+dfsg1-1. (diff)
downloadrustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.tar.xz
rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.zip
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_codegen_cranelift/src/constant.rs')
-rw-r--r--compiler/rustc_codegen_cranelift/src/constant.rs77
1 files changed, 30 insertions, 47 deletions
diff --git a/compiler/rustc_codegen_cranelift/src/constant.rs b/compiler/rustc_codegen_cranelift/src/constant.rs
index 148b66d95..a6bde8840 100644
--- a/compiler/rustc_codegen_cranelift/src/constant.rs
+++ b/compiler/rustc_codegen_cranelift/src/constant.rs
@@ -38,22 +38,8 @@ impl ConstantCx {
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
let mut all_constants_ok = true;
for constant in &fx.mir.required_consts {
- let unevaluated = match fx.monomorphize(constant.literal) {
- ConstantKind::Ty(_) => unreachable!(),
- ConstantKind::Unevaluated(uv, _) => uv,
- ConstantKind::Val(..) => continue,
- };
-
- if let Err(err) = fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
+ if eval_mir_constant(fx, constant).is_none() {
all_constants_ok = false;
- match err {
- ErrorHandled::Reported(_) | ErrorHandled::Linted => {
- fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
- }
- ErrorHandled::TooGeneric => {
- span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
- }
- }
}
}
all_constants_ok
@@ -80,15 +66,15 @@ pub(crate) fn codegen_tls_ref<'tcx>(
}
pub(crate) fn eval_mir_constant<'tcx>(
- fx: &mut FunctionCx<'_, '_, 'tcx>,
+ fx: &FunctionCx<'_, '_, 'tcx>,
constant: &Constant<'tcx>,
-) -> (ConstValue<'tcx>, Ty<'tcx>) {
+) -> Option<(ConstValue<'tcx>, Ty<'tcx>)> {
let constant_kind = fx.monomorphize(constant.literal);
let uv = match constant_kind {
ConstantKind::Ty(const_) => match const_.kind() {
ty::ConstKind::Unevaluated(uv) => uv.expand(),
ty::ConstKind::Value(val) => {
- return (fx.tcx.valtree_to_const_val((const_.ty(), val)), const_.ty());
+ return Some((fx.tcx.valtree_to_const_val((const_.ty(), val)), const_.ty()));
}
err => span_bug!(
constant.span,
@@ -102,22 +88,31 @@ pub(crate) fn eval_mir_constant<'tcx>(
span_bug!(constant.span, "MIR constant refers to static");
}
ConstantKind::Unevaluated(uv, _) => uv,
- ConstantKind::Val(val, _) => return (val, constant_kind.ty()),
+ ConstantKind::Val(val, _) => return Some((val, constant_kind.ty())),
};
- (
- fx.tcx.const_eval_resolve(ty::ParamEnv::reveal_all(), uv, None).unwrap_or_else(|_err| {
- span_bug!(constant.span, "erroneous constant not captured by required_consts");
- }),
- constant_kind.ty(),
- )
+ let val = fx
+ .tcx
+ .const_eval_resolve(ty::ParamEnv::reveal_all(), uv, None)
+ .map_err(|err| match err {
+ ErrorHandled::Reported(_) => {
+ fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
+ }
+ ErrorHandled::TooGeneric => {
+ span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
+ }
+ })
+ .ok();
+ val.map(|val| (val, constant_kind.ty()))
}
pub(crate) fn codegen_constant_operand<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
constant: &Constant<'tcx>,
) -> CValue<'tcx> {
- let (const_val, ty) = eval_mir_constant(fx, constant);
+ let (const_val, ty) = eval_mir_constant(fx, constant).unwrap_or_else(|| {
+ span_bug!(constant.span, "erroneous constant not captured by required_consts")
+ });
codegen_const_value(fx, const_val, ty)
}
@@ -128,7 +123,7 @@ pub(crate) fn codegen_const_value<'tcx>(
ty: Ty<'tcx>,
) -> CValue<'tcx> {
let layout = fx.layout_of(ty);
- assert!(!layout.is_unsized(), "sized const value");
+ assert!(layout.is_sized(), "unsized const value");
if layout.is_zst() {
return CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout);
@@ -262,9 +257,9 @@ pub(crate) fn data_id_for_alloc_id(
mutability: rustc_hir::Mutability,
) -> DataId {
cx.todo.push(TodoItem::Alloc(alloc_id));
- *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
- module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap()
- })
+ *cx.anon_allocs
+ .entry(alloc_id)
+ .or_insert_with(|| module.declare_anonymous_data(mutability.is_mut(), false).unwrap())
}
fn data_id_for_static(
@@ -348,12 +343,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
}
};
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
- module
- .declare_anonymous_data(
- alloc.inner().mutability == rustc_hir::Mutability::Mut,
- false,
- )
- .unwrap()
+ module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
});
(data_id, alloc, None)
}
@@ -398,7 +388,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len()).to_vec();
data_ctx.define(bytes.into_boxed_slice());
- for &(offset, alloc_id) in alloc.provenance().iter() {
+ for &(offset, alloc_id) in alloc.provenance().ptrs().iter() {
let addend = {
let endianness = tcx.data_layout.endian;
let offset = offset.bytes() as usize;
@@ -431,7 +421,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
{
tcx.sess.fatal(&format!(
"Allocation {:?} contains reference to TLS value {:?}",
- alloc, def_id
+ alloc_id, def_id
));
}
@@ -453,20 +443,13 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
assert!(cx.todo.is_empty(), "{:?}", cx.todo);
}
+/// Used only for intrinsic implementations that need a compile-time constant
pub(crate) fn mir_operand_get_const_val<'tcx>(
fx: &FunctionCx<'_, '_, 'tcx>,
operand: &Operand<'tcx>,
) -> Option<ConstValue<'tcx>> {
match operand {
- Operand::Constant(const_) => match fx.monomorphize(const_.literal) {
- ConstantKind::Ty(const_) => Some(
- const_.eval_for_mir(fx.tcx, ParamEnv::reveal_all()).try_to_value(fx.tcx).unwrap(),
- ),
- ConstantKind::Val(val, _) => Some(val),
- ConstantKind::Unevaluated(uv, _) => {
- Some(fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), uv, None).unwrap())
- }
- },
+ Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).unwrap().0),
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
// inside a temporary before being passed to the intrinsic requiring the const argument.
// This code tries to find a single constant defining definition of the referenced local.