Skip to content

Commit 51aa8f2

Browse files
Add support for bitfields in unions
1 parent f242360 commit 51aa8f2

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,11 +655,14 @@ void CIRRecordLowering::lowerUnion() {
655655
// locate the "most appropriate" storage type.
656656
for (const FieldDecl *field : recordDecl->fields()) {
657657
mlir::Type fieldType;
658-
if (field->isBitField())
659-
cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(),
660-
"bitfields in lowerUnion");
661-
else
658+
if (field->isBitField()) {
659+
if (field->isZeroLengthBitField())
660+
continue;
661+
fieldType = getBitfieldStorageType(field->getBitWidthValue());
662+
setBitFieldInfo(field, CharUnits::Zero(), fieldType);
663+
} else {
662664
fieldType = getStorageType(field);
665+
}
663666

664667
// This maps a field to its index. For unions, the index is always 0.
665668
fieldIdxMap[field->getCanonicalDecl()] = 0;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
7+
8+
typedef union {
9+
int x;
10+
int y : 4;
11+
int z : 8;
12+
} demo;
13+
14+
// CIR: !rec_demo = !cir.record<union "demo" {!s32i, !u8i, !u8i}>
15+
// LLVM: %union.demo = type { i32 }
16+
// OGCG: %union.demo = type { i32 }
17+
demo d;
18+
19+

0 commit comments

Comments
 (0)