Skip to content

Scf

scf

Scf = Dialect('scf', [IfOp, ForOp, YieldOp, ExecuteRegionOp, ConditionOp, ParallelOp, ReduceOp, ReduceReturnOp, WhileOp, IndexSwitchOp], []) module-attribute

WhileOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
 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
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
@irdl_op_definition
class WhileOp(IRDLOperation):
    name = "scf.while"
    arguments = var_operand_def()

    res = var_result_def()
    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 scf.condition, scf.yield and the regions
    def verify_(self):
        for idx, arg in enumerate(self.arguments):
            if self.before_region.block.args[idx].type != arg.type:
                raise Exception(
                    f"Block arguments with wrong type, expected {arg.type}, "
                    f"got {self.before_region.block.args[idx].type}"
                )

        for idx, res in enumerate(self.res):
            if self.after_region.block.args[idx].type != res.type:
                raise Exception(
                    f"Block arguments with wrong type, expected {res.type}, "
                    f"got {self.after_region.block.args[idx].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 = 'scf.while' class-attribute instance-attribute

arguments = var_operand_def() class-attribute instance-attribute

res = var_result_def() 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/scf.py
71
72
73
74
75
76
77
78
79
80
81
82
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/scf.py
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def verify_(self):
    for idx, arg in enumerate(self.arguments):
        if self.before_region.block.args[idx].type != arg.type:
            raise Exception(
                f"Block arguments with wrong type, expected {arg.type}, "
                f"got {self.before_region.block.args[idx].type}"
            )

    for idx, res in enumerate(self.res):
        if self.after_region.block.args[idx].type != res.type:
            raise Exception(
                f"Block arguments with wrong type, expected {res.type}, "
                f"got {self.after_region.block.args[idx].type}"
            )

print(printer: Printer)

Source code in xdsl/dialects/scf.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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/scf.py
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
@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

YieldOp dataclass

Bases: AbstractYieldOperation[Attribute]

Source code in xdsl/dialects/scf.py
172
173
174
175
176
177
178
179
180
181
182
@irdl_op_definition
class YieldOp(AbstractYieldOperation[Attribute]):
    name = "scf.yield"

    traits = lazy_traits_def(
        lambda: (
            IsTerminator(),
            HasParent(ForOp, IfOp, ExecuteRegionOp, WhileOp, IndexSwitchOp),
            Pure(),
        )
    )

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

traits = lazy_traits_def(lambda: (IsTerminator(), HasParent(ForOp, IfOp, ExecuteRegionOp, WhileOp, IndexSwitchOp), Pure())) class-attribute instance-attribute

ExecuteRegionOpHasCanonicalizationPatternsTrait dataclass

Bases: HasCanonicalizationPatternsTrait

Source code in xdsl/dialects/scf.py
185
186
187
188
189
190
191
192
class ExecuteRegionOpHasCanonicalizationPatternsTrait(HasCanonicalizationPatternsTrait):
    @classmethod
    def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
        from xdsl.transforms.canonicalization_patterns.scf import (
            SingleBlockExecuteInliner,
        )

        return (SingleBlockExecuteInliner(),)

get_canonicalization_patterns() -> tuple[RewritePattern, ...] classmethod

Source code in xdsl/dialects/scf.py
186
187
188
189
190
191
192
@classmethod
def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
    from xdsl.transforms.canonicalization_patterns.scf import (
        SingleBlockExecuteInliner,
    )

    return (SingleBlockExecuteInliner(),)

ExecuteRegionOp

Bases: IRDLOperation

Operation that executes its region exactly once. See external documentation.

Source code in xdsl/dialects/scf.py
195
196
197
198
199
200
201
202
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
@irdl_op_definition
class ExecuteRegionOp(IRDLOperation):
    """
    Operation that executes its region exactly once.
    See external [documentation](https://mlir.llvm.org/docs/Dialects/SCFDialect/#scfexecute_region-scfexecuteregionop).
    """

    name = "scf.execute_region"

    outs = var_result_def()
    region = region_def()

    traits = traits_def(
        ExecuteRegionOpHasCanonicalizationPatternsTrait(),
    )

    def __init__(
        self,
        result_types: Sequence[Attribute],
        region: Region,
        attr_dict: dict[str, Attribute] | None = None,
    ):
        super().__init__(result_types=(result_types,), regions=(region,))

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        if parser.parse_optional_punctuation("->"):
            result_types = parser.parse_optional_comma_separated_list(
                parser.Delimiter.PAREN, parser.parse_type
            )
            if result_types is None:
                result_types = [parser.parse_type()]
        else:
            result_types = []

        region = parser.parse_region()
        attr_dict = parser.parse_optional_attr_dict()

        return cls(result_types, region, attr_dict)

    def print(self, printer: Printer):
        print_block_terminators = False
        if bool(self.outs):
            printer.print_string(" -> (")
            printer.print_list(self.outs.types, printer.print_attribute)
            printer.print_string(")")
            print_block_terminators = True

        printer.print_string(" ")
        printer.print_region(
            self.region,
            print_entry_block_args=False,
            print_block_terminators=print_block_terminators,
        )

        if bool(self.attributes.keys()):
            printer.print_attr_dict(self.attributes)

    def verify_(self) -> None:
        if self.region.first_block is None:
            raise VerifyException(
                "scf.execute_region op region needs to have at least one block"
            )

name = 'scf.execute_region' class-attribute instance-attribute

outs = var_result_def() class-attribute instance-attribute

region = region_def() class-attribute instance-attribute

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

__init__(result_types: Sequence[Attribute], region: Region, attr_dict: dict[str, Attribute] | None = None)

Source code in xdsl/dialects/scf.py
211
212
213
214
215
216
217
def __init__(
    self,
    result_types: Sequence[Attribute],
    region: Region,
    attr_dict: dict[str, Attribute] | None = None,
):
    super().__init__(result_types=(result_types,), regions=(region,))

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/scf.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
@classmethod
def parse(cls, parser: Parser) -> Self:
    if parser.parse_optional_punctuation("->"):
        result_types = parser.parse_optional_comma_separated_list(
            parser.Delimiter.PAREN, parser.parse_type
        )
        if result_types is None:
            result_types = [parser.parse_type()]
    else:
        result_types = []

    region = parser.parse_region()
    attr_dict = parser.parse_optional_attr_dict()

    return cls(result_types, region, attr_dict)

print(printer: Printer)

Source code in xdsl/dialects/scf.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
def print(self, printer: Printer):
    print_block_terminators = False
    if bool(self.outs):
        printer.print_string(" -> (")
        printer.print_list(self.outs.types, printer.print_attribute)
        printer.print_string(")")
        print_block_terminators = True

    printer.print_string(" ")
    printer.print_region(
        self.region,
        print_entry_block_args=False,
        print_block_terminators=print_block_terminators,
    )

    if bool(self.attributes.keys()):
        printer.print_attr_dict(self.attributes)

verify_() -> None

Source code in xdsl/dialects/scf.py
253
254
255
256
257
def verify_(self) -> None:
    if self.region.first_block is None:
        raise VerifyException(
            "scf.execute_region op region needs to have at least one block"
        )

IfOpHasCanonicalizationPatternsTrait dataclass

Bases: HasCanonicalizationPatternsTrait

Source code in xdsl/dialects/scf.py
260
261
262
263
264
265
266
267
class IfOpHasCanonicalizationPatternsTrait(HasCanonicalizationPatternsTrait):
    @classmethod
    def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
        from xdsl.transforms.canonicalization_patterns.scf import (
            IfPropagateConstantCondition,
        )

        return (IfPropagateConstantCondition(),)

get_canonicalization_patterns() -> tuple[RewritePattern, ...] classmethod

Source code in xdsl/dialects/scf.py
261
262
263
264
265
266
267
@classmethod
def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
    from xdsl.transforms.canonicalization_patterns.scf import (
        IfPropagateConstantCondition,
    )

    return (IfPropagateConstantCondition(),)

IfOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
270
271
272
273
274
275
276
277
278
279
280
281
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
@irdl_op_definition
class IfOp(IRDLOperation):
    name = "scf.if"
    output = var_result_def()
    cond = operand_def(IntegerType(1))

    true_region = region_def("single_block")
    # TODO this should be optional under certain conditions
    false_region = region_def()

    traits = traits_def(
        SingleBlockImplicitTerminator(YieldOp),
        RecursiveMemoryEffect(),
        RecursivelySpeculatable(),
        IfOpHasCanonicalizationPatternsTrait(),
    )

    def __init__(
        self,
        cond: SSAValue | Operation,
        return_types: Sequence[Attribute],
        true_region: Region | Sequence[Block] | Sequence[Operation],
        false_region: Region | Sequence[Block] | Sequence[Operation] | None = None,
        attr_dict: dict[str, Attribute] | None = None,
    ):
        if false_region is None:
            false_region = Region()

        super().__init__(
            operands=[cond],
            result_types=[return_types],
            regions=[true_region, false_region],
            attributes=attr_dict,
        )

    @staticmethod
    def parse_region_with_yield(parser: Parser) -> Region:
        region = parser.parse_region()
        block = region.blocks.last
        if block is None:
            block = Block()
            region.add_block(block)
        last_op = block.last_op
        if last_op is not None and last_op.has_trait(IsTerminator):
            return region

        block.add_op(YieldOp())

        return region

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        cond = parser.parse_operand()
        return_types = []
        if parser.parse_optional_punctuation("->"):
            return_types = parser.parse_comma_separated_list(
                parser.Delimiter.PAREN, parser.parse_type
            )

        then_region = cls.parse_region_with_yield(parser)

        else_region = (
            cls.parse_region_with_yield(parser)
            if parser.parse_optional_keyword("else")
            else Region()
        )

        attr_dict = parser.parse_optional_attr_dict()

        return cls(cond, return_types, then_region, else_region, attr_dict)

    def print(self, printer: Printer):
        printer.print_string(" ")
        printer.print_operand(self.cond)

        print_block_terminators = False
        if bool(self.output):
            printer.print_string(" -> (")
            printer.print_list(self.output.types, printer.print_attribute)
            printer.print_string(")")
            print_block_terminators = True

        printer.print_string(" ")
        printer.print_region(
            self.true_region,
            print_entry_block_args=False,
            print_block_terminators=print_block_terminators,
        )

        if bool(self.false_region.blocks):
            printer.print_string(" else ")
            printer.print_region(
                self.false_region,
                print_entry_block_args=False,
                print_block_terminators=print_block_terminators,
            )

        if bool(self.attributes.keys()):
            printer.print_attr_dict(self.attributes)

name = 'scf.if' class-attribute instance-attribute

output = var_result_def() class-attribute instance-attribute

cond = operand_def(IntegerType(1)) class-attribute instance-attribute

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

false_region = region_def() class-attribute instance-attribute

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

__init__(cond: SSAValue | Operation, return_types: Sequence[Attribute], true_region: Region | Sequence[Block] | Sequence[Operation], false_region: Region | Sequence[Block] | Sequence[Operation] | None = None, attr_dict: dict[str, Attribute] | None = None)

Source code in xdsl/dialects/scf.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
def __init__(
    self,
    cond: SSAValue | Operation,
    return_types: Sequence[Attribute],
    true_region: Region | Sequence[Block] | Sequence[Operation],
    false_region: Region | Sequence[Block] | Sequence[Operation] | None = None,
    attr_dict: dict[str, Attribute] | None = None,
):
    if false_region is None:
        false_region = Region()

    super().__init__(
        operands=[cond],
        result_types=[return_types],
        regions=[true_region, false_region],
        attributes=attr_dict,
    )

parse_region_with_yield(parser: Parser) -> Region staticmethod

Source code in xdsl/dialects/scf.py
305
306
307
308
309
310
311
312
313
314
315
316
317
318
@staticmethod
def parse_region_with_yield(parser: Parser) -> Region:
    region = parser.parse_region()
    block = region.blocks.last
    if block is None:
        block = Block()
        region.add_block(block)
    last_op = block.last_op
    if last_op is not None and last_op.has_trait(IsTerminator):
        return region

    block.add_op(YieldOp())

    return region

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/scf.py
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
@classmethod
def parse(cls, parser: Parser) -> Self:
    cond = parser.parse_operand()
    return_types = []
    if parser.parse_optional_punctuation("->"):
        return_types = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN, parser.parse_type
        )

    then_region = cls.parse_region_with_yield(parser)

    else_region = (
        cls.parse_region_with_yield(parser)
        if parser.parse_optional_keyword("else")
        else Region()
    )

    attr_dict = parser.parse_optional_attr_dict()

    return cls(cond, return_types, then_region, else_region, attr_dict)

