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: remove generic and generic check from TypedAttribute #3504

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
16 changes: 1 addition & 15 deletions tests/irdl/test_attribute_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
IntegerType,
NoneAttr,
Signedness,
StringAttr,
)
from xdsl.ir import (
Attribute,
Expand Down Expand Up @@ -252,23 +251,10 @@ def test_typed_attribute():

@irdl_attr_definition
class TypedAttr( # pyright: ignore[reportUnusedClass]
TypedAttribute[Attribute]
TypedAttribute
):
name = "test.typed"

with pytest.raises(
Exception,
match="A TypedAttribute `type` parameter must be of the same type as the type variable in the TypedAttribute base class.",
):

@irdl_attr_definition
class TypedAttrBis( # pyright: ignore[reportUnusedClass]
TypedAttribute[IntegerAttr[IndexType]]
):
name = "test.typed"

type: ParameterDef[StringAttr]


################################################################################
# IntegerAttr
Expand Down
12 changes: 6 additions & 6 deletions xdsl/dialects/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class IndexType(ParametrizedAttribute):
@irdl_attr_definition
class IntegerAttr(
Generic[_IntegerAttrType],
TypedAttribute[_IntegerAttrType],
TypedAttribute,
):
name = "integer"
value: ParameterDef[IntAttr]
Expand Down Expand Up @@ -504,8 +504,8 @@ def verify(self) -> None:
@staticmethod
def parse_with_type(
parser: AttrParser,
type: AttributeInvT,
) -> TypedAttribute[AttributeInvT]:
type: Attribute,
) -> TypedAttribute:
assert isinstance(type, IntegerType | IndexType)
return IntegerAttr(parser.parse_integer(allow_boolean=(type == i1)), type)

Expand Down Expand Up @@ -634,7 +634,7 @@ def __hash__(self):


@irdl_attr_definition
class FloatAttr(Generic[_FloatAttrType], TypedAttribute[_FloatAttrType]):
class FloatAttr(Generic[_FloatAttrType], TypedAttribute):
name = "float"

value: ParameterDef[FloatData]
Expand Down Expand Up @@ -671,8 +671,8 @@ def __init__(
@staticmethod
def parse_with_type(
parser: AttrParser,
type: AttributeInvT,
) -> TypedAttribute[AttributeInvT]:
type: Attribute,
) -> TypedAttribute:
assert isinstance(type, AnyFloat)
return FloatAttr(parser.parse_float(), type)

Expand Down
6 changes: 3 additions & 3 deletions xdsl/ir/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ def _verify(self):
super()._verify()


class TypedAttribute(ParametrizedAttribute, Generic[AttributeCovT], ABC):
class TypedAttribute(ParametrizedAttribute, ABC):
"""
An attribute with a type.
"""
Expand All @@ -617,8 +617,8 @@ def get_type_index(cls) -> int: ...
@staticmethod
def parse_with_type(
parser: AttrParser,
type: AttributeInvT,
) -> TypedAttribute[AttributeInvT]:
type: Attribute,
) -> TypedAttribute:
"""
Parse the attribute with the given type.
"""
Expand Down
31 changes: 8 additions & 23 deletions xdsl/irdl/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from xdsl.ir import (
Attribute,
AttributeCovT,
AttributeInvT,
Data,
ParametrizedAttribute,
Expand All @@ -34,7 +33,6 @@
from xdsl.utils.hints import (
PropertyType,
get_type_var_from_generic_class,
get_type_var_mapping,
)
from xdsl.utils.runtime_final import runtime_final

Expand Down Expand Up @@ -160,25 +158,6 @@ def from_pyrdl(
name = clsdict["name"]

param_hints = irdl_param_attr_get_param_type_hints(pyrdl_def)
if issubclass(pyrdl_def, TypedAttribute):
pyrdl_def = cast(type[TypedAttribute[Attribute]], pyrdl_def)
try:
param_names = [name for name, _ in param_hints]
type_index = param_names.index("type")
except ValueError:
raise PyRDLAttrDefinitionError(
f"TypedAttribute {pyrdl_def.__name__} should have a 'type' parameter."
)
typed_hint = param_hints[type_index][1]
if get_origin(typed_hint) is Annotated:
typed_hint = get_args(typed_hint)[0]
type_var = get_type_var_mapping(pyrdl_def)[1][AttributeCovT]

if typed_hint != type_var:
raise ValueError(
"A TypedAttribute `type` parameter must be of the same type"
" as the type variable in the TypedAttribute base class."
)

parameters = list[tuple[str, AttrConstraint]]()
for param_name, param_type in param_hints:
Expand Down Expand Up @@ -233,8 +212,14 @@ def irdl_param_attr_definition(cls: _PAttrTT) -> _PAttrTT:
new_fields = get_accessors_from_param_attr_def(attr_def)

if issubclass(cls, TypedAttribute):
parameter_names: tuple[str] = tuple(zip(*attr_def.parameters))[0]
type_index = parameter_names.index("type")
type_indexes = tuple(
i for i, (p, _) in enumerate(attr_def.parameters) if p == "type"
)
if not type_indexes:
raise PyRDLAttrDefinitionError(
f"TypedAttribute {cls.__name__} should have a 'type' parameter."
)
type_index = type_indexes[0]
math-fehr marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def get_type_index(cls: Any) -> int:
Expand Down
Loading