Skip to content

Immutable ir

immutable_ir

ISSAValue dataclass

Bases: ABC

Represents an immutable SSA variable. An immutable SSA variable is either an operation result or a basic block argument.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@dataclass(frozen=True)
class ISSAValue(ABC):
    """
    Represents an immutable SSA variable. An immutable SSA variable is either an operation result
    or a basic block argument.
    """

    type: Attribute
    users: IList[IOperation]

    def _add_user(self, op: IOperation):
        self.users._unfreeze()  # pyright: ignore[reportPrivateUsage]
        self.users.append(op)
        self.users.freeze()

    def _remove_user(self, op: IOperation):
        if op not in self.users:
            raise Exception(
                f"Trying to remove a user ({op.name}) that is not an actual user of this value!"
            )

        self.users._unfreeze()  # pyright: ignore[reportPrivateUsage]
        self.users.remove(op)
        self.users.freeze()

type: Attribute instance-attribute

users: IList[IOperation] instance-attribute

__init__(type: Attribute, users: IList[IOperation]) -> None

IOpResult dataclass

Bases: ISSAValue

Represents an immutable SSA variable defined by an operation result.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@dataclass(frozen=True)
class IOpResult(ISSAValue):
    """Represents an immutable SSA variable defined by an operation result."""

    op: IOperation
    index: int

    def __hash__(self) -> int:
        return hash(id(self.op)) + hash(self.index)

    def __eq__(self, other: object) -> bool:
        if isinstance(other, IOpResult):
            return self.op == other.op and self.index == other.index
        return False

op: IOperation instance-attribute

index: int instance-attribute

__init__(type: Attribute, users: IList[IOperation], op: IOperation, index: int) -> None

__hash__() -> int

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
56
57
def __hash__(self) -> int:
    return hash(id(self.op)) + hash(self.index)

__eq__(other: object) -> bool

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
59
60
61
62
def __eq__(self, other: object) -> bool:
    if isinstance(other, IOpResult):
        return self.op == other.op and self.index == other.index
    return False

IBlockArg dataclass

Bases: ISSAValue

Represents an immutable SSA variable defined by a basic block.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@dataclass(frozen=True)
class IBlockArg(ISSAValue):
    """Represents an immutable SSA variable defined by a basic block."""

    block: IBlock
    index: int

    def __hash__(self) -> int:
        return hash(id(self.block)) + hash(self.index)

    def __eq__(self, other: object) -> bool:
        return self is other

    def __repr__(self) -> str:
        return "BlockArg(type:" + self.type.name + ("attached") + ")"

block: IBlock instance-attribute

index: int instance-attribute

__init__(type: Attribute, users: IList[IOperation], block: IBlock, index: int) -> None

__hash__() -> int

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
72
73
def __hash__(self) -> int:
    return hash(id(self.block)) + hash(self.index)

__eq__(other: object) -> bool

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
75
76
def __eq__(self, other: object) -> bool:
    return self is other

__repr__() -> str

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
78
79
def __repr__(self) -> str:
    return "BlockArg(type:" + self.type.name + ("attached") + ")"

IRegion dataclass

An immutable region contains a CFG of immutable blocks. IRegions are contained in operations.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
 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
