Skip to content

5.9: [FieldSensitivePL] Fix vectorization. #66735

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

Conversation

nate-chandler
Copy link
Contributor

Description: Fix (and enable fixing) a class of miscompiles in the move-only address checker.

The move-only address checker relies heavily on FieldSensitivePrunedLiveness (FSPL), a vectorization of PrunedLiveness (PL). FSPL of width N needs to be able to represent exactly the same information as N copies of PL. In particular, for any instruction, must be able to be a non-user, a non-consuming user, or a consuming user of each of the N fields.

Previously, however, that was not true. FSPL stored a range [i, i+k) of elements at which a given instruction was live. Additionally it stored only a single bit for whether the instruction did or did not consume that range.

The result was incorrect liveness computations in cases where disjoint fields were used, such as

apply(
  @owned (struct_element_addr %s, #S.f1),
  @owned (struct_element_addr %s, #S.f3)
)

and in cases where not all the fields had the same consumed-ness

apply(
  @owned (struct_element_addr %s, #S.f1),
  @guaranteed (struct_element_addr %s, #S.f2)
)

(In the above, S is a move-only struct with three fields, f1, f2, f3 in that order.)

The fix is to change the record corresponding to an instruction to have enough information to store all these possibilities. Here, the representation of two bit vectors of length N, one for live-at and a second for consumed-at, is implemented.
Risk: Low. The fix is involved, but the current behavior is definitely wrong.
Scope: Narrow. This only affects move-only types.
Original PR: #66690
Reviewed By: Andrew Trick ( @atrick )
Testing: Current unit tests and two new tests which were previously miscompiled--though note that a second patch will be necessary to fix the second of these two.
Resolves: rdar://110909290

According to language rules, such lifetimes are fixed and the relative
order of their deinits is guaranteed.

rdar://110913116
The members were declared but undefined.
Its storage vector is intended to be of some type like
`std::vector<std::pair<Key, Optional<Value>>>`, i.e., some collection of
pairs whose `second` is an `Optional<Value>`.  So when constructing a
default instance of that pair, just construct an Optional in the None
case.
FieldSensitivePrunedLiveness is used as a vectorization of
PrunedLiveness.  An instance of FSPL with N elements needs to be able to
represent the same states as N instances of PL.

Previously, it failed to do that in two significant ways:

(1) It attempted to save space for which elements were live by using
    a range.  This failed to account for instructions which are users of
    non-contiguous fields of an aggregate.

    apply(
      @owned (struct_element_addr %s, #S.f1),
      @owned (struct_element_addr %s, #S.f3)
    )

(2) It used a single bit to represent whether the instruction was
    consuming.  This failed to account for instructions which consumed
    some fields and borrowed others.

    apply(
      @owned (struct_element_addr %s, #S.f1),
      @guaranteed (struct_element_addr %s, #S.f2)
    )

The fix for (1) is to use a bit vector to represent which elements
are used by the instruction.  The fix for (2) is to use a second bit
vector to represent which elements are _consumed_ by the instruction.

Adapted the move-checker to use the new representation.

rdar://110909290
@nate-chandler nate-chandler requested a review from a team as a code owner June 17, 2023 21:08
@nate-chandler nate-chandler requested review from atrick and tbkka June 17, 2023 21:11
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler merged commit 7c1f23c into swiftlang:release/5.9 Jun 19, 2023
@nate-chandler nate-chandler deleted the cherrypick/release/5.9/rdar110676577 branch June 19, 2023 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants