Skip to content

Commit 8159b23

Browse files
committed
[Distributed] Handle dispach thunks of decodeNextArgument in Distributed IRGen
correct way to detect dispatch thunk presence in GenDistributed
1 parent 64cd96e commit 8159b23

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/IRGen/GenDistributed.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ void DistributedAccessor::emit() {
693693
}
694694

695695
// Add all of the substitutions to the explosion
696-
if (auto *genericEnvironment = Target->getGenericEnvironment()) {
696+
if (Target->getGenericEnvironment()) {
697697
// swift.type **
698698
llvm::Value *substitutionBuffer =
699699
IGF.Builder.CreateBitCast(substitutions, IGM.TypeMetadataPtrPtrTy);
@@ -817,7 +817,9 @@ ArgumentDecoderInfo DistributedAccessor::findArgumentDecoder(
817817
// is passed indirectly. This is good for structs and enums because
818818
// `decodeNextArgument` is a mutating method, but not for classes because
819819
// in that case heap object is mutated directly.
820-
if (isa<ClassDecl>(decoderDecl)) {
820+
llvm::Function *fnPtr = nullptr;
821+
bool usesDispatchThunk = false;
822+
if (auto classDecl = dyn_cast<ClassDecl>(decoderDecl)) {
821823
auto selfTy = methodTy->getSelfParameter().getSILStorageType(
822824
IGM.getSILModule(), methodTy, expansionContext);
823825

@@ -836,15 +838,35 @@ ArgumentDecoderInfo DistributedAccessor::findArgumentDecoder(
836838
instance);
837839

838840
decoder = instance.claimNext();
839-
}
840841

841-
auto *decodeSIL = IGM.getSILModule().lookUpFunction(SILDeclRef(decodeFn));
842-
auto *fnPtr = IGM.getAddrOfSILFunction(decodeSIL, NotForDefinition,
843-
/*isDynamicallyReplaceable=*/false);
842+
/// When using library evolution functions have another "dispatch thunk"
843+
/// so we must use this instead of the decodeFn directly.
844+
if (classDecl->hasResilientMetadata()) {
845+
if (getMethodDispatch(decodeFn) == swift::MethodDispatch::Class) {
846+
SILDeclRef(decodeFn).getOverriddenVTableEntry().getDecl()->getDeclContext()->dumpContext();
847+
fnPtr = IGM.getAddrOfDispatchThunk(SILDeclRef(decodeFn), NotForDefinition);
848+
usesDispatchThunk = true;
849+
}
850+
}
851+
}
844852

845-
auto methodPtr = FunctionPointer::forDirect(
846-
classifyFunctionPointerKind(decodeSIL), fnPtr,
847-
/*secondaryValue=*/nullptr, signature);
853+
if (!fnPtr) {
854+
auto *decodeSIL = IGM.getSILModule().lookUpFunction(SILDeclRef(decodeFn));
855+
fnPtr = IGM.getAddrOfSILFunction(decodeSIL, NotForDefinition,
856+
/*isDynamicallyReplaceable=*/false);
857+
}
858+
assert(fnPtr);
859+
860+
FunctionPointer methodPtr;
861+
if (usesDispatchThunk) {
862+
methodPtr = FunctionPointer::createUnsigned(
863+
methodTy, fnPtr, signature, /*useSignature=*/true);
864+
} else {
865+
auto *decodeSIL = IGM.getSILModule().lookUpFunction(SILDeclRef(decodeFn));
866+
methodPtr = FunctionPointer::forDirect(
867+
classifyFunctionPointerKind(decodeSIL), fnPtr,
868+
/*secondaryValue=*/nullptr, signature);
869+
}
848870

849871
return {decoder, decoderTy, witnessTable, methodPtr, methodTy};
850872
}

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
524524
{
525525
auto doneRecordingDecl =
526526
C.getDoneRecordingOnDistributedInvocationEncoder(invocationEncoderDecl);
527+
assert(doneRecordingDecl && "Could not find 'doneRecording' ad-hoc requirement witness.");
527528
auto doneRecordingDeclRef =
528529
UnresolvedDeclRefExpr::createImplicit(C, doneRecordingDecl->getName());
529530

0 commit comments

Comments
 (0)