Last active
January 1, 2026 22:44
-
-
Save sueszli/ac533adfadd3495b8b9a14833d76a9e9 to your computer and use it in GitHub Desktop.
Drop `scf.if` if it only has an `scf.yield`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from xdsl.dialects import scf, arith, func | |
| from xdsl.ir import Block, Region | |
| from xdsl.context import Context | |
| from xdsl.pattern_rewriter import PatternRewriteWalker, PatternRewriter, GreedyRewritePatternApplier | |
| from xdsl.transforms.canonicalization_patterns.scf import IfPropagateConstantCondition | |
| from xdsl.printer import Printer | |
| ctx = Context() | |
| ctx.load_dialect(scf.Scf) | |
| ctx.load_dialect(arith.Arith) | |
| ctx.load_dialect(func.Func) | |
| true_op = arith.ConstantOp.from_int_and_width(1, 1) | |
| if_op = scf.IfOp(true_op.result, [], [Block()]) | |
| if_op.true_region.block.add_op(scf.YieldOp()) | |
| func_op = func.FuncOp("test", ([], []), Region([Block([true_op, if_op])])) | |
| print("----- before canonicalization:") | |
| printer = Printer() | |
| printer.print_op(func_op) | |
| print("\n") | |
| pattern = IfPropagateConstantCondition() | |
| applier = GreedyRewritePatternApplier([pattern]) | |
| walker = PatternRewriteWalker(applier) | |
| walker.rewrite_module(func_op) | |
| print("----- after canonicalization:") | |
| printer.print_op(func_op) | |
| print("\n") | |
| """ | |
| ----- before canonicalization: | |
| func.func @test() { | |
| %0 = arith.constant true | |
| scf.if %0 { | |
| } | |
| } | |
| ----- after canonicalization: | |
| func.func @test() { | |
| %0 = arith.constant true | |
| } | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment