summaryrefslogtreecommitdiff
path: root/src
Commit message (Collapse)AuthorAgeFilesLines
...
* Enable GlobalStructInference by default (#4734)Alon Zakai2022-06-161-0/+1
| | | | | | | | | This pass helps on at least one Java microbenchmark in a clear way. Real-world data is mixed, with no obvious benefit. But it does optimize 819 callsites on the real-world 14 MB J2Wasm binary, so we may find it helps if/when we run into those code paths. On that binary (the biggest we have for GC) this pass runs in 0.12 seconds, so there is very little downside to enabling it. It is a fast linear-time operation.
* [Parser][NFC] Small code cleanups (#4729)Thomas Lively2022-06-145-16/+19
| | | | Apply cleanups suggested by aheejin in post-merge code review of previous parser PRs.
* Fix an unused variable warning (#4728)walkingeyerobot2022-06-141-0/+1
|
* Reducer: Support --hybrid (#4726)Alon Zakai2022-06-141-0/+3
|
* SignatureRefining: Do not refine types of imported functions (#4727)Alon Zakai2022-06-141-0/+9
| | | | Spec and VM support for that is not yet stable (atm VMs do not allow complex user- defined types to be passed around).
* [NFC] Optimize non-equirecursive LUB calculations (#4722)Thomas Lively2022-06-141-89/+148
| | | | | | | | | | | | | | | | | | | Equirecursive LUB calculations potentially require building new recursive heap types that did not already exist in the system, so they have a complicated code path that uses a TypeBuilder to construct a LUB from the ground up. In contrast, nominal and isorecursive LUB calculations never introduce new heap types, so computing their LUBs is much simpler. Previously we were using the same code path with the TypeBuilder for all type systems out of convenience, but this commit factors out the LUB calculations for nominal and isorecursive types into a separate code path that does not use a TypeBuilder. Not only should this make LUB calculations faster for GC workloads, it also avoids a mysterious race condition during parallel LUB calculations with isorecursive types that resulted in a temporary type escaping from one thread and being used-after-free from another thread. It would be good to fix that bug properly, but it is very difficult to investigate. Sweeping it under the rug instead is the best trade off for now. Fixes #4719.
* [Parser] Parse function types (#4718)Thomas Lively2022-06-141-11/+369
| | | | | | Begin implementing the second phase of parsing, parsing of type definitions. Extend `valtype` to parse both user-defined and built in ref types, add `type` as a top-level module field, and implement parsers for params, results, and functype definitions.
* [Parser] Begin parsing modules (#4716)Thomas Lively2022-06-107-9/+763
| | | | | | | | | | | Implement the basic infrastructure for the full WAT parser with just enough detail to parse basic modules that contain only imported globals. Parsing functions correspond to elements of the grammar in the text specification and are templatized over context types that correspond to each phase of parsing. Errors are explicitly propagated via `Result<T>` and `MaybeResult<T>` types. Follow-on PRs will implement additional phases of parsing and parsing for new elements in the grammar.
* Include globals when collecting module types (#4717)Thomas Lively2022-06-101-0/+3
| | | | Otherwise when a type is only used on a global, it will be incorrectly omitted from the output.
* GlobalStructInference: Handle >2 globals if values coincide (#4714)Alon Zakai2022-06-081-14/+80
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In GSI we look for a read of a global in a situation like this: $global1: value1 $global2: value2 (struct.get $Type (ref)) If global inference shows this get must be of either $global1 or $global2, then we can optimize to this: (ref) == $global1 ? value1 : value2 We focus on the case of two values because 1 is handled by other passes, and >2 makes the tradeoffs less clear. However, a simple extension is the case where there are more than 2 globals, but there are only two values, and one value is unique to one global: $global1: valueA $global2: valueB $global3: valueA => (ref) == $global2 ? valueB : valueA We can still use a single comparison here, on the global that has the unique value. Then the else will handle all the other globals. This increases the cases that GSI can optimize J2Wasm output by over 50%.
* Update relaxed SIMD instructionsThomas Lively2022-06-079-88/+42
| | | | | Update the opcodes for all relaxed SIMD instructions and remove the unsigned dot product instructions that are no longer in the proposal.
* [Optimize Instructions] Simplify sign extentions (Part 2) (#4382)Max Graey2022-06-071-0/+16
| | | | | | Similar to #4004 but for 32-bit integers i32(x) << 24 >> 24 ==> i32.extend8_s(x) i32(x) << 16 >> 16 ==> i32.extend16_s(x)
* Restore and fix SpillPointers pass (#4570)Alon Zakai2022-06-065-0/+326
| | | | | | | | We have some possible use cases for this pass, and so are restoring it. This reverts the removal in #3261, fixes compile errors in internal API changes since then, and flips the direction of the stack for the wasm backend.
* Add C API for printing stack IR (#4692)ApsarasX2022-06-034-0/+61
| | | | | | | | BinaryenModulePrintStackIR: similar to BinaryenModulePrint BinaryenModuleWriteStackIR: similar to BinaryenModuleWriteText BinaryenModuleAllocateAndWriteStackIR: similar to BinaryenModuleAllocateAndWriteText
* Fuzzer: Add support for creating structs and arrays in makeConst (#4707)Alon Zakai2022-06-011-8/+20
| | | | | | | | #4659 adds a testcase with an import of (ref $struct). This could cause an error in the fuzzer, since it wants to remove imports (because the various fuzzers cannot pass in custom imports - they want to just run the wasm). When it tries to remove that import it tries to create a constant for a struct reference, and fails. To fix that, add enough support to create structs and arrays at least in the simple case where all their fields are defaultable.
* Fuzzer: Refactor makeConst into separate functions [NFC] (#4709)Alon Zakai2022-06-012-85/+118
| | | This just moves code around + adds assertions.
* [Parser] Token classification (#4699)Thomas Lively2022-06-012-22/+183
| | | | | | | | | | | | Add methods to `Token` for determining whether the token can be interpreted as a particular token type, returning the interpreted value as appropriate. These methods perform additional bounds checks for integers and NaN payloads that could not be done during the initial lexing because the lexer did not know what the intended token type was. The float methods also reinterpret integer tokens as floating point tokens since the float grammar is a superset of the integer grammar and inject the NaN payloads into parsed NaN values. Move all bounds checking to these new classifier functions to have it in one place.
* Global Struct Inference pass: Infer two constants in struct.get (#4659)Alon Zakai2022-06-014-0/+248
| | | | | | | | | | | | | | | | | | | | | | | This optimizes constants in the megamorphic case of two: when we know two function references are possible, we could in theory emit this: (select (ref.func A) (ref.func B) (ref.eq (..ref value..) ;; globally, only 2 things are possible here, and one has ;; ref.func A as its value, and the other ref.func B (ref.func A)) That is, compare to one of the values, and emit the two possible values there. Other optimizations can then turn a call_ref on this select into an if over two direct calls, leading to devirtualization. We cannot compare a ref.func directly (since function references are not comparable), and so instead we look at immutable global structs. If we find a struct type that has only two possible values in some field, and the structs are in immutable globals (which happens in the vtable case in j2wasm for example), then we can compare the references of the struct to decide between the two values in the field.
* [NFC] Refactor EHUtils::findPops() method (#4704)Alon Zakai2022-06-013-25/+53
| | | | This moves it out of the validator so it can be used elsewhere. It will be used in #4685
* [WasmGC] Fix try-catch and non-nullable local sets in values in ↵Alon Zakai2022-05-311-5/+17
| | | | | | | | | SimplifyLocals (#4705) Followup to #4703, this also handles the case where there is a non- nullable local.set in the value of a nullable one, which we also cannot optimize. Fixes #4702
* [WasmGC] Fix non-nullable locals and try-catch in SimplifyLocals (#4703)Alon Zakai2022-05-311-3/+38
| | | | | | | Binaryen will not change dominance in SimplifyLocals, however, the current spec's notion of dominance is simpler than ours, and we must not optimize certain cases in order to still validate. See details in the comment and test. Helps #4702
* wasm-emscripten-finalize: Improve detection of mainReadsParams (#4701)Sam Clegg2022-05-311-6/+10
| | | | | | The first way to should detect this is if the main function actually doesn't take any params. They we fallback to looking deeper. In preparation for https://reviews.llvm.org/D75277
* Remove renameMainArgcArgv from wasm-emscripten-finalize (#4700)Sam Clegg2022-05-313-24/+0
| | | | | | | | | This part to finalize is currently not used and was added in preparation for https://reviews.llvm.org/D75277. However, the better solution to dealing with this alternative name for main is on the emscripten side. The main reason for this is that doing the rename here in binaryen would require finalize to always re-write the binary, which is expensive.
* [Parser] Replace Signedness with ternary Sign (#4698)Thomas Lively2022-05-272-25/+23
| | | | | | | | Previously we were tracking whether integer tokens were signed but we did not differentiate between positive and negative signs. Unfortunately, without differentiating them, there's no way to tell the difference between an in-bounds negative integer and a wildly out-of-bounds positive integer when trying to perform bounds checks for s32 tokens. Fix the problem by tracking not only whether there is a sign on an integer token, but also what the sign is.
* [Parser][NFC] Improve comments about default NaN payloads (#4697)Thomas Lively2022-05-272-0/+7
|
* [Parser][NFC] Remove extraneous braces from std::optional returns (#4696)Thomas Lively2022-05-271-9/+9
|
* [Parser][NFC] Create a public wat-lexer.h header (#4695)Thomas Lively2022-05-273-241/+283
| | | | | | wat-parser-internal.h was already quite large after implementing just the lexer, so it made sense to rename it to be lexer-specific and start a new file for the higher-level parser. Also make it a proper .cpp file and split the testable interface out into wat-lexer.h.
* OptimizeInstructions: Turn call_ref of a select into an if over two direct ↵Alon Zakai2022-05-273-67/+219
| | | | | | calls (#4660) This extends the existing call_indirect code to do the same for call_ref, basically. The shared code is added to a new helper utility.
* [EH] Export tags (#4691)Heejin Ahn2022-05-262-1/+2
| | | | | | | | | | | | | | | | | This adds exported tags to `exports` section in wasm-emscripten-finalize metadata so Emscripten can use it. Also fixes a bug in the parser. We have only recognized the export format of ```wasm (tag $e2 (param f32)) (export "e2" (tag $e2)) ``` and ignored this format: ```wasm (tag $e1 (export "e1") (param i32)) ``` Companion patch: https://github.com/emscripten-core/emscripten/pull/17064
* [Parser][NFC] Clarify escaped string lexing (#4694)Thomas Lively2022-05-261-24/+27
| | | | Improve comments and variable names to make it clear that we allocate and build a separate string only when necessary to handle escape sequences.
* [Parser] Lex floating point values (#4693)Thomas Lively2022-05-261-25/+231
| | | | | | | | | | | | | | | Rather than trying to actually implement the parsing of float values, which cannot be done naively due to precision concerns, just parse the float grammar then postprocess the parsed text into a form we can pass to `strtod` to do the actual parsing of the value. Since the float grammar reuses `num` and `hexnum` from the integer grammar but does not care about overflow, add a mode to `LexIntCtx`, `num`, and `hexnum` to allow parsing overflowing numbers. For NaNs, store the payload as a separate value rather than as part of the parsed double. The payload will be injected into the NaN at a higher level of the parser once we know whether we are parsing an f64 or an f32 and therefore know what the allowable payload values are.
* [Parser] Lex keywords (#4688)Thomas Lively2022-05-251-1/+36
| | | | | Also include reserved words that look like keywords to avoid having to find and enumerate all the valid keywords. Invalid keywords will be rejected at a higher level in the parser instead.
* [Parser] Lex strings (#4687)Thomas Lively2022-05-251-1/+164
|
* [Parser] Lex idchar and identifiers (#4686)Thomas Lively2022-05-251-3/+82
|
* [Wasm GC] Fix CFG traversal of call_ref and add missing validation check (#4690)Alon Zakai2022-05-252-1/+34
| | | | | | | | We were missing CallRef in the CFG traversal code in a place where we note possible exceptions. As a result we thought CallRef cannot throw, and were missing some control flow edges. To actually detect the problem, we need to validate non-nullable locals properly, which we were not doing. This adds that as well.
* [Parser] Start a new text format parser (#4680)Thomas Lively2022-05-241-0/+557
| | | | | | | | | | | | | | | | | | | | | | | | | Begin implementing a new text format parser that will accept the standard text format. Start with a lexer that can iterate over tokens in an underlying text buffer. The initial supported tokens are integers, parentheses, and whitespace including comments. The implementation is in a new private internal header so it can be included into a gtest source file even though it is not meant to be a public API. Once the parser is more complete, there will be an additional public header exposing a more concise public API and the private header will be included into a source file that implements that public API. The new parser will improve on the existing text format parser not only because it will accept the full standard text format, but also because its code will be simpler and easier to maintain and because it will hopefully be faster as well. The new parser will be built out of small functions that closely mirror the grammar productions given in the spec and will heavily use C++17 features like string_view, optional, and variant to provide more self-documenting and efficient code. Future PRs will add support for lexing other kinds of tokens followed by support for parsing more complex constructs.
* Add C and JS API functions for accessing memory info (#4682) Jackson Gardner2022-05-243-2/+57
| | | Based on #3573 plus minor fixes
* [Nominal Fuzzing] Fix SignatureRefining by updating types fully at once (#4665)Alon Zakai2022-05-233-20/+66
| | | | | | | | | | Optionally avoid updating types in TypeUpdating::updateParamTypes(). That update is incomplete if the function signature is also changing, which is the case in SignatureRefining (but not DeadArgumentElimination). "Incomplete" means that we updated the local.get type, but the function signature does not match yet. That incomplete state can hit an internal error in GlobalTypeRewriter::updateSignatures where it updates types. To avoid that, do the entire full update only there (in GlobalTypeRewriter::updateSignatures).
* Fix binary parsing of the prototype nominal format (#4679)Thomas Lively2022-05-191-3/+0
| | | | | | We were checking that nominal modules only had a single element in their type sections, but that's not correct for the prototype nominal binary format we still want to support. The test for this missed catching the bug because it wasn't actually parsing in nominal mode.
* [NFC] Deduplicate imported global parsing (#4678)Thomas Lively2022-05-191-23/+7
| | | | | | | | | | Share the logic for parsing imported and non-imported globals of the formats: (import "module" "base" (global $name? type)) (global $name? type init) This fixes #4676, since the deleted logic for parsing imported globals did not handle parsing GC types correctly.
* Validator: Check features for ref.null's type (#4677)Alon Zakai2022-05-182-0/+7
|
* [GC Fuzzing] Avoid non-nullable eqref without GC (#4675)Alon Zakai2022-05-181-2/+22
| | | | | | With only reference types but not GC, we cannot easily create a constant for eqref for example. Only GC adds i31.new etc. To avoid assertions in the fuzzer, avoid randomly picking (ref eq) etc., that is, keep it nullable so that we can emit a (ref.null eq) if we need a constant value of that type.
* wasm-reduce: Fix order in shrinkByReduction call (#4673)Alon Zakai2022-05-171-1/+4
| | | | | | The old code would short-circuit and not do anything after we managed any reduction in the loop here. That would end up doing entire iterations of the whole pipeline before removing another element segment, which could be slow.
* [NFC] Remove unneeded include (#4670)Alon Zakai2022-05-171-1/+0
|
* [NFC] Make InsertOrderedMap::insert's param const (#4669)Alon Zakai2022-05-171-1/+1
| | | | | Being a const reference allows writing insert({a, b}), which will be useful in a future PR, and there is no reason to actually update the reference.
* [NFC] Make PossibleConstantValues::dump() a const function (#4671)Alon Zakai2022-05-171-1/+1
|
* [Fuzzer] Reduce trap probability in function ref fallback code (#4653)Alon Zakai2022-05-161-10/+15
| | | | | | Also improve comments. As suggested in #4647
* Allow TypeBuilder::grow to take 0 as an argument (#4668)Thomas Lively2022-05-161-1/+1
| | | | | | | There's no reason not to allow growing by zero slots, but previously doing so would trigger an assertion. This caused a crash when roundtripping a trivial module. Fixes #4667.
* Ensure symmetric results in PossibleConstantValues (#4662)Alon Zakai2022-05-131-16/+23
| | | | | | | | | | | | | | | | | | | | | | | Previously we could return different results depending on the order we noted things: note(anyref.null); note(funcref.null); get() => anyref.null note(funcref.null); note(anyref.null); get() => funcref.null This is correct, as nulls are equal anyhow, and any could be used in the location we are optimizing. However, it can lead to nondeterminism if the caller's order of notes is nondeterministic. That is the case in DeadArgumentElimination, where we scan functions in parallel, then merge them without special ordering. To fix this, make the note operation symmetric. That seems simplest and least likely to be confusing. We can use the LUB to do that. To avoid duplicating the null logic, refactor note() to use combine().
* Followups to #3841 (#3842)Alon Zakai2022-05-131-6/+0
| | | | | If we don't think that preventing copies in assignment makes sense by itself (since we allow them on construction) then I think we can just remove the restriction and also the implicit copy constructor.