Skip to content

Commit 8763d29

Browse files
[CIR] Add support for bitfields in unions (#145096)
This PR introduces support for bitfields inside unions in CIR.
1 parent 9291ad1 commit 8763d29

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,11 +704,14 @@ void CIRRecordLowering::lowerUnion() {
704704
// locate the "most appropriate" storage type.
705705
for (const FieldDecl *field : recordDecl->fields()) {
706706
mlir::Type fieldType;
707-
if (field->isBitField())
708-
cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(),
709-
"bitfields in lowerUnion");
710-
else
707+
if (field->isBitField()) {
708+
if (field->isZeroLengthBitField())
709+
continue;
710+
fieldType = getBitfieldStorageType(field->getBitWidthValue());
711+
setBitFieldInfo(field, CharUnits::Zero(), fieldType);
712+
} else {
711713
fieldType = getStorageType(field);
714+
}
712715

713716
// This maps a field to its index. For unions, the index is always 0.
714717
fieldIdxMap[field->getCanonicalDecl()] = 0;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
18+
typedef union {
19+
int x;
20+
int y : 3;
21+
int : 0;
22+
int z : 2;
23+
} zero_bit;
24+
25+
// CIR: !rec_zero_bit = !cir.record<union "zero_bit" {!s32i, !u8i, !u8i}>
26+
// LLVM: %union.zero_bit = type { i32 }
27+
// OGCG: %union.zero_bit = type { i32 }
28+
29+
demo d;
30+
zero_bit z;

0 commit comments

Comments
 (0)