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,
extension::prelude::usize_t,
};
fn make_cfg() -> Result<Hugr, BuildError> {
let mut cfg_builder = CFGBuilder::new(Signature::new_endo(usize_t()))?;
// 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 usize, or the second branch and pass it nothing.
let sum_variants = vec![vec![usize_t()].into(), 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(), vec![usize_t()].into())?;
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
// `usize` 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(vec![usize_t(), usize_t()], usize_t()),
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(usize_t()), 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_hugr()?;
Ok(hugr)
};
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
.
§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
, 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
.
§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 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_other_wire(&mut self, src: Node, dst: Node) -> Wire
fn add_other_wire(&mut self, src: Node, dst: Node) -> Wire
other_inputs
or other_outputs
Source§fn add_constant(&mut self, constant: impl Into<Const>) -> ConstID
fn add_constant(&mut self, constant: impl Into<Const>) -> ConstID
Source§fn add_hugr(&mut self, child: Hugr) -> InsertionResult
fn add_hugr(&mut self, child: Hugr) -> InsertionResult
Source§fn add_hugr_view<H: HugrView>(
&mut self,
child: &H,
) -> InsertionResult<H::Node, Node>
fn add_hugr_view<H: HugrView>( &mut self, child: &H, ) -> InsertionResult<H::Node, Node>
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§fn use_extension(&mut self, ext: impl Into<Arc<Extension>>)
fn use_extension(&mut self, ext: impl Into<Arc<Extension>>)
Source§fn use_extensions<Reg>(&mut self, registry: impl IntoIterator<Item = Reg>)where
ExtensionRegistry: Extend<Reg>,
fn use_extensions<Reg>(&mut self, registry: impl IntoIterator<Item = Reg>)where
ExtensionRegistry: Extend<Reg>,
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) -> Result<Hugr, ValidationError<Node>>
fn finish_hugr(self) -> Result<Hugr, ValidationError<Node>>
Source§fn module_root_builder(&mut self) -> ModuleBuilder<&mut Hugr>
fn module_root_builder(&mut self) -> ModuleBuilder<&mut Hugr>
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>
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>
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>
, which can then be
downcast
into Box<dyn 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>
, which 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> DowncastSend for T
impl<T> DowncastSend for T
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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.