|
| 1 | +//===--- SILGenBackDeploy.cpp - SILGen for back deployment ----------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "SILGenFunction.h" |
| 14 | +#include "SILGenFunctionBuilder.h" |
| 15 | +#include "Scope.h" |
| 16 | +#include "swift/SIL/SILDeclRef.h" |
| 17 | + |
| 18 | +using namespace swift; |
| 19 | +using namespace Lowering; |
| 20 | + |
| 21 | +/// Emit the following branch SIL instruction: |
| 22 | +/// \verbatim |
| 23 | +/// if #available(OSVersion) { |
| 24 | +/// <availableBB> |
| 25 | +/// } else { |
| 26 | +/// <unavailableBB> |
| 27 | +/// } |
| 28 | +/// \endverbatim |
| 29 | +static void emitBackDeployIfAvailableCondition(SILGenFunction &SGF, |
| 30 | + AbstractFunctionDecl *AFD, |
| 31 | + SILLocation loc, |
| 32 | + SILBasicBlock *availableBB, |
| 33 | + SILBasicBlock *unavailableBB) { |
| 34 | + PlatformKind platform = targetPlatform(SGF.SGM.getASTContext().LangOpts); |
| 35 | + auto introduced = AFD->getIntroducedOSVersion(platform); |
| 36 | + VersionRange OSVersion = VersionRange::empty(); |
| 37 | + if (introduced.hasValue()) { |
| 38 | + OSVersion = VersionRange::allGTE(*introduced); |
| 39 | + } |
| 40 | + |
| 41 | + SILValue booleanTestValue; |
| 42 | + if (OSVersion.isEmpty() || OSVersion.isAll()) { |
| 43 | + // If there's no check for the current platform, this condition is |
| 44 | + // trivially true. |
| 45 | + SILType i1 = SILType::getBuiltinIntegerType(1, SGF.getASTContext()); |
| 46 | + booleanTestValue = SGF.B.createIntegerLiteral(loc, i1, 1); |
| 47 | + } else { |
| 48 | + booleanTestValue = SGF.emitOSVersionRangeCheck(loc, OSVersion); |
| 49 | + } |
| 50 | + |
| 51 | + SGF.B.createCondBranch(loc, booleanTestValue, availableBB, unavailableBB); |
| 52 | +} |
| 53 | + |
| 54 | +/// Emits a function or method application, forwarding parameters. |
| 55 | +SILValue emitBackDeployForwardApply(SILGenFunction &SGF, |
| 56 | + AbstractFunctionDecl *AFD, SILLocation loc, |
| 57 | + SILDeclRef function, |
| 58 | + SmallVector<SILValue, 8> ¶ms, |
| 59 | + SILBasicBlock *rethrowBB) { |
| 60 | + TypeExpansionContext TEC = SGF.getTypeExpansionContext(); |
| 61 | + auto fnType = SGF.SGM.Types.getConstantOverrideType(TEC, function); |
| 62 | + auto silFnType = |
| 63 | + SILType::getPrimitiveObjectType(fnType).castTo<SILFunctionType>(); |
| 64 | + SILFunctionConventions fnConv(silFnType, SGF.SGM.M); |
| 65 | + |
| 66 | + if (silFnType->hasErrorResult()) |
| 67 | + assert(rethrowBB && "must provide rethrow basic block"); |
| 68 | + |
| 69 | + bool isClassMethod = false; |
| 70 | + if (auto classDecl = dyn_cast<ClassDecl>(AFD->getDeclContext())) { |
| 71 | + if (!classDecl->isFinal() && !AFD->isFinal() && |
| 72 | + !AFD->hasForcedStaticDispatch()) |
| 73 | + isClassMethod = true; |
| 74 | + } |
| 75 | + |
| 76 | + SILBasicBlock *normalBB = SGF.createBasicBlock(); |
| 77 | + SILBasicBlock *errorBB = rethrowBB ? SGF.createBasicBlock() : nullptr; |
| 78 | + |
| 79 | + SILValue functionRef; |
| 80 | + if (isClassMethod) { |
| 81 | + auto selfValue = ManagedValue::forUnmanaged(SGF.F.getSelfArgument()); |
| 82 | + functionRef = |
| 83 | + SGF.emitClassMethodRef(loc, selfValue.getValue(), function, fnType); |
| 84 | + } else { |
| 85 | + functionRef = SGF.emitGlobalFunctionRef(loc, function); |
| 86 | + } |
| 87 | + auto subs = SGF.F.getForwardingSubstitutionMap(); |
| 88 | + |
| 89 | + if (errorBB) { |
| 90 | + SGF.B.createTryApply(loc, functionRef, subs, params, normalBB, errorBB); |
| 91 | + } else { |
| 92 | + auto result = SGF.B.createApply(loc, functionRef, subs, params); |
| 93 | + SGF.B.createBranch(loc, normalBB, {result}); |
| 94 | + } |
| 95 | + |
| 96 | + if (errorBB) { |
| 97 | + SGF.B.emitBlock(errorBB); |
| 98 | + SILValue error = errorBB->createPhiArgument(fnConv.getSILErrorType(TEC), |
| 99 | + OwnershipKind::Owned); |
| 100 | + SGF.B.createBranch(loc, rethrowBB, {error}); |
| 101 | + } |
| 102 | + |
| 103 | + SGF.B.emitBlock(normalBB); |
| 104 | + return normalBB->createPhiArgument(fnConv.getSILResultType(TEC), |
| 105 | + OwnershipKind::Owned); |
| 106 | +} |
| 107 | + |
| 108 | +void SILGenFunction::emitBackDeployedThunk(SILDeclRef thunk) { |
| 109 | + // Generate code equivalent to: |
| 110 | + // |
| 111 | + // func X_thunk(...) async throws -> ... { |
| 112 | + // if #available(...) { |
| 113 | + // return try await X(...) |
| 114 | + // } else { |
| 115 | + // return try await X_clientCopy(...) |
| 116 | + // } |
| 117 | + // } |
| 118 | + |
| 119 | + assert(thunk.isBackDeployed); |
| 120 | + |
| 121 | + // Get a reference to the original declaration. We'll call the original |
| 122 | + // declaration at runtime if it is available. |
| 123 | + SILDeclRef original = thunk.asBackDeployed(false); |
| 124 | + auto AFD = cast<AbstractFunctionDecl>(thunk.getDecl()); |
| 125 | + |
| 126 | + // Use the same generic environment as the original entry point. |
| 127 | + F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(original)); |
| 128 | + |
| 129 | + auto loc = thunk.getAsRegularLocation(); |
| 130 | + loc.markAutoGenerated(); |
| 131 | + Scope scope(Cleanups, CleanupLocation(loc)); |
| 132 | + |
| 133 | + auto methodTy = |
| 134 | + SGM.Types.getConstantOverrideType(getTypeExpansionContext(), thunk); |
| 135 | + auto derivativeFnSILTy = SILType::getPrimitiveObjectType(methodTy); |
| 136 | + auto silFnType = derivativeFnSILTy.castTo<SILFunctionType>(); |
| 137 | + SILFunctionConventions fnConv(silFnType, SGM.M); |
| 138 | + auto resultType = fnConv.getSILResultType(getTypeExpansionContext()); |
| 139 | + |
| 140 | + // Gather params for forwarding. |
| 141 | + SmallVector<SILValue, 8> paramsForForwarding; |
| 142 | + bindParametersForForwarding(AFD->getParameters(), paramsForForwarding); |
| 143 | + if (auto *selfDecl = AFD->getImplicitSelfDecl()) |
| 144 | + bindParameterForForwarding(selfDecl, paramsForForwarding); |
| 145 | + |
| 146 | + auto rethrowBB = |
| 147 | + silFnType->hasErrorResult() ? createBasicBlock("rethrowBB") : nullptr; |
| 148 | + auto returnBB = createBasicBlock("returnBB"); |
| 149 | + |
| 150 | + SILBasicBlock *availableBB = createBasicBlock("availableBB"); |
| 151 | + SILBasicBlock *unavailableBB = createBasicBlock("unavailableBB"); |
| 152 | + |
| 153 | + // if #available(...) { |
| 154 | + // <availableBB> |
| 155 | + // } else { |
| 156 | + // <unavailableBB> |
| 157 | + // } |
| 158 | + emitBackDeployIfAvailableCondition(*this, AFD, loc, availableBB, |
| 159 | + unavailableBB); |
| 160 | + |
| 161 | + // <availableBB>: |
| 162 | + // return (try)? (await)? (self.)?X(...) |
| 163 | + { |
| 164 | + B.emitBlock(availableBB); |
| 165 | + SILValue result = emitBackDeployForwardApply( |
| 166 | + *this, AFD, loc, original, paramsForForwarding, rethrowBB); |
| 167 | + B.createBranch(loc, returnBB, {result}); |
| 168 | + } |
| 169 | + |
| 170 | + // <unavailableBB>: |
| 171 | + // return (try)? (await)? (self.)?X_clientCopy(...) |
| 172 | + { |
| 173 | + B.emitBlock(unavailableBB); |
| 174 | + // FIXME(backDeploy): Emit a forward apply of client copy of function |
| 175 | + ManagedValue undef = emitUndef(resultType); |
| 176 | + B.createBranch(loc, returnBB, {undef}); |
| 177 | + } |
| 178 | + |
| 179 | + // Emit return logic. |
| 180 | + { |
| 181 | + B.emitBlock(returnBB); |
| 182 | + SILValue result = |
| 183 | + returnBB->createPhiArgument(resultType, OwnershipKind::Owned); |
| 184 | + |
| 185 | + B.createReturn(loc, result); |
| 186 | + } |
| 187 | + |
| 188 | + // Emit the rethrow logic. |
| 189 | + if (rethrowBB) { |
| 190 | + B.emitBlock(rethrowBB); |
| 191 | + SILValue error = rethrowBB->createPhiArgument( |
| 192 | + fnConv.getSILErrorType(getTypeExpansionContext()), |
| 193 | + OwnershipKind::Owned); |
| 194 | + |
| 195 | + Cleanups.emitCleanupsForReturn(CleanupLocation(loc), IsForUnwind); |
| 196 | + B.createThrow(loc, error); |
| 197 | + } |
| 198 | +} |
0 commit comments