mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 04:31:33 +00:00
Resolve semantics for more values.
This commit is contained in:
@ -367,6 +367,7 @@ impl LLVMBackend {
|
||||
.expect("size_record not found");
|
||||
|
||||
start_entry.populate_msm(
|
||||
module_info,
|
||||
code_ptr as usize,
|
||||
&map,
|
||||
size_record,
|
||||
|
@ -19,7 +19,7 @@ use wasmer_runtime_core::{
|
||||
module::{ModuleInfo, ModuleInner},
|
||||
structures::{Map, TypedIndex},
|
||||
types::{
|
||||
FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, SigIndex, TableIndex, Type,
|
||||
FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, SigIndex, TableIndex, Type, ImportedFuncIndex,
|
||||
},
|
||||
};
|
||||
use wasmparser::{BinaryReaderError, MemoryImmediate, Operator, Type as WpType};
|
||||
@ -303,6 +303,7 @@ fn resolve_memory_ptr(
|
||||
}
|
||||
|
||||
fn emit_stack_map(
|
||||
module_info: &ModuleInfo,
|
||||
intrinsics: &Intrinsics,
|
||||
builder: &Builder,
|
||||
local_function_id: usize,
|
||||
@ -310,6 +311,7 @@ fn emit_stack_map(
|
||||
kind: StackmapEntryKind,
|
||||
locals: &[PointerValue],
|
||||
state: &State,
|
||||
ctx: &mut CtxType,
|
||||
opcode_offset: usize,
|
||||
) {
|
||||
let stackmap_id = target.entries.len();
|
||||
@ -333,6 +335,76 @@ fn emit_stack_map(
|
||||
params.extend_from_slice(&state.stack);
|
||||
value_semantics.extend((0..state.stack.len()).map(ValueSemantic::WasmStack));
|
||||
|
||||
params.push(ctx.basic());
|
||||
value_semantics.push(ValueSemantic::Ctx);
|
||||
|
||||
if module_info.memories.len() + module_info.imported_memories.len() > 0 {
|
||||
let cache = ctx.memory(MemoryIndex::new(0), intrinsics);
|
||||
match cache {
|
||||
MemoryCache::Dynamic { ptr_to_base_ptr, ptr_to_bounds } => {
|
||||
params.push(ptr_to_base_ptr.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::PointerToMemoryBase);
|
||||
|
||||
params.push(ptr_to_bounds.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::PointerToMemoryBound);
|
||||
}
|
||||
MemoryCache::Static { base_ptr, bounds } => {
|
||||
params.push(base_ptr.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::MemoryBase);
|
||||
|
||||
params.push(bounds.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::MemoryBound);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if module_info.tables.len() + module_info.imported_tables.len() > 0 {
|
||||
let (ptr_to_base_ptr, ptr_to_bounds) = ctx.table_prepare(TableIndex::new(0), intrinsics);
|
||||
|
||||
params.push(ptr_to_base_ptr.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::PointerToTableBase);
|
||||
|
||||
params.push(ptr_to_bounds.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::PointerToMemoryBound);
|
||||
}
|
||||
|
||||
if module_info.globals.len() + module_info.imported_globals.len() > 0 {
|
||||
for i in 0..module_info.globals.len() + module_info.imported_globals.len() {
|
||||
let cache = ctx.global_cache(GlobalIndex::new(i), intrinsics);
|
||||
match cache {
|
||||
GlobalCache::Const { value } => {
|
||||
params.push(value);
|
||||
value_semantics.push(ValueSemantic::Global(i));
|
||||
}
|
||||
GlobalCache::Mut { ptr_to_value } => {
|
||||
params.push(ptr_to_value.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::PointerToGlobal(i));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if module_info.imported_functions.len() > 0 {
|
||||
// TODO: Optimize this
|
||||
for i in 0..module_info.imported_functions.len() {
|
||||
let (func_ptr, ctx_ptr) = ctx.imported_func(ImportedFuncIndex::new(i), intrinsics);
|
||||
|
||||
params.push(func_ptr.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::ImportedFuncPointer(i));
|
||||
|
||||
params.push(ctx_ptr.as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::ImportedFuncCtx(i));
|
||||
}
|
||||
}
|
||||
|
||||
params.push(ctx.signal_mem().as_basic_value_enum());
|
||||
value_semantics.push(ValueSemantic::SignalMem);
|
||||
|
||||
// TODO: sigindices
|
||||
|
||||
assert_eq!(params.len(), value_semantics.len() + 2);
|
||||
|
||||
builder.build_call(intrinsics.experimental_stackmap, ¶ms, &state.var_name());
|
||||
|
||||
target.entries.push(StackmapEntry {
|
||||
@ -487,6 +559,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
|
||||
let mut stackmaps = self.stackmaps.borrow_mut();
|
||||
emit_stack_map(
|
||||
&module_info,
|
||||
&intrinsics,
|
||||
&builder,
|
||||
self.index,
|
||||
@ -494,6 +567,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
StackmapEntryKind::FunctionHeader,
|
||||
&self.locals,
|
||||
&state,
|
||||
self.ctx.as_mut().unwrap(),
|
||||
::std::usize::MAX,
|
||||
);
|
||||
finalize_opcode_stack_map(
|
||||
@ -605,6 +679,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
if let Some(offset) = opcode_offset {
|
||||
let mut stackmaps = self.stackmaps.borrow_mut();
|
||||
emit_stack_map(
|
||||
&info,
|
||||
intrinsics,
|
||||
builder,
|
||||
self.index,
|
||||
@ -612,6 +687,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
StackmapEntryKind::Loop,
|
||||
&self.locals,
|
||||
state,
|
||||
ctx,
|
||||
offset,
|
||||
);
|
||||
let signal_mem = ctx.signal_mem();
|
||||
@ -892,6 +968,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
if let Some(offset) = opcode_offset {
|
||||
let mut stackmaps = self.stackmaps.borrow_mut();
|
||||
emit_stack_map(
|
||||
&info,
|
||||
intrinsics,
|
||||
builder,
|
||||
self.index,
|
||||
@ -899,6 +976,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
StackmapEntryKind::Trappable,
|
||||
&self.locals,
|
||||
state,
|
||||
ctx,
|
||||
offset,
|
||||
);
|
||||
builder.build_call(intrinsics.trap, &[], "trap");
|
||||
@ -908,7 +986,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
self.index,
|
||||
&mut *stackmaps,
|
||||
StackmapEntryKind::Trappable,
|
||||
offset + 1,
|
||||
offset,
|
||||
);
|
||||
}
|
||||
|
||||
@ -1052,6 +1130,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
if let Some(offset) = opcode_offset {
|
||||
let mut stackmaps = self.stackmaps.borrow_mut();
|
||||
emit_stack_map(
|
||||
&info,
|
||||
intrinsics,
|
||||
builder,
|
||||
self.index,
|
||||
@ -1059,6 +1138,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
StackmapEntryKind::Call,
|
||||
&self.locals,
|
||||
state,
|
||||
ctx,
|
||||
offset,
|
||||
)
|
||||
}
|
||||
@ -1241,6 +1321,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
if let Some(offset) = opcode_offset {
|
||||
let mut stackmaps = self.stackmaps.borrow_mut();
|
||||
emit_stack_map(
|
||||
&info,
|
||||
intrinsics,
|
||||
builder,
|
||||
self.index,
|
||||
@ -1248,6 +1329,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||
StackmapEntryKind::Call,
|
||||
&self.locals,
|
||||
state,
|
||||
ctx,
|
||||
offset,
|
||||
)
|
||||
}
|
||||
@ -2737,6 +2819,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
||||
if cfg!(test) {
|
||||
pass_manager.add_verifier_pass();
|
||||
}
|
||||
/*
|
||||
pass_manager.add_lower_expect_intrinsic_pass();
|
||||
pass_manager.add_scalar_repl_aggregates_pass();
|
||||
pass_manager.add_instruction_combining_pass();
|
||||
@ -2749,7 +2832,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
||||
pass_manager.add_reassociate_pass();
|
||||
pass_manager.add_cfg_simplification_pass();
|
||||
pass_manager.add_bit_tracking_dce_pass();
|
||||
pass_manager.add_slp_vectorize_pass();
|
||||
pass_manager.add_slp_vectorize_pass();*/
|
||||
pass_manager.run_on_module(&self.module);
|
||||
|
||||
// self.module.print_to_stderr();
|
||||
|
@ -582,12 +582,11 @@ impl<'a> CtxType<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn table(
|
||||
pub fn table_prepare(
|
||||
&mut self,
|
||||
index: TableIndex,
|
||||
intrinsics: &Intrinsics,
|
||||
builder: &Builder,
|
||||
) -> (PointerValue, IntValue) {
|
||||
) -> (PointerValue, PointerValue) {
|
||||
let (cached_tables, info, ctx_ptr_value, cache_builder) = (
|
||||
&mut self.cached_tables,
|
||||
self.info,
|
||||
@ -646,6 +645,16 @@ impl<'a> CtxType<'a> {
|
||||
}
|
||||
});
|
||||
|
||||
(ptr_to_base_ptr, ptr_to_bounds)
|
||||
}
|
||||
|
||||
pub fn table(
|
||||
&mut self,
|
||||
index: TableIndex,
|
||||
intrinsics: &Intrinsics,
|
||||
builder: &Builder,
|
||||
) -> (PointerValue, IntValue) {
|
||||
let (ptr_to_base_ptr, ptr_to_bounds) = self.table_prepare(index, intrinsics);
|
||||
(
|
||||
builder
|
||||
.build_load(ptr_to_base_ptr, "base_ptr")
|
||||
|
@ -8,6 +8,13 @@ use wasmer_runtime_core::state::{
|
||||
FunctionStateMap, MachineStateDiff, MachineValue, ModuleStateMap, OffsetInfo, RegisterIndex,
|
||||
SuspendOffset, WasmAbstractValue,
|
||||
};
|
||||
use wasmer_runtime_core::vm::Ctx;
|
||||
use wasmer_runtime_core::{
|
||||
module::ModuleInfo,
|
||||
types::{GlobalIndex, TableIndex, LocalOrImport},
|
||||
structures::TypedIndex,
|
||||
vm,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct StackmapRegistry {
|
||||
@ -29,6 +36,19 @@ pub struct StackmapEntry {
|
||||
pub enum ValueSemantic {
|
||||
WasmLocal(usize),
|
||||
WasmStack(usize),
|
||||
Ctx,
|
||||
SignalMem,
|
||||
PointerToMemoryBase,
|
||||
PointerToMemoryBound, // 64-bit
|
||||
MemoryBase,
|
||||
MemoryBound, // 64-bit
|
||||
PointerToGlobal(usize),
|
||||
Global(usize),
|
||||
PointerToTableBase,
|
||||
PointerToTableBound,
|
||||
ImportedFuncPointer(usize),
|
||||
ImportedFuncCtx(usize),
|
||||
DynamicSigindice(usize),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||
@ -69,6 +89,7 @@ pub struct MachineStateDiff {
|
||||
impl StackmapEntry {
|
||||
pub fn populate_msm(
|
||||
&self,
|
||||
module_info: &ModuleInfo,
|
||||
code_addr: usize,
|
||||
llvm_map: &StackMap,
|
||||
size_record: &StkSizeRecord,
|
||||
@ -86,6 +107,7 @@ impl StackmapEntry {
|
||||
.checked_sub(code_addr)
|
||||
.unwrap();
|
||||
let target_offset = func_base_addr + map_record.instruction_offset as usize;
|
||||
assert!(self.is_start);
|
||||
|
||||
if msm.local_functions.len() == self.local_function_id {
|
||||
assert_eq!(self.kind, StackmapEntryKind::FunctionHeader);
|
||||
@ -131,6 +153,31 @@ impl StackmapEntry {
|
||||
wasm_stack.push(WasmAbstractValue::Runtime);
|
||||
MachineValue::WasmStack(x)
|
||||
}
|
||||
ValueSemantic::Ctx => MachineValue::Vmctx,
|
||||
ValueSemantic::SignalMem => MachineValue::VmctxDeref(vec![Ctx::offset_interrupt_signal_mem() as usize, 0]),
|
||||
ValueSemantic::PointerToMemoryBase => MachineValue::VmctxDeref(vec![Ctx::offset_memory_base() as usize]),
|
||||
ValueSemantic::PointerToMemoryBound => MachineValue::VmctxDeref(vec![Ctx::offset_memory_bound() as usize]),
|
||||
ValueSemantic::MemoryBase => MachineValue::VmctxDeref(vec![Ctx::offset_memory_base() as usize, 0]),
|
||||
ValueSemantic::MemoryBound => MachineValue::VmctxDeref(vec![Ctx::offset_memory_bound() as usize, 0]),
|
||||
ValueSemantic::PointerToGlobal(idx) => MachineValue::VmctxDeref(deref_global(module_info, idx, false)),
|
||||
ValueSemantic::Global(idx) => MachineValue::VmctxDeref(deref_global(module_info, idx, true)),
|
||||
ValueSemantic::PointerToTableBase => MachineValue::VmctxDeref(deref_table_base(module_info, 0, false)),
|
||||
ValueSemantic::PointerToTableBound => MachineValue::VmctxDeref(deref_table_bound(module_info, 0, false)),
|
||||
ValueSemantic::ImportedFuncPointer(idx) => MachineValue::VmctxDeref(vec![
|
||||
Ctx::offset_imported_funcs() as usize,
|
||||
vm::ImportedFunc::size() as usize * idx + vm::ImportedFunc::offset_func() as usize,
|
||||
0,
|
||||
]),
|
||||
ValueSemantic::ImportedFuncCtx(idx) => MachineValue::VmctxDeref(vec![
|
||||
Ctx::offset_imported_funcs() as usize,
|
||||
vm::ImportedFunc::size() as usize * idx + vm::ImportedFunc::offset_vmctx() as usize,
|
||||
0,
|
||||
]),
|
||||
ValueSemantic::DynamicSigindice(idx) => MachineValue::VmctxDeref(vec![
|
||||
Ctx::offset_signatures() as usize,
|
||||
idx * 4,
|
||||
0,
|
||||
]),
|
||||
};
|
||||
match loc.ty {
|
||||
LocationType::Register => {
|
||||
@ -488,3 +535,60 @@ impl StackMap {
|
||||
Ok(map)
|
||||
}
|
||||
}
|
||||
|
||||
fn deref_global(info: &ModuleInfo, idx: usize, deref_into_value: bool) -> Vec<usize> {
|
||||
let mut x: Vec<usize> = match GlobalIndex::new(idx).local_or_import(info) {
|
||||
LocalOrImport::Local(idx) => vec![
|
||||
Ctx::offset_globals() as usize,
|
||||
idx.index() * 8,
|
||||
0,
|
||||
],
|
||||
LocalOrImport::Import(idx) => vec![
|
||||
Ctx::offset_imported_globals() as usize,
|
||||
idx.index() * 8,
|
||||
0,
|
||||
],
|
||||
};
|
||||
if deref_into_value {
|
||||
x.push(0);
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
fn deref_table_base(info: &ModuleInfo, idx: usize, deref_into_value: bool) -> Vec<usize> {
|
||||
let mut x: Vec<usize> = match TableIndex::new(idx).local_or_import(info) {
|
||||
LocalOrImport::Local(idx) => vec![
|
||||
Ctx::offset_tables() as usize,
|
||||
idx.index() * 8,
|
||||
0,
|
||||
],
|
||||
LocalOrImport::Import(idx) => vec![
|
||||
Ctx::offset_imported_tables() as usize,
|
||||
idx.index() * 8,
|
||||
0,
|
||||
],
|
||||
};
|
||||
if deref_into_value {
|
||||
x.push(0);
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
fn deref_table_bound(info: &ModuleInfo, idx: usize, deref_into_value: bool) -> Vec<usize> {
|
||||
let mut x: Vec<usize> = match TableIndex::new(idx).local_or_import(info) {
|
||||
LocalOrImport::Local(idx) => vec![
|
||||
Ctx::offset_tables() as usize,
|
||||
idx.index() * 8,
|
||||
8,
|
||||
],
|
||||
LocalOrImport::Import(idx) => vec![
|
||||
Ctx::offset_imported_tables() as usize,
|
||||
idx.index() * 8,
|
||||
8,
|
||||
],
|
||||
};
|
||||
if deref_into_value {
|
||||
x.push(0);
|
||||
}
|
||||
x
|
||||
}
|
Reference in New Issue
Block a user