Files
wasmtime/cranelift/entity/src/map.rs
Benjamin Bouvier 8a9b1a9025 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.
2022-08-12 16:47:43 +00:00

327 lines
8.7 KiB
Rust

//! Densely numbered entity references as mapping keys.
use crate::iter::{Iter, IterMut};
use crate::keys::Keys;
use crate::EntityRef;
use alloc::vec::Vec;
use core::cmp::min;
use core::marker::PhantomData;
use core::ops::{Index, IndexMut};
use core::slice;
#[cfg(feature = "enable-serde")]
use serde::{
de::{Deserializer, SeqAccess, Visitor},
ser::{SerializeSeq, Serializer},
Deserialize, Serialize,
};
/// A mapping `K -> V` for densely indexed entity references.
///
/// The `SecondaryMap` data structure uses the dense index space to implement a map with a vector.
/// Unlike `PrimaryMap`, an `SecondaryMap` can't be used to allocate entity references. It is used
/// to associate secondary information with entities.
///
/// The map does not track if an entry for a key has been inserted or not. Instead it behaves as if
/// all keys have a default entry from the beginning.
#[derive(Debug, Clone, Hash)]
pub struct SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
elems: Vec<V>,
default: V,
unused: PhantomData<K>,
}
/// Shared `SecondaryMap` implementation for all value types.
impl<K, V> SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
/// Create a new empty map.
pub fn new() -> Self
where
V: Default,
{
Self {
elems: Vec::new(),
default: Default::default(),
unused: PhantomData,
}
}
/// Create a new, empty map with the specified capacity.
///
/// The map will be able to hold exactly `capacity` elements without reallocating.
pub fn with_capacity(capacity: usize) -> Self
where
V: Default,
{
Self {
elems: Vec::with_capacity(capacity),
default: Default::default(),
unused: PhantomData,
}
}
/// Create a new empty map with a specified default value.
///
/// This constructor does not require V to implement Default.
pub fn with_default(default: V) -> Self {
Self {
elems: Vec::new(),
default,
unused: PhantomData,
}
}
/// Returns the number of elements the map can hold without reallocating.
pub fn capacity(&self) -> usize {
self.elems.capacity()
}
/// Get the element at `k` if it exists.
#[inline(always)]
pub fn get(&self, k: K) -> Option<&V> {
self.elems.get(k.index())
}
/// Is this map completely empty?
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.elems.is_empty()
}
/// Remove all entries from this map.
#[inline(always)]
pub fn clear(&mut self) {
self.elems.clear()
}
/// Iterate over all the keys and values in this map.
pub fn iter(&self) -> Iter<K, V> {
Iter::new(self.elems.iter())
}
/// Iterate over all the keys and values in this map, mutable edition.
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut::new(self.elems.iter_mut())
}
/// Iterate over all the keys in this map.
pub fn keys(&self) -> Keys<K> {
Keys::with_len(self.elems.len())
}
/// Iterate over all the values in this map.
pub fn values(&self) -> slice::Iter<V> {
self.elems.iter()
}
/// Iterate over all the values in this map, mutable edition.
pub fn values_mut(&mut self) -> slice::IterMut<V> {
self.elems.iter_mut()
}
/// Resize the map to have `n` entries by adding default entries as needed.
pub fn resize(&mut self, n: usize) {
self.elems.resize(n, self.default.clone());
}
/// Slow path for `index_mut` which resizes the vector.
#[cold]
fn resize_for_index_mut(&mut self, i: usize) -> &mut V {
self.elems.resize(i + 1, self.default.clone());
&mut self.elems[i]
}
}
impl<K, V> Default for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + Default,
{
fn default() -> SecondaryMap<K, V> {
SecondaryMap::new()
}
}
/// Immutable indexing into an `SecondaryMap`.
///
/// All keys are permitted. Untouched entries have the default value.
impl<K, V> Index<K> for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
type Output = V;
#[inline(always)]
fn index(&self, k: K) -> &V {
self.elems.get(k.index()).unwrap_or(&self.default)
}
}
/// Mutable indexing into an `SecondaryMap`.
///
/// The map grows as needed to accommodate new keys.
impl<K, V> IndexMut<K> for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
{
#[inline(always)]
fn index_mut(&mut self, k: K) -> &mut V {
let i = k.index();
if i >= self.elems.len() {
return self.resize_for_index_mut(i);
}
&mut self.elems[i]
}
}
impl<K, V> PartialEq for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + PartialEq,
{
fn eq(&self, other: &Self) -> bool {
let min_size = min(self.elems.len(), other.elems.len());
self.default == other.default
&& self.elems[..min_size] == other.elems[..min_size]
&& self.elems[min_size..].iter().all(|e| *e == self.default)
&& other.elems[min_size..].iter().all(|e| *e == other.default)
}
}
impl<K, V> Eq for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + PartialEq + Eq,
{
}
#[cfg(feature = "enable-serde")]
impl<K, V> Serialize for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + PartialEq + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// TODO: bincode encodes option as "byte for Some/None" and then optionally the content
// TODO: we can actually optimize it by encoding manually bitmask, then elements
let mut elems_cnt = self.elems.len();
while elems_cnt > 0 && self.elems[elems_cnt - 1] == self.default {
elems_cnt -= 1;
}
let mut seq = serializer.serialize_seq(Some(1 + elems_cnt))?;
seq.serialize_element(&Some(self.default.clone()))?;
for e in self.elems.iter().take(elems_cnt) {
let some_e = Some(e);
seq.serialize_element(if *e == self.default { &None } else { &some_e })?;
}
seq.end()
}
}
#[cfg(feature = "enable-serde")]
impl<'de, K, V> Deserialize<'de> for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use alloc::fmt;
struct SecondaryMapVisitor<K, V> {
unused: PhantomData<fn(K) -> V>,
}
impl<'de, K, V> Visitor<'de> for SecondaryMapVisitor<K, V>
where
K: EntityRef,
V: Clone + Deserialize<'de>,
{
type Value = SecondaryMap<K, V>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct SecondaryMap")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
match seq.next_element()? {
Some(Some(default_val)) => {
let default_val: V = default_val; // compiler can't infer the type
let mut m = SecondaryMap::with_default(default_val.clone());
let mut idx = 0;
while let Some(val) = seq.next_element()? {
let val: Option<_> = val; // compiler can't infer the type
m[K::new(idx)] = val.unwrap_or_else(|| default_val.clone());
idx += 1;
}
Ok(m)
}
_ => Err(serde::de::Error::custom("Default value required")),
}
}
}
deserializer.deserialize_seq(SecondaryMapVisitor {
unused: PhantomData {},
})
}
}
#[cfg(test)]
mod tests {
use super::*;
// `EntityRef` impl for testing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct E(u32);
impl EntityRef for E {
fn new(i: usize) -> Self {
E(i as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}
#[test]
fn basic() {
let r0 = E(0);
let r1 = E(1);
let r2 = E(2);
let mut m = SecondaryMap::new();
let v: Vec<E> = m.keys().collect();
assert_eq!(v, []);
m[r2] = 3;
m[r1] = 5;
assert_eq!(m[r1], 5);
assert_eq!(m[r2], 3);
let v: Vec<E> = m.keys().collect();
assert_eq!(v, [r0, r1, r2]);
let shared = &m;
assert_eq!(shared[r0], 0);
assert_eq!(shared[r1], 5);
assert_eq!(shared[r2], 3);
}
}