Skip to content

Commit 66f78b7

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent 1345ee4 commit 66f78b7

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

bolt/test/merge-fdata-mixed-bat-no-lbr.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# RUN: split-file %s %t
66
# RUN: not merge-fdata %t/a.fdata %t/b.fdata 2>&1 | FileCheck %s
77

8-
# CHECK: cannot mix profile collected in BOLT and non-BOLT deployments
8+
# CHECK: cannot mix profile with and without boltedcollection
99

1010
#--- a.fdata
1111
boltedcollection

bolt/test/merge-fdata-mixed-mode.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# RUN: split-file %s %t
77
# RUN: not merge-fdata %t/a.fdata %t/b.fdata 2>&1 | FileCheck %s
88

9-
# CHECK: cannot mix 'no_lbr' and 'lbr' profiles.
9+
# CHECK: cannot mix profile with and without no_lbr
1010

1111
#--- a.fdata
1212
no_lbr

bolt/tools/merge-fdata/merge-fdata.cpp

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Support/Signals.h"
2323
#include "llvm/Support/ThreadPool.h"
2424
#include <algorithm>
25+
#include <fstream>
2526
#include <mutex>
2627
#include <unordered_map>
2728

@@ -274,52 +275,40 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
274275

275276
if (isYAML(Filename))
276277
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);
281278

282-
StringRef Buf = MB.get()->getBuffer();
279+
std::ifstream FdataFile(Filename, std::ios::in);
280+
std::string FdataLine;
281+
282+
auto checkMode = [&](const std::string &Key, std::optional<bool> &Flag) {
283+
std::string ErrorMsg = "cannot mix profile with and without " + Key;
284+
auto Pos = FdataFile.tellg();
285+
std::getline(FdataFile, FdataLine);
286+
if (FdataLine.rfind(Key, 0) == 0) {
287+
if (!Flag.value_or(true))
288+
report_error(Filename, ErrorMsg);
289+
Flag = true;
290+
} else {
291+
if (Flag.value_or(false))
292+
report_error(Filename, ErrorMsg);
293+
Flag = false;
294+
// Rewind line
295+
FdataFile.seekg(Pos);
296+
}
297+
};
298+
283299
ProfileTy *Profile;
284300
{
285301
std::lock_guard<std::mutex> Lock(BoltedCollectionMutex);
286302
// 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-
}
303+
checkMode("boltedcollection", BoltedCollection);
301304
// Check if the string "no_lbr" is in the first line
302305
// (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-
}
306+
checkMode("no_lbr", NoLBRCollection);
317307
Profile = &Profiles[tid];
318308
}
319309

320-
SmallVector<StringRef> Lines;
321-
SplitString(Buf, Lines, "\n");
322-
for (StringRef Line : Lines) {
310+
while (std::getline(FdataFile, FdataLine)) {
311+
StringRef Line(FdataLine);
323312
size_t Pos = Line.rfind(" ");
324313
if (Pos == StringRef::npos)
325314
report_error(Filename, "Malformed / corrupted profile");

0 commit comments

Comments
 (0)