6.2: [DCE] Don't delete instructions which consume escaping values. #80781
+85
−21
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.
Explanation: Fix a lifetime-shortening miscompile in DCE.
Dead code elimination deletes instructions. Some of those instructions consume their operands. When such an instruction is deleted, a compensating lifetime-ending instruction (e.g.
destroy_value
) must be inserted. In simple cases, the pass has its own logic to determine where to insert the compensating lifetime-ending instruction. For more complex cases, it depends on the lifetime completion utility, inserting compensating lifetime ends at the liveness boundary. The utility requires (and can't be made not to) complete liveness of the value that it's called on: it is not permitted to complete the lifetime at the liveness boundary of a value which does not have complete liveness. Why? The boundary isn't known. In particular, it is not permitted if the value has a pointer escape.Previously, however, DCE used the utility even in such cases. This could result in inserting a compensating lifetime-ending instruction just after the value escapes to a pointer. Code which then used that pointer would be using a dangling pointer.
Here, the pass is fixed to check whether an instruction consumes a value which escapes to a pointer. If it does, it marks the instruction live, preventing it from being deleted.
Scope: Affects optimized code.
Issue: rdar://149007151
Original PR: #80766
Risk: Low, the fix is to prevent the pass from making a change occasionally.
Testing: Added test.
Reviewer: Andrew Trick ( @atrick )