blob: 2a6e54107c76b808e5470ee79d231e4664ed47e2 [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
Lei Zhangc4a945e2020-06-10 10:23:47 -04007#-------------------------------------------------------------------------------
Ben Vanik31c4dbb2020-11-14 12:11:16 -08008# C/C++ options as used within IREE
Ben Vanik246bc912019-09-19 15:22:12 -07009#-------------------------------------------------------------------------------
Ben Vanik31c4dbb2020-11-14 12:11:16 -080010#
11# ██ ██ █████ ██████ ███ ██ ██ ███ ██ ██████
12# ██ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ██
13# ██ █ ██ ███████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ███
14# ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
15# ███ ███ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██████
16#
17# Everything here is added to *every* iree_cc_library/iree_cc_binary/etc.
18# That includes both runtime and compiler components, and these may propagate
19# out to user code interacting with either (such as custom modules).
20#
21# Be extremely judicious in the use of these flags.
22#
23# - Need to disable a warning?
24# Usually these are encountered in compiler-specific code and can be disabled
25# in a compiler-specific way. Only add global warning disables when it's clear
26# that we never want them or that they'll show up in a lot of places.
27#
28# See: https://stackoverflow.com/questions/3378560/how-to-disable-gcc-warnings-for-a-few-lines-of-code
29#
30# - Need to add a linker dependency?
31# First figure out if you *really* need it. If it's only required on specific
32# platforms and in very specific files clang or msvc are used prefer
33# autolinking. GCC is stubborn and doesn't have autolinking so additional
34# flags may be required there.
35#
36# See: https://en.wikipedia.org/wiki/Auto-linking
37#
38# - Need to tweak a compilation mode setting (debug/asserts/etc)?
39# Don't do that here, and in general *don't do that at all* unless it's behind
40# a very specific IREE-prefixed cmake flag (like IREE_SIZE_OPTIMIZED).
41# There's no one-size solution when we are dealing with cross-project and
42# cross-compiled binaries - there's no safe way to set global options that
43# won't cause someone to break, and you probably don't really need to do
44# change that setting anyway. Follow the rule of least surprise: if the user
45# has CMake's Debug configuration active then don't force things into release
46# mode, etc.
47#
48# - Need to add an include directory?
49# Don't do that here. Always prefer to fully-specify the path from the IREE
50# workspace root when it's known that the compilation will be occuring using
51# the files within the IREE checkout; for example, instead of adding a global
52# include path to third_party/foo/ and #include <foo.h>'ing, just
53# #include "third_party/foo/foo.h". This reduces build configuration, makes it
54# easier for readers to find the files, etc.
55#
56# - Still think you need to add an include directory? (system includes, etc)
57# Don't do that here, either. It's highly doubtful that every single target in
58# all of IREE (both compiler and runtime) on all platforms (both host and
59# cross-compilation targets) needs your special include directory. Add it on
60# the COPTS of the target you are using it in and, ideally, private to that
61# target (used in .c/cc files, not in a .h that leaks the include path
62# requirements to all consumers of the API).
Ben Vanik246bc912019-09-19 15:22:12 -070063
Lei Zhang6c5907b2020-06-02 09:06:08 -070064set(IREE_CXX_STANDARD ${CMAKE_CXX_STANDARD})
Ben Vanik185d30c2019-09-19 14:24:11 -070065
Ben Vanik3c082aa2020-11-16 04:03:11 -080066# TODO(benvanik): fix these names (or remove entirely).
Ben Vanik479ef302020-11-14 08:36:35 -080067set(IREE_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Ben Vanik3c082aa2020-11-16 04:03:11 -080068set(IREE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
69set(IREE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Ben Vanik185d30c2019-09-19 14:24:11 -070070
Ben Vanikcc2aff92019-09-24 10:23:55 -070071iree_select_compiler_opts(IREE_DEFAULT_COPTS
Geoffrey Martin-Nobled9ea5012019-12-10 08:53:35 -080072 CLANG
Geoffrey Martin-Noblee86278d2020-07-16 10:10:51 -070073 # Set clang diagnostics. These largely match the set of warnings used within
74 # Google. They have not been audited super carefully by the IREE team but
75 # are generally thought to be a good set and consistency with those used
Copybara-Service583c5ea2020-11-04 09:19:30 -080076 # internally is very useful when importing. If you feel that some of these
Geoffrey Martin-Noblee86278d2020-07-16 10:10:51 -070077 # should be different, please raise an issue!
Geoffrey Martin-Noblef822f922020-12-28 09:11:19 -080078
79 # Please keep these in sync with build_tools/bazel/iree.bazelrc
80
Geoffrey Martin-Noblee86278d2020-07-16 10:10:51 -070081 "-Wall"
82
83 # Disable warnings we don't care about or that generally have a low
84 # signal/noise ratio.
85 "-Wno-ambiguous-member-template"
86 "-Wno-char-subscripts"
Geoffrey Martin-Noble7a3b63d2021-03-01 13:10:33 -080087 "-Wno-deprecated-declarations"
Geoffrey Martin-Noblee86278d2020-07-16 10:10:51 -070088 "-Wno-extern-c-compat" # Matches upstream. Cannot impact due to extern C inclusion method.
89 "-Wno-gnu-alignof-expression"
90 "-Wno-gnu-variable-sized-type-not-at-end"
91 "-Wno-ignored-optimization-argument"
92 "-Wno-invalid-offsetof" # Technically UB but needed for intrusive ptrs
93 "-Wno-invalid-source-encoding"
94 "-Wno-mismatched-tags"
95 "-Wno-pointer-sign"
96 "-Wno-reserved-user-defined-literal"
97 "-Wno-return-type-c-linkage"
98 "-Wno-self-assign-overloaded"
99 "-Wno-sign-compare"
100 "-Wno-signed-unsigned-wchar"
101 "-Wno-strict-overflow"
102 "-Wno-trigraphs"
103 "-Wno-unknown-pragmas"
104 "-Wno-unknown-warning-option"
105 "-Wno-unused-command-line-argument"
106 "-Wno-unused-const-variable"
107 "-Wno-unused-function"
108 "-Wno-unused-local-typedef"
109 "-Wno-unused-private-field"
110 "-Wno-user-defined-warnings"
111 # Explicitly enable some additional warnings.
112 # Some of these aren't on by default, or under -Wall, or are subsets of
113 # warnings turned off above.
114 "-Wno-ambiguous-member-template"
115 "-Wctad-maybe-unsupported"
116 "-Wfloat-overflow-conversion"
117 "-Wfloat-zero-conversion"
118 "-Wfor-loop-analysis"
119 "-Wformat-security"
120 "-Wgnu-redeclared-enum"
121 "-Wimplicit-fallthrough"
122 "-Winfinite-recursion"
123 "-Wliteral-conversion"
124 "-Wnon-virtual-dtor"
125 "-Woverloaded-virtual"
126 "-Wself-assign"
127 "-Wstring-conversion"
128 "-Wtautological-overlap-compare"
129 "-Wthread-safety"
130 "-Wthread-safety-beta"
131 "-Wunused-comparison"
Geoffrey Martin-Noblee86278d2020-07-16 10:10:51 -0700132 "-Wvla"
Geoffrey Martin-Noblee86278d2020-07-16 10:10:51 -0700133
134 # Turn off some additional warnings (CMake only)
Ben Vanikcc2aff92019-09-24 10:23:55 -0700135 "-Wno-strict-prototypes"
136 "-Wno-shadow-uncaptured-local"
Ben Vanikcc2aff92019-09-24 10:23:55 -0700137 "-Wno-gnu-zero-variadic-macro-arguments"
138 "-Wno-shadow-field-in-constructor"
139 "-Wno-unreachable-code-return"
Ben Vanikcc2aff92019-09-24 10:23:55 -0700140 "-Wno-missing-variable-declarations"
141 "-Wno-gnu-label-as-value"
Geoffrey Martin-Nobled9ea5012019-12-10 08:53:35 -0800142 CLANG_OR_GCC
143 "-Wno-unused-parameter"
Ben Vanik31c4dbb2020-11-14 12:11:16 -0800144 "-Wno-unused-variable"
Geoffrey Martin-Nobled9ea5012019-12-10 08:53:35 -0800145 "-Wno-undef"
Lei Zhang6bd3b1d2020-08-25 21:31:28 -0400146 "-fvisibility=hidden"
Stella Laurenzo516178a2021-01-08 12:18:24 -0800147 # NOTE: The RTTI setting must match what LLVM was compiled with (defaults
148 # to RTTI disabled).
149 "$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>"
150 "$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>"
151
Ben Vanikb8fe0862019-09-25 09:26:03 -0700152 MSVC_OR_CLANG_CL
Scott Todd9fa14f92021-03-09 10:43:38 -0800153 # Default warning level (severe + significant + production quality).
154 # This does not include level 4, "informational", warnings or those that
155 # are off by default.
156 # https://docs.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level
157 # Note that we set CMake policy CMP0092 (if found), making this explicit:
158 # https://cmake.org/cmake/help/v3.15/policy/CMP0092.html
159 "/W3"
160
Ben Vanik31c4dbb2020-11-14 12:11:16 -0800161 # Exclude a bunch of rarely-used APIs, such as crypto/DDE/shell.
162 # https://docs.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers
163 # NOTE: this is not really required anymore for build performance but does
164 # work around some issues that crop up with header version compatibility
165 # (abseil has issues with winsock versions).
Ben Vanikb8fe0862019-09-25 09:26:03 -0700166 "/DWIN32_LEAN_AND_MEAN"
Ben Vanik31c4dbb2020-11-14 12:11:16 -0800167
168 # Don't allow windows.h to define MIN and MAX and conflict with the STL.
169 # There's no legit use for these macros as any code we are writing ourselves
170 # that we want a MIN/MAX in should be using an IREE-prefixed version
171 # instead: iree_min iree_max
172 # https://stackoverflow.com/a/4914108
173 "/DNOMINMAX"
174
175 # Adds M_PI and other constants to <math.h>/<cmath> (to match non-windows).
176 # https://docs.microsoft.com/en-us/cpp/c-runtime-library/math-constants
Scott Toddf3960222020-09-18 15:58:35 -0700177 "/D_USE_MATH_DEFINES"
Ben Vanik31c4dbb2020-11-14 12:11:16 -0800178
Ben Vanikb7c92bf2020-11-14 13:32:28 -0800179 # Disable the "deprecation" warnings about CRT functions like strcpy.
180 # Though the secure versions *are* better, they aren't portable and as such
181 # just make cross-platform code annoying. One solution is to reimplement
182 # them in a portable fashion and use those - and that's what we try to do
183 # in certain places where we can get away with it. Other uses, like getenv,
184 # are fine as these are not intended for use in core runtime code that needs
185 # to be secure (friends don't let friends ship entire compiler stacks
186 # embedded inside security sensitive applications anyway :).
187 # https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt
188 "/D_CRT_SECURE_NO_WARNINGS"
189
190 # With the above said about the "deprecated" functions; this useful flag
191 # will at least try to use them when possible without any change to user
192 # code. Note however because the new versions use templates they won't be
193 # activated in C code; that's fine.
194 # https://docs.microsoft.com/en-us/cpp/c-runtime-library/secure-template-overloads
195 "/D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES"
196
Stella Laurenzo516178a2021-01-08 12:18:24 -0800197 # Configure RTTI generation.
198 # - /GR - Enable generation of RTTI (default)
199 # - /GR- - Disables generation of RTTI
200 # https://docs.microsoft.com/en-us/cpp/build/reference/gr-enable-run-time-type-information?view=msvc-160
201 "/GR-"
Ben Vanik31c4dbb2020-11-14 12:11:16 -0800202
203 # Default max section count is 64k, which is woefully inadequate for some of
204 # the insanely bloated tablegen outputs LLVM/MLIR produces. This cranks it
205 # up to 2^32. It's not great that we have to generate/link files like that
206 # but it's better to not get spurious failures during LTCG.
207 # https://docs.microsoft.com/en-us/cpp/build/reference/bigobj-increase-number-of-sections-in-dot-obj-file
208 "/bigobj"
209
Ben Vanikb7c92bf2020-11-14 13:32:28 -0800210 # "nonstandard extension used : zero-sized array in struct/union"
211 # This happens with unsized or zero-length arrays at the end of structs,
212 # which is completely valid in C where we do it and get this warning. Shut
213 # it up and rely on the better warnings from clang to catch if we try to
214 # use it where it really matters (on a class that has copy/move ctors, etc).
215 # https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-levels-2-and-4-c4200
216 "/wd4200"
217
Ben Vanikc17659f2020-11-14 13:58:45 -0800218 # "signed/unsigned mismatch in comparison"
219 # This is along the lines of a generic implicit conversion warning but tends
220 # to crop up in code that implicitly treats unsigned size_t values as if
221 # they were signed values instead of properly using ssize_t. In certain
222 # cases where the comparison being performed may be guarding access to
223 # memory this can cause unexpected behavior ("-1ull < 512ull, great let's
Ben Vanik7f3a7e32020-11-14 14:16:07 -0800224 # dereference buffer[-1ull]!").
Ben Vanikc17659f2020-11-14 13:58:45 -0800225 # https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4018
226 #
Ben Vanik7f3a7e32020-11-14 14:16:07 -0800227 # TODO(#3844): remove this (or make it per-file to iree/compiler, as LLVM
Ben Vanikc17659f2020-11-14 13:58:45 -0800228 # tends to not care about these kind of things and it crops up there a lot).
Ben Vanikafd7abe2020-10-07 09:01:53 -0700229 "/wd4018"
Ben Vanikc17659f2020-11-14 13:58:45 -0800230
Ben Vanik7f3a7e32020-11-14 14:16:07 -0800231 # Also common in LLVM is mismatching signed/unsigned math. That's even more
232 # dangerous than C4018: almost always these crop up in doing something with
233 # a size_t and a non-size_t value (usually int or something like it) and do
234 # you want out-of-bounds access exploits? Because that's how you get
235 # out-of-bounds access exploits. Before fuzzers took over finding code and
236 # trying to compile it with this warning forced to be an error was a way to
237 # narrow down the places to look for attack vectors. I lived through the
238 # Microsoft SAL/safe-int code red, and once you get used to using the safe
239 # buffer offset/size manipulation functions it eliminates all kinds of
240 # annoying bugs - as well as potential security issues.
241 #
242 # TODO(#3844): work to remove this class of errors from our code. It's
243 # almost entirely in LLVM related stuff so per-file iree/compiler/... would
244 # be fine.
245 "/wd4146" # operator applied to unsigned type, result still unsigned
246 "/wd4244" # possible loss of data
247 "/wd4267" # initializing: possible loss of data
248
Ben Vanikc17659f2020-11-14 13:58:45 -0800249 # Misc tweaks to better match reasonable clang/gcc behavior:
250 "/wd4005" # allow: macro redefinition
251 "/wd4065" # allow: switch statement contains 'default' but no 'case' labels
252 "/wd4141" # allow: inline used more than once
253 "/wd4624" # allow: destructor was implicitly defined as deleted
254
255 # TODO(benvanik): confirm these are all still required and document:
256 "/wd4146" # operator applied to unsigned type, result still unsigned
257 "/wd4244" # possible loss of data
258 "/wd4267" # initializing: possible loss of data
259 "/wd5105" # allow: macro expansion producing 'defined' has undefined behavior
Ben Vanik512d2d32019-09-20 13:22:34 -0700260)
Ben Vanik31c4dbb2020-11-14 12:11:16 -0800261
Scott Toddb8c38112021-03-09 10:46:10 -0800262# On MSVC, CMake sets /GR by default (enabling RTTI), but we set /GR-
263# (disabling it) above. To avoid Command line warning D9025 which warns about
264# overriding the flag value, we remove /GR from global CMake flags.
265#
266# Note: this may have ripple effects on downstream projects using IREE. If this
267# is a problem for your project, please reach out to us and we'll figure out a
268# compatible solution.
269#
270# See also:
271# https://github.com/google/iree/issues/4665.
272# https://discourse.cmake.org/t/how-to-fix-build-warning-d9025-overriding-gr-with-gr/878
273# https://gitlab.kitware.com/cmake/cmake/-/issues/20610
Lei Zhang3075d7b2021-04-22 17:03:55 -0400274if(CMAKE_CXX_FLAGS AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
275 string(REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
Scott Toddb8c38112021-03-09 10:46:10 -0800276endif()
277
Cindy Liu331946c2021-06-01 12:20:30 -0700278if(NOT ANDROID AND ${IREE_ENABLE_THREADING})
Ben Vanik36225a42020-11-14 12:34:04 -0800279 iree_select_compiler_opts(_IREE_PTHREADS_LINKOPTS
280 CLANG_OR_GCC
281 "-lpthread"
282 )
283else()
284 # Android provides its own pthreads support with no linking required.
285endif()
286
Kojo Acquah68c27a02021-04-08 16:28:48 -0700287if(ANDROID)
288 # logging.h on Android needs llog to link in Android logging.
289 iree_select_compiler_opts(_IREE_LOGGING_LINKOPTS
290 CLANG_OR_GCC
291 "-llog"
292 )
293endif()
294
Ben Vanik36225a42020-11-14 12:34:04 -0800295iree_select_compiler_opts(IREE_DEFAULT_LINKOPTS
Ben Vanik36225a42020-11-14 12:34:04 -0800296 CLANG_OR_GCC
297 # Required by all modern software, effectively:
Ben Vanikb70104b2021-05-02 11:19:22 -0700298 "-lm"
Ben Vanik36225a42020-11-14 12:34:04 -0800299 ${_IREE_PTHREADS_LINKOPTS}
Kojo Acquah68c27a02021-04-08 16:28:48 -0700300 ${_IREE_LOGGING_LINKOPTS}
Ben Vanikb34a2e52021-06-01 15:31:35 -0700301 MSVC
302 "-natvis:${CMAKE_SOURCE_DIR}/iree/iree.natvis"
Ben Vanik36225a42020-11-14 12:34:04 -0800303)
Ben Vanik31c4dbb2020-11-14 12:11:16 -0800304
Ben Vanik90faf212020-11-15 17:09:49 -0800305# Add to LINKOPTS on a binary to configure it for X/Wayland/Windows/etc
306# depending on the target cross-compilation platform.
307if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
308 set(IREE_TARGET_GUI_LINKOPTS "-SUBSYSTEM:WINDOWS")
309else()
310 set(IREE_TARGET_GUI_LINKOPTS "")
311endif()
312
Ben Vanik512d2d32019-09-20 13:22:34 -0700313#-------------------------------------------------------------------------------
Ben Vanik0bef3002020-08-08 11:05:19 -0700314# Size-optimized build flags
315#-------------------------------------------------------------------------------
316
317 # TODO(#898): add a dedicated size-constrained configuration.
318if(${IREE_SIZE_OPTIMIZED})
319 iree_select_compiler_opts(IREE_SIZE_OPTIMIZED_DEFAULT_COPTS
Ben Vanik4bb17bd2021-08-04 16:14:21 -0700320 CLANG_OR_GCC
321 "-DIREE_STATUS_MODE=0"
322 "-DIREE_HAL_MODULE_STRING_UTIL_ENABLE=0"
323 "-DIREE_VM_EXT_I64_ENABLE=0"
324 "-DIREE_VM_EXT_F32_ENABLE=0"
Ben Vanik0bef3002020-08-08 11:05:19 -0700325 MSVC_OR_CLANG_CL
326 "/GS-"
327 "/GL"
328 "/Gw"
329 "/Gy"
330 "/DNDEBUG"
331 "/DIREE_STATUS_MODE=0"
Ben Vanik8e965782021-05-04 09:31:25 -0700332 "/DIREE_FLAGS_ENABLE_CLI=0"
333 "/DIREE_HAL_MODULE_STRING_UTIL_ENABLE=0"
334 "/DIREE_VM_EXT_I64_ENABLE=0"
335 "/DIREE_VM_EXT_F32_ENABLE=0"
Ben Vanikdab0b282021-02-05 23:52:57 -0800336 "/Os"
337 "/Oy"
Ben Vanik8e965782021-05-04 09:31:25 -0700338 "/Zi"
339 "/c"
Ben Vanik0bef3002020-08-08 11:05:19 -0700340 )
341 iree_select_compiler_opts(IREE_SIZE_OPTIMIZED_DEFAULT_LINKOPTS
342 MSVC_OR_CLANG_CL
Ben Vanik8e965782021-05-04 09:31:25 -0700343 "-DEBUG:FULL"
Ben Vanikdab0b282021-02-05 23:52:57 -0800344 "-LTCG"
345 "-opt:ref,icf"
Ben Vanik0bef3002020-08-08 11:05:19 -0700346 )
347 # TODO(#898): make this only impact the runtime (IREE_RUNTIME_DEFAULT_...).
348 set(IREE_DEFAULT_COPTS
349 "${IREE_DEFAULT_COPTS}"
350 "${IREE_SIZE_OPTIMIZED_DEFAULT_COPTS}")
351 set(IREE_DEFAULT_LINKOPTS
352 "${IREE_DEFAULT_LINKOPTS}"
353 "${IREE_SIZE_OPTIMIZED_DEFAULT_LINKOPTS}")
354endif()
355
356#-------------------------------------------------------------------------------
Ben Vanik512d2d32019-09-20 13:22:34 -0700357# Compiler: Clang/LLVM
358#-------------------------------------------------------------------------------
359
360# TODO(benvanik): Clang/LLVM options.
361
362#-------------------------------------------------------------------------------
363# Compiler: GCC
364#-------------------------------------------------------------------------------
365
366# TODO(benvanik): GCC options.
367
368#-------------------------------------------------------------------------------
369# Compiler: MSVC
370#-------------------------------------------------------------------------------
371
372# TODO(benvanik): MSVC options.
373
374#-------------------------------------------------------------------------------
Stella Laurenzo275215d2021-08-06 11:50:28 -0700375# Third party: llvm-project
Marius Brehler18152f12020-01-06 09:30:20 -0800376#-------------------------------------------------------------------------------
377
Lei Zhang1ff70612020-06-04 10:12:41 -0400378set(MLIR_TABLEGEN_EXE mlir-tblgen)
379# iree-tblgen is not defined using the add_tablegen mechanism as other TableGen
380# tools in LLVM.
Lei Zhang486e7072020-06-16 11:42:49 -0400381iree_get_executable_path(IREE_TABLEGEN_EXE iree-tblgen)
Marius Brehler9a924632019-12-17 13:56:31 -0800382
Ben Vanik6b112ef2019-10-03 10:45:14 -0700383#-------------------------------------------------------------------------------
Marius Brehlerec38aa02020-07-29 13:18:45 -0700384# Third party: mlir-emitc
385#-------------------------------------------------------------------------------
386
387if(IREE_ENABLE_EMITC)
Marius Brehlerec38aa02020-07-29 13:18:45 -0700388 add_definitions(-DIREE_HAVE_EMITC_DIALECT)
389endif()