OSDN Git Service

[WebAssembly] Rename wasm_rethrow_in_catch intrinsic/builtin
authorHeejin Ahn <aheejin@gmail.com>
Sat, 26 Dec 2020 00:14:35 +0000 (16:14 -0800)
committerHeejin Ahn <aheejin@gmail.com>
Fri, 8 Jan 2021 14:55:04 +0000 (06:55 -0800)
`wasm_rethrow_in_catch` intrinsic and builtin are used in order to
rethrow an exception when the exception is caught but there is no
matching clause within the current `catch`. For example,
```
try {
  foo();
} catch (int n) {
  ...
}
```
If the caught exception does not correspond to C++ `int` type, it should
be rethrown. These intrinsic/builtin were renamed `rethrow_in_catch`
because at the time I thought there would be another intrinsic for C++'s
`throw` keyword, which rethrows an exception. It turned out that `throw`
keyword doesn't require wasm's `rethrow` instruction, so we rename
`rethrow_in_catch` to just `rethrow` here.

Reviewed By: dschuff, tlively

Differential Revision: https://reviews.llvm.org/D94038

12 files changed:
clang/include/clang/Basic/BuiltinsWebAssembly.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/CodeGen/CGException.cpp
clang/test/CodeGen/builtins-wasm.c
clang/test/CodeGenCXX/wasm-eh.cpp
llvm/include/llvm/IR/IntrinsicsWebAssembly.td
llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
llvm/lib/IR/Verifier.cpp
llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td
llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
llvm/test/CodeGen/WebAssembly/exception.ll
llvm/test/CodeGen/WebAssembly/wasmehprepare.ll

index 8448208..080c6b5 100644 (file)
@@ -38,7 +38,7 @@ BUILTIN(__builtin_wasm_max_f64, "ddd", "nc")
 
 // Exception handling builtins.
 TARGET_BUILTIN(__builtin_wasm_throw, "vIUiv*", "r", "exception-handling")
-TARGET_BUILTIN(__builtin_wasm_rethrow_in_catch, "v", "r", "exception-handling")
+TARGET_BUILTIN(__builtin_wasm_rethrow, "v", "r", "exception-handling")
 
 // Atomic wait and notify.
 TARGET_BUILTIN(__builtin_wasm_memory_atomic_wait32, "ii*iLLi", "n", "atomics")
index ea39d64..cf84ad3 100644 (file)
@@ -16583,8 +16583,8 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
     Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_throw);
     return Builder.CreateCall(Callee, {Tag, Obj});
   }
-  case WebAssembly::BI__builtin_wasm_rethrow_in_catch: {
-    Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_rethrow_in_catch);
+  case WebAssembly::BI__builtin_wasm_rethrow: {
+    Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_rethrow);
     return Builder.CreateCall(Callee);
   }
   case WebAssembly::BI__builtin_wasm_memory_atomic_wait32: {
index f8a4869..7a64963 100644 (file)
@@ -1272,7 +1272,7 @@ void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
     assert(RethrowBlock != WasmCatchStartBlock && RethrowBlock->empty());
     Builder.SetInsertPoint(RethrowBlock);
     llvm::Function *RethrowInCatchFn =
-        CGM.getIntrinsic(llvm::Intrinsic::wasm_rethrow_in_catch);
+        CGM.getIntrinsic(llvm::Intrinsic::wasm_rethrow);
     EmitNoreturnRuntimeCallOrInvoke(RethrowInCatchFn, {});
   }
 
index 83924b4..d8b61f5 100644 (file)
@@ -49,10 +49,10 @@ void throw(void *obj) {
   // WEBASSEMBLY64: call void @llvm.wasm.throw(i32 0, i8* %{{.*}})
 }
 
