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

dialects: (builtin) make DenseIntOrFPElementsAttr generic on element type #3492

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alexarice
Copy link
Collaborator

Started trying to make DenseIntOrFPElementsAttr a TypedAttribute.
TypedAttributes force that the type parameter must appear as a type var generic. I am not sure why this is the case so it would be good to know what the original motivation for that was. To get this to go through I have disabled this check.

I then ended up adding a typevar for the elements to DenseIntOrFPElementsAttr which caused a lot of follow on changes and I am unsure if the result is nicer than what I started with. I think DenseIntOrFPElementsAttr can be made into a TypedAttribute without this type var.

I also imagine that extensive use of isattr is bad?

@alexarice alexarice marked this pull request as draft November 20, 2024 16:29
@alexarice alexarice self-assigned this Nov 20, 2024
@alexarice alexarice added the dialects Changes on the dialects label Nov 20, 2024
@alexarice alexarice changed the title [Draft] core: make DenseIntOrFPElementsAttr a TypedAttribute [Draft] dialects: (builtin) make DenseIntOrFPElementsAttr a TypedAttribute Nov 20, 2024
Copy link

codecov bot commented Nov 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.34%. Comparing base (bc5ea1e) to head (93a17b5).

Additional details and impacted files
@@                      Coverage Diff                      @@
##           alexarice/dense-typed-simple    #3492   +/-   ##
=============================================================
  Coverage                         90.34%   90.34%           
=============================================================
  Files                               464      464           
  Lines                             58048    58060   +12     
  Branches                           5549     5549           
=============================================================
+ Hits                              52442    52453   +11     
  Misses                             4179     4179           
- Partials                           1427     1428    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@alexarice alexarice marked this pull request as ready for review November 21, 2024 22:43
@alexarice
Copy link
Collaborator Author

It would be good to get any opinions on this. I'm tempted to see what the non-generic version would look like

Comment on lines +1708 to +1721
AnyDenseElement: TypeAlias = IntegerType | IndexType | AnyFloat
DenseElementT = TypeVar("DenseElementT", bound=AnyDenseElement, covariant=True)
_DenseElementT = TypeVar("_DenseElementT", bound=AnyDenseElement)
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 the equivalent of sized in MLIR? Would it make sense to add this independently of the other changes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I doubt it is as IndexType is not sized right? (Also IndexType should be forbidden from DenseIntOrFPElementsAttr but that's another matter)

Copy link
Member

Choose a reason for hiding this comment

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

I think it's quite relevant, and we should forbid it indeed, I thought we already made that change recently.

Copy link
Member

Choose a reason for hiding this comment

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

Ah it was DenseArrayBase #3258

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think in any case that should be a different PR (which could happen before or after this one)

printer.print_string(">")


DenseIntElementsAttr: TypeAlias = DenseIntOrFPElementsAttr[IntegerType]
Copy link
Member

Choose a reason for hiding this comment

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

This line feels like it could have its own PR, we could probably have a custom constraint to start with to minimise the diff

Copy link
Member

Choose a reason for hiding this comment

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

I still feel like this could be its own PR :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You want a separate PR just for the type alias?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that was the idea, it technically is its own API surface change and seems to be a large contribution to this PR's diff, independent of the other changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see how it makes any impact on the diff at all. Anything that is changed to a DenseIntElementAttr would have had to be changed to a DenseIntOrFPElementsAttr[IntegerType] or at least DenseIntOrFPElementsAttr[AnyDenseElement]

Copy link
Member

Choose a reason for hiding this comment

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

Sorry that's what I mean, this change in the diff of this PR could already be done in main directly, and is not dependent on the changes you make here. Or am I misunderstanding?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see how to separate the changes. I can't change DenseIntOrFPElementsAttr to DenseIntOrFPElementsAttr[AnyDenseElement] before this PR.

I could potentially do two steps of changing every DenseIntOrFPElementsAttr to DenseIntOrFPElementsAttr[AnyDenseElement] and then do a second PR specialising some of these to DenseIntElementsAttr = DenseIntOrFPElementsAttr[IntegerType], but I don't think the first diff will be any smaller.

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry that's not what I meant, I meant something like

Suggested change
DenseIntElementsAttr: TypeAlias = DenseIntOrFPElementsAttr[IntegerType]
DenseIntElementsAttr: TypeAlias = Annotated[DenseIntOrFPElementsAttr, ParametrizedAttrConstraint(Attribute, (BaseAttr(IntegerType),AnyAttr()))]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah I get it now, I could give that a go

Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

I like this overall, and agree that it doesn't necessarily make sense to restrict the type to have the generic parameter. It would be nice to minimise the big diff, if possible, as somme of the changes seem like more obvious improvements than others

@alexarice
Copy link
Collaborator Author

I think the first step is to make DenseIntOrFPElementsAttr a TypedAttribute[AnyDenseElement], without giving it a generic parameter. I think the changes should then be localised to the parsing changes and the removal of the generic check on TypedAttributes

@superlopuh
Copy link
Member

Yep that also crossed my mind, not sure whether there is a single best first step but it would be nice to have a smaller diff for sure.

@alexarice
Copy link
Collaborator Author

I'll split that out and see what it looks like

@alexarice alexarice changed the base branch from main to alexarice/dense-typed-simple November 22, 2024 17:42
@alexarice alexarice changed the title [Draft] dialects: (builtin) make DenseIntOrFPElementsAttr a TypedAttribute dialects: (builtin) make DenseIntOrFPElementsAttr generic on element type Nov 22, 2024
@alexarice
Copy link
Collaborator Author

Reworked and stacked this PR. Still not 100% sure this is desirable

alexarice added a commit that referenced this pull request Nov 22, 2024
…3509)

A simpler version of #3492 which does not make
`DenseIntOrFPElementsAttr` generic.
Base automatically changed from alexarice/dense-typed-simple to main November 22, 2024 23:53
@@ -85,7 +88,7 @@ def dense_int_or_fp_elements_value(
attr: Attribute,
type_attr: builtin.MemRefType[Any],
) -> ShapedArray[Any]:
assert isinstance(attr, builtin.DenseIntOrFPElementsAttr)
assert isattr(attr, base(builtin.DenseIntOrFPElementsAttr[AnyDenseElement]))
Copy link
Member

Choose a reason for hiding this comment

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

I would rather have a AnyDenseIntOrFPElementsAttrConstr TBH

Copy link
Member

Choose a reason for hiding this comment

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

To expand on this thought, it's because base does the type parsing every time this is called, whereas Constr caches that, and is a tiny bit more resilient to API changes in the future

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

Successfully merging this pull request may close these issues.

2 participants