@@ -718,7 +718,7 @@ void swift::deleteDevirtualizedApply(ApplySite old) {
718718 recursivelyDeleteTriviallyDeadInstructions (oldApply, true );
719719}
720720
721- SILFunction *swift::getTargetClassMethod (SILModule &module , ClassDecl *cd,
721+ SILFunction *swift::getTargetClassMethod (SILModule &module , FullApplySite as, ClassDecl *cd,
722722 CanType classType, MethodInst *mi) {
723723 assert ((isa<ClassMethodInst>(mi) || isa<SuperMethodInst>(mi)) &&
724724 " Only class_method and super_method instructions are supported" );
@@ -727,6 +727,11 @@ SILFunction *swift::getTargetClassMethod(SILModule &module, ClassDecl *cd,
727727
728728 SILType silType = SILType::getPrimitiveObjectType (classType);
729729 if (auto *vtable = module .lookUpSpecializedVTable (silType)) {
730+ // We cannot de-virtualize a generic method call to a specialized method.
731+ // This would result in wrong argument/return calling conventions.
732+ if (as.getSubstitutionMap ().hasAnySubstitutableParams ())
733+ return nullptr ;
734+
730735 return vtable->getEntry (module , member)->getImplementation ();
731736 }
732737
@@ -763,7 +768,7 @@ bool swift::canDevirtualizeClassMethod(FullApplySite applySite, ClassDecl *cd,
763768 auto *mi = cast<MethodInst>(applySite.getCallee ());
764769
765770 // Find the implementation of the member which should be invoked.
766- auto *f = getTargetClassMethod (module , cd, classType, mi);
771+ auto *f = getTargetClassMethod (module , applySite, cd, classType, mi);
767772
768773 // If we do not find any such function, we have no function to devirtualize
769774 // to... so bail.
@@ -849,7 +854,7 @@ swift::devirtualizeClassMethod(SILPassManager *pm, FullApplySite applySite,
849854 SILModule &module = applySite.getModule ();
850855 auto *mi = cast<MethodInst>(applySite.getCallee ());
851856
852- auto *f = getTargetClassMethod (module , cd, classType, mi);
857+ auto *f = getTargetClassMethod (module , applySite, cd, classType, mi);
853858
854859 CanSILFunctionType genCalleeType = f->getLoweredFunctionTypeInContext (
855860 TypeExpansionContext (*applySite.getFunction ()));
0 commit comments