print(printer: Printer)

Source code in xdsl/dialects/scf.py
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
def print(self, printer: Printer):
    printer.print_string(" ")
    printer.print_operand(self.cond)

    print_block_terminators = False
    if bool(self.output):
        printer.print_string(" -> (")
        printer.print_list(self.output.types, printer.print_attribute)
        printer.print_string(")")
        print_block_terminators = True

    printer.print_string(" ")
    printer.print_region(
        self.true_region,
        print_entry_block_args=False,
        print_block_terminators=print_block_terminators,
    )

    if bool(self.false_region.blocks):
        printer.print_string(" else ")
        printer.print_region(
            self.false_region,
            print_entry_block_args=False,
            print_block_terminators=print_block_terminators,
        )

    if bool(self.attributes.keys()):
        printer.print_attr_dict(self.attributes)

ForOpHasCanonicalizationPatternsTrait dataclass

Bases: HasCanonicalizationPatternsTrait

Source code in xdsl/dialects/scf.py
371
372
373
374
375
376
377
378
379
class ForOpHasCanonicalizationPatternsTrait(HasCanonicalizationPatternsTrait):
    @classmethod
    def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
        from xdsl.transforms.canonicalization_patterns.scf import (
            RehoistConstInLoops,
            SimplifyTrivialLoops,
        )

        return (SimplifyTrivialLoops(), RehoistConstInLoops())

get_canonicalization_patterns() -> tuple[RewritePattern, ...] classmethod

Source code in xdsl/dialects/scf.py
372
373
374
375
376
377
378
379
@classmethod
def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
    from xdsl.transforms.canonicalization_patterns.scf import (
        RehoistConstInLoops,
        SimplifyTrivialLoops,
    )

    return (SimplifyTrivialLoops(), RehoistConstInLoops())

ForOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
@irdl_op_definition
class ForOp(IRDLOperation):
    name = "scf.for"

    T: ClassVar = VarConstraint("T", base(IndexType) | SignlessIntegerConstraint)

    lb = operand_def(T)
    ub = operand_def(T)
    step = operand_def(T)

    iter_args = var_operand_def()

    res = var_result_def()

    body = region_def("single_block")

    traits = traits_def(
        SingleBlockImplicitTerminator(YieldOp),
        ForOpHasCanonicalizationPatternsTrait(),
        RecursiveMemoryEffect(),
    )

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

        super().__init__(
            operands=[lb, ub, step, iter_args],
            result_types=[[SSAValue.get(a).type for a in iter_args]],
            regions=[body],
        )

    def verify_(self):
        # body block verification
        if not self.body.block.args:
            raise VerifyException(
                "Body block must have induction var as first block arg"
            )

        indvar, *block_iter_args = self.body.block.args
        block_iter_args_num = len(block_iter_args)
        iter_args = self.iter_args
        iter_args_num = len(self.iter_args)

        for opnd in (self.lb, self.ub, self.step):
            if opnd.type != indvar.type:
                raise VerifyException(
                    "Expected induction var to be same type as bounds and step"
                )
        if iter_args_num + 1 != block_iter_args_num + 1:
            raise VerifyException(
                f"Expected {iter_args_num + 1} args, but got {block_iter_args_num + 1}. "
                "Body block must have induction and loop-carried variables as args."
            )
        for i, arg in enumerate(iter_args):
            if block_iter_args[i].type != arg.type:
                raise VerifyException(
                    f"Block arg #{i + 1} expected to be {arg.type}, but got {block_iter_args[i].type}. "
                    "Block args after the induction variable must match the loop-carried variables."
                )
        if (last_op := self.body.block.last_op) is not None and isinstance(
            last_op, YieldOp
        ):
            yieldop = last_op
            if len(yieldop.arguments) != iter_args_num:
                raise VerifyException(
                    f"{yieldop.name} expected {iter_args_num} args, but got {len(yieldop.arguments)}. "
                    f"The {self.name} must yield its loop-carried variables."
                )
            for i, arg in enumerate(yieldop.arguments):
                if iter_args[i].type != arg.type:
                    raise VerifyException(
                        f"Expected yield arg #{i} to be {iter_args[i].type}, but got {arg.type}. "
                        f"{yieldop.name} of {self.name} must match loop-carried variable types."
                    )

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

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        lb, ub, step, iter_arg_operands, body = parse_for_op_like(parser, IndexType())
        _, *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 = 'scf.for' class-attribute instance-attribute

