Skip to content

[sil-optimizer] Make InstructionDeleter and related APIs to use an InstModCallback instead of a notification callback. #37016

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

Merged

Conversation

gottesmm
Copy link
Contributor

@gottesmm gottesmm commented Apr 22, 2021

I recently have been running into the issue that many of these APIs perform the
deletion operation themselves and notify the caller it is going to delete
instead of allowing the caller to specify how the instruction is deleted. This
causes interesting semantic issues (see the loop in deleteInstruction I
simplified) and breaks composition since many parts of the optimizer use
InstModCallbacks for this purpose.

To fix this, I added a notify will be deleted construct to InstModCallback. In a
similar way to the rest of it, if the notify is not set, we do not call any code
implying that we should have good predictable performance in loops since we will
always skip the function call.

I also changed InstModCallback::deleteInst() to notify before deleting so we
have a default safe behavior. All previous use sites of this API do not care
about being notified and the only new use sites of this API are in
InstructionDeleter that perform special notification behavior (it notifies for
certain sets of instructions it is going to delete before it deletes any of
them). To work around this, I added a bool to deleteInst to control this
behavior and defaulted to notifying. This should ensure that all other use sites
still compose correctly.

@gottesmm
Copy link
Contributor Author

@swift-ci test

/// default operation inline. What is nice about this from a perf perspective is
/// that in a loop this property should predict well since you have a single
/// branch that is going to go the same way everytime.
class InstModCallbacks {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just moved this to work around forward declaration issues.

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 35bb49c1a3a49b8f35b862aeee78d42b51fe6043

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 35bb49c1a3a49b8f35b862aeee78d42b51fe6043

…stModCallback instead of a notification callback.

I recently have been running into the issue that many of these APIs perform the
deletion operation themselves and notify the caller it is going to delete
instead of allowing the caller to specify how the instruction is deleted. This
causes interesting semantic issues (see the loop in deleteInstruction I
simplified) and breaks composition since many parts of the optimizer use
InstModCallbacks for this purpose.

To fix this, I added a notify will be deleted construct to InstModCallback. In a
similar way to the rest of it, if the notify is not set, we do not call any code
implying that we should have good predictable performance in loops since we will
always skip the function call.

I also changed InstModCallback::deleteInst() to notify before deleting so we
have a default safe behavior. All previous use sites of this API do not care
about being notified and the only new use sites of this API are in
InstructionDeleter that perform special notification behavior (it notifies for
certain sets of instructions it is going to delete before it deletes any of
them). To work around this, I added a bool to deleteInst to control this
behavior and defaulted to notifying. This should ensure that all other use sites
still compose correctly.
@gottesmm gottesmm force-pushed the pr-eb1b4b484d8990f6f5cb6706dba952b3e27cdc59 branch from 35bb49c to 7b55cbc Compare April 26, 2021 23:39
@gottesmm gottesmm changed the title [inst-opt-util] Make InstructionDeleter and related APIs to use an InstModCallback instead of a notification callback. [sil-optimizer] Make InstructionDeleter and related APIs to use an InstModCallback instead of a notification callback. Apr 26, 2021
@gottesmm
Copy link
Contributor Author

@swift-ci test

@gottesmm gottesmm requested a review from atrick April 26, 2021 23:46
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.

LGTM as long as you comment the callback in another PR explaining the two separate use cases and the fact that InstructionDeleter (for some reason) wants to drop all refs for a bunch of instructions before actually deleting them.

@gottesmm
Copy link
Contributor Author

Thanks will do.

@atrick
Copy link
Contributor

atrick commented Apr 27, 2021

@gottesmm And I almost forgot. We shouldn't need two copies of eraseFromParentWithDebugInsts. When you have callbacks, you should be able to just grab the onDelete callback and forward that along.

@gottesmm gottesmm merged commit 5900a0f into swiftlang:main Apr 27, 2021
@gottesmm
Copy link
Contributor Author

@atrick I will fix that as well.

@gottesmm gottesmm deleted the pr-eb1b4b484d8990f6f5cb6706dba952b3e27cdc59 branch April 27, 2021 04:03
@gottesmm
Copy link
Contributor Author

@atrick the callback in eraseFromParentWithDebugInsts is for a notification, not the delete. That is why I did both.

gottesmm added a commit that referenced this pull request Apr 27, 2021
…481e36205ea258

[sil-optimizer] Fix feedback from #37016 and finish removing confusing constructors
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.

3 participants