pub struct CFGBuilder<T> { /* private fields */ }Expand description
Builder for a crate::ops::CFG child control
flow graph.
These builder methods should ensure that the first two children of a CFG node are the entry node and the exit node.
§Example
/*  Build a control flow graph with the following structure:
           +-----------+
           |   Entry   |
           +-/-----\---+
            /       \
           /         \
          /           \
         /             \
  +-----/----+       +--\-------+
  | Branch A |       | Branch B |
  +-----\----+       +----/-----+
         \               /
          \             /
           \           /
            \         /
           +-\-------/--+
           |    Exit    |
           +------------+
*/
use hugr::{
    builder::{BuildError, CFGBuilder, Container, Dataflow, HugrBuilder, endo_sig, inout_sig},
    extension::{prelude, ExtensionSet},
    ops, type_row,
    types::{Signature, SumType, Type},
    Hugr,
};
const NAT: Type = prelude::USIZE_T;
fn make_cfg() -> Result<Hugr, BuildError> {
    let mut cfg_builder = CFGBuilder::new(Signature::new_endo(NAT))?;
    // Outputs from basic blocks must be packed in a sum which corresponds to
    // which successor to pick. We'll either choose the first branch and pass
    // it a NAT, or the second branch and pass it nothing.
    let sum_variants = vec![type_row![NAT], type_row![]];
    // The second argument says what types will be passed through to every
    // successor, in addition to the appropriate `sum_variants` type.
    let mut entry_b = cfg_builder.entry_builder(sum_variants.clone(), type_row![NAT])?;
    let [inw] = entry_b.input_wires_arr();
    let entry = {
        // Pack the const "42" into the appropriate sum type.
        let left_42 = ops::Value::sum(
            0,
            [prelude::ConstUsize::new(42).into()],
            SumType::new(sum_variants.clone()),
        )?;
        let sum = entry_b.add_load_value(left_42);
        entry_b.finish_with_outputs(sum, [inw])?
    };
    // This block will be the first successor of the entry node. It takes two
    // `NAT` arguments: one from the `sum_variants` type, and another from the
    // entry node's `other_outputs`.
    let mut successor_builder = cfg_builder.simple_block_builder(
        inout_sig(type_row![NAT, NAT], NAT),
        1, // only one successor to this block
    )?;
    let successor_a = {
        // This block has one successor. The choice is denoted by a unary sum.
        let sum_unary = successor_builder.add_load_const(ops::Value::unary_unit_sum());
        // The input wires of a node start with the data embedded in the variant
        // which selected this block.
        let [_forty_two, in_wire] = successor_builder.input_wires_arr();
        successor_builder.finish_with_outputs(sum_unary, [in_wire])?
    };
    // The only argument to this block is the entry node's `other_outputs`.
    let mut successor_builder = cfg_builder.simple_block_builder(endo_sig(NAT), 1)?;
    let successor_b = {
        let sum_unary = successor_builder.add_load_value(ops::Value::unary_unit_sum());
        let [in_wire] = successor_builder.input_wires_arr();
        successor_builder.finish_with_outputs(sum_unary, [in_wire])?
    };
    let exit = cfg_builder.exit_block();
    cfg_builder.branch(&entry, 0, &successor_a)?; // branch 0 goes to successor_a
    cfg_builder.branch(&entry, 1, &successor_b)?; // branch 1 goes to successor_b
    cfg_builder.branch(&successor_a, 0, &exit)?;
    cfg_builder.branch(&successor_b, 0, &exit)?;
    let hugr = cfg_builder.finish_prelude_hugr()?;
    Ok(hugr)
};
#[cfg(not(feature = "extension_inference"))]
assert!(make_cfg().is_ok());Implementations§
source§impl CFGBuilder<Hugr>
 
impl CFGBuilder<Hugr>
sourcepub fn new(signature: Signature) -> Result<Self, BuildError>
 
pub fn new(signature: Signature) -> Result<Self, BuildError>
New CFG rooted HUGR builder
source§impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B>
 
impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B>
sourcepub fn block_builder(
    &mut self,
    inputs: TypeRow,
    sum_rows: impl IntoIterator<Item = TypeRow>,
    other_outputs: TypeRow,
) -> Result<BlockBuilder<&mut Hugr>, BuildError>
 
pub fn block_builder( &mut self, inputs: TypeRow, sum_rows: impl IntoIterator<Item = TypeRow>, other_outputs: TypeRow, ) -> Result<BlockBuilder<&mut Hugr>, BuildError>
Return a builder for a non-entry DataflowBlock child graph with inputs
and outputs and the variants of the branching Sum value
specified by sum_rows. Extension delta will be inferred.
§Errors
This function will return an error if there is an error adding the node.
sourcepub fn block_builder_exts(
    &mut self,
    inputs: TypeRow,
    sum_rows: impl IntoIterator<Item = TypeRow>,
    other_outputs: TypeRow,
    extension_delta: impl Into<ExtensionSet>,
) -> Result<BlockBuilder<&mut Hugr>, BuildError>
 
pub fn block_builder_exts( &mut self, inputs: TypeRow, sum_rows: impl IntoIterator<Item = TypeRow>, other_outputs: TypeRow, extension_delta: impl Into<ExtensionSet>, ) -> Result<BlockBuilder<&mut Hugr>, BuildError>
Return a builder for a non-entry DataflowBlock child graph with inputs
and outputs and the variants of the branching Sum value
specified by sum_rows. Extension delta will be inferred.
§Errors
This function will return an error if there is an error adding the node.
sourcepub fn simple_block_builder(
    &mut self,
    signature: Signature,
    n_cases: usize,
) -> Result<BlockBuilder<&mut Hugr>, BuildError>
 
pub fn simple_block_builder( &mut self, signature: Signature, n_cases: usize, ) -> Result<BlockBuilder<&mut Hugr>, BuildError>
Return a builder for a non-entry DataflowBlock child graph with inputs
and outputs and extension_delta explicitly specified, plus a UnitSum type
(a Sum of n_cases unit types) to select the successor.
§Errors
This function will return an error if there is an error adding the node.
sourcepub fn entry_builder(
    &mut self,
    sum_rows: impl IntoIterator<Item = TypeRow>,
    other_outputs: TypeRow,
) -> Result<BlockBuilder<&mut Hugr>, BuildError>
 
pub fn entry_builder( &mut self, sum_rows: impl IntoIterator<Item = TypeRow>, other_outputs: TypeRow, ) -> Result<BlockBuilder<&mut Hugr>, BuildError>
Return a builder for the entry DataflowBlock child graph with outputs
and the variants of the branching Sum value specified by sum_rows.
Extension delta will be inferred.
§Errors
This function will return an error if an entry block has already been built.
sourcepub fn entry_builder_exts(
    &mut self,
    sum_rows: impl IntoIterator<Item = TypeRow>,
    other_outputs: TypeRow,
    extension_delta: impl Into<ExtensionSet>,
) -> Result<BlockBuilder<&mut Hugr>, BuildError>
 
pub fn entry_builder_exts( &mut self, sum_rows: impl IntoIterator<Item = TypeRow>, other_outputs: TypeRow, extension_delta: impl Into<ExtensionSet>, ) -> Result<BlockBuilder<&mut Hugr>, BuildError>
Return a builder for the entry DataflowBlock child graph with outputs,
the variants of the branching Sum value specified by sum_rows, and
extension_delta explicitly specified. (entry_builder
may be used to infer.)
§Errors
This function will return an error if an entry block has already been built.
sourcepub fn simple_entry_builder(
    &mut self,
    outputs: TypeRow,
    n_cases: usize,
) -> Result<BlockBuilder<&mut Hugr>, BuildError>
 
pub fn simple_entry_builder( &mut self, outputs: TypeRow, n_cases: usize, ) -> Result<BlockBuilder<&mut Hugr>, BuildError>
Return a builder for the entry DataflowBlock child graph with
outputs and a UnitSum type: a Sum of n_cases unit types.
§Errors
This function will return an error if there is an error adding the node.
sourcepub fn simple_entry_builder_exts(
    &mut self,
    outputs: TypeRow,
    n_cases: usize,
    extension_delta: impl Into<ExtensionSet>,
) -> Result<BlockBuilder<&mut Hugr>, BuildError>
 
pub fn simple_entry_builder_exts( &mut self, outputs: TypeRow, n_cases: usize, extension_delta: impl Into<ExtensionSet>, ) -> Result<BlockBuilder<&mut Hugr>, BuildError>
Return a builder for the entry DataflowBlock child graph with
outputs and a Sum of n_cases unit types, and explicit extension_delta.
(simple_entry_builder may be used to infer.)
§Errors
This function will return an error if there is an error adding the node.
sourcepub fn exit_block(&self) -> BasicBlockID
 
pub fn exit_block(&self) -> BasicBlockID
Returns the exit block of this CFGBuilder.
sourcepub fn branch(
    &mut self,
    predecessor: &BasicBlockID,
    branch: usize,
    successor: &BasicBlockID,
) -> Result<(), BuildError>
 
pub fn branch( &mut self, predecessor: &BasicBlockID, branch: usize, successor: &BasicBlockID, ) -> Result<(), BuildError>
Set the branch index successor block of predecessor.
§Errors
This function will return an error if there is an error connecting the blocks.
Trait Implementations§
source§impl<B: AsMut<Hugr> + AsRef<Hugr>> Container for CFGBuilder<B>
 
impl<B: AsMut<Hugr> + AsRef<Hugr>> Container for CFGBuilder<B>
source§fn container_node(&self) -> Node
 
fn container_node(&self) -> Node
source§fn add_child_node(&mut self, node: impl Into<OpType>) -> Node
 
fn add_child_node(&mut self, node: impl Into<OpType>) -> Node
OpType as the final child of the container.source§fn add_other_wire(&mut self, src: Node, dst: Node) -> Wire
 
fn add_other_wire(&mut self, src: Node, dst: Node) -> Wire
other_inputs or  other_outputssource§fn add_constant(&mut self, constant: impl Into<Const>) -> ConstID
 
