Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asm2wasm wasm-only i64 switches with a large (>32 bit) offset #1109 #1111

Merged
merged 1 commit into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/asm2wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,12 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {

auto top = allocator.alloc<Block>();
if (canSwitch) {

// we may need a break for the case where the condition doesn't match
// any of the cases. it should go to the default, if we have one, or
// outside if not
Break* breakWhenNotMatching = nullptr;

if (br->condition->type == i32) {
Binary* offsetor = allocator.alloc<Binary>();
offsetor->op = BinaryOp::SubInt32;
Expand All @@ -2554,7 +2560,28 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
offsetor->left = br->condition;
offsetor->right = builder.makeConst(Literal(int64_t(min)));
offsetor->type = i64;
br->condition = builder.makeUnary(UnaryOp::WrapInt64, offsetor); // TODO: check this fits in 32 bits
// the switch itself can be 32-bit, as the range is in a reasonable range. so after
// offsetting, we need to make sure there are no high bits, then we can just look
// at the lower 32 bits
auto temp = Builder::addVar(function, i64);
auto* block = builder.makeBlock();
block->list.push_back(builder.makeSetLocal(temp, offsetor));
// if high bits, we can break to the default (we'll fill in the name later)
breakWhenNotMatching = builder.makeBreak(Name(), nullptr,
builder.makeUnary(
UnaryOp::WrapInt64,
builder.makeBinary(BinaryOp::ShrUInt64,
builder.makeGetLocal(temp, i64),
builder.makeConst(Literal(int64_t(32)))
)
)
);
block->list.push_back(breakWhenNotMatching);
block->list.push_back(
builder.makeGetLocal(temp, i64)
);
block->finalize();
br->condition = builder.makeUnary(UnaryOp::WrapInt64, block);
}

top->list.push_back(br);
Expand Down Expand Up @@ -2595,6 +2622,9 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
if (br->default_.isNull()) {
br->default_ = top->name;
}
if (breakWhenNotMatching) {
breakWhenNotMatching->name = br->default_;
}
for (size_t i = 0; i < br->targets.size(); i++) {
if (br->targets[i].isNull()) br->targets[i] = br->default_;
}
Expand Down
25 changes: 25 additions & 0 deletions test/wasm-only.asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function asm(global, env, buffer) {

var _fabsf = env._fabsf;
var do_i64 = env.do_i64;
var abort = env.abort;

function loads() {
var i = 0, f = fround(0), d = +0;
Expand Down Expand Up @@ -416,6 +417,28 @@ function asm(global, env, buffer) {
return ($cond|0);
}

function switch64_big_condition1($x) {
$x = i64($x);
switch (i64($x)) {
case i64_const(0,2146435072): {
abort();
break;
}
default: {
return;
}
}
}
function switch64_big_condition2($x) {
$x = i64($x);
switch (i64($x)) {
case i64_const(0,2146435072): {
abort();
break;
}
}
}

function keepAlive() {
loads();
stores();
Expand All @@ -431,6 +454,8 @@ function asm(global, env, buffer) {
unreachable_leftovers(0, 0, 0);
_memchr(0, 0, 0) | 0;
switch64TOOMUCH(i64(0)) | 0;
switch64_big_condition1(i64(0));
switch64_big_condition2(i64(0));
}

function __emscripten_dceable_type_decls() { // dce-able, but this defines the type of fabsf which has no other use
Expand Down
52 changes: 48 additions & 4 deletions test/wasm-only.fromasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(type $FUNCSIG$vdji (func (param f64 i64 i32)))
(type $FUNCSIG$j (func (result i64)))
(type $FUNCSIG$ff (func (param f32) (result f32)))
(type $FUNCSIG$v (func))
(type $legaltype$illegalImport (func (param f64 i32 i32 i32)))
(type $legaltype$illegalImportResult (func (result i32)))
(type $legaltype$_fabsf (func (param f64) (result f64)))
Expand All @@ -10,6 +11,7 @@
(import "env" "illegalImportResult" (func $illegalImportResult (result i64)))
(import "env" "_fabsf" (func $_fabsf (param f32) (result f32)))
(import "env" "do_i64" (func $do_i64 (result i64)))
(import "env" "abort" (func $abort))
(import "env" "illegalImport" (func $legalimport$illegalImport (param f64 i32 i32 i32)))
(import "env" "illegalImportResult" (func $legalimport$illegalImportResult (result i32)))
(import "env" "_fabsf" (func $legalimport$_fabsf (param f64) (result f64)))
Expand Down Expand Up @@ -510,14 +512,24 @@
(block $switch-default
(block $switch-case0
(block $switch-case
(br_table $switch-case0 $switch-default $switch-case $switch-default
(br_if $switch-default
(i32.wrap/i64
(i64.sub
(get_local $0)
(i64.const 42949672965)
(i64.shr_u
(tee_local $0
(i64.sub
(get_local $0)
(i64.const 42949672965)
)
)
(i64.const 32)
)
)
)
(br_table $switch-case0 $switch-default $switch-case $switch-default
(i32.wrap/i64
(get_local $0)
)
)
)
(br $switch
(i32.const 11000)
Expand Down Expand Up @@ -863,6 +875,32 @@
(get_local $0)
)
)
(func $switch64_big_condition1 (param $0 i64)
(local $1 i64)
(block $switch-default
(block $switch-case
(br_if $switch-default
(i32.wrap/i64
(i64.shr_u
(tee_local $1
(i64.sub
(get_local $0)
(i64.const 9218868437227405312)
)
)
(i64.const 32)
)
)
)
(br_table $switch-case $switch-default
(i32.wrap/i64
(get_local $1)
)
)
)
(call $abort)
)
)
(func $keepAlive
(call $loads)
(call $stores)
Expand Down Expand Up @@ -920,6 +958,12 @@
(i64.const 0)
)
)
(call $switch64_big_condition1
(i64.const 0)
)
(call $switch64_big_condition1
(i64.const 0)
)
)
(func $legalstub$illegalParam (param $0 i32) (param $1 i32) (param $2 i32) (param $3 f64)
(call $illegalParam
Expand Down
52 changes: 48 additions & 4 deletions test/wasm-only.fromasm.clamp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(type $FUNCSIG$vdji (func (param f64 i64 i32)))
(type $FUNCSIG$j (func (result i64)))
(type $FUNCSIG$ff (func (param f32) (result f32)))
(type $FUNCSIG$v (func))
(type $legaltype$illegalImport (func (param f64 i32 i32 i32)))
(type $legaltype$illegalImportResult (func (result i32)))
(type $legaltype$_fabsf (func (param f64) (result f64)))
Expand All @@ -10,6 +11,7 @@
(import "env" "illegalImportResult" (func $illegalImportResult (result i64)))
(import "env" "_fabsf" (func $_fabsf (param f32) (result f32)))
(import "env" "do_i64" (func $do_i64 (result i64)))
(import "env" "abort" (func $abort))
(import "env" "illegalImport" (func $legalimport$illegalImport (param f64 i32 i32 i32)))
(import "env" "illegalImportResult" (func $legalimport$illegalImportResult (result i32)))
(import "env" "_fabsf" (func $legalimport$_fabsf (param f64) (result f64)))
Expand Down Expand Up @@ -510,14 +512,24 @@
(block $switch-default
(block $switch-case0
(block $switch-case
(br_table $switch-case0 $switch-default $switch-case $switch-default
(br_if $switch-default
(i32.wrap/i64
(i64.sub
(get_local $0)
(i64.const 42949672965)
(i64.shr_u
(tee_local $0
(i64.sub
(get_local $0)
(i64.const 42949672965)
)
)
(i64.const 32)
)
)
)
(br_table $switch-case0 $switch-default $switch-case $switch-default
(i32.wrap/i64
(get_local $0)
)
)
)
(br $switch
(i32.const 11000)
Expand Down Expand Up @@ -863,6 +875,32 @@
(get_local $0)
)
)
(func $switch64_big_condition1 (param $0 i64)
(local $1 i64)
(block $switch-default
(block $switch-case
(br_if $switch-default
(i32.wrap/i64
(i64.shr_u
(tee_local $1
(i64.sub
(get_local $0)
(i64.const 9218868437227405312)
)
)
(i64.const 32)
)
)
)
(br_table $switch-case $switch-default
(i32.wrap/i64
(get_local $1)
)
)
)
(call $abort)
)
)
(func $keepAlive
(call $loads)
(call $stores)
Expand Down Expand Up @@ -920,6 +958,12 @@
(i64.const 0)
)
)
(call $switch64_big_condition1
(i64.const 0)
)
(call $switch64_big_condition1
(i64.const 0)
)
)
(func $legalstub$illegalParam (param $0 i32) (param $1 i32) (param $2 i32) (param $3 f64)
(call $illegalParam
Expand Down
Loading