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: (irdl) ParamAttrConstraint can infer recursively #3477

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions tests/irdl/test_attr_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import pytest

from xdsl.dialects.builtin import StringAttr
from xdsl.ir import Attribute, ParametrizedAttribute
from xdsl.irdl import (
AllOf,
AnyAttr,
AttrConstraint,
BaseAttr,
ConstraintContext,
EqAttrConstraint,
ParamAttrConstraint,
ParameterDef,
VarConstraint,
eq,
irdl_attr_definition,
)

Expand Down Expand Up @@ -54,3 +57,40 @@ def test_attr_constraint_get_unique_base(
constraint: AttrConstraint, expected: type[Attribute] | None
):
assert constraint.get_unique_base() == expected


def test_param_attr_constraint_inference():
@irdl_attr_definition
class WrapAttr(ParametrizedAttribute):
name = "wrap"

inner: ParameterDef[Attribute]

constr = ParamAttrConstraint(
WrapAttr,
(
eq(
StringAttr("Hello"),
),
),
)

assert constr.can_infer(set())
assert constr.infer(ConstraintContext()) == WrapAttr((StringAttr("Hello"),))

var_constr = ParamAttrConstraint(
WrapAttr,
(
VarConstraint(
"T",
eq(
StringAttr("Hello"),
),
),
),
)

assert var_constr.can_infer({"T"})
assert var_constr.infer(ConstraintContext({"T": StringAttr("Hello")})) == WrapAttr(
(StringAttr("Hello"),)
)
10 changes: 10 additions & 0 deletions xdsl/irdl/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,16 @@ def verify(
for idx, param_constr in enumerate(self.param_constrs):
param_constr.verify(attr.parameters[idx], constraint_context)

def can_infer(self, constraint_names: set[str]) -> bool:
return all(constr.can_infer(constraint_names) for constr in self.param_constrs)

def infer(self, constraint_context: ConstraintContext) -> Attribute:
params = tuple(
constr.infer(constraint_context) for constr in self.param_constrs
)
attr = self.base_attr.new(params)
return attr

def get_resolved_variables(self) -> set[str]:
if not self.param_constrs:
return set()
Expand Down
Loading