Skip to content

feat: add inlining annotation + pass#1449

Open
acl-cqc wants to merge 32 commits into
mainfrom
acl/inline
Open

feat: add inlining annotation + pass#1449
acl-cqc wants to merge 32 commits into
mainfrom
acl/inline

Conversation

@acl-cqc
Copy link
Copy Markdown
Contributor

@acl-cqc acl-cqc commented Mar 16, 2026

Intended as precursor to #1532: this adds the "Always" option only, but takes into account in its own pass, which raises an error if it cannot obey all the annotations.

Python interface is very WIP, not sure I've grokked everything there yet.

@aborgna-q aborgna-q force-pushed the ab/hugr-0.26.0 branch 10 times, most recently from cb4d32c to 2b29cdb Compare March 18, 2026 17:25
Base automatically changed from ab/hugr-0.26.0 to main March 19, 2026 10:33
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 97.44898% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.60%. Comparing base (084c9de) to head (c955955).

Files with missing lines Patch % Lines
tket/src/passes/inline_always.rs 98.33% 3 Missing ⚠️
tket-py/tket/passes/__init__.py 85.71% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1449      +/-   ##
==========================================
+ Coverage   85.52%   85.60%   +0.07%     
==========================================
  Files         184      185       +1     
  Lines       29077    29271     +194     
  Branches    27876    28056     +180     
==========================================
+ Hits        24868    25057     +189     
- Misses       3075     3080       +5     
  Partials     1134     1134              
Flag Coverage Δ
python 90.40% <86.66%> (-0.06%) ⬇️
qis-compiler 91.66% <ø> (ø)
rust 85.39% <98.34%> (+0.08%) ⬆️

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.

Comment thread tket-py/test/test_pass.py
Comment thread tket-py/test/test_pass.py Outdated
@acl-cqc acl-cqc requested a review from aborgna-q May 12, 2026 14:29
@acl-cqc acl-cqc marked this pull request as ready for review May 12, 2026 14:29
@acl-cqc acl-cqc requested a review from a team as a code owner May 12, 2026 14:29
@hugrbot
Copy link
Copy Markdown
Collaborator

hugrbot commented May 21, 2026

This PR contains breaking changes to the public Rust API.
Please deprecate the old API instead (if possible), or mark the PR with a ! to indicate a breaking change.

cargo-semver-checks summary
    Building tket v0.18.0 (current)
     Built [  45.319s] (current)
   Parsing tket v0.18.0 (current)
    Parsed [   0.120s] (current)
  Building tket v0.18.0 (baseline)
     Built [  43.166s] (baseline)
   Parsing tket v0.18.0 (baseline)
    Parsed [   0.106s] (baseline)
  Checking tket v0.18.0 -> v0.18.0 (assume minor change)
   Checked [   0.141s] 196 checks: 194 pass, 1 fail, 1 warn, 56 skip

--- failure enum_no_repr_variant_discriminant_changed: enum variant had its discriminant change value ---

Description:
The enum's variant had its discriminant value change. This breaks downstream code that used its value via a numeric cast like `as isize`.
      ref: https://doc.rust-lang.org/reference/items/enumerations.html#assigning-discriminant-values
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.47.0/src/lints/enum_no_repr_variant_discriminant_changed.ron

Failed in:
variant InlineAnnotation::BestEffort 0 -> 1 in /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/metadata.rs:60
variant InlineAnnotation::Never 1 -> 2 in /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/metadata.rs:62

--- warning partial_ord_enum_variants_reordered: enum variants reordered in #[derive(PartialOrd)] enum ---

Description:
A public enum that derives PartialOrd had its variants reordered. #[derive(PartialOrd)] uses the enum variant order to set the enum's ordering behavior, so this change may break downstream code that relies on the previous order.
      ref: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html#derivable
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.47.0/src/lints/partial_ord_enum_variants_reordered.ron

Failed in:
InlineAnnotation::BestEffort moved from position 1 to 2, in /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/metadata.rs:60
InlineAnnotation::Never moved from position 2 to 3, in /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/metadata.rs:62

   Summary semver requires new major version: 1 major and 0 minor checks failed
   Warning produced 1 major and 0 minor level warnings
  Finished [  90.135s] tket
  Building tket-qec v0.1.0 (current)
     Built [  39.059s] (current)
   Parsing tket-qec v0.1.0 (current)
    Parsed [   0.006s] (current)
  Building tket-qec v0.1.0 (baseline)
     Built [  39.023s] (baseline)
   Parsing tket-qec v0.1.0 (baseline)
    Parsed [   0.006s] (baseline)
  Checking tket-qec v0.1.0 -> v0.1.0 (assume minor change)
   Checked [   0.015s] 196 checks: 196 pass, 56 skip
   Summary no semver update required
  Finished [  79.288s] tket-qec
  Building tket-qsystem v0.24.0 (current)
     Built [  45.172s] (current)
   Parsing tket-qsystem v0.24.0 (current)
    Parsed [   0.026s] (current)
  Building tket-qsystem v0.24.0 (baseline)
     Built [  44.494s] (baseline)
   Parsing tket-qsystem v0.24.0 (baseline)
    Parsed [   0.026s] (baseline)
  Checking tket-qsystem v0.24.0 -> v0.24.0 (assume minor change)
   Checked [   0.060s] 196 checks: 196 pass, 56 skip
   Summary no semver update required
  Finished [  91.201s] tket-qsystem

Copy link
Copy Markdown
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

Some comments, mostly about differentiating the two passes.

Although from the technical side it makes sense to have two of them (we need to do different kinds of traversals for each), I struggle to justify having the split passes from the user point of view.
When would I want to "inline some of the annotated functions, but not the others that I've also annotated"?

Comment thread tket-py/test/test_pass.py
f_id.set_outputs(f_id.input_node[0])

if annotate:
f_id.metadata["tket.inline"] = "always"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
f_id.metadata["tket.inline"] = "always"
f_id.metadata[InlineAnnotation] = "always"

Comment thread tket-py/tket/metadata.py
InlineAnnotationValue: TypeAlias = Literal["never", "best_effort", "always"]


class InlineAnnotation(Metadata[InlineAnnotationValue]):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This needs updates in the docs and in from_json

Comment on lines +28 to +31
/// A [ComposablePass] that inlines `Call`s to functions
/// according to [InlineAnnotation]s.
#[derive(Default, Clone, Debug)]
pub struct InlineAlwaysPass {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should explain what are the differences between this pass an InlineFunctionsPass, not only here but in the other pass' docs and on the python definitions too.

Some(InlineAnnotation::Never) => false,
Some(InlineAnnotation::BestEffort) => true,
None => self.heuristic.should_inline(func, h),
Some(InlineAnnotation::Always) // instruction to InlineAlwaysPass
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm thinking we should treat Always as BestEffort here. We won't inline every occurrence, but at least we should inline the ones we can.

Comment thread tket/src/metadata.rs
pub enum InlineAnnotation {
/// Always inline calls to this function.
///
/// If this cannot be done, an error will be raised.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would probably merit a mention to the different inline passes.

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.

3 participants