blob: 0aaf14258a92fbca0a8a94c4b1a270d1d4a735b4 [file] [log] [blame]
Ben Vanik185d30c2019-09-19 14:24:11 -07001# Copyright 2019 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Scott Toddee30e1b2020-02-03 16:44:15 -080015cmake_minimum_required(VERSION 3.13)
Geoffrey Martin-Noble98bbd962019-11-21 07:27:39 -080016if(POLICY CMP0077)
17 cmake_policy(SET CMP0077 NEW)
18endif()
Scott Toddee30e1b2020-02-03 16:44:15 -080019# Allow target_link_libraries() from other directories (since 3.13):
20# https://cmake.org/cmake/help/v3.13/policy/CMP0079.html
21if(POLICY CMP0079)
22 cmake_policy(SET CMP0079 NEW)
23endif()
Ben Vanik512d2d32019-09-20 13:22:34 -070024set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Ben Vanik185d30c2019-09-19 14:24:11 -070025
Marius Brehler178c82a2019-12-27 15:00:04 -080026project(iree CXX C)
Lei Zhang6c5907b2020-06-02 09:06:08 -070027set(CMAKE_C_STANDARD 11)
28set(CMAKE_CXX_STANDARD 14)
Ben Vanik185d30c2019-09-19 14:24:11 -070029set(IREE_IDE_FOLDER IREE)
30set_property(GLOBAL PROPERTY USE_FOLDERS ON)
31
Lei Zhang7e253da2020-06-10 07:51:19 -070032#-------------------------------------------------------------------------------
33# Project component configuration
34#-------------------------------------------------------------------------------
35
Lei Zhange9967992020-06-03 11:13:00 -040036# LINT.IfChange(iree_options)
Ben Vanik56d44bf2020-05-15 13:20:50 -070037option(IREE_ENABLE_RUNTIME_TRACING "Enables instrumented runtime tracing." OFF)
Stella Laurenzo382122d2020-06-11 16:18:09 -070038option(IREE_ENABLE_MLIR "Enables MLIR/LLVM dependencies." ON)
Marius Brehler4e45a772020-06-02 14:04:18 -070039option(IREE_ENABLE_EMITC "Enables MLIR EmitC dependencies." OFF)
Ben Vanik512d2d32019-09-20 13:22:34 -070040
Marius Brehleredfc57f2019-12-18 11:19:38 -080041option(IREE_BUILD_COMPILER "Builds the IREE compiler." ON)
Ben Vanik512d2d32019-09-20 13:22:34 -070042option(IREE_BUILD_TESTS "Builds IREE unit tests." ON)
Lei Zhang22f0e242020-03-30 12:09:20 -070043option(IREE_BUILD_DOCS "Builds IREE docs." OFF)
Ben Vanik6b112ef2019-10-03 10:45:14 -070044option(IREE_BUILD_SAMPLES "Builds IREE sample projects." ON)
Ben Vanik512d2d32019-09-20 13:22:34 -070045option(IREE_BUILD_DEBUGGER "Builds the IREE debugger app." OFF)
Jenni Kilduff0d09cf32020-10-19 10:23:34 -070046option(IREE_BUILD_JAVA_BINDINGS "Builds the IREE java bindings." OFF)
Ben Vanikb64e9182020-01-30 15:19:37 -080047option(IREE_BUILD_EXPERIMENTAL "Builds experimental projects." OFF)
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080048option(IREE_BUILD_TENSORFLOW_COMPILER "Builds TensorFlow compiler frontend." OFF)
49option(IREE_BUILD_XLA_COMPILER "Builds TensorFlow XLA compiler frontend." OFF)
Ben Vanik512d2d32019-09-20 13:22:34 -070050
Marius Brehler0a4b67f2020-05-08 13:19:16 -070051set(IREE_HAL_DRIVERS_TO_BUILD "all"
Scott Todd6132bb32020-08-11 12:04:40 -070052 CACHE STRING "Semicolon-separated list of HAL drivers to build, or \"all\".")
Marius Brehler0a4b67f2020-05-08 13:19:16 -070053set(IREE_TARGET_BACKENDS_TO_BUILD "all"
Scott Todd6132bb32020-08-11 12:04:40 -070054 CACHE STRING "Semicolon-separated list of target backends to build, or \"all\".")
Stella Laurenzo04b7c372020-12-10 00:05:31 +000055
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080056# Master enable for tensorflow build support.
57# Note that this is a normal CMake variable used to gate build features (not
58# a cache variable that is user-settable).
59set(IREE_ENABLE_TENSORFLOW OFF)
60if(${IREE_BUILD_TENSORFLOW_COMPILER} OR ${IREE_BUILD_XLA_COMPILER})
61 set(IREE_ENABLE_TENSORFLOW ON)
62endif()
63
Stella Laurenzo04b7c372020-12-10 00:05:31 +000064# Default python bindings to enabled for some features.
Stella Laurenzoa4137ef2020-12-09 21:28:08 -080065if(${IREE_ENABLE_TENSORFLOW})
Stella Laurenzo04b7c372020-12-10 00:05:31 +000066 option(IREE_BUILD_PYTHON_BINDINGS "Builds the IREE python bindings" ON)
67else()
68 option(IREE_BUILD_PYTHON_BINDINGS "Builds the IREE python bindings" OFF)
69endif()
70
Lei Zhange71d9732020-06-23 10:21:09 -040071# LINT.ThenChange(
Scott Todd4e5167d2020-06-29 09:30:35 -070072# https://github.com/google/iree/tree/main/build_tools/cmake/iree_cross_compile.cmake:iree_cross_compile_options,
73# https://github.com/google/iree/tree/main/build_tools/cmake/iree_cross_compile.cmake:iree_cross_compile_invoke
Lei Zhange71d9732020-06-23 10:21:09 -040074# )
Marius Brehler0a4b67f2020-05-08 13:19:16 -070075
Ben Vanikb64e9182020-01-30 15:19:37 -080076if(${IREE_BUILD_SAMPLES} OR ${IREE_BUILD_EXPERIMENTAL})
Marius Brehleredfc57f2019-12-18 11:19:38 -080077 set(IREE_BUILD_COMPILER ON CACHE BOOL "Build the IREE compiler for sample projects." FORCE)
78endif()
79
Stella Laurenzo06034152020-01-03 11:26:31 -080080if(${IREE_BUILD_COMPILER})
Stella Laurenzo382122d2020-06-11 16:18:09 -070081 set(IREE_ENABLE_MLIR ON CACHE BOOL "Enable LLVM dependencies if the IREE compiler is build." FORCE)
Stella Laurenzo06034152020-01-03 11:26:31 -080082endif()
83
Ben Vanik85cdd862020-11-14 11:26:12 -080084if(${IREE_ENABLE_MLIR})
Stella Laurenzo382122d2020-06-11 16:18:09 -070085 set(IREE_MLIR_DEP_MODE "BUNDLED" CACHE STRING "One of BUNDLED (default), DISABLED, INSTALLED")
Marius Brehler4e45a772020-06-02 14:04:18 -070086endif()
87
Marius Brehlerfd8be7f2020-06-18 15:59:28 -070088if(${IREE_ENABLE_EMITC})
89 if(NOT ${IREE_ENABLE_MLIR})
90 message(FATAL_ERROR "Enabling EmitC requires setting IREE_ENABLE_MLIR to ON.")
91 endif()
92
93 string(TOUPPER "${IREE_MLIR_DEP_MODE}" uppercase_IREE_MLIR_DEP_MODE)
94 if(NOT uppercase_IREE_MLIR_DEP_MODE MATCHES "^(BUNDLED|INSTALLED)$")
95 message(FATAL_ERROR "Enabling EmitC requires IREE_MLIR_DEP_MODE set to BUNDELED or INSTALLED.")
96 endif()
97endif()
98
Ben Vanik185d30c2019-09-19 14:24:11 -070099#-------------------------------------------------------------------------------
Lei Zhang8bea2e82020-06-04 10:17:56 -0400100# Target and backend configuration
Ben Vanik185d30c2019-09-19 14:24:11 -0700101#-------------------------------------------------------------------------------
102
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700103# List of all HAL drivers to be built by default:
104set(IREE_ALL_HAL_DRIVERS
Scott Todd217904d2020-06-04 13:38:32 -0700105 DyLib
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700106 LLVM
Lei Zhang6171a982020-09-08 12:48:25 -0400107 Metal
Geoffrey Martin-Nobledfb76332020-08-07 11:22:39 -0700108 VMLA
Scott Todd6132bb32020-08-11 12:04:40 -0700109 Vulkan
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700110)
111
Ben Vanik85cdd862020-11-14 11:26:12 -0800112if(IREE_HAL_DRIVERS_TO_BUILD STREQUAL "all")
113 set(IREE_HAL_DRIVERS_TO_BUILD ${IREE_ALL_HAL_DRIVERS})
Ben Vanik01353432020-11-17 03:15:40 -0800114 # For cross compilation towards Android, we don't want LLVM JIT HAL driver.
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400115 if(ANDROID)
Han-Chung Wang8cc22a32020-10-20 08:45:33 -0700116 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD LLVM)
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400117 endif()
Lei Zhang6171a982020-09-08 12:48:25 -0400118
119 # For Apple platforms we need to use Metal instead of Vulkan.
120 if(APPLE)
121 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Vulkan)
122 else()
123 # And Metal isn't available on non-Apple platforms for sure.
124 list(REMOVE_ITEM IREE_HAL_DRIVERS_TO_BUILD Metal)
125 endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700126endif()
Scott Todd6132bb32020-08-11 12:04:40 -0700127message(STATUS "Building HAL drivers: ${IREE_HAL_DRIVERS_TO_BUILD}")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700128
129# Default every IREE_HAL_DRIVER_* to OFF
130foreach(_backend ${IREE_ALL_HAL_DRIVERS})
131 string(TOUPPER "${_backend}" uppercase_backend)
132 set(IREE_HAL_DRIVER_${uppercase_backend} OFF CACHE BOOL "" FORCE)
133endforeach()
134
135# Set IREE_HAL_DRIVER_* based on configuration
136foreach(_backend ${IREE_HAL_DRIVERS_TO_BUILD})
137 string(TOUPPER "${_backend}" uppercase_backend)
138 set(IREE_HAL_DRIVER_${uppercase_backend} ON CACHE BOOL "" FORCE)
139endforeach()
140
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700141# List of all target backends to be built by default:
142set(IREE_ALL_TARGET_BACKENDS
Scott Toddaaa08b12020-12-09 11:00:11 -0800143 DYLIB-LLVM-AOT
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400144 LLVM-IR
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400145 Metal-SPIRV
Lei Zhang7d90e0e2020-07-23 16:01:13 -0400146 Vulkan-SPIRV
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700147 VMLA
148)
149
150if( IREE_TARGET_BACKENDS_TO_BUILD STREQUAL "all" )
151 set( IREE_TARGET_BACKENDS_TO_BUILD ${IREE_ALL_TARGET_BACKENDS} )
152endif()
Scott Todd6132bb32020-08-11 12:04:40 -0700153message(STATUS "Building target backends: ${IREE_TARGET_BACKENDS_TO_BUILD}")
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700154
155# Default every IREE_TARGET_BACKEND_* to OFF
156foreach(_backend ${IREE_ALL_TARGET_BACKENDS})
157 string(TOUPPER "${_backend}" uppercase_backend)
158 set(IREE_TARGET_BACKEND_${uppercase_backend} OFF CACHE BOOL "" FORCE)
159endforeach()
160
161# Set IREE_TARGET_BACKEND_* based on configuration
162foreach(_backend ${IREE_TARGET_BACKENDS_TO_BUILD})
163 string(TOUPPER "${_backend}" uppercase_backend)
164 set(IREE_TARGET_BACKEND_${uppercase_backend} ON CACHE BOOL "" FORCE)
165endforeach()
166
Ben Vanik185d30c2019-09-19 14:24:11 -0700167list(APPEND CMAKE_MODULE_PATH
168 ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800169 ${CMAKE_CURRENT_LIST_DIR}/bindings/python/build_tools/cmake/
Ben Vanik185d30c2019-09-19 14:24:11 -0700170 ${CMAKE_CURRENT_LIST_DIR}/third_party/abseil-cpp/absl/copts/
171)
Ben Vanik512d2d32019-09-20 13:22:34 -0700172
Lei Zhang0d281b72020-06-01 20:00:23 -0400173#-------------------------------------------------------------------------------
174# Cross compiling configuration
175#-------------------------------------------------------------------------------
176
177if(CMAKE_CROSSCOMPILING)
Lei Zhang45695f92020-06-16 19:36:41 -0400178 message(STATUS "Detected cross compilation mode; configuring IREE on host...")
Lei Zhang0d281b72020-06-01 20:00:23 -0400179
180 # C/C++ compilers for host compilation.
181 # Note: we need to explicitly set this because IREE does not work well with
182 # GCC at the moment: https://github.com/google/iree/issues/1269
183 set(IREE_HOST_C_COMPILER "$ENV{IREE_HOST_C_COMPILER}" CACHE FILEPATH "C compiler for host compilation")
184 set(IREE_HOST_CXX_COMPILER "$ENV{IREE_HOST_CXX_COMPILER}" CACHE FILEPATH "C++ compiler for host compilation")
185
186 # Master configuration for the binary directory containing all artifacts
187 # compiled for host.
Lei Zhange71d9732020-06-23 10:21:09 -0400188 if(NOT IREE_HOST_BINARY_ROOT)
189 set(IREE_HOST_BINARY_ROOT "${CMAKE_CURRENT_BINARY_DIR}/host" CACHE FILEPATH "directory containing host artifacts")
190 endif()
Lei Zhang0d281b72020-06-01 20:00:23 -0400191
192 set(IREE_HOST_BUILD_COMPILER ON) # For iree-translate
193 set(IREE_HOST_ENABLE_LLVM ON) # For iree-tblgen
194
195 # Set the host build directory for LLVM to our directory. Otherwise it will
196 # follow its own convention.
Lei Zhang7f6dfbf2020-06-30 14:31:35 -0400197 set(LLVM_NATIVE_BUILD
198 "${IREE_HOST_BINARY_ROOT}/third_party/llvm-project/llvm"
199 CACHE FILEPATH "directory containing host artifacts for LLVM"
200 )
201
202 # And set host C/C++ compiler for LLVM. This makes cross compilation using
203 # Windows as the host platform nicer. Because we have various development
204 # evnironments on Windows (CMD, Cygwin, MSYS, etc.), LLVM can have problems
205 # figuring out the host triple and toolchain. We are passing in the host
206 # C/C++ compiler toolchain for IREE anyway; so we can give LLVM side some
207 # help here. This hides some complexity and ugliness from the users.
208 set(CROSS_TOOLCHAIN_FLAGS_NATIVE
209 "-DCMAKE_C_COMPILER=\"${IREE_HOST_C_COMPILER}\";-DCMAKE_CXX_COMPILER=\"${IREE_HOST_CXX_COMPILER}\""
210 CACHE FILEPATH "LLVM toolchain configuration for host build"
211 )
Lei Zhang0d281b72020-06-01 20:00:23 -0400212
213 include(iree_cross_compile)
214
215 # Use another CMake invocation to configure a build for host.
216 iree_create_configuration(HOST)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400217
Lei Zhang45695f92020-06-16 19:36:41 -0400218 message(STATUS "Done configuring IREE on host in ${IREE_HOST_BINARY_ROOT}")
Lei Zhang0d281b72020-06-01 20:00:23 -0400219endif()
220
221#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400222# IREE compilation toolchain configuration
223#-------------------------------------------------------------------------------
224
225# Enable using lld as the linker for C/C++ targets. This affects IREE and all
226# dependency projects.
227option(IREE_ENABLE_LLD "Use lld when linking" OFF)
228option(IREE_ENABLE_ASAN "Enable address sanitizer" OFF)
229option(IREE_ENABLE_MSAN "Enable memory sanitizer" OFF)
230option(IREE_ENABLE_TSAN "Enable thread sanitizer" OFF)
bjacobe694d952020-11-03 12:05:35 -0500231option(IREE_ENABLE_CCACHE "Use ccache if installed to speed up rebuilds." OFF)
232
Ben Vanik85cdd862020-11-14 11:26:12 -0800233if(${IREE_ENABLE_CCACHE})
bjacobe694d952020-11-03 12:05:35 -0500234 find_program(CCACHE_PROGRAM ccache)
235 if(CCACHE_PROGRAM)
236 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
237 endif()
238endif()
Lei Zhange88470f2020-09-08 13:21:09 -0400239
240#-------------------------------------------------------------------------------
Lei Zhang7e253da2020-06-10 07:51:19 -0700241# IREE utility definitions
Lei Zhang0d281b72020-06-01 20:00:23 -0400242#-------------------------------------------------------------------------------
243
Ben Vanik512d2d32019-09-20 13:22:34 -0700244include(iree_macros)
Ben Vanik185d30c2019-09-19 14:24:11 -0700245include(iree_copts)
bjacobc12ba3b2020-11-03 10:32:00 -0500246include(sanitizers)
Ben Vanik6b112ef2019-10-03 10:45:14 -0700247include(iree_cc_binary)
Ben Vanik185d30c2019-09-19 14:24:11 -0700248include(iree_cc_library)
249include(iree_cc_test)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700250include(iree_tablegen_library)
Lei Zhang22f0e242020-03-30 12:09:20 -0700251include(iree_tablegen_doc)
Scott Todd11adcab2019-12-18 14:10:44 -0800252include(iree_cc_embed_data)
253include(iree_bytecode_module)
Stella Laurenzo66578b02020-07-20 17:34:44 -0700254include(iree_multipy)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800255include(iree_lit_test)
Geoffrey Martin-Noble4526dcc2020-03-09 11:59:52 -0700256include(iree_add_all_subdirs)
Geoffrey Martin-Noblee5fd5b52020-03-31 11:31:30 -0700257include(iree_check_test)
Ben Vanik185d30c2019-09-19 14:24:11 -0700258
Marius Brehlerc4b6b912020-01-15 08:44:23 -0800259set(DEFAULT_CMAKE_BUILD_TYPE "Release")
Ben Vanik85cdd862020-11-14 11:26:12 -0800260if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Marius Brehlerc4b6b912020-01-15 08:44:23 -0800261 message(STATUS "No build type selected, default to ${DEFAULT_CMAKE_BUILD_TYPE}")
262 set(CMAKE_BUILD_TYPE "${DEFAULT_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
263endif()
264
Marius Brehler06ac36e2020-01-10 14:44:11 -0800265set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
266
Ben Vanik185d30c2019-09-19 14:24:11 -0700267#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400268# IREE compilation flags
Scott Todd29d654e2020-06-11 15:24:17 -0700269#-------------------------------------------------------------------------------
270
Lei Zhangdd21f322020-09-10 10:47:33 -0400271iree_append_list_to_string(CMAKE_C_FLAGS_DEBUG ${IREE_C_FLAGS_DEBUG_LIST})
272iree_append_list_to_string(CMAKE_CXX_FLAGS_DEBUG ${IREE_CXX_FLAGS_DEBUG_LIST})
Scott Todd29d654e2020-06-11 15:24:17 -0700273
274set(CMAKE_CXX_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C++ compiler during fast builds." FORCE)
275set(CMAKE_C_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C compiler during fast builds." FORCE)
276set(CMAKE_EXE_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used for linking binaries during fast builds." FORCE)
277set(CMAKE_SHARED_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used by the shared libraries linker binaries during fast builds." FORCE)
278mark_as_advanced(
279 CMAKE_CXX_FLAGS_FASTBUILD
280 CMAKE_C_FLAGS_FASTBUILD
281 CMAKE_EXE_LINKER_FLAGS_FASTBUILD
282 CMAKE_SHARED_LINKER_FLAGS_FASTBUILD
283)
284
285include(iree_setup_toolchain)
286
287#-------------------------------------------------------------------------------
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800288# Configure python early if there are any features that need it.
289# Note that doing this early ensures that dependencies that make incidental
290# use of Python (such as LLVM) resolve the same version.
291#-------------------------------------------------------------------------------
292
293if(${IREE_BUILD_COMPILER} OR
Stella Laurenzoa4137ef2020-12-09 21:28:08 -0800294 ${IREE_BUILD_PYTHON_BINDINGS})
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800295 find_package(Python3 COMPONENTS Interpreter REQUIRED)
296endif()
297
298#-------------------------------------------------------------------------------
Stella Laurenzo382122d2020-06-11 16:18:09 -0700299# MLIR/LLVM Dependency
300# We treat the LLVM dependency specially because we support several different
301# ways to use it:
scotttodd354b6912020-06-24 16:29:35 -0700302# - Bundled (default): a source dependency directly on the
Stella Laurenzo382122d2020-06-11 16:18:09 -0700303# third_party/llvm-project submodule.
304# - External: An external (source or installed) dependency on LLVM.
305# - Provided: When IREE is used as a sub-project, it is assumed that the LLVM
306# dependency is added prior to including this configuration.
307#-------------------------------------------------------------------------------
308
Ben Vanikafd7abe2020-10-07 09:01:53 -0700309# Disable LLVM's warnings.
Ben Vanik89a77fa2020-10-07 17:19:31 -0700310set(LLVM_ENABLE_ASSERTIONS OFF CACHE BOOL "don't use global flags /facepalm")
311set(LLVM_ENABLE_WARNINGS OFF CACHE BOOL "don't use global flags /facepalm")
Ben Vanikafd7abe2020-10-07 09:01:53 -0700312
Stella Laurenzo382122d2020-06-11 16:18:09 -0700313# Adds bundled projects that must be included after the LLVM directory has
scotttodd354b6912020-06-24 16:29:35 -0700314# been added and within the scope of its settings (i.e. build type override,
Stella Laurenzo382122d2020-06-11 16:18:09 -0700315# etc).
316function(add_bundled_mlir_dependent_projects)
317 if(${IREE_ENABLE_EMITC})
Marius Brehler71f76cb2020-11-10 20:56:55 +0100318 add_subdirectory(third_party/mlir-emitc EXCLUDE_FROM_ALL)
319 endif()
320
321 if(${IREE_BUILD_COMPILER})
322 add_subdirectory(third_party/tensorflow/tensorflow/compiler/mlir/hlo EXCLUDE_FROM_ALL)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700323 endif()
324endfunction()
325
326function(add_iree_mlir_src_dep llvm_monorepo_path)
Ben Vanik89a77fa2020-10-07 17:19:31 -0700327 # Stash cmake build type in case LLVM messes with it.
328 set(_CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
329
Thomas Raoux3b6a42a2020-06-26 18:18:48 -0700330 # experimental model builder uses vulkan runner.
331 if(${IREE_BUILD_EXPERIMENTAL})
332 set(MLIR_VULKAN_RUNNER_ENABLED ON)
333 endif()
334
Stella Laurenzo382122d2020-06-11 16:18:09 -0700335 add_subdirectory("${llvm_monorepo_path}/llvm" "third_party/llvm-project/llvm" EXCLUDE_FROM_ALL)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700336
Ben Vanik89a77fa2020-10-07 17:19:31 -0700337 # Reset CMAKE_BUILD_TYPE to its previous setting.
Stella Laurenzo382122d2020-06-11 16:18:09 -0700338 set(CMAKE_BUILD_TYPE "${_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
339endfunction()
340
341if(${IREE_ENABLE_MLIR})
342 if(${IREE_MLIR_DEP_MODE} STREQUAL "DISABLED")
343 message(STATUS "Not adding MLIR/LLVM dep due to IREE_MLIR_DEP_MODE=DISABLED")
344 elseif(${IREE_MLIR_DEP_MODE} STREQUAL "BUNDLED")
345 message(STATUS "Adding bundled LLVM source dependency")
346 add_iree_mlir_src_dep("third_party/llvm-project")
Marius Brehler71f76cb2020-11-10 20:56:55 +0100347
Ben Vanik6bc6f902020-11-16 05:37:08 -0800348 # Extend module path to allow submodules to use LLVM and MLIR CMake modules.
Ben Vanik479ef302020-11-14 08:36:35 -0800349 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/mlir")
350 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/lib/cmake/llvm/")
Marius Brehler71f76cb2020-11-10 20:56:55 +0100351
Ben Vanik6bc6f902020-11-16 05:37:08 -0800352 # Add the bundled include directories for cmake files looking for them.
Marius Brehler71f76cb2020-11-10 20:56:55 +0100353 list(APPEND LLVM_INCLUDE_DIRS
Ben Vanik479ef302020-11-14 08:36:35 -0800354 ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/llvm/include
355 ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/include
Marius Brehler71f76cb2020-11-10 20:56:55 +0100356 )
357 list(APPEND MLIR_INCLUDE_DIRS
Ben Vanik479ef302020-11-14 08:36:35 -0800358 ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/mlir/include
359 ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include
Marius Brehler71f76cb2020-11-10 20:56:55 +0100360 )
361
Ben Vanik6bc6f902020-11-16 05:37:08 -0800362 # Avoid globally modifying paths by instead adding the include paths to the
363 # rules that really should have them in the first place.
364 target_include_directories(LLVMSupport PUBLIC
365 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/llvm/include>
366 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/include>
367 )
368 target_include_directories(MLIRSupport PUBLIC
369 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/mlir/include>
370 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include>
371 )
372
Marius Brehler71f76cb2020-11-10 20:56:55 +0100373 # Set build option to use MHLO alongside with bundled MLIR
374 set(MHLO_BUILD_EMBEDDED ON)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700375 elseif(${IREE_MLIR_DEP_MODE} STREQUAL "INSTALLED")
Stella Laurenzoa1914e72020-07-25 00:06:47 -0700376 # Deps of installed MLIR/LLVM packages.
377 find_package(ZLIB) # See: https://reviews.llvm.org/D79219
Stella Laurenzo382122d2020-06-11 16:18:09 -0700378 message(STATUS "Looking for installed MLIR/LLVM packages (configure with MLIR_DIR variable)")
379 find_package(MLIR REQUIRED CONFIG)
380 message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
381 message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
382 list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
383 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
384 include(TableGen)
385 include(AddLLVM)
386 include(AddMLIR)
387 include(HandleLLVMOptions)
388
389 # Add include/link directories
Ben Vanikafd7abe2020-10-07 09:01:53 -0700390 include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
391 include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
Stella Laurenzo382122d2020-06-11 16:18:09 -0700392 link_directories(${LLVM_BUILD_LIBRARY_DIR})
393 add_definitions(${LLVM_DEFINITIONS})
394 else()
395 message(FATAL "Unsupported IREE_MLIR_DEP_MODE=${IREE_MLIR_DEP_MODE}")
396 endif()
397
Stella Laurenzo382122d2020-06-11 16:18:09 -0700398 add_bundled_mlir_dependent_projects()
399endif()
400
Stella Laurenzo382122d2020-06-11 16:18:09 -0700401#-------------------------------------------------------------------------------
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800402# Python bindings.
Ben Vanik185d30c2019-09-19 14:24:11 -0700403#-------------------------------------------------------------------------------
404
Lei Zhang84787782020-06-02 08:42:25 -0700405if(${IREE_BUILD_PYTHON_BINDINGS})
Stella Laurenzoc8f906c2020-07-21 13:55:50 -0700406 # Note: Optional because python libs can be manually specified.
Marius Brehleraf7b4c32020-11-24 19:19:17 +0100407 find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
Scott Todd4af17b62019-12-17 16:47:00 -0800408endif()
409
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800410#-------------------------------------------------------------------------------
411# Bazel setup (conditional on whether features need it)
412# Depends on python configuration.
413#-------------------------------------------------------------------------------
414
Stella Laurenzoa4137ef2020-12-09 21:28:08 -0800415if(${IREE_ENABLE_TENSORFLOW})
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800416 include(configure_bazel)
417 iree_configure_bazel()
418endif()
419
420#-------------------------------------------------------------------------------
421# Other dependencies.
422#-------------------------------------------------------------------------------
423
Ben Vanik512d2d32019-09-20 13:22:34 -0700424include(external_cc_library)
Ben Vanik2d1808b2020-07-17 19:02:16 -0700425include(flatbuffer_c_library)
Ben Vanik512d2d32019-09-20 13:22:34 -0700426
Ben Vanik71527c22020-07-17 13:09:57 -0700427add_subdirectory(build_tools/third_party/flatcc EXCLUDE_FROM_ALL)
Thomas6456bfb2020-11-18 15:05:13 -0800428add_subdirectory(build_tools/third_party/half EXCLUDE_FROM_ALL)
NatashaKnk0d7e00f2020-11-10 19:16:31 -0800429add_subdirectory(build_tools/third_party/pffft EXCLUDE_FROM_ALL)
Ben Vanik71527c22020-07-17 13:09:57 -0700430add_subdirectory(build_tools/third_party/renderdoc_api EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700431add_subdirectory(build_tools/third_party/ruy EXCLUDE_FROM_ALL)
Ben Vanik28040cd2020-11-14 10:52:22 -0800432add_subdirectory(build_tools/third_party/vulkan_memory_allocator EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700433
Ben Vanikda79d9b2020-10-01 09:52:54 -0700434add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
Marius Brehlerf5022e82019-12-13 15:20:25 -0800435add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700436add_subdirectory(third_party/abseil-cpp EXCLUDE_FROM_ALL)
Ben Vanik71527c22020-07-17 13:09:57 -0700437add_subdirectory(third_party/flatcc EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700438add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
Ben Vanik185d30c2019-09-19 14:24:11 -0700439
Lei Zhang0d281b72020-06-01 20:00:23 -0400440if(CMAKE_CROSSCOMPILING)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400441 # We need flatc to generate some source code. When cross-compiling, we need
442 # to make sure the flatc binary is configured under host environment.
Scott Todd7f4c2d22020-10-14 14:10:22 -0700443 iree_declare_host_excutable(flatc "flatc" BUILDONLY)
444 iree_declare_host_excutable(flatcc_cli "flatcc_cli" BUILDONLY)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400445
Lei Zhang0d281b72020-06-01 20:00:23 -0400446 # Set the FLATBUFFERS_FLATC_EXECUTABLE. It controls where to find the flatc
447 # binary in BuildFlatBuffers().
Lei Zhang486e7072020-06-16 11:42:49 -0400448 iree_get_executable_path(FLATBUFFERS_FLATC_EXECUTABLE flatc)
Lei Zhang1e6a7a72020-06-04 13:12:01 -0400449
Lei Zhang0d281b72020-06-01 20:00:23 -0400450 # Add a custom target to copy the flatc to the binary directory.
Ben Vanikeb53de32020-07-17 19:05:27 -0700451 add_custom_target(iree_host_flatcc_cli
Ben Vanik71527c22020-07-17 13:09:57 -0700452 COMMAND
453 "${CMAKE_COMMAND}" -E copy_if_different
Ben Vanik479ef302020-11-14 08:36:35 -0800454 "${CMAKE_CURRENT_SOURCE_DIR}/third_party/flatcc/bin/flatcc${IREE_HOST_EXECUTABLE_SUFFIX}"
Scott Todd7f4c2d22020-10-14 14:10:22 -0700455 "${IREE_HOST_BINARY_ROOT}/bin/flatcc_cli${IREE_HOST_EXECUTABLE_SUFFIX}"
Ben Vanikeb53de32020-07-17 19:05:27 -0700456 DEPENDS iree_host_build_flatcc_cli
Ben Vanik71527c22020-07-17 13:09:57 -0700457 COMMENT "Installing host flatcc..."
458 )
Ben Vanik14257372020-07-21 08:05:18 -0700459else()
460 # TODO: unify flatc and flatcc handling to the same mechanism.
461 add_executable(iree_host_flatcc_cli ALIAS flatcc_cli)
Lei Zhang0d281b72020-06-01 20:00:23 -0400462endif()
463
Stella Laurenzo06034152020-01-03 11:26:31 -0800464if(${IREE_BUILD_COMPILER})
Marius Brehler87eaa3c2020-07-02 17:39:49 +0200465 add_subdirectory(build_tools/third_party/tensorflow/tensorflow/compiler/mlir/hlo EXCLUDE_FROM_ALL)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700466endif()
467
Marius Brehler29676502019-12-27 17:07:10 -0800468if(${IREE_BUILD_TESTS})
469 add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
Marius Brehler575b63a2020-01-07 09:39:24 -0800470 enable_testing(iree)
Marius Brehler29676502019-12-27 17:07:10 -0800471endif()
472
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800473if(${IREE_BUILD_PYTHON_BINDINGS})
Stella Laurenzo66578b02020-07-20 17:34:44 -0700474 # NOTE: The multipy defaults come from pybind's configuration and must come
475 # after. This should be pulled in locally at some point.
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800476 add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
Stella Laurenzo66578b02020-07-20 17:34:44 -0700477 iree_multipy_configure()
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800478endif()
479
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400480if(${IREE_TARGET_BACKEND_METAL-SPIRV})
481 # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
482 add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL)
483endif()
484
Ben Vanik185d30c2019-09-19 14:24:11 -0700485#-------------------------------------------------------------------------------
Lei Zhang22f0e242020-03-30 12:09:20 -0700486# IREE top-level targets
487#-------------------------------------------------------------------------------
488
489if(${IREE_BUILD_DOCS})
490 # Add a top-level custom target to drive generating all documentation.
491 # Register it to the default target given that IREE_BUILD_DOCS is explicitly
492 # requested.
493 add_custom_target(iree-doc ALL)
494endif()
495
496#-------------------------------------------------------------------------------
Ben Vanik185d30c2019-09-19 14:24:11 -0700497# IREE top-level libraries
498#-------------------------------------------------------------------------------
499
Marius Brehlerf5022e82019-12-13 15:20:25 -0800500add_subdirectory(build_tools/embed_data/)
501
Ben Vanik185d30c2019-09-19 14:24:11 -0700502add_subdirectory(iree/base)
Ben Vanik512d2d32019-09-20 13:22:34 -0700503add_subdirectory(iree/hal)
Marius Brehler9317d1f2020-01-08 10:34:11 -0800504add_subdirectory(iree/modules)
Ben Vanik512d2d32019-09-20 13:22:34 -0700505add_subdirectory(iree/schemas)
Marius Brehler29676502019-12-27 17:07:10 -0800506add_subdirectory(iree/testing)
Marius Brehler476031a2020-03-24 16:14:30 -0700507add_subdirectory(iree/test)
Stella Laurenzo06034152020-01-03 11:26:31 -0800508
Stella Laurenzo382122d2020-06-11 16:18:09 -0700509if(${IREE_ENABLE_MLIR})
Stella Laurenzo06034152020-01-03 11:26:31 -0800510 # The VM requires LLVM to build its op definitions.
511 add_subdirectory(iree/vm)
512endif()
Ben Vanikcc2aff92019-09-24 10:23:55 -0700513
514if(${IREE_BUILD_COMPILER})
515 add_subdirectory(iree/compiler)
Stella Laurenzo382122d2020-06-11 16:18:09 -0700516elseif(${IREE_ENABLE_MLIR})
Stella Laurenzo06034152020-01-03 11:26:31 -0800517 # If not building the compiler, tablegen is still needed
518 # to generate vm ops so deep include it only.
Marius Brehlerc61240a2020-04-20 11:22:59 -0700519 add_subdirectory(iree/compiler/Dialect/IREE/Tools)
Stella Laurenzo06034152020-01-03 11:26:31 -0800520 add_subdirectory(iree/compiler/Dialect/VM/Tools)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700521endif()
Ben Vanik6b112ef2019-10-03 10:45:14 -0700522
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800523if(${IREE_BUILD_PYTHON_BINDINGS})
524 add_subdirectory(bindings/python)
525endif()
526
Jenni Kilduff0d09cf32020-10-19 10:23:34 -0700527if(${IREE_BUILD_JAVA_BINDINGS})
528 add_subdirectory(bindings/java)
529 add_subdirectory(bindings/javatests)
530endif()
531
Ben Vanik6b112ef2019-10-03 10:45:14 -0700532add_subdirectory(iree/tools)
533
534if(${IREE_BUILD_SAMPLES})
535 add_subdirectory(iree/samples)
536endif()
Ben Vanikb64e9182020-01-30 15:19:37 -0800537
538if(${IREE_BUILD_EXPERIMENTAL})
539 add_subdirectory(experimental)
540endif()
Scott Toddee30e1b2020-02-03 16:44:15 -0800541
Stella Laurenzoa4137ef2020-12-09 21:28:08 -0800542if(${IREE_ENABLE_TENSORFLOW})
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800543 add_subdirectory(integrations/tensorflow)
544endif()
545
Marius Brehler42cc5e62020-02-18 11:17:12 -0800546if(${IREE_BUILD_PYTHON_BINDINGS})
547 iree_complete_py_extension_link_options()
548endif()
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700549
550set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
551 CACHE INTERNAL "IREE: Include Directories" FORCE)