201
202
203
204
@dataclass(frozen=True)
class IRegion:
    """An immutable region contains a CFG of immutable blocks. IRegions are contained in operations."""

    blocks: IList[IBlock]
    """Immutable blocks contained in the IRegion. The first block is the entry block."""

    def __hash__(self) -> int:
        return hash(id(self))

    def __eq__(self, value: object) -> bool:
        return self is value

    @property
    def block(self) -> IBlock:
        """
        Returns the block of a single-block region.
        Returns an exception if the region is not single-block.
        """
        if len(self.blocks) != 1:
            raise ValueError(
                "'block' property of IRegion class is only available "
                "for single-block regions."
            )
        return self.blocks[0]

    @property
    def ops(self) -> IList[IOperation]:
        """
        Get the operations of a single-block region.
        Returns an exception if the region is not single-block.
        """
        if len(self.blocks) != 1:
            raise ValueError(
                "'ops' property of IRegion class is only available "
                "for single-block regions."
            )
        return self.block.ops

    def __init__(self, blocks: Sequence[IBlock]):
        """Creates a new immutable region from a sequence of immutable blocks."""

        object.__setattr__(self, "blocks", IList(blocks))
        self.blocks.freeze()

    @classmethod
    def from_mutable(
        cls,
        blocks: Iterable[Block],
        value_map: dict[SSAValue, ISSAValue] | None = None,
        block_map: dict[Block, IBlock] | None = None,
    ) -> IRegion:
        """
        Creates a new immutable region from a sequence of mutable blocks.
        The value_map and block_map are used to map already known correspondings
        of mutable values to immutable values and mutable blocks to immutable blocks.
        """
        if value_map is None:
            value_map = {}
        if block_map is None:
            block_map = {}

        # adding dummy block mappings so that ops have a successor to reference
        # when the actual block is created all successor references will be moved
        # to the correct block
        for block in blocks:
            block_map[block] = IBlock([], [])

        immutable_blocks = [
            IBlock.from_mutable(block, value_map, block_map) for block in blocks
        ]

        region = IRegion(immutable_blocks)

        # clean up successor references to blocks for ops inside this region
        for block, imm_block in zip(blocks, region.blocks):
            if block.parent is None:
                raise InvalidIRException(
                    "Cannot create an IRegion from a mutable Block "
                    "that is not attached to a Region."
                )
            dummy_block = block_map[block]
            for block in region.blocks:
                for op in block.ops:
                    if dummy_block in op.successors:
                        dummy_index = op.successors.index(dummy_block)
                        # replace dummy successor with actual successor
                        object.__setattr__(
                            op,
                            "successors",
                            IList(
                                op.successors[:dummy_index]
                                + [imm_block]
                                + op.successors[dummy_index + 1 :]
                            ),
                        )

        return region

    def to_mutable(
        self,
        value_mapping: dict[ISSAValue, SSAValue] | None = None,
        block_mapping: dict[IBlock, Block] | None = None,
    ) -> Region:
        """
        Returns a mutable region that is a copy of this immutable region.
        The value_mapping and block_mapping are used to map already known correspondings
        of immutable values to mutable values and immutable blocks to mutable blocks.
        """
        if value_mapping is None:
            value_mapping = {}
        if block_mapping is None:
            block_mapping = {}
        mutable_blocks: list[Block] = []
        # All mutable blocks have to be initialized first so that ops can
        # refer to them in their successor lists.
        for block in self.blocks:
            mutable_blocks.append(mutable_block := Block(arg_types=block.arg_types))
            block_mapping[block] = mutable_block
        for block in self.blocks:
            # This will use the already created Block and populate it
            block.to_mutable(value_mapping=value_mapping, block_mapping=block_mapping)
        return Region(mutable_blocks)

blocks: IList[IBlock] instance-attribute

Immutable blocks contained in the IRegion. The first block is the entry block.

block: IBlock property

Returns the block of a single-block region. Returns an exception if the region is not single-block.

ops: IList[IOperation] property

Get the operations of a single-block region. Returns an exception if the region is not single-block.

__hash__() -> int

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
89
90
def __hash__(self) -> int:
    return hash(id(self))

__eq__(value: object) -> bool

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
92
93
def __eq__(self, value: object) -> bool:
    return self is value

__init__(blocks: Sequence[IBlock])

Creates a new immutable region from a sequence of immutable blocks.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
121
122
123
124
125
def __init__(self, blocks: Sequence[IBlock]):
    """Creates a new immutable region from a sequence of immutable blocks."""

    object.__setattr__(self, "blocks", IList(blocks))
    self.blocks.freeze()

from_mutable(blocks: Iterable[Block], value_map: dict[SSAValue, ISSAValue] | None = None, block_map: dict[Block, IBlock] | None = None) -> IRegion classmethod

