From df12cc0c37baf9c1b6eae0f94cce084c290a42e3 Mon Sep 17 00:00:00 2001 From: stephenneuendorffer Date: Thu, 7 Oct 2021 21:44:15 -0700 Subject: [PATCH] Add support for out-of-tree building (#356) Currently I haven't attempted to get the python bits working. Instead the python bits are forcibly disabled. --- CMakeLists.txt | 102 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 85 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a04f7a1a..a2b70b7ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,26 +1,90 @@ -if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) -message(FATAL_ERROR - "This project is intended to be built as part of LLVM via " - "-DLLVM_EXTERNAL_PROJECTS=torch-mlir " - "-DLLVM_EXTERNAL_TORCH_MLIR_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}") +cmake_minimum_required(VERSION 3.12) + +if(POLICY CMP0068) + cmake_policy(SET CMP0068 NEW) + set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON) endif() -option(MLIR_ENABLE_BINDINGS_PYTHON "Enables MLIR Python Bindings" OFF) -option(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER "Enables JIT IR Importer" ON) +if(POLICY CMP0075) + cmake_policy(SET CMP0075 NEW) +endif() + +if(POLICY CMP0077) + cmake_policy(SET CMP0077 NEW) +endif() + +#------------------------------------------------------------------------------- +# Project setup and globals +#------------------------------------------------------------------------------- + +project(npcomp LANGUAGES CXX C) +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 14) + +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + # Out-of-tree build + + #------------------------------------------------------------------------------- + # MLIR/LLVM Configuration + #------------------------------------------------------------------------------- + + 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) + + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + + # 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) + + include(AddMLIRPython) + declare_mlir_python_sources(MLIRPythonSources) + + # Don't try to compile the python extensions at the moment. We need + # to import lots of dependencies from AddMLIRPython to make this work. + set(MLIR_ENABLE_BINDINGS_PYTHON 0) + + set(NPCOMP_BUILT_STANDALONE 1) + set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}") +else() + # In-tree build with LLVM_EXTERNAL_PROJECTS=torch-mlir + + option(MLIR_ENABLE_BINDINGS_PYTHON "Enables MLIR Python Bindings" OFF) + option(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER "Enables JIT IR Importer" ON) + + # TODO: Fix this upstream so that global include directories are not needed. + set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir) + set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include) + set(MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR}/tools/mlir/include) + set(MLIR_INCLUDE_DIRS "${MLIR_INCLUDE_DIR};${MLIR_GENERATED_INCLUDE_DIR}") + endif() set(TORCH_MLIR_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set(TORCH_MLIR_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}") message(STATUS "Building torch-mlir project at ${TORCH_MLIR_SOURCE_DIR} (into ${TORCH_MLIR_BINARY_DIR})") -# TODO: Fix this upstream so that global include directories are not needed. -set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir) -set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include) -set(MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR}/tools/mlir/include) - -# TODO: Needed for tablegen. Remove. -include_directories(SYSTEM ${MLIR_INCLUDE_DIR}) -include_directories(SYSTEM ${MLIR_GENERATED_INCLUDE_DIR}) -include_directories(SYSTEM ${TORCH_MLIR_SOURCE_DIR}/include) +include_directories(${LLVM_INCLUDE_DIRS}) +include_directories(${MLIR_INCLUDE_DIRS}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) +include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) +# link_directories(${LLVM_BUILD_LIBRARY_DIR}) +# add_definitions(${LLVM_DEFINITIONS}) function(torch_mlir_target_includes target) set(_dirs @@ -69,7 +133,6 @@ add_subdirectory(tools) add_custom_target(check-torch-mlir-all) add_dependencies(check-torch-mlir-all check-torch-mlir - check-torch-mlir-python ) if(MLIR_ENABLE_BINDINGS_PYTHON) @@ -78,7 +141,12 @@ if(MLIR_ENABLE_BINDINGS_PYTHON) if(NOT TORCH_MLIR_PYTHON_PACKAGES_DIR) set(TORCH_MLIR_PYTHON_PACKAGES_DIR "${CMAKE_CURRENT_BINARY_DIR}/python_packages") endif() + add_dependencies(check-torch-mlir-all + check-torch-mlir-python + ) add_subdirectory(python) +else() + add_custom_target(TorchMLIRPythonModules) endif() add_subdirectory(test)