[clang] [CIR] Add initial support for bitfields in structs (PR #142041)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Fri May 30 14:04:59 PDT 2025


andykaylor wrote:

@bcardosolopes, I have some general concerns about the way that we're representing bitfields and bitfield accesses. It seems fundamentally inconsistent with the way we represent unions. For example:
```
union U {
  char a;
  int b;
  long c;
};
```
becomes

`!cir.record<union "U" {!s8i, !s32i, !s64i}`

but
```
struct S {
  int x;
  unsigned a:2;
  unsigned b:5;
  unsigned c:7;
} s;
```
becomes

`!cir.record<struct "S" {!s32i, !u16i}`

And all three bitfield members are hidden away inside the second member, leading to the need for two operations to access a bitfield member:

```
    %3 = cir.get_member %2[1] {name = "b"} : !cir.ptr<!rec_S> -> !cir.ptr<!u16i> loc(#loc11)
    %4 = cir.get_bitfield(#bfi_b, %3 : !cir.ptr<!u16i>) -> !u32i loc(#loc12)
```

Why are we representing each member of a union individually but packing bitfields away in integer fields?

What would you think of something like this?

`!cir.record<struct "S" {!s32i, !cir.bitfield<2>, !cir.bitfield<5>, !cir.bitfield<7>}`

That puts a bit more burden on the  `RecordType::getElementOffset` implementation, but I think it would let us defer some of the details to lowering.

https://github.com/llvm/llvm-project/pull/142041


More information about the cfe-commits mailing list