Skip to content
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
1 change: 1 addition & 0 deletions src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {

// The branch label name for this scope. Always fresh, never shadowed.
Name label;
bool labelUsed = false;

std::vector<Expression*> exprStack;
// Whether we have seen an unreachable instruction and are in
Expand Down
12 changes: 10 additions & 2 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,17 @@ Result<> IRBuilder::visitEnd() {
CHECK_ERR(expr);

// If the scope expression cannot be directly labeled, we may need to wrap it
// in a block.
// in a block. It's possible that the scope expression becomes typed
// unreachable when it is finalized, but if the wrapper block is targeted by
// any branches, the target block needs to have the original non-unreachable
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does the target block need to have the original non-unreachable type of the scope expression?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Talked offline.

// type of the scope expression.
auto originalScopeType = scope.getResultType();
auto maybeWrapForLabel = [&](Expression* curr) -> Expression* {
if (scope.label) {
return builder.makeBlock(scope.label, {curr}, scope.getResultType());
return builder.makeBlock(scope.label,
{curr},
scope.labelUsed ? originalScopeType
: scope.getResultType());
}
return curr;
};
Expand Down Expand Up @@ -624,6 +631,7 @@ Result<Name> IRBuilder::getLabelName(Index label) {
// The scope does not already have a name, so we need to create one.
scopeLabel = makeFresh("label");
}
(*scope)->labelUsed = true;
return scopeLabel;
}

Expand Down
44 changes: 43 additions & 1 deletion test/lit/wat-kitchen-sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,48 @@
end
)

;; CHECK: (func $if-else-brs (type $void)
;; CHECK-NEXT: (block $label
;; CHECK-NEXT: (if
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (br $label)
;; CHECK-NEXT: (br $label)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $if-else-brs
i32.const 0
if
br 0
else
br 0
end
)

;; CHECK: (func $if-else-brs-i32 (type $1) (result i32)
;; CHECK-NEXT: (block $label (result i32)
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (br $label
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $label
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $if-else-brs-i32 (result i32)
i32.const 0
if (result i32)
i32.const 1
br 0
else
i32.const 2
br 0
end
)

;; CHECK: (func $loop (type $void)
;; CHECK-NEXT: (loop
;; CHECK-NEXT: (nop)
Expand Down Expand Up @@ -2332,7 +2374,7 @@
(func $ref-func
ref.func $ref-func
drop
ref.func 107
ref.func 109
drop
)

Expand Down