Skip to content

Commit b8efde9

Browse files
committed
[SILGen] Fix ownership handling for createConvertEscapeToNoEscape().
Introduce SILGenBuilder::createConvertEscapeToNoEscape() to correctly handle ownership, and switch all relevant callers to it.
1 parent f24c2eb commit b8efde9

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -929,23 +929,19 @@ SILGenFunction::emitBlockToFunc(SILLocation loc,
929929

930930
// Create it in the current function.
931931
auto thunkValue = B.createFunctionRef(loc, thunk);
932-
SingleValueInstruction *thunkedFn = B.createPartialApply(
932+
ManagedValue thunkedFn = B.createPartialApply(
933933
loc, thunkValue, SILType::getPrimitiveObjectType(substFnTy), subs,
934-
block.forward(*this),
934+
block,
935935
SILType::getPrimitiveObjectType(loweredFuncTyWithoutNoEscape));
936936

937937
if (!loweredFuncTy->isNoEscape()) {
938-
return emitManagedRValueWithCleanup(thunkedFn);
938+
return thunkedFn;
939939
}
940940

941941
// Handle the escaping to noescape conversion.
942942
assert(loweredFuncTy->isNoEscape());
943-
944-
auto &funcTL = getTypeLowering(loweredFuncTy);
945-
SingleValueInstruction *noEscapeThunkFn =
946-
B.createConvertEscapeToNoEscape(loc, thunkedFn, funcTL.getLoweredType());
947-
enterPostponedCleanup(thunkedFn);
948-
return emitManagedRValueWithCleanup(noEscapeThunkFn);
943+
return B.createConvertEscapeToNoEscape(loc, thunkedFn,
944+
SILType::getPrimitiveObjectType(loweredFuncTy));
949945
}
950946

951947
static ManagedValue emitCBridgedToNativeValue(SILGenFunction &SGF,

lib/SILGen/SILGenBuilder.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,28 @@ ManagedValue SILGenBuilder::createConvertFunction(SILLocation loc,
207207
return cloner.clone(result);
208208
}
209209

210+
ManagedValue SILGenBuilder::createConvertEscapeToNoEscape(SILLocation loc,
211+
ManagedValue fn,
212+
SILType resultTy) {
213+
auto fnType = fn.getType().castTo<SILFunctionType>();
214+
auto resultFnType = resultTy.castTo<SILFunctionType>();
215+
216+
// Escaping to noescape conversion.
217+
assert(resultFnType->getRepresentation() ==
218+
SILFunctionTypeRepresentation::Thick &&
219+
fnType->getRepresentation() ==
220+
SILFunctionTypeRepresentation::Thick &&
221+
!fnType->isNoEscape() && resultFnType->isNoEscape() &&
222+
"Expect a escaping to noescape conversion");
223+
224+
bool fnHasCleanup = fn.hasCleanup();
225+
SILValue fnValue = fn.forward(getSILGenFunction());
226+
SILValue result = createConvertEscapeToNoEscape(loc, fnValue, resultTy);
227+
if (fnHasCleanup)
228+
getSILGenFunction().enterPostponedCleanup(fnValue);
229+
return ManagedValue::forTrivialObjectRValue(result);
230+
}
231+
210232
ManagedValue SILGenBuilder::createInitExistentialValue(
211233
SILLocation loc, SILType existentialType, CanType formalConcreteType,
212234
ManagedValue concrete, ArrayRef<ProtocolConformanceRef> conformances) {

lib/SILGen/SILGenBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ class SILGenBuilder : public SILBuilder {
360360
ManagedValue createConvertFunction(SILLocation loc, ManagedValue fn,
361361
SILType resultTy);
362362

363+
using SILBuilder::createConvertEscapeToNoEscape;
364+
ManagedValue createConvertEscapeToNoEscape(SILLocation loc, ManagedValue fn,
365+
SILType resultTy);
366+
363367
using SILBuilder::createStore;
364368
/// Forward \p value into \p address.
365369
///

lib/SILGen/SILGenPoly.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,22 +2921,20 @@ static ManagedValue createThunk(SILGenFunction &SGF,
29212921

29222922
// Create it in our current function.
29232923
auto thunkValue = SGF.B.createFunctionRef(loc, thunk);
2924-
SingleValueInstruction *thunkedFn =
2924+
ManagedValue thunkedFn =
29252925
SGF.B.createPartialApply(loc, thunkValue,
29262926
SILType::getPrimitiveObjectType(substFnType),
2927-
subs, fn.ensurePlusOne(SGF, loc).forward(SGF),
2927+
subs, fn.ensurePlusOne(SGF, loc),
29282928
SILType::getPrimitiveObjectType(toType));
29292929

29302930
if (!expectedType->isNoEscape()) {
2931-
return SGF.emitManagedRValueWithCleanup(thunkedFn, expectedTL);
2931+
return thunkedFn;
29322932
}
29332933

29342934
// Handle the escaping to noescape conversion.
29352935
assert(expectedType->isNoEscape());
2936-
SingleValueInstruction *noEscapeThunkFn = SGF.B.createConvertEscapeToNoEscape(
2937-
loc, thunkedFn, expectedTL.getLoweredType());
2938-
SGF.enterPostponedCleanup(thunkedFn);
2939-
return SGF.emitManagedRValueWithCleanup(noEscapeThunkFn);
2936+
return SGF.B.createConvertEscapeToNoEscape(loc, thunkedFn,
2937+
SILType::getPrimitiveObjectType(expectedType));
29402938
}
29412939

29422940
static CanSILFunctionType buildWithoutActuallyEscapingThunkType(
@@ -3101,18 +3099,8 @@ ManagedValue Transform::transformFunction(ManagedValue fn,
31013099
SGF.B.createThinToThickFunction(Loc, fn.forward(SGF), resTy));
31023100
} else if (newFnType != expectedFnType) {
31033101
// Escaping to noescape conversion.
3104-
assert(expectedFnType->getRepresentation() ==
3105-
SILFunctionTypeRepresentation::Thick &&
3106-
fnType->getRepresentation() ==
3107-
SILFunctionTypeRepresentation::Thick &&
3108-
!fnType->isNoEscape() && expectedFnType->isNoEscape() &&
3109-
"Expect a escaping to noescape conversion");
31103102
SILType resTy = SILType::getPrimitiveObjectType(expectedFnType);
3111-
auto fnValue = fn.forward(SGF);
3112-
SGF.enterPostponedCleanup(fnValue);
3113-
SingleValueInstruction *noEscapeThunkFn =
3114-
SGF.B.createConvertEscapeToNoEscape(Loc, fnValue, resTy);
3115-
fn = SGF.emitManagedRValueWithCleanup(noEscapeThunkFn);
3103+
fn = SGF.B.createConvertEscapeToNoEscape(Loc, fn, resTy);
31163104
}
31173105

31183106
return fn;

0 commit comments

Comments
 (0)