blob: 7c098dba2854144fb072c8361aa5b27a947071e8 [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
Stella Laurenzo0f005e52020-12-27 11:54:33 -080097#-------------------------------------------------------------------------------
98# Experimental project flags
99#-------------------------------------------------------------------------------
100
Ben Vanika372ab52022-07-25 18:40:22 -0700101option(IREE_BUILD_EXPERIMENTAL_VMVX_MMT4D "Enables MMT4D methods in the VMVX module." OFF)
Scott Toddf237b5e2022-01-28 11:02:25 -0800102option(IREE_BUILD_EXPERIMENTAL_WEB_SAMPLES "Builds experimental web samples." OFF)
Jerry Wu16e2d9c2022-11-22 06:19:46 +0000103option(IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS "Builds IREE E2E test artifacts suite generated by the e2e test framework." OFF)
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800104
105#-------------------------------------------------------------------------------
Scott Todd18a54852023-01-27 08:52:12 -0800106# CUDA Toolkit.
107#
108# Using the (optional) CUDA support in the compiler and runtime requires the
109# NVIDIA CUDA Toolkit. The toolkit can either be installed ahead of time or
110# it can be automatically downloaded on certain host architectures.
Scott Todd9fa5de72023-01-23 14:40:18 -0800111#-------------------------------------------------------------------------------
112
Scott Todd18a54852023-01-27 08:52:12 -0800113set(IREE_CUDA_AVAILABLE OFF)
114find_package(CUDAToolkit)
115if(CUDAToolkit_FOUND)
116 set(IREE_CUDA_AVAILABLE ON)
117else()
118 # We can download the SDK in build_tools/third_party/cuda/CMakeLists.txt, if
119 # on a supported platform/arch.
120 if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR WIN32)
121 if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)")
122 set(IREE_CUDA_AVAILABLE ON)
123 endif()
Scott Todd9fa5de72023-01-23 14:40:18 -0800124 endif()
125endif()
126
127#-------------------------------------------------------------------------------
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800128# Runtime HAL Driver Options
129# By default, all runtime drivers supported by the current platform which do
130# not require external deps are enabled by default. This can be changed with:
131# -DIREE_HAL_DRIVER_DEFAULTS=OFF
Ben Vanik185d30c2019-09-19 14:24:11 -0700132#-------------------------------------------------------------------------------
133
Ben Vanik4e6af052022-06-07 17:10:20 -0700134# External HAL drivers; see runtime/src/iree/hal/drivers/CMakeLists.txt for more
135# information on how to declare external drivers.
136set(IREE_EXTERNAL_HAL_DRIVERS "" CACHE STRING "")
137
Ben Vanik89160272022-09-26 16:44:11 -0700138# Additional executable loader deps to add dependent libraries to any target
139# using the default executable loader registration utilities.
140# TODO(benvanik): extend the deps to encompass the built-in loaders too so that
141# we have one flag. We could also support a list of deps and automatically
142# generate the registration from that via a configure file.
143set(IREE_HAL_EXECUTABLE_LOADER_EXTRA_DEPS "" CACHE STRING "")
144
Scott Toddd1620f02021-12-22 14:55:38 -0800145option(IREE_HAL_DRIVER_DEFAULTS "Sets the default value for all runtime HAL drivers" ON)
Ben Vanika9aaebe2022-12-16 09:14:53 -0800146
Scott Todd9fa5de72023-01-23 14:40:18 -0800147# CUDA support is enabled by default if the platform supports the CUDA SDK and
148# not cross compiling. Note: a CUDA-compatible GPU with drivers is still
149# required to actually run CUDA workloads.
150set(IREE_HAL_DRIVER_CUDA_DEFAULT ${IREE_HAL_DRIVER_DEFAULTS})
Scott Todd18a54852023-01-27 08:52:12 -0800151if(NOT IREE_CUDA_AVAILABLE OR CMAKE_CROSSCOMPILING)
Scott Todd9fa5de72023-01-23 14:40:18 -0800152 set(IREE_HAL_DRIVER_CUDA_DEFAULT OFF)
153endif()
154
155# Vulkan support is enabled by default if the platform might support Vulkan.
156# Apple platforms support Metal instead of Vulkan, though MoltenVK may work.
157set(IREE_HAL_DRIVER_VULKAN_DEFAULT ${IREE_HAL_DRIVER_DEFAULTS})
Scott Todd1a1aea62022-04-28 09:12:11 -0700158if(APPLE)
Scott Toddd1620f02021-12-22 14:55:38 -0800159 set(IREE_HAL_DRIVER_VULKAN_DEFAULT OFF)
Scott Toddd1620f02021-12-22 14:55:38 -0800160endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700161
Scott Toddd1620f02021-12-22 14:55:38 -0800162option(IREE_HAL_DRIVER_CUDA "Enables the 'cuda' runtime HAL driver" ${IREE_HAL_DRIVER_CUDA_DEFAULT})
Ben Vanike3058072022-06-06 19:21:44 -0700163option(IREE_HAL_DRIVER_LOCAL_SYNC "Enables the 'local-sync' runtime HAL driver" ${IREE_HAL_DRIVER_DEFAULTS})
164option(IREE_HAL_DRIVER_LOCAL_TASK "Enables the 'local-task' runtime HAL driver" ${IREE_HAL_DRIVER_DEFAULTS})
165option(IREE_HAL_DRIVER_VULKAN "Enables the 'vulkan' runtime HAL driver" ${IREE_HAL_DRIVER_VULKAN_DEFAULT})
166
Ben Vanik6e64b6e2022-06-07 09:14:53 -0700167option(IREE_HAL_EXECUTABLE_LOADER_DEFAULTS "Sets the default value for all runtime HAL executable loaders" ON)
168set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
169set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
170set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
Ben Vanike3058072022-06-06 19:21:44 -0700171
172# Emscripten builds don't support embedded ELF libraries.
173if(EMSCRIPTEN)
Ben Vanik2e8540e2022-06-06 20:39:34 -0700174 set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT OFF)
Ben Vanike3058072022-06-06 19:21:44 -0700175endif()
176
177# If forcing system libraries (for TSAN/debugging tools/etc) then ensure the
178# system library loader is linked in.
Scott Todd352da3f2022-07-20 15:25:11 -0700179if(IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER)
Ben Vanike3058072022-06-06 19:21:44 -0700180 set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT ON)
181endif()
182
Ben Vanik6e64b6e2022-06-07 09:14:53 -0700183# If no local driver is enabled then we force all the loaders off; this allows
184# for simpler checks that don't need to see if both the driver and loader is
185# available.
186if(NOT IREE_HAL_DRIVER_LOCAL_SYNC AND NOT IREE_HAL_DRIVER_LOCAL_TASK)
187 set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT OFF)
188 set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT OFF)
189 set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT OFF)
190endif()
191
Ben Vanik2e8540e2022-06-06 20:39:34 -0700192option(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 -0700193option(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY "Enables the system dynamic library loader for local HAL drivers" ${IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT})
194option(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE "Enables the VMVX module loader for local HAL drivers" ${IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT})
195
Scott Todd0f1f8eb2022-06-10 15:53:30 -0700196if(IREE_BUILD_COMPILER)
197 # The compiler requires the local task driver with the VMVX loader.
198 set(IREE_HAL_DRIVER_LOCAL_TASK ON)
199 set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE ON)
200endif()
201
Ben Vanike3058072022-06-06 19:21:44 -0700202message(STATUS "IREE HAL drivers:")
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800203if(IREE_HAL_DRIVER_CUDA)
204 message(STATUS " - cuda")
205endif()
Ben Vanike3058072022-06-06 19:21:44 -0700206if(IREE_HAL_DRIVER_LOCAL_SYNC)
207 message(STATUS " - local-sync")
208endif()
209if(IREE_HAL_DRIVER_LOCAL_TASK)
210 message(STATUS " - local-task")
211endif()
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800212if(IREE_HAL_DRIVER_VULKAN)
213 message(STATUS " - vulkan")
214endif()
Ben Vanik4e6af052022-06-07 17:10:20 -0700215if(IREE_EXTERNAL_HAL_DRIVERS)
216 message(STATUS " + external: ${IREE_EXTERNAL_HAL_DRIVERS}")
217endif()
Ben Vanik512d2d32019-09-20 13:22:34 -0700218
Ben Vanike3058072022-06-06 19:21:44 -0700219message(STATUS "IREE HAL local executable library loaders:")
Ben Vanik2e8540e2022-06-06 20:39:34 -0700220if(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF)
221 message(STATUS " - embedded-elf")
Ben Vanike3058072022-06-06 19:21:44 -0700222endif()
223if(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY)
224 message(STATUS " - system-library")
225endif()
226if(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE)
227 message(STATUS " - vmvx-module")
228endif()
229
Lei Zhang0d281b72020-06-01 20:00:23 -0400230#-------------------------------------------------------------------------------
Ben Vanik4e6af052022-06-07 17:10:20 -0700231# Experimental ROCM HAL driver
232#-------------------------------------------------------------------------------
233
234set(IREE_EXTERNAL_ROCM_HAL_DRIVER_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/experimental/rocm")
235set(IREE_EXTERNAL_ROCM_HAL_DRIVER_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/experimental/rocm")
236set(IREE_EXTERNAL_ROCM_HAL_DRIVER_TARGET "iree::experimental::rocm::registration")
237set(IREE_EXTERNAL_ROCM_HAL_DRIVER_REGISTER "iree_hal_rocm_driver_module_register")
238
239#-------------------------------------------------------------------------------
Scott Todd382fc632022-06-06 10:57:24 -0700240# Compiler Target Options
Scott Todd9fa5de72023-01-23 14:40:18 -0800241# By default, all compiler targets supported by the current platform are
242# enabled. Some compiler targets like CUDA will install external deps as
243# needed at configure time. This can be changed with:
Scott Todd382fc632022-06-06 10:57:24 -0700244# -DIREE_TARGET_BACKEND_DEFAULTS=OFF
245#-------------------------------------------------------------------------------
246
247option(IREE_TARGET_BACKEND_DEFAULTS "Sets the default value for all compiler target backends" ON)
248
249# The VMVX backend is always enabled.
250cmake_dependent_option(IREE_TARGET_BACKEND_VMVX "Enables the 'vmvx' compiler target backend" ON ${IREE_BUILD_COMPILER} OFF)
251
252# Supported default target backends.
Scott Todd352da3f2022-07-20 15:25:11 -0700253cmake_dependent_option(IREE_TARGET_BACKEND_LLVM_CPU "Enables the 'llvm-cpu' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_BUILD_COMPILER} OFF)
254cmake_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 -0700255cmake_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 -0700256cmake_dependent_option(IREE_TARGET_BACKEND_VULKAN_SPIRV "Enables the 'vulkan-spirv' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_BUILD_COMPILER} OFF)
257
Scott Todd9fa5de72023-01-23 14:40:18 -0800258# Supported default target backends that are only available on certain
259# platforms.
260set(IREE_TARGET_BACKEND_CUDA_DEFAULT ${IREE_TARGET_BACKEND_DEFAULTS})
Scott Todd18a54852023-01-27 08:52:12 -0800261if(NOT IREE_CUDA_AVAILABLE)
Scott Todd9fa5de72023-01-23 14:40:18 -0800262 set(IREE_TARGET_BACKEND_CUDA_DEFAULT OFF)
263endif()
264cmake_dependent_option(IREE_TARGET_BACKEND_CUDA "Enables the 'cuda' compiler target backend" ${IREE_TARGET_BACKEND_CUDA_DEFAULT} ${IREE_BUILD_COMPILER} OFF)
265
Scott Todd382fc632022-06-06 10:57:24 -0700266# Non-default target backends either have additional dependencies or are
267# experimental/niche in some fashion.
Scott Todd382fc632022-06-06 10:57:24 -0700268cmake_dependent_option(IREE_TARGET_BACKEND_ROCM "Enables the 'rocm' compiler target backend" OFF ${IREE_BUILD_COMPILER} OFF)
269# Disable WebGPU by default - it has complex deps and is under development.
270cmake_dependent_option(IREE_TARGET_BACKEND_WEBGPU "Enables the 'webgpu' compiler target backend" OFF ${IREE_BUILD_COMPILER} OFF)
271
272#-------------------------------------------------------------------------------
Scott Todde23d4a72022-07-27 14:16:39 -0700273# Compiler Input Dialects
274#-------------------------------------------------------------------------------
275
276cmake_dependent_option(IREE_INPUT_MHLO "Builds support for compiling MHLO programs" ON ${IREE_BUILD_COMPILER} OFF)
277cmake_dependent_option(IREE_INPUT_TORCH "Builds support for compiling Torch MLIR programs" ON ${IREE_BUILD_COMPILER} OFF)
278cmake_dependent_option(IREE_INPUT_TOSA "Builds support for compiling TOSA programs" ON ${IREE_BUILD_COMPILER} OFF)
279
Marius Brehler36f24512022-08-04 17:39:03 +0200280if(IREE_BUILD_COMPILER)
281 message(STATUS "IREE compiler input dialects:")
282 if(IREE_INPUT_MHLO)
283 message(STATUS " - MHLO")
284 endif()
285 if(IREE_INPUT_TORCH)
286 message(STATUS " - Torch MLIR")
287 endif()
288 if(IREE_INPUT_TOSA)
289 message(STATUS " - TOSA")
290 endif()
Scott Todde23d4a72022-07-27 14:16:39 -0700291endif()
292
293#-------------------------------------------------------------------------------
Scott Todd2a1925c2022-06-13 10:03:52 -0700294# Compiler Output Formats
295#-------------------------------------------------------------------------------
296
Scott Todd2a1925c2022-06-13 10:03:52 -0700297cmake_dependent_option(IREE_OUTPUT_FORMAT_C "Enables the 'vm-c' output format, using MLIR EmitC" ON ${IREE_BUILD_COMPILER} OFF)
298
Marius Brehler36f24512022-08-04 17:39:03 +0200299if(IREE_BUILD_COMPILER)
300 message(STATUS "IREE compiler output formats:")
301 if(IREE_OUTPUT_FORMAT_C)
302 message(STATUS " - C source module")
303 endif()
304 # The 'vm-bytecode' and 'vm-asm' formats are always enabled.
305 message(STATUS " - VM Bytecode")
306 message(STATUS " - VM MLIR Assembly")
Scott Todde23d4a72022-07-27 14:16:39 -0700307endif()
Scott Todde23d4a72022-07-27 14:16:39 -0700308
Scott Todd2a1925c2022-06-13 10:03:52 -0700309#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400310# IREE compilation toolchain configuration
311#-------------------------------------------------------------------------------
312
Lei Zhange88470f2020-09-08 13:21:09 -0400313option(IREE_ENABLE_ASAN "Enable address sanitizer" OFF)
314option(IREE_ENABLE_MSAN "Enable memory sanitizer" OFF)
315option(IREE_ENABLE_TSAN "Enable thread sanitizer" OFF)
bjacob7cf5b842022-04-04 16:48:04 -0400316option(IREE_BYTECODE_MODULE_ENABLE_TSAN "Enable thread sanitizer in IREE modules in tests" OFF)
Jakub Kuderski068b1722022-07-27 18:14:59 -0400317option(IREE_ENABLE_UBSAN "Enable undefined behavior sanitizer" OFF)
Stella Laurenzodeb48052022-11-25 10:41:46 -0800318option(IREE_ENABLE_SPLIT_DWARF "Enable gsplit-dwarf for debug information if the platform supports it" OFF)
319option(IREE_ENABLE_THIN_ARCHIVES "Enables thin ar archives (elf systems only). Disable for released static archives" OFF)
Ben Vanikadadd032022-11-28 23:18:16 -0800320option(IREE_LINK_COMPILER_SHARED_LIBRARY "Links IREE tools using the compiler compiled into a shared library" ON)
bjacob7cf5b842022-04-04 16:48:04 -0400321
322# STREQUAL feels wrong here - we don't care about the exact true-value used,
323# ON or TRUE or something else. But we haven't been able to think of a less bad
Scott Todd7df39732022-06-28 09:21:35 -0700324# alternative. https://github.com/iree-org/iree/pull/8474#discussion_r840790062
bjacob7cf5b842022-04-04 16:48:04 -0400325if(NOT IREE_ENABLE_TSAN STREQUAL IREE_BYTECODE_MODULE_ENABLE_TSAN)
326 message(SEND_ERROR
327 "IREE_ENABLE_TSAN and IREE_BYTECODE_MODULE_ENABLE_TSAN must be "
328 "simultaneously ON or OFF. "
329 "A discrepancy between the two would cause tests to crash as IREE "
330 "runtime code (controlled by IREE_ENABLE_TSAN) calls into test IREE "
331 "modules (controlled by IREE_BYTECODE_MODULE_ENABLE_TSAN)")
332endif()
333
334if(IREE_BYTECODE_MODULE_ENABLE_TSAN)
Scott Todd352da3f2022-07-20 15:25:11 -0700335 if(NOT IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER)
bjacob7cf5b842022-04-04 16:48:04 -0400336 message(SEND_ERROR
337 "When IREE_BYTECODE_MODULE_ENABLE_TSAN is ON, "
Scott Todd352da3f2022-07-20 15:25:11 -0700338 "IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER must also be ON. "
bjacob7cf5b842022-04-04 16:48:04 -0400339 "TSAN instrumentation is not currently supported in embedded modules.")
340 endif()
341endif()
342
Scott Todd86781382023-02-07 14:11:52 -0800343if(IREE_LINK_COMPILER_SHARED_LIBRARY AND IREE_ENABLE_COMPILER_TRACING)
344 message(SEND_ERROR
345 "IREE_ENABLE_COMPILER_TRACING requires "
346 "-DIREE_LINK_COMPILER_SHARED_LIBRARY=OFF (the compiler library must not "
347 "be unloaded before Tracy finishes, static linking is one workaround)")
348endif()
349
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800350option(IREE_ENABLE_CCACHE
351 "[DEPRECATED: Use CMAKE_<LANG>_COMPILER_LAUNCHER configure options or environment variables instead.] Use ccache if installed."
352 OFF)
bjacobe694d952020-11-03 12:05:35 -0500353
Scott Todd1a1aea62022-04-28 09:12:11 -0700354if(IREE_ENABLE_CCACHE)
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800355 message(WARNING
356 "IREE_ENABLE_CCACHE is deprecated. Use CMAKE_<LANG>_COMPILER_LAUNCHER"
357 " configure options or environment variables instead.")
bjacobe694d952020-11-03 12:05:35 -0500358 find_program(CCACHE_PROGRAM ccache)
359 if(CCACHE_PROGRAM)
360 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800361 else()
362 message(SEND_ERROR
363 "IREE_ENABLE_CCACHE was set, but executable `ccache` was not found.")
bjacobe694d952020-11-03 12:05:35 -0500364 endif()
365endif()
Lei Zhange88470f2020-09-08 13:21:09 -0400366
Geoffrey Martin-Noble3fa4f8d2021-09-10 09:25:27 -0700367option(IREE_DEV_MODE "Configure settings to optimize for IREE development (as opposed to CI or release)" OFF)
Stella Laurenzo5b639122021-06-18 14:44:10 -0700368
369#-------------------------------------------------------------------------------
370# IREE assertions
371# We don't love the way this is done, but we have to line it up with how LLVM
372# does it and not diverge, since all implementations and all header users must
373# have the same definition of NDEBUG.
374#
375# LLVM defaults LLVM_ENABLE_ASSERTIONS to ON for Debug builds only but then
376# conditions itself to only update flags if not building Debug. We just let
377# IREE_ENABLE_ASSERTIONS be not conditioned on anything and only update the
378# flags in appropriate build types.
379#
380# If IREE_ENABLE_ASSERTIONS is set ON manually, then
381# - NDEBUG must be undefined
382# - LLVM_ENABLE_ASSERTIONS is forced off in order to keep multiple parties
383# from mucking with globals.
384#
385# Since CMake forces NDEBUG for !Debug builds, some surgery needs to be done
386# at the top level to avoid divergence.
387#-------------------------------------------------------------------------------
388
389option(IREE_ENABLE_ASSERTIONS "Force unset of NDEBUG compile option" OFF)
390
391# Filter -DNDEBUG from CMAKE_CXX_FLAGS_* and CMAKE_C_FLAGS_* (if
392# CMAKE_BUILD_TYPE is not Debug).
393function(iree_fix_ndebug)
Scott Todd1a1aea62022-04-28 09:12:11 -0700394 string(TOUPPER "${CMAKE_BUILD_TYPE}" _UPPERCASE_CMAKE_BUILD_TYPE)
395 if(IREE_ENABLE_ASSERTIONS AND NOT "${_UPPERCASE_CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
Stella Laurenzo5b639122021-06-18 14:44:10 -0700396 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
Scott Todd1a1aea62022-04-28 09:12:11 -0700397 foreach(_FLAGS_VAR_TO_SCRUB
398 CMAKE_CXX_FLAGS_${_UPPERCASE_CMAKE_BUILD_TYPE}
399 CMAKE_C_FLAGS_${_UPPERCASE_CMAKE_BUILD_TYPE})
400 set(_ORIGINAL_FLAGS "${${_FLAGS_VAR_TO_SCRUB}}")
401 string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " _ALTERED_FLAGS "${_ORIGINAL_FLAGS}")
402 if(NOT "${_ORIGINAL_FLAGS}" STREQUAL "${_ALTERED_FLAGS}")
Stella Laurenzo5b639122021-06-18 14:44:10 -0700403 message(STATUS
Scott Todd1a1aea62022-04-28 09:12:11 -0700404 "IREE_ENABLE_ASSERTIONS force disabled NDEBUG for ${_FLAGS_VAR_TO_SCRUB}: '${_ORIGINAL_FLAGS}' -> '${_ALTERED_FLAGS}'")
405 set(${_FLAGS_VAR_TO_SCRUB} "${_ALTERED_FLAGS}" PARENT_SCOPE)
Stella Laurenzo5b639122021-06-18 14:44:10 -0700406 endif()
407 endforeach()
408
409 # Make sure that LLVM doesn't add its own logic for assertion disabling.
410 # We'd like to make sure that we are not dueling over globals.
411 set(LLVM_ENABLE_ASSERTIONS OFF PARENT_SCOPE)
412 endif()
413endfunction()
414iree_fix_ndebug()
415
Lei Zhange88470f2020-09-08 13:21:09 -0400416#-------------------------------------------------------------------------------
Lei Zhang7e253da2020-06-10 07:51:19 -0700417# IREE utility definitions
Lei Zhang0d281b72020-06-01 20:00:23 -0400418#-------------------------------------------------------------------------------
419
Scott Toddd1620f02021-12-22 14:55:38 -0800420list(APPEND CMAKE_MODULE_PATH
421 ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/
Scott Toddd1620f02021-12-22 14:55:38 -0800422)
423
Ben Vanik512d2d32019-09-20 13:22:34 -0700424include(iree_macros)
Ben Vanik185d30c2019-09-19 14:24:11 -0700425include(iree_copts)
Ben Vanik6b112ef2019-10-03 10:45:14 -0700426include(iree_cc_binary)
Ben Vanik185d30c2019-09-19 14:24:11 -0700427include(iree_cc_library)
428include(iree_cc_test)
Scott Todd20d746a2023-01-12 09:11:59 -0800429include(iree_import_binary)
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800430include(iree_external_cmake_options)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700431include(iree_tablegen_library)
Lei Zhang22f0e242020-03-30 12:09:20 -0700432include(iree_tablegen_doc)
CindyLiu40ed02d2021-04-22 16:16:28 +0000433include(iree_c_embed_data)
Ben Vanik7f6c57c2023-02-07 18:04:32 -0800434include(iree_bitcode_library)
Scott Todd11adcab2019-12-18 14:10:44 -0800435include(iree_bytecode_module)
Marius Brehler46e83312021-03-25 00:11:39 +0100436include(iree_c_module)
Stella Laurenzo94363e22020-12-15 13:46:14 -0800437include(iree_python)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800438include(iree_lit_test)
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800439include(iree_llvm)
Geoffrey Martin-Noble4526dcc2020-03-09 11:59:52 -0700440include(iree_add_all_subdirs)
Geoffrey Martin-Noblee5fd5b52020-03-31 11:31:30 -0700441include(iree_check_test)
bjacob5feef482021-10-21 16:53:58 -0400442include(iree_trace_runner_test)
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -0800443include(iree_native_test)
Geoffrey Martin-Noble2811e502022-01-25 09:44:40 -0800444include(iree_cc_binary_benchmark)
Geoffrey Martin-Noble66d48892021-10-29 12:24:58 -0700445include(iree_benchmark_suite)
Han-Chung Wanga6fbb762022-04-15 14:25:04 -0700446include(iree_microbenchmark_suite)
Scott Todd434ff0e2021-12-21 10:38:36 -0800447include(iree_hal_cts_test_suite)
CindyLiuae72b952022-08-23 15:26:08 -0700448include(iree_static_linker_test)
Jerry Wu116db872022-09-22 19:33:13 +0000449include(iree_fetch_artifact)
CindyLiu080198f2022-10-06 07:21:17 -0700450include(iree_run_module_test)
Ben Vanik185d30c2019-09-19 14:24:11 -0700451
Marius Brehler06ac36e2020-01-10 14:44:11 -0800452set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
453
Ben Vanik185d30c2019-09-19 14:24:11 -0700454#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400455# IREE compilation flags
Scott Todd29d654e2020-06-11 15:24:17 -0700456#-------------------------------------------------------------------------------
457
Lei Zhangdd21f322020-09-10 10:47:33 -0400458iree_append_list_to_string(CMAKE_C_FLAGS_DEBUG ${IREE_C_FLAGS_DEBUG_LIST})
459iree_append_list_to_string(CMAKE_CXX_FLAGS_DEBUG ${IREE_CXX_FLAGS_DEBUG_LIST})
Scott Todd29d654e2020-06-11 15:24:17 -0700460
461set(CMAKE_CXX_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C++ compiler during fast builds." FORCE)
462set(CMAKE_C_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C compiler during fast builds." FORCE)
463set(CMAKE_EXE_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used for linking binaries during fast builds." FORCE)
464set(CMAKE_SHARED_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used by the shared libraries linker binaries during fast builds." FORCE)
465mark_as_advanced(
466 CMAKE_CXX_FLAGS_FASTBUILD
467 CMAKE_C_FLAGS_FASTBUILD
468 CMAKE_EXE_LINKER_FLAGS_FASTBUILD
469 CMAKE_SHARED_LINKER_FLAGS_FASTBUILD
470)
471
Scott Todd7649dee2022-07-18 11:31:46 -0700472# Override the system's default linker.
473# See also: https://llvm.org/docs/CMake.html#llvm-use-linker.
474set(IREE_USE_LINKER "" CACHE STRING "")
475# Equivalent to setting -DIREE_USE_LINKER=lld.
476# Note that unlike LLVM's LLVM_ENABLE_LLD, this does _not_ build lld. You will
477# need to either install a recent version of lld or build it from source prior
478# to setting this option. See also: https://lld.llvm.org/#using-lld.
479# This option is disabled on Apple platforms, where lld is not supported.
480cmake_dependent_option(IREE_ENABLE_LLD "Override the system's default linker to lld" OFF "NOT APPLE" OFF)
481
Scott Todd29d654e2020-06-11 15:24:17 -0700482include(iree_setup_toolchain)
483
484#-------------------------------------------------------------------------------
Stella Laurenzo29032b82021-10-14 15:21:44 -0700485# Python
486# If building features that require Python development, find them early in
487# one invocation (some CMake versions are sensitive to resolving out of order).
488# Otherwise, for features that just require the interpreter, find that alone.
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800489#-------------------------------------------------------------------------------
490
Stella Laurenzo29032b82021-10-14 15:21:44 -0700491if(IREE_BUILD_PYTHON_BINDINGS)
492 # After CMake 3.18, we are able to limit the scope of the search to just
493 # Development.Module. Searching for Development will fail in situations where
494 # the Python libraries are not available. When possible, limit to just
495 # Development.Module.
496 # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
Scott Todda24c8c52022-05-17 09:22:47 -0700497 #
498 # Configuring the Development.Module is flaky in multi-project setups.
499 # "Bootstrapping" by first looking for the optional Development component
500 # seems to be robust generally.
501 # See: https://reviews.llvm.org/D118148
502 find_package(Python3 COMPONENTS Interpreter Development)
503 find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700504elseif(IREE_BUILD_COMPILER OR IREE_BUILD_TESTS)
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800505 find_package(Python3 COMPONENTS Interpreter REQUIRED)
506endif()
507
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700508# Extended Python environment checks.
509if(Python3_FOUND)
510 iree_detect_pyyaml()
511endif()
512
513if(IREE_BUILD_TESTS AND NOT IREE_PYYAML_FOUND)
514 message(WARNING "IREE's regression test suite requires PyYAML to run all tests. It is not installed, so some tests will be disabled.")
515endif()
516
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800517#-------------------------------------------------------------------------------
Niloy Sikdar47238df2021-07-28 23:37:33 +0530518# Check if git submodules have been initialized.
519# This will only run if python3 is available.
520#-------------------------------------------------------------------------------
521
Marius Brehler776d7e62021-12-21 22:50:52 +0100522option(IREE_ERROR_ON_MISSING_SUBMODULES "Error if submodules have not been initialized." ON)
523
Niloy Sikdar47238df2021-07-28 23:37:33 +0530524find_package(Python3 COMPONENTS Interpreter QUIET)
Scott Todde5b269a2021-11-16 15:25:02 -0800525find_package(Git)
Marius Brehler776d7e62021-12-21 22:50:52 +0100526if(IREE_ERROR_ON_MISSING_SUBMODULES AND Python3_FOUND AND Git_FOUND)
Scott Todde5b269a2021-11-16 15:25:02 -0800527 # Only check submodule status when the git commit changes.
Niloy Sikdar47238df2021-07-28 23:37:33 +0530528 execute_process(
Scott Todde5b269a2021-11-16 15:25:02 -0800529 COMMAND git rev-parse --short HEAD
Niloy Sikdar47238df2021-07-28 23:37:33 +0530530 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Scott Todde5b269a2021-11-16 15:25:02 -0800531 RESULT_VARIABLE SHORT_HASH_RESULT
532 OUTPUT_VARIABLE SHORT_HASH)
533 string(REGEX REPLACE "\n$" "" SHORT_HASH "${SHORT_HASH}")
Scott Todd2a8cd3b2021-12-13 11:58:44 -0800534 if(SHORT_HASH_RESULT EQUAL "0" AND NOT "${IREE_GIT_SHORT_HASH}" STREQUAL "${SHORT_HASH}")
CindyLiu0e8fdc62022-10-14 09:35:44 -0700535 if(NOT IREE_BUILD_COMPILER)
536 set(CHECK_SUBMODULE_ARGS "--runtime_only")
537 endif()
Scott Todde5b269a2021-11-16 15:25:02 -0800538 execute_process(
CindyLiu0e8fdc62022-10-14 09:35:44 -0700539 COMMAND ${Python3_EXECUTABLE} build_tools/scripts/git/check_submodule_init.py ${CHECK_SUBMODULE_ARGS}
Scott Todde5b269a2021-11-16 15:25:02 -0800540 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
541 RESULT_VARIABLE SUBMODULE_INIT_RESULT
542 )
543 if(NOT SUBMODULE_INIT_RESULT EQUAL "0")
544 message(FATAL_ERROR "check_submodule_init.py failed, see the logs above")
545 else()
546 set(IREE_GIT_SHORT_HASH "${SHORT_HASH}" CACHE STRING "" FORCE)
547 endif()
Niloy Sikdar47238df2021-07-28 23:37:33 +0530548 endif()
549endif()
550
551#-------------------------------------------------------------------------------
Stella Laurenzo98388662022-02-03 15:37:30 -0800552# CUDA configuration for both the compiler and runtime.
553# We do this at the top level so that we can fail fast and make global
554# decisions that effect both compiler and runtime. It also helps with error
555# messaging to do this all in one place, since we can provide very targeted
556# advice.
557#-------------------------------------------------------------------------------
558
Lei Zhang299fc932022-12-08 16:04:35 -0800559set(IREE_CUDA_LIBDEVICE_PATH "" CACHE FILEPATH "Absolute path to an appropriate libdevice.*.bc (needed to build the IREE cuda compiler target)")
Stella Laurenzo98388662022-02-03 15:37:30 -0800560
561# If any CUDA features are being built, try to locate a CUDA SDK. We will fall
562# back to this as needed for specific features.
563if(IREE_TARGET_BACKEND_CUDA OR IREE_HAL_DRIVER_CUDA)
Stella Laurenzoa9d23f62022-12-27 17:28:58 -0800564 add_subdirectory(build_tools/third_party/cuda EXCLUDE_FROM_ALL)
Stella Laurenzo98388662022-02-03 15:37:30 -0800565endif()
566
567#-------------------------------------------------------------------------------
Stella Laurenzo382122d2020-06-11 16:18:09 -0700568# MLIR/LLVM Dependency
Stella Laurenzo382122d2020-06-11 16:18:09 -0700569#-------------------------------------------------------------------------------
570
Stella Laurenzo275215d2021-08-06 11:50:28 -0700571if(NOT IREE_BUILD_COMPILER)
572 message(STATUS "Not adding LLVM/MLIR because the configuration does not require it")
Stella Laurenzo275215d2021-08-06 11:50:28 -0700573else()
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800574 # Get the main LLVM deps.
575 if(IREE_BUILD_BUNDLED_LLVM)
576 iree_llvm_configure_bundled()
577 else()
578 iree_llvm_configure_installed()
Stella Laurenzo688670f2021-09-24 18:16:25 -0700579 endif()
580
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800581 # Also add a library that can be depended on to get LLVM includes setup
582 # properly. bazel_to_cmake targets this for some header only pseudo deps.
583 add_library(IREELLVMIncludeSetup INTERFACE)
584 target_include_directories(IREELLVMIncludeSetup INTERFACE
585 ${LLVM_INCLUDE_DIRS}
586 ${MLIR_INCLUDE_DIRS}
587 ${LLD_INCLUDE_DIRS}
588 )
Stella Laurenzo275215d2021-08-06 11:50:28 -0700589
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800590 # Splice the includes setup into base LLVM libraries so that using them
591 # gets everything nice and tidy. It would be super if some day, LLVM
592 # libraries set their right usage requirements for includes. In the meantime
593 # we add usage requirements to libraries at the root of all things LLVM.
594 iree_llvm_add_usage_requirements(LLVMSupport IREELLVMIncludeSetup)
595 iree_llvm_add_usage_requirements(MLIRSupport IREELLVMIncludeSetup)
Ben Vanik89a77fa2020-10-07 17:19:31 -0700596
Stella Laurenzo62fbef02021-08-09 20:52:48 -0700597 # Add default external projects.
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800598 iree_llvm_add_external_project(mlir-iree-dialects ${CMAKE_CURRENT_SOURCE_DIR}/llvm-external-projects/iree-dialects)
599 iree_llvm_add_external_project(mlir-hlo ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mlir-hlo)
Scott Todde23d4a72022-07-27 14:16:39 -0700600 if(IREE_INPUT_TORCH)
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800601 iree_llvm_add_external_project(torch-mlir-dialects ${CMAKE_CURRENT_SOURCE_DIR}/third_party/torch-mlir-dialects)
Yi Zhangc35fee82022-04-06 14:35:28 -0400602 endif()
Stella Laurenzo382122d2020-06-11 16:18:09 -0700603endif()
604
Stella Laurenzo382122d2020-06-11 16:18:09 -0700605#-------------------------------------------------------------------------------
Scott Todd8b0b0cd2021-12-16 15:13:57 -0800606# Other dependencies
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800607#-------------------------------------------------------------------------------
608
Ben Vanik512d2d32019-09-20 13:22:34 -0700609include(external_cc_library)
Ben Vanik2d1808b2020-07-17 19:02:16 -0700610include(flatbuffer_c_library)
Ben Vanik512d2d32019-09-20 13:22:34 -0700611
Ben Vanika9036492021-06-25 09:39:53 -0700612add_subdirectory(build_tools/third_party/libyaml EXCLUDE_FROM_ALL)
Jerry Wuae05f302022-05-17 13:58:13 +0000613add_subdirectory(build_tools/third_party/llvm-project EXCLUDE_FROM_ALL)
Stella Laurenzo4cf4b5a2022-12-27 14:01:06 -0800614add_subdirectory(build_tools/third_party/tracy_client EXCLUDE_FROM_ALL)
Ben Vanika9036492021-06-25 09:39:53 -0700615add_subdirectory(build_tools/third_party/vulkan_memory_allocator EXCLUDE_FROM_ALL)
Ben Vanik512d2d32019-09-20 13:22:34 -0700616
Stella Laurenzo275215d2021-08-06 11:50:28 -0700617iree_set_googletest_cmake_options()
Marius Brehlerf5022e82019-12-13 15:20:25 -0800618add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700619
620if(IREE_ENABLE_THREADING)
621 iree_set_benchmark_cmake_options()
Cindy Liu331946c2021-06-01 12:20:30 -0700622 add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
Ben Vanik4aae0032022-04-19 15:49:26 -0700623 if(IREE_ENABLE_CPUINFO)
624 iree_set_cpuinfo_cmake_options()
625 add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
626 endif()
Cindy Liu331946c2021-06-01 12:20:30 -0700627endif()
Stella Laurenzo275215d2021-08-06 11:50:28 -0700628
Scott Todd20d746a2023-01-12 09:11:59 -0800629# This defines the iree-flatcc-cli target, so we don't use EXCLUDE_FROM_ALL.
630add_subdirectory(build_tools/third_party/flatcc)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700631
Okwan Kwonc31c8a02023-01-13 16:36:40 -0800632if(IREE_HAL_DRIVER_CUDA)
633 add_subdirectory(build_tools/third_party/nccl EXCLUDE_FROM_ALL)
634endif()
635
Marius Brehler9b86ad52022-06-08 17:09:02 +0200636if(IREE_HAL_DRIVER_VULKAN)
637 add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
638endif()
Ben Vanik185d30c2019-09-19 14:24:11 -0700639
Stella Laurenzo275215d2021-08-06 11:50:28 -0700640if(IREE_BUILD_COMPILER)
Marius Brehler3eaf4442021-01-13 18:22:08 +0100641 add_subdirectory(build_tools/third_party/mlir-hlo EXCLUDE_FROM_ALL)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700642endif()
643
Stella Laurenzo275215d2021-08-06 11:50:28 -0700644if(IREE_BUILD_TESTS)
Geoffrey Martin-Noble10f18222022-06-09 16:00:13 -0700645 include(iree_configure_testing)
Marius Brehler29676502019-12-27 17:07:10 -0800646endif()
647
MaheshRavishankar1547ac22021-09-23 15:22:46 -0700648if(IREE_BUILD_PYTHON_BINDINGS)
Stella Laurenzo95ad84c2021-09-24 14:56:01 -0700649 if(NOT TARGET pybind11::module)
650 message(STATUS "Using bundled pybind11")
651 add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
652 else()
653 message(STATUS "Not including bundled pybind11 (already configured)")
654 endif()
Marius Brehlerf3d73c92020-01-16 16:11:52 -0800655endif()
656
Scott Toddd1620f02021-12-22 14:55:38 -0800657if(IREE_TARGET_BACKEND_METAL_SPIRV)
Stella Laurenzo275215d2021-08-06 11:50:28 -0700658 iree_set_spirv_cross_cmake_options()
Lei Zhang3cbb28e2020-09-22 15:30:27 -0400659 # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
660 add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL)
661endif()
662
Scott Toddf8519ca2021-12-16 16:01:49 -0800663if(IREE_TARGET_BACKEND_WEBGPU)
664 # Tint is needed to compile SPIR-V into WGSL source code.
665 # Tint also requires SPIRV-Tools, which requires SPIRV-Headers.
666 iree_set_spirv_headers_cmake_options()
667 add_subdirectory(third_party/spirv_headers EXCLUDE_FROM_ALL)
668 add_subdirectory(build_tools/third_party/spirv-tools EXCLUDE_FROM_ALL)
669 add_subdirectory(build_tools/third_party/tint EXCLUDE_FROM_ALL)
670endif()
671
Ben Vanik185d30c2019-09-19 14:24:11 -0700672#-------------------------------------------------------------------------------
Lei Zhang22f0e242020-03-30 12:09:20 -0700673# IREE top-level targets
674#-------------------------------------------------------------------------------
675
Jerry Wu16e2d9c2022-11-22 06:19:46 +0000676if(IREE_BUILD_BENCHMARKS OR IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS)
Scott Todd9ca75632022-04-13 10:47:11 -0700677 # Add top-level custom targets to drive generating benchmark suites.
678
679 # iree-benchmark-import-models imports benchmark models from their source
680 # formats, such as .tflite flatbuffers, to IREE-compatible .mlir files.
681 add_custom_target(iree-benchmark-import-models)
682
683 # iree-benchmark-suites fully prepares benchmark models for benchmarking:
684 # * importing from source formats to IREE-compatible .mlir files
685 # * compiling from .mlir files to benchmark-ready .vmfb files
686 # * generating flagfiles for executing the benchmark .vmfb files
Lei Zhangfe4403e2021-06-01 15:09:43 -0400687 add_custom_target(iree-benchmark-suites)
688endif()
689
Jerry Wud41d0732022-11-23 01:43:55 +0000690# TODO(#11263): This conditional block should be merged with the block above
691# once we remove IREE_BUILD_BENCHMARKS.
692if(IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS)
Jerry Wufb87ad12022-12-21 05:59:02 +0000693 # iree-e2e-compile-stats-suites compiles the benchmark models with specific
694 # flags to collect the compilation statistics.
695 add_custom_target(iree-e2e-compile-stats-suites)
696
Jerry Wud41d0732022-11-23 01:43:55 +0000697 # iree-e2e-test-artifacts builds all e2e test artifacts, including benchmarks
698 # and model tests.
699 add_custom_target(iree-e2e-test-artifacts)
Jerry Wud41d0732022-11-23 01:43:55 +0000700 add_dependencies(iree-e2e-test-artifacts
701 iree-benchmark-import-models
702 iree-benchmark-suites
Jerry Wufb87ad12022-12-21 05:59:02 +0000703 iree-e2e-compile-stats-suites
Jerry Wud41d0732022-11-23 01:43:55 +0000704 )
705endif()
706
Scott Todd1a1aea62022-04-28 09:12:11 -0700707if(IREE_BUILD_MICROBENCHMARKS)
Han-Chung Wanga6fbb762022-04-15 14:25:04 -0700708 # Add top-level custom targets to drive generating microbenchmark suites.
709 add_custom_target(iree-microbenchmark-suites)
710endif()
711
Scott Todd1a1aea62022-04-28 09:12:11 -0700712if(IREE_BUILD_DOCS)
Lei Zhang22f0e242020-03-30 12:09:20 -0700713 # Add a top-level custom target to drive generating all documentation.
714 # Register it to the default target given that IREE_BUILD_DOCS is explicitly
715 # requested.
716 add_custom_target(iree-doc ALL)
717endif()
718
Jakub Kuderski3f764682023-02-07 17:14:23 -0500719# Samples may require additional files to be built/configured and will add
720# dependencies to this target.
721# Note: These will be automatically built with test dependencies
722# (`iree-test-deps`).
723add_custom_target(iree-sample-deps
724 COMMENT
725 "Building IREE sample data targets"
726)
727
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800728# Testing rules that require generation will add dependencies to this target.
729# This allows them to be EXCLUDE_FROM_ALL but still invokable.
Jakub Kuderski3f764682023-02-07 17:14:23 -0500730add_custom_target(iree-test-deps
731 COMMENT
732 "Building IREE test deps"
733 DEPENDS
734 iree-sample-deps
735)
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800736
CindyLiu99373e02022-10-12 09:22:24 -0700737# Testing rules that generate test scripts for iree-run-module-test will add
738# dependencies to this target. It is a subset of `iree-test-deps`.
739add_custom_target(iree-run-module-test-deps
740 COMMENT
741 "Building IREE run module test targets"
742)
743
Stella Laurenzoe2dfc652023-02-15 18:12:03 -0800744# Convenience target for running IREE tests.
745add_custom_target(iree-run-tests
746 COMMENT
747 "Run IREE unit tests"
748 WORKING_DIRECTORY
749 "${CMAKE_CURRENT_BINARY_DIR}"
750 USES_TERMINAL
751 COMMAND
752 "${CMAKE_COMMAND}" -E echo
753 "The 'iree-run-tests' target is a helper for running ctest. For advanced"
754 "options, build dependencies and invoke ctest independently as in:"
755 COMMAND
756 "${CMAKE_COMMAND}" -E echo
757 " \\(cd ${CMAKE_CURRENT_BINARY_DIR} \\&\\& cmake --build . --target iree-test-deps \\&\\& ctest --output-on-failure\\)"
758 COMMAND
759 "${CMAKE_COMMAND}" -E echo
760 "Run tests in parallel by setting a variable like CTEST_PARALLEL_LEVEL=25."
761 COMMAND
762 "${CMAKE_CTEST_COMMAND}"
763 --output-on-failure
764)
765add_dependencies(iree-run-tests iree-test-deps)
766
767
Lei Zhang22f0e242020-03-30 12:09:20 -0700768#-------------------------------------------------------------------------------
Ben Vanik185d30c2019-09-19 14:24:11 -0700769# IREE top-level libraries
770#-------------------------------------------------------------------------------
771
Rob Sudermand42e0022022-06-08 16:38:45 -0700772if(IREE_ENABLE_CLANG_TIDY)
773 set(CMAKE_CXX_CLANG_TIDY clang-tidy -warnings-as-errors=*)
774endif()
775
Marius Brehlerf5022e82019-12-13 15:20:25 -0800776add_subdirectory(build_tools/embed_data/)
777
Jerry Wu16e2d9c2022-11-22 06:19:46 +0000778if(IREE_BUILD_BENCHMARKS OR IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS)
Stella Laurenzoa6bf65c2022-01-11 20:47:51 -0800779 find_program(IREE_IMPORT_TFLITE_PATH iree-import-tflite)
780 if(IREE_IMPORT_TFLITE_PATH)
781 message(STATUS "Found ${IREE_IMPORT_TFLITE_PATH} to generate benchmark artifacts")
782 else()
783 message(STATUS "iree-import-tflite not found. Some benchmarks may not configure")
784 endif()
Jerry Wu60798ca2022-06-21 23:36:40 +0000785 find_program(IREE_IMPORT_TF_PATH iree-import-tf)
786 if(IREE_IMPORT_TF_PATH)
787 message(STATUS "Found ${IREE_IMPORT_TF_PATH} to generate benchmark artifacts")
788 else()
789 message(STATUS "iree-import-tf not found. Some benchmarks may not configure")
790 endif()
Jerry Wu16e2d9c2022-11-22 06:19:46 +0000791endif()
792
793# Note: Test deps are not built as part of all (use the iree-test-deps target).
794add_subdirectory(tests EXCLUDE_FROM_ALL)
795
796if(IREE_BUILD_BENCHMARKS)
Lei Zhang08cea042021-07-20 12:44:01 -0400797 add_subdirectory(benchmarks)
Lei Zhangff38a682021-06-08 21:12:24 -0400798endif()
799
Jacques Pienaar702929a2022-12-30 14:46:22 -0800800# tools/ can depend on compiler/ and runtime/.
801# Note: tools sub directory is added before compiler/ so that phony targets for
802# files with the same names from different rules are disambiguated towards
803# those in tools/.
804add_subdirectory(tools)
805
Scott Todd1a1aea62022-04-28 09:12:11 -0700806if(IREE_BUILD_COMPILER)
Stella Laurenzo41a2ceb2022-04-29 12:49:36 -0700807 add_subdirectory(compiler)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700808endif()
Ben Vanik6b112ef2019-10-03 10:45:14 -0700809
Scott Todd4f16b992022-05-17 10:33:53 -0700810add_subdirectory(runtime)
811
Rob Sudermand42e0022022-06-08 16:38:45 -0700812if(IREE_ENABLE_CLANG_TIDY)
813 set(CMAKE_CXX_CLANG_TIDY "")
814endif()
815
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700816if(IREE_BUILD_TRACY)
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700817 if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700818 message(WARNING "Building Tracy (IREE_BUILD_TRACY) on non-Linux is unsupported and may fail below.")
819 endif()
820 add_subdirectory(build_tools/third_party/tracy ${CMAKE_CURRENT_BINARY_DIR}/tracy)
821 if(NOT TARGET IREETracyCaptureServer)
822 message(SEND_ERROR "Could not build Tracy. Either unset IREE_BUILD_TRACY or look for missing dependencies above and install them.")
823 endif()
824endif()
825
Scott Toddf57ab752022-05-23 10:36:44 -0700826# Order constraint: The python bindings install tools targets from tools/
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700827# and tracy, and must come after it.
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700828if(IREE_BUILD_PYTHON_BINDINGS)
Stella Laurenzo60128592022-04-17 21:52:09 -0700829 # Write out a .env file to make IDEs and developers happy.
830 # Yes, we are writing this to the source dir. It is only for IDEs and if
Scott Toddfe9cb172022-12-08 09:17:52 -0800831 # it gets clobbered, it is fine (it is also ignored in .gitignore).
Stella Laurenzo41a2ceb2022-04-29 12:49:36 -0700832 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 -0800833 file(GENERATE OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/.env" CONTENT "${_PYTHONPATH_ENV}")
834 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env" CONTENT "${_PYTHONPATH_ENV}")
835 # Similarly, write out .env.bat for Windows.
836 set(_PYTHONPATH_ENV_BAT "set PYTHONPATH=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}/compiler/bindings/python;${CMAKE_CURRENT_BINARY_DIR}/runtime/bindings/python>\n")
837 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env.bat" CONTENT "${_PYTHONPATH_ENV_BAT}")
Stella Laurenzo77a63cd2021-01-04 17:29:54 -0800838endif()
839
Scott Todd1a1aea62022-04-28 09:12:11 -0700840if(IREE_BUILD_BINDINGS_TFLITE)
Scott Toddada25e62022-05-03 16:16:15 -0700841 add_subdirectory(runtime/bindings/tflite)
Ben Vanikcd1132f2021-01-29 15:58:17 -0800842endif()
843
Scott Todd1a1aea62022-04-28 09:12:11 -0700844if(IREE_BUILD_EXPERIMENTAL_WEB_SAMPLES)
Scott Todd15fce0a2022-02-14 12:43:23 -0800845 add_subdirectory(experimental/web)
Scott Toddf237b5e2022-01-28 11:02:25 -0800846endif()
847
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700848set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
849 CACHE INTERNAL "IREE: Include Directories" FORCE)
Stella Laurenzo688670f2021-09-24 18:16:25 -0700850
Jerry Wu623d1742022-03-31 18:16:02 +0000851#-------------------------------------------------------------------------------
852# IREE benchmark tools
853#-------------------------------------------------------------------------------
854
855add_subdirectory(build_tools/benchmarks)
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700856
857#-------------------------------------------------------------------------------
Jerry Wu71562cc2022-09-20 18:56:41 -0700858# IREE build tools python modules
859#-------------------------------------------------------------------------------
860
861add_subdirectory(build_tools/python)
862
863#-------------------------------------------------------------------------------
Scott Todd4f16b992022-05-17 10:33:53 -0700864# Samples
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700865#-------------------------------------------------------------------------------
866
Scott Todd4c022df2022-05-10 16:55:18 -0700867# samples/ can depend on anything, so we include it last
Stella Laurenzo1e8d1fa2022-04-22 09:50:43 -0700868if(IREE_BUILD_SAMPLES)
869 add_subdirectory(samples)
870endif()
Geoffrey Martin-Noble10f18222022-06-09 16:00:13 -0700871
872if(IREE_BUILD_TESTS)
873 iree_create_ctest_customization()
874endif()