summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/asm2wasm.h5
-rw-r--r--test/unit.asm.js11
2 files changed, 15 insertions, 1 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index 52598ad0b..321d5275e 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -1014,7 +1014,10 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
}
// global var
assert(mappedGlobals.find(name) != mappedGlobals.end());
- return builder.makeSetGlobal(name, process(ast[3]));
+ auto* ret = builder.makeSetGlobal(name, process(ast[3]));
+ // set_global does not return; if our value is trivially not used, don't emit a load (if nontrivially not used, opts get it later)
+ if (astStackHelper.getParent()[0] == STAT) return ret;
+ return builder.makeSequence(ret, builder.makeGetGlobal(name, ret->value->type));
} else if (ast[2][0] == SUB) {
Ref target = ast[2];
assert(target[1][0] == NAME);
diff --git a/test/unit.asm.js b/test/unit.asm.js
index d6d0a27e8..cf2060030 100644
--- a/test/unit.asm.js
+++ b/test/unit.asm.js
@@ -281,6 +281,17 @@ function asm(global, env, buffer) {
return phi() | 0;
}
+ function useSetGlobal() {
+ var x = 0;
+ x = (Int = 10);
+ Int = 20;
+ return (Int = 30) | 0;
+ }
+
+ function usesSetGlobal2() {
+ return (Int = 40, 50) | 0;
+ }
+
function z() {
}
function w() {