Skip to content

Commit bcc6f29

Browse files
authored
Add float operations for isSymmetric util (#3127)
Add floating point Eq and Ne operators to Properties::isSymmetric. Also treat additional float ops as symmetric specifically in OptimizeInstructions when their operands are known to be non-NaN.
1 parent 1871606 commit bcc6f29

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

src/ir/properties.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ inline bool isSymmetric(Binary* binary) {
5151
case XorInt64:
5252
case EqInt64:
5353
case NeInt64:
54+
55+
case EqFloat32:
56+
case NeFloat32:
57+
case EqFloat64:
58+
case NeFloat64:
5459
return true;
5560

5661
default:

src/passes/OptimizeInstructions.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ struct OptimizeInstructions
202202
return nullptr;
203203
}
204204
if (auto* binary = curr->dynCast<Binary>()) {
205-
if (Properties::isSymmetric(binary)) {
205+
if (isSymmetric(binary)) {
206206
canonicalize(binary);
207207
}
208208
if (auto* ext = Properties::getAlmostSignExt(binary)) {
@@ -731,7 +731,7 @@ struct OptimizeInstructions
731731
// Canonicalizing the order of a symmetric binary helps us
732732
// write more concise pattern matching code elsewhere.
733733
void canonicalize(Binary* binary) {
734-
assert(Properties::isSymmetric(binary));
734+
assert(isSymmetric(binary));
735735
FeatureSet features = getModule()->features;
736736
auto swap = [&]() {
737737
assert(EffectAnalyzer::canReorder(
@@ -1591,6 +1591,29 @@ struct OptimizeInstructions
15911591
return InvalidBinary;
15921592
}
15931593
}
1594+
1595+
bool isSymmetric(Binary* binary) {
1596+
if (Properties::isSymmetric(binary)) {
1597+
return true;
1598+
}
1599+
switch (binary->op) {
1600+
case AddFloat32:
1601+
case MulFloat32:
1602+
case AddFloat64:
1603+
case MulFloat64: {
1604+
// If the LHS is known to be non-NaN, the operands can commute.
1605+
// We don't care about the RHS because right now we only know if
1606+
// an expression is non-NaN if it is constant, but if the RHS is
1607+
// constant, then this expression is already canonicalized.
1608+
if (auto* c = binary->left->dynCast<Const>()) {
1609+
return !c->value.isNaN();
1610+
}
1611+
return false;
1612+
}
1613+
default:
1614+
return false;
1615+
}
1616+
}
15941617
};
15951618

15961619
Pass* createOptimizeInstructionsPass() { return new OptimizeInstructions(); }

test/passes/O4_disable-bulk-memory.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,8 @@
968968
(local.get $0)
969969
)
970970
(f64.mul
971-
(f64.const 0.01)
972971
(local.get $4)
972+
(f64.const 0.01)
973973
)
974974
)
975975
)
@@ -980,8 +980,8 @@
980980
(local.get $0)
981981
)
982982
(f64.mul
983-
(f64.const 0.01)
984983
(local.get $5)
984+
(f64.const 0.01)
985985
)
986986
)
987987
)
@@ -992,8 +992,8 @@
992992
(local.get $0)
993993
)
994994
(f64.mul
995-
(f64.const 0.01)
996995
(local.get $6)
996+
(f64.const 0.01)
997997
)
998998
)
999999
)
@@ -1067,12 +1067,12 @@
10671067
(local.get $1)
10681068
(f64.mul
10691069
(f64.mul
1070-
(f64.const 0.5)
10711070
(local.tee $10
10721071
(f64.load offset=48
10731072
(local.get $0)
10741073
)
10751074
)
1075+
(f64.const 0.5)
10761076
)
10771077
(f64.add
10781078
(f64.add

test/wasm2js/conversions-modified.2asm.js.opt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,19 @@ function asmFunc(global, env, buffer) {
156156
}
157157

158158
function legalstub$12($0, $1) {
159-
return Math_fround(+($0 >>> 0) + 4294967296.0 * +($1 | 0));
159+
return Math_fround(+($0 >>> 0) + +($1 | 0) * 4294967296.0);
160160
}
161161

162162
function legalstub$14($0, $1) {
163-
return +($0 >>> 0) + 4294967296.0 * +($1 | 0);
163+
return +($0 >>> 0) + +($1 | 0) * 4294967296.0;
164164
}
165165

166166
function legalstub$16($0, $1) {
167-
return Math_fround(+($0 >>> 0) + 4294967296.0 * +($1 >>> 0));
167+
return Math_fround(+($0 >>> 0) + +($1 >>> 0) * 4294967296.0);
168168
}
169169

170170
function legalstub$18($0, $1) {
171-
return +($0 >>> 0) + 4294967296.0 * +($1 >>> 0);
171+
return +($0 >>> 0) + +($1 >>> 0) * 4294967296.0;
172172
}
173173

174174
function legalstub$22($0, $1) {

test/wasm2js/float-ops.2asm.js.opt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,19 @@ function asmFunc(global, env, buffer) {
255255
}
256256

257257
function legalstub$43($0, $1_1) {
258-
return Math_fround(+($0 >>> 0) + 4294967296.0 * +($1_1 | 0));
258+
return Math_fround(+($0 >>> 0) + +($1_1 | 0) * 4294967296.0);
259259
}
260260

261261
function legalstub$44($0, $1_1) {
262-
return +($0 >>> 0) + 4294967296.0 * +($1_1 | 0);
262+
return +($0 >>> 0) + +($1_1 | 0) * 4294967296.0;
263263
}
264264

265265
function legalstub$45($0, $1_1) {
266-
return Math_fround(+($0 >>> 0) + 4294967296.0 * +($1_1 >>> 0));
266+
return Math_fround(+($0 >>> 0) + +($1_1 >>> 0) * 4294967296.0);
267267
}
268268

269269
function legalstub$46($0, $1_1) {
270-
return +($0 >>> 0) + 4294967296.0 * +($1_1 >>> 0);
270+
return +($0 >>> 0) + +($1_1 >>> 0) * 4294967296.0;
271271
}
272272

273273
var FUNCTION_TABLE = [];

0 commit comments

Comments
 (0)