Skip to content

Commit

Permalink
Try #2195:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Mar 18, 2021
2 parents 031afdf + 896f865 commit d3cccb0
Show file tree
Hide file tree
Showing 13 changed files with 1,523 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/compiler-cranelift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition = "2018"
wasmer-compiler = { path = "../compiler", version = "1.0.2", features = ["translator"], default-features = false }
wasmer-vm = { path = "../vm", version = "1.0.2" }
wasmer-types = { path = "../wasmer-types", version = "1.0.2", default-features = false, features = ["std"] }
cranelift-entity = { version = "0.70", default-features = false }
cranelift-codegen = { version = "0.70", default-features = false, features = ["x86", "arm64"] }
cranelift-frontend = { version = "0.70", default-features = false }
tracing = "0.1"
Expand All @@ -36,7 +37,7 @@ maintenance = { status = "actively-developed" }
[features]
default = ["std", "enable-serde", "unwind"]
unwind = ["cranelift-codegen/unwind", "gimli"]
enable-serde = ["wasmer-compiler/enable-serde", "wasmer-types/enable-serde"]
enable-serde = ["wasmer-compiler/enable-serde", "wasmer-types/enable-serde", "cranelift-entity/enable-serde"]
std = ["cranelift-codegen/std", "cranelift-frontend/std", "wasmer-compiler/std", "wasmer-types/std"]
core = ["hashbrown", "cranelift-codegen/core", "cranelift-frontend/core"]

Expand Down
1 change: 1 addition & 0 deletions lib/compiler-cranelift/src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::translator::{irlibcall_to_libcall, irreloc_to_relocationkind};
use cranelift_codegen::binemit;
use cranelift_codegen::ir::{self, ExternalName};
use cranelift_entity::EntityRef as CraneliftEntityRef;
use wasmer_compiler::{JumpTable, Relocation, RelocationTarget, TrapInformation};
use wasmer_types::entity::EntityRef;
use wasmer_types::{FunctionIndex, LocalFunctionIndex};
Expand Down
3 changes: 2 additions & 1 deletion lib/compiler-cranelift/src/translator/translation_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::u32;
use cranelift_codegen::binemit::Reloc;
use cranelift_codegen::ir::{self, AbiParam};
use cranelift_codegen::isa::TargetFrontendConfig;
use cranelift_entity::{EntityRef as CraneliftEntityRef, SecondaryMap as CraneliftSecondaryMap};
use cranelift_frontend::FunctionBuilder;
use wasmer_compiler::wasm_unsupported;
use wasmer_compiler::wasmparser;
Expand Down Expand Up @@ -148,7 +149,7 @@ pub fn get_vmctx_value_label() -> ir::ValueLabel {

/// Transforms Cranelift JumpTable's into runtime JumpTables
pub fn transform_jump_table(
jt_offsets: SecondaryMap<ir::JumpTable, u32>,
jt_offsets: CraneliftSecondaryMap<ir::JumpTable, u32>,
) -> SecondaryMap<JumpTable, u32> {
let mut func_jt_offsets = SecondaryMap::with_capacity(jt_offsets.capacity());

Expand Down
5 changes: 1 addition & 4 deletions lib/wasmer-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ readme = "README.md"
edition = "2018"

[dependencies]
# We use `cranelift-entity` here because it's a lightweight dependency and it contains
# some useful data structures
cranelift-entity = "0.70"
serde = { version = "1.0", features = ["derive"], optional = true, default-features = false }
thiserror = "1.0"

[features]
default = ["std", "enable-serde"]
std = ["serde/std"]
core = []
enable-serde = ["serde", "cranelift-entity/enable-serde"]
enable-serde = ["serde"]
316 changes: 316 additions & 0 deletions lib/wasmer-types/src/entity/boxed_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
//! Boxed slices for `PrimaryMap`.
use crate::entity::iter::{Iter, IterMut};
use crate::entity::keys::Keys;
use crate::entity::EntityRef;
use crate::lib::std::boxed::Box;
use crate::lib::std::marker::PhantomData;
use crate::lib::std::ops::{Index, IndexMut};
use crate::lib::std::slice;

/// A slice mapping `K -> V` allocating dense entity references.
///
/// The `BoxedSlice` data structure uses the dense index space to implement a map with a boxed
/// slice.
#[derive(Debug, Clone)]
pub struct BoxedSlice<K, V>
where
K: EntityRef,
{
elems: Box<[V]>,
unused: PhantomData<K>,
}

impl<K, V> BoxedSlice<K, V>
where
K: EntityRef,
{
/// Create a new slice from a raw pointer. A safer way to create slices is
/// to use `PrimaryMap::into_boxed_slice()`.
///
/// # Safety
///
/// This relies on `raw` pointing to a valid slice of `V`s.
pub unsafe fn from_raw(raw: *mut [V]) -> Self {
Self {
elems: Box::from_raw(raw),
unused: PhantomData,
}
}

/// Check if `k` is a valid key in the map.
pub fn is_valid(&self, k: K) -> bool {
k.index() < self.elems.len()
}

/// Get the element at `k` if it exists.
pub fn get(&self, k: K) -> Option<&V> {
self.elems.get(k.index())
}

/// Get the element at `k` if it exists, mutable version.
pub fn get_mut(&mut self, k: K) -> Option<&mut V> {
self.elems.get_mut(k.index())
}

/// Is this map completely empty?
pub fn is_empty(&self) -> bool {
self.elems.is_empty()
}

/// Get the total number of entity references created.
pub fn len(&self) -> usize {
self.elems.len()
}

/// 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()
}

/// 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())
}

/// Returns the last element that was inserted in the map.
pub fn last(&self) -> Option<&V> {
self.elems.last()
}
}

