Skip to content

Commit 871adbd

Browse files
committed
[CIR][CIRGen] Lower cir.throw in absence of dtors
1 parent 10d6f4b commit 871adbd

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

clang/include/clang/CIR/MissingFeatures.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct MissingFeatures {
103103
static bool shouldCreateMemCpyFromGlobal() { return false; }
104104
static bool shouldReverseUnaryCondOnBoolExpr() { return false; }
105105
static bool fieldMemcpyizerBuildMemcpy() { return false; }
106-
static bool isTrivialAndisDefaultConstructor() { return false; }
106+
static bool isTrivialCtorOrDtor() { return false; }
107107
static bool isMemcpyEquivalentSpecialMember() { return false; }
108108
static bool constructABIArgDirectExtend() { return false; }
109109
static bool mayHaveIntegerOverflow() { return false; }

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ void CIRGenFunction::buildCXXConstructorCall(
814814
// In LLVM: do nothing.
815815
// In CIR: emit as a regular call, other later passes should lower the
816816
// ctor call into trivial initialization.
817-
assert(!MissingFeatures::isTrivialAndisDefaultConstructor());
817+
assert(!MissingFeatures::isTrivialCtorOrDtor());
818818

819819
if (isMemcpyEquivalentSpecialMember(D)) {
820820
assert(!MissingFeatures::isMemcpyEquivalentSpecialMember());

clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -2210,16 +2210,19 @@ void CIRGenItaniumCXXABI::buildThrow(CIRGenFunction &CGF,
22102210
// LoweringPrepare or some other pass to skip passing the
22112211
// trivial function.
22122212
//
2213-
// TODO(cir): alternatively, dtor could be ignored here and
2214-
// the type used to gather the relevant dtor during
2215-
// LoweringPrepare.
2213+
// TODO(cir): However, such lowering is still NYI, and for
2214+
// the sake of getting cir.throw right, the same OG path is
2215+
// follows here.
22162216
mlir::FlatSymbolRefAttr dtor{};
22172217
if (const RecordType *recordTy = clangThrowType->getAs<RecordType>()) {
22182218
CXXRecordDecl *rec = cast<CXXRecordDecl>(recordTy->getDecl());
2219-
CXXDestructorDecl *dtorD = rec->getDestructor();
2220-
dtor = mlir::FlatSymbolRefAttr::get(
2221-
CGM.getAddrOfCXXStructor(GlobalDecl(dtorD, Dtor_Complete))
2222-
.getSymNameAttr());
2219+
assert(!MissingFeatures::isTrivialCtorOrDtor());
2220+
if (!rec->hasTrivialDestructor()) {
2221+
CXXDestructorDecl *dtorD = rec->getDestructor();
2222+
dtor = mlir::FlatSymbolRefAttr::get(
2223+
CGM.getAddrOfCXXStructor(GlobalDecl(dtorD, Dtor_Complete))
2224+
.getSymNameAttr());
2225+
}
22232226
}
22242227

22252228
// FIXME: When adding support for invoking, we should wrap the throw op

clang/test/CIR/CodeGen/eh.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple aarch64-none-linux-android21 -fclangir -fcxx-exceptions -fexceptions -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -std=c++20 -triple aarch64-none-linux-android21 -fclangir -fcxx-exceptions -fexceptions -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
5+
6+
struct test1_D {
7+
double d;
8+
} d1;
9+
10+
void test1() {
11+
throw d1;
12+
}
13+
14+
// CIR-LABEL: @_Z5test1v
15+
// FIXME: this is overaligned, should be 4.
16+
// CIR: %[[ALLOC:.*]] = cir.alloc.exception 8 -> !cir.ptr<!ty_test1_D>
17+
// CIR: %[[G:.*]] = cir.get_global @d1 : !cir.ptr<!ty_test1_D>
18+
// CIR: cir.call @_ZN7test1_DC1ERKS_(%[[ALLOC]], %[[G]]) : (!cir.ptr<!ty_test1_D>, !cir.ptr<!ty_test1_D>) -> ()
19+
// CIR: cir.throw %[[ALLOC]] : !cir.ptr<!ty_test1_D>, @_ZTI7test1_D
20+
// CIR: cir.unreachable
21+
// CIR: }
22+
23+
// LLVM-LABEL: @_Z5test1v
24+
// FIXME: this is overaligned, should be 4.
25+
// LLVM: %[[ALLOC:.*]] = call ptr @__cxa_allocate_exception(i64 8)
26+
27+
// FIXME: this is a simple store once we fix isTrivialCtorOrDtor().
28+
// LLVM: call void @_ZN7test1_DC1ERKS_(ptr %[[ALLOC]], ptr @d1)
29+
// LLVM: call void @__cxa_throw(ptr %[[ALLOC]], ptr @_ZTI7test1_D, ptr null)
30+
// LLVM: unreachable
31+
// LLVM: }

0 commit comments

Comments
 (0)