@@ -2063,7 +2063,8 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2063
2063
2064
2064
// Recursively collect transitive captures from captured local functions.
2065
2065
llvm::DenseSet<AnyFunctionRef> visitedFunctions;
2066
- llvm::SetVector<CapturedValue> captures;
2066
+ llvm::DenseSet<ValueDecl*> capturedValues;
2067
+ llvm::SmallVector<CapturedValue, 4 > captures;
2067
2068
2068
2069
// If there is a capture of 'self' with dynamic 'Self' type, it goes last so
2069
2070
// that IRGen can pass dynamic 'Self' metadata.
@@ -2144,10 +2145,22 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2144
2145
}
2145
2146
}
2146
2147
}
2147
-
2148
2148
capture_value:
2149
2149
// Collect non-function captures.
2150
- captures.insert (capture);
2150
+ ValueDecl *value = capture.getDecl ();
2151
+ if (capturedValues.count (value)) {
2152
+ for (auto it = captures.begin (); it != captures.end (); ++it) {
2153
+ if (it->getDecl () == value) {
2154
+ // The value is already in the list, it needs to have the logical
2155
+ // AND of each flag of all uses.
2156
+ *it = CapturedValue (value, it->getFlags () & capture.getFlags ());
2157
+ break ;
2158
+ }
2159
+ }
2160
+ } else {
2161
+ capturedValues.insert (value);
2162
+ captures.push_back (capture);
2163
+ }
2151
2164
}
2152
2165
};
2153
2166
collectFunctionCaptures (fn);
@@ -2156,10 +2169,10 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2156
2169
// add it as the final capture. Otherwise, add a fake hidden capture for
2157
2170
// the dynamic 'Self' metatype.
2158
2171
if (selfCapture.hasValue ()) {
2159
- captures.insert (*selfCapture);
2172
+ captures.push_back (*selfCapture);
2160
2173
} else if (capturesDynamicSelf) {
2161
2174
selfCapture = CapturedValue::getDynamicSelfMetadata ();
2162
- captures.insert (*selfCapture);
2175
+ captures.push_back (*selfCapture);
2163
2176
}
2164
2177
2165
2178
// Cache the uniqued set of transitive captures.
0 commit comments