Skip to content

support larger bit field #528

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chengchingwen
Copy link

This PR reworks the bit field get/set property code to support bit field larger than 32 bits

@chengchingwen chengchingwen marked this pull request as draft February 15, 2025 03:37
temporary disable other test

using test

specific int bits

use offset for base

add back tests
@chengchingwen chengchingwen marked this pull request as ready for review February 23, 2025 14:35
@chengchingwen
Copy link
Author

@christiangnrd Could you help review this PR?

@christiangnrd
Copy link
Contributor

I'm not familiar enough with this part of the codebase to review this. I gave it a try but it would take more bandwidth than I have at the moment.

ex = :(f === $(QuoteNode(fsym)) && return (Ptr{$ty}(x + $(4d)), $r, $w))
iszero(w) && continue
d, r = divrem(offset, 8)
@assert r + w <= 128 "Cannot handle bitfield larger than 128 bits"
Copy link
Member

Choose a reason for hiding this comment

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

Is this documented in the standard?

Copy link
Author

Choose a reason for hiding this comment

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

I don't think so. It's just super rare for a c/c++ code to have a primitive type that is larger than 128 bits, so I thought it would be reasonable to be left unhandled until we find a real use case.

Copy link
Member

Choose a reason for hiding this comment

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

The test only has 64-bit test cases. Do you have a specific need for the 128-bit support?

Copy link
Author

Choose a reason for hiding this comment

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

No, I only need 64-bit support

struct LargeBitField {
double a;
short b;
unsigned long long c : 16;
Copy link
Member

Choose a reason for hiding this comment

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

@chengchingwen It looks like you removed the logic for handling straddle. Does it work if you change this to 20?

unsigned long long c : 16;

Copy link
Member

@Gnimuc Gnimuc Mar 30, 2025

Choose a reason for hiding this comment

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

20+48>64, so d overlaps another "word".

Copy link
Author

Choose a reason for hiding this comment

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

@Gnimuc The reason that the old logic has a branch for straddle is because they only perform 32-bit load on 32-bit-aligned "offset" thus a bit field would span across two 32-bit segments as long as the offset is smaller than multiples of 32 and adding the bit width exceeds multiples of 32. The new logic performs variable-sized load on 8-bit-aligned "offset" based on the required bit size.

The bitfield "offset" obtained by getOffsetOf is not the start point of the whole bitfield but a point determined by the compiler that could be a middle point of that bitfield. Having 20 + 48 wouldn't necessarily cause d to overlap another "word" because of alignment and padding. Meanwhile, the "offset" obtained might just exceed the 20 bit part.

Copy link
Member

@Gnimuc Gnimuc Apr 6, 2025

Choose a reason for hiding this comment

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

@chengchingwen It looks like the old logic is for supporting the AIX layout.

https://github.com/llvm/llvm-project/blob/0defd832eb7c0618a67556e6fcbd32dd19e88b97/clang/lib/AST/RecordLayoutBuilder.cpp#L1618-L1639

https://github.com/llvm/llvm-project/blob/0defd832eb7c0618a67556e6fcbd32dd19e88b97/clang/lib/AST/RecordLayoutBuilder.cpp#L1460-L1528

I think we can add an option to let users select which layout they need.

The bitfield "offset" obtained by getOffsetOf is not the start point of the whole bitfield but a point determined by the compiler that could be a middle point of that bitfield. Having 20 + 48 wouldn't necessarily cause d to overlap another "word" because of alignment and padding. Meanwhile, the "offset" obtained might just exceed the 20 bit part.

As we use the value from Clang, could you give it check whether your current impl is compatible with Clang? https://github.com/llvm/llvm-project/blob/main/clang/lib/AST/RecordLayoutBuilder.cpp#L1534

@Gnimuc Gnimuc requested a review from Copilot April 2, 2025 08:18
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.

Files not reviewed (4)
  • src/generator/codegen.jl: Language not supported
  • test/bitfield/bitfield.c: Language not supported
  • test/bitfield/bitfield.h: Language not supported
  • test/test_bitfield.jl: Language not supported

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.

3 participants