Skip to content

Conversation

@hargoniX
Copy link
Contributor

@hargoniX hargoniX commented Dec 6, 2025

This PR introduces the new tagged_return attribute. It allows users to mark extern declarations to be guaranteed to always return tagged return values. Unlike with object or tobject the compiler does not emit reference counting operations for them. In the future information from this attribute will be used for a more powerful analysis to remove reference counts when possible.

@hargoniX hargoniX force-pushed the hbv/better_size_types branch from 9c8223e to b508730 Compare December 6, 2025 01:42
@hargoniX
Copy link
Contributor Author

hargoniX commented Dec 6, 2025

!bench

@leanprover-radar
Copy link

leanprover-radar commented Dec 6, 2025

Benchmark results for b508730 against 72ddc47 are in! @hargoniX

@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Dec 6, 2025
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 72ddc479bf038b6d94f12ae1dd08f28019be9712 --onto 455fd0b4488e2adc85f825a52e2ee7d944a5740a. You can force Mathlib CI using the force-mathlib-ci label. (2025-12-06 02:42:06)

@leanprover-bot
Copy link
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 72ddc479bf038b6d94f12ae1dd08f28019be9712 --onto d3dda9f6d4428a906c096067ecb75e432afc4615. You can force reference manual CI using the force-manual-ci label. (2025-12-06 02:42:07)

@hargoniX hargoniX force-pushed the hbv/better_size_types branch from b508730 to 41a78a2 Compare December 6, 2025 10:57
@hargoniX
Copy link
Contributor Author

hargoniX commented Dec 6, 2025

!bench

@leanprover-radar
Copy link

leanprover-radar commented Dec 6, 2025

Benchmark results for 41a78a2 against 72ddc47 are in! @hargoniX

Minor changes (1)
  • size/all/.ir//bytes: -717kiB (-0.2%)

@Rob23oba
Copy link
Contributor

Rob23oba commented Dec 6, 2025

Right, I also once thought about how you could optimize with knowing that certain things are scalars and in particular, I thought about the opposite direction: inferring that something must be a scalar because of being a parameter to a function (e.g. the index for unchecked getElem accesses with proofs) and also how that might allow converting Nat operations to USize operations (e.g. ByteArray.get => ByteArray.uget, Nat.add => USize.add), such that we could get array[x + 1] to just be an unbox + USize add + direct array access through uget.

@hargoniX
Copy link
Contributor Author

hargoniX commented Dec 7, 2025

Yes, that's what TODO 3 is alluding to. I'm pretty sure all of this can be done with a data flow analysis. But regardless of how that analysis will be implemented, we need to store the information that some extern functions return tagged values statically so that's why I started this way.

@hargoniX hargoniX force-pushed the hbv/better_size_types branch from 41a78a2 to 5258628 Compare December 8, 2025 10:32
@hargoniX hargoniX force-pushed the hbv/better_size_types branch from 5258628 to 3d91c9c Compare December 8, 2025 10:33
@hargoniX hargoniX added the changelog-compiler Compiler, runtime, and FFI label Dec 8, 2025
@hargoniX hargoniX changed the title perf: infer better IR types to reduce refcounting feat: tagged_return attribute Dec 8, 2025
@hargoniX hargoniX marked this pull request as ready for review December 8, 2025 10:39
@hargoniX hargoniX requested a review from leodemoura as a code owner December 8, 2025 10:39
@hargoniX hargoniX enabled auto-merge December 8, 2025 10:39
@hargoniX hargoniX added this pull request to the merge queue Dec 8, 2025
Merged via the queue into master with commit ecce5e6 Dec 8, 2025
28 checks passed
algebraic-dev pushed a commit that referenced this pull request Dec 8, 2025
This PR introduces the new `tagged_return` attribute. It allows users to
mark `extern` declarations to be guaranteed to always return `tagged`
return values. Unlike with `object` or `tobject` the compiler does not
emit reference counting operations for them. In the future information
from this attribute will be used for a more powerful analysis to remove
reference counts when possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-compiler Compiler, runtime, and FFI toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants