-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Enable the CopyPropagation pass #35047
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
034b440
Clearly discriminate OperandOwnership::NonUse and TrivialUse.
atrick e5a4b6b
Fix OperandOwnership for end_apply and convert_escape_to_noescape
atrick 50d7172
Move SWIFT_ASSERT_ONLY to Compiler.h
atrick 392014b
Add a CanonicalOSSALifetime utility.
atrick ce64630
Enable the CopyPropagation pass.
atrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
include/swift/SILOptimizer/Utils/CanonicalOSSALifetime.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
//===--- CanonicalOSSALifetime.h - Canonicalize OSSA lifetimes --*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// Canonicalize the copies and destroys of a single owned or guaranteed OSSA | ||
/// value. | ||
/// | ||
/// This top-level API rewrites the extended lifetime of a SILValue: | ||
/// | ||
/// void canonicalizeValueLifetime(SILValue def, CanonicalOSSALifetime&) | ||
/// | ||
/// The extended lifetime transitively includes the uses of `def` itself along | ||
/// with the uses of any copies of `def`. | ||
/// | ||
/// FIXME: Canonicalization currently bails out if any uses of the def has | ||
/// OperandOwnership::PointerEscape. Once project_box is protected by a borrow | ||
/// scope and mark_dependence is associated with an end_dependence, | ||
/// canonicalization will work everywhere as intended. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_SILOPTIMIZER_UTILS_CANONICALOSSALIFETIME_H | ||
#define SWIFT_SILOPTIMIZER_UTILS_CANONICALOSSALIFETIME_H | ||
|
||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/ADT/SetVector.h" | ||
#include "swift/SIL/SILInstruction.h" | ||
#include "swift/SILOptimizer/Utils/PrunedLiveness.h" | ||
|
||
namespace swift { | ||
|
||
struct CanonicalOSSALifetime; | ||
|
||
/// Top-Level API: rewrites copies and destroys within \p def's extended | ||
/// lifetime. \p lifetime caches transient analysis state across multiple calls | ||
/// and indicates whether any SILAnalyses must be invalidated. | ||
/// | ||
/// Return false if the OSSA structure cannot be recognized (with a proper OSSA | ||
/// representation this will always return true). | ||
bool canonicalizeValueLifetime(SILValue def, CanonicalOSSALifetime &lifetime); | ||
|
||
/// Find the original definition of a potentially copied value. | ||
/// | ||
/// This use-def walk must be consistent with the def-use walks performed within | ||
/// the CanonicalOSSALifetime implementation. | ||
inline SILValue getCanonicalCopiedDef(SILValue v) { | ||
while (true) { | ||
if (auto *copy = dyn_cast<CopyValueInst>(v)) { | ||
v = copy->getOperand(); | ||
continue; | ||
} | ||
return v; | ||
} | ||
} | ||
|
||
/// Information about consumes on the extended-lifetime boundary. Consuming uses | ||
/// within the lifetime are not included--they will consume a copy after | ||
/// rewriting. | ||
/// | ||
/// This result remains valid during copy rewriting. The only instructions | ||
/// referenced it contains are consumes that cannot be deleted. | ||
class CanonicalOSSAConsumeInfo { | ||
// Map blocks on the lifetime boundary to the last consuming instruction. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 slashes here. |
||
llvm::SmallDenseMap<SILBasicBlock *, SILInstruction *, 4> finalBlockConsumes; | ||
|
||
public: | ||
bool empty() const { return finalBlockConsumes.empty(); } | ||
|
||
void clear() { finalBlockConsumes.clear(); } | ||
|
||
void recordFinalConsume(SILInstruction *inst) { | ||
assert(!finalBlockConsumes.count(inst->getParent())); | ||
finalBlockConsumes[inst->getParent()] = inst; | ||
} | ||
|
||
// Return true if this instruction is marked as a final consume point of the | ||
// current def's live range. A consuming instruction can only be claimed once | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 slashes here. |
||
// because instructions like `tuple` can consume the same value via multiple | ||
// operands. | ||
bool claimConsume(SILInstruction *inst) { | ||
auto destroyPos = finalBlockConsumes.find(inst->getParent()); | ||
if (destroyPos != finalBlockConsumes.end() && destroyPos->second == inst) { | ||
finalBlockConsumes.erase(destroyPos); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
SWIFT_ASSERT_ONLY_DECL(void dump() const LLVM_ATTRIBUTE_USED); | ||
}; | ||
|
||
/// Canonicalize OSSA lifetimes. | ||
/// | ||
/// Allows the allocation of analysis state to be reused across calls to | ||
/// canonicalizeValueLifetime(). | ||
struct CanonicalOSSALifetime { | ||
// Current copied def for which this state describes the liveness. | ||
SILValue currDef; | ||
|
||
// Cumulatively, have any instructions been modified by canonicalization? | ||
bool changed = false; | ||
|
||
// Pruned liveness for the extended live range including copies. For this | ||
// purpose, only consuming instructions are considered "lifetime | ||
// ending". end_borrows do not end a liverange that may include owned copies. | ||
PrunedLiveness liveness; | ||
|
||
// Original points in the CFG where the current value's lifetime ends. This | ||
// includes any point in which the value is consumed or destroyed. For | ||
// guaranteed values, it also includes points where the borrow scope | ||
// ends. A backward walk from these blocks must discover all uses on paths | ||
// that lead to a return or throw. | ||
// | ||
// These blocks are not necessarily in the pruned live blocks since | ||
// pruned liveness does not consider destroy_values. | ||
llvm::SmallSetVector<SILBasicBlock *, 8> lifetimeEndBlocks; | ||
|
||
public: | ||
CanonicalOSSAConsumeInfo consumes; | ||
|
||
SILValue def() const { return currDef; } | ||
|
||
void initDef(SILValue def) { | ||
assert(lifetimeEndBlocks.empty() && liveness.empty()); | ||
consumes.clear(); | ||
|
||
currDef = def; | ||
liveness.initializeDefBlock(def->getParentBlock()); | ||
} | ||
|
||
void clearLiveness() { | ||
lifetimeEndBlocks.clear(); | ||
liveness.clear(); | ||
} | ||
|
||
bool isChanged() const { return changed; } | ||
|
||
void setChanged() { changed = true; } | ||
|
||
void updateLivenessForUse(Operand *use) { | ||
// Because this liverange may include owned copies, only record consuming | ||
// instructions as "lifetime ending". | ||
bool consuming = | ||
use->isLifetimeEnding() | ||
&& (use->get().getOwnershipKind() == OwnershipKind::Owned); | ||
liveness.updateForUse(use, consuming); | ||
} | ||
|
||
PrunedLiveBlocks::IsLive getBlockLiveness(SILBasicBlock *bb) const { | ||
return liveness.getBlockLiveness(bb); | ||
} | ||
|
||
enum IsInterestingUser { NonUser, NonConsumingUse, ConsumingUse }; | ||
IsInterestingUser isInterestingUser(SILInstruction *user) const { | ||
// Translate PrunedLiveness "lifetime-ending" uses to consuming uses. For | ||
// the purpose of an extended liverange that includes owned copies, an | ||
// end_borrow is not "lifetime-ending". | ||
switch (liveness.isInterestingUser(user)) { | ||
case PrunedLiveness::NonUser: | ||
return NonUser; | ||
case PrunedLiveness::NonLifetimeEndingUse: | ||
return NonConsumingUse; | ||
case PrunedLiveness::LifetimeEndingUse: | ||
return ConsumingUse; | ||
} | ||
} | ||
|
||
void recordLifetimeEnd(Operand *use) { | ||
lifetimeEndBlocks.insert(use->getUser()->getParent()); | ||
} | ||
|
||
ArrayRef<SILBasicBlock *> getLifetimeEndBlocks() const { | ||
return lifetimeEndBlocks.getArrayRef(); | ||
} | ||
}; | ||
|
||
} // end namespace swift | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have noticed that we are able to do this as follows. Looks much cleaner: