Skip to content

Commit 3465d23

Browse files
committed
[+0-normal-args] Change getNextUncurryLevelRef and emitCurryThunk to traffic in ManagedValues instead of SILValues.
This is a refactor that we want in general and allows me to use ensurePlusOne(...) to ensure we retain all values we put into the partial apply. rdar://34222540
1 parent 3a66381 commit 3465d23

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lib/SILGen/SILGenBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ class SILGenBuilder : public SILBuilder {
9292
SILType substFnTy, SubstitutionList subs,
9393
ArrayRef<ManagedValue> args,
9494
SILType closureTy);
95+
ManagedValue createPartialApply(SILLocation loc, ManagedValue fn,
96+
SILType substFnTy, SubstitutionList subs,
97+
ArrayRef<ManagedValue> args,
98+
SILType closureTy) {
99+
return createPartialApply(loc, fn.getValue(), substFnTy, subs, args,
100+
closureTy);
101+
}
95102

96103
BuiltinInst *createBuiltin(SILLocation loc, Identifier name, SILType resultTy,
97104
SubstitutionList subs, ArrayRef<SILValue> args);

lib/SILGen/SILGenThunk.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ SILValue SILGenFunction::emitDynamicMethodRef(SILLocation loc,
8282
return B.createFunctionRef(loc, F);
8383
}
8484

