Skip to content

List dialect

list_dialect

LIST_DIALECT = Dialect('list', [LengthOp, MapOp, PrintOp, RangeOp, YieldOp], [ListType]) module-attribute

ListType dataclass

Bases: ParametrizedAttribute, TypeAttribute

Source code in xdsl/frontend/listlang/list_dialect.py
28
29
30
31
@irdl_attr_definition
class ListType(ParametrizedAttribute, TypeAttribute):
    name = "list.list"
    elem_type: builtin.IntegerType

name = 'list.list' class-attribute instance-attribute

elem_type: builtin.IntegerType instance-attribute

LengthOp

Bases: IRDLOperation

Source code in xdsl/frontend/listlang/list_dialect.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@irdl_op_definition
class LengthOp(IRDLOperation):
    name = "list.length"

    li = operand_def(ListType)
    result = result_def(builtin.i32)

    def __init__(self, li: SSAValue):
        super().__init__(
            operands=[li],
            result_types=[builtin.i32],
        )

    assembly_format = "$li attr-dict `:` type($li) `->` type($result)"

name = 'list.length' class-attribute instance-attribute

li = operand_def(ListType) class-attribute instance-attribute

result = result_def(builtin.i32) class-attribute instance-attribute

assembly_format = '$li attr-dict `:` type($li) `->` type($result)' class-attribute instance-attribute

__init__(li: SSAValue)

Source code in xdsl/frontend/listlang/list_dialect.py
41
42
43
44
45
def __init__(self, li: SSAValue):
    super().__init__(
        operands=[li],
        result_types=[builtin.i32],
    )

MapOp

Bases: IRDLOperation

Source code in xdsl/frontend/listlang/list_dialect.py
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 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
117
118
119
120
121
122
123
124
125
@irdl_op_definition
class MapOp(IRDLOperation):
    name = "list.map"

    li = operand_def(ListType)
    body = region_def("single_block")
    result = result_def(ListType)

    def __init__(
        self,
        li: SSAValue,
        body: Region,
        result_element_type: builtin.IntegerType,
    ):
        super().__init__(
            operands=[li],
            regions=[body],
            result_types=[ListType(result_element_type)],
        )

    def print(self, printer: Printer):
        printer.print_string(" ")
        printer.print_operand(self.li)
        printer.print_string(" with (")
        printer.print_block_argument(self.body.block.args[0])
        printer.print_string(") -> ")
        printer.print_attribute(self.result.type.elem_type)
        printer.print_string(" ")
        printer.print_region(self.body, print_entry_block_args=False)
        printer.print_op_attributes(self.attributes)

    @classmethod
    def parse(cls, parser: Parser) -> "MapOp":
        unresolved_li = parser.parse_unresolved_operand("Expected list operand")
        parser.parse_keyword("with")
        parser.parse_punctuation("(")
        map_argument = parser.parse_argument()
        parser.parse_punctuation(")")
        parser.parse_punctuation("->")
        result_type = parser.parse_type()
        body = parser.parse_region((map_argument,))
        attr_dict = parser.parse_optional_attr_dict()

        if not isa(map_argument.type, builtin.IntegerType):
            parser.raise_error("input element must be an integer")
        if not isa(result_type, builtin.IntegerType):
            parser.raise_error("result element must be an integer")

        li = parser.resolve_operand(unresolved_li, ListType(map_argument.type))

        op = cls(li, body, result_type)
        op.attributes = attr_dict

        return op

    def verify_(self):
        if len(self.body.block.arg_types) != 1:
            raise VerifyException("body must have exactly one argument")

        input_list_elem_type = cast(ListType, self.li.type).elem_type
        if self.body.block.arg_types[0] != input_list_elem_type:
            raise VerifyException(
                f"argument type ({self.body.block.arg_types[0]}) does not "
                f"match element type of input list ({input_list_elem_type})"
            )

        last_op = self.body.block.last_op
        if last_op is None or not isa(last_op, YieldOp):
            raise VerifyException("missing yield terminator in body region")

        result_list_elem_type = self.result.type.elem_type
        if last_op.yielded.type != result_list_elem_type:
            raise VerifyException(
                f"yielded type ({last_op.yielded.type}) does not match "
                f"element type of result list ({result_list_elem_type})"
            )

name = 'list.map' class-attribute instance-attribute

li = operand_def(ListType) class-attribute instance-attribute

body = region_def('single_block') class-attribute instance-attribute

result = result_def(ListType) class-attribute instance-attribute

__init__(li: SSAValue, body: Region, result_element_type: builtin.IntegerType)

Source code in xdsl/frontend/listlang/list_dialect.py
58
59
60
61
62
63
64
65
66
67
68
def __init__(
    self,
    li: SSAValue,
    body: Region,
    result_element_type: builtin.IntegerType,
):
    super().__init__(
        operands=[li],
        regions=[body],
        result_types=[ListType(result_element_type)],
    )

print(printer: Printer)

Source code in xdsl/frontend/listlang/list_dialect.py
70
71
72
73
74
75
76
77
78
79
def print(self, printer: Printer):
    printer.print_string(" ")
    printer.print_operand(self.li)
    printer.print_string(" with (")
    printer.print_block_argument(self.body.block.args[0])
    printer.print_string(") -> ")
    printer.print_attribute(self.result.type.elem_type)
    printer.print_string(" ")
    printer.print_region(self.body, print_entry_block_args=False)
    printer.print_op_attributes(self.attributes)

