Skip to content

Commit

Permalink
[Wasm Exceptions] Fix binary parsing of a normal break to a try in a …
Browse files Browse the repository at this point in the history
…singleton (#3581)

The fix here is to remove the code with

// maybe we don't need a block here?

That would remove a try's block if we thought it wasn't needed. However,
it just checked for exception branches, but not normal branches, which are
also possible.

At that location, we don't have a good way to find out if the block has other
branches to it aside from scanning its contents. So this PR just gives up on
doing so, which means we add an unnecessary block if the optimizer is not
run. If this matters we could make the binary parser more complicated by
remembering whether a block had branches in the past, but I'm not sure if
it's worth it.
  • Loading branch information
kripken authored Feb 19, 2021
1 parent b6c094c commit e24c1f0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 27 deletions.
4 changes: 0 additions & 4 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5846,10 +5846,6 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
BranchUtils::replaceExceptionTargets(block, block->name, curr->name);
exceptionTargetNames.erase(block->name);
}
// maybe we don't need a block here?
if (block->list.size() == 1) {
curr->body = block->list[0];
}
}
}
if (exceptionTargetNames.find(catchLabel) != exceptionTargetNames.end()) {
Expand Down
Binary file added test/br_to_try.wasm
Binary file not shown.
20 changes: 20 additions & 0 deletions test/br_to_try.wasm.fromBinary
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(module
(type $none_=>_none (func))
(type $i32_=>_none (func (param i32)))
(event $event$0 (attr 0) (param i32))
(func $0
(try $label$3
(do
(block $label$1
(br $label$1)
)
)
(catch $event$0
(drop
(pop i32)
)
)
)
)
)

10 changes: 6 additions & 4 deletions test/exception-handling.wast.fromBinary
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@
)
(try $label$25
(do
(try $label$23
(do
(call $foo)
(block $label$20
(try $label$23
(do
(call $foo)
)
(delegate $label$25)
)
(delegate $label$25)
)
)
(delegate 0)
Expand Down
10 changes: 6 additions & 4 deletions test/exception-handling.wast.fromBinary.noDebugInfo
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@
)
(try $label$25
(do
(try $label$23
(do
(call $0)
(block $label$20
(try $label$23
(do
(call $0)
)
(delegate $label$25)
)
(delegate $label$25)
)
)
(delegate 0)
Expand Down
10 changes: 7 additions & 3 deletions test/passes/dwarf_with_exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ void dwarf_with_exceptions() {
}
}
// How to generate dwarf_with_exceptions.wasm:
// $ clang++ -std=c++14 --target=wasm32-unknown-unknown -g -fwasm-exceptions -Xclang -disable-O0-optnone -c -S -emit-llvm dwarf_with_exceptions.cpp -o temp.ll
// $ clang++ -std=c++14 --target=wasm32-unknown-unknown -g -fwasm-exceptions \
// -Xclang -disable-O0-optnone -c -S -emit-llvm
// dwarf_with_exceptions.cpp -o temp.ll
// $ opt -S -mem2reg -simplifycfg temp.ll -o dwarf_with_exceptions.ll
// Remove some personal info from dwarf_with_exceptions.ll
// $ llc -exception-model=wasm -mattr=+exception-handling -filetype=obj dwarf_with_exceptions.ll -o dwarf_with_exceptions.o
// $ wasm-ld --no-entry --no-gc-sections --allow-undefined dwarf_with_exceptions.o -o dwarf_with_exceptions.wasm
// $ llc -exception-model=wasm -mattr=+exception-handling -filetype=obj \
// dwarf_with_exceptions.ll -o dwarf_with_exceptions.o
// $ wasm-ld --no-entry --no-gc-sections --allow-undefined \
// dwarf_with_exceptions.o -o dwarf_with_exceptions.wasm
28 changes: 16 additions & 12 deletions test/try-delegate.wasm.fromBinary
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
(func $0
(try $label$6
(do
(try $label$4
(do
(block $label$1
(try $label$4
(do
)
(delegate $label$6)
)
(delegate $label$6)
)
)
(catch $event$0
Expand All @@ -17,17 +19,19 @@
(func $1
(try $label$9
(do
(try $label$7
(do
)
(catch $event$0
(drop
(i32.const 0)
(block $label$1
(try $label$7
(do
)
(try $label$6
(do
(catch $event$0
(drop
(i32.const 0)
)
(try $label$6
(do
)
(delegate $label$9)
)
(delegate $label$9)
)
)
)
Expand Down

0 comments on commit e24c1f0

Please sign in to comment.