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

Conversation

566hub
Copy link

@566hub 566hub commented Jul 15, 2024

Achieve union's bitfields additionally.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

When I try to build the testcase, I get a crash in a place that doesn't seem related to the changes here:

for (const auto *Field : recordDecl->fields()) {
    if (Field->isBitField()) {
      if (Field->isZeroLengthBitField(astContext))
        continue;
      llvm_unreachable("NYI");
    }

Can you elaborate a bit more on the approach and what are you trying to achieve? @gitoleg can you also review this PR?

@DukeWJ
Copy link

DukeWJ commented Jul 17, 2024

Thanks for the PR.

When I try to build the testcase, I get a crash in a place that doesn't seem related to the changes here:

for (const auto *Field : recordDecl->fields()) {
    if (Field->isBitField()) {
      if (Field->isZeroLengthBitField(astContext))
        continue;
      llvm_unreachable("NYI");
    }

Can you elaborate a bit more on the approach and what are you trying to achieve? @gitoleg can you also review this PR?

Hi, I have met the scene that codegen union with bitfields with clangir once, and it reported 'NYI' yet. I think this PR just reuse infrastructure of struct bitfields to explain union bitfiels type. I try locally and it seems work, and new union type has carried bitfield info I think.
input

union demo {
    int x : 16;
    int y;
};

int main() {
    union demo a;

    a.x = 0;
    a.y = 1;
}

and the output

!ty_22demo22 = !cir.struct<union "demo" {!cir.int<u, 16>, !cir.int<s, 32>}>
#bfi_x = #cir.bitfield_info<name = "x", storage_type = !u16i, size = 16, offset = 0, is_signed = true>

func {
  %1 = cir.alloca !ty_22demo22, !cir.ptr<!ty_22demo22>, ["a"] {alignment = 4 : i64} loc(#loc17)
  %3 = cir.const #cir.int<0> : !s32i loc(#loc7)
  %4 = cir.cast(bitcast, %1 : !cir.ptr<!ty_22demo22>), !cir.ptr<!u16i> loc(#loc8)
  %5 = cir.set_bitfield(#bfi_x, %4 : !cir.ptr<!u16i>, %3 : !s32i) -> !s32i loc(#loc8)

  %6 = cir.const #cir.int<1> : !s32i loc(#loc9)
  %7 = cir.get_member %1[1] {name = "y"} : !cir.ptr<!ty_22demo22> -> !cir.ptr<!s32i> loc(#loc10)
  cir.store %6, %7 : !s32i, !cir.ptr<!s32i> loc(#loc19)
}

@gitoleg
Copy link
Collaborator

gitoleg commented Jul 17, 2024

looks reasonable ... though frankly speaking I didn't know that it's somehow possible (and useful!) to use bit fields in unions. But anyways, looks ok so far.

@gitoleg gitoleg closed this Jul 17, 2024
@gitoleg gitoleg reopened this Jul 17, 2024
@gitoleg
Copy link
Collaborator

gitoleg commented Jul 17, 2024

sorry, my bad, pushed wrong button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants