|  | 
| 2 | 2 | 
 | 
| 3 | 3 | from kirin import ir, types, rewrite | 
| 4 | 4 | from kirin.passes import aggressive | 
| 5 |  | -from kirin.prelude import basic_no_opt, python_basic | 
|  | 5 | +from kirin.prelude import structural, basic_no_opt, python_basic | 
| 6 | 6 | from kirin.analysis import const | 
| 7 | 7 | from kirin.dialects import py, func, ilist, lowering | 
| 8 | 8 | from kirin.passes.typeinfer import TypeInfer | 
| @@ -328,6 +328,46 @@ def test_ilist_flatten_add_both_new(): | 
| 328 | 328 |     assert test_block.is_equal(expected_block) | 
| 329 | 329 | 
 | 
| 330 | 330 | 
 | 
|  | 331 | +def test_region_boundary_structural(): | 
|  | 332 | + | 
|  | 333 | +    # Do not optimize across region boundary like if-else or basic blocks | 
|  | 334 | +    @structural | 
|  | 335 | +    def test_impl(n: int): | 
|  | 336 | +        a = ilist.IList([]) | 
|  | 337 | + | 
|  | 338 | +        if n > 0: | 
|  | 339 | +            a = a + [n] | 
|  | 340 | + | 
|  | 341 | +        return a | 
|  | 342 | + | 
|  | 343 | +    expected_impl = test_impl.similar() | 
|  | 344 | +    test_impl.print(hint="const") | 
|  | 345 | +    rule = rewrite.Walk(ilist.rewrite.FlattenAdd()) | 
|  | 346 | +    rule.rewrite(test_impl.code) | 
|  | 347 | + | 
|  | 348 | +    assert test_impl.code.is_structurally_equal(expected_impl.code) | 
|  | 349 | + | 
|  | 350 | + | 
|  | 351 | +def test_region_boundary(): | 
|  | 352 | + | 
|  | 353 | +    # Do not optimize across region boundary like if-else or basic blocks | 
|  | 354 | +    @basic_no_opt | 
|  | 355 | +    def test_impl(n: int): | 
|  | 356 | +        a = ilist.IList([]) | 
|  | 357 | + | 
|  | 358 | +        if n > 0: | 
|  | 359 | +            a = a + [n] | 
|  | 360 | + | 
|  | 361 | +        return a | 
|  | 362 | + | 
|  | 363 | +    expected_impl = test_impl.similar() | 
|  | 364 | +    test_impl.print(hint="const") | 
|  | 365 | +    rule = rewrite.Walk(ilist.rewrite.FlattenAdd()) | 
|  | 366 | +    rule.rewrite(test_impl.code) | 
|  | 367 | + | 
|  | 368 | +    assert test_impl.code.is_structurally_equal(expected_impl.code) | 
|  | 369 | + | 
|  | 370 | + | 
| 331 | 371 | def test_ilist_constprop(): | 
| 332 | 372 |     from kirin.analysis import const | 
| 333 | 373 | 
 | 
|  | 
0 commit comments