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

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 3 13:54:00 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
----------------
erichkeane wrote:

Actually. tis a touch of a shame that `getTypeSizeInBits` doesn't just return 0 on `null` `mlir::Type`, else this becomes:

`return dataLayout.getTypeSizeInBits(getUnionStorageType(dataLayout)) + dataLayout.getTypeSizeInBits(getPadding());`

Something to keep an eye out if we have a similar need in the future, might be worth extracting the `if !type, return 0, else get type-size-in-bits' into its own function if this keeps happening.

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


More information about the cfe-commits mailing list