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.
217 lines
6.8 KiB
Rust
217 lines
6.8 KiB
Rust
//! Stack slots.
|
|
//!
|
|
//! The `StackSlotData` struct keeps track of a single stack slot in a function.
|
|
//!
|
|
|
|
use crate::entity::PrimaryMap;
|
|
use crate::ir::entities::{DynamicStackSlot, DynamicType};
|
|
use crate::ir::StackSlot;
|
|
use core::fmt;
|
|
use core::str::FromStr;
|
|
|
|
/// imports only needed for testing.
|
|
#[allow(unused_imports)]
|
|
use crate::ir::{DynamicTypeData, GlobalValueData};
|
|
|
|
#[allow(unused_imports)]
|
|
use crate::ir::types::*;
|
|
|
|
#[cfg(feature = "enable-serde")]
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// The size of an object on the stack, or the size of a stack frame.
|
|
///
|
|
/// We don't use `usize` to represent object sizes on the target platform because Cranelift supports
|
|
/// cross-compilation, and `usize` is a type that depends on the host platform, not the target
|
|
/// platform.
|
|
pub type StackSize = u32;
|
|
|
|
/// The kind of a stack slot.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub enum StackSlotKind {
|
|
/// An explicit stack slot. This is a chunk of stack memory for use by the `stack_load`
|
|
/// and `stack_store` instructions.
|
|
ExplicitSlot,
|
|
/// An explicit stack slot for dynamic vector types. This is a chunk of stack memory
|
|
/// for use by the `dynamic_stack_load` and `dynamic_stack_store` instructions.
|
|
ExplicitDynamicSlot,
|
|
}
|
|
|
|
impl FromStr for StackSlotKind {
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, ()> {
|
|
use self::StackSlotKind::*;
|
|
match s {
|
|
"explicit_slot" => Ok(ExplicitSlot),
|
|
"explicit_dynamic_slot" => Ok(ExplicitDynamicSlot),
|
|
_ => Err(()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for StackSlotKind {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
use self::StackSlotKind::*;
|
|
f.write_str(match *self {
|
|
ExplicitSlot => "explicit_slot",
|
|
ExplicitDynamicSlot => "explicit_dynamic_slot",
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Contents of a stack slot.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct StackSlotData {
|
|
/// The kind of stack slot.
|
|
pub kind: StackSlotKind,
|
|
|
|
/// Size of stack slot in bytes.
|
|
pub size: StackSize,
|
|
}
|
|
|
|
impl StackSlotData {
|
|
/// Create a stack slot with the specified byte size.
|
|
pub fn new(kind: StackSlotKind, size: StackSize) -> Self {
|
|
Self { kind, size }
|
|
}
|
|
|
|
/// Get the alignment in bytes of this stack slot given the stack pointer alignment.
|
|
pub fn alignment(&self, max_align: StackSize) -> StackSize {
|
|
debug_assert!(max_align.is_power_of_two());
|
|
if self.kind == StackSlotKind::ExplicitDynamicSlot {
|
|
max_align
|
|
} else {
|
|
// We want to find the largest power of two that divides both `self.size` and `max_align`.
|
|
// That is the same as isolating the rightmost bit in `x`.
|
|
let x = self.size | max_align;
|
|
// C.f. Hacker's delight.
|
|
x & x.wrapping_neg()
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for StackSlotData {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{} {}", self.kind, self.size)
|
|
}
|
|
}
|
|
|
|
/// Contents of a dynamic stack slot.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct DynamicStackSlotData {
|
|
/// The kind of stack slot.
|
|
pub kind: StackSlotKind,
|
|
|
|
/// The type of this slot.
|
|
pub dyn_ty: DynamicType,
|
|
}
|
|
|
|
impl DynamicStackSlotData {
|
|
/// Create a stack slot with the specified byte size.
|
|
pub fn new(kind: StackSlotKind, dyn_ty: DynamicType) -> Self {
|
|
assert!(kind == StackSlotKind::ExplicitDynamicSlot);
|
|
Self { kind, dyn_ty }
|
|
}
|
|
|
|
/// Get the alignment in bytes of this stack slot given the stack pointer alignment.
|
|
pub fn alignment(&self, max_align: StackSize) -> StackSize {
|
|
debug_assert!(max_align.is_power_of_two());
|
|
max_align
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for DynamicStackSlotData {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{} {}", self.kind, self.dyn_ty)
|
|
}
|
|
}
|
|
|
|
/// All allocated stack slots.
|
|
pub type StackSlots = PrimaryMap<StackSlot, StackSlotData>;
|
|
|
|
/// All allocated dynamic stack slots.
|
|
pub type DynamicStackSlots = PrimaryMap<DynamicStackSlot, DynamicStackSlotData>;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::ir::Function;
|
|
use alloc::string::ToString;
|
|
|
|
#[test]
|
|
fn stack_slot() {
|
|
let mut func = Function::new();
|
|
|
|
let ss0 = func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4));
|
|
let ss1 = func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 8));
|
|
assert_eq!(ss0.to_string(), "ss0");
|
|
assert_eq!(ss1.to_string(), "ss1");
|
|
|
|
assert_eq!(func.sized_stack_slots[ss0].size, 4);
|
|
assert_eq!(func.sized_stack_slots[ss1].size, 8);
|
|
|
|
assert_eq!(func.sized_stack_slots[ss0].to_string(), "explicit_slot 4");
|
|
assert_eq!(func.sized_stack_slots[ss1].to_string(), "explicit_slot 8");
|
|
}
|
|
|
|
#[test]
|
|
fn dynamic_stack_slot() {
|
|
let mut func = Function::new();
|
|
|
|
let int_vector_ty = I32X4;
|
|
let fp_vector_ty = F64X2;
|
|
let scale0 = GlobalValueData::DynScaleTargetConst {
|
|
vector_type: int_vector_ty,
|
|
};
|
|
let scale1 = GlobalValueData::DynScaleTargetConst {
|
|
vector_type: fp_vector_ty,
|
|
};
|
|
let gv0 = func.create_global_value(scale0);
|
|
let gv1 = func.create_global_value(scale1);
|
|
let dtd0 = DynamicTypeData::new(int_vector_ty, gv0);
|
|
let dtd1 = DynamicTypeData::new(fp_vector_ty, gv1);
|
|
let dt0 = func.dfg.make_dynamic_ty(dtd0);
|
|
let dt1 = func.dfg.make_dynamic_ty(dtd1);
|
|
|
|
let dss0 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(
|
|
StackSlotKind::ExplicitDynamicSlot,
|
|
dt0,
|
|
));
|
|
let dss1 = func.create_dynamic_stack_slot(DynamicStackSlotData::new(
|
|
StackSlotKind::ExplicitDynamicSlot,
|
|
dt1,
|
|
));
|
|
assert_eq!(dss0.to_string(), "dss0");
|
|
assert_eq!(dss1.to_string(), "dss1");
|
|
|
|
assert_eq!(
|
|
func.dynamic_stack_slots[dss0].to_string(),
|
|
"explicit_dynamic_slot dt0"
|
|
);
|
|
assert_eq!(
|
|
func.dynamic_stack_slots[dss1].to_string(),
|
|
"explicit_dynamic_slot dt1"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn alignment() {
|
|
let slot = StackSlotData::new(StackSlotKind::ExplicitSlot, 8);
|
|
|
|
assert_eq!(slot.alignment(4), 4);
|
|
assert_eq!(slot.alignment(8), 8);
|
|
assert_eq!(slot.alignment(16), 8);
|
|
|
|
let slot2 = StackSlotData::new(StackSlotKind::ExplicitSlot, 24);
|
|
|
|
assert_eq!(slot2.alignment(4), 4);
|
|
assert_eq!(slot2.alignment(8), 8);
|
|
assert_eq!(slot2.alignment(16), 8);
|
|
assert_eq!(slot2.alignment(32), 8);
|
|
}
|
|
}
|