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

refactor(hugr-llvm)!: Optimise the llvm types used to represent hugr sums. #1855

Merged
merged 15 commits into from
Jan 20, 2025

Conversation

doug-q
Copy link
Collaborator

@doug-q doug-q commented Jan 13, 2025

We rework LLVMSumType and LLVMValueType to have llvm representations that

  • elide the tag field when the sum type has only one variant
  • elide any "uninformational" fields, i.e. Type::UNIT.
  • elide the struct wrapper when there is only a single field
  • reuse fields from other variants where possible.

In particular, the hugr bool_t() is now represented by i1.

BREAKING CHANGE: LLVMSumValue::get_tag_type renamed to tag_type. LLVMSumType::try_new2 renamed to LLVMSumType::try_new. LLVMSumType::get_variant removed.

@doug-q doug-q requested a review from a team as a code owner January 13, 2025 11:57
@doug-q doug-q requested a review from zrho January 13, 2025 11:57
devenv.lock Outdated Show resolved Hide resolved
@hugrbot
Copy link
Collaborator

hugrbot commented Jan 13, 2025

This PR contains breaking changes to the public Rust API.

cargo-semver-checks summary

--- failure inherent_method_missing: pub method removed or renamed ---

Description:
A publicly-visible method or associated fn 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.38.0/src/lints/inherent_method_missing.ron

Failed in:
LLVMSumValue::get_tag_type, previously in file /home/runner/work/hugr/hugr/BASELINE_BRANCH/hugr-llvm/src/sum.rs:283
LLVMSumType::try_new2, previously in file /home/runner/work/hugr/hugr/BASELINE_BRANCH/hugr-llvm/src/sum.rs:35
LLVMSumType::get_tag_type, previously in file /home/runner/work/hugr/hugr/BASELINE_BRANCH/hugr-llvm/src/sum.rs:123
LLVMSumType::get_variant, previously in file /home/runner/work/hugr/hugr/BASELINE_BRANCH/hugr-llvm/src/sum.rs:139
LLVMSumType::try_new2, previously in file /home/runner/work/hugr/hugr/BASELINE_BRANCH/hugr-llvm/src/sum.rs:35
LLVMSumType::get_tag_type, previously in file /home/runner/work/hugr/hugr/BASELINE_BRANCH/hugr-llvm/src/sum.rs:123
LLVMSumType::get_variant, previously in file /home/runner/work/hugr/hugr/BASELINE_BRANCH/hugr-llvm/src/sum.rs:139

@doug-q doug-q changed the title refactor(hugr-llvm): Optimise the llvm types used to represent hugr sums. refactor(hugr-llvm)!: Optimise the llvm types used to represent hugr sums. Jan 13, 2025
@doug-q doug-q requested a review from peter-campora January 13, 2025 14:17
@doug-q
Copy link
Collaborator Author

doug-q commented Jan 13, 2025

@peter-campora I made this for you. Would you mind reviewing?

@doug-q doug-q linked an issue Jan 13, 2025 that may be closed by this pull request
@zrho
Copy link
Contributor

zrho commented Jan 13, 2025

LGTM but I currently lack context to find anything subtle here.

@qartik
Copy link
Member

qartik commented Jan 16, 2025

The LLVM output looks a lot more readable now!

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.

Very nice, the LLVM is a lot more readbale now!

My only real concerns are empty sums and my comment in build_untag

Also, it took me a bit to wrap my head around what's going on in layout_variants_impl. Left a few refactoring suggestions, but that's up to you

hugr-llvm/src/sum.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum.rs Show resolved Hide resolved
hugr-llvm/src/sum.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum.rs Outdated Show resolved Hide resolved
hugr-llvm/src/types.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum.rs Outdated Show resolved Hide resolved
@doug-q
Copy link
Collaborator Author

doug-q commented Jan 17, 2025

Great review, thank you @mark-koch .

Copy link

codecov bot commented Jan 19, 2025

Codecov Report

Attention: Patch coverage is 85.93407% with 64 lines in your changes missing coverage. Please review.

Project coverage is 86.55%. Comparing base (b38a2e8) to head (55932b7).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
hugr-llvm/src/sum.rs 86.04% 33 Missing and 9 partials ⚠️
hugr-llvm/src/extension/collections/array.rs 20.00% 1 Missing and 11 partials ⚠️
hugr-llvm/src/sum/layout.rs 95.41% 6 Missing ⚠️
hugr-llvm/src/emit/ops.rs 0.00% 0 Missing and 3 partials ⚠️
hugr-llvm/src/extension/collections/list.rs 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1855      +/-   ##
==========================================
+ Coverage   86.52%   86.55%   +0.02%     
==========================================
  Files         194      195       +1     
  Lines       35250    35569     +319     
  Branches    32063    32382     +319     
==========================================
+ Hits        30501    30786     +285     
- Misses       2973     3011      +38     
+ Partials     1776     1772       -4     
Flag Coverage Δ
python 92.34% <ø> (ø)
rust 85.98% <85.93%> (+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.

@doug-q doug-q requested a review from mark-koch January 20, 2025 08:11
@doug-q
Copy link
Collaborator Author

doug-q commented Jan 20, 2025

@mark-koch I believe I've address all of your comments. I split layout_variants_impl as you suggested and I think it turned out quite a bit better.

I have added test cases for emission build_get_tag and build_untag for each of the flavours of sum type.

I have restored handling for void sums, but I think we might need a bit more.

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 Doug, looks good!

However, something seems off in the layout since tests are failing the asserts?

hugr-llvm/src/sum/layout.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum/layout.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum.rs Show resolved Hide resolved
hugr-llvm/src/sum.rs Show resolved Hide resolved
hugr-llvm/src/sum/layout.rs Outdated Show resolved Hide resolved
hugr-llvm/src/sum.rs Outdated Show resolved Hide resolved
doug-q and others added 2 commits January 20, 2025 10:45
@doug-q doug-q added this pull request to the merge queue Jan 20, 2025
Merged via the queue into main with commit 8cbbc35 Jan 20, 2025
23 checks passed
@doug-q doug-q deleted the doug/better-sum branch January 20, 2025 11:03
@hugrbot hugrbot mentioned this pull request Jan 16, 2025
github-merge-queue bot pushed a commit that referenced this pull request Jan 20, 2025
## 🤖 New release
* `hugr`: 0.14.1 -> 0.14.2 (✓ API compatible changes)
* `hugr-core`: 0.14.1 -> 0.14.2 (✓ API compatible changes)
* `hugr-model`: 0.16.0 -> 0.17.0 (⚠️ API breaking changes)
* `hugr-llvm`: 0.14.1 -> 0.14.2 (✓ API compatible changes)
* `hugr-passes`: 0.14.1 -> 0.14.2 (✓ API compatible changes)
* `hugr-cli`: 0.14.1 -> 0.14.2 (✓ API compatible changes)

### ⚠️ `hugr-model` breaking changes

```
--- 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.38.0/src/lints/enum_variant_added.ron

Failed in:
  variant Term:Const in /tmp/.tmpszSQ4r/hugr/hugr-model/src/v0/mod.rs:591
  variant Term:ConstFunc in /tmp/.tmpszSQ4r/hugr/hugr-model/src/v0/mod.rs:697
  variant Term:ConstAdt in /tmp/.tmpszSQ4r/hugr/hugr-model/src/v0/mod.rs:703
  variant Term:Bytes in /tmp/.tmpszSQ4r/hugr/hugr-model/src/v0/mod.rs:711
  variant Term:BytesType in /tmp/.tmpszSQ4r/hugr/hugr-model/src/v0/mod.rs:717
  variant Term:Meta in /tmp/.tmpszSQ4r/hugr/hugr-model/src/v0/mod.rs:720
  variant Operation:Const in /tmp/.tmpszSQ4r/hugr/hugr-model/src/v0/mod.rs:409

--- 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.38.0/src/lints/enum_variant_missing.ron

Failed in:
  variant Term::Quote, previously in file /tmp/.tmp8fke0a/hugr-model/src/v0/mod.rs:565

--- failure struct_missing: pub struct removed or renamed ---

Description:
A publicly-visible struct cannot be imported by its prior path. A `pub use` may have been removed, or the struct 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.38.0/src/lints/struct_missing.ron

Failed in:
  struct hugr_model::v0::MetaItem, previously in file /tmp/.tmp8fke0a/hugr-model/src/v0/mod.rs:500
```

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

## `hugr`
<blockquote>

##
[0.14.2](hugr-v0.14.1...hugr-v0.14.2)
- 2025-01-20

### Bug Fixes

- Three bugfixes in model import and export. (#1844)

### Documentation

- Fix typo in `DataflowParent` doc (#1865)

### New Features

- Add CallGraph struct, and dead-function-removal pass (#1796)
- `Value::some`, `::none`, and `SumType::new_option` helpers (#1828)
- Constant values in `hugr-model` (#1838)
- *(hugr-llvm)* Emit ipow (#1839)
- Bytes literal in hugr-model. (#1845)
- Improved representation for metadata in `hugr-model` (#1849)

### Testing

- Add tests for constant value deserialization (#1822)
</blockquote>

## `hugr-core`
<blockquote>

##
[0.14.2](hugr-core-v0.14.1...hugr-core-v0.14.2)
- 2025-01-20

### Bug Fixes

- Three bugfixes in model import and export. (#1844)

### Documentation

- Fix typo in `DataflowParent` doc (#1865)

### New Features

- `Value::some`, `::none`, and `SumType::new_option` helpers (#1828)
- Constant values in `hugr-model` (#1838)
- *(hugr-llvm)* Emit ipow (#1839)
- Bytes literal in hugr-model. (#1845)
- Improved representation for metadata in `hugr-model` (#1849)

### Testing

- Add tests for constant value deserialization (#1822)
</blockquote>

## `hugr-model`
<blockquote>

##
[0.17.0](hugr-model-v0.16.0...hugr-model-v0.17.0)
- 2025-01-20

### Bug Fixes

- Three bugfixes in model import and export. (#1844)

### New Features

- Constant values in `hugr-model` (#1838)
- Bytes literal in hugr-model. (#1845)
- Improved representation for metadata in `hugr-model` (#1849)
</blockquote>

## `hugr-llvm`
<blockquote>

##
[0.14.2](hugr-llvm-v0.14.1...hugr-llvm-v0.14.2)
- 2025-01-20

### New Features

- *(hugr-llvm)* Emit more int ops (#1835)
- Constant values in `hugr-model` (#1838)
- *(hugr-llvm)* Emit ipow (#1839)

### Refactor

- *(hugr-llvm)* [**breaking**] Optimise the llvm types used to represent
hugr sums. (#1855)

### Testing

- Fix failing inot test (#1841)
</blockquote>

## `hugr-passes`
<blockquote>

##
[0.14.2](hugr-passes-v0.14.1...hugr-passes-v0.14.2)
- 2025-01-20

### New Features

- Add CallGraph struct, and dead-function-removal pass (#1796)
</blockquote>

## `hugr-cli`
<blockquote>

##
[0.14.1](hugr-cli-v0.14.0...hugr-cli-v0.14.1)
- 2024-12-18

### New Features

- Print `hugr-cli`'s correct version when using '--version' (#1790)
</blockquote>


</p></details>

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

---------

Co-authored-by: Agustín Borgna <[email protected]>
@hugrbot hugrbot mentioned this pull request Jan 20, 2025
@hugrbot hugrbot mentioned this pull request Feb 5, 2025
github-merge-queue bot pushed a commit that referenced this pull request Feb 5, 2025
## 🤖 New release

* `hugr`: 0.14.3
* `hugr-core`: 0.14.3
* `hugr-model`: 0.17.1
* `hugr-llvm`: 0.14.3
* `hugr-passes`: 0.14.3
* `hugr-cli`: 0.14.3

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

## `hugr`

<blockquote>

##
[0.14.3](hugr-v0.14.2...hugr-v0.14.3)
- 2025-02-05

### Bug Fixes

- Export `RemoveDeadFuncsError` (#1883)
- const-folding Module keeps at least "main" (#1901)
- determine correct bounds of custom types (#1888)
- Exporting converging control flow edges (#1890)

### Documentation

- Fix deprecation warning messages (#1891)
- Explain why `ConstF64` is not PartialEq (#1829)

### New Features

- Special cased array, float and int constants in hugr-model export
(#1857)
- Simplify hugr-model (#1893)
</blockquote>

## `hugr-core`

<blockquote>

##
[0.14.3](hugr-core-v0.14.2...hugr-core-v0.14.3)
- 2025-02-05

### Bug Fixes

- determine correct bounds of custom types (#1888)
- Exporting converging control flow edges (#1890)

### Documentation

- Explain why `ConstF64` is not PartialEq (#1829)

### New Features

- Special cased array, float and int constants in hugr-model export
(#1857)
- Simplify hugr-model (#1893)
</blockquote>

## `hugr-model`

<blockquote>

##
[0.17.1](hugr-model-v0.17.0...hugr-model-v0.17.1)
- 2025-02-05

### Bug Fixes

- determine correct bounds of custom types (#1888)

### New Features

- Special cased array, float and int constants in hugr-model export
(#1857)
- Simplify hugr-model (#1893)
- Do not require `capnp` to be installed to compile `hugr-model` (#1907)
</blockquote>

## `hugr-llvm`

<blockquote>

##
[0.14.2](hugr-llvm-v0.14.1...hugr-llvm-v0.14.2)
- 2025-01-20

### New Features

- *(hugr-llvm)* Emit more int ops (#1835)
- Constant values in `hugr-model` (#1838)
- *(hugr-llvm)* Emit ipow (#1839)

### Refactor

- *(hugr-llvm)* [**breaking**] Optimise the llvm types used to represent
hugr sums. (#1855)

### Testing

- Fix failing inot test (#1841)
</blockquote>

## `hugr-passes`

<blockquote>

##
[0.14.3](hugr-passes-v0.14.2...hugr-passes-v0.14.3)
- 2025-02-05

### Bug Fixes

- Export `RemoveDeadFuncsError` (#1883)
- const-folding Module keeps at least "main" (#1901)

### Documentation

- Fix deprecation warning messages (#1891)
</blockquote>

## `hugr-cli`

<blockquote>

##
[0.14.1](hugr-cli-v0.14.0...hugr-cli-v0.14.1)
- 2024-12-18

### New Features

- Print `hugr-cli`'s correct version when using '--version' (#1790)
</blockquote>


</p></details>

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

---------

Co-authored-by: Agustín Borgna <[email protected]>
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.

Optimise our sum representation
5 participants