Don't use a 1-to-1 mapping between mir local and cranelift_frontend variable

This commit is contained in:
bjorn3 2020-06-27 11:58:44 +02:00
parent 4bfc4a14b6
commit 3227203fe6
5 changed files with 25 additions and 19 deletions

View File

@ -2,6 +2,8 @@ use std::borrow::Cow;
use rustc_middle::mir;
use cranelift_codegen::entity::EntityRef;
use crate::abi::pass_mode::*;
use crate::prelude::*;
@ -72,9 +74,9 @@ pub(super) fn add_local_place_comments<'tcx>(
} = layout;
let (kind, extra) = match *place.inner() {
CPlaceInner::Var(var) => {
assert_eq!(local, var);
("ssa", std::borrow::Cow::Borrowed(""))
CPlaceInner::Var(place_local, var) => {
assert_eq!(local, place_local);
("ssa", Cow::Owned(format!(",var={}", var.index())))
}
CPlaceInner::Addr(ptr, meta) => {
let meta = if let Some(meta) = meta {

View File

@ -51,6 +51,7 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>(
constants_cx: &mut cx.constants_cx,
vtables: &mut cx.vtables,
source_info_set: indexmap::IndexSet::new(),
next_ssa_var: 0,
};
let arg_uninhabited = fx.mir.args_iter().any(|arg| fx.layout_of(fx.monomorphize(&fx.mir.local_decls[arg].ty)).abi.is_uninhabited());

View File

@ -6,10 +6,6 @@ use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef};
use crate::prelude::*;
pub(crate) fn mir_var(loc: Local) -> Variable {
Variable::with_u32(loc.index() as u32)
}
pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
match tcx.data_layout.pointer_size.bits() {
16 => types::I16,
@ -258,6 +254,9 @@ pub(crate) struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
pub(crate) vtables: &'clif mut FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
pub(crate) source_info_set: indexmap::IndexSet<SourceInfo>,
/// This should only be accessed by `CPlace::new_var`.
pub(crate) next_ssa_var: u32,
}
impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, 'tcx, B> {

View File

@ -4,6 +4,7 @@ mod unwind;
use crate::prelude::*;
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::ValueLocRange;
@ -376,8 +377,8 @@ fn place_location<'tcx>(
assert!(place.projection.is_empty()); // FIXME implement them
match local_map[&place.local].inner() {
CPlaceInner::Var(local) => {
let value_label = cranelift_codegen::ir::ValueLabel::from_u32(local.as_u32());
CPlaceInner::Var(_local, var) => {
let value_label = cranelift_codegen::ir::ValueLabel::new(var.index());
if let Some(value_loc_ranges) = value_labels_ranges.get(&value_label) {
let loc_list = LocationList(
value_loc_ranges

View File

@ -1,5 +1,6 @@
use crate::prelude::*;
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::immediates::Offset32;
fn codegen_field<'tcx>(
@ -256,7 +257,7 @@ pub(crate) struct CPlace<'tcx> {
#[derive(Debug, Copy, Clone)]
pub(crate) enum CPlaceInner {
Var(Local),
Var(Local, Variable),
Addr(Pointer, Option<Value>),
}
@ -301,10 +302,12 @@ impl<'tcx> CPlace<'tcx> {
local: Local,
layout: TyAndLayout<'tcx>,
) -> CPlace<'tcx> {
let var = Variable::with_u32(fx.next_ssa_var);
fx.next_ssa_var += 1;
fx.bcx
.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
.declare_var(var, fx.clif_type(layout.ty).unwrap());
CPlace {
inner: CPlaceInner::Var(local),
inner: CPlaceInner::Var(local, var),
layout,
}
}
@ -326,9 +329,9 @@ impl<'tcx> CPlace<'tcx> {
pub(crate) fn to_cvalue(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>) -> CValue<'tcx> {
let layout = self.layout();
match self.inner {
CPlaceInner::Var(var) => {
let val = fx.bcx.use_var(mir_var(var));
fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::from_u32(var.as_u32()));
CPlaceInner::Var(_local, var) => {
let val = fx.bcx.use_var(var);
fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index()));
CValue::by_val(val, layout)
}
CPlaceInner::Addr(ptr, extra) => {
@ -351,7 +354,7 @@ impl<'tcx> CPlace<'tcx> {
pub(crate) fn to_ptr_maybe_unsized(self) -> (Pointer, Option<Value>) {
match self.inner {
CPlaceInner::Addr(ptr, extra) => (ptr, extra),
CPlaceInner::Var(_) => bug!("Expected CPlace::Addr, found {:?}", self),
CPlaceInner::Var(_, _) => bug!("Expected CPlace::Addr, found {:?}", self),
}
}
@ -442,7 +445,7 @@ impl<'tcx> CPlace<'tcx> {
let dst_layout = self.layout();
let to_ptr = match self.inner {
CPlaceInner::Var(var) => {
CPlaceInner::Var(_local, var) => {
let data = CValue(from.0, dst_layout).load_scalar(fx);
let src_ty = fx.bcx.func.dfg.value_type(data);
let dst_ty = fx.clif_type(self.layout().ty).unwrap();
@ -459,8 +462,8 @@ impl<'tcx> CPlace<'tcx> {
}
_ => unreachable!("write_cvalue_transmute: {:?} -> {:?}", src_ty, dst_ty),
};
fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::from_u32(var.as_u32()));
fx.bcx.def_var(mir_var(var), data);
fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var.index()));
fx.bcx.def_var(var, data);
return;
}
CPlaceInner::Addr(ptr, None) => {