Skip to content

Commit 548f2f6

Browse files
committed
[IRGen] Distributed/NFC: Collect all target handling logic into AccessorTarget
1 parent 8923174 commit 548f2f6

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

lib/IRGen/GenDistributed.cpp

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,43 @@ struct ArgumentDecoderInfo {
125125
Callee getCallee() const;
126126
};
127127

128+
struct AccessorTarget {
129+
private:
130+
IRGenFunction &IGF;
131+
SILFunction *Target;
132+
133+
CanSILFunctionType Type;
134+
135+
public:
136+
AccessorTarget(IRGenFunction &IGF, SILFunction *target)
137+
: IGF(IGF), Target(target), Type(target->getLoweredFunctionType()) {}
138+
139+
DeclContext *getDeclContext() const { return Target->getDeclContext(); }
140+
141+
CanSILFunctionType getType() const { return Type; }
142+
143+
bool isGeneric() const { return Target->isGeneric(); }
144+
145+
Callee getCallee(llvm::Value *actorSelf) const;
146+
147+
LinkEntity getLinking() const {
148+
return LinkEntity::forDistributedTargetAccessor(Target);
149+
}
150+
151+
WitnessMetadata *getWitnessMetadata() const {
152+
return nullptr;
153+
}
154+
155+
public:
156+
FunctionPointer getPointerToTarget() const;
157+
};
158+
128159
class DistributedAccessor {
129160
IRGenModule &IGM;
130161
IRGenFunction &IGF;
131162

132163
/// Underlying distributed method for this accessor.
133-
SILFunction *Target;
164+
AccessorTarget Target;
134165

135166
/// The interface type of this accessor function.
136167
CanSILFunctionType AccessorType;
@@ -178,10 +209,6 @@ class DistributedAccessor {
178209
/// all the argument allocations.
179210
void emitReturn(llvm::Value *errorValue);
180211

181-
FunctionPointer getPointerToTarget() const;
182-
183-
Callee getCalleeForDistributedTarget(llvm::Value *self) const;
184-
185212
/// Given an instance of invocation decoder, its type metadata,
186213
/// and protocol witness table, find `decodeNextArgument`.
187214
ArgumentDecoderInfo findArgumentDecoder(llvm::Value *decoder,
@@ -318,17 +345,17 @@ void IRGenModule::emitDistributedTargetAccessor(SILFunction *target) {
318345
DistributedAccessor::DistributedAccessor(IRGenFunction &IGF,
319346
SILFunction *target,
320347
CanSILFunctionType accessorTy)
321-
: IGM(IGF.IGM), IGF(IGF), Target(target), AccessorType(accessorTy),
322-
AsyncLayout(getAsyncContextLayout(
323-
IGM, AccessorType, AccessorType, SubstitutionMap())) {
348+
: IGM(IGF.IGM), IGF(IGF), Target(IGF, target), AccessorType(accessorTy),
349+
AsyncLayout(getAsyncContextLayout(IGM, AccessorType, AccessorType,
350+
SubstitutionMap())) {
324351
if (IGM.DebugInfo)
325352
IGM.DebugInfo->emitArtificialFunction(IGF, IGF.CurFn);
326353
}
327354

328355
void DistributedAccessor::decodeArguments(const ArgumentDecoderInfo &decoder,
329356
llvm::Value *argumentTypes,
330357
Explosion &arguments) {
331-
auto fnType = Target->getLoweredFunctionType();
358+
auto fnType = Target.getType();
332359

333360
// Cover all of the arguments except to `self` of the actor.
334361
auto parameters = fnType->getParameters().drop_back();
@@ -612,7 +639,7 @@ void DistributedAccessor::emitReturn(llvm::Value *errorValue) {
612639
}
613640

614641
void DistributedAccessor::emit() {
615-
auto targetTy = Target->getLoweredFunctionType();
642+
auto targetTy = Target.getType();
616643
SILFunctionConventions targetConv(targetTy, IGF.getSILModule());
617644
TypeExpansionContext expansionContext = IGM.getMaximalTypeExpansionContext();
618645

@@ -657,7 +684,7 @@ void DistributedAccessor::emit() {
657684
Signature::forAsyncEntry(IGM, AccessorType, fpKind)
658685
.getAsyncContextIndex();
659686

660-
auto entity = LinkEntity::forDistributedTargetAccessor(Target);
687+
auto entity = Target.getLinking();
661688
emitAsyncFunctionEntry(IGF, AsyncLayout, entity, asyncContextIdx);
662689
emitAsyncFunctionPointer(IGM, IGF.CurFn, entity, AsyncLayout.getSize());
663690
}
@@ -687,7 +714,7 @@ void DistributedAccessor::emit() {
687714
}
688715

689716
// Add all of the substitutions to the explosion
690-
if (Target->isGeneric()) {
717+
if (Target.isGeneric()) {
691718
// swift.type **
692719
llvm::Value *substitutionBuffer =
693720
IGF.Builder.CreateBitCast(substitutions, IGM.TypeMetadataPtrPtrTy);
@@ -726,13 +753,13 @@ void DistributedAccessor::emit() {
726753
Explosion result;
727754
llvm::Value *targetError = nullptr;
728755

729-
auto callee = getCalleeForDistributedTarget(actorSelf);
756+
auto callee = Target.getCallee(actorSelf);
730757
auto emission =
731758
getCallEmission(IGF, callee.getSwiftContext(), std::move(callee));
732759

733760
emission->begin();
734761
emission->setArgs(arguments, /*isOutlined=*/false,
735-
/*witnessMetadata=*/nullptr);
762+
Target.getWitnessMetadata());
736763

737764
// Load result of the thunk into the location provided by the caller.
738765
// This would only generate code for direct results, if thunk has an
@@ -763,31 +790,29 @@ void DistributedAccessor::emit() {
763790
}
764791
}
765792

766-
FunctionPointer DistributedAccessor::getPointerToTarget() const {
767-
auto fnType = Target->getLoweredFunctionType();
793+
FunctionPointer AccessorTarget::getPointerToTarget() const {
794+
auto &IGM = IGF.IGM;
768795
auto fpKind = classifyFunctionPointerKind(Target);
769-
auto signature = IGM.getSignature(fnType, fpKind);
796+
auto signature = IGM.getSignature(Type, fpKind);
770797

771798
auto *fnPtr =
772799
llvm::ConstantExpr::getBitCast(IGM.getAddrOfAsyncFunctionPointer(Target),
773800
signature.getType()->getPointerTo());
774801

775802
return FunctionPointer::forDirect(
776-
FunctionPointer::Kind(fnType), fnPtr,
803+
FunctionPointer::Kind(Type), fnPtr,
777804
IGM.getAddrOfSILFunction(Target, NotForDefinition), signature);
778805
}
779806

780-
Callee
781-
DistributedAccessor::getCalleeForDistributedTarget(llvm::Value *self) const {
782-
auto fnType = Target->getLoweredFunctionType();
783-
CalleeInfo info{fnType, fnType, SubstitutionMap()};
784-
return {std::move(info), getPointerToTarget(), self};
807+
Callee AccessorTarget::getCallee(llvm::Value *actorSelf) const {
808+
CalleeInfo info{Type, Type, SubstitutionMap()};
809+
return {std::move(info), getPointerToTarget(), actorSelf};
785810
}
786811

787812
ArgumentDecoderInfo DistributedAccessor::findArgumentDecoder(
788813
llvm::Value *decoder, llvm::Value *decoderTy, llvm::Value *witnessTable) {
789814
auto &C = IGM.Context;
790-
DeclContext *targetContext = Target->getDeclContext();
815+
DeclContext *targetContext = Target.getDeclContext();
791816
auto expansionContext = IGM.getMaximalTypeExpansionContext();
792817

793818
/// If the context was a function, unwrap it and look for the decode method
@@ -796,10 +821,9 @@ ArgumentDecoderInfo DistributedAccessor::findArgumentDecoder(
796821
FuncDecl *decodeFn = nullptr;
797822
if (auto func = dyn_cast<AbstractFunctionDecl>(targetContext)) {
798823
decodeFn = C.getDistributedActorArgumentDecodingMethod(
799-
func->getDeclContext()->getSelfNominalTypeDecl());;
824+
func->getDeclContext()->getSelfNominalTypeDecl());
800825
}
801826

802-
803827
// If distributed actor is generic over actor system, we have to
804828
// use witness to reference `decodeNextArgument`.
805829
if (!decodeFn) {

0 commit comments

Comments
 (0)