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

core: Make SSAValue generic #3987

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

core: Make SSAValue generic #3987

wants to merge 1 commit into from

Conversation

math-fehr
Copy link
Collaborator

@math-fehr math-fehr commented Feb 28, 2025

Adds a generic parameter to SSAValue that constrains the type associated
with the SSAValue.
This is useful whenever a function only accepts SSAValue of a given type.

stack-info: PR: #3987, branch: math-fehr/stack/2
Copy link

codecov bot commented Feb 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.99%. Comparing base (85e62b9) to head (1c59e49).

Additional details and impacted files
@@                  Coverage Diff                  @@
##           math-fehr/stack/1    #3987      +/-   ##
=====================================================
- Coverage              90.16%   89.99%   -0.18%     
=====================================================
  Files                    458      466       +8     
  Lines                  58291    59260     +969     
  Branches                5691     5800     +109     
=====================================================
+ Hits                   52559    53330     +771     
- Misses                  4340     4500     +160     
- Partials                1392     1430      +38     

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

Copy link
Collaborator

@alexarice alexarice left a comment

Choose a reason for hiding this comment

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

AttributeCovT has a default value of Attribute right?

@@ -119,6 +119,7 @@ def print(self, *argv: Any) -> None:
self.print_string(arg)
continue
if isinstance(arg, SSAValue):
arg = cast(SSAValue[Attribute], arg)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is yucky but tbf this whole function is yucky

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, the problem is that isinstance(arg, SSAValue) returns an SSAValue[Unknown], and we cannot use isa in this file if I recall

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 sure I fully grasped why pyright can't realise the type is SSAValue[Attribute] here using the lower bound on the type variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

microsoft/pyright#3891

It could work if the field is frozen, but would require too many changes in pyright AFAIU

def foo(arg: SSAValue[IntegerType]):
pass

foo(value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there any way we can make a test that shows a type error? I guess not as it would have to rely on pyright

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I'm discussing it with Sasha, we are trying to find a solution

@@ -478,13 +478,13 @@ def remove_use(self, use: Use):


@dataclass(eq=False)
class SSAValue(IRWithUses, ABC):
class SSAValue(Generic[AttributeCovT], IRWithUses, ABC):
Copy link
Member

Choose a reason for hiding this comment

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

My feeling is that this is unsound unless we make SSAValue immutable first, which blocks most of this PR stack.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Are there places where we modify the SSAValue type in place?

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 am currently trying to see how easy it is to make this immutable, I'll probably add 1-2 commits in the stack

Base automatically changed from math-fehr/stack/1 to main February 28, 2025 17:05
math-fehr added a commit that referenced this pull request Feb 28, 2025
Adds a generic parameter to SSAValue that constrains the type associated
with the SSAValue.
This is useful whenever a function only accepts SSAValue of a given type.

stack-info: PR: #3987, branch: math-fehr/stack/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core xDSL core (ir, textual format, ...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants