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

Commit

Permalink
In debug mode dump extra info when passing -debug -debug-only=jitfrom…
Browse files Browse the repository at this point in the history
…scratch
  • Loading branch information
weliveindetail committed Dec 8, 2019
1 parent 73a565a commit 7690ac1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ dump_target_properties(JitFromScratch COMPILE_DEFINITIONS)
dump_target_properties(JitFromScratch LINK_LIBRARIES)
dump_target_properties(JitFromScratch LINK_FLAGS)

# Only LLVM Debug builds can parse debug arguments
if(${LLVM_BUILD_TYPE} STREQUAL "Debug" AND CMAKE_BUILD_TYPE STREQUAL "Debug")
set(debug_args -debug -debug-only=jitfromscratch)
endif()

add_custom_target(run
COMMAND $<TARGET_FILE:JitFromScratch>
COMMAND $<TARGET_FILE:JitFromScratch> ${debug_args}
COMMENT "Running JitFromScratch"
)

Expand Down
12 changes: 10 additions & 2 deletions JitFromScratch.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#include "JitFromScratch.h"

#include <llvm/ExecutionEngine/Orc/ThreadSafeModule.h>
#include <llvm/Support/Debug.h>

#define DEBUG_TYPE "jitfromscratch"

using namespace llvm;
using namespace llvm::orc;

JitFromScratch::JitFromScratch(ExitOnError ExitOnErr)
: LLJIT(ExitOnErr(LLJITBuilder().create())) {}
JitFromScratch::JitFromScratch(ExitOnError ExitOnErr) {
LLJITBuilder Builder;
ExitOnErr(Builder.prepareForConstruction());
TT = Builder.JTMB->getTargetTriple();
LLJIT = ExitOnErr(Builder.create());
}

Error JitFromScratch::submitModule(std::unique_ptr<Module> M,
std::unique_ptr<LLVMContext> C) {
LLVM_DEBUG(dbgs() << "Submit IR module:\n\n" << *M << "\n\n");
return LLJIT->addIRModule(ThreadSafeModule(std::move(M), std::move(C)));
}
6 changes: 6 additions & 0 deletions JitFromScratch.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <llvm/ADT/Triple.h>
#include <llvm/ExecutionEngine/Orc/LLJIT.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/LLVMContext.h>
Expand All @@ -22,8 +23,13 @@ class JitFromScratch {
return LLJIT->getDataLayout();
}

const llvm::Triple &getTargetTriple() const {
return TT;
}

llvm::Error submitModule(std::unique_ptr<llvm::Module> M,
std::unique_ptr<llvm::LLVMContext> C);
private:
std::unique_ptr<llvm::orc::LLJIT> LLJIT;
llvm::Triple TT;
};
10 changes: 10 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Debug.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/Format.h>
#include <llvm/Support/InitLLVM.h>
Expand All @@ -12,6 +14,8 @@

#include "JitFromScratch.h"

#define DEBUG_TYPE "jitfromscratch"

using namespace llvm;

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

// Parse implicit -debug and -debug-only options.
cl::ParseCommandLineOptions(argc, argv, "JitFromScratch example project\n");

int x[]{0, 1, 2};
int y[]{3, 1, -1};

JitFromScratch Jit(ExitOnErr);

LLVM_DEBUG(dbgs() << "JITing for host target: "
<< Jit.getTargetTriple().normalize() << "\n\n");

auto C = std::make_unique<LLVMContext>();
auto M = std::make_unique<Module>("JitFromScratch", *C);
M->setDataLayout(Jit.getDataLayout());
Expand Down
3 changes: 3 additions & 0 deletions test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
config.test_source_root = os.path.dirname(__file__)
config.suffixes = ['.test']

if config.build_type.lower() == "debug":
config.suffixes.append('.test-debug')

# Add binary directories for JitFromScratch and FileCheck executables
llvm_config.with_environment('PATH', config.jitfromscratch_build_dir, append_path=True)
llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)
1 change: 1 addition & 0 deletions test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
config.python_executable = "@PYTHON_EXECUTABLE@"
config.jitfromscratch_build_dir = "@CMAKE_BINARY_DIR@/@CMAKE_CFG_INTDIR@"
config.build_type = "@CMAKE_BUILD_TYPE@"

import lit.llvm
lit.llvm.initialize(lit_config, config)
Expand Down
11 changes: 11 additions & 0 deletions test/stderr.test-debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Check that output from LLVM_DEBUG is printed in Debug mode (dumps to stderr)
# RUN: JitFromScratch -debug -debug-only=jitfromscratch 2>&1 | FileCheck %s

# CHECK: JITing for host target: {{.*}}
# CHECK-EMPTY:
# CHECK-NEXT: Submit IR module:
# CHECK-EMPTY:
# CHECK-NEXT: ; ModuleID = 'JitFromScratch'
# CHECK-NEXT: source_filename = "JitFromScratch"

# CHECK: Integer Distances: 3, 0, 3

0 comments on commit 7690ac1

Please sign in to comment.