Skip to content

Commit 879c46c

Browse files
authored
Fix IMEX config and installation issues. (#553)
* Fix IMEXConfig.cmake and IMEXTargets.cmake * Fix doxygen issue. * Use if(NOT DEFINED <VAR>) to check if a cmake variable <VAR> is defined. * Fix install issue. * Cleanup MLIR/LLVM header include paths. * Remove meaningless condition checks. * Fix imex-opt install issue. * Don't include mlir and llvm headers as SYSTEM. * Add an example of how to integrate IMEX into an external CMake based project.
1 parent 9b0d8ec commit 879c46c

File tree

8 files changed

+250
-58
lines changed

8 files changed

+250
-58
lines changed

CMakeLists.txt

Lines changed: 116 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ endif()
88

99
set(IMEX_EXTERNAL_PROJECT_BUILD OFF)
1010

11-
if(NOT (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) AND NOT MLIR_BINARY_DIR)
11+
if(NOT (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) AND NOT DEFINED MLIR_BINARY_DIR)
1212
# Building as part of LLVM via the external project mechanism.
1313
set(IMEX_EXTERNAL_PROJECT_BUILD ON)
1414
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
@@ -39,14 +39,21 @@ if(IMEX_EXTERNAL_PROJECT_BUILD)
3939
message(FATAL_ERROR "Invalid llvm version")
4040
endif()
4141

42+
# Variables needed for mlir_tablegen() to function
4243
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir )
4344
set(MLIR_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
45+
46+
set(LLVM_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/include )
47+
48+
# LLVM headers in source tree are already included
49+
# as part of LLVM_EXTERNAL_PROJECTS build
50+
51+
# MLIR headers in source tree
52+
include_directories(${MLIR_INCLUDE_DIR})
53+
# Generated MLIR headers
4454
set(MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)
45-
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
46-
include_directories(SYSTEM ${MLIR_GENERATED_INCLUDE_DIR})
47-
include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
55+
include_directories(${MLIR_GENERATED_INCLUDE_DIR})
4856

49-
set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}")
5057
list(APPEND CMAKE_MODULE_PATH "${MLIR_MAIN_SRC_DIR}/cmake/modules")
5158
else()
5259
message(STATUS "Building IMEX with external MLIR")
@@ -66,17 +73,82 @@ else()
6673
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
6774
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
6875

76+
# For redirecting llvm_add_library() to IMEX's bin and lib folders
6977
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
7078
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
71-
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})
79+
80+
# LLVM_INCLUDE_DIRS is the same as MLIR_INCLUDE_DIRS in an MLIR install tree
81+
include_directories(${MLIR_INCLUDE_DIRS})
7282

7383
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
7484
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
7585
endif()
7686

87+
# For redirecting add_mlir_doc() to IMEX's doc folder
88+
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})
89+
7790
include(TableGen)
7891
include(AddLLVM)
7992
include(AddMLIR)
93+
94+
# Instead of creating a separate AddIMEX,
95+
# redefine some functions included from include(AddMLIR)
96+
# and change the behavior of add_mlir_* functions to
97+
# properly generate IMEXConfig.cmake and IMEXTargets.cmake
98+
function(add_mlir_library_install name)
99+
get_target_export_arg(${name} IMEX export_to_mlirtargets UMBRELLA imex-libraries)
100+
install(TARGETS ${name}
101+
COMPONENT ${name}
102+
${export_to_mlirtargets}
103+
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
104+
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
105+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
106+
# Note that CMake will create a directory like:
107+
# objects-${CMAKE_BUILD_TYPE}/obj.LibName
108+
# and put object files there.
109+
OBJECTS DESTINATION lib${LLVM_LIBDIR_SUFFIX}
110+
)
111+
112+
add_llvm_install_targets(install-${name}
113+
DEPENDS ${name}
114+
COMPONENT ${name})
115+
set_property(GLOBAL APPEND PROPERTY IMEX_ALL_LIBS ${name})
116+
set_property(GLOBAL APPEND PROPERTY IMEX_EXPORTS ${name})
117+
endfunction()
118+
119+
# Declare the library associated with a dialect.
120+
function(add_mlir_dialect_library name)
121+
set_property(GLOBAL APPEND PROPERTY IMEX_DIALECT_LIBS ${name})
122+
add_mlir_library(${ARGV} DEPENDS mlir-headers)
123+
endfunction(add_mlir_dialect_library)
124+
125+
# Declare the library associated with a conversion.
126+
function(add_mlir_conversion_library name)
127+
set_property(GLOBAL APPEND PROPERTY IMEX_CONVERSION_LIBS ${name})
128+
add_mlir_library(${ARGV} DEPENDS mlir-headers)
129+
endfunction(add_mlir_conversion_library)
130+
131+
# Declare the library associated with a translation.
132+
function(add_mlir_translation_library name)
133+
set_property(GLOBAL APPEND PROPERTY IMEX_TRANSLATION_LIBS ${name})
134+
add_mlir_library(${ARGV} DEPENDS mlir-headers)
135+
endfunction(add_mlir_translation_library)
136+
137+
macro(add_imex_tool name)
138+
add_llvm_executable(${name} ${ARGN})
139+
get_target_export_arg(${name} IMEX export_to_imexexports)
140+
install(TARGETS ${name}
141+
${export_to_imexexports}
142+
RUNTIME DESTINATION ${IMEX_TOOLS_INSTALL_DIR}
143+
COMPONENT ${name})
144+
145+
add_llvm_install_targets(install-${name}
146+
DEPENDS ${name}
147+
COMPONENT ${name})
148+
set_property(GLOBAL APPEND PROPERTY IMEX_EXPORTS ${name})
149+
set_target_properties(${name} PROPERTIES FOLDER "Tools")
150+
endmacro()
151+
80152
include(HandleLLVMOptions)
81153

