Skip to content

Lowerings

lowerings

LowerLengthOp

Bases: RewritePattern

Source code in xdsl/frontend/listlang/lowerings.py
34
35
36
37
38
39
40
41
42
43
class LowerLengthOp(RewritePattern):
    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: list_dialect.LengthOp, rewriter: PatternRewriter):
        zero_index = rewriter.insert_op(
            arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
        )
        dim = rewriter.insert_op(tensor.DimOp(op.li, zero_index))
        cast = arith.IndexCastOp(dim, builtin.i32)
        cast.result.name_hint = op.result.name_hint
        rewriter.replace_op(op, cast)

match_and_rewrite(op: list_dialect.LengthOp, rewriter: PatternRewriter)

Source code in xdsl/frontend/listlang/lowerings.py
35
36
37
38
39
40
41
42
43
@op_type_rewrite_pattern
def match_and_rewrite(self, op: list_dialect.LengthOp, rewriter: PatternRewriter):
    zero_index = rewriter.insert_op(
        arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
    )
    dim = rewriter.insert_op(tensor.DimOp(op.li, zero_index))
    cast = arith.IndexCastOp(dim, builtin.i32)
    cast.result.name_hint = op.result.name_hint
    rewriter.replace_op(op, cast)

LowerMapOp

Bases: RewritePattern

Source code in xdsl/frontend/listlang/lowerings.py
 46
 47
 48
 49
 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
class LowerMapOp(RewritePattern):
    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: list_dialect.MapOp, rewriter: PatternRewriter):
        zero = rewriter.insert_op(
            arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
        )
        one = rewriter.insert_op(
            arith.ConstantOp(builtin.IntegerAttr(1, builtin.IndexType()))
        )
        list_len = rewriter.insert_op(tensor.DimOp(op.li, zero))

        tensor_type = _list_type_to_tensor(op.li.type)
        result_tensor_type = _list_type_to_tensor(op.result.type)

        result_uninit = rewriter.insert_op(
            tensor.EmptyOp([list_len.result], result_tensor_type)
        )
        result_uninit.results[0].name_hint = _name_hint_ext(
            op.result.name_hint, "uninit"
        )

        rewriter_ip = rewriter.insertion_point

        for_body = Block(
            [],
            arg_types=(builtin.IndexType(), result_tensor_type),
        )
        ind_var = for_body.args[0]
        ind_var.name_hint = "_i"
        tensor_arg = for_body.args[1]

        rewriter.insertion_point = InsertPoint.at_start(for_body)

        x = rewriter.insert_op(
            tensor.ExtractOp(op.li, [ind_var], tensor_type.element_type)
        )
        x.result.name_hint = op.body.block.args[0].name_hint

        rewriter.inline_block(op.body.block, InsertPoint.at_end(for_body), (x.result,))

        closure_yield = for_body.last_op
        assert isa(closure_yield, list_dialect.YieldOp)
        result_scalar = closure_yield.yielded
        rewriter.erase_op(closure_yield)

        result = rewriter.insert_op(
            tensor.InsertOp(result_scalar, tensor_arg, [ind_var])
        )

        rewriter.insert_op(scf.YieldOp(result.result))

        rewriter.insertion_point = rewriter_ip

        for_op = scf.ForOp(
            zero,
            list_len,
            one,
            result_uninit.results,
            for_body,
        )
        for_op.results[0].name_hint = op.result.name_hint
        rewriter.replace_op(op, for_op)

match_and_rewrite(op: list_dialect.MapOp, rewriter: PatternRewriter)

Source code in xdsl/frontend/listlang/lowerings.py
 47
 48
 49
 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
@op_type_rewrite_pattern
def match_and_rewrite(self, op: list_dialect.MapOp, rewriter: PatternRewriter):
    zero = rewriter.insert_op(
        arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
    )
    one = rewriter.insert_op(
        arith.ConstantOp(builtin.IntegerAttr(1, builtin.IndexType()))
    )
    list_len = rewriter.insert_op(tensor.DimOp(op.li, zero))

    tensor_type = _list_type_to_tensor(op.li.type)
    result_tensor_type = _list_type_to_tensor(op.result.type)

    result_uninit = rewriter.insert_op(
        tensor.EmptyOp([list_len.result], result_tensor_type)
    )
    result_uninit.results[0].name_hint = _name_hint_ext(
        op.result.name_hint, "uninit"
    )

    rewriter_ip = rewriter.insertion_point

    for_body = Block(
        [],
        arg_types=(builtin.IndexType(), result_tensor_type),
    )
    ind_var = for_body.args[0]
    ind_var.name_hint = "_i"
    tensor_arg = for_body.args[1]

    rewriter.insertion_point = InsertPoint.at_start(for_body)

    x = rewriter.insert_op(
        tensor.ExtractOp(op.li, [ind_var], tensor_type.element_type)
    )
    x.result.name_hint = op.body.block.args[0].name_hint

    rewriter.inline_block(op.body.block, InsertPoint.at_end(for_body), (x.result,))

    closure_yield = for_body.last_op
    assert isa(closure_yield, list_dialect.YieldOp)
    result_scalar = closure_yield.yielded
    rewriter.erase_op(closure_yield)

    result = rewriter.insert_op(
        tensor.InsertOp(result_scalar, tensor_arg, [ind_var])
    )

    rewriter.insert_op(scf.YieldOp(result.result))

    rewriter.insertion_point = rewriter_ip

    for_op = scf.ForOp(
        zero,
        list_len,
        one,
        result_uninit.results,
        for_body,
    )
    for_op.results[0].name_hint = op.result.name_hint
    rewriter.replace_op(op, for_op)

