Skip to content

Conversation

aborgna-q
Copy link
Collaborator

@aborgna-q aborgna-q commented Mar 20, 2025

Imports the MakeTuple -> UnpackTuple elimination pass from tket2.
This removes tuple unpacks consuming the output of a MakeTuple, and deletes the packing operation when possible.

This pass does not remove the reverse case, Unpack->MakeTuple. Should it be added here? We should avoid producing overlapping rewrites if doing that (the simple Pack->Unpack->Pack case is easy, but multiports can make things interesting).

Requires #2011
Closes #1810

@aborgna-q aborgna-q requested a review from a team as a code owner March 20, 2025 17:13
@aborgna-q aborgna-q requested review from mark-koch and removed request for a team March 20, 2025 17:13
@aborgna-q aborgna-q force-pushed the ab/sibling-subgraph-from-node branch from b3dc6a5 to bad40dc Compare March 20, 2025 17:26
Copy link
Contributor

@mark-koch mark-koch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Mostly wondering about edge cases with order and inter-graph edges?

Btw, it would be nice to have the opposite UnpackTuple -> MakeTuple elison as well since Guppy sometimes emits those when dealing with structs. Happy for this to be a follow-up though

let mut nodes = unpack_nodes;
nodes.push(pack_node);
let subcirc = SiblingSubgraph::try_from_nodes_with_checker(nodes, hugr, checker).unwrap();
let subcirc_signature = subcirc.signature(hugr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik this will panic if there are order edges?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, tried my hand at fixing it but it take a bit of work since just ignoring order edges makes the nodes-in-subgraph enumeration follow the anyways.

I added docs and a should_panic test. We'll update those once #1974 gets fixed.


let mut nodes = unpack_nodes;
nodes.push(pack_node);
let subcirc = SiblingSubgraph::try_from_nodes_with_checker(nodes, hugr, checker).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unwrap safe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the make-unpack is an inter-graph edge?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • See other comment about order edges. Unwrap is safe otherwise, as we are just constructing a two-node graph.

  • I disabled matching on inter-graph edges to be on the safe side.

Base automatically changed from ab/sibling-subgraph-from-node to main March 21, 2025 15:17
Copy link

codecov bot commented Mar 31, 2025

Codecov Report

Attention: Patch coverage is 88.69048% with 19 lines in your changes missing coverage. Please review.

Project coverage is 83.12%. Comparing base (4734948) to head (dce8f65).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
hugr-passes/src/untuple.rs 88.69% 15 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2012      +/-   ##
==========================================
+ Coverage   83.09%   83.12%   +0.02%     
==========================================
  Files         215      216       +1     
  Lines       40905    41073     +168     
  Branches    37119    37287     +168     
==========================================
+ Hits        33992    34143     +151     
- Misses       5027     5040      +13     
- Partials     1886     1890       +4     
Flag Coverage Δ
python 85.92% <ø> (ø)
rust 82.84% <88.69%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aborgna-q aborgna-q requested a review from mark-koch March 31, 2025 15:57
@aborgna-q aborgna-q added this pull request to the merge queue Apr 1, 2025
Merged via the queue into main with commit 607a77e Apr 1, 2025
25 checks passed
@aborgna-q aborgna-q deleted the ab/untuple branch April 1, 2025 09:23
This was referenced Apr 1, 2025
github-merge-queue bot pushed a commit that referenced this pull request Apr 2, 2025
## 🤖 New release

* `hugr-model`: 0.18.1 -> 0.19.0 (⚠ API breaking changes)
* `hugr-core`: 0.15.2 -> 0.15.3 ~(⚠ API breaking changes)~
* `hugr-llvm`: 0.15.2 -> 0.15.3 (✓ API compatible changes)
* `hugr-passes`: 0.15.2 -> 0.15.3 (✓ API compatible changes)
* `hugr`: 0.15.2 -> 0.15.3 (✓ API compatible changes)
* `hugr-cli`: 0.15.2 -> 0.15.3 (✓ API compatible changes)

### ⚠ `hugr-model` breaking changes

```text
--- failure enum_missing: pub enum removed or renamed ---

Description:
A publicly-visible enum cannot be imported by its prior path. A `pub use` may have been removed, or the enum itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/enum_missing.ron

Failed in:
  enum hugr_model::v0::table::ExtSetPart, previously in file /tmp/.tmpDqJ3X7/hugr-model/src/v0/table/mod.rs:352

--- failure enum_variant_added: enum variant added on exhaustive enum ---

Description:
A publicly-visible enum without #[non_exhaustive] has a new variant.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/enum_variant_added.ron

Failed in:
  variant Term:Func in /tmp/.tmpIpZPlW/hugr/hugr-model/src/v0/table/mod.rs:341

--- failure enum_variant_missing: pub enum variant removed or renamed ---

Description:
A publicly-visible enum has at least one variant that is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/enum_variant_missing.ron

Failed in:
  variant Term::ExtSet, previously in file /tmp/.tmpDqJ3X7/hugr-model/src/v0/table/mod.rs:316
  variant Term::ConstFunc, previously in file /tmp/.tmpDqJ3X7/hugr-model/src/v0/table/mod.rs:321
  variant Term::ExtSet, previously in file /tmp/.tmpDqJ3X7/hugr-model/src/v0/ast/mod.rs:237

--- failure pub_module_level_const_missing: pub module-level const is missing ---

Description:
A public const is missing or renamed
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/pub_module_level_const_missing.ron

Failed in:
  CORE_EXT_SET in file /tmp/.tmpDqJ3X7/hugr-model/src/v0/mod.rs:177
```

### ⚠ `hugr-core` breaking changes (⚠️ ignored ⚠️ )

```text
--- failure enum_variant_added: enum variant added on exhaustive enum ---

Description:
A publicly-visible enum without #[non_exhaustive] has a new variant.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.40.0/src/lints/enum_variant_added.ron

Failed in:
  variant ImportError:OrderHint in /tmp/.tmpIpZPlW/hugr/hugr-core/src/import.rs:73
```

<details><summary><i><b>Changelog</b></i></summary><p>

## `hugr-model`

<blockquote>

##
[0.19.0](hugr-model-v0.18.1...hugr-model-v0.19.0)
- 2025-04-02

### New Features

- Python bindings for `hugr-model`.
([#1959](#1959))
- Remove extension sets from `hugr-model`.
([#2031](#2031))
- Packages in `hugr-model` and envelope support.
([#2026](#2026))
- Represent order edges in `hugr-model` as metadata.
([#2027](#2027))
</blockquote>

## `hugr-core`

<blockquote>

##
[0.16.0](hugr-core-v0.15.2...hugr-core-v0.16.0)
- 2025-04-02

### Documentation

- Provide docs for array ops, fix bad doc for HugrView::poly_func_type
([#2021](#2021))

### New Features

- Expand SimpleReplacement API
([#1920](#1920))
- Python bindings for `hugr-model`.
([#1959](#1959))
- ReplaceTypes pass allows replacing extension types and ops
([#1989](#1989))
- Remove extension sets from `hugr-model`.
([#2031](#2031))
- Packages in `hugr-model` and envelope support.
([#2026](#2026))
- Represent order edges in `hugr-model` as metadata.
([#2027](#2027))
- add `build_expect_sum` to allow specific error messages
([#2032](#2032))
</blockquote>

## `hugr-llvm`

<blockquote>

##
[0.16.0](hugr-llvm-v0.15.2...hugr-llvm-v0.16.0)
- 2025-04-02

### New Features

- *(hugr-llvm)* Add llvm codegen for `arithmetic.float.fpow`
([#2042](#2042))
- *(hugr-llvm)* Emit divmod and mod operations
([#2025](#2025))
</blockquote>

## `hugr-passes`

<blockquote>

##
[0.16.0](hugr-passes-v0.15.2...hugr-passes-v0.16.0)
- 2025-04-02

### New Features

- ReplaceTypes pass allows replacing extension types and ops
([#1989](#1989))
- MakeTuple->UnpackTuple elision pass
([#2012](#2012))
- [**breaking**] Extend LowerTypes pass to linearize by inserting
copy/discard ([#2018](#2018))
</blockquote>

## `hugr`

<blockquote>

##
[0.16.0](hugr-v0.15.2...hugr-v0.16.0)
- 2025-04-02

### Documentation

- Provide docs for array ops, fix bad doc for HugrView::poly_func_type
([#2021](#2021))

### New Features

- MakeTuple->UnpackTuple elision pass
([#2012](#2012))
- [**breaking**] Extend LowerTypes pass to linearize by inserting
copy/discard ([#2018](#2018))
- Expand SimpleReplacement API
([#1920](#1920))
- Python bindings for `hugr-model`.
([#1959](#1959))
- ReplaceTypes pass allows replacing extension types and ops
([#1989](#1989))
- Remove extension sets from `hugr-model`.
([#2031](#2031))
- Represent order edges in `hugr-model` as metadata.
([#2027](#2027))
- add `build_expect_sum` to allow specific error messages
([#2032](#2032))
- Packages in `hugr-model` and envelope support.
([#2026](#2026))
</blockquote>

## `hugr-cli`

<blockquote>

##
[0.16.0](hugr-cli-v0.15.2...hugr-cli-v0.16.0)
- 2025-04-02

### Documentation

- Add usage info to hugr-cli's rustdocs
([#2044](#2044))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

---------

Co-authored-by: Agustín Borgna <agustin.borgna@quantinuum.com>
This was referenced Apr 3, 2025
@hugrbot hugrbot mentioned this pull request Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move the tuple unpacking rewrite from tket2 to hugr-passes
2 participants