-void rethrow_in_catch(void) {
-  return __builtin_wasm_rethrow_in_catch();
-  // WEBASSEMBLY32: call void @llvm.wasm.rethrow.in.catch()
-  // WEBASSEMBLY64: call void @llvm.wasm.rethrow.in.catch()
+void rethrow(void) {
+  return __builtin_wasm_rethrow();
+  // WEBASSEMBLY32: call void @llvm.wasm.rethrow()
+  // WEBASSEMBLY64: call void @llvm.wasm.rethrow()
 }
 
 int memory_atomic_wait32(int *addr, int expected, long long timeout) {
index cc467b4..d3c8400 100644 (file)
@@ -63,7 +63,7 @@ void test0() {
 // CHECK-NEXT:   br label %[[TRY_CONT_BB]]
 
 // CHECK: [[RETHROW_BB]]:
-// CHECK-NEXT:   call void @llvm.wasm.rethrow.in.catch() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
+// CHECK-NEXT:   call void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
 // CHECK-NEXT:   unreachable
 
 // Single catch-all
@@ -233,7 +233,7 @@ void test6() {
 // CHECK:   catchret from %[[CATCHPAD]] to label %{{.*}}
 
 // CHECK: [[RETHROW_BB]]:
-// CHECK-NEXT:   invoke void @llvm.wasm.rethrow.in.catch() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
+// CHECK-NEXT:   invoke void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
 // CHECK-NEXT:          to label %[[UNREACHABLE_BB:.*]] unwind label %[[EHCLEANUP_BB1:.*]]
 
 // CHECK: [[EHCLEANUP_BB2]]:
@@ -297,7 +297,7 @@ void test7() {
 
 // CHECK:   catchret from %[[CATCHPAD0]] to label
 
-// CHECK:   invoke void @llvm.wasm.rethrow.in.catch() {{.*}} [ "funclet"(token %[[CATCHPAD0]]) ]
+// CHECK:   invoke void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD0]]) ]
 
 // CHECK:   %[[CLEANUPPAD1:.*]] = cleanuppad within %[[CATCHPAD0]] []
 // CHECK:   cleanupret from %[[CLEANUPPAD1]] unwind label
@@ -369,11 +369,11 @@ void test8() {
 
 // CHECK:   catchret from %[[CATCHPAD1]] to label
 
-// CHECK:   invoke void @llvm.wasm.rethrow.in.catch() {{.*}} [ "funclet"(token %[[CATCHPAD1]]) ]
+// CHECK:   invoke void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD1]]) ]
 
 // CHECK:   catchret from %[[CATCHPAD0]] to label
 
-// CHECK:   call void @llvm.wasm.rethrow.in.catch() {{.*}} [ "funclet"(token %[[CATCHPAD0]]) ]
+// CHECK:   call void @llvm.wasm.rethrow() {{.*}} [ "funclet"(token %[[CATCHPAD0]]) ]
 // CHECK:   unreachable
 
 // CHECK:   %[[CLEANUPPAD0:.*]] = cleanuppad within %[[CATCHPAD1]] []
index e87700a..92f9c15 100644 (file)
@@ -50,9 +50,10 @@ def int_wasm_trunc_saturate_unsigned : Intrinsic<[llvm_anyint_ty],
 //===----------------------------------------------------------------------===//
 
 // throw / rethrow
+// The immediate argument is an index to a tag, which is 0 for C++.
 def int_wasm_throw : Intrinsic<[], [llvm_i32_ty, llvm_ptr_ty],
                                [Throws, IntrNoReturn, ImmArg<ArgIndex<0>>]>;
-def int_wasm_rethrow_in_catch : Intrinsic<[], [], [Throws, IntrNoReturn]>;
+def int_wasm_rethrow : Intrinsic<[], [], [Throws, IntrNoReturn]>;
 
 // Since wasm does not use landingpad instructions, these instructions return
 // exception pointer and selector values until we lower them in WasmEHPrepare.
index acfdaa0..814c4f5 100644 (file)
@@ -1637,7 +1637,7 @@ void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
   }
 }
 
-// For wasm, there's alwyas a single catch pad attached to a catchswitch, and
+// For wasm, there's always a single catch pad attached to a catchswitch, and
 // the control flow always stops at the single catch pad, as it does for a
 // cleanup pad. In case the exception caught is not of the types the catch pad
 // catches, it will be rethrown by a rethrow.
@@ -2785,7 +2785,7 @@ void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
     case Intrinsic::experimental_gc_statepoint:
       LowerStatepoint(cast<GCStatepointInst>(I), EHPadBB);
       break;
-    case Intrinsic::wasm_rethrow_in_catch: {
+    case Intrinsic::wasm_rethrow: {
       // This is usually done in visitTargetIntrinsic, but this intrinsic is
       // special because it can be invoked, so we manually lower it to a DAG
       // node here.
@@ -2793,7 +2793,7 @@ void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
       Ops.push_back(getRoot()); // inchain
       const TargetLowering &TLI = DAG.getTargetLoweringInfo();
       Ops.push_back(
-          DAG.getTargetConstant(Intrinsic::wasm_rethrow_in_catch, getCurSDLoc(),
+          DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
                                 TLI.getPointerTy(DAG.getDataLayout())));
       SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
       DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
index 5692493..2e12ed6 100644 (file)
@@ -4357,7 +4357,7 @@ void Verifier::visitInstruction(Instruction &I) {
               F->getIntrinsicID() == Intrinsic::experimental_patchpoint_void ||
               F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
               F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
-              F->getIntrinsicID() == Intrinsic::wasm_rethrow_in_catch,
+              F->getIntrinsicID() == Intrinsic::wasm_rethrow,
           "Cannot invoke an intrinsic other than donothing, patchpoint, "
           "statepoint, coro_resume or coro_destroy",
           &I);
index 63aeb1b..0c21005 100644 (file)
@@ -133,10 +133,10 @@ defm THROW : I<(outs), (ins event_op:$tag, variable_ops),
                "throw   \t$tag", "throw   \t$tag", 0x08>;
 defm RETHROW : I<(outs), (ins EXNREF:$exn), (outs), (ins), [],
                  "rethrow \t$exn", "rethrow", 0x09>;
-// Pseudo instruction to be the lowering target of int_wasm_rethrow_in_catch
-// intrinsic. Will be converted to the real rethrow instruction later.
+// Pseudo instruction to be the lowering target of int_wasm_rethrow intrinsic.
+// Will be converted to the real rethrow instruction later.
 let isPseudo = 1 in
-defm RETHROW_IN_CATCH : NRI<(outs), (ins), [(int_wasm_rethrow_in_catch)],
+defm RETHROW_IN_CATCH : NRI<(outs), (ins), [(int_wasm_rethrow)],
                             "rethrow_in_catch", 0>;
 } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
 
index 9c2dec3..d6e852e 100644 (file)
@@ -76,7 +76,7 @@ catch:                                            ; preds = %catch.fallthrough
   catchret from %1 to label %try.cont
 
 rethrow:                                          ; preds = %catch.fallthrough
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %catch, %catch2, %entry
@@ -179,7 +179,7 @@ invoke.cont8:                                     ; preds = %catch6
   catchret from %9 to label %try.cont
 
 rethrow5:                                         ; preds = %catch.start3
-  invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %9) ]
+  invoke void @llvm.wasm.rethrow() [ "funclet"(token %9) ]
           to label %unreachable unwind label %ehcleanup9
 
 try.cont:                                         ; preds = %invoke.cont8, %catch
@@ -187,7 +187,7 @@ try.cont:                                         ; preds = %invoke.cont8, %catc
   catchret from %1 to label %try.cont11
 
 rethrow:                                          ; preds = %catch.start
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
   unreachable
 
 try.cont11:                                       ; preds = %try.cont, %entry
@@ -864,7 +864,7 @@ invoke.cont2:                                     ; preds = %catch
   catchret from %1 to label %for.inc
 
 rethrow:                                          ; preds = %catch.start
-  call void @llvm.wasm.rethrow.in.catch() #6 [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() #6 [ "funclet"(token %1) ]
   unreachable
 
 for.inc:                                          ; preds = %invoke.cont2, %for.body
@@ -1093,7 +1093,7 @@ declare %class.MyClass* @_ZN7MyClassC2ERKS_(%class.MyClass* returned, %class.MyC
 declare i32 @__gxx_wasm_personality_v0(...)
 declare i8* @llvm.wasm.get.exception(token)
 declare i32 @llvm.wasm.get.ehselector(token)
-declare void @llvm.wasm.rethrow.in.catch()
+declare void @llvm.wasm.rethrow()
 declare i32 @llvm.eh.typeid.for(i8*)
 declare i8* @__cxa_begin_catch(i8*)
 declare void @__cxa_end_catch()
index 8944649..22f25c1 100644 (file)
@@ -70,7 +70,7 @@ catch:                                            ; preds = %catch.start
   catchret from %1 to label %try.cont
 
 rethrow:                                          ; preds = %catch.start
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %catch, %entry
@@ -258,7 +258,7 @@ invoke.cont1:                                     ; preds = %catch
   catchret from %1 to label %try.cont
 
 rethrow:                                          ; preds = %catch.start
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %invoke.cont1, %entry
@@ -368,7 +368,7 @@ declare i32 @__gxx_wasm_personality_v0(...)
 declare void @llvm.wasm.throw(i32, i8*)
 declare i8* @llvm.wasm.get.exception(token)
 declare i32 @llvm.wasm.get.ehselector(token)
-declare void @llvm.wasm.rethrow.in.catch()
+declare void @llvm.wasm.rethrow()
 declare i32 @llvm.eh.typeid.for(i8*)
 declare i8* @__cxa_begin_catch(i8*)
 declare void @__cxa_end_catch()
index 9bfdeb4..2f55b49 100644 (file)
@@ -54,7 +54,7 @@ catch:                                            ; preds = %catch.start
 ; CHECK-NEXT:  call i8* @__cxa_begin_catch(i8* %[[EXN]])
 
 rethrow:                                          ; preds = %catch.start
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %entry, %catch
@@ -125,7 +125,7 @@ catch4:                                           ; preds = %catch.start3
   catchret from %6 to label %try.cont7
 
 rethrow:                                          ; preds = %catch.start3
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %6) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %6) ]
   unreachable
 
 try.cont7:                                        ; preds = %try.cont, %catch4
@@ -189,7 +189,7 @@ catch6:                                           ; preds = %catch.start3
   catchret from %7 to label %try.cont
 
 rethrow5:                                         ; preds = %catch.start3
-  invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %7) ]
+  invoke void @llvm.wasm.rethrow() [ "funclet"(token %7) ]
           to label %unreachable unwind label %ehcleanup
 
 try.cont:                                         ; preds = %catch, %catch6
@@ -197,7 +197,7 @@ try.cont:                                         ; preds = %catch, %catch6
   catchret from %1 to label %try.cont9
 
 rethrow:                                          ; preds = %catch.start
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
   unreachable
 
 try.cont9:                                        ; preds = %entry, %try.cont
@@ -274,7 +274,7 @@ catch4:                                           ; preds = %catch.start3
   catchret from %6 to label %try.cont
 
 rethrow:                                          ; preds = %catch.start3
-  invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %6) ]
+  invoke void @llvm.wasm.rethrow() [ "funclet"(token %6) ]
           to label %unreachable unwind label %ehcleanup
 
 try.cont:                                         ; preds = %catch.start, %catch4
@@ -380,7 +380,7 @@ catch11:                                          ; preds = %catch.start8
   catchret from %14 to label %try.cont
 
 rethrow10:                                        ; preds = %catch.start8
-  invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %14) ]
+  invoke void @llvm.wasm.rethrow() [ "funclet"(token %14) ]
           to label %unreachable unwind label %ehcleanup
 
 try.cont:                                         ; preds = %catch.start3, %catch11
@@ -395,7 +395,7 @@ try.cont16:                                       ; preds = %catch, %invoke.cont
   catchret from %1 to label %try.cont19
 
 rethrow:                                          ; preds = %catch.start
-  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
   unreachable
 
 try.cont19:                                       ; preds = %entry, %try.cont16
@@ -604,7 +604,7 @@ declare i8* @llvm.wasm.get.exception(token)
 declare i32 @llvm.wasm.get.ehselector(token)
 declare i32 @llvm.eh.typeid.for(i8*)
 declare void @llvm.wasm.throw(i32, i8*)
-declare void @llvm.wasm.rethrow.in.catch()
+declare void @llvm.wasm.rethrow()
 declare i8* @__cxa_begin_catch(i8*)
 declare void @__cxa_end_catch()
 declare void @__clang_call_terminate(i8*)