Skip to content

Commit 40566fd

Browse files
authored
[WebAssembly] Generate invokes with llvm.wasm.(re)throw (#128105)
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 #128104 for the historical context) #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 #128104 for the CI to pass. Fixes #124710.
1 parent e6f6a1e commit 40566fd

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
@@ -22524,11 +22524,11 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
2252422524
Value *Tag = EmitScalarExpr(E->getArg(0));
2252522525
Value *Obj = EmitScalarExpr(E->getArg(1));
2252622526
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_throw);
22527-
return Builder.CreateCall(Callee, {Tag, Obj});
22527+
return EmitRuntimeCallOrInvoke(Callee, {Tag, Obj});
2252822528
}
2252922529
case WebAssembly::BI__builtin_wasm_rethrow: {
2253022530
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_rethrow);
22531-
return Builder.CreateCall(Callee);
22531+
return EmitRuntimeCallOrInvoke(Callee);
2253222532
}
2253322533
case WebAssembly::BI__builtin_wasm_memory_atomic_wait32: {
2253422534
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)