Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge] Initial support for auto updating and building dependent third party projects #211

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
41 changes: 40 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really not need two flags. If someone is using an LLVM build without building MLIR project, CMake will throw an error and we should at that point inform user what to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I am not understanding it right. What does MLIR_DIR point to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MLIR_DIR is defined to guide "find_package(MLIR)" work correctly in CMake.
LLVM_DIR is for "find_package(LLVM)"
Both were already used in the code base. Not introduced by this PR.
Actually find_package(MLIR) will call find_package(LLVM) internally. So we can clean it up but that requires more changes.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us move to IMEX as the prefix of all our CMake flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like checking out third party repos in IMEX repo. The problem is if you use git clean -dfx the folder will be wiped out. In my Python build_locally.py, I allow users to set a WORKING_DIR or use system temp directory. Let us at least have the option of providing a -DCMAKE_THIRD_PARTY_LIB_PATH` or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR does not remove the option of providing -DCMAKE_THIRD_PARTY_LIB_PATH. It only checks out third party repo if -DCMAKE_THIRD_PARTY_LIB_PATH is not provided.

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()
Expand Down
49 changes: 49 additions & 0 deletions cmake/modules/update_level_zero.cmake
Original file line number Diff line number Diff line change
@@ -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
)
49 changes: 49 additions & 0 deletions cmake/modules/update_mlir.cmake
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions level-zero-sha.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bb7fff05b801e26c3d7858e03e509d1089914d59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps bind to a released tag for L0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That was the intention but did not have time to update tag parsing routine. I already had a SHA parse code so just used a SHA instead of tag for now. The SHA points to tag v1.5.0

3 changes: 3 additions & 0 deletions mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down
20 changes: 20 additions & 0 deletions third-party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions third-party/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# third party repo sources.

Source files for third party projects will be placed in this directory.
28 changes: 28 additions & 0 deletions third-party/level-zero/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)

40 changes: 40 additions & 0 deletions third-party/mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

4 changes: 3 additions & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()