Skip to content

Riscv scf

riscv_scf

RISC-V SCF dialect

RISCV_Scf = Dialect('riscv_scf', [YieldOp, ForOp, RofOp, WhileOp, ConditionOp], []) module-attribute

YieldOp dataclass

Bases: AbstractYieldOperation[RISCVRegisterType]

Source code in xdsl/dialects/riscv_scf.py
53
54
55
56
57
58
59
60
61
62
63
@irdl_op_definition
class YieldOp(AbstractYieldOperation[RISCVRegisterType]):
    name = "riscv_scf.yield"

    traits = lazy_traits_def(
        lambda: (
            IsTerminator(),
            HasParent(WhileOp, ForRofOperation),
            NoMemoryEffect(),
        )
    )

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

traits = lazy_traits_def(lambda: (IsTerminator(), HasParent(WhileOp, ForRofOperation), NoMemoryEffect())) class-attribute instance-attribute

ForRofOperation

Bases: RegisterAllocatableOperation, IRDLOperation, ABC

Source code in xdsl/dialects/riscv_scf.py
 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
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
154
155
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
class ForRofOperation(RegisterAllocatableOperation, IRDLOperation, ABC):
    lb = operand_def(IntRegisterType)
    ub = operand_def(IntRegisterType)
    step_val = opt_operand_def(IntRegisterType)
    step_attr = opt_prop_def(IntegerAttr[IntegerType])

    iter_args = var_operand_def(RISCVRegisterType)

    res = var_result_def(RISCVRegisterType)

    body = region_def("single_block")

    traits = traits_def(SingleBlockImplicitTerminator(YieldOp), RecursiveMemoryEffect())
    irdl_options = (AttrSizedOperandSegments(as_property=True),)

    @property
    def step(self) -> IntegerAttr[IntegerType] | SSAValue:
        """Static step (typed integer) or dynamic register SSA value."""
        if self.step_attr is not None:
            return self.step_attr
        else:
            assert self.step_val is not None, (
                "Exactly one of step_attr or step_val must be set"
            )
            return self.step_val

    def __init__(
        self,
        lb: SSAValue | Operation,
        ub: SSAValue | Operation,
        step: SSAValue | Operation | IntegerAttr,
        iter_args: Sequence[SSAValue | Operation],
        body: Region | Sequence[Operation] | Sequence[Block] | Block,
    ):
        if isinstance(body, Block):
            body = [body]

        if isinstance(step, IntegerAttr):
            step_attr = step
            step_val = None
        else:
            step_attr = None
            step_val = step

        super().__init__(
            operands=[lb, ub, step_val, iter_args],
            properties={"step_attr": step_attr},
            result_types=[[SSAValue.get(a).type for a in iter_args]],
            regions=[body],
        )

    def verify_(self):
        if (self.step_attr is None) == (self.step_val is None):
            raise VerifyException(
                "Exactly one of step_attr (static) or step_val (dynamic) must be set, "
                f"got step_attr={self.step_attr}, step_val={self.step_val}"
            )
        if (len(self.iter_args) + 1) != len(self.body.block.args):
            raise VerifyException(
                f"Wrong number of block arguments, expected {len(self.iter_args) + 1}, got "
                f"{len(self.body.block.args)}. The body must have the induction "
                f"variable and loop-carried variables as arguments."
            )
        if self.body.block.args and (iter_var := self.body.block.args[0]):
            if not isinstance(iter_var.type, IntRegisterType):
                raise VerifyException(
                    f"The first block argument of the body is of type {iter_var.type}"
                    " instead of riscv.IntRegisterType"
                )
        for idx, (arg, block_arg) in enumerate(
            zip(self.iter_args, self.body.block.args[1:])
        ):
            if block_arg.type != arg.type:
                raise VerifyException(
                    f"Block argument {idx + 1} has wrong type, expected {arg.type}, "
                    f"got {block_arg.type}. Arguments after the "
                    f"induction variable must match the carried variables."
                )
        if len(self.body.ops) > 0 and isinstance(
            yieldop := self.body.block.last_op, YieldOp
        ):
            if len(yieldop.arguments) != len(self.iter_args):
                raise VerifyException(
                    f"Expected {len(self.iter_args)} args, got {len(yieldop.arguments)}. "
                    f"The riscv_scf.for must yield its carried variables."
                )
            for iter_arg, yield_arg in zip(self.iter_args, yieldop.arguments):
                if iter_arg.type != yield_arg.type:
                    raise VerifyException(
                        f"Expected {iter_arg.type}, got {yield_arg.type}. The "
                        f"riscv_scf.for's riscv_scf.yield must match carried"
                        f"variables types."
                    )

    def allocate_registers(self, allocator: BlockAllocator) -> None:
        # Allocate values used inside the body but defined outside.
        # Their scope lasts for the whole body execution scope
        live_ins = allocator.live_ins_per_block[self.body.block]
        for live_in in live_ins:
            allocator.allocate_value(live_in)

        yield_op = self.body.block.last_op
        assert yield_op is not None, (
            "last op of riscv_scf.ForOp is guaranteed to be riscv_scf.Yield"
        )
        block_args = self.body.block.args

        # The loop-carried variables are trickier
        # The for op operand, block arg, and yield operand must have the same type
        for block_arg, operand, yield_operand, op_result in zip(
            block_args[1:], self.iter_args, yield_op.operands, self.results
        ):
            allocator.allocate_values_same_reg(
                (block_arg, operand, yield_operand, op_result)
            )

        # Induction variable
        allocator.allocate_value(block_args[0])

        # ub is used throughout the loop; step_val only when dynamic
        allocator.allocate_value(self.ub)
        if self.step_val is not None:
            allocator.allocate_value(self.step_val)

        # Reserve the loop carried variables for allocation within the body
        regs = self.iter_args.types
        assert all(isinstance(reg, RISCVRegisterType) for reg in regs)
        regs = cast(tuple[RISCVRegisterType, ...], regs)
        with allocator.available_registers.reserve_registers(regs):
            allocator.allocate_block(self.body.block)

        # lb is only used as an input to the loop, so free induction variable before
        # allocating lb to it in case it's not yet allocated
        allocator.free_value(self.body.block.args[0])
        allocator.allocate_value(self.lb)

