@@ -74,9 +74,22 @@ static bool foldInverseReabstractionThunks(PartialApplyInst *PAI,
74
74
SILInstruction *SILCombiner::visitPartialApplyInst (PartialApplyInst *PAI) {
75
75
// partial_apply without any substitutions or arguments is just a
76
76
// thin_to_thick_function.
77
- if (!PAI->hasSubstitutions () && (PAI->getNumArguments () == 0 ))
78
- return Builder.createThinToThickFunction (PAI->getLoc (), PAI->getCallee (),
79
- PAI->getType ());
77
+ if (!PAI->hasSubstitutions () && (PAI->getNumArguments () == 0 )) {
78
+ if (!PAI->isOnStack ())
79
+ return Builder.createThinToThickFunction (PAI->getLoc (), PAI->getCallee (),
80
+ PAI->getType ());
81
+
82
+ // Remove dealloc_stack of partial_apply [stack].
83
+ for (auto *Use : PAI->getUses ())
84
+ if (auto *dealloc = dyn_cast<DeallocStackInst>(Use->getUser ()))
85
+ eraseInstFromFunction (*dealloc);
86
+ auto *thinToThick = Builder.createThinToThickFunction (
87
+ PAI->getLoc (), PAI->getCallee (), PAI->getType ());
88
+ replaceInstUsesWith (*PAI, thinToThick);
89
+ eraseInstFromFunction (*PAI);
90
+ return nullptr ;
91
+ }
92
+
80
93
81
94
// partial_apply %reabstraction_thunk_typeAtoB(
82
95
// partial_apply %reabstraction_thunk_typeBtoA %closure_typeB))
@@ -138,6 +151,12 @@ class PartialApplyCombiner {
138
151
139
152
// / Returns true on success.
140
153
bool PartialApplyCombiner::allocateTemporaries () {
154
+ // A partial_apply [stack]'s argument are not owned by the partial_apply and
155
+ // therefore their lifetime must outlive any uses.
156
+ if (PAI->isOnStack ()) {
157
+ return true ;
158
+ }
159
+
141
160
// Copy the original arguments of the partial_apply into newly created
142
161
// temporaries and use these temporaries instead of the original arguments
143
162
// afterwards.
@@ -397,6 +416,15 @@ SILInstruction *PartialApplyCombiner::combine() {
397
416
Uses.append (CFI->getUses ().begin (), CFI->getUses ().end ());
398
417
continue ;
399
418
}
419
+ // Look through mark_dependence users of partial_apply [stack].
420
+ if (auto *MD = dyn_cast<MarkDependenceInst>(User)) {
421
+ if (MD->getValue () == Use->get () &&
422
+ MD->getValue ()->getType ().is <SILFunctionType>() &&
423
+ MD->getValue ()->getType ().castTo <SILFunctionType>()->isNoEscape ()) {
424
+ Uses.append (MD->getUses ().begin (), MD->getUses ().end ());
425
+ }
426
+ continue ;
427
+ }
400
428
// If this use of a partial_apply is not
401
429
// an apply which uses it as a callee, bail.
402
430
auto AI = FullApplySite::isa (User);
0 commit comments