Skip to content

Commit f78515d

Browse files
gitoleglanza
authored andcommitted
[CIR][CodeGen][BugFix] use proper base type for derived class (#404)
In the original codegen a new type is created for the base class, while in CIR we were rewriting the type being processed (due tp misused pointers). This PR fix this, and also makes CIR codegen even with the original one.
1 parent 0c894c9 commit f78515d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,16 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D,
601601
builder.lower(/*nonVirtualBaseType=*/false);
602602

603603
// If we're in C++, compute the base subobject type.
604-
mlir::cir::StructType *BaseTy = nullptr;
604+
mlir::cir::StructType BaseTy;
605605
if (llvm::isa<CXXRecordDecl>(D) && !D->isUnion() &&
606606
!D->hasAttr<FinalAttr>()) {
607-
BaseTy = Ty;
607+
BaseTy = *Ty;
608608
if (builder.astRecordLayout.getNonVirtualSize() !=
609609
builder.astRecordLayout.getSize()) {
610610
CIRRecordLowering baseBuilder(*this, D, /*Packed=*/builder.isPacked);
611+
baseBuilder.lower(/*NonVirtualBaseType=*/true);
611612
auto baseIdentifier = getRecordTypeName(D, ".base");
612-
*BaseTy =
613+
BaseTy =
613614
Builder.getCompleteStructTy(baseBuilder.fieldTypes, baseIdentifier,
614615
/*packed=*/false, D);
615616
// TODO(cir): add something like addRecordTypeName
@@ -630,7 +631,7 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D,
630631

631632
auto RL = std::make_unique<CIRGenRecordLayout>(
632633
Ty ? *Ty : mlir::cir::StructType{},
633-
BaseTy ? *BaseTy : mlir::cir::StructType{},
634+
BaseTy ? BaseTy : mlir::cir::StructType{},
634635
(bool)builder.IsZeroInitializable,
635636
(bool)builder.IsZeroInitializableAsBase);
636637

clang/test/CIR/CodeGen/derived-to-base.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ void C3::Layer::Initialize() {
7777

7878
// CHECK-DAG: !ty_22C23A3ALayer22 = !cir.struct<class "C2::Layer"
7979
// CHECK-DAG: !ty_22C33A3ALayer22 = !cir.struct<struct "C3::Layer"
80+
// CHECK-DAG: !ty_22A22 = !cir.struct<class "A"
81+
// CHECK-DAG: !ty_22A2Ebase22 = !cir.struct<class "A.base"
82+
// CHECK-DAG: !ty_22B22 = !cir.struct<class "B" {!cir.struct<class "A.base"
8083

8184
// CHECK: cir.func @_ZN2C35Layer10InitializeEv
8285

0 commit comments

Comments
 (0)