|
3 | 3 | on [GitHub](https://github.com/QuEraComputing/kirin/issues/new) if you need help or want to |
4 | 4 | contribute. |
5 | 5 |
|
| 6 | +# The Control Flow Dialect |
| 7 | + |
| 8 | +The control flow dialect provides the most generic control flow semantics via [`cf.Branch`][kirin.dialects.cf.Branch] and [`cf.ConditionalBranch`][kirin.dialects.cf.ConditionalBranch]. |
| 9 | + |
| 10 | +## `cf.Branch` |
| 11 | + |
| 12 | +the [`cf.Branch`][kirin.dialects.cf.Branch] statement is used to mark how basic block branches to another basic block without condition. This represents an edge on the control flow graph (CFG). |
| 13 | + |
| 14 | +```mlir |
| 15 | +^1(%2): |
| 16 | + │ %y = py.constant.constant 1 : !py.int |
| 17 | + │ cf.br ^3(%y) |
| 18 | + ^2(%3): |
| 19 | + │ %y_1 = py.constant.constant 2 : !py.int |
| 20 | + │ cf.br ^3(%y_1) |
| 21 | + ^3(%y_2): |
| 22 | + │ func.return %y_2 |
| 23 | +``` |
| 24 | + |
| 25 | +**Definition** the `cf.Branch` statement takes a successor block and its argument. The `cf.Branch` is a terminator thus it should always be the last statement of a block. |
| 26 | + |
| 27 | +!!! note |
| 28 | + [`ir.Statement`][kirin.ir.Statement] does not own any [`ir.Block`][kirin.ir.Block], the [`ir.Region`][kirin.ir.Region] owns blocks. The [`ir.Statement`][kirin.ir.Statement] will only own [`ir.Region`][kirin.ir.Region]. In Kirin, we use similar design as LLVM/MLIR where the phi nodes in SSA form are replaced by block arguments. |
| 29 | + |
| 30 | +## `cf.ConditionalBranch` |
| 31 | + |
| 32 | +The [`cf.ConditionalBranch`][kirin.dialects.cf.ConditionalBranch] statement represents a conditional branching statement that looks like following (the `cf.cond_br` statement): |
| 33 | + |
| 34 | +```mlir |
| 35 | +^0(%main_self, %x): |
| 36 | +│ %0 = py.constant.constant 1 : !py.int |
| 37 | +│ %1 = py.cmp.gt(lhs=%x, rhs=%0) : !py.bool |
| 38 | +│ cf.cond_br %1 goto ^1(%1) else ^2(%1) |
| 39 | +``` |
| 40 | + |
| 41 | +**Definition**, [`cf.ConditionalBranch`][kirin.dialects.cf.ConditionalBranch] takes a boolean condition `cond` of type [`ir.SSAValue`][kirin.ir.SSAValue] and: |
| 42 | + |
| 43 | +- then successor and its argument |
| 44 | +- else successor and its argument |
| 45 | + |
| 46 | +this statement is also a terminator, which means it must be the last statement of a block. |
| 47 | + |
| 48 | +## Combining together - lowering from Python |
| 49 | + |
| 50 | +Now combining these two statemente together, we can represent most of the Python control flows, e.g `if-else` and `for`-loops. These two statement basically just provides a basic way describing the edges on a control flow graph (CFG) by assuming the node only has one or two outgoing edges. |
| 51 | + |
| 52 | +As an example, the following Python program: |
| 53 | + |
| 54 | +```python |
| 55 | +from kirin.prelude import basic_no_opt |
| 56 | + |
| 57 | +@basic_no_opt |
| 58 | +def main(x): |
| 59 | + if x > 1: |
| 60 | + y = 1 |
| 61 | + else: |
| 62 | + y = 2 |
| 63 | + return y |
| 64 | +``` |
| 65 | + |
| 66 | +will be lowered to the following SSA form in `cf` dialect: |
| 67 | + |
| 68 | +```mlir |
| 69 | +func.func main(!Any) -> !Any { |
| 70 | + ^0(%main_self, %x): |
| 71 | + │ %0 = py.constant.constant 1 : !py.int |
| 72 | + │ %1 = py.cmp.gt(lhs=%x, rhs=%0) : !py.bool |
| 73 | + │ cf.cond_br %1 goto ^1(%1) else ^2(%1) |
| 74 | + ^1(%2): |
| 75 | + │ %y = py.constant.constant 1 : !py.int |
| 76 | + │ cf.br ^3(%y) |
| 77 | + ^2(%3): |
| 78 | + │ %y_1 = py.constant.constant 2 : !py.int |
| 79 | + │ cf.br ^3(%y_1) |
| 80 | + ^3(%y_2): |
| 81 | + │ func.return %y_2 |
| 82 | +} // func.func main |
| 83 | +``` |
| 84 | + |
| 85 | +And similarly, we can lower a `for`-loop into the `cf` dialect: |
| 86 | + |
| 87 | +```python |
| 88 | +@basic_no_opt |
| 89 | +def main(x): |
| 90 | + for i in range(5): |
| 91 | + x = x + i |
| 92 | + return x |
| 93 | +``` |
| 94 | + |
| 95 | +will be lowered into the following SSA form: |
| 96 | + |
| 97 | +```mlir |
| 98 | +func.func main(!Any) -> !Any { |
| 99 | + ^0(%main_self, %x_1): |
| 100 | + │ %0 = py.constant.constant 0 : !py.int |
| 101 | + │ %1 = py.constant.constant 5 : !py.int |
| 102 | + │ %2 = py.constant.constant 1 : !py.int |
| 103 | + │ %3 = py.range.range(start=%0, stop=%1, step=%2) : !py.range |
| 104 | + │ %4 = py.iterable.iter(value=%3) : !Any |
| 105 | + │ %5 = py.constant.constant None : !py.NoneType |
| 106 | + │ %6 = py.iterable.next(iter=%4) : !Any |
| 107 | + │ %7 = py.cmp.is(lhs=%6, rhs=%5) : !py.bool |
| 108 | + │ cf.cond_br %7 goto ^2(%x_1) else ^1(%6, %x_1) |
| 109 | + ^1(%i, %x_2): |
| 110 | + │ %x = py.binop.add(%x_2, %i) : ~T |
| 111 | + │ %8 = py.iterable.next(iter=%4) : !Any |
| 112 | + │ %9 = py.cmp.is(lhs=%8, rhs=%5) : !py.bool |
| 113 | + │ cf.cond_br %9 goto ^2(%x) else ^1(%8, %x) |
| 114 | + ^2(%x_3): |
| 115 | + │ func.return %x_3 |
| 116 | +} // func.func main |
| 117 | +``` |
| 118 | + |
| 119 | +However, as you may already notice, lowering from Python directly to `cf` dialect will lose some of the high-level information such as the control flow is actually a for-loop. This information can be useful when one wants to perform some optimization. This is why we are taking the same route as MLIR with a structural IR (via [`ir.Region`][kirin.ir.Region]s). For the interested readers, please proceed to [Structural Control Flow](scf.md) for further reading. |
| 120 | + |
| 121 | +## API Reference |
6 | 122 |
|
7 | 123 | ::: kirin.dialects.cf.stmts |
8 | 124 | options: |
|
0 commit comments