-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathLowerHopToActor.cpp
355 lines (305 loc) · 13.2 KB
/
LowerHopToActor.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//===------- LowerHopToActor.cpp - Lower hop_to_executor on actors --------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "insert-hop-to-executor"
#include "swift/AST/ConformanceLookup.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/FrozenMultiMap.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/Dominance.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "llvm/ADT/ScopedHashTable.h"
using namespace swift;
namespace {
/// Lower hop_to_executor instructions with actor operands.
///
/// While the language centers actors as the core concept, the runtime
/// is largely expressed in terms of executors, which intentionally are
/// an independent concept. Every actor has an executor; actors can
/// customize their executor, subject to three restrictions:
///
/// - Any given actor must report the same executor every time its
/// executor is derived. An actor can be lazy about creating its
/// executor, but it can't have multiple executors, even at different
/// points in its lifetime.
///
/// - Keeping the actor reference alive must keep the executor alive.
///
/// - Derivations of the executor may be freely removed, combined,
/// or sunk by the compiler. (Whether they should also be hoistable
/// is more contentious.)
///
/// To facilitate full optimization of hops, SILGen emits hops to actors
/// with a hop_to_executor with an actor operand. (Among other benefits,
/// this means that OptimizeHopToExecutor will eliminate the derivation
/// operation associated with the hop.) This pass then comes along and
/// inserts the derivations, turning those hops into hops to executors.
/// IRGen expects hops to be to executors before it runs.
class LowerHopToActor {
SILFunction *F;
DominanceInfo *Dominance;
/// A map from an actor value to the dominating instruction that
/// will derive the executor.
llvm::ScopedHashTable<SILValue, SILInstruction *>
ExecutorDerivationForActor;
/// A multi-map from a dominating {hop_to_|extract_}executor instruction
/// to other reachable {hop_to_|extract_}executor instructions.
SmallFrozenMultiMap<SILInstruction *, SILInstruction *, 4>
DominatingActorHops;
void recordDominatingInstFor(SILInstruction *inst);
void rewriteInstructions();
SILValue emitGetExecutor(SILBuilderWithScope &B,
SILLocation loc,
SILValue actor, bool makeOptional);
public:
LowerHopToActor(SILFunction *f,
DominanceInfo *dominance)
: F(f),
Dominance(dominance)
{ }
/// The entry point to the transformation.
bool run();
};
bool LowerHopToActor::run() {
// Record all actor operands to hop_to_executor and extract_executor
// and the dominating instruction that will derive the executor.
auto runOnBlock = [&](SILBasicBlock *block) {
for (auto ii = block->begin(), ie = block->end(); ii != ie; ) {
SILInstruction *inst = &*ii++;
recordDominatingInstFor(inst);
}
};
runInDominanceOrderWithScopes(Dominance, runOnBlock,
ExecutorDerivationForActor);
// If we didn't record any dominating actor hops that need
// transformation, we're done.
if (DominatingActorHops.empty())
return false;
rewriteInstructions();
return true;
}
static bool isOptionalBuiltinExecutor(SILType type) {
if (auto objectType = type.getOptionalObjectType())
return objectType.is<BuiltinExecutorType>();
return false;
}
void LowerHopToActor::recordDominatingInstFor(SILInstruction *inst) {
SILValue actor;
if (auto *hop = dyn_cast<HopToExecutorInst>(inst)) {
// hop_to_executor can take optional and non-optional Builtin.Executor
// values directly. If we see Optional<Builtin.Executor>, there's
// nothing to do.
actor = hop->getTargetExecutor();
if (isOptionalBuiltinExecutor(actor->getType()))
return;
} else if (auto *extract = dyn_cast<ExtractExecutorInst>(inst)) {
// extract_executor can only take non-optional actor values.
actor = extract->getExpectedExecutor();
} else {
return;
}
auto *dominatingInst = ExecutorDerivationForActor.lookup(actor);
if (dominatingInst) {
DominatingActorHops.insert(dominatingInst, inst);
} else {
DominatingActorHops.insert(inst, inst);
ExecutorDerivationForActor.insert(actor, inst);
}
return;
}
void LowerHopToActor::rewriteInstructions() {
// Lower the actor operands to executors. Dominating instructions
// will perform the derivation, and the result will be reused in
// all reachable instructions.
DominatingActorHops.setFrozen();
for (auto domInst : DominatingActorHops.getRange()) {
auto derivationInst = domInst.first;
SILValue actor;
bool makeOptional;
if (auto *hop = dyn_cast<HopToExecutorInst>(derivationInst)) {
actor = hop->getTargetExecutor();
makeOptional = true;
} else if (auto *extract = dyn_cast<ExtractExecutorInst>(derivationInst)) {
actor = extract->getExpectedExecutor();
makeOptional = false;
} else {
continue;
}
// Emit the executor derivation at the dominating instruction.
SILBuilderWithScope builder(derivationInst);
auto executor = emitGetExecutor(
builder, derivationInst->getLoc(), actor, makeOptional);
derivationInst->setOperand(0, executor);
// Set the executor value as the operand for all reachable instructions.
auto reachableInsts = domInst.second;
for (auto inst : reachableInsts) {
if (auto *extract = dyn_cast<ExtractExecutorInst>(inst)) {
extract->replaceAllUsesWith(executor);
extract->eraseFromParent();
continue;
}
inst->setOperand(0, executor);
}
}
}
static bool isDefaultActorType(CanType actorType, ModuleDecl *M,
ResilienceExpansion expansion) {
if (auto cls = actorType.getClassOrBoundGenericClass())
return cls->isDefaultActor(M, expansion);
return false;
}
static AccessorDecl *getUnownedExecutorGetter(ASTContext &ctx,
ProtocolDecl *actorProtocol) {
for (auto member: actorProtocol->getAllMembers()) {
if (auto var = dyn_cast<VarDecl>(member)) {
if (var->getName() == ctx.Id_unownedExecutor)
return var->getAccessor(AccessorKind::Get);
}
}
return nullptr;
}
SILValue LowerHopToActor::emitGetExecutor(SILBuilderWithScope &B,
SILLocation loc, SILValue actor,
bool makeOptional) {
// This is okay because actor types have to be classes and so never
// have multiple abstraction patterns.
CanType actorType = actor->getType().getASTType();
// If the operand is already a BuiltinExecutorType, just wrap it
// in an optional.
if (makeOptional && actor->getType().is<BuiltinExecutorType>()) {
return B.createOptionalSome(
loc, actor,
SILType::getOptionalType(actor->getType()));
}
auto &ctx = F->getASTContext();
auto executorType = SILType::getPrimitiveObjectType(ctx.TheExecutorType);
auto optionalExecutorType = SILType::getOptionalType(executorType);
/// Emit the instructions to derive an executor value from an actor value.
auto getExecutorFor = [&](SILValue actor) -> SILValue {
// If the actor type is a default actor, go ahead and devirtualize here.
auto module = F->getModule().getSwiftModule();
CanType actorType = actor->getType().getASTType();
// Determine if the actor is a "default actor" in which case we'll build a default
// actor executor ref inline, rather than calling out to the user-provided executor function.
if (isDefaultActorType(actorType, module, F->getResilienceExpansion())) {
auto builtinName = ctx.getIdentifier(
getBuiltinName(BuiltinValueKind::BuildDefaultActorExecutorRef));
auto builtinDecl = cast<FuncDecl>(getBuiltinValueDecl(ctx, builtinName));
auto subs = SubstitutionMap::get(builtinDecl->getGenericSignature(),
{actorType},
LookUpConformanceInModule());
return B.createBuiltin(loc, builtinName, executorType, subs, {actor});
}
// Otherwise, go through (Distributed)Actor.unownedExecutor.
auto actorKind = actorType->isDistributedActor() ?
KnownProtocolKind::DistributedActor :
KnownProtocolKind::Actor;
auto actorProtocol = ctx.getProtocol(actorKind);
auto req = getUnownedExecutorGetter(ctx, actorProtocol);
assert(req && "Concurrency library broken");
SILDeclRef fn(req, SILDeclRef::Kind::Func);
// Open an existential actor type.
if (actorType->isExistentialType()) {
actorType = ExistentialArchetypeType::get(actorType)->getCanonicalType();
SILType loweredActorType = F->getLoweredType(actorType);
actor = B.createOpenExistentialRef(loc, actor, loweredActorType);
}
auto actorConf = lookupConformance(actorType, actorProtocol);
assert(actorConf &&
"hop_to_executor with actor that doesn't conform to Actor or DistributedActor");
auto subs = SubstitutionMap::get(req->getGenericSignature(),
{actorType}, {actorConf});
auto fnType = F->getModule().Types.getConstantFunctionType(*F, fn);
auto witness =
B.createWitnessMethod(loc, actorType, actorConf, fn,
SILType::getPrimitiveObjectType(fnType));
auto witnessCall = B.createApply(loc, witness, subs, {actor});
// The protocol requirement returns an UnownedSerialExecutor; extract
// the Builtin.Executor from it.
auto executorDecl = ctx.getUnownedSerialExecutorDecl();
auto executorProps = executorDecl->getStoredProperties();
assert(executorProps.size() == 1);
return B.createStructExtract(loc, witnessCall, executorProps[0]);
};
bool needEndBorrow = false;
SILValue unmarkedExecutor;
if (auto wrappedActor = actorType->getOptionalObjectType()) {
assert(makeOptional);
if (B.hasOwnership() && actor->getOwnershipKind() == OwnershipKind::Owned) {
actor = B.createBeginBorrow(loc, actor);
needEndBorrow = true;
}
// Unwrap the optional and call 'unownedExecutor'.
auto *someDecl = B.getASTContext().getOptionalSomeDecl();
auto *curBB = B.getInsertionPoint()->getParent();
auto *contBB = curBB->split(B.getInsertionPoint());
auto *someBB = B.getFunction().createBasicBlockAfter(curBB);
auto *noneBB = B.getFunction().createBasicBlockAfter(someBB);
unmarkedExecutor = contBB->createPhiArgument(
optionalExecutorType, actor->getOwnershipKind());
SmallVector<std::pair<EnumElementDecl *, SILBasicBlock *>, 1> caseBBs;
caseBBs.push_back(std::make_pair(someDecl, someBB));
B.setInsertionPoint(curBB);
auto *switchEnum = B.createSwitchEnum(loc, actor, noneBB, caseBBs);
SILValue unwrappedActor;
if (B.hasOwnership()) {
unwrappedActor = switchEnum->createOptionalSomeResult();
B.setInsertionPoint(someBB);
} else {
B.setInsertionPoint(someBB);
unwrappedActor = B.createUncheckedEnumData(loc, actor, someDecl);
}
// Call 'unownedExecutor' in the some block and wrap the result into
// an optional.
SILValue unwrappedExecutor = getExecutorFor(unwrappedActor);
SILValue someValue =
B.createOptionalSome(loc, unwrappedExecutor, optionalExecutorType);
B.createBranch(loc, contBB, {someValue});
// In the none case, create a nil executor value, which represents
// the generic executor.
B.setInsertionPoint(noneBB);
SILValue noneValue = B.createOptionalNone(loc, optionalExecutorType);
B.createBranch(loc, contBB, {noneValue});
B.setInsertionPoint(contBB->begin());
} else {
unmarkedExecutor = getExecutorFor(actor);
// Inject the result into an optional if requested.
if (makeOptional) {
unmarkedExecutor = B.createOptionalSome(loc, unmarkedExecutor,
SILType::getOptionalType(unmarkedExecutor->getType()));
}
}
// Mark the dependence of the resulting value on the actor value to
// force the actor to stay alive.
SILValue executor = B.createMarkDependence(loc, unmarkedExecutor, actor,
MarkDependenceKind::Escaping);
if (needEndBorrow) {
B.createEndBorrow(loc, actor);
}
return executor;
}
class LowerHopToActorPass : public SILFunctionTransform {
/// The entry point to the transformation.
void run() override {
auto fn = getFunction();
auto domTree = getAnalysis<DominanceAnalysis>()->get(fn);
LowerHopToActor pass(getFunction(), domTree);
if (pass.run())
invalidateAnalysis(SILAnalysis::InvalidationKind::BranchesAndInstructions);
}
};
} // end anonymous namespace
SILTransform *swift::createLowerHopToActor() {
return new LowerHopToActorPass();
}