Skip to content

Commit c550396

Browse files
committed
Make CapturePropagation a function pass
1 parent 31bbb2c commit c550396

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ STATISTIC(NumCapturesPropagated, "Number of constant captures propagated");
3131
namespace {
3232
/// Propagate constants through closure captures by specializing the partially
3333
/// applied function.
34-
class CapturePropagation : public SILModuleTransform
34+
class CapturePropagation : public SILFunctionTransform
3535
{
3636
public:
3737
void run() override;
@@ -237,7 +237,7 @@ SILFunction *CapturePropagation::specializeConstClosure(PartialApplyInst *PAI,
237237
CanSILFunctionType NewFTy =
238238
Lowering::adjustFunctionType(PAI->getType().castTo<SILFunctionType>(),
239239
SILFunctionType::Representation::Thin);
240-
SILFunction *NewF = getModule()->createFunction(
240+
SILFunction *NewF = OrigF->getModule().createFunction(
241241
SILLinkage::Shared, Name, NewFTy,
242242
/*contextGenericParams*/ nullptr, OrigF->getLocation(), OrigF->isBare(),
243243
OrigF->isTransparent(), Fragile, OrigF->isThunk(),
@@ -292,18 +292,17 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
292292
if (PAI->hasSubstitutions())
293293
return false;
294294

295-
auto *FRI = dyn_cast<FunctionRefInst>(PAI->getCallee());
296-
if (!FRI)
295+
SILFunction *SubstF = PAI->getReferencedFunction();
296+
if (!SubstF)
297297
return false;
298298

299-
assert(!FRI->getFunctionType()->isPolymorphic() &&
299+
assert(!SubstF->getLoweredFunctionType()->isPolymorphic() &&
300300
"cannot specialize generic partial apply");
301301

302302
for (auto Arg : PAI->getArguments()) {
303303
if (!isConstant(Arg))
304304
return false;
305305
}
306-
SILFunction *SubstF = FRI->getReferencedFunction();
307306
if (SubstF->isExternalDeclaration() || !isProfitable(SubstF))
308307
return false;
309308

@@ -312,34 +311,34 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
312311
++NumCapturesPropagated;
313312
SILFunction *NewF = specializeConstClosure(PAI, SubstF);
314313
rewritePartialApply(PAI, NewF);
314+
315+
notifyPassManagerOfFunction(NewF);
315316
return true;
316317
}
317318

318319
void CapturePropagation::run() {
319320
DominanceAnalysis *DA = PM->getAnalysis<DominanceAnalysis>();
321+
auto *F = getFunction();
320322
bool HasChanged = false;
321-
for (auto &F : *getModule()) {
322323

323-
// Don't optimize functions that are marked with the opt.never attribute.
324-
if (!F.shouldOptimize())
324+
// Don't optimize functions that are marked with the opt.never attribute.
325+
if (!F->shouldOptimize())
326+
return;
327+
328+
// Cache cold blocks per function.
329+
ColdBlockInfo ColdBlocks(DA);
330+
for (auto &BB : *F) {
331+
if (ColdBlocks.isCold(&BB))
325332
continue;
326333

327-
// Cache cold blocks per function.
328-
ColdBlockInfo ColdBlocks(DA);
329-
for (auto &BB : F) {
330-
if (ColdBlocks.isCold(&BB))
331-
continue;
332-
333-
auto I = BB.begin();
334-
while (I != BB.end()) {
335-
SILInstruction *Inst = &*I;
336-
++I;
337-
if (PartialApplyInst *PAI = dyn_cast<PartialApplyInst>(Inst))
338-
HasChanged |= optimizePartialApply(PAI);
339-
}
334+
auto I = BB.begin();
335+
while (I != BB.end()) {
336+
SILInstruction *Inst = &*I;
337+
++I;
338+
if (PartialApplyInst *PAI = dyn_cast<PartialApplyInst>(Inst))
339+
HasChanged |= optimizePartialApply(PAI);
340340
}
341341
}
342-
343342
if (HasChanged) {
344343
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
345344
}

0 commit comments

Comments
 (0)