Enable building using LLVM_EXTERNAL_PROJECTS. (#152)

This allows building NPCOMP as an external project of LLVM, similar to
how CIRCT can be built: https://github.com/llvm/circt/pull/227.

The CMake options to use this build style look like this:

```
  -DLLVM_EXTERNAL_PROJECTS=npcomp \
  -DLLVM_EXTERNAL_NPCOMP_SOURCE_DIR=/path/to/mlir-npcomp \
```
pull/148/head
mikeurbach 2021-01-26 11:43:43 -07:00 committed by GitHub
parent f96c05abd4
commit 0f6a65a1c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 39 deletions

View File

@ -13,15 +13,6 @@ if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW) cmake_policy(SET CMP0077 NEW)
endif() endif()
#-------------------------------------------------------------------------------
# Project setup and globals
#-------------------------------------------------------------------------------
project(npcomp LANGUAGES CXX C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Options and settings # Options and settings
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@ -55,38 +46,68 @@ if(MSVC)
) )
endif() endif()
#------------------------------------------------------------------------------- if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
# MLIR/LLVM Configuration # If we are not building as a part of LLVM, build NPCOMP as a standalone
#------------------------------------------------------------------------------- # project, using LLVM as an external library.
find_package(MLIR REQUIRED CONFIG) #-------------------------------------------------------------------------------
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}") # Project setup and globals
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") #-------------------------------------------------------------------------------
#set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin) project(npcomp LANGUAGES CXX C)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib) set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
# Define the default arguments to use with 'lit', and an option for the user to #-------------------------------------------------------------------------------
# override. # MLIR/LLVM Configuration
set(LIT_ARGS_DEFAULT "-sv") #-------------------------------------------------------------------------------
if (MSVC_IDE OR XCODE)
set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") find_package(MLIR REQUIRED CONFIG)
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
#set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
# Define the default arguments to use with 'lit', and an option for the user to
# override.
set(LIT_ARGS_DEFAULT "-sv")
if (MSVC_IDE OR XCODE)
set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
endif()
set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)
set(NPCOMP_BUILT_STANDALONE 1)
set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
else()
# Otherwise, we are building as a part of LLVM, and we need to set up some
# variables and includes.
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>)
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
endif() endif()
set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(AddNPCOMP) include(AddNPCOMP)
include(HandleLLVMOptions)
include(NpcompDetectPythonEnv) include(NpcompDetectPythonEnv)
include(ConfigurePyTorch) include(ConfigurePyTorch)
include_directories(${LLVM_INCLUDE_DIRS}) include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS}) include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${PROJECT_BINARY_DIR}/include) include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
link_directories(${LLVM_BUILD_LIBRARY_DIR}) link_directories(${LLVM_BUILD_LIBRARY_DIR})
add_definitions(${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS})
set(NPCOMP_TABLEGEN_ARGS "") set(NPCOMP_TABLEGEN_ARGS "")
@ -172,8 +193,8 @@ set(MLIR_NPCOMP_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(MLIR_NPCOMP_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(MLIR_NPCOMP_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
add_custom_target(check-npcomp) add_custom_target(check-npcomp)
add_custom_target(check-all) add_custom_target(check-npcomp-all)
add_dependencies(check-all check-npcomp) add_dependencies(check-npcomp-all check-npcomp)
add_subdirectory(include/npcomp) add_subdirectory(include/npcomp)
add_subdirectory(lib) add_subdirectory(lib)

View File

@ -29,4 +29,4 @@ add_lit_testsuite(check-frontends-pytorch "Running the frontends-pytorch regress
set_target_properties(check-frontends-pytorch PROPERTIES FOLDER "Tests") set_target_properties(check-frontends-pytorch PROPERTIES FOLDER "Tests")
add_lit_testsuites(TORCH_MLIR ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${TEST_DEPENDS}) add_lit_testsuites(TORCH_MLIR ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${TEST_DEPENDS})
add_dependencies(check-all check-frontends-pytorch) add_dependencies(check-npcomp-all check-frontends-pytorch)

View File

@ -58,10 +58,11 @@ config.test_exec_root = os.path.join(config.npcomp_obj_root, 'test')
config.npcomp_tools_dir = os.path.join(config.npcomp_obj_root, 'bin') config.npcomp_tools_dir = os.path.join(config.npcomp_obj_root, 'bin')
# Tweak the PATH to include the tools dir. # Tweak the PATH to include the tools dir.
npcomp_python_dir = "python" if config.npcomp_built_standalone else "tools/npcomp/python"
llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True) llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)
llvm_config.with_environment('PYTHONPATH', [ llvm_config.with_environment('PYTHONPATH', [
os.path.join(config.llvm_obj_root, "python"), os.path.join(config.llvm_obj_root, "python"),
os.path.join(config.npcomp_obj_root, "python")], os.path.join(config.npcomp_obj_root, npcomp_python_dir)],
append_path=True) append_path=True)

View File

@ -34,6 +34,7 @@ config.llvm_host_triple = '@LLVM_HOST_TRIPLE@'
config.host_arch = "@HOST_ARCH@" config.host_arch = "@HOST_ARCH@"
config.npcomp_src_root = "@CMAKE_SOURCE_DIR@" config.npcomp_src_root = "@CMAKE_SOURCE_DIR@"
config.npcomp_obj_root = "@CMAKE_BINARY_DIR@" config.npcomp_obj_root = "@CMAKE_BINARY_DIR@"
config.npcomp_built_standalone = bool("@NPCOMP_BUILT_STANDALONE@")
# Support substitution of the tools_dir with user parameters. This is # Support substitution of the tools_dir with user parameters. This is
# used when we can't determine the tool dir at configuration time. # used when we can't determine the tool dir at configuration time.
@ -50,4 +51,4 @@ import lit.llvm
lit.llvm.initialize(lit_config, config) lit.llvm.initialize(lit_config, config)
# Let the main config do the real work. # Let the main config do the real work.
lit_config.load_config(config, "@CMAKE_SOURCE_DIR@/frontends/pytorch/test/lit.cfg.py") lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg.py")

View File

@ -2,4 +2,4 @@ set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls ${NPCOMP_TABLEGEN_ARGS}) mlir_tablegen(Passes.h.inc -gen-pass-decls ${NPCOMP_TABLEGEN_ARGS})
add_public_tablegen_target(NPCOMPConversionPassIncGen) add_public_tablegen_target(NPCOMPConversionPassIncGen)
add_mlir_doc(Passes -gen-pass-doc ConversionPasses ./) add_mlir_doc(Passes -gen-pass-doc NPCOMPConversionPasses ./)

View File

@ -54,10 +54,11 @@ config.npcomp_runtime_shlib = os.path.join(
'libNPCOMPCompilerRuntimeShlib' + config.llvm_shlib_ext) 'libNPCOMPCompilerRuntimeShlib' + config.llvm_shlib_ext)
# Tweak the PATH and PYTHONPATH to include the tools dir. # Tweak the PATH and PYTHONPATH to include the tools dir.
npcomp_python_dir = "python" if config.npcomp_built_standalone else "tools/npcomp/python"
llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True) llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)
llvm_config.with_environment('PYTHONPATH', [ llvm_config.with_environment('PYTHONPATH', [
os.path.join(config.llvm_obj_root, "python"), os.path.join(config.llvm_obj_root, "python"),
os.path.join(config.npcomp_obj_root, "python")], os.path.join(config.npcomp_obj_root, npcomp_python_dir)],
append_path=True) append_path=True)
tool_dirs = [ tool_dirs = [

View File

@ -30,6 +30,7 @@ config.llvm_host_triple = '@LLVM_HOST_TRIPLE@'
config.host_arch = "@HOST_ARCH@" config.host_arch = "@HOST_ARCH@"
config.npcomp_src_root = "@CMAKE_SOURCE_DIR@" config.npcomp_src_root = "@CMAKE_SOURCE_DIR@"
config.npcomp_obj_root = "@CMAKE_BINARY_DIR@" config.npcomp_obj_root = "@CMAKE_BINARY_DIR@"
config.npcomp_built_standalone = bool("@NPCOMP_BUILT_STANDALONE@")
# Optional features. # Optional features.
config.npcomp_enable_iree = @NPCOMP_ENABLE_IREE@ config.npcomp_enable_iree = @NPCOMP_ENABLE_IREE@
@ -49,4 +50,4 @@ import lit.llvm
lit.llvm.initialize(lit_config, config) lit.llvm.initialize(lit_config, config)
# Let the main config do the real work. # Let the main config do the real work.
lit_config.load_config(config, "@CMAKE_SOURCE_DIR@/test/lit.cfg.py") lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg.py")