Skip to content

Conversation

@mark-koch
Copy link
Contributor

@mark-koch mark-koch commented Sep 22, 2025

Closes #2551

Substantial duplication between existing hugr-llvm/src/extension/collections/array.rs and new ...borrow_array.rs.

@mark-koch mark-koch requested a review from tatiana-s September 22, 2025 16:45
@mark-koch mark-koch requested a review from a team as a code owner September 22, 2025 16:45
@mark-koch mark-koch requested review from acl-cqc and removed request for a team and acl-cqc September 22, 2025 16:45
@codecov
Copy link

codecov bot commented Sep 22, 2025

Codecov Report

❌ Patch coverage is 91.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.32%. Comparing base (cd04cf8) to head (14dced7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
hugr-llvm/src/emit/func.rs 91.48% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2574      +/-   ##
==========================================
+ Coverage   83.05%   83.32%   +0.27%     
==========================================
  Files         255      256       +1     
  Lines       48480    50491    +2011     
  Branches    44003    46014    +2011     
==========================================
+ Hits        40265    42074    +1809     
+ Misses       6123     6065      -58     
- Partials     2092     2352     +260     
Flag Coverage Δ
python 91.53% <ø> (ø)
rust 82.53% <91.66%> (+0.33%) ⬆️

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.

let func = ctx
.get_current_module()
.get_function(func_name)
.map(Ok::<_, anyhow::Error>)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Option of Result, etc., is a bit hard to figure out. How about let func = match ctx.get_current_module().get_function(func_name) { Some(f) => f, None => { .... } } which moves your closure into the body and removes the nested ?.

Or perhaps consider let func = if let Some(f) = ctx.blah() {f} else {....}

}

impl<'a, H: HugrView<Node = Node> + 'a> CodegenExtsBuilder<'a, H> {
/// Add a [`BorrowArrayCodegenExtension`] to the given [`CodegenExtsBuilder`] using `ccg`
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: comments should be the other way round?

Copy link
Contributor

@acl-cqc acl-cqc left a comment

Choose a reason for hiding this comment

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

Well I thought I'd take a look at this, but +4,000 lines and I'm stopping a fraction of the way through.... the PR we are thinking this replaces is a third as long as this, right? How many opportunities are there for bugs here? (ReplaceTypes BorrowArray has the benefit that we get type-checking of the output Hugr by the hugr type system, too)

/// The first time this helper is called with a given function name, a function is built
/// using the provided closure. Future invocations with the same name will just emit calls
/// to this function.
pub fn outline_into_function<'c, H: HugrView<Node = Node>, const N: usize>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps get_or_make_function

//! to at least `n * sizeof(T)` bytes. The extra `usize` is an offset pointing to the
//! first element, i.e. the first element is at address `ptr + offset * sizeof(T)`.
//!
//! The rational behind the additional offset is the `pop_left` operation which bumps
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this explanation, but is there no push_left / unpop ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, rational -> rationale

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there no push_left / unpop ?

No, as this would require reallocating the array in general

//! the offset instead of mutating the pointer. This way, we can still free the original
//! pointer when the array is discarded after a pop.
//!
//! We provide utility functions [`array_fat_pointer_ty`], [`build_barray_fat_pointer`], and
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are some of these barray and others just array ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I forgot to rename those after copy pasting 😅

@tatiana-s
Copy link
Contributor

Well I thought I'd take a look at this, but +4,000 lines and I'm stopping a fraction of the way through.... the PR we are thinking this replaces is a third as long as this, right? How many opportunities are there for bugs here? (ReplaceTypes BorrowArray has the benefit that we get type-checking of the output Hugr by the hugr type system, too)

But most of this PR just copies the existing array lowering (including many snapshots) so it is only the bit mask code that is new which is a lot less than the type replacement

@mark-koch mark-koch force-pushed the feat/barray-llvm branch 4 times, most recently from d82bb30 to ba4df3d Compare September 29, 2025 14:05
Base automatically changed from test/panic to main September 29, 2025 15:46
@ss2165
Copy link
Member

ss2165 commented Oct 6, 2025

Please include #2609 before merging done

