Created
December 29, 2025 15:50
-
-
Save sueszli/25f73f9a87c00b3e64164afbfa2d261d to your computer and use it in GitHub Desktop.
this breaks if we don't drop the ELSE region when cond==false
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.context import Context | |
| from xdsl.dialects import scf, arith, func, builtin | |
| from xdsl.dialects.builtin import ModuleOp | |
| from xdsl.ir import Block, Region | |
| from xdsl.transforms.canonicalization_patterns.scf import IfPropagateConstantCondition | |
| from xdsl.pattern_rewriter import PatternRewriteWalker | |
| ctx = Context() | |
| ctx.load_dialect(scf.Scf) | |
| ctx.load_dialect(arith.Arith) | |
| ctx.load_dialect(func.Func) | |
| ctx.load_dialect(builtin.Builtin) | |
| false_val = arith.ConstantOp.from_int_and_width(0, 1) | |
| val_1 = arith.ConstantOp.from_int_and_width(1, 32) | |
| true_block = Block([val_1, scf.YieldOp(val_1)]) | |
| true_region = Region(true_block) | |
| if_op = scf.IfOp(false_val, [], true_region) # no else region | |
| func_body = Region(Block([false_val, if_op, func.ReturnOp()])) | |
| func_op = func.FuncOp("test_false_no_else", ([], []), func_body) | |
| module = ModuleOp([func_op]) | |
| print("\nbefore:\n") | |
| print(module) | |
| # builtin.module { | |
| # func.func @test_false_no_else() { | |
| # %0 = arith.constant false | |
| # scf.if %0 { | |
| # %1 = arith.constant 1 : i32 | |
| # } | |
| # func.return | |
| # } | |
| # } | |
| walker = PatternRewriteWalker(IfPropagateConstantCondition()) # avoid DCE | |
| walker.rewrite_module(module) | |
| print("\nafter:\n") | |
| print(module) | |
| # builtin.module { | |
| # func.func @test_false_no_else() { | |
| # %0 = arith.constant false | |
| # func.return | |
| # } | |
| # } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment