summaryrefslogtreecommitdiff
path: root/src
Commit message (Collapse)AuthorAgeFilesLines
...
* [NFC] Move ModuleUtils copying and renaming logic from header to cpp (#5855)Alon Zakai2023-08-0211-188/+227
| | | | | | | | None of that code is speed-sensitive, or at least doesn't need to be inlined to be fast. Move it to cpp for faster compile times. This caused a cascade of necessary header fixes (i.e. after removing unneeded header inclusions in module-utils.h, files that improperly depended on that stopped working and needed an added include).
* [NFC] Remove some unneeded cruft from PossibleContents::intersect() (#5854)Alon Zakai2023-08-021-25/+4
| | | | | | | | In order of appearance in this diff: * Use heapType rather than otherHeapType when either will do, for brevity. * We checked isNull after checking for isLiteral (line 173), but every null is a literal. * Calling setNoneOrNull simplifies some repetitive code. * We checked isLiteral yet again lower down.
* Fix a fuzz bug in TypeMapper (#5851)Thomas Lively2023-08-025-18/+77
| | | | | | | | | | | | | | | | | | TypeMapper is a utility used to globally rewrite types, mapping some eliminated source types into destination types they should be replaced with. This was previously done by first rewriting all the types in the IR according to the given mapping, then rewriting the type definitions and updating all the types in the IR again. Not only was doing the rewriting twice inefficient, it also introduced a subtle bug where the set of private types eligible to be rewritten could be inconsistent because updating types in the IR could change the types of control flow structures. The fuzzer found a case where this inconsistency caused the type rebuilding to fail. Fix the bug by first building the new types with the mapping applied and only then rewriting the IR a single time. Also add a `TypeBuilder::dump` utility for use in debugging. Fixes #5845.
* GUFA: Infer using TrapsNeverHappen (#5850)Alon Zakai2023-08-023-13/+556
| | | | | | | | | | | | | | | | | | | | | | | | This adds a TrapsNeverHappen oracle that is used inside the main PossibleContents oracle of GUFA. The idea is that when traps never happen we can reason "backwards" from information to things that must be true before it: temp = x.field; x.cast_to<Y>(); // Y is a subtype of x's type X Here we cast x to a subtype. If we assume traps never happen then the cast must succeed, and that means we can assume we had a Y on the previous line, where perhaps that information lets us infer the value of x.field. This PR focuses on calls, which are the more interesting situation to optimize because other passes do some work already inside functions. Specifically, we look for things that will trap in the called function or the caller, such as if the called function always casts a param to some type, we can assume the caller passes such a type in. And if we have a call_ref then any target that would trap cannot be called (at least in a closed world). This has some benefits, in particular when combined with --gufa-cast-all since that casts more things, which lets us apply the inferences made here. I see 3.3% fewer call_ref instructions on a Kotlin testcase, for example. This helps more on -Os when we inline less.
* [Wasm GC] Stop printing deprecated cast etc. instructions (#5852)Thomas Lively2023-08-021-60/+0
| | | | | | | | Stop printing `ref.as_i31`, `br_on_func`, etc. because they have been removed from the spec and are no longer supported by V8. #5614 already made this change for the binary format. Like that PR, leave reading unmodified in case someone is still using these instructions (even though they are useless). They will be fully removed in a future PR as we finalize things ahead of standardizing WasmGC.
* PossibleContents: Support more intersection types (#5847)Alon Zakai2023-07-312-29/+68
|
* Fix binary writing of strings without GC enabled (#5836)Alon Zakai2023-07-311-4/+10
|
* Convert lattice compare function to non-static (#5848)Bruce He2023-07-281-4/+5
| | | | | Changes the static asserts checking a lattice type to require a non-static compare function instead of a static one. Also changes the compare functions of existing lattices to be non-static.
* GUFA: Add a version that casts all of our inferences (#5846)Alon Zakai2023-07-273-12/+84
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | GUFA refines existing casts, but does not add new casts for fear of increasing code size and adding more cast operations at runtime. This PR adds a version that does add all those casts, and it looks like at least code size improves rather than regresses, at least on J2Wasm and Kotlin. That is, this pass adds a lot more casts, but subsequent optimizations benefit enough to shrink overall code size. However, this may still not be worthwhile, as even if code size decreases we may end up doing more casts at runtime, and those casts might be hard to remove, e.g.: (call $foo (x) ;; inferred to be non-null ) (func $foo (param (ref null $A) => (call $foo (ref.cast $A (x) ;; add a cast here ) (func $foo (param (ref $A) ;; later pass refines here That new cast cannot be removed after we refine the function parameter. If the function never benefits from the fact that the input is non-null, then the cast is wasted work (e.g. if the function only compares the input to another value). To use this new pass, try --gufa-cast-all rather than --gufa. As with normal GUFA, running the full optimizer afterwards is important, and even more important in order to get rid of as many of the new casts as possible.
* Fix a crash in TypeRefining on bottom types (#5842)Alon Zakai2023-07-271-2/+7
| | | Followup to #5840
* CFGWalker: Allow users to ignore branches outside the function [NFC] (#5838)Alon Zakai2023-07-264-7/+38
| | | | | | | | | A pass that just operates on locals, for example, does not care about branches outside of the function. That means that when we see a call, then even if EH is enabled we don't need to create a new basic block right after it (unless the call is inside a try-catch - then it might branch to the catch, of course). This makes CFG-using passes 9% faster compared to before this PR. This plus #5827 offset the slowdown from #5823 and overall give an improvement compared to before.
* Add a Fuzzer for Lattice and Transfer Function Properties (#5831)Bruce He2023-07-263-0/+522
| | | | | | | | | | | | | | This change adds a fuzzer which checks the following properties in abstract interpretation static analyses. - Transfer Function Monotonicity - Lattice Element Reflexivity - Lattice Element Transitivity - Lattice Element Anti-Symmetry This is done by randomly generating a module and using its functions as transfer function inputs, along with randomly generated lattice elements (states). Lattice element properties are fuzzed from the randomly generated states also.
* TypeRefining: Add casts when we must (#5840)Alon Zakai2023-07-262-1/+66
| | | | | | | | | See the example in the code and test for a situation that requires this for validation. To fix validation we add a cast. That should practically always be removed by later optimizations, and the fact it took the fuzzer this long to even find such a situation also adds confidence that this won't be adding overhead (and in this situation, the optimizer will definitely remove the cast).
* SmallVector iteration improvements (#5825)Alon Zakai2023-07-261-1/+26
|
* End the current basic block on a Call (#5823)Alon Zakai2023-07-2611-21/+33
| | | | | | | | | | | | | Before this PR, if a call had no paths to a catch in the same function then we skipped creating a new basic block right after it. As a result, we could have a call in the middle of a basic block. If EH is enabled that means we might transfer control flow out of the function from the middle of a block. But it is better to have the property that any transfer of control flow - to another basic block, or outside of the function - can only happen at the end of a basic block. This causes some overhead, but a subsequent PR (#5838) will remove that as a followup, and this PR adds a little code to pass the module and check if EH is enabled, and avoid the overhead if not, which at least avoids regressing the non-EH case until that followup lands.
* Remove incorrect usages of the term "setses" (#5841)Bruce He2023-07-261-20/+20
| | | This change applies to the Reaching Definitions Analysis.
* wasm-merge: Fix locals in merged start (#5837)Alon Zakai2023-07-261-12/+14
| | | | | | | | | Start functions can have locals, which we previously ignored as we just concatenated the bodies together. This makes us copy the second start and call that, keeping them separate (the optimizer can then inline, if that makes sense). Fixes #5835
* [Outlining] Changing stringify values to 32-bit (#5832)Ashley Nelson2023-07-212-6/+10
| | | | | The LLVM suffix tree expects to be provided with a vector of 32-bit unsigned integers. This PR makes it easier to integrate our wasm program string with the suffix tree. Because the range of possible values is reduced from 2^64 to 2^32, a signed integer was added to manage the next separator value and ensure we're using every possible negative number.
* SimplifyLocals: Refinalize after removing redundant tees (#5830)Alon Zakai2023-07-211-0/+3
|
* C API: Add BinaryenAddFunctionWithHeapType which takes a heap type (#5829)Alon Zakai2023-07-212-9/+37
| | | | | | | | | | | This is necessary for WasmGC producers using the C API, so that they can set the heap type of functions. Otherwise the heap type is set structurally using params and results in the old API. The old API is kept for backwards compatibility and convenience (for the structural case, which is all code before WasmGC basically). Fixes #5826
* Add support for debug printing of functions (#5828)Alon Zakai2023-07-202-0/+9
|
* [NFC] Avoid using ControlFlowWalker in CFGWalker (#5827)Alon Zakai2023-07-201-10/+9
| | | | | | ControlFlowWalker builds a stack of control flow items and allows finding the target of a branch using it. But the only use CFGWalker made of that was to map a block or a loop to its branches. We can just use the name of the block or loop in the map, and avoid the extra overhead.
* Static Analysis: Add an API to get the block index of an expression (#5822)Alon Zakai2023-07-202-0/+32
|
* Reaching Definitions Analysis for LocalGraph (#5817)Bruce He2023-07-198-67/+362
| | | | | | | | | | | This change implements a reaching definitions analysis which is intended to be equivalent to the information provided by LocalGraph, specifically the Flower class of LocalGraph. It also introduces a CRTP utility in visitor-transfer-function.h which implements most commonly found visitor-type transfer function functionalities. The MonotoneCFGAnalyzer is also modified to add a phase to collect results after the analysis is solved from the final CFG states.
* [NFC] Allow running multiple analyses in ParallelFunctionAnalysis (#5824)Alon Zakai2023-07-191-1/+10
|
* MemoryPacking: memory.init fixes for 64 bit (#5809)Arthur Islamov2023-07-181-12/+15
| | | | | | Fixes emscripten-core/emscripten#17485 This allows emscripten to complie code with MEMORY64 + PTHREADS by fixing using the proper pointer type in the MemoryPacking pass.
* [Outlining] LLVM Suffix Tree (#5821)Ashley Nelson2023-07-185-1/+754
| | | | | This PR adds LLVM's suffix tree data structure to Binaryen. This suffix tree is implemented using Ukkonen's algorithm for linear-time suffix tree construction, and is intended for fast substring queries. Note: All of the .h and .cpp files included are from LLVM. These files were copied directly instead of imported into our existing LLVM integration (in third_party/) to avoid bumping the commit hash and avoid the potential for complications with upstream changes.
* wasm-merge: Error on import loops (#5820)Alon Zakai2023-07-171-6/+10
|
* [wasm-merge] Handle chains of import/export (#5813)Jérôme Vouillon2023-07-171-1/+16
| | | | | | | When a module item is imported and directly reexported by an intermediate module, we need to perform several name lookups and use its name in the initial module rather than the intermediate name when fusing imports and exports.
* Adding license header to new files (#5819)Ashley Nelson2023-07-133-0/+48
|
* [Outlining] HashStringifyWalker (#5810)Ashley Nelson2023-07-133-1/+97
| | | | | This PR is part of the effort to bring Outlining to Binaryen. HashStringifyWalker is a subclass of StringifyWalker #5772, and used to encode a wasm module as a "string". Our "string" is a vector and each symbol is a uint64_t, providing us with a capacity of 2^64 symbols.
* Small fixes to lattice and analyzer (#5815)Bruce He2023-07-132-9/+9
| | | Updates comments. Moves wasm-traversal.h to transfer function classes.
* Add a pass to sort functions by name (#5811)Alon Zakai2023-07-123-0/+22
|
* Fuzzer: Emit more variations of If (#5806)Alon Zakai2023-07-101-2/+24
| | | | | | Before we always created if-elses. Now we also create an If with one arm some of the time, when we can. Also, sometimes make one if arm unreachable, if we have two arms.
* GUFA: Refine casts (#5805)Alon Zakai2023-07-072-1/+22
| | | | | | | If we see (ref.cast $A) but we have inferred that a more refined type will be present there at runtime $B then we can refine the cast to (ref.cast $B). We could do the same even when a cast is not present, but that would increase code size. This optimization keeps code size constant.
* Generalize Finite Powerset Lattice to Any Given Type (#5800)Bruce He2023-07-063-21/+72
| | | | | | | | | | This change creates a lattice, FinitePowersetLattice, to represent finite powerset lattices constructed from sets containing members of arbitrary type The bitvector finite powerset lattice class is renamed FiniteIntPowersetLattice. The templated FinitePowersetLattice class contains a FiniteIntPowersetLattice object, and over that provides functionality to map lattice element members to bitvector indices. Methods are also provided by the lattice to add or remove members of the given type from lattice members as an abstraction over flipping bits in the bitvector.
* Make the Transfer Function a Generic, Interchangeable Type for the Monotone ↵Bruce He2023-07-064-136/+218
| | | | | | | Analyzer (#5794) This change makes the transfer function an object separate from the monotone analyzer. The transfer function class type is a generic template of the monotone analyzer, and an instance of the transfer function is instantiated and then passed into the monotone analyzer via its constructor. The API of the transfer function is enforced by a static assertion. This change also introduces LivenessTransferFunction as a transfer function for liveness analysis as an example.
* Initial support for `final` types (#5803)Thomas Lively2023-07-0611-64/+152
| | | | | | | | | | | | | | | | | | | | Implement support in the type system for final types, which are not allowed to have any subtypes. Final types are syntactically different from similar non-final types, so type canonicalization is made aware of finality. Similarly, TypeMerging and TypeSSA are updated to work correctly in the presence of final types as well. Implement binary and text parsing and emitting of final types. Use the standard text format to represent final types and interpret the non-standard "struct_subtype" and friends as non-final. This allows a graceful upgrade path for users currently using the non-standard text format, where they can update their code to use final types correctly at the point when they update to use the standard format. Once users have migrated to using the fully expanded standard text format, we can update update Binaryen's parsers to interpret the MVP shorthands as final types to match the spec without breaking those users. To make it safe for V8 to independently start interpreting types declared without `sub` as final, also reserve that shorthand encoding only for types that have no strict subtypes.
* [NFC] Fix the use of "strict" in subtypes.h (#5804)Thomas Lively2023-07-069-24/+24
| | | | | | | | Previously we incorrectly used "strict" to mean the immediate subtypes of a type, when in fact a strict subtype of a type is any subtype excluding the type itself. Rename the incorrect `getStrictSubTypes` to `getImmediateSubTypes`, rename the redundant `getAllStrictSubTypes` to `getStrictSubTypes`, and rename the redundant `getAllSubTypes` to `getSubTypes`. Fixing the capitalization of "SubType" to "Subtype" is left as future work.
* [DeadArgumentElimination] Optimize function body after parameter refinement ↵Jérôme Vouillon2023-07-061-7/+11
| | | | | | (#5799) It can be useful to optimize a function body after its parameters are refined, like we do for other parameter changes.
* Print supertype declarations using the standard format (#5801)Thomas Lively2023-07-061-30/+14
| | | | | | Use the standard "(sub $super ...)" format instead of the non-standard "XXX_supertype ... $super" format. In a follow-on PR implementing final types, this will allow us to print and parse the standard text format for final types right away with a smaller diff.
* OptimizeInstructions: Loop on fallthrough values in RefTest (#5797)Alon Zakai2023-07-051-31/+43
| | | | | This parallels the code in RefCast. Previously we only looked at the type reaching us, but intermediate fallthrough values can let us optimize too. In particular, we were not optimizing (ref.test (local.tee ..)) if the tee was to a less-refined type.
* [Outlining] StringifyWalker (#5772)Ashley Nelson2023-06-302-0/+207
| | | StringifyWalker is a new Walker with UnifiedExpressionVisitor. This walker performs a shallow visit of control-flow (try, if, block, loop) expressions and their simple expression siblings before then visiting the children of each control-flow expression in postorder. As a result, this walker un-nests nested control flow structures, so the expression visit order does not correspond to a normal postorder traversal of the function.
* [NFC] Add a helper to get function DCE names in wasm-metadce (#5793)Alon Zakai2023-06-301-30/+15
|
* [wasm-metadce] Note ref.func connections + fix rooting of segment offsets ↵Jérôme Vouillon2023-06-291-13/+28
| | | | (#5791)
* Dynamic Sized Bitvector Powerset Lattice for Static Analysis (#5784)Bruce He2023-06-295-171/+234
| | | This change introduces a finite-height powerset lattice FinitePowersetLattice where each element's height is determined when constructed in runtime. This is implemented using a vector of `bools. This change also modifies BlockState and MonotoneCFGAnalyzer to be templated on a generic lattice type instead of using a hardcoded lattice. It additionally fixes an iterator bug in MonotoneCFGAnalyzer::fromCFG which assigned a temporary iterator object as predecessor and successor pointers to BlockStates instead of pointers to actual BlockState objects.
* Limit printing of Literal[s] in a general way (#5792)Alon Zakai2023-06-281-16/+49
| | | | | | | | Previously we limited printing in a single Literals. But we can have infinitely recursive GC literals, or just huge graphs even without infinite recursion where no single Literals is that big (but we still get exponential blowup). This PR adds a general limit on how much we print once we start to print a Literal or Literals.
* Fix opt/shrink levels when running the optimizer multiple times, Part 2 (#5787)Alon Zakai2023-06-273-41/+39
| | | | | | | | | | | This is a followup to #5333 . That fixed the selection of which passes to run, but forgot to also fix the global state of the current optimize/shrink levels. This PR fixes that. As a result, running -O3 -Oz will now work as expected: the first -O3 will run the right passes (as #5333 fixed) and while running them, the global optimize/shrinkLevels will be -O3 (and not -Oz), which this PR fixes. A specific result of this is that -O3 -Oz used to inline less, since the invocation of inlining during -O3 thought we were optimizing for size. The new test verifies that we do fully inline in the first -O3 now.
* Liveness Analysis Proof of Concept (#5771)Bruce He2023-06-237-0/+410
| | | This introduces a limited monotone flow-sensitive liveness analysis on local indices as an initial proof of concept for the creation of a monotone flow-sensitive static analysis framework. Tests are included in test/gtest/cfg.cpp.
* PostEmscripten: Preserve __em_js__ exports in side modules (#5780)Sam Clegg2023-06-231-4/+10
|