Skip to content
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
15 changes: 6 additions & 9 deletions scripts/fuzz_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

# parameters

NANS = False
NANS = True

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

Expand All @@ -44,7 +44,7 @@
'--experimental-wasm-return-call'
]

INPUT_SIZE_LIMIT = 50 * 1024
INPUT_SIZE_LIMIT = 150 * 1024

LOG_LIMIT = 125

Expand Down Expand Up @@ -145,10 +145,7 @@ def run_bynterp(wasm):

def run_wasm2js(wasm):
wrapper = run([in_bin('wasm-opt'), wasm, '--emit-js-wrapper=/dev/stdout'] + FEATURE_OPTS)
cmd = [in_bin('wasm2js'), wasm, '--emscripten']
if random.random() < 0.5:
cmd += ['-O']
main = run(cmd + FEATURE_OPTS)
main = run([in_bin('wasm2js'), wasm, '--emscripten'] + FEATURE_OPTS)
with open(os.path.join(options.binaryen_root, 'scripts', 'wasm2js.js')) as f:
glue = f.read()
with open('js.js', 'w') as f:
Expand All @@ -165,9 +162,9 @@ def run_wasm2js(wasm):
def run_vms(prefix):
wasm = prefix + 'wasm'
results = []
# results.append(run_bynterp(wasm))
# results.append(fix_output(run_vm([os.path.expanduser('d8'), prefix + 'js'] + V8_OPTS + ['--', wasm])))
results.append(run_wasm2js(wasm))
results.append(run_bynterp(wasm))
results.append(fix_output(run_vm([os.path.expanduser('d8'), prefix + 'js'] + V8_OPTS + ['--', wasm])))
# results.append(run_wasm2js(wasm))

# append to add results from VMs
# results += [fix_output(run_vm([os.path.expanduser('d8'), prefix + 'js'] + V8_OPTS + ['--', prefix + 'wasm']))]
Expand Down
33 changes: 32 additions & 1 deletion src/tools/wasm2js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ static void replaceInPlace(Ref target, Ref value) {
}
}

static void replaceInPlaceIfPossible(Ref target, Ref value) {
if (target->isArray() && value->isArray()) {
replaceInPlace(target, value);
}
}

static void optimizeJS(Ref ast) {
// Helpers

Expand Down Expand Up @@ -377,7 +383,8 @@ static void optimizeJS(Ref ast) {
}
});

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

std::vector<Ref> breakCapturers;
std::vector<Ref> continueCapturers;
Expand Down Expand Up @@ -437,6 +444,30 @@ static void optimizeJS(Ref ast) {
}
}
});

// Remove unnecessary block/loop labels.

std::set<IString> usedLabelNames;

traversePost(ast, [&](Ref node) {
if (node->isArray() && !node->empty()) {
if (node[0] == BREAK || node[0] == CONTINUE) {
if (!node[1]->isNull()) {
auto label = node[1]->getIString();
usedLabelNames.insert(label);
}
} else if (node[0] == LABEL) {
auto label = node[1]->getIString();
if (usedLabelNames.count(label)) {
// It's used; just erase it from the data structure.
usedLabelNames.erase(label);
} else {
// It's not used - get rid of it.
replaceInPlaceIfPossible(node, node[2]);
}
}
}
});
}

static void emitWasm(Module& wasm,
Expand Down
2 changes: 1 addition & 1 deletion test/wasm2js/br_table_to_loop.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function asmFunc(global, env, buffer) {
var nan = global.NaN;
var infinity = global.Infinity;
function $0() {
loop : while (1) continue;
while (1) continue;
}

function $1() {
Expand Down
2 changes: 1 addition & 1 deletion test/wasm2js/i64-ctz.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function asmFunc(global, env, buffer) {

function __wasm_popcnt_i64($0, $1) {
var $2 = 0, $3 = 0, $4 = 0, $5 = 0;
label$2 : while (1) {
while (1) {
$5 = $3;
$2 = $4;
if ($0 | $1) {
Expand Down
2 changes: 1 addition & 1 deletion test/wasm2js/stack-modified.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function asmFunc(global, env, buffer) {
var $2 = 0, $3 = 0, $4 = 0;
$2 = $1;
$3 = 1;
label$2 : while (1) {
while (1) {
if ($0_1 | $2) {
$3 = _ZN17compiler_builtins3int3mul3Mul3mul17h070e9a1c69faec5bE($0_1, $2, $3, $4);
$4 = i64toi32_i32$HIGH_BITS;
Expand Down
4 changes: 2 additions & 2 deletions test/wasm2js/unary-ops.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function asmFunc(global, env, buffer) {

function __wasm_popcnt_i32($0) {
var $1_1 = 0, $2 = 0;
label$2 : while (1) {
while (1) {
$2 = $1_1;
if ($0) {
$0 = $0 - 1 & $0;
Expand All @@ -112,7 +112,7 @@ function asmFunc(global, env, buffer) {

function __wasm_popcnt_i64($0, $1_1) {
var $2 = 0, $3 = 0, $4 = 0, $5 = 0;
label$2 : while (1) {
while (1) {
$5 = $3;
$2 = $4;
if ($0 | $1_1) {
Expand Down