Remove ancient register allocation (#3401)

This commit is contained in:
Benjamin Bouvier
2021-09-30 21:27:23 +02:00
committed by GitHub
parent 80336f4535
commit bae4ec6427
66 changed files with 112 additions and 15380 deletions

View File

@@ -73,31 +73,19 @@ impl Compiler {
let start_srcloc = FilePos::new(offset as u32);
let end_srcloc = FilePos::new((offset + len) as u32);
let instructions = if let Some(ref mcr) = &context.mach_compile_result {
// New-style backend: we have a `MachCompileResult` that will give us `MachSrcLoc` mapping
// tuples.
collect_address_maps(
body_len,
mcr.buffer
.get_srclocs_sorted()
.into_iter()
.map(|&MachSrcLoc { start, end, loc }| (loc, start, (end - start))),
)
} else {
// Old-style backend: we need to traverse the instruction/encoding info in the function.
let func = &context.func;
let mut blocks = func.layout.blocks().collect::<Vec<_>>();
blocks.sort_by_key(|block| func.offsets[*block]); // Ensure inst offsets always increase
let encinfo = self.isa.encoding_info();
collect_address_maps(
body_len,
blocks
.into_iter()
.flat_map(|block| func.inst_offsets(block, &encinfo))
.map(|(offset, inst, size)| (func.srclocs[inst], offset, size)),
)
};
// New-style backend: we have a `MachCompileResult` that will give us `MachSrcLoc` mapping
// tuples.
let instructions = collect_address_maps(
body_len,
context
.mach_compile_result
.as_ref()
.unwrap()
.buffer
.get_srclocs_sorted()
.into_iter()
.map(|&MachSrcLoc { start, end, loc }| (loc, start, (end - start))),
);
FunctionAddressMap {
instructions: instructions.into(),
@@ -196,10 +184,14 @@ impl wasmtime_environ::Compiler for Compiler {
self.get_function_address_map(&context, &input, code_buf.len() as u32);
let ranges = if tunables.generate_native_debuginfo {
let ranges = context.build_value_labels_ranges(isa).map_err(|error| {
CompileError::Codegen(pretty_error(&context.func, Some(isa), error))
})?;
Some(ranges)
Some(
context
.mach_compile_result
.as_ref()
.unwrap()
.value_labels_ranges
.clone(),
)
} else {
None
};

View File

@@ -1,7 +1,7 @@
use super::address_transform::AddressTransform;
use crate::debug::ModuleMemoryOffset;
use anyhow::{Context, Error, Result};
use cranelift_codegen::ir::{LabelValueLoc, StackSlots, ValueLabel, ValueLoc};
use cranelift_codegen::ir::{LabelValueLoc, StackSlots, ValueLabel};
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::ValueLabelsRanges;
use cranelift_wasm::get_vmctx_value_label;
@@ -129,40 +129,12 @@ impl CompiledExpression {
}
}
const X86_64_STACK_OFFSET: i64 = 16;
fn translate_loc(
loc: LabelValueLoc,
frame_info: Option<&FunctionFrameInfo>,
isa: &dyn TargetIsa,
add_stack_value: bool,
) -> Result<Option<Vec<u8>>> {
Ok(match loc {
LabelValueLoc::ValueLoc(ValueLoc::Reg(reg)) => {
let machine_reg = isa.map_dwarf_register(reg)?;
let mut writer = ExpressionWriter::new();
if add_stack_value {
writer.write_op_reg(machine_reg)?;
} else {
writer.write_op_breg(machine_reg)?;
writer.write_sleb128(0)?;
}
Some(writer.into_vec())
}
LabelValueLoc::ValueLoc(ValueLoc::Stack(ss)) => {
if let Some(frame_info) = frame_info {
if let Some(ss_offset) = frame_info.stack_slots[ss].offset {
let mut writer = ExpressionWriter::new();
writer.write_op_breg(X86_64::RBP.0)?;
writer.write_sleb128(ss_offset as i64 + X86_64_STACK_OFFSET)?;
if !add_stack_value {
writer.write_op(gimli::constants::DW_OP_deref)?;
}
return Ok(Some(writer.into_vec()));
}
}
None
}
LabelValueLoc::Reg(r) => {
let machine_reg = isa.map_regalloc_reg_to_dwarf(r)?;
let mut writer = ExpressionWriter::new();
@@ -183,8 +155,6 @@ fn translate_loc(
}
return Ok(Some(writer.into_vec()));
}
_ => None,
})
}
@@ -197,35 +167,6 @@ fn append_memory_deref(
let mut writer = ExpressionWriter::new();
// FIXME for imported memory
match vmctx_loc {
LabelValueLoc::ValueLoc(ValueLoc::Reg(vmctx_reg)) => {
let reg = isa.map_dwarf_register(vmctx_reg)? as u8;
writer.write_u8(gimli::constants::DW_OP_breg0.0 + reg)?;
let memory_offset = match frame_info.vmctx_memory_offset() {
Some(offset) => offset,
None => {
return Ok(false);
}
};
writer.write_sleb128(memory_offset)?;
}
LabelValueLoc::ValueLoc(ValueLoc::Stack(ss)) => {
if let Some(ss_offset) = frame_info.stack_slots[ss].offset {
writer.write_op_breg(X86_64::RBP.0)?;
writer.write_sleb128(ss_offset as i64 + X86_64_STACK_OFFSET)?;
writer.write_op(gimli::constants::DW_OP_deref)?;
writer.write_op(gimli::constants::DW_OP_consts)?;
let memory_offset = match frame_info.vmctx_memory_offset() {
Some(offset) => offset,
None => {
return Ok(false);
}
};
writer.write_sleb128(memory_offset)?;
writer.write_op(gimli::constants::DW_OP_plus)?;
} else {
return Ok(false);
}
}
LabelValueLoc::Reg(r) => {
let reg = isa.map_regalloc_reg_to_dwarf(r)?;
writer.write_op_breg(reg)?;
@@ -251,9 +192,6 @@ fn append_memory_deref(
writer.write_sleb128(memory_offset)?;
writer.write_op(gimli::constants::DW_OP_plus)?;
}
_ => {
return Ok(false);
}
}
writer.write_op(gimli::constants::DW_OP_deref)?;
writer.write_op(gimli::constants::DW_OP_swap)?;
@@ -416,9 +354,7 @@ impl CompiledExpression {
CompiledExpressionPart::Local { label, trailing } => {
let loc =
*label_location.get(&label).context("label_location")?;
if let Some(expr) =
translate_loc(loc, frame_info, isa, *trailing)?
{
if let Some(expr) = translate_loc(loc, isa, *trailing)? {
code_buf.extend_from_slice(&expr)
} else {
return Ok(None);
@@ -1221,7 +1157,7 @@ mod tests {
}
fn create_mock_value_ranges() -> (ValueLabelsRanges, (ValueLabel, ValueLabel, ValueLabel)) {
use cranelift_codegen::ir::{LabelValueLoc, ValueLoc};
use cranelift_codegen::ir::LabelValueLoc;
use cranelift_codegen::ValueLocRange;
use cranelift_entity::EntityRef;
use std::collections::HashMap;
@@ -1232,7 +1168,7 @@ mod tests {
value_ranges.insert(
value_0,
vec![ValueLocRange {
loc: LabelValueLoc::ValueLoc(ValueLoc::Unassigned),
loc: LabelValueLoc::SPOffset(0),
start: 0,
end: 25,
}],
@@ -1240,7 +1176,7 @@ mod tests {
value_ranges.insert(
value_1,
vec![ValueLocRange {
loc: LabelValueLoc::ValueLoc(ValueLoc::Unassigned),
loc: LabelValueLoc::SPOffset(0),
start: 5,
end: 30,
}],
@@ -1249,12 +1185,12 @@ mod tests {
value_2,
vec![
ValueLocRange {
loc: LabelValueLoc::ValueLoc(ValueLoc::Unassigned),
loc: LabelValueLoc::SPOffset(0),
start: 0,
end: 10,
},
ValueLocRange {
loc: LabelValueLoc::ValueLoc(ValueLoc::Unassigned),
loc: LabelValueLoc::SPOffset(0),
start: 20,
end: 30,
},