Creates a new immutable region from a sequence of mutable blocks. The value_map and block_map are used to map already known correspondings of mutable values to immutable values and mutable blocks to immutable blocks.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
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
@classmethod
def from_mutable(
    cls,
    blocks: Iterable[Block],
    value_map: dict[SSAValue, ISSAValue] | None = None,
    block_map: dict[Block, IBlock] | None = None,
) -> IRegion:
    """
    Creates a new immutable region from a sequence of mutable blocks.
    The value_map and block_map are used to map already known correspondings
    of mutable values to immutable values and mutable blocks to immutable blocks.
    """
    if value_map is None:
        value_map = {}
    if block_map is None:
        block_map = {}

    # adding dummy block mappings so that ops have a successor to reference
    # when the actual block is created all successor references will be moved
    # to the correct block
    for block in blocks:
        block_map[block] = IBlock([], [])

    immutable_blocks = [
        IBlock.from_mutable(block, value_map, block_map) for block in blocks
    ]

    region = IRegion(immutable_blocks)

    # clean up successor references to blocks for ops inside this region
    for block, imm_block in zip(blocks, region.blocks):
        if block.parent is None:
            raise InvalidIRException(
                "Cannot create an IRegion from a mutable Block "
                "that is not attached to a Region."
            )
        dummy_block = block_map[block]
        for block in region.blocks:
            for op in block.ops:
                if dummy_block in op.successors:
                    dummy_index = op.successors.index(dummy_block)
                    # replace dummy successor with actual successor
                    object.__setattr__(
                        op,
                        "successors",
                        IList(
                            op.successors[:dummy_index]
                            + [imm_block]
                            + op.successors[dummy_index + 1 :]
                        ),
                    )

    return region

to_mutable(value_mapping: dict[ISSAValue, SSAValue] | None = None, block_mapping: dict[IBlock, Block] | None = None) -> Region

Returns a mutable region that is a copy of this immutable region. The value_mapping and block_mapping are used to map already known correspondings of immutable values to mutable values and immutable blocks to mutable blocks.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def to_mutable(
    self,
    value_mapping: dict[ISSAValue, SSAValue] | None = None,
    block_mapping: dict[IBlock, Block] | None = None,
) -> Region:
    """
    Returns a mutable region that is a copy of this immutable region.
    The value_mapping and block_mapping are used to map already known correspondings
    of immutable values to mutable values and immutable blocks to mutable blocks.
    """
    if value_mapping is None:
        value_mapping = {}
    if block_mapping is None:
        block_mapping = {}
    mutable_blocks: list[Block] = []
    # All mutable blocks have to be initialized first so that ops can
    # refer to them in their successor lists.
    for block in self.blocks:
        mutable_blocks.append(mutable_block := Block(arg_types=block.arg_types))
        block_mapping[block] = mutable_block
    for block in self.blocks:
        # This will use the already created Block and populate it
        block.to_mutable(value_mapping=value_mapping, block_mapping=block_mapping)
    return Region(mutable_blocks)

IBlock dataclass

An immutable block contains a list of immutable operations. IBlocks are contained in IRegions.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
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
258
259
260
261
262
263
264
265
266
267
268
269
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
@dataclass(frozen=True)
class IBlock:
    """An immutable block contains a list of immutable operations. IBlocks are contained in IRegions."""

    args: IList[IBlockArg]
    ops: IList[IOperation]

    @property
    def arg_types(self) -> list[Attribute]:
        frozen_arg_types = [arg.type for arg in self.args]
        return frozen_arg_types

    def __hash__(self) -> int:
        return id(self)

    def __eq__(self, value: object) -> bool:
        return self is value

    def __repr__(self) -> str:
        return (
            "block of" + str(len(self.ops)) + " operations with args: " + str(self.args)
        )

    def __post_init__(self):
        for arg in self.args:
            object.__setattr__(arg, "block", self)

    def __init__(
        self, args: Sequence[Attribute] | Sequence[IBlockArg], ops: Sequence[IOperation]
    ):
        """Creates a new immutable block."""

        # Type Guards:
        def is_iblock_arg_seq(list: Sequence[Any]) -> TypeGuard[Sequence[IBlockArg]]:
            return all([isinstance(elem, IBlockArg) for elem in list])

        def is_type_seq(list: Sequence[Any]) -> TypeGuard[Sequence[Attribute]]:
            return all([isinstance(elem, Attribute) for elem in list])

        block_args: Sequence[IBlockArg]
        if is_type_seq(args):
            block_args = [
                IBlockArg(type, IList(()), self, idx) for idx, type in enumerate(args)
            ]
        elif is_iblock_arg_seq(args):
            block_args = args
            for block_arg in block_args:
                object.__setattr__(block_arg, "block", self)
        else:
            raise Exception("args for IBlock ill structured")

        object.__setattr__(self, "args", IList(block_args))
        object.__setattr__(self, "ops", IList(ops))

        self.args.freeze()
        self.ops.freeze()

    @classmethod
    def from_mutable(
        cls,
        block: Block,
        value_map: dict[SSAValue, ISSAValue] | None = None,
        block_map: dict[Block, IBlock] | None = None,
    ) -> IBlock:
        """
        Creates an immutable block from a mutable block.
        The value_map and block_map are used to map already known correspondings
        of mutable values to immutable values and mutable blocks to immutable blocks.
        """
        if value_map is None:
            value_map = {}
        if block_map is None:
            block_map = {}

        args: list[IBlockArg] = []
        for arg in block.args:
            # The IBlock that will house this IBlockArg is not constructed yet.
            # After construction the block field will be set by the IBlock.
            immutable_arg = IBlockArg(
                arg.type,
                IList(),
                cast(IBlock, None),
                arg.index,
            )
            args.append(immutable_arg)
            value_map[arg] = immutable_arg

        immutable_ops = [
            IOperation.from_mutable(
                op, value_map=value_map, block_map=block_map, existing_operands=None
            )
            for op in block.ops
        ]

        return IBlock(args, immutable_ops)

    def to_mutable(
        self,
        value_mapping: dict[ISSAValue, SSAValue] | None = None,
        block_mapping: dict[IBlock, Block] | None = None,
    ) -> Block:
        """
        Returns a mutable block that is a copy of this immutable block.
        The value_mapping and block_mapping are used to map already known correspondings
        of immutable values to mutable values and immutable blocks to mutable blocks.
        """
        if value_mapping is None:
            value_mapping = {}
        if block_mapping is None:
            block_mapping = {}

        # Block might already have been created by the Region, look it up
        if self in block_mapping:
            mutable_block = block_mapping[self]
        else:
            mutable_block = Block(arg_types=self.arg_types)
        for idx, arg in enumerate(self.args):
            value_mapping[arg] = mutable_block.args[idx]
        block_mapping[self] = mutable_block

        for immutable_op in self.ops:
            mutable_block.add_op(
                immutable_op.to_mutable(
                    value_mapping=value_mapping, block_mapping=block_mapping
                )
            )
        return mutable_block

