[clang] e1a5bb5 - [ORC][LLJIT] Move enable-debugger-support utility out of LLJITBuilder.

Lang Hames via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 22 16:19:06 PDT 2023


Author: Lang Hames
Date: 2023-09-22T16:18:20-07:00
New Revision: e1a5bb59b91d60c0d87feb78f0e0614589a4c927

URL: https://github.com/llvm/llvm-project/commit/e1a5bb59b91d60c0d87feb78f0e0614589a4c927
DIFF: https://github.com/llvm/llvm-project/commit/e1a5bb59b91d60c0d87feb78f0e0614589a4c927.diff

LOG: [ORC][LLJIT] Move enable-debugger-support utility out of LLJITBuilder.

This change means that debugger support only needs to be linked in if it's
used. The code size of debugger support is expected to increase as we improve
it (e.g. pulling in DWARF parsing), so making it an optional extra is useful
for controlling final binary sizes.

Added: 
    llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupport.h
    llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp

Modified: 
    clang/lib/Interpreter/IncrementalExecutor.cpp
    clang/tools/clang-repl/ClangRepl.cpp
    clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
    clang/unittests/Interpreter/InterpreterTest.cpp
    llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
    llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
    llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
    llvm/tools/lli/lli.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Interpreter/IncrementalExecutor.cpp b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 2c4dfc9a611e021..2692d0618b8649e 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -17,6 +17,7 @@
 #include "clang/Interpreter/PartialTranslationUnit.h"
 #include "llvm/ExecutionEngine/ExecutionEngine.h"
 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
+#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
@@ -46,8 +47,13 @@ IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
   JTMB.addFeatures(TI.getTargetOpts().Features);
   LLJITBuilder Builder;
   Builder.setJITTargetMachineBuilder(JTMB);
-  // Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
-  Builder.setEnableDebuggerSupport(true);
+  Builder.setPrePlatformSetup(
+      [](LLJIT &J) {
+        // Try to enable debugging of JIT'd code (only works with JITLink for
+        // ELF and MachO).
+        consumeError(enableDebuggerSupport(J));
+        return llvm::Error::success();
+      });
 
   if (auto JitOrErr = Builder.create())
     Jit = std::move(*JitOrErr);

