22
22
#include " swift/SIL/SILFunction.h"
23
23
#include " swift/SIL/SILInstruction.h"
24
24
#include " swift/SILOptimizer/Utils/Local.h"
25
- #include " llvm/ADT/BitVector .h"
25
+ #include " llvm/ADT/SmallBitVector .h"
26
26
#include " llvm/Support/CommandLine.h"
27
27
#include " llvm/Support/Debug.h"
28
28
29
29
namespace swift {
30
30
31
+ // / Tries to specialize an \p Apply of a generic function. It can be a full
32
+ // / apply site or a partial apply.
33
+ // / Replaced and now dead instructions are returned in \p DeadApplies.
34
+ // / New created functions, like the specialized callee and thunks, are returned
35
+ // / in \p NewFunctions.
36
+ // /
37
+ // / This is the top-level entry point for specializing an existing call site.
38
+ void trySpecializeApplyOfGeneric (
39
+ ApplySite Apply,
40
+ llvm::SmallVectorImpl<SILInstruction *> &DeadApplies,
41
+ llvm::SmallVectorImpl<SILFunction *> &NewFunctions);
42
+
31
43
// / Helper class to describe re-abstraction of function parameters done during
32
44
// / specialization.
33
45
// /
34
46
// / Specifically, it contains information which parameters and returns are
35
47
// / changed from indirect values to direct values.
36
48
class ReabstractionInfo {
49
+ // / A 1-bit means that this parameter/return value is converted from indirect
50
+ // / to direct.
51
+ llvm::SmallBitVector Conversions;
52
+
53
+ // / The first NumResults bits in Conversions refer to indirect out-parameters.
54
+ unsigned NumResults;
55
+
56
+ // / The function type after applying the substitutions of the original
57
+ // / apply site.
58
+ CanSILFunctionType SubstitutedType;
59
+
60
+ // / The function type after applying the re-abstractions on the
61
+ // / SubstitutedType.
62
+ CanSILFunctionType SpecializedType;
63
+
37
64
public:
38
- // / Constructs the ReabstractionInfo for an apply site \p AI calling the
39
- // / generic function \p Orig .
65
+ // / Constructs the ReabstractionInfo for generic function \p Orig with
66
+ // / substitutions \p ParamSubs .
40
67
// / If specialization is not possible getSpecializedType() will return an
41
68
// / invalid type.
42
- ReabstractionInfo (SILFunction *Orig, ApplySite AI );
69
+ ReabstractionInfo (SILFunction *Orig, ArrayRef<Substitution> ParamSubs );
43
70
44
71
// / Does the \p ArgIdx refer to an indirect out-parameter?
45
72
bool isResultIndex (unsigned ArgIdx) const {
@@ -88,8 +115,8 @@ class ReabstractionInfo {
88
115
return Conversions.size () - numArgs;
89
116
}
90
117
91
- // / Get the function type after applying the substitutions of the original
92
- // / apply site .
118
+ // / Get the function type after applying the substitutions to the original
119
+ // / generic function .
93
120
CanSILFunctionType getSubstitutedType () const { return SubstitutedType; }
94
121
95
122
// / Get the function type after applying the re-abstractions on the
@@ -101,41 +128,61 @@ class ReabstractionInfo {
101
128
// / SubstFTy by applying the re-abstractions.
102
129
CanSILFunctionType createSpecializedType (CanSILFunctionType SubstFTy,
103
130
SILModule &M) const ;
104
- private:
105
- // / A 1-bit means that this parameter/return value is converted from indirect
106
- // / to direct.
107
- llvm::BitVector Conversions;
108
-
109
- // / The first NumResults bits in Conversions refer to indirect out-parameters.
110
- unsigned NumResults;
131
+ };
111
132
112
- // / The function type after applying the substitutions of the original
113
- // / apply site.
114
- CanSILFunctionType SubstitutedType;
133
+ // / Helper class for specializing a generic function given a list of
134
+ // / substitutions.
135
+ class GenericFuncSpecializer {
136
+ SILModule &M;
137
+ SILFunction *GenericFunc;
138
+ ArrayRef<Substitution> ParamSubs;
139
+ const ReabstractionInfo &ReInfo;
115
140
116
- // / The function type after applying the re-abstractions on the
117
- // / SubstitutedType.
118
- CanSILFunctionType SpecializedType;
141
+ TypeSubstitutionMap ContextSubs;
142
+ std::string ClonedName;
143
+ public:
144
+ GenericFuncSpecializer (SILFunction *GenericFunc,
145
+ ArrayRef<Substitution> ParamSubs,
146
+ const ReabstractionInfo &ReInfo);
147
+
148
+ // / If we already have this specialization, reuse it.
149
+ SILFunction *lookupSpecialization ();
150
+
151
+ // / Return a newly created specialized function.
152
+ SILFunction *tryCreateSpecialization ();
153
+
154
+ // / Try to specialize GenericFunc given a list of ParamSubs.
155
+ // / Returns either a new or existing specialized function, or nullptr.
156
+ SILFunction *trySpecialization () {
157
+ if (!ReInfo.getSpecializedType ())
158
+ return nullptr ;
159
+
160
+ SILFunction *SpecializedF = lookupSpecialization ();
161
+ if (!SpecializedF)
162
+ SpecializedF = tryCreateSpecialization ();
163
+
164
+ return SpecializedF;
165
+ }
119
166
};
120
167
121
- // / Tries to specialize an \p Apply of a generic function. It can be a full
122
- // / apply site or a partial apply.
123
- // / Replaced and now dead instructions are returned in \p DeadApplies.
124
- // / New created functions, like the specialized callee and thunks, are returned
125
- // / in \p NewFunctions.
126
- void trySpecializeApplyOfGeneric (ApplySite Apply,
127
- llvm::SmallVectorImpl<SILInstruction *> &DeadApplies,
128
- llvm::SmallVectorImpl<SILFunction *> &NewFunctions);
168
+ // =============================================================================
169
+ // Prespecialized symbol lookup.
170
+ // =============================================================================
129
171
130
- // / Checks if a given mangled name could be a name of a whitelisted specialization.
172
+ // / Checks if a given mangled name could be a name of a whitelisted
173
+ // / specialization.
131
174
bool isWhitelistedSpecialization (StringRef SpecName);
132
175
133
176
// / Create a new apply based on an old one, but with a different
134
177
// / function being applied.
135
178
ApplySite replaceWithSpecializedFunction (ApplySite AI, SILFunction *NewF,
136
179
const ReabstractionInfo &ReInfo);
137
180
138
- SILFunction *getExistingSpecialization (SILModule &M, StringRef FunctionName);
181
+ // / Returns a SILFunction for the symbol specified by FunctioName if it is
182
+ // / visible to the current SILModule. This is used to link call sites to
183
+ // / externally defined specialization and should only be used when the function
184
+ // / body is not required for further optimization or inlining (-Onone).
185
+ SILFunction *lookupPrespecializedSymbol (SILModule &M, StringRef FunctionName);
139
186
140
187
} // end namespace swift
141
188
0 commit comments