Skip to content

add [%exclave] support#51

Merged
lpw25 merged 9 commits intoocaml-flambda:mainfrom
riaqn:unregion
Apr 17, 2023
Merged

add [%exclave] support#51
lpw25 merged 9 commits intoocaml-flambda:mainfrom
riaqn:unregion

Conversation

@riaqn
Copy link
Copy Markdown
Contributor

@riaqn riaqn commented Oct 14, 2022

This patch adds the support for the [%exclave] extension, which can be applied to an expression and results in early ending of the current region before the expression is evaluated. As such, it can only appear in the tail position of the region. A motivating example is as follows:

let foo (local_ x) = 
let local_ y = (complex computation on x) in 
if y then [%exclave] (local_ None)
else  [%exclave] (local_ (Some x))

where foo is supposed to be some helper function that takes arguments from parent region and returns values also in parent region. The intermediate results (y) it uses are stored in its local stack and released upon entering [%exclave]. As a result, you are not allowed to refer to y in [%exclave].

Copy link
Copy Markdown
Contributor

@lpw25 lpw25 left a comment

Choose a reason for hiding this comment

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

A few changes needed but mostly looks good.

@riaqn riaqn force-pushed the unregion branch 2 times, most recently from fa20d57 to 2411722 Compare October 21, 2022 17:16
@riaqn
Copy link
Copy Markdown
Contributor Author

riaqn commented Nov 3, 2022

This PR has been rebased on top of version 4.14.

@riaqn
Copy link
Copy Markdown
Contributor Author

riaqn commented Nov 4, 2022

The commit 5bb4963 (after discussion with @lpw25 who discussed with @stedolan ) made it so that any function ending with [%unregion] will always return local_. This is to accommodate some code in the compiler which relies on that bit to decide if the function might allocate in parent region. This also (unexpectedly) gives the expected output for a test case that wasn't working before (and I didn't know why).

Comment on lines +566 to +616
| Texp_unregion exp ->
Pexp_apply ({
pexp_desc =
Pexp_extension
({ txt = "ocaml.unregion"; loc}
, PStr []);
pexp_loc = loc;
pexp_loc_stack = [];
pexp_attributes = [];
}, [Nolabel, sub.expr sub exp])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I recall that this file is mostly deprecated, but I wrote what I think is right anyway.

Comment on lines +3540 to +3880
| Pexp_apply
({ pexp_desc = Pexp_extension({
txt = "extension.unregion" | "ocaml.unregion" | "unregion" as txt}, PStr []) },
[Nolabel, sbody]) ->
if (txt == "extension.unregion") && not (Clflags.Extension.is_enabled Local) then
raise (Typetexp.Error (loc, Env.empty, Local_not_enabled));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm merging the two very similar pattern-matching cases, and testing for one of them in the body. This might not be a good coding style, but the alternative is to have an auxiliary function that has too many arguments.

Comment on lines 158 to 159
| Texp_unregion e ->
classify_expression env e
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please confirm this is correct

| Texp_probe {handler} ->
expression handler << Dereference
| Texp_probe_is_enabled _ -> empty
| Texp_unregion e -> expression e
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

please confirm this is correct

sub.expr sub e
| Texp_probe {handler;_} -> sub.expr sub handler
| Texp_probe_is_enabled _ -> ()
| Texp_unregion exp -> sub.expr sub exp
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please confirm this is correct

Comment on lines +417 to +440
| Texp_unregion exp ->
Texp_unregion (sub.expr sub exp)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please confirm this is correct

| Texp_letop _
| Texp_extension_constructor _ ->
false
| Texp_unregion e -> is_nonexpansive e
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please confirm this is correct

Comment on lines 3119 to 3652
| Texp_unregion e ->
check e
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

please confirm this is correct

@antalsz
Copy link
Copy Markdown
Contributor

antalsz commented Jan 12, 2023

We should strongly consider switching to using [@unregion] (separately, I want to make that change for [%local]); in terms of making sure code is compatible with standard OCaml, unregion is perfectly ignorable. Also, if we want to support an unregion_ keyword, then we also need to update our syntax-rewriter to support [@unregion]; that's a job for me (as long as the location information is propagated correctly), just let me know when it's necessary.

@goldfirere
Copy link
Copy Markdown
Contributor

Yes -- let's use attributes for anything that can safely be just ignored in the upstream compiler.

@riaqn riaqn force-pushed the unregion branch 2 times, most recently from 79e7e52 to 5f8ca38 Compare January 20, 2023 14:16
@riaqn
Copy link
Copy Markdown
Contributor Author

riaqn commented Jan 23, 2023

Hmm do you think I should make the change in this PR, or do it some time later? @antalsz @goldfirere

@goldfirere
Copy link
Copy Markdown
Contributor

Antal may see things differently, but I think it's best to merge patches when they're not going to cause trouble for others. I think the infrastructure to handle e.g. [@unregion] correctly is still some time away, and so merging what you have and then quickly (this week?) changing it sounds reasonable to me.

@antalsz
Copy link
Copy Markdown
Contributor

antalsz commented Jan 23, 2023

Because we can handle [@unregion] correctly via the refactor-based rewriter, I'd prefer making that change in ocaml-jst before we roll it out to users. Whether that means in this PR or a different PR is something I'm happy to leave in your hands, @riaqn. Also, are we planning to add syntax for it?

@riaqn
Copy link
Copy Markdown
Contributor Author

riaqn commented Feb 9, 2023

I suggest that this PR to be reviewed as it is, without migrating to the new extension syntax framework, because currently the whole local extension is still living in the old world.

Sometime in the future we should migrate the whole local extension to the new syntax framework, but that would deserve a separate PR.

Maybe we can start reviewing now? @stedolan @lpw25

Comment on lines +396 to +415
(* CR zqian: this is suspicious,
because vmode is not the mode of the region;
ideally the `position` should contain the mode
of the region so we can pass it to mode_return;
but this might be fine because of mode-crossing? *)
mode_return vmode
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This one needs some inspection.

@riaqn riaqn requested a review from lpw25 March 6, 2023 10:09
@riaqn riaqn changed the title add [%unregion] support add [%exclave] support Mar 6, 2023
@riaqn
Copy link
Copy Markdown
Contributor Author

riaqn commented Mar 8, 2023

This PR relies on oxcaml/oxcaml#1131 to be merged safely.

Copy link
Copy Markdown
Contributor

@lpw25 lpw25 left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Should be good to go once various superficial things are fixed.

Copy link
Copy Markdown
Contributor

@lpw25 lpw25 left a comment

Choose a reason for hiding this comment

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

Small small suggestions then this is good to go

@lpw25 lpw25 merged commit a2556fc into ocaml-flambda:main Apr 17, 2023
antalsz pushed a commit to antalsz/ocaml-jst that referenced this pull request May 1, 2023
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