T: ClassVar = VarConstraint('T', base(IndexType) | SignlessIntegerConstraint) class-attribute instance-attribute

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

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

step = operand_def(T) class-attribute instance-attribute

iter_args = var_operand_def() class-attribute instance-attribute

res = var_result_def() class-attribute instance-attribute

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

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

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

Source code in xdsl/dialects/scf.py
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
def __init__(
    self,
    lb: SSAValue | Operation,
    ub: SSAValue | Operation,
    step: SSAValue | Operation,
    iter_args: Sequence[SSAValue | Operation],
    body: Region | Sequence[Operation] | Sequence[Block] | Block,
):
    if isinstance(body, Block):
        body = [body]

    super().__init__(
        operands=[lb, ub, step, iter_args],
        result_types=[[SSAValue.get(a).type for a in iter_args]],
        regions=[body],
    )

verify_()

Source code in xdsl/dialects/scf.py
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
463
def verify_(self):
    # body block verification
    if not self.body.block.args:
        raise VerifyException(
            "Body block must have induction var as first block arg"
        )

    indvar, *block_iter_args = self.body.block.args
    block_iter_args_num = len(block_iter_args)
    iter_args = self.iter_args
    iter_args_num = len(self.iter_args)

    for opnd in (self.lb, self.ub, self.step):
        if opnd.type != indvar.type:
            raise VerifyException(
                "Expected induction var to be same type as bounds and step"
            )
    if iter_args_num + 1 != block_iter_args_num + 1:
        raise VerifyException(
            f"Expected {iter_args_num + 1} args, but got {block_iter_args_num + 1}. "
            "Body block must have induction and loop-carried variables as args."
        )
    for i, arg in enumerate(iter_args):
        if block_iter_args[i].type != arg.type:
            raise VerifyException(
                f"Block arg #{i + 1} expected to be {arg.type}, but got {block_iter_args[i].type}. "
                "Block args after the induction variable must match the loop-carried variables."
            )
    if (last_op := self.body.block.last_op) is not None and isinstance(
        last_op, YieldOp
    ):
        yieldop = last_op
        if len(yieldop.arguments) != iter_args_num:
            raise VerifyException(
                f"{yieldop.name} expected {iter_args_num} args, but got {len(yieldop.arguments)}. "
                f"The {self.name} must yield its loop-carried variables."
            )
        for i, arg in enumerate(yieldop.arguments):
            if iter_args[i].type != arg.type:
                raise VerifyException(
                    f"Expected yield arg #{i} to be {iter_args[i].type}, but got {arg.type}. "
                    f"{yieldop.name} of {self.name} must match loop-carried variable types."
                )

print(printer: Printer)

Source code in xdsl/dialects/scf.py
465
466
467
468
469
470
471
472
473
474
def print(self, printer: Printer):
    print_for_op_like(
        printer,
        self.lb,
        self.ub,
        self.step,
        self.iter_args,
        self.body,
        IndexType,
    )

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/scf.py
476
477
478
479
480
481
482
483
484
485
486
487
@classmethod
def parse(cls, parser: Parser) -> Self:
    lb, ub, step, iter_arg_operands, body = parse_for_op_like(parser, IndexType())
    _, *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

ParallelOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
@irdl_op_definition
class ParallelOp(IRDLOperation):
    name = "scf.parallel"
    lowerBound = var_operand_def(IndexType)
    upperBound = var_operand_def(IndexType)
    step = var_operand_def(IndexType)
    initVals = var_operand_def()
    res = var_result_def()

    body = region_def("single_block")

    irdl_options = (AttrSizedOperandSegments(as_property=True),)

    traits = lazy_traits_def(
        lambda: (
            SingleBlockImplicitTerminator(ReduceOp),
            RecursiveMemoryEffect(),
        )
    )

    def __init__(
        self,
        lower_bounds: Sequence[SSAValue | Operation],
        upper_bounds: Sequence[SSAValue | Operation],
        steps: Sequence[SSAValue | Operation],
        body: Region | Sequence[Block] | Sequence[Operation],
        init_vals: Sequence[SSAValue | Operation] = (),
    ):
        super().__init__(
            operands=[lower_bounds, upper_bounds, steps, init_vals],
            regions=[body],
            result_types=[[SSAValue.get(a).type for a in init_vals]],
        )

    def verify_(self) -> None:
        # First check that the number of lower and upper bounds, along with the number of
        # steps is all equal
        if len(self.lowerBound) != len(self.upperBound) or len(self.lowerBound) != len(
            self.step
        ):
            raise VerifyException(
                "Expected the same number of lower bounds, upper "
                "bounds, and steps for scf.parallel. Got "
                f"{len(self.lowerBound)}, {len(self.upperBound)} and "
                f"{len(self.step)}."
            )

        body_args = self.body.block.args
        # Check the number of block arguments equals the number of induction variables as all
        # initVals must be encapsulated in a reduce operation
        if len(self.lowerBound) != len(body_args):
            raise VerifyException(
                "Number of block arguments must exactly equal the number of induction variables"
            )

        reduce_op = self.body.block.last_op
        # Ensured by trait
        assert isinstance(reduce_op, ReduceOp)

        num_reductions = len(reduce_op.reductions)

        # Check that the number of initial values (initVals)
        # equals the number of reductions
        if len(self.initVals) != num_reductions:
            raise VerifyException(
                f"Expected {len(self.initVals)} "
                f"reductions but {num_reductions} provided"
            )

        # Check each induction variable argument is present in the block arguments
        # and the block argument is of type index
        if not all([isinstance(a.type, IndexType) for a in body_args]):
            raise VerifyException(
                "scf.parallel's block must have an index argument"
                " for each induction variable"
            )

        # Now go through each reduction operation and check that the type
        # matches the corresponding initVals type
        for reduction in range(num_reductions):
            arg_type = reduce_op.args[reduction].type
            initValsType = self.initVals[reduction].type
            if initValsType != arg_type:
                raise VerifyException(
                    f"Miss match on scf.parallel argument and reduction op type number {reduction} "
                    f", parallel argment is of type {initValsType} whereas reduction operation is of type {arg_type}"
                )

        # Ensure that the number of reductions matches the
        # number of result types from scf.parallel
        if num_reductions != len(self.res):
            raise VerifyException(
                f"There are {num_reductions} reductions, but {len(self.res)} results expected"
            )

        # Now go through each reduction and ensure that its operand type matches the corresponding
        # scf.parallel result type (there is no result type on scf.reduce, hence we check the
        # operand type)
        for reduction in range(num_reductions):
            arg_type = reduce_op.args[reduction].type
            res_type = self.res[reduction].type
            if res_type != arg_type:
                raise VerifyException(
                    f"Miss match on scf.parallel result type and reduction op type number {reduction} "
                    f", parallel argment is of type {res_type} whereas reduction operation is of type {arg_type}"
                )

name = 'scf.parallel' class-attribute instance-attribute

lowerBound = var_operand_def(IndexType) class-attribute instance-attribute

upperBound = var_operand_def(IndexType) class-attribute instance-attribute

step = var_operand_def(IndexType) class-attribute instance-attribute

initVals = var_operand_def() class-attribute instance-attribute

res = var_result_def() class-attribute instance-attribute

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

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

traits = lazy_traits_def(lambda: (SingleBlockImplicitTerminator(ReduceOp), RecursiveMemoryEffect())) class-attribute instance-attribute

__init__(lower_bounds: Sequence[SSAValue | Operation], upper_bounds: Sequence[SSAValue | Operation], steps: Sequence[SSAValue | Operation], body: Region | Sequence[Block] | Sequence[Operation], init_vals: Sequence[SSAValue | Operation] = ())

Source code in xdsl/dialects/scf.py
510
511
512
513
514
515
516
517
518
519
520
521
522
def __init__(
    self,
    lower_bounds: Sequence[SSAValue | Operation],
    upper_bounds: Sequence[SSAValue | Operation],
    steps: Sequence[SSAValue | Operation],
    body: Region | Sequence[Block] | Sequence[Operation],
    init_vals: Sequence[SSAValue | Operation] = (),
):
    super().__init__(
        operands=[lower_bounds, upper_bounds, steps, init_vals],
        regions=[body],
        result_types=[[SSAValue.get(a).type for a in init_vals]],
    )

