Skip to content

Commit

Permalink
Try #1303:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Mar 17, 2020
2 parents 4746d04 + a9cd6d6 commit f95e034
Show file tree
Hide file tree
Showing 10 changed files with 1,469 additions and 357 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## **[Unreleased]**

- [#1303](https://github.com/wasmerio/wasmer/pull/1303) NaN canonicalization for singlepass backend.
- [#1305](https://github.com/wasmerio/wasmer/pull/1305) Handle panics from DynamicFunc.
- [#1301](https://github.com/wasmerio/wasmer/pull/1301) Update supported stable Rust version to 1.41.1.
- [#1300](https://github.com/wasmerio/wasmer/pull/1300) Add support for multiple versions of WASI tests: wasitests now test all versions of WASI.
Expand Down
30 changes: 21 additions & 9 deletions lib/clif-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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>>>,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand All @@ -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));
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions lib/clif-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ use cranelift_codegen::{
settings::{self, Configurable},
};
use target_lexicon::Triple;
use wasmer_runtime_core::{backend::CompilerConfig, codegen::SimpleStreamingCompilerGen};

#[macro_use]
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();
Expand All @@ -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
Expand All @@ -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<
Expand Down
6 changes: 2 additions & 4 deletions lib/clif-backend/src/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3701,7 +3701,7 @@ impl<'ctx> FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator<'ct
.try_as_basic_value()
.left()
.unwrap();
state.push1_extra(res, i);
state.push1_extra(res, i | ExtraInfo::pending_f32_nan());
}
Operator::F64Trunc => {
let (v, i) = state.pop1_extra()?;
Expand All @@ -3714,7 +3714,7 @@ impl<'ctx> FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator<'ct
.try_as_basic_value()
.left()
.unwrap();
state.push1_extra(res, i);
state.push1_extra(res, i | ExtraInfo::pending_f64_nan());
}
Operator::F32Nearest => {
let (v, i) = state.pop1_extra()?;
Expand All @@ -3727,7 +3727,7 @@ impl<'ctx> FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator<'ct
.try_as_basic_value()
.left()
.unwrap();
state.push1_extra(res, i);
state.push1_extra(res, i | ExtraInfo::pending_f32_nan());
}
Operator::F64Nearest => {
let (v, i) = state.pop1_extra()?;
Expand All @@ -3740,7 +3740,7 @@ impl<'ctx> FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator<'ct
.try_as_basic_value()
.left()
.unwrap();
state.push1_extra(res, i);
state.push1_extra(res, i | ExtraInfo::pending_f64_nan());
}
Operator::F32Abs => {
let (v, i) = state.pop1_extra()?;
Expand Down
4 changes: 4 additions & 0 deletions lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ pub struct CompilerConfig {
/// When enabled there can be a small amount of runtime performance overhead.
pub full_preemption: bool,

/// Whether to enable spec-compliant NaN canonicalization at all places.
/// Enabling this increases runtime overhead.
pub nan_canonicalization: bool,

pub features: Features,

// Target info. Presently only supported by LLVM.
Expand Down
Loading

0 comments on commit f95e034

Please sign in to comment.