LowerPrintOp

Bases: RewritePattern

Source code in xdsl/frontend/listlang/lowerings.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
class LowerPrintOp(RewritePattern):
    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: list_dialect.PrintOp, rewriter: PatternRewriter):
        zero = rewriter.insert_op(
            arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
        )
        one = rewriter.insert_op(
            arith.ConstantOp(builtin.IntegerAttr(1, builtin.IndexType()))
        )
        list_len = rewriter.insert_op(tensor.DimOp(op.li, zero))

        tensor_type = _list_type_to_tensor(op.li.type)

        rewriter_ip = rewriter.insertion_point

        for_body = Block([], arg_types=[builtin.IndexType()])
        ind_var = for_body.args[0]
        ind_var.name_hint = "_i"

        rewriter.insertion_point = InsertPoint.at_start(for_body)

        scalar = rewriter.insert_op(
            tensor.ExtractOp(op.li, [ind_var], tensor_type.element_type)
        )
        ListLangType.from_xdsl(scalar.result.type).print(rewriter, scalar.result)
        rewriter.insert_op(printf.PrintFormatOp(","))
        rewriter.insert_op(scf.YieldOp())

        rewriter.insertion_point = rewriter_ip

        rewriter.replace_op(
            op,
            (
                printf.PrintFormatOp("["),
                scf.ForOp(
                    zero,
                    list_len,
                    one,
                    [],
                    for_body,
                ),
                printf.PrintFormatOp("]"),
            ),
        )

match_and_rewrite(op: list_dialect.PrintOp, rewriter: PatternRewriter)

Source code in xdsl/frontend/listlang/lowerings.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@op_type_rewrite_pattern
def match_and_rewrite(self, op: list_dialect.PrintOp, rewriter: PatternRewriter):
    zero = rewriter.insert_op(
        arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
    )
    one = rewriter.insert_op(
        arith.ConstantOp(builtin.IntegerAttr(1, builtin.IndexType()))
    )
    list_len = rewriter.insert_op(tensor.DimOp(op.li, zero))

    tensor_type = _list_type_to_tensor(op.li.type)

    rewriter_ip = rewriter.insertion_point

    for_body = Block([], arg_types=[builtin.IndexType()])
    ind_var = for_body.args[0]
    ind_var.name_hint = "_i"

    rewriter.insertion_point = InsertPoint.at_start(for_body)

    scalar = rewriter.insert_op(
        tensor.ExtractOp(op.li, [ind_var], tensor_type.element_type)
    )
    ListLangType.from_xdsl(scalar.result.type).print(rewriter, scalar.result)
    rewriter.insert_op(printf.PrintFormatOp(","))
    rewriter.insert_op(scf.YieldOp())

    rewriter.insertion_point = rewriter_ip

    rewriter.replace_op(
        op,
        (
            printf.PrintFormatOp("["),
            scf.ForOp(
                zero,
                list_len,
                one,
                [],
                for_body,
            ),
            printf.PrintFormatOp("]"),
        ),
    )

LowerRangeOp

Bases: RewritePattern

Source code in xdsl/frontend/listlang/lowerings.py
156
157
158
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class LowerRangeOp(RewritePattern):
    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: list_dialect.RangeOp, rewriter: PatternRewriter):
        zero = rewriter.insert_op(
            arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
        )
        one = rewriter.insert_op(
            arith.ConstantOp(builtin.IntegerAttr(1, builtin.IndexType()))
        )

        tensor_length = rewriter.insert_op(arith.SubiOp(op.upper, op.lower))
        tensor_length_index = rewriter.insert_op(
            arith.IndexCastOp(tensor_length.result, builtin.IndexType())
        )
        tensor_length_index.result.name_hint = _name_hint_ext(
            op.result.name_hint, "length"
        )

        tensor_type = _list_type_to_tensor(op.result.type)
        start_tensor = rewriter.insert_op(
            tensor.EmptyOp((tensor_length_index.result,), tensor_type)
        )
        start_tensor.tensor.name_hint = _name_hint_ext(op.result.name_hint, "uninit")

        rewriter_ip = rewriter.insertion_point

        for_body = Block(
            [],
            arg_types=(builtin.IndexType(), tensor_type),
        )
        ind_var = for_body.args[0]
        ind_var.name_hint = "_i"
        tensor_arg = for_body.args[1]

        rewriter.insertion_point = InsertPoint.at_start(for_body)

        ind_var_32 = rewriter.insert_op(arith.IndexCastOp(ind_var, builtin.i32))
        offseted = rewriter.insert_op(arith.AddiOp(op.lower, ind_var_32))
        modified = rewriter.insert_op(
            tensor.InsertOp(offseted.result, tensor_arg, [ind_var])
        )
        modified.result.name_hint = _name_hint_ext(op.result.name_hint, "modified")
        rewriter.insert_op(scf.YieldOp(modified))

        rewriter.insertion_point = rewriter_ip

        for_op = scf.ForOp(
            zero,
            tensor_length_index,
            one,
            [start_tensor],
            for_body,
        )
        for_op.results[0].name_hint = op.result.name_hint
        rewriter.replace_op(op, for_op)

