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 monomorphization pass #1733

Merged
merged 72 commits into from
Dec 12, 2024
Merged

feat!: Add monomorphization pass #1733

merged 72 commits into from
Dec 12, 2024

Conversation

acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented Dec 2, 2024

Closes #1630

#1772 tracks adding tests for substitute.

BREAKING CHANGE: Optrait gains Sized and Clone as supertraits and is no longer object safe.
BREAKING CHANGE: DataflowOptrait gains Sized as supertrait.
BREAKING CHANGE: DataflowOptrait has an additional required method substitute.

Copy link

codecov bot commented Dec 2, 2024

Codecov Report

Attention: Patch coverage is 90.61625% with 67 lines in your changes missing coverage. Please review.

Project coverage is 86.72%. Comparing base (187ea8f) to head (ae2b850).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
hugr-core/src/ops/dataflow.rs 55.55% 28 Missing ⚠️
hugr-passes/src/monomorphize.rs 95.91% 3 Missing and 17 partials ⚠️
hugr-core/src/ops/controlflow.rs 91.30% 10 Missing ⚠️
hugr-core/src/ops/custom.rs 72.00% 7 Missing ⚠️
hugr-core/src/types/type_param.rs 71.42% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1733      +/-   ##
==========================================
+ Coverage   86.68%   86.72%   +0.04%     
==========================================
  Files         184      186       +2     
  Lines       33169    34000     +831     
  Branches    30044    30875     +831     
==========================================
+ Hits        28752    29486     +734     
- Misses       2765     2845      +80     
- Partials     1652     1669      +17     
Flag Coverage Δ
python 92.57% <ø> (ø)
rust 86.13% <90.61%> (+0.06%) ⬆️

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.

@hugrbot
Copy link
Collaborator

hugrbot commented Dec 2, 2024

This PR contains breaking changes to the public Rust API.

cargo-semver-checks summary

--- failure trait_added_supertrait: non-sealed trait added new supertraits ---

Description:
A non-sealed trait added one or more supertraits, which breaks downstream implementations of the trait
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#generic-bounds-tighten
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.38.0/src/lints/trait_added_supertrait.ron

Failed in:
trait hugr_core::ops::OpTrait gained Sized in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/ops.rs:372
trait hugr_core::ops::OpTrait gained Clone in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/ops.rs:372
trait hugr_core::ops::dataflow::DataflowOpTrait gained Sized in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/ops/dataflow.rs:16
trait hugr_core::ops::DataflowOpTrait gained Sized in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/ops/dataflow.rs:16

--- failure trait_method_added: pub trait method added ---

Description:
A non-sealed public trait added a new method without a default implementation, which breaks downstream implementations of the trait
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-item-no-default
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.38.0/src/lints/trait_method_added.ron

Failed in:
trait method hugr_core::ops::dataflow::DataflowOpTrait::substitute in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/ops/dataflow.rs:57
trait method hugr_core::ops::DataflowOpTrait::substitute in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/ops/dataflow.rs:57

--- failure trait_no_longer_object_safe: trait no longer object safe ---

Description:
Trait is no longer object safe, which breaks `dyn Trait` usage.
      ref: https://doc.rust-lang.org/stable/reference/items/traits.html#object-safety
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.38.0/src/lints/trait_no_longer_object_safe.ron

Failed in:
trait OpTrait in file /home/runner/work/hugr/hugr/PR_BRANCH/hugr-core/src/ops.rs:372

@doug-q doug-q changed the title feat: Monomorphize Hugrs feat!: Monomorphize Hugrs Dec 12, 2024
@doug-q doug-q changed the title feat!: Monomorphize Hugrs feat!: Add monomorphization pass. Dec 12, 2024
@doug-q doug-q changed the title feat!: Add monomorphization pass. feat!: Add monomorphization pass Dec 12, 2024
@ss2165 ss2165 self-requested a review December 12, 2024 10:26
}

fn escape_dollar(str: impl AsRef<str>) -> String {
str.as_ref().replace("$", "\\$")
Copy link
Member

Choose a reason for hiding this comment

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

seems a bit risky to be sanitising strings ourselves, should we use a library?

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are no rules on what names functions are allowed, so sanitising is too strong a word for this.

The worst that can happen is that we end up with non-unique names, which is allowed by HUGR and the docs of monomorphize.

Surely there is room for a better, well motivated, and principled design. I think that this is the best we can do for now.

If you have specific suggestions I'm happy to change.

hugr-core/src/builder/build_traits.rs Outdated Show resolved Hide resolved
@@ -259,6 +279,14 @@ impl DataflowOpTrait for OpaqueOp {
fn signature(&self) -> Cow<'_, Signature> {
Cow::Borrowed(&self.signature)
}

fn substitute(&self, subst: &crate::types::Substitution) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this isn't covered, it should be right?

hugr-passes/src/monomorphize.rs Outdated Show resolved Hide resolved
hugr-passes/src/monomorphize.rs Outdated Show resolved Hide resolved
@@ -367,6 +419,26 @@ impl DataflowOpTrait for LoadFunction {
fn static_input(&self) -> Option<EdgeKind> {
Some(EdgeKind::Function(self.func_sig.clone()))
}

Copy link
Member

Choose a reason for hiding this comment

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

yeah there's a lot of substitute code not covered here :/

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that is addressed in the commit message. We will add proptests in a future PR

hugr-passes/src/monomorphize.rs Outdated Show resolved Hide resolved
hugr-passes/src/monomorphize.rs Outdated Show resolved Hide resolved
doug-q and others added 2 commits December 12, 2024 11:11
hugr-passes/src/monomorphize.rs Outdated Show resolved Hide resolved
@@ -367,6 +419,26 @@ impl DataflowOpTrait for LoadFunction {
fn static_input(&self) -> Option<EdgeKind> {
Some(EdgeKind::Function(self.func_sig.clone()))
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that is addressed in the commit message. We will add proptests in a future PR

hugr-core/src/builder/build_traits.rs Outdated Show resolved Hide resolved
}

fn escape_dollar(str: impl AsRef<str>) -> String {
str.as_ref().replace("$", "\\$")
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are no rules on what names functions are allowed, so sanitising is too strong a word for this.

The worst that can happen is that we end up with non-unique names, which is allowed by HUGR and the docs of monomorphize.

Surely there is room for a better, well motivated, and principled design. I think that this is the best we can do for now.

If you have specific suggestions I'm happy to change.

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.

thanks :)

@doug-q doug-q added this pull request to the merge queue Dec 12, 2024
Merged via the queue into main with commit b63aabc Dec 12, 2024
24 checks passed
@doug-q doug-q deleted the acl/monomorphize branch December 12, 2024 12:29
@hugrbot hugrbot mentioned this pull request Dec 12, 2024
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.

Monomorphization Pass
4 participants