82154
set(IMEX_ENABLE_SYCL_RUNTIME 0 CACHE BOOL "Enable the Sycl Runtime")
@@ -110,11 +182,11 @@ if(NOT IMEX_EXTERNAL_PROJECT_BUILD)
110182
COMPONENTS Interpreter)
111183
endif()
112184

113-
include_directories(${LLVM_INCLUDE_DIRS})
114-
include_directories(${MLIR_INCLUDE_DIRS})
185+
# IMEX headers in source tree
115186
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
187+
# Generated IMEX headers
116188
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
117-
link_directories(${LLVM_BUILD_LIBRARY_DIR})
189+
message(STATUS "LLVM_DEFINITIONS: ${LLVM_DEFINITIONS}")
118190
add_definitions(${LLVM_DEFINITIONS})
119191

120192
set(LLVM_LIT_ARGS "-sv" CACHE STRING "lit default options")
@@ -128,6 +200,9 @@ else()
128200
set(IMEX_LIB_DIR ${IMEX_BINARY_DIR}/lib)
129201
endif()
130202

203+
set(IMEX_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING
204+
"Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
205+
131206
include(sanitizers)
132207

133208
add_subdirectory(include)
@@ -140,9 +215,14 @@ if (IMEX_INCLUDE_DOCS)
140215
add_subdirectory(docs)
141216
endif()
142217

143-
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/mlir
218+
# Custom target to install all imex headers
219+
add_custom_target(imex-headers)
220+
set_target_properties(imex-headers PROPERTIES FOLDER "Misc")
221+
222+
# Headers in source tree
223+
install(DIRECTORY include/imex
144224
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
145-
COMPONENT mlir-headers
225+
COMPONENT imex-headers
146226
FILES_MATCHING
147227
PATTERN "*.def"
148228
PATTERN "*.h"
@@ -151,9 +231,10 @@ install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/mlir
151231
PATTERN "LICENSE.TXT"
152232
)
153233

154-
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include
234+
# Generated headers
235+
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/imex
155236
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
156-
COMPONENT mlir-headers
237+
COMPONENT imex-headers
157238
FILES_MATCHING
158239
PATTERN "*.def"
159240
PATTERN "*.h"
@@ -164,4 +245,26 @@ install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include
164245
PATTERN "config.h" EXCLUDE
165246
)
166247

