Treat length of sequences as a "slot" in Useless optimization #586
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.
In the
Value.t
representation of the Useless optimization, sequence and tuple elements are "slots", which combine aUseful.t
lattice element with anExists.t
lattice element. When a value flows, vectors and tuples coerce theUseful.t
component of slots but unify theExists.t
component. This ensures that agree on whether or not the element exists even if they disagree that the element is useful. If the "from"'s element is useful (and necessarily existing) but the "to"'s element is not useful, then forcing the "to"'s element to exist avoids a potentially expensive runtime coercion (e.g., to drop a component of a tuple or, worse, to drop a component of a tuple that is the contents of a vector).Previously, the length of a sequence was represented simply as a
Useful.t
; this allowed a vector (whose contents were not useful) with a useful length to flow to a vector (whose contents are necessarily not useful) with a useless length. 1aad5fe (Optimize representation of sequences in Useless pass; #569) allowed the Useless optimization to change the representation of sequence with useless contents. In particular, a vector with useless contents but useful length becomes aword64
and a vector with useless contents and useless length becomes aunit
.However, when such vectors are themselves components of a tuple, then the program may have a flow of tuples, where the source tuple is changed from
(..., ?? vector, ...)
to(..., word64, ...)
but the destination tuple is changed from(..., ?? vector, ...)
to(..., unit, ...)
. (Note that the unification of theExist.t
components of the corresponding tuple elements is what makes the destination tuple have aunit
element.)This commit treats the length of arrays and vectors as a "slot", so that they track both usefulness and existence. Now, a vector with useless contents but a length that must exist becomes a
word64
(even if the length is useless) and a vector with useless contents and a length that need not exist (and is necessarily useless) becomes aunit
.Fixes #585.