/// Immutable indexing into a `BoxedSlice`.
/// The indexed value must be in the map.
impl<K, V> Index<K> for BoxedSlice<K, V>
where
K: EntityRef,
{
type Output = V;

fn index(&self, k: K) -> &V {
&self.elems[k.index()]
}
}

/// Mutable indexing into a `BoxedSlice`.
impl<K, V> IndexMut<K> for BoxedSlice<K, V>
where
K: EntityRef,
{
fn index_mut(&mut self, k: K) -> &mut V {
&mut self.elems[k.index()]
}
}

impl<'a, K, V> IntoIterator for &'a BoxedSlice<K, V>
where
K: EntityRef,
{
type Item = (K, &'a V);
type IntoIter = Iter<'a, K, V>;

fn into_iter(self) -> Self::IntoIter {
Iter::new(self.elems.iter())
}
}

impl<'a, K, V> IntoIterator for &'a mut BoxedSlice<K, V>
where
K: EntityRef,
{
type Item = (K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;

fn into_iter(self) -> Self::IntoIter {
IterMut::new(self.elems.iter_mut())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::entity::PrimaryMap;
use crate::lib::std::vec::Vec;

// `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 p = PrimaryMap::<E, isize>::new();
let m = p.into_boxed_slice();

let v: Vec<E> = m.keys().collect();
assert_eq!(v, []);

assert!(!m.is_valid(r0));
assert!(!m.is_valid(r1));
}

#[test]
fn iter() {
let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
p.push(12);
p.push(33);
let mut m = p.into_boxed_slice();

let mut i = 0;
for (key, value) in &m {
assert_eq!(key.index(), i);
match i {
0 => assert_eq!(*value, 12),
1 => assert_eq!(*value, 33),
_ => panic!(),
}
i += 1;
}
i = 0;
for (key_mut, value_mut) in m.iter_mut() {
assert_eq!(key_mut.index(), i);
match i {
0 => assert_eq!(*value_mut, 12),
1 => assert_eq!(*value_mut, 33),
_ => panic!(),
}
i += 1;
}
}

#[test]
fn iter_rev() {
let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
p.push(12);
p.push(33);
let mut m = p.into_boxed_slice();

let mut i = 2;
for (key, value) in m.iter().rev() {
i -= 1;
assert_eq!(key.index(), i);
match i {
0 => assert_eq!(*value, 12),
1 => assert_eq!(*value, 33),
_ => panic!(),
}
}

i = 2;
for (key, value) in m.iter_mut().rev() {
i -= 1;
assert_eq!(key.index(), i);
match i {
0 => assert_eq!(*value, 12),
1 => assert_eq!(*value, 33),
_ => panic!(),
}
}
}
#[test]
fn keys() {
let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
p.push(12);
p.push(33);
let m = p.into_boxed_slice();

let mut i = 0;
for key in m.keys() {
assert_eq!(key.index(), i);
i += 1;
}
}

#[test]
fn keys_rev() {
let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
p.push(12);
p.push(33);
let m = p.into_boxed_slice();

let mut i = 2;
for key in m.keys().rev() {
i -= 1;
assert_eq!(key.index(), i);
}
}

#[test]
fn values() {
let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
p.push(12);
p.push(33);
let mut m = p.into_boxed_slice();

let mut i = 0;
for value in m.values() {
match i {
0 => assert_eq!(*value, 12),
1 => assert_eq!(*value, 33),
_ => panic!(),
}
i += 1;
}
i = 0;
for value_mut in m.values_mut() {
match i {
0 => assert_eq!(*value_mut, 12),
1 => assert_eq!(*value_mut, 33),
_ => panic!(),
}
i += 1;
}
}

#[test]
fn values_rev() {
let mut p: PrimaryMap<E, usize> = PrimaryMap::new();
p.push(12);
p.push(33);
let mut m = p.into_boxed_slice();

let mut i = 2;
for value in m.values().rev() {
i -= 1;
match i {
0 => assert_eq!(*value, 12),
1 => assert_eq!(*value, 33),
_ => panic!(),
}
}
i = 2;
for value_mut in m.values_mut().rev() {
i -= 1;
match i {
0 => assert_eq!(*value_mut, 12),
1 => assert_eq!(*value_mut, 33),
_ => panic!(),
}
}
}
}
Loading

0 comments on commit d3cccb0

Please sign in to comment.