[clang] [CIR] Classify around padding and zero-width bit-fields on x86_64 (PR #217517)

Adam Smith via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 22 14:53:46 PDT 2026


https://github.com/adams381 updated https://github.com/llvm/llvm-project/pull/217517

>From bc0e19a60623d1b78759215f702b8f027c85b1f3 Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Wed, 19 Aug 2026 20:01:17 -0700
Subject: [PATCH 1/3] [CIR] Classify around padding and zero-width bit-fields
 on x86_64

The bridge turns down any struct whose CIR type has a pad member, which is every
over-aligned struct and every struct with a gap between two members.  The ABI
library could have handled these all along, since it reads each field's offset
and treats a gap as holding nothing.  However, we were handing the library the
padding as though it were a field, so it saw data where there was none.  Now the
pass gives the library only the members that hold data.

A zero-width bit-field has no member at all, so CIRGen records its offset and
declared width in the record's layout metadata for the pass to read back.

Assisted-by: Cursor / claude-opus-5
---
 .../include/clang/CIR/Dialect/IR/CIRAttrs.td  |  37 ++++-
 .../CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp |  41 ++++-
 .../Transforms/CallConvLoweringPass.cpp       | 129 ++++++++++++---
 ...ering-x86_64-empty-bitfield-member-nyi.cpp |  15 ++
 .../call-conv-lowering-x86_64-padded.c        | 153 ++++++++++++++++++
 ...conv-lowering-x86_64-zero-width-bitfield.c | 113 +++++++++++++
 clang/test/CIR/IR/invalid-record-layout.cir   |  61 +++++++
 .../abi-lowering/x86_64-aggregate-nyi.cir     |  87 ++++++----
 .../abi-lowering/x86_64-struct-padded.cir     | 143 ++++++++++++++++
 .../unittests/CIR/RecordTypeMetadataTest.cpp  |  36 ++++-
 10 files changed, 747 insertions(+), 68 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp
 create mode 100644 clang/test/CIR/CodeGen/call-conv-lowering-x86_64-padded.c
 create mode 100644 clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
 create mode 100644 clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index ff66eead4a7db..155eafe9db87e 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -128,7 +128,22 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
     // non-zero power of two.
     PredAttrTrait<"record_align must be a non-zero power of two",
                   CPred<"$record_align != 0 && "
-                        "::llvm::isPowerOf2_64($record_align)">>
+                        "::llvm::isPowerOf2_64($record_align)">>,
+    PredAttrTrait<"zero_width_bitfield offsets and widths must be parallel",
+                  CPred<"!$zero_width_bitfield_offsets == "
+                        "!$zero_width_bitfield_widths && "
+                        "(!$zero_width_bitfield_offsets || "
+                        "$zero_width_bitfield_offsets.size() == "
+                        "$zero_width_bitfield_widths.size())">>,
+    PredAttrTrait<"a zero_width_bitfield width must be a positive multiple of 8",
+                  CPred<"!$zero_width_bitfield_widths || "
+                        "::llvm::all_of($zero_width_bitfield_widths.asArrayRef(),"
+                        " [](int64_t w) { return w > 0 && w % 8 == 0; })">>,
+    PredAttrTrait<"a zero_width_bitfield offset must not be negative",
+                  CPred<"!$zero_width_bitfield_offsets || "
+                        "::llvm::all_of("
+                        "$zero_width_bitfield_offsets.asArrayRef(),"
+                        " [](int64_t o) { return o >= 0; })">>
   ]> {
   let summary = "ABI layout metadata for a record type";
   let description = [{
@@ -143,6 +158,12 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
     - `record_align_in_bytes`: from `ASTRecordLayout::getAlignment()`.
       Needed because CIR's DataLayout cannot account for
       `__attribute__((aligned(N)))`.
+    - `zero_width_bitfield_offsets` and `zero_width_bitfield_widths`: the
+      bit offset and declared bit width of each zero-width bit-field, one
+      entry per bit-field in each list.  A zero-width bit-field occupies no
+      storage and so has no member in the record type, but x86_64 counts its
+      declared type as user data.  Both lists are omitted from a record that
+      declares no zero-width bit-field.
 
     Example:
     ```
@@ -155,7 +176,13 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
         "NonTrivialDtor" = #cir.record_layout<
           arg_passing_kind = cannot_pass_in_regs,
           has_trivial_dtor = false,
-          record_align = 4>
+          record_align = 4>,
+        "ZeroWidth" = #cir.record_layout<
+          arg_passing_kind = can_pass_in_regs,
+          has_trivial_dtor = true,
+          record_align = 16,
+          zero_width_bitfield_offsets = [64],
+          zero_width_bitfield_widths = [32]>
       }
     }
     ```
@@ -164,7 +191,9 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
   let parameters = (ins
     EnumParameter<CIR_ArgPassingKind>:$arg_passing_kind,
     "bool":$has_trivial_dtor,
-    "uint64_t":$record_align
+    "uint64_t":$record_align,
+    OptionalParameter<"mlir::DenseI64ArrayAttr">:$zero_width_bitfield_offsets,
+    OptionalParameter<"mlir::DenseI64ArrayAttr">:$zero_width_bitfield_widths
   );
 
   let assemblyFormat = [{
@@ -172,6 +201,8 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
       `arg_passing_kind` `=` $arg_passing_kind `,`
       `has_trivial_dtor` `=` $has_trivial_dtor `,`
       `record_align` `=` $record_align
+      (`,` `zero_width_bitfield_offsets` `=` $zero_width_bitfield_offsets^)?
+      (`,` `zero_width_bitfield_widths` `=` $zero_width_bitfield_widths^)?
     `>`
   }];
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
index dc06fa90f9d14..d099fd9e764cf 100644
--- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
@@ -868,11 +868,44 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *rd, cir::RecordType *ty) {
     if (auto *cxxRD = dyn_cast<CXXRecordDecl>(rd))
       hasTrivialDestructor = cxxRD->hasTrivialDestructor();
     const auto &astLayout = astContext.getASTRecordLayout(rd);
-    uint64_t recordAlignInBytes = astLayout.getAlignment().getQuantity();
 
-    cgm.addRecordLayout(ty->getName(), cir::RecordLayoutAttr::get(
-                                           mlirCtx, apk, hasTrivialDestructor,
-                                           recordAlignInBytes));
+    // A zero-width bit-field occupies no storage, so the record type has no
+    // member for it, but its declared type still counts as user data for
+    // argument passing.  The member list cannot carry it: a member's offset
+    // comes from the members before it, so adding one would move the rest.
+    SmallVector<int64_t> zeroWidthOffsets;
+    SmallVector<int64_t> zeroWidthWidths;
+    for (const FieldDecl *field : rd->fields()) {
+      if (!field->isZeroLengthBitField())
+        continue;
+      zeroWidthOffsets.push_back(
+          astLayout.getFieldOffset(field->getFieldIndex()));
+      zeroWidthWidths.push_back(astContext.getTypeSize(field->getType()));
+    }
+
+    mlir::DenseI64ArrayAttr offsetsAttr;
+    mlir::DenseI64ArrayAttr widthsAttr;
+    if (!zeroWidthOffsets.empty()) {
+      offsetsAttr = mlir::DenseI64ArrayAttr::get(mlirCtx, zeroWidthOffsets);
+      widthsAttr = mlir::DenseI64ArrayAttr::get(mlirCtx, zeroWidthWidths);
+    }
+
+    auto layoutForAlign = [&](uint64_t alignInBytes) {
+      return cir::RecordLayoutAttr::get(mlirCtx, apk, hasTrivialDestructor,
+                                        alignInBytes, offsetsAttr, widthsAttr);
+    };
+
+    cgm.addRecordLayout(ty->getName(),
+                        layoutForAlign(astLayout.getAlignment().getQuantity()));
+
+    // A base subobject is a separate type under its own name, so a lookup for
+    // it misses the entry above.  Its fields sit where the complete object
+    // puts them, so the bit-field lists carry over, but it covers only the
+    // non-virtual part and so takes getNonVirtualAlignment().
+    if (baseTy && baseTy.getName() && baseTy.getName() != ty->getName())
+      cgm.addRecordLayout(
+          baseTy.getName(),
+          layoutForAlign(astLayout.getNonVirtualAlignment().getQuantity()));
   }
 
   auto rl = std::make_unique<CIRGenRecordLayout>(
diff --git a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
index 70ad91dca6bc0..1636c952ffa60 100644
--- a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
@@ -76,10 +76,11 @@ namespace {
 // consumes.  Integer (including `_BitInt` up to 128 bits) / pointer / bool /
 // floating-point scalars are handled, as are struct / union / array aggregates,
 // `_Complex`, and a fixed-width vector whose width is a power of two.  Other
-// vectors, packed records, a padded record that holds data, a union no member
-// of which spans its declared size, and a union with an empty-record member are
-// reported NYI by classifyX86_64Function so an unsupported signature fails the
-// pass instead of being misclassified.
+// vectors, packed records, a padded record reached through a named bit-field
+// access unit, a record holding an empty-for-ABI member that occupies bytes, a
+// union no member of which spans its declared size, and a union with an
+// empty-record member are reported NYI by classifyX86_64Function so an
+// unsupported signature fails the pass instead of being misclassified.
 //===----------------------------------------------------------------------===//
 
 /// Whether a struct's declared argument-passing kind (from the module's
@@ -93,9 +94,9 @@ static bool recordCanPassInRegs(ModuleOp modOp, cir::RecordType recTy) {
   return layout.getArgPassingKind() == cir::ArgPassingKind::CanPassInRegs;
 }
 
-/// Whether a union member is an empty record, looking through arrays, since an
-/// array of empty records supplies no bytes either.
-static bool unionMemberIsEmpty(mlir::Type ty) {
+/// Whether a member is an empty record, looking through arrays, since an array
+/// of empty records supplies no bytes either.
+static bool memberIsEmptyRecord(mlir::Type ty) {
   while (auto arrTy = dyn_cast<cir::ArrayType>(ty))
     ty = arrTy.getElementType();
   auto recTy = dyn_cast<cir::RecordType>(ty);
@@ -116,6 +117,52 @@ static llvm::Align recordDeclaredAlign(ModuleOp modOp, cir::RecordType recTy,
   return llvm::Align(layout.getRecordAlign());
 }
 
+/// Adds a field for each zero-width bit-field \p recTy declares, taking the bit
+/// offset and declared bit width from the module's record-layout metadata.
+/// Such a bit-field has no member to map, so the field is synthesized here and
+/// flagged as an unnamed bit-field, which the classifier skips when assigning
+/// eightbyte classes and counts when picking the coerce type.  Returns whether
+/// any were added.
+static bool
+appendZeroWidthBitFields(cir::RecordType recTy, llvm::abi::TypeBuilder &tb,
+                         ModuleOp modOp,
+                         SmallVectorImpl<llvm::abi::FieldInfo> &out) {
+  auto layout = cir::tryGetRecordLayout(modOp, recTy.getName());
+  if (!layout)
+    return false;
+  mlir::DenseI64ArrayAttr offsets = layout.getZeroWidthBitfieldOffsets();
+  mlir::DenseI64ArrayAttr widths = layout.getZeroWidthBitfieldWidths();
+  if (!offsets || !widths)
+    return false;
+
+  // The classifier reads a bit-field field's width but not its alignment, so
+  // the declared alignment need not be reproduced.  The union reduction is its
+  // one reader, and a synthesized field never reaches a union.
+  for (auto [offsetBits, widthBits] :
+       llvm::zip_equal(offsets.asArrayRef(), widths.asArrayRef()))
+    out.push_back(
+        llvm::abi::FieldInfo(tb.getIntegerType(widthBits, llvm::Align(1),
+                                               /*Signed=*/false),
+                             offsetBits, /*IsBitField=*/true, widthBits,
+                             /*IsUnnamedBitField=*/true));
+  return true;
+}
+
+/// Whether \p ty contains a bit-field access unit holding a named bit-field,
+/// looking through member records and array element types.  A unit of only
+/// unnamed bit-fields is left out, since it reaches the classifier flagged as
+/// an unnamed bit-field and is handled the way classic CodeGen handles one.
+static bool reachesNamedBitFieldUnit(mlir::Type ty) {
+  if (auto arrTy = dyn_cast<cir::ArrayType>(ty))
+    return reachesNamedBitFieldUnit(arrTy.getElementType());
+  auto recTy = dyn_cast<cir::RecordType>(ty);
+  if (!recTy)
+    return false;
+  if (llvm::any_of(recTy.getMemberKinds(), cir::isBitFieldAccessUnit))
+    return true;
+  return llvm::any_of(recTy.getMembers(), reachesNamedBitFieldUnit);
+}
+
 /// The CIR types the x86_64 bridge handles.  Scalars: an integer up to 128
 /// bits (including `_BitInt` and `__int128`), pointer, bool, void, or any
 /// floating-point type.  Aggregates: a complete struct or union whose members
@@ -213,14 +260,27 @@ static bool isSupportedType(mlir::Type ty, const DataLayout &dl) {
         // to one member, picked by alignment and then by size, and coerces from
         // that member: an empty one can win either comparison and widen the
         // coercion past what classic emits.
-        if (llvm::any_of(members, unionMemberIsEmpty))
+        if (llvm::any_of(members, memberIsEmptyRecord))
           return false;
       }
-    } else if (recTy.getPadded() && !recTy.isEmptyForABI()) {
-      // A pad member the classifier would have to tell apart from data is not
-      // implemented.  An empty record has no data to confuse it with.
+    } else if (recTy.getPadded() && reachesNamedBitFieldUnit(recTy)) {
+      // A named access unit can be narrower than the type its bit-fields were
+      // declared with, and that declared type is what classic CodeGen coerces
+      // from.  CIR does not record it, so classifying here would coerce to the
+      // unit's width instead.  Nested and array members count, not just this
+      // record.
       return false;
     }
+    // Below, an `empty` member that occupies bytes is read as an unnamed access
+    // unit.  A member that is itself empty for the ABI can occupy bytes too, by
+    // holding a unit of its own, and classic CodeGen reaches those bytes
+    // through the member's own fields rather than as a single unit.
+    for (auto [memberTy, kind] :
+         llvm::zip_equal(recTy.getMembers(), recTy.getMemberKinds()))
+      if (kind == cir::RecordMemberKind::Empty &&
+          dl.getTypeSizeInBits(memberTy).getFixedValue() &&
+          memberIsEmptyRecord(memberTy))
+        return false;
     return llvm::all_of(recTy.getMembers(),
                         [&](mlir::Type m) { return isSupportedType(m, dl); });
   }
@@ -352,17 +412,39 @@ static const llvm::abi::Type *mapCIRType(mlir::Type type,
                                  llvm::abi::StructPacking::Default, flags);
         }
 
-        // An accepted non-empty struct is never padded, so every field here
-        // sits at its naturally-aligned offset.
-        uint64_t offsetBits = 0;
-        for (mlir::Type fieldTy : recTy.getMembers()) {
-          const llvm::abi::Type *mappedField =
-              mapCIRType(fieldTy, typeMapper, dl, modOp);
-          offsetBits =
-              llvm::alignTo(offsetBits, dl.getTypeABIAlignment(fieldTy) * 8);
-          fields.push_back(llvm::abi::FieldInfo(mappedField, offsetBits));
-          offsetBits += dl.getTypeSizeInBits(fieldTy).getFixedValue();
+        // Padding is dropped, so the eightbyte rules see its bytes as holding
+        // nothing, while the record's full size set above still spans them.
+        //
+        // An unnamed bit-field access unit is passed along flagged instead,
+        // since classic CodeGen ignores it when assigning eightbyte classes
+        // and counts it when choosing the coerce type.  A member's presence
+        // alone cannot say both.
+        for (auto [idx, fieldTy, kind] :
+             llvm::enumerate(recTy.getMembers(), recTy.getMemberKinds())) {
+          if (kind == cir::RecordMemberKind::Pad)
+            continue;
+          uint64_t widthBits = dl.getTypeSizeInBits(fieldTy).getFixedValue();
+          bool isUnnamedUnit = kind == cir::RecordMemberKind::Empty;
+          if (isUnnamedUnit && widthBits == 0)
+            continue;
+          assert((!isUnnamedUnit || !memberIsEmptyRecord(fieldTy)) &&
+                 "an empty-for-ABI member must not reach the classifier as an "
+                 "unnamed bit-field");
+          fields.push_back(llvm::abi::FieldInfo(
+              mapCIRType(fieldTy, typeMapper, dl, modOp),
+              recTy.getElementOffset(dl, idx) * 8,
+              /*IsBitField=*/isUnnamedUnit, isUnnamedUnit ? widthBits : 0,
+              /*IsUnnamedBitField=*/isUnnamedUnit));
         }
+
+        // The classifier stops walking fields at the first one past the range
+        // it is asked about, so a synthesized field appended out of order would
+        // hide the fields behind it.
+        if (appendZeroWidthBitFields(recTy, tb, modOp, fields))
+          llvm::stable_sort(fields, [](const llvm::abi::FieldInfo &lhs,
+                                       const llvm::abi::FieldInfo &rhs) {
+            return lhs.OffsetInBits < rhs.OffsetInBits;
+          });
         return tb.getRecordType(
             fields, sizeBits, align, llvm::abi::StructPacking::Default,
             /*BaseClasses=*/{}, /*VirtualBaseClasses=*/{}, flags);
@@ -399,6 +481,11 @@ static std::optional<ArgClassification>
 convertABIArgInfo(const llvm::abi::ArgInfo &info, MLIRContext *ctx,
                   mlir::Type origTy) {
   if (info.isDirect()) {
+    // The rewriter reads a coercion from the start of the value's storage, so a
+    // classification naming an offset into it has no representation here.  The
+    // classifier names one when the low eightbyte holds no field.
+    if (info.getDirectOffset())
+      return std::nullopt;
     // The classifier names a coerce type even where it matches the natural
     // type, so a non-null coerce does not by itself mean a rewrite is needed.
     const llvm::abi::Type *coerceAbi = info.getCoerceToType();
diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp
new file mode 100644
index 0000000000000..7fc5018853b2c
--- /dev/null
+++ b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp
@@ -0,0 +1,15 @@
+// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - 2>&1 | FileCheck %s
+
+// `A` holds no data for the ABI, yet it occupies four bytes, because an
+// unnamed bit-field access unit supplies them.  Reached as a member it takes
+// the same `empty` mark a unit does, so classifying it as one would count
+// bytes classic CodeGen counts through the member's own fields.
+struct A { unsigned : 32; };
+
+struct AsBase : A { int i; };
+void take_base(AsBase s) {}
+// CHECK: not yet implemented for type '!cir.struct<"AsBase" {empty
+
+struct AsMember { [[no_unique_address]] A a; int i; };
+void take_member(AsMember s) {}
+// CHECK: not yet implemented for type '!cir.struct<"AsMember" {empty
diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-padded.c b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-padded.c
new file mode 100644
index 0000000000000..414343cbaf544
--- /dev/null
+++ b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-padded.c
@@ -0,0 +1,153 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --check-prefixes=LLVM,LLVM-CIR --input-file=%t-cir.ll %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --check-prefixes=LLVM,LLVM-OGCG --input-file=%t.ll %s
+
+typedef struct { int x; } __attribute__((aligned(8))) HalfPad;
+typedef struct { int x; } __attribute__((aligned(16))) OvInt;
+typedef struct { char c; int i __attribute__((aligned(8))); } MidPad;
+typedef struct { float f; } __attribute__((aligned(16))) OvFloat;
+typedef struct { float a; float b __attribute__((aligned(8))); } TwoFloat;
+typedef struct { int a; int b __attribute__((aligned(16))); } BigPad;
+typedef struct { float a; float b; } __attribute__((aligned(16))) TwoFSame;
+typedef struct { _Complex float c; } __attribute__((aligned(16))) CplxPad;
+
+// One eightbyte in each register class.
+typedef struct { int a; float b __attribute__((aligned(8))); } MixPad;
+
+typedef struct { OvInt o; } NestOv;
+typedef struct { OvInt a[2]; } ArrOv;
+
+typedef struct { OvInt a[1]; } ArrOne;
+
+struct E {};
+typedef struct { struct E e; int i; } __attribute__((aligned(16))) PadWithEmpty;
+
+// CIR-DAG: !rec_HalfPad = !cir.struct<"HalfPad" {data !s32i, pad !cir.array<!u8i x 4>}>
+// CIR-DAG: !rec_OvInt = !cir.struct<"OvInt" {data !s32i, pad !cir.array<!u8i x 12>}>
+// CIR-DAG: !rec_MidPad = !cir.struct<"MidPad" {data !s8i, pad !cir.array<!u8i x 7>, data !s32i, pad !cir.array<!u8i x 4>}>
+// CIR-DAG: !rec_OvFloat = !cir.struct<"OvFloat" {data !cir.float, pad !cir.array<!u8i x 12>}>
+// CIR-DAG: !rec_TwoFloat = !cir.struct<"TwoFloat" {data !cir.float, pad !cir.array<!u8i x 4>, data !cir.float, pad !cir.array<!u8i x 4>}>
+// CIR-DAG: !rec_BigPad = !cir.struct<"BigPad" {data !s32i, pad !cir.array<!u8i x 12>, data !s32i, pad !cir.array<!u8i x 12>}>
+// CIR-DAG: !rec_NestOv = !cir.struct<"NestOv" {data !rec_OvInt}>
+// CIR-DAG: !rec_ArrOv = !cir.struct<"ArrOv" {data !cir.array<!rec_OvInt x 2>}>
+// CIR-DAG: !rec_PadWithEmpty = !cir.struct<"PadWithEmpty" {empty !rec_E, data !s32i, pad !cir.array<!u8i x 12>}>
+// CIR-DAG: !rec_TwoFSame = !cir.struct<"TwoFSame" {data !cir.float, data !cir.float, pad !cir.array<!u8i x 8>}>
+// CIR-DAG: !rec_CplxPad = !cir.struct<"CplxPad" {data !cir.complex<!cir.float>, pad !cir.array<!u8i x 8>}>
+// CIR-DAG: !rec_ArrOne = !cir.struct<"ArrOne" {data !cir.array<!rec_OvInt x 1>}>
+// CIR-DAG: !rec_MixPad = !cir.struct<"MixPad" {data !s32i, pad !cir.array<!u8i x 4>, data !cir.float, pad !cir.array<!u8i x 4>}>
+
+// Anonymous coercion records are numbered in print order, so capture them.
+// CIR-DAG: ![[F64F32:rec_anon_struct[0-9]*]] = !cir.struct<{data !cir.double, data !cir.float}>
+// CIR-DAG: ![[I64I32:rec_anon_struct[0-9]*]] = !cir.struct<{data !u64i, data !s32i}>
+// CIR-DAG: ![[I64F32:rec_anon_struct[0-9]*]] = !cir.struct<{data !u64i, data !cir.float}>
+
+int take_half(HalfPad h) { return h.x; }
+HalfPad ret_half(int v) { HalfPad h = {v}; return h; }
+
+// CIR: cir.func{{.*}} @take_half(%arg0: !s32i loc{{.*}}) -> !s32i
+// CIR: cir.func{{.*}} @ret_half(%arg0: !s32i {llvm.noundef} loc{{.*}}) -> !s32i
+// LLVM: define dso_local i32 @take_half(i32 %{{.+}})
+// LLVM: define dso_local i32 @ret_half(i32 noundef %{{.+}})
+
+// The second eightbyte is entirely padding, so one register still carries it.
+int take_ov(OvInt o) { return o.x; }
+OvInt ret_ov(int v) { OvInt o = {v}; return o; }
+
+// CIR: cir.func{{.*}} @take_ov(%arg0: !s32i loc{{.*}}) -> !s32i
+// CIR: cir.func{{.*}} @ret_ov(%arg0: !s32i {llvm.noundef} loc{{.*}}) -> !s32i
+// LLVM: define dso_local i32 @take_ov(i32 %{{.+}})
+// LLVM: define dso_local i32 @ret_ov(i32 noundef %{{.+}})
+
+// Each eightbyte carries one member, so the argument flattens into two.
+int take_mid(MidPad m) { return m.i; }
+MidPad ret_mid(int v) { MidPad m = {0, v}; return m; }
+
+// CIR: cir.func{{.*}} @take_mid(%arg0: !u64i loc{{.*}}, %arg1: !s32i loc{{.*}}) -> !s32i
+// CIR: cir.func{{.*}} @ret_mid(%arg0: !s32i {llvm.noundef} loc{{.*}}) -> ![[I64I32]]
+// LLVM: define dso_local i32 @take_mid(i64 %{{.+}}, i32 %{{.+}})
+// LLVM: define dso_local { i64, i32 } @ret_mid(i32 noundef %{{.+}})
+
+float take_ovf(OvFloat o) { return o.f; }
+OvFloat ret_ovf(float v) { OvFloat o = {v}; return o; }
+
+// CIR: cir.func{{.*}} @take_ovf(%arg0: !cir.float loc{{.*}}) -> !cir.float
+// CIR: cir.func{{.*}} @ret_ovf(%arg0: !cir.float {llvm.noundef} loc{{.*}}) -> !cir.float
+// LLVM: define dso_local float @take_ovf(float %{{.+}})
+// LLVM: define dso_local float @ret_ovf(float noundef %{{.+}})
+
+// The low eightbyte holds one float and four padding bytes, which the SSE
+// rules widen to a double rather than packing two floats.
+float take_twof(TwoFloat t) { return t.b; }
+TwoFloat ret_twof(float v) { TwoFloat t = {v, v}; return t; }
+
+// CIR: cir.func{{.*}} @take_twof(%arg0: !cir.double loc{{.*}}, %arg1: !cir.float loc{{.*}}) -> !cir.float
+// CIR: cir.func{{.*}} @ret_twof(%arg0: !cir.float {llvm.noundef} loc{{.*}}) -> ![[F64F32]]
+// LLVM: define dso_local float @take_twof(double %{{.+}}, float %{{.+}})
+// LLVM: define dso_local { double, float } @ret_twof(float noundef %{{.+}})
+
+// Past two eightbytes, so MEMORY, and the record's declared alignment reaches
+// the byval and sret attributes rather than the alignment of its members.
+int take_big(BigPad b) { return b.b; }
+BigPad ret_big(int v) { BigPad b = {0, v}; return b; }
+
+// CIR: cir.func{{.*}} @take_big(%arg0: !cir.ptr<!rec_BigPad> {llvm.align = 16 : i64, llvm.byval = !rec_BigPad, llvm.noalias, llvm.noundef} loc{{.*}}) -> !s32i
+// CIR: cir.func{{.*}} @ret_big(%arg0: !cir.ptr<!rec_BigPad> {llvm.align = 16 : i64, llvm.dead_on_unwind, llvm.noalias, llvm.sret = !rec_BigPad, llvm.writable} loc{{.*}}, %arg1: !s32i {llvm.noundef} loc{{.*}})
+// CIR emits noalias on a byval argument where classic does not, here and
+// wherever else this file splits a byval line by backend.
+// LLVM-CIR: define dso_local i32 @take_big(ptr noalias noundef byval(%struct.BigPad) align 16 %{{.+}})
+// LLVM-OGCG: define dso_local i32 @take_big(ptr noundef byval(%struct.BigPad) align 16 %{{.+}})
+// LLVM: define dso_local void @ret_big(ptr dead_on_unwind noalias writable sret(%struct.BigPad) align 16 %{{.+}}, i32 noundef %{{.+}})
+
+// A padded member keeps its register coercion, while two of them outgrow two
+// eightbytes and go to memory.
+int take_nest(NestOv n) { return n.o.x; }
+int take_arr(ArrOv a) { return a.a[1].x; }
+
+// CIR: cir.func{{.*}} @take_nest(%arg0: !s32i loc{{.*}}) -> !s32i
+// CIR: cir.func{{.*}} @take_arr(%arg0: !cir.ptr<!rec_ArrOv> {llvm.align = 16 : i64, llvm.byval = !rec_ArrOv, llvm.noalias, llvm.noundef} loc{{.*}}) -> !s32i
+// LLVM: define dso_local i32 @take_nest(i32 %{{.+}})
+// LLVM-CIR: define dso_local i32 @take_arr(ptr noalias noundef byval(%struct.ArrOv) align 16 %{{.+}})
+// LLVM-OGCG: define dso_local i32 @take_arr(ptr noundef byval(%struct.ArrOv) align 16 %{{.+}})
+
+// The empty member contributes no field and does not shift the int.
+int take_pwe(PadWithEmpty p) { return p.i; }
+PadWithEmpty ret_pwe(int v) { PadWithEmpty p = {{}, v}; return p; }
+
+// CIR: cir.func{{.*}} @take_pwe(%arg0: !s32i loc{{.*}}) -> !s32i
+// CIR: cir.func{{.*}} @ret_pwe(%arg0: !s32i {llvm.noundef} loc{{.*}}) -> !s32i
+// LLVM: define dso_local i32 @take_pwe(i32 %{{.+}})
+// LLVM: define dso_local i32 @ret_pwe(i32 noundef %{{.+}})
+
+// Both floats share the low eightbyte, so the SSE rules pack them into a vector
+// rather than widening to a double as they do for TwoFloat above.
+float take_2fsame(TwoFSame t) { return t.a; }
+TwoFSame ret_2fsame(float v) { TwoFSame t = {v, v}; return t; }
+
+// CIR: cir.func{{.*}} @take_2fsame(%arg0: !cir.vector<2 x !cir.float> loc{{.*}}) -> !cir.float
+// CIR: cir.func{{.*}} @ret_2fsame(%arg0: !cir.float {llvm.noundef} loc{{.*}}) -> !cir.vector<2 x !cir.float>
+// LLVM: define dso_local float @take_2fsame(<2 x float> %{{.+}})
+// LLVM: define dso_local <2 x float> @ret_2fsame(float noundef %{{.+}})
+
+// A _Complex member reaches the same vector coercion under padding.
+_Complex float take_cplx(CplxPad c) { return c.c; }
+
+// CIR: cir.func{{.*}} @take_cplx(%arg0: !cir.vector<2 x !cir.float> loc{{.*}}) -> !cir.vector<2 x !cir.float>
+// LLVM: define dso_local <2 x float> @take_cplx(<2 x float> %{{.+}})
+
+float take_mix(MixPad m) { return m.b; }
+MixPad ret_mix(int v) { MixPad m = {v, 1.0f}; return m; }
+
+// CIR: cir.func{{.*}} @take_mix(%arg0: !u64i loc{{.*}}, %arg1: !cir.float loc{{.*}}) -> !cir.float
+// LLVM: define dso_local float @take_mix(i64 %{{.+}}, float %{{.+}})
+// CIR: cir.func{{.*}} @ret_mix(%arg0: !s32i {llvm.noundef} loc{{.*}}) -> ![[I64F32]]
+// LLVM: define dso_local { i64, float } @ret_mix(i32 noundef %{{.+}})
+
+// One element, so the element's own padding is classified rather than the whole
+// record going to memory.
+int take_arrone(ArrOne a) { return a.a[0].x; }
+
+// CIR: cir.func{{.*}} @take_arrone(%arg0: !s32i loc{{.*}}) -> !s32i
+// LLVM: define dso_local i32 @take_arrone(i32 %{{.+}})
diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
new file mode 100644
index 0000000000000..987d176511120
--- /dev/null
+++ b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
@@ -0,0 +1,113 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --check-prefixes=LLVM,LLVM-CIR --input-file=%t-cir.ll %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --check-prefixes=LLVM,LLVM-OGCG --input-file=%t.ll %s
+
+typedef struct { int x; unsigned : 0; } __attribute__((aligned(16))) Tail;
+typedef struct { char c; int : 0; int y; } Mid;
+typedef struct { int x; int : 0; int y; } Plain;
+typedef struct { char c; long long : 0; } __attribute__((aligned(16))) Wide;
+typedef struct { char c; int : 0; short s; int : 0; } Twice;
+typedef struct { int x; } __attribute__((aligned(16))) NoZeroWidth;
+typedef struct { Tail t; } Nest;
+struct { int x; unsigned : 0; } __attribute__((aligned(16))) gAnon;
+typedef struct { long long a; int b; unsigned : 0; } Pair;
+typedef struct { char c; int : 0; char d[24]; } TooBig;
+typedef struct { char c; int : 0; long long y; } AfterZeroWidth;
+typedef struct { char c; long long y; } NoZeroWidthPair;
+typedef struct { double d; int : 32; } __attribute__((aligned(16))) UnnamedUnit;
+
+// CIR-DAG: AfterZeroWidth = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
+// CIR-DAG: Mid = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
+// CIR-DAG: Pair = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [96], zero_width_bitfield_widths = [32]>
+// CIR-DAG: Plain = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
+// CIR-DAG: Tail = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
+// CIR-DAG: TooBig = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
+// CIR-DAG: Twice = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32, 64], zero_width_bitfield_widths = [32, 32]>
+// CIR-DAG: Wide = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [64], zero_width_bitfield_widths = [64]>
+// CIR-DAG: anon.0 = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
+
+// A record that declares none carries no list, so record_align closes the
+// attribute.  `Nest` declares none itself, the bit-field being the member's.
+// CIR-DAG: Nest = #cir.record_layout<{{[^>]*}}record_align = 16>
+// CIR-DAG: NoZeroWidth = #cir.record_layout<{{[^>]*}}record_align = 16>
+
+// The bit-field extends the eightbyte's user data past `x`, so it stays i64
+// instead of narrowing to i32 the way the same over-aligned shape does without
+// one.  Compare take_no_zero_width below.
+void take_tail(Tail t) {}
+// CIR: cir.func{{.*}} @take_tail(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_tail(i64 %{{.+}})
+
+// `y` already carries the eightbyte's user data to bit 64, so the bit-field
+// changes nothing here and in take_plain.
+void take_mid(Mid m) {}
+// CIR: cir.func{{.*}} @take_mid(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_mid(i64 %{{.+}})
+
+void take_plain(Plain p) {}
+// CIR: cir.func{{.*}} @take_plain(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_plain(i64 %{{.+}})
+
+// The bit-field sits at bit 64, past the eightbyte holding `c`, so it widens
+// nothing.
+void take_wide(Wide w) {}
+// CIR: cir.func{{.*}} @take_wide(%arg0: !s8i loc
+// LLVM: define{{.*}} void @take_wide(i8 %{{.+}})
+
+// `s` reaches bit 48 and the second bit-field starts at 64, so the pair of
+// entries above is what this case asserts rather than the argument type.
+void take_twice(Twice t) {}
+// CIR: cir.func{{.*}} @take_twice(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_twice(i64 %{{.+}})
+
+void take_no_zero_width(NoZeroWidth n) {}
+// CIR: cir.func{{.*}} @take_no_zero_width(%arg0: !s32i loc
+// LLVM: define{{.*}} void @take_no_zero_width(i32 %{{.+}})
+
+void take_nest(Nest n) {}
+// CIR: cir.func{{.*}} @take_nest(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_nest(i64 %{{.+}})
+
+void take_anon(__typeof__(gAnon) a) {}
+// CIR: cir.func{{.*}} @take_anon(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_anon(i64 %{{.+}})
+
+// The bit-field lands in the second eightbyte, which is classified and coerced
+// on its own.
+void take_pair(Pair p) {}
+// CIR: cir.func{{.*}} @take_pair(%arg0: !s64i loc{{.*}}, %arg1: !u64i loc
+// LLVM: define{{.*}} void @take_pair(i64 %{{.+}}, i64 %{{.+}})
+
+// A data member sits past the bit-field, so the field list only stays in offset
+// order if the synthesized entry is placed rather than appended.
+void take_after_zero_width(AfterZeroWidth a) {}
+// CIR: cir.func{{.*}} @take_after_zero_width(%arg0: !u64i loc{{.*}}, %arg1: !s64i loc
+// LLVM: define{{.*}} void @take_after_zero_width(i64 %{{.+}}, i64 %{{.+}})
+
+void take_no_zw(NoZeroWidthPair n) {}
+// CIR: cir.func{{.*}} @take_no_zw(%arg0: !s8i loc{{.*}}, %arg1: !s64i loc
+// LLVM: define{{.*}} void @take_no_zw(i8 %{{.+}}, i64 %{{.+}})
+
+// The unit occupies four bytes after `d`.  Counting it as data would stop this
+// being a single SSE value and pass it as a double plus an i32.
+void take_unnamed_unit(UnnamedUnit u) {}
+// CIR: cir.func{{.*}} @take_unnamed_unit(%arg0: !cir.double loc
+// LLVM: define{{.*}} void @take_unnamed_unit(double %{{.+}})
+
+// Past two eightbytes the record goes to memory, where the widened coerce type
+// no longer applies.
+void take_too_big(TooBig b) {}
+// CIR: cir.func{{.*}} @take_too_big(%arg0: !cir.ptr<!rec_TooBig> {llvm.align = 8 : i64, llvm.byval = !rec_TooBig, llvm.noalias, llvm.noundef}
+// LLVM-CIR: define{{.*}} void @take_too_big(ptr noalias noundef byval(%struct.TooBig) align 8 %{{.+}})
+// LLVM-OGCG: define{{.*}} void @take_too_big(ptr noundef byval(%struct.TooBig) align 8 %{{.+}})
+
+Tail ret_tail(void) { Tail t = {3}; return t; }
+// CIR: cir.func{{.*}} @ret_tail() -> !u64i
+// LLVM: define{{.*}} i64 @ret_tail()
+
+Pair ret_pair(void) { Pair p = {1, 2}; return p; }
+// CIR: cir.func{{.*}} @ret_pair() -> !rec_anon_struct
+// LLVM: define{{.*}} { i64, i64 } @ret_pair()
diff --git a/clang/test/CIR/IR/invalid-record-layout.cir b/clang/test/CIR/IR/invalid-record-layout.cir
index 61ebc9b37528c..4682985dd4e1e 100644
--- a/clang/test/CIR/IR/invalid-record-layout.cir
+++ b/clang/test/CIR/IR/invalid-record-layout.cir
@@ -17,3 +17,64 @@ module attributes {
                            has_trivial_dtor = true, record_align = 3>}
 } {
 }
+
+// -----
+
+module attributes {
+  cir.record_layouts = {
+    // expected-error @below {{failed to verify that zero_width_bitfield offsets and widths must be parallel}}
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                           has_trivial_dtor = true, record_align = 4,
+                           zero_width_bitfield_offsets = [0, 32],
+                           zero_width_bitfield_widths = [32]>}
+} {
+}
+
+// -----
+
+module attributes {
+  cir.record_layouts = {
+    // expected-error @below {{failed to verify that zero_width_bitfield offsets and widths must be parallel}}
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                           has_trivial_dtor = true, record_align = 4,
+                           zero_width_bitfield_offsets = [0]>}
+} {
+}
+
+// -----
+
+// A width reaches the classifier as an integer type, whose size is read in
+// bytes, so a sub-byte width has no type to carry it.
+module attributes {
+  cir.record_layouts = {
+    // expected-error @below {{failed to verify that a zero_width_bitfield width must be a positive multiple of 8}}
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                           has_trivial_dtor = true, record_align = 4,
+                           zero_width_bitfield_offsets = [0],
+                           zero_width_bitfield_widths = [4]>}
+} {
+}
+
+// -----
+
+module attributes {
+  cir.record_layouts = {
+    // expected-error @below {{failed to verify that a zero_width_bitfield width must be a positive multiple of 8}}
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                           has_trivial_dtor = true, record_align = 4,
+                           zero_width_bitfield_offsets = [0],
+                           zero_width_bitfield_widths = [0]>}
+} {
+}
+
+// -----
+
+module attributes {
+  cir.record_layouts = {
+    // expected-error @below {{failed to verify that a zero_width_bitfield offset must not be negative}}
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                           has_trivial_dtor = true, record_align = 4,
+                           zero_width_bitfield_offsets = [-8],
+                           zero_width_bitfield_widths = [32]>}
+} {
+}
diff --git a/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir b/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir
index 22d8fb3d221b1..188f57230fb39 100644
--- a/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir
+++ b/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir
@@ -4,6 +4,7 @@
 !s16i = !cir.int<s, 16>
 !s32i = !cir.int<s, 32>
 !u8i = !cir.int<u, 8>
+!u32i = !cir.int<u, 32>
 !rec_UPacked = !cir.union<"UPacked" packed {data !s32i, data !cir.array<!s8i x 5>}, padding = {!u8i}>
 !rec_UOverAligned = !cir.union<"UOverAligned" {data !s32i}, padding = {!cir.array<!u8i x 12>}>
 !rec_UShortStorage = !cir.union<"UShortStorage" {data !s16i, data !cir.array<!s8i x 3>}, padding = {!cir.array<!u8i x 2>}>
@@ -11,17 +12,22 @@
 !rec_SWrapsOverAligned = !cir.struct<"SWrapsOverAligned" {data !cir.double, data !rec_UOverAligned}>
 !rec_UEmptyLarge = !cir.union<"UEmptyLarge" {}, padding = {!cir.array<!u8i x 32>}>
 !rec_P = !cir.struct<"P" packed {data !s8i, data !s32i}>
-!rec_Ov = !cir.struct<"Ov" {data !s32i, pad !cir.array<!u8i x 12>}>
 !rec_E = !cir.struct<"E" {pad !u8i}>
 !rec_EOver = !cir.struct<"EOver" {pad !cir.array<!u8i x 16>}>
-!rec_PadByte = !cir.struct<"PadByte" {data !u8i, pad !cir.array<!u8i x 3>}>
-!rec_PadByte2 = !cir.struct<"PadByte2" {data !u8i, pad !u8i}>
 !rec_UPadByte = !cir.union<"UPadByte" {data !u8i}, padding = {!cir.array<!u8i x 3>}>
 !rec_UEmptyOnly = !cir.union<"UEmptyOnly" {data !rec_E}>
 !rec_UEmptyOver = !cir.union<"UEmptyOver" {data !rec_EOver, data !s32i}, padding = {!cir.array<!u8i x 12>}>
 !rec_UArrEmpty = !cir.union<"UArrEmpty" {data !cir.array<!rec_E x 2>, data !s8i}>
 !rec_S1 = !cir.struct<"S1" {data !s16i, data !s16i, data !s16i}>
 !rec_AtomicWrapper = !cir.struct<{data !rec_S1, pad !cir.array<!s8i x 2>}>
+!rec_BitPad = !cir.struct<"BitPad" {bitfield !u8i, pad !cir.array<!u8i x 15>}>
+!rec_InnerBits = !cir.struct<"InnerBits" {bitfield !u32i}>
+!rec_OuterBitPad = !cir.struct<"OuterBitPad" {data !rec_InnerBits, pad !cir.array<!u8i x 12>}>
+!rec_ArrBitPad = !cir.struct<"ArrBitPad" {data !cir.array<!rec_InnerBits x 2>, pad !cir.array<!u8i x 8>}>
+!rec_HoldsAllPad = !cir.struct<"HoldsAllPad" {data !s32i, empty !rec_E}>
+!rec_EBits = !cir.struct<"EBits" {empty !u32i}>
+!rec_HoldsEmptyBits = !cir.struct<"HoldsEmptyBits" {empty !rec_EBits, data !s32i}>
+!rec_LeadPad = !cir.struct<"LeadPad" {pad !cir.array<!u8i x 8>, data !s32i}>
 
 module attributes {
   dlti.dl_spec = #dlti.dl_spec<
@@ -94,30 +100,6 @@ module attributes {
 
   // CHECK: not yet implemented for type '!cir.struct<"P" packed
 
-  // A padded (over-aligned) struct is rejected: it needs pad-aware
-  // classification.
-  cir.func @take_padded(%arg0: !rec_Ov) {
-    cir.return
-  }
-
-  // CHECK: not yet implemented for type '!cir.struct<"Ov" {data !cir.int<s, 32>, pad
-
-  // `struct { unsigned char c; } __attribute__((aligned(4)))` is padded
-  // throughout, but it carries data, so it stays rejected.
-  cir.func @take_padded_byte(%arg0: !rec_PadByte) {
-    cir.return
-  }
-
-  // CHECK: not yet implemented for type '!cir.struct<"PadByte" {data
-
-  // The same at `aligned(2)`, where the padding is a scalar rather than an
-  // array.
-  cir.func @take_padded_byte2(%arg0: !rec_PadByte2) {
-    cir.return
-  }
-
-  // CHECK: not yet implemented for type '!cir.struct<"PadByte2" {data
-
   // A union holds padding in a slot of its own, so its lone byte member is
   // data.  Rejected by the union rule (no member spans the declared size), not
   // the empty-class one.
@@ -148,15 +130,56 @@ module attributes {
   }
 
   // CHECK: not yet implemented for type '!cir.union<"UArrEmpty"
+  // A bit-field access unit is narrower than the declaration it holds, so
+  // classifying around this record's padding would coerce to i8 where classic
+  // CodeGen widens to i64.
+  cir.func @take_bit_pad(%arg0: !rec_BitPad) {
+    cir.return
+  }
+
+  // CHECK: not yet implemented for type '!cir.struct<"BitPad" {bitfield
+
+  // The reject reaches through a member record.  Classic coerces this one to
+  // i32, so the rejection is conservative rather than a divergence avoided.
+  cir.func @take_outer_bit_pad(%arg0: !rec_OuterBitPad) {
+    cir.return
+  }
+
+  // CHECK: not yet implemented for type '!cir.struct<"OuterBitPad" {data
+
+  // And through an array element.
+  cir.func @take_arr_bit_pad(%arg0: !rec_ArrBitPad) {
+    cir.return
+  }
+
+  // CHECK: not yet implemented for type '!cir.struct<"ArrBitPad" {data
+
+  // The all-pad reject also has to hold when the record is reached as a member,
+  // which is the shape a C++ empty class produces.
+  cir.func @take_holds_all_pad(%arg0: !rec_HoldsAllPad) {
+    cir.return
+  }
+
+  // CHECK: not yet implemented for type '!cir.struct<"HoldsAllPad" {data
+
+  // An empty-for-ABI member can occupy bytes without a pad member, by holding
+  // an unnamed access unit of its own.  Classic CodeGen reaches those bytes
+  // through the member's own fields, so this is refused.  An empty base or a
+  // [[no_unique_address]] member of `struct { unsigned : 32; }` produces it.
+  cir.func @take_holds_empty_bits(%arg0: !rec_HoldsEmptyBits) {
+    cir.return
+  }
+
+  // CHECK: not yet implemented for type '!cir.struct<"HoldsEmptyBits" {empty
 
-  // CIRGen wraps an atomic whose value is narrower than its lock-free size in
-  // an anonymous record with a trailing pad member, which the padded check
-  // rejects for the same reason.
-  cir.func @take_atomic_wrapper(%arg0: !rec_AtomicWrapper) {
+  // With no field in the low eightbyte the classifier coerces the high one and
+  // names an offset of 8 into the record, which the rewriter cannot express: it
+  // reads a coercion from offset 0, so the argument would land in the padding.
+  cir.func @take_lead_pad(%arg0: !rec_LeadPad) {
     cir.return
   }
 
-  // CHECK: not yet implemented for type '!cir.struct<{{.*}}pad !cir.array<!cir.int<s, 8> x 2>}>'
+  // CHECK: not yet implemented for the ABI coercion of type '!cir.struct<"LeadPad"
 
   // A scalable vector has no size the eightbyte rules can read.
   cir.func @take_scalable(%arg0: !cir.vector<[4] x !cir.float>) {
diff --git a/clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir b/clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir
new file mode 100644
index 0000000000000..6c91ba0c2d0ff
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir
@@ -0,0 +1,143 @@
+// RUN: cir-opt %s -cir-call-conv-lowering=target=x86_64 | FileCheck %s
+
+!s8i = !cir.int<s, 8>
+!s16i = !cir.int<s, 16>
+!s32i = !cir.int<s, 32>
+!u8i = !cir.int<u, 8>
+!u32i = !cir.int<u, 32>
+!u64i = !cir.int<u, 64>
+!rec_Ov = !cir.struct<"Ov" {data !s32i, pad !cir.array<!u8i x 12>}>
+!rec_Mid = !cir.struct<"Mid" {data !s8i, pad !cir.array<!u8i x 7>, data !s32i, pad !cir.array<!u8i x 4>}>
+!rec_Big = !cir.struct<"Big" {data !s32i, pad !cir.array<!u8i x 12>, data !s32i, pad !cir.array<!u8i x 12>}>
+!rec_NoRegs = !cir.struct<"NoRegs" {data !s32i, pad !cir.array<!u8i x 12>}>
+!rec_S1 = !cir.struct<"S1" {data !s16i, data !s16i, data !s16i}>
+!rec_anon_pad = !cir.struct<{data !rec_S1, pad !cir.array<!s8i x 2>}>
+!rec_UnnamedUnit = !cir.struct<"UnnamedUnit" {data !s32i, empty !u32i, pad !cir.array<!u8i x 8>}>
+!rec_Zw = !cir.struct<"Zw" {data !s32i, pad !cir.array<!u8i x 12>}>
+!rec_UnitSSE = !cir.struct<"UnitSSE" {data !cir.double, empty !u32i, pad !cir.array<!u8i x 4>}>
+!rec_PadByte = !cir.struct<"PadByte" {data !u8i, pad !cir.array<!u8i x 3>}>
+!rec_PadByte2 = !cir.struct<"PadByte2" {data !u8i, pad !u8i}>
+
+module attributes {
+  cir.triple = "x86_64-unknown-linux-gnu",
+  cir.record_layouts = {
+    Big = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                             has_trivial_dtor = true, record_align = 16>,
+    Mid = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                             has_trivial_dtor = true, record_align = 8>,
+    NoRegs = #cir.record_layout<arg_passing_kind = cannot_pass_in_regs,
+                                has_trivial_dtor = false, record_align = 16>,
+    Ov = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                            has_trivial_dtor = true, record_align = 16>,
+    UnnamedUnit = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                                     has_trivial_dtor = true,
+                                     record_align = 16>,
+    PadByte = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                                 has_trivial_dtor = true, record_align = 4>,
+    PadByte2 = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                                  has_trivial_dtor = true, record_align = 2>,
+    UnitSSE = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                                 has_trivial_dtor = true, record_align = 16>,
+    Zw = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                            has_trivial_dtor = true, record_align = 16,
+                            zero_width_bitfield_offsets = [32],
+                            zero_width_bitfield_widths = [32]>
+  },
+  dlti.dl_spec = #dlti.dl_spec<
+    #dlti.dl_entry<i8, dense<8>: vector<2xi64>>,
+    #dlti.dl_entry<i16, dense<16>: vector<2xi64>>,
+    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
+} {
+
+  // The coerce slot is allocated as the whole 16-byte record and bitcast down to
+  // the 4-byte coerce type, not the other way round, or the reload would read
+  // past the slot.
+  cir.func @take_ov(%arg0: !rec_Ov) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_ov(%arg0: !s32i)
+  // CHECK:   %[[SLOT:.*]] = cir.alloca "coerce"{{.*}}: !cir.ptr<!rec_Ov>
+  // CHECK:   %[[VIEW:.*]] = cir.cast bitcast %[[SLOT]] : !cir.ptr<!rec_Ov> -> !cir.ptr<!s32i>
+  // CHECK:   cir.store %arg0, %[[VIEW]] : !s32i, !cir.ptr<!s32i>
+  // CHECK:   %{{.+}} = cir.load %[[SLOT]] : !cir.ptr<!rec_Ov>, !rec_Ov
+
+  // Past two eightbytes, so byval, and the alignment comes from the record's
+  // declared alignment in cir.record_layouts rather than from its members,
+  // whose widest is the four-byte int.
+  cir.func @take_big(%arg0: !rec_Big) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_big(%arg0: !cir.ptr<!rec_Big> {llvm.align = 16 : i64, llvm.byval = !rec_Big, llvm.noalias, llvm.noundef})
+
+  // A record the caller must destroy is passed by reference rather than copied,
+  // so it takes byref instead of byval even though the size rule is the same.
+  cir.func @take_no_regs(%arg0: !rec_NoRegs) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_no_regs(%arg0: !cir.ptr<!rec_NoRegs> {llvm.align = 16 : i64, llvm.byref = !rec_NoRegs})
+
+  // An anonymous record has no entry in cir.record_layouts, so both the
+  // register-passing decision and the alignment come from the fallbacks.
+  cir.func @take_anon_pad(%arg0: !rec_anon_pad) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_anon_pad(%arg0: !u64i)
+
+  // The caller side coerces in the other direction, storing the record and
+  // reloading the register from a view of the same slot.
+  cir.func @call_ov(%arg0: !rec_Ov) {
+    cir.call @take_ov(%arg0) : (!rec_Ov) -> ()
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @call_ov(%arg0: !s32i)
+  // CHECK:   %[[IN:.*]] = cir.load %{{.+}} : !cir.ptr<!rec_Ov>, !rec_Ov
+  // CHECK:   %[[OUT:.*]] = cir.alloca "coerce"{{.*}}: !cir.ptr<!rec_Ov>
+  // CHECK:   cir.store %[[IN]], %[[OUT]] : !rec_Ov, !cir.ptr<!rec_Ov>
+  // CHECK:   %[[OUTVIEW:.*]] = cir.cast bitcast %[[OUT]] : !cir.ptr<!rec_Ov> -> !cir.ptr<!s32i>
+  // CHECK:   %[[REG:.*]] = cir.load %[[OUTVIEW]] : !cir.ptr<!s32i>, !s32i
+  // CHECK:   cir.call @take_ov(%[[REG]]) : (!s32i) -> ()
+
+  // `Ov` differs from this record only in spelling the same bytes `pad`, so
+  // the mark alone is what keeps the eightbyte from narrowing to !s32i.
+  cir.func @take_unnamed_unit(%arg0: !rec_UnnamedUnit) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_unnamed_unit(%arg0: !u64i)
+
+  // `Zw` has the same members as `Ov`, so only the zero-width bit-field in the
+  // layout metadata keeps the eightbyte from narrowing to !s32i.
+  cir.func @take_zw(%arg0: !rec_Zw) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_zw(%arg0: !u64i)
+
+  // The unit occupies the four bytes at offset 8.  Counting it as data would
+  // split this into a double and an !u32i.
+  cir.func @take_unit_sse(%arg0: !rec_UnitSSE) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_unit_sse(%arg0: !cir.double)
+
+  // A record that is padding throughout apart from one byte still coerces to
+  // that byte, at either alignment.
+  cir.func @take_padded_byte(%arg0: !rec_PadByte) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_padded_byte(%arg0: !u8i)
+
+  cir.func @take_padded_byte2(%arg0: !rec_PadByte2) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_padded_byte2(%arg0: !u8i)
+}
diff --git a/clang/unittests/CIR/RecordTypeMetadataTest.cpp b/clang/unittests/CIR/RecordTypeMetadataTest.cpp
index 3e7b0d9054970..5f75709327712 100644
--- a/clang/unittests/CIR/RecordTypeMetadataTest.cpp
+++ b/clang/unittests/CIR/RecordTypeMetadataTest.cpp
@@ -32,8 +32,8 @@ class RecordLayoutAttrTest : public ::testing::Test {
 };
 
 TEST_F(RecordLayoutAttrTest, CanPassInRegs) {
-  auto attr =
-      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 4);
+  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
+                                    true, 4, /*offsets=*/{}, /*widths=*/{});
   EXPECT_EQ(attr.getArgPassingKind(), ArgPassingKind::CanPassInRegs);
   EXPECT_TRUE(attr.getHasTrivialDtor());
   EXPECT_EQ(attr.getRecordAlign(), 4u);
@@ -41,25 +41,44 @@ TEST_F(RecordLayoutAttrTest, CanPassInRegs) {
 
 TEST_F(RecordLayoutAttrTest, CannotPassInRegs) {
   auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CannotPassInRegs,
-                                    false, 4);
+                                    false, 4, /*offsets=*/{}, /*widths=*/{});
   EXPECT_EQ(attr.getArgPassingKind(), ArgPassingKind::CannotPassInRegs);
   EXPECT_FALSE(attr.getHasTrivialDtor());
 }
 
 TEST_F(RecordLayoutAttrTest, CanNeverPassInRegs) {
-  auto attr = RecordLayoutAttr::get(
-      &context, ArgPassingKind::CanNeverPassInRegs, false, 8);
+  auto attr =
+      RecordLayoutAttr::get(&context, ArgPassingKind::CanNeverPassInRegs, false,
+                            8, /*offsets=*/{}, /*widths=*/{});
   EXPECT_EQ(attr.getArgPassingKind(), ArgPassingKind::CanNeverPassInRegs);
   EXPECT_FALSE(attr.getHasTrivialDtor());
   EXPECT_EQ(attr.getRecordAlign(), 8u);
 }
 
 TEST_F(RecordLayoutAttrTest, HighAlignment) {
-  auto attr =
-      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 32);
+  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
+                                    true, 32, /*offsets=*/{}, /*widths=*/{});
   EXPECT_EQ(attr.getRecordAlign(), 32u);
 }
 
+TEST_F(RecordLayoutAttrTest, NoZeroWidthBitFields) {
+  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
+                                    true, 4, /*offsets=*/{}, /*widths=*/{});
+  EXPECT_FALSE(attr.getZeroWidthBitfieldOffsets());
+  EXPECT_FALSE(attr.getZeroWidthBitfieldWidths());
+}
+
+TEST_F(RecordLayoutAttrTest, ZeroWidthBitFields) {
+  auto offsets = mlir::DenseI64ArrayAttr::get(&context, {32, 96});
+  auto widths = mlir::DenseI64ArrayAttr::get(&context, {32, 64});
+  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
+                                    true, 16, offsets, widths);
+  EXPECT_EQ(attr.getZeroWidthBitfieldOffsets().asArrayRef(),
+            ArrayRef<int64_t>({32, 96}));
+  EXPECT_EQ(attr.getZeroWidthBitfieldWidths().asArrayRef(),
+            ArrayRef<int64_t>({32, 64}));
+}
+
 TEST_F(RecordLayoutAttrTest, RecordTypeUnchanged) {
   IntType i32 = IntType::get(&context, 32, true);
   auto ty = StructType::get(&context, getName("Foo"), /*is_class=*/false);
@@ -75,7 +94,8 @@ TEST_F(RecordLayoutAttrTest, ModuleLevelLookup) {
   auto module = mlir::ModuleOp::create(loc);
 
   auto layoutAttr =
-      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 8);
+      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 8,
+                            /*offsets=*/{}, /*widths=*/{});
 
   llvm::SmallVector<mlir::NamedAttribute> entries;
   entries.push_back(mlir::NamedAttribute(getName("TestRecord"), layoutAttr));

>From d4260ca8f9262c04354a63c0b65bb1a0e8f87391 Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Thu, 20 Aug 2026 15:24:37 -0700
Subject: [PATCH 2/3] [CIR] Spell a zero-width bit-field in the record type

A zero-width bit-field occupies no storage, so CIRGen leaves no member behind
for it.  The x86_64 ABI still counts its declared type as user data when it
decides how far an eightbyte can be narrowed.

That leaves an over-aligned `struct { int x; }` and the same struct with an
`unsigned : 0` after `x` with the same CIR type.  Classic passes the first in
an `i32` and the second in an `i64`.

CIRGen now puts it in the type.  It emits a member marked `bitfield` whose type
is a zero-length array of the bit-field's declared type, for the pass to read
back.  A zero-sized member occupies nothing, so the members after it keep their
offsets, one index further along.  Lowering drops the declared type, since LLVM
takes a zero-length array's element alignment and would grow the record.

Assisted-by: Cursor / claude-opus-5
---
 .../include/clang/CIR/Dialect/IR/CIRAttrs.td  |  37 +-----
 clang/include/clang/CIR/Dialect/IR/CIRTypes.h |   7 ++
 .../CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp |  74 ++++++-----
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         |  55 ++++++--
 .../Transforms/CallConvLoweringPass.cpp       |  87 +++++--------
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp |  11 +-
 .../CIR/CodeGen/aapcs-volatile-bitfields.c    |  12 +-
 ...ering-x86_64-empty-bitfield-member-nyi.cpp |   6 +-
 ...conv-lowering-x86_64-zero-width-bitfield.c | 119 ++++++++++++++----
 clang/test/CIR/CodeGen/mms-bitfields.c        |   4 +-
 clang/test/CIR/CodeGen/record-member-kinds.c  |   7 +-
 .../test/CIR/CodeGen/record-member-kinds.cpp  |  21 +++-
 clang/test/CIR/IR/invalid-record-layout.cir   |  61 ---------
 .../CIR/Lowering/copy-skip-tail-padding.cir   |  29 +++++
 .../abi-lowering/x86_64-aggregate-nyi.cir     |  28 +++--
 .../abi-lowering/x86_64-struct-padded.cir     |  22 +++-
 clang/unittests/CIR/RecordMemberKindTest.cpp  |  95 ++++++++++++++
 .../unittests/CIR/RecordTypeMetadataTest.cpp  |  36 ++----
 18 files changed, 419 insertions(+), 292 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 155eafe9db87e..ff66eead4a7db 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -128,22 +128,7 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
     // non-zero power of two.
     PredAttrTrait<"record_align must be a non-zero power of two",
                   CPred<"$record_align != 0 && "
-                        "::llvm::isPowerOf2_64($record_align)">>,
-    PredAttrTrait<"zero_width_bitfield offsets and widths must be parallel",
-                  CPred<"!$zero_width_bitfield_offsets == "
-                        "!$zero_width_bitfield_widths && "
-                        "(!$zero_width_bitfield_offsets || "
-                        "$zero_width_bitfield_offsets.size() == "
-                        "$zero_width_bitfield_widths.size())">>,
-    PredAttrTrait<"a zero_width_bitfield width must be a positive multiple of 8",
-                  CPred<"!$zero_width_bitfield_widths || "
-                        "::llvm::all_of($zero_width_bitfield_widths.asArrayRef(),"
-                        " [](int64_t w) { return w > 0 && w % 8 == 0; })">>,
-    PredAttrTrait<"a zero_width_bitfield offset must not be negative",
-                  CPred<"!$zero_width_bitfield_offsets || "
-                        "::llvm::all_of("
-                        "$zero_width_bitfield_offsets.asArrayRef(),"
-                        " [](int64_t o) { return o >= 0; })">>
+                        "::llvm::isPowerOf2_64($record_align)">>
   ]> {
   let summary = "ABI layout metadata for a record type";
   let description = [{
@@ -158,12 +143,6 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
     - `record_align_in_bytes`: from `ASTRecordLayout::getAlignment()`.
       Needed because CIR's DataLayout cannot account for
       `__attribute__((aligned(N)))`.
-    - `zero_width_bitfield_offsets` and `zero_width_bitfield_widths`: the
-      bit offset and declared bit width of each zero-width bit-field, one
-      entry per bit-field in each list.  A zero-width bit-field occupies no
-      storage and so has no member in the record type, but x86_64 counts its
-      declared type as user data.  Both lists are omitted from a record that
-      declares no zero-width bit-field.
 
     Example:
     ```
@@ -176,13 +155,7 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
         "NonTrivialDtor" = #cir.record_layout<
           arg_passing_kind = cannot_pass_in_regs,
           has_trivial_dtor = false,
-          record_align = 4>,
-        "ZeroWidth" = #cir.record_layout<
-          arg_passing_kind = can_pass_in_regs,
-          has_trivial_dtor = true,
-          record_align = 16,
-          zero_width_bitfield_offsets = [64],
-          zero_width_bitfield_widths = [32]>
+          record_align = 4>
       }
     }
     ```
@@ -191,9 +164,7 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
   let parameters = (ins
     EnumParameter<CIR_ArgPassingKind>:$arg_passing_kind,
     "bool":$has_trivial_dtor,
-    "uint64_t":$record_align,
-    OptionalParameter<"mlir::DenseI64ArrayAttr">:$zero_width_bitfield_offsets,
-    OptionalParameter<"mlir::DenseI64ArrayAttr">:$zero_width_bitfield_widths
+    "uint64_t":$record_align
   );
 
   let assemblyFormat = [{
@@ -201,8 +172,6 @@ def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
       `arg_passing_kind` `=` $arg_passing_kind `,`
       `has_trivial_dtor` `=` $has_trivial_dtor `,`
       `record_align` `=` $record_align
-      (`,` `zero_width_bitfield_offsets` `=` $zero_width_bitfield_offsets^)?
-      (`,` `zero_width_bitfield_widths` `=` $zero_width_bitfield_widths^)?
     `>`
   }];
 
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
index cc1cdfe2cbe52..c3722b14c08b8 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
@@ -54,6 +54,13 @@ inline bool isBitFieldAccessUnit(RecordMemberKind kind) {
   return kind == RecordMemberKind::BitField;
 }
 
+/// Whether a record member is a zero-width bit-field, spelled as a zero-length
+/// array of the bit-field's declared type under the bit-field mark.  The ABI
+/// counts that declared type as user data, while the zero length keeps the
+/// member out of the record's storage, so it contributes neither size nor
+/// alignment to the record's layout.
+bool isZeroWidthBitField(mlir::Type memberTy, RecordMemberKind kind);
+
 /// Returns true if the type is a CIR sized type.
 ///
 /// Types are sized if they implement SizedTypeInterface and
diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
index d099fd9e764cf..4ac3a63ffd33a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
@@ -64,6 +64,21 @@ struct CIRRecordLowering final {
       return offset < other.offset;
     }
   };
+
+  static bool isZeroWidthBitField(const MemberInfo &member) {
+    return cir::isZeroWidthBitField(member.data, member.memberKind);
+  }
+
+  /// A zero-width bit-field contributes nothing to a run of bit-fields, so it
+  /// would otherwise leave no member at all.  The array's element type is what
+  /// the ABI counts as user data, and its zero length keeps the member out of
+  /// the record's storage.
+  MemberInfo makeZeroWidthBitFieldInfo(const FieldDecl *field) {
+    return makeStorageInfo(
+        bitsToCharUnits(getFieldBitOffset(field)),
+        cir::ArrayType::get(cirGenTypes.convertTypeForMem(field->getType()), 0),
+        cir::RecordMemberKind::BitField);
+  }
   // The constructor.
   CIRRecordLowering(CIRGenTypes &cirGenTypes, const RecordDecl *recordDecl,
                     bool packed);
@@ -412,6 +427,7 @@ CIRRecordLowering::accumulateBitFields(RecordDecl::field_iterator field,
     for (; field != fieldEnd && field->isBitField(); ++field) {
       // Zero-width bitfields end runs.
       if (field->isZeroLengthBitField()) {
+        members.push_back(makeZeroWidthBitFieldInfo(*field));
         run = fieldEnd;
         continue;
       }
@@ -634,6 +650,8 @@ CIRRecordLowering::accumulateBitFields(RecordDecl::field_iterator field,
       assert(field != fieldEnd && field->isBitField() &&
              "Accumulating past end of bitfields");
       assert(!barrier && "Accumulating across barrier");
+      if (field->isZeroLengthBitField())
+        members.push_back(makeZeroWidthBitFieldInfo(*field));
       // Accumulate this bitfield into the current (potential) span.
       bitSizeSinceBegin += field->getBitWidthValue();
       ++field;
@@ -722,7 +740,7 @@ void CIRRecordLowering::determinePacked(bool nvBaseType) {
                          : CharUnits::Zero();
 
   for (const MemberInfo &member : members) {
-    if (!member.data)
+    if (!member.data || isZeroWidthBitField(member))
       continue;
     // If any member falls at an offset that it not a multiple of its alignment,
     // then the entire record must be packed.
@@ -752,6 +770,19 @@ void CIRRecordLowering::insertPadding() {
   for (const MemberInfo &member : members) {
     if (!member.data)
       continue;
+    // A consumer recovers each member's offset by accumulating the sizes of the
+    // members before it, so the padding this one sits inside has to be split at
+    // its offset for that sum to come out right.
+    if (isZeroWidthBitField(member)) {
+      // The offset can sit behind the running size, since the access unit
+      // covering the bits around it may be wider than the offset it was
+      // declared at.
+      if (member.offset > size) {
+        padding.push_back(std::make_pair(size, member.offset - size));
+        size = member.offset;
+      }
+      continue;
+    }
     CharUnits offset = member.offset;
     assert(offset >= size);
     // Insert padding if we need to.
@@ -868,44 +899,11 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *rd, cir::RecordType *ty) {
     if (auto *cxxRD = dyn_cast<CXXRecordDecl>(rd))
       hasTrivialDestructor = cxxRD->hasTrivialDestructor();
     const auto &astLayout = astContext.getASTRecordLayout(rd);
+    uint64_t recordAlignInBytes = astLayout.getAlignment().getQuantity();
 
-    // A zero-width bit-field occupies no storage, so the record type has no
-    // member for it, but its declared type still counts as user data for
-    // argument passing.  The member list cannot carry it: a member's offset
-    // comes from the members before it, so adding one would move the rest.
-    SmallVector<int64_t> zeroWidthOffsets;
-    SmallVector<int64_t> zeroWidthWidths;
-    for (const FieldDecl *field : rd->fields()) {
-      if (!field->isZeroLengthBitField())
-        continue;
-      zeroWidthOffsets.push_back(
-          astLayout.getFieldOffset(field->getFieldIndex()));
-      zeroWidthWidths.push_back(astContext.getTypeSize(field->getType()));
-    }
-
-    mlir::DenseI64ArrayAttr offsetsAttr;
-    mlir::DenseI64ArrayAttr widthsAttr;
-    if (!zeroWidthOffsets.empty()) {
-      offsetsAttr = mlir::DenseI64ArrayAttr::get(mlirCtx, zeroWidthOffsets);
-      widthsAttr = mlir::DenseI64ArrayAttr::get(mlirCtx, zeroWidthWidths);
-    }
-
-    auto layoutForAlign = [&](uint64_t alignInBytes) {
-      return cir::RecordLayoutAttr::get(mlirCtx, apk, hasTrivialDestructor,
-                                        alignInBytes, offsetsAttr, widthsAttr);
-    };
-
-    cgm.addRecordLayout(ty->getName(),
-                        layoutForAlign(astLayout.getAlignment().getQuantity()));
-
-    // A base subobject is a separate type under its own name, so a lookup for
-    // it misses the entry above.  Its fields sit where the complete object
-    // puts them, so the bit-field lists carry over, but it covers only the
-    // non-virtual part and so takes getNonVirtualAlignment().
-    if (baseTy && baseTy.getName() && baseTy.getName() != ty->getName())
-      cgm.addRecordLayout(
-          baseTy.getName(),
-          layoutForAlign(astLayout.getNonVirtualAlignment().getQuantity()));
+    cgm.addRecordLayout(ty->getName(), cir::RecordLayoutAttr::get(
+                                           mlirCtx, apk, hasTrivialDestructor,
+                                           recordAlignInBytes));
   }
 
   auto rl = std::make_unique<CIRGenRecordLayout>(
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index 5ee0e493ef918..adc15c7ddbf9c 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -681,12 +681,25 @@ void RecordType::removeABIConversionNamePrefix() {
   return mlir::cast<UnionType>(*this).removeABIConversionNamePrefix();
 }
 
+bool cir::isZeroWidthBitField(mlir::Type memberTy, RecordMemberKind kind) {
+  if (kind != RecordMemberKind::BitField)
+    return false;
+  auto arrTy = mlir::dyn_cast<ArrayType>(memberTy);
+  return arrTy && arrTy.getSize() == 0;
+}
+
 bool RecordType::isEmptyForABI() const {
   // An incomplete record has no members yet, which must not read as vacuously
   // holding no data.
   if (isIncomplete())
     return false;
-  return llvm::none_of(getMemberKinds(), holdsDataForABI);
+  // A zero-width bit-field occupies no storage and holds no data, so a record
+  // of nothing but those is empty for the ABI just as the AST predicate says.
+  return llvm::none_of(
+      llvm::zip_equal(getMembers(), getMemberKinds()), [](const auto &pair) {
+        auto [memberTy, kind] = pair;
+        return holdsDataForABI(kind) && !isZeroWidthBitField(memberTy, kind);
+      });
 }
 
 //===----------------------------------------------------------------------===//
@@ -841,7 +854,9 @@ StructType::computeStructSize(const mlir::DataLayout &dataLayout) const {
   unsigned recordSize = 0;
   uint64_t recordAlignment = 1;
 
-  for (mlir::Type ty : getMembers()) {
+  for (auto [ty, kind] : llvm::zip_equal(getMembers(), getMemberKinds())) {
+    if (isZeroWidthBitField(ty, kind))
+      continue;
     // This assumes that we're calculating size based on the ABI alignment, not
     // the preferred alignment for each type.
     const uint64_t tyAlign =
@@ -867,20 +882,24 @@ unsigned
 StructType::computeStructDataSize(const mlir::DataLayout &dataLayout) const {
   assert(isComplete() && "Cannot get layout of incomplete records");
 
-  // Tail padding is the trailing run of pad members.  A member of any other
-  // kind stays inside the data size, only pad being reusable by a derived
-  // class.
+  // Tail padding is the trailing run of pad members, which is what a derived
+  // class may reuse.  A zero-width bit-field holds no storage, so it does not
+  // end that run.  A member of any other kind stays inside the data size.
   llvm::ArrayRef<mlir::Type> members = getMembers();
   llvm::ArrayRef<RecordMemberKind> kinds = getMemberKinds();
   assert(kinds.size() == members.size() &&
          "the two drop_back calls below must stay in step");
-  while (!kinds.empty() && kinds.back() == RecordMemberKind::Pad) {
+  while (!kinds.empty() &&
+         (kinds.back() == RecordMemberKind::Pad ||
+          isZeroWidthBitField(members.back(), kinds.back()))) {
     kinds = kinds.drop_back();
     members = members.drop_back();
   }
 
   unsigned recordSize = 0;
-  for (mlir::Type ty : members) {
+  for (auto [ty, kind] : llvm::zip_equal(members, kinds)) {
+    if (isZeroWidthBitField(ty, kind))
+      continue;
     const uint64_t tyAlign =
         (getPacked() ? 1 : dataLayout.getTypeABIAlignment(ty));
     recordSize = llvm::alignTo(recordSize, tyAlign);
@@ -898,9 +917,12 @@ StructType::computeStructAlignment(const mlir::DataLayout &dataLayout) const {
   assert(isComplete() && "Cannot get layout of incomplete records");
 
   uint64_t recordAlignment = 1;
-  for (mlir::Type ty : getMembers())
+  for (auto [ty, kind] : llvm::zip_equal(getMembers(), getMemberKinds())) {
+    if (isZeroWidthBitField(ty, kind))
+      continue;
     recordAlignment =
         std::max(dataLayout.getTypeABIAlignment(ty), recordAlignment);
+  }
   return recordAlignment;
 }
 
@@ -913,16 +935,23 @@ uint64_t StructType::getElementOffset(const ::mlir::DataLayout &dataLayout,
   assert(isComplete() && "Cannot get layout of incomplete records");
   assert(idx < getNumElements());
   llvm::ArrayRef<mlir::Type> members = getMembers();
+  llvm::ArrayRef<RecordMemberKind> kinds = getMemberKinds();
 
   unsigned offset = 0;
-  for (mlir::Type ty :
-       llvm::make_range(members.begin(), std::next(members.begin(), idx))) {
-    const llvm::Align tyAlign =
-        llvm::Align(getPacked() ? 1 : dataLayout.getTypeABIAlignment(ty));
+  for (unsigned i = 0; i != idx; ++i) {
+    if (isZeroWidthBitField(members[i], kinds[i]))
+      continue;
+    const llvm::Align tyAlign = llvm::Align(
+        getPacked() ? 1 : dataLayout.getTypeABIAlignment(members[i]));
     offset = llvm::alignTo(offset, tyAlign);
-    offset += dataLayout.getTypeSize(ty);
+    offset += dataLayout.getTypeSize(members[i]);
   }
 
+  // A zero-width bit-field imposes no alignment, so its offset is where the
+  // storage ahead of it ends.
+  if (isZeroWidthBitField(members[idx], kinds[idx]))
+    return offset;
+
   const llvm::Align tyAlign = llvm::Align(
       getPacked() ? 1 : dataLayout.getTypeABIAlignment(members[idx]));
   offset = llvm::alignTo(offset, tyAlign);
diff --git a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
index 1636c952ffa60..ed706e7c13b10 100644
--- a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
@@ -117,49 +117,21 @@ static llvm::Align recordDeclaredAlign(ModuleOp modOp, cir::RecordType recTy,
   return llvm::Align(layout.getRecordAlign());
 }
 
-/// Adds a field for each zero-width bit-field \p recTy declares, taking the bit
-/// offset and declared bit width from the module's record-layout metadata.
-/// Such a bit-field has no member to map, so the field is synthesized here and
-/// flagged as an unnamed bit-field, which the classifier skips when assigning
-/// eightbyte classes and counts when picking the coerce type.  Returns whether
-/// any were added.
-static bool
-appendZeroWidthBitFields(cir::RecordType recTy, llvm::abi::TypeBuilder &tb,
-                         ModuleOp modOp,
-                         SmallVectorImpl<llvm::abi::FieldInfo> &out) {
-  auto layout = cir::tryGetRecordLayout(modOp, recTy.getName());
-  if (!layout)
-    return false;
-  mlir::DenseI64ArrayAttr offsets = layout.getZeroWidthBitfieldOffsets();
-  mlir::DenseI64ArrayAttr widths = layout.getZeroWidthBitfieldWidths();
-  if (!offsets || !widths)
-    return false;
-
-  // The classifier reads a bit-field field's width but not its alignment, so
-  // the declared alignment need not be reproduced.  The union reduction is its
-  // one reader, and a synthesized field never reaches a union.
-  for (auto [offsetBits, widthBits] :
-       llvm::zip_equal(offsets.asArrayRef(), widths.asArrayRef()))
-    out.push_back(
-        llvm::abi::FieldInfo(tb.getIntegerType(widthBits, llvm::Align(1),
-                                               /*Signed=*/false),
-                             offsetBits, /*IsBitField=*/true, widthBits,
-                             /*IsUnnamedBitField=*/true));
-  return true;
-}
-
-/// Whether \p ty contains a bit-field access unit holding a named bit-field,
-/// looking through member records and array element types.  A unit of only
-/// unnamed bit-fields is left out, since it reaches the classifier flagged as
-/// an unnamed bit-field and is handled the way classic CodeGen handles one.
+/// Whether \p ty reaches a bit-field access unit holding a named bit-field,
+/// looking through member records and array element types.
 static bool reachesNamedBitFieldUnit(mlir::Type ty) {
   if (auto arrTy = dyn_cast<cir::ArrayType>(ty))
     return reachesNamedBitFieldUnit(arrTy.getElementType());
   auto recTy = dyn_cast<cir::RecordType>(ty);
   if (!recTy)
     return false;
-  if (llvm::any_of(recTy.getMemberKinds(), cir::isBitFieldAccessUnit))
-    return true;
+  // A zero-length array under the bit-field mark is a zero-width bit-field, not
+  // an access unit, and it carries its declared type rather than a unit width.
+  for (auto [memberTy, kind] :
+       llvm::zip_equal(recTy.getMembers(), recTy.getMemberKinds()))
+    if (cir::isBitFieldAccessUnit(kind) &&
+        !cir::isZeroWidthBitField(memberTy, kind))
+      return true;
   return llvm::any_of(recTy.getMembers(), reachesNamedBitFieldUnit);
 }
 
@@ -267,14 +239,13 @@ static bool isSupportedType(mlir::Type ty, const DataLayout &dl) {
       // A named access unit can be narrower than the type its bit-fields were
       // declared with, and that declared type is what classic CodeGen coerces
       // from.  CIR does not record it, so classifying here would coerce to the
-      // unit's width instead.  Nested and array members count, not just this
-      // record.
+      // unit's width instead.
       return false;
     }
-    // Below, an `empty` member that occupies bytes is read as an unnamed access
-    // unit.  A member that is itself empty for the ABI can occupy bytes too, by
-    // holding a unit of its own, and classic CodeGen reaches those bytes
-    // through the member's own fields rather than as a single unit.
+    // An `empty` member that occupies bytes is later read as an unnamed access
+    // unit.  One that is itself an empty-for-ABI record can occupy bytes by
+    // holding a unit of its own, which classic CodeGen reaches through the
+    // member's fields rather than as one unit.
     for (auto [memberTy, kind] :
          llvm::zip_equal(recTy.getMembers(), recTy.getMemberKinds()))
       if (kind == cir::RecordMemberKind::Empty &&
@@ -413,38 +384,36 @@ static const llvm::abi::Type *mapCIRType(mlir::Type type,
         }
 
         // Padding is dropped, so the eightbyte rules see its bytes as holding
-        // nothing, while the record's full size set above still spans them.
-        //
-        // An unnamed bit-field access unit is passed along flagged instead,
-        // since classic CodeGen ignores it when assigning eightbyte classes
-        // and counts it when choosing the coerce type.  A member's presence
+        // nothing, while the record's full size set above still spans them.  An
+        // unnamed bit-field access unit is flagged rather than dropped, since
+        // classic CodeGen ignores it when assigning eightbyte classes but
+        // counts it when choosing the coerce type, and a member's presence
         // alone cannot say both.
         for (auto [idx, fieldTy, kind] :
              llvm::enumerate(recTy.getMembers(), recTy.getMemberKinds())) {
           if (kind == cir::RecordMemberKind::Pad)
             continue;
-          uint64_t widthBits = dl.getTypeSizeInBits(fieldTy).getFixedValue();
+          // A zero-width bit-field occupies nothing, so its declared type is
+          // what the ABI counts, and that is the element type it carries.
+          mlir::Type countedTy = fieldTy;
           bool isUnnamedUnit = kind == cir::RecordMemberKind::Empty;
+          if (cir::isZeroWidthBitField(fieldTy, kind)) {
+            countedTy = cast<cir::ArrayType>(fieldTy).getElementType();
+            isUnnamedUnit = true;
+          }
+          uint64_t widthBits = dl.getTypeSizeInBits(countedTy).getFixedValue();
           if (isUnnamedUnit && widthBits == 0)
             continue;
-          assert((!isUnnamedUnit || !memberIsEmptyRecord(fieldTy)) &&
+          assert((!isUnnamedUnit || !memberIsEmptyRecord(countedTy)) &&
                  "an empty-for-ABI member must not reach the classifier as an "
                  "unnamed bit-field");
           fields.push_back(llvm::abi::FieldInfo(
-              mapCIRType(fieldTy, typeMapper, dl, modOp),
+              mapCIRType(countedTy, typeMapper, dl, modOp),
               recTy.getElementOffset(dl, idx) * 8,
               /*IsBitField=*/isUnnamedUnit, isUnnamedUnit ? widthBits : 0,
               /*IsUnnamedBitField=*/isUnnamedUnit));
         }
 
-        // The classifier stops walking fields at the first one past the range
-        // it is asked about, so a synthesized field appended out of order would
-        // hide the fields behind it.
-        if (appendZeroWidthBitFields(recTy, tb, modOp, fields))
-          llvm::stable_sort(fields, [](const llvm::abi::FieldInfo &lhs,
-                                       const llvm::abi::FieldInfo &rhs) {
-            return lhs.OffsetInBits < rhs.OffsetInBits;
-          });
         return tb.getRecordType(
             fields, sizeBits, align, llvm::abi::StructPacking::Default,
             /*BaseClasses=*/{}, /*VirtualBaseClasses=*/{}, flags);
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 578b328b622ee..e7d0c34f9291c 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -3928,7 +3928,16 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
   });
   converter.addConversion([&](cir::StructType type) -> mlir::Type {
     llvm::SmallVector<mlir::Type> llvmMembers;
-    for (mlir::Type ty : type.getMembers()) {
+    for (auto [ty, kind] :
+         llvm::zip_equal(type.getMembers(), type.getMemberKinds())) {
+      // Argument passing has already read the declared type, and carrying it
+      // into LLVM would apply the element's alignment and grow the record.  The
+      // member stays, since the members after it are numbered from it.
+      if (cir::isZeroWidthBitField(ty, kind)) {
+        llvmMembers.push_back(mlir::LLVM::LLVMArrayType::get(
+            mlir::IntegerType::get(type.getContext(), 8), 0));
+        continue;
+      }
       mlir::Type memberTy = convertTypeForMemory(converter, dataLayout, ty);
       // A null member means an unsupported type (e.g. a _BitInt with byte-array
       // storage); propagate the conversion failure instead of building an
diff --git a/clang/test/CIR/CodeGen/aapcs-volatile-bitfields.c b/clang/test/CIR/CodeGen/aapcs-volatile-bitfields.c
index ecde8f24b2334..b9fc29d0f3087 100644
--- a/clang/test/CIR/CodeGen/aapcs-volatile-bitfields.c
+++ b/clang/test/CIR/CodeGen/aapcs-volatile-bitfields.c
@@ -109,14 +109,16 @@ int check_load(st1 *s1) {
 // OGCG:   ret i32 [[CLEAR]]
 
 // this volatile bit-field container overlaps with a zero-length bit-field,
-// so it may be accessed without using the container's width.
+// so it may be accessed without using the container's width.  That bit-field
+// is a zero-sized member of the CIR record, so `b` sits one index later there
+// than in the classic record, at the same offset.
 int check_load_exception(st3 *s3) {
   return s3->b;
 }
 
 // CIR:  cir.func {{.*}} @check_load_exception
 // CIR:    [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_st3>>, !cir.ptr<!rec_st3>
-// CIR:    [[MEMBER:%.*]] = cir.get_member [[LOAD]][2] {name = "b"} : !cir.ptr<!rec_st3> -> !cir.ptr<!u8i>
+// CIR:    [[MEMBER:%.*]] = cir.get_member [[LOAD]][3] {name = "b"} : !cir.ptr<!rec_st3> -> !cir.ptr<!u8i>
 // CIR:    [[BITFI:%.*]] = cir.get_bitfield align(4) (#bfi_b1, [[MEMBER]] {is_volatile} : !cir.ptr<!u8i>) -> !u32i
 // CIR:    [[CAST:%.*]] = cir.cast integral [[BITFI]] : !u32i -> !s32i
 // CIR:    cir.store [[CAST]], [[RETVAL:%.*]] : !s32i, !cir.ptr<!s32i>
@@ -125,7 +127,7 @@ int check_load_exception(st3 *s3) {
 
 // LLVM:define dso_local i32 @check_load_exception
 // LLVM:  [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
-// LLVM:  [[MEMBER:%.*]] = getelementptr inbounds nuw %struct.st3, ptr [[LOAD]], i32 0, i32 2
+// LLVM:  [[MEMBER:%.*]] = getelementptr inbounds nuw %struct.st3, ptr [[LOAD]], i32 0, i32 3
 // LLVM:  [[LOADVOL:%.*]] = load volatile i8, ptr [[MEMBER]], align 4
 // LLVM:  [[CLEAR:%.*]] = and i8 [[LOADVOL]], 31
 // LLVM:  [[CAST:%.*]] = zext i8 [[CLEAR]] to i32
@@ -211,13 +213,13 @@ void check_store_exception(st3 *s3) {
 // CIR:  cir.func {{.*}} @check_store_exception
 // CIR:    [[CONST:%.*]] = cir.const #cir.int<2> : !u32i
 // CIR:    [[LOAD:%.*]] = cir.load align(8) {{.*}} : !cir.ptr<!cir.ptr<!rec_st3>>, !cir.ptr<!rec_st3>
-// CIR:    [[MEMBER:%.*]] = cir.get_member [[LOAD]][2] {name = "b"} : !cir.ptr<!rec_st3> -> !cir.ptr<!u8i>
+// CIR:    [[MEMBER:%.*]] = cir.get_member [[LOAD]][3] {name = "b"} : !cir.ptr<!rec_st3> -> !cir.ptr<!u8i>
 // CIR:    [[SETBF:%.*]] = cir.set_bitfield align(4) (#bfi_b1, [[MEMBER]] : !cir.ptr<!u8i>, [[CONST]] : !u32i) {is_volatile} -> !u32i
 // CIR:    cir.return
 
 // LLVM:define dso_local void @check_store_exception
 // LLVM:  [[LOAD:%.*]] = load ptr, ptr {{.*}}, align 8
-// LLVM:  [[MEMBER:%.*]] = getelementptr inbounds nuw %struct.st3, ptr [[LOAD]], i32 0, i32 2
+// LLVM:  [[MEMBER:%.*]] = getelementptr inbounds nuw %struct.st3, ptr [[LOAD]], i32 0, i32 3
 // LLVM:  [[LOADVOL:%.*]] = load volatile i8, ptr [[MEMBER]], align 4
 // LLVM:  [[CLEAR:%.*]] = and i8 [[LOADVOL]], -32
 // LLVM:  [[SET:%.*]] = or i8 [[CLEAR]], 2
diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp
index 7fc5018853b2c..36d029f46732c 100644
--- a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp
+++ b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-empty-bitfield-member-nyi.cpp
@@ -1,9 +1,7 @@
 // RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - 2>&1 | FileCheck %s
 
-// `A` holds no data for the ABI, yet it occupies four bytes, because an
-// unnamed bit-field access unit supplies them.  Reached as a member it takes
-// the same `empty` mark a unit does, so classifying it as one would count
-// bytes classic CodeGen counts through the member's own fields.
+// `A` holds no data for the ABI yet occupies four bytes, which an unnamed
+// bit-field access unit supplies.
 struct A { unsigned : 32; };
 
 struct AsBase : A { int i; };
diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
index 987d176511120..4e15cf7d32f1a 100644
--- a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
+++ b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
@@ -10,29 +10,68 @@ typedef struct { char c; int : 0; int y; } Mid;
 typedef struct { int x; int : 0; int y; } Plain;
 typedef struct { char c; long long : 0; } __attribute__((aligned(16))) Wide;
 typedef struct { char c; int : 0; short s; int : 0; } Twice;
+typedef struct { char c; int : 0; long long : 0; short s; } Adjacent;
+typedef struct { int : 0; int x; } __attribute__((aligned(16))) AtStart;
+typedef struct { int : 0; } OnlyZeroWidth;
 typedef struct { int x; } __attribute__((aligned(16))) NoZeroWidth;
 typedef struct { Tail t; } Nest;
+typedef struct { Tail t[1]; } ArrayOfTail;
 struct { int x; unsigned : 0; } __attribute__((aligned(16))) gAnon;
 typedef struct { long long a; int b; unsigned : 0; } Pair;
 typedef struct { char c; int : 0; char d[24]; } TooBig;
 typedef struct { char c; int : 0; long long y; } AfterZeroWidth;
 typedef struct { char c; long long y; } NoZeroWidthPair;
 typedef struct { double d; int : 32; } __attribute__((aligned(16))) UnnamedUnit;
-
-// CIR-DAG: AfterZeroWidth = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
-// CIR-DAG: Mid = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
-// CIR-DAG: Pair = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [96], zero_width_bitfield_widths = [32]>
-// CIR-DAG: Plain = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
-// CIR-DAG: Tail = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
-// CIR-DAG: TooBig = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
-// CIR-DAG: Twice = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32, 64], zero_width_bitfield_widths = [32, 32]>
-// CIR-DAG: Wide = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [64], zero_width_bitfield_widths = [64]>
-// CIR-DAG: anon.0 = #cir.record_layout<{{[^>]*}}zero_width_bitfield_offsets = [32], zero_width_bitfield_widths = [32]>
-
-// A record that declares none carries no list, so record_align closes the
-// attribute.  `Nest` declares none itself, the bit-field being the member's.
-// CIR-DAG: Nest = #cir.record_layout<{{[^>]*}}record_align = 16>
-// CIR-DAG: NoZeroWidth = #cir.record_layout<{{[^>]*}}record_align = 16>
+typedef struct { char c; long long : 0; char d; } WiderThanRecord;
+typedef struct { int a; int : 0; int fam[]; } ZeroWidthAndFam;
+typedef struct { char c; int : 0; int y; } __attribute__((packed)) Packed;
+typedef struct { int x; unsigned : 0; }
+    __attribute__((ms_struct, aligned(16))) MsTail;
+typedef union { int a; Mid m; } ZeroWidthUnion;
+
+// CIR-DAG: !rec_Tail = !cir.struct<"Tail" {data !s32i, bitfield !cir.array<!u32i x 0>, pad !cir.array<!u8i x 12>}>
+// CIR-DAG: !rec_Mid = !cir.struct<"Mid" {data !s8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, data !s32i}>
+// CIR-DAG: !rec_Plain = !cir.struct<"Plain" {data !s32i, bitfield !cir.array<!s32i x 0>, data !s32i}>
+// CIR-DAG: !rec_Wide = !cir.struct<"Wide" {data !s8i, pad !cir.array<!u8i x 7>, bitfield !cir.array<!s64i x 0>, pad !cir.array<!u8i x 8>}>
+// CIR-DAG: !rec_Twice = !cir.struct<"Twice" {data !s8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, data !s16i, pad !cir.array<!u8i x 2>, bitfield !cir.array<!s32i x 0>}>
+// CIR-DAG: !rec_Adjacent = !cir.struct<"Adjacent" {data !s8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, pad !cir.array<!u8i x 4>, bitfield !cir.array<!s64i x 0>, data !s16i}>
+// CIR-DAG: !rec_AtStart = !cir.struct<"AtStart" {bitfield !cir.array<!s32i x 0>, data !s32i, pad !cir.array<!u8i x 12>}>
+// CIR-DAG: !rec_Pair = !cir.struct<"Pair" {data !s64i, data !s32i, bitfield !cir.array<!u32i x 0>}>
+// CIR-DAG: !rec_TooBig = !cir.struct<"TooBig" {data !s8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, data !cir.array<!s8i x 24>}>
+// CIR-DAG: !rec_AfterZeroWidth = !cir.struct<"AfterZeroWidth" {data !s8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, data !s64i}>
+// CIR-DAG: !rec_WiderThanRecord = !cir.struct<"WiderThanRecord" {data !s8i, pad !cir.array<!u8i x 7>, bitfield !cir.array<!s64i x 0>, data !s8i}>
+// CIR-DAG: !rec_Packed = !cir.struct<"Packed" {data !s8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, data !s32i}>
+
+// The Microsoft layout builds bit-field runs on its own path.
+// CIR-DAG: !rec_MsTail = !cir.struct<"MsTail" {data !s32i, bitfield !cir.array<!u32i x 0>, pad !cir.array<!u8i x 12>}>
+
+// CIR-DAG: !rec_OnlyZeroWidth = !cir.struct<"OnlyZeroWidth" {bitfield !cir.array<!s32i x 0>}>
+
+// An anonymous record reaches the same path under its generated name.
+// CIR-DAG: !rec_anon2E0 = !cir.struct<"anon.0" {data !s32i, bitfield !cir.array<!u32i x 0>, pad !cir.array<!u8i x 12>}>
+
+// A zero-length array under `data` is a flexible array member, which keeps
+// its alignment contribution.
+// CIR-DAG: !rec_ZeroWidthAndFam = !cir.struct<"ZeroWidthAndFam" {data !s32i, bitfield !cir.array<!s32i x 0>, data !cir.array<!s32i x 0>}>
+
+// CIR-DAG: !rec_NoZeroWidth = !cir.struct<"NoZeroWidth" {data !s32i, pad !cir.array<!u8i x 12>}>
+// CIR-DAG: !rec_NoZeroWidthPair = !cir.struct<"NoZeroWidthPair" {data !s8i, data !s64i}>
+// CIR-DAG: !rec_Nest = !cir.struct<"Nest" {data !rec_Tail}>
+// CIR-DAG: !rec_ArrayOfTail = !cir.struct<"ArrayOfTail" {data !cir.array<!rec_Tail x 1>}>
+
+// The alias number depends on emission order, so bind it by content.
+// CIR-DAG: ![[PAIR_RET:rec_anon_struct[0-9]*]] = !cir.struct<{data !s64i, data !u64i}>
+
+// The lowered member carries no type.  The declared type would make
+// `WiderThanRecord` eight-byte aligned where the source says one.
+// LLVM-CIR-DAG: %struct.Tail = type { i32, [0 x i8], [12 x i8] }
+// LLVM-OGCG-DAG: %struct.Tail = type { i32, [12 x i8] }
+// LLVM-CIR-DAG: %struct.Mid = type { i8, [3 x i8], [0 x i8], i32 }
+// LLVM-OGCG-DAG: %struct.Mid = type { i8, i32 }
+// LLVM-CIR-DAG: %struct.Wide = type { i8, [7 x i8], [0 x i8], [8 x i8] }
+// LLVM-OGCG-DAG: %struct.Wide = type { i8, [15 x i8] }
+// LLVM-CIR-DAG: %struct.WiderThanRecord = type { i8, [7 x i8], [0 x i8], i8 }
+// LLVM-OGCG-DAG: %struct.WiderThanRecord = type { i8, [7 x i8], i8 }
 
 // The bit-field extends the eightbyte's user data past `x`, so it stays i64
 // instead of narrowing to i32 the way the same over-aligned shape does without
@@ -57,12 +96,23 @@ void take_wide(Wide w) {}
 // CIR: cir.func{{.*}} @take_wide(%arg0: !s8i loc
 // LLVM: define{{.*}} void @take_wide(i8 %{{.+}})
 
-// `s` reaches bit 48 and the second bit-field starts at 64, so the pair of
-// entries above is what this case asserts rather than the argument type.
 void take_twice(Twice t) {}
 // CIR: cir.func{{.*}} @take_twice(%arg0: !u64i loc
 // LLVM: define{{.*}} void @take_twice(i64 %{{.+}})
 
+void take_adjacent(Adjacent a) {}
+// CIR: cir.func{{.*}} @take_adjacent(%arg0: !u64i loc{{.*}}, %arg1: !s16i loc
+// LLVM: define{{.*}} void @take_adjacent(i64 %{{.+}}, i16 %{{.+}})
+
+// At offset zero the bit-field reaches no further than `x` does.
+void take_at_start(AtStart a) {}
+// CIR: cir.func{{.*}} @take_at_start(%arg0: !s32i loc
+// LLVM: define{{.*}} void @take_at_start(i32 %{{.+}})
+
+void take_only_zero_width(OnlyZeroWidth o) {}
+// CIR: cir.func{{.*}} @take_only_zero_width()
+// LLVM: define{{.*}} void @take_only_zero_width()
+
 void take_no_zero_width(NoZeroWidth n) {}
 // CIR: cir.func{{.*}} @take_no_zero_width(%arg0: !s32i loc
 // LLVM: define{{.*}} void @take_no_zero_width(i32 %{{.+}})
@@ -71,6 +121,10 @@ void take_nest(Nest n) {}
 // CIR: cir.func{{.*}} @take_nest(%arg0: !u64i loc
 // LLVM: define{{.*}} void @take_nest(i64 %{{.+}})
 
+void take_array_of_tail(ArrayOfTail a) {}
+// CIR: cir.func{{.*}} @take_array_of_tail(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_array_of_tail(i64 %{{.+}})
+
 void take_anon(__typeof__(gAnon) a) {}
 // CIR: cir.func{{.*}} @take_anon(%arg0: !u64i loc
 // LLVM: define{{.*}} void @take_anon(i64 %{{.+}})
@@ -81,15 +135,13 @@ void take_pair(Pair p) {}
 // CIR: cir.func{{.*}} @take_pair(%arg0: !s64i loc{{.*}}, %arg1: !u64i loc
 // LLVM: define{{.*}} void @take_pair(i64 %{{.+}}, i64 %{{.+}})
 
-// A data member sits past the bit-field, so the field list only stays in offset
-// order if the synthesized entry is placed rather than appended.
 void take_after_zero_width(AfterZeroWidth a) {}
 // CIR: cir.func{{.*}} @take_after_zero_width(%arg0: !u64i loc{{.*}}, %arg1: !s64i loc
 // LLVM: define{{.*}} void @take_after_zero_width(i64 %{{.+}}, i64 %{{.+}})
 
-void take_no_zw(NoZeroWidthPair n) {}
-// CIR: cir.func{{.*}} @take_no_zw(%arg0: !s8i loc{{.*}}, %arg1: !s64i loc
-// LLVM: define{{.*}} void @take_no_zw(i8 %{{.+}}, i64 %{{.+}})
+void take_no_zero_width_pair(NoZeroWidthPair n) {}
+// CIR: cir.func{{.*}} @take_no_zero_width_pair(%arg0: !s8i loc{{.*}}, %arg1: !s64i loc
+// LLVM: define{{.*}} void @take_no_zero_width_pair(i8 %{{.+}}, i64 %{{.+}})
 
 // The unit occupies four bytes after `d`.  Counting it as data would stop this
 // being a single SSE value and pass it as a double plus an i32.
@@ -97,6 +149,27 @@ void take_unnamed_unit(UnnamedUnit u) {}
 // CIR: cir.func{{.*}} @take_unnamed_unit(%arg0: !cir.double loc
 // LLVM: define{{.*}} void @take_unnamed_unit(double %{{.+}})
 
+void take_wider_than_record(WiderThanRecord w) {}
+// CIR: cir.func{{.*}} @take_wider_than_record(%arg0: !u64i loc{{.*}}, %arg1: !s8i loc
+// LLVM: define{{.*}} void @take_wider_than_record(i64 %{{.+}}, i8 %{{.+}})
+
+void take_zero_width_and_fam(ZeroWidthAndFam *p) {}
+// CIR: cir.func{{.*}} @take_zero_width_and_fam(%arg0: !cir.ptr<!rec_ZeroWidthAndFam>
+// LLVM: define{{.*}} void @take_zero_width_and_fam(ptr noundef %{{.+}})
+
+void take_packed(Packed p) {}
+// CIR: cir.func{{.*}} @take_packed(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_packed(i64 %{{.+}})
+
+void take_ms_tail(MsTail t) {}
+// CIR: cir.func{{.*}} @take_ms_tail(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_ms_tail(i64 %{{.+}})
+
+// A union reduces to its largest member, which drops the bit-field with it.
+void take_union(ZeroWidthUnion u) {}
+// CIR: cir.func{{.*}} @take_union(%arg0: !u64i loc
+// LLVM: define{{.*}} void @take_union(i64 %{{.+}})
+
 // Past two eightbytes the record goes to memory, where the widened coerce type
 // no longer applies.
 void take_too_big(TooBig b) {}
@@ -109,5 +182,5 @@ Tail ret_tail(void) { Tail t = {3}; return t; }
 // LLVM: define{{.*}} i64 @ret_tail()
 
 Pair ret_pair(void) { Pair p = {1, 2}; return p; }
-// CIR: cir.func{{.*}} @ret_pair() -> !rec_anon_struct
+// CIR: cir.func{{.*}} @ret_pair() -> ![[PAIR_RET]]
 // LLVM: define{{.*}} { i64, i64 } @ret_pair()
diff --git a/clang/test/CIR/CodeGen/mms-bitfields.c b/clang/test/CIR/CodeGen/mms-bitfields.c
index f9832f55e5405..be88a03d201f9 100644
--- a/clang/test/CIR/CodeGen/mms-bitfields.c
+++ b/clang/test/CIR/CodeGen/mms-bitfields.c
@@ -30,8 +30,8 @@ struct s3 {
     int c : 14;
 } zero_bit;
 
-// CIR-DAG:  !rec_s3 = !cir.struct<"s3" {bitfield !s32i, bitfield !s32i}>
-// LLVM-DAG: %struct.s3 = type { i32, i32 }
+// CIR-DAG:  !rec_s3 = !cir.struct<"s3" {bitfield !s32i, bitfield !cir.array<!s32i x 0>, bitfield !s32i}>
+// LLVM-DAG: %struct.s3 = type { i32, [0 x i8], i32 }
 // OGCG-DAG: %struct.s3 = type { i32, i32 }
 
 #pragma pack (push,1)
diff --git a/clang/test/CIR/CodeGen/record-member-kinds.c b/clang/test/CIR/CodeGen/record-member-kinds.c
index 831b68f23c4f9..3155896137225 100644
--- a/clang/test/CIR/CodeGen/record-member-kinds.c
+++ b/clang/test/CIR/CodeGen/record-member-kinds.c
@@ -70,12 +70,13 @@ struct MsEmptyMiddle {
 // CIR-DAG: !rec_MsEmptyMiddle = !cir.struct<"MsEmptyMiddle" {bitfield !s32i, empty !s8i, bitfield !s16i}>
 
 // A zero-width bit-field ends the run here too, so the unit after it is a
-// fresh one that has to be marked on its own.
+// fresh one that has to be marked on its own, and the bit-field itself becomes
+// a member.
 struct MsZeroWidthSplit { int a : 3; int : 0; int : 3; } __attribute__((ms_struct));
-// CIR-DAG: !rec_MsZeroWidthSplit = !cir.struct<"MsZeroWidthSplit" {bitfield !s32i, empty !s32i}>
+// CIR-DAG: !rec_MsZeroWidthSplit = !cir.struct<"MsZeroWidthSplit" {bitfield !s32i, bitfield !cir.array<!s32i x 0>, empty !s32i}>
 
 struct MsZeroWidthSplit2 { int : 3; int : 0; int b : 3; } __attribute__((ms_struct));
-// CIR-DAG: !rec_MsZeroWidthSplit2 = !cir.struct<"MsZeroWidthSplit2" {empty !s32i, bitfield !s32i}>
+// CIR-DAG: !rec_MsZeroWidthSplit2 = !cir.struct<"MsZeroWidthSplit2" {empty !s32i, bitfield !cir.array<!s32i x 0>, bitfield !s32i}>
 
 union UnnamedBitUnion { int : 8; };
 // CIR-DAG: !rec_UnnamedBitUnion = !cir.union<"UnnamedBitUnion" {empty !u8i}>
diff --git a/clang/test/CIR/CodeGen/record-member-kinds.cpp b/clang/test/CIR/CodeGen/record-member-kinds.cpp
index 379f756912985..0ee7ac407d240 100644
--- a/clang/test/CIR/CodeGen/record-member-kinds.cpp
+++ b/clang/test/CIR/CodeGen/record-member-kinds.cpp
@@ -46,6 +46,9 @@ struct DerivesBitFieldBase : BitFieldBase { int i; };
 struct HasBitFieldVBase : virtual BitFieldBase { int i; };
 // CIR-DAG: !rec_HasBitFieldVBase = !cir.struct<"HasBitFieldVBase" packed {data !cir.vptr, data !s32i, empty !rec_BitFieldBase, pad !cir.array<!u8i x 3>}>
 
+struct ZeroWidthWide { char c; long long : 0; char d; };
+// CIR-DAG: !rec_ZeroWidthWide = !cir.struct<"ZeroWidthWide" {data !s8i, pad !cir.array<!u8i x 7>, bitfield !cir.array<!s64i x 0>, data !s8i}>
+
 struct ZeroLenEmptyArr { Empty e[0]; };
 // CIR-DAG: !rec_ZeroLenEmptyArr = !cir.struct<"ZeroLenEmptyArr" {empty !cir.array<!rec_Empty x 0>}>
 
@@ -77,13 +80,14 @@ struct NamedFirst { int a : 8; int : 16; };
 struct UnnamedFirst { int : 16; int a : 8; };
 // CIR-DAG: !rec_UnnamedFirst = !cir.struct<"UnnamedFirst" {bitfield !u32i}>
 
-// A zero-length bit-field separates one span into two units, and a record can
-// carry a data unit and an empty unit at once, in either order.
+// A zero-length bit-field separates one span into two units and becomes a
+// member of its own.  A record can carry a data unit and an empty unit at
+// once, in either order.
 struct SpanMixed { int a : 3; int : 0; int : 3; };
-// CIR-DAG: !rec_SpanMixed = !cir.struct<"SpanMixed" {bitfield !u8i, pad !cir.array<!u8i x 3>, empty !u8i, pad !cir.array<!u8i x 3>}>
+// CIR-DAG: !rec_SpanMixed = !cir.struct<"SpanMixed" {bitfield !u8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, empty !u8i, pad !cir.array<!u8i x 3>}>
 
 struct SpanEmptyFirst { int : 3; int : 0; int b : 3; };
-// CIR-DAG: !rec_SpanEmptyFirst = !cir.struct<"SpanEmptyFirst" {empty !u8i, pad !cir.array<!u8i x 3>, bitfield !u8i, pad !cir.array<!u8i x 3>}>
+// CIR-DAG: !rec_SpanEmptyFirst = !cir.struct<"SpanEmptyFirst" {empty !u8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, bitfield !u8i, pad !cir.array<!u8i x 3>}>
 
 // One unit covering both of these would need more than one register, so they
 // split into two units that are marked independently.
@@ -129,13 +133,20 @@ struct DerivedClipped : Clipped { char c; };
 // Name every record so that its CIR type reaches the output.
 void useTypes(HoldsEmpty *, NuaEmpty *, NuaEmptyUnion *, NuaPolyUnion *,
               NuaDerivedUnion *, NuaDerivesNonEmptyUnion *, BitFieldBase *,
-              DerivesBitFieldBase *, HasBitFieldVBase *, ZeroLenEmptyArr *,
+              DerivesBitFieldBase *, HasBitFieldVBase *, ZeroWidthWide *,
+              ZeroLenEmptyArr *,
               EmptyArr2 *, NuaEmptyArr *, OnlyUnnamedBit *, NamedClipped *,
               NamedFirst *, UnnamedFirst *, SpanMixed *, SpanEmptyFirst *,
               WideSpanMixed *, WideSpanEmptyFirst *, WideSpanAllEmpty *,
               UnnamedBitUnion *, NoMemberUnion *, Pod *, NearlyEmptyVBase *,
               HasNearlyEmptyVBase *, Clipped *, NamedClippedTail *) {}
 
+// The zero-length bit-field member leaves the record's alignment alone, so
+// these still agree with the classic layout.
+SpanMixed gSpanMixed;
+// LLVM-DAG: @gSpanMixed = global %struct.SpanMixed zeroinitializer, align 4
+SpanEmptyFirst gSpanEmptyFirst;
+// LLVM-DAG: @gSpanEmptyFirst = global %struct.SpanEmptyFirst zeroinitializer, align 4
 Empty gEmpty;
 AlignasTail gAlignasTail;
 
diff --git a/clang/test/CIR/IR/invalid-record-layout.cir b/clang/test/CIR/IR/invalid-record-layout.cir
index 4682985dd4e1e..61ebc9b37528c 100644
--- a/clang/test/CIR/IR/invalid-record-layout.cir
+++ b/clang/test/CIR/IR/invalid-record-layout.cir
@@ -17,64 +17,3 @@ module attributes {
                            has_trivial_dtor = true, record_align = 3>}
 } {
 }
-
-// -----
-
-module attributes {
-  cir.record_layouts = {
-    // expected-error @below {{failed to verify that zero_width_bitfield offsets and widths must be parallel}}
-    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
-                           has_trivial_dtor = true, record_align = 4,
-                           zero_width_bitfield_offsets = [0, 32],
-                           zero_width_bitfield_widths = [32]>}
-} {
-}
-
-// -----
-
-module attributes {
-  cir.record_layouts = {
-    // expected-error @below {{failed to verify that zero_width_bitfield offsets and widths must be parallel}}
-    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
-                           has_trivial_dtor = true, record_align = 4,
-                           zero_width_bitfield_offsets = [0]>}
-} {
-}
-
-// -----
-
-// A width reaches the classifier as an integer type, whose size is read in
-// bytes, so a sub-byte width has no type to carry it.
-module attributes {
-  cir.record_layouts = {
-    // expected-error @below {{failed to verify that a zero_width_bitfield width must be a positive multiple of 8}}
-    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
-                           has_trivial_dtor = true, record_align = 4,
-                           zero_width_bitfield_offsets = [0],
-                           zero_width_bitfield_widths = [4]>}
-} {
-}
-
-// -----
-
-module attributes {
-  cir.record_layouts = {
-    // expected-error @below {{failed to verify that a zero_width_bitfield width must be a positive multiple of 8}}
-    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
-                           has_trivial_dtor = true, record_align = 4,
-                           zero_width_bitfield_offsets = [0],
-                           zero_width_bitfield_widths = [0]>}
-} {
-}
-
-// -----
-
-module attributes {
-  cir.record_layouts = {
-    // expected-error @below {{failed to verify that a zero_width_bitfield offset must not be negative}}
-    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
-                           has_trivial_dtor = true, record_align = 4,
-                           zero_width_bitfield_offsets = [-8],
-                           zero_width_bitfield_widths = [32]>}
-} {
-}
diff --git a/clang/test/CIR/Lowering/copy-skip-tail-padding.cir b/clang/test/CIR/Lowering/copy-skip-tail-padding.cir
index e8146a522cab1..a521fba2f3e0e 100644
--- a/clang/test/CIR/Lowering/copy-skip-tail-padding.cir
+++ b/clang/test/CIR/Lowering/copy-skip-tail-padding.cir
@@ -5,6 +5,7 @@
 !u8i = !cir.int<u, 8>
 !u16i = !cir.int<u, 16>
 !s32i = !cir.int<s, 32>
+!s64i = !cir.int<s, 64>
 
 !rec_Plain = !cir.struct<"Plain" {data !s32i, data !s32i}>
 !rec_Trail =
@@ -23,6 +24,12 @@
 !rec_AlignasTail =
     !cir.struct<"AlignasTail" {data !s8i, pad !cir.array<!u8i x 7>, data !s32i,
                                pad !cir.array<!u8i x 4>}>
+!rec_TailZw =
+    !cir.struct<"TailZw" {data !s8i, pad !cir.array<!u8i x 3>,
+                          bitfield !cir.array<!s32i x 0>}>
+!rec_ZwAlign =
+    !cir.struct<"ZwAlign" {data !s8i, bitfield !cir.array<!s64i x 0>,
+                           data !s8i}>
 !rec_SpanMixed =
     !cir.struct<"SpanMixed" {data !u8i, pad !cir.array<!u8i x 3>, empty !u8i,
                              pad !cir.array<!u8i x 3>}>
@@ -127,6 +134,28 @@ module attributes {cir.triple = "x86_64-unknown-linux-gnu",
     cir.return
   }
 
+  // A zero-width bit-field holds no storage, so it neither ends the trailing
+  // run nor keeps the padding ahead of it inside the data size.
+  // CHECK-LABEL: llvm.func @tail_zero_width
+  // CHECK:         %[[N:.*]] = llvm.mlir.constant(1 : i64) : i64
+  // CHECK:         "llvm.intr.memcpy"(%{{.+}}, %{{.+}}, %[[N]])
+  cir.func @tail_zero_width(%arg0: !cir.ptr<!rec_TailZw>,
+                            %arg1: !cir.ptr<!rec_TailZw>) {
+    cir.copy %arg0 to %arg1 skip_tail_padding : !cir.ptr<!rec_TailZw>
+    cir.return
+  }
+
+  // Nor does it lend the record its declared type's alignment, which would
+  // otherwise push the member after it up to offset eight.
+  // CHECK-LABEL: llvm.func @zero_width_align
+  // CHECK:         %[[N:.*]] = llvm.mlir.constant(2 : i64) : i64
+  // CHECK:         "llvm.intr.memcpy"(%{{.+}}, %{{.+}}, %[[N]])
+  cir.func @zero_width_align(%arg0: !cir.ptr<!rec_ZwAlign>,
+                             %arg1: !cir.ptr<!rec_ZwAlign>) {
+    cir.copy %arg0 to %arg1 skip_tail_padding : !cir.ptr<!rec_ZwAlign>
+    cir.return
+  }
+
   // The run stops at the empty member: the pad after it drops, the pad
   // before it does not.
   // CHECK-LABEL: llvm.func @span_mixed
diff --git a/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir b/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir
index 188f57230fb39..2322608a80768 100644
--- a/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir
+++ b/clang/test/CIR/Transforms/abi-lowering/x86_64-aggregate-nyi.cir
@@ -12,6 +12,10 @@
 !rec_SWrapsOverAligned = !cir.struct<"SWrapsOverAligned" {data !cir.double, data !rec_UOverAligned}>
 !rec_UEmptyLarge = !cir.union<"UEmptyLarge" {}, padding = {!cir.array<!u8i x 32>}>
 !rec_P = !cir.struct<"P" packed {data !s8i, data !s32i}>
+!rec_NamedPlusZeroWidth =
+    !cir.struct<"NamedPlusZeroWidth" {bitfield !u8i, pad !cir.array<!u8i x 3>,
+                                      bitfield !cir.array<!s32i x 0>,
+                                      data !s32i, pad !cir.array<!u8i x 8>}>
 !rec_E = !cir.struct<"E" {pad !u8i}>
 !rec_EOver = !cir.struct<"EOver" {pad !cir.array<!u8i x 16>}>
 !rec_UPadByte = !cir.union<"UPadByte" {data !u8i}, padding = {!cir.array<!u8i x 3>}>
@@ -62,12 +66,9 @@ module attributes {
 
   // CHECK: not yet implemented for type '!cir.union<"UShortStorage"
 
-  // This is the shape CIRGen produces for a union of narrow bitfields, where
-  // the byte-sized members understate a wider storage unit that is all user
-  // data.  It is also the shape of a union of two `unsigned char` members
-  // carrying an alignment attribute, where the same bytes really are padding.
-  // The two coerce differently in classic CodeGen and are identical here, which
-  // is why neither is accepted.
+  // A union of narrow bit-fields and a union of two aligned `unsigned char`
+  // members produce this same shape, and classic CodeGen coerces them
+  // differently, so neither can be accepted.
   cir.func @take_byte_blob_union(%arg0: !rec_UByteBlobs) {
     cir.return
   }
@@ -83,10 +84,9 @@ module attributes {
   // CHECK: not yet implemented for type '!cir.struct<"SWrapsOverAligned"
 
   // A member-less union past two eightbytes still classifies Indirect in
-  // classic CodeGen (SysV's MEMORY rule applies unconditionally above that
-  // size, regardless of content), but this bridge has no member to build an
-  // Indirect coercion from.  Below the threshold the same shape is accepted
-  // and classifies Ignore, matching classic; see x86_64-union.cir take_empty.
+  // classic CodeGen, but this bridge has no member to build an Indirect
+  // coercion from.  Below the threshold the same shape is accepted and
+  // classifies Ignore, matching classic; see x86_64-union.cir take_empty.
   cir.func @take_empty_large_union(%arg0: !rec_UEmptyLarge) {
     cir.return
   }
@@ -224,4 +224,12 @@ module attributes {
   }
 
   // CHECK: not yet implemented for type '!cir.vector<2 x !cir.long_double<!cir.f80>>
+
+  // A zero-width bit-field no longer refuses a padded record, but a named
+  // access unit still does, and one record can carry both.
+  cir.func @take_named_plus_zero_width(%arg0: !rec_NamedPlusZeroWidth) {
+    cir.return
+  }
+
+  // CHECK: not yet implemented for type '!cir.struct<"NamedPlusZeroWidth"
 }
diff --git a/clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir b/clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir
index 6c91ba0c2d0ff..ccfd8aa218d6f 100644
--- a/clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir
+++ b/clang/test/CIR/Transforms/abi-lowering/x86_64-struct-padded.cir
@@ -3,6 +3,7 @@
 !s8i = !cir.int<s, 8>
 !s16i = !cir.int<s, 16>
 !s32i = !cir.int<s, 32>
+!s64i = !cir.int<s, 64>
 !u8i = !cir.int<u, 8>
 !u32i = !cir.int<u, 32>
 !u64i = !cir.int<u, 64>
@@ -13,7 +14,8 @@
 !rec_S1 = !cir.struct<"S1" {data !s16i, data !s16i, data !s16i}>
 !rec_anon_pad = !cir.struct<{data !rec_S1, pad !cir.array<!s8i x 2>}>
 !rec_UnnamedUnit = !cir.struct<"UnnamedUnit" {data !s32i, empty !u32i, pad !cir.array<!u8i x 8>}>
-!rec_Zw = !cir.struct<"Zw" {data !s32i, pad !cir.array<!u8i x 12>}>
+!rec_Zw = !cir.struct<"Zw" {data !s32i, bitfield !cir.array<!u32i x 0>, pad !cir.array<!u8i x 12>}>
+!rec_ZwWide = !cir.struct<"ZwWide" {data !s8i, pad !cir.array<!u8i x 7>, bitfield !cir.array<!s64i x 0>, data !s8i}>
 !rec_UnitSSE = !cir.struct<"UnitSSE" {data !cir.double, empty !u32i, pad !cir.array<!u8i x 4>}>
 !rec_PadByte = !cir.struct<"PadByte" {data !u8i, pad !cir.array<!u8i x 3>}>
 !rec_PadByte2 = !cir.struct<"PadByte2" {data !u8i, pad !u8i}>
@@ -39,9 +41,9 @@ module attributes {
     UnitSSE = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
                                  has_trivial_dtor = true, record_align = 16>,
     Zw = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
-                            has_trivial_dtor = true, record_align = 16,
-                            zero_width_bitfield_offsets = [32],
-                            zero_width_bitfield_widths = [32]>
+                            has_trivial_dtor = true, record_align = 16>,
+    ZwWide = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+                                has_trivial_dtor = true, record_align = 1>
   },
   dlti.dl_spec = #dlti.dl_spec<
     #dlti.dl_entry<i8, dense<8>: vector<2xi64>>,
@@ -111,14 +113,22 @@ module attributes {
 
   // CHECK: cir.func{{.*}} @take_unnamed_unit(%arg0: !u64i)
 
-  // `Zw` has the same members as `Ov`, so only the zero-width bit-field in the
-  // layout metadata keeps the eightbyte from narrowing to !s32i.
+  // `Zw` holds the same storage as `Ov`, so only the zero-width bit-field keeps
+  // the eightbyte from narrowing to !s32i.
   cir.func @take_zw(%arg0: !rec_Zw) {
     cir.return
   }
 
   // CHECK: cir.func{{.*}} @take_zw(%arg0: !u64i)
 
+  // The declared type is wider than the byte the record ends on, so the first
+  // eightbyte stays i64 while the second narrows to the trailing char.
+  cir.func @take_zw_wide(%arg0: !rec_ZwWide) {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_zw_wide(%arg0: !u64i, %arg1: !s8i)
+
   // The unit occupies the four bytes at offset 8.  Counting it as data would
   // split this into a double and an !u32i.
   cir.func @take_unit_sse(%arg0: !rec_UnitSSE) {
diff --git a/clang/unittests/CIR/RecordMemberKindTest.cpp b/clang/unittests/CIR/RecordMemberKindTest.cpp
index d266078eb9129..512820174f3f0 100644
--- a/clang/unittests/CIR/RecordMemberKindTest.cpp
+++ b/clang/unittests/CIR/RecordMemberKindTest.cpp
@@ -10,6 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/Diagnostics.h"
 #include "mlir/IR/MLIRContext.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
@@ -81,6 +82,100 @@ TEST_F(RecordMemberKindTest, EmptyForTheABIWhenNoMemberHoldsData) {
                    .isEmptyForABI());
 }
 
+TEST_F(RecordMemberKindTest, AZeroWidthBitFieldIsAZeroLengthArrayUnderTheMark) {
+  IntType u8 = getU8();
+  IntType s32 = IntType::get(&context, 32, true);
+  auto zeroLen = cir::ArrayType::get(s32, 0);
+  auto oneLen = cir::ArrayType::get(s32, 1);
+
+  EXPECT_TRUE(cir::isZeroWidthBitField(zeroLen, RecordMemberKind::BitField));
+  // The mark alone is a bit-field access unit, which holds storage.
+  EXPECT_FALSE(cir::isZeroWidthBitField(u8, RecordMemberKind::BitField));
+  EXPECT_FALSE(cir::isZeroWidthBitField(oneLen, RecordMemberKind::BitField));
+  // A zero-length array under `data` is a flexible array member.
+  EXPECT_FALSE(cir::isZeroWidthBitField(zeroLen, RecordMemberKind::Data));
+  EXPECT_FALSE(cir::isZeroWidthBitField(zeroLen, RecordMemberKind::Empty));
+  EXPECT_FALSE(cir::isZeroWidthBitField(zeroLen, RecordMemberKind::Pad));
+}
+
+TEST_F(RecordMemberKindTest, AZeroWidthBitFieldHoldsNoDataForTheABI) {
+  IntType u8 = getU8();
+  IntType s32 = IntType::get(&context, 32, true);
+  auto zeroLen = cir::ArrayType::get(s32, 0);
+
+  // A record of nothing but zero-width bit-fields declares no storage.
+  EXPECT_TRUE(makeStruct("zw", {zeroLen}, {RecordMemberKind::BitField})
+                  .isEmptyForABI());
+  EXPECT_TRUE(
+      makeStruct("zwzw", {zeroLen, zeroLen},
+                 {RecordMemberKind::BitField, RecordMemberKind::BitField})
+          .isEmptyForABI());
+  EXPECT_TRUE(makeStruct("zwpad", {zeroLen, u8},
+                         {RecordMemberKind::BitField, RecordMemberKind::Pad})
+                  .isEmptyForABI());
+  // A flexible array member holds data, so the same type under `data` does not.
+  EXPECT_FALSE(
+      makeStruct("fam", {zeroLen}, {RecordMemberKind::Data}).isEmptyForABI());
+  EXPECT_FALSE(makeStruct("zwdata", {zeroLen, u8},
+                          {RecordMemberKind::BitField, RecordMemberKind::Data})
+                   .isEmptyForABI());
+}
+
+TEST_F(RecordMemberKindTest, AZeroWidthBitFieldLendsNoSizeOrAlignment) {
+  IntType s8 = IntType::get(&context, 8, true);
+  IntType s64 = IntType::get(&context, 64, true);
+  IntType u8 = getU8();
+  auto zeroLen = cir::ArrayType::get(s64, 0);
+  auto pad7 = cir::ArrayType::get(u8, 7);
+
+  // Without the member the record is two bytes at alignment one.  The declared
+  // type would take it to sixteen bytes at alignment eight.
+  mlir::Type members[] = {s8, pad7, zeroLen, s8};
+  cir::RecordMemberKind kinds[] = {
+      RecordMemberKind::Data, RecordMemberKind::Pad, RecordMemberKind::BitField,
+      RecordMemberKind::Data};
+  auto ty = StructType::get(&context, getName("ZwLayout"), /*is_class=*/false);
+  ty.complete(members, /*packed=*/false, kinds);
+
+  OpBuilder builder(&context);
+  auto module = ModuleOp::create(builder.getUnknownLoc());
+  mlir::DataLayout dl(module);
+
+  EXPECT_EQ(dl.getTypeSizeInBits(ty).getFixedValue(), 72u);
+  EXPECT_EQ(dl.getTypeABIAlignment(ty), 1u);
+  // The member sits where the storage ahead of it ends, and the member after it
+  // is not pushed along by the declared type's alignment.
+  EXPECT_EQ(ty.getElementOffset(dl, 2), 8u);
+  EXPECT_EQ(ty.getElementOffset(dl, 3), 8u);
+
+  module->erase();
+}
+
+TEST_F(RecordMemberKindTest, ATrailingZeroWidthBitFieldLeavesTailPadding) {
+  IntType s8 = IntType::get(&context, 8, true);
+  IntType s32 = IntType::get(&context, 32, true);
+  IntType u8 = getU8();
+  auto zeroLen = cir::ArrayType::get(s32, 0);
+  auto pad3 = cir::ArrayType::get(u8, 3);
+
+  // The trailing run is the pad plus the zero-width bit-field, so a derived
+  // class may reuse every byte after the first.
+  mlir::Type members[] = {s8, pad3, zeroLen};
+  cir::RecordMemberKind kinds[] = {RecordMemberKind::Data,
+                                   RecordMemberKind::Pad,
+                                   RecordMemberKind::BitField};
+  auto ty = StructType::get(&context, getName("ZwTail"), /*is_class=*/false);
+  ty.complete(members, /*packed=*/false, kinds);
+
+  OpBuilder builder(&context);
+  auto module = ModuleOp::create(builder.getUnknownLoc());
+  mlir::DataLayout dl(module);
+
+  EXPECT_EQ(ty.computeStructDataSize(dl), 1u);
+
+  module->erase();
+}
+
 TEST_F(RecordMemberKindTest, PaddedFollowsThePadKinds) {
   IntType u8 = getU8();
   EXPECT_FALSE(makeStruct("d", {u8}, {RecordMemberKind::Data}).getPadded());
diff --git a/clang/unittests/CIR/RecordTypeMetadataTest.cpp b/clang/unittests/CIR/RecordTypeMetadataTest.cpp
index 5f75709327712..3e7b0d9054970 100644
--- a/clang/unittests/CIR/RecordTypeMetadataTest.cpp
+++ b/clang/unittests/CIR/RecordTypeMetadataTest.cpp
@@ -32,8 +32,8 @@ class RecordLayoutAttrTest : public ::testing::Test {
 };
 
 TEST_F(RecordLayoutAttrTest, CanPassInRegs) {
-  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
-                                    true, 4, /*offsets=*/{}, /*widths=*/{});
+  auto attr =
+      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 4);
   EXPECT_EQ(attr.getArgPassingKind(), ArgPassingKind::CanPassInRegs);
   EXPECT_TRUE(attr.getHasTrivialDtor());
   EXPECT_EQ(attr.getRecordAlign(), 4u);
@@ -41,44 +41,25 @@ TEST_F(RecordLayoutAttrTest, CanPassInRegs) {
 
 TEST_F(RecordLayoutAttrTest, CannotPassInRegs) {
   auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CannotPassInRegs,
-                                    false, 4, /*offsets=*/{}, /*widths=*/{});
+                                    false, 4);
   EXPECT_EQ(attr.getArgPassingKind(), ArgPassingKind::CannotPassInRegs);
   EXPECT_FALSE(attr.getHasTrivialDtor());
 }
 
 TEST_F(RecordLayoutAttrTest, CanNeverPassInRegs) {
-  auto attr =
-      RecordLayoutAttr::get(&context, ArgPassingKind::CanNeverPassInRegs, false,
-                            8, /*offsets=*/{}, /*widths=*/{});
+  auto attr = RecordLayoutAttr::get(
+      &context, ArgPassingKind::CanNeverPassInRegs, false, 8);
   EXPECT_EQ(attr.getArgPassingKind(), ArgPassingKind::CanNeverPassInRegs);
   EXPECT_FALSE(attr.getHasTrivialDtor());
   EXPECT_EQ(attr.getRecordAlign(), 8u);
 }
 
 TEST_F(RecordLayoutAttrTest, HighAlignment) {
-  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
-                                    true, 32, /*offsets=*/{}, /*widths=*/{});
+  auto attr =
+      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 32);
   EXPECT_EQ(attr.getRecordAlign(), 32u);
 }
 
-TEST_F(RecordLayoutAttrTest, NoZeroWidthBitFields) {
-  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
-                                    true, 4, /*offsets=*/{}, /*widths=*/{});
-  EXPECT_FALSE(attr.getZeroWidthBitfieldOffsets());
-  EXPECT_FALSE(attr.getZeroWidthBitfieldWidths());
-}
-
-TEST_F(RecordLayoutAttrTest, ZeroWidthBitFields) {
-  auto offsets = mlir::DenseI64ArrayAttr::get(&context, {32, 96});
-  auto widths = mlir::DenseI64ArrayAttr::get(&context, {32, 64});
-  auto attr = RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs,
-                                    true, 16, offsets, widths);
-  EXPECT_EQ(attr.getZeroWidthBitfieldOffsets().asArrayRef(),
-            ArrayRef<int64_t>({32, 96}));
-  EXPECT_EQ(attr.getZeroWidthBitfieldWidths().asArrayRef(),
-            ArrayRef<int64_t>({32, 64}));
-}
-
 TEST_F(RecordLayoutAttrTest, RecordTypeUnchanged) {
   IntType i32 = IntType::get(&context, 32, true);
   auto ty = StructType::get(&context, getName("Foo"), /*is_class=*/false);
@@ -94,8 +75,7 @@ TEST_F(RecordLayoutAttrTest, ModuleLevelLookup) {
   auto module = mlir::ModuleOp::create(loc);
 
   auto layoutAttr =
-      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 8,
-                            /*offsets=*/{}, /*widths=*/{});
+      RecordLayoutAttr::get(&context, ArgPassingKind::CanPassInRegs, true, 8);
 
   llvm::SmallVector<mlir::NamedAttribute> entries;
   entries.push_back(mlir::NamedAttribute(getName("TestRecord"), layoutAttr));

>From 91039b02c11fefae212a191af955bcce5eaf50e2 Mon Sep 17 00:00:00 2001
From: Adam Smith <adams at nvidia.com>
Date: Sat, 22 Aug 2026 14:52:14 -0700
Subject: [PATCH 3/3] [CIR] Fix lowering a constant record with a zero-width
 bit-field

A record constant built a member at a time tried to write the bit-field's
element, which keeps the declared type, into a field that lowers to no
bytes.  The verifier rejected the insert and the compile aborted.  The
visitor now writes a zero of the field's own type there instead.  A
flexible array member's field also holds no bytes, but it does have the
element's type, so it keeps its own value.

holdsDataForABI takes the member type alongside the kind now, so a
zero-width bit-field cannot read as holding data from the kind alone.

Assisted-by: Cursor / claude-opus-5
---
 clang/include/clang/CIR/Dialect/IR/CIRTypes.h | 34 ++++++++++++++-----
 .../CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp |  3 +-
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         |  8 +----
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 22 +++++++++++-
 ...conv-lowering-x86_64-zero-width-bitfield.c | 26 ++++++++++++++
 clang/unittests/CIR/RecordMemberKindTest.cpp  |  7 ++++
 6 files changed, 82 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
index c3722b14c08b8..bf2de3bc4b924 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
@@ -23,6 +23,8 @@
 #include "clang/CIR/Dialect/IR/CIRAttrs.h"
 #include "clang/CIR/Dialect/IR/CIROpsEnums.h"
 #include "clang/CIR/Interfaces/CIRTypeInterfaces.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 
 namespace llvm {
@@ -38,11 +40,32 @@ struct UnionTypeStorage;
 
 bool isValidFundamentalIntWidth(unsigned width);
 
-/// Whether a member of this kind holds data for argument passing.
-inline bool holdsDataForABI(RecordMemberKind kind) {
+/// Whether a record member is a zero-width bit-field, spelled as a zero-length
+/// array of the bit-field's declared type under the bit-field mark.  The ABI
+/// counts that declared type as user data, while the zero length keeps the
+/// member out of the record's storage, so it contributes neither size nor
+/// alignment to the record's layout.
+bool isZeroWidthBitField(mlir::Type memberTy, RecordMemberKind kind);
+
+/// Whether a member holds data for argument passing.  The kind alone does not
+/// answer this: a zero-width bit-field takes the bit-field mark but occupies no
+/// storage, so it holds none.
+inline bool holdsDataForABI(mlir::Type memberTy, RecordMemberKind kind) {
+  if (isZeroWidthBitField(memberTy, kind))
+    return false;
   return kind == RecordMemberKind::Data || kind == RecordMemberKind::BitField;
 }
 
+/// Whether any member holds data for argument passing.  The two ranges are the
+/// member types and their marks, in order.
+inline bool anyMemberHoldsDataForABI(llvm::ArrayRef<mlir::Type> memberTys,
+                                     llvm::ArrayRef<RecordMemberKind> kinds) {
+  return llvm::any_of(llvm::zip_equal(memberTys, kinds), [](const auto &pair) {
+    auto [memberTy, kind] = pair;
+    return holdsDataForABI(memberTy, kind);
+  });
+}
+
 /// Whether a member of this kind is a bit-field access unit holding data.  The
 /// compiler chooses an access unit's width, so the member can be narrower than
 /// the declared type of the bit-fields it holds.  A true answer does not mean
@@ -54,13 +77,6 @@ inline bool isBitFieldAccessUnit(RecordMemberKind kind) {
   return kind == RecordMemberKind::BitField;
 }
 
-/// Whether a record member is a zero-width bit-field, spelled as a zero-length
-/// array of the bit-field's declared type under the bit-field mark.  The ABI
-/// counts that declared type as user data, while the zero length keeps the
-/// member out of the record's storage, so it contributes neither size nor
-/// alignment to the record's layout.
-bool isZeroWidthBitField(mlir::Type memberTy, RecordMemberKind kind);
-
 /// Returns true if the type is a CIR sized type.
 ///
 /// Types are sized if they implement SizedTypeInterface and
diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
index 4ac3a63ffd33a..a103b68331a8d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
@@ -1042,7 +1042,8 @@ void CIRRecordLowering::lowerUnion(bool nonVirtualBaseType) {
     // and still marks bitfield.  Computed before clearFields() drops the
     // variant marks.
     const cir::RecordMemberKind storageKind = makeMemberKind(
-        /*holdsData=*/llvm::any_of(getFieldKinds(), cir::holdsDataForABI),
+        /*holdsData=*/
+        cir::anyMemberHoldsDataForABI(getFieldTypes(), getFieldKinds()),
         /*isBitFieldAccessUnit=*/llvm::any_of(getFieldKinds(),
                                               cir::isBitFieldAccessUnit));
     clearFields();
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index adc15c7ddbf9c..c5865798f7ae0 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -693,13 +693,7 @@ bool RecordType::isEmptyForABI() const {
   // holding no data.
   if (isIncomplete())
     return false;
-  // A zero-width bit-field occupies no storage and holds no data, so a record
-  // of nothing but those is empty for the ABI just as the AST predicate says.
-  return llvm::none_of(
-      llvm::zip_equal(getMembers(), getMemberKinds()), [](const auto &pair) {
-        auto [memberTy, kind] = pair;
-        return holdsDataForABI(kind) && !isZeroWidthBitField(memberTy, kind);
-      });
+  return !anyMemberHoldsDataForABI(getMembers(), getMemberKinds());
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index e7d0c34f9291c..65bd2064b956e 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -857,6 +857,23 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstRecordAttr constRecord) {
 
   uint64_t insertIdx = 0;
   auto paddingItr = paddingAddedIndexes.begin();
+  auto structTy = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(llvmTy);
+
+  // The field \p elt lands in, when that field holds no bytes and the element
+  // kept a type it cannot take.  Only a zero-width bit-field is like this.  A
+  // flexible array member's field also holds no bytes, but does match.
+  auto emptyFieldFor = [&](uint64_t idx, mlir::Attribute elt) -> mlir::Type {
+    if (!structTy || idx >= structTy.getBody().size())
+      return {};
+    mlir::Type fieldTy = structTy.getBody()[idx];
+    auto arrayTy = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(fieldTy);
+    if (!arrayTy || arrayTy.getNumElements() != 0)
+      return {};
+    auto typedElt = mlir::dyn_cast<mlir::TypedAttr>(elt);
+    if (!typedElt || converter->convertType(typedElt.getType()) == fieldTy)
+      return {};
+    return fieldTy;
+  };
 
   // Iteratively lower each constant element of the record.
   for (auto [idx, elt] : llvm::enumerate(constRecord.getMembers())) {
@@ -865,7 +882,10 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstRecordAttr constRecord) {
       ++paddingItr;
     }
 
-    mlir::Value init = visit(elt);
+    mlir::Type emptyTy = emptyFieldFor(insertIdx, elt);
+    mlir::Value init = emptyTy
+                           ? mlir::LLVM::ZeroOp::create(rewriter, loc, emptyTy)
+                           : visit(elt);
     result = mlir::LLVM::InsertValueOp::create(rewriter, loc, result, init,
                                                insertIdx);
     ++insertIdx;
diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
index 4e15cf7d32f1a..62c16aff0daf5 100644
--- a/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
+++ b/clang/test/CIR/CodeGen/call-conv-lowering-x86_64-zero-width-bitfield.c
@@ -29,6 +29,32 @@ typedef struct { int x; unsigned : 0; }
     __attribute__((ms_struct, aligned(16))) MsTail;
 typedef union { int a; Mid m; } ZeroWidthUnion;
 
+// An initializer the lowering builds a member at a time writes the empty field
+// in place of the bit-field's element, since the field holds no bytes and is
+// not the declared type the element carries.  A relocatable and a `_BitInt`
+// both take that path, reached through an array element and a member record
+// as well.
+extern int arr[4];
+typedef struct { int *p; int : 0; int y; } InitPtr;
+InitPtr gInitPtr = {&arr[2], 5};
+typedef struct { _BitInt(7) b; int : 0; int y; } InitBitInt;
+InitBitInt gInitBitInt = {3, 4};
+InitPtr gInitArr[2] = {{&arr[1], 1}, {&arr[2], 2}};
+typedef struct { InitPtr inner; int z; } InitNest;
+InitNest gInitNest = {{&arr[3], 7}, 9};
+// LLVM-CIR-DAG: @gInitPtr = global %struct.InitPtr { ptr getelementptr{{.*}}(i8, ptr @arr, i64 8), [0 x i8] zeroinitializer, i32 5 }, align 8
+// LLVM-OGCG-DAG: @gInitPtr = global { ptr, i32, [4 x i8] } { ptr getelementptr{{.*}}(i8, ptr @arr, i64 8), i32 5, [4 x i8] zeroinitializer }, align 8
+// LLVM-CIR-DAG: @gInitBitInt = global %struct.InitBitInt { i8 3, [3 x i8] zeroinitializer, [0 x i8] zeroinitializer, i32 4 }, align 4
+// LLVM-OGCG-DAG: @gInitBitInt = global { i8, [3 x i8], i32 } { i8 3, [3 x i8] zeroinitializer, i32 4 }, align 4
+// LLVM-CIR-DAG: @gInitArr = global [2 x %struct.InitPtr] [%struct.InitPtr { ptr getelementptr{{.*}}(i8, ptr @arr, i64 4), [0 x i8] zeroinitializer, i32 1 }, %struct.InitPtr { ptr getelementptr{{.*}}(i8, ptr @arr, i64 8), [0 x i8] zeroinitializer, i32 2 }], align 16
+// LLVM-CIR-DAG: @gInitNest = global %struct.InitNest { %struct.InitPtr { ptr getelementptr{{.*}}(i8, ptr @arr, i64 12), [0 x i8] zeroinitializer, i32 7 }, i32 9 }, align 8
+
+// A flexible array member's field holds no bytes too, but it does have the
+// element's type, so it keeps its own initializer.
+typedef struct { int *p; int fam[]; } FamRel;
+FamRel gFamRel = {&arr[2]};
+// LLVM-DAG: @gFamRel = global %struct.FamRel { ptr getelementptr{{.*}}(i8, ptr @arr, i64 8), [0 x i32] zeroinitializer }, align 8
+
 // CIR-DAG: !rec_Tail = !cir.struct<"Tail" {data !s32i, bitfield !cir.array<!u32i x 0>, pad !cir.array<!u8i x 12>}>
 // CIR-DAG: !rec_Mid = !cir.struct<"Mid" {data !s8i, pad !cir.array<!u8i x 3>, bitfield !cir.array<!s32i x 0>, data !s32i}>
 // CIR-DAG: !rec_Plain = !cir.struct<"Plain" {data !s32i, bitfield !cir.array<!s32i x 0>, data !s32i}>
diff --git a/clang/unittests/CIR/RecordMemberKindTest.cpp b/clang/unittests/CIR/RecordMemberKindTest.cpp
index 512820174f3f0..e8460b7c00d63 100644
--- a/clang/unittests/CIR/RecordMemberKindTest.cpp
+++ b/clang/unittests/CIR/RecordMemberKindTest.cpp
@@ -103,6 +103,13 @@ TEST_F(RecordMemberKindTest, AZeroWidthBitFieldHoldsNoDataForTheABI) {
   IntType s32 = IntType::get(&context, 32, true);
   auto zeroLen = cir::ArrayType::get(s32, 0);
 
+  EXPECT_FALSE(cir::holdsDataForABI(zeroLen, RecordMemberKind::BitField));
+  EXPECT_TRUE(cir::holdsDataForABI(u8, RecordMemberKind::BitField));
+  // A zero-length array under `data` is a flexible array member, which does.
+  EXPECT_TRUE(cir::holdsDataForABI(zeroLen, RecordMemberKind::Data));
+  EXPECT_FALSE(cir::holdsDataForABI(u8, RecordMemberKind::Pad));
+  EXPECT_FALSE(cir::holdsDataForABI(u8, RecordMemberKind::Empty));
+
   // A record of nothing but zero-width bit-fields declares no storage.
   EXPECT_TRUE(makeStruct("zw", {zeroLen}, {RecordMemberKind::BitField})
                   .isEmptyForABI());



More information about the cfe-commits mailing list