Skip to content

[Parser] Parse rec groups #4785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/update_lit_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<name>\S*)$')
Expand Down
50 changes: 42 additions & 8 deletions src/wasm/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ struct ParseDeclsCtx {

// The module element definitions we are parsing in this phase.
std::vector<DefPos> typeDefs;
std::vector<DefPos> subtypeDefs;
std::vector<DefPos> globalDefs;

// Counters used for generating names for module elements.
Expand Down Expand Up @@ -350,7 +351,7 @@ struct ParseTypeDefsCtx {
// Parse the names of types and fields as we go.
std::vector<TypeNames> 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)
Expand Down Expand Up @@ -930,7 +931,7 @@ template<typename Ctx> Result<> strtype(Ctx& ctx, ParseInput& in) {
// subtype ::= '(' 'type' id? '(' 'sub' typeidx? strtype ')' ')'
// | '(' 'type' id? strtype ')'
template<typename Ctx> MaybeResult<> subtype(Ctx& ctx, ParseInput& in) {
[[maybe_unused]] auto start = in.getPos();
[[maybe_unused]] auto pos = in.getPos();

if (!in.takeSExprStart("type"sv)) {
return {};
Expand Down Expand Up @@ -969,7 +970,10 @@ template<typename Ctx> MaybeResult<> subtype(Ctx& ctx, ParseInput& in) {
}

if constexpr (parsingDecls<Ctx>) {
ctx.typeDefs.push_back({name, start});
ctx.subtypeDefs.push_back({name, pos});
}
if constexpr (parsingTypeDefs<Ctx>) {
++ctx.index;
}

return Ok{};
Expand All @@ -978,8 +982,35 @@ template<typename Ctx> MaybeResult<> subtype(Ctx& ctx, ParseInput& in) {
// deftype ::= '(' 'rec' subtype* ')'
// | subtype
template<typename Ctx> 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<Ctx>) {
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>) {
ctx.builder.createRecGroup(startIndex, groupLen);
}
} else if (auto type = subtype(ctx, in)) {
CHECK_ERR(type);
} else {
return {};
}

if constexpr (parsingDecls<Ctx>) {
ctx.typeDefs.push_back({{}, pos});
}

return Ok{};
}

// global ::= '(' 'global' id? ('(' 'export' name ')')* gt:globaltype e:expr ')'
Expand Down Expand Up @@ -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<HeapType> 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;
Expand Down
53 changes: 35 additions & 18 deletions test/lit/isorecursive-good.wast
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
;; 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
;; RUN: wasm-opt %s -all --nominal -S -o - | filecheck %s --check-prefix NOMINAL

(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)
)
Expand Down
77 changes: 44 additions & 33 deletions test/lit/isorecursive-output-ordering.wast
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
;; 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

(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))
)

Expand All @@ -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)
)
Expand All @@ -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))
)

Expand All @@ -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)
Expand All @@ -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)
)
Expand Down
6 changes: 3 additions & 3 deletions test/lit/isorecursive-singleton-group.wast
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
)
10 changes: 5 additions & 5 deletions test/lit/isorecursive-whole-group.wast
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
)
Loading