Implement an incremental compilation cache for Cranelift (#4551)

This is the implementation of https://github.com/bytecodealliance/wasmtime/issues/4155, using the "inverted API" approach suggested by @cfallin (thanks!) in Cranelift, and trait object to provide a backend for an all-included experience in Wasmtime. 

After the suggestion of Chris, `Function` has been split into mostly two parts:

- on the one hand, `FunctionStencil` contains all the fields required during compilation, and that act as a compilation cache key: if two function stencils are the same, then the result of their compilation (`CompiledCodeBase<Stencil>`) will be the same. This makes caching trivial, as the only thing to cache is the `FunctionStencil`.
- on the other hand, `FunctionParameters` contain the... function parameters that are required to finalize the result of compilation into a `CompiledCode` (aka `CompiledCodeBase<Final>`) with proper final relocations etc., by applying fixups and so on.

Most changes are here to accomodate those requirements, in particular that `FunctionStencil` should be `Hash`able to be used as a key in the cache:

- most source locations are now relative to a base source location in the function, and as such they're encoded as `RelSourceLoc` in the `FunctionStencil`. This required changes so that there's no need to explicitly mark a `SourceLoc` as the base source location, it's automatically detected instead the first time a non-default `SourceLoc` is set.
- user-defined external names in the `FunctionStencil` (aka before this patch `ExternalName::User { namespace, index }`) are now references into an external table of `UserExternalNameRef -> UserExternalName`, present in the `FunctionParameters`, and must be explicitly declared using `Function::declare_imported_user_function`.
- some refactorings have been made for function names:
  - `ExternalName` was used as the type for a `Function`'s name; while it thus allowed `ExternalName::Libcall` in this place, this would have been quite confusing to use it there. Instead, a new enum `UserFuncName` is introduced for this name, that's either a user-defined function name (the above `UserExternalName`) or a test case name.
  - The future of `ExternalName` is likely to become a full reference into the `FunctionParameters`'s mapping, instead of being "either a handle for user-defined external names, or the thing itself for other variants". I'm running out of time to do this, and this is not trivial as it implies touching ISLE which I'm less familiar with.

The cache computes a sha256 hash of the `FunctionStencil`, and uses this as the cache key. No equality check (using `PartialEq`) is performed in addition to the hash being the same, as we hope that this is sufficient data to avoid collisions.

A basic fuzz target has been introduced that tries to do the bare minimum:

- check that a function successfully compiled and cached will be also successfully reloaded from the cache, and returns the exact same function.
- check that a trivial modification in the external mapping of `UserExternalNameRef -> UserExternalName` hits the cache, and that other modifications don't hit the cache.
  - This last check is less efficient and less likely to happen, so probably should be rethought a bit.

Thanks to both @alexcrichton and @cfallin for your very useful feedback on Zulip.

Some numbers show that for a large wasm module we're using internally, this is a 20% compile-time speedup, because so many `FunctionStencil`s are the same, even within a single module. For a group of modules that have a lot of code in common, we get hit rates up to 70% when they're used together. When a single function changes in a wasm module, every other function is reloaded; that's still slower than I expect (between 10% and 50% of the overall compile time), so there's likely room for improvement. 

Fixes #4155.
This commit is contained in:
Benjamin Bouvier
2022-08-12 18:47:43 +02:00
committed by GitHub
parent ac9725840d
commit 8a9b1a9025
103 changed files with 2176 additions and 693 deletions

View File

@@ -3,7 +3,7 @@
use regalloc2::Allocation;
use crate::binemit::{CodeOffset, Reloc, StackMap};
use crate::ir::types::*;
use crate::ir::{types::*, RelSourceLoc};
use crate::ir::{LibCall, MemFlags, TrapCode};
use crate::isa::aarch64::inst::*;
use crate::machinst::{ty_bits, Reg, RegClass, Writable};
@@ -617,7 +617,7 @@ pub struct EmitState {
/// Safepoint stack map for upcoming instruction, as provided to `pre_safepoint()`.
stack_map: Option<StackMap>,
/// Current source-code location corresponding to instruction to be emitted.
cur_srcloc: SourceLoc,
cur_srcloc: RelSourceLoc,
}
impl MachInstEmitState<Inst> for EmitState {
@@ -626,7 +626,7 @@ impl MachInstEmitState<Inst> for EmitState {
virtual_sp_offset: 0,
nominal_sp_to_fp: abi.frame_size() as i64,
stack_map: None,
cur_srcloc: SourceLoc::default(),
cur_srcloc: Default::default(),
}
}
@@ -634,7 +634,7 @@ impl MachInstEmitState<Inst> for EmitState {
self.stack_map = Some(stack_map);
}
fn pre_sourceloc(&mut self, srcloc: SourceLoc) {
fn pre_sourceloc(&mut self, srcloc: RelSourceLoc) {
self.cur_srcloc = srcloc;
}
}
@@ -648,7 +648,7 @@ impl EmitState {
self.stack_map = None;
}
fn cur_srcloc(&self) -> SourceLoc {
fn cur_srcloc(&self) -> RelSourceLoc {
self.cur_srcloc
}
}
@@ -954,7 +954,7 @@ impl MachInstEmit for Inst {
};
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() && !flags.notrap() {
if !srcloc.is_default() && !flags.notrap() {
// Register the offset at which the actual load instruction starts.
sink.add_trap(TrapCode::HeapOutOfBounds);
}
@@ -1074,7 +1074,7 @@ impl MachInstEmit for Inst {
};
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() && !flags.notrap() {
if !srcloc.is_default() && !flags.notrap() {
// Register the offset at which the actual store instruction starts.
sink.add_trap(TrapCode::HeapOutOfBounds);
}
@@ -1151,7 +1151,7 @@ impl MachInstEmit for Inst {
let rt2 = allocs.next(rt2);
let mem = mem.with_allocs(&mut allocs);
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() && !flags.notrap() {
if !srcloc.is_default() && !flags.notrap() {
// Register the offset at which the actual store instruction starts.
sink.add_trap(TrapCode::HeapOutOfBounds);
}
@@ -1183,7 +1183,7 @@ impl MachInstEmit for Inst {
let rt2 = allocs.next(rt2.to_reg());
let mem = mem.with_allocs(&mut allocs);
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() && !flags.notrap() {
if !srcloc.is_default() && !flags.notrap() {
// Register the offset at which the actual load instruction starts.
sink.add_trap(TrapCode::HeapOutOfBounds);
}
@@ -1223,7 +1223,7 @@ impl MachInstEmit for Inst {
let mem = mem.with_allocs(&mut allocs);
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() && !flags.notrap() {
if !srcloc.is_default() && !flags.notrap() {
// Register the offset at which the actual load instruction starts.
sink.add_trap(TrapCode::HeapOutOfBounds);
}
@@ -1269,7 +1269,7 @@ impl MachInstEmit for Inst {
let mem = mem.with_allocs(&mut allocs);
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() && !flags.notrap() {
if !srcloc.is_default() && !flags.notrap() {
// Register the offset at which the actual store instruction starts.
sink.add_trap(TrapCode::HeapOutOfBounds);
}
@@ -1417,7 +1417,7 @@ impl MachInstEmit for Inst {
// again:
sink.bind_label(again_label);
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
sink.put4(enc_ldaxr(ty, x27wr, x25)); // ldaxr x27, [x25]
@@ -1541,7 +1541,7 @@ impl MachInstEmit for Inst {
}
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
if op == AtomicRMWLoopOp::Xchg {
@@ -1603,7 +1603,7 @@ impl MachInstEmit for Inst {
// again:
sink.bind_label(again_label);
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
// ldaxr x27, [x25]
@@ -1630,7 +1630,7 @@ impl MachInstEmit for Inst {
sink.use_label_at_offset(br_out_offset, out_label, LabelUse::Branch19);
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
sink.put4(enc_stlxr(ty, x24wr, x28, x25)); // stlxr w24, x28, [x25]
@@ -2662,7 +2662,7 @@ impl MachInstEmit for Inst {
let (q, size) = size.enc_size();
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() && !flags.notrap() {
if !srcloc.is_default() && !flags.notrap() {
// Register the offset at which the actual load instruction starts.
sink.add_trap(TrapCode::HeapOutOfBounds);
}

View File

@@ -4,7 +4,7 @@ use crate::binemit::{Addend, CodeOffset, Reloc};
use crate::ir::types::{
B1, B128, B16, B32, B64, B8, F32, F64, FFLAGS, I128, I16, I32, I64, I8, I8X16, IFLAGS, R32, R64,
};
use crate::ir::{types, ExternalName, MemFlags, Opcode, SourceLoc, Type};
use crate::ir::{types, ExternalName, MemFlags, Opcode, Type};
use crate::isa::CallConv;
use crate::machinst::*;
use crate::{settings, CodegenError, CodegenResult};
@@ -2704,7 +2704,7 @@ impl Inst {
&Inst::EmitIsland { needed_space } => format!("emit_island {}", needed_space),
&Inst::ElfTlsGetAddr { ref symbol } => {
format!("x0 = elf_tls_get_addr {}", symbol)
format!("x0 = elf_tls_get_addr {}", symbol.display(None))
}
&Inst::Unwind { ref inst } => {
format!("unwind {:?}", inst)

View File

@@ -70,8 +70,7 @@ impl crate::isa::unwind::systemv::RegisterMapper<Reg> for RegisterMapper {
mod tests {
use crate::cursor::{Cursor, FuncCursor};
use crate::ir::{
types, AbiParam, ExternalName, Function, InstBuilder, Signature, StackSlotData,
StackSlotKind,
types, AbiParam, Function, InstBuilder, Signature, StackSlotData, StackSlotKind,
};
use crate::isa::{lookup, CallConv};
use crate::settings::{builder, Flags};
@@ -108,8 +107,7 @@ mod tests {
}
fn create_function(call_conv: CallConv, stack_slot: Option<StackSlotData>) -> Function {
let mut func =
Function::with_name_signature(ExternalName::user(0, 0), Signature::new(call_conv));
let mut func = Function::with_name_signature(Default::default(), Signature::new(call_conv));
let block0 = func.dfg.make_block();
let mut pos = FuncCursor::new(&mut func);
@@ -153,7 +151,7 @@ mod tests {
fn create_multi_return_function(call_conv: CallConv) -> Function {
let mut sig = Signature::new(call_conv);
sig.params.push(AbiParam::new(types::I32));
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
let mut func = Function::with_name_signature(Default::default(), sig);
let block0 = func.dfg.make_block();
let v0 = func.dfg.append_block_param(block0, types::I32);

View File

@@ -7,7 +7,8 @@ use crate::isa::aarch64::settings as aarch64_settings;
use crate::isa::unwind::systemv;
use crate::isa::{Builder as IsaBuilder, TargetIsa};
use crate::machinst::{
compile, CompiledCode, MachTextSectionBuilder, Reg, TextSectionBuilder, VCode,
compile, CompiledCode, CompiledCodeStencil, MachTextSectionBuilder, Reg, TextSectionBuilder,
VCode,
};
use crate::result::CodegenResult;
use crate::settings as shared_settings;
@@ -65,7 +66,11 @@ impl AArch64Backend {
}
impl TargetIsa for AArch64Backend {
fn compile_function(&self, func: &Function, want_disasm: bool) -> CodegenResult<CompiledCode> {
fn compile_function(
&self,
func: &Function,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil> {
let flags = self.flags();
let (vcode, regalloc_result) = self.compile_vcode(func, flags.clone())?;
@@ -80,7 +85,7 @@ impl TargetIsa for AArch64Backend {
log::debug!("disassembly:\n{}", disasm);
}
Ok(CompiledCode {
Ok(CompiledCodeStencil {
buffer,
frame_size,
disasm: emit_result.disasm,
@@ -204,7 +209,7 @@ mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::ir::types::*;
use crate::ir::{AbiParam, ExternalName, Function, InstBuilder, JumpTableData, Signature};
use crate::ir::{AbiParam, Function, InstBuilder, JumpTableData, Signature, UserFuncName};
use crate::isa::CallConv;
use crate::settings;
use crate::settings::Configurable;
@@ -213,7 +218,7 @@ mod test {
#[test]
fn test_compile_function() {
let name = ExternalName::testcase("test0");
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));
@@ -252,7 +257,7 @@ mod test {
#[test]
fn test_branch_lowering() {
let name = ExternalName::testcase("test0");
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));
@@ -320,7 +325,7 @@ mod test {
#[test]
fn test_br_table() {
let name = ExternalName::testcase("test0");
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));

View File

@@ -49,7 +49,7 @@ use crate::flowgraph;
use crate::ir::{self, Function};
#[cfg(feature = "unwind")]
use crate::isa::unwind::systemv::RegisterMappingError;
use crate::machinst::{CompiledCode, TextSectionBuilder, UnwindInfoKind};
use crate::machinst::{CompiledCode, CompiledCodeStencil, TextSectionBuilder, UnwindInfoKind};
use crate::settings;
use crate::settings::SetResult;
use crate::CodegenResult;
@@ -230,7 +230,11 @@ pub trait TargetIsa: fmt::Display + Send + Sync {
fn dynamic_vector_bytes(&self, dynamic_ty: ir::Type) -> u32;
/// Compile the given function.
fn compile_function(&self, func: &Function, want_disasm: bool) -> CodegenResult<CompiledCode>;
fn compile_function(
&self,
func: &Function,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil>;
#[cfg(feature = "unwind")]
/// Map a regalloc::Reg to its corresponding DWARF register.

View File

@@ -326,7 +326,7 @@ impl PrettyPrint for MemArg {
&MemArg::Label { target } => target.to_string(),
&MemArg::Symbol {
ref name, offset, ..
} => format!("{} + {}", name, offset),
} => format!("{} + {}", name.display(None), offset),
// Eliminated by `mem_finalize()`.
&MemArg::InitialSPOffset { .. }
| &MemArg::NominalSPOffset { .. }

View File

@@ -1,8 +1,8 @@
//! S390x ISA: binary code emission.
use crate::binemit::{Reloc, StackMap};
use crate::ir::MemFlags;
use crate::ir::{SourceLoc, TrapCode};
use crate::ir::TrapCode;
use crate::ir::{MemFlags, RelSourceLoc};
use crate::isa::s390x::inst::*;
use crate::isa::s390x::settings as s390x_settings;
use crate::machinst::reg::count_operands;
@@ -133,7 +133,7 @@ pub fn mem_emit(
if add_trap && mem.can_trap() {
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
}
@@ -201,7 +201,7 @@ pub fn mem_rs_emit(
if add_trap && mem.can_trap() {
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
}
@@ -243,7 +243,7 @@ pub fn mem_imm8_emit(
if add_trap && mem.can_trap() {
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
}
@@ -281,7 +281,7 @@ pub fn mem_imm16_emit(
if add_trap && mem.can_trap() {
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
}
@@ -308,7 +308,7 @@ pub fn mem_mem_emit(
) {
if add_trap && (dst.can_trap() || src.can_trap()) {
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if srcloc != Default::default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
}
@@ -343,7 +343,7 @@ pub fn mem_vrx_emit(
if add_trap && mem.can_trap() {
let srcloc = state.cur_srcloc();
if srcloc != SourceLoc::default() {
if !srcloc.is_default() {
sink.add_trap(TrapCode::HeapOutOfBounds);
}
}
@@ -1256,7 +1256,7 @@ pub struct EmitState {
/// Safepoint stack map for upcoming instruction, as provided to `pre_safepoint()`.
stack_map: Option<StackMap>,
/// Current source-code location corresponding to instruction to be emitted.
cur_srcloc: SourceLoc,
cur_srcloc: RelSourceLoc,
}
impl MachInstEmitState<Inst> for EmitState {
@@ -1265,7 +1265,7 @@ impl MachInstEmitState<Inst> for EmitState {
virtual_sp_offset: 0,
initial_sp_offset: abi.frame_size() as i64,
stack_map: None,
cur_srcloc: SourceLoc::default(),
cur_srcloc: Default::default(),
}
}
@@ -1273,7 +1273,7 @@ impl MachInstEmitState<Inst> for EmitState {
self.stack_map = Some(stack_map);
}
fn pre_sourceloc(&mut self, srcloc: SourceLoc) {
fn pre_sourceloc(&mut self, srcloc: RelSourceLoc) {
self.cur_srcloc = srcloc;
}
}
@@ -1287,7 +1287,7 @@ impl EmitState {
self.stack_map = None;
}
fn cur_srcloc(&self) -> SourceLoc {
fn cur_srcloc(&self) -> RelSourceLoc {
self.cur_srcloc
}
}

View File

@@ -2932,10 +2932,12 @@ impl Inst {
let link = pretty_print_reg(link.to_reg(), allocs);
let tls_symbol = match &info.tls_symbol {
None => "".to_string(),
Some(SymbolReloc::TlsGd { name }) => format!(":tls_gdcall:{}", name),
Some(SymbolReloc::TlsGd { name }) => {
format!(":tls_gdcall:{}", name.display(None))
}
_ => unreachable!(),
};
format!("brasl {}, {}{}", link, info.dest, tls_symbol)
format!("brasl {}, {}{}", link, info.dest.display(None), tls_symbol)
}
&Inst::CallInd { link, ref info, .. } => {
let link = pretty_print_reg(link.to_reg(), allocs);
@@ -3003,8 +3005,10 @@ impl Inst {
let rd = pretty_print_reg(rd.to_reg(), allocs);
let tmp = pretty_print_reg(writable_spilltmp_reg().to_reg(), &mut empty_allocs);
let symbol = match &**symbol_reloc {
SymbolReloc::Absolute { name, offset } => format!("{} + {}", name, offset),
SymbolReloc::TlsGd { name } => format!("{}@tlsgd", name),
SymbolReloc::Absolute { name, offset } => {
format!("{} + {}", name.display(None), offset)
}
SymbolReloc::TlsGd { name } => format!("{}@tlsgd", name.display(None)),
};
format!("bras {}, 12 ; data {} ; lg {}, 0({})", tmp, symbol, rd, tmp)
}

View File

@@ -101,8 +101,7 @@ impl crate::isa::unwind::systemv::RegisterMapper<Reg> for RegisterMapper {
mod tests {
use crate::cursor::{Cursor, FuncCursor};
use crate::ir::{
types, AbiParam, ExternalName, Function, InstBuilder, Signature, StackSlotData,
StackSlotKind,
types, AbiParam, Function, InstBuilder, Signature, StackSlotData, StackSlotKind,
};
use crate::isa::{lookup, CallConv};
use crate::settings::{builder, Flags};
@@ -139,8 +138,7 @@ mod tests {
}
fn create_function(call_conv: CallConv, stack_slot: Option<StackSlotData>) -> Function {
let mut func =
Function::with_name_signature(ExternalName::user(0, 0), Signature::new(call_conv));
let mut func = Function::with_name_signature(Default::default(), Signature::new(call_conv));
let block0 = func.dfg.make_block();
let mut pos = FuncCursor::new(&mut func);
@@ -187,7 +185,7 @@ mod tests {
) -> Function {
let mut sig = Signature::new(call_conv);
sig.params.push(AbiParam::new(types::I32));
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
let mut func = Function::with_name_signature(Default::default(), sig);
let block0 = func.dfg.make_block();
let v0 = func.dfg.append_block_param(block0, types::I32);

View File

@@ -3,6 +3,7 @@
// Pull in the ISLE generated code.
pub mod generated_code;
use crate::ir::ExternalName;
// Types that the generated ISLE code uses via `use super::*`.
use crate::isa::s390x::abi::{S390xMachineDeps, REG_SAVE_AREA_SIZE};
use crate::isa::s390x::inst::{

View File

@@ -7,7 +7,8 @@ use crate::isa::s390x::settings as s390x_settings;
use crate::isa::unwind::systemv::RegisterMappingError;
use crate::isa::{Builder as IsaBuilder, TargetIsa};
use crate::machinst::{
compile, CompiledCode, MachTextSectionBuilder, Reg, TextSectionBuilder, VCode,
compile, CompiledCode, CompiledCodeStencil, MachTextSectionBuilder, Reg, TextSectionBuilder,
VCode,
};
use crate::result::CodegenResult;
use crate::settings as shared_settings;
@@ -63,7 +64,11 @@ impl S390xBackend {
}
impl TargetIsa for S390xBackend {
fn compile_function(&self, func: &Function, want_disasm: bool) -> CodegenResult<CompiledCode> {
fn compile_function(
&self,
func: &Function,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil> {
let flags = self.flags();
let (vcode, regalloc_result) = self.compile_vcode(func)?;
@@ -78,7 +83,7 @@ impl TargetIsa for S390xBackend {
log::debug!("disassembly:\n{}", disasm);
}
Ok(CompiledCode {
Ok(CompiledCodeStencil {
buffer,
frame_size,
disasm: emit_result.disasm,
@@ -186,7 +191,8 @@ mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::ir::types::*;
use crate::ir::{AbiParam, ExternalName, Function, InstBuilder, Signature};
use crate::ir::UserFuncName;
use crate::ir::{AbiParam, Function, InstBuilder, Signature};
use crate::isa::CallConv;
use crate::settings;
use crate::settings::Configurable;
@@ -195,7 +201,7 @@ mod test {
#[test]
fn test_compile_function() {
let name = ExternalName::testcase("test0");
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));
@@ -233,7 +239,7 @@ mod test {
#[test]
fn test_branch_lowering() {
let name = ExternalName::testcase("test0");
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));

View File

@@ -1,7 +1,7 @@
//! Implementation of the standard x64 ABI.
use crate::ir::types::*;
use crate::ir::{self, types, ExternalName, LibCall, MemFlags, Opcode, Signature, TrapCode, Type};
use crate::ir::{self, types, LibCall, MemFlags, Opcode, Signature, TrapCode, Type};
use crate::ir::{types::*, ExternalName};
use crate::isa;
use crate::isa::{unwind::UnwindInst, x64::inst::*, x64::settings as x64_settings, CallConv};
use crate::machinst::abi_impl::*;

View File

@@ -13,9 +13,11 @@
//! -- isa::x64::inst::emit_tests::test_x64_emit
use super::*;
use crate::ir::UserExternalNameRef;
use crate::isa::x64;
use alloc::boxed::Box;
use alloc::vec::Vec;
use cranelift_entity::EntityRef as _;
impl Inst {
fn neg(size: OperandSize, src: Writable<Reg>) -> Inst {
@@ -3395,17 +3397,14 @@ fn test_x64_emit() {
// CallKnown
insns.push((
Inst::call_known(
ExternalName::User {
namespace: 0,
index: 0,
},
ExternalName::User(UserExternalNameRef::new(0)),
smallvec![],
smallvec![],
PRegSet::default(),
Opcode::Call,
),
"E800000000",
"call User { namespace: 0, index: 0 }",
"call User(userextname0)",
));
// ========================================================
@@ -3449,38 +3448,29 @@ fn test_x64_emit() {
insns.push((
Inst::LoadExtName {
dst: Writable::from_reg(r11),
name: Box::new(ExternalName::User {
namespace: 0,
index: 0,
}),
name: Box::new(ExternalName::User(UserExternalNameRef::new(0))),
offset: 0,
},
"4C8B1D00000000",
"load_ext_name u0:0+0, %r11",
"load_ext_name userextname0+0, %r11",
));
insns.push((
Inst::LoadExtName {
dst: Writable::from_reg(r11),
name: Box::new(ExternalName::User {
namespace: 0,
index: 0,
}),
name: Box::new(ExternalName::User(UserExternalNameRef::new(0))),
offset: 0x12345678,
},
"4C8B1D000000004981C378563412",
"load_ext_name u0:0+305419896, %r11",
"load_ext_name userextname0+305419896, %r11",
));
insns.push((
Inst::LoadExtName {
dst: Writable::from_reg(r11),
name: Box::new(ExternalName::User {
namespace: 0,
index: 0,
}),
name: Box::new(ExternalName::User(UserExternalNameRef::new(0))),
offset: -0x12345678,
},
"4C8B1D000000004981EB78563412",
"load_ext_name u0:0+-305419896, %r11",
"load_ext_name userextname0+-305419896, %r11",
));
// ========================================================
@@ -4690,35 +4680,26 @@ fn test_x64_emit() {
insns.push((
Inst::ElfTlsGetAddr {
symbol: ExternalName::User {
namespace: 0,
index: 0,
},
symbol: ExternalName::User(UserExternalNameRef::new(0)),
},
"66488D3D00000000666648E800000000",
"%rax = elf_tls_get_addr User { namespace: 0, index: 0 }",
"%rax = elf_tls_get_addr User(userextname0)",
));
insns.push((
Inst::MachOTlsGetAddr {
symbol: ExternalName::User {
namespace: 0,
index: 0,
},
symbol: ExternalName::User(UserExternalNameRef::new(0)),
},
"488B3D00000000FF17",
"%rax = macho_tls_get_addr User { namespace: 0, index: 0 }",
"%rax = macho_tls_get_addr User(userextname0)",
));
insns.push((
Inst::CoffTlsGetAddr {
symbol: ExternalName::User {
namespace: 0,
index: 0,
},
symbol: ExternalName::User(UserExternalNameRef::new(0)),
},
"8B050000000065488B0C2558000000488B04C1488D8000000000",
"%rax = coff_tls_get_addr User { namespace: 0, index: 0 }",
"%rax = coff_tls_get_addr User(userextname0)",
));
// ========================================================

View File

@@ -1,7 +1,7 @@
//! This module defines x86_64-specific machine instruction types.
use crate::binemit::{Addend, CodeOffset, Reloc, StackMap};
use crate::ir::{types, ExternalName, Opcode, SourceLoc, TrapCode, Type};
use crate::ir::{types, ExternalName, Opcode, RelSourceLoc, TrapCode, Type};
use crate::isa::x64::abi::X64ABIMachineSpec;
use crate::isa::x64::inst::regs::pretty_print_reg;
use crate::isa::x64::settings as x64_settings;
@@ -1624,7 +1624,7 @@ impl PrettyPrint for Inst {
format!(
"{} {}+{}, {}",
ljustify("load_ext_name".into()),
name,
name.display(None),
offset,
dst,
)
@@ -2424,7 +2424,7 @@ pub struct EmitState {
/// Safepoint stack map for upcoming instruction, as provided to `pre_safepoint()`.
stack_map: Option<StackMap>,
/// Current source location.
cur_srcloc: SourceLoc,
cur_srcloc: RelSourceLoc,
}
/// Constant state used during emissions of a sequence of instructions.
@@ -2465,7 +2465,7 @@ impl MachInstEmitState<Inst> for EmitState {
virtual_sp_offset: 0,
nominal_sp_to_fp: abi.frame_size() as i64,
stack_map: None,
cur_srcloc: SourceLoc::default(),
cur_srcloc: Default::default(),
}
}
@@ -2473,7 +2473,7 @@ impl MachInstEmitState<Inst> for EmitState {
self.stack_map = Some(stack_map);
}
fn pre_sourceloc(&mut self, srcloc: SourceLoc) {
fn pre_sourceloc(&mut self, srcloc: RelSourceLoc) {
self.cur_srcloc = srcloc;
}
}

View File

@@ -97,8 +97,7 @@ impl crate::isa::unwind::systemv::RegisterMapper<Reg> for RegisterMapper {
mod tests {
use crate::cursor::{Cursor, FuncCursor};
use crate::ir::{
types, AbiParam, ExternalName, Function, InstBuilder, Signature, StackSlotData,
StackSlotKind,
types, AbiParam, Function, InstBuilder, Signature, StackSlotData, StackSlotKind,
};
use crate::isa::{lookup, CallConv};
use crate::settings::{builder, Flags};
@@ -135,8 +134,7 @@ mod tests {
}
fn create_function(call_conv: CallConv, stack_slot: Option<StackSlotData>) -> Function {
let mut func =
Function::with_name_signature(ExternalName::user(0, 0), Signature::new(call_conv));
let mut func = Function::with_name_signature(Default::default(), Signature::new(call_conv));
let block0 = func.dfg.make_block();
let mut pos = FuncCursor::new(&mut func);
@@ -177,7 +175,7 @@ mod tests {
fn create_multi_return_function(call_conv: CallConv) -> Function {
let mut sig = Signature::new(call_conv);
sig.params.push(AbiParam::new(types::I32));
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
let mut func = Function::with_name_signature(Default::default(), sig);
let block0 = func.dfg.make_block();
let v0 = func.dfg.append_block_param(block0, types::I32);

View File

@@ -8,8 +8,10 @@ use crate::ir::{condcodes::IntCC, Function, Type};
use crate::isa::unwind::systemv;
use crate::isa::x64::{inst::regs::create_reg_env_systemv, settings as x64_settings};
use crate::isa::Builder as IsaBuilder;
use crate::machinst::Reg;
use crate::machinst::{compile, CompiledCode, MachTextSectionBuilder, TextSectionBuilder, VCode};
use crate::machinst::{
compile, CompiledCode, CompiledCodeStencil, MachTextSectionBuilder, Reg, TextSectionBuilder,
VCode,
};
use crate::result::{CodegenError, CodegenResult};
use crate::settings::{self as shared_settings, Flags};
use alloc::{boxed::Box, vec::Vec};
@@ -57,7 +59,11 @@ impl X64Backend {
}
impl TargetIsa for X64Backend {
fn compile_function(&self, func: &Function, want_disasm: bool) -> CodegenResult<CompiledCode> {
fn compile_function(
&self,
func: &Function,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil> {
let flags = self.flags();
let (vcode, regalloc_result) = self.compile_vcode(func, flags.clone())?;
@@ -72,7 +78,7 @@ impl TargetIsa for X64Backend {
log::trace!("disassembly:\n{}", disasm);
}
Ok(CompiledCode {
Ok(CompiledCodeStencil {
buffer,
frame_size,
disasm: emit_result.disasm,
@@ -201,8 +207,8 @@ fn isa_constructor(
mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::ir::{types::*, SourceLoc, ValueLabel, ValueLabelStart};
use crate::ir::{AbiParam, ExternalName, Function, InstBuilder, JumpTableData, Signature};
use crate::ir::{types::*, RelSourceLoc, SourceLoc, UserFuncName, ValueLabel, ValueLabelStart};
use crate::ir::{AbiParam, Function, InstBuilder, JumpTableData, Signature};
use crate::isa::CallConv;
use crate::settings;
use crate::settings::Configurable;
@@ -217,7 +223,7 @@ mod test {
/// well do the test here, where we have a backend to use.
#[test]
fn test_cold_blocks() {
let name = ExternalName::testcase("test0");
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));
@@ -271,35 +277,35 @@ mod test {
pos.func.dfg.values_labels.as_mut().unwrap().insert(
v0,
crate::ir::ValueLabelAssignments::Starts(vec![ValueLabelStart {
from: SourceLoc::new(1),
from: RelSourceLoc::new(1),
label: ValueLabel::new(1),
}]),
);
pos.func.dfg.values_labels.as_mut().unwrap().insert(
v1,
crate::ir::ValueLabelAssignments::Starts(vec![ValueLabelStart {
from: SourceLoc::new(2),
from: RelSourceLoc::new(2),
label: ValueLabel::new(1),
}]),
);
pos.func.dfg.values_labels.as_mut().unwrap().insert(
v2,
crate::ir::ValueLabelAssignments::Starts(vec![ValueLabelStart {
from: SourceLoc::new(3),
from: RelSourceLoc::new(3),
label: ValueLabel::new(1),
}]),
);
pos.func.dfg.values_labels.as_mut().unwrap().insert(
v3,
crate::ir::ValueLabelAssignments::Starts(vec![ValueLabelStart {
from: SourceLoc::new(4),
from: RelSourceLoc::new(4),
label: ValueLabel::new(1),
}]),
);
pos.func.dfg.values_labels.as_mut().unwrap().insert(
v4,
crate::ir::ValueLabelAssignments::Starts(vec![ValueLabelStart {
from: SourceLoc::new(5),
from: RelSourceLoc::new(5),
label: ValueLabel::new(1),
}]),
);
@@ -371,7 +377,7 @@ mod test {
// expands during emission.
#[test]
fn br_table() {
let name = ExternalName::testcase("test0");
let name = UserFuncName::testcase("test0");
let mut sig = Signature::new(CallConv::SystemV);
sig.params.push(AbiParam::new(I32));
sig.returns.push(AbiParam::new(I32));