match_and_rewrite(op: list_dialect.RangeOp, rewriter: PatternRewriter)

Source code in xdsl/frontend/listlang/lowerings.py
157
158
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
@op_type_rewrite_pattern
def match_and_rewrite(self, op: list_dialect.RangeOp, rewriter: PatternRewriter):
    zero = rewriter.insert_op(
        arith.ConstantOp(builtin.IntegerAttr(0, builtin.IndexType()))
    )
    one = rewriter.insert_op(
        arith.ConstantOp(builtin.IntegerAttr(1, builtin.IndexType()))
    )

    tensor_length = rewriter.insert_op(arith.SubiOp(op.upper, op.lower))
    tensor_length_index = rewriter.insert_op(
        arith.IndexCastOp(tensor_length.result, builtin.IndexType())
    )
    tensor_length_index.result.name_hint = _name_hint_ext(
        op.result.name_hint, "length"
    )

    tensor_type = _list_type_to_tensor(op.result.type)
    start_tensor = rewriter.insert_op(
        tensor.EmptyOp((tensor_length_index.result,), tensor_type)
    )
    start_tensor.tensor.name_hint = _name_hint_ext(op.result.name_hint, "uninit")

    rewriter_ip = rewriter.insertion_point

    for_body = Block(
        [],
        arg_types=(builtin.IndexType(), tensor_type),
    )
    ind_var = for_body.args[0]
    ind_var.name_hint = "_i"
    tensor_arg = for_body.args[1]

    rewriter.insertion_point = InsertPoint.at_start(for_body)

    ind_var_32 = rewriter.insert_op(arith.IndexCastOp(ind_var, builtin.i32))
    offseted = rewriter.insert_op(arith.AddiOp(op.lower, ind_var_32))
    modified = rewriter.insert_op(
        tensor.InsertOp(offseted.result, tensor_arg, [ind_var])
    )
    modified.result.name_hint = _name_hint_ext(op.result.name_hint, "modified")
    rewriter.insert_op(scf.YieldOp(modified))

    rewriter.insertion_point = rewriter_ip

    for_op = scf.ForOp(
        zero,
        tensor_length_index,
        one,
        [start_tensor],
        for_body,
    )
    for_op.results[0].name_hint = op.result.name_hint
    rewriter.replace_op(op, for_op)

LowerListToTensor dataclass

Bases: ModulePass

Lowers list dialect to a tensor-based representation.

Source code in xdsl/frontend/listlang/lowerings.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
class LowerListToTensor(ModulePass):
    """
    Lowers list dialect to a tensor-based representation.
    """

    name = "lower-list-to-tensor"

    def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
        PatternRewriteWalker(
            GreedyRewritePatternApplier(
                [
                    LowerLengthOp(),
                    LowerMapOp(),
                    LowerPrintOp(),
                    LowerRangeOp(),
                ]
            ),
        ).rewrite_module(op)

name = 'lower-list-to-tensor' class-attribute instance-attribute

apply(ctx: Context, op: builtin.ModuleOp) -> None

Source code in xdsl/frontend/listlang/lowerings.py
220
221
222
223
224
225
226
227
228
229
230
def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
    PatternRewriteWalker(
        GreedyRewritePatternApplier(
            [
                LowerLengthOp(),
                LowerMapOp(),
                LowerPrintOp(),
                LowerRangeOp(),
            ]
        ),
    ).rewrite_module(op)

WrapModuleInFunc dataclass

Bases: ModulePass

Wraps the free-standing ops in a module into a func.func function.

Source code in xdsl/frontend/listlang/lowerings.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
class WrapModuleInFunc(ModulePass):
    """
    Wraps the free-standing ops in a module into a func.func function.
    """

    name = "wrap-module-in-func"

    def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
        old_module_region = op.detach_region(0)
        new_module_region = Region(
            [Block([func.FuncOp("main", ((), ()), old_module_region)])]
        )
        old_module_region.block.add_op(func.ReturnOp())
        op.add_region(new_module_region)

name = 'wrap-module-in-func' class-attribute instance-attribute

apply(ctx: Context, op: builtin.ModuleOp) -> None

Source code in xdsl/frontend/listlang/lowerings.py
240
241
242
243
244
245
246
def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
    old_module_region = op.detach_region(0)
    new_module_region = Region(
        [Block([func.FuncOp("main", ((), ()), old_module_region)])]
    )
    old_module_region.block.add_op(func.ReturnOp())
    op.add_region(new_module_region)