diff --git a/.gitignore b/.gitignore index 2684a546f..a2d365bce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Third party projects +third-party/level-zero/level-zero/ +third-party/mlir/llvm-project/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/CMakeLists.txt b/CMakeLists.txt index 808bcd35b..3ae88c884 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.5) +# Match requirement from LLVM/MLIR +cmake_minimum_required(VERSION 3.13.4) project(mlir-extensions) +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules/") set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -70,6 +72,43 @@ macro(apply_llvm_compile_flags target) target_compile_definitions(${target} PRIVATE ${LLVM_DEFINITIONS}) endmacro() +# Check if using external LLVM/MLIR install tree +if (DEFINED MLIR_DIR) + if (DEFINED MLIR_DIR) + set(MLIR_EXT_USE_LLVM_INSTALL_TREE ON) + else() + message(FATAL_ERROR "Need to define both LLVM_DIR and MLIR_DIR or none.") + endif() +elseif (DEFINED LLVM_DIR) + message(FATAL_ERROR "Need to define both LLVM_DIR and MLIR_DIR or none.") +else() + set(MLIR_EXT_USE_LLVM_INSTALL_TREE OFF) +endif() + +if(GPU_ENABLE) + # Check if using external level-zero + if(DEFINED LEVEL_ZERO_DIR) + set(MLIR_EXT_USE_EXT_LEVEL_ZERO ON) + else() + set(MLIR_EXT_USE_EXT_LEVEL_ZERO OFF) + endif() + message(STATUS "MLIR_EXT_USE_EXT_LEVEL_ZERO: ${MLIR_EXT_USE_EXT_LEVEL_ZERO}") +endif() + +add_subdirectory(third-party) + +if(NOT MLIR_EXT_USE_LLVM_INSTALL_TREE) + set(LLVM_DIR ${PROJECT_BINARY_DIR}/third-party/mlir/llvm-project/llvm/lib/cmake/llvm) + set(MLIR_DIR ${PROJECT_BINARY_DIR}/lib/cmake/mlir) +endif() + +if(GPU_ENABLE) + if(NOT MLIR_EXT_USE_EXT_LEVEL_ZERO) + set(LEVEL_ZERO_DIR ${PROJECT_BINARY_DIR}) + message(STATUS "LEVEL_ZERO_DIR set to: ${PROJECT_BINARY_DIR}") + endif() +endif() + if(GPU_ENABLE) add_subdirectory(dpcomp_gpu_runtime) endif() diff --git a/cmake/modules/update_level_zero.cmake b/cmake/modules/update_level_zero.cmake new file mode 100644 index 000000000..28cd91e07 --- /dev/null +++ b/cmake/modules/update_level_zero.cmake @@ -0,0 +1,49 @@ +#=============================================================================== +# Copyright 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#=============================================================================== + +set(LEVEL_ZERO_VERSION_FILE "${PROJECT_SOURCE_DIR}/level-zero-sha.txt") +file(READ "${LEVEL_ZERO_VERSION_FILE}" REVISION_FILE) +string(REGEX MATCH "([A-Za-z0-9]+)" _ ${REVISION_FILE}) +set(MLIR_EXT_LEVEL_ZERO_COMMIT_ID ${CMAKE_MATCH_1}) +message(STATUS "LEVEL_ZERO COMMIT ID: ${MLIR_EXT_LEVEL_ZERO_COMMIT_ID}") +set(MLIR_EXT_LEVEL_ZERO_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/level-zero") + +if (NOT EXISTS "${MLIR_EXT_LEVEL_ZERO_SOURCE_DIR}") + message(STATUS "Cloning LEVEL_ZERO git repo") + execute_process(COMMAND git clone https://github.com/oneapi-src/level-zero.git + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND_ECHO STDOUT + OUTPUT_VARIABLE LEVEL_ZERO_CLONE_OUTPUT + ERROR_VARIABLE LEVEL_ZERO_CLONE_ERROR + ) +else() + message(STATUS "LEVEL_ZERO git repo already cloned.") +endif() +execute_process(COMMAND git fetch --prune + WORKING_DIRECTORY ${MLIR_EXT_LEVEL_ZERO_SOURCE_DIR} + COMMAND_ECHO STDOUT + OUTPUT_VARIABLE LEVEL_ZERO_PULL_OUTPUT + ERROR_VARIABLE LEVEL_ZERO_PULL_ERROR + ) + +message(STATUS "LEVEL_ZERO: checkout ${MLIR_EXT_LEVEL_ZERO_COMMIT_ID}") + +execute_process(COMMAND git checkout ${MLIR_EXT_LEVEL_ZERO_COMMIT_ID} + WORKING_DIRECTORY ${MLIR_EXT_LEVEL_ZERO_SOURCE_DIR} + COMMAND_ECHO STDOUT + OUTPUT_VARIABLE LEVEL_ZERO_CHECKOUT_OUTPUT + ERROR_VARIABLE LEVEL_ZERO_CHECKOUT_ERROR + ) diff --git a/cmake/modules/update_mlir.cmake b/cmake/modules/update_mlir.cmake new file mode 100644 index 000000000..1ce12a3f6 --- /dev/null +++ b/cmake/modules/update_mlir.cmake @@ -0,0 +1,49 @@ +#=============================================================================== +# Copyright 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#=============================================================================== + +set(LLVM_VERSION_FILE "${PROJECT_SOURCE_DIR}/llvm-sha.txt") +file(READ "${LLVM_VERSION_FILE}" REVISION_FILE) +string(REGEX MATCH "([A-Za-z0-9]+)" _ ${REVISION_FILE}) +set(MLIR_EXT_LLVM_COMMIT_ID ${CMAKE_MATCH_1}) +message(STATUS "LLVM COMMIT ID: ${MLIR_EXT_LLVM_COMMIT_ID}") +set(MLIR_EXT_LLVM_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/llvm-project") + +if (NOT EXISTS "${MLIR_EXT_LLVM_SOURCE_DIR}") + message(STATUS "Cloning LLVM git repo") + execute_process(COMMAND git clone https://github.com/llvm/llvm-project.git + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND_ECHO STDOUT + OUTPUT_VARIABLE LLVM_CLONE_OUTPUT + ERROR_VARIABLE LLVM_CLONE_ERROR + ) +else() + message(STATUS "LLVM git repo already cloned.") +endif() +execute_process(COMMAND git fetch --prune + WORKING_DIRECTORY ${MLIR_EXT_LLVM_SOURCE_DIR} + COMMAND_ECHO STDOUT + OUTPUT_VARIABLE LLVM_PULL_OUTPUT + ERROR_VARIABLE LLVM_PULL_ERROR + ) + +message(STATUS "LLVM: checkout ${MLIR_EXT_LLVM_COMMIT_ID}") + +execute_process(COMMAND git checkout ${MLIR_EXT_LLVM_COMMIT_ID} + WORKING_DIRECTORY ${MLIR_EXT_LLVM_SOURCE_DIR} + COMMAND_ECHO STDOUT + OUTPUT_VARIABLE LLVM_CHECKOUT_OUTPUT + ERROR_VARIABLE LLVM_CHECKOUT_ERROR + ) diff --git a/level-zero-sha.txt b/level-zero-sha.txt new file mode 100644 index 000000000..ec03887f5 --- /dev/null +++ b/level-zero-sha.txt @@ -0,0 +1 @@ +bb7fff05b801e26c3d7858e03e509d1089914d59 diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt index 5a68e31aa..d905869e3 100644 --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -27,6 +27,7 @@ include(HandleLLVMOptions) file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/../llvm-sha.txt EXPECTED_LLVM_SHA) message(STATUS "Expected llvm sha: \"${EXPECTED_LLVM_SHA}\"") +if(MLIR_EXT_USE_LLVM_INSTALL_TREE) file(STRINGS ${LLVM_INCLUDE_DIR}/llvm/Support/VCSRevision.h REVISION_FILE_DATA) message(DEBUG "VCSRevision: ${REVISION_FILE_DATA}") string(REGEX MATCH "\"([^\"]*)\"" LLVM_SHA ${REVISION_FILE_DATA}) @@ -36,6 +37,7 @@ message(STATUS "llvm sha: \"${LLVM_SHA}\"") if (NOT EXPECTED_LLVM_SHA STREQUAL LLVM_SHA) message(FATAL_ERROR "Invalid llvm version") endif() +endif() add_subdirectory(include/mlir-extensions/dialect/plier) add_subdirectory(include/mlir-extensions/dialect/plier_util) @@ -143,6 +145,7 @@ target_link_libraries(${MLIR_EXTENSIONS_LIB} PRIVATE target_include_directories(${MLIR_EXTENSIONS_LIB} SYSTEM PRIVATE ${MLIR_INCLUDE_DIRS} + ${LLVM_INCLUDE_DIRS} PRIVATE ./lib ) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt new file mode 100644 index 000000000..3763577a0 --- /dev/null +++ b/third-party/CMakeLists.txt @@ -0,0 +1,20 @@ +#=============================================================================== +# Copyright 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#=============================================================================== + +# third party projects +message(STATUS "Configure third-party projects.") +add_subdirectory(mlir) +add_subdirectory(level-zero) diff --git a/third-party/README.md b/third-party/README.md new file mode 100644 index 000000000..e4ed2dde4 --- /dev/null +++ b/third-party/README.md @@ -0,0 +1,3 @@ +# third party repo sources. + +Source files for third party projects will be placed in this directory. diff --git a/third-party/level-zero/CMakeLists.txt b/third-party/level-zero/CMakeLists.txt new file mode 100644 index 000000000..eceed8b96 --- /dev/null +++ b/third-party/level-zero/CMakeLists.txt @@ -0,0 +1,28 @@ +#=============================================================================== +# Copyright 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#=============================================================================== + +if ((NOT GPU_ENABLE) OR MLIR_EXT_USE_EXT_LEVEL_ZERO) + return() +endif() + +# Update MLIR source +include(update_level_zero) +include(ExternalProject) +ExternalProject_Add(level-zero + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/level-zero + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR} + ) + diff --git a/third-party/mlir/CMakeLists.txt b/third-party/mlir/CMakeLists.txt new file mode 100644 index 000000000..a1568b2d3 --- /dev/null +++ b/third-party/mlir/CMakeLists.txt @@ -0,0 +1,40 @@ +#=============================================================================== +# Copyright 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#=============================================================================== + +if (MLIR_EXT_USE_LLVM_INSTALL_TREE) + message(STATUS "Using LLVM install tree.") + return() +endif() + +# Update MLIR source +include(update_mlir) + +# Add MLIR/LLVM +if (LINUX) + set(LLVM_ENABLE_LLD ON CACHE INTERNAL "") +endif() +set(LLVM_INSTALL_UTILS ON CACHE INTERNAL "") +set(LLVM_ENABLE_PROJECTS mlir CACHE INTERNAL "") +set(LLVM_TARGETS_TO_BUILD "host" CACHE INTERNAL "") +set(LLVM_INCLUDE_TOOLS ON CACHE INTERNAL "") +set(LLVM_BUILD_EXAMPLES ON CACHE INTERNAL "") +set(LLVM_ENABLE_ASSERTIONS ON CACHE INTERNAL "") +set(LLVM_ENABLE_RTTI ON CACHE INTERNAL "") +# ON by default +#set(LLVM_INCLUDE_TESTS ON CACHE INTERNAL "") +set(MLIR_INCLUDE_INTEGRATION_TESTS ON CACHE INTERNAL "") +add_subdirectory(llvm-project/llvm) + diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 34f0bd715..fdf03f4e9 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -13,4 +13,6 @@ # limitations under the License. add_subdirectory(dpcomp-opt) -add_subdirectory(FileCheck) +if (MLIR_EXT_USE_LLVM_INSTALL_TREE) + add_subdirectory(FileCheck) +endif()