@@ -68,7 +68,7 @@ class SwiftCASOutputBackend::Implementation {
6868 auto ProducingInput = OutputToInputMap.find (ResolvedPath);
6969 assert (ProducingInput != OutputToInputMap.end () && " Unknown output file" );
7070
71- std::string InputFilename = ProducingInput->second .first . getFileName () ;
71+ unsigned InputIndex = ProducingInput->second .first ;
7272 auto OutputType = ProducingInput->second .second ;
7373
7474 // Uncached output kind.
@@ -77,18 +77,18 @@ class SwiftCASOutputBackend::Implementation {
7777
7878 return std::make_unique<SwiftCASOutputFile>(
7979 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);
8383 });
8484 }
8585
8686 void initBackend (const FrontendInputsAndOutputs &InputsAndOutputs);
8787
88- Error storeImpl (StringRef Path, StringRef Bytes, StringRef CorrespondingInput ,
88+ Error storeImpl (StringRef Path, StringRef Bytes, unsigned InputIndex ,
8989 file_types::ID OutputKind);
9090
91- Error finalizeCacheKeysFor (StringRef Input );
91+ Error finalizeCacheKeysFor (unsigned InputIndex );
9292
9393private:
9494 friend class SwiftCASOutputBackend ;
@@ -98,8 +98,11 @@ class SwiftCASOutputBackend::Implementation {
9898 const FrontendInputsAndOutputs &InputsAndOutputs;
9999 FrontendOptions::ActionType Action;
100100
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;
103106};
104107
105108SwiftCASOutputBackend::SwiftCASOutputBackend (
@@ -127,14 +130,14 @@ file_types::ID SwiftCASOutputBackend::getOutputFileType(StringRef Path) const {
127130}
128131
129132Error SwiftCASOutputBackend::storeImpl (StringRef Path, StringRef Bytes,
130- StringRef CorrespondingInput ,
133+ unsigned InputIndex ,
131134 file_types::ID OutputKind) {
132- return Impl.storeImpl (Path, Bytes, CorrespondingInput , OutputKind);
135+ return Impl.storeImpl (Path, Bytes, InputIndex , OutputKind);
133136}
134137
135- Error SwiftCASOutputBackend::storeCachedDiagnostics (StringRef InputFile ,
138+ Error SwiftCASOutputBackend::storeCachedDiagnostics (unsigned InputIndex ,
136139 StringRef Bytes) {
137- return storeImpl (" <cached-diagnostics>" , Bytes, InputFile ,
140+ return storeImpl (" <cached-diagnostics>" , Bytes, InputIndex ,
138141 file_types::ID::TY_CachedDiagnostics);
139142}
140143
@@ -145,57 +148,63 @@ void SwiftCASOutputBackend::Implementation::initBackend(
145148 // input it actually comes from. Maybe the solution is just not to cache
146149 // any commands write output to `-`.
147150 file_types::ID mainOutputType = InputsAndOutputs.getPrincipalOutputType ();
148- auto addInput = [&](const InputFile &Input) {
151+ auto addInput = [&](const InputFile &Input, unsigned Index ) {
149152 if (!Input.outputFilename ().empty ())
150153 OutputToInputMap.insert (
151- {Input.outputFilename (), {Input , mainOutputType}});
154+ {Input.outputFilename (), {Index , mainOutputType}});
152155 Input.getPrimarySpecificPaths ()
153156 .SupplementaryOutputs .forEachSetOutputAndType (
154157 [&](const std::string &Out, file_types::ID ID) {
155158 if (!file_types::isProducedFromDiagnostics (ID))
156- OutputToInputMap.insert ({Out, {Input , ID}});
159+ OutputToInputMap.insert ({Out, {Index , ID}});
157160 });
158161 };
159- llvm::for_each (InputsAndOutputs.getAllInputs (), addInput);
162+
163+ for (unsigned idx = 0 ; idx < InputsAndOutputs.getAllInputs ().size (); ++idx)
164+ addInput (InputsAndOutputs.getAllInputs ()[idx], idx);
160165
161166 // FIXME: Cached diagnostics is associated with the first output producing
162167 // 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 ());
166175}
167176
168177Error SwiftCASOutputBackend::Implementation::storeImpl (
169- StringRef Path, StringRef Bytes, StringRef CorrespondingInput ,
178+ StringRef Path, StringRef Bytes, unsigned InputIndex ,
170179 file_types::ID OutputKind) {
171180 Optional<ObjectRef> BytesRef;
172181 if (Error E = CAS.storeFromString (None, Bytes).moveInto (BytesRef))
173182 return E;
174183
175184 LLVM_DEBUG (llvm::dbgs () << " DEBUG: producing CAS output of type \' "
176185 << file_types::getTypeName (OutputKind)
177- << " \' for input \' " << CorrespondingInput << " \' : \' "
186+ << " \' for input \' " << InputIndex << " \' : \' "
178187 << CAS.getID (*BytesRef).toString () << " \'\n " ;);
179188
180- OutputRefs[CorrespondingInput ].insert ({OutputKind, *BytesRef});
189+ OutputRefs[InputIndex ].insert ({OutputKind, *BytesRef});
181190
182- return finalizeCacheKeysFor (CorrespondingInput );
191+ return finalizeCacheKeysFor (InputIndex );
183192}
184193
185194Error 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" );
189198
190199 // If not all outputs for the input are emitted, return.
191200 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 ));
194203 }))
195204 return Error::success ();
196205
197206 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) {
199208 OutputsForInput.emplace_back (E.first , E.second );
200209 });
201210 // Sort to a stable ordering for deterministic output cache object.
@@ -237,14 +246,14 @@ Error SwiftCASOutputBackend::Implementation::finalizeCacheKeysFor(
237246 return Err;
238247 }
239248
240- auto CacheKey = createCompileJobCacheKeyForOutput (CAS, BaseKey, Input );
249+ auto CacheKey = createCompileJobCacheKeyForOutput (CAS, BaseKey, InputIndex );
241250 if (!CacheKey)
242251 return CacheKey.takeError ();
243252
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 " ;);
248257
249258 if (auto E = Cache.put (CAS.getID (*CacheKey), CAS.getID (*Result)))
250259 return E;
0 commit comments