Skip to content

Commit

Permalink
[Parser] Improve parsed IR for multivalue returns (WebAssembly#6378)
Browse files Browse the repository at this point in the history
Rather than reassembling a tuple from multiple pops, let the pop implementation
assemble the tuple. This produces less code in cases where there is already a
tuple of the proper size on top of the stack. It also simplifies the code.
  • Loading branch information
tlively committed Mar 5, 2024
1 parent d1a7d4b commit 71bf4b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,10 @@ Result<> IRBuilder::visitReturn(Return* curr) {
size_t n = func->getResults().size();
if (n == 0) {
curr->value = nullptr;
} else if (n == 1) {
auto val = pop();
} else {
auto val = pop(n);
CHECK_ERR(val);
curr->value = *val;
} else {
std::vector<Expression*> vals(n);
for (size_t i = 0; i < n; ++i) {
auto val = pop();
CHECK_ERR(val);
vals[n - i - 1] = *val;
}
curr->value = builder.makeTupleMake(vals);
}
return Ok{};
}
Expand Down
12 changes: 11 additions & 1 deletion test/lit/wat-kitchen-sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -3662,6 +3662,16 @@
return
)

;; CHECK: (func $return-multivalue (type $4) (result i32 i64)
;; CHECK-NEXT: (return
;; CHECK-NEXT: (call $return-multivalue)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $return-multivalue (result i32 i64)
call $return-multivalue
return
)

;; CHECK: (func $ref-is-null (type $45) (param $0 anyref) (result i32)
;; CHECK-NEXT: (ref.is_null
;; CHECK-NEXT: (local.get $0)
Expand All @@ -3683,7 +3693,7 @@
(func $ref-func
ref.func $ref-func
drop
ref.func 156
ref.func 157
drop
)

Expand Down

0 comments on commit 71bf4b3

Please sign in to comment.