Skip to content

Revert recent dead-code elimination improvement in constant prop. #40317

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 1 commit into from
Nov 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,15 @@ ConstantFolder::processWorkList() {
}

// Go through all users of the constant and try to fold them.
//
// FIXME: remove this temporary deleter. It is dangerous because any use of
// the original deleter will invalidate its iterators. It is currently used
// to work around bugs that are exposed in the -Onone stdlib build when the
// same deleter is used for both the dead code elimination above and the
// dead use elimination below.
auto tempCallbacks = deleter.getCallbacks();
InstructionDeleter tempDeleter(std::move(tempCallbacks));

for (auto Result : I->getResults()) {
for (auto *Use : Result->getUses()) {
SILInstruction *User = Use->getUser();
Expand All @@ -1841,7 +1850,7 @@ ConstantFolder::processWorkList() {
// this as part of the constant folding logic, because there is no value
// they can produce (other than empty tuple, which is wasteful).
if (isa<CondFailInst>(User))
deleter.trackIfDead(User);
tempDeleter.trackIfDead(User);

// See if we have an instruction that is read none and has a stateless
// inverse. If we do, add it to the worklist so we can check its users
Expand Down Expand Up @@ -1947,15 +1956,15 @@ ConstantFolder::processWorkList() {
// it, we exit the worklist as expected.
SILValue r = User->getResult(Index);
if (r->use_empty()) {
deleter.trackIfDead(User);
tempDeleter.trackIfDead(User);
continue;
}

// Otherwise, do the RAUW.
User->getResult(Index)->replaceAllUsesWith(C);
// Record the user if it is dead to perform the necessary cleanups
// later.
deleter.trackIfDead(User);
tempDeleter.trackIfDead(User);

// The new constant could be further folded now, add it to the
// worklist.
Expand All @@ -1967,7 +1976,7 @@ ConstantFolder::processWorkList() {

// Eagerly DCE. We do this after visiting all users to ensure we don't
// invalidate the uses iterator.
deleter.cleanupDeadInstructions();
tempDeleter.cleanupDeadInstructions();
}

// TODO: refactor this code outside of the method. Passes should not merge
Expand Down