@acl-cqc
Copy link
Contributor

acl-cqc commented Oct 8, 2025

Ok I guess this is now a joint PR ;-), hopefully this link shows just the changes I've made to Mark's original: https://github.com/CQCL/hugr/pull/2574/files/439f09c27e6591c125088000e03d8556b88fef2b..6cf427bee12c8f65fa35e3374cf4b47c5c9dd9ed

@acl-cqc acl-cqc requested a review from ss2165 October 8, 2025 11:01
Copy link
Member

@ss2165 ss2165 left a comment

Choose a reason for hiding this comment

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

Just some minor docstring/refactor suggestions

go(ctx, block)
})
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MaskCheck {
Copy link
Member

Choose a reason for hiding this comment

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

docstrings on these would be good


/// Emits instructions to flip the borrowed mask flag for a given index.
fn build_mask_flip<'c, H: HugrView<Node = Node>>(
fn check_mask_elem<'c, H: HugrView<Node = Node>>(
Copy link
Member

Choose a reason for hiding this comment

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

docstring 🙏

Copy link
Member

Choose a reason for hiding this comment

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

this would seem to be a natural method on MaskCheck

Copy link
Contributor

Choose a reason for hiding this comment

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

:-D agreed, I was just thinking that myself. Done...

@acl-cqc acl-cqc enabled auto-merge October 8, 2025 12:57
@acl-cqc acl-cqc added this pull request to the merge queue Oct 8, 2025
Merged via the queue into main with commit 4379a0f Oct 8, 2025
27 checks passed
@acl-cqc acl-cqc deleted the feat/barray-llvm branch October 8, 2025 13:08
@hugrbot hugrbot mentioned this pull request Oct 7, 2025
github-merge-queue bot pushed a commit that referenced this pull request Oct 13, 2025
## 🤖 New release

* `hugr-model`: 0.23.0 -> 0.24.0
* `hugr-core`: 0.23.0 -> 0.24.0 (✓ API compatible changes)
* `hugr-llvm`: 0.23.0 -> 0.24.0 (✓ API compatible changes)
* `hugr-passes`: 0.23.0 -> 0.24.0 (✓ API compatible changes)
* `hugr-persistent`: 0.3.0 -> 0.3.1 (✓ API compatible changes)
* `hugr`: 0.23.0 -> 0.24.0 (✓ API compatible changes)
* `hugr-cli`: 0.23.0 -> 0.24.0 (✓ API compatible changes)

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

## `hugr-model`

<blockquote>

##
[0.23.0](hugr-model-v0.22.4...hugr-model-v0.23.0)
- 2025-09-30

### Bug Fixes

- [**breaking**] Appease `cargo-audit` by replacing unmaintained
dependencies ([#2572](#2572))

### New Features

- Documentation and error hints
([#2523](#2523))
</blockquote>

## `hugr-core`

<blockquote>

##
[0.24.0](hugr-core-v0.23.0...hugr-core-v0.24.0)
- 2025-10-13

### Bug Fixes

- Preserve offset for CFG edges when serializing to JSON
([#2606](#2606))

### New Features

- LLVM lowering for borrow arrays using bitmasks
([#2574](#2574))
- *(py, core, llvm)* add `is_borrowed` op for BorrowArray
([#2610](#2610))

### Refactor

- [**breaking**] consistent inout order in borrow array
([#2621](#2621))
</blockquote>

## `hugr-llvm`

<blockquote>

##
[0.24.0](hugr-llvm-v0.23.0...hugr-llvm-v0.24.0)
- 2025-10-13

### New Features

- LLVM lowering for borrow arrays using bitmasks
([#2574](#2574))
- *(py, core, llvm)* add `is_borrowed` op for BorrowArray
([#2610](#2610))

### Refactor

- [**breaking**] consistent inout order in borrow array
([#2621](#2621))
</blockquote>

## `hugr-passes`

<blockquote>

##
[0.24.0](hugr-passes-v0.23.0...hugr-passes-v0.24.0)
- 2025-10-13

### New Features

- Add handler for copying / discarding borrow arrays to default
lineariser ([#2602](#2602))
</blockquote>

## `hugr-persistent`

<blockquote>

##
[0.3.1](hugr-persistent-v0.3.0...hugr-persistent-v0.3.1)
- 2025-10-13

### Bug Fixes

- *(test)* No extension serialisation in persistent-hugr testing
([#2612](#2612))

### New Features

- *(persistent)* Redesign CommitStateSpace, bound Commit lifetime
([#2534](#2534))
</blockquote>

## `hugr`

<blockquote>

##
[0.24.0](hugr-v0.23.0...hugr-v0.24.0)
- 2025-10-13

### Bug Fixes

- Preserve offset for CFG edges when serializing to JSON
([#2606](#2606))

### New Features

- Add handler for copying / discarding borrow arrays to default
lineariser ([#2602](#2602))
- LLVM lowering for borrow arrays using bitmasks
([#2574](#2574))
- *(py, core, llvm)* add `is_borrowed` op for BorrowArray
([#2610](#2610))

### Refactor

- [**breaking**] consistent inout order in borrow array
([#2621](#2621))
</blockquote>

## `hugr-cli`

<blockquote>

##
[0.24.0](hugr-cli-v0.23.0...hugr-cli-v0.24.0)
- 2025-10-13

### Documentation

- update cli readme ([#2601](#2601))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
@hugrbot hugrbot mentioned this pull request Oct 14, 2025
github-merge-queue bot pushed a commit that referenced this pull request Nov 3, 2025
## 🤖 New release

* `hugr-model`: 0.24.0 -> 0.24.1 (✓ API compatible changes)
* `hugr-core`: 0.24.0 -> 0.24.1 (✓ API compatible changes)
* `hugr-llvm`: 0.24.0 -> 0.24.1 (✓ API compatible changes)
* `hugr-passes`: 0.24.0 -> 0.24.1 (✓ API compatible changes)
* `hugr-persistent`: 0.3.1 -> 0.3.2 (✓ API compatible changes)
* `hugr`: 0.24.0 -> 0.24.1 (✓ API compatible changes)
* `hugr-cli`: 0.24.0 -> 0.24.1 (✓ API compatible changes)

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

## `hugr-model`

<blockquote>

##
[0.24.1](hugr-model-v0.24.0...hugr-model-v0.24.1)
- 2025-11-03

### Bug Fixes

- Correct conversion of `table::Term::Tuple` to `ast::Term`
([#2653](#2653))
</blockquote>

## `hugr-core`

<blockquote>

##
[0.24.1](hugr-core-v0.24.0...hugr-core-v0.24.1)
- 2025-11-03

### Bug Fixes

- validation outside entrypoint, normalize_cfgs w/ nonlocal edges
([#2633](#2633))
- SiblingSubgraph::try_from_nodes for non-entrypoint region
([#2655](#2655))
- Correct conversion of `table::Term::Tuple` to `ast::Term`
([#2653](#2653))

### New Features

- track package descriptions when loading
([#2639](#2639))
- *(cli)* describe sub-command
([#2650](#2650))
</blockquote>

## `hugr-llvm`

<blockquote>

##
[0.24.0](hugr-llvm-v0.23.0...hugr-llvm-v0.24.0)
- 2025-10-13

### New Features

- LLVM lowering for borrow arrays using bitmasks
([#2574](#2574))
- *(py, core, llvm)* add `is_borrowed` op for BorrowArray
([#2610](#2610))

### Refactor

- [**breaking**] consistent inout order in borrow array
([#2621](#2621))
</blockquote>

## `hugr-passes`

<blockquote>

##
[0.24.1](hugr-passes-v0.24.0...hugr-passes-v0.24.1)
- 2025-11-03

### Bug Fixes

- validation outside entrypoint, normalize_cfgs w/ nonlocal edges
([#2633](#2633))
</blockquote>

## `hugr-persistent`

<blockquote>

##
[0.3.2](hugr-persistent-v0.3.1...hugr-persistent-v0.3.2)
- 2025-11-03

### New Features

- *(persistent)* More efficient HugrView iterators for PersistentHugr
([#2595](#2595))
</blockquote>

## `hugr`

<blockquote>

##
[0.24.1](hugr-v0.24.0...hugr-v0.24.1)
- 2025-11-03

### Bug Fixes

- validation outside entrypoint, normalize_cfgs w/ nonlocal edges
([#2633](#2633))
- SiblingSubgraph::try_from_nodes for non-entrypoint region
([#2655](#2655))
- Correct conversion of `table::Term::Tuple` to `ast::Term`
([#2653](#2653))

### New Features

- track package descriptions when loading
([#2639](#2639))
- *(cli)* describe sub-command
([#2650](#2650))
</blockquote>

## `hugr-cli`

<blockquote>

##
[0.24.1](hugr-cli-v0.24.0...hugr-cli-v0.24.1)
- 2025-11-03

### New Features

- track package descriptions when loading
([#2639](#2639))
- *(cli)* describe sub-command
([#2650](#2650))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
@hugrbot hugrbot mentioned this pull request Nov 3, 2025
github-merge-queue bot pushed a commit that referenced this pull request Nov 4, 2025
## 🤖 New release

* `hugr-model`: 0.24.1 -> 0.24.2
* `hugr-core`: 0.24.1 -> 0.24.2
* `hugr-llvm`: 0.24.1 -> 0.24.2
* `hugr-passes`: 0.24.1 -> 0.24.2 (✓ API compatible changes)
* `hugr`: 0.24.1 -> 0.24.2 (✓ API compatible changes)
* `hugr-cli`: 0.24.1 -> 0.24.2 (✓ API compatible changes)
* `hugr-persistent`: 0.3.2 -> 0.3.3

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

## `hugr-model`

<blockquote>

##
[0.24.1](hugr-model-v0.24.0...hugr-model-v0.24.1)
- 2025-11-03

### Bug Fixes

- Correct conversion of `table::Term::Tuple` to `ast::Term`
([#2653](#2653))
</blockquote>

## `hugr-core`

<blockquote>

##
[0.24.1](hugr-core-v0.24.0...hugr-core-v0.24.1)
- 2025-11-03

### Bug Fixes

- validation outside entrypoint, normalize_cfgs w/ nonlocal edges
([#2633](#2633))
- SiblingSubgraph::try_from_nodes for non-entrypoint region
([#2655](#2655))
- Correct conversion of `table::Term::Tuple` to `ast::Term`
([#2653](#2653))

### New Features

- track package descriptions when loading
([#2639](#2639))
- *(cli)* describe sub-command
([#2650](#2650))
</blockquote>

## `hugr-llvm`

<blockquote>

##
[0.24.0](hugr-llvm-v0.23.0...hugr-llvm-v0.24.0)
- 2025-10-13

### New Features

- LLVM lowering for borrow arrays using bitmasks
([#2574](#2574))
- *(py, core, llvm)* add `is_borrowed` op for BorrowArray
([#2610](#2610))

### Refactor

- [**breaking**] consistent inout order in borrow array
([#2621](#2621))
</blockquote>

## `hugr-passes`

<blockquote>

##
[0.24.2](hugr-passes-v0.24.1...hugr-passes-v0.24.2)
- 2025-11-03

### Bug Fixes

- ReplaceTypes: operate on whole Hugr, with set_regions
([#2662](#2662))
</blockquote>

## `hugr`

<blockquote>

##
[0.24.2](hugr-v0.24.1...hugr-v0.24.2)
- 2025-11-03

### Bug Fixes

- ReplaceTypes: operate on whole Hugr, with set_regions
([#2662](#2662))
</blockquote>

## `hugr-cli`

<blockquote>

##
[0.24.1](hugr-cli-v0.24.0...hugr-cli-v0.24.1)
- 2025-11-03

### New Features

- track package descriptions when loading
([#2639](#2639))
- *(cli)* describe sub-command
([#2650](#2650))
</blockquote>

## `hugr-persistent`

<blockquote>

##
[0.3.2](hugr-persistent-v0.3.1...hugr-persistent-v0.3.2)
- 2025-11-03

### New Features

- *(persistent)* More efficient HugrView iterators for PersistentHugr
([#2595](#2595))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
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.

hugr-llvm: Add direct lowering for borrow arrays

5 participants