@@ -712,7 +712,7 @@ void swift::deleteDevirtualizedApply(ApplySite old) {
712712 recursivelyDeleteTriviallyDeadInstructions (oldApply, true );
713713}
714714
715- SILFunction *swift::getTargetClassMethod (SILModule &module , ClassDecl *cd,
715+ SILFunction *swift::getTargetClassMethod (SILModule &module , FullApplySite as, ClassDecl *cd,
716716 CanType classType, MethodInst *mi) {
717717 assert ((isa<ClassMethodInst>(mi) || isa<SuperMethodInst>(mi)) &&
718718 " Only class_method and super_method instructions are supported" );
@@ -721,6 +721,11 @@ SILFunction *swift::getTargetClassMethod(SILModule &module, ClassDecl *cd,
721721
722722 SILType silType = SILType::getPrimitiveObjectType (classType);
723723 if (auto *vtable = module .lookUpSpecializedVTable (silType)) {
724+ // We cannot de-virtualize a generic method call to a specialized method.
725+ // This would result in wrong argument/return calling conventions.
726+ if (as.getSubstitutionMap ().hasAnySubstitutableParams ())
727+ return nullptr ;
728+
724729 return vtable->getEntry (module , member)->getImplementation ();
725730 }
726731
@@ -757,7 +762,7 @@ bool swift::canDevirtualizeClassMethod(FullApplySite applySite, ClassDecl *cd,
757762 auto *mi = cast<MethodInst>(applySite.getCallee ());
758763
759764 // Find the implementation of the member which should be invoked.
760- auto *f = getTargetClassMethod (module , cd, classType, mi);
765+ auto *f = getTargetClassMethod (module , applySite, cd, classType, mi);
761766
762767 // If we do not find any such function, we have no function to devirtualize
763768 // to... so bail.
@@ -843,7 +848,7 @@ swift::devirtualizeClassMethod(SILPassManager *pm, FullApplySite applySite,
843848 SILModule &module = applySite.getModule ();
844849 auto *mi = cast<MethodInst>(applySite.getCallee ());
845850
846- auto *f = getTargetClassMethod (module , cd, classType, mi);
851+ auto *f = getTargetClassMethod (module , applySite, cd, classType, mi);
847852
848853 CanSILFunctionType genCalleeType = f->getLoweredFunctionTypeInContext (
849854 TypeExpansionContext (*applySite.getFunction ()));
0 commit comments