From 32a95a89eba6138cdf84030c19c205816f1d8c0a Mon Sep 17 00:00:00 2001 From: Ryan Hunt Date: Mon, 6 Jan 2020 15:36:06 -0600 Subject: [PATCH] Wasm: Add support for typed select instruction Reference types introduces a typed select operation. It has identical execution semantics so no codegen change is needed. --- cranelift/wasm/src/code_translator.rs | 10 +++++++--- cranelift/wasmtests/select.wat | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 cranelift/wasmtests/select.wat diff --git a/cranelift/wasm/src/code_translator.rs b/cranelift/wasm/src/code_translator.rs index 58da8536ae..dcbfe8eeb9 100644 --- a/cranelift/wasm/src/code_translator.rs +++ b/cranelift/wasm/src/code_translator.rs @@ -132,6 +132,13 @@ pub fn translate_operator( let (arg1, arg2, cond) = state.pop3(); state.push1(builder.ins().select(cond, arg1, arg2)); } + Operator::TypedSelect { ty: _ } => { + // We ignore the explicit type parameter as it is only needed for + // validation, which we require to have been performed before + // translation. + let (arg1, arg2, cond) = state.pop3(); + state.push1(builder.ins().select(cond, arg1, arg2)); + } Operator::Nop => { // We do nothing } @@ -968,9 +975,6 @@ pub fn translate_operator( Operator::F32Le | Operator::F64Le => { translate_fcmp(FloatCC::LessThanOrEqual, builder, state) } - Operator::TypedSelect { .. } => { - return Err(wasm_unsupported!("proposed typed select operator {:?}", op)) - } Operator::RefNull => state.push1(builder.ins().null(environ.reference_type())), Operator::RefIsNull => { let arg = state.pop1(); diff --git a/cranelift/wasmtests/select.wat b/cranelift/wasmtests/select.wat new file mode 100644 index 0000000000..45ef241833 --- /dev/null +++ b/cranelift/wasmtests/select.wat @@ -0,0 +1,19 @@ +(module + (func $untyped-select (result i32) + i32.const 42 + i32.const 24 + i32.const 1 + select) + + (func $typed-select-1 (result anyref) + ref.null + ref.null + i32.const 1 + select (result anyref)) + + (func $typed-select-2 (param anyref) (result anyref) + ref.null + local.get 0 + i32.const 1 + select (result anyref)) +)