Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add TailLoop::BREAK_TAG and CONTINUE_TAG #1626

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions hugr-core/src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::extension::prelude::MakeTuple;
use crate::hugr::hugrmut::InsertionResult;
use crate::hugr::views::HugrView;
use crate::hugr::{NodeMetadata, ValidationError};
use crate::ops::{self, OpTag, OpTrait, OpType, Tag};
use crate::ops::{self, OpTag, OpTrait, OpType, Tag, TailLoop};
use crate::utils::collect_array;
use crate::{IncomingPort, Node, OutgoingPort};

Expand Down Expand Up @@ -623,7 +623,11 @@ pub trait Dataflow: Container {
tail_loop: ops::TailLoop,
values: impl IntoIterator<Item = Wire>,
) -> Result<Wire, BuildError> {
self.make_sum(0, [tail_loop.just_inputs, tail_loop.just_outputs], values)
self.make_sum(
TailLoop::CONTINUE_TAG,
[tail_loop.just_inputs, tail_loop.just_outputs],
values,
)
}

/// Use the wires in `values` to return a wire corresponding to the
Expand All @@ -640,7 +644,11 @@ pub trait Dataflow: Container {
loop_op: ops::TailLoop,
values: impl IntoIterator<Item = Wire>,
) -> Result<Wire, BuildError> {
self.make_sum(1, [loop_op.just_inputs, loop_op.just_outputs], values)
self.make_sum(
TailLoop::BREAK_TAG,
[loop_op.just_inputs, loop_op.just_outputs],
values,
)
}

/// Add a [`ops::Call`] node, calling `function`, with inputs
Expand Down
10 changes: 10 additions & 0 deletions hugr-core/src/ops/controlflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ impl DataflowOpTrait for TailLoop {
}

impl TailLoop {
/// The [tag] for a loop body output to indicate the loop should iterate again.
///
/// [tag]: crate::ops::constant::Sum::tag
pub const CONTINUE_TAG: usize = 0;

/// The [tag] for a loop body output to indicate the loop should exit with the supplied values.
///
/// [tag]: crate::ops::constant::Sum::tag
pub const BREAK_TAG: usize = 1;

/// Build the output TypeRow of the child graph of a TailLoop node.
pub(crate) fn body_output_row(&self) -> TypeRow {
let sum_type = Type::new_sum([self.just_inputs.clone(), self.just_outputs.clone()]);
Expand Down
Loading