[clang] Allow packing fields into tail padding of record fields (PR #122197)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 8 16:28:21 PST 2025


================
@@ -389,14 +389,28 @@ void CGRecordLowering::accumulateFields(bool isNonVirtualBaseType) {
       // Empty fields have no storage.
       ++Field;
     } else {
-      // Use base subobject layout for the potentially-overlapping field,
-      // as it is done in RecordLayoutBuilder
-      Members.push_back(MemberInfo(
-          bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
-          Field->isPotentiallyOverlapping()
-              ? getStorageType(Field->getType()->getAsCXXRecordDecl())
-              : getStorageType(*Field),
-          *Field));
+      CharUnits CurOffset = bitsToCharUnits(getFieldBitOffset(*Field));
+      llvm::Type *StorageType = getStorageType(*Field);
+
+      // Detect cases when the next field needs to be packed into tail padding
+      // of a record field. This is typically caused by [[no_unique_address]],
+      // but we try to infer when that is the case rather than checking for the
+      // attribute explicitly because the attribute is typically not present in
+      // debug info. Use the base subobject LLVM struct type in these cases,
+      // which will be less than data size bytes.
+      if (const CXXRecordDecl *FieldRD =
+              Field->getType()->getAsCXXRecordDecl()) {
+        CharUnits NextOffset = Layout.getNonVirtualSize();
+        auto NextField = std::next(Field);
----------------
efriedma-quic wrote:

I think NextField needs to be the next non-empty field, specifically, not just any field.

https://github.com/llvm/llvm-project/pull/122197


More information about the cfe-commits mailing list