diff  --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index 51741fd1a27ef4a..a29a2ebac434ab5 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -152,9 +152,7 @@ int main(int argc, const char **argv) {
   llvm::InitializeAllAsmPrinters();
 
   if (OptHostSupportsJit) {
-    auto J = llvm::orc::LLJITBuilder()
-               .setEnableDebuggerSupport(true)
-               .create();
+    auto J = llvm::orc::LLJITBuilder().create();
     if (J)
       llvm::outs() << "true\n";
     else {

diff  --git a/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp b/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
index 8700f506d9b17d4..70e10b1e53bd9d6 100644
--- a/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
+++ b/clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp
@@ -52,9 +52,7 @@ TEST(InterpreterTest, CatchException) {
   llvm::InitializeNativeTargetAsmPrinter();
 
   {
-    auto J = llvm::orc::LLJITBuilder()
-                 .setEnableDebuggerSupport(true)
-                 .create();
+    auto J = llvm::orc::LLJITBuilder().create();
     if (!J) {
       // The platform does not support JITs.
       // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we

diff  --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 62e5bacbdd0b6d8..5f2911e9a7adad3 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -191,9 +191,7 @@ static std::string MangleName(NamedDecl *ND) {
 }
 
 static bool HostSupportsJit() {
-  auto J = llvm::orc::LLJITBuilder()
-             .setEnableDebuggerSupport(true)
-             .create();
+  auto J = llvm::orc::LLJITBuilder().create();
   if (J)
     return true;
   LLVMConsumeError(llvm::wrap(J.takeError()));

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupport.h b/llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupport.h
new file mode 100644
index 000000000000000..1b47d7d41bb9b5a
--- /dev/null
+++ b/llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupport.h
@@ -0,0 +1,28 @@
+//===-- DebugerSupport.h - Utils for enabling debugger support --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Utilities for enabling debugger support.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_DEBUGGERSUPPORT_H
+#define LLVM_EXECUTIONENGINE_ORC_DEBUGGERSUPPORT_H
+
+#include "llvm/Support/Error.h"
+
+namespace llvm {
+namespace orc {
+
+class LLJIT;
+
+Error enableDebuggerSupport(LLJIT &J);
+
+} // namespace orc
+} // namespace llvm
+
+#endif // LLVM_EXECUTIONENGINE_ORC_DEBUGGERSUPPORT_H

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
index 415ec7228f058de..8ab8ff8a0034bf3 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
@@ -322,7 +322,6 @@ class LLJITBuilderState {
   unique_function<Error(LLJIT &)> PrePlatformSetup;
   PlatformSetupFunction SetUpPlatform;
   unsigned NumCompileThreads = 0;
-  bool EnableDebuggerSupport = false;
 
   /// Called prior to JIT class construcion to fix up defaults.
   Error prepareForConstruction();
@@ -455,12 +454,6 @@ class LLJITBuilderSetters {
     return impl();
   }
 
-  /// Enable / disable debugger support (off by default).
-  SetterImpl &setEnableDebuggerSupport(bool EnableDebuggerSupport) {
-    impl().EnableDebuggerSupport = EnableDebuggerSupport;
-    return impl();
-  }
-
   /// Set an ExecutorProcessControl object.
   ///
   /// If the platform uses ObjectLinkingLayer by default and no

diff  --git a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
index 3256ed8b7362c66..5956a898de1256d 100644
--- a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
@@ -13,6 +13,7 @@ add_llvm_component_library(LLVMOrcJIT
   CompileUtils.cpp
   Core.cpp
   DebugObjectManagerPlugin.cpp
+  DebuggerSupport.cpp
   DebuggerSupportPlugin.cpp
   DebugUtils.cpp
   EPCDynamicLibrarySearchGenerator.cpp

diff  --git a/llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp b/llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp
new file mode 100644
index 000000000000000..68d68f07b586203
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp
@@ -0,0 +1,61 @@
+//===------ DebuggerSupport.cpp - Utils for enabling debugger support -----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
+#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
+#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
+#include "llvm/ExecutionEngine/Orc/LLJIT.h"
+
+#define DEBUG_TYPE "orc"
+
+using namespace llvm;
+using namespace llvm::orc;
+
+namespace llvm::orc {
+
+Error enableDebuggerSupport(LLJIT &J) {
+  auto *ObjLinkingLayer = dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer());
+  if (!ObjLinkingLayer)
+    return make_error<StringError>("Cannot enable LLJIT debugger support: "
+                                   "Debugger support requires JITLink",
+                                   inconvertibleErrorCode());
+  auto ProcessSymsJD = J.getProcessSymbolsJITDylib();
+  if (!ProcessSymsJD)
+    return make_error<StringError>("Cannot enable LLJIT debugger support: "
+                                   "Process symbols are not available",
+                                   inconvertibleErrorCode());
+
+  auto &ES = J.getExecutionSession();
+  const auto &TT = J.getTargetTriple();
+
+  switch (TT.getObjectFormat()) {
+  case Triple::ELF: {
+    auto Registrar = createJITLoaderGDBRegistrar(ES);
+    if (!Registrar)
+      return Registrar.takeError();
+    ObjLinkingLayer->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
+        ES, std::move(*Registrar), true, true));
+    return Error::success();
+  }
+  case Triple::MachO: {
+    auto DS = GDBJITDebugInfoRegistrationPlugin::Create(ES, *ProcessSymsJD, TT);
+    if (!DS)
+      return DS.takeError();
+    ObjLinkingLayer->addPlugin(std::move(*DS));
+    return Error::success();
+  }
+  default:
+    return make_error<StringError>(
+        "Cannot enable LLJIT debugger support: " +
+            Triple::getObjectFormatTypeName(TT.getObjectFormat()) +
+            " is not supported",
+        inconvertibleErrorCode());
+  }
+}
+
+} // namespace llvm::orc

diff  --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
index 01ce7a4c64ec16e..1a4251353e2dab0 100644
--- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
@@ -10,8 +10,6 @@
 #include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
 #include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
-#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
-#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
@@ -781,18 +779,8 @@ Error LLJITBuilderState::prepareForConstruction() {
 
   // If we need a process JITDylib but no setup function has been given then
   // create a default one.
-  if (!SetupProcessSymbolsJITDylib &&
-      (LinkProcessSymbolsByDefault || EnableDebuggerSupport)) {
-
-    LLVM_DEBUG({
-      dbgs() << "Creating default Process JD setup function (neeeded for";
-      if (LinkProcessSymbolsByDefault)
-        dbgs() << " <link-process-syms-by-default>";
-      if (EnableDebuggerSupport)
-        dbgs() << " <debugger-support>";
-      dbgs() << ")\n";
-    });
-
+  if (!SetupProcessSymbolsJITDylib && LinkProcessSymbolsByDefault) {
+    LLVM_DEBUG(dbgs() << "Creating default Process JD setup function\n");
     SetupProcessSymbolsJITDylib = [this](LLJIT &J) -> Expected<JITDylibSP> {
       auto &JD =
           J.getExecutionSession().createBareJITDylib("<Process Symbols>");
@@ -1014,46 +1002,6 @@ LLJIT::LLJIT(LLJITBuilderState &S, Error &Err)
     }
   }
 
-  if (S.EnableDebuggerSupport) {
-    if (auto *OLL = dyn_cast<ObjectLinkingLayer>(ObjLinkingLayer.get())) {
-      switch (TT.getObjectFormat()) {
-      case Triple::ELF: {
-        auto Registrar = createJITLoaderGDBRegistrar(*ES);
-        if (!Registrar) {
-          Err = Registrar.takeError();
-          return;
-        }
-        OLL->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
-            *ES, std::move(*Registrar), true, true));
-        break;
-      }
-      case Triple::MachO: {
-        assert(ProcessSymbols && "ProcessSymbols JD should be available when "
-                                 "EnableDebuggerSupport is set");
-        auto DS =
-            GDBJITDebugInfoRegistrationPlugin::Create(*ES, *ProcessSymbols, TT);
-        if (!DS) {
-          Err = DS.takeError();
-          return;
-        }
-        OLL->addPlugin(std::move(*DS));
-        break;
-      }
-      default:
-        LLVM_DEBUG({
-          dbgs() << "Cannot enable LLJIT debugger support: "
-                 << Triple::getObjectFormatTypeName(TT.getObjectFormat())
-                 << " not supported.\n";
-        });
-      }
-    } else {
-      LLVM_DEBUG({
-        dbgs() << "Cannot enable LLJIT debugger support: "
-                  " debugger support is only available when using JITLink.\n";
-      });
-    }
-  }
-
   if (S.PrePlatformSetup) {
     if (auto Err2 = S.PrePlatformSetup(*this)) {
       Err = std::move(Err2);

diff  --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index 360ffa628a47b3f..abe1c7556699b54 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -26,6 +26,7 @@
 #include "llvm/ExecutionEngine/MCJIT.h"
 #include "llvm/ExecutionEngine/ObjectCache.h"
 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
+#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
 #include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
@@ -844,6 +845,17 @@ int mingw_noop_main(void) {
   return 0;
 }
 
+// Try to enable debugger support for the given instance.
+// This alway returns success, but prints a warning if it's not able to enable
+// debugger support.
+Error tryEnableDebugSupport(orc::LLJIT &J) {
+  if (auto Err = enableDebuggerSupport(J)) {
+    [[maybe_unused]] std::string ErrMsg = toString(std::move(Err));
+    LLVM_DEBUG(dbgs() << "lli: " << ErrMsg << "\n");
+  }
+  return Error::success();
+}
+
 int runOrcJIT(const char *ProgName) {
   // Start setting up the JIT environment.
 
@@ -924,6 +936,9 @@ int runOrcJIT(const char *ProgName) {
       });
   }
 
+  // Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
+  Builder.setPrePlatformSetup(tryEnableDebugSupport);
+
   // Set up LLJIT platform.
   LLJITPlatform P = Platform;
   if (P == LLJITPlatform::Auto)
@@ -960,9 +975,6 @@ int runOrcJIT(const char *ProgName) {
     });
   }
 
-  // Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
-  Builder.setEnableDebuggerSupport(true);
-
   auto J = ExitOnErr(Builder.create());
 
   auto *ObjLayer = &J->getObjLinkingLayer();


        


More information about the cfe-commits mailing list