Skip to content

Commit cff4b75

Browse files
authored
wasm2js: remove unnecessary labels (#2108)
1 parent e54b1d3 commit cff4b75

6 files changed

Lines changed: 43 additions & 15 deletions

File tree

scripts/fuzz_opt.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
# parameters
2828

29-
NANS = False
29+
NANS = True
3030

3131
FEATURE_OPTS = [] # '--all-features' etc
3232

@@ -44,7 +44,7 @@
4444
'--experimental-wasm-return-call'
4545
]
4646

47-
INPUT_SIZE_LIMIT = 50 * 1024
47+
INPUT_SIZE_LIMIT = 150 * 1024
4848

4949
LOG_LIMIT = 125
5050

@@ -145,10 +145,7 @@ def run_bynterp(wasm):
145145

146146
def run_wasm2js(wasm):
147147
wrapper = run([in_bin('wasm-opt'), wasm, '--emit-js-wrapper=/dev/stdout'] + FEATURE_OPTS)
148-
cmd = [in_bin('wasm2js'), wasm, '--emscripten']
149-
if random.random() < 0.5:
150-
cmd += ['-O']
151-
main = run(cmd + FEATURE_OPTS)
148+
main = run([in_bin('wasm2js'), wasm, '--emscripten'] + FEATURE_OPTS)
152149
with open(os.path.join(options.binaryen_root, 'scripts', 'wasm2js.js')) as f:
153150
glue = f.read()
154151
with open('js.js', 'w') as f:
@@ -165,9 +162,9 @@ def run_wasm2js(wasm):
165162
def run_vms(prefix):
166163
wasm = prefix + 'wasm'
167164
results = []
168-
# results.append(run_bynterp(wasm))
169-
# results.append(fix_output(run_vm([os.path.expanduser('d8'), prefix + 'js'] + V8_OPTS + ['--', wasm])))
170-
results.append(run_wasm2js(wasm))
165+
results.append(run_bynterp(wasm))
166+
results.append(fix_output(run_vm([os.path.expanduser('d8'), prefix + 'js'] + V8_OPTS + ['--', wasm])))
167+
# results.append(run_wasm2js(wasm))
171168

172169
# append to add results from VMs
173170
# results += [fix_output(run_vm([os.path.expanduser('d8'), prefix + 'js'] + V8_OPTS + ['--', prefix + 'wasm']))]

src/tools/wasm2js.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ static void replaceInPlace(Ref target, Ref value) {
136136
}
137137
}
138138

139+
static void replaceInPlaceIfPossible(Ref target, Ref value) {
140+
if (target->isArray() && value->isArray()) {
141+
replaceInPlace(target, value);
142+
}
143+
}
144+
139145
static void optimizeJS(Ref ast) {
140146
// Helpers
141147

@@ -377,7 +383,8 @@ static void optimizeJS(Ref ast) {
377383
}
378384
});
379385

380-
// Remove unnecessary break/continue labels, when referring to the top level.
386+
// Remove unnecessary break/continue labels, when the name is that of the
387+
// highest target anyhow, which we would reach without the name.
381388

382389
std::vector<Ref> breakCapturers;
383390
std::vector<Ref> continueCapturers;
@@ -437,6 +444,30 @@ static void optimizeJS(Ref ast) {
437444
}
438445
}
439446
});
447+
448+
// Remove unnecessary block/loop labels.
449+
450+
std::set<IString> usedLabelNames;
451+
452+
traversePost(ast, [&](Ref node) {
453+
if (node->isArray() && !node->empty()) {
454+
if (node[0] == BREAK || node[0] == CONTINUE) {
455+
if (!node[1]->isNull()) {
456+
auto label = node[1]->getIString();
457+
usedLabelNames.insert(label);
458+
}
459+
} else if (node[0] == LABEL) {
460+
auto label = node[1]->getIString();
461+
if (usedLabelNames.count(label)) {
462+
// It's used; just erase it from the data structure.
463+
usedLabelNames.erase(label);
464+
} else {
465+
// It's not used - get rid of it.
466+
replaceInPlaceIfPossible(node, node[2]);
467+
}
468+
}
469+
}
470+
});
440471
}
441472

442473
static void emitWasm(Module& wasm,

test/wasm2js/br_table_to_loop.2asm.js.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function asmFunc(global, env, buffer) {
2222
var nan = global.NaN;
2323
var infinity = global.Infinity;
2424
function $0() {
25-
loop : while (1) continue;
25+
while (1) continue;
2626
}
2727

2828
function $1() {

test/wasm2js/i64-ctz.2asm.js.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function asmFunc(global, env, buffer) {
5757

5858
function __wasm_popcnt_i64($0, $1) {
5959
var $2 = 0, $3 = 0, $4 = 0, $5 = 0;
60-
label$2 : while (1) {
60+
while (1) {
6161
$5 = $3;
6262
$2 = $4;
6363
if ($0 | $1) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function asmFunc(global, env, buffer) {
3030
var $2 = 0, $3 = 0, $4 = 0;
3131
$2 = $1;
3232
$3 = 1;
33-
label$2 : while (1) {
33+
while (1) {
3434
if ($0_1 | $2) {
3535
$3 = _ZN17compiler_builtins3int3mul3Mul3mul17h070e9a1c69faec5bE($0_1, $2, $3, $4);
3636
$4 = i64toi32_i32$HIGH_BITS;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function asmFunc(global, env, buffer) {
9898

9999
function __wasm_popcnt_i32($0) {
100100
var $1_1 = 0, $2 = 0;
101-
label$2 : while (1) {
101+
while (1) {
102102
$2 = $1_1;
103103
if ($0) {
104104
$0 = $0 - 1 & $0;
@@ -112,7 +112,7 @@ function asmFunc(global, env, buffer) {
112112

113113
function __wasm_popcnt_i64($0, $1_1) {
114114
var $2 = 0, $3 = 0, $4 = 0, $5 = 0;
115-
label$2 : while (1) {
115+
while (1) {
116116
$5 = $3;
117117
$2 = $4;
118118
if ($0 | $1_1) {

0 commit comments

Comments
 (0)