Cfg#
- class hugr.build.cfg.Cfg(*input_types: Type)[source]#
Bases:
ParentBuilder
[CFG
],AbstractContextManager
Builder class for a HUGR control flow graph, with the HUGR root node being a
CFG
.- Parameters:
input_types – The input types for the CFG. Outputs are computed
block. (by propagating types through the control flow graph to the exit)
Examples
>>> cfg = Cfg(tys.Bool, tys.Unit) >>> cfg.parent_op CFG(inputs=[Bool, Unit])
Methods
Add a new block to the CFG and start building it.
Start building the entry block of the CFG.
Start building a block that succeeds an existing block.
Add a branching control flow link between blocks.
Branch from a block to the exit block.
Generate an input port for this node.
Start building a CFG nested inside an existing HUGR graph.
Generate an output port for this node.
OutPort corresponding to this
Wire
.Returns an iterator over the output ports of this node.
Generate a port in direction for this node with offset.
Convert to a
Node
.Attributes
Node for entry block of the CFG.
Metadata associated with this node.
The parent node's operation.
The HUGR instance this CFG is part of.
The parent node of the CFG.
The node holding the root of the exit block.
- add_block(*input_types: Type) Block [source]#
Add a new block to the CFG and start building it.
- Parameters:
input_types – The input types for the block.
- Returns:
The block builder.
Examples
>>> cfg = Cfg(tys.Bool) >>> with cfg.add_block(tys.Unit) as b: b.set_single_succ_outputs(*b.inputs())
- add_entry() Block [source]#
Start building the entry block of the CFG.
- Returns:
The entry block builder.
Examples
>>> cfg = Cfg(tys.Bool) >>> entry = cfg.add_entry() >>> entry.set_outputs(*entry.inputs())
- add_successor(pred: Wire) Block [source]#
Start building a block that succeeds an existing block.
- Parameters:
pred – The wire from the predecessor block to the new block. The
block. (port of the wire determines the branching index of the new)
- Returns:
The new block builder.
Examples
>>> cfg = Cfg(tys.Bool) >>> with cfg.add_entry() as entry: entry.set_single_succ_outputs() >>> with cfg.add_successor(entry[0]) as b: b.set_single_succ_outputs(*b.inputs())
- branch(src: Wire, dst: ToNode) None [source]#
Add a branching control flow link between blocks.
- Parameters:
src – The wire from the predecessor block.
dst – The destination block.
Examples
>>> cfg = Cfg(tys.Bool) >>> with cfg.add_entry() as entry: entry.set_single_succ_outputs() >>> b = cfg.add_block(tys.Unit) >>> cfg.branch(entry[0], b)
- branch_exit(src: Wire) None [source]#
Branch from a block to the exit block.
- Parameters:
src – The wire from the predecessor block.
Examples
>>> cfg = Cfg(tys.Bool) >>> with cfg.add_entry() as entry: entry.set_single_succ_outputs() >>> cfg.branch_exit(entry[0])
- property entry: Node#
Node for entry block of the CFG.
Examples
>>> cfg = Cfg(tys.Bool) >>> cfg.entry Node(1)
- inp(offset: int) InPort #
Generate an input port for this node.
- Parameters:
offset – port offset.
- Returns:
Incoming port for this node.
Examples
>>> Node(0).inp(1) InPort(Node(0), 1)
- classmethod new_nested(input_types: TypeRow, hugr: Hugr, parent: ToNode | None = None) Cfg [source]#
Start building a CFG nested inside an existing HUGR graph.
- Parameters:
input_types – The input types for the CFG.
hugr – The HUGR instance this CFG is part of.
parent – The parent node for the CFG: defaults to the root of the HUGR instance.
- Returns:
The new CFG builder.
Examples
>>> hugr = Hugr() >>> cfg = Cfg.new_nested([tys.Bool], hugr) >>> cfg.parent_op CFG(inputs=[Bool])
- out(offset: int) OutPort #
Generate an output port for this node.
- Parameters:
offset – port offset.
- Returns:
Outgoing port for this node.
Examples
>>> Node(0).out(1) OutPort(Node(0), 1)
- property parent_op: OpVar#
The parent node’s operation.