Skip to content

[WebAssembly] Make llvm.wasm.throw invokable #128104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3360,16 +3360,28 @@ void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
case Intrinsic::experimental_gc_statepoint:
LowerStatepoint(cast<GCStatepointInst>(I), EHPadBB);
break;
// wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
// but these intrinsics are special because they can be invoked, so we
// manually lower it to a DAG node here.
case Intrinsic::wasm_throw: {
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
std::array<SDValue, 4> Ops = {
getControlRoot(), // inchain for the terminator node
DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
TLI.getPointerTy(DAG.getDataLayout())),
getValue(I.getArgOperand(0)), // tag
getValue(I.getArgOperand(1)) // thrown value
};
SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
break;
}
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.
SmallVector<SDValue, 8> Ops;
Ops.push_back(getControlRoot()); // inchain for the terminator node
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Ops.push_back(
std::array<SDValue, 2> Ops = {
getControlRoot(), // inchain for the terminator node
DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
TLI.getPointerTy(DAG.getDataLayout())));
TLI.getPointerTy(DAG.getDataLayout()))};
Comment on lines +3381 to +3384
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just makes the existing wasm_rethrow use std::array and NFC

SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
break;
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/CodeGen/WasmEHPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,8 @@ bool WasmEHPrepareImpl::prepareThrows(Function &F) {
// delete all following instructions within the BB, and delete all the dead
// children of the BB as well.
for (User *U : ThrowF->users()) {
// A call to @llvm.wasm.throw() is only generated from __cxa_throw()
// builtin call within libcxxabi, and cannot be an InvokeInst.
auto *ThrowI = cast<CallInst>(U);
if (ThrowI->getFunction() != &F)
auto *ThrowI = dyn_cast<CallInst>(U);
if (!ThrowI || ThrowI->getFunction() != &F)
continue;
Changed = true;
auto *BB = ThrowI->getParent();
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5203,10 +5203,12 @@ void Verifier::visitInstruction(Instruction &I) {
F->getIntrinsicID() == Intrinsic::experimental_patchpoint ||
F->getIntrinsicID() == Intrinsic::fake_use ||
F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
F->getIntrinsicID() == Intrinsic::wasm_throw ||
F->getIntrinsicID() == Intrinsic::wasm_rethrow ||
IsAttachedCallOperand(F, CBI, i),
"Cannot invoke an intrinsic other than donothing, patchpoint, "
"statepoint, coro_resume, coro_destroy or clang.arc.attachedcall",
"statepoint, coro_resume, coro_destroy, clang.arc.attachedcall or "
"wasm.(re)throw",
&I);
Check(F->getParent() == &M, "Referencing function in another module!", &I,
&M, F, F->getParent());
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/CodeGen/WebAssembly/exception.ll
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,32 @@ unreachable: ; preds = %entry
unreachable
}

; This tests whether llvm.wasm.throw intrinsic can invoked and iseled correctly.

; CHECK-LABEL: invoke_throw:
; CHECK: try_table (catch __cpp_exception 0)
; CHECK: local.get 0
; CHECK: throw __cpp_exception
; CHECK: end_try_table
define void @invoke_throw(ptr %p) personality ptr @__gxx_wasm_personality_v0 {
entry:
invoke void @llvm.wasm.throw(i32 0, ptr %p)
to label %try.cont unwind label %catch.dispatch

catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller

catch.start: ; preds = %catch.dispatch
%1 = catchpad within %0 [ptr null]
%2 = call ptr @llvm.wasm.get.exception(token %1)
%3 = call i32 @llvm.wasm.get.ehselector(token %1)
%4 = call ptr @__cxa_begin_catch(ptr %2) #4 [ "funclet"(token %1) ]
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont

try.cont: ; preds = %catch, %entry
ret void
}

declare void @foo()
declare void @bar(ptr)
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Verifier/invoke.ll
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contb:

define i8 @f2() personality ptr @__gxx_personality_v0 {
entry:
; CHECK: Cannot invoke an intrinsic other than donothing, patchpoint, statepoint, coro_resume, coro_destroy or clang.arc.attachedcall
; CHECK: Cannot invoke an intrinsic other than donothing, patchpoint, statepoint, coro_resume, coro_destroy, clang.arc.attachedcall or wasm.(re)throw
invoke void @llvm.trap()
to label %cont unwind label %lpad

Expand Down