Include backend metadata in tiering module.

This commit is contained in:
losfair 2019-10-11 21:05:17 +08:00
parent db59127f71
commit 94f97109ff

View File

@ -1,4 +1,4 @@
use crate::backend::{Compiler, CompilerConfig}; use crate::backend::{Backend, Compiler, CompilerConfig};
use crate::compile_with_config; use crate::compile_with_config;
use crate::fault::{ use crate::fault::{
catch_unsafe_unwind, ensure_sighandler, pop_code_version, push_code_version, with_ctx, catch_unsafe_unwind, ensure_sighandler, pop_code_version, push_code_version, with_ctx,
@ -36,6 +36,7 @@ struct OptimizationState {
} }
struct OptimizationOutcome { struct OptimizationOutcome {
backend_id: Backend,
module: Module, module: Module,
} }
@ -46,6 +47,7 @@ unsafe impl Sync for CtxWrapper {}
unsafe fn do_optimize( unsafe fn do_optimize(
binary: &[u8], binary: &[u8],
backend_id: Backend,
compiler: Box<dyn Compiler>, compiler: Box<dyn Compiler>,
ctx: &Mutex<CtxWrapper>, ctx: &Mutex<CtxWrapper>,
state: &OptimizationState, state: &OptimizationState,
@ -65,7 +67,7 @@ unsafe fn do_optimize(
let ctx_inner = ctx.lock().unwrap(); let ctx_inner = ctx.lock().unwrap();
if !ctx_inner.0.is_null() { if !ctx_inner.0.is_null() {
*state.outcome.lock().unwrap() = Some(OptimizationOutcome { module }); *state.outcome.lock().unwrap() = Some(OptimizationOutcome { backend_id, module });
set_wasm_interrupt_on_ctx(ctx_inner.0); set_wasm_interrupt_on_ctx(ctx_inner.0);
} }
} }
@ -77,7 +79,8 @@ pub unsafe fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
import_object: &ImportObject, import_object: &ImportObject,
start_raw: extern "C" fn(&mut Ctx), start_raw: extern "C" fn(&mut Ctx),
baseline: &mut Instance, baseline: &mut Instance,
optimized_backends: Vec<Box<dyn Fn() -> Box<dyn Compiler> + Send>>, baseline_backend: Backend,
optimized_backends: Vec<(Backend, Box<dyn Fn() -> Box<dyn Compiler> + Send>)>,
interactive_shell: F, interactive_shell: F,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_sighandler(); ensure_sighandler();
@ -99,9 +102,9 @@ pub unsafe fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
let ctx_box = ctx_box.clone(); let ctx_box = ctx_box.clone();
let opt_state = opt_state.clone(); let opt_state = opt_state.clone();
::std::thread::spawn(move || { ::std::thread::spawn(move || {
for backend in optimized_backends { for (backend_id, backend) in optimized_backends {
if !ctx_box.lock().unwrap().0.is_null() { if !ctx_box.lock().unwrap().0.is_null() {
do_optimize(&wasm_binary, backend(), &ctx_box, &opt_state); do_optimize(&wasm_binary, backend_id, backend(), &ctx_box, &opt_state);
} }
} }
}); });
@ -117,6 +120,7 @@ pub unsafe fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
.get_module_state_map() .get_module_state_map()
.unwrap(), .unwrap(),
base: baseline.module.runnable_module.get_code().unwrap().as_ptr() as usize, base: baseline.module.runnable_module.get_code().unwrap().as_ptr() as usize,
backend: baseline_backend,
}); });
let n_versions: Cell<usize> = Cell::new(1); let n_versions: Cell<usize> = Cell::new(1);
@ -127,7 +131,7 @@ pub unsafe fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
})); }));
loop { loop {
let new_optimized: Option<&mut Instance> = { let new_optimized: Option<(Backend, &mut Instance)> = {
let mut outcome = opt_state.outcome.lock().unwrap(); let mut outcome = opt_state.outcome.lock().unwrap();
if let Some(x) = outcome.take() { if let Some(x) = outcome.take() {
let instance = x let instance = x
@ -136,12 +140,12 @@ pub unsafe fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
.map_err(|e| format!("Can't instantiate module: {:?}", e))?; .map_err(|e| format!("Can't instantiate module: {:?}", e))?;
// Keep the optimized code alive. // Keep the optimized code alive.
optimized_instances.push(instance); optimized_instances.push(instance);
optimized_instances.last_mut() optimized_instances.last_mut().map(|y| (x.backend_id, y))
} else { } else {
None None
} }
}; };
if let Some(optimized) = new_optimized { if let Some((backend_id, optimized)) = new_optimized {
let base = module_info.imported_functions.len(); let base = module_info.imported_functions.len();
let code_ptr = optimized let code_ptr = optimized
.module .module
@ -178,6 +182,7 @@ pub unsafe fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
.get_code() .get_code()
.unwrap() .unwrap()
.as_ptr() as usize, .as_ptr() as usize,
backend: backend_id,
}); });
n_versions.set(n_versions.get() + 1); n_versions.set(n_versions.get() + 1);