verify_() -> None

Source code in xdsl/dialects/scf.py
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
def verify_(self) -> None:
    # First check that the number of lower and upper bounds, along with the number of
    # steps is all equal
    if len(self.lowerBound) != len(self.upperBound) or len(self.lowerBound) != len(
        self.step
    ):
        raise VerifyException(
            "Expected the same number of lower bounds, upper "
            "bounds, and steps for scf.parallel. Got "
            f"{len(self.lowerBound)}, {len(self.upperBound)} and "
            f"{len(self.step)}."
        )

    body_args = self.body.block.args
    # Check the number of block arguments equals the number of induction variables as all
    # initVals must be encapsulated in a reduce operation
    if len(self.lowerBound) != len(body_args):
        raise VerifyException(
            "Number of block arguments must exactly equal the number of induction variables"
        )

    reduce_op = self.body.block.last_op
    # Ensured by trait
    assert isinstance(reduce_op, ReduceOp)

    num_reductions = len(reduce_op.reductions)

    # Check that the number of initial values (initVals)
    # equals the number of reductions
    if len(self.initVals) != num_reductions:
        raise VerifyException(
            f"Expected {len(self.initVals)} "
            f"reductions but {num_reductions} provided"
        )

    # Check each induction variable argument is present in the block arguments
    # and the block argument is of type index
    if not all([isinstance(a.type, IndexType) for a in body_args]):
        raise VerifyException(
            "scf.parallel's block must have an index argument"
            " for each induction variable"
        )

    # Now go through each reduction operation and check that the type
    # matches the corresponding initVals type
    for reduction in range(num_reductions):
        arg_type = reduce_op.args[reduction].type
        initValsType = self.initVals[reduction].type
        if initValsType != arg_type:
            raise VerifyException(
                f"Miss match on scf.parallel argument and reduction op type number {reduction} "
                f", parallel argment is of type {initValsType} whereas reduction operation is of type {arg_type}"
            )

    # Ensure that the number of reductions matches the
    # number of result types from scf.parallel
    if num_reductions != len(self.res):
        raise VerifyException(
            f"There are {num_reductions} reductions, but {len(self.res)} results expected"
        )

    # Now go through each reduction and ensure that its operand type matches the corresponding
    # scf.parallel result type (there is no result type on scf.reduce, hence we check the
    # operand type)
    for reduction in range(num_reductions):
        arg_type = reduce_op.args[reduction].type
        res_type = self.res[reduction].type
        if res_type != arg_type:
            raise VerifyException(
                f"Miss match on scf.parallel result type and reduction op type number {reduction} "
                f", parallel argment is of type {res_type} whereas reduction operation is of type {arg_type}"
            )

ReduceOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
@irdl_op_definition
class ReduceOp(IRDLOperation):
    name = "scf.reduce"
    args = var_operand_def()

    reductions = var_region_def("single_block")

    traits = lazy_traits_def(
        lambda: (
            RecursiveMemoryEffect(),
            HasParent(ParallelOp),
            IsTerminator(),
            SingleBlockImplicitTerminator(ReduceReturnOp),
        )
    )

    assembly_format = "(`(` $args^ `:` type($args) `)`)? $reductions attr-dict"

    def __init__(
        self,
        args: Sequence[SSAValue | Operation] = (),
        regions: Sequence[Region] = (),
    ):
        super().__init__(operands=(args,), regions=(regions,))

    def verify_(self) -> None:
        if len(self.args) != len(self.reductions):
            raise VerifyException(
                "scf.reduce must have the same number of arguments and regions"
                f"but got {len(self.args)} arguments and {len(self.reductions)} regions"
            )
        for region, argument in zip(self.reductions, self.args):
            if len(region.block.args) != 2:
                raise VerifyException(
                    "scf.reduce block must have exactly two arguments, but "
                    f"{len(region.block.args)} were provided"
                )

            if region.block.args[0].type != region.block.args[1].type:
                raise VerifyException(
                    "scf.reduce block argument types must be the same but have "
                    f"{region.block.args[0].type} and {region.block.args[1].type}"
                )

            if region.block.args[0].type != argument.type:
                raise VerifyException(
                    "scf.reduce block argument types must match the operand type "
                    f" but have {region.block.args[0].type} and {argument.type}"
                )

            last_op = region.block.last_op

            # Should be checked by traits
            assert isinstance(last_op, ReduceReturnOp)

            if last_op.result.type != argument.type:
                raise VerifyException(
                    "scf.reduce.return result type at end of scf.reduce block must"
                    f" match the reduction operand type but have {last_op.result.type} "
                    f"and {argument.type}"
                )

name = 'scf.reduce' class-attribute instance-attribute

args = var_operand_def() class-attribute instance-attribute

reductions = var_region_def('single_block') class-attribute instance-attribute

traits = lazy_traits_def(lambda: (RecursiveMemoryEffect(), HasParent(ParallelOp), IsTerminator(), SingleBlockImplicitTerminator(ReduceReturnOp))) class-attribute instance-attribute

