Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support stack_ for primitives that allocate #3471

Merged
merged 2 commits into from
Feb 12, 2025
Merged

Support stack_ for primitives that allocate #3471

merged 2 commits into from
Feb 12, 2025

Conversation

riaqn
Copy link
Contributor

@riaqn riaqn commented Jan 14, 2025

This PR allows writing stack_ (ref "hello").

See my comments below for details.

@riaqn riaqn marked this pull request as draft January 14, 2025 17:59
@goldfirere
Copy link
Collaborator

I see why we might want this, but was this change motivated by a concrete use? In addition, a return mode of poly isn’t always right — for example, %identity has that property, I believe, but isn’t an allocation. If we wanted to do this, I think it would be better just to list out the primitives that optionally stack-allocate. But I probably favor not doing this at all, but instead to make the stack-allocation apparent in the named external, like ref_stack. If someone wants to ensure a stack allocation, they just use ref_stack.

@goldfirere
Copy link
Collaborator

OK — I found the Slack thread that provides more context (and that already suggests not to use the “poly” criterion). But I still think that we should just have a stack_ref instead of allowing this. Let’s continue the design debate on Slack.

@mshinwell mshinwell added the locals Local allocation label Jan 15, 2025
@riaqn riaqn force-pushed the stack-makemutable branch from dcc5810 to b775384 Compare January 21, 2025 16:12
@riaqn riaqn changed the title stack_ supports all primitives that returns poly return mode Introduce [@alloc_poly] for primitives to indicate allocation Jan 21, 2025
@riaqn
Copy link
Contributor Author

riaqn commented Jan 21, 2025

As discussed offline, we now introduce a new attribute [@alloc_poly] on external:

external[@alloc_poly] ref : 'a -> ('a ref[@local_opt]) = "%makemutable"

which indicates that the [@local_opt] is in fact an allocation. Both the behavior of stack_, and the register_allocation in type_ident are improved based on this.

This change needs to be noted upon release: users might need to update their external to use this attribute, to ensure the existing register_allocation optimization continues to happen.

Updated stdlib to use this, so that the tests pass. I might have missed some (and will be fixed in future PRs).

@riaqn riaqn force-pushed the stack-makemutable branch 3 times, most recently from 1f8e34c to bc59dfc Compare January 22, 2025 10:28
@riaqn
Copy link
Contributor Author

riaqn commented Jan 27, 2025

This is currently blocked by some miscompilation issue on arm64 and GI regalloc (shown in CI). AFAIK @xclerc is looking.

@riaqn riaqn force-pushed the stack-makemutable branch from bc59dfc to 84ce878 Compare February 7, 2025 17:21
@riaqn
Copy link
Contributor Author

riaqn commented Feb 7, 2025

Per offline discussion with @goldfirere , this is now implemented differently and no longer requires additional user annotations.

When user writes stack_ (%foo bar) where foo is a primitive, the typing makes the whole expression local and triggers mode error if the expected mode is more strict than that. If typing goes through, it gets picked up again by lambda and:

  • check that foo is indeed a lambda primitive that allocates. External C functions will be rejected. Nonallocating primitives such as %identity will be rejected.
  • if so, check that foo, given how its declared (by local_opt or local_), will allocate on stack.

@riaqn riaqn marked this pull request as ready for review February 7, 2025 17:23
@riaqn riaqn requested a review from ncik-roberts February 7, 2025 17:23
@riaqn riaqn changed the title Introduce [@alloc_poly] for primitives to indicate allocation Support stack_ for primitives that allocate Feb 7, 2025
@riaqn riaqn force-pushed the stack-makemutable branch from 84ce878 to 45e31af Compare February 7, 2025 17:43
@riaqn riaqn force-pushed the stack-makemutable branch from 5772934 to c97634a Compare February 12, 2025 14:25
@riaqn riaqn requested review from ccasin and removed request for ncik-roberts February 12, 2025 14:25
Copy link
Collaborator

@goldfirere goldfirere left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks

lambda/translprim.ml Outdated Show resolved Hide resolved
@riaqn riaqn force-pushed the stack-makemutable branch from 3bc16d4 to a7f0ee4 Compare February 12, 2025 15:46
@riaqn riaqn enabled auto-merge (squash) February 12, 2025 15:46
@riaqn riaqn merged commit c0d19af into main Feb 12, 2025
24 checks passed
@riaqn riaqn deleted the stack-makemutable branch February 12, 2025 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
locals Local allocation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants