[clang] [CIR] Fix record layout for a union with no storage type (PR #213591)

Adam Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 3 14:48:33 PDT 2026


================
@@ -727,15 +727,16 @@ StructType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
 llvm::TypeSize
 UnionType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
                              mlir::DataLayoutEntryListRef params) const {
-  mlir::Type storage = getUnionStorageType(dataLayout);
-  if (!storage)
-    return llvm::TypeSize::getFixed(0);
+  // A union whose member list came out empty has no storage type, so whatever
+  // size it has lives entirely in the padding field below.  Sum both.
+  llvm::TypeSize size = llvm::TypeSize::getFixed(0);
+  if (mlir::Type storage = getUnionStorageType(dataLayout))
+    size += dataLayout.getTypeSizeInBits(storage);
   // The padding field holds enough bytes to bring the total up to the AST
----------------
adams381 wrote:

I've simplified the comments to a single top comment that is now a three-liner.  The effect is subtle enough I don't want it to get lost again.

I agree about `getTypeSizeInBits`.  I'll try to keep that in mind.

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


More information about the cfe-commits mailing list