https://github.com/npmania updated https://github.com/llvm/llvm-project/pull/240658
>From 7aab9449dd0b451d2e93b35a5a699bf117fcfbe8 Mon Sep 17 00:00:00 2001 From: Jaeyong Lee <[email protected]> Date: Tue, 10 Nov 2026 10:35:22 +0900 Subject: [PATCH 1/4] [CIR] Fix bitfield layout before empty fields Do not skip empty fields that are still emitted in the record layout in accumulateBitFields. Fixes https://github.com/llvm/llvm-project/issues/235744 --- .../CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp index 2ed7d34320390d..79d976029c6b5f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp @@ -195,6 +195,14 @@ struct CIRRecordLowering final { /*isNamedBitField=*/false); } + bool isEmptyFieldForCIRLayout(const FieldDecl *fd) const { + if (fd->isZeroLengthBitField()) + return true; + return fd->isPotentiallyOverlapping() && + CodeGenUtils::isEmptyFieldForLayout(astContext, fd) && + isEmptyFieldForABI(astContext, fd); + } + /// The mark for a base subobject. A base contributes no ABI data when it is /// empty for the ABI, which is not the same as CXXRecordDecl::isEmpty(): a /// base holding only unnamed bit-fields is laid out but carries no data. @@ -636,7 +644,7 @@ CIRRecordLowering::accumulateBitFields(RecordDecl::field_iterator field, // non-reusable tail padding. CharUnits limitOffset; for (auto probe = field; probe != fieldEnd; ++probe) - if (!CodeGenUtils::isEmptyFieldForLayout(astContext, *probe)) { + if (!isEmptyFieldForCIRLayout(*probe)) { // A member with storage sets the limit. assert((getFieldBitOffset(*probe) % charBits) == 0 && "Next storage is not byte-aligned"); @@ -734,8 +742,7 @@ void CIRRecordLowering::accumulateFields(bool nonVirtualBaseType) { field = accumulateBitFields(field, fieldEnd); assert((field == fieldEnd || !field->isBitField()) && "Failed to accumulate all the bitfields"); - } else if (CodeGenUtils::isEmptyFieldForLayout(astContext, *field) && - field->isPotentiallyOverlapping()) { + } else if (isEmptyFieldForCIRLayout(*field)) { // We lay out normal empty fields, as they are required for GEPs/getting // function pointers. However 'no-unique-address' lends some additional // complexity. These fields take up no real space, but would also have to @@ -747,15 +754,6 @@ void CIRRecordLowering::accumulateFields(bool nonVirtualBaseType) { // problem with taking the address of one of these, so it is in practice // not a horrifyingly problematic issue. assert(!cir::MissingFeatures::noUniqueAddressLayout()); - // Dropping the field leaves no member to mark, so its bytes read as - // padding. That is only sound when the field carries no ABI data, so - // make sure we add a member for it. if it does. - if (!isEmptyFieldForABI(astContext, *field)) - members.push_back( - MemberInfo(bitsToCharUnits(getFieldBitOffset(*field)), - MemberInfo::InfoKind::Field, - getStorageType(field->getType()->getAsCXXRecordDecl()), - getFieldMemberKind(*field), *field)); ++field; } else { // Use base subobject layout for potentially-overlapping fields, >From 72f5f8b1250246df584dcb71fdd92ed252d61ec5 Mon Sep 17 00:00:00 2001 From: Jaeyong Lee <[email protected]> Date: Tue, 10 Nov 2026 11:15:00 +0900 Subject: [PATCH 2/4] Add regression tests --- .../test/CIR/CodeGen/record-member-kinds.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/clang/test/CIR/CodeGen/record-member-kinds.cpp b/clang/test/CIR/CodeGen/record-member-kinds.cpp index 9a2df738ca9911..fc566290d6f4d8 100644 --- a/clang/test/CIR/CodeGen/record-member-kinds.cpp +++ b/clang/test/CIR/CodeGen/record-member-kinds.cpp @@ -167,6 +167,32 @@ struct NamedFirst { int a : 8; int : 16; }; struct UnnamedFirst { int : 16; int a : 8; }; // CIR-DAG: !rec_UnnamedFirst = !cir.struct<"UnnamedFirst" {bitfield !cir.bitfield<!u32i, [#cir.bitfield_decl<!s32i, 16, unnamed>, #cir.bitfield_decl<!s32i, 8>]>}> +struct BitfieldBeforeEmpty { + unsigned a : 16; + unsigned b : 8; + Empty e; +}; +// CIR-DAG: !rec_BitfieldBeforeEmpty = !cir.struct<"BitfieldBeforeEmpty" {bitfield !cir.bitfield<!u16i, [#cir.bitfield_decl<!u32i, 16>]>, bitfield !cir.bitfield<!u8i, [#cir.bitfield_decl<!u32i, 8>]>, data !rec_Empty}> +// LLVMCIR-DAG: %struct.BitfieldBeforeEmpty = type { i16, i8, %struct.Empty } +// OGCG-DAG: %struct.BitfieldBeforeEmpty = type { i32 } + +struct BitfieldBeforeNuaEmpty { + unsigned a : 16; + unsigned b : 8; + [[no_unique_address]] Empty e; +}; +// CIR-DAG: !rec_BitfieldBeforeNuaEmpty = !cir.struct<"BitfieldBeforeNuaEmpty" {bitfield !cir.bitfield<!u32i, [#cir.bitfield_decl<!u32i, 16>, #cir.bitfield_decl<!u32i, 8>]>}> +// LLVM-DAG: %struct.BitfieldBeforeNuaEmpty = type { i32 } + +struct BitfieldBeforeNuaHoldsAbiData { + unsigned a : 16; + unsigned b : 8; + [[no_unique_address]] EmptyForLayoutOnly e; +}; +// CIR-DAG: !rec_BitfieldBeforeNuaHoldsAbiData = !cir.struct<"BitfieldBeforeNuaHoldsAbiData" {bitfield !cir.bitfield<!u16i, [#cir.bitfield_decl<!u32i, 16>]>, bitfield !cir.bitfield<!u8i, [#cir.bitfield_decl<!u32i, 8>]>, data !rec_EmptyForLayoutOnly}> +// LLVMCIR-DAG: %struct.BitfieldBeforeNuaHoldsAbiData = type { i16, i8, %struct.EmptyForLayoutOnly } +// OGCG-DAG: %struct.BitfieldBeforeNuaHoldsAbiData = type { i32 } + // A zero-length bit-field separates one span into two units, so the field // after it sits in a unit of its own. A record can carry a named unit and an // unnamed one at once, in either order. @@ -238,6 +264,9 @@ void useTypes(HoldsEmpty *, NuaEmpty *, NuaEmptyUnion *, NuaPolyUnion *, NuaMixedInner nmi2; TwoFields tf; DeepOuter deep; + BitfieldBeforeEmpty bbe; + BitfieldBeforeNuaEmpty bbne; + BitfieldBeforeNuaHoldsAbiData bbnhad; } // The zero-length bit-field member leaves the record's alignment alone, so >From cf5584d66ebf95f25fb9f21e856824c39bdf2cb9 Mon Sep 17 00:00:00 2001 From: Jaeyong Lee <[email protected]> Date: Tue, 17 Nov 2026 11:10:48 +0900 Subject: [PATCH 3/4] Add comment for isEmptyFieldForCIRLayout --- clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp index 79d976029c6b5f..81833f0c08ea12 100644 --- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp @@ -195,6 +195,7 @@ struct CIRRecordLowering final { /*isNamedBitField=*/false); } + /// Return true if the field has no storage in the CIR record layout. bool isEmptyFieldForCIRLayout(const FieldDecl *fd) const { if (fd->isZeroLengthBitField()) return true; >From 2e2de213c161c26b34dfbcddf6ab176f98475094 Mon Sep 17 00:00:00 2001 From: Jaeyong Lee <[email protected]> Date: Tue, 17 Nov 2026 11:14:49 +0900 Subject: [PATCH 4/4] Update comment in accumulateFields --- clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp index 81833f0c08ea12..82c5070469e95c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp @@ -760,8 +760,9 @@ void CIRRecordLowering::accumulateFields(bool nonVirtualBaseType) { // Use base subobject layout for potentially-overlapping fields, // as it is done in RecordLayoutBuilder. // - // The mark comes from isEmptyFieldForABI, not the isEmptyFieldForLayout - // above. Neither predicate subsumes the other. + // The mark comes from isEmptyFieldForABI, instead of the + // isEmptyFieldForCIRLayout above, since fields emitted here can still be + // empty for the ABI. members.push_back(MemberInfo( bitsToCharUnits(getFieldBitOffset(*field)), MemberInfo::InfoKind::Field, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
