22
22
#include " llvm/Support/Signals.h"
23
23
#include " llvm/Support/ThreadPool.h"
24
24
#include < algorithm>
25
+ #include < fstream>
25
26
#include < mutex>
26
27
#include < unordered_map>
27
28
@@ -274,52 +275,36 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
274
275
275
276
if (isYAML (Filename))
276
277
report_error (Filename, " cannot mix YAML and legacy formats" );
277
- ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
278
- MemoryBuffer::getFileOrSTDIN (Filename);
279
- if (std::error_code EC = MB.getError ())
280
- report_error (Filename, EC);
281
278
282
- StringRef Buf = MB.get ()->getBuffer ();
279
+ std::ifstream FdataFile (Filename, std::ios::in);
280
+ std::string FdataLine;
281
+ std::getline (FdataFile, FdataLine);
282
+
283
+ auto checkMode = [&](const std::string &Key, std::optional<bool > &Flag) {
284
+ const bool KeyIsSet = FdataLine.rfind (Key, 0 ) == 0 ;
285
+
286
+ if (!Flag.has_value ())
287
+ Flag = KeyIsSet;
288
+ else if (*Flag != KeyIsSet)
289
+ report_error (Filename, " cannot mix profile with and without " + Key);
290
+ if (KeyIsSet)
291
+ // Advance line
292
+ std::getline (FdataFile, FdataLine);
293
+ };
294
+
283
295
ProfileTy *Profile;
284
296
{
285
297
std::lock_guard<std::mutex> Lock (BoltedCollectionMutex);
286
298
// Check if the string "boltedcollection" is in the first line
287
- if (Buf.starts_with (" boltedcollection\n " )) {
288
- if (!BoltedCollection.value_or (true ))
289
- report_error (
290
- Filename,
291
- " cannot mix profile collected in BOLT and non-BOLT deployments" );
292
- BoltedCollection = true ;
293
- Buf = Buf.drop_front (17 );
294
- } else {
295
- if (BoltedCollection.value_or (false ))
296
- report_error (
297
- Filename,
298
- " cannot mix profile collected in BOLT and non-BOLT deployments" );
299
- BoltedCollection = false ;
300
- }
299
+ checkMode (" boltedcollection" , BoltedCollection);
301
300
// Check if the string "no_lbr" is in the first line
302
301
// (or second line if BoltedCollection is true)
303
- size_t CheckNoLBRPos = Buf.find (' \n ' );
304
- if (CheckNoLBRPos != StringRef::npos) {
305
- StringRef FirstLine = Buf.substr (0 , CheckNoLBRPos);
306
- if (FirstLine.contains (" no_lbr" )) {
307
- if (!NoLBRCollection.value_or (true ))
308
- report_error (Filename, " cannot mix 'no_lbr' and 'lbr' profiles" );
309
- NoLBRCollection = true ;
310
- Buf = Buf.drop_front (CheckNoLBRPos + 1 );
311
- } else {
312
- if (NoLBRCollection.value_or (false ))
313
- report_error (Filename, " cannot mix 'no_lbr' and 'lbr' profiles" );
314
- NoLBRCollection = false ;
315
- }
316
- }
302
+ checkMode (" no_lbr" , NoLBRCollection);
317
303
Profile = &Profiles[tid];
318
304
}
319
305
320
- SmallVector<StringRef> Lines;
321
- SplitString (Buf, Lines, " \n " );
322
- for (StringRef Line : Lines) {
306
+ do {
307
+ StringRef Line (FdataLine);
323
308
size_t Pos = Line.rfind (" " );
324
309
if (Pos == StringRef::npos)
325
310
report_error (Filename, " Malformed / corrupted profile" );
@@ -329,7 +314,7 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
329
314
report_error (Filename, " Malformed / corrupted profile counter" );
330
315
Count += Profile->lookup (Signature);
331
316
Profile->insert_or_assign (Signature, Count);
332
- }
317
+ } while ( std::getline (FdataFile, FdataLine));
333
318
};
334
319
335
320
// The final reduction has non-trivial cost, make sure each thread has at
0 commit comments