Remove dependency on TargetIsa from Wasmtime crates (#3178)

This commit started off by deleting the `cranelift_codegen::settings`
reexport in the `wasmtime-environ` crate and then basically played
whack-a-mole until everything compiled again. The main result of this is
that the `wasmtime-*` family of crates have generally less of a
dependency on the `TargetIsa` trait and type from Cranelift. While the
dependency isn't entirely severed yet this is at least a significant
start.

This commit is intended to be largely refactorings, no functional
changes are intended here. The refactorings are:

* A `CompilerBuilder` trait has been added to `wasmtime_environ` which
  server as an abstraction used to create compilers and configure them
  in a uniform fashion. The `wasmtime::Config` type now uses this
  instead of cranelift-specific settings. The `wasmtime-jit` crate
  exports the ability to create a compiler builder from a
  `CompilationStrategy`, which only works for Cranelift right now. In a
  cranelift-less build of Wasmtime this is expected to return a trait
  object that fails all requests to compile.

* The `Compiler` trait in the `wasmtime_environ` crate has been souped
  up with a number of methods that Wasmtime and other crates needed.

* The `wasmtime-debug` crate is now moved entirely behind the
  `wasmtime-cranelift` crate.

* The `wasmtime-cranelift` crate is now only depended on by the
  `wasmtime-jit` crate.

* Wasm types in `cranelift-wasm` no longer contain their IR type,
  instead they only contain the `WasmType`. This is required to get
  everything to align correctly but will also be required in a future
  refactoring where the types used by `cranelift-wasm` will be extracted
  to a separate crate.

* I moved around a fair bit of code in `wasmtime-cranelift`.

* Some gdb-specific jit-specific code has moved from `wasmtime-debug` to
  `wasmtime-jit`.
This commit is contained in:
Alex Crichton
2021-08-16 09:55:39 -05:00
committed by GitHub
parent 7c0948fe0b
commit 0313e30d76
47 changed files with 1529 additions and 1384 deletions

View File

@@ -5,7 +5,7 @@
use crate::export::Export;
use crate::externref::VMExternRefActivationsTable;
use crate::memory::{Memory, RuntimeMemoryCreator};
use crate::table::{Table, TableElement};
use crate::table::{Table, TableElement, TableElementType};
use crate::traphandlers::Trap;
use crate::vmcontext::{
VMCallerCheckedAnyfunc, VMContext, VMFunctionImport, VMGlobalDefinition, VMGlobalImport,
@@ -25,7 +25,7 @@ use std::{mem, ptr, slice};
use wasmtime_environ::entity::{packed_option::ReservedValue, EntityRef, EntitySet, PrimaryMap};
use wasmtime_environ::wasm::{
DataIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, ElemIndex, EntityIndex,
FuncIndex, GlobalIndex, MemoryIndex, TableElementType, TableIndex, WasmType,
FuncIndex, GlobalIndex, MemoryIndex, TableIndex, WasmType,
};
use wasmtime_environ::{ir, HostPtr, Module, VMOffsets};
@@ -590,7 +590,7 @@ impl Instance {
)?;
},
TableElementType::Val(_) => {
TableElementType::Extern => {
debug_assert!(elements.iter().all(|e| *e == FuncIndex::reserved_value()));
table.fill(dst, TableElement::ExternRef(None), len)?;
}

View File

@@ -1051,8 +1051,7 @@ mod test {
use crate::{Imports, VMSharedSignatureIndex};
use wasmtime_environ::{
entity::EntityRef,
ir::Type,
wasm::{Global, GlobalInit, Memory, SignatureIndex, Table, TableElementType, WasmType},
wasm::{Global, GlobalInit, Memory, SignatureIndex, Table, WasmType},
MemoryPlan, ModuleType, TablePlan, TableStyle,
};
@@ -1088,7 +1087,6 @@ mod test {
style: TableStyle::CallerChecksSignature,
table: Table {
wasm_ty: WasmType::FuncRef,
ty: TableElementType::Func,
minimum: 0,
maximum: None,
},
@@ -1144,7 +1142,6 @@ mod test {
module.globals.push(Global {
wasm_ty: WasmType::I32,
ty: Type::int(32).unwrap(),
mutability: false,
initializer: GlobalInit::I32Const(0),
});
@@ -1208,7 +1205,6 @@ mod test {
style: TableStyle::CallerChecksSignature,
table: Table {
wasm_ty: WasmType::FuncRef,
ty: TableElementType::Func,
minimum: 0,
maximum: None,
},
@@ -1258,7 +1254,6 @@ mod test {
module.globals.push(Global {
wasm_ty: WasmType::I32,
ty: Type::int(32).unwrap(),
mutability: false,
initializer: GlobalInit::I32Const(0),
});
@@ -1281,7 +1276,6 @@ mod test {
style: TableStyle::CallerChecksSignature,
table: Table {
wasm_ty: WasmType::FuncRef,
ty: TableElementType::Func,
minimum: 11,
maximum: None,
},

View File

@@ -58,16 +58,14 @@
use crate::externref::VMExternRef;
use crate::instance::Instance;
use crate::table::Table;
use crate::table::{Table, TableElementType};
use crate::traphandlers::{raise_lib_trap, Trap};
use crate::vmcontext::{VMCallerCheckedAnyfunc, VMContext};
use backtrace::Backtrace;
use std::mem;
use std::ptr::{self, NonNull};
use wasmtime_environ::ir::TrapCode;
use wasmtime_environ::wasm::{
DataIndex, ElemIndex, GlobalIndex, MemoryIndex, TableElementType, TableIndex,
};
use wasmtime_environ::wasm::{DataIndex, ElemIndex, GlobalIndex, MemoryIndex, TableIndex};
const TOINT_32: f32 = 1.0 / f32::EPSILON;
const TOINT_64: f64 = 1.0 / f64::EPSILON;
@@ -214,9 +212,7 @@ pub unsafe extern "C" fn wasmtime_table_grow(
let table_index = TableIndex::from_u32(table_index);
let element = match instance.table_element_type(table_index) {
TableElementType::Func => (init_value as *mut VMCallerCheckedAnyfunc).into(),
TableElementType::Val(ty) => {
debug_assert_eq!(ty, crate::ref_type());
TableElementType::Extern => {
let init_value = if init_value.is_null() {
None
} else {
@@ -249,8 +245,7 @@ pub unsafe extern "C" fn wasmtime_table_fill(
let val = val as *mut VMCallerCheckedAnyfunc;
table.fill(dst, val.into(), len)
}
TableElementType::Val(ty) => {
debug_assert_eq!(ty, crate::ref_type());
TableElementType::Extern => {
let val = if val.is_null() {
None
} else {

View File

@@ -8,7 +8,7 @@ use anyhow::{bail, Result};
use std::convert::{TryFrom, TryInto};
use std::ops::Range;
use std::ptr;
use wasmtime_environ::wasm::TableElementType;
use wasmtime_environ::wasm::WasmType;
use wasmtime_environ::{ir, TablePlan};
/// An element going into or coming out of a table.
@@ -22,6 +22,12 @@ pub enum TableElement {
ExternRef(Option<VMExternRef>),
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum TableElementType {
Func,
Extern,
}
// The usage of `*mut VMCallerCheckedAnyfunc` is safe w.r.t. thread safety, this
// just relies on thread-safety of `VMExternRef` itself.
unsafe impl Send for TableElement where VMExternRef: Send {}
@@ -38,7 +44,7 @@ impl TableElement {
unsafe fn from_raw(ty: TableElementType, ptr: usize) -> Self {
match ty {
TableElementType::Func => Self::FuncRef(ptr as _),
TableElementType::Val(_) => Self::ExternRef(if ptr == 0 {
TableElementType::Extern => Self::ExternRef(if ptr == 0 {
None
} else {
Some(VMExternRef::from_raw(ptr as *mut u8))
@@ -54,7 +60,7 @@ impl TableElement {
unsafe fn clone_from_raw(ty: TableElementType, ptr: usize) -> Self {
match ty {
TableElementType::Func => Self::FuncRef(ptr as _),
TableElementType::Val(_) => Self::ExternRef(if ptr == 0 {
TableElementType::Extern => Self::ExternRef(if ptr == 0 {
None
} else {
Some(VMExternRef::clone_from_raw(ptr as *mut u8))
@@ -122,6 +128,14 @@ pub enum Table {
},
}
fn wasm_to_table_type(ty: WasmType) -> Result<TableElementType> {
match ty {
WasmType::FuncRef => Ok(TableElementType::Func),
WasmType::ExternRef => Ok(TableElementType::Extern),
ty => bail!("invalid table element type {:?}", ty),
}
}
impl Table {
/// Create a new dynamic (movable) table instance for the specified table plan.
pub fn new_dynamic(
@@ -130,7 +144,7 @@ impl Table {
) -> Result<Self> {
Self::limit_new(plan, limiter)?;
let elements = vec![0; plan.table.minimum as usize];
let ty = plan.table.ty.clone();
let ty = wasm_to_table_type(plan.table.wasm_ty)?;
let maximum = plan.table.maximum;
Ok(Table::Dynamic {
@@ -148,7 +162,7 @@ impl Table {
) -> Result<Self> {
Self::limit_new(plan, limiter)?;
let size = plan.table.minimum;
let ty = plan.table.ty.clone();
let ty = wasm_to_table_type(plan.table.wasm_ty)?;
let data = match plan.table.maximum {
Some(max) if (max as usize) < data.len() => &mut data[..max as usize],
_ => data,
@@ -403,7 +417,7 @@ impl Table {
fn type_matches(&self, val: &TableElement) -> bool {
match (&val, self.element_type()) {
(TableElement::FuncRef(_), TableElementType::Func) => true,
(TableElement::ExternRef(_), TableElementType::Val(_)) => true,
(TableElement::ExternRef(_), TableElementType::Extern) => true,
_ => false,
}
}
@@ -449,7 +463,7 @@ impl Table {
dst_table.elements_mut()[dst_range]
.copy_from_slice(&src_table.elements()[src_range]);
}
TableElementType::Val(_) => {
TableElementType::Extern => {
// We need to clone each `externref`
let dst = dst_table.elements_mut();
let src = src_table.elements();
@@ -469,7 +483,7 @@ impl Table {
// `funcref` are `Copy`, so just do a memmove
dst.copy_within(src_range, dst_range.start);
}
TableElementType::Val(_) => {
TableElementType::Extern => {
// We need to clone each `externref` while handling overlapping
// ranges
if dst_range.start <= src_range.start {