args: IList[IBlockArg] instance-attribute

ops: IList[IOperation] instance-attribute

arg_types: list[Attribute] property

__hash__() -> int

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
219
220
def __hash__(self) -> int:
    return id(self)

__eq__(value: object) -> bool

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
222
223
def __eq__(self, value: object) -> bool:
    return self is value

__repr__() -> str

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
225
226
227
228
def __repr__(self) -> str:
    return (
        "block of" + str(len(self.ops)) + " operations with args: " + str(self.args)
    )

__post_init__()

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
230
231
232
def __post_init__(self):
    for arg in self.args:
        object.__setattr__(arg, "block", self)

__init__(args: Sequence[Attribute] | Sequence[IBlockArg], ops: Sequence[IOperation])

Creates a new immutable block.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
234
235
236
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
def __init__(
    self, args: Sequence[Attribute] | Sequence[IBlockArg], ops: Sequence[IOperation]
):
    """Creates a new immutable block."""

    # Type Guards:
    def is_iblock_arg_seq(list: Sequence[Any]) -> TypeGuard[Sequence[IBlockArg]]:
        return all([isinstance(elem, IBlockArg) for elem in list])

    def is_type_seq(list: Sequence[Any]) -> TypeGuard[Sequence[Attribute]]:
        return all([isinstance(elem, Attribute) for elem in list])

    block_args: Sequence[IBlockArg]
    if is_type_seq(args):
        block_args = [
            IBlockArg(type, IList(()), self, idx) for idx, type in enumerate(args)
        ]
    elif is_iblock_arg_seq(args):
        block_args = args
        for block_arg in block_args:
            object.__setattr__(block_arg, "block", self)
    else:
        raise Exception("args for IBlock ill structured")

    object.__setattr__(self, "args", IList(block_args))
    object.__setattr__(self, "ops", IList(ops))

    self.args.freeze()
    self.ops.freeze()

from_mutable(block: Block, value_map: dict[SSAValue, ISSAValue] | None = None, block_map: dict[Block, IBlock] | None = None) -> IBlock classmethod

