Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,067 changes: 282 additions & 785 deletions crates/cranelift/src/alias_region.rs

Large diffs are not rendered by default.

39 changes: 29 additions & 10 deletions crates/cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ impl Compiler {
caller_vmctx,
wasmtime_environ::VMCONTEXT_MAGIC,
);
let vm_store_context =
alias_regions.vmctx_store_context(&mut builder.cursor(), caller_vmctx);
let vm_store_context = alias_regions
.vmctx()
.store_context()
.load(&mut builder.cursor(), caller_vmctx);
save_last_wasm_exit_fp_and_pc(
&mut builder,
pointer_type,
Expand Down Expand Up @@ -266,8 +268,10 @@ impl Compiler {
// Increment the "execution version" on the VMStoreContext if
// guest debugging is enabled.
if self.tunables.debug_guest {
let vmstore_ctx_ptr =
alias_regions.vmctx_store_context(&mut builder.cursor(), caller_vmctx);
let vmstore_ctx_ptr = alias_regions
.vmctx()
.store_context()
.load(&mut builder.cursor(), caller_vmctx);
let old_version = alias_regions
.vmstore_context_execution_version(&mut builder.cursor(), vmstore_ctx_ptr);
let new_version = builder.ins().iadd_imm_s(old_version, 1);
Expand Down Expand Up @@ -343,7 +347,10 @@ impl Compiler {
vmctx,
wasmtime_environ::VMCONTEXT_MAGIC,
);
let vm_store_context = alias_regions.vmctx_store_context(&mut builder.cursor(), vmctx);
let vm_store_context = alias_regions
.vmctx()
.store_context()
.load(&mut builder.cursor(), vmctx);
save_last_wasm_exit_fp_and_pc(
&mut builder,
pointer_type,
Expand Down Expand Up @@ -550,7 +557,9 @@ impl wasmtime_environ::Compiler for Compiler {
if !isa.triple().is_pulley() {
let store_ctx = func_env
.alias_regions
.vmctx_store_context_load(&mut context.func);
.vmctx()
.store_context()
.to_deferred_load(&mut context.func);
let stack_limit = func_env
.alias_regions
.vmstore_context_stack_limit_load(&mut context.func);
Expand Down Expand Up @@ -608,7 +617,7 @@ impl wasmtime_environ::Compiler for Compiler {
symbol,
wasmtime_environ::VMCONTEXT_MAGIC,
|alias_regions, _pointer_type, cursor, vmctx| {
alias_regions.vmctx_store_context(cursor, vmctx)
alias_regions.vmctx().store_context().load(cursor, vmctx)
},
)
}
Expand Down Expand Up @@ -656,7 +665,7 @@ impl wasmtime_environ::Compiler for Compiler {
symbol,
wasmtime_environ::VMCONTEXT_MAGIC,
|alias_regions, _pointer_type, cursor, vmctx| {
alias_regions.vmctx_store_context(cursor, vmctx)
alias_regions.vmctx().store_context().load(cursor, vmctx)
},
),
// Delegate to a helper to finish compiling this.
Expand Down Expand Up @@ -1358,7 +1367,10 @@ impl Compiler {
{
// Builtins are stored in an array in all `VMContext`s. First load the
// base pointer of the array...
let array_addr = alias_regions.vmctx_builtin_functions(&mut builder.cursor(), vmctx);
let array_addr = alias_regions
.vmctx()
.builtin_functions()
.load(&mut builder.cursor(), vmctx);
// ... and then load the entry in the array that corresponds to this
// builtin.
let func_addr = alias_regions.builtin_functions_array_element(
Expand Down Expand Up @@ -1408,7 +1420,14 @@ impl Compiler {
if !self.emit_debug_checks {
return;
}
let magic = alias_regions.vmctx_magic(&mut builder.cursor(), vmctx);
// NB: a `VMComponentContext`'s `magic` field is deliberately read
// through the `VMContext` accessor: both types keep `magic` at offset
// zero, and this check is the one place that reads a vmctx's magic
// without knowing which of the two it has.
let magic = alias_regions
.vmctx()
.magic()
.load(&mut builder.cursor(), vmctx);
let is_expected_vmctx = builder.ins().icmp_imm_s(
ir::condcodes::IntCC::Equal,
magic,
Expand Down
113 changes: 56 additions & 57 deletions crates/cranelift/src/compiler/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ impl<'a> TrampolineCompiler<'a> {
WasmArgs::ValRawList,
|me, params| {
let vmctx = params[0];
let lowering_data = me.alias_regions.vmcomponent_lowering_data(
&mut me.builder.cursor(),
vmctx,
*index,
);
let lowering_data = me
.alias_regions
.vmcomponent_lowering_data(*index)
.load(&mut me.builder.cursor(), vmctx);
params.extend([
lowering_data,
me.index_value(*lower_ty),
Expand Down Expand Up @@ -993,11 +992,10 @@ impl<'a> TrampolineCompiler<'a> {
HostCallee::Lowering(index) => {
// Load host function pointer from the vmcontext and then call that
// indirect function pointer with the list of arguments.
let host_fn = self.alias_regions.vmcomponent_lowering_callee(
&mut self.builder.cursor(),
vmctx,
index,
);
let host_fn = self
.alias_regions
.vmcomponent_lowering_callee(index)
.load(&mut self.builder.cursor(), vmctx);
let host_sig = {
let mut sig = ir::Signature::new(CallConv::triple_default(self.isa.triple()));
for param in host_args.iter() {
Expand Down Expand Up @@ -1209,9 +1207,12 @@ impl<'a> TrampolineCompiler<'a> {
// Stash the old value of `may_block` and then set it to false.
let old_may_block = self
.alias_regions
.vmcomponent_task_may_block(&mut self.builder.cursor(), vmctx);
.vmcomponent()
.task_may_block()
.readonly()
.load(&mut self.builder.cursor(), vmctx);
let zero = self.builder.ins().iconst(ir::types::I32, i64::from(0));
self.alias_regions.store_vmcomponent_task_may_block(
self.alias_regions.vmcomponent().task_may_block().store(
&mut self.builder.cursor(),
vmctx,
zero,
Expand Down Expand Up @@ -1254,11 +1255,11 @@ impl<'a> TrampolineCompiler<'a> {
// NB: despite the vmcontext storing nullable funcrefs for function
// pointers we know this is statically never null due to the
// `has_destructor` check above.
let dtor_func_ref = self.alias_regions.vmcomponent_resource_destructor(
&mut self.builder.cursor(),
vmctx,
index,
);
let dtor_func_ref = self
.alias_regions
.vmcomponent()
.resource_destructors(index)
.load(&mut self.builder.cursor(), vmctx);
if self.compiler.emit_debug_checks {
self.builder
.ins()
Expand Down Expand Up @@ -1326,7 +1327,7 @@ impl<'a> TrampolineCompiler<'a> {
self.raise_if_host_trapped(result.unwrap());

// Restore the old value of `may_block`
self.alias_regions.store_vmcomponent_task_may_block(
self.alias_regions.vmcomponent().task_may_block().store(
&mut self.builder.cursor(),
vmctx,
old_may_block,
Expand Down Expand Up @@ -1354,7 +1355,9 @@ impl<'a> TrampolineCompiler<'a> {

fn load_memory(&mut self, vmctx: ir::Value, memory: RuntimeMemoryIndex) -> ir::Value {
self.alias_regions
.vmcomponent_runtime_memory(&mut self.builder.cursor(), vmctx, memory)
.vmcomponent()
.memories(memory)
.load(&mut self.builder.cursor(), vmctx)
}

fn load_callback(
Expand All @@ -1364,11 +1367,11 @@ impl<'a> TrampolineCompiler<'a> {
) -> ir::Value {
let pointer_type = self.isa.pointer_type();
match callback {
Some(idx) => self.alias_regions.vmcomponent_runtime_callback(
&mut self.builder.cursor(),
vmctx,
idx,
),
Some(idx) => self
.alias_regions
.vmcomponent()
.callbacks(idx)
.load(&mut self.builder.cursor(), vmctx),
None => self.builder.ins().iconst(pointer_type, 0),
}
}
Expand All @@ -1380,11 +1383,11 @@ impl<'a> TrampolineCompiler<'a> {
) -> ir::Value {
let pointer_type = self.isa.pointer_type();
match post_return {
Some(idx) => self.alias_regions.vmcomponent_runtime_post_return(
&mut self.builder.cursor(),
vmctx,
idx,
),
Some(idx) => self
.alias_regions
.vmcomponent()
.post_returns(idx)
.load(&mut self.builder.cursor(), vmctx),
None => self.builder.ins().iconst(pointer_type, 0),
}
}
Expand All @@ -1402,7 +1405,9 @@ impl<'a> TrampolineCompiler<'a> {
// per-process.
let builtins_array = self
.alias_regions
.vmcomponent_builtins(&mut self.builder.cursor(), vmctx);
.vmcomponent()
.builtins()
.load(&mut self.builder.cursor(), vmctx);
// Next load the function pointer at `offset` and return that.
self.alias_regions
.component_builtin_functions_array_element(
Expand Down Expand Up @@ -1543,11 +1548,11 @@ impl<'a> TrampolineCompiler<'a> {
fn check_may_leave_instance(&mut self, instance: RuntimeComponentInstanceIndex) {
let vmctx = self.builder.func.dfg.block_params(self.block0)[0];

let may_leave = self.alias_regions.vmcomponent_instance_may_leave(
&mut self.builder.cursor(),
vmctx,
instance,
);
let may_leave = self
.alias_regions
.vmcomponent()
.may_leave(instance)
.load(&mut self.builder.cursor(), vmctx);
let (mut traps, builder) = self.traps();
traps.trapz(builder, may_leave, TRAP_CANNOT_LEAVE_COMPONENT);
}
Expand Down Expand Up @@ -1581,7 +1586,9 @@ impl<'a> TrampolineCompiler<'a> {
fn load_vm_store_context(&mut self) -> ir::Value {
let caller_vmctx = self.abi_load_params()[1];
self.alias_regions
.vmctx_store_context(&mut self.builder.cursor(), caller_vmctx)
.vmctx()
.store_context()
.load(&mut self.builder.cursor(), caller_vmctx)
}
}

Expand Down Expand Up @@ -1690,22 +1697,17 @@ impl ComponentCompiler for Compiler {
// Implement the array-abi trampoline in terms of calling the
// wasm-abi trampoline.
Abi::Array => {
let offsets =
VMComponentOffsets::new(self.isa.pointer_bytes(), &component.component);
return Ok(self.array_to_wasm_trampoline(
key,
FuncKey::ComponentTrampoline(Abi::Wasm, trampoline_index),
sig,
symbol,
wasmtime_environ::component::VMCOMPONENT_MAGIC,
|alias_regions, pointer_type, cursor, vmctx| {
alias_regions.vmcomponent_context_generic_load(
cursor,
pointer_type,
ir::MemFlagsData::trusted().with_readonly().with_can_move(),
vmctx,
offsets.vm_store_context(),
)
|alias_regions, _pointer_type, cursor, vmctx| {
alias_regions
.vmcomponent()
.store_context()
.load(cursor, vmctx)
},
)?);
}
Expand Down Expand Up @@ -1774,22 +1776,17 @@ impl ComponentCompiler for Compiler {
// Implement the array-abi trampoline in terms of calling the
// wasm-abi trampoline.
Abi::Array => {
let offsets =
VMComponentOffsets::new(self.isa.pointer_bytes(), &component.component);
return Ok(self.array_to_wasm_trampoline(
FuncKey::UnsafeIntrinsic(abi, intrinsic),
FuncKey::UnsafeIntrinsic(Abi::Wasm, intrinsic),
&wasm_func_ty,
symbol,
wasmtime_environ::component::VMCOMPONENT_MAGIC,
|alias_regions, pointer_type, cursor, vmctx| {
alias_regions.vmcomponent_context_generic_load(
cursor,
pointer_type,
ir::MemFlagsData::trusted().with_readonly().with_can_move(),
vmctx,
offsets.vm_store_context(),
)
|alias_regions, _pointer_type, cursor, vmctx| {
alias_regions
.vmcomponent()
.store_context()
.load(cursor, vmctx)
},
)?);
}
Expand Down Expand Up @@ -2427,7 +2424,9 @@ where
let caller_vmctx = params[1];
self.traps
.alias_regions()
.vmctx_store_context(&mut self.builder.cursor(), caller_vmctx)
.vmctx()
.store_context()
.load(&mut self.builder.cursor(), caller_vmctx)
}
}

Expand Down
9 changes: 6 additions & 3 deletions crates/cranelift/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,20 @@ impl<'a> Compilation<'a> {
let memory_offset = if ofs.num_imported_memories > 0 {
let index = MemoryIndex::new(0);
ModuleMemoryOffset::Imported {
offset_to_vm_memory_definition: ofs.vmctx_vmmemory_import(index)
offset_to_vm_memory_definition: ofs.imported_memories().at(index)
+ u32::from(ofs.ptr.vm_memory_import().from()),
offset_to_memory_base: ofs.ptr.vm_memory_definition().base().into(),
}
} else if ofs.num_owned_memories > 0 {
let index = OwnedMemoryIndex::new(0);
ModuleMemoryOffset::Defined(ofs.vmctx_vmmemory_definition_base(index))
ModuleMemoryOffset::Defined(
ofs.owned_memories().at(index)
+ u32::from(ofs.ptr.vm_memory_definition().base()),
)
} else if ofs.num_defined_memories > 0 {
let index = DefinedMemoryIndex::new(0);
ModuleMemoryOffset::Imported {
offset_to_vm_memory_definition: ofs.vmctx_vmmemory_pointer(index),
offset_to_vm_memory_definition: ofs.memories().at(index),
offset_to_memory_base: ofs.ptr.vm_memory_definition().base().into(),
}
} else {
Expand Down
Loading
Loading