lb = operand_def(IntRegisterType) class-attribute instance-attribute

ub = operand_def(IntRegisterType) class-attribute instance-attribute

step_val = opt_operand_def(IntRegisterType) class-attribute instance-attribute

step_attr = opt_prop_def(IntegerAttr[IntegerType]) class-attribute instance-attribute

iter_args = var_operand_def(RISCVRegisterType) class-attribute instance-attribute

res = var_result_def(RISCVRegisterType) class-attribute instance-attribute

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

traits = traits_def(SingleBlockImplicitTerminator(YieldOp), RecursiveMemoryEffect()) class-attribute instance-attribute

irdl_options = (AttrSizedOperandSegments(as_property=True),) class-attribute instance-attribute

step: IntegerAttr[IntegerType] | SSAValue property

Static step (typed integer) or dynamic register SSA value.

__init__(lb: SSAValue | Operation, ub: SSAValue | Operation, step: SSAValue | Operation | IntegerAttr, iter_args: Sequence[SSAValue | Operation], body: Region | Sequence[Operation] | Sequence[Block] | Block)

Source code in xdsl/dialects/riscv_scf.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def __init__(
    self,
    lb: SSAValue | Operation,
    ub: SSAValue | Operation,
    step: SSAValue | Operation | IntegerAttr,
    iter_args: Sequence[SSAValue | Operation],
    body: Region | Sequence[Operation] | Sequence[Block] | Block,
):
    if isinstance(body, Block):
        body = [body]

    if isinstance(step, IntegerAttr):
        step_attr = step
        step_val = None
    else:
        step_attr = None
        step_val = step

    super().__init__(
        operands=[lb, ub, step_val, iter_args],
        properties={"step_attr": step_attr},
        result_types=[[SSAValue.get(a).type for a in iter_args]],
        regions=[body],
    )

verify_()

Source code in xdsl/dialects/riscv_scf.py
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
154
155
156
157
158
def verify_(self):
    if (self.step_attr is None) == (self.step_val is None):
        raise VerifyException(
            "Exactly one of step_attr (static) or step_val (dynamic) must be set, "
            f"got step_attr={self.step_attr}, step_val={self.step_val}"
        )
    if (len(self.iter_args) + 1) != len(self.body.block.args):
        raise VerifyException(
            f"Wrong number of block arguments, expected {len(self.iter_args) + 1}, got "
            f"{len(self.body.block.args)}. The body must have the induction "
            f"variable and loop-carried variables as arguments."
        )
    if self.body.block.args and (iter_var := self.body.block.args[0]):
        if not isinstance(iter_var.type, IntRegisterType):
            raise VerifyException(
                f"The first block argument of the body is of type {iter_var.type}"
                " instead of riscv.IntRegisterType"
            )
    for idx, (arg, block_arg) in enumerate(
        zip(self.iter_args, self.body.block.args[1:])
    ):
        if block_arg.type != arg.type:
            raise VerifyException(
                f"Block argument {idx + 1} has wrong type, expected {arg.type}, "
                f"got {block_arg.type}. Arguments after the "
                f"induction variable must match the carried variables."
            )
    if len(self.body.ops) > 0 and isinstance(
        yieldop := self.body.block.last_op, YieldOp
    ):
        if len(yieldop.arguments) != len(self.iter_args):
            raise VerifyException(
                f"Expected {len(self.iter_args)} args, got {len(yieldop.arguments)}. "
                f"The riscv_scf.for must yield its carried variables."
            )
        for iter_arg, yield_arg in zip(self.iter_args, yieldop.arguments):
            if iter_arg.type != yield_arg.type:
                raise VerifyException(
                    f"Expected {iter_arg.type}, got {yield_arg.type}. The "
                    f"riscv_scf.for's riscv_scf.yield must match carried"
                    f"variables types."
                )

allocate_registers(allocator: BlockAllocator) -> None

Source code in xdsl/dialects/riscv_scf.py
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
def allocate_registers(self, allocator: BlockAllocator) -> None:
    # Allocate values used inside the body but defined outside.
    # Their scope lasts for the whole body execution scope
    live_ins = allocator.live_ins_per_block[self.body.block]
    for live_in in live_ins:
        allocator.allocate_value(live_in)

    yield_op = self.body.block.last_op
    assert yield_op is not None, (
        "last op of riscv_scf.ForOp is guaranteed to be riscv_scf.Yield"
    )
    block_args = self.body.block.args

    # The loop-carried variables are trickier
    # The for op operand, block arg, and yield operand must have the same type
    for block_arg, operand, yield_operand, op_result in zip(
        block_args[1:], self.iter_args, yield_op.operands, self.results
    ):
        allocator.allocate_values_same_reg(
            (block_arg, operand, yield_operand, op_result)
        )

    # Induction variable
    allocator.allocate_value(block_args[0])

    # ub is used throughout the loop; step_val only when dynamic
    allocator.allocate_value(self.ub)
    if self.step_val is not None:
        allocator.allocate_value(self.step_val)

    # Reserve the loop carried variables for allocation within the body
    regs = self.iter_args.types
    assert all(isinstance(reg, RISCVRegisterType) for reg in regs)
    regs = cast(tuple[RISCVRegisterType, ...], regs)
    with allocator.available_registers.reserve_registers(regs):
        allocator.allocate_block(self.body.block)

    # lb is only used as an input to the loop, so free induction variable before
    # allocating lb to it in case it's not yet allocated
    allocator.free_value(self.body.block.args[0])
    allocator.allocate_value(self.lb)

ForOp dataclass

Bases: ForRofOperation

A for loop, counting up from lb to ub by step each iteration.