assembly_format = '(`(` $args^ `:` type($args) `)`)? $reductions attr-dict' class-attribute instance-attribute

__init__(args: Sequence[SSAValue | Operation] = (), regions: Sequence[Region] = ())

Source code in xdsl/dialects/scf.py
616
617
618
619
620
621
def __init__(
    self,
    args: Sequence[SSAValue | Operation] = (),
    regions: Sequence[Region] = (),
):
    super().__init__(operands=(args,), regions=(regions,))

verify_() -> None

Source code in xdsl/dialects/scf.py
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
def verify_(self) -> None:
    if len(self.args) != len(self.reductions):
        raise VerifyException(
            "scf.reduce must have the same number of arguments and regions"
            f"but got {len(self.args)} arguments and {len(self.reductions)} regions"
        )
    for region, argument in zip(self.reductions, self.args):
        if len(region.block.args) != 2:
            raise VerifyException(
                "scf.reduce block must have exactly two arguments, but "
                f"{len(region.block.args)} were provided"
            )

        if region.block.args[0].type != region.block.args[1].type:
            raise VerifyException(
                "scf.reduce block argument types must be the same but have "
                f"{region.block.args[0].type} and {region.block.args[1].type}"
            )

        if region.block.args[0].type != argument.type:
            raise VerifyException(
                "scf.reduce block argument types must match the operand type "
                f" but have {region.block.args[0].type} and {argument.type}"
            )

        last_op = region.block.last_op

        # Should be checked by traits
        assert isinstance(last_op, ReduceReturnOp)

        if last_op.result.type != argument.type:
            raise VerifyException(
                "scf.reduce.return result type at end of scf.reduce block must"
                f" match the reduction operand type but have {last_op.result.type} "
                f"and {argument.type}"
            )

ReduceReturnOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
661
662
663
664
665
666
667
668
669
670
671
@irdl_op_definition
class ReduceReturnOp(IRDLOperation):
    name = "scf.reduce.return"
    result = operand_def()

    traits = traits_def(HasParent(ReduceOp), IsTerminator(), Pure())

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

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

name = 'scf.reduce.return' class-attribute instance-attribute

result = operand_def() class-attribute instance-attribute

traits = traits_def(HasParent(ReduceOp), IsTerminator(), Pure()) class-attribute instance-attribute

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

__init__(result: SSAValue | Operation)

Source code in xdsl/dialects/scf.py
670
671
def __init__(self, result: SSAValue | Operation):
    super().__init__(operands=[result])

ConditionOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
@irdl_op_definition
class ConditionOp(IRDLOperation):
    name = "scf.condition"
    condition = operand_def(IntegerType(1))
    args = var_operand_def()

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

    assembly_format = "`(` $condition `)` attr-dict ($args^ `:` type($args))?"

    def __init__(
        self,
        condition: SSAValue | Operation,
        *args: SSAValue | Operation,
    ):
        super().__init__(operands=(condition, args))

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

condition = operand_def(IntegerType(1)) class-attribute instance-attribute

args = var_operand_def() class-attribute instance-attribute

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

assembly_format = '`(` $condition `)` attr-dict ($args^ `:` type($args))?' class-attribute instance-attribute

__init__(condition: SSAValue | Operation, *args: SSAValue | Operation)

Source code in xdsl/dialects/scf.py
684
685
686
687
688
689
def __init__(
    self,
    condition: SSAValue | Operation,
    *args: SSAValue | Operation,
):
    super().__init__(operands=(condition, args))

IndexSwitchOp

Bases: IRDLOperation

