implement valtrees as the type-system representation for constant values

This commit is contained in:
b-naber 2022-02-16 10:56:01 +01:00
parent edab34ab2a
commit 705d818bd5
116 changed files with 1606 additions and 1032 deletions

View file

@ -19,7 +19,7 @@ impl<'tcx> MirPass<'tcx> for ConstDebugInfo {
sess.opts.debugging_opts.unsound_mir_opts && sess.mir_opt_level() > 0
}
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
trace!("running ConstDebugInfo on {:?}", body.source);
for (local, constant) in find_optimization_oportunities(body) {

View file

@ -67,6 +67,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
true
}
#[instrument(skip(self, tcx), level = "debug")]
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// will be evaluated by miri and produce its errors there
if body.source.promoted.is_some() {
@ -687,7 +688,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
Operand::Constant(Box::new(Constant {
span,
user_ty: None,
literal: ty::Const::from_scalar(self.tcx, scalar, ty).into(),
literal: ConstantKind::from_scalar(self.tcx, scalar, ty),
}))
}
@ -765,20 +766,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
if let Some(Some(alloc)) = alloc {
// Assign entire constant in a single statement.
// We can't use aggregates, as we run after the aggregate-lowering `MirPhase`.
let const_val = ConstValue::ByRef { alloc, offset: Size::ZERO };
let literal = ConstantKind::Val(const_val, ty);
*rval = Rvalue::Use(Operand::Constant(Box::new(Constant {
span: source_info.span,
user_ty: None,
literal: self
.ecx
.tcx
.mk_const(ty::ConstS {
ty,
kind: ty::ConstKind::Value(ConstValue::ByRef {
alloc,
offset: Size::ZERO,
}),
})
.into(),
literal,
})));
}
}

View file

@ -470,7 +470,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
Rvalue::Use(Operand::Constant(Box::new(Constant {
span,
user_ty: None,
literal: ty::Const::from_bool(self.tcx, val).into(),
literal: ConstantKind::from_bool(self.tcx, val),
})))
}

View file

@ -991,7 +991,7 @@ fn insert_panic_block<'tcx>(
cond: Operand::Constant(Box::new(Constant {
span: body.span,
user_ty: None,
literal: ty::Const::from_bool(tcx, false).into(),
literal: ConstantKind::from_bool(tcx, false),
})),
expected: true,
msg: message,

View file

@ -3,8 +3,8 @@
use crate::MirPass;
use rustc_hir::Mutability;
use rustc_middle::mir::{
BinOp, Body, Constant, LocalDecls, Operand, Place, ProjectionElem, Rvalue, SourceInfo,
Statement, StatementKind, Terminator, TerminatorKind, UnOp,
BinOp, Body, Constant, ConstantKind, LocalDecls, Operand, Place, ProjectionElem, Rvalue,
SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
};
use rustc_middle::ty::{self, TyCtxt};
@ -129,8 +129,8 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
return;
}
let constant =
Constant { span: source_info.span, literal: len.into(), user_ty: None };
let literal = ConstantKind::from_const(len, self.tcx);
let constant = Constant { span: source_info.span, literal, user_ty: None };
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
}
}

View file

@ -531,8 +531,10 @@ fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
None => {}
Some(other) => panic!("do not use `optimized_mir` for constants: {:?}", other),
}
debug!("about to call mir_drops_elaborated...");
let mut body =
tcx.mir_drops_elaborated_and_const_checked(ty::WithOptConstParam::unknown(did)).steal();
debug!("body: {:#?}", body);
run_optimization_passes(tcx, &mut body);
debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR");

View file

@ -34,7 +34,7 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
Rvalue::Use(Operand::Constant(Box::new(Constant {
span: terminator.source_info.span,
user_ty: None,
literal: ty::Const::zero_sized(tcx, tcx.types.unit).into(),
literal: ConstantKind::zero_sized(tcx.types.unit),
}))),
))),
});

View file

@ -15,7 +15,7 @@ impl<'tcx> MirPass<'tcx> for RemoveNoopLandingPads {
sess.panic_strategy() != PanicStrategy::Abort
}
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!("remove_noop_landing_pads({:?})", body);
self.remove_nop_landing_pads(body)
}
@ -81,6 +81,8 @@ impl RemoveNoopLandingPads {
}
fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
debug!("body: {:#?}", body);
// make sure there's a single resume block
let resume_block = {
let patch = MirPatch::new(body);

View file

@ -430,7 +430,7 @@ impl<'tcx> CloneShimBuilder<'tcx> {
let func = Operand::Constant(Box::new(Constant {
span: self.span,
user_ty: None,
literal: ty::Const::zero_sized(tcx, func_ty).into(),
literal: ConstantKind::zero_sized(func_ty),
}));
let ref_loc = self.make_place(
@ -630,7 +630,7 @@ fn build_call_shim<'tcx>(
Operand::Constant(Box::new(Constant {
span,
user_ty: None,
literal: ty::Const::zero_sized(tcx, ty).into(),
literal: ConstantKind::zero_sized(ty),
})),
rcvr.into_iter().collect::<Vec<_>>(),
)