@@ -68,7 +68,7 @@ class SwiftCASOutputBackend::Implementation {
68
68
auto ProducingInput = OutputToInputMap.find (ResolvedPath);
69
69
assert (ProducingInput != OutputToInputMap.end () && " Unknown output file" );
70
70
71
- std::string InputFilename = ProducingInput->second .first . getFileName () ;
71
+ unsigned InputIndex = ProducingInput->second .first ;
72
72
auto OutputType = ProducingInput->second .second ;
73
73
74
74
// Uncached output kind.
@@ -77,18 +77,18 @@ class SwiftCASOutputBackend::Implementation {
77
77
78
78
return std::make_unique<SwiftCASOutputFile>(
79
79
ResolvedPath,
80
- [this , InputFilename , OutputType](StringRef Path,
81
- StringRef Bytes) -> Error {
82
- return storeImpl (Path, Bytes, InputFilename , OutputType);
80
+ [this , InputIndex , OutputType](StringRef Path,
81
+ StringRef Bytes) -> Error {
82
+ return storeImpl (Path, Bytes, InputIndex , OutputType);
83
83
});
84
84
}
85
85
86
86
void initBackend (const FrontendInputsAndOutputs &InputsAndOutputs);
87
87
88
- Error storeImpl (StringRef Path, StringRef Bytes, StringRef CorrespondingInput ,
88
+ Error storeImpl (StringRef Path, StringRef Bytes, unsigned InputIndex ,
89
89
file_types::ID OutputKind);
90
90
91
- Error finalizeCacheKeysFor (StringRef Input );
91
+ Error finalizeCacheKeysFor (unsigned InputIndex );
92
92
93
93
private:
94
94
friend class SwiftCASOutputBackend ;
@@ -98,8 +98,11 @@ class SwiftCASOutputBackend::Implementation {
98
98
const FrontendInputsAndOutputs &InputsAndOutputs;
99
99
FrontendOptions::ActionType Action;
100
100
101
- StringMap<std::pair<const InputFile &, file_types::ID>> OutputToInputMap;
102
- StringMap<DenseMap<file_types::ID, ObjectRef>> OutputRefs;
101
+ // Map from output path to the input index and output kind.
102
+ StringMap<std::pair<unsigned , file_types::ID>> OutputToInputMap;
103
+
104
+ // A vector of output refs where the index is the input index.
105
+ SmallVector<DenseMap<file_types::ID, ObjectRef>> OutputRefs;
103
106
};
104
107
105
108
SwiftCASOutputBackend::SwiftCASOutputBackend (
@@ -127,14 +130,14 @@ file_types::ID SwiftCASOutputBackend::getOutputFileType(StringRef Path) const {
127
130
}
128
131
129
132
Error SwiftCASOutputBackend::storeImpl (StringRef Path, StringRef Bytes,
130
- StringRef CorrespondingInput ,
133
+ unsigned InputIndex ,
131
134
file_types::ID OutputKind) {
132
- return Impl.storeImpl (Path, Bytes, CorrespondingInput , OutputKind);
135
+ return Impl.storeImpl (Path, Bytes, InputIndex , OutputKind);
133
136
}
134
137
135
- Error SwiftCASOutputBackend::storeCachedDiagnostics (StringRef InputFile ,
138
+ Error SwiftCASOutputBackend::storeCachedDiagnostics (unsigned InputIndex ,
136
139
StringRef Bytes) {
137
- return storeImpl (" <cached-diagnostics>" , Bytes, InputFile ,
140
+ return storeImpl (" <cached-diagnostics>" , Bytes, InputIndex ,
138
141
file_types::ID::TY_CachedDiagnostics);
139
142
}
140
143
@@ -145,57 +148,63 @@ void SwiftCASOutputBackend::Implementation::initBackend(
145
148
// input it actually comes from. Maybe the solution is just not to cache
146
149
// any commands write output to `-`.
147
150
file_types::ID mainOutputType = InputsAndOutputs.getPrincipalOutputType ();
148
- auto addInput = [&](const InputFile &Input) {
151
+ auto addInput = [&](const InputFile &Input, unsigned Index ) {
149
152
if (!Input.outputFilename ().empty ())
150
153
OutputToInputMap.insert (
151
- {Input.outputFilename (), {Input , mainOutputType}});
154
+ {Input.outputFilename (), {Index , mainOutputType}});
152
155
Input.getPrimarySpecificPaths ()
153
156
.SupplementaryOutputs .forEachSetOutputAndType (
154
157
[&](const std::string &Out, file_types::ID ID) {
155
158
if (!file_types::isProducedFromDiagnostics (ID))
156
- OutputToInputMap.insert ({Out, {Input , ID}});
159
+ OutputToInputMap.insert ({Out, {Index , ID}});
157
160
});
158
161
};
159
- llvm::for_each (InputsAndOutputs.getAllInputs (), addInput);
162
+
163
+ for (unsigned idx = 0 ; idx < InputsAndOutputs.getAllInputs ().size (); ++idx)
164
+ addInput (InputsAndOutputs.getAllInputs ()[idx], idx);
160
165
161
166
// FIXME: Cached diagnostics is associated with the first output producing
162
167
// input file.
163
- OutputToInputMap.insert ({" <cached-diagnostics>" ,
164
- {InputsAndOutputs.getFirstOutputProducingInput (),
165
- file_types::TY_CachedDiagnostics}});
168
+ OutputToInputMap.insert (
169
+ {" <cached-diagnostics>" ,
170
+ {InputsAndOutputs.getIndexOfFirstOutputProducingInput (),
171
+ file_types::TY_CachedDiagnostics}});
172
+
173
+ // Resize the output refs to hold all inputs.
174
+ OutputRefs.resize (InputsAndOutputs.getAllInputs ().size ());
166
175
}
167
176
168
177
Error SwiftCASOutputBackend::Implementation::storeImpl (
169
- StringRef Path, StringRef Bytes, StringRef CorrespondingInput ,
178
+ StringRef Path, StringRef Bytes, unsigned InputIndex ,
170
179
file_types::ID OutputKind) {
171
180
Optional<ObjectRef> BytesRef;
172
181
if (Error E = CAS.storeFromString (None, Bytes).moveInto (BytesRef))
173
182
return E;
174
183
175
184
LLVM_DEBUG (llvm::dbgs () << " DEBUG: producing CAS output of type \' "
176
185
<< file_types::getTypeName (OutputKind)
177
- << " \' for input \' " << CorrespondingInput << " \' : \' "
186
+ << " \' for input \' " << InputIndex << " \' : \' "
178
187
<< CAS.getID (*BytesRef).toString () << " \'\n " ;);
179
188
180
- OutputRefs[CorrespondingInput ].insert ({OutputKind, *BytesRef});
189
+ OutputRefs[InputIndex ].insert ({OutputKind, *BytesRef});
181
190
182
- return finalizeCacheKeysFor (CorrespondingInput );
191
+ return finalizeCacheKeysFor (InputIndex );
183
192
}
184
193
185
194
Error SwiftCASOutputBackend::Implementation::finalizeCacheKeysFor (
186
- StringRef Input ) {
187
- auto Entry = OutputRefs. find (Input) ;
188
- assert (Entry != OutputRefs. end () && " Unexpected input" );
195
+ unsigned InputIndex ) {
196
+ auto ProducedOutputs = OutputRefs[InputIndex] ;
197
+ assert (!ProducedOutputs. empty () && " Expect outputs for this input" );
189
198
190
199
// If not all outputs for the input are emitted, return.
191
200
if (!llvm::all_of (OutputToInputMap, [&](auto &E) {
192
- return (E.second .first . getFileName () != Input ||
193
- Entry-> second .count (E.second .second ));
201
+ return (E.second .first != InputIndex ||
202
+ ProducedOutputs .count (E.second .second ));
194
203
}))
195
204
return Error::success ();
196
205
197
206
std::vector<std::pair<file_types::ID, ObjectRef>> OutputsForInput;
198
- llvm::for_each (Entry-> second , [&OutputsForInput](auto E) {
207
+ llvm::for_each (ProducedOutputs , [&OutputsForInput](auto E) {
199
208
OutputsForInput.emplace_back (E.first , E.second );
200
209
});
201
210
// Sort to a stable ordering for deterministic output cache object.
@@ -237,14 +246,14 @@ Error SwiftCASOutputBackend::Implementation::finalizeCacheKeysFor(
237
246
return Err;
238
247
}
239
248
240
- auto CacheKey = createCompileJobCacheKeyForOutput (CAS, BaseKey, Input );
249
+ auto CacheKey = createCompileJobCacheKeyForOutput (CAS, BaseKey, InputIndex );
241
250
if (!CacheKey)
242
251
return CacheKey.takeError ();
243
252
244
- LLVM_DEBUG (llvm::dbgs () << " DEBUG: writing cache entry for input \' " << Input
245
- << " \' : \' " << CAS. getID (*CacheKey). toString ()
246
- << " \' => \' " << CAS.getID (*Result ).toString ()
247
- << " \'\n " ;);
253
+ LLVM_DEBUG (llvm::dbgs () << " DEBUG: writing cache entry for input \' "
254
+ << InputIndex << " \' : \' "
255
+ << CAS.getID (*CacheKey ).toString () << " \' => \' "
256
+ << CAS. getID (*Result). toString () << " \'\n " ;);
248
257
249
258
if (auto E = Cache.put (CAS.getID (*CacheKey), CAS.getID (*Result)))
250
259
return E;
0 commit comments