Skip to content

Commit

Permalink
[CIR][CodeGen][BugFix] use proper base type for derived class (#404)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gitoleg authored and lanza committed Mar 23, 2024
1 parent 0c894c9 commit f78515d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,15 +601,16 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D,
builder.lower(/*nonVirtualBaseType=*/false);

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

auto RL = std::make_unique<CIRGenRecordLayout>(
Ty ? *Ty : mlir::cir::StructType{},
BaseTy ? *BaseTy : mlir::cir::StructType{},
BaseTy ? BaseTy : mlir::cir::StructType{},
(bool)builder.IsZeroInitializable,
(bool)builder.IsZeroInitializableAsBase);

Expand Down
3 changes: 3 additions & 0 deletions clang/test/CIR/CodeGen/derived-to-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ void C3::Layer::Initialize() {

// CHECK-DAG: !ty_22C23A3ALayer22 = !cir.struct<class "C2::Layer"
// CHECK-DAG: !ty_22C33A3ALayer22 = !cir.struct<struct "C3::Layer"
// CHECK-DAG: !ty_22A22 = !cir.struct<class "A"
// CHECK-DAG: !ty_22A2Ebase22 = !cir.struct<class "A.base"
// CHECK-DAG: !ty_22B22 = !cir.struct<class "B" {!cir.struct<class "A.base"

// CHECK: cir.func @_ZN2C35Layer10InitializeEv

Expand Down

0 comments on commit f78515d

Please sign in to comment.