[PATCH] D147852: [WebAssembly] Handle unreachable in block-like structure in type checker

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 8 07:17:53 PDT 2023


aheejin created this revision.
aheejin added reviewers: sbc100, dschuff, aardappel.
Herald added subscribers: pmatos, asb, wingo, ecnelises, sunfish, hiraditya, jgravelle-google.
Herald added a project: All.
aheejin requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We disable type check in unreachable code, but when the unreachable code
is enclosed within a block-like structure, the block as a whole has a
valid type and we should continue type checking after the block. But it
looks we currently only do that for blocks and not other block-like
structures (loops, trys, and ifs). This CL fixes it and add tests for
them.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147852

Files:
  llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
  llvm/test/MC/WebAssembly/type-checker-errors.s


Index: llvm/test/MC/WebAssembly/type-checker-errors.s
===================================================================
--- llvm/test/MC/WebAssembly/type-checker-errors.s
+++ llvm/test/MC/WebAssembly/type-checker-errors.s
@@ -513,3 +513,44 @@
   f32.add
 # CHECK: :[[@LINE+1]]:3: error: 1 superfluous return values
   end_function
+
+unreachable_within_block:
+  .functype unreachable_within_block () -> ()
+  block
+  unreachable
+  end_block
+# CHECK: :[[@LINE+1]]:3: error: empty stack while popping value
+  drop
+  end_function
+
+unreachable_within_loop:
+  .functype unreachable_within_loop () -> ()
+  loop
+  unreachable
+  end_loop
+# CHECK: :[[@LINE+1]]:3: error: empty stack while popping value
+  drop
+  end_function
+
+unreachable_within_if:
+  .functype unreachable_within_if () -> ()
+  i32.const 0
+  if
+  unreachable
+  else
+  unreachable
+  end_if
+# CHECK: :[[@LINE+1]]:3: error: empty stack while popping value
+  drop
+  end_function
+
+unreachable_within_try:
+  .functype unreachable_within_try () -> ()
+  try
+  unreachable
+  catch_all
+  unreachable
+  end_try
+# CHECK: :[[@LINE+1]]:3: error: empty stack while popping value
+  drop
+  end_function
Index: llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
+++ llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
@@ -303,7 +303,7 @@
              Name == "else" || Name == "end_try") {
     if (checkEnd(ErrorLoc, Name == "else"))
       return true;
-    if (Name == "end_block")
+    if (Name.startswith("end_"))
       Unreachable = false;
   } else if (Name == "return") {
     if (endOfFunction(ErrorLoc))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147852.511888.patch
Type: text/x-patch
Size: 1762 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230408/6ce28893/attachment.bin>


More information about the llvm-commits mailing list