Skip to content

Enable ArrayPropertyOpt on OSSA #35396

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 2 commits into from
Jan 21, 2021
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
12 changes: 8 additions & 4 deletions lib/SIL/Utils/BasicBlockUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
if (!succBB->getNumArguments())
return;
args.push_back(newEdgeBB->createPhiArgument(
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
succBB->getArgument(0)->getType(),
succBB->getArgument(0)->getOwnershipKind()));
return;
}
case SILInstructionKind::CheckedCastAddrBranchInst: {
Expand All @@ -206,7 +207,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
if (!succBB->getNumArguments())
return;
args.push_back(newEdgeBB->createPhiArgument(
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
succBB->getArgument(0)->getType(),
succBB->getArgument(0)->getOwnershipKind()));
return;
}
case SILInstructionKind::CheckedCastValueBranchInst: {
Expand All @@ -215,7 +217,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
if (!succBB->getNumArguments())
return;
args.push_back(newEdgeBB->createPhiArgument(
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
succBB->getArgument(0)->getType(),
succBB->getArgument(0)->getOwnershipKind()));
return;
}

Expand All @@ -225,7 +228,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
if (!succBB->getNumArguments())
return;
args.push_back(newEdgeBB->createPhiArgument(
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
succBB->getArgument(0)->getType(),
succBB->getArgument(0)->getOwnershipKind()));
return;
}

Expand Down
34 changes: 17 additions & 17 deletions lib/SILOptimizer/LoopTransforms/ArrayPropertyOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class ArrayPropertiesAnalysis {
/// Check if it is profitable to specialize a loop when you see an apply
/// instruction. We consider it is not profitable to specialize the loop when:
/// 1. The callee is not found in the module, or cannot be determined
/// 2. The number of instructions the analysis scans has exceeded the AnalysisThreshold
/// 2. The number of instructions the analysis scans has exceeded the
/// AnalysisThreshold
uint32_t checkProfitabilityRecursively(SILFunction *Callee) {
if (!Callee)
return AnalysisThreshold;
Expand All @@ -118,16 +119,19 @@ class ArrayPropertiesAnalysis {
for (auto &BB : *Callee) {
for (auto &I : BB) {
if (InstCount++ >= AnalysisThreshold) {
LLVM_DEBUG(llvm::dbgs() << "ArrayPropertyOpt: Disabled Reason - Exceeded Analysis Threshold in "
LLVM_DEBUG(llvm::dbgs() << "ArrayPropertyOpt: Disabled Reason - "
"Exceeded Analysis Threshold in "
<< BB.getParent()->getName() << "\n");
InstCountCache[Callee] = AnalysisThreshold;
return AnalysisThreshold;
}
if (auto Apply = FullApplySite::isa(&I)) {
auto Callee = Apply.getReferencedFunctionOrNull();
if (!Callee) {
LLVM_DEBUG(llvm::dbgs() << "ArrayPropertyOpt: Disabled Reason - Found opaque code in "
<< BB.getParent()->getName() << "\n");
LLVM_DEBUG(
llvm::dbgs()
<< "ArrayPropertyOpt: Disabled Reason - Found opaque code in "
<< BB.getParent()->getName() << "\n");
LLVM_DEBUG(Apply.dump());
LLVM_DEBUG(I.getOperand(0)->dump());
}
Expand Down Expand Up @@ -182,9 +186,11 @@ class ArrayPropertiesAnalysis {
if (!FoundHoistable)
return false;

// If the LoopInstCount exceeds the threshold, we will disable the optimization on this loop
// For loops of deeper nesting we increase the threshold by an additional 10%
if (LoopInstCount > LoopInstCountThreshold * (1 + (Loop->getLoopDepth() - 1) / 10)) {
// If the LoopInstCount exceeds the threshold, we will disable the
// optimization on this loop For loops of deeper nesting we increase the
// threshold by an additional 10%
if (LoopInstCount >
LoopInstCountThreshold * (1 + (Loop->getLoopDepth() - 1) / 10)) {
LLVM_DEBUG(llvm::dbgs() << "Exceeded LoopInstCountThreshold\n");
return false;
}
Expand All @@ -205,8 +211,9 @@ class ArrayPropertiesAnalysis {
}
}

LLVM_DEBUG(llvm::dbgs() << "Profitable ArrayPropertyOpt in "
<< Loop->getLoopPreheader()->getParent()->getName() << "\n");
LLVM_DEBUG(llvm::dbgs()
<< "Profitable ArrayPropertyOpt in "
<< Loop->getLoopPreheader()->getParent()->getName() << "\n");
LLVM_DEBUG(Loop->dump());
return true;
}
Expand All @@ -216,7 +223,7 @@ class ArrayPropertiesAnalysis {
/// Strip the struct load and the address projection to the location
/// holding the array struct.
SILValue stripArrayStructLoad(SILValue V) {
if (auto LI = dyn_cast<LoadInst>(V)) {
if (auto LI = dyn_cast<LoadInst>(lookThroughCopyValueInsts(V))) {
auto Val = LI->getOperand();
// We could have two arrays in a surrounding container so we can only
// strip off the 'array struct' project.
Expand Down Expand Up @@ -745,10 +752,6 @@ class SwiftArrayPropertyOptPass : public SILFunctionTransform {
void run() override {
auto *Fn = getFunction();

// FIXME: Add support for ownership.
if (Fn->hasOwnership())
return;

// Don't hoist array property calls at Osize.
if (Fn->optimizeForSize())
return;
Expand Down Expand Up @@ -782,7 +785,6 @@ class SwiftArrayPropertyOptPass : public SILFunctionTransform {

// Specialize the identified loop nest based on the 'array.props' calls.
if (HasChanged) {
LLVM_DEBUG(getFunction()->viewCFG());
DominanceInfo *DT = DA->get(getFunction());

// Process specialized loop-nests in loop-tree post-order (bottom-up).
Expand All @@ -795,8 +797,6 @@ class SwiftArrayPropertyOptPass : public SILFunctionTransform {
// Verify that no illegal critical edges were created.
getFunction()->verifyCriticalEdges();

LLVM_DEBUG(getFunction()->viewCFG());

// We preserve the dominator tree. Let's invalidate everything
// else.
DA->lockInvalidation();
Expand Down
Loading