Skip to content

Commit 4fed39d

Browse files
committed
[ThinLTO] Fix nondeterministic exit on error.
In the multi-threaded case, if a thread hits an error, we mimick LLVMContext's behavior of reporting the error and exit-ing. However, this doesn't cleanly join the other threads, so depending on how fast the process exits, other threads may report 'terminate called without an active exception'. To avoid this non-determinsim, and without introducing a more complicated design, we just report the error, but not exit early. We do track whether we hit errors and exit(1) after joining. Differential Revision: https://reviews.llvm.org/D115574
1 parent fe1b5b5 commit 4fed39d

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

llvm/tools/llvm-lto2/llvm-lto2.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/Support/PluginLoader.h"
3030
#include "llvm/Support/TargetSelect.h"
3131
#include "llvm/Support/Threading.h"
32+
#include <atomic>
3233

3334
using namespace llvm;
3435
using namespace lto;
@@ -236,13 +237,6 @@ static int run(int argc, char **argv) {
236237
std::vector<std::unique_ptr<MemoryBuffer>> MBs;
237238

238239
Config Conf;
239-
Conf.DiagHandler = [](const DiagnosticInfo &DI) {
240-
DiagnosticPrinterRawOStream DP(errs());
241-
DI.print(DP);
242-
errs() << '\n';
243-
if (DI.getSeverity() == DS_Error)
244-
exit(1);
245-
};
246240

247241
Conf.CPU = codegen::getMCPU();
248242
Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple());
@@ -314,9 +308,25 @@ static int run(int argc, char **argv) {
314308
else
315309
Backend = createInProcessThinBackend(
316310
llvm::heavyweight_hardware_concurrency(Threads));
311+
// Track whether we hit an error; in particular, in the multi-threaded case,
312+
// we can't exit() early because the rest of the threads wouldn't have had a
313+
// change to be join-ed, and that would result in a "terminate called without
314+
// an active exception". Altogether, this results in nondeterministic
315+
// behavior. Instead, we don't exit in the multi-threaded case, but we make
316+
// sure to report the error and then at the end (after joining cleanly)
317+
// exit(1).
318+
std::atomic<bool> HasErrors;
319+
std::atomic_init(&HasErrors, false);
320+
Conf.DiagHandler = [&](const DiagnosticInfo &DI) {
321+
DiagnosticPrinterRawOStream DP(errs());
322+
DI.print(DP);
323+
errs() << '\n';
324+
if (DI.getSeverity() == DS_Error)
325+
HasErrors = true;
326+
};
327+
317328
LTO Lto(std::move(Conf), std::move(Backend));
318329

319-
bool HasErrors = false;
320330
for (std::string F : InputFilenames) {
321331
std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
322332
std::unique_ptr<InputFile> Input =
@@ -381,7 +391,7 @@ static int run(int argc, char **argv) {
381391
"failed to create cache");
382392

383393
check(Lto.run(AddStream, Cache), "LTO::run failed");
384-
return 0;
394+
return static_cast<int>(HasErrors);
385395
}
386396

387397
static int dumpSymtab(int argc, char **argv) {

0 commit comments

Comments
 (0)