blob: 5dd0e5d63918aaec14ad6a2914135446410045bc [file] [log] [blame]
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07001# Copyright 2019 The IREE Authors
Ben Vanik185d30c2019-09-19 14:24:11 -07002#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07003# Licensed under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ben Vanik185d30c2019-09-19 14:24:11 -07006
Geoffrey Martin-Noble4fd7a1d2022-11-17 15:09:49 -08007cmake_minimum_required(VERSION 3.21...3.24)
Ben Vanik552c5f42021-06-21 17:39:14 -07008
9# LLVM requires CMP0116 for tblgen: https://reviews.llvm.org/D101083
10# CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()`
11# New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html
12set(CMAKE_POLICY_DEFAULT_CMP0116 OLD)
Ben Vanika49a5f52023-02-21 12:19:17 -080013if(POLICY CMP0116)
14 cmake_policy(SET CMP0116 OLD)
15endif()
Ben Vanik552c5f42021-06-21 17:39:14 -070016
Ben Vanik512d2d32019-09-20 13:22:34 -070017set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Ben Vanik185d30c2019-09-19 14:24:11 -070018
Stella Laurenzo688670f2021-09-24 18:16:25 -070019project(IREE ASM C CXX)
Lei Zhang6c5907b2020-06-02 09:06:08 -070020set(CMAKE_C_STANDARD 11)
Scott Todd07349fa2022-07-27 12:16:05 -070021set(CMAKE_CXX_STANDARD 17)
Stella Laurenzobc09f0e2022-07-27 16:22:39 -070022# LLVM defines this as a CACHE property and uses a policy that causes the
23# cache value to take precedence. This is causing us to mix 17/14 across
24# the boundary.
25# TODO: Remove this once the LLVM mechanism is updated. See:
26# https://discourse.llvm.org/t/important-new-toolchain-requirements-to-build-llvm-will-most-likely-be-landing-within-a-week-prepare-your-buildbots/61447/9
27set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to" FORCE)
Ben Vanik185d30c2019-09-19 14:24:11 -070028set(IREE_IDE_FOLDER IREE)
29set_property(GLOBAL PROPERTY USE_FOLDERS ON)
30
Scott Todd1a1aea62022-04-28 09:12:11 -070031if(MSVC)
Ben Vanik96fd2772021-04-19 20:30:56 -070032 enable_language(ASM_MASM)
33else()
34 enable_language(ASM)
35endif()
36
Cindy Liub1bfb9c2022-06-27 10:46:13 -070037# Set the default CMake build type so some of the build type dependent setting
38# in the submodules and functions (IREE assertion) can be set properly.
Cindy Liu23dafd02022-06-22 15:36:21 -070039set(DEFAULT_CMAKE_BUILD_TYPE "Release")
40if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
41 message(STATUS "No build type selected, default to ${DEFAULT_CMAKE_BUILD_TYPE}")
42 set(CMAKE_BUILD_TYPE "${DEFAULT_CMAKE_BUILD_TYPE}" CACHE STRING "Build type (default ${DEFAULT_CMAKE_BUILD_TYPE})" FORCE)
43endif()
44
Scott Todd99e9f5b2021-12-30 10:56:12 -080045include(CMakeDependentOption)
46
Lei Zhang7e253da2020-06-10 07:51:19 -070047#-------------------------------------------------------------------------------
48# Project component configuration
49#-------------------------------------------------------------------------------
50
Ben Vanik56d44bf2020-05-15 13:20:50 -070051option(IREE_ENABLE_RUNTIME_TRACING "Enables instrumented runtime tracing." OFF)
Scott Todd1cd9f0a2021-07-29 10:05:44 -070052option(IREE_ENABLE_COMPILER_TRACING "Enables instrumented compiler tracing." OFF)
Ben Vanik2d79c202022-10-24 15:08:57 -070053option(IREE_ENABLE_RENDERDOC_PROFILING "Enables profiling HAL devices with the RenderDoc tool." OFF)
Geoffrey Martin-Noblefe74bd42021-10-27 22:17:13 -070054option(IREE_ENABLE_THREADING "Builds IREE in with thread library support." ON)
Rob Sudermand42e0022022-06-08 16:38:45 -070055option(IREE_ENABLE_CLANG_TIDY "Builds IREE in with clang tidy enabled on IREE's libraries." OFF)
Ben Vanik512d2d32019-09-20 13:22:34 -070056
Ben Vanik4aae0032022-04-19 15:49:26 -070057# TODO(#8469): remove the dependency on cpuinfo entirely.
58option(IREE_ENABLE_CPUINFO "Enables runtime use of cpuinfo for processor topology detection." ON)
59
Marius Brehleredfc57f2019-12-18 11:19:38 -080060option(IREE_BUILD_COMPILER "Builds the IREE compiler." ON)
Ben Vanik512d2d32019-09-20 13:22:34 -070061option(IREE_BUILD_TESTS "Builds IREE unit tests." ON)
Lei Zhang22f0e242020-03-30 12:09:20 -070062option(IREE_BUILD_DOCS "Builds IREE docs." OFF)
Ben Vanik6b112ef2019-10-03 10:45:14 -070063option(IREE_BUILD_SAMPLES "Builds IREE sample projects." ON)
Scott Todd382fc632022-06-06 10:57:24 -070064option(IREE_BUILD_PYTHON_BINDINGS "Builds the IREE python bindings" OFF)
Stella Laurenzod3770ff2021-10-18 22:54:18 -070065option(IREE_BUILD_TRACY "Builds tracy server tools." OFF)
Stella Laurenzo96d959e2023-02-19 11:58:14 -080066option(IREE_BUILD_BUNDLED_LLVM "Builds the bundled llvm-project (vs using installed)" ON)
Ben Vanikcd1132f2021-01-29 15:58:17 -080067
Scott Todd352da3f2022-07-20 15:25:11 -070068option(IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER "Use the system linker when generating IREE modules in tests/samples/benchmarks (useful for Tracy)." OFF)
bjacob1af90ee2022-03-28 22:10:53 -040069
Stella Laurenzobb1de962021-01-05 08:18:33 -080070# Properties controlling version and naming of release artifacts.
Stella Laurenzof33f5b52022-01-08 17:49:52 +000071set(IREE_RELEASE_PACKAGE_SUFFIX "" CACHE STRING "Suffix to append to distributed package names")
Stella Laurenzobb1de962021-01-05 08:18:33 -080072set(IREE_RELEASE_VERSION "0.1a1" CACHE STRING "Version to embed in distributed packages")
73set(IREE_RELEASE_REVISION "HEAD" CACHE STRING "Version control revision information to embed in distributed packages")
Jacques Pienaar27779b22022-12-22 15:37:56 -080074option(IREE_EMBED_RELEASE_INFO "Embed the IREE version information in built artifacts." OFF)
Stella Laurenzobb1de962021-01-05 08:18:33 -080075
Scott Toddde426de2023-01-13 08:26:02 -080076# Using already built host binaries, such as for cross-compilation.
77set(IREE_HOST_BIN_DIR_DEFAULT "")
78if(IREE_HOST_BINARY_ROOT)
79 message(WARNING "IREE_HOST_BINARY_ROOT is deprecated. Use IREE_HOST_BIN_DIR"
80 " pointing directly to the directory containing binaries"
81 " instead.")
82 set(IREE_HOST_BIN_DIR_DEFAULT "${IREE_HOST_BINARY_ROOT}/bin")
83endif()
84set(IREE_HOST_BIN_DIR "${IREE_HOST_BIN_DIR_DEFAULT}" CACHE STRING "Path to directory containing IREE binary tools to use instead of building them from source.")
85
Ben Vanikcd1132f2021-01-29 15:58:17 -080086option(IREE_BUILD_BINDINGS_TFLITE "Builds the IREE TFLite C API compatibility shim" ON)
Kojo Acquah5bf39942021-04-22 16:01:28 -070087option(IREE_BUILD_BINDINGS_TFLITE_JAVA "Builds the IREE TFLite Java bindings with the C API compatibility shim" ON)
Ben Vanikcd1132f2021-01-29 15:58:17 -080088
Jerry Wub6b1dee2022-06-01 13:45:16 +000089# Properties building and controlling benchmark suites.
90option(IREE_BUILD_BENCHMARKS "Builds IREE benchmark suites." OFF)
91# TODO(pzread): Currently IREE_ENABLE_COMPILATION_BENCHMARKS needs
92# IREE_BUILD_BENCHMARKS. Eventually we will decouple them.
93cmake_dependent_option(IREE_ENABLE_COMPILATION_BENCHMARKS "Builds IREE benchmark suites for compilation statistics." OFF ${IREE_BUILD_BENCHMARKS} OFF)
94
95option(IREE_BUILD_MICROBENCHMARKS "Builds IREE microbenchmark suites." OFF)
96
Jerry Wu57404972023-03-02 22:16:11 +000097# Leave the old name around for a bit so that people can update their configs
98# and previous cached values will automatically carry over.
99# TODO(pzread): Remove this option after 2023-04-01
100option(IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS "DEPRECATED alias for IREE_BUILD_E2E_TEST_ARTIFACTS" OFF)
101option(IREE_BUILD_E2E_TEST_ARTIFACTS "Builds IREE E2E test artifacts suite generated by the e2e test framework." "${IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS}")
102
Stella Laurenzo289b9a12023-02-24 13:06:32 -0800103# For development, builds LLVM (and in the future) the whole compiler as
104# individual shared libraries similar to if passing -DBUILD_SHARED_LIBS=ON
105# to a standalone LLVM build. This can dramatically reduce linking time and
106# makes the management of some dependencies more strict.
107# This option is considered experimental and should not be relied on until
108# CI coverage is established.
109option(IREE_COMPILER_BUILD_SHARED_LIBS "Enables BUILD_SHARED_LIBS CMake mode for LLVM and the compiler (this is only suitable for development)" OFF)
110
111# Must be defined as an option (CMake does not do it automatically), even though
112# we override it for different parts of the tree.
113# This option is considered experimental and should not be relied on until
114# CI coverage is established.
115option(BUILD_SHARED_LIBS "Instructs CMake to build libraries as shared if possible" OFF)
116
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800117#-------------------------------------------------------------------------------
118# Experimental project flags
119#-------------------------------------------------------------------------------
120
Ben Vanika372ab52022-07-25 18:40:22 -0700121option(IREE_BUILD_EXPERIMENTAL_VMVX_MMT4D "Enables MMT4D methods in the VMVX module." OFF)
Scott Toddf237b5e2022-01-28 11:02:25 -0800122option(IREE_BUILD_EXPERIMENTAL_WEB_SAMPLES "Builds experimental web samples." OFF)
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800123
124#-------------------------------------------------------------------------------
Scott Todd18a54852023-01-27 08:52:12 -0800125# CUDA Toolkit.
126#
127# Using the (optional) CUDA support in the compiler and runtime requires the
128# NVIDIA CUDA Toolkit. The toolkit can either be installed ahead of time or
129# it can be automatically downloaded on certain host architectures.
Scott Todd9fa5de72023-01-23 14:40:18 -0800130#-------------------------------------------------------------------------------
131
Scott Todd18a54852023-01-27 08:52:12 -0800132set(IREE_CUDA_AVAILABLE OFF)
133find_package(CUDAToolkit)
134if(CUDAToolkit_FOUND)
135 set(IREE_CUDA_AVAILABLE ON)
136else()
137 # We can download the SDK in build_tools/third_party/cuda/CMakeLists.txt, if
138 # on a supported platform/arch.
139 if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR WIN32)
140 if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)")
141 set(IREE_CUDA_AVAILABLE ON)
142 endif()
Scott Todd9fa5de72023-01-23 14:40:18 -0800143 endif()
144endif()
145
146#-------------------------------------------------------------------------------
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800147# Runtime HAL Driver Options
148# By default, all runtime drivers supported by the current platform which do
149# not require external deps are enabled by default. This can be changed with:
150# -DIREE_HAL_DRIVER_DEFAULTS=OFF
Ben Vanik185d30c2019-09-19 14:24:11 -0700151#-------------------------------------------------------------------------------
152
Ben Vanik4e6af052022-06-07 17:10:20 -0700153# External HAL drivers; see runtime/src/iree/hal/drivers/CMakeLists.txt for more
154# information on how to declare external drivers.
155set(IREE_EXTERNAL_HAL_DRIVERS "" CACHE STRING "")
156
Ben Vanik89160272022-09-26 16:44:11 -0700157# Additional executable loader deps to add dependent libraries to any target
158# using the default executable loader registration utilities.
159# TODO(benvanik): extend the deps to encompass the built-in loaders too so that
160# we have one flag. We could also support a list of deps and automatically
161# generate the registration from that via a configure file.
162set(IREE_HAL_EXECUTABLE_LOADER_EXTRA_DEPS "" CACHE STRING "")
163
Scott Toddd1620f02021-12-22 14:55:38 -0800164option(IREE_HAL_DRIVER_DEFAULTS "Sets the default value for all runtime HAL drivers" ON)
Ben Vanika9aaebe2022-12-16 09:14:53 -0800165
Scott Todd9fa5de72023-01-23 14:40:18 -0800166# CUDA support is enabled by default if the platform supports the CUDA SDK and
167# not cross compiling. Note: a CUDA-compatible GPU with drivers is still
168# required to actually run CUDA workloads.
169set(IREE_HAL_DRIVER_CUDA_DEFAULT ${IREE_HAL_DRIVER_DEFAULTS})
Scott Todd18a54852023-01-27 08:52:12 -0800170if(NOT IREE_CUDA_AVAILABLE OR CMAKE_CROSSCOMPILING)
Scott Todd9fa5de72023-01-23 14:40:18 -0800171 set(IREE_HAL_DRIVER_CUDA_DEFAULT OFF)
172endif()
173
174# Vulkan support is enabled by default if the platform might support Vulkan.
175# Apple platforms support Metal instead of Vulkan, though MoltenVK may work.
176set(IREE_HAL_DRIVER_VULKAN_DEFAULT ${IREE_HAL_DRIVER_DEFAULTS})
Scott Todd1a1aea62022-04-28 09:12:11 -0700177if(APPLE)
Scott Toddd1620f02021-12-22 14:55:38 -0800178 set(IREE_HAL_DRIVER_VULKAN_DEFAULT OFF)
Scott Toddd1620f02021-12-22 14:55:38 -0800179endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700180
Scott Toddd1620f02021-12-22 14:55:38 -0800181option(IREE_HAL_DRIVER_CUDA "Enables the 'cuda' runtime HAL driver" ${IREE_HAL_DRIVER_CUDA_DEFAULT})
Ben Vanike3058072022-06-06 19:21:44 -0700182option(IREE_HAL_DRIVER_LOCAL_SYNC "Enables the 'local-sync' runtime HAL driver" ${IREE_HAL_DRIVER_DEFAULTS})
183option(IREE_HAL_DRIVER_LOCAL_TASK "Enables the 'local-task' runtime HAL driver" ${IREE_HAL_DRIVER_DEFAULTS})
184option(IREE_HAL_DRIVER_VULKAN "Enables the 'vulkan' runtime HAL driver" ${IREE_HAL_DRIVER_VULKAN_DEFAULT})
185
Ben Vanik6e64b6e2022-06-07 09:14:53 -0700186option(IREE_HAL_EXECUTABLE_LOADER_DEFAULTS "Sets the default value for all runtime HAL executable loaders" ON)
187set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
188set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
189set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
Ben Vanike3058072022-06-06 19:21:44 -0700190
191# Emscripten builds don't support embedded ELF libraries.
192if(EMSCRIPTEN)
Ben Vanik2e8540e2022-06-06 20:39:34 -0700193 set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT OFF)
Ben Vanike3058072022-06-06 19:21:44 -0700194endif()
195
196# If forcing system libraries (for TSAN/debugging tools/etc) then ensure the
197# system library loader is linked in.
Scott Todd352da3f2022-07-20 15:25:11 -0700198if(IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER)
Ben Vanike3058072022-06-06 19:21:44 -0700199 set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT ON)
200endif()
201
Ben Vanik6e64b6e2022-06-07 09:14:53 -0700202# If no local driver is enabled then we force all the loaders off; this allows
203# for simpler checks that don't need to see if both the driver and loader is
204# available.
205if(NOT IREE_HAL_DRIVER_LOCAL_SYNC AND NOT IREE_HAL_DRIVER_LOCAL_TASK)
206 set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT OFF)
207 set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT OFF)
208 set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT OFF)
209endif()
210
Ben Vanik2e8540e2022-06-06 20:39:34 -0700211option(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF "Enables the embedded dynamic library loader for local HAL drivers" ${IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT})
Ben Vanike3058072022-06-06 19:21:44 -0700212option(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY "Enables the system dynamic library loader for local HAL drivers" ${IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT})
213option(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE "Enables the VMVX module loader for local HAL drivers" ${IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT})
214
Scott Todd0f1f8eb2022-06-10 15:53:30 -0700215if(IREE_BUILD_COMPILER)
216 # The compiler requires the local task driver with the VMVX loader.
217 set(IREE_HAL_DRIVER_LOCAL_TASK ON)
218 set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE ON)
219endif()
220
Ben Vanike3058072022-06-06 19:21:44 -0700221message(STATUS "IREE HAL drivers:")
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800222if(IREE_HAL_DRIVER_CUDA)
223 message(STATUS " - cuda")
224endif()
Ben Vanike3058072022-06-06 19:21:44 -0700225if(IREE_HAL_DRIVER_LOCAL_SYNC)
226 message(STATUS " - local-sync")
227endif()
228if(IREE_HAL_DRIVER_LOCAL_TASK)
229 message(STATUS " - local-task")
230endif()
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800231if(IREE_HAL_DRIVER_VULKAN)
232 message(STATUS " - vulkan")
233endif()
Ben Vanik4e6af052022-06-07 17:10:20 -0700234if(IREE_EXTERNAL_HAL_DRIVERS)
235 message(STATUS " + external: ${IREE_EXTERNAL_HAL_DRIVERS}")
236endif()
Ben Vanik512d2d32019-09-20 13:22:34 -0700237
Ben Vanike3058072022-06-06 19:21:44 -0700238message(STATUS "IREE HAL local executable library loaders:")
Ben Vanik2e8540e2022-06-06 20:39:34 -0700239if(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF)
240 message(STATUS " - embedded-elf")
Ben Vanike3058072022-06-06 19:21:44 -0700241endif()
242if(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY)
243 message(STATUS " - system-library")
244endif()
245if(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE)
246 message(STATUS " - vmvx-module")
247endif()
248
Lei Zhang0d281b72020-06-01 20:00:23 -0400249#-------------------------------------------------------------------------------
Ben Vanik4e6af052022-06-07 17:10:20 -0700250# Experimental ROCM HAL driver
251#-------------------------------------------------------------------------------
252
253set(IREE_EXTERNAL_ROCM_HAL_DRIVER_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/experimental/rocm")
254set(IREE_EXTERNAL_ROCM_HAL_DRIVER_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/experimental/rocm")
255set(IREE_EXTERNAL_ROCM_HAL_DRIVER_TARGET "iree::experimental::rocm::registration")
256set(IREE_EXTERNAL_ROCM_HAL_DRIVER_REGISTER "iree_hal_rocm_driver_module_register")
257
258#-------------------------------------------------------------------------------
Scott Todd382fc632022-06-06 10:57:24 -0700259# Compiler Target Options
Scott Todd9fa5de72023-01-23 14:40:18 -0800260# By default, all compiler targets supported by the current platform are
261# enabled. Some compiler targets like CUDA will install external deps as
262# needed at configure time. This can be changed with:
Scott Todd382fc632022-06-06 10:57:24 -0700263# -DIREE_TARGET_BACKEND_DEFAULTS=OFF
264#-------------------------------------------------------------------------------
265
266option(IREE_TARGET_BACKEND_DEFAULTS "Sets the default value for all compiler target backends" ON)
267
268# The VMVX backend is always enabled.
269cmake_dependent_option(IREE_TARGET_BACKEND_VMVX "Enables the 'vmvx' compiler target backend" ON ${IREE_BUILD_COMPILER} OFF)
270
271# Supported default target backends.
Scott Todd352da3f2022-07-20 15:25:11 -0700272cmake_dependent_option(IREE_TARGET_BACKEND_LLVM_CPU "Enables the 'llvm-cpu' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_BUILD_COMPILER} OFF)
273cmake_dependent_option(IREE_TARGET_BACKEND_LLVM_CPU_WASM "Enables WebAssembly in the 'llvm-cpu' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_TARGET_BACKEND_LLVM_CPU} OFF)
Scott Todd382fc632022-06-06 10:57:24 -0700274cmake_dependent_option(IREE_TARGET_BACKEND_METAL_SPIRV "Enables the 'metal-spirv' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_BUILD_COMPILER} OFF)
Scott Todd382fc632022-06-06 10:57:24 -0700275cmake_dependent_option(IREE_TARGET_BACKEND_VULKAN_SPIRV "Enables the 'vulkan-spirv' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_BUILD_COMPILER} OFF)
276
Scott Todd9fa5de72023-01-23 14:40:18 -0800277# Supported default target backends that are only available on certain
278# platforms.
279set(IREE_TARGET_BACKEND_CUDA_DEFAULT ${IREE_TARGET_BACKEND_DEFAULTS})
Scott Todd18a54852023-01-27 08:52:12 -0800280if(NOT IREE_CUDA_AVAILABLE)
Scott Todd9fa5de72023-01-23 14:40:18 -0800281 set(IREE_TARGET_BACKEND_CUDA_DEFAULT OFF)
282endif()
283cmake_dependent_option(IREE_TARGET_BACKEND_CUDA "Enables the 'cuda' compiler target backend" ${IREE_TARGET_BACKEND_CUDA_DEFAULT} ${IREE_BUILD_COMPILER} OFF)
284
Scott Todd382fc632022-06-06 10:57:24 -0700285# Non-default target backends either have additional dependencies or are
286# experimental/niche in some fashion.
Scott Todd382fc632022-06-06 10:57:24 -0700287cmake_dependent_option(IREE_TARGET_BACKEND_ROCM "Enables the 'rocm' compiler target backend" OFF ${IREE_BUILD_COMPILER} OFF)
288# Disable WebGPU by default - it has complex deps and is under development.
289cmake_dependent_option(IREE_TARGET_BACKEND_WEBGPU "Enables the 'webgpu' compiler target backend" OFF ${IREE_BUILD_COMPILER} OFF)
290
291#-------------------------------------------------------------------------------
Scott Todde23d4a72022-07-27 14:16:39 -0700292# Compiler Input Dialects
293#-------------------------------------------------------------------------------
294
295cmake_dependent_option(IREE_INPUT_MHLO "Builds support for compiling MHLO programs" ON ${IREE_BUILD_COMPILER} OFF)
296cmake_dependent_option(IREE_INPUT_TORCH "Builds support for compiling Torch MLIR programs" ON ${IREE_BUILD_COMPILER} OFF)
297cmake_dependent_option(IREE_INPUT_TOSA "Builds support for compiling TOSA programs" ON ${IREE_BUILD_COMPILER} OFF)
298
Marius Brehler36f24512022-08-04 17:39:03 +0200299if(IREE_BUILD_COMPILER)
300 message(STATUS "IREE compiler input dialects:")
301 if(IREE_INPUT_MHLO)
302 message(STATUS " - MHLO")
303 endif()
304 if(IREE_INPUT_TORCH)
305 message(STATUS " - Torch MLIR")
306 endif()
307 if(IREE_INPUT_TOSA)
308 message(STATUS " - TOSA")
309 endif()
Scott Todde23d4a72022-07-27 14:16:39 -0700310endif()
311
312#-------------------------------------------------------------------------------
Scott Todd2a1925c2022-06-13 10:03:52 -0700313# Compiler Output Formats
314#-------------------------------------------------------------------------------
315
Scott Todd2a1925c2022-06-13 10:03:52 -0700316cmake_dependent_option(IREE_OUTPUT_FORMAT_C "Enables the 'vm-c' output format, using MLIR EmitC" ON ${IREE_BUILD_COMPILER} OFF)
317
Marius Brehler36f24512022-08-04 17:39:03 +0200318if(IREE_BUILD_COMPILER)
319 message(STATUS "IREE compiler output formats:")
320 if(IREE_OUTPUT_FORMAT_C)
321 message(STATUS " - C source module")
322 endif()
323 # The 'vm-bytecode' and 'vm-asm' formats are always enabled.
324 message(STATUS " - VM Bytecode")
325 message(STATUS " - VM MLIR Assembly")
Scott Todde23d4a72022-07-27 14:16:39 -0700326endif()
Scott Todde23d4a72022-07-27 14:16:39 -0700327
Scott Todd2a1925c2022-06-13 10:03:52 -0700328#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400329# IREE compilation toolchain configuration
330#-------------------------------------------------------------------------------
331
Lei Zhange88470f2020-09-08 13:21:09 -0400332option(IREE_ENABLE_ASAN "Enable address sanitizer" OFF)
333option(IREE_ENABLE_MSAN "Enable memory sanitizer" OFF)
334option(IREE_ENABLE_TSAN "Enable thread sanitizer" OFF)
bjacob7cf5b842022-04-04 16:48:04 -0400335option(IREE_BYTECODE_MODULE_ENABLE_TSAN "Enable thread sanitizer in IREE modules in tests" OFF)
Jakub Kuderski068b1722022-07-27 18:14:59 -0400336option(IREE_ENABLE_UBSAN "Enable undefined behavior sanitizer" OFF)
Stella Laurenzodeb48052022-11-25 10:41:46 -0800337option(IREE_ENABLE_SPLIT_DWARF "Enable gsplit-dwarf for debug information if the platform supports it" OFF)
338option(IREE_ENABLE_THIN_ARCHIVES "Enables thin ar archives (elf systems only). Disable for released static archives" OFF)
Ben Vanikadadd032022-11-28 23:18:16 -0800339option(IREE_LINK_COMPILER_SHARED_LIBRARY "Links IREE tools using the compiler compiled into a shared library" ON)
bjacob7cf5b842022-04-04 16:48:04 -0400340
341# STREQUAL feels wrong here - we don't care about the exact true-value used,
342# ON or TRUE or something else. But we haven't been able to think of a less bad
Scott Toddc7b29122023-02-21 17:33:35 -0800343# alternative. https://github.com/openxla/iree/pull/8474#discussion_r840790062
bjacob7cf5b842022-04-04 16:48:04 -0400344if(NOT IREE_ENABLE_TSAN STREQUAL IREE_BYTECODE_MODULE_ENABLE_TSAN)
345 message(SEND_ERROR
346 "IREE_ENABLE_TSAN and IREE_BYTECODE_MODULE_ENABLE_TSAN must be "
347 "simultaneously ON or OFF. "
348 "A discrepancy between the two would cause tests to crash as IREE "
349 "runtime code (controlled by IREE_ENABLE_TSAN) calls into test IREE "
350 "modules (controlled by IREE_BYTECODE_MODULE_ENABLE_TSAN)")
351endif()
352
353if(IREE_BYTECODE_MODULE_ENABLE_TSAN)
Scott Todd352da3f2022-07-20 15:25:11 -0700354 if(NOT IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER)
bjacob7cf5b842022-04-04 16:48:04 -0400355 message(SEND_ERROR
356 "When IREE_BYTECODE_MODULE_ENABLE_TSAN is ON, "
Scott Todd352da3f2022-07-20 15:25:11 -0700357 "IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER must also be ON. "
bjacob7cf5b842022-04-04 16:48:04 -0400358 "TSAN instrumentation is not currently supported in embedded modules.")
359 endif()
360endif()
361
Scott Todd86781382023-02-07 14:11:52 -0800362if(IREE_LINK_COMPILER_SHARED_LIBRARY AND IREE_ENABLE_COMPILER_TRACING)
363 message(SEND_ERROR
364 "IREE_ENABLE_COMPILER_TRACING requires "
365 "-DIREE_LINK_COMPILER_SHARED_LIBRARY=OFF (the compiler library must not "
366 "be unloaded before Tracy finishes, static linking is one workaround)")
367endif()
368
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800369option(IREE_ENABLE_CCACHE
370 "[DEPRECATED: Use CMAKE_<LANG>_COMPILER_LAUNCHER configure options or environment variables instead.] Use ccache if installed."
371 OFF)
bjacobe694d952020-11-03 12:05:35 -0500372
Scott Todd1a1aea62022-04-28 09:12:11 -0700373if(IREE_ENABLE_CCACHE)
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800374 message(WARNING
375 "IREE_ENABLE_CCACHE is deprecated. Use CMAKE_<LANG>_COMPILER_LAUNCHER"
376 " configure options or environment variables instead.")
bjacobe694d952020-11-03 12:05:35 -0500377 find_program(CCACHE_PROGRAM ccache)
378 if(CCACHE_PROGRAM)
379 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800380 else()
381 message(SEND_ERROR
382 "IREE_ENABLE_CCACHE was set, but executable `ccache` was not found.")
bjacobe694d952020-11-03 12:05:35 -0500383 endif()
384endif()
Lei Zhange88470f2020-09-08 13:21:09 -0400385
Geoffrey Martin-Noble3fa4f8d2021-09-10 09:25:27 -0700386option(IREE_DEV_MODE "Configure settings to optimize for IREE development (as opposed to CI or release)" OFF)
Stella Laurenzo5b639122021-06-18 14:44:10 -0700387
388#-------------------------------------------------------------------------------
389# IREE assertions
390# We don't love the way this is done, but we have to line it up with how LLVM
391# does it and not diverge, since all implementations and all header users must
392# have the same definition of NDEBUG.
393#
394# LLVM defaults LLVM_ENABLE_ASSERTIONS to ON for Debug builds only but then
395# conditions itself to only update flags if not building Debug. We just let
396# IREE_ENABLE_ASSERTIONS be not conditioned on anything and only update the
397# flags in appropriate build types.
398#
399# If IREE_ENABLE_ASSERTIONS is set ON manually, then
400# - NDEBUG must be undefined
401# - LLVM_ENABLE_ASSERTIONS is forced off in order to keep multiple parties
402# from mucking with globals.
403#
404# Since CMake forces NDEBUG for !Debug builds, some surgery needs to be done
405# at the top level to avoid divergence.
406#-------------------------------------------------------------------------------
407
408option(IREE_ENABLE_ASSERTIONS "Force unset of NDEBUG compile option" OFF)
409
410# Filter -DNDEBUG from CMAKE_CXX_FLAGS_* and CMAKE_C_FLAGS_* (if
411# CMAKE_BUILD_TYPE is not Debug).
412function(iree_fix_ndebug)
Scott Todd1a1aea62022-04-28 09:12:11 -0700413 string(TOUPPER "${CMAKE_BUILD_TYPE}" _UPPERCASE_CMAKE_BUILD_TYPE)
414 if(IREE_ENABLE_ASSERTIONS AND NOT "${_UPPERCASE_CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
Stella Laurenzo5b639122021-06-18 14:44:10 -0700415 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
Scott Todd1a1aea62022-04-28 09:12:11 -0700416 foreach(_FLAGS_VAR_TO_SCRUB
417 CMAKE_CXX_FLAGS_${_UPPERCASE_CMAKE_BUILD_TYPE}
418 CMAKE_C_FLAGS_${_UPPERCASE_CMAKE_BUILD_TYPE})
419 set(_ORIGINAL_FLAGS "${${_FLAGS_VAR_TO_SCRUB}}")
420 string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " _ALTERED_FLAGS "${_ORIGINAL_FLAGS}")
421 if(NOT "${_ORIGINAL_FLAGS}" STREQUAL "${_ALTERED_FLAGS}")
Stella Laurenzo5b639122021-06-18 14:44:10 -0700422 message(STATUS
Scott Todd1a1aea62022-04-28 09:12:11 -0700423 "IREE_ENABLE_ASSERTIONS force disabled NDEBUG for ${_FLAGS_VAR_TO_SCRUB}: '${_ORIGINAL_FLAGS}' -> '${_ALTERED_FLAGS}'")
424 set(${_FLAGS_VAR_TO_SCRUB} "${_ALTERED_FLAGS}" PARENT_SCOPE)
Stella Laurenzo5b639122021-06-18 14:44:10 -0700425 endif()
426 endforeach()
427
428 # Make sure that LLVM doesn't add its own logic for assertion disabling.
429 # We'd like to make sure that we are not dueling over globals.
430 set(LLVM_ENABLE_ASSERTIONS OFF PARENT_SCOPE)
431 endif()
432endfunction()
433iree_fix_ndebug()
434
Lei Zhange88470f2020-09-08 13:21:09 -0400435#-------------------------------------------------------------------------------
Lei Zhang7e253da2020-06-10 07:51:19 -0700436# IREE utility definitions
Lei Zhang0d281b72020-06-01 20:00:23 -0400437#-------------------------------------------------------------------------------
438
Scott Toddd1620f02021-12-22 14:55:38 -0800439list(APPEND CMAKE_MODULE_PATH
440 ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/
Scott Toddd1620f02021-12-22 14:55:38 -0800441)
442
Ben Vanik512d2d32019-09-20 13:22:34 -0700443include(iree_macros)
Ben Vanik185d30c2019-09-19 14:24:11 -0700444include(iree_copts)
Ben Vanik6b112ef2019-10-03 10:45:14 -0700445include(iree_cc_binary)
Ben Vanik185d30c2019-09-19 14:24:11 -0700446include(iree_cc_library)
447include(iree_cc_test)
Scott Todd20d746a2023-01-12 09:11:59 -0800448include(iree_import_binary)
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800449include(iree_external_cmake_options)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700450include(iree_tablegen_library)
Lei Zhang22f0e242020-03-30 12:09:20 -0700451include(iree_tablegen_doc)
CindyLiu40ed02d2021-04-22 16:16:28 +0000452include(iree_c_embed_data)
Ben Vanik7f6c57c2023-02-07 18:04:32 -0800453include(iree_bitcode_library)
Scott Todd11adcab2019-12-18 14:10:44 -0800454include(iree_bytecode_module)
Marius Brehler46e83312021-03-25 00:11:39 +0100455include(iree_c_module)
Stella Laurenzo94363e22020-12-15 13:46:14 -0800456include(iree_python)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800457include(iree_lit_test)
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800458include(iree_llvm)
Geoffrey Martin-Noble4526dcc2020-03-09 11:59:52 -0700459include(iree_add_all_subdirs)
Geoffrey Martin-Noblee5fd5b52020-03-31 11:31:30 -0700460include(iree_check_test)
bjacob5feef482021-10-21 16:53:58 -0400461include(iree_trace_runner_test)
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -0800462include(iree_native_test)
Geoffrey Martin-Noble2811e502022-01-25 09:44:40 -0800463include(iree_cc_binary_benchmark)
Geoffrey Martin-Noble66d48892021-10-29 12:24:58 -0700464include(iree_benchmark_suite)
Han-Chung Wanga6fbb762022-04-15 14:25:04 -0700465include(iree_microbenchmark_suite)
Scott Todd434ff0e2021-12-21 10:38:36 -0800466include(iree_hal_cts_test_suite)
CindyLiuae72b952022-08-23 15:26:08 -0700467include(iree_static_linker_test)
Jerry Wu116db872022-09-22 19:33:13 +0000468include(iree_fetch_artifact)
CindyLiu080198f2022-10-06 07:21:17 -0700469include(iree_run_module_test)
Ben Vanik185d30c2019-09-19 14:24:11 -0700470
Marius Brehler06ac36e2020-01-10 14:44:11 -0800471set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
472
Ben Vanik185d30c2019-09-19 14:24:11 -0700473#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400474# IREE compilation flags
Scott Todd29d654e2020-06-11 15:24:17 -0700475#-------------------------------------------------------------------------------
476
Lei Zhangdd21f322020-09-10 10:47:33 -0400477iree_append_list_to_string(CMAKE_C_FLAGS_DEBUG ${IREE_C_FLAGS_DEBUG_LIST})
478iree_append_list_to_string(CMAKE_CXX_FLAGS_DEBUG ${IREE_CXX_FLAGS_DEBUG_LIST})
Scott Todd29d654e2020-06-11 15:24:17 -0700479
480set(CMAKE_CXX_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C++ compiler during fast builds." FORCE)
481set(CMAKE_C_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C compiler during fast builds." FORCE)
482set(CMAKE_EXE_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used for linking binaries during fast builds." FORCE)
483set(CMAKE_SHARED_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used by the shared libraries linker binaries during fast builds." FORCE)
484mark_as_advanced(
485 CMAKE_CXX_FLAGS_FASTBUILD
486 CMAKE_C_FLAGS_FASTBUILD
487 CMAKE_EXE_LINKER_FLAGS_FASTBUILD
488 CMAKE_SHARED_LINKER_FLAGS_FASTBUILD
489)
490
Scott Todd7649dee2022-07-18 11:31:46 -0700491# Override the system's default linker.
492# See also: https://llvm.org/docs/CMake.html#llvm-use-linker.
493set(IREE_USE_LINKER "" CACHE STRING "")
494# Equivalent to setting -DIREE_USE_LINKER=lld.
495# Note that unlike LLVM's LLVM_ENABLE_LLD, this does _not_ build lld. You will
496# need to either install a recent version of lld or build it from source prior
497# to setting this option. See also: https://lld.llvm.org/#using-lld.
498# This option is disabled on Apple platforms, where lld is not supported.
499cmake_dependent_option(IREE_ENABLE_LLD "Override the system's default linker to lld" OFF "NOT APPLE" OFF)
500
Scott Todd29d654e2020-06-11 15:24:17 -0700501include(iree_setup_toolchain)
502
503#-------------------------------------------------------------------------------
Stella Laurenzo29032b82021-10-14 15:21:44 -0700504# Python
505# If building features that require Python development, find them early in
506# one invocation (some CMake versions are sensitive to resolving out of order).
507# Otherwise, for features that just require the interpreter, find that alone.
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800508#-------------------------------------------------------------------------------
509
Stella Laurenzo29032b82021-10-14 15:21:44 -0700510if(IREE_BUILD_PYTHON_BINDINGS)
511 # After CMake 3.18, we are able to limit the scope of the search to just
512 # Development.Module. Searching for Development will fail in situations where
513 # the Python libraries are not available. When possible, limit to just
514 # Development.Module.
515 # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
Scott Todda24c8c52022-05-17 09:22:47 -0700516 #
517 # Configuring the Development.Module is flaky in multi-project setups.
518 # "Bootstrapping" by first looking for the optional Development component
519 # seems to be robust generally.
520 # See: https://reviews.llvm.org/D118148
521 find_package(Python3 COMPONENTS Interpreter Development)
522 find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700523elseif(IREE_BUILD_COMPILER OR IREE_BUILD_TESTS)
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800524 find_package(Python3 COMPONENTS Interpreter REQUIRED)
525endif()
526
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700527# Extended Python environment checks.
528if(Python3_FOUND)
529 iree_detect_pyyaml()
530endif()
531
532if(IREE_BUILD_TESTS AND NOT IREE_PYYAML_FOUND)
533 message(WARNING "IREE's regression test suite requires PyYAML to run all tests. It is not installed, so some tests will be disabled.")
534endif()
535
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800536#-------------------------------------------------------------------------------
Niloy Sikdar47238df2021-07-28 23:37:33 +0530537# Check if git submodules have been initialized.
538# This will only run if python3 is available.
539#-------------------------------------------------------------------------------
540
Marius Brehler776d7e62021-12-21 22:50:52 +0100541option(IREE_ERROR_ON_MISSING_SUBMODULES "Error if submodules have not been initialized." ON)
542
Niloy Sikdar47238df2021-07-28 23:37:33 +0530543find_package(Python3 COMPONENTS Interpreter QUIET)
Scott Todde5b269a2021-11-16 15:25:02 -0800544find_package(Git)
Marius Brehler776d7e62021-12-21 22:50:52 +0100545if(IREE_ERROR_ON_MISSING_SUBMODULES AND Python3_FOUND AND Git_FOUND)
Scott Todde5b269a2021-11-16 15:25:02 -0800546 # Only check submodule status when the git commit changes.
Niloy Sikdar47238df2021-07-28 23:37:33 +0530547 execute_process(
Scott Todde5b269a2021-11-16 15:25:02 -0800548 COMMAND git rev-parse --short HEAD
Niloy Sikdar47238df2021-07-28 23:37:33 +0530549 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Scott Todde5b269a2021-11-16 15:25:02 -0800550 RESULT_VARIABLE SHORT_HASH_RESULT
551 OUTPUT_VARIABLE SHORT_HASH)
552 string(REGEX REPLACE "\n$" "" SHORT_HASH "${SHORT_HASH}")
Scott Todd2a8cd3b2021-12-13 11:58:44 -0800553 if(SHORT_HASH_RESULT EQUAL "0" AND NOT "${IREE_GIT_SHORT_HASH}" STREQUAL "${SHORT_HASH}")
CindyLiu0e8fdc62022-10-14 09:35:44 -0700554 if(NOT IREE_BUILD_COMPILER)
555 set(CHECK_SUBMODULE_ARGS "--runtime_only")
556 endif()
Scott Todde5b269a2021-11-16 15:25:02 -0800557 execute_process(
CindyLiu0e8fdc62022-10-14 09:35:44 -0700558 COMMAND ${Python3_EXECUTABLE} build_tools/scripts/git/check_submodule_init.py ${CHECK_SUBMODULE_ARGS}
Scott Todde5b269a2021-11-16 15:25:02 -0800559 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
560 RESULT_VARIABLE SUBMODULE_INIT_RESULT
561 )
562 if(NOT SUBMODULE_INIT_RESULT EQUAL "0")
563 message(FATAL_ERROR "check_submodule_init.py failed, see the logs above")
564 else()
565 set(IREE_GIT_SHORT_HASH "${SHORT_HASH}" CACHE STRING "" FORCE)
566 endif()
Niloy Sikdar47238df2021-07-28 23:37:33 +0530567 endif()
568endif()
569
570#-------------------------------------------------------------------------------
Lei Zhang22f0e242020-03-30 12:09:20 -0700571# IREE top-level targets
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800572# We define these here because various things in the build tree adds
573# dependencies to them.
Lei Zhang22f0e242020-03-30 12:09:20 -0700574#-------------------------------------------------------------------------------
575
Jerry Wu57404972023-03-02 22:16:11 +0000576if(IREE_BUILD_BENCHMARKS OR IREE_BUILD_E2E_TEST_ARTIFACTS)
Scott Todd9ca75632022-04-13 10:47:11 -0700577 # Add top-level custom targets to drive generating benchmark suites.
578
579 # iree-benchmark-import-models imports benchmark models from their source
580 # formats, such as .tflite flatbuffers, to IREE-compatible .mlir files.
581 add_custom_target(iree-benchmark-import-models)
582
583 # iree-benchmark-suites fully prepares benchmark models for benchmarking:
584 # * importing from source formats to IREE-compatible .mlir files
585 # * compiling from .mlir files to benchmark-ready .vmfb files
586 # * generating flagfiles for executing the benchmark .vmfb files
Lei Zhangfe4403e2021-06-01 15:09:43 -0400587 add_custom_target(iree-benchmark-suites)
588endif()
589
Jerry Wud41d0732022-11-23 01:43:55 +0000590# TODO(#11263): This conditional block should be merged with the block above
591# once we remove IREE_BUILD_BENCHMARKS.
Jerry Wu57404972023-03-02 22:16:11 +0000592if(IREE_BUILD_E2E_TEST_ARTIFACTS)
Jerry Wufb87ad12022-12-21 05:59:02 +0000593 # iree-e2e-compile-stats-suites compiles the benchmark models with specific
594 # flags to collect the compilation statistics.
595 add_custom_target(iree-e2e-compile-stats-suites)
596
Jerry Wud41d0732022-11-23 01:43:55 +0000597 # iree-e2e-test-artifacts builds all e2e test artifacts, including benchmarks
598 # and model tests.
599 add_custom_target(iree-e2e-test-artifacts)
Jerry Wud41d0732022-11-23 01:43:55 +0000600 add_dependencies(iree-e2e-test-artifacts
601 iree-benchmark-import-models
602 iree-benchmark-suites
Jerry Wufb87ad12022-12-21 05:59:02 +0000603 iree-e2e-compile-stats-suites
Jerry Wud41d0732022-11-23 01:43:55 +0000604 )
605endif()
606
Scott Todd1a1aea62022-04-28 09:12:11 -0700607if(IREE_BUILD_MICROBENCHMARKS)
Han-Chung Wanga6fbb762022-04-15 14:25:04 -0700608 # Add top-level custom targets to drive generating microbenchmark suites.
609 add_custom_target(iree-microbenchmark-suites)
610endif()
611
Scott Todd1a1aea62022-04-28 09:12:11 -0700612if(IREE_BUILD_DOCS)
Lei Zhang22f0e242020-03-30 12:09:20 -0700613 # Add a top-level custom target to drive generating all documentation.
614 # Register it to the default target given that IREE_BUILD_DOCS is explicitly
615 # requested.
616 add_custom_target(iree-doc ALL)
617endif()
618
Jakub Kuderski3f764682023-02-07 17:14:23 -0500619# Samples may require additional files to be built/configured and will add
620# dependencies to this target.
621# Note: These will be automatically built with test dependencies
622# (`iree-test-deps`).
623add_custom_target(iree-sample-deps
624 COMMENT
625 "Building IREE sample data targets"
626)
627
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800628# Testing rules that require generation will add dependencies to this target.
629# This allows them to be EXCLUDE_FROM_ALL but still invokable.
Jakub Kuderski3f764682023-02-07 17:14:23 -0500630add_custom_target(iree-test-deps
631 COMMENT
632 "Building IREE test deps"
633 DEPENDS
634 iree-sample-deps
635)
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800636
CindyLiu99373e02022-10-12 09:22:24 -0700637# Testing rules that generate test scripts for iree-run-module-test will add
638# dependencies to this target. It is a subset of `iree-test-deps`.
639add_custom_target(iree-run-module-test-deps
640 COMMENT
641 "Building IREE run module test targets"
642)
643
Stella Laurenzoe2dfc652023-02-15 18:12:03 -0800644# Convenience target for running IREE tests.
645add_custom_target(iree-run-tests
646 COMMENT
647 "Run IREE unit tests"
648 WORKING_DIRECTORY
649 "${CMAKE_CURRENT_BINARY_DIR}"
650 USES_TERMINAL
651 COMMAND
652 "${CMAKE_COMMAND}" -E echo
653 "The 'iree-run-tests' target is a helper for running ctest. For advanced"
654 "options, build dependencies and invoke ctest independently as in:"
655 COMMAND
656 "${CMAKE_COMMAND}" -E echo
657 " \\(cd ${CMAKE_CURRENT_BINARY_DIR} \\&\\& cmake --build . --target iree-test-deps \\&\\& ctest --output-on-failure\\)"
658 COMMAND
659 "${CMAKE_COMMAND}" -E echo
660 "Run tests in parallel by setting a variable like CTEST_PARALLEL_LEVEL=25."
661 COMMAND
662 "${CMAKE_CTEST_COMMAND}"
663 --output-on-failure
664)
665add_dependencies(iree-run-tests iree-test-deps)
666
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800667#-------------------------------------------------------------------------------
668# CUDA configuration for both the compiler and runtime.
669# We do this at the top level so that we can fail fast and make global
670# decisions that effect both compiler and runtime. It also helps with error
671# messaging to do this all in one place, since we can provide very targeted
672# advice.
673#-------------------------------------------------------------------------------
674
675set(IREE_CUDA_LIBDEVICE_PATH "" CACHE FILEPATH "Absolute path to an appropriate libdevice.*.bc (needed to build the IREE cuda compiler target)")
676
677# If any CUDA features are being built, try to locate a CUDA SDK. We will fall
678# back to this as needed for specific features.
679if(IREE_TARGET_BACKEND_CUDA OR IREE_HAL_DRIVER_CUDA)
680 add_subdirectory(build_tools/third_party/cuda EXCLUDE_FROM_ALL)
681endif()
682
683#-------------------------------------------------------------------------------
684# MLIR/LLVM Dependency
685#-------------------------------------------------------------------------------
686
687if(NOT IREE_BUILD_COMPILER)
688 message(STATUS "Not adding LLVM/MLIR because the configuration does not require it")
689else()
Stella Laurenzo289b9a12023-02-24 13:06:32 -0800690 # Force enable BUILD_SHARED_LIBS for the compiler if instructed.
691 set(_IREE_ORIG_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
692 if(IREE_COMPILER_BUILD_SHARED_LIBS)
693 set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
694 endif()
695
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800696 # Get the main LLVM deps.
697 if(IREE_BUILD_BUNDLED_LLVM)
698 iree_llvm_configure_bundled()
699 else()
700 iree_llvm_configure_installed()
701 endif()
702
703 # Also add a library that can be depended on to get LLVM includes setup
704 # properly. bazel_to_cmake targets this for some header only pseudo deps.
705 add_library(IREELLVMIncludeSetup INTERFACE)
706 target_include_directories(IREELLVMIncludeSetup INTERFACE
707 ${LLVM_INCLUDE_DIRS}
708 ${MLIR_INCLUDE_DIRS}
709 ${LLD_INCLUDE_DIRS}
710 )
711
712 # Splice the includes setup into base LLVM libraries so that using them
713 # gets everything nice and tidy. It would be super if some day, LLVM
714 # libraries set their right usage requirements for includes. In the meantime
715 # we add usage requirements to libraries at the root of all things LLVM.
716 iree_llvm_add_usage_requirements(LLVMSupport IREELLVMIncludeSetup)
717 iree_llvm_add_usage_requirements(MLIRSupport IREELLVMIncludeSetup)
718
719 # Add default external projects.
720 iree_llvm_add_external_project(mlir-iree-dialects ${CMAKE_CURRENT_SOURCE_DIR}/llvm-external-projects/iree-dialects)
721 iree_llvm_add_external_project(mlir-hlo ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mlir-hlo)
722 if(IREE_INPUT_TORCH)
723 iree_llvm_add_external_project(torch-mlir-dialects ${CMAKE_CURRENT_SOURCE_DIR}/third_party/torch-mlir-dialects)
724 endif()
725
726 # Ensure that LLVM-based dependencies needed for testing are included.
727 add_dependencies(iree-test-deps FileCheck)
728 if(IREE_LLD_TARGET)
729 add_dependencies(iree-test-deps ${IREE_LLD_TARGET})
730 endif()
731 if(IREE_CLANG_TARGET)
732 add_dependencies(iree-test-deps ${IREE_CLANG_TARGET})
733 endif()
Stella Laurenzo289b9a12023-02-24 13:06:32 -0800734
735 set(BUILD_SHARED_LIBS ${_IREE_ORIG_BUILD_SHARED_LIBS} CACHE BOOL "" FORCE)
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800736endif()
737
738#-------------------------------------------------------------------------------
739# Other dependencies
740#-------------------------------------------------------------------------------
741
742include(external_cc_library)
743include(flatbuffer_c_library)
744
745add_subdirectory(build_tools/third_party/libyaml EXCLUDE_FROM_ALL)
746add_subdirectory(build_tools/third_party/llvm-project EXCLUDE_FROM_ALL)
747add_subdirectory(build_tools/third_party/tracy_client EXCLUDE_FROM_ALL)
748add_subdirectory(build_tools/third_party/vulkan_memory_allocator EXCLUDE_FROM_ALL)
749
750iree_set_googletest_cmake_options()
751add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
752
753if(IREE_ENABLE_THREADING)
754 iree_set_benchmark_cmake_options()
755 add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
756 if(IREE_ENABLE_CPUINFO)
757 iree_set_cpuinfo_cmake_options()
758 add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
759 endif()
760endif()
761
762# This defines the iree-flatcc-cli target, so we don't use EXCLUDE_FROM_ALL.
763add_subdirectory(build_tools/third_party/flatcc)
764
765if(IREE_HAL_DRIVER_CUDA)
766 add_subdirectory(build_tools/third_party/nccl EXCLUDE_FROM_ALL)
767endif()
768
769if(IREE_HAL_DRIVER_VULKAN)
770 add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
771endif()
772
773if(IREE_BUILD_COMPILER)
774 add_subdirectory(build_tools/third_party/mlir-hlo EXCLUDE_FROM_ALL)
775endif()
776
777if(IREE_BUILD_TESTS)
778 include(iree_configure_testing)
779endif()
780
781if(IREE_BUILD_PYTHON_BINDINGS)
782 if(NOT TARGET pybind11::module)
783 message(STATUS "Using bundled pybind11")
784 add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
785 else()
786 message(STATUS "Not including bundled pybind11 (already configured)")
787 endif()
788endif()
789
790if(IREE_TARGET_BACKEND_METAL_SPIRV)
791 iree_set_spirv_cross_cmake_options()
792 # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
793 add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL)
794endif()
795
796if(IREE_TARGET_BACKEND_WEBGPU)
797 # Tint is needed to compile SPIR-V into WGSL source code.
798 # Tint also requires SPIRV-Tools, which requires SPIRV-Headers.
799 iree_set_spirv_headers_cmake_options()
800 add_subdirectory(third_party/spirv_headers EXCLUDE_FROM_ALL)
801 add_subdirectory(build_tools/third_party/spirv-tools EXCLUDE_FROM_ALL)
802 add_subdirectory(build_tools/third_party/tint EXCLUDE_FROM_ALL)
803endif()
Stella Laurenzoe2dfc652023-02-15 18:12:03 -0800804
Lei Zhang22f0e242020-03-30 12:09:20 -0700805#-------------------------------------------------------------------------------
Ben Vanik185d30c2019-09-19 14:24:11 -0700806# IREE top-level libraries
807#-------------------------------------------------------------------------------
808
Rob Sudermand42e0022022-06-08 16:38:45 -0700809if(IREE_ENABLE_CLANG_TIDY)
810 set(CMAKE_CXX_CLANG_TIDY clang-tidy -warnings-as-errors=*)
811endif()
812
Marius Brehlerf5022e82019-12-13 15:20:25 -0800813add_subdirectory(build_tools/embed_data/)
814
Jerry Wu57404972023-03-02 22:16:11 +0000815if(IREE_BUILD_BENCHMARKS OR IREE_BUILD_E2E_TEST_ARTIFACTS)
Stella Laurenzoa6bf65c2022-01-11 20:47:51 -0800816 find_program(IREE_IMPORT_TFLITE_PATH iree-import-tflite)
817 if(IREE_IMPORT_TFLITE_PATH)
818 message(STATUS "Found ${IREE_IMPORT_TFLITE_PATH} to generate benchmark artifacts")
819 else()
820 message(STATUS "iree-import-tflite not found. Some benchmarks may not configure")
821 endif()
Jerry Wu60798ca2022-06-21 23:36:40 +0000822 find_program(IREE_IMPORT_TF_PATH iree-import-tf)
823 if(IREE_IMPORT_TF_PATH)
824 message(STATUS "Found ${IREE_IMPORT_TF_PATH} to generate benchmark artifacts")
825 else()
826 message(STATUS "iree-import-tf not found. Some benchmarks may not configure")
827 endif()
Jerry Wu16e2d9c2022-11-22 06:19:46 +0000828endif()
829
830# Note: Test deps are not built as part of all (use the iree-test-deps target).
831add_subdirectory(tests EXCLUDE_FROM_ALL)
832
833if(IREE_BUILD_BENCHMARKS)
Lei Zhang08cea042021-07-20 12:44:01 -0400834 add_subdirectory(benchmarks)
Lei Zhangff38a682021-06-08 21:12:24 -0400835endif()
836
Jacques Pienaar702929a2022-12-30 14:46:22 -0800837# tools/ can depend on compiler/ and runtime/.
838# Note: tools sub directory is added before compiler/ so that phony targets for
839# files with the same names from different rules are disambiguated towards
840# those in tools/.
841add_subdirectory(tools)
Stella Laurenzo309dc5b2023-02-22 23:07:43 -0800842add_subdirectory(compiler)
Scott Todd4f16b992022-05-17 10:33:53 -0700843add_subdirectory(runtime)
844
Rob Sudermand42e0022022-06-08 16:38:45 -0700845if(IREE_ENABLE_CLANG_TIDY)
846 set(CMAKE_CXX_CLANG_TIDY "")
847endif()
848
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700849if(IREE_BUILD_TRACY)
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700850 if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700851 message(WARNING "Building Tracy (IREE_BUILD_TRACY) on non-Linux is unsupported and may fail below.")
852 endif()
853 add_subdirectory(build_tools/third_party/tracy ${CMAKE_CURRENT_BINARY_DIR}/tracy)
854 if(NOT TARGET IREETracyCaptureServer)
855 message(SEND_ERROR "Could not build Tracy. Either unset IREE_BUILD_TRACY or look for missing dependencies above and install them.")
856 endif()
857endif()
858
Scott Toddf57ab752022-05-23 10:36:44 -0700859# Order constraint: The python bindings install tools targets from tools/
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700860# and tracy, and must come after it.
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700861if(IREE_BUILD_PYTHON_BINDINGS)
Stella Laurenzo60128592022-04-17 21:52:09 -0700862 # Write out a .env file to make IDEs and developers happy.
863 # Yes, we are writing this to the source dir. It is only for IDEs and if
Scott Toddfe9cb172022-12-08 09:17:52 -0800864 # it gets clobbered, it is fine (it is also ignored in .gitignore).
Stella Laurenzo41a2ceb2022-04-29 12:49:36 -0700865 set(_PYTHONPATH_ENV "PYTHONPATH=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}/compiler/bindings/python;${CMAKE_CURRENT_BINARY_DIR}/runtime/bindings/python>\n")
Scott Toddfe9cb172022-12-08 09:17:52 -0800866 file(GENERATE OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/.env" CONTENT "${_PYTHONPATH_ENV}")
867 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env" CONTENT "${_PYTHONPATH_ENV}")
868 # Similarly, write out .env.bat for Windows.
869 set(_PYTHONPATH_ENV_BAT "set PYTHONPATH=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}/compiler/bindings/python;${CMAKE_CURRENT_BINARY_DIR}/runtime/bindings/python>\n")
870 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env.bat" CONTENT "${_PYTHONPATH_ENV_BAT}")
Stella Laurenzo77a63cd2021-01-04 17:29:54 -0800871endif()
872
Scott Todd1a1aea62022-04-28 09:12:11 -0700873if(IREE_BUILD_BINDINGS_TFLITE)
Scott Toddada25e62022-05-03 16:16:15 -0700874 add_subdirectory(runtime/bindings/tflite)
Ben Vanikcd1132f2021-01-29 15:58:17 -0800875endif()
876
Scott Todd1a1aea62022-04-28 09:12:11 -0700877if(IREE_BUILD_EXPERIMENTAL_WEB_SAMPLES)
Scott Todd15fce0a2022-02-14 12:43:23 -0800878 add_subdirectory(experimental/web)
Scott Toddf237b5e2022-01-28 11:02:25 -0800879endif()
880
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700881set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
882 CACHE INTERNAL "IREE: Include Directories" FORCE)
Stella Laurenzo688670f2021-09-24 18:16:25 -0700883
Jerry Wu623d1742022-03-31 18:16:02 +0000884#-------------------------------------------------------------------------------
885# IREE benchmark tools
886#-------------------------------------------------------------------------------
887
888add_subdirectory(build_tools/benchmarks)
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700889
890#-------------------------------------------------------------------------------
Jerry Wu71562cc2022-09-20 18:56:41 -0700891# IREE build tools python modules
892#-------------------------------------------------------------------------------
893
894add_subdirectory(build_tools/python)
895
896#-------------------------------------------------------------------------------
Scott Todd4f16b992022-05-17 10:33:53 -0700897# Samples
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700898#-------------------------------------------------------------------------------
899
Scott Todd4c022df2022-05-10 16:55:18 -0700900# samples/ can depend on anything, so we include it last
Stella Laurenzo1e8d1fa2022-04-22 09:50:43 -0700901if(IREE_BUILD_SAMPLES)
902 add_subdirectory(samples)
903endif()
Geoffrey Martin-Noble10f18222022-06-09 16:00:13 -0700904
905if(IREE_BUILD_TESTS)
906 iree_create_ctest_customization()
907endif()