Skip to content

Commit 9627944

Browse files
[CIR] Fix access to bitfields inside a union
1 parent 3ecfc03 commit 9627944

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,11 @@ Address CIRGenFunction::getAddrOfBitFieldStorage(LValue base,
372372
unsigned index) {
373373
mlir::Location loc = getLoc(field->getLocation());
374374
cir::PointerType fieldPtr = cir::PointerType::get(fieldType);
375+
auto rec = cast<cir::RecordType>(base.getAddress().getElementType());
376+
if (index == 0 && rec.isUnion())
377+
return base.getAddress();
375378
cir::GetMemberOp sea = getBuilder().createGetMember(
376379
loc, fieldPtr, base.getPointer(), field->getName(), index);
377-
auto rec = cast<cir::RecordType>(base.getAddress().getElementType());
378380
CharUnits offset = CharUnits::fromQuantity(
379381
rec.getElementOffset(cgm.getDataLayout().layout, index));
380382
return Address(sea, base.getAlignment().alignmentAtOffset(offset));

clang/test/CIR/CodeGen/bitfield-union.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,44 @@ typedef union {
2828

2929
demo d;
3030
zero_bit z;
31+
32+
int main() {
33+
demo d;
34+
d.x = 1;
35+
d.y = 2;
36+
d.z = 0;
37+
}
38+
39+
// CIR: #bfi_y = #cir.bitfield_info<name = "y", storage_type = !u8i, size = 4, offset = 0, is_signed = true>
40+
// CIR: #bfi_z = #cir.bitfield_info<name = "z", storage_type = !u8i, size = 8, offset = 0, is_signed = true>
41+
42+
// CIR: cir.func no_proto dso_local @main
43+
// CIR: [[ALLOC:%.*]] = cir.alloca !rec_demo, !cir.ptr<!rec_demo>, ["d"] {alignment = 4 : i64}
44+
// CIR: [[ONE:%.*]] = cir.const #cir.int<1> : !s32i
45+
// CIR: [[X:%.*]] = cir.get_member [[ALLOC]][0] {name = "x"} : !cir.ptr<!rec_demo> -> !cir.ptr<!s32i>
46+
// CIR: cir.store align(4) [[ONE]], [[X]] : !s32i, !cir.ptr<!s32i>
47+
// CIR: [[TWO:%.*]] = cir.const #cir.int<2> : !s32i
48+
// CIR: [[BITCAST:%.*]] = cir.cast(bitcast, [[ALLOC]] : !cir.ptr<!rec_demo>), !cir.ptr<!u8i>
49+
// CIR: [[SET:%.*]] = cir.set_bitfield align(4) (#bfi_y, [[BITCAST]] : !cir.ptr<!u8i>, [[TWO]] : !s32i) -> !s32i
50+
// CIR: [[ZERO:%.*]] = cir.const #cir.int<0> : !s32i
51+
// CIR: [[BITCAST2:%.*]] = cir.cast(bitcast, [[ALLOC]] : !cir.ptr<!rec_demo>), !cir.ptr<!u8i>
52+
// CIR: [[SET2:%.*]] = cir.set_bitfield align(4) (#bfi_z, [[BITCAST2]] : !cir.ptr<!u8i>, [[ZERO]] : !s32i) -> !s32i
53+
// CIR: cir.return
54+
55+
// LLVM: define dso_local i32 @main
56+
// LLVM: [[ALLOC:%.*]] = alloca %union.demo, i64 1, align 4
57+
// LLVM: store i32 1, ptr [[ALLOC]], align 4
58+
// LLVM: [[BFLOAD:%.*]] = load i8, ptr [[ALLOC]], align 4
59+
// LLVM: [[CLEAR:%.*]] = and i8 [[BFLOAD]], -16
60+
// LLVM: [[SET:%.*]] = or i8 [[CLEAR]], 2
61+
// LLVM: store i8 [[SET]], ptr [[ALLOC]], align 4
62+
// LLVM: store i8 0, ptr [[ALLOC]], align 4
63+
64+
// OGCG: define dso_local i32 @main
65+
// OGCG: [[ALLOC:%.*]] = alloca %union.demo, align 4
66+
// OGCG: store i32 1, ptr [[ALLOC]], align 4
67+
// OGCG: [[BFLOAD:%.*]] = load i8, ptr [[ALLOC]], align 4
68+
// OGCG: [[CLEAR:%.*]] = and i8 [[BFLOAD]], -16
69+
// OGCG: [[SET:%.*]] = or i8 [[CLEAR]], 2
70+
// OGCG: store i8 [[SET]], ptr [[ALLOC]], align 4
71+
// OGCG: store i8 0, ptr [[ALLOC]], align 4

0 commit comments

Comments
 (0)