Pattern rewriter
pattern_rewriter
¶
PatternRewriterListener
dataclass
¶
Bases: BuilderListener
A listener for pattern rewriter events.
Source code in xdsl/pattern_rewriter.py
operation_removal_handler: list[Callable[[Operation], None]] = field(default_factory=list[Callable[[Operation], None]], kw_only=True)
class-attribute
instance-attribute
¶
Callbacks that are called when an operation is removed.
operation_modification_handler: list[Callable[[Operation], None]] = field(default_factory=list[Callable[[Operation], None]], kw_only=True)
class-attribute
instance-attribute
¶
Callbacks that are called when an operation is modified.
operation_replacement_handler: list[Callable[[Operation, Sequence[SSAValue | None]], None]] = field(default_factory=list[Callable[[Operation, Sequence[SSAValue | None]], None]], kw_only=True)
class-attribute
instance-attribute
¶
Callbacks that are called when an operation is replaced.
__init__(*, operation_insertion_handler: list[Callable[[Operation], None]] = list[Callable[[Operation], None]](), block_creation_handler: list[Callable[[Block], None]] = list[Callable[[Block], None]](), operation_removal_handler: list[Callable[[Operation], None]] = list[Callable[[Operation], None]](), operation_modification_handler: list[Callable[[Operation], None]] = list[Callable[[Operation], None]](), operation_replacement_handler: list[Callable[[Operation, Sequence[SSAValue | None]], None]] = list[Callable[[Operation, Sequence[SSAValue | None]], None]]()) -> None
¶
handle_operation_removal(op: Operation) -> None
¶
Pass the operation that will be removed to the registered callbacks.
handle_operation_modification(op: Operation) -> None
¶
Pass the operation that was just modified to the registered callbacks.
handle_operation_replacement(op: Operation, new_results: Sequence[SSAValue | None]) -> None
¶
Pass the operation that will be replaced to the registered callbacks.
Source code in xdsl/pattern_rewriter.py
extend_from_listener(listener: BuilderListener | PatternRewriterListener)
¶
Forward all callbacks from listener to this listener.
Source code in xdsl/pattern_rewriter.py
PatternRewriter
dataclass
¶
Bases: Builder, PatternRewriterListener
A rewriter used during pattern matching. Once an operation is matched, this rewriter is used to apply modification to the operation and its children.
Source code in xdsl/pattern_rewriter.py
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 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 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
has_done_action: bool = field(default=False, init=False)
class-attribute
instance-attribute
¶
Has the rewriter done any action during the current match.
current_operation: Operation
property
writable
¶
The matched operation.
__init__(insert_point: Operation | InsertPoint)
¶
Source code in xdsl/pattern_rewriter.py
insert(op: InsertOpInvT, insertion_point: InsertPoint | None = None) -> InsertOpInvT
¶
Insert operations at a certain location in a block.
erase(op: Operation, safe_erase: bool = True)
¶
Erase an operation. If safe_erase is True, check that the operation has no uses. Otherwise, replace its uses with ErasedSSAValue.
Source code in xdsl/pattern_rewriter.py
erase_op(op: Operation, safe_erase: bool = True)
¶
replace_all_uses_with(from_value: SSAValue, to_value: SSAValue | None, safe_erase: bool = True)
¶
Replace all uses of old with new.
Source code in xdsl/pattern_rewriter.py
replace_uses_with_if(from_value: SSAValue, to_value: SSAValue, predicate: Callable[[Use], bool])
¶
Replace uses of old satisfying predicate with new.
Source code in xdsl/pattern_rewriter.py
replace_matched_op(new_ops: Operation | Sequence[Operation], new_results: Sequence[SSAValue | None] | None = None, safe_erase: bool = True)
¶
Replace the matched operation with new operations. Also, optionally specify SSA values to replace the operation results. If safe_erase is True, check that the operation has no uses. Otherwise, replace its uses with ErasedSSAValue.
Source code in xdsl/pattern_rewriter.py
replace(op: Operation, new_ops: Operation | Sequence[Operation], new_results: Sequence[SSAValue | None] | None = None, safe_erase: bool = True)
¶
Replace an operation with new operations. Also, optionally specify SSA values to replace the operation results. If safe_erase is True, check that the operation has no uses. Otherwise, replace its uses with ErasedSSAValue.
Source code in xdsl/pattern_rewriter.py
replace_op(op: Operation, new_ops: Operation | Sequence[Operation], new_results: Sequence[SSAValue | None] | None = None, safe_erase: bool = True)
¶
Replace an operation with new operations.
Source code in xdsl/pattern_rewriter.py
replace_value_with_new_type(val: SSAValue, new_type: Attribute) -> SSAValue
¶
Replace a value with a value of a new type, and return the new value. This will insert the new value in the operation or block, and remove the existing value.
Source code in xdsl/pattern_rewriter.py
insert_block_argument(block: Block, index: int, arg_type: Attribute) -> BlockArgument
¶
erase_block_argument(arg: BlockArgument, safe_erase: bool = True) -> None
¶
Erase a new block argument. If safe_erase is true, then raise an exception if the block argument has still uses, otherwise, replace it with an ErasedSSAValue.
Source code in xdsl/pattern_rewriter.py
inline_block(block: Block, insertion_point: InsertPoint, arg_values: Sequence[SSAValue] = ())
¶
Move the block operations to the specified insertion point.
Source code in xdsl/pattern_rewriter.py
move_region_contents_to_new_regions(region: Region) -> Region
¶
Move the region blocks to a new region.
inline_region(region: Region, insertion_point: BlockInsertPoint) -> None
¶
Move the region blocks to the specified insertion point.
notify_op_modified(op: Operation) -> None
¶
Notify the rewriter that an operation was modified in the pattern. This will correctly update the rewriter state.
Source code in xdsl/pattern_rewriter.py
RewritePattern
¶
Bases: ABC
A side-effect free rewrite pattern matching on a DAG.
Source code in xdsl/pattern_rewriter.py
match_and_rewrite(op: Operation, rewriter: PatternRewriter)
abstractmethod
¶
Match an operation, and optionally perform a rewrite using the rewriter.
TypeConversionPattern
dataclass
¶
Bases: RewritePattern
Base pattern for type conversion. It is supposed to be inherited from, then one can
implement convert_type to define the conversion.
It will convert an Operations' result types, dictionary attributes, and block arguments.
One can use @attr_type_rewrite_pattern on this defined method to automatically filter
on the Attribute type used.
This base pattern defines two flags:
recursive(defaulting to False): recurse over structured attributes to convert parameters. e.g. a recusrivei32toindexconversion will convertvector<i32>tovector<index>.ops(defaulting to any Operation) is a tuple of Operation types on which to apply the defined attribute conversion.
Source code in xdsl/pattern_rewriter.py
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 | |
recursive: bool = False
class-attribute
instance-attribute
¶
recurse over structured attributes to convert parameters. Defaults to False.
ops: tuple[type[Operation], ...] | None = None
class-attribute
instance-attribute
¶
A tuple of Operation types on which to apply the defined attribute conversion. Defaults to any operation type.
__init__(recursive: bool = False, ops: tuple[type[Operation], ...] | None = None) -> None
¶
convert_type(typ: Attribute) -> Attribute | None
abstractmethod
¶
The method to implement to define a TypeConversionPattern
This defines how the input Attribute should be converted. It allows returning None, meaning "this attribute should not be converted".
Source code in xdsl/pattern_rewriter.py
match_and_rewrite(op: Operation, rewriter: PatternRewriter)
¶
Pattern application implementation
Source code in xdsl/pattern_rewriter.py
GreedyRewritePatternApplier
dataclass
¶
Bases: RewritePattern
Apply a list of patterns in order until one pattern matches, and then use this rewrite. By default, the applier attempts to fold the operation first. If the operation is trivially dead, it is erased.
Source code in xdsl/pattern_rewriter.py
rewrite_patterns: list[RewritePattern]
instance-attribute
¶
The list of rewrites to apply in order.
ctx: Context | None = field(default=None)
class-attribute
instance-attribute
¶
Used to materialize constant operations when folding.
folding_enabled: bool = field(default=False, kw_only=True)
class-attribute
instance-attribute
¶
Whether the folders should be invoked. If this is True, the GreedyRewritePatternApplier must also have a context.
dce_enabled: bool = field(default=True, kw_only=True)
class-attribute
instance-attribute
¶
Whether trivial dead code elimination should be run on all operations before attempting to rewrite them.
__init__(rewrite_patterns: list[RewritePattern], ctx: Context | None = None, *, folding_enabled: bool = False, dce_enabled: bool = True) -> None
¶
match_and_rewrite(op: Operation, rewriter: PatternRewriter) -> None
¶
Source code in xdsl/pattern_rewriter.py
PatternRewriteWalker
dataclass
¶
Walks the IR in the block and instruction order, and rewrite it in place. Previous references to the walked operations are invalid after the walk. Can walk either first the regions, or first the owner operation. The walker will also walk recursively on the created operations.
Source code in xdsl/pattern_rewriter.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 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 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 | |
pattern: RewritePattern
instance-attribute
¶
Pattern to apply during the walk.
walk_regions_first: bool = field(default=False)
class-attribute
instance-attribute
¶
Choose if the walker should first walk the operation regions first, or the operation itself.
apply_recursively: bool = field(default=True)
class-attribute
instance-attribute
¶
Apply recursively rewrites on new operations.
walk_reverse: bool = field(default=False)
class-attribute
instance-attribute
¶
Walk the regions and blocks in reverse order. That way, all uses are replaced before the definitions.
post_walk_func: Callable[[Region, PatternRewriterListener], bool] | None = field(default=None)
class-attribute
instance-attribute
¶
Function to call between each walk of the IR.
listener: PatternRewriterListener = field(default_factory=PatternRewriterListener)
class-attribute
instance-attribute
¶
The listener that will be called when an operation or block is modified.
__init__(pattern: RewritePattern, walk_regions_first: bool = False, apply_recursively: bool = True, walk_reverse: bool = False, post_walk_func: Callable[[Region, PatternRewriterListener], bool] | None = None, listener: PatternRewriterListener = PatternRewriterListener()) -> None
¶
rewrite_module(module: ModuleOp) -> bool
¶
Rewrite operations nested in the given operation by repeatedly applying the
pattern. Returns True if the IR was mutated.
rewrite_region(region: Region) -> bool
¶
Rewrite operations nested in the given operation by repeatedly applying the
pattern. Returns True if the IR was mutated.
Source code in xdsl/pattern_rewriter.py
op_type_rewrite_pattern(func: Callable[[_RewritePatternT, _OperationT, PatternRewriter], None]) -> Callable[[_RewritePatternT, Operation, PatternRewriter], None]
¶
This function is intended to be used as a decorator on a RewritePatter method. It uses type hints to match on a specific operation type before calling the decorated function.
Source code in xdsl/pattern_rewriter.py
attr_constr_rewrite_pattern(constr: AttrConstraint[_AttributeT]) -> Callable[[Callable[[_TypeConversionPatternT, _AttributeT], Attribute | None]], Callable[[_TypeConversionPatternT, Attribute], Attribute | None]]
¶
This function is intended to be used as a decorator on a TypeConversionPattern method. It uses the passed constraint to match on a specific attribute type before calling the decorated function.
Source code in xdsl/pattern_rewriter.py
attr_type_rewrite_pattern(func: Callable[[_TypeConversionPatternT, _AttributeT], Attribute | None]) -> Callable[[_TypeConversionPatternT, Attribute], Attribute | None]
¶
This function is intended to be used as a decorator on a TypeConversionPattern method. It uses type hints to match on a specific attribute type before calling the decorated function.