Skip to content

Commit 4acd476

Browse files
authored
wasm-reduce: Improve tryToReduceCurrentToConst() (#6193)
Avoid replacing with the exact same thing in the case of RefNull and a default tuple. Also be more careful with handling of numbers. Before we exited immediately if we saw a number, but we can try to replace a number with a 0 or a 1, even if it was a number before. That is, we consider 1 simpler than e.g. 12345678, and 0 simpler than 1.
1 parent 5e3f81c commit 4acd476

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

src/tools/wasm-reduce.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "ir/iteration.h"
3232
#include "ir/literal-utils.h"
3333
#include "ir/properties.h"
34+
#include "ir/utils.h"
3435
#include "pass.h"
3536
#include "support/colors.h"
3637
#include "support/command-line.h"
@@ -42,6 +43,7 @@
4243
#include "wasm-builder.h"
4344
#include "wasm-io.h"
4445
#include "wasm-validator.h"
46+
4547
#ifdef _WIN32
4648
#ifndef NOMINMAX
4749
#define NOMINMAX
@@ -71,6 +73,7 @@ std::string GetLastErrorStdStr() {
7173
return std::string();
7274
}
7375
#endif
76+
7477
using namespace wasm;
7578

7679
// A timeout on every execution of the command.
@@ -1139,33 +1142,47 @@ struct Reducer
11391142
return false;
11401143
}
11411144

1142-
// try to replace a concrete value with a trivial constant
1145+
// Try to replace a concrete value with a trivial constant.
11431146
bool tryToReduceCurrentToConst() {
11441147
auto* curr = getCurrent();
1145-
if (curr->is<Const>()) {
1146-
return false;
1147-
}
1148-
// try to replace with a trivial value
1149-
if (curr->type.isNullable()) {
1148+
1149+
// References.
1150+
if (curr->type.isNullable() && !curr->is<RefNull>()) {
11501151
RefNull* n = builder->makeRefNull(curr->type.getHeapType());
11511152
return tryToReplaceCurrent(n);
11521153
}
1154+
1155+
// Tuples.
11531156
if (curr->type.isTuple() && curr->type.isDefaultable()) {
11541157
Expression* n =
11551158
builder->makeConstantExpression(Literal::makeZeros(curr->type));
1159+
if (ExpressionAnalyzer::equal(n, curr)) {
1160+
return false;
1161+
}
11561162
return tryToReplaceCurrent(n);
11571163
}
1164+
1165+
// Numbers. We try to replace them with a 0 or a 1.
11581166
if (!curr->type.isNumber()) {
11591167
return false;
11601168
}
1161-
// It's a number: try to replace it with a 0 or a 1 (trying more values
1162-
// could make sense too, but these handle most cases).
1169+
auto* existing = curr->dynCast<Const>();
1170+
if (existing && existing->value.isZero()) {
1171+
// It's already a zero.
1172+
return false;
1173+
}
11631174
auto* c = builder->makeConst(Literal::makeZero(curr->type));
11641175
if (tryToReplaceCurrent(c)) {
11651176
return true;
11661177
}
1178+
// It's not a zero, and can't be replaced with a zero. Try to make it a one,
1179+
// if it isn't already.
1180+
if (existing &&
1181+
existing->value == Literal::makeFromInt32(1, existing->type)) {
1182+
// It's already a one.
1183+
return false;
1184+
}
11671185
c->value = Literal::makeOne(curr->type);
1168-
c->type = curr->type;
11691186
return tryToReplaceCurrent(c);
11701187
}
11711188

0 commit comments

Comments
 (0)