@@ -550,8 +550,9 @@ bool ReabstractionInfo::canBeSpecialized(ApplySite Apply, SILFunction *Callee,
550550ReabstractionInfo::ReabstractionInfo (
551551 ModuleDecl *targetModule, bool isWholeModule, ApplySite Apply,
552552 SILFunction *Callee, SubstitutionMap ParamSubs, IsSerialized_t Serialized,
553- bool ConvertIndirectToDirect, OptRemark::Emitter *ORE)
553+ bool ConvertIndirectToDirect, bool dropMetatypeArgs, OptRemark::Emitter *ORE)
554554 : ConvertIndirectToDirect(ConvertIndirectToDirect),
555+ dropMetatypeArgs(dropMetatypeArgs),
555556 TargetModule(targetModule), isWholeModule(isWholeModule),
556557 Serialized(Serialized) {
557558 if (!prepareAndCheck (Apply, Callee, ParamSubs, ORE))
@@ -683,6 +684,7 @@ void ReabstractionInfo::createSubstitutedAndSpecializedTypes() {
683684 SubstitutedType->getParameters ().size ();
684685 Conversions.resize (NumArgs);
685686 TrivialArgs.resize (NumArgs);
687+ droppedMetatypeArgs.resize (NumArgs);
686688
687689 SILFunctionConventions substConv (SubstitutedType, M);
688690 TypeExpansionContext resilienceExp = getResilienceExpansion ();
@@ -737,11 +739,17 @@ void ReabstractionInfo::createSubstitutedAndSpecializedTypes() {
737739 case ParameterConvention::Indirect_In_Constant:
738740 case ParameterConvention::Indirect_Inout:
739741 case ParameterConvention::Indirect_InoutAliasable:
742+ break ;
743+
740744 case ParameterConvention::Direct_Owned:
741745 case ParameterConvention::Direct_Unowned:
742- case ParameterConvention::Direct_Guaranteed:
746+ case ParameterConvention::Direct_Guaranteed: {
747+ CanType ty = PI.getInterfaceType ();
748+ if (dropMetatypeArgs && isa<MetatypeType>(ty) && !ty->hasArchetype ())
749+ droppedMetatypeArgs.set (IdxToInsert);
743750 break ;
744751 }
752+ }
745753 }
746754
747755 // Produce a specialized type, which is the substituted type with
@@ -852,11 +860,16 @@ createSpecializedType(CanSILFunctionType SubstFTy, SILModule &M) const {
852860 // No conversion: re-use the original, substituted result info.
853861 SpecializedResults.push_back (RI);
854862 }
855- unsigned ParamIdx = 0 ;
863+ unsigned idx = 0 ;
856864 for (SILParameterInfo PI : SubstFTy->getParameters ()) {
865+ unsigned paramIdx = idx++;
857866 PI = PI.getUnsubstituted (M, SubstFTy, context);
858- bool isTrivial = TrivialArgs.test (param2ArgIndex (ParamIdx));
859- if (!isParamConverted (ParamIdx++)) {
867+
868+ if (isDroppedMetatypeArg (param2ArgIndex (paramIdx)))
869+ continue ;
870+
871+ bool isTrivial = TrivialArgs.test (param2ArgIndex (paramIdx));
872+ if (!isParamConverted (paramIdx)) {
860873 // No conversion: re-use the original, substituted parameter info.
861874 SpecializedParams.push_back (PI);
862875 continue ;
@@ -1854,7 +1867,8 @@ GenericFuncSpecializer::GenericFuncSpecializer(
18541867 ClonedName = Mangler.manglePrespecialized (ParamSubs);
18551868 } else {
18561869 ClonedName = Mangler.mangleReabstracted (ParamSubs,
1857- ReInfo.needAlternativeMangling ());
1870+ ReInfo.needAlternativeMangling (),
1871+ ReInfo.hasDroppedMetatypeArgs ());
18581872 }
18591873 }
18601874 LLVM_DEBUG (llvm::dbgs () << " Specialized function " << ClonedName << ' \n ' );
@@ -1995,6 +2009,9 @@ prepareCallArguments(ApplySite AI, SILBuilder &Builder,
19952009 return true ;
19962010 }
19972011
2012+ if (ReInfo.isDroppedMetatypeArg (ArgIdx))
2013+ return true ;
2014+
19982015 // Handle arguments for formal parameters.
19992016 unsigned paramIdx = ArgIdx - substConv.getSILArgIndexOfFirstParam ();
20002017 if (!ReInfo.isParamConverted (paramIdx)) {
@@ -2418,7 +2435,7 @@ static bool createPrespecialized(StringRef UnspecializedName,
24182435 ReabstractionInfo ReInfo (M.getSwiftModule (), M.isWholeModule (), ApplySite (),
24192436 UnspecFunc, Apply.getSubstitutionMap (),
24202437 IsNotSerialized,
2421- /* ConvertIndirectToDirect=*/ true , nullptr );
2438+ /* ConvertIndirectToDirect=*/ true );
24222439
24232440 if (!ReInfo.canBeSpecialized ())
24242441 return false ;
@@ -2599,7 +2616,9 @@ void swift::trySpecializeApplyOfGeneric(
25992616 ReabstractionInfo ReInfo (FuncBuilder.getModule ().getSwiftModule (),
26002617 FuncBuilder.getModule ().isWholeModule (), Apply, RefF,
26012618 Apply.getSubstitutionMap (), Serialized,
2602- /* ConvertIndirectToDirect=*/ true , &ORE);
2619+ /* ConvertIndirectToDirect=*/ true ,
2620+ /* dropMetatypeArgs=*/ isMandatory,
2621+ &ORE);
26032622 if (!ReInfo.canBeSpecialized ())
26042623 return ;
26052624
0 commit comments