Skip to content

Commit f5bde39

Browse files
Make the project compatible with FetchContent and find_package (#200)
- Use the Gc prefix in the target names. - Added a separate shared library for oneDNN integration - GcDnnl. - The GcDnnl library and the OneDNNGraphDialect are not built if GC_ENABLE_DNNL=OFF. - Added new build options: GC_ENABLE_TEST(renamed), GC_ENABLE_TEST_DNNL, GC_ENABLE_TEST_MLIR, GC_ENABLE_OPT. - Renamed the GC_USE_GPU option to GC_ENABLE_GPU to be consistent with other options. - Export the OpenMP flags to resolve the "JIT session error: Symbols not found: __kmpc_...". - Minor compile.sh script enhancements.
1 parent de0376f commit f5bde39

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+480
-312
lines changed

CMakeLists.txt

Lines changed: 88 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,99 +18,111 @@
1818
cmake_minimum_required(VERSION 3.20)
1919
project(GraphCompiler VERSION "0.1.0" LANGUAGES C CXX)
2020

21-
set(CMAKE_CXX_STANDARD 17)
21+
############################# Cmake options ####################################
22+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2223
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2324
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
25+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
2426

25-
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
26-
27-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
27+
# Silence a false positive GCC -Wunused-but-set-parameter warning in constexpr
28+
# cases, by marking SelectedCase as used. See
29+
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827 for details. The issue is
30+
# fixed in GCC 10.
31+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "10.0")
32+
check_cxx_compiler_flag("-Wno-unused-but-set-parameter" CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER)
33+
append_if(CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER "-Wno-unused-but-set-parameter" CMAKE_CXX_FLAGS)
34+
endif()
35+
################################################################################
2836

29-
option(GC_LEGACY_ENABLE ON)
30-
option(GC_TEST_ENABLE "Build the tests" ON)
31-
option(GC_USE_GPU "Enable GPU backend" OFF)
37+
############################ Build options #####################################
38+
option(GC_ENABLE_LEGACY ON)
39+
option(GC_ENABLE_DNNL "Enable the oneDNN library integration" ON)
40+
option(GC_ENABLE_TEST "Build the tests" ON)
41+
option(GC_ENABLE_TEST_DNNL "Build the dnnl tests" ${GC_ENABLE_DNNL})
42+
option(GC_ENABLE_TEST_MLIR "Build the mlir tests" ON)
43+
option(GC_ENABLE_TOOLS "Build the tools" ON)
44+
option(GC_ENABLE_OPT "Build gc-opt" ${GC_ENABLE_TOOLS})
45+
option(GC_ENABLE_IMEX "Enable Intel® Extension for MLIR" OFF)
3246
option(GC_ENABLE_BINDINGS_PYTHON "Enable Graph Complier Python Binding" ON)
3347
option(GC_DEV_LINK_LLVM_DYLIB "Link dynamic libraries of LLVM and MLIR. For developers only. Do not use it in packing the library." OFF)
3448

35-
if(GC_LEGACY_ENABLE)
36-
add_subdirectory(legacy/core)
37-
endif()
38-
39-
find_package(MLIR REQUIRED CONFIG)
40-
41-
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
42-
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
43-
44-
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
45-
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
46-
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})
47-
48-
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
49-
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
50-
51-
include(TableGen)
52-
include(AddLLVM)
53-
include(AddMLIR)
54-
include(HandleLLVMOptions)
55-
56-
if(GC_USE_GPU)
57-
include(imex)
58-
if(GC_DEV_LINK_LLVM_DYLIB)
59-
message(WARN "GPU backend may not be compatible with dynamic linking to LLVM")
60-
endif()
61-
endif()
62-
63-
if(GC_ENABLE_BINDINGS_PYTHON AND NOT MLIR_ENABLE_BINDINGS_PYTHON)
64-
message(STATUS "Failed to enable Python API due to the 'MLIR_ENABLE_BINDINGS_PYTHON' for LLVM is not ON.")
65-
set(GC_ENABLE_BINDINGS_PYTHON OFF CACHE BOOL "" FORCE)
49+
if(GC_ENABLE_LEGACY)
50+
add_subdirectory(legacy/core)
6651
endif()
6752

