Skip to content

fix: reuse generated operation functions - #1995

Merged
qartik merged 5 commits into
mainfrom
codex-reuse-generated-op-functions
Sep 11, 2026
Merged

qartik merged 5 commits into
mainfrom
codex-reuse-generated-op-functions

Conversation

@qartik

@qartik qartik commented Sep 11, 2026

Copy link
Copy Markdown
Member

Summary

  • make cached OpFunctionMap definitions public while HUGR linking runs
  • reuse NodeTemplate::call_to_function so OnMultiDefn::UseTarget coalesces repeated definitions
  • rely on the existing qsystem hiding phase to restore generated helpers to private visibility
  • add generic and wrapped-barrier regressions asserting repeated operations share one definition

Root cause

OpFunctionMap cached one generated HUGR per operation shape, but its replacement template embedded a private function HUGR at every call site. Name linking only coalesces public definitions, so OnMultiDefn::UseTarget had no effect and repeated barriers produced thousands of equivalent wrapper functions. Wide wrappers then amplified LLVM IPSCCP memory use.

The fix belongs in func_as_node_template: cached helpers are now linker-visible when passed through the existing call_to_function path. QSystemRebasePass already records pre-existing public functions and hides newly introduced helpers after lowering, so the final HUGR retains private linkage without adding a parallel call-template mechanism.

Validation

  • cargo check -p tket -p tket-qsystem
  • cargo test -p tket --lib (604 passed, 2 ignored)
  • cargo test -p tket-qsystem --lib (167 passed, 1 ignored)
  • just check: formatting, Ruff, mypy, Cargo check/docs, and pg-libs tests passed; the full local gate remains blocked by unrelated existing nightly Clippy findings and macOS hugrenv @rpath resolution in aggregate nextest/Python discovery
  • git diff --check origin/main...HEAD

@codecov

codecov Bot commented Sep 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.14286% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.17%. Comparing base (be362a3) to head (d50d194).

Files with missing lines Patch % Lines
tket/src/passes/utils/unpack_container.rs 78.94% 0 Missing and 8 partials ⚠️
...c/passes/utils/unpack_container/op_function_map.rs 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1995      +/-   ##
==========================================
- Coverage   87.18%   87.17%   -0.01%     
==========================================
  Files         201      201              
  Lines       31957    32010      +53     
  Branches    30377    30430      +53     
==========================================
+ Hits        27861    27905      +44     
- Misses       2891     2892       +1     
- Partials     1205     1213       +8     
Flag Coverage Δ
python 91.81% <ø> (ø)
qis-compiler 74.88% <ø> (ø)
rust 86.93% <87.14%> (-0.01%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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.

@qartik
qartik marked this pull request as ready for review September 11, 2026 13:39
@qartik
qartik requested a review from a team as a code owner September 11, 2026 13:39
@qartik
qartik requested a review from aborgna-q September 11, 2026 13:39
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 11, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-11T13:44:02.113465Z ec600da Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation consistently reuses inserted definitions and includes targeted regression coverage.

Pull request overview

Reuses cached lowering functions to prevent duplicate private function definitions and excessive LLVM memory use.

Changes:

  • Adds templates for calling existing HUGR functions.
  • Inserts each cached lowering function once.
  • Adds repeated-operation and barrier regressions.
File summaries
File Description
tket/src/passes/utils/unpack_container/op_function_map.rs Inserts cached definitions once and registers direct calls.
tket/src/passes/utils/unpack_container.rs Tests shared generic lowering functions.
tket/src/passes/replace_types.rs Adds existing-function call templates.
tket-qsystem/src/extension/qsystem/barrier.rs Tests shared wrapped-barrier functions.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@aborgna-q
aborgna-q requested a review from acl-cqc September 11, 2026 14:14
@aborgna-q

Copy link
Copy Markdown
Collaborator

This looks like a bug in NodeTemplate::call_to_function; since the created function is private we’re not unifying the calls even with OnMultiDefn::UseTarget set.

This PR adds a new NodeTemplate::CallFunction, but I’m wondering if we should fix call_to_function to create public definitions instead. We'd be polluting the public namespace, but perhaps we can mark them as private afterwards?

@acl-cqc

acl-cqc commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Yes, func_as_node_template is designed to use linking to combine multiple function definitions together. "Public" means "visible to the linker", the function should be public. (We used to have a CallFunction but it was removed in favour of this mechanism.)

See also Quantinuum/hugr#2766, but note that for the moment QSystemRebasePass has a hide_funcs mechanism to make functions non-public after linking is done.

Possibly func_as_node_template should check that the function is public, and/or make it so.

You can also link in FuncDecls to call for each op, then construct the body that you want only if some FuncDecl was actually added, if constructing the body is expensive, but this do-compilation-faster has not seemed worth the complexity so far.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Public helper names are not unique for every cached operation shape, causing linker signature conflicts.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread tket/src/passes/utils/unpack_container/op_function_map.rs
@qartik
qartik added this pull request to the merge queue Sep 11, 2026
Merged via the queue into main with commit d9692dd Sep 11, 2026
27 checks passed
@qartik
qartik deleted the codex-reuse-generated-op-functions branch September 11, 2026 16:47
@hugrbot hugrbot mentioned this pull request Sep 8, 2026
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.

4 participants