Source code in xdsl/dialects/scf.py
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
@irdl_op_definition
class IndexSwitchOp(IRDLOperation):
    name = "scf.index_switch"

    arg = operand_def(IndexType)
    cases = prop_def(DenseArrayBase.constr(i64))

    output = var_result_def()

    default_region = region_def("single_block")
    case_regions = var_region_def("single_block")

    traits = traits_def(RecursiveMemoryEffect(), SingleBlockImplicitTerminator(YieldOp))

    def __init__(
        self,
        arg: Operation | SSAValue,
        cases: DenseArrayBase,
        default_region: Region,
        case_regions: Sequence[Region],
        result_types: Sequence[Attribute],
        attr_dict: dict[str, Attribute] | None = None,
    ):
        properties = {
            "cases": cases,
        }

        super().__init__(
            operands=(arg,),
            attributes=attr_dict,
            properties=properties,
            regions=(default_region, case_regions),
            result_types=(result_types,),
        )

    def _verify_region(self, region: Region, name: str):
        yield_op = region.block.last_op
        assert isinstance(yield_op, YieldOp)

        if yield_op.operand_types != self.result_types:
            raise VerifyException(
                f"region {name} returns values of types ({', '.join(str(x) for x in yield_op.operand_types)})"
                f" but expected ({', '.join(str(x) for x in self.result_types)})"
            )

    def verify_(self) -> None:
        if len(self.cases) != len(self.case_regions):
            raise VerifyException(
                f"has {len(self.case_regions)} case regions but {len(self.cases)} case values"
            )

        cases = self.cases
        if len(set(cases.iter_values())) != len(cases):
            raise VerifyException("has duplicate case value")

        self._verify_region(self.default_region, "default")
        for name, region in zip(cases.iter_values(), self.case_regions, strict=True):
            self._verify_region(region, str(name))

    def print(self, printer: Printer):
        printer.print_string(" ")
        printer.print_operand(self.arg)
        attr_dict = {k: v for k, v in self.attributes.items() if k != "cases"}
        if attr_dict:
            printer.print_string(" ")
            printer.print_attr_dict(attr_dict)
        if self.result_types:
            printer.print_string(" -> ")
            printer.print_list(self.result_types, printer.print_attribute)
        printer.print_string("\n")
        for case_value, case_region in zip(
            self.cases.iter_values(), self.case_regions, strict=True
        ):
            printer.print_string(f"case {case_value} ")
            printer.print_region(case_region)
            printer.print_string("\n")

        printer.print_string("default ")
        printer.print_region(self.default_region)

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        arg = parser.parse_operand()
        attr_dict = parser.parse_optional_attr_dict()
        result_types: list[TypeAttribute] = []
        if parser.parse_optional_punctuation("->"):
            types = parser.parse_optional_undelimited_comma_separated_list(
                parser.parse_optional_type, parser.parse_type
            )
            if types is None:
                parser.raise_error("result types not found")
            result_types = types
        case_values: list[int] = []
        case_regions: list[Region] = []
        while parser.parse_optional_keyword("case"):
            case_values.append(parser.parse_integer())
            case_regions.append(parser.parse_region())
        cases = DenseArrayBase.from_list(i64, case_values)
        parser.parse_keyword("default")
        default_region = parser.parse_region()
        return cls(arg, cases, default_region, case_regions, result_types, attr_dict)

name = 'scf.index_switch' class-attribute instance-attribute

arg = operand_def(IndexType) class-attribute instance-attribute

cases = prop_def(DenseArrayBase.constr(i64)) class-attribute instance-attribute

output = var_result_def() class-attribute instance-attribute

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

case_regions = var_region_def('single_block') class-attribute instance-attribute

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

__init__(arg: Operation | SSAValue, cases: DenseArrayBase, default_region: Region, case_regions: Sequence[Region], result_types: Sequence[Attribute], attr_dict: dict[str, Attribute] | None = None)

Source code in xdsl/dialects/scf.py
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
def __init__(
    self,
    arg: Operation | SSAValue,
    cases: DenseArrayBase,
    default_region: Region,
    case_regions: Sequence[Region],
    result_types: Sequence[Attribute],
    attr_dict: dict[str, Attribute] | None = None,
):
    properties = {
        "cases": cases,
    }

    super().__init__(
        operands=(arg,),
        attributes=attr_dict,
        properties=properties,
        regions=(default_region, case_regions),
        result_types=(result_types,),
    )

verify_() -> None

Source code in xdsl/dialects/scf.py
737
738
739
740
741
742
743
744
745
746
747
748
749
def verify_(self) -> None:
    if len(self.cases) != len(self.case_regions):
        raise VerifyException(
            f"has {len(self.case_regions)} case regions but {len(self.cases)} case values"
        )

    cases = self.cases
    if len(set(cases.iter_values())) != len(cases):
        raise VerifyException("has duplicate case value")

    self._verify_region(self.default_region, "default")
    for name, region in zip(cases.iter_values(), self.case_regions, strict=True):
        self._verify_region(region, str(name))

print(printer: Printer)

Source code in xdsl/dialects/scf.py
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
def print(self, printer: Printer):
    printer.print_string(" ")
    printer.print_operand(self.arg)
    attr_dict = {k: v for k, v in self.attributes.items() if k != "cases"}
    if attr_dict:
        printer.print_string(" ")
        printer.print_attr_dict(attr_dict)
    if self.result_types:
        printer.print_string(" -> ")
        printer.print_list(self.result_types, printer.print_attribute)
    printer.print_string("\n")
    for case_value, case_region in zip(
        self.cases.iter_values(), self.case_regions, strict=True
    ):
        printer.print_string(f"case {case_value} ")
        printer.print_region(case_region)
        printer.print_string("\n")

    printer.print_string("default ")
    printer.print_region(self.default_region)

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/scf.py
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
@classmethod
def parse(cls, parser: Parser) -> Self:
    arg = parser.parse_operand()
    attr_dict = parser.parse_optional_attr_dict()
    result_types: list[TypeAttribute] = []
    if parser.parse_optional_punctuation("->"):
        types = parser.parse_optional_undelimited_comma_separated_list(
            parser.parse_optional_type, parser.parse_type
        )
        if types is None:
            parser.raise_error("result types not found")
        result_types = types
    case_values: list[int] = []
    case_regions: list[Region] = []
    while parser.parse_optional_keyword("case"):
        case_values.append(parser.parse_integer())
        case_regions.append(parser.parse_region())
    cases = DenseArrayBase.from_list(i64, case_values)
    parser.parse_keyword("default")
    default_region = parser.parse_region()
    return cls(arg, cases, default_region, case_regions, result_types, attr_dict)