Skip to content

Commit

Permalink
rm RawCompiledModule
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed May 26, 2020
1 parent 7691be6 commit b54ba6b
Showing 1 changed file with 44 additions and 94 deletions.
138 changes: 44 additions & 94 deletions crates/jit/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,40 @@ pub enum SetupError {
DebugInfo(#[from] anyhow::Error),
}

/// This is similar to `CompiledModule`, but references the data initializers
/// from the wasm buffer rather than holding its own copy.
struct RawCompiledModule<'data> {
struct FinishedFunctions(BoxedSlice<DefinedFuncIndex, *mut [VMFunctionBody]>);

unsafe impl Send for CompiledModule {}
unsafe impl Sync for CompiledModule {}

/// A compiled wasm module, ready to be instantiated.
pub struct CompiledModule {
code_memory: CodeMemory,
module: Module,
finished_functions: BoxedSlice<DefinedFuncIndex, *mut [VMFunctionBody]>,
finished_functions: FinishedFunctions,
trampolines: PrimaryMap<SignatureIndex, VMTrampoline>,
data_initializers: Box<[DataInitializer<'data>]>,
data_initializers: Box<[OwnedDataInitializer]>,
#[allow(dead_code)]
dbg_jit_registration: Option<GdbJitImageRegistration>,
traps: Traps,
address_transform: ModuleAddressMap,
}

impl<'data> RawCompiledModule<'data> {
/// Create a new `RawCompiledModule` by compiling the wasm module in `data` and instatiating it.
fn new(
impl std::ops::Deref for CompiledModule {
type Target = Module;
fn deref(&self) -> &Self::Target {
&self.module
}
}

impl std::ops::DerefMut for CompiledModule {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.module
}
}

impl CompiledModule {
/// Compile a data buffer into a `CompiledModule`, which may then be instantiated.
pub fn new<'data>(
compiler: &Compiler,
data: &'data [u8],
profiler: &dyn ProfilingAgent,
Expand All @@ -83,15 +101,24 @@ impl<'data> RawCompiledModule<'data> {

let compilation = compiler.compile(&translation, debug_data)?;

link_module(&translation.module, &compilation);
let module = translation.module;

link_module(&module, &compilation);

// Make all code compiled thus far executable.
let mut code_memory = compilation.code_memory;
code_memory.publish(compiler.isa());

let data_initializers = translation
.data_initializers
.into_iter()
.map(OwnedDataInitializer::new)
.collect::<Vec<_>>()
.into_boxed_slice();

// Initialize profiler and load the wasm module
profiler.module_load(
&translation.module,
&module,
&compilation.finished_functions,
compilation.dbg_image.as_deref(),
);
Expand All @@ -105,97 +132,20 @@ impl<'data> RawCompiledModule<'data> {
None
};

let finished_functions =
FinishedFunctions(compilation.finished_functions.into_boxed_slice());

Ok(Self {
code_memory,
module: translation.module,
finished_functions: compilation.finished_functions.into_boxed_slice(),
module,
finished_functions,
trampolines: compilation.trampolines,
data_initializers: translation.data_initializers.into_boxed_slice(),
data_initializers,
dbg_jit_registration,
traps: compilation.traps,
address_transform: compilation.address_transform,
})
}
}

struct FinishedFunctions(BoxedSlice<DefinedFuncIndex, *mut [VMFunctionBody]>);

unsafe impl Send for CompiledModule {}
unsafe impl Sync for CompiledModule {}

/// A compiled wasm module, ready to be instantiated.
pub struct CompiledModule {
code_memory: CodeMemory,
module: Module,
finished_functions: FinishedFunctions,
trampolines: PrimaryMap<SignatureIndex, VMTrampoline>,
data_initializers: Box<[OwnedDataInitializer]>,
#[allow(dead_code)]
dbg_jit_registration: Option<GdbJitImageRegistration>,
traps: Traps,
address_transform: ModuleAddressMap,
}

impl std::ops::Deref for CompiledModule {
type Target = Module;
fn deref(&self) -> &Self::Target {
&self.module
}
}

impl std::ops::DerefMut for CompiledModule {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.module
}
}

impl CompiledModule {
/// Compile a data buffer into a `CompiledModule`, which may then be instantiated.
pub fn new<'data>(
compiler: &Compiler,
data: &'data [u8],
profiler: &dyn ProfilingAgent,
) -> Result<Self, SetupError> {
let raw = RawCompiledModule::<'data>::new(compiler, data, profiler)?;

Ok(Self::from_parts(
raw.code_memory,
raw.module,
raw.finished_functions,
raw.trampolines,
raw.data_initializers
.iter()
.map(OwnedDataInitializer::new)
.collect::<Vec<_>>()
.into_boxed_slice(),
raw.dbg_jit_registration,
raw.traps,
raw.address_transform,
))
}

/// Construct a `CompiledModule` from component parts.
pub fn from_parts(
code_memory: CodeMemory,
module: Module,
finished_functions: BoxedSlice<DefinedFuncIndex, *mut [VMFunctionBody]>,
trampolines: PrimaryMap<SignatureIndex, VMTrampoline>,
data_initializers: Box<[OwnedDataInitializer]>,
dbg_jit_registration: Option<GdbJitImageRegistration>,
traps: Traps,
address_transform: ModuleAddressMap,
) -> Self {
Self {
code_memory,
module: module,
finished_functions: FinishedFunctions(finished_functions),
trampolines,
data_initializers,
dbg_jit_registration,
traps,
address_transform,
}
}

/// Crate an `Instance` from this `CompiledModule`.
///
Expand Down Expand Up @@ -292,7 +242,7 @@ pub struct OwnedDataInitializer {
}

impl OwnedDataInitializer {
fn new(borrowed: &DataInitializer<'_>) -> Self {
fn new(borrowed: DataInitializer<'_>) -> Self {
Self {
location: borrowed.location.clone(),
data: borrowed.data.to_vec().into_boxed_slice(),
Expand Down

0 comments on commit b54ba6b

Please sign in to comment.