-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSILGenBackDeploy.cpp
225 lines (192 loc) · 8.25 KB
/
SILGenBackDeploy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//===--- SILGenBackDeploy.cpp - SILGen for back deployment ----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "SILGenFunction.h"
#include "SILGenFunctionBuilder.h"
#include "Scope.h"
#include "swift/SIL/SILDeclRef.h"
using namespace swift;
using namespace Lowering;
/// Given a value, extracts all elements to `result` from this value if it's a
/// tuple. Otherwise, add this value directly to `result`.
static void extractAllElements(SILValue val, SILLocation loc,
SILBuilder &builder,
SmallVectorImpl<SILValue> &result) {
auto &fn = builder.getFunction();
auto tupleType = val->getType().getAs<TupleType>();
if (!tupleType) {
result.push_back(val);
return;
}
if (!fn.hasOwnership()) {
for (auto i : range(tupleType->getNumElements()))
result.push_back(builder.createTupleExtract(loc, val, i));
return;
}
if (tupleType->getNumElements() == 0)
return;
builder.emitDestructureValueOperation(loc, val, result);
}
/// Emit the following branch SIL instruction:
/// \verbatim
/// if #available(OSVersion) {
/// <availableBB>
/// } else {
/// <unavailableBB>
/// }
/// \endverbatim
static void emitBackDeployIfAvailableCondition(SILGenFunction &SGF,
AbstractFunctionDecl *AFD,
SILLocation loc,
SILBasicBlock *availableBB,
SILBasicBlock *unavailableBB) {
PlatformKind platform = targetPlatform(SGF.SGM.getASTContext().LangOpts);
auto version = AFD->getBackDeployBeforeOSVersion(platform);
VersionRange OSVersion = VersionRange::empty();
if (version.hasValue()) {
OSVersion = VersionRange::allGTE(*version);
}
SILValue booleanTestValue;
if (OSVersion.isEmpty() || OSVersion.isAll()) {
// If there's no check for the current platform, this condition is
// trivially true.
SILType i1 = SILType::getBuiltinIntegerType(1, SGF.getASTContext());
booleanTestValue = SGF.B.createIntegerLiteral(loc, i1, 1);
} else {
booleanTestValue = SGF.emitOSVersionRangeCheck(loc, OSVersion);
}
SGF.B.createCondBranch(loc, booleanTestValue, availableBB, unavailableBB);
}
/// Emits a function or method application, forwarding parameters.
static void emitBackDeployForwardApplyAndReturnOrThrow(
SILGenFunction &SGF, AbstractFunctionDecl *AFD, SILLocation loc,
SILDeclRef function, SmallVector<SILValue, 8> ¶ms) {
// Only statically dispatched class methods are supported.
if (auto classDecl = dyn_cast<ClassDecl>(AFD->getDeclContext())) {
assert(classDecl->isFinal() || AFD->isFinal() ||
AFD->hasForcedStaticDispatch());
}
TypeExpansionContext TEC = SGF.getTypeExpansionContext();
auto fnType = SGF.SGM.Types.getConstantOverrideType(TEC, function);
auto silFnType =
SILType::getPrimitiveObjectType(fnType).castTo<SILFunctionType>();
SILFunctionConventions fnConv(silFnType, SGF.SGM.M);
SILValue functionRef = SGF.emitGlobalFunctionRef(loc, function);
auto subs = SGF.F.getForwardingSubstitutionMap();
SmallVector<SILValue, 4> directResults;
// If the function is a coroutine, we need to use 'begin_apply'.
if (silFnType->isCoroutine()) {
assert(!silFnType->hasErrorResult() && "throwing coroutine?");
// Apply the coroutine, yield the result, and finally branch to either the
// terminal return or unwind basic block via intermediate basic blocks. The
// intermediates are needed to avoid forming critical edges.
SILBasicBlock *resumeBB = SGF.createBasicBlock();
SILBasicBlock *unwindBB = SGF.createBasicBlock();
auto *apply = SGF.B.createBeginApply(loc, functionRef, subs, params);
SmallVector<SILValue, 4> rawResults;
for (auto result : apply->getAllResults())
rawResults.push_back(result);
auto token = rawResults.pop_back_val();
SGF.B.createEndApply(loc, token);
SGF.B.createYield(loc, rawResults, resumeBB, unwindBB);
// Emit resume block.
SGF.B.emitBlock(resumeBB);
SGF.B.createBranch(loc, SGF.ReturnDest.getBlock());
// Emit unwind block.
SGF.B.emitBlock(unwindBB);
SGF.B.createBranch(loc, SGF.CoroutineUnwindDest.getBlock());
return;
}
// Use try_apply for functions that throw.
if (silFnType->hasErrorResult()) {
// Apply the throwing function and forward the results and the error to the
// return/throw blocks via intermediate basic blocks. The intermediates
// are needed to avoid forming critical edges.
SILBasicBlock *normalBB = SGF.createBasicBlock();
SILBasicBlock *errorBB = SGF.createBasicBlock();
SGF.B.createTryApply(loc, functionRef, subs, params, normalBB, errorBB);
// Emit error block.
SGF.B.emitBlock(errorBB);
SILValue error = errorBB->createPhiArgument(fnConv.getSILErrorType(TEC),
OwnershipKind::Owned);
SGF.B.createBranch(loc, SGF.ThrowDest.getBlock(), {error});
// Emit normal block.
SGF.B.emitBlock(normalBB);
SILValue result = normalBB->createPhiArgument(fnConv.getSILResultType(TEC),
OwnershipKind::Owned);
SmallVector<SILValue, 4> directResults;
extractAllElements(result, loc, SGF.B, directResults);
SGF.B.createBranch(loc, SGF.ReturnDest.getBlock(), directResults);
return;
}
// The original function is neither throwing nor a couroutine. Apply it and
// forward its results straight to the return block.
auto *apply = SGF.B.createApply(loc, functionRef, subs, params);
extractAllElements(apply, loc, SGF.B, directResults);
SGF.B.createBranch(loc, SGF.ReturnDest.getBlock(), directResults);
}
void SILGenFunction::emitBackDeploymentThunk(SILDeclRef thunk) {
// Generate code equivalent to:
//
// func X_thunk(...) async throws -> ... {
// if #available(...) {
// return try await X(...)
// } else {
// return try await X_fallback(...)
// }
// }
assert(thunk.isBackDeploymentThunk());
auto loc = thunk.getAsRegularLocation();
loc.markAutoGenerated();
Scope scope(Cleanups, CleanupLocation(loc));
auto FD = cast<FuncDecl>(thunk.getDecl());
F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(thunk));
emitBasicProlog(FD->getParameters(), FD->getImplicitSelfDecl(),
FD->getResultInterfaceType(), FD, FD->hasThrows(),
FD->getThrowsLoc());
prepareEpilog(FD->getResultInterfaceType(), FD->hasThrows(),
CleanupLocation(FD));
// Gather the entry block's arguments up so that we can forward them.
SmallVector<SILValue, 8> paramsForForwarding;
SILBasicBlock *entryBlock = getFunction().getEntryBlock();
for (SILArgument *arg :
make_range(entryBlock->args_begin(), entryBlock->args_end())) {
paramsForForwarding.emplace_back(arg);
}
SILBasicBlock *availableBB = createBasicBlock("availableBB");
SILBasicBlock *unavailableBB = createBasicBlock("unavailableBB");
// if #available(...) {
// <availableBB>
// } else {
// <unavailableBB>
// }
emitBackDeployIfAvailableCondition(*this, FD, loc, availableBB,
unavailableBB);
// <availableBB>:
// return (try)? (await)? (self.)?X(...)
{
B.emitBlock(availableBB);
SILDeclRef original =
thunk.asBackDeploymentKind(SILDeclRef::BackDeploymentKind::None);
emitBackDeployForwardApplyAndReturnOrThrow(*this, FD, loc, original,
paramsForForwarding);
}
// <unavailableBB>:
// return (try)? (await)? (self.)?X_fallback(...)
{
B.emitBlock(unavailableBB);
SILDeclRef fallback =
thunk.asBackDeploymentKind(SILDeclRef::BackDeploymentKind::Fallback);
emitBackDeployForwardApplyAndReturnOrThrow(*this, FD, loc, fallback,
paramsForForwarding);
}
emitEpilog(FD);
}