diff --git a/scripts/update_lit_checks.py b/scripts/update_lit_checks.py index 20a585145da..b7b836e57cc 100755 --- a/scripts/update_lit_checks.py +++ b/scripts/update_lit_checks.py @@ -40,7 +40,7 @@ ALL_ITEMS = '|'.join(['type', 'import', 'global', 'memory', 'data', 'table', 'elem', 'tag', 'export', 'start', 'func']) ITEM_NAME = r'\$?[^\s()]*|"[^\s()]*"' -ITEM_RE = re.compile(r'(^\s*)\((' + ALL_ITEMS + r')\s+(' + ITEM_NAME + ').*$', +ITEM_RE = re.compile(r'(?:^\s*\(rec\s*)?(^\s*)\((' + ALL_ITEMS + r')\s+(' + ITEM_NAME + ').*$', re.MULTILINE) FUZZ_EXEC_FUNC = re.compile(r'^\[fuzz-exec\] calling (?P\S*)$') diff --git a/src/wasm/wat-parser.cpp b/src/wasm/wat-parser.cpp index b81a92464dd..bb756159b83 100644 --- a/src/wasm/wat-parser.cpp +++ b/src/wasm/wat-parser.cpp @@ -314,6 +314,7 @@ struct ParseDeclsCtx { // The module element definitions we are parsing in this phase. std::vector typeDefs; + std::vector subtypeDefs; std::vector globalDefs; // Counters used for generating names for module elements. @@ -350,7 +351,7 @@ struct ParseTypeDefsCtx { // Parse the names of types and fields as we go. std::vector names; - // The index of the type definition we are parsing. + // The index of the subtype definition we are parsing. Index index = 0; ParseTypeDefsCtx(TypeBuilder& builder, const IndexMap& typeIndices) @@ -930,7 +931,7 @@ template Result<> strtype(Ctx& ctx, ParseInput& in) { // subtype ::= '(' 'type' id? '(' 'sub' typeidx? strtype ')' ')' // | '(' 'type' id? strtype ')' template MaybeResult<> subtype(Ctx& ctx, ParseInput& in) { - [[maybe_unused]] auto start = in.getPos(); + [[maybe_unused]] auto pos = in.getPos(); if (!in.takeSExprStart("type"sv)) { return {}; @@ -969,7 +970,10 @@ template MaybeResult<> subtype(Ctx& ctx, ParseInput& in) { } if constexpr (parsingDecls) { - ctx.typeDefs.push_back({name, start}); + ctx.subtypeDefs.push_back({name, pos}); + } + if constexpr (parsingTypeDefs) { + ++ctx.index; } return Ok{}; @@ -978,8 +982,35 @@ template MaybeResult<> subtype(Ctx& ctx, ParseInput& in) { // deftype ::= '(' 'rec' subtype* ')' // | subtype template MaybeResult<> deftype(Ctx& ctx, ParseInput& in) { - // TODO: rec - return subtype(ctx, in); + [[maybe_unused]] auto pos = in.getPos(); + + if (in.takeSExprStart("rec"sv)) { + [[maybe_unused]] size_t startIndex = 0; + if constexpr (parsingTypeDefs) { + startIndex = ctx.index; + } + size_t groupLen = 0; + while (auto type = subtype(ctx, in)) { + CHECK_ERR(type); + ++groupLen; + } + if (!in.takeRParen()) { + return in.err("expected type definition or end of recursion group"); + } + if constexpr (parsingTypeDefs) { + ctx.builder.createRecGroup(startIndex, groupLen); + } + } else if (auto type = subtype(ctx, in)) { + CHECK_ERR(type); + } else { + return {}; + } + + if constexpr (parsingDecls) { + ctx.typeDefs.push_back({{}, pos}); + } + + return Ok{}; } // global ::= '(' 'global' id? ('(' 'export' name ')')* gt:globaltype e:expr ')' @@ -1175,15 +1206,18 @@ Result<> parseModule(Module& wasm, std::string_view input) { } } - auto typeIndices = createIndexMap(input, decls.typeDefs); + auto typeIndices = createIndexMap(input, decls.subtypeDefs); CHECK_ERR(typeIndices); // Parse type definitions. std::vector types; { - TypeBuilder builder(decls.typeDefs.size()); + TypeBuilder builder(decls.subtypeDefs.size()); ParseTypeDefsCtx ctx(builder, *typeIndices); - CHECK_ERR(parseDefs(ctx, input, decls.typeDefs, deftype)); + for (auto& typeDef : decls.typeDefs) { + ParseInput in(input, typeDef.pos); + CHECK_ERR(deftype(ctx, in)); + } auto built = builder.build(); if (auto* err = built.getError()) { std::stringstream msg; diff --git a/test/lit/isorecursive-good.wast b/test/lit/isorecursive-good.wast index 7ced0d8d31c..da14b38c8ce 100644 --- a/test/lit/isorecursive-good.wast +++ b/test/lit/isorecursive-good.wast @@ -1,4 +1,4 @@ -;; TODO: Autogenerate these checks! The current script cannot handle `rec`. +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. ;; RUN: wasm-opt %s -all --hybrid -S -o - | filecheck %s --check-prefix HYBRID ;; RUN: wasm-opt %s -all --hybrid --roundtrip -S -o - | filecheck %s --check-prefix HYBRID @@ -6,46 +6,63 @@ (module -;; HYBRID: (rec -;; HYBRID-NEXT: (type $super-struct (struct_subtype (field i32) data)) -;; HYBRID-NEXT: (type $sub-struct (struct_subtype (field i32) (field i64) $super-struct)) -;; HYBRID-NEXT: ) - -;; HYBRID: (rec -;; HYBRID-NEXT: (type $super-array (array_subtype (ref $super-struct) data)) -;; HYBRID-NEXT: (type $sub-array (array_subtype (ref $sub-struct) $super-array)) -;; HYBRID-NEXT: ) - -;; NOMINAL-NOT: rec - -;; NOMINAL: (type $super-struct (struct_subtype (field i32) data)) -;; NOMINAL-NEXT: (type $sub-struct (struct_subtype (field i32) (field i64) $super-struct)) - -;; NOMINAL: (type $super-array (array_subtype (ref $super-struct) data)) -;; NOMINAL-NEXT: (type $sub-array (array_subtype (ref $sub-struct) $super-array)) (rec + ;; HYBRID: (rec + ;; HYBRID-NEXT: (type $super-struct (struct_subtype (field i32) data)) + ;; NOMINAL: (type $super-struct (struct_subtype (field i32) data)) (type $super-struct (struct i32)) + ;; HYBRID: (type $sub-struct (struct_subtype (field i32) (field i64) $super-struct)) + ;; NOMINAL: (type $sub-struct (struct_subtype (field i32) (field i64) $super-struct)) (type $sub-struct (struct_subtype i32 i64 $super-struct)) ) (rec + ;; HYBRID: (rec + ;; HYBRID-NEXT: (type $super-array (array_subtype (ref $super-struct) data)) + ;; NOMINAL: (type $super-array (array_subtype (ref $super-struct) data)) (type $super-array (array (ref $super-struct))) + ;; HYBRID: (type $sub-array (array_subtype (ref $sub-struct) $super-array)) + ;; NOMINAL: (type $sub-array (array_subtype (ref $sub-struct) $super-array)) (type $sub-array (array_subtype (ref $sub-struct) $super-array)) ) + ;; HYBRID: (func $make-super-struct (type $none_=>_ref|$super-struct|) (result (ref $super-struct)) + ;; HYBRID-NEXT: (call $make-sub-struct) + ;; HYBRID-NEXT: ) + ;; NOMINAL: (func $make-super-struct (type $none_=>_ref|$super-struct|) (result (ref $super-struct)) + ;; NOMINAL-NEXT: (call $make-sub-struct) + ;; NOMINAL-NEXT: ) (func $make-super-struct (result (ref $super-struct)) (call $make-sub-struct) ) + ;; HYBRID: (func $make-sub-struct (type $none_=>_ref|$sub-struct|) (result (ref $sub-struct)) + ;; HYBRID-NEXT: (unreachable) + ;; HYBRID-NEXT: ) + ;; NOMINAL: (func $make-sub-struct (type $none_=>_ref|$sub-struct|) (result (ref $sub-struct)) + ;; NOMINAL-NEXT: (unreachable) + ;; NOMINAL-NEXT: ) (func $make-sub-struct (result (ref $sub-struct)) (unreachable) ) + ;; HYBRID: (func $make-super-array (type $none_=>_ref|$super-array|) (result (ref $super-array)) + ;; HYBRID-NEXT: (call $make-sub-array) + ;; HYBRID-NEXT: ) + ;; NOMINAL: (func $make-super-array (type $none_=>_ref|$super-array|) (result (ref $super-array)) + ;; NOMINAL-NEXT: (call $make-sub-array) + ;; NOMINAL-NEXT: ) (func $make-super-array (result (ref $super-array)) (call $make-sub-array) ) + ;; HYBRID: (func $make-sub-array (type $none_=>_ref|$sub-array|) (result (ref $sub-array)) + ;; HYBRID-NEXT: (unreachable) + ;; HYBRID-NEXT: ) + ;; NOMINAL: (func $make-sub-array (type $none_=>_ref|$sub-array|) (result (ref $sub-array)) + ;; NOMINAL-NEXT: (unreachable) + ;; NOMINAL-NEXT: ) (func $make-sub-array (result (ref $sub-array)) (unreachable) ) diff --git a/test/lit/isorecursive-output-ordering.wast b/test/lit/isorecursive-output-ordering.wast index ae965123f61..acca0496971 100644 --- a/test/lit/isorecursive-output-ordering.wast +++ b/test/lit/isorecursive-output-ordering.wast @@ -1,4 +1,4 @@ -;; TODO: Autogenerate these checks! The current script cannot handle `rec`. +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. ;; RUN: foreach %s %t wasm-opt -all --hybrid -S -o - | filecheck %s ;; RUN: foreach %s %t wasm-opt -all --hybrid --roundtrip -S -o - | filecheck %s @@ -6,26 +6,25 @@ (module ;; Test that we order groups by average uses. - ;; CHECK: (rec - ;; CHECK-NEXT: (type $unused-6 (struct_subtype data)) - ;; CHECK-NEXT: (type $used-a-bit (struct_subtype data)) - ;; CHECK-NEXT: ) - - ;; CHECK-NEXT: (rec - ;; CHECK-NEXT: (type $unused-1 (struct_subtype data)) - ;; CHECK-NEXT: (type $unused-2 (struct_subtype data)) - ;; CHECK-NEXT: (type $unused-3 (struct_subtype data)) - ;; CHECK-NEXT: (type $unused-4 (struct_subtype data)) - ;; CHECK-NEXT: (type $used-a-lot (struct_subtype data)) - ;; CHECK-NEXT: (type $unused-5 (struct_subtype data)) - ;; CHECK-NEXT: ) (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $unused-6 (struct_subtype data)) + + ;; CHECK: (type $used-a-bit (struct_subtype data)) + + ;; CHECK: (rec + ;; CHECK-NEXT: (type $unused-1 (struct_subtype data)) (type $unused-1 (struct_subtype data)) + ;; CHECK: (type $unused-2 (struct_subtype data)) (type $unused-2 (struct_subtype data)) + ;; CHECK: (type $unused-3 (struct_subtype data)) (type $unused-3 (struct_subtype data)) + ;; CHECK: (type $unused-4 (struct_subtype data)) (type $unused-4 (struct_subtype data)) + ;; CHECK: (type $used-a-lot (struct_subtype data)) (type $used-a-lot (struct_subtype data)) + ;; CHECK: (type $unused-5 (struct_subtype data)) (type $unused-5 (struct_subtype data)) ) @@ -34,6 +33,9 @@ (type $used-a-bit (struct_subtype data)) ) + ;; CHECK: (func $use (type $ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_=>_ref|$used-a-bit|_ref|$used-a-bit|_ref|$used-a-bit|_ref|$used-a-bit|) (param $0 (ref $used-a-lot)) (param $1 (ref $used-a-lot)) (param $2 (ref $used-a-lot)) (param $3 (ref $used-a-lot)) (param $4 (ref $used-a-lot)) (param $5 (ref $used-a-lot)) (result (ref $used-a-bit) (ref $used-a-bit) (ref $used-a-bit) (ref $used-a-bit)) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) (func $use (param (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot)) (result (ref $used-a-bit) (ref $used-a-bit) (ref $used-a-bit) (ref $used-a-bit)) (unreachable) ) @@ -42,33 +44,25 @@ (module ;; Test that we respect dependencies between groups before considering counts. - ;; CHECK: (rec - ;; CHECK-NEXT: (type $leaf (struct_subtype data)) - ;; CHECK-NEXT: (type $unused (struct_subtype data)) - ;; CHECK-NEXT: ) - - ;; CHECK-NEXT: (rec - ;; CHECK-NEXT: (type $shrub (struct_subtype $leaf)) - ;; CHECK-NEXT: (type $used-a-ton (struct_subtype data)) - ;; CHECK-NEXT: ) - - ;; CHECK-NEXT: (rec - ;; CHECK-NEXT: (type $twig (struct_subtype data)) - ;; CHECK-NEXT: (type $used-a-bit (struct_subtype (field (ref $leaf)) data)) - ;; CHECK-NEXT: ) - - ;; CHECK-NEXT: (rec - ;; CHECK-NEXT: (type $root (struct_subtype data)) - ;; CHECK-NEXT: (type $used-a-lot (struct_subtype $twig)) - ;; CHECK-NEXT: ) (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $leaf (struct_subtype data)) (type $leaf (struct_subtype data)) + ;; CHECK: (type $unused (struct_subtype data)) (type $unused (struct_subtype data)) ) (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $shrub (struct_subtype $leaf)) + + ;; CHECK: (type $used-a-ton (struct_subtype data)) + + ;; CHECK: (rec + ;; CHECK-NEXT: (type $twig (struct_subtype data)) (type $twig (struct_subtype data)) + ;; CHECK: (type $used-a-bit (struct_subtype (field (ref $leaf)) data)) (type $used-a-bit (struct_subtype (ref $leaf) data)) ) @@ -78,10 +72,23 @@ ) (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $root (struct_subtype data)) (type $root (struct_subtype data)) + ;; CHECK: (type $used-a-lot (struct_subtype $twig)) (type $used-a-lot (struct_subtype $twig)) ) + ;; CHECK: (func $use (type $ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_ref|$used-a-lot|_=>_ref|$used-a-bit|_ref|$used-a-bit|_ref|$used-a-bit|) (param $0 (ref $used-a-lot)) (param $1 (ref $used-a-lot)) (param $2 (ref $used-a-lot)) (param $3 (ref $used-a-lot)) (param $4 (ref $used-a-lot)) (param $5 (ref $used-a-lot)) (result (ref $used-a-bit) (ref $used-a-bit) (ref $used-a-bit)) + ;; CHECK-NEXT: (local $6 (ref null $used-a-ton)) + ;; CHECK-NEXT: (local $7 (ref null $used-a-ton)) + ;; CHECK-NEXT: (local $8 (ref null $used-a-ton)) + ;; CHECK-NEXT: (local $9 (ref null $used-a-ton)) + ;; CHECK-NEXT: (local $10 (ref null $used-a-ton)) + ;; CHECK-NEXT: (local $11 (ref null $used-a-ton)) + ;; CHECK-NEXT: (local $12 (ref null $used-a-ton)) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) (func $use (param (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot) (ref $used-a-lot)) (result (ref $used-a-bit) (ref $used-a-bit) (ref $used-a-bit)) (local (ref null $used-a-ton) (ref null $used-a-ton) (ref null $used-a-ton) (ref null $used-a-ton) (ref null $used-a-ton) (ref null $used-a-ton) (ref null $used-a-ton)) (unreachable) @@ -92,9 +99,13 @@ ;; Test that basic heap type children do not trigger assertions. (rec + ;; CHECK: (type $contains-basic (struct_subtype (field (ref any)) data)) (type $contains-basic (struct_subtype (ref any) data)) ) + ;; CHECK: (func $use (type $ref|$contains-basic|_=>_none) (param $0 (ref $contains-basic)) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) (func $use (param (ref $contains-basic)) (unreachable) ) diff --git a/test/lit/isorecursive-singleton-group.wast b/test/lit/isorecursive-singleton-group.wast index eeb92ac0993..c36717376a9 100644 --- a/test/lit/isorecursive-singleton-group.wast +++ b/test/lit/isorecursive-singleton-group.wast @@ -1,4 +1,4 @@ -;; TODO: Autogenerate these checks! The current script cannot handle `rec`. +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. ;; RUN: wasm-opt %s -all --hybrid -S -o - | filecheck %s ;; RUN: wasm-opt %s -all --hybrid --roundtrip -S -o - | filecheck %s @@ -8,13 +8,13 @@ (module -;; CHECK-NOT: rec -;; CHECK: (type $singleton (struct_subtype data)) (rec + ;; CHECK: (type $singleton (struct_subtype data)) (type $singleton (struct_subtype data)) ) ;; Use the type so it appears in the output. + ;; CHECK: (global $g (ref null $singleton) (ref.null $singleton)) (global $g (ref null $singleton) (ref.null $singleton)) ) diff --git a/test/lit/isorecursive-whole-group.wast b/test/lit/isorecursive-whole-group.wast index 4d349a3cb63..f2a60deda36 100644 --- a/test/lit/isorecursive-whole-group.wast +++ b/test/lit/isorecursive-whole-group.wast @@ -1,4 +1,4 @@ -;; TODO: Autogenerate these checks! The current script cannot handle `rec`. +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. ;; RUN: wasm-opt %s -all --hybrid -S -o - | filecheck %s ;; RUN: wasm-opt %s -all --hybrid --roundtrip -S -o - | filecheck %s @@ -8,15 +8,15 @@ (module -;; CHECK: (rec -;; CHECK-NEXT: (type $used (struct_subtype data)) -;; CHECK-NEXT: (type $unused (struct_subtype data)) -;; CHECK-NEXT: ) (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $used (struct_subtype data)) (type $used (struct_subtype data)) + ;; CHECK: (type $unused (struct_subtype data)) (type $unused (struct_subtype data)) ) + ;; CHECK: (global $g (ref null $used) (ref.null $used)) (global $g (ref null $used) (ref.null $used)) ) diff --git a/test/lit/nominal-to-isorecursive.wast b/test/lit/nominal-to-isorecursive.wast index c1deadb546e..beb0aaee9ad 100644 --- a/test/lit/nominal-to-isorecursive.wast +++ b/test/lit/nominal-to-isorecursive.wast @@ -1,4 +1,4 @@ -;; TODO: Autogenerate these checks! The current script cannot handle `rec`. +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. ;; RUN: wasm-as %s -all --nominal -g -o %t.wasm ;; RUN: wasm-dis %t.wasm -all --hybrid -o - | filecheck %s @@ -6,30 +6,29 @@ ;; Check that the nominal binary format is parseable as isorecursive with a ;; single recursion group. -;; CHECK: (module -;; CHECK-NEXT: (rec -;; CHECK-NEXT: (type $make-super-t (func_subtype (result (ref $super)) func)) -;; CHECK-NEXT: (type $make-sub-t (func_subtype (result (ref $sub)) func)) -;; CHECK-NEXT: (type $super (struct_subtype (field i32) data)) -;; CHECK-NEXT: (type $sub (struct_subtype (field i32) $super)) -;; CHECK-NEXT: ) -;; CHECK-NEXT: (func $make-super (type $make-super-t) (result (ref $super)) -;; CHECK-NEXT: (unreachable) -;; CHECK-NEXT: ) -;; CHECK-NEXT: (func $make-sub (type $make-sub-t) (result (ref $sub)) -;; CHECK-NEXT: (unreachable) -;; CHECK-NEXT: ) -;; CHECK-NEXT: ) (module + ;; CHECK: (rec + ;; CHECK-NEXT: (type $make-super-t (func_subtype (result (ref $super)) func)) + + ;; CHECK: (type $make-sub-t (func_subtype (result (ref $sub)) func)) + + ;; CHECK: (type $super (struct_subtype (field i32) data)) (type $super (struct i32)) + ;; CHECK: (type $sub (struct_subtype (field i32) $super)) (type $sub (struct_subtype i32 $super)) (type $make-super-t (func (result (ref $super)))) (type $make-sub-t (func (result (ref $sub)))) + ;; CHECK: (func $make-super (type $make-super-t) (result (ref $super)) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) (func $make-super (type $make-super-t) (unreachable) ) + ;; CHECK: (func $make-sub (type $make-sub-t) (result (ref $sub)) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) (func $make-sub (type $make-sub-t) (unreachable) ) diff --git a/test/lit/wat-kitchen-sink.wast b/test/lit/wat-kitchen-sink.wast index 315cb172c95..9ec1b7c5dfc 100644 --- a/test/lit/wat-kitchen-sink.wast +++ b/test/lit/wat-kitchen-sink.wast @@ -4,16 +4,24 @@ (module $parse ;; types - ;; CHECK: (type $s0 (struct_subtype data)) - - ;; CHECK: (type $void (func_subtype func)) - (type $void (func)) + (rec + ;; CHECK: (type $void (func_subtype func)) + (type $void (func)) + ) ;; CHECK: (type $many (func_subtype (param i32 i64 f32 f64) (result anyref (ref func)) func)) (type $many (func (param $x i32) (param i64 f32) (param) (param $y f64) (result anyref (ref func)))) - (type $s0 (sub (struct))) - (type $s1 (struct (field))) + (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $s0 (struct_subtype data)) + (type $s0 (sub (struct))) + ;; CHECK: (type $s1 (struct_subtype data)) + (type $s1 (struct (field))) + ) + + (rec) + ;; CHECK: (type $s2 (struct_subtype (field i32) data)) (type $s2 (struct i32)) ;; CHECK: (type $s3 (struct_subtype (field i64) data)) @@ -78,7 +86,7 @@ ;; CHECK: (import "mod" "s0" (global $s0 (mut (ref $s0)))) -;; CHECK: (import "mod" "s1" (global $s1 (mut (ref $s0)))) +;; CHECK: (import "mod" "s1" (global $s1 (mut (ref $s1)))) ;; CHECK: (import "mod" "s2" (global $s2 (mut (ref $s2))))