Source code in xdsl/dialects/riscv_scf.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
@irdl_op_definition
class ForOp(ForRofOperation):
    """
    A for loop, counting up from lb to ub by step each iteration.
    """

    name = "riscv_scf.for"

    def print(self, printer: Printer):
        print_for_op_like(
            printer,
            self.lb,
            self.ub,
            self.step,
            self.iter_args,
            self.body,
        )

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        lb, ub, step, iter_arg_operands, body = parse_for_op_like(
            parser, allow_static_step=True
        )
        _, *iter_args = body.block.args

        for_op = cls(lb, ub, step, iter_arg_operands, body)

        if not iter_args:
            for trait in for_op.get_traits_of_type(SingleBlockImplicitTerminator):
                ensure_terminator(for_op, trait)

        return for_op

name = 'riscv_scf.for' class-attribute instance-attribute

print(printer: Printer)

Source code in xdsl/dialects/riscv_scf.py
211
212
213
214
215
216
217
218
219
def print(self, printer: Printer):
    print_for_op_like(
        printer,
        self.lb,
        self.ub,
        self.step,
        self.iter_args,
        self.body,
    )

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_scf.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
@classmethod
def parse(cls, parser: Parser) -> Self:
    lb, ub, step, iter_arg_operands, body = parse_for_op_like(
        parser, allow_static_step=True
    )
    _, *iter_args = body.block.args

    for_op = cls(lb, ub, step, iter_arg_operands, body)

    if not iter_args:
        for trait in for_op.get_traits_of_type(SingleBlockImplicitTerminator):
            ensure_terminator(for_op, trait)

    return for_op

RofOp dataclass

Bases: ForRofOperation

Reverse Order For loop.

MLIR's for loops have the constraint of always executing from lb to ub, so in order to express loops that count down from ub to lb, the rof op is needed.

Rof has the semantics of going from ub to lb, decrementing by step each time. The implicit constraints are that lb < ub, and step > 0.

In order to convert a for to a rof, one needs to switch lb and ub. (for the normalized case that (ub - lb) % step == 0)

Source code in xdsl/dialects/riscv_scf.py
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
@irdl_op_definition
class RofOp(ForRofOperation):
    """
    Reverse Order For loop.

    MLIR's for loops have the constraint of always executing from lb to ub,
    so in order to express loops that count down from ub to lb, the rof op
    is needed.

    Rof has the semantics of going from ub to lb, decrementing by step each time.
    The implicit constraints are that lb < ub, and step > 0.

    In order to convert a for to a rof, one needs to switch lb and ub.
    (for the normalized case that (ub - lb) % step == 0)
    """

    name = "riscv_scf.rof"

    def print(self, printer: Printer):
        print_for_op_like(
            printer,
            self.ub,
            self.lb,
            self.step,
            self.iter_args,
            self.body,
            bound_words=["down", "to"],
        )

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        ub, lb, step, iter_arg_operands, body = parse_for_op_like(
            parser, bound_words=["down", "to"], allow_static_step=True
        )
        _, *iter_args = body.block.args

        rof_op = cls(lb, ub, step, iter_arg_operands, body)

        if not iter_args:
            for trait in rof_op.get_traits_of_type(SingleBlockImplicitTerminator):
                ensure_terminator(rof_op, trait)

        return rof_op

name = 'riscv_scf.rof' class-attribute instance-attribute

print(printer: Printer)

Source code in xdsl/dialects/riscv_scf.py
255
256
257
258
259
260
261
262
263
264
def print(self, printer: Printer):
    print_for_op_like(
        printer,
        self.ub,
        self.lb,
        self.step,
        self.iter_args,
        self.body,
        bound_words=["down", "to"],
    )

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_scf.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
@classmethod
def parse(cls, parser: Parser) -> Self:
    ub, lb, step, iter_arg_operands, body = parse_for_op_like(
        parser, bound_words=["down", "to"], allow_static_step=True
    )
    _, *iter_args = body.block.args

    rof_op = cls(lb, ub, step, iter_arg_operands, body)

    if not iter_args:
        for trait in rof_op.get_traits_of_type(SingleBlockImplicitTerminator):
            ensure_terminator(rof_op, trait)

    return rof_op

