Skip to content

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions include/swift/Basic/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,12 @@
#define SWIFT_CRASH_BUG_REPORT_MESSAGE \
"Please " SWIFT_BUG_REPORT_MESSAGE_BASE " and the crash backtrace."

#ifdef NDEBUG
#define SWIFT_ASSERT_ONLY_DECL(X)
#define SWIFT_ASSERT_ONLY(X) do { } while (false)
#else
#define SWIFT_ASSERT_ONLY_DECL(X) X
#define SWIFT_ASSERT_ONLY(X) do { X; } while (false)
#endif

#endif // SWIFT_BASIC_COMPILER_H
17 changes: 12 additions & 5 deletions include/swift/SIL/SILValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,15 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
/// lifetime constraints.
struct OperandOwnership {
enum innerty : uint8_t {
/// Uses of ownership None. These uses are incompatible with values that
/// have ownership but are otherwise not verified.
None,
/// Operands that do not use the value. They only represent a dependence
/// on a dominating definition and do not require liveness.
/// (type-dependent operands)
NonUse,

/// Uses that can only handle trivial values. The operand value must have
/// None ownership. These uses require liveness but are otherwise
/// unverified.
TrivialUse,

/// Use the value only for the duration of the operation, which may have
/// side effects. Requires an owned or guaranteed value.
Expand Down Expand Up @@ -726,8 +732,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
/// Defined inline so the switch is eliminated for constant OperandOwnership.
inline OwnershipConstraint OperandOwnership::getOwnershipConstraint() {
switch (value) {
case OperandOwnership::None:
case OperandOwnership::TrivialUse:
return {OwnershipKind::None, UseLifetimeConstraint::NonLifetimeEnding};
case OperandOwnership::NonUse:
case OperandOwnership::InstantaneousUse:
case OperandOwnership::UnownedInstantaneousUse:
case OperandOwnership::ForwardingUnowned:
Expand Down Expand Up @@ -770,7 +777,7 @@ ValueOwnershipKind::getForwardingOperandOwnership(bool allowUnowned) const {
}
llvm_unreachable("invalid value ownership");
case OwnershipKind::None:
return OperandOwnership::None;
return OperandOwnership::TrivialUse;
case OwnershipKind::Guaranteed:
return OperandOwnership::ForwardingBorrow;
case OwnershipKind::Owned:
Expand Down
187 changes: 187 additions & 0 deletions include/swift/SILOptimizer/Utils/CanonicalOSSALifetime.h
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)) {
Copy link
Contributor

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:

while (auto *copy = dyn_cast<CopyValueInst>(v)) {
  v = copy->getOperand();
}
return 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
10 changes: 1 addition & 9 deletions include/swift/SILOptimizer/Utils/PrunedLiveness.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@

#include "swift/SIL/SILBasicBlock.h"

#ifdef NDEBUG
#define SWIFT_ASSERT_ONLY_MEMBER(X)
#define SWIFT_ASSERT_ONLY(X) do { } while (false)
#else
#define SWIFT_ASSERT_ONLY_MEMBER(X) X
#define SWIFT_ASSERT_ONLY(X) do { X; } while (false)
#endif

namespace swift {

/// Discover "pruned" liveness for an arbitrary set of uses. The client builds
Expand Down Expand Up @@ -140,7 +132,7 @@ class PrunedLiveBlocks {
llvm::SmallDenseMap<SILBasicBlock *, bool, 4> liveBlocks;

// Once the first use has been seen, no definitions can be added.
SWIFT_ASSERT_ONLY_MEMBER(bool seenUse = false);
SWIFT_ASSERT_ONLY_DECL(bool seenUse = false);

public:
bool empty() const { return liveBlocks.empty(); }
Expand Down
Loading