Skip to content
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

[DCE] Tweaked code to end borrows before destroys. #39760

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Oct 14, 2021

If a phi argument is dead and reborrowing it was dependent on some other value, that other value on which it was dependent may have already itself been deleted. In that case, the destroy_value would have been added just before the terminator of the predecessors of the block which contained the dead phi. So, when deciding where to insert the end_borrow, iterate backwards from the end of the block, skipping the terminator updating the insertion point every time a destroy_value instruction is encountered until we hit an instruction with a different opcode. This ensures that no matter how many destroy_values may have been added just before the terminator, the end_borrow will preceed them.

This commit just tweaks the preexisting logic that checked for this condition. Specifically, the previous code didn't handle the case where the block contains only a terminator and a destroy_value.

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler force-pushed the dce/end-borrow-before-destroy-value branch from d46e2bf to f40f7d6 Compare October 14, 2021 23:19
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please clean test windows platform

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

The original code looks like it could be secretly shrinking the borrow scope, and consequently reordering destroys. Not something I expected this pass to do. It's probably ok but either merits

  • a warning in the file-level pass comment that this pass shrinks scopes and reoders destroys
  • or a FIXME here to make sure the scope is only hoisted over newly inserted destroys. That would require a SmallPtrSet of inserted destroy_value instructions

@nate-chandler nate-chandler force-pushed the dce/end-borrow-before-destroy-value branch 2 times, most recently from bd2ae13 to 8dc6ae6 Compare October 19, 2021 18:48
If a phi argument is dead and it reborrowing it was dependent on some
other value, that other value on which it was dependent may have already
itself been deleted.  In that case, the destroy_value would have been
added just before the terminator of the predecessors of the block which
contained the dead phi.  So, when deciding where to insert the
end_borrow, iterate backwards from the end of the block, skipping the
terminator updating the insertion point every time a destroy_value
instruction is encountered until we hit an instruction with a different
opcode.  This ensures that no matter how many destroy_values may have
been added just before the terminator, the end_borrow will preceed them.

This commit just tweaks the preexisting logic that checked for this
condition.  Specifically, the previous code didn't handle the case where
the block contains only a terminator and a destroy_value.
@nate-chandler nate-chandler force-pushed the dce/end-borrow-before-destroy-value branch from 8dc6ae6 to 4d92cce Compare October 19, 2021 19:05
@nate-chandler
Copy link
Contributor Author

nate-chandler commented Oct 19, 2021

Added a FIXME. It seems like destroys might also be getting reordered depending on the ordering of the phis:

sil hidden [ossa] @foo : $@convention(thin) (Builtin.Int1, @owned Klass, @owned Klass) -> () {
  bb0(%condition : $Builtin.Int1, %instance_1 : @owned $Klass, %instance_2 : @owned $Klass):
    cond_br %condition, bb1, bb2

  bb1:
    br bb3(%instance_1 : $Klass, %instance_2 : $Klass)

  bb2:
    br bb3(%instance_1 : $Klass, %instance_2 : $Klass)

  bb3(%original_in_1 : @owned $Klass, %original_in_2 : @owned $Klass):
    destroy_value %original_in_1 : $Klass
    destroy_value %original_in_2 : $Klass
    %result = tuple ()
    return %result : $()
}

->

sil hidden [ossa] @add_end_borrow_after_destroy_value : $@convention(thin) (Builtin.Int1, @owned Klass, @owned Klass) -> () {
// %1                                             // user: %4
// %2                                             // user: %3
bb0(%0 : $Builtin.Int1, %1 : @owned $Klass, %2 : @owned $Klass):
  destroy_value %2 : $Klass                       // id: %3
  destroy_value %1 : $Klass                       // id: %4
  br bb1                                          // id: %5

bb1:                                              // Preds: bb0
  %6 = tuple ()                                   // user: %7
  return %6 : $()                                 // id: %7
} // end sil function 'add_end_borrow_after_destroy_value'

@nate-chandler
Copy link
Contributor Author

@swift-ci please smoke test and merge

@nate-chandler
Copy link
Contributor Author

@swift-ci please smoke test linux platform

@nate-chandler nate-chandler merged commit 367734a into swiftlang:main Oct 19, 2021
@nate-chandler nate-chandler deleted the dce/end-borrow-before-destroy-value branch October 19, 2021 22:29
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.

2 participants