From 0c75123b07f89f25ad1037ea55b1fbe827554402 Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Mon, 8 Jun 2015 23:18:14 +0100 Subject: [PATCH] modify build process to make LLVM optional and prepare to link to libgccjit --- CMakeLists.txt | 135 ++++++++++++++++++++++++++--------------- cmake/FindGCCJIT.cmake | 13 ++++ src/ravijit.cpp | 76 +++++++++++++++++++++++ tests/test_gccjit.c | 1 + 4 files changed, 175 insertions(+), 50 deletions(-) create mode 100644 cmake/FindGCCJIT.cmake create mode 100644 tests/test_gccjit.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f0e5796..5ed2d4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,13 +5,31 @@ enable_language(CXX) enable_language(C) enable_testing() -find_package(LLVM REQUIRED CONFIG) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") +option(LLVM_JIT "Controls whether LLVM JIT compilation will be enabled" ON) +option(GCC_JIT "Controls whether GCC JIT compilation will be enabled" OFF) + +if (LLVM_JIT) + find_package(LLVM REQUIRED CONFIG) + + message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") + message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") + + include_directories(${LLVM_INCLUDE_DIRS}) + add_definitions(${LLVM_DEFINITIONS}) + add_definitions(-DUSE_LLVM) +endif() + +if (GCC_JIT) + find_package(GCCJIT REQUIRED) + + message(STATUS "Found GCCJIT") + + include_directories(${GCCJIT_INCLUDE_DIRS}) + add_definitions(-DUSE_GCCJIT) +endif() -include_directories(${LLVM_INCLUDE_DIRS}) -add_definitions(${LLVM_DEFINITIONS}) if (MSVC) set(CMAKE_C_FLAGS_DEBUG "/Od /D_DEBUG /MDd /Zi /RTC1 /EHsc") @@ -72,15 +90,19 @@ endif() include_directories("${PROJECT_SOURCE_DIR}/include") add_definitions(-DLUA_COMPAT_MODULE -DCOCO_DISABLE) +# define LLVM JIT compiler sources +if (LLVM_JIT) + set (LLVM_JIT_SRCS src/ravi_llvmtypes.cpp + src/ravi_llvmcodegen.cpp src/ravi_llvmforprep.cpp src/ravi_llvmcomp.cpp + src/ravi_llvmreturn.cpp src/ravi_llvmload.cpp src/ravi_llvmforloop.cpp + src/ravi_llvmarith1.cpp src/ravi_llvmcall.cpp src/ravi_llvmtable.cpp + src/ravi_llvmarith2.cpp src/ravi_llvmtforcall.cpp src/ravi_llvmrest.cpp) +endif() # define the lua core source files set (LUA_CORE_SRCS src/lapi.c src/lcode.c src/lctype.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c src/lstring.c src/ltable.c src/ltm.c src/lundump.c src/lcoco.c - src/lvm.c src/lzio.c src/ravijit.cpp src/ravi_llvmtypes.cpp - src/ravi_llvmcodegen.cpp src/ravi_llvmforprep.cpp src/ravi_llvmcomp.cpp - src/ravi_llvmreturn.cpp src/ravi_llvmload.cpp src/ravi_llvmforloop.cpp - src/ravi_llvmarith1.cpp src/ravi_llvmcall.cpp src/ravi_llvmtable.cpp - src/ravi_llvmarith2.cpp src/ravi_llvmtforcall.cpp src/ravi_llvmrest.cpp) + src/lvm.c src/lzio.c src/ravijit.cpp) # define the lua lib source files set (LUA_LIB_SRCS src/lauxlib.c src/lbaselib.c src/lbitlib.c src/lcorolib.c src/ldblib.c src/liolib.c src/lmathlib.c src/loslib.c src/ltablib.c src/lstrlib.c src/loadlib.c src/linit.c src/lutf8lib.c) @@ -104,43 +126,45 @@ endif() # InstCombine ScalarOpts Analysis ipa Object ExecutionEngine MC MCParser MCJIT ProfileData ipo # RuntimeDyld Target X86CodeGen X86AsmParser X86Disassembler X86AsmPrinter X86Desc X86Info X86Utils AsmParser) -llvm_map_components_to_libnames(llvm_libs - Analysis - Core - CodeGen - AsmParser - AsmPrinter - BitReader - ExecutionEngine - InstCombine - ipa - ipo - MC - MCJIT - MCParser - Object - RuntimeDyld - ScalarOpts - Support - Target - TransformUtils - X86CodeGen - X86AsmParser - X86Disassembler - X86AsmPrinter - X86Desc - X86Info - X86Utils - ) - -message(STATUS "llvm_libs ${llvm_libs}") +if (LLVM_JIT) + llvm_map_components_to_libnames(llvm_libs + Analysis + Core + CodeGen + AsmParser + AsmPrinter + BitReader + ExecutionEngine + InstCombine + ipa + ipo + MC + MCJIT + MCParser + Object + RuntimeDyld + ScalarOpts + Support + Target + TransformUtils + X86CodeGen + X86AsmParser + X86Disassembler + X86AsmPrinter + X86Desc + X86Info + X86Utils + ) + message(STATUS "llvm_libs ${llvm_libs}") +endif() + #Main library -add_library(ravi SHARED ${RAVI_HEADERS} ${LUA_LIB_SRCS} ${LUA_CORE_SRCS}) +add_library(ravi SHARED ${RAVI_HEADERS} ${LUA_LIB_SRCS} ${LUA_CORE_SRCS} ${LLVM_JIT_SRCS} ${GCC_JIT_SRCS}) if (MSVC) set_target_properties(ravi PROPERTIES DEFINE_SYMBOL "LUA_BUILD_AS_DLL") endif() -target_link_libraries(ravi ${EXTRA_LIBRARIES} ${llvm_libs}) +target_link_libraries(ravi ${EXTRA_LIBRARIES} ${llvm_libs} ${GCCJIT_LIBRARIES}) #add_library(ravis ${RAVI_HEADERS} ${LUA_LIB_SRCS} ${LUA_CORE_SRCS}) #target_link_libraries(ravis ${EXTRA_LIBRARIES} ${llvm_libs}) @@ -151,16 +175,27 @@ target_link_libraries(lua ravi) #add_executable(luac src/luac.c) #target_link_libraries(luac ravis) -#VM test -add_executable(test_vm tests/test_vm.c) -target_link_libraries(test_vm ravi) +if (LLVM_JIT) + #VM test + add_executable(test_vm tests/test_vm.c) + target_link_libraries(test_vm ravi) + + #LLVM playground + add_executable(test_llvm tests/test_llvm.cpp) + target_link_libraries(test_llvm ravi) +endif() -#LLVM playground -add_executable(test_llvm tests/test_llvm.cpp) -target_link_libraries(test_llvm ravi) + +if (GCC_JIT) + #GCC JIT playground + add_executable(test_gccjit tests/test_gccjit.c) + target_link_libraries(test_gccjit ravi) +endif() add_executable(test_misc tests/test_misc.c) -add_test(TestVM test_vm) -add_test(TestLLVM test_llvm) +if (LLVM_JIT) + add_test(TestVM test_vm) + add_test(TestLLVM test_llvm) +endif() add_test(TestMisc test_misc) diff --git a/cmake/FindGCCJIT.cmake b/cmake/FindGCCJIT.cmake new file mode 100644 index 0000000..a964646 --- /dev/null +++ b/cmake/FindGCCJIT.cmake @@ -0,0 +1,13 @@ +find_path(GCCJIT_INC libgccjit.h + PATHS + ~/local/include +) + +find_library(GCCJIT_LIB + NAMES gccjit libgccjit + PATHS + ~/local/lib +) + +set( GCCJIT_INCLUDE_DIRS "${GCCJIT_INC}" ) +set( GCCJIT_LIBRARIES "${GCCJIT_LIB}" ) diff --git a/src/ravijit.cpp b/src/ravijit.cpp index 303d663..b4e0187 100644 --- a/src/ravijit.cpp +++ b/src/ravijit.cpp @@ -20,6 +20,7 @@ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ +#ifdef USE_LLVM #include "ravi_llvmcodegen.h" /* @@ -360,10 +361,15 @@ std::unique_ptr RaviJITStateFactory::newJITState() { } } +#endif + + #ifdef __cplusplus extern "C" { #endif +#ifdef USE_LLVM + #include "lualib.h" #include "lauxlib.h" @@ -466,6 +472,60 @@ void raviV_dumpllvmasm(struct lua_State *L, struct Proto *p) { } } +#else + +// TODO we probably do not need all the headers +// below + +#define lvm_c +#define LUA_CORE + +#include "lprefix.h" +#include "lua.h" +#include "lobject.h" +#include "lstate.h" +#include "lualib.h" +#include "lauxlib.h" + + +// Initialize the JIT State and attach it to the +// Global Lua State +// If a JIT State already exists then this function +// will return -1 +int raviV_initjit(struct lua_State *L) { + return -1; +} + +// Free up the JIT State +void raviV_close(struct lua_State *L) { +} + +// Compile a Lua function +// If JIT is turned off then compilation is skipped +// Compilation occurs if either auto compilation is ON (subject to some thresholds) +// or if a manual compilation request was made +// Returns true if compilation was successful +int raviV_compile(struct lua_State *L, struct Proto *p, int manual_request, + int dump) { + return false; +} + +// Free the JIT compiled function +// Note that this is called by the garbage collector +void raviV_freeproto(struct lua_State *L, struct Proto *p) { +} + +// Dump the LLVM IR +void raviV_dumpllvmir(struct lua_State *L, struct Proto *p) { +} + +// Dump the LLVM ASM +void raviV_dumpllvmasm(struct lua_State *L, struct Proto *p) { +} + +#endif + + // Test if the given function is compiled static int ravi_is_compiled(lua_State *L) { int n = lua_gettop(L); @@ -535,6 +595,7 @@ static int ravi_dump_llvmasm(lua_State *L) { // Turn on/off auto JIT compilation static int ravi_auto(lua_State *L) { +#ifdef USE_LLVM global_State *G = G(L); int n = lua_gettop(L); if (G->ravi_state == NULL) { @@ -560,10 +621,14 @@ static int ravi_auto(lua_State *L) { G->ravi_state->jit->set_minexeccount(min_exec_count); } return 3; +#else + return 0; +#endif } // Turn on/off the JIT compiler static int ravi_jitenable(lua_State *L) { +#ifdef USE_LLVM global_State *G = G(L); int n = lua_gettop(L); bool value = false; @@ -576,10 +641,14 @@ static int ravi_jitenable(lua_State *L) { if (n == 1 && G->ravi_state) G->ravi_state->jit->set_enabled(value); return 1; +#else + return 0; +#endif } // Set LLVM optimization level static int ravi_optlevel(lua_State *L) { +#ifdef USE_LLVM global_State *G = G(L); int n = lua_gettop(L); int value = 1; @@ -592,10 +661,14 @@ static int ravi_optlevel(lua_State *L) { if (n == 1 && G->ravi_state) G->ravi_state->jit->set_optlevel(value); return 1; +#else + return 0; +#endif } // Set LLVM code size level static int ravi_sizelevel(lua_State *L) { +#ifdef USE_LLVM global_State *G = G(L); int n = lua_gettop(L); int value = 0; @@ -608,6 +681,9 @@ static int ravi_sizelevel(lua_State *L) { if (n == 1 && G->ravi_state) G->ravi_state->jit->set_sizelevel(value); return 1; +#else + return 0; +#endif } static const luaL_Reg ravilib[] = {{"iscompiled", ravi_is_compiled}, diff --git a/tests/test_gccjit.c b/tests/test_gccjit.c new file mode 100644 index 0000000..6724ae3 --- /dev/null +++ b/tests/test_gccjit.c @@ -0,0 +1 @@ +#include \ No newline at end of file