Commit 560463e9 by Tianqi Chen Committed by GitHub

[BUILD] Enable RTTI of most part of library, example extension pkg. (#161)

parent 1efc4ca0
...@@ -92,6 +92,7 @@ ENV/ ...@@ -92,6 +92,7 @@ ENV/
*~ *~
build build
config.mk config.mk
config.cmake
build_* build_*
Win32 Win32
*.dir *.dir
......
...@@ -5,12 +5,16 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build/private/local_config.cmake) ...@@ -5,12 +5,16 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build/private/local_config.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/build/private/local_config.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/build/private/local_config.cmake)
endif() endif()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/config.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake)
endif()
include(cmake/Util.cmake) include(cmake/Util.cmake)
tvm_option(USE_CUDA "Build with CUDA" ON) tvm_option(USE_CUDA "Build with CUDA" ON)
tvm_option(USE_OPENCL "Build with OpenCL" OFF) tvm_option(USE_OPENCL "Build with OpenCL" OFF)
tvm_option(USE_RPC "Build with RPC" OFF) tvm_option(USE_RPC "Build with RPC" OFF)
tvm_option(USE_LLVM "Build with LLVM" OFF) tvm_option(USE_LLVM "Build with LLVM" OFF)
tvm_option(USE_RTTI "Build with RTTI" OFF) tvm_option(USE_RTTI "Build with RTTI" ON)
tvm_option(USE_MSVC_MT "Build with MT" OFF) tvm_option(USE_MSVC_MT "Build with MT" OFF)
include_directories("include") include_directories("include")
...@@ -42,7 +46,7 @@ if(MSVC) ...@@ -42,7 +46,7 @@ if(MSVC)
else(MSVC) else(MSVC)
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-std=c++11" SUPPORT_CXX11) check_cxx_compiler_flag("-std=c++11" SUPPORT_CXX11)
set(CMAKE_C_FLAGS "-O3 -fno-rtti -Wall -std=c++11 -fPIC") set(CMAKE_C_FLAGS "-O3 -Wall -std=c++11 -fPIC")
set(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS}) set(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
endif(MSVC) endif(MSVC)
...@@ -83,7 +87,11 @@ find_package(CUDA) ...@@ -83,7 +87,11 @@ find_package(CUDA)
${CUDA_TOOLKIT_ROOT_DIR}/lib/x64 ${CUDA_TOOLKIT_ROOT_DIR}/lib/x64
${CUDA_TOOLKIT_ROOT_DIR}/lib/win32) ${CUDA_TOOLKIT_ROOT_DIR}/lib/win32)
list(APPEND TVM_LINKER_LIBS ${CUDA_NVRTC_LIB}) list(APPEND TVM_LINKER_LIBS ${CUDA_NVRTC_LIB})
endif() else(MSVC)
find_library(CUDA_NVRTC_LIB nvrtc
${CUDA_TOOLKIT_ROOT_DIR}/lib64)
list(APPEND TVM_LINKER_LIBS ${CUDA_NVRTC_LIB})
endif(MSVC)
add_definitions(-DTVM_CUDA_RUNTIME=1) add_definitions(-DTVM_CUDA_RUNTIME=1)
else(USE_CUDA) else(USE_CUDA)
add_definitions(-DTVM_CUDA_RUNTIME=0) add_definitions(-DTVM_CUDA_RUNTIME=0)
...@@ -110,11 +118,15 @@ if(USE_LLVM) ...@@ -110,11 +118,15 @@ if(USE_LLVM)
message(STATUS "Build with LLVM support...") message(STATUS "Build with LLVM support...")
include_directories(${LLVM_INCLUDE_DIRS}) include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS})
add_definitions(-DTVM_LLVM_VERSION=${LLVM_PACKAGE_VERSION})
llvm_map_components_to_libnames(LLVM_LIBS all) llvm_map_components_to_libnames(LLVM_LIBS all)
list(REMOVE_ITEM LLVM_LIBS LTO) list(REMOVE_ITEM LLVM_LIBS LTO)
list(APPEND TVM_LINKER_LIBS ${LLVM_LIBS}) list(APPEND TVM_LINKER_LIBS ${LLVM_LIBS})
add_definitions(-DTVM_LLVM_VERSION=${LLVM_PACKAGE_VERSION})
list(APPEND COMPILER_SRCS ${COMPILER_LLVM_SRCS}) list(APPEND COMPILER_SRCS ${COMPILER_LLVM_SRCS})
if(NOT MSVC)
set_property(SOURCE ${COMPILER_LLVM_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS
"-fno-rtti -DDMLC_ENABLE_RTTI=0")
endif()
endif(USE_LLVM) endif(USE_LLVM)
if(NOT USE_RTTI) if(NOT USE_RTTI)
......
Subproject commit 4fffc62c124651c1cde18f31957db413b677d601 Subproject commit efe5b5cc3c89da5d5e39570f6776d39d8acacacc
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// See documents at https://jenkins.io/doc/book/pipeline/jenkinsfile/ // See documents at https://jenkins.io/doc/book/pipeline/jenkinsfile/
// tvm libraries // tvm libraries
tvm_lib = 'lib/libtvm.so, lib/libtvm_runtime.so, lib/libtvm.a, HalideIR/lib/libHalideIR.a, config.mk' tvm_lib = 'lib/libtvm.so, lib/libtvm_runtime.so, config.mk'
// command to start a docker container // command to start a docker container
docker_run = 'tests/ci_build/ci_build.sh' docker_run = 'tests/ci_build/ci_build.sh'
// timeout in minutes // timeout in minutes
......
...@@ -12,7 +12,7 @@ include $(config) ...@@ -12,7 +12,7 @@ include $(config)
.PHONY: clean all test doc pylint cpplint lint verilog cython cython2 cython3 .PHONY: clean all test doc pylint cpplint lint verilog cython cython2 cython3
BUILD_TARGETS ?= lib/libtvm.so lib/libtvm_runtime.so lib/libtvm.a BUILD_TARGETS ?= lib/libtvm.so lib/libtvm_runtime.so
all: ${BUILD_TARGETS} all: ${BUILD_TARGETS}
# The source code dependencies # The source code dependencies
...@@ -42,9 +42,10 @@ ALL_DEP = $(CC_OBJ) $(CONTRIB_OBJ) $(LIB_HALIDEIR) ...@@ -42,9 +42,10 @@ ALL_DEP = $(CC_OBJ) $(CONTRIB_OBJ) $(LIB_HALIDEIR)
RUNTIME_DEP = $(RUNTIME_OBJ) RUNTIME_DEP = $(RUNTIME_OBJ)
# The flags # The flags
LDFLAGS = -pthread -lm LDFLAGS = -pthread -lm -ldl
CFLAGS = -std=c++11 -Wall -O2 -fno-rtti\ CFLAGS = -std=c++11 -Wall -O2\
-Iinclude -Idlpack/include -Idmlc-core/include -IHalideIR/src -fPIC -DDMLC_ENABLE_RTTI=0 -Iinclude -Idlpack/include -Idmlc-core/include -IHalideIR/src -fPIC
LLVM_CFLAGS= -fno-rtti -DDMLC_ENABLE_RTTI=0
FRAMEWORKS = FRAMEWORKS =
OBJCFLAGS = -fno-objc-arc OBJCFLAGS = -fno-objc-arc
...@@ -93,7 +94,7 @@ ifdef LLVM_CONFIG ...@@ -93,7 +94,7 @@ ifdef LLVM_CONFIG
LLVM_VERSION=$(shell $(LLVM_CONFIG) --version| cut -b 1,3) LLVM_VERSION=$(shell $(LLVM_CONFIG) --version| cut -b 1,3)
LLVM_INCLUDE=$(filter -I%, $(shell $(LLVM_CONFIG) --cxxflags)) LLVM_INCLUDE=$(filter -I%, $(shell $(LLVM_CONFIG) --cxxflags))
LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags --libs --system-libs) LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags --libs --system-libs)
CFLAGS += $(LLVM_INCLUDE) -DTVM_LLVM_VERSION=$(LLVM_VERSION) LLVM_CFLAGS += $(LLVM_INCLUDE) -DTVM_LLVM_VERSION=$(LLVM_VERSION)
endif endif
include make/contrib/cblas.mk include make/contrib/cblas.mk
...@@ -113,16 +114,23 @@ test: $(TEST) ...@@ -113,16 +114,23 @@ test: $(TEST)
include verilog/verilog.mk include verilog/verilog.mk
verilog: $(VER_LIBS) verilog: $(VER_LIBS)
build/%.o: src/%.cc
# Special rules for LLVM related modules.
build/codegen/llvm/%.o: src/codegen/llvm/%.cc
@mkdir -p $(@D) @mkdir -p $(@D)
$(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d $(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d
$(CXX) -c $(CFLAGS) -c $< -o $@ $(CXX) -c $(CFLAGS) $(LLVM_CFLAGS) -c $< -o $@
build/%.o: src/%.mm build/runtime/metal/%.o: src/runtime/metal/%.mm
@mkdir -p $(@D) @mkdir -p $(@D)
$(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d $(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d
$(CXX) $(OBJCFLAGS) -c $(CFLAGS) -c $< -o $@ $(CXX) $(OBJCFLAGS) -c $(CFLAGS) -c $< -o $@
build/%.o: src/%.cc
@mkdir -p $(@D)
$(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d
$(CXX) -c $(CFLAGS) -c $< -o $@
lib/libtvm.so: $(ALL_DEP) $(RUNTIME_DEP) lib/libtvm.so: $(ALL_DEP) $(RUNTIME_DEP)
@mkdir -p $(@D) @mkdir -p $(@D)
$(CXX) $(CFLAGS) $(FRAMEWORKS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS) $(CXX) $(CFLAGS) $(FRAMEWORKS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS)
...@@ -131,9 +139,6 @@ lib/libtvm_runtime.so: $(RUNTIME_DEP) ...@@ -131,9 +139,6 @@ lib/libtvm_runtime.so: $(RUNTIME_DEP)
@mkdir -p $(@D) @mkdir -p $(@D)
$(CXX) $(CFLAGS) $(FRAMEWORKS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS) $(CXX) $(CFLAGS) $(FRAMEWORKS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS)
lib/libtvm.a: $(ALL_DEP) $(RUNTIME_DEP)
@mkdir -p $(@D)
ar crv $@ $(filter %.o, $?)
$(LIB_HALIDEIR): LIBHALIDEIR $(LIB_HALIDEIR): LIBHALIDEIR
...@@ -141,7 +146,7 @@ LIBHALIDEIR: ...@@ -141,7 +146,7 @@ LIBHALIDEIR:
+ cd HalideIR; make lib/libHalideIR.a ; cd $(ROOTDIR) + cd HalideIR; make lib/libHalideIR.a ; cd $(ROOTDIR)
cpplint: cpplint:
python dmlc-core/scripts/lint.py tvm cpp include src verilog python dmlc-core/scripts/lint.py tvm cpp include src verilog examples/extension/src
pylint: pylint:
pylint python/tvm --rcfile=$(ROOTDIR)/tests/lint/pylintrc pylint python/tvm --rcfile=$(ROOTDIR)/tests/lint/pylintrc
......
# Minimum Makefile for the extension package
TVM_ROOT=$(shell cd ../..; pwd)
PKG_CFLAGS = -std=c++11 -O2 -fPIC\
-I${TVM_ROOT}/include\
-I${TVM_ROOT}/dmlc-core/include\
-I${TVM_ROOT}/dlpack/include\
-I${TVM_ROOT}/HalideIR/src
PKG_LDFLAGS =-L${TVM_ROOT}/lib
lib/libtvm_ext.so: src/tvm_ext.cc
@mkdir -p $(@D)
$(CXX) $(PKG_CFLAGS) -shared -o $@ $^ $(PKG_LDFLAGS) -ltvm
Example Extension Library
=========================
This folder contains an example extension library of TVM.
It demonstrates how can other library extend TVM in both C++ and python API.
- The library extends TVM's functionality by link libtvm
- The python module load the new shared library and can interpolate with TVM's python API.
/*!
* Copyright (c) 2017 by Contributors
* \brief Example package that uses TVM.
* \file tvm_ext.cc
*/
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h>
#include <tvm/packed_func_ext.h>
namespace tvm_ext {
using namespace tvm;
using namespace tvm::runtime;
TVM_REGISTER_GLOBAL("tvm_ext.bind_add")
.set_body([](TVMArgs args_, TVMRetValue *rv_) {
PackedFunc pf = args_[0];
int b = args_[1];
*rv_ = PackedFunc([pf, b](TVMArgs args, TVMRetValue *rv) {
*rv = pf(b, args[0]);
});
});
TVM_REGISTER_GLOBAL("tvm_ext.sym_add")
.set_body([](TVMArgs args, TVMRetValue *rv) {
Var a = args[0];
Var b = args[1];
*rv = a + b;
});
} // namespace tvm_ext
import tvm_ext
import tvm
def test_bind_add():
def add(a, b):
return a + b
f = tvm_ext.bind_add(add, 1)
assert f(2) == 3
def test_sym_add():
a = tvm.var('a')
b = tvm.var('b')
c = tvm_ext.sym_add(a, b)
assert c.a == a and c.b == b
if __name__ == "__main__":
test_bind_add()
test_sym_add()
"""Example extension package of TVM."""
from __future__ import absolute_import
import os
import ctypes
def load_lib():
"""Load library, the functions will be registered into TVM"""
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
lib = ctypes.CDLL(os.path.join(curr_path, "../lib/libtvm_ext.so"),
ctypes.RTLD_GLOBAL)
return lib
_LIB = load_lib()
import tvm
# Expose two functions into python
bind_add = tvm.get_global_func("tvm_ext.bind_add")
sym_add = tvm.get_global_func("tvm_ext.sym_add")
...@@ -29,6 +29,7 @@ def find_lib_path(): ...@@ -29,6 +29,7 @@ def find_lib_path():
dll_path.append(os.path.join(curr_path, '../../../windows', vs_configuration)) dll_path.append(os.path.join(curr_path, '../../../windows', vs_configuration))
elif os.name == "posix" and os.environ.get('LD_LIBRARY_PATH', None): elif os.name == "posix" and os.environ.get('LD_LIBRARY_PATH', None):
dll_path.extend([p.strip() for p in os.environ['LD_LIBRARY_PATH'].split(":")]) dll_path.extend([p.strip() for p in os.environ['LD_LIBRARY_PATH'].split(":")])
dll_path = [os.path.abspath(x) for x in dll_path]
if os.name == 'nt': if os.name == 'nt':
lib_dll_path = [os.path.join(p, 'libtvm.dll') for p in dll_path] lib_dll_path = [os.path.join(p, 'libtvm.dll') for p in dll_path]
......
...@@ -4,9 +4,9 @@ GTEST_INC=$(GTEST_PATH)/include/ ...@@ -4,9 +4,9 @@ GTEST_INC=$(GTEST_PATH)/include/
TEST_SRC = $(wildcard tests/cpp/*_test.cc) TEST_SRC = $(wildcard tests/cpp/*_test.cc)
TEST = $(patsubst tests/cpp/%_test.cc, tests/cpp/%_test, $(TEST_SRC)) TEST = $(patsubst tests/cpp/%_test.cc, tests/cpp/%_test, $(TEST_SRC))
tests/cpp/%_test: tests/cpp/%_test.cc lib/libtvm.a HalideIR/lib/libHalideIR.a tests/cpp/%_test: tests/cpp/%_test.cc lib/libtvm.so
$(CXX) -std=c++11 $(CFLAGS) -MM -MT tests/cpp/$* $< >tests/cpp/$*.d $(CXX) -std=c++11 $(CFLAGS) -MM -MT tests/cpp/$* $< >tests/cpp/$*.d
$(CXX) -std=c++11 $(CFLAGS) -I$(GTEST_INC) -o $@ $(filter %.cc %.a, $^) \ $(CXX) -std=c++11 $(CFLAGS) -I$(GTEST_INC) -o $@ $(filter %.cc %.a, $^) \
-L$(GTEST_LIB) $(LDFLAGS) -lgtest -L$(GTEST_LIB) $(LDFLAGS) -lgtest -Llib -ltvm
-include tests/cpp/*.d -include tests/cpp/*.d
#!/bin/bash #!/bin/bash
export LD_LIBRARY_PATH=lib:${LD_LIBRARY_PATH}
make test -j8 || exit -1 make test -j8 || exit -1
for test in tests/cpp/*_test; do for test in tests/cpp/*_test; do
./$test || exit -1 ./$test || exit -1
......
#!/bin/bash #!/bin/bash
export PYTHONPATH=python:examples/extension
export LD_LIBRARY_PATH=lib:${LD_LIBRARY_PATH}
export PYTHONPATH=python # Test extern package package
cd examples/extension
make || exit -1
cd ../..
python -m nose -v examples/extension/tests || exit -1
# Test TVM
make cython || exit -1 make cython || exit -1
TVM_FFI=cython python -m nose -v tests/python/integration || exit -1 TVM_FFI=cython python -m nose -v tests/python/integration || exit -1
TVM_FFI=ctypes python3 -m nose -v tests/python/integration || exit -1 TVM_FFI=ctypes python3 -m nose -v tests/python/integration || exit -1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment