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

wasm2js: Stop emitting nan and infinity #5391

Merged
merged 1 commit into from
Jan 4, 2023
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
20 changes: 6 additions & 14 deletions src/emscripten-optimizer/simple_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -919,25 +919,17 @@ struct JSPrinter {
void printName(Ref node) { emit(node->getCString()); }

static char* numToString(double d, bool finalize = true) {
// If this number is NaN or infinite then things are a bit tricky. In JS we
// want to eventually use `NaN` and/or `Infinity`, but neither of those
// identifiers are valid in asm.js. Instead we have to explicitly import
// `NaN` and `Infinity` from the global environment, and those names are
// bound locally in an asm function as `nan` and `infinity`.
//
// TODO: the JS names of `NaN` and `Infinity` should be used once literal
// asm.js code isn't generated any more
if (std::isnan(d)) {
if (std::signbit(d)) {
return (char*)"-nan";
return (char*)"-NaN";
} else {
return (char*)"nan";
return (char*)"NaN";
}
} else if (!std::isfinite(d)) {
if (std::signbit(d)) {
return (char*)"-infinity";
return (char*)"-Infinity";
} else {
return (char*)"infinity";
return (char*)"Infinity";
}
}
bool neg = d < 0;
Expand Down Expand Up @@ -1193,10 +1185,10 @@ struct JSPrinter {
ensure(1); // we temporarily append a 0
char* curr = buffer + last; // ensure might invalidate
buffer[used] = 0;
if (strstr(curr, "infinity")) {
if (strstr(curr, "Infinity")) {
return;
}
if (strstr(curr, "nan")) {
if (strstr(curr, "NaN")) {
return;
}
if (strchr(curr, '.')) {
Expand Down
7 changes: 0 additions & 7 deletions src/tools/wasm2js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,13 +788,6 @@ void AssertionEmitter::fixCalls(Ref asmjs, Name asmModule) {
}

void AssertionEmitter::emit() {
// TODO: nan and infinity shouldn't be needed once literal asm.js code isn't
// generated
out << R"(
var nan = NaN;
var infinity = Infinity;
)";

// When equating floating point values in spec tests we want to use bitwise
// equality like wasm does. Unfortunately though NaN makes this tricky. JS
// implementations like Spidermonkey and JSC will canonicalize NaN loads from
Expand Down
9 changes: 0 additions & 9 deletions src/wasm2js.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,6 @@ void Wasm2JSBuilder::addBasics(Ref ast, Module* wasm) {
addMath(MATH_CEIL, CEIL);
addMath(MATH_TRUNC, TRUNC);
addMath(MATH_SQRT, SQRT);
// TODO: this shouldn't be needed once we stop generating literal asm.js code
// NaN and Infinity variables
Ref nanVar = ValueBuilder::makeVar();
ast->push_back(nanVar);
ValueBuilder::appendToVar(nanVar, "nan", ValueBuilder::makeName("NaN"));
Ref infinityVar = ValueBuilder::makeVar();
ast->push_back(infinityVar);
ValueBuilder::appendToVar(
infinityVar, "infinity", ValueBuilder::makeName("Infinity"));
}

static bool needsQuoting(Name name) {
Expand Down
2 changes: 0 additions & 2 deletions test/binaryen.js/emit_asmjs.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function main($0) {
$0 = $0 | 0;
return $0 | 0;
Expand Down
4 changes: 0 additions & 4 deletions test/wasm2asm.asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ function asmFunc(global, env, buffer) {
var Math_ceil = global.Math.ceil;
var Math_sqrt = global.Math.sqrt;
var abort = env.abort;
var nan = global.NaN;
var infinity = global.Infinity;
var i64toi32_i32$HIGH_BITS = 0;
function $0() {

Expand Down Expand Up @@ -54,8 +52,6 @@ function asmFunc(global, env, buffer) {
var HEAP32 = new Int32Array(__array_buffer);
var HEAPF32 = new Float32Array(__array_buffer);
var HEAPF64 = new Float64Array(__array_buffer);
var nan = NaN;
var infinity = Infinity;
;

function f32Equal(a, b) {
Expand Down
4 changes: 0 additions & 4 deletions test/wasm2asm.traps.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ function asmFunc(global, env, buffer) {
var Math_ceil = global.Math.ceil;
var Math_sqrt = global.Math.sqrt;
var abort = env.abort;
var nan = global.NaN;
var infinity = global.Infinity;
var i64toi32_i32$HIGH_BITS = 0;
function $0() {

Expand Down Expand Up @@ -54,8 +52,6 @@ function asmFunc(global, env, buffer) {
var HEAP32 = new Int32Array(__array_buffer);
var HEAPF32 = new Float32Array(__array_buffer);
var HEAPF64 = new Float64Array(__array_buffer);
var nan = NaN;
var infinity = Infinity;
;

function f32Equal(a, b) {
Expand Down
5 changes: 0 additions & 5 deletions test/wasm2js.asserts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

var nan = NaN;
var infinity = Infinity;

function f32Equal(a, b) {
var i = new Int32Array(1);
var f = new Float32Array(i.buffer);
Expand Down Expand Up @@ -41,8 +38,6 @@ function asmFunc0(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {

}
Expand Down
5 changes: 0 additions & 5 deletions test/wasm2js.traps.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

var nan = NaN;
var infinity = Infinity;

function f32Equal(a, b) {
var i = new Int32Array(1);
var f = new Float32Array(i.buffer);
Expand Down Expand Up @@ -41,8 +38,6 @@ function asmFunc0(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {

}
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/add_div.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function foo($0) {
$0 = $0 | 0;
return (($0 >>> 0) / (100 >>> 0) | 0) + (($0 | 0) / (-100 | 0) | 0) | 0 | 0;
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/add_div.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function foo($0) {
$0 = $0 | 0;
return (($0 | 0) / -100 | 0) + (($0 >>> 0) / 100 | 0) | 0;
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/atomic_fence.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {

}
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/atomic_fence.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {

}
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/atomics_32.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {
var i64toi32_i32$0 = 0, i64toi32_i32$2 = 0, i64toi32_i32$1 = 0;
Atomics.compareExchange(HEAP8, 1024, 1, 2) | 0;
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/atomics_32.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {
Atomics.compareExchange(HEAP8, 1024, 1, 2) | 0;
Atomics.compareExchange(HEAP16, 512, 1, 2) | 0;
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/base64.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
return {

};
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/base64.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
return {

};
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
var env = imports.env;
var setTempRet0 = env.setTempRet0;
var i64toi32_i32$HIGH_BITS = 0;
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br_table.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
var env = imports.env;
var setTempRet0 = env.setTempRet0;
var i64toi32_i32$HIGH_BITS = 0;
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br_table_hoisting.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function zed($0) {
$0 = $0 | 0;
zed($0 | 0);
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br_table_hoisting.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function zed($0) {
zed($0);
}
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br_table_temp.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function dummy() {

}
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br_table_temp.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function dummy() {

}
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br_table_to_loop.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {
block : {
loop : while (1) switch (1 | 0) {
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/br_table_to_loop.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {
wasm2js_trap();
}
Expand Down
2 changes: 0 additions & 2 deletions test/wasm2js/break-drop.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {

}
Expand Down
10 changes: 0 additions & 10 deletions test/wasm2js/bulk-memory.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
return {

};
Expand Down Expand Up @@ -49,8 +47,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0($0_1, $1_1, $2) {
$0_1 = $0_1 | 0;
$1_1 = $1_1 | 0;
Expand Down Expand Up @@ -151,8 +147,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0($0_1, $1_1, $2) {
$0_1 = $0_1 | 0;
$1_1 = $1_1 | 0;
Expand Down Expand Up @@ -231,8 +225,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0($0_1, $1_1, $2) {
$0_1 = $0_1 | 0;
$1_1 = $1_1 | 0;
Expand Down Expand Up @@ -340,8 +332,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {
wasm2js_data_drop(0);
}
Expand Down
10 changes: 0 additions & 10 deletions test/wasm2js/bulk-memory.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
return {

};
Expand Down Expand Up @@ -49,8 +47,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0($0_1, $1_1, $2) {
$0_1 = $0_1 | 0;
$1_1 = $1_1 | 0;
Expand Down Expand Up @@ -151,8 +147,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0($0_1, $1_1, $2) {
$0_1 = $0_1 | 0;
$1_1 = $1_1 | 0;
Expand Down Expand Up @@ -231,8 +225,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0($0_1, $1_1, $2) {
$0_1 = $0_1 | 0;
$1_1 = $1_1 | 0;
Expand Down Expand Up @@ -306,8 +298,6 @@ function asmFunc(imports) {
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var nan = NaN;
var infinity = Infinity;
function $0() {

}
Expand Down
Loading