Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR]Achieve union's bitfields additionally. #742

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 9 additions & 8 deletions clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 271,6 @@ void CIRRecordLowering::lower(bool nonVirtualBaseType) {

CharUnits Size = nonVirtualBaseType ? astRecordLayout.getNonVirtualSize()
: astRecordLayout.getSize();
if (recordDecl->isUnion()) {
llvm_unreachable("NYI");
// lowerUnion();
// computeVolatileBitfields();
return;
}
accumulateFields();

// RD implies C
Expand Down Expand Up @@ -316,13 310,20 @@ void CIRRecordLowering::lowerUnion() {
// type would work fine and be simpler but would be different than what we've
// been doing and cause lit tests to change.
for (const auto *Field : recordDecl->fields()) {

mlir::Type FieldType = nullptr;
if (Field->isBitField()) {
if (Field->isZeroLengthBitField(astContext))
continue;
llvm_unreachable("NYI");

FieldType = getBitfieldStorageType(Field->getBitWidthValue(astContext));

setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
} else {
FieldType = getStorageType(Field);
}
fields[Field->getCanonicalDecl()] = 0;
auto FieldType = getStorageType(Field);
// auto FieldType = getStorageType(Field);
// Compute zero-initializable status.
// This union might not be zero initialized: it may contain a pointer to
// data member which might have some exotic initialization sequence.
Expand Down
32 changes: 32 additions & 0 deletions clang/test/CIR/CodeGen/bitfield-union.c
Original file line number Diff line number Diff line change
@@ -0,0 1,32 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

void main() {
union demo {
int x;
int y : 4;
int z : 8;
};
union demo d;
d.x = 1;
d.y = 2;
d.z = 0;
}

// CHECK: !ty_22demo22 = !cir.struct<union "demo" {!cir.int<s, 32>, !cir.int<u, 8>, !cir.int<u, 8>}>
// CHECK: #bfi_y = #cir.bitfield_info<name = "y", storage_type = !u8i, size = 4, offset = 0, is_signed = true>
// CHECK: #bfi_z = #cir.bitfield_info<name = "z", storage_type = !u8i, size = 8, offset = 0, is_signed = true>

// cir.func no_proto @main() extra(#fn_attr) {
// %0 = cir.alloca !ty_22demo22, !cir.ptr<!ty_22demo22>, ["d"] {alignment = 4 : i64}
// %1 = cir.const #cir.int<1> : !s32i
// %2 = cir.get_member %0[0] {name = "x"} : !cir.ptr<!ty_22demo22> -> !cir.ptr<!s32i>
// cir.store %1, %2 : !s32i, !cir.ptr<!s32i>
// %3 = cir.const #cir.int<2> : !s32i
// %4 = cir.cast(bitcast, %0 : !cir.ptr<!ty_22demo22>), !cir.ptr<!u8i>
// %5 = cir.set_bitfield(#bfi_y, %4 : !cir.ptr<!u8i>, %3 : !s32i) -> !s32i
// %6 = cir.const #cir.int<0> : !s32i loc(#loc10)
// %7 = cir.cast(bitcast, %0 : !cir.ptr<!ty_22demo22>), !cir.ptr<!u8i>
// %8 = cir.set_bitfield(#bfi_z, %7 : !cir.ptr<!u8i>, %6 : !s32i) -> !s32i
// cir.return
// }
Loading