Skip to content

Conversation

@mark-koch
Copy link
Collaborator

@mark-koch mark-koch commented Jun 17, 2025

Closes #1018. Tracked PRs:

BREAKING CHANGE: All to_hugr methods on types, arguments, and parameters now require a ToHugrContext
BREAKING CHANGE: CompileableDef.compile_outer now requires a ToHugrContext
BREAKING CHANGE: guppy.hugr_op now passes the compiler context to the function generating the op
BREAKING CHANGE: CheckedFunctionDef now implements MonomorphizableDef instead of CompileableDef
BREAKING CHANGE: CompilerContext.build_compiled_def now requires an instantiation for the definition's type parameters
BREAKING CHANGE: The ToHugrContext protocol now requires two additional methods: type_var_to_hugr and const_var_to_hugr
BREAKING CHANGE: CompilerContext.{compiled, worklist} and CompilationEngine.compiled are now indexed by a tuple of DefId and optional PartiallyMonomorphizedArgs

@codecov-commenter
Copy link

codecov-commenter commented Jun 17, 2025

Codecov Report

Attention: Patch coverage is 94.24779% with 26 lines in your changes missing coverage. Please review.

Project coverage is 92.32%. Comparing base (959a4e4) to head (780d964).

Files with missing lines Patch % Lines
guppylang/compiler/core.py 90.57% 13 Missing ⚠️
guppylang/decorator.py 64.28% 5 Missing ⚠️
guppylang/std/_internal/util.py 72.72% 3 Missing ⚠️
guppylang/compiler/expr_compiler.py 97.56% 1 Missing ⚠️
guppylang/std/_internal/compiler/list.py 83.33% 1 Missing ⚠️
guppylang/tys/arg.py 85.71% 1 Missing ⚠️
guppylang/tys/param.py 85.71% 1 Missing ⚠️
guppylang/tys/ty.py 97.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1033      +/-   ##
==========================================
+ Coverage   92.30%   92.32%   +0.02%     
==========================================
  Files         112      112              
  Lines       10915    11119     +204     
==========================================
+ Hits        10075    10266     +191     
- Misses        840      853      +13     

☔ 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.

mark-koch and others added 5 commits July 23, 2025 15:20
This is required as a precusor to #1018 to handle monomorphisation: When
compiling to Hugr, some bound type variables will need to be replaced
with a monotype. We will need access to the compiler context to look
this information up.

Instead of requiring the whole compiler context, I added a new
`ToHugrContext` protocol in which we can put everything that is needed
during the Hugr translation of types (empty for now but will be filled
in a subsequent PR). The main motivation for this is that the context
needs to change when we go under a binder.

BREAKING CHANGE: All `to_hugr` methods on types, arguments, and
parameters now require a `ToHugrContext`
BREAKING CHAGNE: `CompileableDef.compile_outer` now requires a
`ToHugrContext`
BREAKING CHANGE: `guppy.hugr_op` now passes the compiler context to the
function generating the op
Adds a new `guppy.const_var` declaration that generalises
`guppy.nat_var` to arbitrary copyable+droppable types. This is part of
#1033.

For basic testing, I also updated type argument parsing to accept `bool`
and `float` const literals. However, the current solution for this is
not ideal (see #1030), but should be addressed in a separate PR.

Also note that Hugr lowering of these const generics is not implemented
in this PR, so the integration tests currently only check the code
instead of compiling to Hugr. Hugr compilation via monomorphisation will
follow in a separate PR.
Lowers the const generic introduceds in #1034 to Hugr via
monomorphization. Part of #1033.

* In `definition.common`, we add new `MonomorphizableDef` abstract base
classes to mark definitions that need to be lowered via
monomorphisation. `CheckedFunctionDef` is changed to implement
`MonomorphizableDef` instead of `CompileableDef`. All other definitions
are unchanged.

* The `checker.core.CompilerContext` is the main driver for
monomorphisation:
* When calling `CompilerContext.build_compiled_def`, we now also need to
pass the type arguments for the definition we want to compile.
* If the to be compiled function is a `MonomorphizableDef`, the context
decides which of its parameters will need to be monomorphised. This is
done by the `requires_monomorphization` function. Currently, those are
exactly the non-nat const parameters of the function (note that nat
params can be lowered to Hugr bounded nats, so monomorphization is not
needed)
* From the provided type arguments, the context picks the subset that
correspond to these to-be-monomorphized parameters, which is done via
the `partially_monomorphize_args` function. We represent these partial
monomorphizations using the `PartiallyMonomorphizedArgs` type.
* A `mono_args: PartiallyMonomorphizedArgs` is a sequence of `Argument |
None`, so that `mono_args[i] == None` means that the argument with index
`i` is not part of the monomorphization. Otherwise, `mono_args[i]`
specifies the monomorphic instantiation for parameter `i`. So a function
with 3 parameters where no monomorphization is required at all, will
have `mono_args = (None, None, None)`.
* The worklist and compilation store in the `CompilerContext` are now
indexed via a pair of `DefId` and optional `PartiallyMonomorphizedArgs`.
This way, the worklist can contain the same definition multiple times,
but with different monomorphized args.
* The same also applies to the `global_funcs` stored in the context. By
taking potential mono-arguments into account, we can create multiple
global functions corresponding to the same `GlobalConstId`. This is used
in `CustomFunctionDef.load_with_args` to support custom functions with
params that require monomorphization.

* The context also has two new methods: `type_var_to_hugr` and
`const_var_to_hugr`. Those are used when compiling types and type
arguments to Hugr. The context stores the current monomorphisation of
the function that is being compiled, and when we request to lower a
variable to Hugr, the context makes sure that the de Bruijn indices are
down-shifted to account for the monomorphized parameters.
* Similarly, `ExprCompiler.visit_GenericParamValue` looks up the value
corresponding to the current monomorphization in the context

BREAKING CHANGE: `CheckedFunctionDef` now implements
`MonomorphizableDef` instead of `CompileableDef`
BREAKING CHANGE: `CompilerContext.build_compiled_def` now requires an
instantiation for the definition's type parameters
BREAKING CHANGE: The `ToHugrContext` protocol now requires two
additional methods: `type_var_to_hugr` and `const_var_to_hugr`
BREAKING CHANGE: `CompilerContext.{compiled, worklist}` and
`CompilationEngine.compiled` are now indexed by a tuple of `DefId` and
optional `PartiallyMonomorphizedArgs`

---------

Co-authored-by: Alan Lawrence <[email protected]>
@mark-koch mark-koch requested a review from ss2165 July 23, 2025 16:35
@mark-koch mark-koch marked this pull request as ready for review July 23, 2025 16:35
@mark-koch mark-koch requested a review from a team as a code owner July 23, 2025 16:35
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.

🚀

@mark-koch mark-koch added this pull request to the merge queue Jul 24, 2025
Merged via the queue into main with commit bcf9865 Jul 24, 2025
5 checks passed
@mark-koch mark-koch deleted the feat/mono branch July 24, 2025 08:07
github-merge-queue bot pushed a commit that referenced this pull request Jul 30, 2025
github-merge-queue bot pushed a commit that referenced this pull request Aug 4, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.21.0](guppylang-v0.20.0...guppylang-v0.21.0)
(2025-08-04)


### ⚠ BREAKING CHANGES

* All compiler-internal and non-userfacing functionality is moved into a
new `guppylang_internals` package
* `guppy.compile(foo)` and `guppy.check(foo)` replaced with
`foo.check()` and `foo.compile()`
* default HUGR output uses compressed binary encoding.
* `guppylang.tracing.object.GuppyDefinition` moved to
`guppylang.defs.GuppyDefinition`
`guppylang.tracing.object.TypeVarGuppyDefinition` moved and renamed to
`guppylang.defs.GuppyTypeVarDefinition`
* All `to_hugr` methods on types, arguments, and parameters now require
a `ToHugrContext` `CompileableDef.compile_outer` now requires a
`ToHugrContext` `guppy.hugr_op` now passes the compiler context to the
function generating the op `CheckedFunctionDef` now implements
`MonomorphizableDef` instead of `CompileableDef`
`CompilerContext.build_compiled_def` now requires an instantiation for
the definition's type parameters The `ToHugrContext` protocol now
requires two additional methods: `type_var_to_hugr` and
`const_var_to_hugr` `CompilerContext.{compiled, worklist}` and
`CompilationEngine.compiled` are now indexed by a tuple of `DefId` and
optional `PartiallyMonomorphizedArgs`
* comptime code that previously used constant integers outside i64 will
now fail to compile.
* Capturing closures are now disabled by default. Enabling them requires
calling `guppylang.enable_experimental_features()`, however note that
they are not supported throughout the stack.

### Features

* Add `Future` type
([#1075](#1075))
([5ad7673](5ad7673))
* add error when constant integer out of bounds
([#1084](#1084))
([eee77ae](eee77ae))
* Add guppy version metadata to hugr entrypoint
([#1039](#1039))
([0eafbd9](0eafbd9)),
closes [#1037](#1037)
* Add manual registration of extensions
([#1045](#1045))
([4b42936](4b42936))
* add qsystem op for measure leaked
([#1057](#1057))
([c555727](c555727))
* add selene via optional feature and use for testing
([#1081](#1081))
([cefc70e](cefc70e))
* Add support for V and Vdg.
([#1094](#1094))
([6b0d44a](6b0d44a))
* Allow indexing on tuples
([#1038](#1038))
([0e9097e](0e9097e)),
closes [#711](#711)
* Declare WASM modules in guppy
([#942](#942))
([e1240fb](e1240fb))
* Extend comptime arguments to arbitrary non-linear types
([#1110](#1110))
([384dd8c](384dd8c))
* Make decorator return types more precise
([#1115](#1115))
([c84e8b1](c84e8b1))
* set hugr entrypoint to compiled function
([#1063](#1063))
([16bd267](16bd267))
* store used extensions and versions in HUGR metadata
([#1049](#1049))
([a9a300c](a9a300c)),
closes [#1048](#1048)
* Support arbitrary const generics via monomorphisation
([#1033](#1033))
([bcf9865](bcf9865))
* Support Python 3.12 generic syntax
([#1051](#1051))
([ab2e118](ab2e118)),
closes [#823](#823)
* Top level compile + emulate Interface
([#1127](#1127))
([5e2f595](5e2f595))
* update to hugr-py v0.13
([#1083](#1083))
([8f071c8](8f071c8))
* use `core.` prefix for metadata keys
([#1055](#1055))
([2bf0d68](2bf0d68))


### Bug Fixes

* Allow array comprehension syntax in comptime functions
([#1068](#1068))
([da8f04a](da8f04a)),
closes [#1067](#1067)
* Allow struct redefinitions for Python &lt; 3.13
([#1108](#1108))
([959a4e4](959a4e4)),
closes [#1107](#1107)
* Correctly detect
`[@Custom](https://github.com/custom)_guppy_decorator` in nested scopes
([#1086](#1086))
([678583c](678583c))
* Fix diagnostics rendering for comptime entrypoints
([#1099](#1099))
([fdd2676](fdd2676)),
closes [#1097](#1097)
* Fix frame lookup for Python 3.12 annotation scopes
([#1120](#1120))
([a69e489](a69e489)),
closes [#1116](#1116)
* Fix hugr conversion and bounds checks on numeric literals
([#1100](#1100))
([73d5e92](73d5e92))
* Fix Jupyter notebook diagnostic rendering
([#1109](#1109))
([6002474](6002474))
* Fix nested function definitions in Python 3.12
([#1064](#1064))
([090f920](090f920))
* Stop showing temporary variables in comptime diagnostics
([#1112](#1112))
([63854c5](63854c5)),
closes [#1111](#1111)
* support comptime entrypoint
([#1079](#1079))
([721e3dd](721e3dd))
* Turn capturing closures into experimental feature
([#1065](#1065))
([a959b18](a959b18))


### Documentation

* add docstrings for emulator module
([#1131](#1131))
([b33e065](b33e065))
* add quantum and qsystem gate definitions
([#912](#912))
([32a4bbc](32a4bbc))
* Fix docstrings
([#1128](#1128))
([0aded85](0aded85))
* Improve RNG docs
([#1043](#1043))
([8640f06](8640f06))
* replace `compile_module` usage in README
([#1041](#1041))
([03ccf3a](03ccf3a))
* Update guppy examples
([#1121](#1121))
([b994655](b994655))


### Code Refactoring

* Split up into `guppylang_internals` package
([#1126](#1126))
([81d50c0](81d50c0))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: Seyon Sivarajah <[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.

Support arbitrary const generic params via monomorphisation

4 participants