Skip to content

Commit

Permalink
add initial support for __cxa_rethrow
Browse files Browse the repository at this point in the history
  • Loading branch information
bruteforceboy committed Feb 3, 2025
1 parent c4e5842 commit 39e1a35
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
25 changes: 24 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,30 @@ mlir::Value CIRGenItaniumCXXABI::getCXXDestructorImplicitParam(

void CIRGenItaniumCXXABI::emitRethrow(CIRGenFunction &CGF, bool isNoReturn) {
// void __cxa_rethrow();
llvm_unreachable("NYI");

cir::FuncType FTy =
CGF.getBuilder().getFuncType({}, CGF.getBuilder().getVoidTy());

auto Fn = CGF.CGM.createRuntimeFunction(FTy, "__cxa_rethrow");

if (isNoReturn) {
auto &builder = CGF.getBuilder();

auto callOp = builder.createTryCallOp(Fn.getLoc(), Fn, {});

// The idea here is to create
// an unreachable continue block for the rethrow, but
// there is a YieldOp that terminates the TryOp already, so creating an
// UnreachableOp in-place causes us to have two terminators which isn't
// valid CIR. So, to make this valid CIR the UnreachableOp is created in a
// separate scope.
builder.create<cir::ScopeOp>(Fn.getLoc(), /*scopeBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc) {
b.create<cir::UnreachableOp>(loc);
});
} else {
llvm_unreachable("NYI");
}
}

void CIRGenItaniumCXXABI::emitThrow(CIRGenFunction &CGF,
Expand Down
37 changes: 37 additions & 0 deletions clang/test/CIR/CodeGen/try-catch-dtors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,40 @@ void bar() {
// CIR: cir.store %[[V3]], %[[V1]] : !s32i, !cir.ptr<!s32i>
// CIR: cir.call @_ZN1AD2Ev(%[[V0]]) : (!cir.ptr<!ty_A>) -> () extra(#fn_attr)
// CIR: cir.return

struct S {
S() {}
};

void refoo() {
int r = 1;
try {
S s;
throw;
} catch (...) {
++r;
}
}

// CIR-LABEL: @_Z5refoov()
// CIR: %[[V0:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["r", init] {alignment = 4 : i64}
// CIR: %[[V1:.*]] = cir.const #cir.int<1> : !s32i
// CIR: cir.store %[[V1]], %[[V0]] : !s32i, !cir.ptr<!s32i>
// CIR: cir.scope {
// CIR: %[[V2:.*]] = cir.alloca !ty_S, !cir.ptr<!ty_S>, ["s", init] {alignment = 1 : i64}
// CIR: cir.try {
// CIR: cir.call exception @_ZN1SC2Ev(%[[V2]]) : (!cir.ptr<!ty_S>) -> ()
// CIR: cir.call exception @__cxa_rethrow() : () -> ()
// CIR: cir.scope {
// CIR: cir.unreachable
// CIR: }
// CIR: cir.yield
// CIR: } catch [type #cir.all {
// CIR: %[[V3:.*]] = cir.catch_param -> !cir.ptr<!void>
// CIR: %[[V4:.*]] = cir.load %[[V0]] : !cir.ptr<!s32i>, !s32i
// CIR: %[[V5:.*]] = cir.unary(inc, %[[V4]]) : !s32i, !s32i
// CIR: cir.store %[[V5]], %[[V0]] : !s32i, !cir.ptr<!s32i>
// CIR: cir.yield
// CIR: }]
// CIR: }
// CIR: cir.return

0 comments on commit 39e1a35

Please sign in to comment.