-
Notifications
You must be signed in to change notification settings - Fork 140
[CIR][CodeGen] Updates GlobalViewAttr's indices computation for the union type #1584
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
base: main
Are you sure you want to change the base?
Conversation
I think I have a better solution and just update the index computation - so once you are good with it - I can rename the PR.
Both of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is much better, apologies for the delay! LGTM pending some nits
@@ -95,15 +95,17 @@ void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( | |||
unsigned AlignMask = Layout.getABITypeAlign(Elts[I]).value() - 1; | |||
if (RecordTy.getPacked()) | |||
AlignMask = 0; | |||
Pos = (Pos + AlignMask) & ~AlignMask; | |||
if (!RecordTy.isUnion()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some comment here so it's easy to understand why unions are different?
assert(Offset >= 0); | ||
if (Offset < Pos + EltSize) { | ||
Indices.push_back(I); | ||
SubType = Elts[I]; | ||
Offset -= Pos; | ||
break; | ||
} | ||
Pos += EltSize; | ||
if (!RecordTy.isUnion()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar here!
done! |
This PR introduces a new attribute for the explicit byte offset for
GlobalViewAttr
. It's a proposal, so feel free to reject it once it's not good from your point of view.The problem is (as usually) with globals and unions: looks like we can not really use indexes in the
GlobalView
to address an array of unions. For example, the next program prints4
now, but it should be42
:The problem is that we compute wrong indices in
CIRGenBuilder::computeGlobalViewIndicesFromFlatOffset
. Maybe it can be even fixed in this case, but I have a feeling that the fix would be a bit fragile.So instead of trying to support indexes for the array of unions I suggest to use the offset explicitly.
From the implementation point of view there are some changes in
CIRGenBuilder
- but nothing really new is in there - I just did not want to introduce copy-pasta for theisOffsetInUnion
function that is pretty the same as formercomputeGlobalViewIndicesFromFlatOffset
.