Skip to content

Commit 8702f84

Browse files
committed
[WebAssembly] Generate invokes with llvm.wasm.(re)throw
Even though `__builtin_wasm_throw`, which is lowered down to `llvm.wasm.throw`, throws, ```cpp try { __builtin_wasm_throw(0, obj); } catch (...) { } ``` does not generate `invoke`. This is because we have assumed the intrinsic cannot be invoked, which doesn't make much sense. (See llvm#128104 for the historical context) llvm#128104 made `llvm.wasm.throw` intrinsic invokable in the backend. This actually generates `invoke`s in Clang for `__builtin_wasm_throw`. While we're at it, this also generates `invoke`s for `__builtin_wasm_rethrow`, which is actually not used anywhere in C++ support. I haven't deleted it just in case in may have uses later. (For example, to support rethrow functionality that carries stack trace with exnref) Depends on llvm#128104 for the CI to pass. Fixes llvm#124710.
1 parent 7c24041 commit 8702f84

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -22493,11 +22493,11 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
2249322493
Value *Tag = EmitScalarExpr(E->getArg(0));
2249422494
Value *Obj = EmitScalarExpr(E->getArg(1));
2249522495
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_throw);
22496-
return Builder.CreateCall(Callee, {Tag, Obj});
22496+
return EmitRuntimeCallOrInvoke(Callee, {Tag, Obj});
2249722497
}
2249822498
case WebAssembly::BI__builtin_wasm_rethrow: {
2249922499
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_rethrow);
22500-
return Builder.CreateCall(Callee);
22500+
return EmitRuntimeCallOrInvoke(Callee);
2250122501
}
2250222502
case WebAssembly::BI__builtin_wasm_memory_atomic_wait32: {
2250322503
Value *Addr = EmitScalarExpr(E->getArg(0));
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -triple wasm32-unknown-unknown -fexceptions -fcxx-exceptions -target-feature +reference-types -target-feature +exception-handling -target-feature +multivalue -exception-model=wasm -emit-llvm -o - %s | FileCheck %s
2+
3+
// Check if __builtin_wasm_throw and __builtin_wasm_rethrow are correctly
4+
// invoked when placed in try-catch.
5+
6+
void throw_in_try(void *obj) {
7+
try {
8+
__builtin_wasm_throw(0, obj);
9+
} catch (...) {
10+
}
11+
// CHECK: invoke void @llvm.wasm.throw(i32 0, ptr %{{.*}})
12+
}
13+
14+
void rethrow_in_try() {
15+
try {
16+
__builtin_wasm_rethrow();
17+
} catch (...) {
18+
}
19+
// CHECK: invoke void @llvm.wasm.rethrow()
20+
}

0 commit comments

Comments
 (0)