@@ -2063,8 +2063,7 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2063
2063
2064
2064
// Recursively collect transitive captures from captured local functions.
2065
2065
llvm::DenseSet<AnyFunctionRef> visitedFunctions;
2066
- llvm::DenseSet<ValueDecl*> capturedValues;
2067
- llvm::SmallVector<CapturedValue, 4 > captures;
2066
+ llvm::MapVector<ValueDecl*,CapturedValue> captures;
2068
2067
2069
2068
// If there is a capture of 'self' with dynamic 'Self' type, it goes last so
2070
2069
// that IRGen can pass dynamic 'Self' metadata.
@@ -2170,29 +2169,29 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2170
2169
capture_value:
2171
2170
// Collect non-function captures.
2172
2171
ValueDecl *value = capture.getDecl ();
2173
- if (capturedValues.count (value)) {
2174
- for (auto it = captures.begin (); it != captures.end (); ++it) {
2175
- if (it->getDecl () == value) {
2176
- *it = it->mergeFlags (capture);
2177
- break ;
2178
- }
2179
- }
2172
+ auto existing = captures.find (value);
2173
+ if (existing != captures.end ()) {
2174
+ existing->second = existing->second .mergeFlags (capture);
2180
2175
} else {
2181
- capturedValues.insert (value);
2182
- captures.push_back (capture);
2176
+ captures.insert (std::pair<ValueDecl *, CapturedValue>(value, capture));
2183
2177
}
2184
2178
}
2185
2179
};
2186
2180
collectFunctionCaptures (fn);
2187
2181
2182
+ SmallVector<CapturedValue, 4 > resultingCaptures;
2183
+ for (auto capturePair : captures) {
2184
+ resultingCaptures.push_back (capturePair.second );
2185
+ }
2186
+
2188
2187
// If we captured the dynamic 'Self' type and we have a 'self' value also,
2189
2188
// add it as the final capture. Otherwise, add a fake hidden capture for
2190
2189
// the dynamic 'Self' metatype.
2191
2190
if (selfCapture.hasValue ()) {
2192
- captures .push_back (*selfCapture);
2191
+ resultingCaptures .push_back (*selfCapture);
2193
2192
} else if (capturesDynamicSelf) {
2194
2193
selfCapture = CapturedValue::getDynamicSelfMetadata ();
2195
- captures .push_back (*selfCapture);
2194
+ resultingCaptures .push_back (*selfCapture);
2196
2195
}
2197
2196
2198
2197
// Cache the uniqued set of transitive captures.
@@ -2201,7 +2200,7 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2201
2200
auto &cachedCaptures = inserted.first ->second ;
2202
2201
cachedCaptures.setGenericParamCaptures (capturesGenericParams);
2203
2202
cachedCaptures.setDynamicSelfType (capturesDynamicSelf);
2204
- cachedCaptures.setCaptures (Context.AllocateCopy (captures ));
2203
+ cachedCaptures.setCaptures (Context.AllocateCopy (resultingCaptures ));
2205
2204
2206
2205
return cachedCaptures;
2207
2206
}
0 commit comments