fn add_constant(&mut self, constant: impl Into<Const>) -> ConstID
source§fn define_function(
    &mut self,
    name: impl Into<String>,
    signature: impl Into<PolyFuncType>,
) -> Result<FunctionBuilder<&mut Hugr>, BuildError>
 
fn define_function( &mut self, name: impl Into<String>, signature: impl Into<PolyFuncType>, ) -> Result<FunctionBuilder<&mut Hugr>, BuildError>
ops::FuncDefn node and returns a builder to define the function
body graph. Read moresource§fn add_hugr(&mut self, child: Hugr) -> InsertionResult
 
fn add_hugr(&mut self, child: Hugr) -> InsertionResult
source§fn add_hugr_view(&mut self, child: &impl HugrView) -> InsertionResult
 
fn add_hugr_view(&mut self, child: &impl HugrView) -> InsertionResult
source§fn set_metadata(&mut self, key: impl AsRef<str>, meta: impl Into<NodeMetadata>)
 
fn set_metadata(&mut self, key: impl AsRef<str>, meta: impl Into<NodeMetadata>)
source§fn set_child_metadata(
    &mut self,
    child: Node,
    key: impl AsRef<str>,
    meta: impl Into<NodeMetadata>,
)
 
fn set_child_metadata( &mut self, child: Node, key: impl AsRef<str>, meta: impl Into<NodeMetadata>, )
source§impl<T: Debug> Debug for CFGBuilder<T>
 
impl<T: Debug> Debug for CFGBuilder<T>
source§impl HugrBuilder for CFGBuilder<Hugr>
 
impl HugrBuilder for CFGBuilder<Hugr>
source§fn finish_hugr(
    self,
    extension_registry: &ExtensionRegistry,
) -> Result<Hugr, ValidationError>
 
fn finish_hugr( self, extension_registry: &ExtensionRegistry, ) -> Result<Hugr, ValidationError>
source§fn finish_prelude_hugr(self) -> Result<Hugr, ValidationError>where
    Self: Sized,
 
fn finish_prelude_hugr(self) -> Result<Hugr, ValidationError>where
    Self: Sized,
source§impl<T: PartialEq> PartialEq for CFGBuilder<T>
 
impl<T: PartialEq> PartialEq for CFGBuilder<T>
source§impl<H: AsMut<Hugr> + AsRef<Hugr>> SubContainer for CFGBuilder<H>
 
impl<H: AsMut<Hugr> + AsRef<Hugr>> SubContainer for CFGBuilder<H>
source§type ContainerHandle = BuildHandle<CfgID>
 
type ContainerHandle = BuildHandle<CfgID>
source§fn finish_sub_container(self) -> Result<Self::ContainerHandle, BuildError>
 
fn finish_sub_container(self) -> Result<Self::ContainerHandle, BuildError>
impl<T> StructuralPartialEq for CFGBuilder<T>
Auto Trait Implementations§
impl<T> Freeze for CFGBuilder<T>where
    T: Freeze,
impl<T> RefUnwindSafe for CFGBuilder<T>where
    T: RefUnwindSafe,
impl<T> Send for CFGBuilder<T>where
    T: Send,
impl<T> Sync for CFGBuilder<T>where
    T: Sync,
impl<T> Unpin for CFGBuilder<T>where
    T: Unpin,
impl<T> UnwindSafe for CFGBuilder<T>where
    T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
    T: Any,
 
impl<T> Downcast for Twhere
    T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
 
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
 
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.source§fn as_any(&self) -> &(dyn Any + 'static)
 
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
 
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.source§impl<T> DowncastSync for T
 
impl<T> DowncastSync for T
source§impl<T> FmtForward for T
 
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
    Self: Binary,
 
fn fmt_binary(self) -> FmtBinary<Self>where
    Self: Binary,
self to use its Binary implementation when Debug-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
    Self: Display,
 
fn fmt_display(self) -> FmtDisplay<Self>where
    Self: Display,
self to use its Display implementation when
Debug-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
    Self: LowerExp,
 
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
    Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
    Self: LowerHex,
 
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
    Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
    Self: Octal,
 
fn fmt_octal(self) -> FmtOctal<Self>where
    Self: Octal,
self to use its Octal implementation when Debug-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
    Self: Pointer,
 
fn fmt_pointer(self) -> FmtPointer<Self>where
    Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
    Self: UpperExp,
 
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
    Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
    Self: UpperHex,
 
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
    Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
 
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Pipe for Twhere
    T: ?Sized,
 
impl<T> Pipe for Twhere
    T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
    Self: Sized,
 
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
    Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
    R: 'a,
 
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
    R: 'a,
self and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
    R: 'a,
 
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
    R: 'a,
self and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
 
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
    &'a mut self,
    func: impl FnOnce(&'a mut B) -> R,
) -> R
 
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
 
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
 
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
 
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.source§impl<T> Tap for T
 
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
 
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
 
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
 
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
 
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
 
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
 
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
 
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
 
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
 
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
 
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
 
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
 
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
 
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.