Skip to content

Commit

Permalink
Enable gdb and perf event listeners
Browse files Browse the repository at this point in the history
The following patch ensure that query symbols are properly exposed to
gdb and linux's perf.

Note that the following [1] patch must be applied to perf for proper
unwinding. Afterward, invoke perf as follow:

```
```

[1] https://lore.kernel.org/lkml/20191003105716.GB23291@krava/T/#u
  • Loading branch information
fsaintjacques committed Dec 28, 2019
1 parent 69ce13e commit 077a9b7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ option(BUILD_TESTS "Builder unit tests" ON)
option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." ON)

include(CompilerToolchain)
find_package(LLVM 8 REQUIRED CONFIG)
find_package(LLVM 9 REQUIRED CONFIG)

add_subdirectory(src/jitmap)
add_subdirectory(tools)
Expand Down
2 changes: 1 addition & 1 deletion src/jitmap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ target_include_directories(jitmap PUBLIC ${LLVM_INCLUDE_DIRS})

# Required for the jit engine
set(LLVM_CORE_COMPONENTS support core irreader orcjit vectorize)
set(LLVM_NATIVE_JIT_COMPONENTS x86codegen x86asmparser x86disassembler x86asmprinter x86desc x86info x86utils)
set(LLVM_NATIVE_JIT_COMPONENTS x86codegen x86asmparser x86disassembler x86desc x86info x86utils perfjitevents)

llvm_map_components_to_libnames(LLVM_LIBRARIES ${LLVM_CORE_COMPONENTS} ${LLVM_NATIVE_JIT_COMPONENTS})
target_link_libraries(jitmap ${LLVM_LIBRARIES})
47 changes: 43 additions & 4 deletions src/jitmap/query/jit.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include <memory>

#include <llvm/ADT/Triple.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/ExecutionEngine/JITEventListener.h>
#include <llvm/ExecutionEngine/JITSymbol.h>
#include <llvm/ExecutionEngine/Orc/ExecutionUtils.h>
#include <llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h>
Expand All @@ -10,12 +14,13 @@
#include <llvm/Support/Error.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Target/TargetMachine.h>

#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/Utils.h>
#include <llvm/Transforms/Vectorize.h>
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
Expand Down Expand Up @@ -81,10 +86,44 @@ auto InitHostTargetMachineBuilder(const CompilerOptions& opts) {
return machine_builder;
}

std::unique_ptr<orc::LLJIT> InitLLJIT(orc::JITTargetMachineBuilder builder,
// Register a custom ObjectLinkerLayer to support (query) symbols with gdb and perf.
// LLJIT (via ORC) doesn't support explicitly the llvm::JITEventListener
// interface. This is the missing glue.
std::unique_ptr<orc::ObjectLayer> ObjectLinkingLayerFactory(
orc::ExecutionSession& execution_session) {
auto memory_manager_factory = []() {
return std::make_unique<llvm::SectionMemoryManager>();
};

auto linking_layer = std::make_unique<orc::RTDyldObjectLinkingLayer>(
execution_session, std::move(memory_manager_factory));

std::vector<llvm::JITEventListener*> listeners{
llvm::JITEventListener::createGDBRegistrationListener(),
llvm::JITEventListener::createPerfJITEventListener()};

// Lambda invoked whenever a new symbol is added.
auto notify_loaded = [listeners](orc::VModuleKey key,
const llvm::object::ObjectFile& object,
const llvm::RuntimeDyld::LoadedObjectInfo& info) {
for (auto listener : listeners) {
if (listener) listener->notifyObjectLoaded(key, object, info);
}
};

linking_layer->setNotifyLoaded(notify_loaded);

return linking_layer;
}

std::unique_ptr<orc::LLJIT> InitLLJIT(orc::JITTargetMachineBuilder machine_builder,
llvm::DataLayout layout,
const CompilerOptions& options) {
return ExpectOrRaise(orc::LLJIT::Create(builder, layout, options.compiler_threads));
return ExpectOrRaise(orc::LLJITBuilder()
.setJITTargetMachineBuilder(machine_builder)
.setNumCompileThreads(options.compiler_threads)
.setObjectLinkingLayerCreator(ObjectLinkingLayerFactory)
.create());
}

auto AsThreadSafeModule(QueryIRCodeGen::ModuleAndContext module_context) {
Expand Down
4 changes: 4 additions & 0 deletions tests/query/jit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ TEST_F(JitTest, CompileAndExecuteTest) {

AssertQueryResult("a & b & c & d & e", {a, b, c, d, e}, a & b & c & d & e);
AssertQueryResult("a | b | c | d | e", {a, b, c, d, e}, a | b | c | d | e);

// Complex re-use of inputs
AssertQueryResult("(a | b) & (((!a & c) | (d & b)) ^ (!e & b))", {a, b, c, d, e},
(a | b) & (((~a & c) | (d & b)) ^ (~e & b)));
}

} // namespace query
Expand Down

0 comments on commit 077a9b7

Please sign in to comment.