Enable nan canonicalization for cranelift backend.

This commit is contained in:
losfair
2020-03-17 13:09:22 +08:00
parent 8f2bb8464a
commit 3ee7f43b1c
3 changed files with 31 additions and 16 deletions

View File

@ -19,7 +19,7 @@ use std::mem;
use std::sync::{Arc, RwLock};
use wasmer_runtime_core::error::CompileError;
use wasmer_runtime_core::{
backend::{CacheGen, Token},
backend::{CacheGen, CompilerConfig, Token},
cache::{Artifact, Error as CacheError},
codegen::*,
memory::MemoryType,
@ -36,7 +36,7 @@ use wasmparser::Type as WpType;
static BACKEND_ID: &str = "cranelift";
pub struct CraneliftModuleCodeGenerator {
isa: Box<dyn isa::TargetIsa>,
isa: Option<Box<dyn isa::TargetIsa>>,
signatures: Option<Arc<Map<SigIndex, FuncSig>>>,
pub clif_signatures: Map<SigIndex, ir::Signature>,
function_signatures: Option<Arc<Map<FuncIndex, SigIndex>>>,
@ -47,9 +47,8 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
for CraneliftModuleCodeGenerator
{
fn new() -> Self {
let isa = get_isa();
CraneliftModuleCodeGenerator {
isa,
isa: None,
clif_signatures: Map::new(),
functions: vec![],
function_signatures: None,
@ -100,7 +99,7 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
position: Position::default(),
func_env: FunctionEnvironment {
module_info: Arc::clone(&module_info),
target_config: self.isa.frontend_config().clone(),
target_config: self.isa.as_ref().unwrap().frontend_config().clone(),
clif_signatures: self.clif_signatures.clone(),
},
loc,
@ -162,9 +161,9 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
}
let (func_resolver_builder, debug_metadata, handler_data) =
FuncResolverBuilder::new(&*self.isa, func_bodies, module_info)?;
FuncResolverBuilder::new(&**self.isa.as_ref().unwrap(), func_bodies, module_info)?;
let trampolines = Arc::new(Trampolines::new(&*self.isa, module_info));
let trampolines = Arc::new(Trampolines::new(&**self.isa.as_ref().unwrap(), module_info));
let signatures_empty = Map::new();
let signatures = if self.signatures.is_some() {
@ -191,9 +190,19 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
))
}
fn feed_compiler_config(&mut self, config: &CompilerConfig) -> Result<(), CodegenError> {
self.isa = Some(get_isa(Some(config)));
Ok(())
}
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), CodegenError> {
self.signatures = Some(Arc::new(signatures));
let call_conv = self.isa.frontend_config().default_call_conv;
let call_conv = self
.isa
.as_ref()
.unwrap()
.frontend_config()
.default_call_conv;
for (_sig_idx, func_sig) in self.signatures.as_ref().unwrap().iter() {
self.clif_signatures
.push(convert_func_sig(func_sig, call_conv));
@ -1302,7 +1311,10 @@ fn generate_signature(
}
fn pointer_type(mcg: &CraneliftModuleCodeGenerator) -> ir::Type {
ir::Type::int(u16::from(mcg.isa.frontend_config().pointer_bits())).unwrap()
ir::Type::int(u16::from(
mcg.isa.as_ref().unwrap().frontend_config().pointer_bits(),
))
.unwrap()
}
/// Declare local variables for the signature parameters that correspond to WebAssembly locals.

View File

@ -29,6 +29,7 @@ use cranelift_codegen::{
settings::{self, Configurable},
};
use target_lexicon::Triple;
use wasmer_runtime_core::{backend::CompilerConfig, codegen::SimpleStreamingCompilerGen};
#[macro_use]
extern crate serde_derive;
@ -36,7 +37,7 @@ extern crate serde_derive;
extern crate rayon;
extern crate serde;
fn get_isa() -> Box<dyn isa::TargetIsa> {
fn get_isa(config: Option<&CompilerConfig>) -> Box<dyn isa::TargetIsa> {
let flags = {
let mut builder = settings::builder();
builder.set("opt_level", "speed_and_size").unwrap();
@ -48,6 +49,12 @@ fn get_isa() -> Box<dyn isa::TargetIsa> {
builder.set("enable_verifier", "false").unwrap();
}
if let Some(config) = config {
if config.nan_canonicalization {
builder.set("enable_nan_canonicalization", "true").unwrap();
}
}
let flags = settings::Flags::new(builder);
debug_assert_eq!(flags.opt_level(), settings::OptLevel::SpeedAndSize);
flags
@ -58,8 +65,6 @@ fn get_isa() -> Box<dyn isa::TargetIsa> {
/// The current version of this crate
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
use wasmer_runtime_core::codegen::SimpleStreamingCompilerGen;
/// Streaming compiler implementation for the Cranelift backed. Compiles web assembly binary into
/// machine code.
pub type CraneliftCompiler = SimpleStreamingCompilerGen<

View File

@ -212,8 +212,7 @@ fn wasm_ty_to_clif(ty: Type) -> ir::types::Type {
}
fn generate_trampoline_signature() -> ir::Signature {
let isa = super::get_isa();
let call_convention = isa.default_call_conv();
let call_convention = super::get_isa(None).default_call_conv();
let mut sig = ir::Signature::new(call_convention);
let ptr_param = ir::AbiParam {
@ -229,8 +228,7 @@ fn generate_trampoline_signature() -> ir::Signature {
}
fn generate_export_signature(func_sig: &FuncSig) -> ir::Signature {
let isa = super::get_isa();
let call_convention = isa.default_call_conv();
let call_convention = super::get_isa(None).default_call_conv();
let mut export_clif_sig = ir::Signature::new(call_convention);
let func_sig_iter = func_sig.params().iter().map(|wasm_ty| ir::AbiParam {