summaryrefslogtreecommitdiff
path: root/test/wasm2js
Commit message (Collapse)AuthorAgeFilesLines
* [Wasm GC] SimplifyLocals: Switch local.get to use a more refined type when ↵Alon Zakai2022-11-015-53/+30
| | | | | | | | | | | | | | | possible (#5194) (local.set $refined (cast (local.get $plain))) .. .. (local.get $plain) .. ;; we can change this to read from $refined By using the more refined type we may be able to eliminate casts later. To do this, look at the fallthrough value (so we can look through a cast or a block value - this is the reason for the small wasm2js improvements in tests), and also extend the code that picks which local index to read to look at types (previously we just ignored any pairs of locals with different types).
* wasm2js: Support for flexible module import naming (#5114)Sam Clegg2022-10-05115-404/+324
| | | | | | | | The previous code was making emscripten-specific assumptions about imports basically all coming from the `env` module. I can't find a way to make this backwards compatible so may do a combined roll with the emscripten-side change: https://github.com/emscripten-core/emscripten/pull/17806
* Vacuum: Ignore effects at the entire function scope when possible (#5053)Alon Zakai2022-09-193-9/+0
| | | | | | | | | | | | | | | Recently we added logic to ignore effects that don't "escape" past the function call. That is, e.g. local.set only affects the current function scope, and once the call stack is unwound it no longer matters as an effect. This moves that logic to a shared place, and uses it in the core Vacuum logic. The new constructor in EffectAnalyzer receives a function and then scans it as a whole. This works just like e.g. scanning a Block as a whole (if we see a break in the block, that has an effect only inside it, and the Block + children doesn't have a branch effect). Various tests are updated so they don't optimize away trivially, by adding new return values for them.
* Effects: Clarify trap effect meaning, and consider infinite loops to trap ↵Alon Zakai2022-09-162-1/+6
| | | | | | | | | | | | | | | | | | | | | due to timeout (#5039) I think this simplifies the logic behind what we consider to trap. Before we had kind of a hack in visitLoop that now has a more clear reasoning behind it: we consider as trapping things that trap in all VMs all the time, or will eventually. So a single allocation doesn't trap, but an unbounded amount can, and an infinite loop is considered to trap as well (a timeout in a VM will be hit eventually, somehow). This means we cannot optimize way a trivial infinite loop with no effects in it, while (1) {} But we can optimize it out in trapsNeverHappen mode. In any event, such a loop is not a realistic situation; an infinite loop with some other effect in it, like a call to an import, will not be optimized out, of course. Also clarify some other things regarding traps and trapsNeverHappen following recent discussions in https://github.com/emscripten-core/emscripten/issues/17732 Specifically, TNH will never be allowed to remove calls to imports.
* wasm2js: Don't assume that `env.abort` can always be impored. (#5049)Sam Clegg2022-09-16115-532/+365
| | | | | | This import was being injected and then used to implement trapping. Rather than injecting an import that doesn't exist in the original module we instead use the existing mechanism to implement this as an internal helper.
* wasm2js: Have instantiate function take standard import object (#5018)Sam Clegg2022-09-14115-159/+306
| | | | | | | | | | | Previously we were assuming asmLibraryArg which is what emscripten passes as the `env` import object but using this method is more flexible and should allow wasm2js to work with import that are not all form a single object. The slight size increase here is just temporary until emscripten gets updated. See https://github.com/emscripten-core/emscripten/pull/17737
* Avoid adding new unneeded names to blocks in text roundtripping (#4943)Alon Zakai2022-08-229-2741/+2660
| | | | | | | | | | | | | | | | | | | | | | | Previously the wat parser would turn this input: (block (nop) ) into something like this: (block $block17 (nop) ) It just added a name all the time, in case the block is referred to by an index later even though it doesn't have a name. This PR makes us rountrip more precisely by not adding such names: if there was no name before, and there is no break by index, then do not add a name. In addition, this will be useful for non-nullable locals since whether a block has a name or not matters there. Like #4912, this makes us more regular in our usage of block names.
* [Wasm2JS] Fix lowering of i64.extendN_s instructions (#4321)taylor.fish2022-06-243-4/+218
|
* [wasm2js] Support exports of Globals (#4523)magic-akari2022-03-173-0/+80
| | | | | | Export an object with a `.value` property like the wasm JS API does in browsers, and implement them with a getter and setter. Fixes #4522
* Turn an assertion on not colliding with an internal name into an error (#4422)Alon Zakai2022-01-052-12/+12
| | | | | | Without this, the result in a build without assertions might be quite confusing. See #4410 Also make the internal names more obviously internal names.
* Allow fractional timeouts in wasm2js Atomics.wait. Followup to #4385 (#4387)Alon Zakai2021-12-142-2/+2
|
* Implement timeout argument in wasm2js_atomic_wait_i32 (#4385)Sam Clegg2021-12-112-4/+14
| | | | | | Also, fix bug where pointer was being used direcltly to index into Int32Array. I suppose this code had basically zero users until I tried to land this change in emscripten: https://github.com/emscripten-core/emscripten/pull/15742
* CoalesceLocals: Use ValueNumbering (#4355)Alon Zakai2021-11-241-12/+9
| | | | | | | | | | | | This removes the old hardcoded value numbering in that pass and makes it use the new code that was split into helper code. The immediate benefit of this is to make the code aware of identical constants: if two locals have the same constant then they do not interfere. Future improvements to numbering will also automatically help here. This changes some constants in existing tests so that they keep testing what they were testing before, and adds new tests for the new benefit here. This implements a proposed TODO from #4314
* wasm2js: Don't assume the existence of js assert function (#4357)Sam Clegg2021-11-242-2/+2
| | | | | | Its seems that with this emscripten change DCE is able to remove the `assert` JS runtime function making this call to assert fail with `ReferenceError: assert is not defined`.
* CoalesceLocals: Rewrite the algorithm to be linear and to ignore copies (#4314)Alon Zakai2021-11-103-35/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The old algorithm can be summarized as: In each basic block, start at the beginning. Each pair of live locals there might interfere with each other, as they might arrive from different entry blocks with different values. Afterwards, go through the block and find overlapping live ranges, and mark interferences there as well. This is non-linear because at the start of the block we do a double-loop over all pairs of live locals, which in general can be O(N^2) (N - number of locals). It also has the downside of ignoring copies: if two locals have overlapping live ranges but they must have identical values on those ranges, they do not actually interfere, for example x = 10; y = x; .. // live ranges overlap here foo(x, y); // live ranges end here. We can ignore this overlap since the copy shows they are identical there, but the pass did not take this into account. To some extent other passes can remove such copies (SimplifyLocals, MergeLocals, RedundantSetElimination), but in general this was a weak spot for the optimizer. I realized there is a solution to both these problems: In Wasm, given that we have a default value for all locals, if a local is live at the start of a block then it must be live at the end of all the blocks reaching it. That is so because the liveness will extend backwards all the way to some set of the local, possibly all the way to the zero-initialization at the start of the function, and it extends that way through all predecessor blocks. A consequence of this is that there are no interferences between locals that only occur during a merge: The live ranges include the predecessor blocks, and theirs, and so forth, until we reach a block where one of the locals is assigned a value different than the other. That is a necessary and sufficient condition for intererence, and therefore when processing a block we only need to look at its contents, and can ignore the merging of control flow, which allows us to be linear. More details on this and on the new algorithm in comments in the source, but the basic idea is that it simply goes through each block in a linear way, finding which values are assigned to each local (using a numbering of unique values), and noting which are live at each time. If two locals are live and one is assigned a value that is not the same as the value in the other, mark them as interfering. This is of substantial benefit to j2wasm output, I believe because it is common there to find local subexpression elimination opportunities after inlining, and each time we find one we add a local. If we inline different functions into the same target, we may end up with copied locals for each of them. (This was not noticed in the past because it is very rare on LLVM output, which has already had inlining and GVN etc. done.) There is a small benefit to LLVM output as well, though just a few percent at best. However, it is enough to be noticeable on some of the code size tests. This is also faster than the previous pass. It's normally not noticeable as this pass is not one of the slowest anyhow, but I found some real-world codebases where the pass becomes 50% faster. I have not found any case where it is slower than the old algorithm. Fuzzed over several days to be sure this is correct, and also verified on the emscripten test suite.
* [OptimizeInstructions] Canonicalize relational ops with near zero on rhs (#4272)Max Graey2021-10-263-5/+5
| | | | | | | | | | | | | Canonicalize: (signed)x > -1 ==> x >= 0 (signed)x <= -1 ==> x < 0 (signed)x < 1 ==> x <= 0 (signed)x >= 1 ==> x > 0 (unsigned)x < 1 ==> x == 0 (unsigned)x >= 1 ==> x != 0 This should help #4265, and in general 0 is usually a more common constant, and reasonable to canonicalize to.
* OptimizeInstructions: Optimize boolean selects (#4147)Alon Zakai2021-09-131-4/+4
| | | | | | | | | | | | If all a select's inputs are boolean, we can sometimes turn the select into an AND or an OR operation, x ? y : 0 => x & y x ? 1 : y => x | y I believe LLVM aggressively canonicalizes to this form. It makes sense to do here too as it is smaller (save the constant 0 or 1). It also allows further optimizations (which is why LLVM does it) but I don't think we have those yet.
* Enable LocalCSE by default (#4089)Alon Zakai2021-08-194-77/+84
| | | | | | | | | | | | Enable it in -O3 and -Os and higher. This helps very little on output from LLVM, but also it does not alter compile times much anyhow. On code that has not been run through an optimizing compiler already, this can help quite a lot, e.g., 15% of code size on some wasm GC samples. This will not normally help with speed, as optimizing VMs do such things anyhow. However, this can help baseline compilers and interpreters and so forth.
* [Optimize Instructions] Combine reinterprets, loads and stores (#4006)Max Graey2021-07-211-9/+1
| | | | | | | | | | | | | | | | | | | | | | | | Fixes #3973 Loads: f32.reinterpret_i32(i32.load(x)) => f32.load(x) f64.reinterpret_i64(i64.load(x)) => f64.load(x) i32.reinterpret_f32(f32.load(x)) => i32.load(x) i64.reinterpret_f64(f64.load(x)) => i64.load(x) Stores: f32.store(y, f32.reinterpret_i32(x)) => i32.store(y, x) f64.store(y, f64.reinterpret_i64(x)) => i64.store(y, x) i32.store(y, i32.reinterpret_f32(x)) => f32.store(y, x) i64.store(y, i64.reinterpret_f64(x)) => f64.store(y, x) Also optimize reinterprets that are undone: i32.reinterpret_f32(f32.reinterpret_i32(x)) => x i64.reinterpret_f64(f64.reinterpret_i64(x)) => x f32.reinterpret_i32(i32.reinterpret_f32(x)) => x f64.reinterpret_i64(i64.reinterpret_f64(x)) => x
* OptimizeInstructions: Move identical unary code out of if/select arms (#3828)Alon Zakai2021-04-212-4/+4
| | | | | | | | | | | | | | | | | | | | | (select (foo (X) ) (foo (Y) ) (condition) ) => (foo (select (X) (Y) (condition) ) ) To make this simpler, refactor optimizeTernary to be templated.
* Remove passive keyword from data segment parser (#3757)Abbas Mashayekh2021-03-301-2/+2
| | | | | | | | The passive keyword has been removed from spec's text format, and now any data segment that doesn't have an offset is considered as passive. This PR remove that from both parser and the Print pass, plus all tests that used that syntax. Fixes #2339
* Inlining: Always inline single-use functions (#3730)Alon Zakai2021-03-299-303/+213
| | | | | | | | | | | | | | | | This implements emscripten-core/emscripten#13744 Inlining functions with a single use allows us to remove the function afterward. That looks highly beneficial, shrinking every single benchmark in emscripten's benchmark suite, by an average of 2% on the macrobenchmarks and 3.5% on all of them. Speed also improves, although mostly on the microbenchmarks so that might be less realistic. There may be a slight downside to startup time due to emitting larger functions, but given the baseline compilers in VMs these days it seems worth it, as the delay would be just to get to the upper tier. On the benchmark suite the risk seems low. See more details in the PR above.
* Add a test for memory.size in wasm2js (#3547)Alon Zakai2021-02-043-2/+13
| | | Support has been there all along, but we didn't have a reference test of it.
* Rename atomic.notify and *.atomic.wait (#3353)Heejin Ahn2020-11-131-3/+3
| | | | | | | | | | | | | | - atomic.notify -> memory.atomic.notify - i32.atomic.wait -> memory.atomic.wait32 - i64.atomic.wait -> memory.atomic.wait64 See WebAssembly/threads#149. This renames instruction name printing but not the internal data structure names, such as `AtomicNotify`, which are not always the same as printed instruction names anyway. This also does not modify C API. But this fixes interface functions in binaryen.js because it seems binaryen.js's interface functions all follow the corresponding instruction names.
* wasm2js: Declare data segments before calling asmFunc (#3337)Sam Clegg2020-11-1129-380/+383
| | | | | | | | | This is because we maybe need to reference the segments during the start function. For example in the case of pthreads we conditionally load passive segments during start. Tested in emscripten with: tests/runner.py wasm2js1
* wasm2js: Support for exported memory (#3323)Sam Clegg2020-11-1029-206/+197
| | | | | The asmFunc now sets the outer scope's `bufferView` variable as well as its own internal views.
* [wasm2js] Use native JavaScript Math.trunc (#3329)Max Graey2020-11-10113-18/+148
|
* Canonicalize subtraction with constant on the right to addition (#3321)Max Graey2020-11-1014-20/+20
| | | | | | | Using addition in more places is better for gzip, and helps simplify the optimizer as well. Add a FinalOptimizer phase to do optimizations like our signed LEB tweaks, to reduce binary size in the rare case when we do want a subtraction.
* wasm2js: Remove global dict arguments to asmFunc (#3325)Sam Clegg2020-11-05113-4233/+2336
|
* MemoryPacking: Properly notice zeroFilledMemory (#3306)Alon Zakai2020-11-023-7/+3
| | | We can only pack memory if we know it is zero-filled before us.
* RemoveUnusedBrs: Properly check for effects in selectify() (#3310)Alon Zakai2020-11-013-33/+23
| | | | | Selectify turns an if-else into a select where possible. Previously we abandoned hope if any part of the if had a side effect. But it's fine for the condition to have a side effect, so long as moving it to the end doesn't invalidate the arms.
* Canonicalize relationals as well (#3303)Max Graey2020-10-306-14/+14
|
* Rewrite DCE pass (#3274)Alon Zakai2020-10-264-49/+69
| | | | | | | | | | | | | | | | | | | | | | | | | The DCE pass is one of the oldest in binaryen, and had quite a lot of cruft from the changes in unreachability and other stuff in wasm and binaryen's history. This PR rewrites it from scratch, making it about 1/3 the size. I noticed this when looking for places to use code autogeneration. The old version had annoying boilerplate, while the new one avoids any need for it. There may be noticeable differences, as the old pass did more than it needed to. It overlapped with remove-unused-names for some reason I don't remember. The new pass leaves that to the other pass to do. I added another run of remove-unused-names to avoid noticeable differences in optimized builds, but you can see differences in the testcases that only run DCE by itself. (The test differences in this PR are mostly whitespace.) (The overlap is that if a block ended up not needed, that is, all branches to it were removed, the old DCE would remove the block.) This pass is about 15% faster than the old version. However, when adding another run of remove-unused-names the difference basically vanishes, so this isn't a speedup.
* Optimize relations of subtractions and zero (#3275)Max Graey2020-10-251-3/+3
|
* Optimize power of two float divisions (#3018)Max Graey2020-10-132-4/+4
|
* wasm2js: override incoming memory's grow method (#3185)Sam Clegg2020-09-3012-36/+46
| | | | | | | | | | This will allow for the completely removal of `__growWasmMemory` as a followup. We currently unconditionally generate this function in `generateMemoryGrowthFunction`. See #3180
* Remove unnecessary "new" on Table generation in wasm2js (#3163)Alon Zakai2020-09-302-2/+2
| | | It's not an actual constructor, just a JS function that returns the object.
* wasm2js: Skip heap creation in the absence of wasm memory. NFC (#3167)Sam Clegg2020-09-24107-1239/+2296
| | | | | Also, format the asmFunc call to make it more readable in the ES6 modules case.
* Remove stale test output (#3157)Sam Clegg2020-09-2113-10225/+0
| | | | | | | | | | | | These test output files are ignored and so contain stale output that is neither checked during `check.py` not updated during `auto_update_tests.py`. There are three clases to tests here: 1. Spec tests that end in 64.wast are ignored by scripts/test/wasm2js.py 2. Spec tests that are globallyi ignoed by shared.py:SPEC_TESTS_TO_SKIP 3. hello_world.2asm.js.. I cant tell where this came remove it seems like an anomaly.
* wasm2js: Support exported tables (#3152)Sam Clegg2020-09-21110-158/+48
|
* Add float operations for isSymmetric util (#3127)Max Graey2020-09-142-8/+8
| | | 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.
* Fix wasm2js memory import in case it is minified (#3113)Alon Zakai2020-09-103-0/+134
| | | | | | | | | | | | | | | It was hardcoded as "env.memory", which is usually correct. But if we minify import names, as in -O3 in emscripten, we need to use the minified name. Note how in the test it now emits var memory = env.a; for the import. Fixes emscripten-core/emscripten#12123 This was not noticed earlier since that import is only used in memory growth. The tests that would catch it are wasm2js3.test*memory_growth* but we only run wasm2js1 on CI. I'll add testing after this lands.
* wasm2js: Add an "Export" scope for name resolution (#2998)Alon Zakai2020-07-302-34/+34
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously we used "Top" for both exports and the top level (which has functions and globals). The warning about name collisions there was meant only for exports (where if a name collides and so it must be renamed, means that there will be an externally-visible oddness for the user). But it applied to functions too, which could be annoying, and was not dangerous (at worst, it might be confusing when reading the emitted JS and seeing NAME_1, NAME_2, but there is no effect on execution or on exports). To fix this, add a new Export name scope. This separates function names from export names. However, it runs into another issue which is that when checking for a name conflict we had a big set of all the names in all the scopes. That is, FOO would only ever be used in one scope, period, and other appearances of that Name in wasm would get a suffix. As a result, if an exported function FOO has the name foo, we'd export it as FOO but name the function FOO_1 which is annoying. To fix that, keep sets of all names in each scope. When mangling a name we can then only care about the relevant scope, EXCEPT for local names, which must also not conflict with function names. That is, this would be bad: function foo(bar) { var bar = 0; } function bar() { .. It's not ok to call a parameter "bar" if there is a function by that name (well, it could be if it isn't called in that scope). So when mangling the Local scope, also check the Top one as well. The test output changes are due to non-overlapping scopes, specifically Local and Label. It's fine to have foo : while(1) { var foo = 5; } Those "foo"s do not conflict. Fixes emscripten-core/emscripten#11743
* wasm2js: Remove an incorrect optimization (#3004)Alon Zakai2020-07-291-2/+2
| | | | optimizeBoolean does not receive a boolean, it is done when the output flows into a boolean context.
* wasm2js: Don't remove an | 0 or >>> 0 in a boolean context (#2993)Alon Zakai2020-07-282-8/+8
| | | | | | | | | | | | | It is usually fine to do if (x | 0) => if (x) since it just cares if the value is 0 or not. However, if the cast turns it into 0, then that is incorrect, which the fuzzer found as -2147483648 + -2147483648 | 0 (the sum is 2^32, which | 0 is 0). We can maybe look into doing this in a safe way, but for now just remove it. It doesn't have a big impact on code size as this is pretty rare (e.g. the minimal runtime code size test is not broken by this).
* wasm2js: coerce function pointer indexes (#2979)Alon Zakai2020-07-229-14/+122
| | | | | | | | | | | | | | | | | We emit FUNCTION_TABLE[ptr], where FUNCTION_TABLE is a JS array. That is a rare case where true is handled differently than 1 (a typed array or an add would cast, etc.), so we must explicitly cast there. Fixes an issue that existed before, but became a problem due to #2869 which optimized some selects into a form that emitted a true or a false, and if that was a function pointer, it could be bad, see https://app.circleci.com/pipelines/github/emscripten-core/emscripten/6699/workflows/0c4da49c-75d0-4b0a-8fac-686a8330a3fe/jobs/336520 The new test/wasm2js/indirect-select.2asm.js.opt output shows what happened there. Verified as passing emscripten's wasm2js1 wasm2js2 test suites.
* Optimize select with const arms (#2869)Max Graey2020-07-222-53/+21
| | | | | x ? 1 : 0 => !!x and so forth.
* wasm2js: Fix a bug with adjacent reinterprets (#2964)Alon Zakai2020-07-2018-80/+220
| | | | | | | | | | | i64 reinterprets were lowered in the i64 pass, and i32s at the very end, in wasm2js itself. This could break since in between the i64 pass and wasm2js we run optimizations, and the optimizer was not aware of what we lower the i32 reinterprets to - calls to use scratch memory. Those calls have a side effect of altering scratch memory. The optimizer just saw an i32 reinterpret, and moved it across the i64 reinterpret's scratch memory calls. This makes 32-bit reinterprets use separate scratch memory from 64-bit ones, which means they can never interfere with each other.
* wasm2js: Sign-extend support (#2949)Alon Zakai2020-07-103-0/+94
| | | | | | The usual "trick" to extend: shift left so the sign bit in the small integer is now the sign bit in a 32-bit integer, then shift right to spread that sign bit out and return the lower bits to their proper place, (x << 24) >> 24.
* Wasm2js Atomics support (#2924)Alon Zakai2020-06-233-0/+339
| | | | | Atomic loads, stores, RMW, cmpXchg, wait, and notify. This is enough to get the asm.js atomics tests in the emscripten test suite to pass, at least (but they are a subset of the entire pthreads suite).