[PATCH] D96532: [WebAssemblly] Fix rethrow's argument computation

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 11 11:39:57 PST 2021


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

Previously we assumed `rethrow`'s argument was always 0, but it turned
out `rethrow` follows the same rule with `br` or `delegate`:
https://github.com/WebAssembly/exception-handling/pull/137
https://github.com/WebAssembly/exception-handling/issues/146#issuecomment-777349038

Currently `rethrow`s generated by our backend always rethrow the
exception caught by the innermost enclosing catch, so this adds a
function to compute that and replaces `rethrow`'s argument with its
computed result.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96532

Files:
  llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td
  llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll


Index: llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
+++ llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
@@ -128,7 +128,7 @@
 ; CHECK:         br        2                           # 2: down to label[[L1]]
 ; CHECK:       end_try
 ; CHECK:     end_block                                 # label[[L0]]:
-; CHECK:     rethrow   0                               # to caller
+; CHECK:     rethrow   1                               # to caller
 ; CHECK:   end_block                                   # label[[L1]]:
 ; CHECK:   call      __cxa_end_catch
 ; CHECK: end_try
Index: llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td
+++ llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td
@@ -133,8 +133,8 @@
                "throw   \t$tag", "throw   \t$tag", 0x08>;
 defm RETHROW : NRI<(outs), (ins i32imm:$depth), [], "rethrow \t$depth", 0x09>;
 } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
-// For C++ support, we only rethrow the latest exception, thus always setting
-// the depth to 0.
+// The depth argument will be computed in CFGStackify. We set it to 0 here for
+// now.
 def : Pat<(int_wasm_rethrow), (RETHROW 0)>;
 
 // Region within which an exception is caught: try / end_try
Index: llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
@@ -86,6 +86,7 @@
                           const MachineBasicBlock *MBB);
   unsigned getDelegateDepth(const SmallVectorImpl<EndMarkerInfo> &Stack,
                             const MachineBasicBlock *MBB);
+  unsigned getRethrowDepth(const SmallVectorImpl<EndMarkerInfo> &Stack);
   void rewriteDepthImmediates(MachineFunction &MF);
   void fixEndsAtEndOfFunction(MachineFunction &MF);
   void cleanupFunctionData(MachineFunction &MF);
@@ -1551,6 +1552,20 @@
   return Depth;
 }
 
+unsigned WebAssemblyCFGStackify::getRethrowDepth(
+    const SmallVectorImpl<EndMarkerInfo> &Stack) {
+  unsigned Depth = 0;
+  // In our current implementation, rethrows always rethrow the exception caught
+  // by the innermost enclosing catch. So find the nearest end_try marker.
+  for (auto X : reverse(Stack)) {
+    if (X.second->getOpcode() == WebAssembly::END_TRY)
+      break;
+    ++Depth;
+  }
+  assert(Depth < Stack.size() && "Rethrow destination should be in scope");
+  return Depth;
+}
+
 void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) {
   // Now rewrite references to basic blocks to be depth immediates.
   SmallVector<EndMarkerInfo, 8> Stack;
@@ -1585,6 +1600,10 @@
         Stack.push_back(std::make_pair(EndToBegin[&MI]->getParent(), &MI));
         break;
 
+      case WebAssembly::RETHROW:
+        MI.getOperand(0).setImm(getRethrowDepth(Stack));
+        break;
+
       default:
         if (MI.isTerminator()) {
           // Rewrite MBB operands to be depth immediates.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96532.323102.patch
Type: text/x-patch
Size: 3209 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210211/e7b1de86/attachment.bin>


More information about the llvm-commits mailing list