Skip to content
Merged
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
29 changes: 29 additions & 0 deletions lib/SILOptimizer/ARC/ARCRegionState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
#include "swift/SILOptimizer/Analysis/LoopRegionAnalysis.h"
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"

using namespace swift;

llvm::cl::opt<bool> verifyARCLoopSummary(
"verify-arc-loop-summary", llvm::cl::init(false),
llvm::cl::desc("Verify if loop summary is correct in ARCLoopsOpts"));

//===----------------------------------------------------------------------===//
// ARCRegionState
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -282,6 +287,18 @@ bool ARCRegionState::processLoopBottomUp(
OtherState->second.checkAndResetKnownSafety(
I, OtherState->first, checkIfRefCountInstIsMatched, RCIA, AA);
}
#ifndef NDEBUG
// Verify updateForDifferentLoopInst is conservative enough that the flow
// sensitive native of the loop summarized instructions does not matter.
if (verifyARCLoopSummary) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this verification inside the loop over SummarizedInterestingInsts? Shouldn't it be outside? Otherwise it's unnecessarily quadratic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I fixed it.

auto NewRefCountState = OtherState->second;
for (auto *I : State->getSummarizedInterestingInsts()) {
NewRefCountState.updateForDifferentLoopInst(I, AA);
}
assert(NewRefCountState.getLatticeState() ==
OtherState->second.getLatticeState());
}
#endif
}

return false;
Expand Down Expand Up @@ -420,6 +437,18 @@ bool ARCRegionState::processLoopTopDown(
OtherState->second.checkAndResetKnownSafety(
I, OtherState->first, checkIfRefCountInstIsMatched, RCIA, AA);
}
#ifndef NDEBUG
// Verify updateForDifferentLoopInst is conservative enough that the flow
// sensitive native of the loop summarized instructions does not matter.
if (verifyARCLoopSummary) {
auto NewRefCountState = OtherState->second;
for (auto *I : State->getSummarizedInterestingInsts()) {
NewRefCountState.updateForDifferentLoopInst(I, AA);
}
assert(NewRefCountState.getLatticeState() ==
OtherState->second.getLatticeState());
}
#endif
}

return false;
Expand Down
8 changes: 8 additions & 0 deletions lib/SILOptimizer/ARC/RefCountState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,17 @@ void BottomUpRefCountState::checkAndResetKnownSafety(
clearKnownSafe();
}

// This function is conservative enough that the flow sensitive nature of
// loop summarized instructions does not matter.
void BottomUpRefCountState::updateForDifferentLoopInst(SILInstruction *I,
AliasAnalysis *AA) {
// If we are not tracking anything, bail.
if (!isTrackingRefCount())
return;

if (valueCanBeGuaranteedUsedGivenLatticeState()) {
// Any instruction that may need guaranteed use or may decrement the
// refcount will turn off CodeMotionSafety
if (mayGuaranteedUseValue(I, getRCRoot(), AA) ||
mayDecrementRefCount(I, getRCRoot(), AA)) {
LLVM_DEBUG(llvm::dbgs() << " Found potential guaranteed use:\n "
Expand Down Expand Up @@ -917,12 +921,16 @@ void TopDownRefCountState::checkAndResetKnownSafety(
clearKnownSafe();
}

// This function is conservative enough that the flow sensitive nature of
// loop summarized instructions does not matter.
void TopDownRefCountState::updateForDifferentLoopInst(SILInstruction *I,
AliasAnalysis *AA) {
// If we are not tracking anything, bail.
if (!isTrackingRefCount())
return;

// Any instruction that may need guaranteed use or may decrement the
// refcount will turn off CodeMotionSafety
if (valueCanBeGuaranteedUsedGivenLatticeState()) {
if (mayGuaranteedUseValue(I, getRCRoot(), AA) ||
mayDecrementRefCount(I, getRCRoot(), AA)) {
Expand Down
10 changes: 10 additions & 0 deletions lib/SILOptimizer/ARC/RefCountState.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class BottomUpRefCountState : public RefCountState {
BottomUpRefCountState(BottomUpRefCountState &&) = default;
BottomUpRefCountState &operator=(BottomUpRefCountState &&) = default;

/// Getter for LatticeState
LatticeState getLatticeState() const {return LatState;}

/// Return true if the release can be moved to the retain.
bool isCodeMotionSafe() const {
return LatState != LatticeState::MightBeDecremented;
Expand Down Expand Up @@ -205,6 +208,8 @@ class BottomUpRefCountState : public RefCountState {
/// The main difference in between this routine and update for same loop inst
/// is that if we see any decrements on a value, we treat it as being
/// guaranteed used. We treat any uses as regular uses.
/// This function is conservative enough that the flow sensitive nature of
/// loop summarized instructions does not matter.
void updateForDifferentLoopInst(
SILInstruction *I,
AliasAnalysis *AA);
Expand Down Expand Up @@ -312,6 +317,9 @@ class TopDownRefCountState : public RefCountState {
TopDownRefCountState(TopDownRefCountState &&) = default;
TopDownRefCountState &operator=(TopDownRefCountState &&) = default;

/// Getter for LatticeState
LatticeState getLatticeState() const {return LatState;}

/// Return true if the retain can be moved to the release.
bool isCodeMotionSafe() const {
return LatState != LatticeState::MightBeUsed;
Expand Down Expand Up @@ -350,6 +358,8 @@ class TopDownRefCountState : public RefCountState {
/// The main difference in between this routine and update for same loop inst
/// is that if we see any decrements on a value, we treat it as being
/// guaranteed used. We treat any uses as regular uses.
/// This function is conservative enough that the flow sensitive nature of
/// loop summarized instructions does not matter.
void updateForDifferentLoopInst(
SILInstruction *I,
AliasAnalysis *AA);
Expand Down