Pattern rewriter
pattern_rewriter
PatternRewriterListener
dataclass
Bases: BuilderListener
A listener for pattern rewriter events.
Source code in xdsl/pattern_rewriter.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
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.
Source code in xdsl/pattern_rewriter.py
57 58 59 60 | |
handle_operation_modification(op: Operation) -> None
Pass the operation that was just modified to the registered callbacks.
Source code in xdsl/pattern_rewriter.py
62 63 64 65 | |
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
67 68 69 70 71 72 | |
extend_from_listener(listener: BuilderListener | PatternRewriterListener)
Forward all callbacks from listener to this listener.
Source code in xdsl/pattern_rewriter.py
74 75 76 77 78 79 80 81 82 83 84 | |
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
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 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 350 | |
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 = current_operation
instance-attribute
The matched operation.
__init__(current_operation: Operation)
Source code in xdsl/pattern_rewriter.py
125 126 127 128 | |
insert_op(op: InsertOpInvT, insertion_point: InsertPoint | None = None) -> InsertOpInvT
Insert operations at a certain location in a block.
Source code in xdsl/pattern_rewriter.py
130 131 132 133 134 135 136 137 | |
insert_op_before_matched_op(op: InsertOpInvT) -> InsertOpInvT
Insert operations before the matched operation.
Source code in xdsl/pattern_rewriter.py
139 140 141 142 143 144 | |
insert_op_after_matched_op(op: InsertOpInvT) -> InsertOpInvT
Insert operations after the matched operation.
Source code in xdsl/pattern_rewriter.py
146 147 148 149 150 151 | |
erase_matched_op(safe_erase: bool = True)
Erase the operation that was matched to. 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
153 154 155 156 157 158 159 160 | |
erase_op(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
162 163 164 165 166 167 168 169 170 | |
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
172 173 174 175 176 177 178 179 180 181 182 | |
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
184 185 186 187 188 189 190 191 192 193 194 195 | |
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
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
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. 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
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 | |
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
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
insert_block_argument(block: Block, index: int, arg_type: Attribute) -> BlockArgument
Insert a new block argument.
Source code in xdsl/pattern_rewriter.py
281 282 283 284 285 286 | |
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
288 289 290 291 292 293 294 295 296 | |
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
298 299 300 301 302 303 304 305 306 307 308 | |
inline_block_before_matched_op(block: Block, arg_values: Sequence[SSAValue] = ())
Move the block operations before the matched operation. The block should not be a parent of the operation.
Source code in xdsl/pattern_rewriter.py
310 311 312 313 314 315 316 317 318 319 320 | |
inline_block_after_matched_op(block: Block, arg_values: Sequence[SSAValue] = ())
Move the block operations after the matched operation. The block should not be a parent of the operation.
Source code in xdsl/pattern_rewriter.py
322 323 324 325 326 327 328 329 330 331 332 | |
move_region_contents_to_new_regions(region: Region) -> Region
Move the region blocks to a new region.
Source code in xdsl/pattern_rewriter.py
334 335 336 337 | |
inline_region(region: Region, insertion_point: BlockInsertPoint) -> None
Move the region blocks to the specified insertion point.
Source code in xdsl/pattern_rewriter.py
339 340 341 342 | |
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
344 345 346 347 348 349 350 | |
RewritePattern
Bases: ABC
A side-effect free rewrite pattern matching on a DAG.
Source code in xdsl/pattern_rewriter.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
match_and_rewrite(op: Operation, rewriter: PatternRewriter)
abstractmethod
Match an operation, and optionally perform a rewrite using the rewriter.
Source code in xdsl/pattern_rewriter.py
361 362 363 364 365 366 | |
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
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 | |
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
455 456 457 458 459 460 461 462 463 464 | |
match_and_rewrite(op: Operation, rewriter: PatternRewriter)
Pattern application implementation
Source code in xdsl/pattern_rewriter.py
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 | |
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
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 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 | |
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
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 | |
Worklist
dataclass
Source code in xdsl/pattern_rewriter.py
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 | |
__init__() -> None
is_empty() -> bool
Check if the worklist is empty.
Source code in xdsl/pattern_rewriter.py
658 659 660 661 662 | |
push(op: Operation)
Push an operation to the end of the worklist, if it is not already in it.
Source code in xdsl/pattern_rewriter.py
664 665 666 667 668 669 670 | |
pop() -> Operation | None
Pop the operation at the end of the worklist.
Source code in xdsl/pattern_rewriter.py
672 673 674 675 676 677 678 679 680 681 682 683 | |
remove(op: Operation)
Remove an operation from the worklist.
Source code in xdsl/pattern_rewriter.py
685 686 687 688 689 690 | |
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
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 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 | |
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.
Source code in xdsl/pattern_rewriter.py
802 803 804 805 806 807 | |
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
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | |
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
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 | |
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
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
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.
Source code in xdsl/pattern_rewriter.py
567 568 569 570 571 572 573 574 575 576 577 578 | |