parse(parser: Parser) -> MapOp classmethod

Source code in xdsl/frontend/listlang/list_dialect.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
@classmethod
def parse(cls, parser: Parser) -> "MapOp":
    unresolved_li = parser.parse_unresolved_operand("Expected list operand")
    parser.parse_keyword("with")
    parser.parse_punctuation("(")
    map_argument = parser.parse_argument()
    parser.parse_punctuation(")")
    parser.parse_punctuation("->")
    result_type = parser.parse_type()
    body = parser.parse_region((map_argument,))
    attr_dict = parser.parse_optional_attr_dict()

    if not isa(map_argument.type, builtin.IntegerType):
        parser.raise_error("input element must be an integer")
    if not isa(result_type, builtin.IntegerType):
        parser.raise_error("result element must be an integer")

    li = parser.resolve_operand(unresolved_li, ListType(map_argument.type))

    op = cls(li, body, result_type)
    op.attributes = attr_dict

    return op

verify_()

Source code in xdsl/frontend/listlang/list_dialect.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def verify_(self):
    if len(self.body.block.arg_types) != 1:
        raise VerifyException("body must have exactly one argument")

    input_list_elem_type = cast(ListType, self.li.type).elem_type
    if self.body.block.arg_types[0] != input_list_elem_type:
        raise VerifyException(
            f"argument type ({self.body.block.arg_types[0]}) does not "
            f"match element type of input list ({input_list_elem_type})"
        )

    last_op = self.body.block.last_op
    if last_op is None or not isa(last_op, YieldOp):
        raise VerifyException("missing yield terminator in body region")

    result_list_elem_type = self.result.type.elem_type
    if last_op.yielded.type != result_list_elem_type:
        raise VerifyException(
            f"yielded type ({last_op.yielded.type}) does not match "
            f"element type of result list ({result_list_elem_type})"
        )

PrintOp

Bases: IRDLOperation

Source code in xdsl/frontend/listlang/list_dialect.py
128
129
130
131
132
133
134
135
136
137
138
139
@irdl_op_definition
class PrintOp(IRDLOperation):
    name = "list.print"

    li = operand_def(ListType)

    def __init__(self, li: SSAValue):
        super().__init__(
            operands=[li],
        )

    assembly_format = "$li attr-dict `:` type($li)"

name = 'list.print' class-attribute instance-attribute

li = operand_def(ListType) class-attribute instance-attribute

assembly_format = '$li attr-dict `:` type($li)' class-attribute instance-attribute

__init__(li: SSAValue)

Source code in xdsl/frontend/listlang/list_dialect.py
134
135
136
137
def __init__(self, li: SSAValue):
    super().__init__(
        operands=[li],
    )

RangeOp

Bases: IRDLOperation

Source code in xdsl/frontend/listlang/list_dialect.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
@irdl_op_definition
class RangeOp(IRDLOperation):
    name = "list.range"

    lower = operand_def(builtin.i32)
    upper = operand_def(builtin.i32)
    result = result_def(ListType)

    def __init__(self, lower: SSAValue, upper: SSAValue, result_type: ListType):
        super().__init__(
            operands=[lower, upper],
            result_types=[result_type],
        )

    assembly_format = "$lower `to` $upper attr-dict `:` type($result)"

name = 'list.range' class-attribute instance-attribute

lower = operand_def(builtin.i32) class-attribute instance-attribute

upper = operand_def(builtin.i32) class-attribute instance-attribute

result = result_def(ListType) class-attribute instance-attribute

assembly_format = '$lower `to` $upper attr-dict `:` type($result)' class-attribute instance-attribute

__init__(lower: SSAValue, upper: SSAValue, result_type: ListType)

Source code in xdsl/frontend/listlang/list_dialect.py
150
151
152
153
154
def __init__(self, lower: SSAValue, upper: SSAValue, result_type: ListType):
    super().__init__(
        operands=[lower, upper],
        result_types=[result_type],
    )

YieldOp

Bases: IRDLOperation

Source code in xdsl/frontend/listlang/list_dialect.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
@irdl_op_definition
class YieldOp(IRDLOperation):
    name = "list.yield"

    yielded = operand_def(AnyOf([builtin.IntegerType, ListType]))

    traits = lazy_traits_def(
        lambda: (
            IsTerminator(),
            HasParent(MapOp),
        )
    )

    def __init__(self, yielded: SSAValue):
        super().__init__(
            operands=[yielded],
        )

    assembly_format = "$yielded attr-dict `:` type($yielded)"

name = 'list.yield' class-attribute instance-attribute

yielded = operand_def(AnyOf([builtin.IntegerType, ListType])) class-attribute instance-attribute

traits = lazy_traits_def(lambda: (IsTerminator(), HasParent(MapOp))) class-attribute instance-attribute

assembly_format = '$yielded attr-dict `:` type($yielded)' class-attribute instance-attribute

__init__(yielded: SSAValue)

Source code in xdsl/frontend/listlang/list_dialect.py
172
173
174
175
def __init__(self, yielded: SSAValue):
    super().__init__(
        operands=[yielded],
    )