@@ -649,7 +649,7 @@ bool ReabstractionInfo::prepareAndCheck(ApplySite Apply, SILFunction *Callee,
649649
650650bool ReabstractionInfo::canBeSpecialized (ApplySite Apply, SILFunction *Callee,
651651 SubstitutionMap ParamSubs) {
652- ReabstractionInfo ReInfo;
652+ ReabstractionInfo ReInfo (Callee-> getModule ()) ;
653653 return ReInfo.prepareAndCheck (Apply, Callee, ParamSubs);
654654}
655655
@@ -659,6 +659,7 @@ ReabstractionInfo::ReabstractionInfo(
659659 bool ConvertIndirectToDirect, bool dropMetatypeArgs, OptRemark::Emitter *ORE)
660660 : ConvertIndirectToDirect(ConvertIndirectToDirect),
661661 dropMetatypeArgs(dropMetatypeArgs),
662+ M(&Callee->getModule ()),
662663 TargetModule(targetModule), isWholeModule(isWholeModule),
663664 Serialized(Serialized) {
664665 if (!prepareAndCheck (Apply, Callee, ParamSubs, ORE))
@@ -772,8 +773,6 @@ bool ReabstractionInfo::isPartialSpecialization() const {
772773}
773774
774775void ReabstractionInfo::createSubstitutedAndSpecializedTypes () {
775- auto &M = Callee->getModule ();
776-
777776 // Find out how the function type looks like after applying the provided
778777 // substitutions.
779778 if (!SubstitutedType) {
@@ -792,7 +791,7 @@ void ReabstractionInfo::createSubstitutedAndSpecializedTypes() {
792791 TrivialArgs.resize (NumArgs);
793792 droppedMetatypeArgs.resize (NumArgs);
794793
795- SILFunctionConventions substConv (SubstitutedType, M );
794+ SILFunctionConventions substConv (SubstitutedType, getModule () );
796795 TypeExpansionContext resilienceExp = getResilienceExpansion ();
797796 TypeExpansionContext minimalExp (ResilienceExpansion::Minimal,
798797 TargetModule, isWholeModule);
@@ -863,26 +862,25 @@ void ReabstractionInfo::createSubstitutedAndSpecializedTypes() {
863862 // Produce a specialized type, which is the substituted type with
864863 // the parameters/results passing conventions adjusted according
865864 // to the conversions selected above.
866- SpecializedType = createSpecializedType (SubstitutedType, M );
865+ SpecializedType = createSpecializedType (SubstitutedType, getModule () );
867866}
868867
869868ReabstractionInfo::TypeCategory ReabstractionInfo::
870869getReturnTypeCategory (const SILResultInfo &RI,
871870 const SILFunctionConventions &substConv,
872871 TypeExpansionContext typeExpansion) {
873- auto &M = Callee->getModule ();
874872 auto ResultTy = substConv.getSILType (RI, typeExpansion);
875873 ResultTy = Callee->mapTypeIntoContext (ResultTy);
876- auto &TL = M .Types .getTypeLowering (ResultTy, typeExpansion);
874+ auto &TL = getModule () .Types .getTypeLowering (ResultTy, typeExpansion);
877875
878876 if (!TL.isLoadable ())
879877 return NotLoadable;
880878
881- if (RI.getReturnValueType (M , SubstitutedType, typeExpansion)
879+ if (RI.getReturnValueType (getModule () , SubstitutedType, typeExpansion)
882880 ->isVoid ())
883881 return NotLoadable;
884882
885- if (!shouldExpand (M , ResultTy))
883+ if (!shouldExpand (getModule () , ResultTy))
886884 return NotLoadable;
887885
888886 return TL.isTrivial () ? LoadableAndTrivial : Loadable;
@@ -892,10 +890,9 @@ ReabstractionInfo::TypeCategory ReabstractionInfo::
892890getParamTypeCategory (const SILParameterInfo &PI,
893891 const SILFunctionConventions &substConv,
894892 TypeExpansionContext typeExpansion) {
895- auto &M = Callee->getModule ();
896893 auto ParamTy = substConv.getSILType (PI, typeExpansion);
897- ParamTy = Callee-> mapTypeIntoContext (ParamTy);
898- auto &TL = M .Types .getTypeLowering (ParamTy, typeExpansion);
894+ ParamTy = mapTypeIntoContext (ParamTy);
895+ auto &TL = getModule () .Types .getTypeLowering (ParamTy, typeExpansion);
899896
900897 if (!TL.isLoadable ())
901898 return NotLoadable;
@@ -908,7 +905,6 @@ CanSILFunctionType
908905ReabstractionInfo::createSubstitutedType (SILFunction *OrigF,
909906 SubstitutionMap SubstMap,
910907 bool HasUnboundGenericParams) {
911- auto &M = OrigF->getModule ();
912908 if ((SpecializedGenericSig &&
913909 SpecializedGenericSig->areAllParamsConcrete ()) ||
914910 !HasUnboundGenericParams) {
@@ -920,8 +916,8 @@ ReabstractionInfo::createSubstitutedType(SILFunction *OrigF,
920916
921917 auto lowered = OrigF->getLoweredFunctionType ();
922918 auto genSub =
923- lowered->substGenericArgs (M , SubstMap, getResilienceExpansion ());
924- auto unsub = genSub->getUnsubstitutedType (M );
919+ lowered->substGenericArgs (getModule () , SubstMap, getResilienceExpansion ());
920+ auto unsub = genSub->getUnsubstitutedType (getModule () );
925921 auto specialized = CanSpecializedGenericSig.getReducedType (unsub);
926922
927923 // First substitute concrete types into the existing function type.
@@ -935,7 +931,7 @@ ReabstractionInfo::createSubstitutedType(SILFunction *OrigF,
935931 CanSpecializedGenericSig, FnTy->getExtInfo (), FnTy->getCoroutineKind (),
936932 FnTy->getCalleeConvention (), FnTy->getParameters (), FnTy->getYields (),
937933 FnTy->getResults (), FnTy->getOptionalErrorResult (),
938- FnTy->getPatternSubstitutions (), SubstitutionMap (), M .getASTContext (),
934+ FnTy->getPatternSubstitutions (), SubstitutionMap (), getModule () .getASTContext (),
939935 FnTy->getWitnessMethodConformanceOrInvalid ());
940936
941937 // This is an interface type. It should not have any archetypes.
@@ -972,6 +968,16 @@ CanSILFunctionType ReabstractionInfo::createThunkType(PartialApplyInst *forPAI)
972968 return newFnTy;
973969}
974970
971+ SILType ReabstractionInfo::mapTypeIntoContext (SILType type) const {
972+ if (Callee) {
973+ return Callee->mapTypeIntoContext (type);
974+ }
975+ assert (!methodDecl.isNull ());
976+ if (auto *genericEnv = M->Types .getConstantGenericEnvironment (methodDecl))
977+ return genericEnv->mapTypeIntoContext (getModule (), type);
978+ return type;
979+ }
980+
975981// / Convert the substituted function type into a specialized function type based
976982// / on the ReabstractionInfo.
977983CanSILFunctionType ReabstractionInfo::
@@ -1081,15 +1087,13 @@ void ReabstractionInfo::performFullSpecializationPreparation(
10811087 assert ((!EnablePartialSpecialization || !HasUnboundGenericParams) &&
10821088 " Only full specializations are handled here" );
10831089
1084- SILModule &M = Callee->getModule ();
1085-
10861090 this ->Callee = Callee;
10871091
10881092 // Get the original substitution map.
10891093 ClonerParamSubMap = ParamSubs;
10901094
10911095 SubstitutedType = Callee->getLoweredFunctionType ()->substGenericArgs (
1092- M , ClonerParamSubMap, getResilienceExpansion ());
1096+ getModule () , ClonerParamSubMap, getResilienceExpansion ());
10931097 CallerParamSubMap = {};
10941098 createSubstitutedAndSpecializedTypes ();
10951099}
@@ -1889,8 +1893,6 @@ void FunctionSignaturePartialSpecializer::createSpecializedGenericSignature(
18891893void ReabstractionInfo::performPartialSpecializationPreparation (
18901894 SILFunction *Caller, SILFunction *Callee,
18911895 SubstitutionMap ParamSubs) {
1892- SILModule &M = Callee->getModule ();
1893-
18941896 // Caller is the SILFunction containing the apply instruction.
18951897 CanGenericSignature CallerGenericSig;
18961898 GenericEnvironment *CallerGenericEnv = nullptr ;
@@ -1910,7 +1912,7 @@ void ReabstractionInfo::performPartialSpecializationPreparation(
19101912 llvm::dbgs () << " Callee generic signature is:\n " ;
19111913 CalleeGenericSig->print (llvm::dbgs ()));
19121914
1913- FunctionSignaturePartialSpecializer FSPS (M ,
1915+ FunctionSignaturePartialSpecializer FSPS (getModule () ,
19141916 CallerGenericSig, CallerGenericEnv,
19151917 CalleeGenericSig, CalleeGenericEnv,
19161918 ParamSubs);
@@ -1968,7 +1970,7 @@ ReabstractionInfo::ReabstractionInfo(ModuleDecl *targetModule,
19681970 bool isWholeModule, SILFunction *Callee,
19691971 GenericSignature SpecializedSig,
19701972 bool isPrespecialization)
1971- : TargetModule(targetModule), isWholeModule(isWholeModule),
1973+ : M(&Callee-> getModule ()), TargetModule(targetModule), isWholeModule(isWholeModule),
19721974 isPrespecialization(isPrespecialization) {
19731975 Serialized =
19741976 this ->isPrespecialization ? IsNotSerialized : Callee->isSerialized ();
@@ -1979,13 +1981,11 @@ ReabstractionInfo::ReabstractionInfo(ModuleDecl *targetModule,
19791981 this ->Callee = Callee;
19801982 ConvertIndirectToDirect = true ;
19811983
1982- SILModule &M = Callee->getModule ();
1983-
19841984 auto CalleeGenericSig =
19851985 Callee->getLoweredFunctionType ()->getInvocationGenericSignature ();
19861986 auto *CalleeGenericEnv = Callee->getGenericEnvironment ();
19871987
1988- FunctionSignaturePartialSpecializer FSPS (M ,
1988+ FunctionSignaturePartialSpecializer FSPS (getModule () ,
19891989 CalleeGenericSig, CalleeGenericEnv,
19901990 SpecializedSig);
19911991
@@ -3091,7 +3091,7 @@ void swift::trySpecializeApplyOfGeneric(
30913091
30923092 // Check if there is a pre-specialization available in a library.
30933093 SpecializedFunction prespecializedF{};
3094- ReabstractionInfo prespecializedReInfo;
3094+ ReabstractionInfo prespecializedReInfo (FuncBuilder. getModule ()) ;
30953095 bool replacePartialApplyWithoutReabstraction = false ;
30963096
30973097 if (usePrespecialized (FuncBuilder, Apply, RefF, ReInfo, prespecializedReInfo,
0 commit comments