15
15
#include " swift/SIL/SILFunction.h"
16
16
#include " swift/SIL/Dominance.h"
17
17
#include " swift/SILOptimizer/Analysis/DominanceAnalysis.h"
18
+ #include " swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
18
19
#include " swift/SILOptimizer/PassManager/Transforms.h"
19
20
#include " llvm/ADT/ScopedHashTable.h"
20
21
@@ -49,19 +50,26 @@ namespace {
49
50
class LowerHopToActor {
50
51
SILFunction *F;
51
52
DominanceInfo *Dominance;
53
+ SILOptFunctionBuilder &functionBuilder;
52
54
53
55
// / A map from an actor value to the executor we've derived for it.
54
56
llvm::ScopedHashTable<SILValue, SILValue> ExecutorForActor;
55
57
56
58
bool processHop (HopToExecutorInst *hop);
57
59
bool processExtract (ExtractExecutorInst *extract);
58
60
59
- SILValue emitGetExecutor (SILBuilderWithScope &B, SILLocation loc,
61
+ SILValue emitGetExecutor (SILBuilderWithScope &B,
62
+ SILLocation loc,
60
63
SILValue actor, bool makeOptional);
61
64
62
65
public:
63
- LowerHopToActor (SILFunction *f, DominanceInfo *dominance)
64
- : F(f), Dominance(dominance) { }
66
+ LowerHopToActor (SILFunction *f,
67
+ SILOptFunctionBuilder &FunctionBuilder,
68
+ DominanceInfo *dominance)
69
+ : F(f),
70
+ Dominance (dominance),
71
+ functionBuilder(FunctionBuilder)
72
+ { }
65
73
66
74
// / The entry point to the transformation.
67
75
bool run ();
@@ -154,28 +162,6 @@ static AccessorDecl *getUnownedExecutorGetter(ASTContext &ctx,
154
162
return nullptr ;
155
163
}
156
164
157
- static AccessorDecl *getUnwrapLocalUnownedExecutorGetter (ASTContext &ctx,
158
- ProtocolDecl *actorProtocol) {
159
- for (auto member: actorProtocol->getAllMembers ()) { // FIXME: remove this, just go to the extension
160
- if (auto var = dyn_cast<VarDecl>(member)) {
161
- if (var->getName () == ctx.Id__unwrapLocalUnownedExecutor )
162
- return var->getAccessor (AccessorKind::Get);
163
- }
164
- }
165
-
166
- for (auto extension: actorProtocol->getExtensions ()) {
167
- for (auto member: extension->getAllMembers ()) {
168
- if (auto var = dyn_cast<VarDecl>(member)) {
169
- if (var->getName () == ctx.Id__unwrapLocalUnownedExecutor ) {
170
- return var->getAccessor (AccessorKind::Get);
171
- }
172
- }
173
- }
174
- }
175
-
176
- return nullptr ;
177
- }
178
-
179
165
SILValue LowerHopToActor::emitGetExecutor (SILBuilderWithScope &B,
180
166
SILLocation loc, SILValue actor,
181
167
bool makeOptional) {
@@ -212,7 +198,8 @@ SILValue LowerHopToActor::emitGetExecutor(SILBuilderWithScope &B,
212
198
} else if (actorType->isDistributedActor ()) {
213
199
auto actorKind = KnownProtocolKind::DistributedActor;
214
200
auto actorProtocol = ctx.getProtocol (actorKind);
215
- auto req = getUnwrapLocalUnownedExecutorGetter (ctx, actorProtocol);
201
+ // auto req = getUnwrapLocalUnownedExecutorGetter(ctx, actorProtocol);
202
+ auto req = ctx.getGetUnwrapLocalDistributedActorUnownedExecutor ();
216
203
assert (req && " Distributed library broken" );
217
204
SILDeclRef fn (req, SILDeclRef::Kind::Func);
218
205
@@ -222,12 +209,19 @@ SILValue LowerHopToActor::emitGetExecutor(SILBuilderWithScope &B,
222
209
223
210
auto subs = SubstitutionMap::get (req->getGenericSignature (),
224
211
{actorType}, {actorConf});
225
- auto fnType = F->getModule ().Types .getConstantFunctionType (*F, fn);
226
212
227
- auto witness =
228
- B.createWitnessMethod (loc, actorType, actorConf, fn,
229
- SILType::getPrimitiveObjectType (fnType));
230
- auto witnessCall = B.createApply (loc, witness, subs, {actor});
213
+ // Find the unwrap function
214
+ FuncDecl *funcDecl = ctx.getGetUnwrapLocalDistributedActorUnownedExecutor ();
215
+ assert (funcDecl);
216
+ auto funcDeclRef = SILDeclRef (funcDecl, SILDeclRef::Kind::Func);
217
+
218
+ SILFunction *unwrapExecutorFun =
219
+ functionBuilder.getOrCreateFunction (
220
+ loc, funcDeclRef, ForDefinition_t::NotForDefinition);
221
+ assert (unwrapExecutorFun && " no sil function!" );
222
+ auto funcRef =
223
+ B.createFunctionRef (loc, unwrapExecutorFun);
224
+ auto witnessCall = B.createApply (loc, funcRef, subs, {actor});
231
225
232
226
// The protocol requirement returns an Optional<UnownedSerialExecutor>;
233
227
// extract the Builtin.Executor from it.
@@ -286,7 +280,8 @@ class LowerHopToActorPass : public SILFunctionTransform {
286
280
void run () override {
287
281
auto fn = getFunction ();
288
282
auto domTree = getAnalysis<DominanceAnalysis>()->get (fn);
289
- LowerHopToActor pass (getFunction (), domTree);
283
+ auto functionBuilder = SILOptFunctionBuilder (*this );
284
+ LowerHopToActor pass (getFunction (), functionBuilder, domTree);
290
285
if (pass.run ())
291
286
invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
292
287
}
0 commit comments