85-
static SILValue getNextUncurryLevelRef(SILGenFunction &SGF,
86-
SILLocation loc,
87-
SILDeclRef thunk,
88-
SILValue selfArg,
89-
SubstitutionList curriedSubs) {
85+
static ManagedValue getNextUncurryLevelRef(SILGenFunction &SGF, SILLocation loc,
86+
SILDeclRef thunk,
87+
ManagedValue selfArg,
88+
SubstitutionList curriedSubs) {
9089
auto *vd = thunk.getDecl();
9190

9291
// Reference the next uncurrying level of the function.
@@ -95,23 +94,26 @@ static SILValue getNextUncurryLevelRef(SILGenFunction &SGF,
9594

9695
// If the function is natively foreign, reference its foreign entry point.
9796
if (requiresForeignToNativeThunk(vd))
98-
return SGF.emitGlobalFunctionRef(loc, next);
97+
return ManagedValue::forUnmanaged(SGF.emitGlobalFunctionRef(loc, next));
9998

10099
// If the thunk is a curry thunk for a direct method reference, we are
101100
// doing a direct dispatch (eg, a fragile 'super.foo()' call).
102101
if (thunk.isDirectReference)
103-
return SGF.emitGlobalFunctionRef(loc, next);
102+
return ManagedValue::forUnmanaged(SGF.emitGlobalFunctionRef(loc, next));
104103

105104
auto constantInfo = SGF.SGM.Types.getConstantInfo(next);
106105

107106
if (auto *func = dyn_cast<AbstractFunctionDecl>(vd)) {
108107
if (getMethodDispatch(func) == MethodDispatch::Class) {
109108
// Use the dynamic thunk if dynamic.
110109
if (vd->isDynamic())
111-
return SGF.emitDynamicMethodRef(loc, next, constantInfo.SILFnType);
110+
return ManagedValue::forUnmanaged(
111+
SGF.emitDynamicMethodRef(loc, next, constantInfo.SILFnType));
112112

113113
auto methodTy = SGF.SGM.Types.getConstantOverrideType(next);
114-
return SGF.emitClassMethodRef(loc, selfArg, next, methodTy);
114+
SILValue result =
115+
SGF.emitClassMethodRef(loc, selfArg.getValue(), next, methodTy);
116+
return ManagedValue::forUnmanaged(result);
115117
}
116118

117119
// If the fully-uncurried reference is to a generic method, look up the
@@ -125,13 +127,14 @@ static SILValue getNextUncurryLevelRef(SILGenFunction &SGF,
125127
auto origSelfType = protocol->getSelfInterfaceType()->getCanonicalType();
126128
auto substSelfType = origSelfType.subst(subMap)->getCanonicalType();
127129
auto conformance = subMap.lookupConformance(origSelfType, protocol);
128-
return SGF.B.createWitnessMethod(loc, substSelfType, *conformance, next,
129-
constantInfo.getSILType());
130+
auto result = SGF.B.createWitnessMethod(loc, substSelfType, *conformance,
131+
next, constantInfo.getSILType());
132+
return ManagedValue::forUnmanaged(result);
130133
}
131134
}
132135

133136
// Otherwise, emit a direct call.
134-
return SGF.emitGlobalFunctionRef(loc, next);
137+
return ManagedValue::forUnmanaged(SGF.emitGlobalFunctionRef(loc, next));
135138
}
136139

137140
void SILGenFunction::emitCurryThunk(SILDeclRef thunk) {
@@ -148,13 +151,13 @@ void SILGenFunction::emitCurryThunk(SILDeclRef thunk) {
148151
auto selfTy = vd->getInterfaceType()->castTo<AnyFunctionType>()
149152
->getInput();
150153
selfTy = vd->getInnermostDeclContext()->mapTypeIntoContext(selfTy);
151-
auto selfArg = F.begin()->createFunctionArgument(getLoweredType(selfTy));
154+
ManagedValue selfArg =
155+
B.createFunctionArgument(getLoweredType(selfTy), nullptr);
152156

153157
// Forward substitutions.
154158
auto subs = F.getForwardingSubstitutions();
155159

156-
SILValue toFn = getNextUncurryLevelRef(*this, vd, thunk,
157-
selfArg, subs);
160+
ManagedValue toFn = getNextUncurryLevelRef(*this, vd, thunk, selfArg, subs);
158161

159162
// FIXME: Using the type from the ConstantInfo instead of looking at
160163
// getConstantOverrideInfo() for methods looks suspect in the presence
@@ -163,25 +166,23 @@ void SILGenFunction::emitCurryThunk(SILDeclRef thunk) {
163166
SGM.Types.getConstantInfo(thunk).SILFnType, SGM.M);
164167
SILType resultTy = fromConv.getSingleSILResultType();
165168
resultTy = F.mapTypeIntoContext(resultTy);
166-
auto substTy = toFn->getType().substGenericArgs(SGM.M, subs);
169+
auto substTy = toFn.getType().substGenericArgs(SGM.M, subs);
167170

168171
auto calleeConvention = ParameterConvention::Direct_Guaranteed;
169172

170173
// Partially apply the next uncurry level and return the result closure.
171174
auto closureTy = SILGenBuilder::getPartialApplyResultType(
172-
toFn->getType(), /*appliedParams=*/1, SGM.M, subs, calleeConvention);
173-
SILValue toClosure =
174-
B.createPartialApply(vd, toFn, substTy, subs, {selfArg}, closureTy);
175+
toFn.getType(), /*appliedParams=*/1, SGM.M, subs, calleeConvention);
176+
ManagedValue toClosure =
177+
B.createPartialApply(vd, toFn, substTy, subs, {selfArg}, closureTy);
175178
if (resultTy != closureTy) {
176179
CanSILFunctionType resultFnTy = resultTy.castTo<SILFunctionType>();
177180
CanSILFunctionType closureFnTy = closureTy.castTo<SILFunctionType>();
178181
if (resultFnTy->isABICompatibleWith(closureFnTy).isCompatible()) {
179182
toClosure = B.createConvertFunction(vd, toClosure, resultTy);
180183
} else {
181184
toClosure =
182-
emitCanonicalFunctionThunk(vd, ManagedValue::forUnmanaged(toClosure),
183-
closureFnTy, resultFnTy)
184-
.forward(*this);
185+
emitCanonicalFunctionThunk(vd, toClosure, closureFnTy, resultFnTy);
185186
}
186187
}
187188
B.createReturn(ImplicitReturnLocation::getImplicitReturnLoc(vd), toClosure);

0 commit comments

Comments
 (0)