Skip to content

Cleanup dead phis in SILMem2Reg for OSSA #38265

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
merged 3 commits into from
Jul 8, 2021

Conversation

meg-gupta
Copy link
Contributor

@meg-gupta meg-gupta commented Jul 6, 2021

Mem2Reg creates phis proactively that may be unnecessary.
Unnecessary phis are those without uses or operands to other
proactive phis that are unnecessary.

Even though this does not translate to a real issue at runtime, we can
see ownership verification errors. This PR identifies such phis and deletes them.

Fixes rdar://79349413

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta meg-gupta force-pushed the alternatemem2regfix2 branch 2 times, most recently from 0c7b3eb to 1268709 Compare July 6, 2021 19:51
@meg-gupta meg-gupta marked this pull request as ready for review July 6, 2021 19:52
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta meg-gupta requested a review from gottesmm July 6, 2021 19:54
@meg-gupta
Copy link
Contributor Author

@gottesmm I opened this in favor of the approach in #36339

@swift-ci
Copy link
Contributor

swift-ci commented Jul 6, 2021

Build failed
Swift Test Linux Platform
Git Sha - 1268709898fb0a25deb39223aec660f5659ec316

@meg-gupta
Copy link
Contributor Author

@swift-ci clean test Linux Platform


// If already visited, don't explore further.
if (visitedPhis.contains(phi))
return true;
Copy link
Contributor

@eeckstein eeckstein Jul 7, 2021

Choose a reason for hiding this comment

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

return true looks wrong here. E.g.

bb1(%phi1):
  br bb2(%phi1)
bb2(%phi2): // not dead!
  // use %phi2

Let's say isUnnecessaryPhi is called for %phi1 first - and returns false (which is correct). When isUnnecessaryPhi is then called for %phi2, which is already in visitedPhis, it will return true.

I don't think you can solve the dead-phi-removal problem by walking the def-use graph in "forward" direction.
Instead, you can mark all used phi arguments and propagate those flags backward. Like it's done in DeadCodeElimination.

Copy link
Contributor Author

@meg-gupta meg-gupta Jul 7, 2021

Choose a reason for hiding this comment

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

I clear the visitedPhis set in the main loop before successive calls. It's not actually a cache. Going to look at DCE now.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, I didn't see that. But then the problem is that the algorithm is quadratic - in case of long phi-chains.

BTW, as you can have only one phi argument per block, you could also use a BasicBlockSet: visitedBlock instead of visitedPhis. With that you don't need to keep the set around for multiple isUnnecessaryPhi calls for performance reasons. You can just declare it inside isUnnecessaryPhi

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 am following the approach of backward propagating like DCE now. Thanks for pointing me to that. I did leave the new livePhis as a SmallPtrSet for readability reasons.

@meg-gupta meg-gupta force-pushed the alternatemem2regfix2 branch from 1268709 to 5966ed4 Compare July 8, 2021 07:30
meg-gupta added 2 commits July 8, 2021 00:33
Mem2Reg creates phis proactively that may be unnecessary.
Unnecessary phis are those without uses or operands to other
proactive phis that are unnecessary.

Even though this does not translate to a real issue at runtime, we can
see ownership verification errors. This PR identifies such phis and deletes them.
With such alloc_stack we can pass over-consumed values to a
proactively added phi. Even though such a SIL does not have
issues at runtime, they raise an ownership verification error.
@meg-gupta meg-gupta force-pushed the alternatemem2regfix2 branch from 5966ed4 to dc57d6b Compare July 8, 2021 07:33
@meg-gupta
Copy link
Contributor Author

@swift-ci test

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

LGTM!

@swift-ci
Copy link
Contributor

swift-ci commented Jul 8, 2021

Build failed
Swift Test OS X Platform
Git Sha - dc57d6b

@meg-gupta
Copy link
Contributor Author

lldb test failed..
@swift-ci test

@meg-gupta
Copy link
Contributor Author

@eeckstein Thanks for the review!

@swift-ci
Copy link
Contributor

swift-ci commented Jul 8, 2021

Build failed
Swift Test Linux Platform
Git Sha - dc57d6b

@meg-gupta
Copy link
Contributor Author

@swift-ci clean test Linux Platform

@meg-gupta meg-gupta merged commit 5cb144f into swiftlang:main Jul 8, 2021
@meg-gupta meg-gupta deleted the alternatemem2regfix2 branch July 8, 2021 22:15
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