Skip to content

Variant unboxing for parameters and returns #2328

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lthls
Copy link
Contributor

@lthls lthls commented Mar 1, 2024

This PR adds support for variant unboxing on top of #1271.

I don't think we should merge it, as currently the code doesn't simplify away as well as we want to (even with Simplify). We will likely need some match-in-match support, and maybe some better support for poison-like values.
Also, we should probably restrict it to a subset of variant types for which it is reasonable.

But it should still work at preventing allocations:

let[@inline never] f (x[@unboxable] : _ result) =
  match x with Error _ -> 0 | Ok n -> n

let g x err =
  f (Ok x) + f (Error err)

Here, g will not allocate anything, but the unboxed version of f will do three switches instead of one. One is there because this PR always adds a switch on is_int, which for result should always return false, and the two other ones are equivalent but cannot be simplified yet.

@mshinwell mshinwell added the flambda2 Prerequisite for, or part of, flambda2 label Mar 4, 2024
@mshinwell
Copy link
Collaborator

Let's restrict this to reasonable types, and put it behind a flag for the moment, since we need match-in-match to avoid code being worse in the case where the unboxed function gets inlined.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
flambda2 Prerequisite for, or part of, flambda2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants