Skip to content

Fix handling of declarative segments #7660

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion scripts/test/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ def get_tests(test_dir, extensions=[], recursive=False):
'const.wast', # Hex float constant not recognized as out of range
'conversions.wast', # Promoted NaN should be canonical
'data.wast', # Constant global references allowed by GC
'elem.wast', # Requires modeling empty declarative segments
'f32.wast', # Adding -0 and -nan should give a canonical NaN
'f64.wast', # Adding -0 and -nan should give a canonical NaN
'float_exprs.wast', # Adding 0 and NaN should give canonical NaN
Expand Down
13 changes: 13 additions & 0 deletions src/parser/context-decls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ Result<> ParseDeclsCtx::addElem(
return Ok{};
}

Result<> ParseDeclsCtx::addDeclareElem(Name name, ElemListT&&, Index) {
auto e = std::make_unique<ElementSegment>();
if (name) {
e->setExplicitName(name);
} else {
name = std::to_string(elemCounter++);
name = Names::getValidElementSegmentName(wasm, name);
e->name = name;
}
wasm.addElementSegment(std::move(e));
return Ok{};
}

Result<> ParseDeclsCtx::addData(Name name,
MemoryIdxT*,
std::optional<ExprT>,
Expand Down
2 changes: 1 addition & 1 deletion src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {

Result<> addElem(Name, TableIdxT*, std::optional<ExprT>, ElemListT&&, Index);

Result<> addDeclareElem(Name, ElemListT&&, Index) { return Ok{}; }
Result<> addDeclareElem(Name, ElemListT&&, Index);

Result<> addData(Name name,
MemoryIdxT*,
Expand Down
6 changes: 5 additions & 1 deletion test/lit/basic/multi-table.wast
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
;; CHECK-TEXT: (elem $empty func)
;; CHECK-BIN: (elem $empty func)
(elem $empty func)
;; CHECK-TEXT: (elem $declarative func)
;; CHECK-BIN: (elem $declarative func)
(elem $declarative declare func $h)

;; This elem will be emitted as usesExpressions because of the type of the
Expand Down Expand Up @@ -142,7 +144,9 @@

;; CHECK-BIN-NODEBUG: (elem $8 func)

;; CHECK-BIN-NODEBUG: (elem $9 (table $3) (i32.const 0) (ref null $0) (item (ref.func $0)) (item (ref.func $2)))
;; CHECK-BIN-NODEBUG: (elem $9 func)

;; CHECK-BIN-NODEBUG: (elem $10 (table $3) (i32.const 0) (ref null $0) (item (ref.func $0)) (item (ref.func $2)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if the binary writer searched for an empty segment with an appropriate type to reuse as the declarative segment to avoid having round trips increase the number of segments like this.


;; CHECK-BIN-NODEBUG: (func $0 (type $0)
;; CHECK-BIN-NODEBUG-NEXT: (drop
Expand Down
Loading