Skip to content

Commit 06967ca

Browse files
bcardosolopeslanza
authored andcommitted
[CIR][CIRGen] Get more vtable and dtor working
1 parent ae3af92 commit 06967ca

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,8 @@ void CIRGenFunction::buildDestructorBody(FunctionArgList &Args) {
12051205
if (CGM.getCodeGenOpts().StrictVTablePointers &&
12061206
CGM.getCodeGenOpts().OptimizationLevel > 0)
12071207
llvm_unreachable("NYI");
1208-
llvm_unreachable("NYI");
1208+
initializeVTablePointers(getLoc(Dtor->getSourceRange()),
1209+
Dtor->getParent());
12091210
}
12101211

12111212
if (isTryBody)
@@ -1466,7 +1467,12 @@ mlir::Value CIRGenFunction::GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
14661467
if (Delegating) {
14671468
llvm_unreachable("NYI");
14681469
} else if (RD == Base) {
1469-
llvm_unreachable("NYI");
1470+
// If the record matches the base, this is the complete ctor/dtor
1471+
// variant calling the base variant in a class with virtual bases.
1472+
assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) &&
1473+
"doing no-op VTT offset in base dtor/ctor?");
1474+
assert(!ForVirtualBase && "Can't have same class as virtual base!");
1475+
SubVTTIndex = 0;
14701476
} else {
14711477
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
14721478
CharUnits BaseOffset = ForVirtualBase ? Layout.getVBaseClassOffset(Base)

clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ commonBuildCXXMemberOrOperatorCall(CIRGenFunction &CGF, const CXXMethodDecl *MD,
5252

5353
// If there is an implicit parameter (e.g. VTT), emit it.
5454
if (ImplicitParam) {
55-
llvm_unreachable("NYI");
55+
Args.add(RValue::get(ImplicitParam), ImplicitParamTy);
5656
}
5757

5858
const auto *FPT = MD->getType()->castAs<FunctionProtoType>();

clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,18 @@ class CIRGenItaniumCXXABI : public cir::CIRGenCXXABI {
131131
// The Itanium ABI has separate complete-object vs. base-object variants of
132132
// both constructors and destructors.
133133
if (isa<CXXDestructorDecl>(GD.getDecl())) {
134-
llvm_unreachable("NYI");
134+
switch (GD.getDtorType()) {
135+
case Dtor_Complete:
136+
case Dtor_Deleting:
137+
return true;
138+
139+
case Dtor_Base:
140+
return false;
141+
142+
case Dtor_Comdat:
143+
llvm_unreachable("emitting dtor comdat as function?");
144+
}
145+
llvm_unreachable("bad dtor kind");
135146
}
136147
if (isa<CXXConstructorDecl>(GD.getDecl())) {
137148
switch (GD.getCtorType()) {
@@ -379,7 +390,7 @@ bool CIRGenItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
379390

380391
// Check if we have a base destructor.
381392
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
382-
llvm_unreachable("NYI");
393+
return true;
383394

384395
return false;
385396
}

clang/test/CIR/CodeGen/vtt.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int f() {
4949
// CIR: cir.global linkonce_odr @_ZTT1D = #cir.const_array<[#cir.global_view<@_ZTV1D, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTC1D0_1B, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTC1D0_1B, [0 : i32, 1 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTC1D16_1C, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTC1D16_1C, [0 : i32, 1 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTV1D, [0 : i32, 2 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTV1D, [0 : i32, 1 : i32, 3 : i32]> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 7> {alignment = 8 : i64}
5050

5151
// Class B constructor
52-
// CIR: cir.func linkonce_odr @_ZN1BC2Ev(%arg0: !cir.ptr<!ty_B> loc({{.*}}), %arg1: !cir.ptr<!cir.ptr<!void>> loc({{.*}})) extra(#fn_attr) {
52+
// CIR: cir.func linkonce_odr @_ZN1BC2Ev(%arg0: !cir.ptr<!ty_B>
5353
// CIR: %{{[0-9]+}} = cir.vtt.address_point %{{[0-9]+}} : !cir.ptr<!cir.ptr<!void>>, offset = 0 -> !cir.ptr<!cir.ptr<!void>>
5454
// CIR: %{{[0-9]+}} = cir.load align(8) %{{[0-9]+}} : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
5555
// CIR: %{{[0-9]+}} = cir.cast(bitcast, %{{[0-9]+}} : !cir.ptr<!ty_B>), !cir.ptr<!cir.ptr<!void>>
@@ -80,7 +80,7 @@ int f() {
8080
// LLVM: }
8181

8282
// Class C constructor
83-
// CIR: cir.func linkonce_odr @_ZN1CC2Ev(%arg0: !cir.ptr<!ty_C> loc({{.*}}), %arg1: !cir.ptr<!cir.ptr<!void>> loc({{.*}})) extra(#fn_attr) {
83+
// CIR: cir.func linkonce_odr @_ZN1CC2Ev(%arg0: !cir.ptr<!ty_C>
8484
// CIR: %{{[0-9]+}} = cir.vtt.address_point %{{[0-9]+}} : !cir.ptr<!cir.ptr<!void>>, offset = 0 -> !cir.ptr<!cir.ptr<!void>>
8585
// CIR: %{{[0-9]+}} = cir.load align(8) %{{[0-9]+}} : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
8686
// CIR: %{{[0-9]+}} = cir.cast(bitcast, %{{[0-9]+}} : !cir.ptr<!ty_C>), !cir.ptr<!cir.ptr<!void>>
@@ -100,7 +100,7 @@ int f() {
100100
// CIR: }
101101

102102
// Class D constructor
103-
// CIR: cir.func linkonce_odr @_ZN1DC1Ev(%arg0: !cir.ptr<!ty_D> loc({{.*}})) extra(#fn_attr) {
103+
// CIR: cir.func linkonce_odr @_ZN1DC1Ev(%arg0: !cir.ptr<!ty_D>
104104
// CIR: %{{[0-9]+}} = cir.alloca !cir.ptr<!ty_D>, !cir.ptr<!cir.ptr<!ty_D>>, ["this", init] {alignment = 8 : i64}
105105
// CIR: cir.store %arg0, %{{[0-9]+}} : !cir.ptr<!ty_D>, !cir.ptr<!cir.ptr<!ty_D>>
106106
// CIR: %[[D_PTR:.*]] = cir.load %0 : !cir.ptr<!cir.ptr<!ty_D>>, !cir.ptr<!ty_D>
@@ -148,4 +148,37 @@ int f() {
148148
// LLVM: %[[BASE_C:.*]] = getelementptr i8, ptr %[[THIS]], i32 16
149149
// LLVM: call void @_ZN1CC2Ev(ptr %[[BASE_C]], ptr getelementptr inbounds ([7 x ptr], ptr @_ZTT1D, i32 0, i32 3))
150150
// LLVM: ret void
151-
// LLVM: }
151+
// LLVM: }
152+
153+
namespace other {
154+
struct A {
155+
A();
156+
~A();
157+
};
158+
159+
struct B : virtual A {
160+
B();
161+
~B();
162+
};
163+
164+
extern int foo();
165+
B::B() {
166+
int x = foo();
167+
}
168+
169+
B::~B() {
170+
int y = foo();
171+
}
172+
}
173+
174+
// CIR-LABEL: cir.func @_ZN5other1BD1Ev(
175+
// CIR-SAME: %[[VAL_0:.*]]: !cir.ptr<!ty_other3A3AB>
176+
// CIR: %[[VAL_1:.*]] = cir.alloca !cir.ptr<!ty_other3A3AB>, !cir.ptr<!cir.ptr<!ty_other3A3AB>>, ["this", init] {alignment = 8 : i64}
177+
// CIR: cir.store %[[VAL_0]], %[[VAL_1]] : !cir.ptr<!ty_other3A3AB>, !cir.ptr<!cir.ptr<!ty_other3A3AB>>
178+
// CIR: %[[VAL_2:.*]] = cir.load %[[VAL_1]] : !cir.ptr<!cir.ptr<!ty_other3A3AB>>, !cir.ptr<!ty_other3A3AB>
179+
// CIR: %[[VAL_3:.*]] = cir.vtt.address_point @_ZTTN5other1BE, offset = 0 -> !cir.ptr<!cir.ptr<!void>>
180+
// CIR: cir.call @_ZN5other1BD2Ev(%[[VAL_2]], %[[VAL_3]]) : (!cir.ptr<!ty_other3A3AB>, !cir.ptr<!cir.ptr<!void>>) -> ()
181+
// CIR: %[[VAL_4:.*]] = cir.base_class_addr(%[[VAL_2]] : !cir.ptr<!ty_other3A3AB> nonnull) [0] -> !cir.ptr<!ty_other3A3AA>
182+
// CIR: cir.call @_ZN5other1AD2Ev(%[[VAL_4]]) : (!cir.ptr<!ty_other3A3AA>) -> ()
183+
// CIR: cir.return
184+
// CIR: }

0 commit comments

Comments
 (0)