Conversation
6e43c90 to
fc6b555
Compare
Contributor
|
What exactly was the unsoundness with the previous approach? (I think I'm missing some context here) At first sight, the new approach looks more complicated and fragile: instead of the invariant that each element of tuple_modes is always below |
stedolan
reviewed
Aug 13, 2024
Contributor
stedolan
left a comment
There was a problem hiding this comment.
After an offline explanation, this LGTM.
(Code looks good but I think the comments explaining tuple_modes could be improved)
stedolan
approved these changes
Aug 13, 2024
ITO444
pushed a commit
to ITO444/flambda-backend
that referenced
this pull request
Aug 14, 2024
Merged
lukemaurer
pushed a commit
that referenced
this pull request
Oct 23, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current
mode_coercehas a soundness bug wrt its treatment oftuple_modes. The bug was exposed during development of #2741, but in the end worked around so it won't be exposed in that particular way.It is still a bug and needs to be fixed. In this PR:
tuple_modes.The design
I tried to understand the current design of
tuple_modesbut found it difficult (partially because I've messed it up over time). So instead I came up with a new design that works as follows. Consider the example:The simplest design, which is to not have
tuple_modesat all, is to have a singlemodethat will the mode of(e0, e1)and(x0, x1). That means if any ofe0/e1islocal, bothx0/x1will belocal. However, a typical ocaml programer thinks tuples as primitive langauge construct, and they would expectx0to beglobalife0isglobalregardless ofe1.To that end, we consider a canonical example to motivate our design:
We want the following two groups of mode constraints to hold.
The
regional_to_localmapping is to represent allocation: if bothe0ande1areregional, the tuple will be allocated in the current regionlocalinstead of the parent regionregional. In implementation we move it to the RHS of<=asregional_to_global.Following the existing design, we first look at the pattern: If the pattern is a tuple pattern, we create a mode variable
modefor the tuple, and a list of mode variablestuple_modescorresponding to tuple fields. If the pattern is not tuple pattern, we lettuple_modes = None. Bothmodeandtuple_modesare used to constructexpected_pat_modeandexpected_mode, passed to patterns and expressions accordingly.The type checking of patterns will try to make use of
tuple_modesto get better modes; but it's sound to ignore that field. For the above example,xwill enjoymode, and[x0;x1]will enjoytuple_modes.The type checking of expression is responsible for enforcing the above constraints. If the expression is
Pexp_tuple [e0; e1], we enforce the first group of constraints. Otherwise the expression is some generale, we enforce the second group of constraints. In any case, iftuple_modes = None, then we don't need to enforce the related constraints.Note: it's unsound for the type-checking of expressions to ignore
expected_mode.tuple_modes. Therefore, I replaced all usage ofexpected_mode.modewithas_single_mode expected_modeorget_single_mode_exn expected_mode. The former allows treatingexpected_modeas a single mode. The latter is used when we are suretuple_modeswill beNone. To do this properly we should makeexpected_modeabstract (future work 😉 ).