Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnects Store state fields from Compiler #1761

Merged
merged 39 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
aef8458
mv SignatureRegistry
yurydelendik May 22, 2020
050aebe
mv code_memory
yurydelendik May 22, 2020
d7defe2
Move Arc to CompiledModule level; unsafe Sync+Send
yurydelendik May 26, 2020
5a3a142
Threads example
yurydelendik May 26, 2020
9b08221
rm interrupts from Compiler
yurydelendik May 26, 2020
45717c3
rm Sync+Send from Module
yurydelendik May 26, 2020
dc497a5
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik May 26, 2020
d47371f
c api
yurydelendik May 26, 2020
d4e4ade
rn module_ref()
yurydelendik May 26, 2020
a2b432b
small fixes
yurydelendik May 26, 2020
ede93de
rm Send+Sync from CompiledModule
yurydelendik May 26, 2020
2e73c4a
rm RawCompiledModule
yurydelendik May 26, 2020
edcf487
rm jit_code_registration; win32 test; any
yurydelendik May 27, 2020
3f7c2a5
rm SignatureRegistry lock
yurydelendik May 27, 2020
1ccdee7
replace Deref with InstanceContext
yurydelendik May 27, 2020
810ec8a
fix test
yurydelendik May 27, 2020
3ab0e3c
Redesign API to remove Store from Module::new
yurydelendik May 27, 2020
51b901b
fix docs
yurydelendik May 27, 2020
02f79f8
fix fuzz
yurydelendik May 27, 2020
2009eb7
jit_int comments; fix 'let _ =' lifetime
yurydelendik Jun 1, 2020
02bd6f4
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik Jun 1, 2020
2f23c7f
Fix linker tests
yurydelendik Jun 1, 2020
bde14b9
Assert Module and Compiler's Sync+Send
yurydelendik Jun 1, 2020
3f04e73
One Compiler instance per Engine
yurydelendik Jun 1, 2020
6fd50db
Add thread safety doc
yurydelendik Jun 1, 2020
acc6603
check same engine constraint
yurydelendik Jun 1, 2020
b3d3eda
Reduces amount of held Module data
yurydelendik Jun 1, 2020
6e1a590
Change signatures of wasmtime_module_new/wasmtime_instance_new
yurydelendik Jun 1, 2020
449cc52
mv alloc/drop outside of lock
yurydelendik Jun 1, 2020
4048b5a
mv build_compiler()
yurydelendik Jun 1, 2020
ce9bedf
register_jit_code comment
yurydelendik Jun 1, 2020
9cf7a61
Remove signatures() accessors where possible
yurydelendik Jun 1, 2020
88093a6
fix tests
yurydelendik Jun 1, 2020
fc9d289
revert some changes
yurydelendik Jun 1, 2020
4cefa4a
Fix intermitent failures in threads tests
yurydelendik Jun 1, 2020
5fc0a16
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik Jun 2, 2020
65f2c00
mv unwrap(); reduce code churn
yurydelendik Jun 2, 2020
757ce85
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik Jun 2, 2020
430a417
RELEASES.md
yurydelendik Jun 2, 2020
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 change: 1 addition & 0 deletions Cargo.lock

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

41 changes: 40 additions & 1 deletion crates/c-api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{handle_result, wasmtime_error_t};
use crate::{wasm_byte_vec_t, wasm_exporttype_vec_t, wasm_importtype_vec_t};
use crate::{wasm_exporttype_t, wasm_importtype_t, wasm_store_t};
use std::ptr;
use wasmtime::{HostRef, Module};
use wasmtime::{HostRef, Module, SendableModule};

#[repr(C)]
#[derive(Clone)]
Expand All @@ -20,6 +20,14 @@ impl wasm_module_t {
}
}

#[repr(C)]
#[derive(Clone)]
pub struct wasm_shared_module_t {
module: SendableModule,
}

wasmtime_c_api_macros::declare_own!(wasm_shared_module_t);

#[no_mangle]
pub extern "C" fn wasm_module_new(
store: &wasm_store_t,
Expand Down Expand Up @@ -95,3 +103,34 @@ pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_imp
.collect::<Vec<_>>();
out.set_buffer(buffer);
}

#[no_mangle]
pub extern "C" fn wasm_module_share(module: &wasm_module_t) -> Box<wasm_shared_module_t> {
Box::new(wasm_shared_module_t {
module: module.module.borrow().share(),
})
}

#[no_mangle]
pub extern "C" fn wasm_module_obtain(
store: &wasm_store_t,
shared_module: &wasm_shared_module_t,
) -> Box<wasm_module_t> {
let module = shared_module
.module
.clone()
.place_into(&store.store.borrow());
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
.collect::<Vec<_>>();
let exports = module
.exports()
.map(|e| wasm_exporttype_t::new(e.name().to_owned(), e.ty()))
.collect::<Vec<_>>();
Box::new(wasm_module_t {
module: HostRef::new(module),
imports,
exports,
})
}
11 changes: 5 additions & 6 deletions crates/jit/src/code_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ impl CodeMemoryEntry {
Ok(Self { mmap, registry })
}

fn contains(&self, addr: usize) -> bool {
fn range(&self) -> (usize, usize) {
let start = self.mmap.as_ptr() as usize;
let end = start + self.mmap.len();
start <= addr && addr < end
(start, end)
}
}

Expand Down Expand Up @@ -243,11 +243,10 @@ impl CodeMemory {
Ok(())
}

/// Returns whether any published segment of this code memory contains
/// `addr`.
pub fn published_contains(&self, addr: usize) -> bool {
/// Returns all published segment ranges.
pub fn published_ranges<'a>(&'a self) -> impl Iterator<Item = (usize, usize)> + 'a {
self.entries[..self.published]
.iter()
.any(|entry| entry.contains(addr))
.map(|entry| entry.range())
}
}
64 changes: 19 additions & 45 deletions crates/jit/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ use cranelift_codegen::Context;
use cranelift_codegen::{binemit, ir};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use std::collections::HashMap;
use std::sync::Arc;
use wasmtime_debug::{emit_debugsections_image, DebugInfoData};
use wasmtime_environ::entity::{EntityRef, PrimaryMap};
use wasmtime_environ::isa::{TargetFrontendConfig, TargetIsa};
use wasmtime_environ::wasm::{DefinedFuncIndex, DefinedMemoryIndex, MemoryIndex};
use wasmtime_environ::wasm::{DefinedFuncIndex, DefinedMemoryIndex, MemoryIndex, SignatureIndex};
use wasmtime_environ::{
CacheConfig, CompileError, CompiledFunction, Compiler as _C, ModuleAddressMap,
ModuleMemoryOffset, ModuleTranslation, ModuleVmctxInfo, Relocation, RelocationTarget,
Relocations, Traps, Tunables, VMOffsets,
};
use wasmtime_runtime::{
InstantiationError, SignatureRegistry, VMFunctionBody, VMInterrupts, VMSharedSignatureIndex,
VMTrampoline,
};
use wasmtime_runtime::{InstantiationError, VMFunctionBody, VMTrampoline};

