-
Notifications
You must be signed in to change notification settings - Fork 140
[CIR][CodeGen] Introduce bytes offset attribute #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
clang/test/CIR/CodeGen/union-array.c
Outdated
|
||
static U2 g1[3] = {{0x42},{0x42},{0x42}}; | ||
int* g2 = &g1[1].f1.s1; | ||
// CIR: cir.global external @g2 = #cir.global_view<@g1, offset 24> : !cir.ptr<!s32i> |
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.
Shouldn't this test plain LLVM
as well?
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.
I just wanted to show how the original LLVM looks like
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.
OG is fine, just asking for the extra one too
// ByteOffsetAttr | ||
//===----------------------------------------------------------------------===// | ||
|
||
def ByteOffsetAttr : CIR_Attr<"ByteOffset", "byte_offset"> { |
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.
Instead of adding ByteOffsetAttr
, shouldn't we just use mlir::IntegerAttr directly?
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.
we can! I just thought it would be more explicit
An integer attribute is a literal attribute that represents an integral | ||
value of the specified integer type. | ||
}]; | ||
let parameters = (ins "uint64_t":$value); |
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.
Could the offset somehow be negative?
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.
I think it can not be negative
@@ -772,21 +789,44 @@ def GlobalViewAttr : CIR_Attr<"GlobalView", "global_view", [TypedAttrInterface]> | |||
|
|||
let parameters = (ins AttributeSelfTypeParameter<"">:$type, | |||
"mlir::FlatSymbolRefAttr":$symbol, | |||
OptionalParameter<"mlir::ArrayAttr">:$indices); | |||
OptionalParameter<"mlir::ArrayAttr">:$indices, | |||
OptionalParameter<"ByteOffsetAttr">:$offset |
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 both indices
and offset
be used at the same time? Having two options is confusing, how would they relate to each other or how a user would pick up the right one to use?
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.
I see now that in the verifier you blocked this case, can you add some doc here to explain when is one supposed to be used versus the other? That might help me think more about the design.
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.
I'll also add that it might be better to (a) have only one optional attribute of the Attribute
type, (b) make the verifier distinguish if they come from valid attributes with isa<>
, (c) explain and give examples in the doc, and (d) add builder helpers.
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.
well, that's why I added a verifier for the GlobalViewAttr
and check that they can't be used together.
The question about a user is a good one. I would say that indices should be used by default, and byte offset in the very rare cases like this one. But in the same time it implies that careful user should check both every time. So may be it's not a good idea after all, what do you think?
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 |
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
.