248+
add_llvm_install_targets(install-imex-headers
249+
DEPENDS imex-headers
250+
COMPONENT imex-headers)
251+
252+
253+
# Custom target to install all imex libraries
254+
add_custom_target(imex-libraries)
255+
set_target_properties(imex-libraries PROPERTIES FOLDER "Misc")
256+
257+
add_llvm_install_targets(install-imex-libraries
258+
DEPENDS imex-libraries
259+
COMPONENT imex-libraries)
260+
261+
get_property(IMEX_LIBS GLOBAL PROPERTY IMEX_ALL_LIBS)
262+
if(IMEX_LIBS)
263+
list(REMOVE_DUPLICATES IMEX_LIBS)
264+
foreach(lib ${IMEX_LIBS})
265+
add_dependencies(imex-libraries ${lib})
266+
add_dependencies(install-imex-libraries install-${lib})
267+
endforeach()
268+
endif()
269+
167270
add_subdirectory(cmake/modules)

cmake/modules/CMakeLists.txt

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,37 +73,6 @@ function(find_prefix_from_config out_var prefix_var path_to_leave)
7373
endfunction()
7474
endif() # if(NOT IMEX_EXTERNAL_PROJECT_BUILD)
7575

76-
# -----------------------------------------------------------------------------#
77-
# We do not want to recreate the whole MLIR build infrastructure. As such the
78-
79-
# helper function extracts the IMEX dialect, conversion, and transform libs from
80-
# the global MLIR properties.
81-
function(get_imex_libs lib_type)
82-
if(${lib_type} STREQUAL "ALL")
83-
get_property(LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
84-
set(target_property_name "IMEX_ALL_LIBS")
85-
elseif(${lib_type} STREQUAL "CONVERSION")
86-
get_property(LIBS GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
87-
set(target_property_name "IMEX_CONVERSION_LIBS")
88-
elseif(${lib_type} STREQUAL "DIALECT")
89-
get_property(LIBS GLOBAL PROPERTY MLIR_DIALECT_LIBS)
90-
set(target_property_name "IMEX_DIALECT_LIBS")
91-
elseif(${lib_type} STREQUAL "TRANSLATION")
92-
get_property(LIBS GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
93-
set(target_property_name "IMEX_TRANSLATION_LIBS")
94-
else()
95-
message(FATAL_ERROR "Unknown category of library type")
96-
endif()
97-
98-
foreach(LIB ${LIBS})
99-
string(FIND ${LIB} "IMEX" starts_with_imex)
100-
101-
if(starts_with_imex GREATER -1)
102-
set_property(GLOBAL APPEND PROPERTY ${target_property_name} ${LIB})
103-
endif()
104-
endforeach(LIB ${LIBS})
105-
endfunction(get_imex_libs)
106-
10776
# -----------------------------------------------------------------------------#
10877

10978
# Generate a list of CMake library targets so that other CMake projects can
@@ -112,15 +81,10 @@ endfunction(get_imex_libs)
11281
set(IMEX_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/imex)
11382
set(imex_cmake_builddir "${IMEX_BINARY_DIR}/${IMEX_INSTALL_PACKAGE_DIR}")
11483

84+
# Export build tree targets
11585
get_property(IMEX_EXPORTS GLOBAL PROPERTY IMEX_EXPORTS)
11686
export(TARGETS ${IMEX_EXPORTS} FILE ${imex_cmake_builddir}/IMEXTargets.cmake)
11787

118-
# Get the list of the IMEX libs
119-
get_imex_libs("ALL")
120-
get_imex_libs("CONVERSION")
121-
get_imex_libs("DIALECT")
122-
get_imex_libs("TRANSLATION")
123-
12488
get_property(IMEX_ALL_LIBS GLOBAL PROPERTY IMEX_ALL_LIBS)
12589
get_property(IMEX_DIALECT_LIBS GLOBAL PROPERTY IMEX_DIALECT_LIBS)
12690
get_property(IMEX_CONVERSION_LIBS GLOBAL PROPERTY IMEX_CONVERSION_LIBS)

cmake/modules/IMEXConfig.cmake.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
@IMEX_CONFIG_CODE@
44

5-
find_package(LLVM REQUIRED CONFIG)
5+
# MLIRConfig.cmake calls find_package(LLVM REQUIRED CONFIG)
6+
# so no need to call that here.
67
find_package(MLIR REQUIRED CONFIG)
78

89
set(IMEX_EXPORTED_TARGETS "@IMEX_EXPORTS@")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
add_mlir_dialect(GPUXOps gpux)
2-
add_mlir_doc(GPUXOps GPUXDialect Dialects/ -gen-dialect-doc)
2+
add_mlir_doc(GPUXOps GPUXDialect Dialects/ -gen-dialect-doc -dialect=gpux)

integration/example/CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
cmake_minimum_required(VERSION 3.13.4...3.18)
2+
3+
# CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()`
4+
# New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html
5+
if(POLICY CMP0116)
6+
cmake_policy(SET CMP0116 OLD)
7+
endif()
8+
9+
# Building standalone.
10+
project(imex-integration-example LANGUAGES CXX C)
11+
12+
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
13+
14+
find_package(IMEX REQUIRED CONFIG)
15+
16+
message(STATUS "Using IMEXConfig.cmake in: ${IMEX_DIR}")
17+
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
18+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
19+
20+
# For redirecting llvm_add_library() and others
21+
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
22+
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
23+
24+
# LLVM_INCLUDE_DIRS is the same as MLIR_INCLUDE_DIRS in an MLIR install tree
25+
include_directories(${MLIR_INCLUDE_DIRS})
26+
include_directories(${IMEX_INCLUDE_DIRS})
27+
28+
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
29+
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
30+
31+
include(TableGen)
32+
include(AddLLVM)
33+
include(AddMLIR)
34+
include(HandleLLVMOptions)
35+
36+
get_property(mlir_dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
37+
get_property(mlir_conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
38+
get_property(imex_dialect_libs GLOBAL PROPERTY IMEX_DIALECT_LIBS)
39+
get_property(imex_conversion_libs GLOBAL PROPERTY IMEX_CONVERSION_LIBS)
40+
set(LIBS
41+
${mlir_dialect_libs}
42+
${mlir_conversion_libs}
43+
${imex_dialect_libs}
44+
${imex_conversion_libs}
45+
MLIROptLib
46+
IMEXTransforms
47+
IMEXUtil
48+
)
49+
add_llvm_executable(sample-opt sample-opt.cpp)
50+
51+
llvm_update_compile_flags(sample-opt)
52+
target_link_libraries(sample-opt PRIVATE ${LIBS} IMEXTransforms)

integration/example/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2022 Intel Corporation
3+
4+
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
-->
6+
7+
# IMEX Integration Example
8+
9+
Example showing how to use IMEX as an external library from another CMake based project.
10+
11+
## Prestep for building
12+
13+
Build and install IMEX
14+
15+
## Building sample-opt
16+
```
17+
cmake -B build -S . -GNinja -DIMEX_DIR=<path to directory with IMEXConfig.cmake> -DMLIR_DIR=<path to directory with MLIRConfig.cmake>
18+
cmake --build build
19+
```
20+
You can find executable sample-opt at build/bin/sample-opt
21+
22+
## License
23+
This code is made available under the Apache License 2.0 with LLVM Exceptions.
24+
See the `LICENSE.txt` file for more details.

integration/example/sample-opt.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===- sample-opt.cpp ---------------------------------------------*- C++
2+
//-*-===//
3+
//
4+
// Copyright 2022 Intel Corporation
5+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This file defines the IMEX optimizer driver.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include <llvm/Support/CommandLine.h>
16+
#include <llvm/Support/InitLLVM.h>
17+
#include <llvm/Support/SourceMgr.h>
18+
#include <llvm/Support/ToolOutputFile.h>
19+
#include <mlir/IR/Dialect.h>
20+
#include <mlir/IR/MLIRContext.h>
21+
#include <mlir/InitAllDialects.h>
22+
#include <mlir/InitAllPasses.h>
23+
#include <mlir/Pass/Pass.h>
24+
#include <mlir/Pass/PassManager.h>
25+
#include <mlir/Support/FileUtilities.h>
26+
#include <mlir/Tools/mlir-opt/MlirOptMain.h>
27+
28+
#include <imex/InitIMEXDialects.h>
29+
#include <imex/InitIMEXPasses.h>
30+
#include <imex/Transforms/Passes.h>
31+
32+
int main(int argc, char **argv) {
33+
::mlir::registerAllPasses();
34+
::imex::registerAllPasses();
35+
36+
::mlir::DialectRegistry registry;
37+
::mlir::registerAllDialects(registry);
38+
::imex::registerAllDialects(registry);
39+
40+
return ::mlir::asMainReturnCode(::mlir::MlirOptMain(
41+
argc, argv,
42+
"IMEX integration example: out of tree IMEX optimizer driver\n",
43+
registry));
44+
}

0 commit comments

Comments
 (0)