/// Select which kind of compilation to use.
#[derive(Copy, Clone, Debug)]
Expand All @@ -48,12 +44,9 @@ pub enum CompilationStrategy {
/// TODO: Consider using cranelift-module.
pub struct Compiler {
isa: Box<dyn TargetIsa>,
code_memory: CodeMemory,
signatures: SignatureRegistry,
strategy: CompilationStrategy,
cache_config: CacheConfig,
tunables: Tunables,
interrupts: Arc<VMInterrupts>,
}

impl Compiler {
Expand All @@ -66,29 +59,32 @@ impl Compiler {
) -> Self {
Self {
isa,
code_memory: CodeMemory::new(),
signatures: SignatureRegistry::new(),
strategy,
cache_config,
tunables,
interrupts: Arc::new(VMInterrupts::default()),
}
}
}

#[allow(missing_docs)]
pub struct Compilation {
pub code_memory: CodeMemory,
pub finished_functions: PrimaryMap<DefinedFuncIndex, *mut [VMFunctionBody]>,
pub relocations: Relocations,
pub trampolines: HashMap<VMSharedSignatureIndex, VMTrampoline>,
pub trampoline_relocations: HashMap<VMSharedSignatureIndex, Vec<Relocation>>,
pub trampolines: PrimaryMap<SignatureIndex, VMTrampoline>,
pub trampoline_relocations: HashMap<SignatureIndex, Vec<Relocation>>,
pub jt_offsets: PrimaryMap<DefinedFuncIndex, ir::JumpTableOffsets>,
pub dbg_image: Option<Vec<u8>>,
pub traps: Traps,
pub address_transform: ModuleAddressMap,
}

impl Compiler {
/// Return the isa.
pub fn isa(&self) -> &dyn TargetIsa {
self.isa.as_ref()
}

/// Return the target's frontend configuration settings.
pub fn frontend_config(&self) -> TargetFrontendConfig {
self.isa.frontend_config()
Expand All @@ -99,17 +95,14 @@ impl Compiler {
&self.tunables
}

/// Return the handle by which to interrupt instances
pub fn interrupts(&self) -> &Arc<VMInterrupts> {
&self.interrupts
}

/// Compile the given function bodies.
pub(crate) fn compile<'data>(
&mut self,
&self,
translation: &ModuleTranslation,
debug_data: Option<DebugInfoData>,
) -> Result<Compilation, SetupError> {
let mut code_memory = CodeMemory::new();

let (compilation, relocations, address_transform, value_ranges, stack_slots, traps) =
match self.strategy {
// For now, interpret `Auto` as `Cranelift` since that's the most stable
Expand All @@ -135,7 +128,7 @@ impl Compiler {
// Allocate all of the compiled functions into executable memory,
// copying over their contents.
let finished_functions =
allocate_functions(&mut self.code_memory, &compilation).map_err(|message| {
allocate_functions(&mut code_memory, &compilation).map_err(|message| {
SetupError::Instantiate(InstantiationError::Resource(format!(
"failed to allocate memory for functions: {}",
message
Expand All @@ -147,21 +140,17 @@ impl Compiler {
// guarantees that all functions (including indirect ones through
// tables) have a trampoline when invoked through the wasmtime API.
let mut cx = FunctionBuilderContext::new();
let mut trampolines = HashMap::new();
let mut trampolines = PrimaryMap::new();
let mut trampoline_relocations = HashMap::new();
for sig in translation.module.local.signatures.values() {
let index = self.signatures.register(sig);
if trampolines.contains_key(&index) {
continue;
}
for (index, sig) in translation.module.local.signatures.iter() {
let (trampoline, relocations) = make_trampoline(
&*self.isa,
&mut self.code_memory,
&mut code_memory,
&mut cx,
sig,
std::mem::size_of::<u128>(),
)?;
trampolines.insert(index, trampoline);
trampolines.push(trampoline);

// Typically trampolines do not have relocations, so if one does
// show up be sure to log it in case anyone's listening and there's
Expand Down Expand Up @@ -215,6 +204,7 @@ impl Compiler {
let jt_offsets = compilation.get_jt_offsets();

Ok(Compilation {
code_memory,
finished_functions,
relocations,
trampolines,
Expand All @@ -225,22 +215,6 @@ impl Compiler {
address_transform,
})
}

/// Make memory containing compiled code executable.
pub(crate) fn publish_compiled_code(&mut self) {
self.code_memory.publish(self.isa.as_ref());
}

/// Shared signature registry.
pub fn signatures(&self) -> &SignatureRegistry {
&self.signatures
}

/// Returns whether or not the given address falls within the JIT code
/// managed by the compiler
pub fn is_in_jit_code(&self, addr: usize) -> bool {
self.code_memory.published_contains(addr)
}
}

/// Create a trampoline for invoking a function.
Expand Down
Loading