Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void CIRRecordLowering::lowerUnion() {
}
// If we have no storage type just pad to the appropriate size and return.
if (!StorageType)
llvm_unreachable("no-storage union NYI");
return appendPaddingBytes(LayoutSize);
// If our storage size was bigger than our required size (can happen in the
// case of packed bitfields on Itanium) then just use an I8 array.
if (LayoutSize < getSize(StorageType))
Expand Down
10 changes: 7 additions & 3 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,9 @@ RecordType::computeUnionSize(const mlir::DataLayout &dataLayout) const {
unsigned recordSize = 0;
llvm::Align recordAlignment{1};

auto largestMember = getLargestMember(dataLayout);
recordSize = dataLayout.getTypeSize(largestMember);
Type largestMember = getLargestMember(dataLayout);
if (largestMember)
recordSize = dataLayout.getTypeSize(largestMember);

// If the union is padded, add the padding to the size.
if (getPadded()) {
Expand Down Expand Up @@ -517,7 +518,10 @@ RecordType::computeStructAlignment(const mlir::DataLayout &dataLayout) const {

uint64_t
RecordType::computeUnionAlignment(const mlir::DataLayout &dataLayout) const {
auto largestMember = getLargestMember(dataLayout);
Type largestMember = getLargestMember(dataLayout);
// use 1 byte alignment for empty union
if (!largestMember)
return 1;
return dataLayout.getTypeABIAlignment(largestMember);
}

Expand Down
22 changes: 22 additions & 0 deletions clang/test/CIR/CodeGen/union-empty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM

union EmptyUnion {
EmptyUnion() = default;
};

void f0() {
EmptyUnion e;
};

// CIR: !rec_EmptyUnion = !cir.record<union "EmptyUnion" padded {!u8i}>
// CIR: cir.func dso_local @_Z2f0v()
// CIR: %0 = cir.alloca !rec_EmptyUnion, !cir.ptr<!rec_EmptyUnion>, ["e"] {alignment = 1 : i64}
// CIR: cir.return

// LLVM: %union.EmptyUnion = type { i8 }
// LLVM: define dso_local void @_Z2f0v()
// LLVM: %1 = alloca %union.EmptyUnion, i64 1, align 1
// LLVM: ret void
Loading