Skip to content

Commit ddb44a6

Browse files
committed
[llvm-profdata] Don't treat non-fatal merge errors as fatal
This fixes an issue seen on the coverage bot: http://lab.llvm.org:8080/green/view/Experimental/job/clang-stage2-coverage-R/1930 Profile merging shouldn't fail if a single counter mismatch is detected. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@318555 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 4f5d63b commit ddb44a6

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

test/tools/llvm-profdata/Inputs/counter-mismatch-1.proftext

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ foo
22
1024
33
1
44
0
5+
6+
foo
7+
1024
8+
5
9+
0
10+
0
11+
0
12+
0
13+
0

test/tools/llvm-profdata/threaded-count-mismatch.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Test multithreaded error reporting.
22

3-
RUN: not llvm-profdata merge -j 4 -o %t.profdata \
3+
RUN: llvm-profdata merge -j 4 -o %t.profdata \
44
RUN: %S/Inputs/counter-mismatch-1.proftext \
55
RUN: %S/Inputs/counter-mismatch-2.proftext \
66
RUN: %S/Inputs/counter-mismatch-3.proftext \

tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ using namespace llvm;
3737

3838
enum ProfileFormat { PF_None = 0, PF_Text, PF_Binary, PF_GCC };
3939

40-
static void exitWithError(Twine Message, std::string Whence = "",
41-
std::string Hint = "") {
42-
errs() << "error: ";
40+
static void warn(StringRef Prefix, Twine Message, std::string Whence = "",
41+
std::string Hint = "") {
42+
errs() << Prefix;
4343
if (!Whence.empty())
4444
errs() << Whence << ": ";
4545
errs() << Message << "\n";
4646
if (!Hint.empty())
4747
errs() << Hint << "\n";
48+
}
49+
50+
static void exitWithError(Twine Message, std::string Whence = "",
51+
std::string Hint = "") {
52+
warn("error: ", Message, Whence, Hint);
4853
::exit(1);
4954
}
5055

@@ -129,6 +134,22 @@ struct WriterContext {
129134
ErrLock(ErrLock), WriterErrorCodes(WriterErrorCodes) {}
130135
};
131136

137+
/// Determine whether an error is fatal for profile merging.
138+
static bool isFatalError(instrprof_error IPE) {
139+
switch (IPE) {
140+
default:
141+
return true;
142+
case instrprof_error::success:
143+
case instrprof_error::eof:
144+
case instrprof_error::unknown_function:
145+
case instrprof_error::hash_mismatch:
146+
case instrprof_error::count_mismatch:
147+
case instrprof_error::counter_overflow:
148+
case instrprof_error::value_site_count_mismatch:
149+
return false;
150+
}
151+
}
152+
132153
/// Load an input into a writer context.
133154
static void loadInput(const WeightedFile &Input, WriterContext *WC) {
134155
std::unique_lock<std::mutex> CtxGuard{WC->Lock};
@@ -177,8 +198,13 @@ static void loadInput(const WeightedFile &Input, WriterContext *WC) {
177198
FuncName, firstTime);
178199
});
179200
}
180-
if (Reader->hasError())
181-
WC->Err = Reader->getError();
201+
if (Reader->hasError()) {
202+
if (Error E = Reader->getError()) {
203+
instrprof_error IPE = InstrProfError::take(std::move(E));
204+
if (isFatalError(IPE))
205+
WC->Err = make_error<InstrProfError>(IPE);
206+
}
207+
}
182208
}
183209

184210
/// Merge the \p Src writer context into \p Dst.
@@ -262,10 +288,20 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs,
262288
}
263289

264290
// Handle deferred hard errors encountered during merging.
265-
for (std::unique_ptr<WriterContext> &WC : Contexts)
266-
if (WC->Err)
291+
for (std::unique_ptr<WriterContext> &WC : Contexts) {
292+
if (!WC->Err)
293+
continue;
294+
if (!WC->Err.isA<InstrProfError>())
267295
exitWithError(std::move(WC->Err), WC->ErrWhence);
268296

297+
instrprof_error IPE = InstrProfError::take(std::move(WC->Err));
298+
if (isFatalError(IPE))
299+
exitWithError(make_error<InstrProfError>(IPE), WC->ErrWhence);
300+
else
301+
warn("warning: ", toString(make_error<InstrProfError>(IPE)),
302+
WC->ErrWhence);
303+
}
304+
269305
InstrProfWriter &Writer = Contexts[0]->Writer;
270306
if (OutputFormat == PF_Text) {
271307
if (Error E = Writer.writeText(Output))

0 commit comments

Comments
 (0)