[PATCH] D130577: clang-driver: use llvm_fast_shutdown

Nicolai Hähnle via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 26 07:53:11 PDT 2022


nhaehnle created this revision.
nhaehnle added reviewers: efriedma, lattner, mehdi_amini.
Herald added a project: All.
nhaehnle requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Avoid running almost all global destructors in the common case.

Use C->isForDiagnostics() for the decision of whether to do this, which
is the same condition as used elsewhere for triggering "-disable-free".


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130577

Files:
  clang/tools/driver/driver.cpp


Index: clang/tools/driver/driver.cpp
===================================================================
--- clang/tools/driver/driver.cpp
+++ clang/tools/driver/driver.cpp
@@ -34,6 +34,7 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FastShutdown.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/InitLLVM.h"
@@ -327,16 +328,17 @@
   return 1;
 }
 
-int clang_main(int Argc, char **Argv) {
-  noteBottomOfStack();
-  llvm::InitLLVM X(Argc, Argv);
-  llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
-                        " and include the crash backtrace, preprocessed "
-                        "source, and associated run script.\n");
+// Return (FastShutdown, RetCode).
+//
+// Fast shutdown is implemented in the caller. We want the locals of this
+// function to be cleaned up even when fast shutdown is used, as at least the
+// driver's destructor must run to properly flush and close output streams (at
+// least the compilation database stream).
+static std::pair<bool, int> ExecuteClang(int Argc, char **Argv) {
   SmallVector<const char *, 256> Args(Argv, Argv + Argc);
 
   if (llvm::sys::Process::FixupStandardFileDescriptors())
-    return 1;
+    return {false, 1};
 
   llvm::InitializeAllTargets();
 
@@ -385,7 +387,7 @@
       auto newEnd = std::remove(Args.begin(), Args.end(), nullptr);
       Args.resize(newEnd - Args.begin());
     }
-    return ExecuteCC1Tool(Args);
+    return {false, ExecuteCC1Tool(Args)};
   }
 
   // Handle options that need handling before the real command line parsing in
@@ -494,7 +496,7 @@
     if (!Level) {
       llvm::errs() << "Unknown value for " << A->getSpelling() << ": '"
                    << A->getValue() << "'\n";
-      return 1;
+      return {false, 1};
     }
     ReproLevel = *Level;
   }
@@ -572,5 +574,25 @@
 
   // If we have multiple failing commands, we return the result of the first
   // failing command.
+  bool FastShutdown = !C->isForDiagnostics();
+  return {FastShutdown, Res};
+}
+
+int clang_main(int Argc, char **Argv) {
+  noteBottomOfStack();
+  llvm::InitLLVM X(Argc, Argv);
+  llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
+                        " and include the crash backtrace, preprocessed "
+                        "source, and associated run script.\n");
+
+  bool FastShutdown;
+  int Res;
+  std::tie(FastShutdown, Res) = ExecuteClang(Argc, Argv);
+
+  if (FastShutdown) {
+    llvm::llvm_fast_shutdown();
+    llvm::sys::Process::Exit(Res, /*NoCleanup=*/true);
+  }
+
   return Res;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130577.447697.patch
Type: text/x-patch
Size: 2694 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220726/eb0de886/attachment.bin>


More information about the cfe-commits mailing list