68-
if(GC_ENABLE_BINDINGS_PYTHON)
69-
include(MLIRDetectPythonEnv)
70-
mlir_configure_python_dev_packages()
53+
if(GC_ENABLE_DNNL)
54+
set(GC_ONEDNN_DIALECT_LIB_NAME MLIROneDNNGraph)
7155
endif()
56+
################################################################################
7257

73-
include_directories(
74-
${LLVM_INCLUDE_DIRS}
75-
${MLIR_INCLUDE_DIRS}
76-
${PROJECT_BINARY_DIR}/include
77-
${PROJECT_SOURCE_DIR}/include
58+
############################## Targets #########################################
59+
# All common options, includes etc. are added to this interface target.
60+
add_library(GcInterface INTERFACE)
61+
target_compile_features(GcInterface INTERFACE cxx_std_17)
62+
target_include_directories(GcInterface INTERFACE
63+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
64+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
65+
$<INSTALL_INTERFACE:include>
7866
)
7967

80-
# The paths are added in the subfolders using the gc_add_path() function.
81-
# These lists are also used by tests.
82-
set(GC_LIB_SOURCES CACHE INTERNAL "The graph_compiler library source paths")
83-
set(GC_LIB_INCLUDES CACHE INTERNAL "The graph_compiler library include paths")
84-
85-
add_definitions(${LLVM_DEFINITIONS})
86-
87-
set(GC_MLIR_CXX_FLAGS "")
88-
# Silence a false positive GCC -Wunused-but-set-parameter warning in constexpr
89-
# cases, by marking SelectedCase as used. See
90-
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827 for details. The issue is
91-
# fixed in GCC 10.
92-
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "10.0")
93-
check_cxx_compiler_flag("-Wno-unused-but-set-parameter" CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER)
94-
append_if(CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER "-Wno-unused-but-set-parameter" GC_MLIR_CXX_FLAGS)
95-
endif()
96-
include("cmake/version.cmake")
68+
include(functions)
69+
include(version)
70+
include(mlir)
9771

9872
add_subdirectory(include)
9973
add_subdirectory(lib)
10074
add_subdirectory(src)
101-
if(GC_ENABLE_BINDINGS_PYTHON)
102-
message(STATUS "Enabling Python API")
103-
add_subdirectory(python)
104-
endif()
75+
add_subdirectory(python)
76+
add_subdirectory(test)
77+
################################################################################
10578

106-
set(GC_LIB_LINKED_LIBS
107-
GCJitWrapper
108-
GCCpuRuntime
79+
############################### Install ########################################
80+
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ TYPE INCLUDE)
81+
install(DIRECTORY ${PROJECT_BINARY_DIR}/include/ TYPE INCLUDE
82+
REGEX "CMake.*|.*cmake" EXCLUDE)
83+
84+
# Export the targets
85+
get_property(GC_TOOLS GLOBAL PROPERTY GC_TOOLS)
86+
get_property(GC_MLIR_LIBS GLOBAL PROPERTY GC_MLIR_LIBS)
87+
get_property(GC_PASS_LIBS GLOBAL PROPERTY GC_PASS_LIBS)
88+
get_property(GC_DIALECT_LIBS GLOBAL PROPERTY GC_DIALECT_LIBS)
89+
install(TARGETS
90+
GcInterface
91+
${GC_TOOLS}
92+
${GC_MLIR_LIBS}
93+
${GC_PASS_LIBS}
94+
${GC_DIALECT_LIBS}
95+
EXPORT ${PROJECT_NAME}Targets
96+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
97+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
98+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
99+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
100+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
101+
)
102+
export(EXPORT ${PROJECT_NAME}Targets
103+
FILE "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake"
104+
)
105+
install(EXPORT ${PROJECT_NAME}Targets
106+
FILE ${PROJECT_NAME}Targets.cmake
107+
DESTINATION lib/cmake/${PROJECT_NAME}
109108
)
110-
add_mlir_library(graph_compiler SHARED ${GC_LIB_SOURCES})
111-
target_include_directories(graph_compiler PUBLIC ${GC_LIB_INCLUDES})
112-
target_compile_options(graph_compiler PRIVATE -fvisibility=hidden -fexceptions)
113-
target_link_options(graph_compiler PRIVATE -Wl,--gc-sections)
114-
target_link_libraries(graph_compiler PRIVATE ${GC_LIB_LINKED_LIBS})
115109

