@@ -53,20 +53,69 @@ class ReabstractionInfo {
53
53
// / out-parameters.
54
54
unsigned NumFormalIndirectResults;
55
55
56
- // / The function type after applying the substitutions of the original
57
- // / apply site .
56
+ // / The function type after applying the substitutions used to call the
57
+ // / specialized function .
58
58
CanSILFunctionType SubstitutedType;
59
59
60
60
// / The function type after applying the re-abstractions on the
61
61
// / SubstitutedType.
62
62
CanSILFunctionType SpecializedType;
63
63
64
+ // / The generic environment to be used by the specialization.
65
+ GenericEnvironment *SpecializedGenericEnv;
66
+
67
+ // / The generic signature of the specialization.
68
+ // / It is nullptr if the specialization is not polymorphic.
69
+ GenericSignature *SpecializedGenericSig;
70
+
71
+ // Set of the substitutions used by the caller's apply instruction before
72
+ // any transformations performed by the generic specializer.
73
+ // It uses archetypes.
74
+ SubstitutionList OriginalParamSubs;
75
+
76
+ // Set of substitutions to be used by the caller's apply when it calls a
77
+ // specialized function.
78
+ // It uses archetypes.
79
+ SubstitutionList CallerParamSubs;
80
+
81
+ // Set of substitutions to be used by the cloner during cloning.
82
+ // It maps to concrete types for any types which were replaced by
83
+ // concrete types in the caller's apply substitution list. All other
84
+ // types are replaced by their respective archetypes.
85
+ SubstitutionList ClonerParamSubs;
86
+
87
+ // Reference to the original generic non-specialized function.
88
+ SILFunction *OriginalF;
89
+
90
+ // The apply site which invokes the generic function.
91
+ ApplySite Apply;
92
+
93
+ // Set if a specialized function has unbound generic parameters.
94
+ bool HasUnboundGenericParams;
95
+
96
+ // Substitutions to be used for creating a new function type
97
+ // for the specialized function.
98
+ // It uses interface types.
99
+ SubstitutionMap CallerInterfaceSubs;
100
+
101
+ // Create a new substituted type with the updated signature.
102
+ CanSILFunctionType createSubstitutedType (SILFunction *OrigF,
103
+ const SubstitutionMap &SubstMap,
104
+ bool HasUnboundGenericParams);
105
+
106
+ void createSubstitutedAndSpecializedTypes ();
64
107
public:
65
108
// / Constructs the ReabstractionInfo for generic function \p Orig with
66
109
// / substitutions \p ParamSubs.
67
110
// / If specialization is not possible getSpecializedType() will return an
68
111
// / invalid type.
69
- ReabstractionInfo (SILFunction *Orig, SubstitutionList ParamSubs);
112
+ ReabstractionInfo (ApplySite Apply, SILFunction *Callee,
113
+ SubstitutionList ParamSubs);
114
+
115
+ // / Constructs the ReabstractionInfo for generic function \p Orig with
116
+ // / additional requirements. Requirements may contain new layout,
117
+ // / conformances or same concrete type requirements.
118
+ ReabstractionInfo (SILFunction *Orig, ArrayRef<Requirement> Requirements);
70
119
71
120
// / Returns true if the \p ParamIdx'th (non-result) formal parameter is
72
121
// / converted from indirect to direct.
@@ -81,6 +130,16 @@ class ReabstractionInfo {
81
130
return Conversions.test (ResultIdx);
82
131
}
83
132
133
+ // / Gets the total number of original function arguments.
134
+ unsigned getNumArguments () const { return Conversions.size (); }
135
+
136
+ // / Returns true if the \p ArgIdx'th argument is converted from an
137
+ // / indirect
138
+ // / result or parameter to a direct result or parameter.
139
+ bool isArgConverted (unsigned ArgIdx) const {
140
+ return Conversions.test (ArgIdx);
141
+ }
142
+
84
143
// / Returns true if there are any conversions from indirect to direct values.
85
144
bool hasConversions () const { return Conversions.any (); }
86
145
@@ -92,6 +151,15 @@ class ReabstractionInfo {
92
151
Conversions.resize (Conversions.size () - numPartialApplyArgs);
93
152
}
94
153
154
+ // / Returns the index of the first argument of an apply site, which may be
155
+ // / > 0 in case of a partial_apply.
156
+ unsigned getIndexOfFirstArg (ApplySite Apply) const {
157
+ unsigned numArgs = Apply.getNumArguments ();
158
+ assert (numArgs == Conversions.size () ||
159
+ (numArgs < Conversions.size () && isa<PartialApplyInst>(Apply)));
160
+ return Conversions.size () - numArgs;
161
+ }
162
+
95
163
// / Get the function type after applying the substitutions to the original
96
164
// / generic function.
97
165
CanSILFunctionType getSubstitutedType () const { return SubstitutedType; }
@@ -101,10 +169,45 @@ class ReabstractionInfo {
101
169
// / possible.
102
170
CanSILFunctionType getSpecializedType () const { return SpecializedType; }
103
171
172
+ GenericEnvironment *getSpecializedGenericEnvironment () const {
173
+ return SpecializedGenericEnv;
174
+ }
175
+
176
+ SubstitutionList getCallerParamSubstitutions () const {
177
+ return CallerParamSubs;
178
+ }
179
+
180
+ SubstitutionList getClonerParamSubstitutions () const {
181
+ return ClonerParamSubs;
182
+ }
183
+
184
+ SubstitutionList getOriginalParamSubstitutions () const {
185
+ return OriginalParamSubs;
186
+ }
187
+
104
188
// / Create a specialized function type for a specific substituted type \p
105
189
// / SubstFTy by applying the re-abstractions.
106
190
CanSILFunctionType createSpecializedType (CanSILFunctionType SubstFTy,
107
191
SILModule &M) const ;
192
+
193
+ SILFunction *getNonSpecializedFunction () const { return OriginalF; }
194
+
195
+ // / Map type into a context of the specialized function.
196
+ Type mapTypeIntoContext (Type type) const ;
197
+
198
+ // / Map SIL type into a context of the specialized function.
199
+ SILType mapTypeIntoContext (SILType type) const ;
200
+
201
+ SILModule &getModule () const { return OriginalF->getModule (); }
202
+
203
+ // / Returns true if generic specialization is possible.
204
+ bool canBeSpecialized () const ;
205
+
206
+ // / Returns true if it is a full generic specialization.
207
+ bool isFullSpecialization () const ;
208
+
209
+ // / Returns true if it is a partial generic specialization.
210
+ bool isPartialSpecialization () const ;
108
211
};
109
212
110
213
// / Helper class for specializing a generic function given a list of
@@ -118,6 +221,7 @@ class GenericFuncSpecializer {
118
221
119
222
SubstitutionMap ContextSubs;
120
223
std::string ClonedName;
224
+
121
225
public:
122
226
GenericFuncSpecializer (SILFunction *GenericFunc,
123
227
SubstitutionList ParamSubs,
@@ -129,7 +233,7 @@ class GenericFuncSpecializer {
129
233
130
234
// / Return a newly created specialized function.
131
235
SILFunction *tryCreateSpecialization ();
132
-
236
+
133
237
// / Try to specialize GenericFunc given a list of ParamSubs.
134
238
// / Returns either a new or existing specialized function, or nullptr.
135
239
SILFunction *trySpecialization () {
@@ -142,6 +246,10 @@ class GenericFuncSpecializer {
142
246
143
247
return SpecializedF;
144
248
}
249
+
250
+ StringRef getClonedName () {
251
+ return ClonedName;
252
+ }
145
253
};
146
254
147
255
// =============================================================================
0 commit comments