Skip to content

Commit

Permalink
core: Add support for typed SSA values in isa
Browse files Browse the repository at this point in the history
`isa(arg, SSAValue[T])` will check that `arg` is an SSA value, and that
its type is `T`.

stack-info: PR: #3988, branch: math-fehr/stack/3
  • Loading branch information
math-fehr committed Feb 10, 2025
1 parent 1f5d2be commit d825f9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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])
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)
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

0 comments on commit d825f9f

Please sign in to comment.