WhileOp

Bases: IRDLOperation

Source code in xdsl/dialects/riscv_scf.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
@irdl_op_definition
class WhileOp(IRDLOperation):
    name = "riscv_scf.while"
    arguments = var_operand_def(RISCVRegisterType)

    res = var_result_def(RISCVRegisterType)
    before_region = region_def()
    after_region = region_def()

    traits = traits_def(RecursiveMemoryEffect())

    def __init__(
        self,
        arguments: Sequence[SSAValue | Operation],
        result_types: Sequence[Attribute],
        before_region: Region | Sequence[Operation] | Sequence[Block],
        after_region: Region | Sequence[Operation] | Sequence[Block],
    ):
        super().__init__(
            operands=[arguments],
            result_types=[result_types],
            regions=[before_region, after_region],
        )

    # TODO verify dependencies between riscv_scf.condition, riscv_scf.yield and the regions
    def verify_(self):
        for idx, (block_arg, arg) in enumerate(
            zip(
                self.before_region.block.args,
                self.arguments,
                strict=True,
            )
        ):
            if block_arg.type != arg.type:
                raise VerifyException(
                    f"Block arguments at {idx} has wrong type,"
                    f" expected {arg.type},"
                    f" got {block_arg.type}"
                )

        for idx, (block_arg, res) in enumerate(
            zip(
                self.after_region.block.args,
                self.res,
                strict=True,
            )
        ):
            if block_arg.type != res.type:
                raise VerifyException(
                    f"Block arguments at {idx} has wrong type,"
                    f" expected {res.type},"
                    f" got {block_arg.type}"
                )

    @staticmethod
    def _print_pair(printer: Printer, pair: tuple[SSAValue, SSAValue]):
        printer.print_ssa_value(pair[0])
        printer.print_string(" = ")
        printer.print_ssa_value(pair[1])

    def print(self, printer: Printer):
        printer.print_string(" (")
        block_args = self.before_region.block.args
        printer.print_list(
            zip(block_args, self.arguments, strict=True),
            lambda pair: self._print_pair(printer, pair),
        )
        printer.print_string(") : ")
        printer.print_operation_type(self)
        printer.print_string(" ")
        printer.print_region(self.before_region, print_entry_block_args=False)
        printer.print_string(" do ")
        printer.print_region(self.after_region)
        if self.attributes:
            printer.print_op_attributes(self.attributes, print_keyword=True)

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        def parse_assignment():
            arg = parser.parse_argument(expect_type=False)
            parser.parse_punctuation("=")
            operand = parser.parse_unresolved_operand()
            return arg, operand

        tuples = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN,
            parse_assignment,
        )

        parser.parse_punctuation(":")
        type_pos = parser.pos
        function_type = parser.parse_function_type()

        if len(tuples) != len(function_type.inputs.data):
            parser.raise_error(
                f"Mismatch between block argument count ({len(tuples)}) and operand count ({len(function_type.inputs.data)})",
                type_pos,
                parser.pos,
            )

        block_args = tuple(
            block_arg.resolve(t)
            for ((block_arg, _), t) in zip(
                tuples, function_type.inputs.data, strict=True
            )
        )

        arguments = tuple(
            parser.resolve_operand(operand, t)
            for ((_, operand), t) in zip(tuples, function_type.inputs.data, strict=True)
        )

        before_region = parser.parse_region(block_args)
        parser.parse_characters("do")
        after_region = parser.parse_region()

        attrs = parser.parse_optional_attr_dict_with_keyword()

        op = cls(arguments, function_type.outputs.data, before_region, after_region)

        if attrs is not None:
            op.attributes |= attrs.data

        return op

name = 'riscv_scf.while' class-attribute instance-attribute

arguments = var_operand_def(RISCVRegisterType) class-attribute instance-attribute

res = var_result_def(RISCVRegisterType) class-attribute instance-attribute

before_region = region_def() class-attribute instance-attribute

after_region = region_def() class-attribute instance-attribute

traits = traits_def(RecursiveMemoryEffect()) class-attribute instance-attribute

