Skip to content

Attribute

Attribute dataclass

Bases: ABC

A compile-time value. Attributes are used to represent SSA variable types, and can be attached on operations to give extra information.

Source code in xdsl/ir/core.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@dataclass(frozen=True)
class Attribute(ABC):
    """
    A compile-time value.
    Attributes are used to represent SSA variable types, and can be attached
    on operations to give extra information.
    """

    name: ClassVar[str] = field(init=False, repr=False)
    """The attribute name should be a static field in the attribute classes."""

    def __post_init__(self):
        self._verify()
        if not isinstance(self, Data | ParametrizedAttribute):
            raise TypeError("Attributes should only be Data or ParameterizedAttribute")

    def _verify(self):
        self.verify()

    def verify(self) -> None:
        """
        Check that the attribute parameters satisfy the expected invariants.
        Raise a VerifyException otherwise.
        """
        pass

    def __str__(self) -> str:
        from xdsl.printer import Printer

        res = StringIO()
        printer = Printer(stream=res)
        printer.print_attribute(self)
        return res.getvalue()

name: str = field(init=False, repr=False) class-attribute

The attribute name should be a static field in the attribute classes.

verify() -> None

Check that the attribute parameters satisfy the expected invariants. Raise a VerifyException otherwise.

Source code in xdsl/ir/core.py
103
104
105
106
107
108
def verify(self) -> None:
    """
    Check that the attribute parameters satisfy the expected invariants.
    Raise a VerifyException otherwise.
    """
    pass

Data dataclass

Bases: Generic[DataElement], Attribute, ABC

An attribute represented by a Python structure.

Source code in xdsl/ir/core.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
@dataclass(frozen=True)
class Data(Generic[DataElement], Attribute, ABC):
    """An attribute represented by a Python structure."""

    data: DataElement

    @classmethod
    def new(cls: type[Self], params: Any) -> Self:
        """
        Create a new `Data` given its parameter.

        The `params` argument should be of the same type as the `Data` generic
        argument.

        This function should be preferred over `__init__` when instantiating
        attributes in a generic way (i.e., without knowing their concrete type
        statically).
        """
        # Create the new attribute object, without calling its __init__.
        # We do this to allow users to redefine their own __init__.
        attr = cls.__new__(cls)

        # Call the __init__ of Data, which will set the parameters field.
        Data.__init__(attr, params)  # pyright: ignore[reportUnknownMemberType]
        return attr

    @classmethod
    @abstractmethod
    def parse_parameter(cls, parser: AttrParser) -> DataElement:
        """Parse the attribute parameter."""

    @abstractmethod
    def print_parameter(self, printer: Printer) -> None:
        """Print the attribute parameter."""

data: DataElement instance-attribute

print_parameter(printer: Printer) -> None abstractmethod

Print the attribute parameter.

Source code in xdsl/ir/core.py
190
191
192
@abstractmethod
def print_parameter(self, printer: Printer) -> None:
    """Print the attribute parameter."""

parse_parameter(parser: AttrParser) -> DataElement abstractmethod classmethod

Parse the attribute parameter.

Source code in xdsl/ir/core.py
185
186
187
188
@classmethod
@abstractmethod
def parse_parameter(cls, parser: AttrParser) -> DataElement:
    """Parse the attribute parameter."""

ParametrizedAttribute dataclass

Bases: Attribute

An attribute parametrized by other attributes.

Source code in xdsl/ir/core.py
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
@dataclass(frozen=True, init=False)
class ParametrizedAttribute(Attribute):
    """An attribute parametrized by other attributes."""

    parameters: tuple[Attribute, ...] = field()

    def __init__(self, parameters: Sequence[Attribute] = ()):
        object.__setattr__(self, "parameters", tuple(parameters))
        super().__init__()

    @classmethod
    def new(cls: type[Self], params: Sequence[Attribute]) -> Self:
        """
        Create a new `ParametrizedAttribute` given its parameters.

        This function should be preferred over `__init__` when instantiating
        attributes in a generic way (i.e., without knowing their concrete type
        statically).
        """
        # Create the new attribute object, without calling its __init__.
        # We do this to allow users to redefine their own __init__.
        attr = cls.__new__(cls)

        # Call the __init__ of ParametrizedAttribute, which will set the
        # parameters field.
        ParametrizedAttribute.__init__(attr, tuple(params))
        return attr

    @classmethod
    def parse_parameters(cls, parser: AttrParser) -> Sequence[Attribute]:
        """Parse the attribute parameters."""
        return parser.parse_paramattr_parameters()

    def print_parameters(self, printer: Printer) -> None:
        """Print the attribute parameters."""
        printer.print_paramattr_parameters(self.parameters)

    @classmethod
    def get_irdl_definition(cls) -> ParamAttrDef:
        """Get the IRDL attribute definition."""
        ...

    def _verify(self):
        # Verifier generated by irdl_attr_def
        t: type[ParametrizedAttribute] = type(self)
        attr_def = t.get_irdl_definition()
        attr_def.verify(self)
        super()._verify()

parameters: tuple[Attribute, ...] = field() class-attribute instance-attribute

print_parameters(printer: Printer) -> None

Print the attribute parameters.

Source code in xdsl/ir/core.py
412
413
414
def print_parameters(self, printer: Printer) -> None:
    """Print the attribute parameters."""
    printer.print_paramattr_parameters(self.parameters)

parse_parameters(parser: AttrParser) -> Sequence[Attribute] classmethod

Parse the attribute parameters.

Source code in xdsl/ir/core.py
407
408
409
410
@classmethod
def parse_parameters(cls, parser: AttrParser) -> Sequence[Attribute]:
    """Parse the attribute parameters."""
    return parser.parse_paramattr_parameters()

get_irdl_definition() -> ParamAttrDef classmethod

Get the IRDL attribute definition.

Source code in xdsl/ir/core.py
416
417
418
419
@classmethod
def get_irdl_definition(cls) -> ParamAttrDef:
    """Get the IRDL attribute definition."""
    ...

TypedAttribute dataclass

Bases: ParametrizedAttribute, ABC

An attribute with a type.

Source code in xdsl/ir/core.py
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
class TypedAttribute(ParametrizedAttribute, ABC):
    """
    An attribute with a type.
    """

    @classmethod
    def get_type_index(cls) -> int: ...

    def get_type(self) -> Attribute:
        return self.parameters[self.get_type_index()]

    @staticmethod
    def parse_with_type(
        parser: AttrParser,
        type: Attribute,
    ) -> TypedAttribute:
        """
        Parse the attribute with the given type.
        """
        ...

    @abstractmethod
    def print_without_type(self, printer: Printer): ...