Skip to content

Partial apply arguments pluszero #14547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/SILGen/SILGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ class SILGenBuilder : public SILBuilder {
SILType substFnTy, SubstitutionList subs,
ArrayRef<ManagedValue> args,
SILType closureTy);
ManagedValue createPartialApply(SILLocation loc, ManagedValue fn,
SILType substFnTy, SubstitutionList subs,
ArrayRef<ManagedValue> args,
SILType closureTy) {
return createPartialApply(loc, fn.getValue(), substFnTy, subs, args,
closureTy);
}

BuiltinInst *createBuiltin(SILLocation loc, Identifier name, SILType resultTy,
SubstitutionList subs, ArrayRef<SILValue> args);
Expand Down
49 changes: 25 additions & 24 deletions lib/SILGen/SILGenThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ SILValue SILGenFunction::emitDynamicMethodRef(SILLocation loc,
return B.createFunctionRef(loc, F);
}

static SILValue getNextUncurryLevelRef(SILGenFunction &SGF,
SILLocation loc,
SILDeclRef thunk,
SILValue selfArg,
SubstitutionList curriedSubs) {
static ManagedValue getNextUncurryLevelRef(SILGenFunction &SGF, SILLocation loc,
SILDeclRef thunk,
ManagedValue selfArg,
SubstitutionList curriedSubs) {
auto *vd = thunk.getDecl();

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

// If the function is natively foreign, reference its foreign entry point.
if (requiresForeignToNativeThunk(vd))
return SGF.emitGlobalFunctionRef(loc, next);
return ManagedValue::forUnmanaged(SGF.emitGlobalFunctionRef(loc, next));

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

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

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

auto methodTy = SGF.SGM.Types.getConstantOverrideType(next);
return SGF.emitClassMethodRef(loc, selfArg, next, methodTy);
SILValue result =
SGF.emitClassMethodRef(loc, selfArg.getValue(), next, methodTy);
return ManagedValue::forUnmanaged(result);
}

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

// Otherwise, emit a direct call.
return SGF.emitGlobalFunctionRef(loc, next);
return ManagedValue::forUnmanaged(SGF.emitGlobalFunctionRef(loc, next));
}

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

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

SILValue toFn = getNextUncurryLevelRef(*this, vd, thunk,
selfArg, subs);
ManagedValue toFn = getNextUncurryLevelRef(*this, vd, thunk, selfArg, subs);

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

auto calleeConvention = ParameterConvention::Direct_Guaranteed;
auto substTy = toFn.getType().substGenericArgs(SGM.M, subs);

// Partially apply the next uncurry level and return the result closure.
selfArg = selfArg.ensurePlusOne(*this, vd);
auto calleeConvention = ParameterConvention::Direct_Guaranteed;
auto closureTy = SILGenBuilder::getPartialApplyResultType(
toFn->getType(), /*appliedParams=*/1, SGM.M, subs, calleeConvention);
SILValue toClosure =
B.createPartialApply(vd, toFn, substTy, subs, {selfArg}, closureTy);
toFn.getType(), /*appliedParams=*/1, SGM.M, subs, calleeConvention);
ManagedValue toClosure =
B.createPartialApply(vd, toFn, substTy, subs, {selfArg}, closureTy);
if (resultTy != closureTy) {
CanSILFunctionType resultFnTy = resultTy.castTo<SILFunctionType>();
CanSILFunctionType closureFnTy = closureTy.castTo<SILFunctionType>();
if (resultFnTy->isABICompatibleWith(closureFnTy).isCompatible()) {
toClosure = B.createConvertFunction(vd, toClosure, resultTy);
} else {
toClosure =
emitCanonicalFunctionThunk(vd, ManagedValue::forUnmanaged(toClosure),
closureFnTy, resultFnTy)
.forward(*this);
emitCanonicalFunctionThunk(vd, toClosure, closureFnTy, resultFnTy);
}
}
B.createReturn(ImplicitReturnLocation::getImplicitReturnLoc(vd), toClosure);
Expand Down