__init__(arguments: Sequence[SSAValue | Operation], result_types: Sequence[Attribute], before_region: Region | Sequence[Operation] | Sequence[Block], after_region: Region | Sequence[Operation] | Sequence[Block])

Source code in xdsl/dialects/riscv_scf.py
293
294
295
296
297
298
299
300
301
302
303
304
def __init__(
    self,
    arguments: Sequence[SSAValue | Operation],
    result_types: Sequence[Attribute],
    before_region: Region | Sequence[Operation] | Sequence[Block],
    after_region: Region | Sequence[Operation] | Sequence[Block],
):
    super().__init__(
        operands=[arguments],
        result_types=[result_types],
        regions=[before_region, after_region],
    )

verify_()

Source code in xdsl/dialects/riscv_scf.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
def verify_(self):
    for idx, (block_arg, arg) in enumerate(
        zip(
            self.before_region.block.args,
            self.arguments,
            strict=True,
        )
    ):
        if block_arg.type != arg.type:
            raise VerifyException(
                f"Block arguments at {idx} has wrong type,"
                f" expected {arg.type},"
                f" got {block_arg.type}"
            )

    for idx, (block_arg, res) in enumerate(
        zip(
            self.after_region.block.args,
            self.res,
            strict=True,
        )
    ):
        if block_arg.type != res.type:
            raise VerifyException(
                f"Block arguments at {idx} has wrong type,"
                f" expected {res.type},"
                f" got {block_arg.type}"
            )

print(printer: Printer)

Source code in xdsl/dialects/riscv_scf.py
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def print(self, printer: Printer):
    printer.print_string(" (")
    block_args = self.before_region.block.args
    printer.print_list(
        zip(block_args, self.arguments, strict=True),
        lambda pair: self._print_pair(printer, pair),
    )
    printer.print_string(") : ")
    printer.print_operation_type(self)
    printer.print_string(" ")
    printer.print_region(self.before_region, print_entry_block_args=False)
    printer.print_string(" do ")
    printer.print_region(self.after_region)
    if self.attributes:
        printer.print_op_attributes(self.attributes, print_keyword=True)

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_scf.py
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
@classmethod
def parse(cls, parser: Parser) -> Self:
    def parse_assignment():
        arg = parser.parse_argument(expect_type=False)
        parser.parse_punctuation("=")
        operand = parser.parse_unresolved_operand()
        return arg, operand

    tuples = parser.parse_comma_separated_list(
        parser.Delimiter.PAREN,
        parse_assignment,
    )

    parser.parse_punctuation(":")
    type_pos = parser.pos
    function_type = parser.parse_function_type()

    if len(tuples) != len(function_type.inputs.data):
        parser.raise_error(
            f"Mismatch between block argument count ({len(tuples)}) and operand count ({len(function_type.inputs.data)})",
            type_pos,
            parser.pos,
        )

    block_args = tuple(
        block_arg.resolve(t)
        for ((block_arg, _), t) in zip(
            tuples, function_type.inputs.data, strict=True
        )
    )

    arguments = tuple(
        parser.resolve_operand(operand, t)
        for ((_, operand), t) in zip(tuples, function_type.inputs.data, strict=True)
    )

    before_region = parser.parse_region(block_args)
    parser.parse_characters("do")
    after_region = parser.parse_region()

    attrs = parser.parse_optional_attr_dict_with_keyword()

    op = cls(arguments, function_type.outputs.data, before_region, after_region)

    if attrs is not None:
        op.attributes |= attrs.data

    return op

ConditionOp

Bases: IRDLOperation

Source code in xdsl/dialects/riscv_scf.py
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
@irdl_op_definition
class ConditionOp(IRDLOperation):
    name = "riscv_scf.condition"
    cond = operand_def(IntRegisterType)
    arguments = var_operand_def(RISCVRegisterType)

    traits = traits_def(HasParent(WhileOp), IsTerminator(), NoMemoryEffect())

    def __init__(self, cond: SSAValue | Operation, *output_ops: SSAValue | Operation):
        super().__init__(operands=[cond, output_ops])

    def print(self, printer: Printer):
        printer.print_string("(")
        printer.print_ssa_value(self.cond)
        printer.print_string(" : ")
        printer.print_attribute(self.cond.type)
        printer.print_string(") ")
        if self.attributes:
            printer.print_op_attributes(self.attributes)
        if self.arguments:
            printer.print_string(" ")
            printer.print_list(self.arguments, printer.print_ssa_value)
            printer.print_string(" : ")
            printer.print_list(
                self.arguments, lambda val: printer.print_attribute(val.type)
            )

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        parser.parse_punctuation("(")
        unresolved_cond = parser.parse_unresolved_operand("cond expected")
        parser.parse_punctuation(":")
        cond_type = parser.parse_type()
        parser.parse_punctuation(")")
        cond = parser.resolve_operand(unresolved_cond, cond_type)
        attrs = parser.parse_optional_attr_dict()

        # scf.condition is a terminator, so the list of arguments cannot be confused with
        # the results of a hypothetical operation on the next line.
        pos = parser.pos
        unresolved_arguments = parser.parse_optional_undelimited_comma_separated_list(
            parser.parse_optional_unresolved_operand, parser.parse_unresolved_operand
        )
        if unresolved_arguments is not None:
            parser.parse_punctuation(":")
            types = parser.parse_comma_separated_list(
                parser.Delimiter.NONE, parser.parse_type
            )
            arguments = parser.resolve_operands(unresolved_arguments, types, pos)
        else:
            arguments: Sequence[SSAValue] = ()

        op = cls(cond, *arguments)
        op.attributes = attrs
        return op

name = 'riscv_scf.condition' class-attribute instance-attribute

cond = operand_def(IntRegisterType) class-attribute instance-attribute

arguments = var_operand_def(RISCVRegisterType) class-attribute instance-attribute

traits = traits_def(HasParent(WhileOp), IsTerminator(), NoMemoryEffect()) class-attribute instance-attribute

__init__(cond: SSAValue | Operation, *output_ops: SSAValue | Operation)

Source code in xdsl/dialects/riscv_scf.py
416
417
def __init__(self, cond: SSAValue | Operation, *output_ops: SSAValue | Operation):
    super().__init__(operands=[cond, output_ops])

print(printer: Printer)

Source code in xdsl/dialects/riscv_scf.py
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
def print(self, printer: Printer):
    printer.print_string("(")
    printer.print_ssa_value(self.cond)
    printer.print_string(" : ")
    printer.print_attribute(self.cond.type)
    printer.print_string(") ")
    if self.attributes:
        printer.print_op_attributes(self.attributes)
    if self.arguments:
        printer.print_string(" ")
        printer.print_list(self.arguments, printer.print_ssa_value)
        printer.print_string(" : ")
        printer.print_list(
            self.arguments, lambda val: printer.print_attribute(val.type)
        )

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_scf.py
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
@classmethod
def parse(cls, parser: Parser) -> Self:
    parser.parse_punctuation("(")
    unresolved_cond = parser.parse_unresolved_operand("cond expected")
    parser.parse_punctuation(":")
    cond_type = parser.parse_type()
    parser.parse_punctuation(")")
    cond = parser.resolve_operand(unresolved_cond, cond_type)
    attrs = parser.parse_optional_attr_dict()

    # scf.condition is a terminator, so the list of arguments cannot be confused with
    # the results of a hypothetical operation on the next line.
    pos = parser.pos
    unresolved_arguments = parser.parse_optional_undelimited_comma_separated_list(
        parser.parse_optional_unresolved_operand, parser.parse_unresolved_operand
    )
    if unresolved_arguments is not None:
        parser.parse_punctuation(":")
        types = parser.parse_comma_separated_list(
            parser.Delimiter.NONE, parser.parse_type
        )
        arguments = parser.resolve_operands(unresolved_arguments, types, pos)
    else:
        arguments: Sequence[SSAValue] = ()

    op = cls(cond, *arguments)
    op.attributes = attrs
    return op