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 BaseAttr inferrable #3491

Merged
merged 1 commit into from
Nov 20, 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
34 changes: 33 additions & 1 deletion tests/irdl/test_attr_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from xdsl.dialects.builtin import StringAttr
from xdsl.ir import Attribute, ParametrizedAttribute
from xdsl.ir import Attribute, Data, ParametrizedAttribute
from xdsl.irdl import (
AllOf,
AnyAttr,
Expand Down Expand Up @@ -105,3 +105,35 @@ class WrapAttr(BaseWrapAttr): ...
),
)
assert not base_constr.can_infer(set())


def test_base_attr_constraint_inference():
class BaseNoParamAttr(ParametrizedAttribute):
name = "no_param"

@irdl_attr_definition
class WithParamAttr(ParametrizedAttribute):
name = "with_param"

inner: ParameterDef[Attribute]

@irdl_attr_definition
class DataAttr(Data[int]):
name = "data"

@irdl_attr_definition
class NoParamAttr(BaseNoParamAttr): ...

constr = BaseAttr(NoParamAttr)

assert constr.can_infer(set())
assert constr.infer({}) == NoParamAttr()

base_constr = BaseAttr(BaseNoParamAttr)
assert not base_constr.can_infer(set())

with_param_constr = BaseAttr(WithParamAttr)
assert not with_param_constr.can_infer(set())

data_constr = BaseAttr(DataAttr)
assert not data_constr.can_infer(set())
12 changes: 12 additions & 0 deletions xdsl/irdl/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ def verify(
f"{attr} should be of base attribute {self.attr.name}"
)

def can_infer(self, var_constraint_names: Set[str]) -> bool:
return (
is_runtime_final(self.attr)
and issubclass(self.attr, ParametrizedAttribute)
and not self.attr.get_irdl_definition().parameters
)

def infer(self, variables: dict[str, ConstraintVariableType]) -> AttributeCovT:
assert issubclass(self.attr, ParametrizedAttribute)
attr = self.attr.new(())
return attr

def get_unique_base(self) -> type[Attribute] | None:
if is_runtime_final(self.attr):
return self.attr
Expand Down
Loading