@@ -712,7 +712,7 @@ void swift::deleteDevirtualizedApply(ApplySite old) {
712
712
recursivelyDeleteTriviallyDeadInstructions (oldApply, true );
713
713
}
714
714
715
- SILFunction *swift::getTargetClassMethod (SILModule &module , ClassDecl *cd,
715
+ SILFunction *swift::getTargetClassMethod (SILModule &module , FullApplySite as, ClassDecl *cd,
716
716
CanType classType, MethodInst *mi) {
717
717
assert ((isa<ClassMethodInst>(mi) || isa<SuperMethodInst>(mi)) &&
718
718
" Only class_method and super_method instructions are supported" );
@@ -721,6 +721,11 @@ SILFunction *swift::getTargetClassMethod(SILModule &module, ClassDecl *cd,
721
721
722
722
SILType silType = SILType::getPrimitiveObjectType (classType);
723
723
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
+
724
729
return vtable->getEntry (module , member)->getImplementation ();
725
730
}
726
731
@@ -757,7 +762,7 @@ bool swift::canDevirtualizeClassMethod(FullApplySite applySite, ClassDecl *cd,
757
762
auto *mi = cast<MethodInst>(applySite.getCallee ());
758
763
759
764
// 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);
761
766
762
767
// If we do not find any such function, we have no function to devirtualize
763
768
// to... so bail.
@@ -843,7 +848,7 @@ swift::devirtualizeClassMethod(SILPassManager *pm, FullApplySite applySite,
843
848
SILModule &module = applySite.getModule ();
844
849
auto *mi = cast<MethodInst>(applySite.getCallee ());
845
850
846
- auto *f = getTargetClassMethod (module , cd, classType, mi);
851
+ auto *f = getTargetClassMethod (module , applySite, cd, classType, mi);
847
852
848
853
CanSILFunctionType genCalleeType = f->getLoweredFunctionTypeInContext (
849
854
TypeExpansionContext (*applySite.getFunction ()));
0 commit comments