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_block

Add a new block to the CFG and start building it.

add_entry

Start building the entry block of the CFG.

add_successor

Start building a block that succeeds an existing block.

branch

Add a branching control flow link between blocks.

branch_exit

Branch from a block to the exit block.

inp

Generate an input port for this node.

new_nested

Start building a CFG nested inside an existing HUGR graph.

out

Generate an output port for this node.

out_port

OutPort corresponding to this Wire.

outputs

Returns an iterator over the output ports of this node.

port

Generate a port in direction for this node with offset.

to_node

Convert to a Node.

Attributes

entry

Node for entry block of the CFG.

metadata

Metadata associated with this node.

parent_op

The parent node's operation.

hugr

The HUGR instance this CFG is part of.

parent_node

The parent node of the CFG.

exit

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)
exit: Node#

The node holding the root of the exit block.

hugr: Hugr#

The HUGR instance this CFG is part of.

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)
property metadata: dict[str, object]#

Metadata associated with this node.

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)
out_port() OutPort#

OutPort corresponding to this Wire.

outputs() Iterator[OutPort]#

Returns an iterator over the output ports of this node.

parent_node: Node#

The parent node of the CFG.

property parent_op: OpVar#

The parent node’s operation.

port(offset: int, direction: Direction) InPort | OutPort#

Generate a port in direction for this node with offset.

Examples

>>> Node(0).port(1, Direction.INCOMING)
InPort(Node(0), 1)
>>> Node(0).port(1, Direction.OUTGOING)
OutPort(Node(0), 1)
to_node() Node#

Convert to a Node.