Skip to content

Commit 38b4187

Browse files
authored
Merge pull request #35396 from meg-gupta/arraypropoptossapr
2 parents 31d43bf + f6f5826 commit 38b4187

File tree

5 files changed

+715
-21
lines changed

5 files changed

+715
-21
lines changed

lib/SIL/Utils/BasicBlockUtils.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
197197
if (!succBB->getNumArguments())
198198
return;
199199
args.push_back(newEdgeBB->createPhiArgument(
200-
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
200+
succBB->getArgument(0)->getType(),
201+
succBB->getArgument(0)->getOwnershipKind()));
201202
return;
202203
}
203204
case SILInstructionKind::CheckedCastAddrBranchInst: {
@@ -206,7 +207,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
206207
if (!succBB->getNumArguments())
207208
return;
208209
args.push_back(newEdgeBB->createPhiArgument(
209-
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
210+
succBB->getArgument(0)->getType(),
211+
succBB->getArgument(0)->getOwnershipKind()));
210212
return;
211213
}
212214
case SILInstructionKind::CheckedCastValueBranchInst: {
@@ -215,7 +217,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
215217
if (!succBB->getNumArguments())
216218
return;
217219
args.push_back(newEdgeBB->createPhiArgument(
218-
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
220+
succBB->getArgument(0)->getType(),
221+
succBB->getArgument(0)->getOwnershipKind()));
219222
return;
220223
}
221224

@@ -225,7 +228,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
225228
if (!succBB->getNumArguments())
226229
return;
227230
args.push_back(newEdgeBB->createPhiArgument(
228-
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
231+
succBB->getArgument(0)->getType(),
232+
succBB->getArgument(0)->getOwnershipKind()));
229233
return;
230234
}
231235

lib/SILOptimizer/LoopTransforms/ArrayPropertyOpt.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class ArrayPropertiesAnalysis {
102102
/// Check if it is profitable to specialize a loop when you see an apply
103103
/// instruction. We consider it is not profitable to specialize the loop when:
104104
/// 1. The callee is not found in the module, or cannot be determined
105-
/// 2. The number of instructions the analysis scans has exceeded the AnalysisThreshold
105+
/// 2. The number of instructions the analysis scans has exceeded the
106+
/// AnalysisThreshold
106107
uint32_t checkProfitabilityRecursively(SILFunction *Callee) {
107108
if (!Callee)
108109
return AnalysisThreshold;
@@ -118,16 +119,19 @@ class ArrayPropertiesAnalysis {
118119
for (auto &BB : *Callee) {
119120
for (auto &I : BB) {
120121
if (InstCount++ >= AnalysisThreshold) {
121-
LLVM_DEBUG(llvm::dbgs() << "ArrayPropertyOpt: Disabled Reason - Exceeded Analysis Threshold in "
122+
LLVM_DEBUG(llvm::dbgs() << "ArrayPropertyOpt: Disabled Reason - "
123+
"Exceeded Analysis Threshold in "
122124
<< BB.getParent()->getName() << "\n");
123125
InstCountCache[Callee] = AnalysisThreshold;
124126
return AnalysisThreshold;
125127
}
126128
if (auto Apply = FullApplySite::isa(&I)) {
127129
auto Callee = Apply.getReferencedFunctionOrNull();
128130
if (!Callee) {
129-
LLVM_DEBUG(llvm::dbgs() << "ArrayPropertyOpt: Disabled Reason - Found opaque code in "
130-
<< BB.getParent()->getName() << "\n");
131+
LLVM_DEBUG(
132+
llvm::dbgs()
133+
<< "ArrayPropertyOpt: Disabled Reason - Found opaque code in "
134+
<< BB.getParent()->getName() << "\n");
131135
LLVM_DEBUG(Apply.dump());
132136
LLVM_DEBUG(I.getOperand(0)->dump());
133137
}
@@ -182,9 +186,11 @@ class ArrayPropertiesAnalysis {
182186
if (!FoundHoistable)
183187
return false;
184188

185-
// If the LoopInstCount exceeds the threshold, we will disable the optimization on this loop
186-
// For loops of deeper nesting we increase the threshold by an additional 10%
187-
if (LoopInstCount > LoopInstCountThreshold * (1 + (Loop->getLoopDepth() - 1) / 10)) {
189+
// If the LoopInstCount exceeds the threshold, we will disable the
190+
// optimization on this loop For loops of deeper nesting we increase the
191+
// threshold by an additional 10%
192+
if (LoopInstCount >
193+
LoopInstCountThreshold * (1 + (Loop->getLoopDepth() - 1) / 10)) {
188194
LLVM_DEBUG(llvm::dbgs() << "Exceeded LoopInstCountThreshold\n");
189195
return false;
190196
}
@@ -205,8 +211,9 @@ class ArrayPropertiesAnalysis {
205211
}
206212
}
207213

208-
LLVM_DEBUG(llvm::dbgs() << "Profitable ArrayPropertyOpt in "
209-
<< Loop->getLoopPreheader()->getParent()->getName() << "\n");
214+
LLVM_DEBUG(llvm::dbgs()
215+
<< "Profitable ArrayPropertyOpt in "
216+
<< Loop->getLoopPreheader()->getParent()->getName() << "\n");
210217
LLVM_DEBUG(Loop->dump());
211218
return true;
212219
}
@@ -216,7 +223,7 @@ class ArrayPropertiesAnalysis {
216223
/// Strip the struct load and the address projection to the location
217224
/// holding the array struct.
218225
SILValue stripArrayStructLoad(SILValue V) {
219-
if (auto LI = dyn_cast<LoadInst>(V)) {
226+
if (auto LI = dyn_cast<LoadInst>(lookThroughCopyValueInsts(V))) {
220227
auto Val = LI->getOperand();
221228
// We could have two arrays in a surrounding container so we can only
222229
// strip off the 'array struct' project.
@@ -745,10 +752,6 @@ class SwiftArrayPropertyOptPass : public SILFunctionTransform {
745752
void run() override {
746753
auto *Fn = getFunction();
747754

748-
// FIXME: Add support for ownership.
749-
if (Fn->hasOwnership())
750-
return;
751-
752755
// Don't hoist array property calls at Osize.
753756
if (Fn->optimizeForSize())
754757
return;
@@ -782,7 +785,6 @@ class SwiftArrayPropertyOptPass : public SILFunctionTransform {
782785

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

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

798-
LLVM_DEBUG(getFunction()->viewCFG());
799-
800800
// We preserve the dominator tree. Let's invalidate everything
801801
// else.
802802
DA->lockInvalidation();

0 commit comments

Comments
 (0)