Skip to content

Compactify

CFGCompactify dataclass

CFGCompactify()

Bases: RewriteRule

Compactify the CFG by removing dead blocks and merging blocks if the statement uses the SSACFG convention. Do nothing if given ir.Region or ir.Block due to no context of the region.

To compactify hierarchical CFG, combine this rule with kirin.rewrite.Walk to recursively apply this rule to all statements.

CFGEdge dataclass

CFGEdge(cfg: CFG)

Bases: RewriteRule

Merge non-branching blocks on the edge of the CFG.

Example:

/---> [B] --> [D] --> [E]

[A]-----> [C] -------------^

[B] and [D] are non-branching blocks on the same edge. They can be merged into one block.

/---> [B,D] --> [E]

[A]-----> [C] -------^

CompactifyRegion dataclass

CompactifyRegion(cfg: CFG)

Bases: RewriteRule

Wrapper to share the CFG object with same CFG region.

Source code in src/kirin/rewrite/compactify.py
258
259
260
261
262
263
264
def __init__(self, cfg: CFG):
    self.cfg = cfg
    self.rule = Fixpoint(
        Chain(
            DeadBlock(cfg), Walk(DuplicatedBranch()), SkipBlock(cfg), CFGEdge(cfg)
        )
    )

DeadBlock dataclass

DeadBlock(cfg: CFG)

Bases: RewriteRule

Compactify the CFG by removing dead blocks.

DuplicatedBranch dataclass

DuplicatedBranch()

Bases: RewriteRule

Merge duplicated branches into a single branch.

Example:

[A]-->[B]
  -----^

Merge the two branches into one without changing the CFG:

[A]-->[B]

SkipBlock dataclass

SkipBlock(cfg: CFG)

Bases: RewriteRule

Simplify a block that only contains a branch statement.