[PATCH] D108785: [WebAssembly] Fix PHI when bypassing longjmps

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 26 12:56:01 PDT 2021


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

When doing Emscritpen EH, if SjLj is also enabled and used and if the
thrown exception has a possiblity being a longjmp instead of an
exception, we shoudln't swallow it; we should rethrow, or relay it. It
was done in D106525 <https://reviews.llvm.org/D106525> and the code is here:
https://github.com/llvm/llvm-project/blob/8441a8eea8007b9eaaaabf76055949180a702d6d/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp#L858-L898

Here is the pseudocode of that part: (copied from comments)

  if (%__THREW__.val == 0 || %__THREW__.val == 1)
    goto %tail
  else
    goto %longjmp.rethrow
  
  longjmp.rethrow: ;; This is longjmp. Rethrow it
    %__threwValue.val = __threwValue
    emscripten_longjmp(%__THREW__.val, %__threwValue.val);
  
  tail: ;; Nothing happened or an exception is thrown
    ... Continue exception handling ...

If the current BB (where the `invoke` is created) has successors that
has the current BB as its PHI incoming node, now that has to change to
`tail` in the pseudocode, because `tail` is the latest BB that is
connected with the next BB, but this was missing.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108785

Files:
  llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
  llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj.ll


Index: llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj.ll
+++ llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj.ll
@@ -65,6 +65,10 @@
 ; CHECK-NEXT: %or = or i1 %cmp.eq.zero, %cmp.eq.one
 ; CHECK-NEXT: br i1 %or, label %tail, label %longjmp.rethrow
 
+; CHECK: try.cont:
+; CHECK-NEXT:  %phi = phi i32 [ undef, %tail ], [ undef, %lpad ]
+; CHECK-NEXT:  ret void
+
 ; CHECK:    tail:
 ; CHECK-NEXT: %cmp = icmp eq i32 %__THREW__.val, 1
 ; CHECK-NEXT: br i1 %cmp, label %lpad, label %try.cont
@@ -84,6 +88,7 @@
   br label %try.cont
 
 try.cont:                                         ; preds = %entry, %lpad
+ %phi = phi i32 [ undef, %entry ], [ undef, %lpad ]
   ret void
 }
 
Index: llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
@@ -891,6 +891,7 @@
 
         IRB.CreateUnreachable();
         IRB.SetInsertPoint(Tail);
+        BB.replaceSuccessorsPhiUsesWith(&BB, Tail);
       }
 
       // Insert a branch based on __THREW__ variable


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108785.368966.patch
Type: text/x-patch
Size: 1280 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210826/914c1d0c/attachment.bin>


More information about the llvm-commits mailing list