Skip to content

Commit c3f0ed7

Browse files
committed
[mlir] Register the GDB listener with ExecutionEngine to enable debugging JIT'd code
Differential Revision: https://reviews.llvm.org/D73932
1 parent 0bfc489 commit c3f0ed7

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

mlir/include/mlir/ExecutionEngine/ExecutionEngine.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace llvm {
2626
template <typename T> class Expected;
2727
class Module;
2828
class ExecutionEngine;
29+
class JITEventListener;
2930
class MemoryBuffer;
3031
} // namespace llvm
3132

@@ -97,15 +98,18 @@ class ExecutionEngine {
9798
void dumpToObjectFile(StringRef filename);
9899

99100
private:
100-
// Ordering of llvmContext and jit is important for destruction purposes: the
101-
// jit must be destroyed before the context.
101+
/// Ordering of llvmContext and jit is important for destruction purposes: the
102+
/// jit must be destroyed before the context.
102103
llvm::LLVMContext llvmContext;
103104

104-
// Underlying LLJIT.
105+
/// Underlying LLJIT.
105106
std::unique_ptr<llvm::orc::LLJIT> jit;
106107

107-
// Underlying cache.
108+
/// Underlying cache.
108109
std::unique_ptr<SimpleObjectCache> cache;
110+
111+
/// GDB notification listener.
112+
llvm::JITEventListener *gdbListener;
109113
};
110114

111115
template <typename... Args>

mlir/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "llvm/Bitcode/BitcodeReader.h"
2020
#include "llvm/Bitcode/BitcodeWriter.h"
21+
#include "llvm/ExecutionEngine/JITEventListener.h"
2122
#include "llvm/ExecutionEngine/ObjectCache.h"
2223
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
2324
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
@@ -182,7 +183,8 @@ static void packFunctionArguments(Module *module) {
182183
}
183184

184185
ExecutionEngine::ExecutionEngine(bool enableObjectCache)
185-
: cache(enableObjectCache ? nullptr : new SimpleObjectCache()) {}
186+
: cache(enableObjectCache ? nullptr : new SimpleObjectCache()),
187+
gdbListener(llvm::JITEventListener::createGDBRegistrationListener()) {}
186188

187189
Expected<std::unique_ptr<ExecutionEngine>> ExecutionEngine::create(
188190
ModuleOp m, std::function<Error(llvm::Module *)> transformer,
@@ -221,6 +223,14 @@ Expected<std::unique_ptr<ExecutionEngine>> ExecutionEngine::create(
221223
const Triple &TT) {
222224
auto objectLayer = std::make_unique<RTDyldObjectLinkingLayer>(
223225
session, []() { return std::make_unique<SectionMemoryManager>(); });
226+
objectLayer->setNotifyLoaded(
227+
[engine = engine.get()](
228+
llvm::orc::VModuleKey, const llvm::object::ObjectFile &object,
229+
const llvm::RuntimeDyld::LoadedObjectInfo &objectInfo) {
230+
uint64_t key = static_cast<uint64_t>(
231+
reinterpret_cast<uintptr_t>(object.getData().data()));
232+
engine->gdbListener->notifyObjectLoaded(key, object, objectInfo);
233+
});
224234
auto dataLayout = deserModule->getDataLayout();
225235
llvm::orc::JITDylib *mainJD = session.getJITDylibByName("<main>");
226236
if (!mainJD)

0 commit comments

Comments
 (0)