[PATCH] D115574: [ThinLTO] Fix nondeterministic exit on error.

Mircea Trofin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 10 21:00:55 PST 2021


mtrofin created this revision.
mtrofin added a reviewer: tejohnson.
Herald added subscribers: ormris, steven_wu, mgrang, hiraditya, inglorion.
mtrofin requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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, in the
multi-threaded case. We do track whether we hit errors and exit(1) after
joining.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115574

Files:
  llvm/tools/llvm-lto2/llvm-lto2.cpp


Index: llvm/tools/llvm-lto2/llvm-lto2.cpp
===================================================================
--- llvm/tools/llvm-lto2/llvm-lto2.cpp
+++ llvm/tools/llvm-lto2/llvm-lto2.cpp
@@ -29,6 +29,7 @@
 #include "llvm/Support/PluginLoader.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/Threading.h"
+#include <atomic>
 
 using namespace llvm;
 using namespace lto;
@@ -236,13 +237,6 @@
   std::vector<std::unique_ptr<MemoryBuffer>> MBs;
 
   Config Conf;
-  Conf.DiagHandler = [](const DiagnosticInfo &DI) {
-    DiagnosticPrinterRawOStream DP(errs());
-    DI.print(DP);
-    errs() << '\n';
-    if (DI.getSeverity() == DS_Error)
-      exit(1);
-  };
 
   Conf.CPU = codegen::getMCPU();
   Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple());
@@ -314,9 +308,30 @@
   else
     Backend = createInProcessThinBackend(
         llvm::heavyweight_hardware_concurrency(Threads));
+  const bool SingleThreaded = ThinLTODistributedIndexes;
+  // Track whether we hit an error; in particular, in the multi-threaded case,
+  // we can't exit() early because the rest of the threads wouldn't have had a
+  // change to be join-ed, and that would result in a "terminate called without
+  // an active exception". Altogether, this results in nondeterministic
+  // behavior. Instead, we don't exit in the multi-threaded case, but we make
+  // sure to report the error and then at the end (after joining cleanly)
+  // exit(1).
+  std::atomic<bool> HasErrors;
+  std::atomic_init(&HasErrors, false);
+  Conf.DiagHandler = [&](const DiagnosticInfo &DI) {
+    DiagnosticPrinterRawOStream DP(errs());
+    DI.print(DP);
+    errs() << '\n';
+    if (DI.getSeverity() != DS_Error)
+      return;
+    if (SingleThreaded)
+      exit(1);
+    else
+      HasErrors = true;
+  };
+
   LTO Lto(std::move(Conf), std::move(Backend));
 
-  bool HasErrors = false;
   for (std::string F : InputFilenames) {
     std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
     std::unique_ptr<InputFile> Input =
@@ -381,7 +396,7 @@
                   "failed to create cache");
 
   check(Lto.run(AddStream, Cache), "LTO::run failed");
-  return 0;
+  return static_cast<int>(HasErrors);
 }
 
 static int dumpSymtab(int argc, char **argv) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115574.393660.patch
Type: text/x-patch
Size: 2272 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211211/c4213d26/attachment.bin>


More information about the llvm-commits mailing list