summaryrefslogtreecommitdiff
path: root/src
Commit message (Collapse)AuthorAgeFilesLines
...
* Add space between options in --help text (#3940)Thomas Lively2021-06-171-0/+1
| | | | | Improve the legibility of the option documentation by adding vertical space between options. This is particularly helpful to delimit the text of options with longer explanations.
* [wasm2js] Refactor assertion parsing (#3938)Thomas Lively2021-06-171-19/+31
| | | | | | | | | | | | | | | Assertions were previously parsed by replacing "invoke" with "call" and using the normal s-expr parser. The parseCall method of the s-expr parser uses the call target to look up the correct signature on the module, but the invoke targets in assertions use export names rather than internal function names, so the signature lookups were inserting new bogus entries with default values. This issue didn't seem to cause any big problems before, but #3935 turns it into a hard error because the default `HeapType` does not have an associated signature. Fix the problem (at least in the common case of trivial arguments and expected results) by manually construction a `Call` expression rather than depending on the s-expr parser to construct it.
* [Wasm GC] rtt.fresh_sub (#3936)Alon Zakai2021-06-1713-11/+72
| | | | | | | | | | This is the same as rtt.sub, but creates a "new" rtt each time. See https://docs.google.com/document/d/1DklC3qVuOdLHSXB5UXghM_syCh-4cMinQ50ICiXnK3Q/edit# The old Literal implementation of rtts becomes a little more complex here, as it was designed for the original spec where only structure matters. It may be worth a complete redesign there, but for now as the spec is in flux I think the approach here is good enough.
* [Wasm GC] Add experimental support for non-nullable locals (#3932)Alon Zakai2021-06-154-7/+33
| | | | | | | | | | | | | | This adds a new feature flag, GCNNLocals that enables support for non-nullable locals. No validation is applied to check that they are actually assigned before their use yet - this just allows experimentation to begin. This feature is not enabled by default even with -all. If we enabled it, then it would take effect in most of our tests and likely confuse current users as well. Instead, the flag must be opted in explicitly using --enable-gc-nn-locals. That is, this is an experimental feature flag, and as such must be explicitly enabled. (Once the spec stabilizes, we will remove the feature anyhow when we implement the final status of non-nullability. )
* Parsing and emitting nominal types (#3933)Thomas Lively2021-06-158-16/+85
| | | | | | | Adds a `--nominal` option to switch the type machinery from equirecursive to nominal. Implements binary and text parsing and emitting of nominal types using new type constructor opcodes and an `(extends $super)` text syntax extension. When not in nominal mode, these extensions will still be parsed but will not have any effect and will not be used when emitting.
* [wasm-split] Add an option to emit a placeholder map (#3931)Thomas Lively2021-06-123-5/+37
| | | | | | The new instruction emits a file containing a map between placeholder index and the name of the split out function that placeholder is replacing in the table. This map is intended to be useful for debugging, as discussed in https://github.com/emscripten-core/emscripten/issues/14330.
* Nominal subtyping (#3927)Thomas Lively2021-06-112-75/+241
| | | | | | | | | | | | | Add methods to the TypeBuilder interface to declare subtyping relationships between the built types. These relationships are validated and recorded globally as part of type building. If the relationships are not valid, a fatal error is produced. In the future, it would be better to report the error to the TypeBuilder client code, but this behavior is sufficient for now. Also updates SubTyper and TypeBounder to be aware of nominal mode so that subtyping and LUBs are correctly calculated. Tests of the failing behavior will be added in a future PR that exposes this functionality to the command line, since the current `example` testing infrastructure cannot handle testing fatal errors.
* [EH] Allow catch/delegate-less trys (#3924)Heejin Ahn2021-06-103-11/+7
| | | | This removes the restriction that `try` should have at least one `catch`/`catch_all`/`delegate`. See WebAssembly/exception-handling#157.
* Store signatures as HeapTypes when parsing binaries (#3929)Thomas Lively2021-06-102-24/+39
| | | | | | | When parsing func.ref instructions, we need to get the HeapType corresponding to the referenced function's signature. Since constructing HeapTypes from Signatures can be expensive under equirecursive typing, keep track of the original function signature HeapTypes directly during parsing rather than storing them as Signatures.
* [Wasm GC] Support struct.new in global initializers (#3930)Alon Zakai2021-06-091-1/+2
|
* [Wasm GC] Properly validate BrOn* (#3928)Alon Zakai2021-06-081-1/+1
| | | | | | The noteBreak call was in the wrong place, causing us to not note breaks from BrOnNull for example, which could make validation miss errors. Noticed in #3926
* Initial nominal typing support (#3919)Thomas Lively2021-06-082-40/+173
| | | | | | | | | | | | | | | | | | | | In nominal mode, HeapType constructors besides the Signature constructor always produce fresh types distinct from any previously created types. The HeapType constructor that takes a Signature maintains its previous behavior of constructing a canonical representative of the given signature because it is used frequently throughout the code base and never in a situation that would benefit from creating a fresh type. It is left as future work to clean up this discrepancy between the Signature HeapType constructor and other HeapType constructors. TypeBuilder skips shape and global canonicalization in nominal mode and always creates a fresh type for each of its entries. For this to work without any canonicalization, the TypeBuilder allocates temporary types on the global Type store and does not support building basic HeapTypes in nominal mode. The new mode is not available in any of the Binaryen tools yet because it is still missing critical functionality like the ability to declare subtyping relations and correctly calculate LUBs. This functionality will be implemented in future PRs.
* [OptimizeInstructions] Handle post-MVP sign extended operations (#3910)Max Graey2021-06-032-3/+37
| | | fixes part of #3906
* [wasm-split] Add a merge-profiles mode (#3917)Thomas Lively2021-06-021-63/+173
| | | | | | | Given a list of profiles for the same module, --merge-profiles produces a single combined profile the contains the minimum timestamp among the original profiles for each function. When verbose output is enabled, also emit a message for each profile that could individually be removed without affecting the set of functions in the combined profile, as suggested in #3912.
* [Wasm GC] Add negated BrOn* operations (#3913)Alon Zakai2021-06-0214-49/+228
| | | | | | They are basically the flip versions. The only interesting part in the impl is that their returned typed and sent types are different. Spec: https://docs.google.com/document/d/1DklC3qVuOdLHSXB5UXghM_syCh-4cMinQ50ICiXnK3Q/edit
* [wasm-split] Make option validation declarative (#3916)Thomas Lively2021-06-011-88/+166
| | | | | | In anticipation of adding a third wasm-split mode, merge-profiles, in addition to the existing split and instrument modes, refactor wasm-split's option validation to let the valid modes be declared for each option. This approach is more scalable and robust than the ad-hoc validation we had previously.
* [wasm-split] Minimize names of newly created exports (#3905)Thomas Lively2021-06-013-2/+15
| | | | | | | | | wasm-split would previously use internal function names to create the external names of the functions that are newly exported from the primary module to be imported into the secondary module. When the input module contains full function names (as is commonly the case when emitting symbol maps), this caused the function names to be preserved as the export names, even when names are otherwise being stripped. To save on code size and properly anonymize functions, generate minimal export names when debuginfo is disabled instead.
* [NFC] Factor out and simplify minified name generation (#3909)Thomas Lively2021-05-274-90/+89
| | | | Simplifies the public API to not unnecessarily take an index and simplifies the implementation to use a single integer as state rather than a vector of indices.
* [Wasm GC] Add experimental array.copy (#3911)Alon Zakai2021-05-2720-7/+218
| | | | | | | | Spec for it is here: https://docs.google.com/document/d/1DklC3qVuOdLHSXB5UXghM_syCh-4cMinQ50ICiXnK3Q/edit# Also reorder some things in wasm.h that were not in the canonical order (that has no effect, but it is confusing to read).
* [Wasm GC] Implement CFGWalker support for BrOn* (#3908)Alon Zakai2021-05-261-40/+17
| | | | | | | | | | | | | Without adding logic there, it simply ignored the branch, which could lead to bad optimizations (thinking code is unreachable when it was). There isn't a trivial way to add a static error to force us to add new classes to CFGWalker. But this PR generalizes the code there to handle all branches and all unreachable instructions in a generic way. The only thing we'll need to remember to do in the future is to add control flow structures. (And normally the fuzzer should quickly find such bugs, but we don't have full fuzzing enabled for GC yet.) Fixes #3907
* [wasm-split] Add an option to emit only the module names (#3901)Thomas Lively2021-05-256-21/+87
| | | | | | Even when other names are stripped, it can be useful for wasm-split to preserve the module name so that the split modules can be differentiated in stack traces. Adding this option to wasm-split requires adding similar options to ModuleWriter and WasmBinaryWriter.
* Add SIMDLoadStoreLane get/setters to C/JS API (#3904)Daniel Wirtz2021-05-253-0/+173
|
* [EH] Change Walker::TaskFunc back to function pointer (#3899)Heejin Ahn2021-05-202-14/+16
| | | | | | | | | | `Walker::TaskFunc` has changed from a function pointer to `std::function` in #3494, mainly to make the EH support for `CFGWalker` easier. We didn't notice much performance difference then, but it was recently reported that it creased binaryen.js code size and performance. This changes `Walker::TaskFunc` back to a function pointer and does a little more work to manage catch index in `CFGWalker` side. Hopefully fixes #3857.
* Emit imported functions first in symbol maps (#3900)Thomas Lively2021-05-201-2/+5
| | | | | | | | Imported functions come first when modules are emitted, so to ensure the function indices are correct, they need to come first in the symbol maps. We never noticed this bug before because imported functions are always the first functions when a module is parsed, so the bug never mattered in practice. However, wasm-split adds new imported functions after parsing and these were causing the symbol map indices to be incorrect.
* [Wasm GC] Validate struct.get/set heap types early in text parsing (#3897)Alon Zakai2021-05-201-0/+6
| | | | We must do that before assuming the type is a heap type in getStructIndex, or we'd hit an assert there.
* Fix usage comment for ExtractFunction (#3896)Alon Zakai2021-05-191-1/+1
| | | Fixes #3895
* [wasm-split] Add a --symbolmap option (#3894)Thomas Lively2021-05-191-0/+24
| | | | | The new option emits a symbol map file for each of the split modules. The file names are created by appending ".symbols" to each of the Wasm output file names.
* Switch from Hopcroft's to Valmari and Lehtinen's DFA minimization (#3883)Thomas Lively2021-05-181-204/+415
| | | | | | | | | | | Valmari and Lehtinen's algorithm is broadly similar to Hopcroft's algorithm, but it more precisely keeps track of which input transitions might be able to split a partition of states so it ends up doing much less work. Unlike our implementation of Hopcroft's algorithm, which naively used sets of HeapTypes, this new algorithm also uses optimized data structures that can split partitions in constant time and never reallocate. This change improves the shape canonicalization time for a real-world unoptimized type section from 40 minutes to 1.5 seconds.
* wasm-reduce: Always decrease the factor (#3849)Alon Zakai2021-05-181-3/+9
| | | | | | When things go well, the reducer shrinks the factor by 50% or more, but when things are slow it kept the factor unchanged. That is annoying in some cases where you really have no benefit from reduction until the factor gets small. So this at least reduces it by 10% in each iteration.
* Remove Type ordering (#3793)Thomas Lively2021-05-1812-238/+76
| | | | | | | | | As found in #3682, the current implementation of type ordering is not correct, and although the immediate issue would be easy to fix, I don't think the current intended comparison algorithm is correct in the first place. Rather than try to switch to using a correct algorithm (which I am not sure I know how to implement, although I have an idea) this PR removes Type ordering entirely. In places that used Type ordering with std::set or std::map because they require deterministic iteration order, this PR uses InsertOrdered{Set,Map} instead.
* [Wasm GC] Heap2Local: Replace the allocation with null (#3893)Alon Zakai2021-05-171-24/+54
| | | | | | | | | | | | | | | | Previously we would try to stop using the allocation as much as possible, for example not writing it to locals any more, and leaving it to other passes to actually remove it (and remove gets of those locals etc.). This seemed simpler and more modular, but does not actually work in some cases as the fuzzer has found. Specifically, if we stop writing our allocation to locals, then if we do a (ref.as_non_null (local.get ..)) of that, then we will trap on the null present in the local. Instead, this changes our rewriting to do slightly more work, but it is simpler in the end. We replace the allocation with a null, and replace all the places that use it accordingly, for example, updating types to be nullable, and removing RefAsNonNulls, etc. This literally gets rid of the allocation and all the places it flows to (leaving less for other passes to do later).
* [Wasm GC] Fix printing of unreachable Array operations (#3892)Alon Zakai2021-05-171-0/+22
| | | | Similar to struct operations, if the reference is unreachable then we do not know the heap type, and cannot print the full expression.
* Add namespace and include guard to insert_ordered.h (#3891)Thomas Lively2021-05-172-3/+12
|
* Do not attempt to preserve DWARF if a previous pass removes it (#3887)Alon Zakai2021-05-173-6/+42
| | | | | | | | | | | If we run a pass that removes DWARF followed by one that could destroy it, then there is no possible problem - there is nothing left to destroy. We can run the later pass with no issues (and no warnings). Also add an assertion on running a pass runner only once. That has always been the assumption, and now that we track whether the added passes remove debug info, we need to check it. Fixes emscripten-core/emscripten#14161
* [NFC] Move InsertOrdered{Set,Map} into a new header (#3888)Thomas Lively2021-05-172-113/+137
| | | | | | Move the InsertOrderedSet and InsertOrderedMap implementations out of Relooper.h and into a new insert_ordered.h so they can be used more widely. Only changes the implementation code to use unordered_maps and `WASM_UNREACHABLE` instead of `abort`.
* Remove unused argument major (#3889)Paulo Matos2021-05-171-4/+2
| | | | Affects `printMajor` and `printMedium`. There is no usage of this optional argument in the source code.
* Disable colors when writing module as text (#3890)Paulo Matos2021-05-171-0/+4
| | | Via the C API.
* Support --symbolmap and --symbolmap=FOO in wasm-opt (#3885)Alon Zakai2021-05-144-8/+22
| | | | | | | | | | wasm-as supports --symbolmap=FOO as an argument. We got a request to support the same in wasm-opt. wasm-opt does have --print-function-map which does the same, but as a pass. To unify them, use the new pass arg sugar from #3882 which allows us to add a --symbolmap pass whose argument can be set as --symbolmap=FOO. That perfectly matches the wasm-as notation. For now, keep the old --print-function-map notation as well, to not break emscripten. After we remove it there we can remove it here.
* Add pass argument sugar to commandline (#3882)Alon Zakai2021-05-134-15/+40
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We have --pass-arg that allows sending an argument to a pass, like this: wasm-opt --do-stuff --pass-arg=do-stuff@FUNCTION_NAME With this PR that is equivalent to this: wasm-opt --do-stuff=FUNCTION_NAME That is,one can just give an argument to a pass on the commandline. This fixes the Optional mode in command-line.h/cpp. That was not actually used anywhere before this PR. Also rename --extract-function's pass argument to match it. That is, the usage used to be wasm-opt --extract-function --pass-arg=extract@FUNCTION_NAME Note how the pass name differed from the pass-arg name. This changes it to match. This is a breaking change, but I doubt this is used enough to justify any deprecation / backwards compatibility effort, and any usage is almost certainly manual, and with PR writing it manually becomes easier as one can do wasm-opt --extract-function=FUNCTION_NAME The existing test for that is kept (&renamed), and a new test added to test the new notation. This is a step towards unifying the symbol map functionality between wasm-as and wasm-opt (later PRs will turn the symbol mapping pass into a pass that receives an argument).
* RemoveUnusedModuleElements: The start function may be imported (#3884)Alon Zakai2021-05-131-1/+1
| | | | | Without this fix we can segfault, as it has no body. Fixes #3879
* [Wasm GC] Heap2Local: Handle branches (#3881)Alon Zakai2021-05-122-16/+62
| | | | | | | | | | | | | | | | | | If we branch to a block, and there are no other branches or a final value on the block either, then there is no mixing, and we may be able to optimize the allocation. Before this PR, all branches stopped us. To do this, add some helpers in BranchUtils. The main flow logic in Heap2Local used to stop when we reached a child for the second time. With branches, however, a child can flow both to its immediate parent, and to branch targets, and so the proper thing to look at is when we reach a parent for the second time (which would definitely indicate mixing). Tests are added for the new functionality. Note that some existing tests already covered some things we should not optimize, and so no tests were needed for them. The existing ones are: $get-through-block, $branch-to-block.
* Heap2Local: Use escape analysis to turn heap allocations into local data (#3866)Alon Zakai2021-05-125-1/+709
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If we allocate some GC data, and do not let the reference escape, then we can replace the allocation with locals, one local for each field in the allocation basically. This avoids the allocation, and also allows us to optimize the locals further. On the Dart DeltaBlue benchmark, this is a 24% speedup (making it faster than the JS version, incidentially), and also a 6% reduction in code size. The tests are not the best way to show what this does, as the pass assumes other passes will clean up after. Here is an example to clarify. First, in pseudocode: ref = new Int(42) do { ref.set(ref.get() + 1) } while (import(ref.get()) That is, we allocate an int on the heap and use it as a counter. Unnecessarily, as it could be a normal int on the stack. Wat: (module ;; A boxed integer: an entire struct just to hold an int. (type $boxed-int (struct (field (mut i32)))) (import "env" "import" (func $import (param i32) (result i32))) (func "example" (local $ref (ref null $boxed-int)) ;; Allocate a boxed integer of 42 and save the reference to it. (local.set $ref (struct.new_with_rtt $boxed-int (i32.const 42) (rtt.canon $boxed-int) ) ) ;; Increment the integer in a loop, looking for some condition. (loop $loop (struct.set $boxed-int 0 (local.get $ref) (i32.add (struct.get $boxed-int 0 (local.get $ref) ) (i32.const 1) ) ) (br_if $loop (call $import (struct.get $boxed-int 0 (local.get $ref) ) ) ) ) ) ) Before this pass, the optimizer could do essentially nothing with this. Even with this pass, running -O1 has no effect, as the pass is only used in -O2+. However, running --heap2local -O1 leads to this: (func $0 (local $0 i32) (local.set $0 (i32.const 42) ) (loop $loop (br_if $loop (call $import (local.tee $0 (i32.add (local.get $0) (i32.const 1) ) ) ) ) ) ) All the GC heap operations have been removed, and we just have a plain int now, allowing a bunch of other opts to run. That output is basically the optimal code, I think.
* Printing: Add a comment when we cannot emit something (#3878)Alon Zakai2021-05-111-0/+3
| | | | | | If we can't emit something, and instead emit a replacement for it (as is the case for a StructSet with an unreachable RTT, so we have no known heap type for it), add a comment that mentions it is a replacement. This might avoid confusion while debugging.
* ExtractFunction: Do not always remove the memory and table (#3877)Alon Zakai2021-05-112-17/+23
| | | | | | | | | Instead, run RemoveUnusedModuleElements, which does that sort of thing. That is, this pass just "extracts" the function by turning all others into imports, and then they should almost all be removable via RemoveUnusedModuleElements, depending on whether they are used in the table or not, whether the extracted function calls them, etc. Without this, we would error if a function was in the table, and so this fixes #3876
* [Wasm GC] Fix StructSet::finalize on an unreachable value (#3874)Alon Zakai2021-05-102-2/+29
| | | | | | Also fix printing of unreachable StructSets, which must handle the case of an unreachable reference, which means we do not know the RTT, and so we must print a replacement for the StructSet somehow. Emit a block with drops, fixing the old behavior which was missing the drops.
* [Wasm GC] Fix precomputing of incompatible fallthrough values (#3875)Alon Zakai2021-05-102-4/+21
| | | | | | | | | | | | | | | | | | | | | | Precompute not only computes values, but looks at the fallthrough, (local.set 0 (block ..stuff we can ignore.. ;; the fallthrough we care about - if a value is set to local 0, it is this (i32.const 10) ) ) Normally that is fine, but the fuzzer found a case where it is not: RefCast may return a different type than the fallthrough, even an incompatible type if we try to do something bad like cast a function to a struct. As we may then propagate the value to a place that expects the proper type, this can cause an error. To fix this, check if the precomputed value is a proper subtype. If it is not, then do not look through into the fallthrough, but compute the entire thing. (In the case of a bad RefCast of a func to a struct, it would then indicate a trap happens, and we would not precompute the value.)
* Implement all Builder::replaceWithIdenticalType() cases as best we can (#3872)Alon Zakai2021-05-101-6/+4
| | | | | | The method had TODOs which it halted on. But we should not halt the entire program, as this is a best-effort attempt to replace a node with something simpler of the same type (we call it when we know the value is not actually used).
* [Wasm GC] Fix casting code in interpreter (#3873)Alon Zakai2021-05-101-5/+9
| | | | | | | | | | The logic there would construct the cast value separately for functions and data (as we must), and then in an attempt to share code, would then check if the cast succeed or not (and if not, do nothing with the cast value). But this was wrong, as in some weird casts (like a struct to a function) we cannot construct a valid cast value, and we error there. Instead, check if the cast works first, once we know enough to do so, and only then construct the cast value if so.
* [Wasm GC] Fix Array initialization of a packed value (#3868)Alon Zakai2021-05-071-1/+2
| | | | | | We truncated and extended packed values in get and set, but not during initialization. Found by the fuzzer.
* Fix interpreting of a ref.cast of a function that is not on the module (#3863)Alon Zakai2021-05-061-3/+12
| | | | | | | | | | | | | | | | | | | | | Binaryen allows optimizing functions in function-parallel passes while the module is still being built, that is, while not all the other functions have even been added to the module yet. Since the removal of asm2wasm that has not been heavily tested, but the fuzzer found a closely related bug: in passes like inlining-optimizing, that inline and then optimize the functions we inlined into, the mechanism for optimizing only the relevant functions is to create a module with only some of them. (We only want to optimize the relevant ones, that we inlined into, because this happens after the main optimization pipeline - we don't want to re-optimize all the functions if we just inlined into one of them.) The specific bug here is that ref.cast of a funcref looked up the target function on the module (in order to get its signature, to see if the cast has the right RTT for it). The fix is to return a nonconstant flow in that case, as it is something we cannot precompute. (This does mean we may miss some optimization opportunities, but as in the case of where we optimize functions before the module is fully built up, we do still get 99% of function-local optimizations that way, and a subsequent round of full optimizations can be done later if necessary.)