@@ -31,7 +31,7 @@ STATISTIC(NumCapturesPropagated, "Number of constant captures propagated");
31
31
namespace {
32
32
// / Propagate constants through closure captures by specializing the partially
33
33
// / applied function.
34
- class CapturePropagation : public SILModuleTransform
34
+ class CapturePropagation : public SILFunctionTransform
35
35
{
36
36
public:
37
37
void run () override ;
@@ -237,7 +237,7 @@ SILFunction *CapturePropagation::specializeConstClosure(PartialApplyInst *PAI,
237
237
CanSILFunctionType NewFTy =
238
238
Lowering::adjustFunctionType (PAI->getType ().castTo <SILFunctionType>(),
239
239
SILFunctionType::Representation::Thin);
240
- SILFunction *NewF = getModule ()-> createFunction (
240
+ SILFunction *NewF = OrigF-> getModule (). createFunction (
241
241
SILLinkage::Shared, Name, NewFTy,
242
242
/* contextGenericParams*/ nullptr , OrigF->getLocation (), OrigF->isBare (),
243
243
OrigF->isTransparent (), Fragile, OrigF->isThunk (),
@@ -292,18 +292,17 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
292
292
if (PAI->hasSubstitutions ())
293
293
return false ;
294
294
295
- auto *FRI = dyn_cast<FunctionRefInst>( PAI->getCallee () );
296
- if (!FRI )
295
+ SILFunction *SubstF = PAI->getReferencedFunction ( );
296
+ if (!SubstF )
297
297
return false ;
298
298
299
- assert (!FRI-> getFunctionType ()->isPolymorphic () &&
299
+ assert (!SubstF-> getLoweredFunctionType ()->isPolymorphic () &&
300
300
" cannot specialize generic partial apply" );
301
301
302
302
for (auto Arg : PAI->getArguments ()) {
303
303
if (!isConstant (Arg))
304
304
return false ;
305
305
}
306
- SILFunction *SubstF = FRI->getReferencedFunction ();
307
306
if (SubstF->isExternalDeclaration () || !isProfitable (SubstF))
308
307
return false ;
309
308
@@ -312,34 +311,34 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
312
311
++NumCapturesPropagated;
313
312
SILFunction *NewF = specializeConstClosure (PAI, SubstF);
314
313
rewritePartialApply (PAI, NewF);
314
+
315
+ notifyPassManagerOfFunction (NewF);
315
316
return true ;
316
317
}
317
318
318
319
void CapturePropagation::run () {
319
320
DominanceAnalysis *DA = PM->getAnalysis <DominanceAnalysis>();
321
+ auto *F = getFunction ();
320
322
bool HasChanged = false ;
321
- for (auto &F : *getModule ()) {
322
323
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))
325
332
continue ;
326
333
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);
340
340
}
341
341
}
342
-
343
342
if (HasChanged) {
344
343
invalidateAnalysis (SILAnalysis::InvalidationKind::Everything);
345
344
}
0 commit comments