From f070e8c10a15a02735fbd9b88c4c569a8c786933 Mon Sep 17 00:00:00 2001 From: Thomas Lively <7121787+tlively@users.noreply.github.com> Date: Mon, 26 Aug 2019 12:40:38 -0700 Subject: Do not hoist truncation of wasm2js divisions (#2305) It is not valid to defer the truncation of divisions because accumulated non-integral results can produce different values when they are combined before truncation. This was causing a test failure in the Rust test suite. --- src/tools/wasm2js.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 2007a65d8..ebed16226 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -353,11 +353,13 @@ static void optimizeJS(Ref ast) { } else if (isUnary(node, L_NOT)) { node[2] = optimizeBoolean(node[2]); } - // Add/subtract can merge coercions up. + // Add/subtract can merge coercions up, except when a child is a division, + // which needs to be eagerly truncated to remove fractional results. else if (isBinary(node, PLUS) || isBinary(node, MINUS)) { auto left = node[2]; auto right = node[3]; - if (isOrZero(left) && isOrZero(right)) { + if (isOrZero(left) && isOrZero(right) && !isBinary(left[2], DIV) && + !isBinary(right[2], DIV)) { auto op = node[1]->getIString(); // Add a coercion on top. node[1]->setString(OR); -- cgit v1.2.3