Skip to content
This repository was archived by the owner on Jul 3, 2021. It is now read-only.

Commit 7690ac1

Browse files
In debug mode dump extra info when passing -debug -debug-only=jitfromscratch
1 parent 73a565a commit 7690ac1

File tree

7 files changed

+47
-3
lines changed

7 files changed

+47
-3
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ dump_target_properties(JitFromScratch COMPILE_DEFINITIONS)
108108
dump_target_properties(JitFromScratch LINK_LIBRARIES)
109109
dump_target_properties(JitFromScratch LINK_FLAGS)
110110

111+
# Only LLVM Debug builds can parse debug arguments
112+
if(${LLVM_BUILD_TYPE} STREQUAL "Debug" AND CMAKE_BUILD_TYPE STREQUAL "Debug")
113+
set(debug_args -debug -debug-only=jitfromscratch)
114+
endif()
115+
111116
add_custom_target(run
112-
COMMAND $<TARGET_FILE:JitFromScratch>
117+
COMMAND $<TARGET_FILE:JitFromScratch> ${debug_args}
113118
COMMENT "Running JitFromScratch"
114119
)
115120

JitFromScratch.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#include "JitFromScratch.h"
22

33
#include <llvm/ExecutionEngine/Orc/ThreadSafeModule.h>
4+
#include <llvm/Support/Debug.h>
5+
6+
#define DEBUG_TYPE "jitfromscratch"
47

58
using namespace llvm;
69
using namespace llvm::orc;
710

8-
JitFromScratch::JitFromScratch(ExitOnError ExitOnErr)
9-
: LLJIT(ExitOnErr(LLJITBuilder().create())) {}
11+
JitFromScratch::JitFromScratch(ExitOnError ExitOnErr) {
12+
LLJITBuilder Builder;
13+
ExitOnErr(Builder.prepareForConstruction());
14+
TT = Builder.JTMB->getTargetTriple();
15+
LLJIT = ExitOnErr(Builder.create());
16+
}
1017

1118
Error JitFromScratch::submitModule(std::unique_ptr<Module> M,
1219
std::unique_ptr<LLVMContext> C) {
20+
LLVM_DEBUG(dbgs() << "Submit IR module:\n\n" << *M << "\n\n");
1321
return LLJIT->addIRModule(ThreadSafeModule(std::move(M), std::move(C)));
1422
}

JitFromScratch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <llvm/ADT/Triple.h>
34
#include <llvm/ExecutionEngine/Orc/LLJIT.h>
45
#include <llvm/IR/DataLayout.h>
56
#include <llvm/IR/LLVMContext.h>
@@ -22,8 +23,13 @@ class JitFromScratch {
2223
return LLJIT->getDataLayout();
2324
}
2425

26+
const llvm::Triple &getTargetTriple() const {
27+
return TT;
28+
}
29+
2530
llvm::Error submitModule(std::unique_ptr<llvm::Module> M,
2631
std::unique_ptr<llvm::LLVMContext> C);
2732
private:
2833
std::unique_ptr<llvm::orc::LLJIT> LLJIT;
34+
llvm::Triple TT;
2935
};

main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <llvm/IR/DataLayout.h>
33
#include <llvm/IR/LLVMContext.h>
44
#include <llvm/IR/Module.h>
5+
#include <llvm/Support/CommandLine.h>
6+
#include <llvm/Support/Debug.h>
57
#include <llvm/Support/Error.h>
68
#include <llvm/Support/Format.h>
79
#include <llvm/Support/InitLLVM.h>
@@ -12,6 +14,8 @@
1214

1315
#include "JitFromScratch.h"
1416

17+
#define DEBUG_TYPE "jitfromscratch"
18+
1519
using namespace llvm;
1620

1721
Expected<std::string> codegenIR(Module &module, unsigned items) {
@@ -61,11 +65,17 @@ int main(int argc, char **argv) {
6165
InitializeNativeTargetAsmPrinter();
6266
InitializeNativeTargetAsmParser();
6367

68+
// Parse implicit -debug and -debug-only options.
69+
cl::ParseCommandLineOptions(argc, argv, "JitFromScratch example project\n");
70+
6471
int x[]{0, 1, 2};
6572
int y[]{3, 1, -1};
6673

6774
JitFromScratch Jit(ExitOnErr);
6875

76+
LLVM_DEBUG(dbgs() << "JITing for host target: "
77+
<< Jit.getTargetTriple().normalize() << "\n\n");
78+
6979
auto C = std::make_unique<LLVMContext>();
7080
auto M = std::make_unique<Module>("JitFromScratch", *C);
7181
M->setDataLayout(Jit.getDataLayout());

test/lit.cfg.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
config.test_source_root = os.path.dirname(__file__)
1111
config.suffixes = ['.test']
1212

13+
if config.build_type.lower() == "debug":
14+
config.suffixes.append('.test-debug')
15+
1316
# Add binary directories for JitFromScratch and FileCheck executables
1417
llvm_config.with_environment('PATH', config.jitfromscratch_build_dir, append_path=True)
1518
llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)

test/lit.site.cfg.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
66
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
77
config.python_executable = "@PYTHON_EXECUTABLE@"
88
config.jitfromscratch_build_dir = "@CMAKE_BINARY_DIR@/@CMAKE_CFG_INTDIR@"
9+
config.build_type = "@CMAKE_BUILD_TYPE@"
910

1011
import lit.llvm
1112
lit.llvm.initialize(lit_config, config)

test/stderr.test-debug

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Check that output from LLVM_DEBUG is printed in Debug mode (dumps to stderr)
2+
# RUN: JitFromScratch -debug -debug-only=jitfromscratch 2>&1 | FileCheck %s
3+
4+
# CHECK: JITing for host target: {{.*}}
5+
# CHECK-EMPTY:
6+
# CHECK-NEXT: Submit IR module:
7+
# CHECK-EMPTY:
8+
# CHECK-NEXT: ; ModuleID = 'JitFromScratch'
9+
# CHECK-NEXT: source_filename = "JitFromScratch"
10+
11+
# CHECK: Integer Distances: 3, 0, 3

0 commit comments

Comments
 (0)