Creates an immutable block from a mutable block. The value_map and block_map are used to map already known correspondings of mutable values to immutable values and mutable blocks to immutable blocks.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
264
265
266
267
268
269
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
@classmethod
def from_mutable(
    cls,
    block: Block,
    value_map: dict[SSAValue, ISSAValue] | None = None,
    block_map: dict[Block, IBlock] | None = None,
) -> IBlock:
    """
    Creates an immutable block from a mutable block.
    The value_map and block_map are used to map already known correspondings
    of mutable values to immutable values and mutable blocks to immutable blocks.
    """
    if value_map is None:
        value_map = {}
    if block_map is None:
        block_map = {}

    args: list[IBlockArg] = []
    for arg in block.args:
        # The IBlock that will house this IBlockArg is not constructed yet.
        # After construction the block field will be set by the IBlock.
        immutable_arg = IBlockArg(
            arg.type,
            IList(),
            cast(IBlock, None),
            arg.index,
        )
        args.append(immutable_arg)
        value_map[arg] = immutable_arg

    immutable_ops = [
        IOperation.from_mutable(
            op, value_map=value_map, block_map=block_map, existing_operands=None
        )
        for op in block.ops
    ]

    return IBlock(args, immutable_ops)

to_mutable(value_mapping: dict[ISSAValue, SSAValue] | None = None, block_mapping: dict[IBlock, Block] | None = None) -> Block

Returns a mutable block that is a copy of this immutable block. The value_mapping and block_mapping are used to map already known correspondings of immutable values to mutable values and immutable blocks to mutable blocks.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
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
def to_mutable(
    self,
    value_mapping: dict[ISSAValue, SSAValue] | None = None,
    block_mapping: dict[IBlock, Block] | None = None,
) -> Block:
    """
    Returns a mutable block that is a copy of this immutable block.
    The value_mapping and block_mapping are used to map already known correspondings
    of immutable values to mutable values and immutable blocks to mutable blocks.
    """
    if value_mapping is None:
        value_mapping = {}
    if block_mapping is None:
        block_mapping = {}

    # Block might already have been created by the Region, look it up
    if self in block_mapping:
        mutable_block = block_mapping[self]
    else:
        mutable_block = Block(arg_types=self.arg_types)
    for idx, arg in enumerate(self.args):
        value_mapping[arg] = mutable_block.args[idx]
    block_mapping[self] = mutable_block

    for immutable_op in self.ops:
        mutable_block.add_op(
            immutable_op.to_mutable(
                value_mapping=value_mapping, block_mapping=block_mapping
            )
        )
    return mutable_block

IOperation dataclass

Represents an immutable operation.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
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
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
488
489
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
@dataclass(frozen=True)
class IOperation:
    """Represents an immutable operation."""

    __match_args__ = ("op_type", "operands", "results", "successors", "regions")
    name: str
    op_type: type[Operation]
    properties: immutabledict[str, Attribute]
    attributes: immutabledict[str, Attribute]
    operands: IList[ISSAValue]
    results: IList[IOpResult]
    successors: IList[IBlock]
    regions: IList[IRegion]

    def __init__(
        self,
        name: str,
        op_type: type[Operation],
        attributes: immutabledict[str, Attribute],
        properties: immutabledict[str, Attribute],
        operands: Sequence[ISSAValue],
        result_types: Sequence[Attribute],
        successors: Sequence[IBlock],
        regions: Sequence[IRegion],
    ) -> None:
        object.__setattr__(self, "name", name)
        object.__setattr__(self, "op_type", op_type)
        object.__setattr__(self, "attributes", attributes)
        object.__setattr__(self, "properties", properties)
        object.__setattr__(self, "operands", IList(operands))
        for operand in operands:
            operand._add_user(self)  # pyright: ignore[reportPrivateUsage]
        object.__setattr__(
            self,
            "results",
            IList(
                [
                    IOpResult(type, IList(()), self, idx)
                    for idx, type in enumerate(result_types)
                ]
            ),
        )
        object.__setattr__(self, "successors", IList(successors))
        object.__setattr__(self, "regions", IList(regions))

        self.operands.freeze()
        self.results.freeze()
        self.successors.freeze()
        self.regions.freeze()

    @classmethod
    def get(
        cls,
        name: str,
        op_type: type[Operation],
        operands: Sequence[ISSAValue],
        result_types: Sequence[Attribute],
        attributes: immutabledict[str, Attribute],
        properties: immutabledict[str, Attribute],
        successors: Sequence[IBlock],
        regions: Sequence[IRegion],
    ) -> IOperation:
        return cls(
            name,
            op_type,
            properties,
            attributes,
            operands,
            result_types,
            successors,
            regions,
        )

    def __hash__(self) -> int:
        return hash(id(self))

    def __eq__(self, value: object, /) -> bool:
        return self is value

    @property
    def result(self) -> IOpResult:
        """
        Get the result of a of an IOperation with a single result.
        Returns an exception if the operation does not have exactly one result.
        """
        if len(self.results) != 1:
            raise ValueError(
                "'result' property of IOperation class is only available "
                "for IOperations with exactly one result."
            )
        return self.results[0]

    @property
    def region(self) -> IRegion:
        """
        Get the region of a of an IOperation with a single region.
        Returns an exception if the operation does not have exactly one region.
        """
        if len(self.regions) != 1:
            raise ValueError(
                "'region' property of IOperation class is only available "
                "for IOperations with exactly one region."
            )
        return self.regions[0]

    @property
    def result_types(self) -> list[Attribute]:
        return [result.type for result in self.results]

    def to_mutable(
        self,
        value_mapping: dict[ISSAValue, SSAValue] | None = None,
        block_mapping: dict[IBlock, Block] | None = None,
    ) -> Operation:
        """
        Returns a mutable operation that is a copy of this immutable operation.
        The value_mapping and block_mapping are used to map already known correspondings
        of immutable values to mutable values and immutable blocks to mutable blocks.
        """
        if value_mapping is None:
            value_mapping = {}
        if block_mapping is None:
            block_mapping = {}

        mutable_operands: list[SSAValue] = []
        for operand in self.operands:
            if operand in value_mapping:
                mutable_operands.append(value_mapping[operand])
            else:
                print(f"ERROR: op {self.name} uses SSAValue before definition")
                # Continuing to enable printing the IR including missing
                # operands for investigation
                mutable_operands.append(
                    OpResult(operand.type, cast(Operation, None), 0)
                )

        mutable_successors: list[Block] = []
        for successor in self.successors:
            if successor in block_mapping:
                mutable_successors.append(block_mapping[successor])
            else:
                raise InvalidIRException(
                    "Invalid IR: Block is not defined in the current region"
                )

        mutable_regions: list[Region] = []
        for region in self.regions:
            mutable_regions.append(
                region.to_mutable(
                    value_mapping=value_mapping, block_mapping=block_mapping
                )
            )

        new_op: Operation = self.op_type.create(
            operands=mutable_operands,
            result_types=[result.type for result in self.results],
            properties=dict(self.properties),
            attributes=dict(self.attributes),
            successors=mutable_successors,
            regions=mutable_regions,
        )

        # Add the results of this operation to the value mapping
        # so other operations can use them as operands.
        for idx, result in enumerate(self.results):
            m_result = new_op.results[idx]
            value_mapping[result] = m_result

        return new_op

    @classmethod
    def from_mutable(
        cls,
        op: Operation,
        value_map: dict[SSAValue, ISSAValue] | None = None,
        block_map: dict[Block, IBlock] | None = None,
        existing_operands: Sequence[ISSAValue] | None = None,
    ) -> IOperation:
        """
        Returns an immutable operation that is a copy of the given mutable operation.
        The value_map and block_map are used to map already known correspondings
        of mutable values to immutable values and mutable blocks to immutable blocks.
        """
        assert isinstance(op, Operation)
        op_type = op.__class__

        if value_map is None:
            value_map = {}
        if block_map is None:
            block_map = {}

        operands: list[ISSAValue] = []
        if existing_operands is None:
            for operand in op.operands:
                if isinstance(operand, OpResult):
                    if operand in value_map:
                        operands.append(value_map[operand])
                    else:
                        raise Exception("Operand used before definition")
                elif isinstance(operand, BlockArgument):
                    if operand not in value_map:
                        raise Exception(
                            "Block argument expected in mapping for op: " + op.name
                        )
                    operands.append(value_map[operand])
                else:
                    raise Exception(
                        "Operand is expected to be either OpResult or BlockArgument"
                    )
        else:
            operands.extend(existing_operands)

        properties: immutabledict[str, Attribute] = immutabledict(op.properties)
        attributes: immutabledict[str, Attribute] = immutabledict(op.attributes)

        successors: list[IBlock] = []
        for successor in op.successors:
            if successor in block_map:
                successors.append(block_map[successor])
            else:
                raise Exception(
                    "Successor not defined in current region, `from_mutable`\
                          probably has to be called on the parent operation."
                )

        regions: list[IRegion] = []
        for region in op.regions:
            regions.append(IRegion.from_mutable(region.blocks, value_map, block_map))

        immutable_op = IOperation.get(
            op.name,
            op_type,
            operands,
            op.result_types,
            properties,
            attributes,
            successors,
            regions,
        )

        for idx, result in enumerate(op.results):
            value_map[result] = immutable_op.results[idx]

        return immutable_op

    def get_attribute(self, name: str) -> Attribute:
        return self.attributes[name]

    def get_attributes_copy(self) -> dict[str, Attribute]:
        return dict(self.attributes)

__match_args__ = ('op_type', 'operands', 'results', 'successors', 'regions') class-attribute instance-attribute

name: str instance-attribute

op_type: type[Operation] instance-attribute

properties: immutabledict[str, Attribute] instance-attribute

attributes: immutabledict[str, Attribute] instance-attribute

operands: IList[ISSAValue] instance-attribute

results: IList[IOpResult] instance-attribute

successors: IList[IBlock] instance-attribute

regions: IList[IRegion] instance-attribute

result: IOpResult property

Get the result of a of an IOperation with a single result. Returns an exception if the operation does not have exactly one result.

region: IRegion property

Get the region of a of an IOperation with a single region. Returns an exception if the operation does not have exactly one region.

result_types: list[Attribute] property

__init__(name: str, op_type: type[Operation], attributes: immutabledict[str, Attribute], properties: immutabledict[str, Attribute], operands: Sequence[ISSAValue], result_types: Sequence[Attribute], successors: Sequence[IBlock], regions: Sequence[IRegion]) -> None

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
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
def __init__(
    self,
    name: str,
    op_type: type[Operation],
    attributes: immutabledict[str, Attribute],
    properties: immutabledict[str, Attribute],
    operands: Sequence[ISSAValue],
    result_types: Sequence[Attribute],
    successors: Sequence[IBlock],
    regions: Sequence[IRegion],
) -> None:
    object.__setattr__(self, "name", name)
    object.__setattr__(self, "op_type", op_type)
    object.__setattr__(self, "attributes", attributes)
    object.__setattr__(self, "properties", properties)
    object.__setattr__(self, "operands", IList(operands))
    for operand in operands:
        operand._add_user(self)  # pyright: ignore[reportPrivateUsage]
    object.__setattr__(
        self,
        "results",
        IList(
            [
                IOpResult(type, IList(()), self, idx)
                for idx, type in enumerate(result_types)
            ]
        ),
    )
    object.__setattr__(self, "successors", IList(successors))
    object.__setattr__(self, "regions", IList(regions))

    self.operands.freeze()
    self.results.freeze()
    self.successors.freeze()
    self.regions.freeze()

get(name: str, op_type: type[Operation], operands: Sequence[ISSAValue], result_types: Sequence[Attribute], attributes: immutabledict[str, Attribute], properties: immutabledict[str, Attribute], successors: Sequence[IBlock], regions: Sequence[IRegion]) -> IOperation classmethod

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
@classmethod
def get(
    cls,
    name: str,
    op_type: type[Operation],
    operands: Sequence[ISSAValue],
    result_types: Sequence[Attribute],
    attributes: immutabledict[str, Attribute],
    properties: immutabledict[str, Attribute],
    successors: Sequence[IBlock],
    regions: Sequence[IRegion],
) -> IOperation:
    return cls(
        name,
        op_type,
        properties,
        attributes,
        operands,
        result_types,
        successors,
        regions,
    )

__hash__() -> int

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
413
414
def __hash__(self) -> int:
    return hash(id(self))

__eq__(value: object) -> bool

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
416
417
def __eq__(self, value: object, /) -> bool:
    return self is value

to_mutable(value_mapping: dict[ISSAValue, SSAValue] | None = None, block_mapping: dict[IBlock, Block] | None = None) -> Operation

Returns a mutable operation that is a copy of this immutable operation. The value_mapping and block_mapping are used to map already known correspondings of immutable values to mutable values and immutable blocks to mutable blocks.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
def to_mutable(
    self,
    value_mapping: dict[ISSAValue, SSAValue] | None = None,
    block_mapping: dict[IBlock, Block] | None = None,
) -> Operation:
    """
    Returns a mutable operation that is a copy of this immutable operation.
    The value_mapping and block_mapping are used to map already known correspondings
    of immutable values to mutable values and immutable blocks to mutable blocks.
    """
    if value_mapping is None:
        value_mapping = {}
    if block_mapping is None:
        block_mapping = {}

    mutable_operands: list[SSAValue] = []
    for operand in self.operands:
        if operand in value_mapping:
            mutable_operands.append(value_mapping[operand])
        else:
            print(f"ERROR: op {self.name} uses SSAValue before definition")
            # Continuing to enable printing the IR including missing
            # operands for investigation
            mutable_operands.append(
                OpResult(operand.type, cast(Operation, None), 0)
            )

    mutable_successors: list[Block] = []
    for successor in self.successors:
        if successor in block_mapping:
            mutable_successors.append(block_mapping[successor])
        else:
            raise InvalidIRException(
                "Invalid IR: Block is not defined in the current region"
            )

    mutable_regions: list[Region] = []
    for region in self.regions:
        mutable_regions.append(
            region.to_mutable(
                value_mapping=value_mapping, block_mapping=block_mapping
            )
        )

    new_op: Operation = self.op_type.create(
        operands=mutable_operands,
        result_types=[result.type for result in self.results],
        properties=dict(self.properties),
        attributes=dict(self.attributes),
        successors=mutable_successors,
        regions=mutable_regions,
    )

    # Add the results of this operation to the value mapping
    # so other operations can use them as operands.
    for idx, result in enumerate(self.results):
        m_result = new_op.results[idx]
        value_mapping[result] = m_result

    return new_op

from_mutable(op: Operation, value_map: dict[SSAValue, ISSAValue] | None = None, block_map: dict[Block, IBlock] | None = None, existing_operands: Sequence[ISSAValue] | None = None) -> IOperation classmethod

Returns an immutable operation that is a copy of the given mutable operation. The value_map and block_map are used to map already known correspondings of mutable values to immutable values and mutable blocks to immutable blocks.

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
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
@classmethod
def from_mutable(
    cls,
    op: Operation,
    value_map: dict[SSAValue, ISSAValue] | None = None,
    block_map: dict[Block, IBlock] | None = None,
    existing_operands: Sequence[ISSAValue] | None = None,
) -> IOperation:
    """
    Returns an immutable operation that is a copy of the given mutable operation.
    The value_map and block_map are used to map already known correspondings
    of mutable values to immutable values and mutable blocks to immutable blocks.
    """
    assert isinstance(op, Operation)
    op_type = op.__class__

    if value_map is None:
        value_map = {}
    if block_map is None:
        block_map = {}

    operands: list[ISSAValue] = []
    if existing_operands is None:
        for operand in op.operands:
            if isinstance(operand, OpResult):
                if operand in value_map:
                    operands.append(value_map[operand])
                else:
                    raise Exception("Operand used before definition")
            elif isinstance(operand, BlockArgument):
                if operand not in value_map:
                    raise Exception(
                        "Block argument expected in mapping for op: " + op.name
                    )
                operands.append(value_map[operand])
            else:
                raise Exception(
                    "Operand is expected to be either OpResult or BlockArgument"
                )
    else:
        operands.extend(existing_operands)

    properties: immutabledict[str, Attribute] = immutabledict(op.properties)
    attributes: immutabledict[str, Attribute] = immutabledict(op.attributes)

    successors: list[IBlock] = []
    for successor in op.successors:
        if successor in block_map:
            successors.append(block_map[successor])
        else:
            raise Exception(
                "Successor not defined in current region, `from_mutable`\
                      probably has to be called on the parent operation."
            )

    regions: list[IRegion] = []
    for region in op.regions:
        regions.append(IRegion.from_mutable(region.blocks, value_map, block_map))

    immutable_op = IOperation.get(
        op.name,
        op_type,
        operands,
        op.result_types,
        properties,
        attributes,
        successors,
        regions,
    )

    for idx, result in enumerate(op.results):
        value_map[result] = immutable_op.results[idx]

    return immutable_op

get_attribute(name: str) -> Attribute

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
585
586
def get_attribute(self, name: str) -> Attribute:
    return self.attributes[name]

get_attributes_copy() -> dict[str, Attribute]

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
588
589
def get_attributes_copy(self) -> dict[str, Attribute]:
    return dict(self.attributes)

get_immutable_copy(op: Operation) -> IOperation

Source code in xdsl/rewriting/composable_rewriting/immutable_ir/immutable_ir.py
336
337
def get_immutable_copy(op: Operation) -> IOperation:
    return IOperation.from_mutable(op)