[PATCH] D118408: [WebAssembly] Nullify unnecessary setjmp invokes

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 27 13:35:48 PST 2022


aheejin created this revision.
aheejin added a reviewer: dschuff.
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.

This is similar to D116619 <https://reviews.llvm.org/D116619>, but now it handles `invoke`s. The reason we
didn't handle `invoke`s back then was we didn't support Wasm EH + Wasm
SjLj together, and the only case SjLj transformation will see `invoke`s
is when we are using Wasm EH. (In Emscripten EH, they would have been
transformed to `call`s to invoke wrappers.)

But after D117610 <https://reviews.llvm.org/D117610> we support Wasm EH + Wasm SjLj together and we can
nullify `invoke`s to `setjmp` when there is no other longjmpable calls
within the function. Actually this is very unlikely to happen in
practice, because we treat destructors as longjmpable and also treat
`__cxa_end_catch` as longjmpable even if it is not.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118408

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


Index: llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj.ll
+++ llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj.ll
@@ -239,6 +239,11 @@
 ; CHECK-NEXT:    catchswitch within none [label %catch.longjmp] unwind to caller
 }
 
+; void @cleanuppad_no_parent {
+;   jmp_buf buf;
+;   Temp t;
+;   setjmp(buf);
+; }
 define void @cleanuppad_no_parent() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
 ; CHECK-LABEL: @cleanuppad_no_parent
 entry:
@@ -266,6 +271,29 @@
   cleanupret from %0 unwind to caller
 }
 
+; This case was adapted from @cleanuppad_no_parent by removing allocas and
+; destructor calls, to generate a situation that there's only 'invoke @setjmp'
+; and no other longjmpable calls.
+define i32 @setjmp_only() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
+; CHECK-LABEL: @setjmp_only
+entry:
+  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
+  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
+  %call = invoke i32 @setjmp(%struct.__jmp_buf_tag* noundef %arraydecay) #0
+          to label %invoke.cont unwind label %ehcleanup
+
+invoke.cont:                                      ; preds = %entry
+  ret i32 %call
+; CHECK: invoke.cont:
+; The remaining setjmp call is converted to constant 0, because setjmp returns 0
+; when called directly.
+; CHECK:   ret i32 0
+
+ehcleanup:                                        ; preds = %entry
+  %0 = cleanuppad within none []
+  cleanupret from %0 unwind to caller
+}
+
 declare void @foo()
 ; Function Attrs: nounwind
 declare %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* %this) #2
Index: llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll
+++ llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll
@@ -140,10 +140,13 @@
   ; free cannot longjmp
   call void @free(i8* %ptr)
   ret i32 %call
+; CHECK: entry:
 ; CHECK-NOT: @malloc
 ; CHECK-NOT: %setjmpTable
 ; CHECK-NOT: @saveSetjmp
 ; CHECK-NOT: @testSetjmp
+; The remaining setjmp call is converted to constant 0, because setjmp returns 0
+; when called directly.
 ; CHECK: ret i32 0
 }
 
Index: llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
@@ -869,15 +869,17 @@
   Function *SetjmpF = M.getFunction("setjmp");
   SmallVector<Instruction *, 1> ToErase;
 
-  for (User *U : SetjmpF->users()) {
-    auto *CI = dyn_cast<CallInst>(U);
-    // FIXME 'invoke' to setjmp can happen when we use Wasm EH + Wasm SjLj, but
-    // we don't support two being used together yet.
-    if (!CI)
-      report_fatal_error("Wasm EH + Wasm SjLj is not fully supported yet");
-    BasicBlock *BB = CI->getParent();
+  for (User *U : make_early_inc_range(SetjmpF->users())) {
+    auto *CB = dyn_cast<CallBase>(U);
+    BasicBlock *BB = CB->getParent();
     if (BB->getParent() != F) // in other function
       continue;
+    CallInst *CI = nullptr;
+    // setjmp cannot throw. So if it is an invoke, lower it to a call
+    if (auto *II = dyn_cast<InvokeInst>(CB))
+      CI = llvm::changeToCall(II);
+    else
+      CI = cast<CallInst>(CB);
     ToErase.push_back(CI);
     CI->replaceAllUsesWith(IRB.getInt32(0));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118408.403783.patch
Type: text/x-patch
Size: 3593 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220127/08308454/attachment.bin>


More information about the llvm-commits mailing list