[clang] [CIR] Avoid bitfield storage overlapping empty fields (PR #240658)
Jaeyong Lee via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 12:54:18 PST 2026
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 <jay at glendalab.com>
Date: Tue, 10 Nov 2026 10:35:22 +0900
Subject: [PATCH 1/5] [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 <jay at glendalab.com>
Date: Tue, 10 Nov 2026 11:15:00 +0900
Subject: [PATCH 2/5] 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 <jay at glendalab.com>
Date: Tue, 17 Nov 2026 11:10:48 +0900
Subject: [PATCH 3/5] 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 <jay at glendalab.com>
Date: Tue, 17 Nov 2026 11:14:49 +0900
Subject: [PATCH 4/5] 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,
>From 2ff7f8263571bc38e380e447c0459d520f850c49 Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Wed, 2 Dec 2026 05:52:28 +0900
Subject: [PATCH 5/5] Update comments and expand bitfield tests
---
.../CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp | 22 +++++++------
.../test/CIR/CodeGen/record-member-kinds.cpp | 32 +++++++++++++++++++
2 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
index 82c5070469e95c..120b14e6fd31db 100644
--- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
@@ -196,6 +196,8 @@ struct CIRRecordLowering final {
}
/// Return true if the field has no storage in the CIR record layout.
+ /// A potentially-overlapping field that still holds ABI data keeps its
+ /// member, since otherwise its bytes read as padding.
bool isEmptyFieldForCIRLayout(const FieldDecl *fd) const {
if (fd->isZeroLengthBitField())
return true;
@@ -744,13 +746,14 @@ void CIRRecordLowering::accumulateFields(bool nonVirtualBaseType) {
assert((field == fieldEnd || !field->isBitField()) &&
"Failed to accumulate all the bitfields");
} 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
- // be the correct 'GEP' offset to work here, but then mess with the
- // layout. We likely need to come up with a new 'type' to support these,
- // then figure out some way to lower these to the correct offset, likely
- // by calculating the correct offset into the struct, and forming a
+ // We lay out normal empty fields for the ABI mark, while
+ // emitAddrOfFieldStorage addresses them by byte offset instead of member
+ // index. However 'no-unique-address' lends some additional complexity.
+ // These fields take up no real space, but would also have to be the
+ // correct 'GEP' offset to work here, but then mess with the layout. We
+ // likely need to come up with a new 'type' to support these, then figure
+ // out some way to lower these to the correct offset, likely by
+ // calculating the correct offset into the struct, and forming a
// replacement by-byte GEP in LowerToLLVM. However, this is only a
// problem with taking the address of one of these, so it is in practice
// not a horrifyingly problematic issue.
@@ -760,9 +763,8 @@ void CIRRecordLowering::accumulateFields(bool nonVirtualBaseType) {
// Use base subobject layout for potentially-overlapping fields,
// as it is done in RecordLayoutBuilder.
//
- // The mark comes from isEmptyFieldForABI, instead of the
- // isEmptyFieldForCIRLayout above, since fields emitted here can still be
- // empty for the ABI.
+ // A field kept here can still be empty for the ABI (a C empty struct
+ // or zero-length array), so its mark comes from isEmptyFieldForABI.
members.push_back(MemberInfo(
bitsToCharUnits(getFieldBitOffset(*field)),
MemberInfo::InfoKind::Field,
diff --git a/clang/test/CIR/CodeGen/record-member-kinds.cpp b/clang/test/CIR/CodeGen/record-member-kinds.cpp
index fc566290d6f4d8..c06baf5600e86d 100644
--- a/clang/test/CIR/CodeGen/record-member-kinds.cpp
+++ b/clang/test/CIR/CodeGen/record-member-kinds.cpp
@@ -167,6 +167,7 @@ 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>]>}>
+// Bitfield storage does not extend past empty fields kept as members.
struct BitfieldBeforeEmpty {
unsigned a : 16;
unsigned b : 8;
@@ -277,6 +278,10 @@ SpanEmptyFirst gSpanEmptyFirst;
// LLVM-DAG: @gSpanEmptyFirst = global %struct.SpanEmptyFirst zeroinitializer, align 4
Empty gEmpty;
AlignasTail gAlignasTail;
+BitfieldBeforeEmpty gBitfieldBeforeEmpty = {0x1234, 0x56, {}};
+// CIR-DAG: cir.global external @gBitfieldBeforeEmpty = #cir.const_record<{#cir.int<4660> : !u16i, #cir.int<86> : !u8i, #cir.zero : !rec_Empty}> : !rec_BitfieldBeforeEmpty
+// LLVMCIR-DAG: @gBitfieldBeforeEmpty = global %struct.BitfieldBeforeEmpty { i16 4660, i8 86, %struct.Empty zeroinitializer }, align 4
+// OGCG-DAG: @gBitfieldBeforeEmpty = global { i8, i8, i8, i8 } { i8 52, i8 18, i8 86, i8 undef }, align 4
int getAlignasTailI() { return gAlignasTail.i; }
@@ -356,3 +361,30 @@ DeepInner *getDeepOuterD(DeepOuter &s) { return &s.d; }
// LLVMCIR: getelementptr inbounds nuw %struct.DeepOuter, ptr %{{.+}}, i32 0, i32 1
// OGCG: define {{.*}} @_Z13getDeepOuterD{{.*}}(
// OGCG: getelementptr inbounds i8, ptr %{{.+}}, i64 4
+
+void setBitfieldBeforeEmptyB(BitfieldBeforeEmpty &s, unsigned v) { s.b = v; }
+// CIR: cir.func{{.*}} @_Z23setBitfieldBeforeEmptyB{{.*}}(
+// CIR: %[[M:.*]] = cir.get_member %{{.+}}[1] {name = "b"} : !cir.ptr<!rec_BitfieldBeforeEmpty> -> !cir.ptr<!u8i>
+// CIR: %{{.*}} = cir.set_bitfield align(2) (#bfi_b, %[[M]] : !cir.ptr<!u8i>, %{{.+}} : !u32i) -> !u32i
+// LLVMCIR: define {{.*}} @_Z23setBitfieldBeforeEmptyB{{.*}}(
+// LLVMCIR: %[[P:.*]] = getelementptr inbounds nuw %struct.BitfieldBeforeEmpty, ptr %{{.+}}, i32 0, i32 1
+// LLVMCIR: %[[T:.*]] = trunc i32 %{{.+}} to i8
+// LLVMCIR: store i8 %[[T]], ptr %[[P]], align 2
+// OGCG: define {{.*}} @_Z23setBitfieldBeforeEmptyB{{.*}}(
+// OGCG: %[[BFP:.*]] = load ptr, ptr %{{.+}}, align 8
+// OGCG: %[[LOAD:.*]] = load i32, ptr %[[BFP]], align 4
+// OGCG: %[[SHL:.*]] = shl i32 %{{.+}}, 16
+// OGCG: %[[CLR:.*]] = and i32 %[[LOAD]], -16711681
+// OGCG: %[[SET:.*]] = or i32 %[[CLR]], %[[SHL]]
+// OGCG: store i32 %[[SET]], ptr %[[BFP]], align 4
+
+EmptyForLayoutOnly *
+getBitfieldBeforeNuaHoldsAbiDataE(BitfieldBeforeNuaHoldsAbiData &s) {
+ return &s.e;
+}
+// CIR: cir.func{{.*}} @_Z33getBitfieldBeforeNuaHoldsAbiDataE{{.*}}(
+// CIR: %{{.*}} = cir.get_member %{{.+}}[2] {name = "e"} : !cir.ptr<!rec_BitfieldBeforeNuaHoldsAbiData> -> !cir.ptr<!rec_EmptyForLayoutOnly>
+// LLVMCIR: define {{.*}} @_Z33getBitfieldBeforeNuaHoldsAbiDataE{{.*}}(
+// LLVMCIR: getelementptr inbounds nuw %struct.BitfieldBeforeNuaHoldsAbiData, ptr %{{.+}}, i32 0, i32 2
+// OGCG: define {{.*}} @_Z33getBitfieldBeforeNuaHoldsAbiDataE{{.*}}(
+// OGCG: getelementptr inbounds i8, ptr %{{.+}}, i64 3
More information about the cfe-commits
mailing list