blob: 1a5828ac04f0efa2a4f7dd57a536f624e4e274d2 [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 +000089option(IREE_BUILD_MICROBENCHMARKS "Builds IREE microbenchmark suites." OFF)
90
Jerry Wu57404972023-03-02 22:16:11 +000091option(IREE_BUILD_E2E_TEST_ARTIFACTS "Builds IREE E2E test artifacts suite generated by the e2e test framework." "${IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS}")
92
Stella Laurenzo289b9a12023-02-24 13:06:32 -080093# For development, builds LLVM (and in the future) the whole compiler as
94# individual shared libraries similar to if passing -DBUILD_SHARED_LIBS=ON
95# to a standalone LLVM build. This can dramatically reduce linking time and
96# makes the management of some dependencies more strict.
97# This option is considered experimental and should not be relied on until
98# CI coverage is established.
99option(IREE_COMPILER_BUILD_SHARED_LIBS "Enables BUILD_SHARED_LIBS CMake mode for LLVM and the compiler (this is only suitable for development)" OFF)
100
101# Must be defined as an option (CMake does not do it automatically), even though
102# we override it for different parts of the tree.
103# This option is considered experimental and should not be relied on until
104# CI coverage is established.
105option(BUILD_SHARED_LIBS "Instructs CMake to build libraries as shared if possible" OFF)
106
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800107#-------------------------------------------------------------------------------
Ben Vanik5363ea32023-05-02 15:47:58 -0700108# IREE command-line tooling configuration
109#-------------------------------------------------------------------------------
110
111# External user modules linked into IREE tooling (iree-run-module/etc).
112# These are only available in the internal IREE tools and here for testing
113# custom modules in standard workflows. This is not a deployment mechanism and
114# users integrating IREE into their libraries or applications will need to
115# manage the building and registering/resolving the modules themselves.
116#
117# See runtime/src/iree/tooling/modules/CMakeLists.txt for more information on
118# how to declare external modules.
119set(IREE_EXTERNAL_TOOLING_MODULES "" CACHE STRING "")
120
121#-------------------------------------------------------------------------------
Stella Laurenzoc6092c42023-03-25 19:55:37 -0700122# Compiler plugins
123# IREE compiler plugins can be statically built into the compiler by adding
124# directories to IREE_COMPILER_PLUGIN_PATHS. If directories are relative, they
125# are interpreted against the IREE source directory.
126#
127# In addition, certain in-tree plugin directories are included regardless of
128# this variable (possibly based on other flags).
129#
130# A plugin directory must contain an iree_compiler_plugin_group.cmake file that
131# is loaded and must specify:
132# - any needed add_subdirectory() calls to populate targets.
133# - iree_compiler_register_plugin() targets (either directly or transitively)
134# which associates a plugin id with a registration target.
135#
136# All iree_compiler_plugin_group.cmake files are included from the binary
137# directory ${IREE_BINARY_DIR}/compiler/plugins and must create a sub binary
138# directory that is globally unique. It is recommended to prefix with org
139# name or project. The following names are reserved:
140# - builtin
141# - samples
142#
143# Example:
144# add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/example samples/example)
145#-------------------------------------------------------------------------------
146
147set(IREE_COMPILER_PLUGIN_PATHS "" CACHE STRING "Paths to external compiler plugins")
148set(IREE_COMPILER_BUILTIN_PLUGIN_PATHS "compiler/plugins")
149if(IREE_BUILD_SAMPLES)
150 list(APPEND IREE_COMPILER_BUILTIN_PLUGIN_PATHS "samples/compiler_plugins")
151endif()
152
153#-------------------------------------------------------------------------------
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800154# Experimental project flags
155#-------------------------------------------------------------------------------
156
bjacob3dc368e2023-05-09 12:50:18 -0400157option(IREE_BUILD_EXPERIMENTAL_CPU_UKERNEL_PLUGINS "Build experimental plugins making builtin ukernels available to llvm-cpu modules compiled with --iree-llvmcpu-enable-microkernels" OFF)
Scott Toddf237b5e2022-01-28 11:02:25 -0800158option(IREE_BUILD_EXPERIMENTAL_WEB_SAMPLES "Builds experimental web samples." OFF)
Stella Laurenzo0f005e52020-12-27 11:54:33 -0800159
160#-------------------------------------------------------------------------------
Scott Todd18a54852023-01-27 08:52:12 -0800161# CUDA Toolkit.
162#
163# Using the (optional) CUDA support in the compiler and runtime requires the
164# NVIDIA CUDA Toolkit. The toolkit can either be installed ahead of time or
165# it can be automatically downloaded on certain host architectures.
Scott Todd9fa5de72023-01-23 14:40:18 -0800166#-------------------------------------------------------------------------------
167
Scott Todd18a54852023-01-27 08:52:12 -0800168set(IREE_CUDA_AVAILABLE OFF)
169find_package(CUDAToolkit)
170if(CUDAToolkit_FOUND)
171 set(IREE_CUDA_AVAILABLE ON)
172else()
173 # We can download the SDK in build_tools/third_party/cuda/CMakeLists.txt, if
174 # on a supported platform/arch.
175 if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR WIN32)
176 if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)")
177 set(IREE_CUDA_AVAILABLE ON)
178 endif()
Scott Todd9fa5de72023-01-23 14:40:18 -0800179 endif()
180endif()
181
182#-------------------------------------------------------------------------------
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800183# Runtime HAL Driver Options
184# By default, all runtime drivers supported by the current platform which do
185# not require external deps are enabled by default. This can be changed with:
186# -DIREE_HAL_DRIVER_DEFAULTS=OFF
Ben Vanik185d30c2019-09-19 14:24:11 -0700187#-------------------------------------------------------------------------------
188
Ben Vanik4e6af052022-06-07 17:10:20 -0700189# External HAL drivers; see runtime/src/iree/hal/drivers/CMakeLists.txt for more
190# information on how to declare external drivers.
191set(IREE_EXTERNAL_HAL_DRIVERS "" CACHE STRING "")
192
Ben Vanik89160272022-09-26 16:44:11 -0700193# Additional executable loader deps to add dependent libraries to any target
194# using the default executable loader registration utilities.
195# TODO(benvanik): extend the deps to encompass the built-in loaders too so that
196# we have one flag. We could also support a list of deps and automatically
197# generate the registration from that via a configure file.
198set(IREE_HAL_EXECUTABLE_LOADER_EXTRA_DEPS "" CACHE STRING "")
199
Ben Vanike19fc8e2023-04-14 16:08:01 -0700200# Additional executable import provider deps to add dependent libraries to any
201# target using the default executable import registration utilities.
202# TODO(benvanik): extend the deps to encompass the built-in imports too so that
203# we have one flag. We could also support a list of deps and automatically
204# generate the registration from that via a configure file.
205set(IREE_HAL_EXECUTABLE_PLUGIN_EXTRA_DEPS "" CACHE STRING "")
206
Scott Toddd1620f02021-12-22 14:55:38 -0800207option(IREE_HAL_DRIVER_DEFAULTS "Sets the default value for all runtime HAL drivers" ON)
Ben Vanika9aaebe2022-12-16 09:14:53 -0800208
Scott Todd9fa5de72023-01-23 14:40:18 -0800209# CUDA support is enabled by default if the platform supports the CUDA SDK and
210# not cross compiling. Note: a CUDA-compatible GPU with drivers is still
211# required to actually run CUDA workloads.
212set(IREE_HAL_DRIVER_CUDA_DEFAULT ${IREE_HAL_DRIVER_DEFAULTS})
Scott Todd18a54852023-01-27 08:52:12 -0800213if(NOT IREE_CUDA_AVAILABLE OR CMAKE_CROSSCOMPILING)
Scott Todd9fa5de72023-01-23 14:40:18 -0800214 set(IREE_HAL_DRIVER_CUDA_DEFAULT OFF)
215endif()
216
217# Vulkan support is enabled by default if the platform might support Vulkan.
218# Apple platforms support Metal instead of Vulkan, though MoltenVK may work.
219set(IREE_HAL_DRIVER_VULKAN_DEFAULT ${IREE_HAL_DRIVER_DEFAULTS})
Scott Todd1a1aea62022-04-28 09:12:11 -0700220if(APPLE)
Scott Toddd1620f02021-12-22 14:55:38 -0800221 set(IREE_HAL_DRIVER_VULKAN_DEFAULT OFF)
Scott Toddd1620f02021-12-22 14:55:38 -0800222endif()
Marius Brehler0a4b67f2020-05-08 13:19:16 -0700223
Scott Toddd1620f02021-12-22 14:55:38 -0800224option(IREE_HAL_DRIVER_CUDA "Enables the 'cuda' runtime HAL driver" ${IREE_HAL_DRIVER_CUDA_DEFAULT})
Ben Vanike3058072022-06-06 19:21:44 -0700225option(IREE_HAL_DRIVER_LOCAL_SYNC "Enables the 'local-sync' runtime HAL driver" ${IREE_HAL_DRIVER_DEFAULTS})
226option(IREE_HAL_DRIVER_LOCAL_TASK "Enables the 'local-task' runtime HAL driver" ${IREE_HAL_DRIVER_DEFAULTS})
227option(IREE_HAL_DRIVER_VULKAN "Enables the 'vulkan' runtime HAL driver" ${IREE_HAL_DRIVER_VULKAN_DEFAULT})
228
Ben Vanik6e64b6e2022-06-07 09:14:53 -0700229option(IREE_HAL_EXECUTABLE_LOADER_DEFAULTS "Sets the default value for all runtime HAL executable loaders" ON)
230set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
231set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
232set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT ${IREE_HAL_EXECUTABLE_LOADER_DEFAULTS})
Ben Vanike3058072022-06-06 19:21:44 -0700233
Ben Vanike19fc8e2023-04-14 16:08:01 -0700234option(IREE_HAL_EXECUTABLE_PLUGIN_DEFAULTS "Sets the default value for all runtime HAL executable plugin mechanisms" ON)
235set(IREE_HAL_EXECUTABLE_PLUGIN_EMBEDDED_ELF_DEFAULT ${IREE_HAL_EXECUTABLE_PLUGIN_DEFAULTS})
236set(IREE_HAL_EXECUTABLE_PLUGIN_SYSTEM_LIBRARY_DEFAULT ${IREE_HAL_EXECUTABLE_PLUGIN_DEFAULTS})
237
Ben Vanike3058072022-06-06 19:21:44 -0700238# Emscripten builds don't support embedded ELF libraries.
239if(EMSCRIPTEN)
Ben Vanik2e8540e2022-06-06 20:39:34 -0700240 set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT OFF)
Ben Vanike19fc8e2023-04-14 16:08:01 -0700241 set(IREE_HAL_EXECUTABLE_PLUGIN_EMBEDDED_ELF_DEFAULT OFF)
Ben Vanike3058072022-06-06 19:21:44 -0700242endif()
243
244# If forcing system libraries (for TSAN/debugging tools/etc) then ensure the
245# system library loader is linked in.
Scott Todd352da3f2022-07-20 15:25:11 -0700246if(IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER)
Ben Vanike3058072022-06-06 19:21:44 -0700247 set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT ON)
Ben Vanike19fc8e2023-04-14 16:08:01 -0700248 set(IREE_HAL_EXECUTABLE_PLUGIN_SYSTEM_LIBRARY_DEFAULT ON)
Ben Vanike3058072022-06-06 19:21:44 -0700249endif()
250
Ben Vanike19fc8e2023-04-14 16:08:01 -0700251# If no local driver is enabled then we force all the loaders/imports off; this
252# allows for simpler checks that don't need to see if both the driver and
253# feature is available.
Ben Vanik6e64b6e2022-06-07 09:14:53 -0700254if(NOT IREE_HAL_DRIVER_LOCAL_SYNC AND NOT IREE_HAL_DRIVER_LOCAL_TASK)
255 set(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF_DEFAULT OFF)
256 set(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT OFF)
257 set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT OFF)
Ben Vanike19fc8e2023-04-14 16:08:01 -0700258 set(IREE_HAL_EXECUTABLE_PLUGIN_EMBEDDED_ELF_DEFAULT OFF)
259 set(IREE_HAL_EXECUTABLE_PLUGIN_SYSTEM_LIBRARY_DEFAULT OFF)
Ben Vanik6e64b6e2022-06-07 09:14:53 -0700260endif()
261
Ben Vanik2e8540e2022-06-06 20:39:34 -0700262option(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 -0700263option(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY "Enables the system dynamic library loader for local HAL drivers" ${IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY_DEFAULT})
264option(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE "Enables the VMVX module loader for local HAL drivers" ${IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE_DEFAULT})
265
Ben Vanike19fc8e2023-04-14 16:08:01 -0700266option(IREE_HAL_EXECUTABLE_PLUGIN_EMBEDDED_ELF "Enables the embedded dynamic library plugin mechanism for local HAL drivers" ${IREE_HAL_EXECUTABLE_PLUGIN_EMBEDDED_ELF_DEFAULT})
267option(IREE_HAL_EXECUTABLE_PLUGIN_SYSTEM_LIBRARY "Enables the system dynamic library plugin mechanism for local HAL drivers" ${IREE_HAL_EXECUTABLE_PLUGIN_SYSTEM_LIBRARY_DEFAULT})
268
Scott Todd0f1f8eb2022-06-10 15:53:30 -0700269if(IREE_BUILD_COMPILER)
270 # The compiler requires the local task driver with the VMVX loader.
271 set(IREE_HAL_DRIVER_LOCAL_TASK ON)
272 set(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE ON)
273endif()
274
Ben Vanike3058072022-06-06 19:21:44 -0700275message(STATUS "IREE HAL drivers:")
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800276if(IREE_HAL_DRIVER_CUDA)
277 message(STATUS " - cuda")
278endif()
Ben Vanike3058072022-06-06 19:21:44 -0700279if(IREE_HAL_DRIVER_LOCAL_SYNC)
280 message(STATUS " - local-sync")
281endif()
282if(IREE_HAL_DRIVER_LOCAL_TASK)
283 message(STATUS " - local-task")
284endif()
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800285if(IREE_HAL_DRIVER_VULKAN)
286 message(STATUS " - vulkan")
287endif()
Ben Vanik4e6af052022-06-07 17:10:20 -0700288if(IREE_EXTERNAL_HAL_DRIVERS)
289 message(STATUS " + external: ${IREE_EXTERNAL_HAL_DRIVERS}")
290endif()
Ben Vanik512d2d32019-09-20 13:22:34 -0700291
Ben Vanike3058072022-06-06 19:21:44 -0700292message(STATUS "IREE HAL local executable library loaders:")
Ben Vanik2e8540e2022-06-06 20:39:34 -0700293if(IREE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF)
294 message(STATUS " - embedded-elf")
Ben Vanike3058072022-06-06 19:21:44 -0700295endif()
296if(IREE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY)
297 message(STATUS " - system-library")
298endif()
299if(IREE_HAL_EXECUTABLE_LOADER_VMVX_MODULE)
300 message(STATUS " - vmvx-module")
301endif()
302
Ben Vanike19fc8e2023-04-14 16:08:01 -0700303message(STATUS "IREE HAL local executable plugin mechanisms:")
304if(IREE_HAL_EXECUTABLE_PLUGIN_EMBEDDED_ELF)
305 message(STATUS " - embedded-elf")
306endif()
307if(IREE_HAL_EXECUTABLE_PLUGIN_SYSTEM_LIBRARY)
308 message(STATUS " - system-library")
309endif()
310
Lei Zhang0d281b72020-06-01 20:00:23 -0400311#-------------------------------------------------------------------------------
Ben Vanik4e6af052022-06-07 17:10:20 -0700312# Experimental ROCM HAL driver
313#-------------------------------------------------------------------------------
314
315set(IREE_EXTERNAL_ROCM_HAL_DRIVER_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/experimental/rocm")
316set(IREE_EXTERNAL_ROCM_HAL_DRIVER_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/experimental/rocm")
317set(IREE_EXTERNAL_ROCM_HAL_DRIVER_TARGET "iree::experimental::rocm::registration")
318set(IREE_EXTERNAL_ROCM_HAL_DRIVER_REGISTER "iree_hal_rocm_driver_module_register")
319
320#-------------------------------------------------------------------------------
Scott Todd382fc632022-06-06 10:57:24 -0700321# Compiler Target Options
Scott Todd9fa5de72023-01-23 14:40:18 -0800322# By default, all compiler targets supported by the current platform are
323# enabled. Some compiler targets like CUDA will install external deps as
324# needed at configure time. This can be changed with:
Scott Todd382fc632022-06-06 10:57:24 -0700325# -DIREE_TARGET_BACKEND_DEFAULTS=OFF
326#-------------------------------------------------------------------------------
327
328option(IREE_TARGET_BACKEND_DEFAULTS "Sets the default value for all compiler target backends" ON)
329
330# The VMVX backend is always enabled.
331cmake_dependent_option(IREE_TARGET_BACKEND_VMVX "Enables the 'vmvx' compiler target backend" ON ${IREE_BUILD_COMPILER} OFF)
332
333# Supported default target backends.
Scott Todd352da3f2022-07-20 15:25:11 -0700334cmake_dependent_option(IREE_TARGET_BACKEND_LLVM_CPU "Enables the 'llvm-cpu' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_BUILD_COMPILER} OFF)
335cmake_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 -0700336cmake_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 -0700337cmake_dependent_option(IREE_TARGET_BACKEND_VULKAN_SPIRV "Enables the 'vulkan-spirv' compiler target backend" ${IREE_TARGET_BACKEND_DEFAULTS} ${IREE_BUILD_COMPILER} OFF)
338
Scott Todd9fa5de72023-01-23 14:40:18 -0800339# Supported default target backends that are only available on certain
340# platforms.
341set(IREE_TARGET_BACKEND_CUDA_DEFAULT ${IREE_TARGET_BACKEND_DEFAULTS})
Scott Todd18a54852023-01-27 08:52:12 -0800342if(NOT IREE_CUDA_AVAILABLE)
Scott Todd9fa5de72023-01-23 14:40:18 -0800343 set(IREE_TARGET_BACKEND_CUDA_DEFAULT OFF)
344endif()
345cmake_dependent_option(IREE_TARGET_BACKEND_CUDA "Enables the 'cuda' compiler target backend" ${IREE_TARGET_BACKEND_CUDA_DEFAULT} ${IREE_BUILD_COMPILER} OFF)
346
Scott Todd382fc632022-06-06 10:57:24 -0700347# Non-default target backends either have additional dependencies or are
348# experimental/niche in some fashion.
Scott Todd382fc632022-06-06 10:57:24 -0700349cmake_dependent_option(IREE_TARGET_BACKEND_ROCM "Enables the 'rocm' compiler target backend" OFF ${IREE_BUILD_COMPILER} OFF)
350# Disable WebGPU by default - it has complex deps and is under development.
351cmake_dependent_option(IREE_TARGET_BACKEND_WEBGPU "Enables the 'webgpu' compiler target backend" OFF ${IREE_BUILD_COMPILER} OFF)
352
353#-------------------------------------------------------------------------------
Scott Todde23d4a72022-07-27 14:16:39 -0700354# Compiler Input Dialects
355#-------------------------------------------------------------------------------
356
357cmake_dependent_option(IREE_INPUT_MHLO "Builds support for compiling MHLO programs" ON ${IREE_BUILD_COMPILER} OFF)
358cmake_dependent_option(IREE_INPUT_TORCH "Builds support for compiling Torch MLIR programs" ON ${IREE_BUILD_COMPILER} OFF)
359cmake_dependent_option(IREE_INPUT_TOSA "Builds support for compiling TOSA programs" ON ${IREE_BUILD_COMPILER} OFF)
360
Marius Brehler36f24512022-08-04 17:39:03 +0200361if(IREE_BUILD_COMPILER)
362 message(STATUS "IREE compiler input dialects:")
363 if(IREE_INPUT_MHLO)
364 message(STATUS " - MHLO")
365 endif()
366 if(IREE_INPUT_TORCH)
367 message(STATUS " - Torch MLIR")
368 endif()
369 if(IREE_INPUT_TOSA)
370 message(STATUS " - TOSA")
371 endif()
Scott Todde23d4a72022-07-27 14:16:39 -0700372endif()
373
374#-------------------------------------------------------------------------------
Scott Todd2a1925c2022-06-13 10:03:52 -0700375# Compiler Output Formats
376#-------------------------------------------------------------------------------
377
Scott Todd2a1925c2022-06-13 10:03:52 -0700378cmake_dependent_option(IREE_OUTPUT_FORMAT_C "Enables the 'vm-c' output format, using MLIR EmitC" ON ${IREE_BUILD_COMPILER} OFF)
379
Marius Brehler36f24512022-08-04 17:39:03 +0200380if(IREE_BUILD_COMPILER)
381 message(STATUS "IREE compiler output formats:")
382 if(IREE_OUTPUT_FORMAT_C)
Ben Vanik09630d62023-04-13 14:21:40 -0700383 message(STATUS " - 'vm-c': textual C source module")
Marius Brehler36f24512022-08-04 17:39:03 +0200384 endif()
385 # The 'vm-bytecode' and 'vm-asm' formats are always enabled.
Ben Vanik09630d62023-04-13 14:21:40 -0700386 message(STATUS " - 'vm-bytecode': VM bytecode")
387 message(STATUS " - 'vm-asm': VM MLIR assembly")
Scott Todde23d4a72022-07-27 14:16:39 -0700388endif()
Scott Todde23d4a72022-07-27 14:16:39 -0700389
Scott Todd2a1925c2022-06-13 10:03:52 -0700390#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400391# IREE compilation toolchain configuration
392#-------------------------------------------------------------------------------
393
Lei Zhange88470f2020-09-08 13:21:09 -0400394option(IREE_ENABLE_ASAN "Enable address sanitizer" OFF)
395option(IREE_ENABLE_MSAN "Enable memory sanitizer" OFF)
396option(IREE_ENABLE_TSAN "Enable thread sanitizer" OFF)
bjacob7cf5b842022-04-04 16:48:04 -0400397option(IREE_BYTECODE_MODULE_ENABLE_TSAN "Enable thread sanitizer in IREE modules in tests" OFF)
Jakub Kuderski068b1722022-07-27 18:14:59 -0400398option(IREE_ENABLE_UBSAN "Enable undefined behavior sanitizer" OFF)
Stella Laurenzodeb48052022-11-25 10:41:46 -0800399option(IREE_ENABLE_SPLIT_DWARF "Enable gsplit-dwarf for debug information if the platform supports it" OFF)
400option(IREE_ENABLE_THIN_ARCHIVES "Enables thin ar archives (elf systems only). Disable for released static archives" OFF)
Ben Vanikadadd032022-11-28 23:18:16 -0800401option(IREE_LINK_COMPILER_SHARED_LIBRARY "Links IREE tools using the compiler compiled into a shared library" ON)
bjacob7cf5b842022-04-04 16:48:04 -0400402
403# STREQUAL feels wrong here - we don't care about the exact true-value used,
404# 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 -0800405# alternative. https://github.com/openxla/iree/pull/8474#discussion_r840790062
bjacob7cf5b842022-04-04 16:48:04 -0400406if(NOT IREE_ENABLE_TSAN STREQUAL IREE_BYTECODE_MODULE_ENABLE_TSAN)
407 message(SEND_ERROR
408 "IREE_ENABLE_TSAN and IREE_BYTECODE_MODULE_ENABLE_TSAN must be "
409 "simultaneously ON or OFF. "
410 "A discrepancy between the two would cause tests to crash as IREE "
411 "runtime code (controlled by IREE_ENABLE_TSAN) calls into test IREE "
412 "modules (controlled by IREE_BYTECODE_MODULE_ENABLE_TSAN)")
413endif()
414
415if(IREE_BYTECODE_MODULE_ENABLE_TSAN)
Scott Todd352da3f2022-07-20 15:25:11 -0700416 if(NOT IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER)
bjacob7cf5b842022-04-04 16:48:04 -0400417 message(SEND_ERROR
418 "When IREE_BYTECODE_MODULE_ENABLE_TSAN is ON, "
Scott Todd352da3f2022-07-20 15:25:11 -0700419 "IREE_BYTECODE_MODULE_FORCE_LLVM_SYSTEM_LINKER must also be ON. "
bjacob7cf5b842022-04-04 16:48:04 -0400420 "TSAN instrumentation is not currently supported in embedded modules.")
421 endif()
422endif()
423
Scott Todd86781382023-02-07 14:11:52 -0800424if(IREE_LINK_COMPILER_SHARED_LIBRARY AND IREE_ENABLE_COMPILER_TRACING)
425 message(SEND_ERROR
426 "IREE_ENABLE_COMPILER_TRACING requires "
427 "-DIREE_LINK_COMPILER_SHARED_LIBRARY=OFF (the compiler library must not "
428 "be unloaded before Tracy finishes, static linking is one workaround)")
429endif()
430
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800431option(IREE_ENABLE_CCACHE
432 "[DEPRECATED: Use CMAKE_<LANG>_COMPILER_LAUNCHER configure options or environment variables instead.] Use ccache if installed."
433 OFF)
bjacobe694d952020-11-03 12:05:35 -0500434
Scott Todd1a1aea62022-04-28 09:12:11 -0700435if(IREE_ENABLE_CCACHE)
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800436 message(WARNING
437 "IREE_ENABLE_CCACHE is deprecated. Use CMAKE_<LANG>_COMPILER_LAUNCHER"
438 " configure options or environment variables instead.")
bjacobe694d952020-11-03 12:05:35 -0500439 find_program(CCACHE_PROGRAM ccache)
440 if(CCACHE_PROGRAM)
441 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
Geoffrey Martin-Noble61ea1ed2022-11-23 16:25:57 -0800442 else()
443 message(SEND_ERROR
444 "IREE_ENABLE_CCACHE was set, but executable `ccache` was not found.")
bjacobe694d952020-11-03 12:05:35 -0500445 endif()
446endif()
Lei Zhange88470f2020-09-08 13:21:09 -0400447
Geoffrey Martin-Noble3fa4f8d2021-09-10 09:25:27 -0700448option(IREE_DEV_MODE "Configure settings to optimize for IREE development (as opposed to CI or release)" OFF)
Stella Laurenzo5b639122021-06-18 14:44:10 -0700449
450#-------------------------------------------------------------------------------
451# IREE assertions
452# We don't love the way this is done, but we have to line it up with how LLVM
453# does it and not diverge, since all implementations and all header users must
454# have the same definition of NDEBUG.
455#
456# LLVM defaults LLVM_ENABLE_ASSERTIONS to ON for Debug builds only but then
457# conditions itself to only update flags if not building Debug. We just let
458# IREE_ENABLE_ASSERTIONS be not conditioned on anything and only update the
459# flags in appropriate build types.
460#
461# If IREE_ENABLE_ASSERTIONS is set ON manually, then
462# - NDEBUG must be undefined
463# - LLVM_ENABLE_ASSERTIONS is forced off in order to keep multiple parties
464# from mucking with globals.
465#
466# Since CMake forces NDEBUG for !Debug builds, some surgery needs to be done
467# at the top level to avoid divergence.
468#-------------------------------------------------------------------------------
469
470option(IREE_ENABLE_ASSERTIONS "Force unset of NDEBUG compile option" OFF)
471
472# Filter -DNDEBUG from CMAKE_CXX_FLAGS_* and CMAKE_C_FLAGS_* (if
473# CMAKE_BUILD_TYPE is not Debug).
474function(iree_fix_ndebug)
Scott Todd1a1aea62022-04-28 09:12:11 -0700475 string(TOUPPER "${CMAKE_BUILD_TYPE}" _UPPERCASE_CMAKE_BUILD_TYPE)
476 if(IREE_ENABLE_ASSERTIONS AND NOT "${_UPPERCASE_CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
Stella Laurenzo5b639122021-06-18 14:44:10 -0700477 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
Scott Todd1a1aea62022-04-28 09:12:11 -0700478 foreach(_FLAGS_VAR_TO_SCRUB
479 CMAKE_CXX_FLAGS_${_UPPERCASE_CMAKE_BUILD_TYPE}
480 CMAKE_C_FLAGS_${_UPPERCASE_CMAKE_BUILD_TYPE})
481 set(_ORIGINAL_FLAGS "${${_FLAGS_VAR_TO_SCRUB}}")
482 string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " _ALTERED_FLAGS "${_ORIGINAL_FLAGS}")
483 if(NOT "${_ORIGINAL_FLAGS}" STREQUAL "${_ALTERED_FLAGS}")
Stella Laurenzo5b639122021-06-18 14:44:10 -0700484 message(STATUS
Scott Todd1a1aea62022-04-28 09:12:11 -0700485 "IREE_ENABLE_ASSERTIONS force disabled NDEBUG for ${_FLAGS_VAR_TO_SCRUB}: '${_ORIGINAL_FLAGS}' -> '${_ALTERED_FLAGS}'")
486 set(${_FLAGS_VAR_TO_SCRUB} "${_ALTERED_FLAGS}" PARENT_SCOPE)
Stella Laurenzo5b639122021-06-18 14:44:10 -0700487 endif()
488 endforeach()
489
490 # Make sure that LLVM doesn't add its own logic for assertion disabling.
491 # We'd like to make sure that we are not dueling over globals.
492 set(LLVM_ENABLE_ASSERTIONS OFF PARENT_SCOPE)
493 endif()
494endfunction()
495iree_fix_ndebug()
496
Lei Zhange88470f2020-09-08 13:21:09 -0400497#-------------------------------------------------------------------------------
Lei Zhang7e253da2020-06-10 07:51:19 -0700498# IREE utility definitions
Lei Zhang0d281b72020-06-01 20:00:23 -0400499#-------------------------------------------------------------------------------
500
Scott Toddd1620f02021-12-22 14:55:38 -0800501list(APPEND CMAKE_MODULE_PATH
502 ${CMAKE_CURRENT_LIST_DIR}/build_tools/cmake/
Scott Toddd1620f02021-12-22 14:55:38 -0800503)
504
Ben Vanik512d2d32019-09-20 13:22:34 -0700505include(iree_macros)
Ben Vanik185d30c2019-09-19 14:24:11 -0700506include(iree_copts)
Ben Vanik6b112ef2019-10-03 10:45:14 -0700507include(iree_cc_binary)
Ben Vanik185d30c2019-09-19 14:24:11 -0700508include(iree_cc_library)
509include(iree_cc_test)
Stella Laurenzo17b3e5f2023-03-21 16:00:57 -0700510include(iree_compiler_plugin)
Scott Todd20d746a2023-01-12 09:11:59 -0800511include(iree_import_binary)
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800512include(iree_external_cmake_options)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700513include(iree_tablegen_library)
Lei Zhang22f0e242020-03-30 12:09:20 -0700514include(iree_tablegen_doc)
CindyLiu40ed02d2021-04-22 16:16:28 +0000515include(iree_c_embed_data)
Ben Vanik7f6c57c2023-02-07 18:04:32 -0800516include(iree_bitcode_library)
Scott Todd11adcab2019-12-18 14:10:44 -0800517include(iree_bytecode_module)
Marius Brehler46e83312021-03-25 00:11:39 +0100518include(iree_c_module)
Stella Laurenzo94363e22020-12-15 13:46:14 -0800519include(iree_python)
Geoffrey Martin-Noblef0eaf372020-01-28 10:03:14 -0800520include(iree_lit_test)
Stella Laurenzo96d959e2023-02-19 11:58:14 -0800521include(iree_llvm)
Geoffrey Martin-Noble4526dcc2020-03-09 11:59:52 -0700522include(iree_add_all_subdirs)
Geoffrey Martin-Noblee5fd5b52020-03-31 11:31:30 -0700523include(iree_check_test)
bjacob5feef482021-10-21 16:53:58 -0400524include(iree_trace_runner_test)
Geoffrey Martin-Noble435c2702022-01-24 15:56:56 -0800525include(iree_native_test)
Geoffrey Martin-Noble2811e502022-01-25 09:44:40 -0800526include(iree_cc_binary_benchmark)
Geoffrey Martin-Noble66d48892021-10-29 12:24:58 -0700527include(iree_benchmark_suite)
Han-Chung Wanga6fbb762022-04-15 14:25:04 -0700528include(iree_microbenchmark_suite)
Scott Todd434ff0e2021-12-21 10:38:36 -0800529include(iree_hal_cts_test_suite)
CindyLiuae72b952022-08-23 15:26:08 -0700530include(iree_static_linker_test)
Jerry Wu116db872022-09-22 19:33:13 +0000531include(iree_fetch_artifact)
CindyLiu080198f2022-10-06 07:21:17 -0700532include(iree_run_module_test)
Ben Vanik185d30c2019-09-19 14:24:11 -0700533
Stella Laurenzobe0f1e12023-04-03 13:34:38 -0700534# Default any sub-tree which doesn't provide its own package namespacing
535# to derive it relative to this directory and prefixed with iree/.
536set(IREE_PACKAGE_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}")
Lei Zhang923535d2023-04-06 18:07:43 -0400537set(IREE_PACKAGE_ROOT_PREFIX "iree")
Stella Laurenzobe0f1e12023-04-03 13:34:38 -0700538
Marius Brehler06ac36e2020-01-10 14:44:11 -0800539set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
540
Ben Vanik185d30c2019-09-19 14:24:11 -0700541#-------------------------------------------------------------------------------
Lei Zhange88470f2020-09-08 13:21:09 -0400542# IREE compilation flags
Scott Todd29d654e2020-06-11 15:24:17 -0700543#-------------------------------------------------------------------------------
544
Lei Zhangdd21f322020-09-10 10:47:33 -0400545iree_append_list_to_string(CMAKE_C_FLAGS_DEBUG ${IREE_C_FLAGS_DEBUG_LIST})
546iree_append_list_to_string(CMAKE_CXX_FLAGS_DEBUG ${IREE_CXX_FLAGS_DEBUG_LIST})
Scott Todd29d654e2020-06-11 15:24:17 -0700547
548set(CMAKE_CXX_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C++ compiler during fast builds." FORCE)
549set(CMAKE_C_FLAGS_FASTBUILD "-gmlt" CACHE STRING "Flags used by the C compiler during fast builds." FORCE)
550set(CMAKE_EXE_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used for linking binaries during fast builds." FORCE)
551set(CMAKE_SHARED_LINKER_FLAGS_FASTBUILD "-Wl,-S" CACHE STRING "Flags used by the shared libraries linker binaries during fast builds." FORCE)
552mark_as_advanced(
553 CMAKE_CXX_FLAGS_FASTBUILD
554 CMAKE_C_FLAGS_FASTBUILD
555 CMAKE_EXE_LINKER_FLAGS_FASTBUILD
556 CMAKE_SHARED_LINKER_FLAGS_FASTBUILD
557)
558
Scott Todd7649dee2022-07-18 11:31:46 -0700559# Override the system's default linker.
560# See also: https://llvm.org/docs/CMake.html#llvm-use-linker.
561set(IREE_USE_LINKER "" CACHE STRING "")
562# Equivalent to setting -DIREE_USE_LINKER=lld.
563# Note that unlike LLVM's LLVM_ENABLE_LLD, this does _not_ build lld. You will
564# need to either install a recent version of lld or build it from source prior
565# to setting this option. See also: https://lld.llvm.org/#using-lld.
566# This option is disabled on Apple platforms, where lld is not supported.
567cmake_dependent_option(IREE_ENABLE_LLD "Override the system's default linker to lld" OFF "NOT APPLE" OFF)
568
Scott Todd29d654e2020-06-11 15:24:17 -0700569include(iree_setup_toolchain)
570
571#-------------------------------------------------------------------------------
Stella Laurenzo29032b82021-10-14 15:21:44 -0700572# Python
573# If building features that require Python development, find them early in
574# one invocation (some CMake versions are sensitive to resolving out of order).
575# Otherwise, for features that just require the interpreter, find that alone.
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800576#-------------------------------------------------------------------------------
577
Stella Laurenzo29032b82021-10-14 15:21:44 -0700578if(IREE_BUILD_PYTHON_BINDINGS)
579 # After CMake 3.18, we are able to limit the scope of the search to just
580 # Development.Module. Searching for Development will fail in situations where
581 # the Python libraries are not available. When possible, limit to just
582 # Development.Module.
583 # See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
Scott Todda24c8c52022-05-17 09:22:47 -0700584 #
585 # Configuring the Development.Module is flaky in multi-project setups.
586 # "Bootstrapping" by first looking for the optional Development component
587 # seems to be robust generally.
588 # See: https://reviews.llvm.org/D118148
589 find_package(Python3 COMPONENTS Interpreter Development)
590 find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700591elseif(IREE_BUILD_COMPILER OR IREE_BUILD_TESTS)
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800592 find_package(Python3 COMPONENTS Interpreter REQUIRED)
593endif()
594
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700595# Extended Python environment checks.
596if(Python3_FOUND)
597 iree_detect_pyyaml()
598endif()
599
600if(IREE_BUILD_TESTS AND NOT IREE_PYYAML_FOUND)
601 message(WARNING "IREE's regression test suite requires PyYAML to run all tests. It is not installed, so some tests will be disabled.")
602endif()
603
Stella Laurenzoa3e97f12020-12-05 23:29:13 -0800604#-------------------------------------------------------------------------------
Niloy Sikdar47238df2021-07-28 23:37:33 +0530605# Check if git submodules have been initialized.
606# This will only run if python3 is available.
607#-------------------------------------------------------------------------------
608
Marius Brehler776d7e62021-12-21 22:50:52 +0100609option(IREE_ERROR_ON_MISSING_SUBMODULES "Error if submodules have not been initialized." ON)
610
Niloy Sikdar47238df2021-07-28 23:37:33 +0530611find_package(Python3 COMPONENTS Interpreter QUIET)
Scott Todde5b269a2021-11-16 15:25:02 -0800612find_package(Git)
Marius Brehler776d7e62021-12-21 22:50:52 +0100613if(IREE_ERROR_ON_MISSING_SUBMODULES AND Python3_FOUND AND Git_FOUND)
Scott Todde5b269a2021-11-16 15:25:02 -0800614 # Only check submodule status when the git commit changes.
Niloy Sikdar47238df2021-07-28 23:37:33 +0530615 execute_process(
Scott Todde5b269a2021-11-16 15:25:02 -0800616 COMMAND git rev-parse --short HEAD
Niloy Sikdar47238df2021-07-28 23:37:33 +0530617 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Scott Todde5b269a2021-11-16 15:25:02 -0800618 RESULT_VARIABLE SHORT_HASH_RESULT
619 OUTPUT_VARIABLE SHORT_HASH)
620 string(REGEX REPLACE "\n$" "" SHORT_HASH "${SHORT_HASH}")
Scott Todd2a8cd3b2021-12-13 11:58:44 -0800621 if(SHORT_HASH_RESULT EQUAL "0" AND NOT "${IREE_GIT_SHORT_HASH}" STREQUAL "${SHORT_HASH}")
CindyLiu0e8fdc62022-10-14 09:35:44 -0700622 if(NOT IREE_BUILD_COMPILER)
623 set(CHECK_SUBMODULE_ARGS "--runtime_only")
624 endif()
Scott Todde5b269a2021-11-16 15:25:02 -0800625 execute_process(
CindyLiu0e8fdc62022-10-14 09:35:44 -0700626 COMMAND ${Python3_EXECUTABLE} build_tools/scripts/git/check_submodule_init.py ${CHECK_SUBMODULE_ARGS}
Scott Todde5b269a2021-11-16 15:25:02 -0800627 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
628 RESULT_VARIABLE SUBMODULE_INIT_RESULT
629 )
630 if(NOT SUBMODULE_INIT_RESULT EQUAL "0")
631 message(FATAL_ERROR "check_submodule_init.py failed, see the logs above")
632 else()
633 set(IREE_GIT_SHORT_HASH "${SHORT_HASH}" CACHE STRING "" FORCE)
634 endif()
Niloy Sikdar47238df2021-07-28 23:37:33 +0530635 endif()
636endif()
637
638#-------------------------------------------------------------------------------
Lei Zhang22f0e242020-03-30 12:09:20 -0700639# IREE top-level targets
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800640# We define these here because various things in the build tree adds
641# dependencies to them.
Lei Zhang22f0e242020-03-30 12:09:20 -0700642#-------------------------------------------------------------------------------
643
Jerry Wu57404972023-03-02 22:16:11 +0000644if(IREE_BUILD_E2E_TEST_ARTIFACTS)
Jerry Wu43305f72023-05-23 17:17:20 +0000645 # iree-benchmark-import-models imports benchmark models from their source
646 # formats, such as .tflite flatbuffers, to IREE-compatible .mlir files.
647 add_custom_target(iree-benchmark-import-models)
648
649 # iree-benchmark-suites* fully prepares benchmark models for benchmarking:
650 # * importing from source formats to IREE-compatible .mlir files
651 # * compiling from .mlir files to benchmark-ready .vmfb files
652 # Build default benchmark suites.
653 add_custom_target(iree-benchmark-suites)
654 # Build long-running benchmark suites.
655 add_custom_target(iree-benchmark-suites-long)
656
657 # iree-e2e-compile-stats-suites* compiles the benchmark models with specific
Jerry Wufb87ad12022-12-21 05:59:02 +0000658 # flags to collect the compilation statistics.
Jerry Wu43305f72023-05-23 17:17:20 +0000659 # Build for default benchmark suites.
Jerry Wufb87ad12022-12-21 05:59:02 +0000660 add_custom_target(iree-e2e-compile-stats-suites)
Jerry Wu43305f72023-05-23 17:17:20 +0000661 # Build for long-running benchmark suites.
662 add_custom_target(iree-e2e-compile-stats-suites-long)
Jerry Wufb87ad12022-12-21 05:59:02 +0000663
Jerry Wud41d0732022-11-23 01:43:55 +0000664 # iree-e2e-test-artifacts builds all e2e test artifacts, including benchmarks
665 # and model tests.
666 add_custom_target(iree-e2e-test-artifacts)
Jerry Wud41d0732022-11-23 01:43:55 +0000667 add_dependencies(iree-e2e-test-artifacts
668 iree-benchmark-import-models
669 iree-benchmark-suites
Jerry Wu43305f72023-05-23 17:17:20 +0000670 iree-benchmark-suites-long
Jerry Wufb87ad12022-12-21 05:59:02 +0000671 iree-e2e-compile-stats-suites
Jerry Wu43305f72023-05-23 17:17:20 +0000672 iree-e2e-compile-stats-suites-long
Jerry Wud41d0732022-11-23 01:43:55 +0000673 )
674endif()
675
Scott Todd1a1aea62022-04-28 09:12:11 -0700676if(IREE_BUILD_MICROBENCHMARKS)
Han-Chung Wanga6fbb762022-04-15 14:25:04 -0700677 # Add top-level custom targets to drive generating microbenchmark suites.
678 add_custom_target(iree-microbenchmark-suites)
679endif()
680
Scott Todd1a1aea62022-04-28 09:12:11 -0700681if(IREE_BUILD_DOCS)
Lei Zhang22f0e242020-03-30 12:09:20 -0700682 # Add a top-level custom target to drive generating all documentation.
683 # Register it to the default target given that IREE_BUILD_DOCS is explicitly
684 # requested.
685 add_custom_target(iree-doc ALL)
686endif()
687
Jakub Kuderski3f764682023-02-07 17:14:23 -0500688# Samples may require additional files to be built/configured and will add
689# dependencies to this target.
690# Note: These will be automatically built with test dependencies
691# (`iree-test-deps`).
692add_custom_target(iree-sample-deps
693 COMMENT
694 "Building IREE sample data targets"
695)
696
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800697# Testing rules that require generation will add dependencies to this target.
698# This allows them to be EXCLUDE_FROM_ALL but still invokable.
Jakub Kuderski3f764682023-02-07 17:14:23 -0500699add_custom_target(iree-test-deps
700 COMMENT
701 "Building IREE test deps"
702 DEPENDS
703 iree-sample-deps
704)
Stella Laurenzo74b04b72022-03-02 10:21:11 -0800705
CindyLiu99373e02022-10-12 09:22:24 -0700706# Testing rules that generate test scripts for iree-run-module-test will add
707# dependencies to this target. It is a subset of `iree-test-deps`.
708add_custom_target(iree-run-module-test-deps
709 COMMENT
710 "Building IREE run module test targets"
711)
712
Stella Laurenzoe2dfc652023-02-15 18:12:03 -0800713# Convenience target for running IREE tests.
714add_custom_target(iree-run-tests
715 COMMENT
716 "Run IREE unit tests"
717 WORKING_DIRECTORY
718 "${CMAKE_CURRENT_BINARY_DIR}"
719 USES_TERMINAL
720 COMMAND
721 "${CMAKE_COMMAND}" -E echo
722 "The 'iree-run-tests' target is a helper for running ctest. For advanced"
723 "options, build dependencies and invoke ctest independently as in:"
724 COMMAND
725 "${CMAKE_COMMAND}" -E echo
726 " \\(cd ${CMAKE_CURRENT_BINARY_DIR} \\&\\& cmake --build . --target iree-test-deps \\&\\& ctest --output-on-failure\\)"
727 COMMAND
728 "${CMAKE_COMMAND}" -E echo
729 "Run tests in parallel by setting a variable like CTEST_PARALLEL_LEVEL=25."
730 COMMAND
731 "${CMAKE_CTEST_COMMAND}"
732 --output-on-failure
733)
734add_dependencies(iree-run-tests iree-test-deps)
735
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800736#-------------------------------------------------------------------------------
737# CUDA configuration for both the compiler and runtime.
738# We do this at the top level so that we can fail fast and make global
739# decisions that effect both compiler and runtime. It also helps with error
740# messaging to do this all in one place, since we can provide very targeted
741# advice.
742#-------------------------------------------------------------------------------
743
744set(IREE_CUDA_LIBDEVICE_PATH "" CACHE FILEPATH "Absolute path to an appropriate libdevice.*.bc (needed to build the IREE cuda compiler target)")
745
746# If any CUDA features are being built, try to locate a CUDA SDK. We will fall
747# back to this as needed for specific features.
748if(IREE_TARGET_BACKEND_CUDA OR IREE_HAL_DRIVER_CUDA)
749 add_subdirectory(build_tools/third_party/cuda EXCLUDE_FROM_ALL)
750endif()
751
752#-------------------------------------------------------------------------------
753# MLIR/LLVM Dependency
754#-------------------------------------------------------------------------------
755
756if(NOT IREE_BUILD_COMPILER)
757 message(STATUS "Not adding LLVM/MLIR because the configuration does not require it")
758else()
Stella Laurenzo289b9a12023-02-24 13:06:32 -0800759 # Force enable BUILD_SHARED_LIBS for the compiler if instructed.
760 set(_IREE_ORIG_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
761 if(IREE_COMPILER_BUILD_SHARED_LIBS)
762 set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
763 endif()
764
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800765 # Get the main LLVM deps.
766 if(IREE_BUILD_BUNDLED_LLVM)
767 iree_llvm_configure_bundled()
768 else()
769 iree_llvm_configure_installed()
770 endif()
771
772 # Also add a library that can be depended on to get LLVM includes setup
773 # properly. bazel_to_cmake targets this for some header only pseudo deps.
774 add_library(IREELLVMIncludeSetup INTERFACE)
775 target_include_directories(IREELLVMIncludeSetup INTERFACE
776 ${LLVM_INCLUDE_DIRS}
777 ${MLIR_INCLUDE_DIRS}
778 ${LLD_INCLUDE_DIRS}
779 )
780
781 # Splice the includes setup into base LLVM libraries so that using them
782 # gets everything nice and tidy. It would be super if some day, LLVM
783 # libraries set their right usage requirements for includes. In the meantime
784 # we add usage requirements to libraries at the root of all things LLVM.
785 iree_llvm_add_usage_requirements(LLVMSupport IREELLVMIncludeSetup)
786 iree_llvm_add_usage_requirements(MLIRSupport IREELLVMIncludeSetup)
787
788 # Add default external projects.
789 iree_llvm_add_external_project(mlir-iree-dialects ${CMAKE_CURRENT_SOURCE_DIR}/llvm-external-projects/iree-dialects)
790 iree_llvm_add_external_project(mlir-hlo ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mlir-hlo)
791 if(IREE_INPUT_TORCH)
792 iree_llvm_add_external_project(torch-mlir-dialects ${CMAKE_CURRENT_SOURCE_DIR}/third_party/torch-mlir-dialects)
793 endif()
794
795 # Ensure that LLVM-based dependencies needed for testing are included.
796 add_dependencies(iree-test-deps FileCheck)
797 if(IREE_LLD_TARGET)
798 add_dependencies(iree-test-deps ${IREE_LLD_TARGET})
799 endif()
800 if(IREE_CLANG_TARGET)
801 add_dependencies(iree-test-deps ${IREE_CLANG_TARGET})
802 endif()
Stella Laurenzo289b9a12023-02-24 13:06:32 -0800803
804 set(BUILD_SHARED_LIBS ${_IREE_ORIG_BUILD_SHARED_LIBS} CACHE BOOL "" FORCE)
Stella Laurenzo9be15aa2023-02-21 17:00:29 -0800805endif()
806
807#-------------------------------------------------------------------------------
808# Other dependencies
809#-------------------------------------------------------------------------------
810
811include(external_cc_library)
812include(flatbuffer_c_library)
813
814add_subdirectory(build_tools/third_party/libyaml EXCLUDE_FROM_ALL)
815add_subdirectory(build_tools/third_party/llvm-project EXCLUDE_FROM_ALL)
816add_subdirectory(build_tools/third_party/tracy_client EXCLUDE_FROM_ALL)
817add_subdirectory(build_tools/third_party/vulkan_memory_allocator EXCLUDE_FROM_ALL)
818
819iree_set_googletest_cmake_options()
820add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
821
822if(IREE_ENABLE_THREADING)
823 iree_set_benchmark_cmake_options()
824 add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
825 if(IREE_ENABLE_CPUINFO)
826 iree_set_cpuinfo_cmake_options()
827 add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
828 endif()
829endif()
830
831# This defines the iree-flatcc-cli target, so we don't use EXCLUDE_FROM_ALL.
832add_subdirectory(build_tools/third_party/flatcc)
833
834if(IREE_HAL_DRIVER_CUDA)
835 add_subdirectory(build_tools/third_party/nccl EXCLUDE_FROM_ALL)
836endif()
837
838if(IREE_HAL_DRIVER_VULKAN)
839 add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
840endif()
841
842if(IREE_BUILD_COMPILER)
843 add_subdirectory(build_tools/third_party/mlir-hlo EXCLUDE_FROM_ALL)
844endif()
845
846if(IREE_BUILD_TESTS)
847 include(iree_configure_testing)
848endif()
849
850if(IREE_BUILD_PYTHON_BINDINGS)
851 if(NOT TARGET pybind11::module)
852 message(STATUS "Using bundled pybind11")
853 add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
854 else()
855 message(STATUS "Not including bundled pybind11 (already configured)")
856 endif()
857endif()
858
859if(IREE_TARGET_BACKEND_METAL_SPIRV)
860 iree_set_spirv_cross_cmake_options()
861 # SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
862 add_subdirectory(third_party/spirv_cross EXCLUDE_FROM_ALL)
863endif()
864
865if(IREE_TARGET_BACKEND_WEBGPU)
866 # Tint is needed to compile SPIR-V into WGSL source code.
867 # Tint also requires SPIRV-Tools, which requires SPIRV-Headers.
868 iree_set_spirv_headers_cmake_options()
869 add_subdirectory(third_party/spirv_headers EXCLUDE_FROM_ALL)
870 add_subdirectory(build_tools/third_party/spirv-tools EXCLUDE_FROM_ALL)
871 add_subdirectory(build_tools/third_party/tint EXCLUDE_FROM_ALL)
872endif()
Stella Laurenzoe2dfc652023-02-15 18:12:03 -0800873
Lei Zhang22f0e242020-03-30 12:09:20 -0700874#-------------------------------------------------------------------------------
Ben Vanik185d30c2019-09-19 14:24:11 -0700875# IREE top-level libraries
876#-------------------------------------------------------------------------------
877
Rob Sudermand42e0022022-06-08 16:38:45 -0700878if(IREE_ENABLE_CLANG_TIDY)
879 set(CMAKE_CXX_CLANG_TIDY clang-tidy -warnings-as-errors=*)
880endif()
881
Marius Brehlerf5022e82019-12-13 15:20:25 -0800882add_subdirectory(build_tools/embed_data/)
883
Jerry Wu36641f92023-05-24 19:30:38 +0000884if(IREE_BUILD_E2E_TEST_ARTIFACTS)
Stella Laurenzoa6bf65c2022-01-11 20:47:51 -0800885 find_program(IREE_IMPORT_TFLITE_PATH iree-import-tflite)
886 if(IREE_IMPORT_TFLITE_PATH)
887 message(STATUS "Found ${IREE_IMPORT_TFLITE_PATH} to generate benchmark artifacts")
888 else()
889 message(STATUS "iree-import-tflite not found. Some benchmarks may not configure")
890 endif()
Jerry Wu60798ca2022-06-21 23:36:40 +0000891 find_program(IREE_IMPORT_TF_PATH iree-import-tf)
892 if(IREE_IMPORT_TF_PATH)
893 message(STATUS "Found ${IREE_IMPORT_TF_PATH} to generate benchmark artifacts")
894 else()
895 message(STATUS "iree-import-tf not found. Some benchmarks may not configure")
896 endif()
Jerry Wu16e2d9c2022-11-22 06:19:46 +0000897endif()
898
899# Note: Test deps are not built as part of all (use the iree-test-deps target).
900add_subdirectory(tests EXCLUDE_FROM_ALL)
901
Jacques Pienaar702929a2022-12-30 14:46:22 -0800902# tools/ can depend on compiler/ and runtime/.
903# Note: tools sub directory is added before compiler/ so that phony targets for
904# files with the same names from different rules are disambiguated towards
905# those in tools/.
906add_subdirectory(tools)
Stella Laurenzo309dc5b2023-02-22 23:07:43 -0800907add_subdirectory(compiler)
Scott Todd4f16b992022-05-17 10:33:53 -0700908add_subdirectory(runtime)
909
Rob Sudermand42e0022022-06-08 16:38:45 -0700910if(IREE_ENABLE_CLANG_TIDY)
911 set(CMAKE_CXX_CLANG_TIDY "")
912endif()
913
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700914if(IREE_BUILD_TRACY)
Stella Laurenzo3149b6d2021-10-24 18:45:17 -0700915 if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700916 message(WARNING "Building Tracy (IREE_BUILD_TRACY) on non-Linux is unsupported and may fail below.")
917 endif()
918 add_subdirectory(build_tools/third_party/tracy ${CMAKE_CURRENT_BINARY_DIR}/tracy)
919 if(NOT TARGET IREETracyCaptureServer)
920 message(SEND_ERROR "Could not build Tracy. Either unset IREE_BUILD_TRACY or look for missing dependencies above and install them.")
921 endif()
922endif()
923
Scott Toddf57ab752022-05-23 10:36:44 -0700924# Order constraint: The python bindings install tools targets from tools/
Stella Laurenzod3770ff2021-10-18 22:54:18 -0700925# and tracy, and must come after it.
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700926if(IREE_BUILD_PYTHON_BINDINGS)
Stella Laurenzo60128592022-04-17 21:52:09 -0700927 # Write out a .env file to make IDEs and developers happy.
928 # Yes, we are writing this to the source dir. It is only for IDEs and if
Scott Toddfe9cb172022-12-08 09:17:52 -0800929 # it gets clobbered, it is fine (it is also ignored in .gitignore).
Stella Laurenzo41a2ceb2022-04-29 12:49:36 -0700930 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 -0800931 file(GENERATE OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/.env" CONTENT "${_PYTHONPATH_ENV}")
932 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env" CONTENT "${_PYTHONPATH_ENV}")
933 # Similarly, write out .env.bat for Windows.
934 set(_PYTHONPATH_ENV_BAT "set PYTHONPATH=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}/compiler/bindings/python;${CMAKE_CURRENT_BINARY_DIR}/runtime/bindings/python>\n")
935 file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env.bat" CONTENT "${_PYTHONPATH_ENV_BAT}")
Stella Laurenzo77a63cd2021-01-04 17:29:54 -0800936endif()
937
Scott Todd1a1aea62022-04-28 09:12:11 -0700938if(IREE_BUILD_BINDINGS_TFLITE)
Scott Toddada25e62022-05-03 16:16:15 -0700939 add_subdirectory(runtime/bindings/tflite)
Ben Vanikcd1132f2021-01-29 15:58:17 -0800940endif()
941
Scott Todd1a1aea62022-04-28 09:12:11 -0700942if(IREE_BUILD_EXPERIMENTAL_WEB_SAMPLES)
Scott Todd15fce0a2022-02-14 12:43:23 -0800943 add_subdirectory(experimental/web)
Scott Toddf237b5e2022-01-28 11:02:25 -0800944endif()
945
bjacob3dc368e2023-05-09 12:50:18 -0400946if(IREE_BUILD_EXPERIMENTAL_CPU_UKERNEL_PLUGINS)
947 add_subdirectory(experimental/cpu_ukernel)
948endif()
949
Stella Laurenzo03e48db2020-06-11 18:35:13 -0700950set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
951 CACHE INTERNAL "IREE: Include Directories" FORCE)
Stella Laurenzo688670f2021-09-24 18:16:25 -0700952
Jerry Wu623d1742022-03-31 18:16:02 +0000953#-------------------------------------------------------------------------------
Jerry Wu161daa72023-05-22 22:08:49 +0000954# IREE build tools
Jerry Wu623d1742022-03-31 18:16:02 +0000955#-------------------------------------------------------------------------------
956
957add_subdirectory(build_tools/benchmarks)
Jerry Wu161daa72023-05-22 22:08:49 +0000958add_subdirectory(build_tools/github_actions)
Jerry Wu71562cc2022-09-20 18:56:41 -0700959add_subdirectory(build_tools/python)
960
961#-------------------------------------------------------------------------------
Stella Laurenzoc6092c42023-03-25 19:55:37 -0700962# Optional features
Stella Laurenzo3b44a0a2022-04-18 19:57:57 -0700963#-------------------------------------------------------------------------------
964
Scott Todd4c022df2022-05-10 16:55:18 -0700965# samples/ can depend on anything, so we include it last
Stella Laurenzo1e8d1fa2022-04-22 09:50:43 -0700966if(IREE_BUILD_SAMPLES)
967 add_subdirectory(samples)
968endif()
Geoffrey Martin-Noble10f18222022-06-09 16:00:13 -0700969
970if(IREE_BUILD_TESTS)
971 iree_create_ctest_customization()
972endif()