From 6e055f0c28c1092af9402e25b1c03325e7458f62 Mon Sep 17 00:00:00 2001 From: Bo Qiao Date: Wed, 27 Apr 2022 23:53:55 +0800 Subject: [PATCH] [Build] [refactor] Define runtime build target (#4838) * Move LLVM Cmake to its own dir * Suppress warning from submodules * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Use current source dir * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Separate Vulkan runtime files from codegen * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- CMakeLists.txt | 11 +---------- cmake/TaichiCore.cmake | 13 ++++++++----- taichi/backends/dx/dx_program.h | 2 +- .../backends/vulkan/aot_module_builder_impl.h | 2 +- .../vulkan/aot_module_loader_impl.cpp | 2 +- .../backends/vulkan/aot_module_loader_impl.h | 2 +- taichi/backends/vulkan/snode_tree_manager.cpp | 2 +- taichi/backends/vulkan/vulkan_program.h | 2 +- taichi/python/export_misc.cpp | 2 +- taichi/runtime/llvm/CMakeLists.txt | 19 +++++++++++++++++++ taichi/runtime/vulkan/CMakeLists.txt | 13 +++++++++++++ .../{backends => runtime}/vulkan/runtime.cpp | 2 +- taichi/{backends => runtime}/vulkan/runtime.h | 0 13 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 taichi/runtime/llvm/CMakeLists.txt create mode 100644 taichi/runtime/vulkan/CMakeLists.txt rename taichi/{backends => runtime}/vulkan/runtime.cpp (99%) rename taichi/{backends => runtime}/vulkan/runtime.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7387e8b648b56..75d71cb604609 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,16 +148,7 @@ if (${CLANG_VERSION_MAJOR} VERSION_GREATER ${CLANG_HIGHEST_VERSION}) endif() endif() -# Build llvm-runtime for host arch and cuda (if available) -foreach(arch IN LISTS HOST_ARCH CUDA_ARCH) - add_custom_target( - "generate_llvm_runtime_${arch}" - COMMAND ${CLANG_EXECUTABLE} ${CLANG_OSX_FLAGS} -c runtime.cpp -o "runtime_${arch}.bc" -fno-exceptions -emit-llvm -std=c++17 -D "ARCH_${arch}" -I ${PROJECT_SOURCE_DIR}; - WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/taichi/runtime/llvm" - ) - add_dependencies(${CORE_LIBRARY_NAME} "generate_llvm_runtime_${arch}") - install(FILES "${PROJECT_SOURCE_DIR}/taichi/runtime/llvm/runtime_${arch}.bc" DESTINATION ${CMAKE_INSTALL_PREFIX}/python/taichi/_lib/runtime) -endforeach() +add_subdirectory(taichi/runtime/llvm) configure_file(taichi/common/version.h.in ${CMAKE_SOURCE_DIR}/taichi/common/version.h) configure_file(taichi/common/commit_hash.h.in ${CMAKE_SOURCE_DIR}/taichi/common/commit_hash.h) diff --git a/cmake/TaichiCore.cmake b/cmake/TaichiCore.cmake index 687130d9e19d5..8b740b5e74f82 100644 --- a/cmake/TaichiCore.cmake +++ b/cmake/TaichiCore.cmake @@ -16,6 +16,9 @@ option(TI_EMSCRIPTENED "Build using emscripten" OFF) # projects. set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) +# Suppress warnings from submodules introduced by the above symbol visibility change +set(CMAKE_POLICY_DEFAULT_CMP0063 NEW) +set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) set(INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/python/taichi/_lib) if(ANDROID) @@ -140,10 +143,6 @@ file(GLOB TAICHI_OPENGL_REQUIRED_SOURCE "taichi/backends/opengl/codegen_opengl.*" "taichi/backends/opengl/struct_opengl.*" ) -file(GLOB TAICHI_VULKAN_REQUIRED_SOURCE - "taichi/backends/vulkan/runtime.h" - "taichi/backends/vulkan/runtime.cpp" -) list(REMOVE_ITEM TAICHI_CORE_SOURCE ${TAICHI_BACKEND_SOURCE}) @@ -198,7 +197,7 @@ if (TI_WITH_VULKAN) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_WITH_VULKAN") list(APPEND TAICHI_CORE_SOURCE ${TAICHI_VULKAN_SOURCE}) endif() -list(APPEND TAICHI_CORE_SOURCE ${TAICHI_VULKAN_REQUIRED_SOURCE}) + if (TI_WITH_DX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_WITH_DX11") @@ -389,8 +388,12 @@ if (TI_WITH_VULKAN) install(FILES ${CMAKE_BINARY_DIR}/libMoltenVK.dylib DESTINATION ${INSTALL_LIB_DIR}/runtime) endif() endif() + + add_subdirectory(taichi/runtime/vulkan) + target_link_libraries(${CORE_LIBRARY_NAME} vulkan_runtime) endif () + # Optional dependencies if (APPLE) diff --git a/taichi/backends/dx/dx_program.h b/taichi/backends/dx/dx_program.h index 4974139147b0c..493cb5c62c69a 100644 --- a/taichi/backends/dx/dx_program.h +++ b/taichi/backends/dx/dx_program.h @@ -3,7 +3,7 @@ #ifdef TI_WITH_DX11 #include "taichi/backends/dx/dx_device.h" -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" #include "taichi/backends/vulkan/snode_tree_manager.h" #include "taichi/program/program_impl.h" diff --git a/taichi/backends/vulkan/aot_module_builder_impl.h b/taichi/backends/vulkan/aot_module_builder_impl.h index bbd6b40e4df48..0accfcc203343 100644 --- a/taichi/backends/vulkan/aot_module_builder_impl.h +++ b/taichi/backends/vulkan/aot_module_builder_impl.h @@ -5,7 +5,7 @@ #include "taichi/aot/module_builder.h" #include "taichi/backends/vulkan/aot_utils.h" -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" #include "taichi/codegen/spirv/snode_struct_compiler.h" #include "taichi/codegen/spirv/kernel_utils.h" diff --git a/taichi/backends/vulkan/aot_module_loader_impl.cpp b/taichi/backends/vulkan/aot_module_loader_impl.cpp index a773f71f37def..5f87eb4fc9ca8 100644 --- a/taichi/backends/vulkan/aot_module_loader_impl.cpp +++ b/taichi/backends/vulkan/aot_module_loader_impl.cpp @@ -3,7 +3,7 @@ #include #include -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" namespace taichi { namespace lang { diff --git a/taichi/backends/vulkan/aot_module_loader_impl.h b/taichi/backends/vulkan/aot_module_loader_impl.h index 7d32d991f2e89..b188281cb749d 100644 --- a/taichi/backends/vulkan/aot_module_loader_impl.h +++ b/taichi/backends/vulkan/aot_module_loader_impl.h @@ -5,7 +5,7 @@ #include #include "taichi/backends/vulkan/aot_utils.h" -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" #include "taichi/codegen/spirv/kernel_utils.h" #include "taichi/aot/module_loader.h" diff --git a/taichi/backends/vulkan/snode_tree_manager.cpp b/taichi/backends/vulkan/snode_tree_manager.cpp index 0bfb6d2f01edd..b7d4816ae42d9 100644 --- a/taichi/backends/vulkan/snode_tree_manager.cpp +++ b/taichi/backends/vulkan/snode_tree_manager.cpp @@ -1,6 +1,6 @@ #include "taichi/backends/vulkan/snode_tree_manager.h" -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" namespace taichi { namespace lang { diff --git a/taichi/backends/vulkan/vulkan_program.h b/taichi/backends/vulkan/vulkan_program.h index a94f2abbb2ba7..b3b33348c525f 100644 --- a/taichi/backends/vulkan/vulkan_program.h +++ b/taichi/backends/vulkan/vulkan_program.h @@ -6,7 +6,7 @@ #include "taichi/backends/vulkan/vulkan_device_creator.h" #include "taichi/backends/vulkan/vulkan_utils.h" #include "taichi/backends/vulkan/vulkan_loader.h" -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" #include "taichi/backends/vulkan/snode_tree_manager.h" #include "taichi/backends/vulkan/vulkan_device.h" #include "vk_mem_alloc.h" diff --git a/taichi/python/export_misc.cpp b/taichi/python/export_misc.cpp index e75df0b88d856..a1abb389ab782 100644 --- a/taichi/python/export_misc.cpp +++ b/taichi/python/export_misc.cpp @@ -5,7 +5,7 @@ #include "taichi/backends/metal/api.h" #include "taichi/backends/opengl/opengl_api.h" -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" #include "taichi/backends/dx/dx_api.h" #include "taichi/common/core.h" #include "taichi/common/interface.h" diff --git a/taichi/runtime/llvm/CMakeLists.txt b/taichi/runtime/llvm/CMakeLists.txt new file mode 100644 index 0000000000000..fdb5c723c2fb7 --- /dev/null +++ b/taichi/runtime/llvm/CMakeLists.txt @@ -0,0 +1,19 @@ +# ./taichi/runtime/llvm/CMakeLists.txt + +function(COMPILE_LLVM_RUNTIME rtm_arch) + message(STATUS "Compiling LLVM byte code file for arch ${rtm_arch}") + # Keep this for now, as .bc need to be generated. + add_custom_target( + "generate_llvm_runtime_${rtm_arch}" + COMMAND ${CLANG_EXECUTABLE} ${CLANG_OSX_FLAGS} -c runtime.cpp -o "runtime_${rtm_arch}.bc" -fno-exceptions -emit-llvm -std=c++17 -D "ARCH_${rtm_arch}" -I ${PROJECT_SOURCE_DIR}; + # TODO, it's better to avoid polluting the source dir, keep in build + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + ) + add_dependencies(${CORE_LIBRARY_NAME} "generate_llvm_runtime_${rtm_arch}") + install(FILES "${CMAKE_SOURCE_DIR}/taichi/runtime/llvm/runtime_${arch}.bc" DESTINATION ${CMAKE_INSTALL_PREFIX}/python/taichi/_lib/runtime) +endfunction() + +# Build llvm-runtime for host arch and cuda (if available) +foreach(arch IN LISTS HOST_ARCH CUDA_ARCH) + compile_llvm_runtime(${arch}) +endforeach() diff --git a/taichi/runtime/vulkan/CMakeLists.txt b/taichi/runtime/vulkan/CMakeLists.txt new file mode 100644 index 0000000000000..00ecee7a09caf --- /dev/null +++ b/taichi/runtime/vulkan/CMakeLists.txt @@ -0,0 +1,13 @@ +# ./taichi/runtime/vulkan/CMakeLists.txt + +add_library(vulkan_runtime) +target_sources(vulkan_runtime + PRIVATE + runtime.cpp + ) +target_include_directories(vulkan_runtime + PRIVATE + ${PROJECT_SOURCE_DIR}/external/SPIRV-Tools/include + ${PROJECT_SOURCE_DIR}/external/eigen + ${PROJECT_SOURCE_DIR}/external/FP16/include + ) diff --git a/taichi/backends/vulkan/runtime.cpp b/taichi/runtime/vulkan/runtime.cpp similarity index 99% rename from taichi/backends/vulkan/runtime.cpp rename to taichi/runtime/vulkan/runtime.cpp index 7da6b181719b6..82478b05bf427 100644 --- a/taichi/backends/vulkan/runtime.cpp +++ b/taichi/runtime/vulkan/runtime.cpp @@ -1,4 +1,4 @@ -#include "taichi/backends/vulkan/runtime.h" +#include "taichi/runtime/vulkan/runtime.h" #include "taichi/program/program.h" #include diff --git a/taichi/backends/vulkan/runtime.h b/taichi/runtime/vulkan/runtime.h similarity index 100% rename from taichi/backends/vulkan/runtime.h rename to taichi/runtime/vulkan/runtime.h