summaryrefslogtreecommitdiff
path: root/src/passes/PostEmscripten.cpp
Commit message (Collapse)AuthorAgeFilesLines
* [NFC] Remove excessive debug logging from binary reading (#6927)Alon Zakai2024-09-101-0/+1
| | | | | | | | We were doing a debug logging for every LEB byte. It turns out that the isDebugEnabled() calls are expensive when called so frequently: in a release+assertion build, even with debug disabled, these checks are the highest thing in the profile. This PR removes the checks, which makes binary reading 12% faster.
* Allow different arguments for multiple instances of a pass (#6687)Christian Speckner2024-07-151-4/+2
| | | | | | | | | | | | Each pass instance can now store an argument for it, which can be different. This may be a breaking change for the corner case of running a pass multiple times and setting the pass's argument multiple times as well (before, the last pass argument affected them all; now, it affects the last instance only). This only affects arguments with the name of a pass; others remain global, as before (and multiple passes can read them, in fact). See the CHANGELOG for details. Fixes #6646
* Asyncify-verbose: Show all reasons why a function is instrumented (#6457)Dannii Willis2024-04-081-5/+5
| | | | Helps emscripten-core/emscripten#17380 by logging all the reasons why we instrument a function, and not just the first as we did before.
* [PostEmscripten] Fix calcSegmentOffsets for large offsets (#6260)Sam Clegg2024-01-311-4/+3
| | | | Specifically offsets larger than 2^32 which were being interpreted misinterpreted here as very large int64_t values.
* [Wasm64] Fix PostEmscripten::optimizeExceptions on invokes with an i64 ↵Alon Zakai2023-11-011-1/+1
| | | | | | | | argument (#6074) In wasm64, function pointers are 64-bit like all pointers. fixes #6073
* PostEmscripten: Preserve __em_js__ exports in side modules (#5780)Sam Clegg2023-06-231-4/+10
|
* Use Names instead of indices to identify segments (#5618)Thomas Lively2023-04-041-15/+13
| | | | | | | | | | All top-level Module elements are identified and referred to by Name, but for historical reasons element and data segments were referred to by index instead. Fix this inconsistency by using Names to refer to segments from expressions that use them. Also parse and print segment names like we do for other elements. The C API is partially converted to use names instead of indices, but there are still many functions that refer to data segments by index. Finishing the conversion can be done in the future once it becomes necessary.
* Add post-emscripten-side-module pass argument (#5274)Sam Clegg2022-11-181-3/+10
| | | | | | | In this mode we don't remove the start/stop_em_asm symbols or data. This is because with side modules we read this information directly from the wasm binaryen at runtime. See https://github.com/emscripten-core/emscripten/pull/18228
* Make `Name` a pointer, length pair (#5122)Thomas Lively2022-10-111-4/+2
| | | | | | | | | | | | | | | | | | | | | | | With the goal of supporting null characters (i.e. zero bytes) in strings. Rewrite the underlying interned `IString` to store a `std::string_view` rather than a `const char*`, reduce the number of map lookups necessary to intern a string, and present a more immutable interface. Most importantly, replace the `c_str()` method that returned a `const char*` with a `toString()` method that returns a `std::string`. This new method can correctly handle strings containing null characters. A `const char*` can still be had by calling `data()` on the `std::string_view`, although this usage should be discouraged. This change is NFC in spirit, although not in practice. It does not intend to support any particular new functionality, but it is probably now possible to use strings containing null characters in at least some cases. At least one parser bug is also incidentally fixed. Follow-on PRs will explicitly support and test strings containing nulls for particular use cases. The C API still uses `const char*` to represent strings. As strings containing nulls become better supported by the rest of Binaryen, this will no longer be sufficient. Updating the C and JS APIs to use pointer, length pairs is left as future work.
* [NFC] Remove `cashew::` namespace from IString (#5126)Thomas Lively2022-10-101-2/+4
| | | | As an NFC preliminary change that will minimize the diff in #5122, which moves IString to the wasm namespace.
* Strip em_js_deps exports (#5109)Sam Clegg2022-10-041-0/+8
| | | These are only needed for the metadata extraction in emcc.
* Refactor interaction between Pass and PassRunner (#5093)Thomas Lively2022-09-301-10/+14
| | | | | | | | | | | | | | Previously only WalkerPasses had access to the `getPassRunner` and `getPassOptions` methods. Move those methods to `Pass` so all passes can use them. As a result, the `PassRunner` passed to `Pass::run` and `Pass::runOnFunction` is no longer necessary, so remove it. Also update `Pass::create` to return a unique_ptr, which is more efficient than having it return a raw pointer only to have the `PassRunner` wrap that raw pointer in a `unique_ptr`. Delete the unused template `PassRunner::getLast()`, which looks like it was intended to enable retrieving previous analyses and has been in the code base since 2015 but is not implemented anywhere.
* Cleanup em_asm/em_js strings as part of PostEmscripten (#4871)Sam Clegg2022-08-041-0/+180
| | | | Rather than doing it as a side effect of dumping the metadata in wasm-emscripten-finalize.
* Handle invokes of invalid function pointers. See #14174 (#3951)Alon Zakai2021-06-241-13/+25
| | | | | | | | | PostEmscripten will turn an invoke of a constant function pointer index into a direct call. However, due to UB it is possible to have invalid function pointers, and we should not crash on that (and do nothing to optimize, of course). Mostly whitespace; to avoid deep nesting, I added more early returns.
* [reference-types] Support passive elem segments (#3572)Abbas Mashayekh2021-03-051-1/+1
| | | | | | | | | | | Passive element segments do not belong to any table, so the link between Table and elem needs to be weaker; i.e. an elem may have a table in case of active segments, or simply be a collection of function references in case of passive/declarative segments. This PR takes Table::Segment out and turns it into a first class module element just like tables and functions. It also implements early support for parsing, printing, encoding and decoding passive/declarative elem segments.
* [reference-types] remove single table restriction in IR (#3517)Abbas Mashayekh2021-02-091-2/+2
| | | Adds support for modules with multiple tables. Adds a field for the table name to `CallIndirect` and updates the C/JS APIs accordingly.
* Rename Indirect to NonDirect in CallGraphPropertyAnalysis in preparation for ↵Alon Zakai2020-11-131-2/+2
| | | | | | | | | | CallRef (#3355) This is in preparation for CallRef, which takes a reference to a function and calls it. CallGraphPropertyAnalysis needs to be aware of anything that is not a direct call, and "NonDirect" is meant to cover both CallIndirect and CallRef.
* Remove OptimizeCalls from PostEmscripten. NFC. (#3326)Sam Clegg2020-11-061-36/+0
| | | We no longer build modules that import `global.Math`.
* Rename Emscripten EHSjLj functions in wasm backend (#3191)Heejin Ahn2020-10-101-18/+0
| | | | | | | | | | | Now that we are renaming invoke wrappers and `emscripten_longjmp_jmpbuf` in the wasm backend, this deletes all related renaming routines and relevant tests. Depends on #3192. Addresses: #3043 and #3081 Companions: https://reviews.llvm.org/D88697 emscripten-core/emscripten#12399
* Remove unused stack-pointer and emscripten-sbrk-ptr pass args from ↵Sam Clegg2020-09-221-52/+0
| | | | | PostEmscripten (#3161) These were removed completely from the emscripten side in #12057
* Stop renaming longjmp in wasm-emscripten-finalize (#3111)Alon Zakai2020-09-111-0/+18
| | | | | | | | | | | Instead of finalize renaming emscripten_longjmp_jmpbuf to emscripten_longjmp, do nothing in finalize. But in the optional --post-emscripten pass, rename it there if both exist, so that we don't end up using two imports (other optimization passes can then remove an unneeded import). Depends on emscripten-core/emscripten#12157 to land first so that emscripten can handle both names, and it is just an optimization to have one or the other. See https://github.com/WebAssembly/binaryen/issues/3043
* Asyncify verbose option (#3022)Alon Zakai2020-08-061-4/+5
| | | | | | | | | | | | | | | | This logs out the decisions made about instrumenting functions, which can help figure out why a function is instrumented, or to get a list of what might need to be. As the test shows, it can print things like this: [asyncify] import is an import that can change the state [asyncify] calls-import can change the state due to import [asyncify] calls-calls-import can change the state due to calls-import [asyncify] calls-calls-calls-import can change the state due to calls-calls-import (the test has calls-calls-calls-import => calls-calls-import => calls-import -> import).
* Add a builder.makeConst helper template (#2971)Alon Zakai2020-07-211-3/+2
|
* Add stack-pointer argument to post-emscripten pass. (#2823)Sam Clegg2020-05-011-0/+21
| | | | | | | This allows emscripten to statically set the initial value of the stack pointer. Should allow use to avoid doing it dynamically at startup: https://github.com/emscripten-core/emscripten/pull/11031
* Avoid fp$ access in MAIN_MODULES (#2704)Alon Zakai2020-03-271-3/+4
| | | | | | | | | | | | | | | | Depends on emscripten-core/emscripten#10741 which ensures that table indexes are unique. With that guarantee, a main module can just add its function pointers into the table, and use them based on that index. The loader will then see them in the table and then give other modules the identical function pointer for a function, ensuring function pointer equality. This avoids calling fp$ functions during startup for the main module's own functions (which are slow). We do still call fp$s of things we import from outside, as we don't have anything to put in the table for them, we depend on the loader for that. I suspect this can also be done with SIDE_MODULES, but did not want to try too much at once.
* Handle indirect calls in CallGraphPropertyAnalysis (#2624)Alon Zakai2020-01-241-1/+3
| | | | | | | | | | | | | We ignored them, which is a bad default, as typically they imply we can call anything in the table (and the table might change). Instead, notice indirect calls during traversal, and force the user to decide whether to ignore them or not. This was only an issue in PostEmscripten because the other user, Asyncify, already had indirect call analysis because it needed it for other things. Fixes a bug uncovered by #2619 and fixes the current binaryen roll.
* Consistent detection of invoke_ functions in PostEmscripten.cpp (#2619)Sam Clegg2020-01-231-3/+6
| | | | | | | We should be looking at the import name when determining if a function is an invoke function. This is a precursor to re-landing the fix for https://github.com/emscripten-core/emscripten/issues/9950.
* Optimize away invoke_ calls where possible (#2442)Alon Zakai2019-11-191-0/+81
| | | | | | | | | | | | When we see invoke_ calls in emscripten-generated code, we know they call into JS just to do a try-catch for exceptions. If the target being called cannot throw, which we check in a whole-program manner, then we can simply skip the invoke. I confirmed that this fixes the regression in emscripten-core/emscripten#9817 (comment) (that is, with this optimization, upstream is around as fast as fastcomp). When we have native wasm exception handling, this can be extended to optimize that as well.
* Apply the sbrk/brk value at compile time (#2366)Alon Zakai2019-09-301-0/+22
| | | | | We've had an option to set the location of the sbrk ptr, but not the value. Applying the value as well is necessary for standalone wasm, as otherwise we set it in JS.
* Add an option for the PostEmscripten pass to set the sbrk ptr location ↵Alon Zakai2019-09-041-2/+29
| | | | | | | (which is called DYNAMICTOP_PTR in emscripten) (#2325) The assumption is that an import env.emscripten_get_sbrk_ptr exists, and we replace the value returned from there with a constant. (We can't do all this in wasm-emscripten-finalize, as it happens before the JS compiler runs, which can add more static allocations; we only know where the sbrk ptr is later in compilation.) This just replaces the import with a function returning the constant; inlining etc. can help more later. By setting this at compile time we can reduce code size and avoid importing it at runtime, which makes us more compatible with wasi (less special imports).
* Reflect instruction renaming in code (#2128)Heejin Ahn2019-05-211-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | - Reflected new renamed instruction names in code and tests: - `get_local` -> `local.get` - `set_local` -> `local.set` - `tee_local` -> `local.tee` - `get_global` -> `global.get` - `set_global` -> `global.set` - `current_memory` -> `memory.size` - `grow_memory` -> `memory.grow` - Removed APIs related to old instruction names in Binaryen.js and added APIs with new names if they are missing. - Renamed `typedef SortedVector LocalSet` to `SetsOfLocals` to prevent name clashes. - Resolved several TODO renaming items in wasm-binary.h: - `TableSwitch` -> `BrTable` - `I32ConvertI64` -> `I32WrapI64` - `I64STruncI32` -> `I64SExtendI32` - `I64UTruncI32` -> `I64UExtendI32` - `F32ConvertF64` -> `F32DemoteI64` - `F64ConvertF32` -> `F64PromoteF32` - Renamed `BinaryenGetFeatures` and `BinaryenSetFeatures` to `BinaryenModuleGetFeatures` and `BinaryenModuleSetFeatures` for consistency.
* clang-tidy braces changes (#2075)Alon Zakai2019-05-011-1/+2
| | | Applies the changes in #2065, and temprarily disables the hook since it's too slow to run on a change this large. We should re-enable it in a later commit.
* Apply format changes from #2048 (#2059)Alon Zakai2019-04-261-9/+12
| | | Mass change to apply clang-format to everything. We are applying this in a PR by me so the (git) blame is all mine ;) but @aheejin did all the work to get clang-format set up and all the manual work to tidy up some things to make the output nicer in #2048
* Consistently optimize small added constants into load/store offsets (#1924)Alon Zakai2019-03-011-60/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | See #1919 - we did not do this consistently before. This adds a lowMemoryUnused option to PassOptions. It can be passed on the commandline with --low-memory-unused. If enabled, we run the new optimize-added-constants pass, which does the real work here, replacing older code in post-emscripten. Aside from running at the proper time (unlike the old pass, see #1919), this also has a -propagate mode, which can do stuff like this: y = x + 10 [..] load(y) [..] load(y) => y = x + 10 [..] load(x, offset=10) [..] load(x, offset=10) That is, it can propagate such offsets to the loads/stores. This pattern is common in big interpreter loops, where the pointers are offsets into a big struct of state. The pass does this propagation by using a new feature of LocalGraph, which can verify which locals are in SSA mode. Binaryen IR is not SSA (intentionally, since it's a later IR), but if a local only has a single set for all gets, that means that local is in such a state, and can be optimized. The tricky thing is that all locals are initialized to zero, so there are at minimum two sets. But if we verify that the real set dominates all the gets, then the zero initialization cannot reach them, and we are safe. This PR also makes safe-heap aware of lowMemoryUnused. If so, we check for not just an access of 0, but the range 0-1023. This makes zlib 5% faster, with either the wasm backend or asm2wasm. It also makes it 0.5% smaller. Also helps sqlite (1.5% faster) and lua (1% faster)
* Unify imported and non-imported things (#1678)Alon Zakai2018-09-191-4/+5
| | | | | | | | | | | | | | Fixes #1649 This moves us to a single object for functions, which can be imported or nor, and likewise for globals (as a result, GetGlobals do not need to check if the global is imported or not, etc.). All imported things now inherit from Importable, which has the module and base of the import, and if they are set then it is an import. For convenient iteration, there are a few helpers like ModuleUtils::iterDefinedGlobals(wasm, [&](Global* global) { .. use global .. }); as often iteration only cares about imported or defined (non-imported) things.
* notation change: AST => IR (#1245)Alon Zakai2017-10-241-1/+1
| | | The IR is indeed a tree, but not an "abstract syntax tree" since there is no language for which it is the syntax (except in the most trivial and meaningless sense).
* optimize pow (#934)Alon Zakai2017-03-101-0/+23
| | | | | | * optimize pow(x,2) => x*x * optimize pow(x, 0.5) => sqrt(x)
* Default Walker subclasses to using Visitor<SubType> (#921)jgravelle-google2017-02-231-1/+1
| | | | Most module walkers use PostWalker<T, Visitor<T>>, let that pattern be expressed as simply PostWalker<T>
* Fix regression from #850 (#851)Alon Zakai2016-11-301-22/+35
| | | | | | * fix regression from #850 - it is not always safe to fold added offsets into load/store offsets, as the add wraps but offset does not * small refactoring to simplify asm2wasm pass invocation
* Add initialization functions for passes to avoid missing pass registration ↵Jukka Jylänki2016-06-211-1/+3
| | | | due to linker dead code elimination. Fixes #577.
* move function parallelism to pass and pass runner, which allows more ↵Alon Zakai2016-06-031-1/+3
| | | | efficient parallel execution (#564)
* use separate internal opcodes for binary variantsAlon Zakai2016-05-181-1/+1
|
* create a UnifiedExpressionVisitor for passes that want a single visitor ↵Alon Zakai2016-04-181-1/+1
| | | | function, to avoid confusion with having both visit* and visitExpression in a single pass (#357)
* Function parallelism (#343)Alon Zakai2016-04-151-0/+2
| | | | * allow traversals to mark themselves as function-parallel, in which case we run them using a thread pool. also mark some thread-safety risks (interned strings, arena allocators) with assertions they modify only on the main thread
* dyn_cast => dynCastAlon Zakai2016-04-111-3/+3
|
* De-recurse traversals (#333)Alon Zakai2016-04-111-1/+1
| | | | | | | | | | | | * refactor core walking to not recurse * add a simplify-locals test * reuse parent's non-branchey scan logic in SimpleExecutionWalker, reduce code duplication * update wasm.js * rename things following comments
* optimize store offsets from emscripten outputAlon Zakai2016-01-311-2/+12
|
* optimize load offsets from emscripten outputAlon Zakai2016-01-311-0/+68