Skip to content

[WebAssembly] Allow try_table to target loops in AsmTypeCheck #111432

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

Merged
merged 3 commits into from
Oct 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,16 @@ bool WebAssemblyAsmTypeCheck::checkTryTable(SMLoc ErrorLoc,
if (Level < BlockInfoStack.size()) {
const auto &DestBlockInfo =
BlockInfoStack[BlockInfoStack.size() - Level - 1];
if (compareTypes(SentTypes, DestBlockInfo.Sig.Returns)) {
ArrayRef<wasm::ValType> DestTypes;
if (DestBlockInfo.IsLoop)
DestTypes = DestBlockInfo.Sig.Params;
else
DestTypes = DestBlockInfo.Sig.Returns;
if (compareTypes(SentTypes, DestTypes)) {
std::string ErrorMsg =
ErrorMsgBase + "type mismatch, catch tag type is " +
getTypesString(SentTypes) + ", but destination's type is " +
getTypesString(DestBlockInfo.Sig.Returns);
getTypesString(DestTypes);
Error |= typeError(ErrorLoc, ErrorMsg);
}
} else {
Expand Down
27 changes: 24 additions & 3 deletions llvm/test/MC/WebAssembly/annotations.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.section .text.test_annotation,"",@
.type test_annotation,@function
test_annotation:
.functype test_annotation () -> ()
.functype test_annotation (exnref) -> ()
.tagtype __cpp_exception i32
.tagtype __c_longjmp i32
try
Expand Down Expand Up @@ -54,8 +54,18 @@ test_annotation:
return
end_block
drop
end_function

i32.const 0
loop (i32) -> ()
local.get 0
loop (exnref) -> ()
try_table (catch __cpp_exception 1) (catch_all_ref 0)
end_try_table
drop
end_loop
drop
end_loop
end_function

# CHECK: test_annotation:
# CHECK: try
Expand Down Expand Up @@ -105,5 +115,16 @@ test_annotation:
# CHECK-NEXT: return
# CHECK-NEXT: end_block # label7:
# CHECK-NEXT: drop
# CHECK-NEXT: end_function

# CHECK: i32.const 0
# CHECK-NEXT: loop (i32) -> () # label12:
# CHECK-NEXT: local.get 0
# CHECK-NEXT: loop (exnref) -> () # label13:
# CHECK-NEXT: try_table (catch __cpp_exception 1) (catch_all_ref 0) # 1: up to label12
# CHECK-NEXT: # 0: up to label13
# CHECK-NEXT: end_try_table # label14:
# CHECK-NEXT: drop
# CHECK-NEXT: end_loop
# CHECK-NEXT: drop
# CHECK-NEXT: end_loop
# CHECK-NEXT: end_function
25 changes: 24 additions & 1 deletion llvm/test/MC/WebAssembly/eh-assembly.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.functype foo () -> ()

eh_test:
.functype eh_test () -> ()
.functype eh_test (exnref) -> ()

# try_table with all four kinds of catch clauses
block exnref
Expand Down Expand Up @@ -82,6 +82,18 @@ eh_test:
end_try_table
drop
drop

# try_table targeting loops
i32.const 0
loop (i32) -> ()
local.get 0
loop (exnref) -> ()
try_table (catch __cpp_exception 1) (catch_all_ref 0)
end_try_table
drop
end_loop
drop
end_loop
end_function

eh_legacy_test:
Expand Down Expand Up @@ -203,6 +215,17 @@ eh_legacy_test:
# CHECK-NEXT: drop
# CHECK-NEXT: drop

# CHECK: i32.const 0
# CHECK-NEXT: loop (i32) -> ()
# CHECK-NEXT: local.get 0
# CHECK-NEXT: loop (exnref) -> ()
# CHECK-NEXT: try_table (catch __cpp_exception 1) (catch_all_ref 0)
# CHECK: end_try_table
# CHECK-NEXT: drop
# CHECK-NEXT: end_loop
# CHECK-NEXT: drop
# CHECK-NEXT: end_loop

# CHECK: eh_legacy_test:
# CHECK: try
# CHECK-NEXT: i32.const 3
Expand Down
17 changes: 17 additions & 0 deletions llvm/test/MC/WebAssembly/type-checker-errors.s
Original file line number Diff line number Diff line change
Expand Up @@ -966,4 +966,21 @@ eh_test:
end_block
end_block
drop

loop
i32.const 0
loop (i32) -> ()
loop (i32) -> ()
loop
# CHECK: :[[@LINE+4]]:11: error: try_table: catch index 0: type mismatch, catch tag type is [i32], but destination's type is []
# CHECK: :[[@LINE+3]]:11: error: try_table: catch index 1: type mismatch, catch tag type is [i32, exnref], but destination's type is [i32]
# CHECK: :[[@LINE+2]]:11: error: try_table: catch index 2: type mismatch, catch tag type is [], but destination's type is [i32]
# CHECK: :[[@LINE+1]]:11: error: try_table: catch index 3: type mismatch, catch tag type is [exnref], but destination's type is []
try_table (catch __cpp_exception 0) (catch_ref __cpp_exception 1) (catch_all 2) (catch_all_ref 3)
end_try_table
end_loop
drop
end_loop
end_loop
end_loop
end_function
Loading