Skip to content

Commit 2e26ec6

Browse files
authored
[Parser] Fix Table64 parsing (#7298)
We had some hardcoded i32 indexes, but the table might be table64. Also fix some IRBuilder calls of ChildTyper, that now need to know the table name.
1 parent d9fa84a commit 2e26ec6

3 files changed

Lines changed: 64 additions & 13 deletions

File tree

src/ir/child-typer.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
8181
note(ptrp, wasm.getMemory(mem)->addressType);
8282
}
8383

84+
void noteTableIndex(Expression** indexp, Name table) {
85+
note(indexp, wasm.getTable(table)->addressType);
86+
}
87+
8488
void noteAny(Expression** childp) { self().noteAnyType(childp); }
8589

8690
void noteAnyReference(Expression** childp) {
@@ -746,35 +750,43 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
746750
note(&curr->right, eqref);
747751
}
748752

749-
void visitTableGet(TableGet* curr) { note(&curr->index, Type::i32); }
753+
void visitTableGet(TableGet* curr) {
754+
noteTableIndex(&curr->index, curr->table);
755+
}
750756

751757
void visitTableSet(TableSet* curr) {
752-
note(&curr->index, Type::i32);
758+
noteTableIndex(&curr->index, curr->table);
753759
note(&curr->value, wasm.getTable(curr->table)->type);
754760
}
755761

756762
void visitTableSize(TableSize* curr) {}
757763

758764
void visitTableGrow(TableGrow* curr) {
759765
note(&curr->value, wasm.getTable(curr->table)->type);
760-
note(&curr->delta, Type::i32);
766+
noteTableIndex(&curr->delta, curr->table);
761767
}
762768

763769
void visitTableFill(TableFill* curr) {
764770
auto type = wasm.getTable(curr->table)->type;
765-
note(&curr->dest, Type::i32);
771+
noteTableIndex(&curr->dest, curr->table);
766772
note(&curr->value, type);
767-
note(&curr->size, Type::i32);
773+
noteTableIndex(&curr->size, curr->table);
768774
}
769775

770776
void visitTableCopy(TableCopy* curr) {
771-
note(&curr->dest, Type::i32);
772-
note(&curr->source, Type::i32);
773-
note(&curr->size, Type::i32);
777+
noteTableIndex(&curr->dest, curr->destTable);
778+
noteTableIndex(&curr->source, curr->sourceTable);
779+
780+
// The size depends on both dest and source.
781+
auto* sourceTable = wasm.getTable(curr->sourceTable);
782+
auto* destTable = wasm.getTable(curr->destTable);
783+
Type sizeType =
784+
sourceTable->is64() && destTable->is64() ? Type::i64 : Type::i32;
785+
note(&curr->size, sizeType);
774786
}
775787

776788
void visitTableInit(TableInit* curr) {
777-
note(&curr->dest, wasm.getTable(curr->table)->addressType);
789+
noteTableIndex(&curr->dest, curr->table);
778790
note(&curr->offset, Type::i32);
779791
note(&curr->size, Type::i32);
780792
}

src/wasm/wasm-ir-builder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,7 @@ Result<> IRBuilder::makeRefEq() {
17691769

17701770
Result<> IRBuilder::makeTableGet(Name table) {
17711771
TableGet curr;
1772+
curr.table = table;
17721773
CHECK_ERR(visitTableGet(&curr));
17731774
auto type = wasm.getTable(table)->type;
17741775
push(builder.makeTableGet(table, curr.index, type));
@@ -1806,6 +1807,8 @@ Result<> IRBuilder::makeTableFill(Name table) {
18061807

18071808
Result<> IRBuilder::makeTableCopy(Name destTable, Name srcTable) {
18081809
TableCopy curr;
1810+
curr.destTable = destTable;
1811+
curr.sourceTable = srcTable;
18091812
CHECK_ERR(visitTableCopy(&curr));
18101813
push(builder.makeTableCopy(
18111814
curr.dest, curr.source, curr.size, destTable, srcTable));
Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
1-
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
2-
;; RUN: wasm-opt %s -all --roundtrip -S -o -
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
;; RUN: wasm-opt %s -all --roundtrip -S -o - | filecheck %s
33

44
(module
5+
;; CHECK: (type $functype (func))
56
(type $functype (func))
6-
(table $0 48 funcref)
7+
8+
;; CHECK: (table $table 48 funcref)
9+
(table $table 48 funcref)
10+
;; CHECK: (table $table64 i64 96 funcref)
11+
(table $table64 i64 96 funcref)
12+
713
;; a type that appears in the table and nowhere else. this test checks that
814
;; we do not crash during the roundtrip on seeing an unexpected type that
915
;; collectHeapTypes() did not scan.
10-
(elem (table $0) (i32.const 0) funcref (ref.null $functype))
16+
(elem (table $table) (i32.const 0) funcref (ref.null $functype))
17+
18+
;; CHECK: (elem $0 (table $table) (i32.const 0) funcref (item (ref.null nofunc)))
19+
20+
;; CHECK: (func $set (type $functype)
21+
;; CHECK-NEXT: (drop
22+
;; CHECK-NEXT: (i64.const 0)
23+
;; CHECK-NEXT: )
24+
;; CHECK-NEXT: (drop
25+
;; CHECK-NEXT: (i32.const 0)
26+
;; CHECK-NEXT: )
27+
;; CHECK-NEXT: (table.set $table64
28+
;; CHECK-NEXT: (unreachable)
29+
;; CHECK-NEXT: (unreachable)
30+
;; CHECK-NEXT: )
31+
;; CHECK-NEXT: )
32+
(func $set
33+
(table.set $table64
34+
(i64.const 0)
35+
(block (result funcref)
36+
;; This unreachable code must not confuse the parser. Specifically the
37+
;; index should not be replaced with the i32, which would not validate.
38+
(i32.lt_u
39+
(i32.const 0)
40+
(unreachable)
41+
)
42+
(ref.null nofunc)
43+
)
44+
)
45+
)
1146
)
47+

0 commit comments

Comments
 (0)