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

api: use constraints to carry type information in pyrdl #2890

Merged
merged 11 commits into from
Jul 16, 2024

Conversation

superlopuh
Copy link
Member

@superlopuh superlopuh commented Jul 16, 2024

PR 2/2 to make the Pylance errors in operation definitions go away.

The proposal is to introduce a constr mentod to construct a constraint given a PyRDL type, add | and & operators on constraints to combine them, and to require a constraint instead of a type for operand, result, attribute, and property definitions.

Pylance errors: 317 (-172)

@superlopuh superlopuh added the API Related to changes regarding API of constructs label Jul 16, 2024
@superlopuh superlopuh self-assigned this Jul 16, 2024
Copy link

codecov bot commented Jul 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.90%. Comparing base (ff314f8) to head (b6f25d8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2890      +/-   ##
==========================================
- Coverage   89.93%   89.90%   -0.03%     
==========================================
  Files         402      403       +1     
  Lines       50386    50413      +27     
  Branches     7786     7787       +1     
==========================================
+ Hits        45313    45324      +11     
- Misses       3847     3862      +15     
- Partials     1226     1227       +1     

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

Copy link
Collaborator

@AntonLydike AntonLydike left a comment

Choose a reason for hiding this comment

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

Can we get some documentation on what constr is, how we should use it, and why we need it?

Otherwise GTG

Comment on lines 437 to 440
def constr(
irdl: GenericAttrConstraint[AttributeInvT] | AttributeInvT | type[AttributeInvT],
) -> GenericAttrConstraint[AttributeInvT]:
return cast(GenericAttrConstraint[AttributeInvT], irdl_to_attr_constraint(irdl))
Copy link
Collaborator

Choose a reason for hiding this comment

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

can I has docstring to explain why it's necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added docstrings

Copy link
Collaborator

@math-fehr math-fehr left a comment

Choose a reason for hiding this comment

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

Looks good to me, I think this is going to solve our typing issue.
I'm just sad we won't have operand_def(i32) anymore (and I understand why we could but don't want to allow this).

I think that we should also probably rename constr into is and eq? Just so this is much clearer to the user?

And also, thanks a lot for taking care of it 🦄 !

def constr(
irdl: GenericAttrConstraint[AttributeInvT] | AttributeInvT | type[AttributeInvT],
) -> GenericAttrConstraint[AttributeInvT]:
return cast(GenericAttrConstraint[AttributeInvT], irdl_to_attr_constraint(irdl))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this a temporary thing? My guess is that irdl_to_attr_constraint should have the exact same signature at some point right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I'll look into how bad things would get if I were to give it this signature now, maybe it won't be that big a diff.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK it's a necessary temporary thing, at least until the things that irdl_to_attr_constraint are a little more strongly typed than just being Any, which is the current case in a few places it's called.


TensorTypeF64: TypeAlias = TensorType[Float64Type]
UnrankedTensorTypeF64: TypeAlias = UnrankedTensorType[Float64Type]
AnyTensorTypeF64: TypeAlias = TensorTypeF64 | UnrankedTensorTypeF64
AnyTensorTypeF64Constr = constr(TensorTypeF64) | constr(UnrankedTensorTypeF64)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not a big fan of having to not allow things like operand_def(TensorType[Float64Type]).
I feel that allowing operand_def(TensorType[Float64Type]) would be quite useful? It should be possible with just an overload on operand_def.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it is allowed right now, it's the union that isn't, this is what this line provides.

Comment on lines 75 to 77
def __or__(
self, value: GenericAttrConstraint[AttributeInvT], /
) -> AnyOf[AttributeCovT | AttributeInvT]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def __or__(
self, value: GenericAttrConstraint[AttributeInvT], /
) -> AnyOf[AttributeCovT | AttributeInvT]:
def __or__(
self, value: GenericAttrConstraint[AttributeCovT2], /
) -> AnyOf[AttributeCovT | AttributeCovT2]:

Both arguments should be covariant here I believe

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll give this a go

xdsl/irdl/test.py Outdated Show resolved Hide resolved
Base automatically changed from sasha/irdl/typed-constraints/init to main July 16, 2024 21:57
@superlopuh superlopuh merged commit e8d53cf into main Jul 16, 2024
9 checks passed
@superlopuh superlopuh deleted the sasha/irdl/typed-constraints/init2 branch July 16, 2024 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Related to changes regarding API of constructs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants