summaryrefslogtreecommitdiff
path: root/src/wasm
Commit message (Collapse)AuthorAgeFilesLines
* Use non-nullable ref.cast for non-nullable input (#5335)Thomas Lively2022-12-093-5/+33
| | | | | | | | | | | | We switched from emitting the legacy `ref.cast_static` instruction to emitting `ref.cast null` in #5331, but that wasn't quite correct. The legacy instruction had polymorphic typing so that its output type was nullable if and only if its input type was nullable. In contrast, `ref.cast null` always has a a nullable output type. Fix our output by instead emitting non-nullable `ref.cast` if the output should be non-nullable. Parse `ref.cast` in binary and text forms as well. Since the IR can only represent the legacy polymorphic semantics, disallow unsupported casts from nullable to non-nullable references or vice versa for now.
* Allow casting to basic heap types (#5332)Thomas Lively2022-12-083-34/+44
| | | | | | | The standard casting instructions now allow casting to basic heap types, not just user-defined types, but they also require that the intended type and argument type have a common supertype. Update the validator to use the standard rules, update the binary parser and printer to allow basic types, and update the tests to remove or modify newly invalid test cases.
* Validate ref.as_* argument is a reference (#5330)Alon Zakai2022-12-081-1/+5
| | | | | | | | | | | | | | Without this we hit an assert with no line number info (or in a no-asserts build, bad things can happen). With this: $ bin/wasm-opt -all ~/Downloads/crash.wat --nominal [parse exception: Invalid ref for ref.as (at 155065:119)] Fatal: error parsing wasm (That can only happen for ref.as_non_null, as all the others do not have that assert - their types do not depend on the child's type, so their finalize does not error. Still, it is nice to validate earlier for them as well, so this PR handles them all.)
* Add standard versions of WasmGC casts (#5331)Thomas Lively2022-12-074-63/+42
| | | | | | | We previously supported only the non-standard cast instructions introduced when we were experimenting with nominal types. Parse the names and opcodes of their standard counterparts and switch to emitting the standard names and opcodes. Port all of the tests to use the standard instructions, but add additional tests showing that the non-standard versions are still parsed correctly.
* [Parser][NFC] Add `Idx` to type aliases representing indices (#5326)Thomas Lively2022-12-061-38/+42
| | | | | | | Previously we had types like `LocalT` and `MemoryT` to represent references to locals and memories, but when we added field indices in #5255, we had to use `FieldIdxT` instead of `FieldT` because `FieldT` was already in use as the type representing a field itself. Update `LocalT`, `MemoryT` and `GlobalT` to have `Idx` in their names to be consistent with `FieldIdxT`.
* [NFC] Do not read past the end of a string_view (#5317)Thomas Lively2022-12-021-5/+5
| | | | | | | | wasm-s-parser.cpp was detecting the end of type strings by looking for null characters, but those null characters would be past the end of the relevant string_view. Bring that code in line with similar code by checking the length of the string_view instead. Fixes an assertion failure in MSVC debug mode. Fixes #5312.
* [Parser] Avoid calling `strtod` on NaNs entirely (#5316)Thomas Lively2022-12-021-5/+6
| | | | | | | | MSVC's implementation of `strtod` doesn't return a negative Nan for "-nan", so we already had a workaround to explicitly handle that case without calling `strtod`. Unfortunately the workaround was not used for negative NaNs with payloads, so there were still bugs. Fix the problem and make the code even more portable by avoiding `strtod` completely for any kind of nan, positive or negative, with or without payload.
* Remove more uses of NAN (#5310)Thomas Lively2022-12-021-0/+2
| | | | | In favor of the more portable code snippet using `std::copysign`. Also reintroduce assertions that the NaNs have the expected signs. This continues work started in #5302.
* Use C++17's [[maybe_unused]]. NFC (#5309)Sam Clegg2022-12-021-2/+1
|
* [Parser] Do not assume `NAN` is positive (#5302)Thomas Lively2022-11-291-3/+4
| | | | | | | It turns out that this assumption does not necessarily hold on Windows with Visual Studio 2019. Instead of using `NAN` and `-NAN`, explicitly construct positive and negative NaN values with `std::copysign`, which should be portable. Fixes #5291.
* Fix validation and inlining bugs (#5301)Thomas Lively2022-11-291-2/+5
| | | | | | | | | | | | | | Inlining had a bug where it gave return_calls in inlined callees concrete types even when they should have remained unreachable. This bug flew under the radar because validation had a bug where it allowed expressions to have concrete types when they should have been unreachable. The fuzzer found this bug by adding another pass after inlining where the unexpected types caused an assertion failure. Fix the bugs and add a test that would have triggered the inlining bug. Unfortunately the test would have also passed before this change due to the validation bug, but it's better than nothing. Fixes #5294.
* Validator: Print the field number on subtyping errors (#5297)Alon Zakai2022-11-291-4/+6
|
* Remove equirecursive typing (#5240)Thomas Lively2022-11-232-1062/+19
| | | | Equirecursive is no longer standards track and its implementation is extremely complex. Remove it.
* Change the default type system to isorecursive (#5239)Thomas Lively2022-11-231-1/+1
| | | | | | | | | | This makes Binaryen's default type system match the WasmGC spec. Update the way type definitions without supertypes are printed to reduce the output diff for MVP tests that do not involve WasmGC. Also port some type-builder.cpp tests from test/example to test/gtest since they needed to be rewritten to work with isorecursive type anyway. A follow-on PR will remove equirecursive types completely.
* Rename UserSection -> CustomSection. NFC (#5288)Sam Clegg2022-11-223-96/+96
| | | This reflects that naming used in the spec.
* [NFC] Expand comment about validating function type features (#5286)Thomas Lively2022-11-221-1/+3
| | | This addresses feedback missed in #5279.
* Validate that GC is enabled for rec groups and supertypes (#5279)Thomas Lively2022-11-222-8/+13
| | | | | | | | | Update `HeapType::getFeatures` to report that GC is used for heap types that have nontrivial recursion groups or supertypes. Update validation to check the features on function heap types, not just their individual params and results. This fixes a fuzz bug in #5239 where initial contents included a rec group but the fuzzer disabled GC. Since the resulting module passed validation, the rec groups made it into the binary output, making the type section malformed.
* Fix isorecursive canonicalization (#5269)Thomas Lively2022-11-171-5/+4
| | | | | | | | | | | | | | Fixes a longstanding problem with isorecursive canonicalization that only showed up in MacOS and occasionally Windows builds. The problem was that `RecGroupEquator` was not quite correct in the presence of self-references in rec groups. Specifically, `RecGroupEquator` did not differentiate between instances of the same type appearing across two rec groups where the type was a self-reference in one group but not in the other. The reason this only showed up occasionally on some platforms was that this bug could only cause incorrect behavior if two groups that would incorrectly be compared as equal were hashed into the same bucket of a hash map. Apparently the hash map used on Linux never hashes the two problematic groups into the same bucket.
* Revert "Revert "Make `call_ref` type annotations mandatory (#5246)" (#5265)" ↵Thomas Lively2022-11-162-50/+16
| | | | | (#5266) This reverts commit 570007dbecf86db5ddba8d303896d841fc2b2d27.
* Revert "Make `call_ref` type annotations mandatory (#5246)" (#5265)Thomas Lively2022-11-162-16/+50
| | | | | This reverts commit b2054b72b7daa89b7ad161c0693befad06a20c90. It looks like the necessary V8 change has not rolled out everywhere yet.
* Fix an unused var warning in some compilers (#5260)Alon Zakai2022-11-151-2/+1
|
* Switch from `typedef` to `using` in C++ code. NFC (#5258)Sam Clegg2022-11-151-1/+1
| | | | This is more modern and (IMHO) easier to read than that old C typedef syntax.
* [Parser] Parse struct allocation and accessor instructions (#5255)Thomas Lively2022-11-151-7/+128
| | | | | Including support for parsing field indices. Although only numeric field indices are supported at the moment, set up the code to make it straightforward to implement type-dependent symbolic field names in the future.
* Make `call_ref` type annotations mandatory (#5246)Thomas Lively2022-11-152-50/+16
| | | | They were optional for a while to allow users to gracefully transition to using them, but now make them mandatory to match the upstream WasmGC spec.
* [Parser] Parse `ref.is*`, `ref.eq`, `i31.new`, and `i31.get*` (#5247)Thomas Lively2022-11-141-4/+36
|
* Implement `array.new_data` and `array.new_elem` (#5214)Thomas Lively2022-11-077-10/+184
| | | | | | | | | In order to test them, fix the binary and text parsers to accept passive data segments even if a module has no memory. In addition to parsing and emitting the new instructions, also implement their validation and interpretation. Test the interpretation directly with wasm-shell tests adapted from the upstream spec tests. Running the upstream spec tests directly would require fixing too many bugs in the legacy text parser, so it will have to wait for the new text parser to be ready.
* Fix binary parsing of data segment memory (#5208)Thomas Lively2022-11-032-5/+6
| | | | | | | | | | | | The binary parser was eagerly getting the name of memories to set the `memory` field of data segments, but that meant that when the memory names were updated later while parsing the names section, the data segment memory fields would become out of date. Update the issue by deferring setting the `memory` fields like we do for other parts of IR that reference memories. Also fix a segfault in the validator that was triggered by the reproducer for this bug before the bug was fixed. Fixes #5204.
* [NFC] Mention relevant flags in validator errors (#5203)Alon Zakai2022-11-011-93/+116
| | | | | | | | | | E.g. Atomic operation (atomics are disabled) => Atomic operations require threads [--enable-threads]
* Work around some gcc 10.x issues (#5199)Alon Zakai2022-10-311-5/+5
| | | See #5188
* [Parser] Parse `return` (#5181)Thomas Lively2022-10-251-2/+36
| | | | | | | | | Unlike in the legacy parser, we cannot depend on the folded text format to determine how many values to return, so we determine that solely based on the current function context. To handle multivalue return correctly, fix a bug in which we could synthesize new `unreachable`s and place them before existing unreachable instructions (like returns) at the end of instruction sequences.
* [Parser][NFC] Combine InstrParserCtx into ParseDefsCtx (#5179)Thomas Lively2022-10-211-302/+312
| | | | | ParseDefsCtx was the only client of the CRTP InstrParserCtx utility and the separation between the two did not serve a real purpose. Simplify the code by combining them.
* [Parser] Parse `memory.copy` and `memory.fill` (#5178)Thomas Lively2022-10-211-2/+42
|
* [Parser] Parse loads and stores (#5174)Thomas Lively2022-10-212-46/+275
| | | | | | | | | | Add parsing functions for `memarg`s, the offset and align fields of load and store instructions. These fields are interesting because they are lexically reserved words that need to be further parsed to extract their actual values. On top of that, add support for parsing all of the load and store instructions. This required fixing a buffer overflow problem in the generated parser code and adding more information to the signatures of the SIMD load and store instructions. `SIMDLoadStoreLane` instructions are particularly interesting because they may require backtracking to parse correctly.
* [Parser] Parse shared memory declarations (#5173)Thomas Lively2022-10-211-14/+18
|
* [NFC] Avoid re-parsing instruction names (#5171)Thomas Lively2022-10-202-142/+57
| | | | | | | | | Since gen-s-parser.py is essentially a giant table mapping instruction names to the information necessary to construct the corresponding IR nodes, there should be no need to further parse instruction names after the code generated by gen-s-parser.py runs. However, memory instruction parsing still parsed instruction names to get information such as size and default alignment. The new parser does not have the ability to parse that information out of instruction names, so put it in the gen-s-parser.py table instead.
* Remove excessive validation that is not in the wasm spec (#5167)Alon Zakai2022-10-201-28/+1
| | | | | | | | Specifically if a segment offset was a const, we checked that it made sense. But the wasm spec doesn't do that, and it actually causes some issues (#5163). In theory this extra validation might be useful - compile-time error rather than runtime - but if we want this it should probably be an optional thing, like an opt-in flag or a --lint pass or such.
* [Parser] Parse `memory.size` and `memory.grow` (#5165)Thomas Lively2022-10-201-8/+76
| | | | | Also add the ability to parse memory indexes to correctly handle the multi-memory versions of these instructions. Add and use a conversion from `Result` to `MaybeResult` as well.
* [Parser] Parse memories (#5164)Thomas Lively2022-10-191-23/+204
| | | | | | | Parse 32-bit and 64-bit memories, including their initial and max sizes. Shared memories are left to a follow-up PR. The memory abbreviation that includes inline data is parsed, but the associated data segment is not yet created. Also do some minor simplifications in neighboring helper functions for other kinds of module elements.
* [NFC] Remove obsolete `makePush` functions (#5159)Thomas Lively2022-10-191-6/+0
| | | `Push` expressions were removed in #2867, so we no longer need to make them.
* [Parser] Parse SIMD ternary expressions and shifts (#5158)Thomas Lively2022-10-191-2/+22
|
* [Parser] Parse SIMD lane manipulation instructions (#5156)Thomas Lively2022-10-191-5/+58
| | | Including all `SIMDExtract`, `SIMDReplace`, `SIMDShuffle` expressions.
* [Parser] Parse global.get and global.set (#5155)Thomas Lively2022-10-191-15/+75
| | | | Also add some missing error checking for the similar local instructions and make some neighboring styling more consistent.
* Parse and emit `array.len` without a type annotation (#5151)Thomas Lively2022-10-183-8/+12
| | | Test that we can still parse the old annotated form as well.
* Implement `array` basic heap type (#5148)Thomas Lively2022-10-186-27/+87
| | | | | | | | | `array` is the supertype of all defined array types and for now is a subtype of `data`. (Once `data` becomes `struct` this will no longer be true.) Update the binary and text parsing of `array.len` to ignore the obsolete type annotation and update the binary emitting to emit a zero in place of the old type annotation and the text printing to print an arbitrary heap type for the annotation. A follow-on PR will add support for the newer unannotated version of `array.len`.
* Exhaustively test basic heap type relationships (#5147)Thomas Lively2022-10-171-4/+3
| | | | | | As the number of basic heap types has grown, the complexity of the subtype and LUB calculations has grown as well. To ensure that they are correct, test the complete matrix of basic types and trivial user-defined types. Fix the subtype calculation to make string types subtypes of `any` to make the test pass.
* Binary format: Don't emit empty Memory sections (#5145)Alon Zakai2022-10-171-1/+1
| | | | If the only memories are imported, we don't need the section. We were already doing that for tables, functions, etc.
* [Parser] Validate type annotations on `select` (#5139)Thomas Lively2022-10-131-1/+5
| | | | | Since the type annotations are not stored explicitly in Binaryen IR, we have to validate them in the parser. Implement this and fix a newly-caught incorrect annotation in the tests.
* Add "struct" and "structref" as an alias for "data" and "dataref" (#5141)Thomas Lively2022-10-131-2/+4
| | | | | | | In the upstream spec, `data` has been replaced with a type called `struct`. To allow for a graceful update in Binaryen, start by introducing "struct" as an alias for "data". Once users have stopped emitting `data` directly, future PRs will remove `data` and update the subtyping so that arrays are no longer subtypes of `struct`.
* [Parser] Parse `local.set` and `local.tee` (#5138)Thomas Lively2022-10-131-2/+29
|
* [NFC] Add an explicit deduction guide for `WithPosition` (#5140)Thomas Lively2022-10-131-0/+3
| | | | | Since our usage of `WithPosition` depends on C++17 class template argument deduction, it triggers a clang warning `-Wctad-maybe-unsupported`. Silence the warning by providing an explicit deduction guide.