forked from onnx/onnx-mlir
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MLIR.cmake
195 lines (165 loc) · 6.77 KB
/
MLIR.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# SPDX-License-Identifier: Apache-2.0
if (DEFINED ENV{MLIR_DIR})
set(MLIR_DIR $ENV{MLIR_DIR} CACHE PATH "Path to directory containing MLIRConfig.cmake")
elseif (NOT DEFINED MLIR_DIR)
message(FATAL_ERROR "MLIR_DIR is not configured but it is required. "
"Please set the env variable MLIR_DIR or the corresponding cmake configuration option.")
endif()
find_package(MLIR REQUIRED CONFIG)
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
set(BUILD_SHARED_LIBS ${LLVM_ENABLE_SHARED_LIBS} CACHE BOOL "" FORCE)
message(STATUS "BUILD_SHARED_LIBS : " ${BUILD_SHARED_LIBS})
# onnx uses exceptions, so we need to make sure that LLVM_REQUIRES_EH is set to ON, so that
# the functions from HandleLLVMOptions and AddLLVM don't disable exceptions.
set(LLVM_REQUIRES_EH ON)
message(STATUS "LLVM_REQUIRES_EH : " ${LLVM_REQUIRES_EH})
# LLVM_HOST_TRIPLE is exported as part of the llvm config, so we should be able to leverage it.
# If, for some reason, it is not set, default to an empty string which is the old default behavior of onnx-mlir.
set(ONNX_MLIR_DEFAULT_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING "Default triple for onnx-mlir.")
message(STATUS "ONNX_MLIR_DEFAULT_TRIPLE: " ${ONNX_MLIR_DEFAULT_TRIPLE})
# If CMAKE_INSTALL_PREFIX was not provided explicitly and we are not using an install of
# LLVM and a CMakeCache.txt exists,
# force CMAKE_INSTALL_PREFIX to be the same as the LLVM build.
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND NOT LLVM_INSTALL_PREFIX)
if (EXISTS ${LLVM_BINARY_DIR}/CMakeCache.txt)
file(STRINGS ${LLVM_BINARY_DIR}/CMakeCache.txt prefix REGEX CMAKE_INSTALL_PREFIX)
string(REGEX REPLACE "CMAKE_INSTALL_PREFIX:PATH=" "" prefix ${prefix})
string(REGEX REPLACE "//.*" "" prefix ${prefix})
set(CMAKE_INSTALL_PREFIX ${prefix} CACHE PATH "" FORCE)
endif()
endif()
message(STATUS "CMAKE_INSTALL_PREFIX : " ${CMAKE_INSTALL_PREFIX})
# The tablegen functions below are modeled based on the corresponding functions
# in mlir: https://github.com/llvm/llvm-project/blob/main/mlir/cmake/modules/AddMLIR.cmake
function(add_onnx_mlir_dialect_doc dialect dialect_tablegen_file)
# Generate Dialect Documentation
set(LLVM_TARGET_DEFINITIONS ${dialect_tablegen_file})
tablegen(MLIR ${dialect}.md -gen-op-doc "-I${ONNX_MLIR_SRC_ROOT}")
set(GEN_DOC_FILE ${ONNX_MLIR_SRC_ROOT}/docs/Dialects/${dialect}.md)
add_custom_command(
OUTPUT ${GEN_DOC_FILE}
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/${dialect}.md
${GEN_DOC_FILE}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${dialect}.md)
add_custom_target(${dialect}DocGen DEPENDS ${GEN_DOC_FILE})
add_dependencies(onnx-mlir-docs ${dialect}DocGen)
endfunction()
add_custom_target(onnx-mlir-docs)
function(add_onnx_mlir_dialect dialect)
set(LLVM_TARGET_DEFINITIONS ${dialect}.td)
mlir_tablegen(${dialect}.hpp.inc -gen-op-decls "-I${ONNX_MLIR_SRC_ROOT}")
mlir_tablegen(${dialect}.cpp.inc -gen-op-defs "-I${ONNX_MLIR_SRC_ROOT}")
add_public_tablegen_target(OM${dialect}IncGen)
endfunction()
function(add_onnx_mlir_rewriter rewriter)
set(LLVM_TARGET_DEFINITIONS ${rewriter}.td)
mlir_tablegen(ONNX${rewriter}.inc -gen-rewriters "-I${ONNX_MLIR_SRC_ROOT}")
add_public_tablegen_target(OMONNX${rewriter}IncGen)
endfunction()
function(add_onnx_mlir_interface interface)
set(LLVM_TARGET_DEFINITIONS ${interface}.td)
mlir_tablegen(${interface}.hpp.inc -gen-op-interface-decls)
mlir_tablegen(${interface}.cpp.inc -gen-op-interface-defs)
add_public_tablegen_target(OM${interface}IncGen)
endfunction()
# add_onnx_mlir_library(name sources...
# This function (generally) has the same semantic as add_library. In
# addition is supports the arguments below and it does the following
# by default (unless an argument overrides this):
# 1. Add the library
# 2. Add the default target_include_directories
# 3. Add the library to a global property ONNX_MLIR_LIBS
# 4. Add an install target for the library
# EXCLUDE_FROM_OM_LIBS
# Do not add the library to the ONNX_MLIR_LIBS property.
# NO_INSTALL
# Do not add an install target for the library.
# DEPENDS targets...
# Same semantics as add_dependencies().
# INCLUDE_DIRS include_dirs...
# Same semantics as target_include_directories().
# LINK_LIBS lib_targets...
# Same semantics as target_link_libraries().
# )
function(add_onnx_mlir_library name)
cmake_parse_arguments(ARG
"EXCLUDE_FROM_OM_LIBS;NO_INSTALL"
""
"DEPENDS;INCLUDE_DIRS;LINK_LIBS"
${ARGN}
)
if (NOT ARG_EXCLUDE_FROM_OM_LIBS)
set_property(GLOBAL APPEND PROPERTY ONNX_MLIR_LIBS ${name})
endif()
add_library(${name} ${ARG_UNPARSED_ARGUMENTS})
llvm_update_compile_flags(${name})
if (ARG_DEPENDS)
add_dependencies(${name} ${ARG_DEPENDS})
endif()
if (ARG_INCLUDE_DIRS)
target_include_directories(${name} ${ARG_INCLUDE_DIRS})
endif()
target_include_directories(${name}
PUBLIC
${ONNX_MLIR_SRC_ROOT}
${ONNX_MLIR_BIN_ROOT}
)
if (ARG_LINK_LIBS)
target_link_libraries(${name} ${ARG_LINK_LIBS})
endif()
if (NOT ARG_NO_INSTALL)
install(TARGETS ${name}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
endif()
endfunction(add_onnx_mlir_library)
# add_onnx_mlir_executable(name sources...
# This function (generally) has the same semantic as add_executable.
# In addition is supports the arguments below and it does the following
# by default (unless an argument overrides this):
# 1. Add the executable
# 2. Add an install target for the executable
# NO_INSTALL
# Do not add an install target for the executable.
# DEPENDS targets...
# Same semantics as add_dependencies().
# INCLUDE_DIRS include_dirs...
# Same semantics as target_include_directories().
# LINK_LIBS lib_targets...
# Same semantics as target_link_libraries().
# )
function(add_onnx_mlir_executable name)
cmake_parse_arguments(ARG
"NO_INSTALL"
""
"DEPENDS;INCLUDE_DIRS;LINK_LIBS"
${ARGN}
)
add_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
llvm_update_compile_flags(${name})
if (ARG_DEPENDS)
add_dependencies(${name} ${ARG_DEPENDS})
endif()
if (ARG_INCLUDE_DIRS)
target_include_directories(${name} ${ARG_INCLUDE_DIRS})
endif()
if (ARG_LINK_LIBS)
target_link_libraries(${name} ${ARG_LINK_LIBS})
endif()
if (NOT ARG_NO_INSTALL)
install(TARGETS ${name} DESTINATION bin)
endif()
endfunction(add_onnx_mlir_executable)