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: Add support for typed SSA values in isa #3988

Open
wants to merge 1 commit into
base: math-fehr/stack/2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion tests/test_is_satisfying_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
IntegerAttr,
IntegerType,
StringAttr,
i32,
)
from xdsl.ir import Attribute, ParametrizedAttribute
from xdsl.ir import Attribute, ParametrizedAttribute, SSAValue
from xdsl.irdl import BaseAttr, EqAttrConstraint, ParameterDef, irdl_attr_definition
from xdsl.utils.hints import isa
from xdsl.utils.isattr import isattr
from xdsl.utils.test_value import TestSSAValue


class Class1:
Expand Down Expand Up @@ -398,3 +400,16 @@ def test_isattr():
assert not isattr(IntAttr(1), BaseAttr(StringAttr))
assert isattr(IntAttr(1), EqAttrConstraint(IntAttr(1)))
assert not isattr(IntAttr(1), EqAttrConstraint(IntAttr(2)))


################################################################################
# SSAValue
################################################################################


def test_ssavalue():
a = TestSSAValue(i32)

assert isa(a, SSAValue)
assert isa(a, SSAValue[IntegerType])
assert not isa(a, SSAValue[StringAttr])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we have a test with a generic attribute like VectorType[IntegerType] or something.

13 changes: 12 additions & 1 deletion xdsl/utils/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
get_origin,
)

from xdsl.ir import ParametrizedAttribute
from xdsl.ir import ParametrizedAttribute, SSAValue
from xdsl.utils.exceptions import VerifyException

if TYPE_CHECKING:
Expand Down Expand Up @@ -102,6 +102,17 @@ def isa(arg: Any, hint: "TypeForm[_T]") -> TypeGuard[_T]:
except VerifyException:
return False

if origin is SSAValue:
if not isinstance(arg, SSAValue):
return False
arg = cast(SSAValue, arg)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to do isa(arg.type, get_args(hint)[0]) here?

constraint = irdl_to_attr_constraint(get_args(hint)[0])
try:
constraint.verify(arg.type, ConstraintContext())
return True
except VerifyException:
return False

raise ValueError(f"isa: unsupported type hint '{hint}' {get_origin(hint)}")


Expand Down
Loading