116-
add_subdirectory(test)
110+
# Generate the config files
111+
include(CMakePackageConfigHelpers)
112+
configure_package_config_file(
113+
${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in
114+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
115+
INSTALL_DESTINATION "lib/cmake/${PROJECT_NAME}"
116+
NO_SET_AND_CHECK_MACRO
117+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
118+
)
119+
write_basic_package_version_file(
120+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
121+
COMPATIBILITY AnyNewerVersion
122+
)
123+
install(FILES
124+
${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
125+
${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
126+
DESTINATION "lib/cmake/${PROJECT_NAME}"
127+
)
128+
################################################################################

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ cmake --build build --target install
3333

3434
Notes
3535
* It is recommended to add optional options `-DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_LINK_LLVM_DYLIB=ON` to the command `cmake -G Ninja llvm ...` above **if you are building for CPU only**. These will enable the build of LLVM/MLIR dynamic libraries and let MLIR/LLVM tools link to them, to reduce the installed binary size of LLVM/MLIR. These options also enable the `GC_DEV_LINK_LLVM_DYLIB` option of graph-compiler repo (see below).
36-
* The option `-DLLVM_INSTALL_GTEST=ON` is optional, if the tests of graph-compiler are disabled (see `GC_TEST_ENABLE` below).
36+
* The option `-DLLVM_INSTALL_GTEST=ON` is optional, if the tests of graph-compiler are disabled (see `GC_ENABLE_TEST` below).
3737
* If you would like to enable GPU components of Graph Compiler, please make sure to statically link Graph Compiler and LLVM(MLIR). It is a known issue that LLVM shared library cannot be linked together with IGC (Intel's low level GPU compiler). Make sure `LLVM_BUILD_LLVM_DYLIB` and `LLVM_LINK_LLVM_DYLIB` are `OFF` (they are off by default). Also make sure Graph Compiler's cmake option `GC_DEV_LINK_LLVM_DYLIB` is `OFF` when configuring Graph Compiler (see below).
3838

3939
We have now installed LLVM at `llvm-project/llvm-install`.
@@ -59,7 +59,7 @@ Notes:
5959
* `/PATH/TO/llvm-project/llvm-install` should be the install path of LLVM. If you installed LLVM elsewhere by `-DCMAKE_INSTALL_PREFIX` option when building LLVM, you need to change the path in `-DMLIR_DIR` accordingly.
6060
* The cmake option `-DLLVM_EXTERNAL_LIT` is for the tests of this project. It requires the `lit` tool to be installed in the system. You can install it via `pip install lit`. If you don't need to run the tests of this repo, you can omit this option in the command line.
6161

62-
More notes if GPU components are on (`-DGC_USE_GPU=ON`):
62+
More notes if Intel® Extension for MLIR is on (`-DGC_ENABLE_IMEX=ON`):
6363
* make sure the OpenCL runtime is installed in your system. You can either
6464
install using OS-provided package (Ubuntu 22.04)
6565
```sh
@@ -70,11 +70,11 @@ sudo apt install -y intel-opencl-icd opencl-c-headers
7070

7171
Graph Compiler supports the following build-time options.
7272

73-
| CMake Option | Supported values (defaults in bold) | Description |
74-
|:--------------------------------|:---------------------------------------|:---------------------------------------------------------------------------------------|
75-
| GC_LEGACY_ENABLE | **ON**, OFF | Controls building the legacy graph-compiler component |
76-
| GC_TEST_ENABLE | **ON**, OFF | Controls building the tests |
77-
| GC_DEV_LINK_LLVM_DYLIB | ON, **OFF** | Controls dynamic link LLVM/MLIR libraries, mainly for developer |
78-
| GC_ENABLE_BINDINGS_PYTHON | **ON**, OFF | Controls building the Python API |
79-
| GC_USE_GPU | ON, **OFF** | Whether to enable the GPU components |
73+
| CMake Option | Supported values (defaults in bold) | Description |
74+
|:--------------------------|:---------------------------------------|:----------------------------------------------------------------|
75+
| GC_ENABLE_LEGACY | **ON**, OFF | Controls building the legacy graph-compiler component |
76+
| GC_ENABLE_TEST | **ON**, OFF | Controls building the tests |
77+
| GC_DEV_LINK_LLVM_DYLIB | ON, **OFF** | Controls dynamic link LLVM/MLIR libraries, mainly for developer |
78+
| GC_ENABLE_BINDINGS_PYTHON | **ON**, OFF | Controls building the Python API |
79+
| GC_ENABLE_IMEX | ON, **OFF** | Whether to enable the GPU components |
8080

cmake/Config.cmake.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@PACKAGE_INIT@
2+
3+
include ("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
4+
5+
find_package(MLIR REQUIRED CONFIG)
6+
7+
include_directories(
8+
${LLVM_INCLUDE_DIRS}
9+
${MLIR_INCLUDE_DIRS}
10+
)

cmake/functions.cmake

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@ function(gc_fetch_content
1818
# Optional arguments:
1919
# SKIP_ADD: Populate but do not add the content to the project.
2020
# SKIP_FIND: Do not use find_package().
21-
# CMAKE_ARGS: Passed to FetchContent_Declare.
21+
# SET: key=value variables to be set before the content population.
2222
)
2323
string(TOUPPER ${name} uname)
24-
cmake_parse_arguments(GC_${uname} "SKIP_ADD;SKIP_FIND" "" "CMAKE_ARGS" ${ARGN})
24+
cmake_parse_arguments(GC_${uname} "SKIP_ADD;SKIP_FIND" "" "SET" ${ARGN})
2525

26-
if (DEFINED GC_${uname}_CMAKE_ARGS)
27-
message(STATUS "${name}_CMAKE_ARGS: ${GC_${uname}_CMAKE_ARGS}")
26+
if (DEFINED GC_${uname}_SET)
27+
foreach (var ${GC_${uname}_SET})
28+
string(REGEX REPLACE "([^=]+)=(.*)" "\\1;\\2" var ${var})
29+
list(GET var 0 key)
30+
list(GET var 1 value)
31+
message(STATUS "Setting ${key}=${value}")
32+
set(${key} ${value})
33+
endforeach ()
2834
endif ()
2935

3036
if (DEFINED GC_${uname}_SRC_DIR)
3137
FetchContent_Declare(
3238
${name}
3339
SOURCE_DIR ${GC_${uname}_SRC_DIR}
34-
CMAKE_ARGS ${GC_${uname}_CMAKE_ARGS}
3540
)
3641
else ()
3742
if (DEFINED GC_${uname}_VERSION)
@@ -53,7 +58,6 @@ function(gc_fetch_content
5358
GIT_REPOSITORY ${git_repository}
5459
GIT_TAG ${git_tag_or_version}
5560
GIT_PROGRESS TRUE
56-
CMAKE_ARGS ${GC_${uname}_CMAKE_ARGS}
5761
FIND_PACKAGE_ARGS ${FIND_PACKAGE_ARGS}
5862
)
5963
endif ()
@@ -75,28 +79,6 @@ function(gc_fetch_content
7579
endif ()
7680
endfunction()
7781

78-
# Add one or multiple paths to the specified list.
79-
# The paths could be specified as a list of files or a GLOB pattern:
80-
# gc_add_path(SOURCES GLOB "src/*.cpp")
81-
# gc_add_path(INCLUDES include1 include2 include3)
82-
function(gc_add_path list_name paths)
83-
if (paths STREQUAL "GLOB")
84-
file(GLOB paths ${ARGN})
85-
list(APPEND ${list_name} ${paths})
86-
else ()
87-
get_filename_component(path ${paths} ABSOLUTE)
88-
list(APPEND ${list_name} ${path})
89-
foreach (path ${ARGN})
90-
get_filename_component(path ${path} ABSOLUTE)
91-
list(APPEND ${list_name} ${path})
92-
endforeach ()
93-
endif ()
94-
set(${list_name} ${${list_name}}
95-
CACHE INTERNAL "${list_name} paths"
96-
)
97-
endfunction()
98-
99-
10082
macro(gc_set_mlir_link_components VAR)
10183
if(GC_DEV_LINK_LLVM_DYLIB)
10284
set(${VAR}
@@ -108,3 +90,31 @@ macro(gc_set_mlir_link_components VAR)
10890
)
10991
endif()
11092
endmacro()
93+
94+
function(gc_add_mlir_library name)
95+
add_mlir_library(${ARGV})
96+
97+
if(name MATCHES ".+Passes")
98+
set_property(GLOBAL APPEND PROPERTY GC_PASS_LIBS ${name})
99+
else()
100+
set_property(GLOBAL APPEND PROPERTY GC_MLIR_LIBS ${name})
101+
endif()
102+
103+
if(GcInterface IN_LIST ARGN)
104+
if(SHARED IN_LIST ARGN)
105+
target_link_libraries(${name} PUBLIC GcInterface)
106+
else()
107+
target_link_libraries(obj.${name} PUBLIC GcInterface)
108+
endif()
109+
endif()
110+
endfunction()
111+
112+
function(gc_add_mlir_dialect_library name)
113+
add_mlir_dialect_library(${ARGV})
114+
target_link_libraries(obj.${name} PUBLIC GcInterface)
115+
set_property(GLOBAL APPEND PROPERTY GC_DIALECT_LIBS ${name})
116+
117+
if(GcInterface IN_LIST ARGN)
118+
target_link_libraries(obj.${name} PUBLIC GcInterface)
119+
endif()
120+
endfunction()

cmake/gtest.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
include_guard()
2-
include(functions)
32

43
gc_fetch_content(
5-
GTest
6-
v1.14.0
7-
https://github.com/google/googletest.git
4+
GTest
5+
v1.14.0
6+
https://github.com/google/googletest.git
7+
SET INSTALL_GTEST=OFF
88
)

cmake/imex.cmake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ include_guard()
22

33
get_property(IMEX_INCLUDES GLOBAL PROPERTY IMEX_INCLUDES)
44
if (NOT DEFINED IMEX_INCLUDES)
5-
include(functions)
6-
set(IMEX_CHECK_LLVM_VERSION ON)
7-
set(IMEX_ENABLE_L0_RUNTIME 0)
8-
# TODO: Change to main https://github.com/oneapi-src/oneDNN.git when all the
5+
if(GC_DEV_LINK_LLVM_DYLIB)
6+
message(WARN "GPU backend may not be compatible with dynamic linking to LLVM")
7+
endif()
8+
9+
# TODO: Change to main https://github.com/intel/mlir-extensions when all the
910
# required functionality is merged.
1011
gc_fetch_content(imex 496b240093b5e132b60c5ee69878300fe69be300 https://github.com/Menooker/mlir-extensions
11-
CMAKE_ARGS "-DMLIR_DIR=${MLIR_DIR};-DIMEX_CHECK_LLVM_VERSION=ON;-DIMEX_ENABLE_L0_RUNTIME=0"
12+
SET IMEX_CHECK_LLVM_VERSION=ON IMEX_ENABLE_L0_RUNTIME=0
1213
)
1314

1415
set(IMEX_INCLUDES

0 commit comments

Comments
 (0)