Skip to content

Commit e1a5bb5

Browse files
committed
[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.
1 parent ab78962 commit e1a5bb5

File tree

10 files changed

+118
-75
lines changed

10 files changed

+118
-75
lines changed

clang/lib/Interpreter/IncrementalExecutor.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Interpreter/PartialTranslationUnit.h"
1818
#include "llvm/ExecutionEngine/ExecutionEngine.h"
1919
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
20+
#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
2021
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
2122
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2223
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
@@ -46,8 +47,13 @@ IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
4647
JTMB.addFeatures(TI.getTargetOpts().Features);
4748
LLJITBuilder Builder;
4849
Builder.setJITTargetMachineBuilder(JTMB);
49-
// Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
50-
Builder.setEnableDebuggerSupport(true);
50+
Builder.setPrePlatformSetup(
51+
[](LLJIT &J) {
52+
// Try to enable debugging of JIT'd code (only works with JITLink for
53+
// ELF and MachO).
54+
consumeError(enableDebuggerSupport(J));
55+
return llvm::Error::success();
56+
});
5157

5258
if (auto JitOrErr = Builder.create())
5359
Jit = std::move(*JitOrErr);

clang/tools/clang-repl/ClangRepl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ int main(int argc, const char **argv) {
152152
llvm::InitializeAllAsmPrinters();
153153

154154
if (OptHostSupportsJit) {
155-
auto J = llvm::orc::LLJITBuilder()
156-
.setEnableDebuggerSupport(true)
157-
.create();
155+
auto J = llvm::orc::LLJITBuilder().create();
158156
if (J)
159157
llvm::outs() << "true\n";
160158
else {

clang/unittests/Interpreter/ExceptionTests/InterpreterExceptionTest.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ TEST(InterpreterTest, CatchException) {
5252
llvm::InitializeNativeTargetAsmPrinter();
5353

5454
{
55-
auto J = llvm::orc::LLJITBuilder()
56-
.setEnableDebuggerSupport(true)
57-
.create();
55+
auto J = llvm::orc::LLJITBuilder().create();
5856
if (!J) {
5957
// The platform does not support JITs.
6058
// Using llvm::consumeError will require typeinfo for ErrorInfoBase, we

clang/unittests/Interpreter/InterpreterTest.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ static std::string MangleName(NamedDecl *ND) {
191191
}
192192

193193
static bool HostSupportsJit() {
194-
auto J = llvm::orc::LLJITBuilder()
195-
.setEnableDebuggerSupport(true)
196-
.create();
194+
auto J = llvm::orc::LLJITBuilder().create();
197195
if (J)
198196
return true;
199197
LLVMConsumeError(llvm::wrap(J.takeError()));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- DebugerSupport.h - Utils for enabling debugger support --*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Utilities for enabling debugger support.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_EXECUTIONENGINE_ORC_DEBUGGERSUPPORT_H
14+
#define LLVM_EXECUTIONENGINE_ORC_DEBUGGERSUPPORT_H
15+
16+
#include "llvm/Support/Error.h"
17+
18+
namespace llvm {
19+
namespace orc {
20+
21+
class LLJIT;
22+
23+
Error enableDebuggerSupport(LLJIT &J);
24+
25+
} // namespace orc
26+
} // namespace llvm
27+
28+
#endif // LLVM_EXECUTIONENGINE_ORC_DEBUGGERSUPPORT_H

llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ class LLJITBuilderState {
322322
unique_function<Error(LLJIT &)> PrePlatformSetup;
323323
PlatformSetupFunction SetUpPlatform;
324324
unsigned NumCompileThreads = 0;
325-
bool EnableDebuggerSupport = false;
326325

327326
/// Called prior to JIT class construcion to fix up defaults.
328327
Error prepareForConstruction();
@@ -455,12 +454,6 @@ class LLJITBuilderSetters {
455454
return impl();
456455
}
457456

458-
/// Enable / disable debugger support (off by default).
459-
SetterImpl &setEnableDebuggerSupport(bool EnableDebuggerSupport) {
460-
impl().EnableDebuggerSupport = EnableDebuggerSupport;
461-
return impl();
462-
}
463-
464457
/// Set an ExecutorProcessControl object.
465458
///
466459
/// If the platform uses ObjectLinkingLayer by default and no

llvm/lib/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_llvm_component_library(LLVMOrcJIT
1313
CompileUtils.cpp
1414
Core.cpp
1515
DebugObjectManagerPlugin.cpp
16+
DebuggerSupport.cpp
1617
DebuggerSupportPlugin.cpp
1718
DebugUtils.cpp
1819
EPCDynamicLibrarySearchGenerator.cpp
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===------ DebuggerSupport.cpp - Utils for enabling debugger support -----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
10+
#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
11+
#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
12+
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
13+
14+
#define DEBUG_TYPE "orc"
15+
16+
using namespace llvm;
17+
using namespace llvm::orc;
18+
19+
namespace llvm::orc {
20+
21+
Error enableDebuggerSupport(LLJIT &J) {
22+
auto *ObjLinkingLayer = dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer());
23+
if (!ObjLinkingLayer)
24+
return make_error<StringError>("Cannot enable LLJIT debugger support: "
25+
"Debugger support requires JITLink",
26+
inconvertibleErrorCode());
27+
auto ProcessSymsJD = J.getProcessSymbolsJITDylib();
28+
if (!ProcessSymsJD)
29+
return make_error<StringError>("Cannot enable LLJIT debugger support: "
30+
"Process symbols are not available",
31+
inconvertibleErrorCode());
32+
33+
auto &ES = J.getExecutionSession();
34+
const auto &TT = J.getTargetTriple();
35+
36+
switch (TT.getObjectFormat()) {
37+
case Triple::ELF: {
38+
auto Registrar = createJITLoaderGDBRegistrar(ES);
39+
if (!Registrar)
40+
return Registrar.takeError();
41+
ObjLinkingLayer->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
42+
ES, std::move(*Registrar), true, true));
43+
return Error::success();
44+
}
45+
case Triple::MachO: {
46+
auto DS = GDBJITDebugInfoRegistrationPlugin::Create(ES, *ProcessSymsJD, TT);
47+
if (!DS)
48+
return DS.takeError();
49+
ObjLinkingLayer->addPlugin(std::move(*DS));
50+
return Error::success();
51+
}
52+
default:
53+
return make_error<StringError>(
54+
"Cannot enable LLJIT debugger support: " +
55+
Triple::getObjectFormatTypeName(TT.getObjectFormat()) +
56+
" is not supported",
57+
inconvertibleErrorCode());
58+
}
59+
}
60+
61+
} // namespace llvm::orc

llvm/lib/ExecutionEngine/Orc/LLJIT.cpp

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
1111
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
1212
#include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
13-
#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
14-
#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
1513
#include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
1614
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
1715
#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
@@ -781,18 +779,8 @@ Error LLJITBuilderState::prepareForConstruction() {
781779

782780
// If we need a process JITDylib but no setup function has been given then
783781
// create a default one.
784-
if (!SetupProcessSymbolsJITDylib &&
785-
(LinkProcessSymbolsByDefault || EnableDebuggerSupport)) {
786-
787-
LLVM_DEBUG({
788-
dbgs() << "Creating default Process JD setup function (neeeded for";
789-
if (LinkProcessSymbolsByDefault)
790-
dbgs() << " <link-process-syms-by-default>";
791-
if (EnableDebuggerSupport)
792-
dbgs() << " <debugger-support>";
793-
dbgs() << ")\n";
794-
});
795-
782+
if (!SetupProcessSymbolsJITDylib && LinkProcessSymbolsByDefault) {
783+
LLVM_DEBUG(dbgs() << "Creating default Process JD setup function\n");
796784
SetupProcessSymbolsJITDylib = [this](LLJIT &J) -> Expected<JITDylibSP> {
797785
auto &JD =
798786
J.getExecutionSession().createBareJITDylib("<Process Symbols>");
@@ -1014,46 +1002,6 @@ LLJIT::LLJIT(LLJITBuilderState &S, Error &Err)
10141002
}
10151003
}
10161004

1017-
if (S.EnableDebuggerSupport) {
1018-
if (auto *OLL = dyn_cast<ObjectLinkingLayer>(ObjLinkingLayer.get())) {
1019-
switch (TT.getObjectFormat()) {
1020-
case Triple::ELF: {
1021-
auto Registrar = createJITLoaderGDBRegistrar(*ES);
1022-
if (!Registrar) {
1023-
Err = Registrar.takeError();
1024-
return;
1025-
}
1026-
OLL->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
1027-
*ES, std::move(*Registrar), true, true));
1028-
break;
1029-
}
1030-
case Triple::MachO: {
1031-
assert(ProcessSymbols && "ProcessSymbols JD should be available when "
1032-
"EnableDebuggerSupport is set");
1033-
auto DS =
1034-
GDBJITDebugInfoRegistrationPlugin::Create(*ES, *ProcessSymbols, TT);
1035-
if (!DS) {
1036-
Err = DS.takeError();
1037-
return;
1038-
}
1039-
OLL->addPlugin(std::move(*DS));
1040-
break;
1041-
}
1042-
default:
1043-
LLVM_DEBUG({
1044-
dbgs() << "Cannot enable LLJIT debugger support: "
1045-
<< Triple::getObjectFormatTypeName(TT.getObjectFormat())
1046-
<< " not supported.\n";
1047-
});
1048-
}
1049-
} else {
1050-
LLVM_DEBUG({
1051-
dbgs() << "Cannot enable LLJIT debugger support: "
1052-
" debugger support is only available when using JITLink.\n";
1053-
});
1054-
}
1055-
}
1056-
10571005
if (S.PrePlatformSetup) {
10581006
if (auto Err2 = S.PrePlatformSetup(*this)) {
10591007
Err = std::move(Err2);

llvm/tools/lli/lli.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/ExecutionEngine/MCJIT.h"
2727
#include "llvm/ExecutionEngine/ObjectCache.h"
2828
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
29+
#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
2930
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
3031
#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
3132
#include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
@@ -844,6 +845,17 @@ int mingw_noop_main(void) {
844845
return 0;
845846
}
846847

848+
// Try to enable debugger support for the given instance.
849+
// This alway returns success, but prints a warning if it's not able to enable
850+
// debugger support.
851+
Error tryEnableDebugSupport(orc::LLJIT &J) {
852+
if (auto Err = enableDebuggerSupport(J)) {
853+
[[maybe_unused]] std::string ErrMsg = toString(std::move(Err));
854+
LLVM_DEBUG(dbgs() << "lli: " << ErrMsg << "\n");
855+
}
856+
return Error::success();
857+
}
858+
847859
int runOrcJIT(const char *ProgName) {
848860
// Start setting up the JIT environment.
849861

@@ -924,6 +936,9 @@ int runOrcJIT(const char *ProgName) {
924936
});
925937
}
926938

939+
// Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
940+
Builder.setPrePlatformSetup(tryEnableDebugSupport);
941+
927942
// Set up LLJIT platform.
928943
LLJITPlatform P = Platform;
929944
if (P == LLJITPlatform::Auto)
@@ -960,9 +975,6 @@ int runOrcJIT(const char *ProgName) {
960975
});
961976
}
962977

963-
// Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
964-
Builder.setEnableDebuggerSupport(true);
965-
966978
auto J = ExitOnErr(Builder.create());
967979

968980
auto *ObjLayer = &J->getObjLinkingLayer();

0 commit comments

Comments
 (0)