1 ============================
2 Clang Compiler User's Manual
3 ============================
5 .. include:: <isonum.txt>
13 The Clang Compiler is an open-source compiler for the C family of
14 programming languages, aiming to be the best in class implementation of
15 these languages. Clang builds on the LLVM optimizer and code generator,
16 allowing it to provide high-quality optimization and code generation
17 support for many targets. For more general information, please see the
18 `Clang Web Site <https://clang.llvm.org>`_ or the `LLVM Web
19 Site <https://llvm.org>`_.
21 This document describes important notes about using Clang as a compiler
22 for an end-user, documenting the supported features, command line
23 options, etc. If you are interested in using Clang to build a tool that
24 processes code, please see :doc:`InternalsManual`. If you are interested in the
25 `Clang Static Analyzer <https://clang-analyzer.llvm.org>`_, please see its web
28 Clang is one component in a complete toolchain for C family languages.
29 A separate document describes the other pieces necessary to
30 :doc:`assemble a complete toolchain <Toolchain>`.
32 Clang is designed to support the C family of programming languages,
33 which includes :ref:`C <c>`, :ref:`Objective-C <objc>`, :ref:`C++ <cxx>`, and
34 :ref:`Objective-C++ <objcxx>` as well as many dialects of those. For
35 language-specific information, please see the corresponding language
38 - :ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO
40 - :ref:`Objective-C Language <objc>`: ObjC 1, ObjC 2, ObjC 2.1, plus
41 variants depending on base language.
42 - :ref:`C++ Language <cxx>`
43 - :ref:`Objective C++ Language <objcxx>`
44 - :ref:`OpenCL Kernel Language <opencl>`: OpenCL C 1.0, 1.1, 1.2, 2.0, 3.0,
45 and C++ for OpenCL 1.0 and 2021.
47 In addition to these base languages and their dialects, Clang supports a
48 broad variety of language extensions, which are documented in the
49 corresponding language section. These extensions are provided to be
50 compatible with the GCC, Microsoft, and other popular compilers as well
51 as to improve functionality through Clang-specific features. The Clang
52 driver and language features are intentionally designed to be as
53 compatible with the GNU GCC compiler as reasonably possible, easing
54 migration from GCC to Clang. In most cases, code "just works".
55 Clang also provides an alternative driver, :ref:`clang-cl`, that is designed
56 to be compatible with the Visual C++ compiler, cl.exe.
58 In addition to language specific features, Clang has a variety of
59 features that depend on what CPU architecture or operating system is
60 being compiled for. Please see the :ref:`Target-Specific Features and
61 Limitations <target_features>` section for more details.
63 The rest of the introduction introduces some basic :ref:`compiler
64 terminology <terminology>` that is used throughout this manual and
65 contains a basic :ref:`introduction to using Clang <basicusage>` as a
66 command line compiler.
73 Front end, parser, backend, preprocessor, undefined behavior,
81 Intro to how to use a C compiler for newbies.
83 compile + link compile then link debug info enabling optimizations
84 picking a language to use, defaults to C17 by default. Autosenses based
85 on extension. using a makefile
90 This section is generally an index into other sections. It does not go
91 into depth on the ones that are covered by other sections. However, the
92 first part introduces the language selection and other high level
93 options like :option:`-c`, :option:`-g`, etc.
95 Options to Control Error and Warning Messages
96 ---------------------------------------------
100 Turn warnings into errors.
102 .. This is in plain monospaced font because it generates the same label as
103 .. -Werror, and Sphinx complains.
107 Turn warning "foo" into an error.
109 .. option:: -Wno-error=foo
111 Turn warning "foo" into a warning even if :option:`-Werror` is specified.
115 Enable warning "foo".
116 See the :doc:`diagnostics reference <DiagnosticsReference>` for a complete
117 list of the warning flags that can be specified in this way.
121 Disable warning "foo".
125 Disable all diagnostics.
127 .. option:: -Weverything
129 :ref:`Enable all diagnostics. <diagnostics_enable_everything>`
131 .. option:: -pedantic
133 Warn on language extensions.
135 .. option:: -pedantic-errors
137 Error on language extensions.
139 .. option:: -Wsystem-headers
141 Enable warnings from system headers.
143 .. option:: -ferror-limit=123
145 Stop emitting diagnostics after 123 errors have been produced. The default is
146 20, and the error limit can be disabled with `-ferror-limit=0`.
148 .. option:: -ftemplate-backtrace-limit=123
150 Only emit up to 123 template instantiation notes within the template
151 instantiation backtrace for a single warning or error. The default is 10, and
152 the limit can be disabled with `-ftemplate-backtrace-limit=0`.
154 .. option:: --warning-suppression-mappings=foo.txt
156 :ref:`Suppress certain diagnostics for certain files. <warning_suppression_mappings>`
158 .. _cl_diag_formatting:
160 Formatting of Diagnostics
161 ^^^^^^^^^^^^^^^^^^^^^^^^^
163 Clang aims to produce beautiful diagnostics by default, particularly for
164 new users that first come to Clang. However, different people have
165 different preferences, and sometimes Clang is driven not by a human,
166 but by a program that wants consistent and easily parsable output. For
167 these cases, Clang provides a wide range of options to control the exact
168 output format of the diagnostics that it generates.
170 .. _opt_fshow-column:
172 .. option:: -f[no-]show-column
174 Print column number in diagnostic.
176 This option, which defaults to on, controls whether or not Clang
177 prints the column number of a diagnostic. For example, when this is
178 enabled, Clang will print something like:
182 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
187 When this is disabled, Clang will print "test.c:28: warning..." with
190 The printed column numbers count bytes from the beginning of the
191 line; take care if your source contains multibyte characters.
193 .. _opt_fshow-source-location:
195 .. option:: -f[no-]show-source-location
197 Print source file/line/column information in diagnostic.
199 This option, which defaults to on, controls whether or not Clang
200 prints the filename, line number and column number of a diagnostic.
201 For example, when this is enabled, Clang will print something like:
205 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
210 When this is disabled, Clang will not print the "test.c:28:8: "
213 .. _opt_fcaret-diagnostics:
215 .. option:: -f[no-]caret-diagnostics
217 Print source line and ranges from source code in diagnostic.
218 This option, which defaults to on, controls whether or not Clang
219 prints the source line, source ranges, and caret when emitting a
220 diagnostic. For example, when this is enabled, Clang will print
225 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
230 .. option:: -f[no-]color-diagnostics
232 This option, which defaults to on when a color-capable terminal is
233 detected, controls whether or not Clang prints diagnostics in color.
235 When this option is enabled, Clang will use colors to highlight
236 specific parts of the diagnostic, e.g.,
238 .. nasty hack to not lose our dignity
243 <b><span style="color:black">test.c:28:8: <span style="color:magenta">warning</span>: extra tokens at end of #endif directive [-Wextra-tokens]</span></b>
245 <span style="color:green">^</span>
246 <span style="color:green">//</span>
249 When this is disabled, Clang will just print:
253 test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
258 If the ``NO_COLOR`` environment variable is defined and not empty
259 (regardless of value), color diagnostics are disabled. If ``NO_COLOR`` is
260 defined and ``-fcolor-diagnostics`` is passed on the command line, Clang
261 will honor the command line argument.
263 .. option:: -fansi-escape-codes
265 Controls whether ANSI escape codes are used instead of the Windows Console
266 API to output colored diagnostics. This option is only used on Windows and
269 .. option:: -fdiagnostics-format=clang/msvc/vi
271 Changes diagnostic output format to better match IDEs and command line tools.
273 This option controls the output format of the filename, line number,
274 and column printed in diagnostic messages. The options, and their
275 affect on formatting a simple conversion diagnostic, follow:
280 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
285 t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int'
290 t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int'
292 .. _opt_fdiagnostics-show-option:
294 .. option:: -f[no-]diagnostics-show-option
296 Enable ``[-Woption]`` information in diagnostic line.
298 This option, which defaults to on, controls whether or not Clang
299 prints the associated :ref:`warning group <cl_diag_warning_groups>`
300 option name when outputting a warning diagnostic. For example, in
305 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
310 Passing **-fno-diagnostics-show-option** will prevent Clang from
311 printing the [:option:`-Wextra-tokens`] information in
312 the diagnostic. This information tells you the flag needed to enable
313 or disable the diagnostic, either from the command line or through
314 :ref:`#pragma GCC diagnostic <pragma_GCC_diagnostic>`.
316 .. option:: -fdiagnostics-show-category=none/id/name
318 Enable printing category information in diagnostic line.
320 This option, which defaults to "none", controls whether or not Clang
321 prints the category associated with a diagnostic when emitting it.
322 Each diagnostic may or many not have an associated category, if it
323 has one, it is listed in the diagnostic categorization field of the
324 diagnostic line (in the []'s).
326 For example, a format string warning will produce these three
327 renditions based on the setting of this option:
331 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat]
332 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1]
333 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String]
335 This category can be used by clients that want to group diagnostics
336 by category, so it should be a high level category. We want dozens
337 of these, not hundreds or thousands of them.
339 .. _opt_fsave-optimization-record:
341 .. option:: -f[no-]save-optimization-record[=<format>]
343 Enable optimization remarks during compilation and write them to a separate
346 This option, which defaults to off, controls whether Clang writes
347 optimization reports to a separate file. By recording diagnostics in a file,
348 users can parse or sort the remarks in a convenient way.
350 By default, the serialization format is YAML.
352 The supported serialization formats are:
354 - .. _opt_fsave_optimization_record_yaml:
356 ``-fsave-optimization-record=yaml``: A structured YAML format.
358 - .. _opt_fsave_optimization_record_bitstream:
360 ``-fsave-optimization-record=bitstream``: A binary format based on LLVM
363 The output file is controlled by :option:`-foptimization-record-file`.
365 In the absence of an explicit output file, the file is chosen using the
368 ``<base>.opt.<format>``
370 where ``<base>`` is based on the output file of the compilation (whether
371 it's explicitly specified through `-o` or not) when used with `-c` or `-S`.
374 * ``clang -fsave-optimization-record -c in.c -o out.o`` will generate
377 * ``clang -fsave-optimization-record -c in.c`` will generate
380 When targeting (Thin)LTO, the base is derived from the output filename, and
381 the extension is not dropped.
383 When targeting ThinLTO, the following scheme is used:
385 ``<base>.opt.<format>.thin.<num>.<format>``
387 Darwin-only: when used for generating a linked binary from a source file
388 (through an intermediate object file), the driver will invoke `cc1` to
389 generate a temporary object file. The temporary remark file will be emitted
390 next to the object file, which will then be picked up by `dsymutil` and
391 emitted in the .dSYM bundle. This is available for all formats except YAML.
395 ``clang -fsave-optimization-record=bitstream in.c -o out`` will generate
397 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.o``
399 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.opt.bitstream``
403 * ``out.dSYM/Contents/Resources/Remarks/out``
405 Darwin-only: compiling for multiple architectures will use the following
408 ``<base>-<arch>.opt.<format>``
410 Note that this is incompatible with passing the
411 :option:`-foptimization-record-file` option.
413 .. option:: -foptimization-record-file
415 Control the file to which optimization reports are written. This implies
416 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`.
418 On Darwin platforms, this is incompatible with passing multiple
419 ``-arch <arch>`` options.
421 .. option:: -foptimization-record-passes
423 Only include passes which match a specified regular expression.
425 When optimization reports are being output (see
426 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`), this
427 option controls the passes that will be included in the final report.
429 If this option is not used, all the passes are included in the optimization
432 .. _opt_fdiagnostics-show-hotness:
434 .. option:: -f[no-]diagnostics-show-hotness
436 Enable profile hotness information in diagnostic line.
438 This option controls whether Clang prints the profile hotness associated
439 with diagnostics in the presence of profile-guided optimization information.
440 This is currently supported with optimization remarks (see
441 :ref:`Options to Emit Optimization Reports <rpass>`). The hotness information
442 allows users to focus on the hot optimization remarks that are likely to be
443 more relevant for run-time performance.
445 For example, in this output, the block containing the callsite of `foo` was
446 executed 3000 times according to the profile data:
450 s.c:7:10: remark: foo inlined into bar (hotness: 3000) [-Rpass-analysis=inline]
451 sum += foo(x, x - 2);
454 This option is implied when
455 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>` is used.
456 Otherwise, it defaults to off.
458 .. option:: -fdiagnostics-hotness-threshold
460 Prevent optimization remarks from being output if they do not have at least
463 This option, which defaults to zero, controls the minimum hotness an
464 optimization remark would need in order to be output by Clang. This is
465 currently supported with optimization remarks (see :ref:`Options to Emit
466 Optimization Reports <rpass>`) when profile hotness information in
467 diagnostics is enabled (see
468 :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`).
470 .. _opt_fdiagnostics-fixit-info:
472 .. option:: -f[no-]diagnostics-fixit-info
474 Enable "FixIt" information in the diagnostics output.
476 This option, which defaults to on, controls whether or not Clang
477 prints the information on how to fix a specific diagnostic
478 underneath it when it knows. For example, in this output:
482 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
487 Passing **-fno-diagnostics-fixit-info** will prevent Clang from
488 printing the "//" line at the end of the message. This information
489 is useful for users who may not understand what is wrong, but can be
490 confusing for machine parsing.
492 .. _opt_fdiagnostics-print-source-range-info:
494 .. option:: -fdiagnostics-print-source-range-info
496 Print machine parsable information about source ranges.
497 This option makes Clang print information about source ranges in a machine
498 parsable format after the file/line/column number information. The
499 information is a simple sequence of brace enclosed ranges, where each range
500 lists the start and end line/column locations. For example, in this output:
504 exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float')
505 P = (P-42) + Gamma*4;
508 The {}'s are generated by -fdiagnostics-print-source-range-info.
510 The printed column numbers count bytes from the beginning of the
511 line; take care if your source contains multibyte characters.
513 .. option:: -fdiagnostics-parseable-fixits
515 Print Fix-Its in a machine parseable form.
517 This option makes Clang print available Fix-Its in a machine
518 parseable format at the end of diagnostics. The following example
519 illustrates the format:
523 fix-it:"t.cpp":{7:25-7:29}:"Gamma"
525 The range printed is a half-open range, so in this example the
526 characters at column 25 up to but not including column 29 on line 7
527 in t.cpp should be replaced with the string "Gamma". Either the
528 range or the replacement string may be empty (representing strict
529 insertions and strict erasures, respectively). Both the file name
530 and the insertion string escape backslash (as "\\\\"), tabs (as
531 "\\t"), newlines (as "\\n"), double quotes(as "\\"") and
532 non-printable characters (as octal "\\xxx").
534 The printed column numbers count bytes from the beginning of the
535 line; take care if your source contains multibyte characters.
537 .. option:: -fno-elide-type
539 Turns off elision in template type printing.
541 The default for template type printing is to elide as many template
542 arguments as possible, removing those which are the same in both
543 template types, leaving only the differences. Adding this flag will
544 print all the template arguments. If supported by the terminal,
545 highlighting will still appear on differing arguments.
551 t.cc:4:5: note: candidate function not viable: no known conversion from 'vector<map<[...], map<float, [...]>>>' to 'vector<map<[...], map<double, [...]>>>' for 1st argument;
557 t.cc:4:5: note: candidate function not viable: no known conversion from 'vector<map<int, map<float, int>>>' to 'vector<map<int, map<double, int>>>' for 1st argument;
559 .. option:: -fdiagnostics-show-template-tree
561 Template type diffing prints a text tree.
563 For diffing large templated types, this option will cause Clang to
564 display the templates as an indented text tree, one argument per
565 line, with differences marked inline. This is compatible with
572 t.cc:4:5: note: candidate function not viable: no known conversion from 'vector<map<[...], map<float, [...]>>>' to 'vector<map<[...], map<double, [...]>>>' for 1st argument;
574 With :option:`-fdiagnostics-show-template-tree`:
578 t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument;
587 .. option:: -fcaret-diagnostics-max-lines:
589 Controls how many lines of code clang prints for diagnostics. By default,
590 clang prints a maximum of 16 lines of code.
593 .. option:: -fdiagnostics-show-line-numbers:
595 Controls whether clang will print a margin containing the line number on
596 the left of each line of code it prints for diagnostics.
602 test.cpp:5:1: error: 'main' must return 'int'
608 With -fno-diagnostics-show-line-numbers:
612 test.cpp:5:1: error: 'main' must return 'int'
619 .. _cl_diag_warning_groups:
621 Individual Warning Groups
622 ^^^^^^^^^^^^^^^^^^^^^^^^^
624 TODO: Generate this from tblgen. Define one anchor per warning group.
626 .. option:: -Wextra-tokens
628 Warn about excess tokens at the end of a preprocessor directive.
630 This option, which defaults to on, enables warnings about extra
631 tokens at the end of preprocessor directives. For example:
635 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
639 These extra tokens are not strictly conforming, and are usually best
640 handled by commenting them out.
642 .. option:: -Wambiguous-member-template
644 Warn about unqualified uses of a member template whose name resolves to
645 another template at the location of the use.
647 This option, which defaults to on, enables a warning in the
652 template<typename T> struct set{};
653 template<typename T> struct trait { typedef const T& type; };
655 template<typename T> void set(typename trait<T>::type value) {}
662 C++ [basic.lookup.classref] requires this to be an error, but,
663 because it's hard to work around, Clang downgrades it to a warning
666 .. option:: -Wbind-to-temporary-copy
668 Warn about an unusable copy constructor when binding a reference to a
671 This option enables warnings about binding a
672 reference to a temporary when the temporary doesn't have a usable
673 copy constructor. For example:
680 NonCopyable(const NonCopyable&);
682 void foo(const NonCopyable&);
684 foo(NonCopyable()); // Disallowed in C++98; allowed in C++11.
689 struct NonCopyable2 {
691 NonCopyable2(NonCopyable2&);
693 void foo(const NonCopyable2&);
695 foo(NonCopyable2()); // Disallowed in C++98; allowed in C++11.
698 Note that if ``NonCopyable2::NonCopyable2()`` has a default argument
699 whose instantiation produces a compile error, that error will still
700 be a hard error in C++98 mode even if this warning is turned off.
702 Options to Control Clang Crash Diagnostics
703 ------------------------------------------
705 As unbelievable as it may sound, Clang does crash from time to time.
706 Generally, this only occurs to those living on the `bleeding
707 edge <https://llvm.org/releases/download.html#svn>`_. Clang goes to great
708 lengths to assist you in filing a bug report. Specifically, Clang
709 generates preprocessed source file(s) and associated run script(s) upon
710 a crash. These files should be attached to a bug report to ease
711 reproducibility of the failure. Below are the command line options to
712 control the crash diagnostics.
714 .. option:: -fcrash-diagnostics=<val>
718 * ``off`` (Disable auto-generation of preprocessed source files during a clang crash.)
719 * ``compiler`` (Generate diagnostics for compiler crashes (default))
720 * ``all`` (Generate diagnostics for all tools which support it)
722 .. option:: -fno-crash-diagnostics
724 Disable auto-generation of preprocessed source files during a clang crash.
726 The -fno-crash-diagnostics flag can be helpful for speeding the process
727 of generating a delta reduced test case.
729 .. option:: -fcrash-diagnostics-dir=<dir>
731 Specify where to write the crash diagnostics files; defaults to the
732 usual location for temporary files.
734 .. envvar:: CLANG_CRASH_DIAGNOSTICS_DIR=<dir>
736 Like ``-fcrash-diagnostics-dir=<dir>``, specifies where to write the
737 crash diagnostics files, but with lower precedence than the option.
739 Clang is also capable of generating preprocessed source file(s) and associated
740 run script(s) even without a crash. This is specially useful when trying to
741 generate a reproducer for warnings or errors while using modules.
743 .. option:: -gen-reproducer
745 Generates preprocessed source files, a reproducer script and if relevant, a
746 cache containing: built module pcm's and all headers needed to rebuild the
751 Options to Emit Optimization Reports
752 ------------------------------------
754 Optimization reports trace, at a high-level, all the major decisions
755 done by compiler transformations. For instance, when the inliner
756 decides to inline function ``foo()`` into ``bar()``, or the loop unroller
757 decides to unroll a loop N times, or the vectorizer decides to
758 vectorize a loop body.
760 Clang offers a family of flags which the optimizers can use to emit
761 a diagnostic in three cases:
763 1. When the pass makes a transformation (`-Rpass`).
765 2. When the pass fails to make a transformation (`-Rpass-missed`).
767 3. When the pass determines whether or not to make a transformation
770 NOTE: Although the discussion below focuses on `-Rpass`, the exact
771 same options apply to `-Rpass-missed` and `-Rpass-analysis`.
773 Since there are dozens of passes inside the compiler, each of these flags
774 take a regular expression that identifies the name of the pass which should
775 emit the associated diagnostic. For example, to get a report from the inliner,
776 compile the code with:
778 .. code-block:: console
780 $ clang -O2 -Rpass=inline code.cc -o code
781 code.cc:4:25: remark: foo inlined into bar [-Rpass=inline]
782 int bar(int j) { return foo(j, j - 2); }
785 Note that remarks from the inliner are identified with `[-Rpass=inline]`.
786 To request a report from every optimization pass, you should use
787 `-Rpass=.*` (in fact, you can use any valid POSIX regular
788 expression). However, do not expect a report from every transformation
789 made by the compiler. Optimization remarks do not really make sense
790 outside of the major transformations (e.g., inlining, vectorization,
791 loop optimizations) and not every optimization pass supports this
794 Note that when using profile-guided optimization information, profile hotness
795 information can be included in the remarks (see
796 :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`).
801 1. Optimization remarks that refer to function names will display the
802 mangled name of the function. Since these remarks are emitted by the
803 back end of the compiler, it does not know anything about the input
804 language, nor its mangling rules.
806 2. Some source locations are not displayed correctly. The front end has
807 a more detailed source location tracking than the locations included
808 in the debug info (e.g., the front end can locate code inside macro
809 expansions). However, the locations used by `-Rpass` are
810 translated from debug annotations. That translation can be lossy,
811 which results in some remarks having no location information.
813 Options to Emit Resource Consumption Reports
814 --------------------------------------------
816 These are options that report execution time and consumed memory of different
819 .. option:: -fproc-stat-report=
821 This option requests driver to print used memory and execution time of each
822 compilation step. The ``clang`` driver during execution calls different tools,
823 like compiler, assembler, linker etc. With this option the driver reports
824 total execution time, the execution time spent in user mode and peak memory
825 usage of each the called tool. Value of the option specifies where the report
826 is sent to. If it specifies a regular file, the data are saved to this file in
829 .. code-block:: console
831 $ clang -fproc-stat-report=abc foo.c
833 clang-11,"/tmp/foo-123456.o",92000,84000,87536
834 ld,"a.out",900,8000,53568
836 The data on each row represent:
838 * file name of the tool executable,
839 * output file name in quotes,
840 * total execution time in microseconds,
841 * execution time in user mode in microseconds,
842 * peak memory usage in Kb.
844 It is possible to specify this option without any value. In this case statistics
845 are printed on standard output in human readable format:
847 .. code-block:: console
849 $ clang -fproc-stat-report foo.c
850 clang-11: output=/tmp/foo-855a8e.o, total=68.000 ms, user=60.000 ms, mem=86920 Kb
851 ld: output=a.out, total=8.000 ms, user=4.000 ms, mem=52320 Kb
853 The report file specified in the option is locked for write, so this option
854 can be used to collect statistics in parallel builds. The report file is not
855 cleared, new data is appended to it, thus making possible to accumulate build
858 You can also use environment variables to control the process statistics reporting.
859 Setting ``CC_PRINT_PROC_STAT`` to ``1`` enables the feature, the report goes to
860 stdout in human readable format.
861 Setting ``CC_PRINT_PROC_STAT_FILE`` to a fully qualified file path makes it report
862 process statistics to the given file in the CSV format. Specifying a relative
863 path will likely lead to multiple files with the same name created in different
864 directories, since the path is relative to a changing working directory.
866 These environment variables are handy when you need to request the statistics
867 report without changing your build scripts or alter the existing set of compiler
868 options. Note that ``-fproc-stat-report`` take precedence over ``CC_PRINT_PROC_STAT``
869 and ``CC_PRINT_PROC_STAT_FILE``.
871 .. code-block:: console
873 $ export CC_PRINT_PROC_STAT=1
874 $ export CC_PRINT_PROC_STAT_FILE=~/project-build-proc-stat.csv
879 Clang options that don't fit neatly into other categories.
881 .. option:: -fgnuc-version=
883 This flag controls the value of ``__GNUC__`` and related macros. This flag
884 does not enable or disable any GCC extensions implemented in Clang. Setting
885 the version to zero causes Clang to leave ``__GNUC__`` and other
886 GNU-namespaced macros, such as ``__GXX_WEAK__``, undefined.
890 When emitting a dependency file, use formatting conventions appropriate
891 for NMake or Jom. Ignored unless another option causes Clang to emit a
894 When Clang emits a dependency file (e.g., you supplied the -M option)
895 most filenames can be written to the file without any special formatting.
896 Different Make tools will treat different sets of characters as "special"
897 and use different conventions for telling the Make tool that the character
898 is actually part of the filename. Normally Clang uses backslash to "escape"
899 a special character, which is the convention used by GNU Make. The -MV
900 option tells Clang to put double-quotes around the entire filename, which
901 is the convention used by NMake and Jom.
903 .. option:: -femit-dwarf-unwind=<value>
905 When to emit DWARF unwind (EH frame) info. This is a Mach-O-specific option.
909 * ``no-compact-unwind`` - Only emit DWARF unwind when compact unwind encodings
910 aren't available. This is the default for arm64.
911 * ``always`` - Always emit DWARF unwind regardless.
912 * ``default`` - Use the platform-specific default (``always`` for all
913 non-arm64-platforms).
915 ``no-compact-unwind`` is a performance optimization -- Clang will emit smaller
916 object files that are more quickly processed by the linker. This may cause
917 binary compatibility issues on older x86_64 targets, however, so use it with
920 .. option:: -fdisable-block-signature-string
922 Instruct clang not to emit the signature string for blocks. Disabling the
923 string can potentially break existing code that relies on it. Users should
924 carefully consider this possibiilty when using the flag.
926 .. _configuration-files:
931 Configuration files group command-line options and allow all of them to be
932 specified just by referencing the configuration file. They may be used, for
933 example, to collect options required to tune compilation for particular
934 target, such as ``-L``, ``-I``, ``-l``, ``--sysroot``, codegen options, etc.
936 Configuration files can be either specified on the command line or loaded
937 from default locations. If both variants are present, the default configuration
938 files are loaded first.
940 The command line option ``--config=`` can be used to specify explicit
941 configuration files in a Clang invocation. If the option is used multiple times,
942 all specified files are loaded, in order. For example:
946 clang --config=/home/user/cfgs/testing.txt
947 clang --config=debug.cfg --config=runtimes.cfg
949 If the provided argument contains a directory separator, it is considered as
950 a file path, and options are read from that file. Otherwise the argument is
951 treated as a file name and is searched for sequentially in the directories:
955 - the directory where Clang executable resides.
957 Both user and system directories for configuration files can be specified
958 either during build or during runtime. At build time, use
959 ``CLANG_CONFIG_FILE_USER_DIR`` and ``CLANG_CONFIG_FILE_SYSTEM_DIR``. At run
960 time use the ``--config-user-dir=`` and ``--config-system-dir=`` command line
961 options. Specifying config directories at runtime overrides the config
962 directories set at build time The first file found is used. It is an error if
963 the required file cannot be found.
965 The default configuration files are searched for in the same directories
966 following the rules described in the next paragraphs. Loading default
967 configuration files can be disabled entirely via passing
968 the ``--no-default-config`` flag.
970 First, the algorithm searches for a configuration file named
971 ``<triple>-<driver>.cfg`` where `triple` is the triple for the target being
972 built for, and `driver` is the name of the currently used driver. The algorithm
973 first attempts to use the canonical name for the driver used, then falls back
974 to the one found in the executable name.
976 The following canonical driver names are used:
978 - ``clang`` for the ``gcc`` driver (used to compile C programs)
979 - ``clang++`` for the ``gxx`` driver (used to compile C++ programs)
980 - ``clang-cpp`` for the ``cpp`` driver (pure preprocessor)
981 - ``clang-cl`` for the ``cl`` driver
982 - ``flang`` for the ``flang`` driver
983 - ``clang-dxc`` for the ``dxc`` driver
985 For example, when calling ``x86_64-pc-linux-gnu-clang-g++``,
986 the driver will first attempt to use the configuration file named::
988 x86_64-pc-linux-gnu-clang++.cfg
990 If this file is not found, it will attempt to use the name found
991 in the executable instead::
993 x86_64-pc-linux-gnu-clang-g++.cfg
995 Note that options such as ``--driver-mode=``, ``--target=``, ``-m32`` affect
996 the search algorithm. For example, the aforementioned executable called with
997 ``-m32`` argument will instead search for::
999 i386-pc-linux-gnu-clang++.cfg
1001 If none of the aforementioned files are found, the driver will instead search
1002 for separate driver and target configuration files and attempt to load both.
1003 The former is named ``<driver>.cfg`` while the latter is named
1004 ``<triple>.cfg``. Similarly to the previous variants, the canonical driver name
1005 will be preferred, and the compiler will fall back to the actual name.
1007 For example, ``x86_64-pc-linux-gnu-clang-g++`` will attempt to load two
1008 configuration files named respectively::
1011 x86_64-pc-linux-gnu.cfg
1013 with fallback to trying::
1016 x86_64-pc-linux-gnu.cfg
1018 It is not an error if either of these files is not found.
1020 The configuration file consists of command-line options specified on one or
1021 more lines. Lines composed of whitespace characters only are ignored as well as
1022 lines in which the first non-blank character is ``#``. Long options may be split
1023 between several lines by a trailing backslash. Here is example of a
1028 # Several options on line
1029 -c --target=x86_64-unknown-linux-gnu
1031 # Long option split between lines
1032 -I/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../\
1035 # other config files may be included
1038 Files included by ``@file`` directives in configuration files are resolved
1039 relative to the including file. For example, if a configuration file
1040 ``~/.llvm/target.cfg`` contains the directive ``@os/linux.opts``, the file
1041 ``linux.opts`` is searched for in the directory ``~/.llvm/os``. Another way to
1042 include a file content is using the command line option ``--config=``. It works
1043 similarly but the included file is searched for using the rules for configuration
1046 To generate paths relative to the configuration file, the ``<CFGDIR>`` token may
1047 be used. This will expand to the absolute path of the directory containing the
1050 In cases where a configuration file is deployed alongside SDK contents, the
1051 SDK directory can remain fully portable by using ``<CFGDIR>`` prefixed paths.
1052 In this way, the user may only need to specify a root configuration file with
1053 ``--config=`` to establish every aspect of the SDK with the compiler:
1058 -isystem <CFGDIR>/include
1060 -T <CFGDIR>/ldscripts/link.ld
1062 Usually, config file options are placed before command-line options, regardless
1063 of the actual operation to be performed. The exception is being made for the
1064 options prefixed with the ``$`` character. These will be used only when linker
1065 is being invoked, and added after all of the command-line specified linker
1066 inputs. Here is some example of ``$``-prefixed options:
1073 Language and Target-Independent Features
1074 ========================================
1076 Controlling Errors and Warnings
1077 -------------------------------
1079 Clang provides a number of ways to control which code constructs cause
1080 it to emit errors and warning messages, and how they are displayed to
1083 Controlling How Clang Displays Diagnostics
1084 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1086 When Clang emits a diagnostic, it includes rich information in the
1087 output, and gives you fine-grain control over which information is
1088 printed. Clang has the ability to print this information, and these are
1089 the options that control it:
1091 #. A file/line/column indicator that shows exactly where the diagnostic
1092 occurs in your code [:ref:`-fshow-column <opt_fshow-column>`,
1093 :ref:`-fshow-source-location <opt_fshow-source-location>`].
1094 #. A categorization of the diagnostic as a note, warning, error, or
1096 #. A text string that describes what the problem is.
1097 #. An option that indicates how to control the diagnostic (for
1098 diagnostics that support it)
1099 [:ref:`-fdiagnostics-show-option <opt_fdiagnostics-show-option>`].
1100 #. A :ref:`high-level category <diagnostics_categories>` for the diagnostic
1101 for clients that want to group diagnostics by class (for diagnostics
1103 [:option:`-fdiagnostics-show-category`].
1104 #. The line of source code that the issue occurs on, along with a caret
1105 and ranges that indicate the important locations
1106 [:ref:`-fcaret-diagnostics <opt_fcaret-diagnostics>`].
1107 #. "FixIt" information, which is a concise explanation of how to fix the
1108 problem (when Clang is certain it knows)
1109 [:ref:`-fdiagnostics-fixit-info <opt_fdiagnostics-fixit-info>`].
1110 #. A machine-parsable representation of the ranges involved (off by
1112 [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`].
1114 For more information please see :ref:`Formatting of
1115 Diagnostics <cl_diag_formatting>`.
1120 All diagnostics are mapped into one of these 6 classes:
1129 .. _diagnostics_categories:
1131 Diagnostic Categories
1132 ^^^^^^^^^^^^^^^^^^^^^
1134 Though not shown by default, diagnostics may each be associated with a
1135 high-level category. This category is intended to make it possible to
1136 triage builds that produce a large number of errors or warnings in a
1139 Categories are not shown by default, but they can be turned on with the
1140 :option:`-fdiagnostics-show-category` option.
1141 When set to "``name``", the category is printed textually in the
1142 diagnostic output. When it is set to "``id``", a category number is
1143 printed. The mapping of category names to category id's can be obtained
1144 by running '``clang --print-diagnostic-categories``'.
1146 Controlling Diagnostics via Command Line Flags
1147 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1149 TODO: -W flags, -pedantic, etc
1151 .. _pragma_gcc_diagnostic:
1153 Controlling Diagnostics via Pragmas
1154 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1156 Clang can also control what diagnostics are enabled through the use of
1157 pragmas in the source code. This is useful for turning off specific
1158 warnings in a section of source code. Clang supports GCC's pragma for
1159 compatibility with existing source code, so ``#pragma GCC diagnostic``
1160 and ``#pragma clang diagnostic`` are synonyms for Clang. GCC will ignore
1161 ``#pragma clang diagnostic``, though.
1163 The pragma may control any warning that can be used from the command
1164 line. Warnings may be set to ignored, warning, error, or fatal. The
1165 following example code will tell Clang or GCC to ignore the ``-Wall``
1170 #pragma GCC diagnostic ignored "-Wall"
1172 Clang also allows you to push and pop the current warning state. This is
1173 particularly useful when writing a header file that will be compiled by
1174 other people, because you don't know what warning flags they build with.
1176 In the below example :option:`-Wextra-tokens` is ignored for only a single line
1177 of code, after which the diagnostics return to whatever state had previously
1183 #endif foo // warning: extra tokens at end of #endif directive
1185 #pragma GCC diagnostic push
1186 #pragma GCC diagnostic ignored "-Wextra-tokens"
1189 #endif foo // no warning
1191 #pragma GCC diagnostic pop
1193 The push and pop pragmas will save and restore the full diagnostic state
1194 of the compiler, regardless of how it was set. It should be noted that while Clang
1195 supports the GCC pragma, Clang and GCC do not support the exact same set
1196 of warnings, so even when using GCC compatible #pragmas there is no
1197 guarantee that they will have identical behaviour on both compilers.
1199 Clang also doesn't yet support GCC behavior for ``#pragma diagnostic pop``
1200 that doesn't have a corresponding ``#pragma diagnostic push``. In this case
1201 GCC pretends that there is a ``#pragma diagnostic push`` at the very beginning
1202 of the source file, so "unpaired" ``#pragma diagnostic pop`` matches that
1203 implicit push. This makes a difference for ``#pragma GCC diagnostic ignored``
1204 which are not guarded by push and pop. Refer to
1205 `GCC documentation <https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html>`_
1208 Like GCC, Clang accepts ``ignored``, ``warning``, ``error``, and ``fatal``
1209 severity levels. They can be used to change severity of a particular diagnostic
1210 for a region of source file. A notable difference from GCC is that diagnostic
1211 not enabled via command line arguments can't be enabled this way yet.
1213 Some diagnostics associated with a ``-W`` flag have the error severity by
1214 default. They can be ignored or downgraded to warnings:
1219 #pragma GCC diagnostic warning "-Wimplicit-function-declaration"
1220 int main(void) { puts(""); }
1222 In addition to controlling warnings and errors generated by the compiler, it is
1223 possible to generate custom warning and error messages through the following
1228 // The following will produce warning messages
1229 #pragma message "some diagnostic message"
1230 #pragma GCC warning "TODO: replace deprecated feature"
1232 // The following will produce an error message
1233 #pragma GCC error "Not supported"
1235 These pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor
1236 directives, except that they may also be embedded into preprocessor macros via
1237 the C99 ``_Pragma`` operator, for example:
1242 #define DEFER(M,...) M(__VA_ARGS__)
1243 #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__))))
1245 CUSTOM_ERROR("Feature not available");
1247 Controlling Diagnostics in System Headers
1248 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1250 Warnings are suppressed when they occur in system headers. By default,
1251 an included file is treated as a system header if it is found in an
1252 include path specified by ``-isystem``, but this can be overridden in
1255 The ``system_header`` pragma can be used to mark the current file as
1256 being a system header. No warnings will be produced from the location of
1257 the pragma onwards within the same file.
1262 #endif foo // warning: extra tokens at end of #endif directive
1264 #pragma clang system_header
1267 #endif foo // no warning
1269 The `--system-header-prefix=` and `--no-system-header-prefix=`
1270 command-line arguments can be used to override whether subsets of an include
1271 path are treated as system headers. When the name in a ``#include`` directive
1272 is found within a header search path and starts with a system prefix, the
1273 header is treated as a system header. The last prefix on the
1274 command-line which matches the specified header name takes precedence.
1277 .. code-block:: console
1279 $ clang -Ifoo -isystem bar --system-header-prefix=x/ \
1280 --no-system-header-prefix=x/y/
1282 Here, ``#include "x/a.h"`` is treated as including a system header, even
1283 if the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated
1284 as not including a system header, even if the header is found in
1287 A ``#include`` directive which finds a file relative to the current
1288 directory is treated as including a system header if the including file
1289 is treated as a system header.
1291 Controlling Deprecation Diagnostics in Clang-Provided C Runtime Headers
1292 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1294 Clang is responsible for providing some of the C runtime headers that cannot be
1295 provided by a platform CRT, such as implementation limits or when compiling in
1296 freestanding mode. Define the ``_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS`` macro
1297 prior to including such a C runtime header to disable the deprecation warnings.
1298 Note that the C Standard Library headers are allowed to transitively include
1299 other standard library headers (see 7.1.2p5), and so the most appropriate use
1300 of this macro is to set it within the build system using ``-D`` or before any
1301 include directives in the translation unit.
1305 #define _CLANG_DISABLE_CRT_DEPRECATION_WARNINGS
1306 #include <stdint.h> // Clang CRT deprecation warnings are disabled.
1307 #include <stdatomic.h> // Clang CRT deprecation warnings are disabled.
1309 .. _diagnostics_enable_everything:
1311 Enabling All Diagnostics
1312 ^^^^^^^^^^^^^^^^^^^^^^^^
1314 In addition to the traditional ``-W`` flags, one can enable **all** diagnostics
1315 by passing :option:`-Weverything`. This works as expected with
1316 :option:`-Werror`, and also includes the warnings from :option:`-pedantic`. Some
1317 diagnostics contradict each other, therefore, users of :option:`-Weverything`
1318 often disable many diagnostics such as `-Wno-c++98-compat` and `-Wno-c++-compat`
1319 because they contradict recent C++ standards.
1321 Since :option:`-Weverything` enables every diagnostic, we generally don't
1322 recommend using it. `-Wall` `-Wextra` are a better choice for most projects.
1323 Using :option:`-Weverything` means that updating your compiler is more difficult
1324 because you're exposed to experimental diagnostics which might be of lower
1325 quality than the default ones. If you do use :option:`-Weverything` then we
1326 advise that you address all new compiler diagnostics as they get added to Clang,
1327 either by fixing everything they find or explicitly disabling that diagnostic
1328 with its corresponding `Wno-` option.
1330 Note that when combined with :option:`-w` (which disables all warnings),
1331 disabling all warnings wins.
1333 .. _warning_suppression_mappings:
1335 Controlling Diagnostics via Suppression Mappings
1336 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1338 Warning suppression mappings enable users to suppress Clang's diagnostics at a
1339 per-file granularity. This allows enforcing diagnostics in specific parts of the
1340 project even if there are violations in some headers.
1342 .. code-block:: console
1348 $ clang --warning-suppression-mappings=mapping.txt -Wunused foo/bar.cc
1349 # This compilation won't emit any unused findings for sources under foo/
1350 # directory. But it'll still complain for all the other sources, e.g:
1352 #include "dir/include.h" // Clang flags unused declarations here.
1353 #include "foo/include.h" // but unused warnings under this source is omitted.
1354 #include "next_to_bar_cc.h" // as are unused warnings from this header file.
1355 // Further, unused warnings in the remainder of bar.cc are also omitted.
1358 See :doc:`WarningSuppressionMappings` for details about the file format and
1361 Controlling Static Analyzer Diagnostics
1362 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1364 While not strictly part of the compiler, the diagnostics from Clang's
1365 `static analyzer <https://clang-analyzer.llvm.org>`_ can also be
1366 influenced by the user via changes to the source code. See the available
1367 `annotations <analyzer/user-docs/Annotations.html>`_ and the analyzer's
1368 `FAQ page <analyzer/user-docs/FAQ.html#exclude-code>`_ for more information.
1370 .. _usersmanual-precompiled-headers:
1375 `Precompiled headers <https://en.wikipedia.org/wiki/Precompiled_header>`_
1376 are a general approach employed by many compilers to reduce compilation
1377 time. The underlying motivation of the approach is that it is common for
1378 the same (and often large) header files to be included by multiple
1379 source files. Consequently, compile times can often be greatly improved
1380 by caching some of the (redundant) work done by a compiler to process
1381 headers. Precompiled header files, which represent one of many ways to
1382 implement this optimization, are literally files that represent an
1383 on-disk cache that contains the vital information necessary to reduce
1384 some of the work needed to process a corresponding header file. While
1385 details of precompiled headers vary between compilers, precompiled
1386 headers have been shown to be highly effective at speeding up program
1387 compilation on systems with very large system headers (e.g., macOS).
1389 Generating a PCH File
1390 ^^^^^^^^^^^^^^^^^^^^^
1392 To generate a PCH file using Clang, one invokes Clang with the
1393 `-x <language>-header` option. This mirrors the interface in GCC
1394 for generating PCH files:
1396 .. code-block:: console
1398 $ gcc -x c-header test.h -o test.h.gch
1399 $ clang -x c-header test.h -o test.h.pch
1404 A PCH file can then be used as a prefix header when a ``-include-pch``
1405 option is passed to ``clang``:
1407 .. code-block:: console
1409 $ clang -include-pch test.h.pch test.c -o test
1411 The ``clang`` driver will check if the PCH file ``test.h.pch`` is
1412 available; if so, the contents of ``test.h`` (and the files it includes)
1413 will be processed from the PCH file. Otherwise, Clang will report an error.
1417 Clang does *not* automatically use PCH files for headers that are directly
1418 included within a source file or indirectly via :option:`-include`.
1421 .. code-block:: console
1423 $ clang -x c-header test.h -o test.h.pch
1426 $ clang test.c -o test
1428 In this example, ``clang`` will not automatically use the PCH file for
1429 ``test.h`` since ``test.h`` was included directly in the source file and not
1430 specified on the command line using ``-include-pch``.
1432 Relocatable PCH Files
1433 ^^^^^^^^^^^^^^^^^^^^^
1435 It is sometimes necessary to build a precompiled header from headers
1436 that are not yet in their final, installed locations. For example, one
1437 might build a precompiled header within the build tree that is then
1438 meant to be installed alongside the headers. Clang permits the creation
1439 of "relocatable" precompiled headers, which are built with a given path
1440 (into the build directory) and can later be used from an installed
1443 To build a relocatable precompiled header, place your headers into a
1444 subdirectory whose structure mimics the installed location. For example,
1445 if you want to build a precompiled header for the header ``mylib.h``
1446 that will be installed into ``/usr/include``, create a subdirectory
1447 ``build/usr/include`` and place the header ``mylib.h`` into that
1448 subdirectory. If ``mylib.h`` depends on other headers, then they can be
1449 stored within ``build/usr/include`` in a way that mimics the installed
1452 Building a relocatable precompiled header requires two additional
1453 arguments. First, pass the ``--relocatable-pch`` flag to indicate that
1454 the resulting PCH file should be relocatable. Second, pass
1455 ``-isysroot /path/to/build``, which makes all includes for your library
1456 relative to the build directory. For example:
1458 .. code-block:: console
1460 # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch
1462 When loading the relocatable PCH file, the various headers used in the
1463 PCH file are found from the system header root. For example, ``mylib.h``
1464 can be found in ``/usr/include/mylib.h``. If the headers are installed
1465 in some other system root, the ``-isysroot`` option can be used provide
1466 a different system root from which the headers will be based. For
1467 example, ``-isysroot /Developer/SDKs/MacOSX10.4u.sdk`` will look for
1468 ``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``.
1470 Relocatable precompiled headers are intended to be used in a limited
1471 number of cases where the compilation environment is tightly controlled
1472 and the precompiled header cannot be generated after headers have been
1475 .. _controlling-fp-behavior:
1477 Controlling Floating Point Behavior
1478 -----------------------------------
1480 Clang provides a number of ways to control floating point behavior, including
1481 with command line options and source pragmas. This section
1482 describes the various floating point semantic modes and the corresponding options.
1484 .. csv-table:: Floating Point Semantic Modes
1485 :header: "Mode", "Values"
1488 "ffp-exception-behavior", "{ignore, strict, maytrap}",
1489 "fenv_access", "{off, on}", "(none)"
1490 "frounding-math", "{dynamic, tonearest, downward, upward, towardzero}"
1491 "ffp-contract", "{on, off, fast, fast-honor-pragmas}"
1492 "fdenormal-fp-math", "{IEEE, PreserveSign, PositiveZero}"
1493 "fdenormal-fp-math-fp32", "{IEEE, PreserveSign, PositiveZero}"
1494 "fmath-errno", "{on, off}"
1495 "fhonor-nans", "{on, off}"
1496 "fhonor-infinities", "{on, off}"
1497 "fsigned-zeros", "{on, off}"
1498 "freciprocal-math", "{on, off}"
1499 "fallow-approximate-fns", "{on, off}"
1500 "fassociative-math", "{on, off}"
1501 "fcomplex-arithmetic", "{basic, improved, full, promoted}"
1503 This table describes the option settings that correspond to the three
1504 floating point semantic models: precise (the default), strict, and fast.
1507 .. csv-table:: Floating Point Models
1508 :header: "Mode", "Precise", "Strict", "Fast", "Aggressive"
1509 :widths: 25, 25, 25, 25, 25
1511 "except_behavior", "ignore", "strict", "ignore", "ignore"
1512 "fenv_access", "off", "on", "off", "off"
1513 "rounding_mode", "tonearest", "dynamic", "tonearest", "tonearest"
1514 "contract", "on", "off", "fast", "fast"
1515 "support_math_errno", "on", "on", "off", "off"
1516 "no_honor_nans", "off", "off", "off", "on"
1517 "no_honor_infinities", "off", "off", "off", "on"
1518 "no_signed_zeros", "off", "off", "on", "on"
1519 "allow_reciprocal", "off", "off", "on", "on"
1520 "allow_approximate_fns", "off", "off", "on", "on"
1521 "allow_reassociation", "off", "off", "on", "on"
1522 "complex_arithmetic", "full", "full", "promoted", "basic"
1524 The ``-ffp-model`` option does not modify the ``fdenormal-fp-math``
1525 setting, but it does have an impact on whether ``crtfastmath.o`` is
1526 linked. Because linking ``crtfastmath.o`` has a global effect on the
1527 program, and because the global denormal handling can be changed in
1528 other ways, the state of ``fdenormal-fp-math`` handling cannot
1529 be assumed in any function based on fp-model. See :ref:`crtfastmath.o`
1532 .. option:: -ffast-math
1534 Enable fast-math mode. This option lets the
1535 compiler make aggressive, potentially-lossy assumptions about
1536 floating-point math. These include:
1538 * Floating-point math obeys regular algebraic rules for real numbers (e.g.
1539 ``+`` and ``*`` are associative, ``x/y == x * (1/y)``, and
1540 ``(a + b) * c == a * c + b * c``),
1541 * No ``NaN`` or infinite values will be operands or results of
1542 floating-point operations,
1543 * ``+0`` and ``-0`` may be treated as interchangeable.
1545 ``-ffast-math`` also defines the ``__FAST_MATH__`` preprocessor
1546 macro. Some math libraries recognize this macro and change their behavior.
1547 With the exception of ``-ffp-contract=fast``, using any of the options
1548 below to disable any of the individual optimizations in ``-ffast-math``
1549 will cause ``__FAST_MATH__`` to no longer be set.
1550 ``-ffast-math`` enables ``-fcx-limited-range``.
1552 This option implies:
1554 * ``-fno-honor-infinities``
1556 * ``-fno-honor-nans``
1560 * ``-fno-math-errno``
1562 * ``-ffinite-math-only``
1564 * ``-fassociative-math``
1566 * ``-freciprocal-math``
1568 * ``-fno-signed-zeros``
1570 * ``-fno-trapping-math``
1572 * ``-fno-rounding-math``
1574 * ``-ffp-contract=fast``
1576 Note: ``-ffast-math`` causes ``crtfastmath.o`` to be linked with code unless
1577 ``-shared`` or ``-mno-daz-ftz`` is present. See
1578 :ref:`crtfastmath.o` for more details.
1580 .. option:: -fno-fast-math
1582 Disable fast-math mode. This options disables unsafe floating-point
1583 optimizations by preventing the compiler from making any transformations that
1584 could affect the results.
1586 This option implies:
1588 * ``-fhonor-infinities``
1592 * ``-fno-approx-func``
1594 * ``-fno-finite-math-only``
1596 * ``-fno-associative-math``
1598 * ``-fno-reciprocal-math``
1600 * ``-fsigned-zeros``
1602 * ``-ffp-contract=on``
1604 Also, this option resets following options to their target-dependent defaults.
1606 * ``-f[no-]math-errno``
1608 There is ambiguity about how ``-ffp-contract``, ``-ffast-math``,
1609 and ``-fno-fast-math`` behave when combined. To keep the value of
1610 ``-ffp-contract`` consistent, we define this set of rules:
1612 * ``-ffast-math`` sets ``ffp-contract`` to ``fast``.
1614 * ``-fno-fast-math`` sets ``-ffp-contract`` to ``on`` (``fast`` for CUDA and
1617 * If ``-ffast-math`` and ``-ffp-contract`` are both seen, but
1618 ``-ffast-math`` is not followed by ``-fno-fast-math``, ``ffp-contract``
1619 will be given the value of whichever option was last seen.
1621 * If ``-fno-fast-math`` is seen and ``-ffp-contract`` has been seen at least
1622 once, the ``ffp-contract`` will get the value of the last seen value of
1625 * If ``-fno-fast-math`` is seen and ``-ffp-contract`` has not been seen, the
1626 ``-ffp-contract`` setting is determined by the default value of
1629 Note: ``-fno-fast-math`` causes ``crtfastmath.o`` to not be linked with code
1630 unless ``-mdaz-ftz`` is present.
1632 .. option:: -fdenormal-fp-math=<value>
1634 Select which denormal numbers the code is permitted to require.
1638 * ``ieee`` - IEEE 754 denormal numbers
1639 * ``preserve-sign`` - the sign of a flushed-to-zero number is preserved in the sign of 0
1640 * ``positive-zero`` - denormals are flushed to positive zero
1642 The default value depends on the target. For most targets, defaults to
1645 .. option:: -f[no-]strict-float-cast-overflow
1647 When a floating-point value is not representable in a destination integer
1648 type, the code has undefined behavior according to the language standard.
1649 By default, Clang will not guarantee any particular result in that case.
1650 With the 'no-strict' option, Clang will saturate towards the smallest and
1651 largest representable integer values instead. NaNs will be converted to zero.
1652 Defaults to ``-fstrict-float-cast-overflow``.
1654 .. option:: -f[no-]math-errno
1656 Require math functions to indicate errors by setting errno.
1657 The default varies by ToolChain. ``-fno-math-errno`` allows optimizations
1658 that might cause standard C math functions to not set ``errno``.
1659 For example, on some systems, the math function ``sqrt`` is specified
1660 as setting ``errno`` to ``EDOM`` when the input is negative. On these
1661 systems, the compiler cannot normally optimize a call to ``sqrt`` to use
1662 inline code (e.g. the x86 ``sqrtsd`` instruction) without additional
1663 checking to ensure that ``errno`` is set appropriately.
1664 ``-fno-math-errno`` permits these transformations.
1666 On some targets, math library functions never set ``errno``, and so
1667 ``-fno-math-errno`` is the default. This includes most BSD-derived
1668 systems, including Darwin.
1670 .. option:: -f[no-]trapping-math
1672 Control floating point exception behavior. ``-fno-trapping-math`` allows optimizations that assume that floating point operations cannot generate traps such as divide-by-zero, overflow and underflow.
1674 - The option ``-ftrapping-math`` behaves identically to ``-ffp-exception-behavior=strict``.
1675 - The option ``-fno-trapping-math`` behaves identically to ``-ffp-exception-behavior=ignore``. This is the default.
1677 .. option:: -ffp-contract=<value>
1679 Specify when the compiler is permitted to form fused floating-point
1680 operations, such as fused multiply-add (FMA). Fused operations are
1681 permitted to produce more precise results than performing the same
1682 operations separately.
1684 The C standard permits intermediate floating-point results within an
1685 expression to be computed with more precision than their type would
1686 normally allow. This permits operation fusing, and Clang takes advantage
1687 of this by default. This behavior can be controlled with the ``FP_CONTRACT``
1688 and ``clang fp contract`` pragmas. Please refer to the pragma documentation
1689 for a description of how the pragmas interact with this option.
1693 * ``fast`` (fuse across statements disregarding pragmas, default for CUDA)
1694 * ``on`` (fuse in the same statement unless dictated by pragmas, default for languages other than CUDA/HIP)
1695 * ``off`` (never fuse)
1696 * ``fast-honor-pragmas`` (fuse across statements unless dictated by pragmas, default for HIP)
1698 .. option:: -f[no-]honor-infinities
1700 Allow floating-point optimizations that assume arguments and results are
1702 Defaults to ``-fhonor-infinities``.
1704 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1705 has the same effect as specifying ``-ffinite-math-only``.
1707 .. option:: -f[no-]honor-nans
1709 Allow floating-point optimizations that assume arguments and results are
1711 Defaults to ``-fhonor-nans``.
1713 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used,
1714 has the same effect as specifying ``-ffinite-math-only``.
1716 .. option:: -f[no-]approx-func
1718 Allow certain math function calls (such as ``log``, ``sqrt``, ``pow``, etc)
1719 to be replaced with an approximately equivalent set of instructions
1720 or alternative math function calls. For example, a ``pow(x, 0.25)``
1721 may be replaced with ``sqrt(sqrt(x))``, despite being an inexact result
1722 in cases where ``x`` is ``-0.0`` or ``-inf``.
1723 Defaults to ``-fno-approx-func``.
1725 .. option:: -f[no-]signed-zeros
1727 Allow optimizations that ignore the sign of floating point zeros.
1728 Defaults to ``-fsigned-zeros``.
1730 .. option:: -f[no-]associative-math
1732 Allow floating point operations to be reassociated.
1733 Defaults to ``-fno-associative-math``.
1735 .. option:: -f[no-]reciprocal-math
1737 Allow division operations to be transformed into multiplication by a
1738 reciprocal. This can be significantly faster than an ordinary division
1739 but can also have significantly less precision. Defaults to
1740 ``-fno-reciprocal-math``.
1742 .. option:: -f[no-]unsafe-math-optimizations
1744 Allow unsafe floating-point optimizations.
1745 ``-funsafe-math-optimizations`` also implies:
1748 * ``-fassociative-math``
1749 * ``-freciprocal-math``
1750 * ``-fno-signed-zeros``
1751 * ``-fno-trapping-math``
1752 * ``-ffp-contract=fast``
1754 ``-fno-unsafe-math-optimizations`` implies:
1756 * ``-fno-approx-func``
1757 * ``-fno-associative-math``
1758 * ``-fno-reciprocal-math``
1759 * ``-fsigned-zeros``
1760 * ``-ffp-contract=on``
1762 There is ambiguity about how ``-ffp-contract``,
1763 ``-funsafe-math-optimizations``, and ``-fno-unsafe-math-optimizations``
1764 behave when combined. Explanation in :option:`-fno-fast-math` also applies
1767 Defaults to ``-fno-unsafe-math-optimizations``.
1769 .. option:: -f[no-]finite-math-only
1771 Allow floating-point optimizations that assume arguments and results are
1772 not NaNs or +-Inf. ``-ffinite-math-only`` defines the
1773 ``__FINITE_MATH_ONLY__`` preprocessor macro.
1774 ``-ffinite-math-only`` implies:
1776 * ``-fno-honor-infinities``
1777 * ``-fno-honor-nans``
1779 ``-ffno-inite-math-only`` implies:
1781 * ``-fhonor-infinities``
1784 Defaults to ``-fno-finite-math-only``.
1786 .. option:: -f[no-]rounding-math
1788 Force floating-point operations to honor the dynamically-set rounding mode by default.
1790 The result of a floating-point operation often cannot be exactly represented in the result type and therefore must be rounded. IEEE 754 describes different rounding modes that control how to perform this rounding, not all of which are supported by all implementations. C provides interfaces (``fesetround`` and ``fesetenv``) for dynamically controlling the rounding mode, and while it also recommends certain conventions for changing the rounding mode, these conventions are not typically enforced in the ABI. Since the rounding mode changes the numerical result of operations, the compiler must understand something about it in order to optimize floating point operations.
1792 Note that floating-point operations performed as part of constant initialization are formally performed prior to the start of the program and are therefore not subject to the current rounding mode. This includes the initialization of global variables and local ``static`` variables. Floating-point operations in these contexts will be rounded using ``FE_TONEAREST``.
1794 - The option ``-fno-rounding-math`` allows the compiler to assume that the rounding mode is set to ``FE_TONEAREST``. This is the default.
1795 - The option ``-frounding-math`` forces the compiler to honor the dynamically-set rounding mode. This prevents optimizations which might affect results if the rounding mode changes or is different from the default; for example, it prevents floating-point operations from being reordered across most calls and prevents constant-folding when the result is not exactly representable.
1797 .. option:: -ffp-model=<value>
1799 Specify floating point behavior. ``-ffp-model`` is an umbrella
1800 option that encompasses functionality provided by other, single
1801 purpose, floating point options. Valid values are: ``precise``, ``strict``,
1802 ``fast``, and ``aggressive``.
1805 * ``precise`` Disables optimizations that are not value-safe on
1806 floating-point data, although FP contraction (FMA) is enabled
1807 (``-ffp-contract=on``). This is the default behavior. This value resets
1808 ``-fmath-errno`` to its target-dependent default.
1809 * ``strict`` Enables ``-frounding-math`` and
1810 ``-ffp-exception-behavior=strict``, and disables contractions (FMA). All
1811 of the ``-ffast-math`` enablements are disabled. Enables
1812 ``STDC FENV_ACCESS``: by default ``FENV_ACCESS`` is disabled. This option
1813 setting behaves as though ``#pragma STDC FENV_ACCESS ON`` appeared at the
1814 top of the source file.
1815 * ``fast`` Behaves identically to specifying ``-funsafe-math-optimizations``,
1816 ``-fno-math-errno`` and ``-fcomplex-arithmetic=promoted``
1817 ``ffp-contract=fast``
1818 * ``aggressive`` Behaves identically to specifying both ``-ffast-math`` and
1819 ``ffp-contract=fast``
1821 Note: If your command line specifies multiple instances
1822 of the ``-ffp-model`` option, or if your command line option specifies
1823 ``-ffp-model`` and later on the command line selects a floating point
1824 option that has the effect of negating part of the ``ffp-model`` that
1825 has been selected, then the compiler will issue a diagnostic warning
1826 that the override has occurred.
1828 .. option:: -ffp-exception-behavior=<value>
1830 Specify the floating-point exception behavior.
1832 Valid values are: ``ignore``, ``maytrap``, and ``strict``.
1833 The default value is ``ignore``. Details:
1835 * ``ignore`` The compiler assumes that the exception status flags will not be read and that floating point exceptions will be masked.
1836 * ``maytrap`` The compiler avoids transformations that may raise exceptions that would not have been raised by the original code. Constant folding performed by the compiler is exempt from this option.
1837 * ``strict`` The compiler ensures that all transformations strictly preserve the floating point exception semantics of the original code.
1839 .. option:: -ffp-eval-method=<value>
1841 Specify the floating-point evaluation method for intermediate results within
1842 a single expression of the code.
1844 Valid values are: ``source``, ``double``, and ``extended``.
1845 For 64-bit targets, the default value is ``source``. For 32-bit x86 targets
1846 however, in the case of NETBSD 6.99.26 and under, the default value is
1847 ``double``; in the case of NETBSD greater than 6.99.26, with NoSSE, the
1848 default value is ``extended``, with SSE the default value is ``source``.
1851 * ``source`` The compiler uses the floating-point type declared in the source program as the evaluation method.
1852 * ``double`` The compiler uses ``double`` as the floating-point evaluation method for all float expressions of type that is narrower than ``double``.
1853 * ``extended`` The compiler uses ``long double`` as the floating-point evaluation method for all float expressions of type that is narrower than ``long double``.
1855 .. option:: -f[no-]protect-parens
1857 This option pertains to floating-point types, complex types with
1858 floating-point components, and vectors of these types. Some arithmetic
1859 expression transformations that are mathematically correct and permissible
1860 according to the C and C++ language standards may be incorrect when dealing
1861 with floating-point types, such as reassociation and distribution. Further,
1862 the optimizer may ignore parentheses when computing arithmetic expressions
1863 in circumstances where the parenthesized and unparenthesized expression
1864 express the same mathematical value. For example (a+b)+c is the same
1865 mathematical value as a+(b+c), but the optimizer is free to evaluate the
1866 additions in any order regardless of the parentheses. When enabled, this
1867 option forces the optimizer to honor the order of operations with respect
1868 to parentheses in all circumstances.
1869 Defaults to ``-fno-protect-parens``.
1871 Note that floating-point contraction (option `-ffp-contract=`) is disabled
1872 when `-fprotect-parens` is enabled. Also note that in safe floating-point
1873 modes, such as `-ffp-model=precise` or `-ffp-model=strict`, this option
1874 has no effect because the optimizer is prohibited from making unsafe
1877 .. option:: -fexcess-precision:
1879 The C and C++ standards allow floating-point expressions to be computed as if
1880 intermediate results had more precision (and/or a wider range) than the type
1881 of the expression strictly allows. This is called excess precision
1883 Excess precision arithmetic can improve the accuracy of results (although not
1884 always), and it can make computation significantly faster if the target lacks
1885 direct hardware support for arithmetic in a particular type. However, it can
1886 also undermine strict floating-point reproducibility.
1888 Under the standards, assignments and explicit casts force the operand to be
1889 converted to its formal type, discarding any excess precision. Because data
1890 can only flow between statements via an assignment, this means that the use
1891 of excess precision arithmetic is a reliable local property of a single
1892 statement, and results do not change based on optimization. However, when
1893 excess precision arithmetic is in use, Clang does not guarantee strict
1894 reproducibility, and future compiler releases may recognize more
1895 opportunities to use excess precision arithmetic, e.g. with floating-point
1898 Clang does not use excess precision arithmetic for most types or on most
1899 targets. For example, even on pre-SSE X86 targets where ``float`` and
1900 ``double`` computations must be performed in the 80-bit X87 format, Clang
1901 rounds all intermediate results correctly for their type. Clang currently
1902 uses excess precision arithmetic by default only for the following types and
1905 * ``_Float16`` on X86 targets without ``AVX512-FP16``.
1907 The ``-fexcess-precision=<value>`` option can be used to control the use of
1908 excess precision arithmetic. Valid values are:
1910 * ``standard`` - The default. Allow the use of excess precision arithmetic
1911 under the constraints of the C and C++ standards. Has no effect except on
1912 the types and targets listed above.
1913 * ``fast`` - Accepted for GCC compatibility, but currently treated as an
1914 alias for ``standard``.
1915 * ``16`` - Forces ``_Float16`` operations to be emitted without using excess
1916 precision arithmetic.
1918 .. option:: -fcomplex-arithmetic=<value>:
1920 This option specifies the implementation for complex multiplication and division.
1922 Valid values are: ``basic``, ``improved``, ``full`` and ``promoted``.
1924 * ``basic`` Implementation of complex division and multiplication using
1925 algebraic formulas at source precision. No special handling to avoid
1926 overflow. NaN and infinite values are not handled.
1927 * ``improved`` Implementation of complex division using the Smith algorithm
1928 at source precision. Smith's algorithm for complex division.
1929 See SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
1930 This value offers improved handling for overflow in intermediate
1931 calculations, but overflow may occur. NaN and infinite values are not
1932 handled in some cases.
1933 * ``full`` Implementation of complex division and multiplication using a
1934 call to runtime library functions (generally the case, but the BE might
1935 sometimes replace the library call if it knows enough about the potential
1936 range of the inputs). Overflow and non-finite values are handled by the
1937 library implementation. For the case of multiplication overflow will occur in
1938 accordance with normal floating-point rules. This is the default value.
1939 * ``promoted`` Implementation of complex division using algebraic formulas at
1940 higher precision. Overflow is handled. Non-finite values are handled in some
1941 cases. If the target does not have native support for a higher precision
1942 data type, the implementation for the complex operation using the Smith
1943 algorithm will be used. Overflow may still occur in some cases. NaN and
1944 infinite values are not handled.
1946 .. option:: -fcx-limited-range:
1948 This option is aliased to ``-fcomplex-arithmetic=basic``. It enables the
1949 naive mathematical formulas for complex division and multiplication with no
1950 NaN checking of results. The default is ``-fno-cx-limited-range`` aliased to
1951 ``-fcomplex-arithmetic=full``. This option is enabled by the ``-ffast-math``
1954 .. option:: -fcx-fortran-rules:
1956 This option is aliased to ``-fcomplex-arithmetic=improved``. It enables the
1957 naive mathematical formulas for complex multiplication and enables application
1958 of Smith's algorithm for complex division. See SMITH, R. L. Algorithm 116:
1959 Complex division. Commun. ACM 5, 8 (1962).
1960 The default is ``-fno-cx-fortran-rules`` aliased to
1961 ``-fcomplex-arithmetic=full``.
1963 .. _floating-point-environment:
1965 Accessing the floating point environment
1966 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1967 Many targets allow floating point operations to be configured to control things
1968 such as how inexact results should be rounded and how exceptional conditions
1969 should be handled. This configuration is called the floating point environment.
1970 C and C++ restrict access to the floating point environment by default, and the
1971 compiler is allowed to assume that all operations are performed in the default
1972 environment. When code is compiled in this default mode, operations that depend
1973 on the environment (such as floating-point arithmetic and `FLT_ROUNDS`) may have
1974 undefined behavior if the dynamic environment is not the default environment; for
1975 example, `FLT_ROUNDS` may or may not simply return its default value for the target
1976 instead of reading the dynamic environment, and floating-point operations may be
1977 optimized as if the dynamic environment were the default. Similarly, it is undefined
1978 behavior to change the floating point environment in this default mode, for example
1979 by calling the `fesetround` function.
1980 C provides two pragmas to allow code to dynamically modify the floating point environment:
1982 - ``#pragma STDC FENV_ACCESS ON`` allows dynamic changes to the entire floating
1985 - ``#pragma STDC FENV_ROUND FE_DYNAMIC`` allows dynamic changes to just the floating
1986 point rounding mode. This may be more optimizable than ``FENV_ACCESS ON`` because
1987 the compiler can still ignore the possibility of floating-point exceptions by default.
1989 Both of these can be used either at the start of a block scope, in which case
1990 they cover all code in that scope (unless they're turned off in a child scope),
1991 or at the top level in a file, in which case they cover all subsequent function
1992 bodies until they're turned off. Note that it is undefined behavior to enter
1993 code that is *not* covered by one of these pragmas from code that *is* covered
1994 by one of these pragmas unless the floating point environment has been restored
1995 to its default state. See the C standard for more information about these pragmas.
1997 The command line option ``-frounding-math`` behaves as if the translation unit
1998 began with ``#pragma STDC FENV_ROUND FE_DYNAMIC``. The command line option
1999 ``-ffp-model=strict`` behaves as if the translation unit began with ``#pragma STDC FENV_ACCESS ON``.
2001 Code that just wants to use a specific rounding mode for specific floating point
2002 operations can avoid most of the hazards of the dynamic floating point environment
2003 by using ``#pragma STDC FENV_ROUND`` with a value other than ``FE_DYNAMIC``.
2007 A note about ``crtfastmath.o``
2008 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2009 ``-ffast-math`` and ``-funsafe-math-optimizations`` without the ``-shared``
2010 option cause ``crtfastmath.o`` to be
2011 automatically linked, which adds a static constructor that sets the FTZ/DAZ
2012 bits in MXCSR, affecting not only the current compilation unit but all static
2013 and shared libraries included in the program. This decision can be overridden
2014 by using either the flag ``-mdaz-ftz`` or ``-mno-daz-ftz`` to respectively
2015 link or not link ``crtfastmath.o``.
2017 .. _FLT_EVAL_METHOD:
2019 A note about ``__FLT_EVAL_METHOD__``
2020 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2021 The ``__FLT_EVAL_METHOD__`` is not defined as a traditional macro, and so it
2022 will not appear when dumping preprocessor macros. Instead, the value
2023 ``__FLT_EVAL_METHOD__`` expands to is determined at the point of expansion
2024 either from the value set by the ``-ffp-eval-method`` command line option or
2025 from the target. This is because the ``__FLT_EVAL_METHOD__`` macro
2026 cannot expand to the correct evaluation method in the presence of a ``#pragma``
2027 which alters the evaluation method. An error is issued if
2028 ``__FLT_EVAL_METHOD__`` is expanded inside a scope modified by
2029 ``#pragma clang fp eval_method``.
2031 .. _fp-constant-eval:
2033 A note about Floating Point Constant Evaluation
2034 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2036 In C, the only place floating point operations are guaranteed to be evaluated
2037 during translation is in the initializers of variables of static storage
2038 duration, which are all notionally initialized before the program begins
2039 executing (and thus before a non-default floating point environment can be
2040 entered). But C++ has many more contexts where floating point constant
2041 evaluation occurs. Specifically: for static/thread-local variables,
2042 first try evaluating the initializer in a constant context, including in the
2043 constant floating point environment (just like in C), and then, if that fails,
2044 fall back to emitting runtime code to perform the initialization (which might
2045 in general be in a different floating point environment).
2047 Consider this example when compiled with ``-frounding-math``
2049 .. code-block:: console
2051 constexpr float func_01(float x, float y) {
2054 float V1 = func_01(1.0F, 0x0.000001p0F);
2056 The C++ rule is that initializers for static storage duration variables are
2057 first evaluated during translation (therefore, in the default rounding mode),
2058 and only evaluated at runtime (and therefore in the runtime rounding mode) if
2059 the compile-time evaluation fails. This is in line with the C rules;
2060 C11 F.8.5 says: *All computation for automatic initialization is done (as if)
2061 at execution time; thus, it is affected by any operative modes and raises
2062 floating-point exceptions as required by IEC 60559 (provided the state for the
2063 FENV_ACCESS pragma is ‘‘on’’). All computation for initialization of objects
2064 that have static or thread storage duration is done (as if) at translation
2065 time.* C++ generalizes this by adding another phase of initialization
2066 (at runtime) if the translation-time initialization fails, but the
2067 translation-time evaluation of the initializer of succeeds, it will be
2068 treated as a constant initializer.
2071 .. _controlling-code-generation:
2073 Controlling Code Generation
2074 ---------------------------
2076 Clang provides a number of ways to control code generation. The options
2079 .. option:: -f[no-]sanitize=check1,check2,...
2081 Turn on runtime checks for various forms of undefined or suspicious
2084 This option controls whether Clang adds runtime checks for various
2085 forms of undefined or suspicious behavior, and is disabled by
2086 default. If a check fails, a diagnostic message is produced at
2087 runtime explaining the problem. The main checks are:
2089 - .. _opt_fsanitize_address:
2091 ``-fsanitize=address``:
2092 :doc:`AddressSanitizer`, a memory error
2094 - .. _opt_fsanitize_thread:
2096 ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector.
2097 - .. _opt_fsanitize_memory:
2099 ``-fsanitize=memory``: :doc:`MemorySanitizer`,
2100 a detector of uninitialized reads. Requires instrumentation of all
2102 - .. _opt_fsanitize_undefined:
2104 ``-fsanitize=undefined``: :doc:`UndefinedBehaviorSanitizer`,
2105 a fast and compatible undefined behavior checker.
2106 - .. _opt_fsanitize_type:
2108 ``-fsanitize=type``: :doc:`TypeSanitizer`, a detector for strict
2109 aliasing violations.
2110 - ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data
2112 - ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>`
2113 checks. Requires ``-flto``.
2114 - ``-fsanitize=kcfi``: kernel indirect call forward-edge control flow
2116 - ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>`
2117 protection against stack-based memory corruption errors.
2118 - ``-fsanitize=realtime``: :doc:`RealtimeSanitizer`,
2119 a real-time safety checker.
2121 There are more fine-grained checks available: see
2122 the :ref:`list <ubsan-checks>` of specific kinds of
2123 undefined behavior that can be detected and the :ref:`list <cfi-schemes>`
2124 of control flow integrity schemes.
2126 The ``-fsanitize=`` argument must also be provided when linking, in
2127 order to link to the appropriate runtime library.
2129 It is not possible to combine more than one of the ``-fsanitize=address``,
2130 ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same
2133 .. option:: -f[no-]sanitize-recover=check1,check2,...
2135 .. option:: -f[no-]sanitize-recover[=all]
2137 Controls which checks enabled by ``-fsanitize=`` flag are non-fatal.
2138 If the check is fatal, program will halt after the first error
2139 of this kind is detected and error report is printed.
2141 By default, non-fatal checks are those enabled by
2142 :doc:`UndefinedBehaviorSanitizer`,
2143 except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some
2144 sanitizers may not support recovery (or not support it by default
2145 e.g. :doc:`AddressSanitizer`), and always crash the program after the issue
2148 Note that the ``-fsanitize-trap`` flag has precedence over this flag.
2149 This means that if a check has been configured to trap elsewhere on the
2150 command line, or if the check traps by default, this flag will not have
2151 any effect unless that sanitizer's trapping behavior is disabled with
2152 ``-fno-sanitize-trap``.
2154 For example, if a command line contains the flags ``-fsanitize=undefined
2155 -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment``
2156 will have no effect on its own; it will need to be accompanied by
2157 ``-fno-sanitize-trap=alignment``.
2159 .. option:: -f[no-]sanitize-trap=check1,check2,...
2161 .. option:: -f[no-]sanitize-trap[=all]
2163 Controls which checks enabled by the ``-fsanitize=`` flag trap. This
2164 option is intended for use in cases where the sanitizer runtime cannot
2165 be used (for instance, when building libc or a kernel module), or where
2166 the binary size increase caused by the sanitizer runtime is a concern.
2168 This flag is only compatible with :doc:`control flow integrity
2169 <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer`
2170 checks other than ``vptr``.
2172 This flag is enabled by default for sanitizers in the ``cfi`` group.
2174 .. option:: -fsanitize-ignorelist=/path/to/ignorelist/file
2176 Disable or modify sanitizer checks for objects (source files, functions,
2177 variables, types) listed in the file. See
2178 :doc:`SanitizerSpecialCaseList` for file format description.
2180 .. option:: -fno-sanitize-ignorelist
2182 Don't use ignorelist file, if it was specified earlier in the command line.
2184 .. option:: -f[no-]sanitize-coverage=[type,features,...]
2186 Enable simple code coverage in addition to certain sanitizers.
2187 See :doc:`SanitizerCoverage` for more details.
2189 .. option:: -f[no-]sanitize-address-outline-instrumentation
2191 Controls how address sanitizer code is generated. If enabled will always use
2192 a function call instead of inlining the code. Turning this option on could
2193 reduce the binary size, but might result in a worse run-time performance.
2195 See :doc: `AddressSanitizer` for more details.
2197 .. option:: -f[no-]sanitize-stats
2199 Enable simple statistics gathering for the enabled sanitizers.
2200 See :doc:`SanitizerStats` for more details.
2202 .. option:: -fsanitize-undefined-trap-on-error
2204 Deprecated alias for ``-fsanitize-trap=undefined``.
2206 .. option:: -fsanitize-cfi-cross-dso
2208 Enable cross-DSO control flow integrity checks. This flag modifies
2209 the behavior of sanitizers in the ``cfi`` group to allow checking
2210 of cross-DSO virtual and indirect calls.
2212 .. option:: -fsanitize-cfi-icall-generalize-pointers
2214 Generalize pointers in return and argument types in function type signatures
2215 checked by Control Flow Integrity indirect call checking. See
2216 :doc:`ControlFlowIntegrity` for more details.
2218 .. option:: -fsanitize-cfi-icall-experimental-normalize-integers
2220 Normalize integers in return and argument types in function type signatures
2221 checked by Control Flow Integrity indirect call checking. See
2222 :doc:`ControlFlowIntegrity` for more details.
2224 This option is currently experimental.
2226 .. option:: -fstrict-vtable-pointers
2228 Enable optimizations based on the strict rules for overwriting polymorphic
2229 C++ objects, i.e. the vptr is invariant during an object's lifetime.
2230 This enables better devirtualization. Turned off by default, because it is
2233 .. option:: -fwhole-program-vtables
2235 Enable whole-program vtable optimizations, such as single-implementation
2236 devirtualization and virtual constant propagation, for classes with
2237 :doc:`hidden LTO visibility <LTOVisibility>`. Requires ``-flto``.
2239 .. option:: -f[no]split-lto-unit
2241 Controls splitting the :doc:`LTO unit <LTOVisibility>` into regular LTO and
2242 :doc:`ThinLTO` portions, when compiling with -flto=thin. Defaults to false
2243 unless ``-fsanitize=cfi`` or ``-fwhole-program-vtables`` are specified, in
2244 which case it defaults to true. Splitting is required with ``fsanitize=cfi``,
2245 and it is an error to disable via ``-fno-split-lto-unit``. Splitting is
2246 optional with ``-fwhole-program-vtables``, however, it enables more
2247 aggressive whole program vtable optimizations (specifically virtual constant
2250 When enabled, vtable definitions and select virtual functions are placed
2251 in the split regular LTO module, enabling more aggressive whole program
2252 vtable optimizations required for CFI and virtual constant propagation.
2253 However, this can increase the LTO link time and memory requirements over
2254 pure ThinLTO, as all split regular LTO modules are merged and LTO linked
2257 .. option:: -fforce-emit-vtables
2259 In order to improve devirtualization, forces emitting of vtables even in
2260 modules where it isn't necessary. It causes more inline virtual functions
2263 .. option:: -fno-assume-sane-operator-new
2265 Don't assume that the C++'s new operator is sane.
2267 This option tells the compiler to do not assume that C++'s global
2268 new operator will always return a pointer that does not alias any
2269 other pointer when the function returns.
2271 .. option:: -fassume-nothrow-exception-dtor
2273 Assume that an exception object' destructor will not throw, and generate
2274 less code for catch handlers. A throw expression of a type with a
2275 potentially-throwing destructor will lead to an error.
2277 By default, Clang assumes that the exception object may have a throwing
2278 destructor. For the Itanium C++ ABI, Clang generates a landing pad to
2279 destroy local variables and call ``_Unwind_Resume`` for the code
2280 ``catch (...) { ... }``. This option tells Clang that an exception object's
2281 destructor will not throw and code simplification is possible.
2283 .. option:: -ftrap-function=[name]
2285 Instruct code generator to emit a function call to the specified
2286 function name for ``__builtin_trap()``.
2288 LLVM code generator translates ``__builtin_trap()`` to a trap
2289 instruction if it is supported by the target ISA. Otherwise, the
2290 builtin is translated into a call to ``abort``. If this option is
2291 set, then the code generator will always lower the builtin to a call
2292 to the specified function regardless of whether the target ISA has a
2293 trap instruction. This option is useful for environments (e.g.
2294 deeply embedded) where a trap cannot be properly handled, or when
2295 some custom behavior is desired.
2297 .. option:: -ftls-model=[model]
2299 Select which TLS model to use.
2301 Valid values are: ``global-dynamic``, ``local-dynamic``,
2302 ``initial-exec`` and ``local-exec``. The default value is
2303 ``global-dynamic``. The compiler may use a different model if the
2304 selected model is not supported by the target, or if a more
2305 efficient model can be used. The TLS model can be overridden per
2306 variable using the ``tls_model`` attribute.
2308 .. option:: -femulated-tls
2310 Select emulated TLS model, which overrides all -ftls-model choices.
2312 In emulated TLS mode, all access to TLS variables are converted to
2313 calls to __emutls_get_address in the runtime library.
2315 .. option:: -mhwdiv=[values]
2317 Select the ARM modes (arm or thumb) that support hardware division
2320 Valid values are: ``arm``, ``thumb`` and ``arm,thumb``.
2321 This option is used to indicate which mode (arm or thumb) supports
2322 hardware division instructions. This only applies to the ARM
2325 .. option:: -m[no-]crc
2327 Enable or disable CRC instructions.
2329 This option is used to indicate whether CRC instructions are to
2330 be generated. This only applies to the ARM architecture.
2332 CRC instructions are enabled by default on ARMv8.
2334 .. option:: -mgeneral-regs-only
2336 Generate code which only uses the general purpose registers.
2338 This option restricts the generated code to use general registers
2339 only. This only applies to the AArch64 architecture.
2341 .. option:: -mcompact-branches=[values]
2343 Control the usage of compact branches for MIPSR6.
2345 Valid values are: ``never``, ``optimal`` and ``always``.
2346 The default value is ``optimal`` which generates compact branches
2347 when a delay slot cannot be filled. ``never`` disables the usage of
2348 compact branches and ``always`` generates compact branches whenever
2351 .. option:: -f[no-]max-type-align=[number]
2353 Instruct the code generator to not enforce a higher alignment than the given
2354 number (of bytes) when accessing memory via an opaque pointer or reference.
2355 This cap is ignored when directly accessing a variable or when the pointee
2356 type has an explicit “aligned” attribute.
2358 The value should usually be determined by the properties of the system allocator.
2359 Some builtin types, especially vector types, have very high natural alignments;
2360 when working with values of those types, Clang usually wants to use instructions
2361 that take advantage of that alignment. However, many system allocators do
2362 not promise to return memory that is more than 8-byte or 16-byte-aligned. Use
2363 this option to limit the alignment that the compiler can assume for an arbitrary
2364 pointer, which may point onto the heap.
2366 This option does not affect the ABI alignment of types; the layout of structs and
2367 unions and the value returned by the alignof operator remain the same.
2369 This option can be overridden on a case-by-case basis by putting an explicit
2370 “aligned” alignment on a struct, union, or typedef. For example:
2372 .. code-block:: console
2374 #include <immintrin.h>
2375 // Make an aligned typedef of the AVX-512 16-int vector type.
2376 typedef __v16si __aligned_v16si __attribute__((aligned(64)));
2378 void initialize_vector(__aligned_v16si *v) {
2379 // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the
2380 // value of -fmax-type-align.
2383 .. option:: -faddrsig, -fno-addrsig
2385 Controls whether Clang emits an address-significance table into the object
2386 file. Address-significance tables allow linkers to implement `safe ICF
2387 <https://research.google.com/pubs/archive/36912.pdf>`_ without the false
2388 positives that can result from other implementation techniques such as
2389 relocation scanning. Address-significance tables are enabled by default
2390 on ELF targets when using the integrated assembler. This flag currently
2391 only has an effect on ELF targets.
2393 .. _funique_internal_linkage_names:
2395 .. option:: -f[no]-unique-internal-linkage-names
2397 Controls whether Clang emits a unique (best-effort) symbol name for internal
2398 linkage symbols. When this option is set, compiler hashes the main source
2399 file path from the command line and appends it to all internal symbols. If a
2400 program contains multiple objects compiled with the same command-line source
2401 file path, the symbols are not guaranteed to be unique. This option is
2402 particularly useful in attributing profile information to the correct
2403 function when multiple functions with the same private linkage name exist
2406 It should be noted that this option cannot guarantee uniqueness and the
2407 following is an example where it is not unique when two modules contain
2408 symbols with the same private linkage name:
2410 .. code-block:: console
2412 $ cd $P/foo && clang -c -funique-internal-linkage-names name_conflict.c
2413 $ cd $P/bar && clang -c -funique-internal-linkage-names name_conflict.c
2414 $ cd $P && clang foo/name_conflict.o && bar/name_conflict.o
2416 .. option:: -f[no]-basic-block-address-map:
2417 Emits a ``SHT_LLVM_BB_ADDR_MAP`` section which includes address offsets for each
2418 basic block in the program, relative to the parent function address.
2421 .. option:: -fbasic-block-sections=[all, list=<arg>, none]
2423 Controls how Clang emits text sections for basic blocks. With values ``all``
2424 and ``list=<arg>``, each basic block or a subset of basic blocks can be placed
2425 in its own unique section.
2427 With the ``list=<arg>`` option, a file containing the subset of basic blocks
2428 that need to placed in unique sections can be specified. The format of the
2429 file is as follows. For example, ``list=spec.txt`` where ``spec.txt`` is the
2438 will place the machine basic block with ``id 2`` in function ``foo`` in a
2439 unique section. It will also place all basic blocks of functions ``bar``
2442 Further, section clusters can also be specified using the ``list=<arg>``
2443 option. For example, ``list=spec.txt`` where ``spec.txt`` contains:
2451 will create two unique sections for function ``foo`` with the first
2452 containing the odd numbered basic blocks and the second containing the
2453 even numbered basic blocks.
2455 Basic block sections allow the linker to reorder basic blocks and enables
2456 link-time optimizations like whole program inter-procedural basic block
2459 .. option:: -fcodegen-data-generate[=<path>]
2461 Emit the raw codegen (CG) data into custom sections in the object file.
2462 Currently, this option also combines the raw CG data from the object files
2463 into an indexed CG data file specified by the <path>, for LLD MachO only.
2464 When the <path> is not specified, `default.cgdata` is created.
2465 The CG data file combines all the outlining instances that occurred locally
2466 in each object file.
2468 .. code-block:: console
2470 $ clang -fuse-ld=lld -Oz -fcodegen-data-generate code.cc
2472 For linkers that do not yet support this feature, `llvm-cgdata` can be used
2473 manually to merge this CG data in object files.
2475 .. code-block:: console
2477 $ clang -c -fuse-ld=lld -Oz -fcodegen-data-generate code.cc
2478 $ llvm-cgdata --merge -o default.cgdata code.o
2480 .. option:: -fcodegen-data-use[=<path>]
2482 Read the codegen data from the specified path to more effectively outline
2483 functions across compilation units. When the <path> is not specified,
2484 `default.cgdata` is used. This option can create many identically outlined
2485 functions that can be optimized by the conventional linker’s identical code
2488 .. code-block:: console
2490 $ clang -fuse-ld=lld -Oz -Wl,--icf=safe -fcodegen-data-use code.cc
2492 .. _strict_aliasing:
2497 The C and C++ standards require accesses to objects in memory to use l-values of
2498 an appropriate type for the object. This is called *strict aliasing* or
2499 *type-based alias analysis*. Strict aliasing enhances a variety of powerful
2500 memory optimizations, including reordering, combining, and eliminating memory
2501 accesses. These optimizations can lead to unexpected behavior in code that
2502 violates the strict aliasing rules. For example:
2506 void advance(size_t *index, double *data) {
2507 double value = data[*index];
2508 /* Clang may assume that this store does not change the contents of `data`. */
2510 /* Clang may assume that this store does not change the contents of `index`. */
2511 data[*index] = value;
2512 /* Either of these facts may create significant optimization opportunities
2513 if Clang is able to inline this function. */
2516 Strict aliasing can be explicitly enabled with ``-fstrict-aliasing`` and
2517 disabled with ``-fno-strict-aliasing``. ``clang-cl`` defaults to
2518 ``-fno-strict-aliasing``; see . Otherwise, Clang defaults to ``-fstrict-aliasing``.
2520 C and C++ specify slightly different rules for strict aliasing. To improve
2521 language interoperability, Clang allows two types to alias if either language
2522 would permit it. This includes applying the C++ similar types rule to C,
2523 allowing ``int **`` to alias ``int const * const *``. Clang also relaxes the
2524 standard aliasing rules in the following ways:
2526 * All integer types of the same size are permitted to alias each other,
2527 including signed and unsigned types.
2528 * ``void*`` is permitted to alias any pointer type, ``void**`` is permitted to
2529 alias any pointer to pointer type, and so on.
2531 Code which violates strict aliasing has undefined behavior. A program that
2532 works in one version of Clang may not work in another because of changes to the
2533 optimizer. Clang provides a :doc:`TypeSanitizer` to help detect
2534 violations of the strict aliasing rules, but it is currently still experimental.
2535 Code that is known to violate strict aliasing should generally be built with
2536 ``-fno-strict-aliasing`` if the violation cannot be fixed.
2538 Clang supports several ways to fix a violation of strict aliasing:
2540 * L-values of the character types ``char`` and ``unsigned char`` (as well as
2541 other types, depending on the standard) are permitted to access objects of
2544 * Library functions such as ``memcpy`` and ``memset`` are specified as treating
2545 memory as characters and therefore are not limited by strict aliasing. If a
2546 value of one type must be reinterpreted as another (e.g. to read the bits of a
2547 floating-point number), use ``memcpy`` to copy the representation to an object
2548 of the destination type. This has no overhead over a direct l-value access
2549 because Clang should reliably optimize calls to these functions to use simple
2550 loads and stores when they are used with small constant sizes.
2552 * The attribute ``may_alias`` can be added to a ``typedef`` to give l-values of
2553 that type the same aliasing power as the character types.
2555 Clang makes a best effort to avoid obvious miscompilations from strict aliasing
2556 by only considering type information when it cannot prove that two accesses must
2557 refer to the same memory. However, it is not recommended that programmers
2558 intentionally rely on this instead of using one of the solutions above because
2559 it is too easy for the compiler's analysis to be blocked in surprising ways.
2561 In Clang 20, Clang strengthened its implementation of strict aliasing for
2562 accesses of pointer type. Previously, all accesses of pointer type were
2563 permitted to alias each other, but Clang now distinguishes different pointers
2564 by their pointee type, except as limited by the relaxations around qualifiers
2565 and ``void*`` described above. The previous behavior of treating all pointers as
2566 aliasing can be restored using ``-fno-pointer-tbaa``.
2568 Profile Guided Optimization
2569 ---------------------------
2571 Profile information enables better optimization. For example, knowing that a
2572 branch is taken very frequently helps the compiler make better decisions when
2573 ordering basic blocks. Knowing that a function ``foo`` is called more
2574 frequently than another function ``bar`` helps the inliner. Optimization
2575 levels ``-O2`` and above are recommended for use of profile guided optimization.
2577 Clang supports profile guided optimization with two different kinds of
2578 profiling. A sampling profiler can generate a profile with very low runtime
2579 overhead, or you can build an instrumented version of the code that collects
2580 more detailed profile information. Both kinds of profiles can provide execution
2581 counts for instructions in the code and information on branches taken and
2582 function invocation.
2584 Regardless of which kind of profiling you use, be careful to collect profiles
2585 by running your code with inputs that are representative of the typical
2586 behavior. Code that is not exercised in the profile will be optimized as if it
2587 is unimportant, and the compiler may make poor optimization choices for code
2588 that is disproportionately used while profiling.
2590 Differences Between Sampling and Instrumentation
2591 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2593 Although both techniques are used for similar purposes, there are important
2594 differences between the two:
2596 1. Profile data generated with one cannot be used by the other, and there is no
2597 conversion tool that can convert one to the other. So, a profile generated
2598 via ``-fprofile-generate`` or ``-fprofile-instr-generate`` must be used with
2599 ``-fprofile-use`` or ``-fprofile-instr-use``. Similarly, sampling profiles
2600 generated by external profilers must be converted and used with ``-fprofile-sample-use``
2601 or ``-fauto-profile``.
2603 2. Instrumentation profile data can be used for code coverage analysis and
2606 3. Sampling profiles can only be used for optimization. They cannot be used for
2607 code coverage analysis. Although it would be technically possible to use
2608 sampling profiles for code coverage, sample-based profiles are too
2609 coarse-grained for code coverage purposes; it would yield poor results.
2611 4. Sampling profiles must be generated by an external tool. The profile
2612 generated by that tool must then be converted into a format that can be read
2613 by LLVM. The section on sampling profilers describes one of the supported
2614 sampling profile formats.
2617 Using Sampling Profilers
2618 ^^^^^^^^^^^^^^^^^^^^^^^^
2620 Sampling profilers are used to collect runtime information, such as
2621 hardware counters, while your application executes. They are typically
2622 very efficient and do not incur a large runtime overhead. The
2623 sample data collected by the profiler can be used during compilation
2624 to determine what the most executed areas of the code are.
2626 Using the data from a sample profiler requires some changes in the way
2627 a program is built. Before the compiler can use profiling information,
2628 the code needs to execute under the profiler. The following is the
2629 usual build cycle when using sample profilers for optimization:
2631 1. Build the code with source line table information. You can use all the
2632 usual build flags that you always build your application with. The only
2633 requirement is that DWARF debug info including source line information is
2634 generated. This DWARF information is important for the profiler to be able
2635 to map instructions back to source line locations. The usefulness of this
2636 DWARF information can be improved with the ``-fdebug-info-for-profiling``
2637 and ``-funique-internal-linkage-names`` options.
2641 .. code-block:: console
2643 $ clang++ -O2 -gline-tables-only \
2644 -fdebug-info-for-profiling -funique-internal-linkage-names \
2647 While MSVC-style targets default to CodeView debug information, DWARF debug
2648 information is required to generate source-level LLVM profiles. Use
2649 ``-gdwarf`` to include DWARF debug information:
2651 .. code-block:: winbatch
2653 > clang-cl /O2 -gdwarf -gline-tables-only ^
2654 /clang:-fdebug-info-for-profiling /clang:-funique-internal-linkage-names ^
2655 code.cc /Fe:code /fuse-ld=lld /link /debug:dwarf
2659 :ref:`-funique-internal-linkage-names <funique_internal_linkage_names>`
2660 generates unique names based on given command-line source file paths. If
2661 your build system uses absolute source paths and these paths may change
2662 between steps 1 and 4, then the uniqued function names may change and result
2663 in unused profile data. Consider omitting this option in such cases.
2665 2. Run the executable under a sampling profiler. The specific profiler
2666 you use does not really matter, as long as its output can be converted
2667 into the format that the LLVM optimizer understands.
2669 Two such profilers are the Linux Perf profiler
2670 (https://perf.wiki.kernel.org/) and Intel's Sampling Enabling Product (SEP),
2671 available as part of `Intel VTune
2672 <https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/vtune-profiler.html>`_.
2673 While Perf is Linux-specific, SEP can be used on Linux, Windows, and FreeBSD.
2675 The LLVM tool ``llvm-profgen`` can convert output of either Perf or SEP. An
2676 external project, `AutoFDO <https://github.com/google/autofdo>`_, also
2677 provides a ``create_llvm_prof`` tool which supports Linux Perf output.
2681 .. code-block:: console
2683 $ perf record -b -e BR_INST_RETIRED.NEAR_TAKEN:uppp ./code
2685 If the event above is unavailable, ``branches:u`` is probably next-best.
2687 Note the use of the ``-b`` flag. This tells Perf to use the Last Branch
2688 Record (LBR) to record call chains. While this is not strictly required,
2689 it provides better call information, which improves the accuracy of
2694 .. code-block:: console
2696 $ sep -start -out code.tb7 -ec BR_INST_RETIRED.NEAR_TAKEN:precise=yes:pdir -lbr no_filter:usr -perf-script brstack -app ./code
2698 This produces a ``code.perf.data.script`` output which can be used with
2699 ``llvm-profgen``'s ``--perfscript`` input option.
2701 3. Convert the collected profile data to LLVM's sample profile format. This is
2702 currently supported via the `AutoFDO <https://github.com/google/autofdo>`_
2703 converter ``create_llvm_prof``. Once built and installed, you can convert
2704 the ``perf.data`` file to LLVM using the command:
2706 .. code-block:: console
2708 $ create_llvm_prof --binary=./code --out=code.prof
2710 This will read ``perf.data`` and the binary file ``./code`` and emit
2711 the profile data in ``code.prof``. Note that if you ran ``perf``
2712 without the ``-b`` flag, you need to use ``--use_lbr=false`` when
2713 calling ``create_llvm_prof``.
2715 Alternatively, the LLVM tool ``llvm-profgen`` can also be used to generate
2716 the LLVM sample profile:
2718 .. code-block:: console
2720 $ llvm-profgen --binary=./code --output=code.prof --perfdata=perf.data
2722 When using SEP the output is in the textual format corresponding to
2723 ``llvm-profgen --perfscript``. For example:
2725 .. code-block:: console
2727 $ llvm-profgen --binary=./code --output=code.prof --perfscript=code.perf.data.script
2730 4. Build the code again using the collected profile. This step feeds
2731 the profile back to the optimizers. This should result in a binary
2732 that executes faster than the original one. Note that you are not
2733 required to build the code with the exact same arguments that you
2734 used in the first step. The only requirement is that you build the code
2735 with the same debug info options and ``-fprofile-sample-use``.
2739 .. code-block:: console
2741 $ clang++ -O2 -gline-tables-only \
2742 -fdebug-info-for-profiling -funique-internal-linkage-names \
2743 -fprofile-sample-use=code.prof code.cc -o code
2747 .. code-block:: winbatch
2749 > clang-cl /O2 -gdwarf -gline-tables-only ^
2750 /clang:-fdebug-info-for-profiling /clang:-funique-internal-linkage-names ^
2751 -fprofile-sample-use=code.prof code.cc /Fe:code -fuse-ld=lld /link /debug:dwarf
2753 [OPTIONAL] Sampling-based profiles can have inaccuracies or missing block/
2754 edge counters. The profile inference algorithm (profi) can be used to infer
2755 missing blocks and edge counts, and improve the quality of profile data.
2756 Enable it with ``-fsample-profile-use-profi``. For example, on Linux:
2758 .. code-block:: console
2760 $ clang++ -fsample-profile-use-profi -O2 -gline-tables-only \
2761 -fdebug-info-for-profiling -funique-internal-linkage-names \
2762 -fprofile-sample-use=code.prof code.cc -o code
2766 .. code-block:: winbatch
2768 > clang-cl /clang:-fsample-profile-use-profi /O2 -gdwarf -gline-tables-only ^
2769 /clang:-fdebug-info-for-profiling /clang:-funique-internal-linkage-names ^
2770 -fprofile-sample-use=code.prof code.cc /Fe:code -fuse-ld=lld /link /debug:dwarf
2772 Sample Profile Formats
2773 """"""""""""""""""""""
2775 Since external profilers generate profile data in a variety of custom formats,
2776 the data generated by the profiler must be converted into a format that can be
2777 read by the backend. LLVM supports three different sample profile formats:
2779 1. ASCII text. This is the easiest one to generate. The file is divided into
2780 sections, which correspond to each of the functions with profile
2781 information. The format is described below. It can also be generated from
2782 the binary or gcov formats using the ``llvm-profdata`` tool.
2784 2. Binary encoding. This uses a more efficient encoding that yields smaller
2785 profile files. This is the format generated by the ``create_llvm_prof`` tool
2786 in https://github.com/google/autofdo.
2788 3. GCC encoding. This is based on the gcov format, which is accepted by GCC. It
2789 is only interesting in environments where GCC and Clang co-exist. This
2790 encoding is only generated by the ``create_gcov`` tool in
2791 https://github.com/google/autofdo. It can be read by LLVM and
2792 ``llvm-profdata``, but it cannot be generated by either.
2794 If you are using Linux Perf to generate sampling profiles, you can use the
2795 conversion tool ``create_llvm_prof`` described in the previous section.
2796 Otherwise, you will need to write a conversion tool that converts your
2797 profiler's native format into one of these three.
2800 Sample Profile Text Format
2801 """"""""""""""""""""""""""
2803 This section describes the ASCII text format for sampling profiles. It is,
2804 arguably, the easiest one to generate. If you are interested in generating any
2805 of the other two, consult the ``ProfileData`` library in LLVM's source tree
2806 (specifically, ``include/llvm/ProfileData/SampleProfReader.h``).
2808 .. code-block:: console
2810 function1:total_samples:total_head_samples
2811 offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ]
2812 offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ]
2814 offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]
2815 offsetA[.discriminator]: fnA:num_of_total_samples
2816 offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ]
2817 offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ]
2818 offsetB[.discriminator]: fnB:num_of_total_samples
2819 offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ]
2821 This is a nested tree in which the indentation represents the nesting level
2822 of the inline stack. There are no blank lines in the file. And the spacing
2823 within a single line is fixed. Additional spaces will result in an error
2824 while reading the file.
2826 Any line starting with the '#' character is completely ignored.
2828 Inlined calls are represented with indentation. The Inline stack is a
2829 stack of source locations in which the top of the stack represents the
2830 leaf function, and the bottom of the stack represents the actual
2831 symbol to which the instruction belongs.
2833 Function names must be mangled in order for the profile loader to
2834 match them in the current translation unit. The two numbers in the
2835 function header specify how many total samples were accumulated in the
2836 function (first number), and the total number of samples accumulated
2837 in the prologue of the function (second number). This head sample
2838 count provides an indicator of how frequently the function is invoked.
2840 There are two types of lines in the function body.
2842 - Sampled line represents the profile information of a source location.
2843 ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]``
2845 - Callsite line represents the profile information of an inlined callsite.
2846 ``offsetA[.discriminator]: fnA:num_of_total_samples``
2848 Each sampled line may contain several items. Some are optional (marked
2851 a. Source line offset. This number represents the line number
2852 in the function where the sample was collected. The line number is
2853 always relative to the line where symbol of the function is
2854 defined. So, if the function has its header at line 280, the offset
2855 13 is at line 293 in the file.
2857 Note that this offset should never be a negative number. This could
2858 happen in cases like macros. The debug machinery will register the
2859 line number at the point of macro expansion. So, if the macro was
2860 expanded in a line before the start of the function, the profile
2861 converter should emit a 0 as the offset (this means that the optimizers
2862 will not be able to associate a meaningful weight to the instructions
2865 b. [OPTIONAL] Discriminator. This is used if the sampled program
2866 was compiled with DWARF discriminator support
2867 (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators).
2868 DWARF discriminators are unsigned integer values that allow the
2869 compiler to distinguish between multiple execution paths on the
2870 same source line location.
2872 For example, consider the line of code ``if (cond) foo(); else bar();``.
2873 If the predicate ``cond`` is true 80% of the time, then the edge
2874 into function ``foo`` should be considered to be taken most of the
2875 time. But both calls to ``foo`` and ``bar`` are at the same source
2876 line, so a sample count at that line is not sufficient. The
2877 compiler needs to know which part of that line is taken more
2880 This is what discriminators provide. In this case, the calls to
2881 ``foo`` and ``bar`` will be at the same line, but will have
2882 different discriminator values. This allows the compiler to correctly
2883 set edge weights into ``foo`` and ``bar``.
2885 c. Number of samples. This is an integer quantity representing the
2886 number of samples collected by the profiler at this source
2889 d. [OPTIONAL] Potential call targets and samples. If present, this
2890 line contains a call instruction. This models both direct and
2891 number of samples. For example,
2893 .. code-block:: console
2895 130: 7 foo:3 bar:2 baz:7
2897 The above means that at relative line offset 130 there is a call
2898 instruction that calls one of ``foo()``, ``bar()`` and ``baz()``,
2899 with ``baz()`` being the relatively more frequently called target.
2901 As an example, consider a program with the call chain ``main -> foo -> bar``.
2902 When built with optimizations enabled, the compiler may inline the
2903 calls to ``bar`` and ``foo`` inside ``main``. The generated profile
2904 could then be something like this:
2906 .. code-block:: console
2914 This profile indicates that there were a total of 35,504 samples
2915 collected in main. All of those were at line 1 (the call to ``foo``).
2916 Of those, 31,977 were spent inside the body of ``bar``. The last line
2917 of the profile (``2: 0``) corresponds to line 2 inside ``main``. No
2918 samples were collected there.
2922 Profiling with Instrumentation
2923 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2925 Clang also supports profiling via instrumentation. This requires building a
2926 special instrumented version of the code and has some runtime
2927 overhead during the profiling, but it provides more detailed results than a
2928 sampling profiler. It also provides reproducible results, at least to the
2929 extent that the code behaves consistently across runs.
2931 Clang supports two types of instrumentation: frontend-based and IR-based.
2932 Frontend-based instrumentation can be enabled with the option ``-fprofile-instr-generate``,
2933 and IR-based instrumentation can be enabled with the option ``-fprofile-generate``.
2934 For best performance with PGO, IR-based instrumentation should be used. It has
2935 the benefits of lower instrumentation overhead, smaller raw profile size, and
2936 better runtime performance. Frontend-based instrumentation, on the other hand,
2937 has better source correlation, so it should be used with source line-based
2940 The flag ``-fcs-profile-generate`` also instruments programs using the same
2941 instrumentation method as ``-fprofile-generate``. However, it performs a
2942 post-inline late instrumentation and can produce context-sensitive profiles.
2945 Here are the steps for using profile guided optimization with
2948 1. Build an instrumented version of the code by compiling and linking with the
2949 ``-fprofile-generate`` or ``-fprofile-instr-generate`` option.
2951 .. code-block:: console
2953 $ clang++ -O2 -fprofile-instr-generate code.cc -o code
2955 2. Run the instrumented executable with inputs that reflect the typical usage.
2956 By default, the profile data will be written to a ``default.profraw`` file
2957 in the current directory. You can override that default by using option
2958 ``-fprofile-instr-generate=`` or by setting the ``LLVM_PROFILE_FILE``
2959 environment variable to specify an alternate file. If non-default file name
2960 is specified by both the environment variable and the command line option,
2961 the environment variable takes precedence. The file name pattern specified
2962 can include different modifiers: ``%p``, ``%h``, ``%m``, ``%t``, and ``%c``.
2964 Any instance of ``%p`` in that file name will be replaced by the process
2965 ID, so that you can easily distinguish the profile output from multiple
2968 .. code-block:: console
2970 $ LLVM_PROFILE_FILE="code-%p.profraw" ./code
2972 The modifier ``%h`` can be used in scenarios where the same instrumented
2973 binary is run in multiple different host machines dumping profile data
2974 to a shared network based storage. The ``%h`` specifier will be substituted
2975 with the hostname so that profiles collected from different hosts do not
2978 While the use of ``%p`` specifier can reduce the likelihood for the profiles
2979 dumped from different processes to clobber each other, such clobbering can still
2980 happen because of the ``pid`` re-use by the OS. Another side-effect of using
2981 ``%p`` is that the storage requirement for raw profile data files is greatly
2982 increased. To avoid issues like this, the ``%m`` specifier can used in the profile
2983 name. When this specifier is used, the profiler runtime will substitute ``%m``
2984 with a unique integer identifier associated with the instrumented binary. Additionally,
2985 multiple raw profiles dumped from different processes that share a file system (can be
2986 on different hosts) will be automatically merged by the profiler runtime during the
2987 dumping. If the program links in multiple instrumented shared libraries, each library
2988 will dump the profile data into its own profile data file (with its unique integer
2989 id embedded in the profile name). Note that the merging enabled by ``%m`` is for raw
2990 profile data generated by profiler runtime. The resulting merged "raw" profile data
2991 file still needs to be converted to a different format expected by the compiler (
2994 .. code-block:: console
2996 $ LLVM_PROFILE_FILE="code-%m.profraw" ./code
2998 See `this <SourceBasedCodeCoverage.html#running-the-instrumented-program>`_ section
2999 about the ``%t``, and ``%c`` modifiers.
3001 3. Combine profiles from multiple runs and convert the "raw" profile format to
3002 the input expected by clang. Use the ``merge`` command of the
3003 ``llvm-profdata`` tool to do this.
3005 .. code-block:: console
3007 $ llvm-profdata merge -output=code.profdata code-*.profraw
3009 Note that this step is necessary even when there is only one "raw" profile,
3010 since the merge operation also changes the file format.
3012 4. Build the code again using the ``-fprofile-use`` or ``-fprofile-instr-use``
3013 option to specify the collected profile data.
3015 .. code-block:: console
3017 $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code
3019 You can repeat step 4 as often as you like without regenerating the
3020 profile. As you make changes to your code, clang may no longer be able to
3021 use the profile data. It will warn you when this happens.
3023 Note that ``-fprofile-use`` option is semantically equivalent to
3024 its GCC counterpart, it *does not* handle profile formats produced by GCC.
3025 Both ``-fprofile-use`` and ``-fprofile-instr-use`` accept profiles in the
3026 indexed format, regardeless whether it is produced by frontend or the IR pass.
3028 .. option:: -fprofile-generate[=<dirname>]
3030 The ``-fprofile-generate`` and ``-fprofile-generate=`` flags will use
3031 an alternative instrumentation method for profile generation. When
3032 given a directory name, it generates the profile file
3033 ``default_%m.profraw`` in the directory named ``dirname`` if specified.
3034 If ``dirname`` does not exist, it will be created at runtime. ``%m`` specifier
3035 will be substituted with a unique id documented in step 2 above. In other words,
3036 with ``-fprofile-generate[=<dirname>]`` option, the "raw" profile data automatic
3037 merging is turned on by default, so there will no longer any risk of profile
3038 clobbering from different running processes. For example,
3040 .. code-block:: console
3042 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
3044 When ``code`` is executed, the profile will be written to the file
3045 ``yyy/zzz/default_xxxx.profraw``.
3047 To generate the profile data file with the compiler readable format, the
3048 ``llvm-profdata`` tool can be used with the profile directory as the input:
3050 .. code-block:: console
3052 $ llvm-profdata merge -output=code.profdata yyy/zzz/
3054 If the user wants to turn off the auto-merging feature, or simply override the
3055 the profile dumping path specified at command line, the environment variable
3056 ``LLVM_PROFILE_FILE`` can still be used to override
3057 the directory and filename for the profile file at runtime.
3058 To override the path and filename at compile time, use
3059 ``-Xclang -fprofile-instrument-path=/path/to/file_pattern.profraw``.
3061 .. option:: -fcs-profile-generate[=<dirname>]
3063 The ``-fcs-profile-generate`` and ``-fcs-profile-generate=`` flags will use
3064 the same instrumentation method, and generate the same profile as in the
3065 ``-fprofile-generate`` and ``-fprofile-generate=`` flags. The difference is
3066 that the instrumentation is performed after inlining so that the resulted
3067 profile has a better context sensitive information. They cannot be used
3068 together with ``-fprofile-generate`` and ``-fprofile-generate=`` flags.
3069 They are typically used in conjunction with ``-fprofile-use`` flag.
3070 The profile generated by ``-fcs-profile-generate`` and ``-fprofile-generate``
3071 can be merged by llvm-profdata. A use example:
3073 .. code-block:: console
3075 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
3077 $ llvm-profdata merge -output=code.profdata yyy/zzz/
3079 The first few steps are the same as that in ``-fprofile-generate``
3080 compilation. Then perform a second round of instrumentation.
3082 .. code-block:: console
3084 $ clang++ -O2 -fprofile-use=code.profdata -fcs-profile-generate=sss/ttt \
3087 $ llvm-profdata merge -output=cs_code.profdata sss/ttt code.profdata
3089 The resulted ``cs_code.prodata`` combines ``code.profdata`` and the profile
3090 generated from binary ``cs_code``. Profile ``cs_code.profata`` can be used by
3091 ``-fprofile-use`` compilation.
3093 .. code-block:: console
3095 $ clang++ -O2 -fprofile-use=cs_code.profdata
3097 The above command will read both profiles to the compiler at the identical
3098 point of instrumentations.
3100 .. option:: -fprofile-use[=<pathname>]
3102 Without any other arguments, ``-fprofile-use`` behaves identically to
3103 ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a
3104 profile file, it reads from that file. If ``pathname`` is a directory name,
3105 it reads from ``pathname/default.profdata``.
3107 .. option:: -fprofile-update[=<method>]
3109 Unless ``-fsanitize=thread`` is specified, the default is ``single``, which
3110 uses non-atomic increments. The counters can be inaccurate under thread
3111 contention. ``atomic`` uses atomic increments which is accurate but has
3112 overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported
3113 by the target, or ``single`` otherwise.
3115 .. option:: -ftemporal-profile
3117 Enables the temporal profiling extension for IRPGO to improve startup time by
3118 reducing ``.text`` section page faults. To do this, we instrument function
3119 timestamps to measure when each function is called for the first time and use
3120 this data to generate a function order to improve startup.
3122 The profile is generated as normal.
3124 .. code-block:: console
3126 $ clang++ -O2 -fprofile-generate -ftemporal-profile code.cc -o code
3128 $ llvm-profdata merge -o code.profdata yyy/zzz
3130 Using the resulting profile, we can generate a function order to pass to the
3131 linker via ``--symbol-ordering-file`` for ELF or ``-order_file`` for Mach-O.
3133 .. code-block:: console
3135 $ llvm-profdata order code.profdata -o code.orderfile
3136 $ clang++ -O2 -Wl,--symbol-ordering-file=code.orderfile code.cc -o code
3138 Or the profile can be passed to LLD directly.
3140 .. code-block:: console
3142 $ clang++ -O2 -fuse-ld=lld -Wl,--irpgo-profile=code.profdata,--bp-startup-sort=function code.cc -o code
3144 For more information, please read the RFC:
3145 https://discourse.llvm.org/t/rfc-temporal-profiling-extension-for-irpgo/68068
3147 Fine Tuning Profile Collection
3148 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3150 The PGO infrastructure provides user program knobs to fine tune profile
3151 collection. Specifically, the PGO runtime provides the following functions
3152 that can be used to control the regions in the program where profiles should
3155 * ``void __llvm_profile_set_filename(const char *Name)``: changes the name of
3156 the profile file to ``Name``.
3157 * ``void __llvm_profile_reset_counters(void)``: resets all counters to zero.
3158 * ``int __llvm_profile_dump(void)``: write the profile data to disk.
3159 * ``int __llvm_orderfile_dump(void)``: write the order file to disk.
3161 For example, the following pattern can be used to skip profiling program
3162 initialization, profile two specific hot regions, and skip profiling program
3170 // Reset all profile counters to 0 to omit profile collected during
3171 // initialize()'s execution.
3172 __llvm_profile_reset_counters();
3174 // Dump the profile for hot region 1.
3175 __llvm_profile_set_filename("region1.profraw");
3176 __llvm_profile_dump();
3178 // Reset counters before proceeding to hot region 2.
3179 __llvm_profile_reset_counters();
3181 // Dump the profile for hot region 2.
3182 __llvm_profile_set_filename("region2.profraw");
3183 __llvm_profile_dump();
3185 // Since the profile has been dumped, no further profile data
3186 // will be collected beyond the above __llvm_profile_dump().
3191 These APIs' names can be introduced to user programs in two ways.
3192 They can be declared as weak symbols on platforms which support
3193 treating weak symbols as ``null`` during linking. For example, the user can
3198 __attribute__((weak)) int __llvm_profile_dump(void);
3200 // Then later in the same source file
3201 if (__llvm_profile_dump)
3202 if (__llvm_profile_dump() != 0) { ... }
3203 // The first if condition tests if the symbol is actually defined.
3204 // Profile dumping only happens if the symbol is defined. Hence,
3205 // the user program works correctly during normal (not profile-generate)
3208 Alternatively, the user program can include the header
3209 ``profile/instr_prof_interface.h``, which contains the API names. For example,
3213 #include "profile/instr_prof_interface.h"
3215 // Then later in the same source file
3216 if (__llvm_profile_dump() != 0) { ... }
3218 The user code does not need to check if the API names are defined, because
3219 these names are automatically replaced by ``(0)`` or the equivalence of noop
3220 if the ``clang`` is not compiling for profile generation.
3222 Such replacement can happen because ``clang`` adds one of two macros depending
3223 on the ``-fprofile-generate`` and the ``-fprofile-use`` flags.
3225 * ``__LLVM_INSTR_PROFILE_GENERATE``: defined when one of
3226 ``-fprofile[-instr]-generate``/``-fcs-profile-generate`` is in effect.
3227 * ``__LLVM_INSTR_PROFILE_USE``: defined when one of
3228 ``-fprofile-use``/``-fprofile-instr-use`` is in effect.
3230 The two macros can be used to provide more flexibiilty so a user program
3231 can execute code specifically intended for profile generate or profile use.
3232 For example, a user program can have special logging during profile generate:
3236 #if __LLVM_INSTR_PROFILE_GENERATE
3237 expensive_logging_of_full_program_state();
3240 The logging is automatically excluded during a normal build of the program,
3241 hence it does not impact performance during a normal execution.
3243 It is advised to use such fine tuning only in a program's cold regions. The weak
3244 symbols can introduce extra control flow (the ``if`` checks), while the macros
3245 (hence declarations they guard in ``profile/instr_prof_interface.h``)
3246 can change the control flow of the functions that use them between profile
3247 generation and profile use (which can lead to discarded counters in such
3248 functions). Using these APIs in the program's cold regions introduces less
3249 overhead and leads to more optimized code.
3251 Disabling Instrumentation
3252 ^^^^^^^^^^^^^^^^^^^^^^^^^
3254 In certain situations, it may be useful to disable profile generation or use
3255 for specific files in a build, without affecting the main compilation flags
3256 used for the other files in the project.
3258 In these cases, you can use the flag ``-fno-profile-instr-generate`` (or
3259 ``-fno-profile-generate``) to disable profile generation, and
3260 ``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use.
3262 Note that these flags should appear after the corresponding profile
3263 flags to have an effect.
3267 When none of the translation units inside a binary is instrumented, in the
3268 case of Fuchsia the profile runtime will not be linked into the binary and
3269 no profile will be produced, while on other platforms the profile runtime
3270 will be linked and profile will be produced but there will not be any
3273 Instrumenting only selected files or functions
3274 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3276 Sometimes it's useful to only instrument certain files or functions. For
3277 example in automated testing infrastructure, it may be desirable to only
3278 instrument files or functions that were modified by a patch to reduce the
3279 overhead of instrumenting a full system.
3281 This can be done using the ``-fprofile-list`` option.
3283 .. option:: -fprofile-list=<pathname>
3285 This option can be used to apply profile instrumentation only to selected
3286 files or functions. ``pathname`` should point to a file in the
3287 :doc:`SanitizerSpecialCaseList` format which selects which files and
3288 functions to instrument.
3290 .. code-block:: console
3292 $ clang++ -O2 -fprofile-instr-generate -fprofile-list=fun.list code.cc -o code
3294 The option can be specified multiple times to pass multiple files.
3296 .. code-block:: console
3298 $ clang++ -O2 -fprofile-instr-generate -fcoverage-mapping -fprofile-list=fun.list -fprofile-list=code.list code.cc -o code
3300 Supported sections are ``[clang]``, ``[llvm]``, and ``[csllvm]`` representing
3301 clang PGO, IRPGO, and CSIRPGO, respectively. Supported prefixes are ``function``
3302 and ``source``. Supported categories are ``allow``, ``skip``, and ``forbid``.
3303 ``skip`` adds the ``skipprofile`` attribute while ``forbid`` adds the
3304 ``noprofile`` attribute to the appropriate function. Use
3305 ``default:<allow|skip|forbid>`` to specify the default category.
3307 .. code-block:: console
3310 # The following cases are for clang instrumentation.
3313 # We might not want to profile functions that are inlined in many places.
3314 function:inlinedLots=skip
3316 # We want to forbid profiling where it might be dangerous.
3317 source:lib/unsafe/*.cc=forbid
3319 # Otherwise we allow profiling.
3324 An older format is also supported, but it is only able to add the
3325 ``noprofile`` attribute.
3326 To filter individual functions or entire source files use ``fun:<name>`` or
3327 ``src:<file>`` respectively. To exclude a function or a source file, use
3328 ``!fun:<name>`` or ``!src:<file>`` respectively. The format also supports
3329 wildcard expansion. The compiler generated functions are assumed to be located
3330 in the main source file. It is also possible to restrict the filter to a
3331 particular instrumentation type by using a named section.
3333 .. code-block:: none
3335 # all functions whose name starts with foo will be instrumented.
3338 # except for foo1 which will be excluded from instrumentation.
3341 # every function in path/to/foo.cc will be instrumented.
3344 # bar will be instrumented only when using backend instrumentation.
3345 # Recognized section names are clang, llvm and csllvm.
3349 When the file contains only excludes, all files and functions except for the
3350 excluded ones will be instrumented. Otherwise, only the files and functions
3351 specified will be instrumented.
3353 Instrument function groups
3354 ^^^^^^^^^^^^^^^^^^^^^^^^^^
3356 Sometimes it is desirable to minimize the size overhead of instrumented
3357 binaries. One way to do this is to partition functions into groups and only
3358 instrument functions in a specified group. This can be done using the
3359 `-fprofile-function-groups` and `-fprofile-selected-function-group` options.
3361 .. option:: -fprofile-function-groups=<N>, -fprofile-selected-function-group=<i>
3363 The following uses 3 groups
3365 .. code-block:: console
3367 $ clang++ -Oz -fprofile-generate=group_0/ -fprofile-function-groups=3 -fprofile-selected-function-group=0 code.cc -o code.0
3368 $ clang++ -Oz -fprofile-generate=group_1/ -fprofile-function-groups=3 -fprofile-selected-function-group=1 code.cc -o code.1
3369 $ clang++ -Oz -fprofile-generate=group_2/ -fprofile-function-groups=3 -fprofile-selected-function-group=2 code.cc -o code.2
3371 After collecting raw profiles from the three binaries, they can be merged into
3372 a single profile like normal.
3374 .. code-block:: console
3376 $ llvm-profdata merge -output=code.profdata group_*/*.profraw
3382 When the program is compiled after a change that affects many symbol names,
3383 pre-existing profile data may no longer match the program. For example:
3385 * switching from libstdc++ to libc++ will result in the mangled names of all
3386 functions taking standard library types to change
3387 * renaming a widely-used type in C++ will result in the mangled names of all
3388 functions that have parameters involving that type to change
3389 * moving from a 32-bit compilation to a 64-bit compilation may change the
3390 underlying type of ``size_t`` and similar types, resulting in changes to
3393 Clang allows use of a profile remapping file to specify that such differences
3394 in mangled names should be ignored when matching the profile data against the
3397 .. option:: -fprofile-remapping-file=<file>
3399 Specifies a file containing profile remapping information, that will be
3400 used to match mangled names in the profile data to mangled names in the
3403 The profile remapping file is a text file containing lines of the form
3405 .. code-block:: text
3407 fragmentkind fragment1 fragment2
3409 where ``fragmentkind`` is one of ``name``, ``type``, or ``encoding``,
3410 indicating whether the following mangled name fragments are
3411 <`name <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.name>`_>s,
3412 <`type <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.type>`_>s, or
3413 <`encoding <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.encoding>`_>s,
3415 Blank lines and lines starting with ``#`` are ignored.
3417 For convenience, built-in <substitution>s such as ``St`` and ``Ss``
3418 are accepted as <name>s (even though they technically are not <name>s).
3420 For example, to specify that ``absl::string_view`` and ``std::string_view``
3421 should be treated as equivalent when matching profile data, the following
3422 remapping file could be used:
3424 .. code-block:: text
3426 # absl::string_view is considered equivalent to std::string_view
3427 type N4absl11string_viewE St17basic_string_viewIcSt11char_traitsIcEE
3429 # std:: might be std::__1:: in libc++ or std::__cxx11:: in libstdc++
3431 name 3std St7__cxx11
3433 Matching profile data using a profile remapping file is supported on a
3434 best-effort basis. For example, information regarding indirect call targets is
3435 currently not remapped. For best results, you are encouraged to generate new
3436 profile data matching the updated program, or to remap the profile data
3437 using the ``llvm-cxxmap`` and ``llvm-profdata merge`` tools.
3441 Profile data remapping is currently only supported for C++ mangled names
3442 following the Itanium C++ ABI mangling scheme. This covers all C++ targets
3443 supported by Clang other than Windows.
3445 GCOV-based Profiling
3446 --------------------
3448 GCOV is a test coverage program, it helps to know how often a line of code
3449 is executed. When instrumenting the code with ``--coverage`` option, some
3450 counters are added for each edge linking basic blocks.
3452 At compile time, gcno files are generated containing information about
3453 blocks and edges between them. At runtime the counters are incremented and at
3454 exit the counters are dumped in gcda files.
3456 The tool ``llvm-cov gcov`` will parse gcno, gcda and source files to generate
3457 a report ``.c.gcov``.
3459 .. option:: -fprofile-filter-files=[regexes]
3461 Define a list of regexes separated by a semi-colon.
3462 If a file name matches any of the regexes then the file is instrumented.
3464 .. code-block:: console
3466 $ clang --coverage -fprofile-filter-files=".*\.c$" foo.c
3468 For example, this will only instrument files finishing with ``.c``, skipping ``.h`` files.
3470 .. option:: -fprofile-exclude-files=[regexes]
3472 Define a list of regexes separated by a semi-colon.
3473 If a file name doesn't match all the regexes then the file is instrumented.
3475 .. code-block:: console
3477 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" foo.c
3479 For example, this will instrument all the files except the ones in ``/usr/include``.
3481 If both options are used then a file is instrumented if its name matches any
3482 of the regexes from ``-fprofile-filter-list`` and doesn't match all the regexes
3483 from ``-fprofile-exclude-list``.
3485 .. code-block:: console
3487 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" \
3488 -fprofile-filter-files="^/usr/.*$"
3490 In that case ``/usr/foo/oof.h`` is instrumented since it matches the filter regex and
3491 doesn't match the exclude regex, but ``/usr/include/foo.h`` doesn't since it matches
3494 Controlling Debug Information
3495 -----------------------------
3497 Controlling Size of Debug Information
3498 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3500 Debug info kind generated by Clang can be set by one of the flags listed
3501 below. If multiple flags are present, the last one is used.
3505 Don't generate any debug info (default).
3507 .. option:: -gline-tables-only
3509 Generate line number tables only.
3511 This kind of debug info allows to obtain stack traces with function names,
3512 file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It
3513 doesn't contain any other data (e.g. description of local variables or
3514 function parameters).
3516 .. option:: -fstandalone-debug
3518 Clang supports a number of optimizations to reduce the size of debug
3519 information in the binary. They work based on the assumption that
3520 the debug type information can be spread out over multiple
3521 compilation units. Specifically, the optimizations are:
3523 - will not emit type definitions for types that are not needed by a
3524 module and could be replaced with a forward declaration.
3525 - will only emit type info for a dynamic C++ class in the module that
3526 contains the vtable for the class.
3527 - will only emit type info for a C++ class (non-trivial, non-aggregate)
3528 in the modules that contain a definition for one of its constructors.
3529 - will only emit type definitions for types that are the subject of explicit
3530 template instantiation declarations in the presence of an explicit
3531 instantiation definition for the type.
3533 The **-fstandalone-debug** option turns off these optimizations.
3534 This is useful when working with 3rd-party libraries that don't come
3535 with debug information. Note that Clang will never emit type
3536 information for types that are not referenced at all by the program.
3538 .. option:: -fno-standalone-debug
3540 On Darwin **-fstandalone-debug** is enabled by default. The
3541 **-fno-standalone-debug** option can be used to get to turn on the
3542 vtable-based optimization described above.
3546 Generate complete debug info.
3548 .. option:: -feliminate-unused-debug-types
3550 By default, Clang does not emit type information for types that are defined
3551 but not used in a program. To retain the debug info for these unused types,
3552 the negation **-fno-eliminate-unused-debug-types** can be used.
3553 This can be particulary useful on Windows, when using NATVIS files that
3554 can reference const symbols that would otherwise be stripped, even in full
3555 debug or standalone debug modes.
3557 Controlling Macro Debug Info Generation
3558 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3560 Debug info for C preprocessor macros increases the size of debug information in
3561 the binary. Macro debug info generated by Clang can be controlled by the flags
3564 .. option:: -fdebug-macro
3566 Generate debug info for preprocessor macros. This flag is discarded when
3569 .. option:: -fno-debug-macro
3571 Do not generate debug info for preprocessor macros (default).
3573 Controlling Debugger "Tuning"
3574 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3576 While Clang generally emits standard DWARF debug info (http://dwarfstd.org),
3577 different debuggers may know how to take advantage of different specific DWARF
3578 features. You can "tune" the debug info for one of several different debuggers.
3580 .. option:: -ggdb, -glldb, -gsce, -gdbx
3582 Tune the debug info for the ``gdb``, ``lldb``, Sony PlayStation\ |reg|
3583 debugger, or ``dbx``, respectively. Each of these options implies **-g**.
3584 (Therefore, if you want both **-gline-tables-only** and debugger tuning, the
3585 tuning option must come first.)
3587 Controlling LLVM IR Output
3588 --------------------------
3590 Controlling Value Names in LLVM IR
3591 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3593 Emitting value names in LLVM IR increases the size and verbosity of the IR.
3594 By default, value names are only emitted in assertion-enabled builds of Clang.
3595 However, when reading IR it can be useful to re-enable the emission of value
3596 names to improve readability.
3598 .. option:: -fdiscard-value-names
3600 Discard value names when generating LLVM IR.
3602 .. option:: -fno-discard-value-names
3604 Do not discard value names when generating LLVM IR. This option can be used
3605 to re-enable names for release builds of Clang.
3608 Comment Parsing Options
3609 -----------------------
3611 Clang parses Doxygen and non-Doxygen style documentation comments and attaches
3612 them to the appropriate declaration nodes. By default, it only parses
3613 Doxygen-style comments and ignores ordinary comments starting with ``//`` and
3616 .. option:: -Wdocumentation
3618 Emit warnings about use of documentation comments. This warning group is off
3621 This includes checking that ``\param`` commands name parameters that actually
3622 present in the function signature, checking that ``\returns`` is used only on
3623 functions that actually return a value etc.
3625 .. option:: -Wno-documentation-unknown-command
3627 Don't warn when encountering an unknown Doxygen command.
3629 .. option:: -fparse-all-comments
3631 Parse all comments as documentation comments (including ordinary comments
3632 starting with ``//`` and ``/*``).
3634 .. option:: -fcomment-block-commands=[commands]
3636 Define custom documentation commands as block commands. This allows Clang to
3637 construct the correct AST for these custom commands, and silences warnings
3638 about unknown commands. Several commands must be separated by a comma
3639 *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines
3640 custom commands ``\foo`` and ``\bar``.
3642 It is also possible to use ``-fcomment-block-commands`` several times; e.g.
3643 ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same
3651 The support for standard C in clang is feature-complete except for the
3652 C99 floating-point pragmas.
3654 Extensions supported by clang
3655 -----------------------------
3657 See :doc:`LanguageExtensions`.
3659 Differences between various standard modes
3660 ------------------------------------------
3662 clang supports the -std option, which changes what language mode clang uses.
3663 The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, gnu11, c17,
3664 gnu17, c23, gnu23, c2y, gnu2y, and various aliases for those modes. If no -std
3665 option is specified, clang defaults to gnu17 mode. Many C99 and C11 features
3666 are supported in earlier modes as a conforming extension, with a warning. Use
3667 ``-pedantic-errors`` to request an error if a feature from a later standard
3668 revision is used in an earlier mode.
3670 Differences between all ``c*`` and ``gnu*`` modes:
3672 - ``c*`` modes define "``__STRICT_ANSI__``".
3673 - Target-specific defines not prefixed by underscores, like ``linux``,
3674 are defined in ``gnu*`` modes.
3675 - Trigraphs default to being off in ``gnu*`` modes; they can be enabled
3676 by the ``-trigraphs`` option.
3677 - The parser recognizes ``asm`` and ``typeof`` as keywords in ``gnu*`` modes;
3678 the variants ``__asm__`` and ``__typeof__`` are recognized in all modes.
3679 - The parser recognizes ``inline`` as a keyword in ``gnu*`` mode, in
3680 addition to recognizing it in the ``*99`` and later modes for which it is
3681 part of the ISO C standard. The variant ``__inline__`` is recognized in all
3683 - The Apple "blocks" extension is recognized by default in ``gnu*`` modes
3684 on some platforms; it can be enabled in any mode with the ``-fblocks``
3687 Differences between ``*89`` and ``*94`` modes:
3689 - Digraphs are not recognized in c89 mode.
3691 Differences between ``*94`` and ``*99`` modes:
3693 - The ``*99`` modes default to implementing ``inline`` / ``__inline__``
3694 as specified in C99, while the ``*89`` modes implement the GNU version.
3695 This can be overridden for individual functions with the ``__gnu_inline__``
3697 - The scope of names defined inside a ``for``, ``if``, ``switch``, ``while``,
3698 or ``do`` statement is different. (example: ``if ((struct x {int x;}*)0) {}``.)
3699 - ``__STDC_VERSION__`` is not defined in ``*89`` modes.
3700 - ``inline`` is not recognized as a keyword in ``c89`` mode.
3701 - ``restrict`` is not recognized as a keyword in ``*89`` modes.
3702 - Commas are allowed in integer constant expressions in ``*99`` modes.
3703 - Arrays which are not lvalues are not implicitly promoted to pointers
3705 - Some warnings are different.
3707 Differences between ``*99`` and ``*11`` modes:
3709 - Warnings for use of C11 features are disabled.
3710 - ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``.
3712 Differences between ``*11`` and ``*17`` modes:
3714 - ``__STDC_VERSION__`` is defined to ``201710L`` rather than ``201112L``.
3716 Differences between ``*17`` and ``*23`` modes:
3718 - ``__STDC_VERSION__`` is defined to ``202311L`` rather than ``201710L``.
3719 - ``nullptr`` and ``nullptr_t`` are supported, only in ``*23`` mode.
3720 - ``ATOMIC_VAR_INIT`` is removed from ``*23`` mode.
3721 - ``bool``, ``true``, ``false``, ``alignas``, ``alignof``, ``static_assert``,
3722 and ``thread_local`` are now first-class keywords, only in ``*23`` mode.
3723 - ``typeof`` and ``typeof_unqual`` are supported, only ``*23`` mode.
3724 - Bit-precise integers (``_BitInt(N)``) are supported by default in ``*23``
3725 mode, and as an extension in ``*17`` and earlier modes.
3726 - ``[[]]`` attributes are supported by default in ``*23`` mode, and as an
3727 extension in ``*17`` and earlier modes.
3729 Differences between ``*23`` and ``*2y`` modes:
3731 - ``__STDC_VERSION__`` is defined to ``202400L`` rather than ``202311L``.
3733 GCC extensions not implemented yet
3734 ----------------------------------
3736 clang tries to be compatible with gcc as much as possible, but some gcc
3737 extensions are not implemented yet:
3739 - clang does not support decimal floating point types (``_Decimal32`` and
3741 - clang does not support nested functions; this is a complex feature
3742 which is infrequently used, so it is unlikely to be implemented
3743 anytime soon. In C++11 it can be emulated by assigning lambda
3744 functions to local variables, e.g:
3748 auto const local_function = [&](int parameter) {
3754 - clang only supports global register variables when the register specified
3755 is non-allocatable (e.g. the stack pointer). Support for general global
3756 register variables is unlikely to be implemented soon because it requires
3757 additional LLVM backend support.
3758 - clang does not support static initialization of flexible array
3759 members. This appears to be a rarely used extension, but could be
3760 implemented pending user demand.
3761 - clang does not support
3762 ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is
3763 used rarely, but in some potentially interesting places, like the
3764 glibc headers, so it may be implemented pending user demand. Note
3765 that because clang pretends to be like GCC 4.2, and this extension
3766 was introduced in 4.3, the glibc headers will not try to use this
3767 extension with clang at the moment.
3768 - clang does not support the gcc extension for forward-declaring
3769 function parameters; this has not shown up in any real-world code
3770 yet, though, so it might never be implemented.
3772 This is not a complete list; if you find an unsupported extension
3773 missing from this list, please send an e-mail to cfe-dev. This list
3774 currently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this
3775 list does not include bugs in mostly-implemented features; please see
3777 tracker <https://bugs.llvm.org/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_
3778 for known existing bugs (FIXME: Is there a section for bug-reporting
3779 guidelines somewhere?).
3781 Intentionally unsupported GCC extensions
3782 ----------------------------------------
3784 - clang does not support the gcc extension that allows variable-length
3785 arrays in structures. This is for a few reasons: one, it is tricky to
3786 implement, two, the extension is completely undocumented, and three,
3787 the extension appears to be rarely used. Note that clang *does*
3788 support flexible array members (arrays with a zero or unspecified
3789 size at the end of a structure).
3790 - GCC accepts many expression forms that are not valid integer constant
3791 expressions in bit-field widths, enumerator constants, case labels,
3792 and in array bounds at global scope. Clang also accepts additional
3793 expression forms in these contexts, but constructs that GCC accepts due to
3794 simplifications GCC performs while parsing, such as ``x - x`` (where ``x`` is a
3795 variable) will likely never be accepted by Clang.
3796 - clang does not support ``__builtin_apply`` and friends; this extension
3797 is extremely obscure and difficult to implement reliably.
3801 Microsoft extensions
3802 --------------------
3804 clang has support for many extensions from Microsoft Visual C++. To enable these
3805 extensions, use the ``-fms-extensions`` command-line option. This is the default
3806 for Windows targets. Clang does not implement every pragma or declspec provided
3807 by MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma
3808 comment(lib)`` are well supported.
3810 clang has a ``-fms-compatibility`` flag that makes clang accept enough
3811 invalid C++ to be able to parse most Microsoft headers. For example, it
3812 allows `unqualified lookup of dependent base class members
3813 <https://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is
3814 a common compatibility issue with clang. This flag is enabled by default
3815 for Windows targets.
3817 ``-fdelayed-template-parsing`` lets clang delay parsing of function template
3818 definitions until the end of a translation unit. This flag is enabled by
3819 default for Windows targets.
3821 For compatibility with existing code that compiles with MSVC, clang defines the
3822 ``_MSC_VER`` and ``_MSC_FULL_VER`` macros. When on Windows, these default to
3823 either the same value as the currently installed version of cl.exe, or ``1933``
3824 and ``193300000`` (respectively). The ``-fms-compatibility-version=`` flag
3825 overrides these values. It accepts a dotted version tuple, such as 19.00.23506.
3826 Changing the MSVC compatibility version makes clang behave more like that
3827 version of MSVC. For example, ``-fms-compatibility-version=19`` will enable
3828 C++14 features and define ``char16_t`` and ``char32_t`` as builtin types.
3832 C++ Language Features
3833 =====================
3835 clang fully implements all of standard C++98 except for exported
3836 templates (which were removed in C++11), all of standard C++11,
3837 C++14, and C++17, and most of C++20.
3839 See the `C++ support in Clang <https://clang.llvm.org/cxx_status.html>`_ page
3840 for detailed information on C++ feature support across Clang versions.
3842 Controlling implementation limits
3843 ---------------------------------
3845 .. option:: -fbracket-depth=N
3847 Sets the limit for nested parentheses, brackets, and braces to N. The
3850 .. option:: -fconstexpr-depth=N
3852 Sets the limit for constexpr function invocations to N. The default is 512.
3854 .. option:: -fconstexpr-steps=N
3856 Sets the limit for the number of full-expressions evaluated in a single
3857 constant expression evaluation. This also controls the maximum size
3858 of array and dynamic array allocation that can be constant evaluated.
3859 The default is 1048576.
3861 .. option:: -ftemplate-depth=N
3863 Sets the limit for recursively nested template instantiations to N. The
3866 .. option:: -foperator-arrow-depth=N
3868 Sets the limit for iterative calls to 'operator->' functions to N. The
3873 Objective-C Language Features
3874 =============================
3878 Objective-C++ Language Features
3879 ===============================
3886 Clang supports all OpenMP 4.5 directives and clauses. See :doc:`OpenMPSupport`
3887 for additional details.
3889 Use `-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with
3892 Use `-fopenmp-simd` to enable OpenMP simd features only, without linking
3893 the runtime library; for combined constructs
3894 (e.g. ``#pragma omp parallel for simd``) the non-simd directives and clauses
3895 will be ignored. This can be disabled with `-fno-openmp-simd`.
3897 Controlling implementation limits
3898 ---------------------------------
3900 .. option:: -fopenmp-use-tls
3902 Controls code generation for OpenMP threadprivate variables. In presence of
3903 this option all threadprivate variables are generated the same way as thread
3904 local variables, using TLS support. If `-fno-openmp-use-tls`
3905 is provided or target does not support TLS, code generation for threadprivate
3906 variables relies on OpenMP runtime library.
3913 Clang can be used to compile OpenCL kernels for execution on a device
3914 (e.g. GPU). It is possible to compile the kernel into a binary (e.g. for AMDGPU)
3915 that can be uploaded to run directly on a device (e.g. using
3916 `clCreateProgramWithBinary
3917 <https://www.khronos.org/registry/OpenCL/specs/opencl-1.1.pdf#111>`_) or
3918 into generic bitcode files loadable into other toolchains.
3920 Compiling to a binary using the default target from the installation can be done
3923 .. code-block:: console
3925 $ echo "kernel void k(){}" > test.cl
3928 Compiling for a specific target can be done by specifying the triple corresponding
3929 to the target, for example:
3931 .. code-block:: console
3933 $ clang --target=nvptx64-unknown-unknown test.cl
3934 $ clang --target=amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
3936 Compiling to bitcode can be done as follows:
3938 .. code-block:: console
3940 $ clang -c -emit-llvm test.cl
3942 This will produce a file `test.bc` that can be used in vendor toolchains
3943 to perform machine code generation.
3945 Note that if compiled to bitcode for generic targets such as SPIR/SPIR-V,
3946 portable IR is produced that can be used with various vendor
3947 tools as well as open source tools such as `SPIRV-LLVM Translator
3948 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_
3949 to produce SPIR-V binary. More details are provided in `the offline
3950 compilation from OpenCL kernel sources into SPIR-V using open source
3952 <https://github.com/KhronosGroup/OpenCL-Guide/blob/main/chapters/os_tooling.md>`_.
3953 From clang 14 onwards SPIR-V can be generated directly as detailed in
3954 :ref:`the SPIR-V support section <spir-v>`.
3956 Clang currently supports OpenCL C language standards up to v2.0. Clang mainly
3957 supports full profile. There is only very limited support of the embedded
3959 From clang 9 a C++ mode is available for OpenCL (see
3960 :ref:`C++ for OpenCL <cxx_for_opencl>`).
3962 OpenCL v3.0 support is complete but it remains in experimental state, see more
3963 details about the experimental features and limitations in :doc:`OpenCLSupport`
3966 OpenCL Specific Options
3967 -----------------------
3969 Most of the OpenCL build options from `the specification v2.0 section 5.8.4
3970 <https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf#200>`_ are available.
3974 .. code-block:: console
3976 $ clang -cl-std=CL2.0 -cl-single-precision-constant test.cl
3979 Many flags used for the compilation for C sources can also be passed while
3980 compiling for OpenCL, examples: ``-c``, ``-O<1-4|s>``, ``-o``, ``-emit-llvm``, etc.
3982 Some extra options are available to support special OpenCL features.
3984 .. option:: -cl-no-stdinc
3986 Allows to disable all extra types and functions that are not native to the compiler.
3987 This might reduce the compilation speed marginally but many declarations from the
3988 OpenCL standard will not be accessible. For example, the following will fail to
3991 .. code-block:: console
3993 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
3994 $ clang -cl-std=CL2.0 -cl-no-stdinc test.cl
3995 error: use of undeclared identifier 'get_enqueued_local_size'
3996 error: use of undeclared identifier 'get_local_size'
3998 More information about the standard types and functions is provided in :ref:`the
3999 section on the OpenCL Header <opencl_header>`.
4005 Enables/Disables support of OpenCL extensions and optional features. All OpenCL
4006 targets set a list of extensions that they support. Clang allows to amend this using
4007 the ``-cl-ext`` flag with a comma-separated list of extensions prefixed with
4008 ``'+'`` or ``'-'``. The syntax: ``-cl-ext=<(['-'|'+']<extension>[,])+>``, where
4009 extensions can be either one of `the OpenCL published extensions
4010 <https://www.khronos.org/registry/OpenCL>`_
4011 or any vendor extension. Alternatively, ``'all'`` can be used to enable
4012 or disable all known extensions.
4014 Example disabling double support for the 64-bit SPIR-V target:
4016 .. code-block:: console
4018 $ clang -c --target=spirv64 -cl-ext=-cl_khr_fp64 test.cl
4020 Enabling all extensions except double support in R600 AMD GPU can be done using:
4022 .. code-block:: console
4024 $ clang --target=r600 -cl-ext=-all,+cl_khr_fp16 test.cl
4026 Note that some generic targets e.g. SPIR/SPIR-V enable all extensions/features in
4032 OpenCL targets are derived from the regular Clang target classes. The OpenCL
4033 specific parts of the target representation provide address space mapping as
4034 well as a set of supported extensions.
4039 There is a set of concrete HW architectures that OpenCL can be compiled for.
4043 .. code-block:: console
4045 $ clang --target=amdgcn-amd-amdhsa -mcpu=gfx900 test.cl
4047 - For Nvidia architectures:
4049 .. code-block:: console
4051 $ clang --target=nvptx64-unknown-unknown test.cl
4057 - A SPIR-V binary can be produced for 32 or 64 bit targets.
4059 .. code-block:: console
4061 $ clang --target=spirv32 -c test.cl
4062 $ clang --target=spirv64 -c test.cl
4064 More details can be found in :ref:`the SPIR-V support section <spir-v>`.
4066 - SPIR is available as a generic target to allow portable bitcode to be produced
4067 that can be used across GPU toolchains. The implementation follows `the SPIR
4068 specification <https://www.khronos.org/spir>`_. There are two flavors
4069 available for 32 and 64 bits.
4071 .. code-block:: console
4073 $ clang --target=spir test.cl -emit-llvm -c
4074 $ clang --target=spir64 test.cl -emit-llvm -c
4076 Clang will generate SPIR v1.2 compatible IR for OpenCL versions up to 2.0 and
4077 SPIR v2.0 for OpenCL v2.0 or C++ for OpenCL.
4079 - x86 is used by some implementations that are x86 compatible and currently
4080 remains for backwards compatibility (with older implementations prior to
4081 SPIR target support). For "non-SPMD" targets which cannot spawn multiple
4082 work-items on the fly using hardware, which covers practically all non-GPU
4083 devices such as CPUs and DSPs, additional processing is needed for the kernels
4084 to support multiple work-item execution. For this, a 3rd party toolchain,
4085 such as for example `POCL <http://portablecl.org/>`_, can be used.
4087 This target does not support multiple memory segments and, therefore, the fake
4088 address space map can be added using the :ref:`-ffake-address-space-map
4089 <opencl_fake_address_space_map>` flag.
4091 All known OpenCL extensions and features are set to supported in the generic targets,
4092 however :option:`-cl-ext` flag can be used to toggle individual extensions and
4100 By default Clang will include standard headers and therefore most of OpenCL
4101 builtin functions and types are available during compilation. The
4102 default declarations of non-native compiler types and functions can be disabled
4103 by using flag :option:`-cl-no-stdinc`.
4105 The following example demonstrates that OpenCL kernel sources with various
4106 standard builtin functions can be compiled without the need for an explicit
4107 includes or compiler flags.
4109 .. code-block:: console
4111 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl
4112 $ clang -cl-std=CL2.0 test.cl
4114 More information about the default headers is provided in :doc:`OpenCLSupport`.
4119 Most of the ``cl_khr_*`` extensions to OpenCL C from `the official OpenCL
4120 registry <https://www.khronos.org/registry/OpenCL/>`_ are available and
4121 configured per target depending on the support available in the specific
4124 It is possible to alter the default extensions setting per target using
4125 ``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details).
4127 Vendor extensions can be added flexibly by declaring the list of types and
4128 functions associated with each extensions enclosed within the following
4129 compiler pragma directives:
4133 #pragma OPENCL EXTENSION the_new_extension_name : begin
4134 // declare types and functions associated with the extension here
4135 #pragma OPENCL EXTENSION the_new_extension_name : end
4137 For example, parsing the following code adds ``my_t`` type and ``my_func``
4138 function to the custom ``my_ext`` extension.
4142 #pragma OPENCL EXTENSION my_ext : begin
4147 #pragma OPENCL EXTENSION my_ext : end
4149 There is no conflict resolution for identifier clashes among extensions.
4150 It is therefore recommended that the identifiers are prefixed with a
4151 double underscore to avoid clashing with user space identifiers. Vendor
4152 extension should use reserved identifier prefix e.g. amd, arm, intel.
4154 Clang also supports language extensions documented in `The OpenCL C Language
4155 Extensions Documentation
4156 <https://github.com/KhronosGroup/Khronosdotorg/blob/main/api/opencl/assets/OpenCL_LangExt.pdf>`_.
4158 OpenCL-Specific Attributes
4159 --------------------------
4161 OpenCL support in Clang contains a set of attribute taken directly from the
4162 specification as well as additional attributes.
4164 See also :doc:`AttributeReference`.
4169 Clang supports this attribute to comply to OpenCL v2.0 conformance, but it
4170 does not have any effect on the IR. For more details reffer to the specification
4172 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_
4178 The implementation of this feature mirrors the unroll hint for C.
4179 More details on the syntax can be found in the specification
4181 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#61>`_
4186 To make sure no invalid optimizations occur for single program multiple data
4187 (SPMD) / single instruction multiple thread (SIMT) Clang provides attributes that
4188 can be used for special functions that have cross work item semantics.
4189 An example is the subgroup operations such as `intel_sub_group_shuffle
4190 <https://www.khronos.org/registry/cl/extensions/intel/cl_intel_subgroups.txt>`_
4194 // Define custom my_sub_group_shuffle(data, c)
4195 // that makes use of intel_sub_group_shuffle
4197 if (r0) r1 = computeA();
4198 // Shuffle data from r1 into r3
4199 // of threads id r2.
4200 r3 = my_sub_group_shuffle(r1, r2);
4201 if (r0) r3 = computeB();
4203 with non-SPMD semantics this is optimized to the following equivalent code:
4209 // Incorrect functionality! The data in r1
4210 // have not been computed by all threads yet.
4211 r3 = my_sub_group_shuffle(r1, r2);
4214 r3 = my_sub_group_shuffle(r1, r2);
4218 Declaring the function ``my_sub_group_shuffle`` with the convergent attribute
4223 my_sub_group_shuffle() __attribute__((convergent));
4225 Using ``convergent`` guarantees correct execution by keeping CFG equivalence
4226 wrt operations marked as ``convergent``. CFG ``G´`` is equivalent to ``G`` wrt
4227 node ``Ni`` : ``iff ∀ Nj (i≠j)`` domination and post-domination relations with
4228 respect to ``Ni`` remain the same in both ``G`` and ``G´``.
4233 ``noduplicate`` is more restrictive with respect to optimizations than
4234 ``convergent`` because a convergent function only preserves CFG equivalence.
4235 This allows some optimizations to happen as long as the control flow remains
4240 for (int i=0; i<4; i++)
4241 my_sub_group_shuffle()
4247 my_sub_group_shuffle();
4248 my_sub_group_shuffle();
4249 my_sub_group_shuffle();
4250 my_sub_group_shuffle();
4252 while using ``noduplicate`` would disallow this. Also ``noduplicate`` doesn't
4253 have the same safe semantics of CFG as ``convergent`` and can cause changes in
4254 CFG that modify semantics of the original program.
4256 ``noduplicate`` is kept for backwards compatibility only and it considered to be
4257 deprecated for future uses.
4264 Starting from clang 9 kernel code can contain C++17 features: classes, templates,
4265 function overloading, type deduction, etc. Please note that this is not an
4266 implementation of `OpenCL C++
4267 <https://www.khronos.org/registry/OpenCL/specs/2.2/pdf/OpenCL_Cxx.pdf>`_ and
4268 there is no plan to support it in clang in any new releases in the near future.
4270 Clang currently supports C++ for OpenCL 1.0 and 2021.
4271 For detailed information about this language refer to the C++ for OpenCL
4272 Programming Language Documentation available
4273 in `the latest build
4274 <https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html>`_
4275 or in `the official release
4276 <https://github.com/KhronosGroup/OpenCL-Docs/releases/tag/cxxforopencl-docrev2021.12>`_.
4278 To enable the C++ for OpenCL mode, pass one of following command line options when
4279 compiling ``.clcpp`` file:
4281 - C++ for OpenCL 1.0: ``-cl-std=clc++``, ``-cl-std=CLC++``, ``-cl-std=clc++1.0``,
4282 ``-cl-std=CLC++1.0``, ``-std=clc++``, ``-std=CLC++``, ``-std=clc++1.0`` or
4285 - C++ for OpenCL 2021: ``-cl-std=clc++2021``, ``-cl-std=CLC++2021``,
4286 ``-std=clc++2021``, ``-std=CLC++2021``.
4291 template<class T> T add( T x, T y )
4296 __kernel void test( __global float* a, __global float* b)
4298 auto index = get_global_id(0);
4299 a[index] = add(b[index], b[index+1]);
4303 .. code-block:: console
4305 clang -cl-std=clc++1.0 test.clcpp
4306 clang -cl-std=clc++ -c --target=spirv64 test.cl
4309 By default, files with ``.clcpp`` extension are compiled with the C++ for
4312 .. code-block:: console
4316 For backward compatibility files with ``.cl`` extensions can also be compiled
4317 in C++ for OpenCL mode but the desirable language mode must be activated with
4320 .. code-block:: console
4322 clang -cl-std=clc++ test.cl
4324 Support of C++ for OpenCL 2021 is currently in experimental phase, refer to
4325 :doc:`OpenCLSupport` for more details.
4327 C++ for OpenCL kernel sources can also be compiled online in drivers supporting
4328 `cl_ext_cxx_for_opencl
4329 <https://www.khronos.org/registry/OpenCL/extensions/ext/cl_ext_cxx_for_opencl.html>`_
4332 Constructing and destroying global objects
4333 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4335 Global objects with non-trivial constructors require the constructors to be run
4336 before the first kernel using the global objects is executed. Similarly global
4337 objects with non-trivial destructors require destructor invocation just after
4338 the last kernel using the program objects is executed.
4339 In OpenCL versions earlier than v2.2 there is no support for invoking global
4340 constructors. However, an easy workaround is to manually enqueue the
4341 constructor initialization kernel that has the following name scheme
4342 ``_GLOBAL__sub_I_<compiled file name>``.
4343 This kernel is only present if there are global objects with non-trivial
4344 constructors present in the compiled binary. One way to check this is by
4345 passing ``CL_PROGRAM_KERNEL_NAMES`` to ``clGetProgramInfo`` (OpenCL v2.0
4346 s5.8.7) and then checking whether any kernel name matches the naming scheme of
4347 global constructor initialization kernel above.
4349 Note that if multiple files are compiled and linked into libraries, multiple
4350 kernels that initialize global objects for multiple modules would have to be
4353 Applications are currently required to run initialization of global objects
4354 manually before running any kernels in which the objects are used.
4356 .. code-block:: console
4358 clang -cl-std=clc++ test.cl
4360 If there are any global objects to be initialized, the final binary will
4361 contain the ``_GLOBAL__sub_I_test.cl`` kernel to be enqueued.
4363 Note that the manual workaround only applies to objects declared at the
4364 program scope. There is no manual workaround for the construction of static
4365 objects with non-trivial constructors inside functions.
4367 Global destructors can not be invoked manually in the OpenCL v2.0 drivers.
4368 However, all memory used for program scope objects should be released on
4369 ``clReleaseProgram``.
4373 Limited experimental support of C++ standard libraries for OpenCL is
4374 described in :doc:`OpenCLSupport` page.
4376 .. _target_features:
4378 Target-Specific Features and Limitations
4379 ========================================
4381 CPU Architectures Features and Limitations
4382 ------------------------------------------
4387 The support for X86 (both 32-bit and 64-bit) is considered stable on
4388 Darwin (macOS), Linux, FreeBSD, and Dragonfly BSD: it has been tested
4389 to correctly compile many large C, C++, Objective-C, and Objective-C++
4392 On ``x86_64-mingw32``, passing i128(by value) is incompatible with the
4393 Microsoft x64 calling convention. You might need to tweak
4394 ``WinX86_64ABIInfo::classify()`` in lib/CodeGen/Targets/X86.cpp.
4396 For the X86 target, clang supports the `-m16` command line
4397 argument which enables 16-bit code output. This is broadly similar to
4398 using ``asm(".code16gcc")`` with the GNU toolchain. The generated code
4399 and the ABI remains 32-bit but the assembler emits instructions
4400 appropriate for a CPU running in 16-bit mode, with address-size and
4401 operand-size prefixes to enable 32-bit addressing and operations.
4403 Several micro-architecture levels as specified by the x86-64 psABI are defined.
4404 They are cumulative in the sense that features from previous levels are
4405 implicitly included in later levels.
4407 - ``-march=x86-64``: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
4408 - ``-march=x86-64-v2``: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
4409 - ``-march=x86-64-v3``: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
4410 - ``-march=x86-64-v4``: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
4412 `Intel AVX10 ISA <https://cdrdv2.intel.com/v1/dl/getContent/784267>`_ is
4413 a major new vector ISA incorporating the modern vectorization aspects of
4414 Intel AVX-512. This ISA will be supported on all future Intel processors.
4415 Users are supposed to use the new options ``-mavx10.N`` and ``-mavx10.N-512``
4416 on these processors and should not use traditional AVX512 options anymore.
4418 The ``N`` in ``-mavx10.N`` represents a continuous integer number starting
4419 from ``1``. ``-mavx10.N`` is an alias of ``-mavx10.N-256``, which means to
4420 enable all instructions within AVX10 version N at a maximum vector length of
4421 256 bits. ``-mavx10.N-512`` enables all instructions at a maximum vector
4422 length of 512 bits, which is a superset of instructions ``-mavx10.N`` enabled.
4424 Current binaries built with AVX512 features can run on Intel AVX10/512 capable
4425 processors without re-compile, but cannot run on AVX10/256 capable processors.
4426 Users need to re-compile their code with ``-mavx10.N``, and maybe update some
4427 code that calling to 512-bit X86 specific intrinsics and passing or returning
4428 512-bit vector types in function call, if they want to run on AVX10/256 capable
4429 processors. Binaries built with ``-mavx10.N`` can run on both AVX10/256 and
4430 AVX10/512 capable processors.
4432 Users can add a ``-mno-evex512`` in the command line with AVX512 options if
4433 they want to run the binary on both legacy AVX512 and new AVX10/256 capable
4434 processors. The option has the same constraints as ``-mavx10.N``, i.e.,
4435 cannot call to 512-bit X86 specific intrinsics and pass or return 512-bit vector
4436 types in function call.
4438 Users should avoid using AVX512 features in function target attributes when
4439 developing code for AVX10. If they have to do so, they need to add an explicit
4440 ``evex512`` or ``no-evex512`` together with AVX512 features for 512-bit or
4441 non-512-bit functions respectively to avoid unexpected code generation. Both
4442 command line option and target attribute of EVEX512 feature can only be used
4443 with AVX512. They don't affect vector size of AVX10.
4445 User should not mix the use AVX10 and AVX512 options together at any time,
4446 because the option combinations are conflicting sometimes. For example, a
4447 combination of ``-mavx512f -mavx10.1-256`` doesn't show a clear intention to
4448 compiler, since instructions in AVX512F and AVX10.1/256 intersect but do not
4449 overlap. In this case, compiler will emit warning for it, but the behavior
4450 is determined. It will generate the same code as option ``-mavx10.1-512``.
4451 A similar case is ``-mavx512f -mavx10.2-256``, which equals to
4452 ``-mavx10.1-512 -mavx10.2-256``, because ``avx10.2-256`` implies ``avx10.1-256``
4453 and ``-mavx512f -mavx10.1-256`` equals to ``-mavx10.1-512``.
4455 There are some new macros introduced with AVX10 support. ``-mavx10.1-256`` will
4456 enable ``__AVX10_1__`` and ``__EVEX256__``, while ``-mavx10.1-512`` enables
4457 ``__AVX10_1__``, ``__EVEX256__``, ``__EVEX512__`` and ``__AVX10_1_512__``.
4458 Besides, both ``-mavx10.1-256`` and ``-mavx10.1-512`` will enable all AVX512
4459 feature specific macros. A AVX512 feature will enable both ``__EVEX256__``,
4460 ``__EVEX512__`` and its own macro. So ``__EVEX512__`` can be used to guard code
4461 that can run on both legacy AVX512 and AVX10/512 capable processors but cannot
4462 run on AVX10/256, while a AVX512 macro like ``__AVX512F__`` cannot tell the
4463 difference among the three options. Users need to check additional macros
4464 ``__AVX10_1__`` and ``__EVEX512__`` if they want to make distinction.
4469 The support for ARM (specifically ARMv6 and ARMv7) is considered stable
4470 on Darwin (iOS): it has been tested to correctly compile many large C,
4471 C++, Objective-C, and Objective-C++ codebases. Clang only supports a
4472 limited number of ARM architectures. It does not yet fully support
4478 The support for PowerPC (especially PowerPC64) is considered stable
4479 on Linux and FreeBSD: it has been tested to correctly compile many
4480 large C and C++ codebases. PowerPC (32bit) is still missing certain
4481 features (e.g. PIC code on ELF platforms).
4486 clang currently contains some support for other architectures (e.g. Sparc);
4487 however, significant pieces of code generation are still missing, and they
4488 haven't undergone significant testing.
4490 clang contains limited support for the MSP430 embedded processor, but
4491 both the clang support and the LLVM backend support are highly
4494 Other platforms are completely unsupported at the moment. Adding the
4495 minimal support needed for parsing and semantic analysis on a new
4496 platform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source
4497 tree. This level of support is also sufficient for conversion to LLVM IR
4498 for simple programs. Proper support for conversion to LLVM IR requires
4499 adding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to
4500 change soon, though. Generating assembly requires a suitable LLVM
4503 Operating System Features and Limitations
4504 -----------------------------------------
4509 Clang has experimental support for targeting "Cygming" (Cygwin / MinGW)
4512 See also :ref:`Microsoft Extensions <c_ms>`.
4517 Clang works on Cygwin-1.7.
4522 Clang works on some mingw32 distributions. Clang assumes directories as
4525 - ``C:/mingw/include``
4527 - ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++``
4529 On MSYS, a few tests might fail.
4534 For 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang
4537 - ``GCC versions 4.5.0 to 4.5.3, 4.6.0 to 4.6.2, or 4.7.0 (for the C++ header search path)``
4538 - ``some_directory/bin/gcc.exe``
4539 - ``some_directory/bin/clang.exe``
4540 - ``some_directory/bin/clang++.exe``
4541 - ``some_directory/bin/../include/c++/GCC_version``
4542 - ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32``
4543 - ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32``
4544 - ``some_directory/bin/../include/c++/GCC_version/backward``
4545 - ``some_directory/bin/../x86_64-w64-mingw32/include``
4546 - ``some_directory/bin/../i686-w64-mingw32/include``
4547 - ``some_directory/bin/../include``
4549 This directory layout is standard for any toolchain you will find on the
4550 official `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_.
4552 Clang expects the GCC executable "gcc.exe" compiled for
4553 ``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH.
4555 `Some tests might fail <https://bugs.llvm.org/show_bug.cgi?id=9072>`_ on
4556 ``x86_64-w64-mingw32``.
4560 TOC Data Transformation
4561 """""""""""""""""""""""
4562 TOC data transformation is off by default (``-mno-tocdata``).
4563 When ``-mtocdata`` is specified, the TOC data transformation will be applied to
4564 all suitable variables with static storage duration, including static data
4565 members of classes and block-scope static variables (if not marked as exceptions,
4568 Suitable variables must:
4570 - have complete types
4571 - be independently generated (i.e., not placed in a pool)
4572 - be at most as large as a pointer
4573 - not be aligned more strictly than a pointer
4574 - not be structs containing flexible array members
4575 - not have internal linkage
4577 - not have section attributes
4578 - not be thread local storage
4580 The TOC data transformation results in the variable, not its address,
4581 being placed in the TOC. This eliminates the need to load the address of the
4582 variable from the TOC.
4585 If the TOC data transformation is applied to a variable whose definition
4586 is imported, the linker will generate fixup code for reading or writing to the
4589 When multiple toc-data options are used, the last option used has the affect.
4590 For example: -mno-tocdata=g5,g1 -mtocdata=g1,g2 -mno-tocdata=g2 -mtocdata=g3,g4
4591 results in -mtocdata=g1,g3,g4
4593 Names of variables not having external linkage will be ignored.
4597 .. option:: -mno-tocdata
4599 This is the default behaviour. Only variables explicitly specified with
4600 ``-mtocdata=`` will have the TOC data transformation applied.
4602 .. option:: -mtocdata
4604 Apply the TOC data transformation to all suitable variables with static
4605 storage duration (including static data members of classes and block-scope
4606 static variables) that are not explicitly specified with ``-mno-tocdata=``.
4608 .. option:: -mno-tocdata=
4610 Can be used in conjunction with ``-mtocdata`` to mark the comma-separated
4611 list of external linkage variables, specified using their mangled names, as
4612 exceptions to ``-mtocdata``.
4614 .. option:: -mtocdata=
4616 Apply the TOC data transformation to the comma-separated list of external
4617 linkage variables, specified using their mangled names, if they are suitable.
4618 Emit diagnostics for all unsuitable variables specified.
4620 Default Visibility Export Mapping
4621 """""""""""""""""""""""""""""""""
4622 The ``-mdefault-visibility-export-mapping=`` option can be used to control
4623 mapping of default visibility to an explicit shared object export
4624 (i.e. XCOFF exported visibility). Three values are provided for the option:
4626 * ``-mdefault-visibility-export-mapping=none``: no additional export
4627 information is created for entities with default visibility.
4628 * ``-mdefault-visibility-export-mapping=explicit``: mark entities for export
4629 if they have explicit (e.g. via an attribute) default visibility from the
4630 source, including RTTI.
4631 * ``-mdefault-visibility-export-mapping=all``: set XCOFF exported visibility
4632 for all entities with default visibility from any source. This gives a
4633 export behavior similar to ELF platforms where all entities with default
4634 visibility are exported.
4641 Clang supports generation of SPIR-V conformant to `the OpenCL Environment
4643 <https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html>`_.
4645 To generate SPIR-V binaries, Clang uses the external ``llvm-spirv`` tool from the
4646 `SPIRV-LLVM-Translator repo
4647 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_.
4649 Prior to the generation of SPIR-V binary with Clang, ``llvm-spirv``
4650 should be built or installed. Please refer to `the following instructions
4651 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator#build-instructions>`_
4652 for more details. Clang will look for ``llvm-spirv-<LLVM-major-version>`` and
4653 ``llvm-spirv`` executables, in this order, in the ``PATH`` environment variable.
4654 Clang uses ``llvm-spirv`` with `the widely adopted assembly syntax package
4655 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator/#build-with-spirv-tools>`_.
4658 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator/releases>`_ of
4659 ``llvm-spirv`` is aligned with Clang major releases. The same applies to the
4660 main development branch. It is therefore important to ensure the ``llvm-spirv``
4661 version is in alignment with the Clang version. For troubleshooting purposes
4662 ``llvm-spirv`` can be `tested in isolation
4663 <https://github.com/KhronosGroup/SPIRV-LLVM-Translator#test-instructions>`_.
4665 Example usage for OpenCL kernel compilation:
4667 .. code-block:: console
4669 $ clang --target=spirv32 -c test.cl
4670 $ clang --target=spirv64 -c test.cl
4672 Both invocations of Clang will result in the generation of a SPIR-V binary file
4673 `test.o` for 32 bit and 64 bit respectively. This file can be imported
4674 by an OpenCL driver that support SPIR-V consumption or it can be compiled
4675 further by offline SPIR-V consumer tools.
4677 Converting to SPIR-V produced with the optimization levels other than `-O0` is
4678 currently available as an experimental feature and it is not guaranteed to work
4681 Clang also supports integrated generation of SPIR-V without use of ``llvm-spirv``
4682 tool as an experimental feature when ``-fintegrated-objemitter`` flag is passed in
4685 .. code-block:: console
4687 $ clang --target=spirv32 -fintegrated-objemitter -c test.cl
4689 Note that only very basic functionality is supported at this point and therefore
4690 it is not suitable for arbitrary use cases. This feature is only enabled when clang
4691 build is configured with ``-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=SPIRV`` option.
4693 Linking is done using ``spirv-link`` from `the SPIRV-Tools project
4694 <https://github.com/KhronosGroup/SPIRV-Tools#linker>`_. Similar to other external
4695 linkers, Clang will expect ``spirv-link`` to be installed separately and to be
4696 present in the ``PATH`` environment variable. Please refer to `the build and
4697 installation instructions
4698 <https://github.com/KhronosGroup/SPIRV-Tools#build>`_.
4700 .. code-block:: console
4702 $ clang --target=spirv64 test1.cl test2.cl
4704 More information about the SPIR-V target settings and supported versions of SPIR-V
4705 format can be found in `the SPIR-V target guide
4706 <https://llvm.org/docs/SPIRVUsage.html>`__.
4713 clang-cl is an alternative command-line interface to Clang, designed for
4714 compatibility with the Visual C++ compiler, cl.exe.
4716 To enable clang-cl to find system headers, libraries, and the linker when run
4717 from the command-line, it should be executed inside a Visual Studio Native Tools
4718 Command Prompt or a regular Command Prompt where the environment has been set
4719 up using e.g. `vcvarsall.bat <https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_.
4721 clang-cl can also be used from inside Visual Studio by selecting the LLVM
4722 Platform Toolset. The toolset is not part of the installer, but may be installed
4724 `Visual Studio Marketplace <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain>`_.
4725 To use the toolset, select a project in Solution Explorer, open its Property
4726 Page (Alt+F7), and in the "General" section of "Configuration Properties"
4727 change "Platform Toolset" to LLVM. Doing so enables an additional Property
4728 Page for selecting the clang-cl executable to use for builds.
4730 To use the toolset with MSBuild directly, invoke it with e.g.
4731 ``/p:PlatformToolset=LLVM``. This allows trying out the clang-cl toolchain
4732 without modifying your project files.
4734 It's also possible to point MSBuild at clang-cl without changing toolset by
4735 passing ``/p:CLToolPath=c:\llvm\bin /p:CLToolExe=clang-cl.exe``.
4737 When using CMake and the Visual Studio generators, the toolset can be set with the ``-T`` flag:
4741 cmake -G"Visual Studio 16 2019" -T LLVM ..
4743 When using CMake with the Ninja generator, set the ``CMAKE_C_COMPILER`` and
4744 ``CMAKE_CXX_COMPILER`` variables to clang-cl:
4748 cmake -GNinja -DCMAKE_C_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe"
4749 -DCMAKE_CXX_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" ..
4752 Command-Line Options
4753 --------------------
4755 To be compatible with cl.exe, clang-cl supports most of the same command-line
4756 options. Those options can start with either ``/`` or ``-``. It also supports
4757 some of Clang's core options, such as the ``-W`` options.
4759 Options that are known to clang-cl, but not currently supported, are ignored
4760 with a warning. For example:
4764 clang-cl.exe: warning: argument unused during compilation: '/AI'
4766 To suppress warnings about unused arguments, use the ``-Qunused-arguments`` option.
4768 Options that are not known to clang-cl will be ignored by default. Use the
4769 ``-Werror=unknown-argument`` option in order to treat them as errors. If these
4770 options are spelled with a leading ``/``, they will be mistaken for a filename:
4774 clang-cl.exe: error: no such file or directory: '/foobar'
4776 Please `file a bug <https://github.com/llvm/llvm-project/issues/new?labels=clang-cl>`_
4777 for any valid cl.exe flags that clang-cl does not understand.
4779 Execute ``clang-cl /?`` to see a list of supported options:
4783 CL.EXE COMPATIBILITY OPTIONS:
4784 /? Display available options
4785 /arch:<value> Set architecture for code generation
4786 /Brepro- Emit an object file which cannot be reproduced over time
4787 /Brepro Emit an object file which can be reproduced over time
4788 /clang:<arg> Pass <arg> to the clang driver
4789 /C Don't discard comments when preprocessing
4791 /d1PP Retain macro definitions in /E mode
4792 /d1reportAllClassLayout Dump record layout information
4793 /diagnostics:caret Enable caret and column diagnostics (on by default)
4794 /diagnostics:classic Disable column and caret diagnostics
4795 /diagnostics:column Disable caret diagnostics but keep column info
4796 /D <macro[=value]> Define macro
4797 /EH<value> Exception handling model
4798 /EP Disable linemarker output and preprocess to stdout
4799 /execution-charset:<value>
4800 Runtime encoding, supports only UTF-8
4801 /E Preprocess to stdout
4802 /FA Output assembly code file during compilation
4803 /Fa<file or directory> Output assembly code to this file during compilation (with /FA)
4804 /Fe<file or directory> Set output executable file or directory (ends in / or \)
4805 /FI <value> Include file before parsing
4806 /Fi<file> Set preprocess output file name (with /P)
4807 /Fo<file or directory> Set output object file, or directory (ends in / or \) (with /c)
4813 /Fp<filename> Set pch filename (with /Yc and /Yu)
4814 /GA Assume thread-local variables are defined in the executable
4815 /Gd Set __cdecl as a default calling convention
4816 /GF- Disable string pooling
4817 /GF Enable string pooling (default)
4818 /GR- Disable emission of RTTI data
4819 /Gregcall Set __regcall as a default calling convention
4820 /GR Enable emission of RTTI data
4821 /Gr Set __fastcall as a default calling convention
4822 /GS- Disable buffer security check
4823 /GS Enable buffer security check (default)
4824 /Gs Use stack probes (default)
4825 /Gs<value> Set stack probe size (default 4096)
4826 /guard:<value> Enable Control Flow Guard with /guard:cf,
4827 or only the table with /guard:cf,nochecks.
4828 Enable EH Continuation Guard with /guard:ehcont
4829 /Gv Set __vectorcall as a default calling convention
4830 /Gw- Don't put each data item in its own section
4831 /Gw Put each data item in its own section
4832 /GX- Disable exception handling
4833 /GX Enable exception handling
4834 /Gy- Don't put each function in its own section (default)
4835 /Gy Put each function in its own section
4836 /Gz Set __stdcall as a default calling convention
4837 /help Display available options
4838 /imsvc <dir> Add directory to system include search path, as if part of %INCLUDE%
4839 /I <dir> Add directory to include search path
4840 /J Make char type unsigned
4841 /LDd Create debug DLL
4843 /link <options> Forward options to the linker
4844 /MDd Use DLL debug run-time
4845 /MD Use DLL run-time
4846 /MTd Use static debug run-time
4847 /MT Use static run-time
4848 /O0 Disable optimization
4849 /O1 Optimize for size (same as /Og /Os /Oy /Ob2 /GF /Gy)
4850 /O2 Optimize for speed (same as /Og /Oi /Ot /Oy /Ob2 /GF /Gy)
4851 /Ob0 Disable function inlining
4852 /Ob1 Only inline functions which are (explicitly or implicitly) marked inline
4853 /Ob2 Inline functions as deemed beneficial by the compiler
4855 /Od Disable optimization
4857 /Oi- Disable use of builtin functions
4858 /Oi Enable use of builtin functions
4859 /Os Optimize for size (like clang -Os)
4860 /Ot Optimize for speed (like clang -O3)
4861 /Ox Deprecated (same as /Og /Oi /Ot /Oy /Ob2); use /O2 instead
4862 /Oy- Disable frame pointer omission (x86 only, default)
4863 /Oy Enable frame pointer omission (x86 only)
4864 /O<flags> Set multiple /O flags at once; e.g. '/O2y-' for '/O2 /Oy-'
4865 /o <file or directory> Set output file or directory (ends in / or \)
4866 /P Preprocess to file
4867 /Qvec- Disable the loop vectorization passes
4868 /Qvec Enable the loop vectorization passes
4869 /showFilenames- Don't print the name of each compiled file (default)
4870 /showFilenames Print the name of each compiled file
4871 /showIncludes Print info about included files to stderr
4872 /source-charset:<value> Source encoding, supports only UTF-8
4873 /std:<value> Language standard to compile for
4874 /TC Treat all source files as C
4875 /Tc <filename> Specify a C source file
4876 /TP Treat all source files as C++
4877 /Tp <filename> Specify a C++ source file
4878 /utf-8 Set source and runtime encoding to UTF-8 (default)
4879 /U <macro> Undefine macro
4880 /vd<value> Control vtordisp placement
4881 /vmb Use a best-case representation method for member pointers
4882 /vmg Use a most-general representation for member pointers
4883 /vmm Set the default most-general representation to multiple inheritance
4884 /vms Set the default most-general representation to single inheritance
4885 /vmv Set the default most-general representation to virtual inheritance
4886 /volatile:iso Volatile loads and stores have standard semantics
4887 /volatile:ms Volatile loads and stores have acquire and release semantics
4888 /W0 Disable all warnings
4892 /W4 Enable -Wall and -Wextra
4893 /Wall Enable -Weverything
4894 /WX- Do not treat warnings as errors
4895 /WX Treat warnings as errors
4896 /w Disable all warnings
4897 /X Don't add %INCLUDE% to the include search path
4898 /Y- Disable precompiled headers, overrides /Yc and /Yu
4899 /Yc<filename> Generate a pch file for all code up to and including <filename>
4900 /Yu<filename> Load a pch file and use it instead of all code up to and including <filename>
4901 /Z7 Enable CodeView debug information in object files
4902 /Zc:char8_t Enable C++20 char8_t type
4903 /Zc:char8_t- Disable C++20 char8_t type
4904 /Zc:dllexportInlines- Don't dllexport/dllimport inline member functions of dllexport/import classes
4905 /Zc:dllexportInlines dllexport/dllimport inline member functions of dllexport/import classes (default)
4906 /Zc:sizedDealloc- Disable C++14 sized global deallocation functions
4907 /Zc:sizedDealloc Enable C++14 sized global deallocation functions
4908 /Zc:strictStrings Treat string literals as const
4909 /Zc:threadSafeInit- Disable thread-safe initialization of static variables
4910 /Zc:threadSafeInit Enable thread-safe initialization of static variables
4911 /Zc:trigraphs- Disable trigraphs (default)
4912 /Zc:trigraphs Enable trigraphs
4913 /Zc:twoPhase- Disable two-phase name lookup in templates
4914 /Zc:twoPhase Enable two-phase name lookup in templates
4915 /Zi Alias for /Z7. Does not produce PDBs.
4916 /Zl Don't mention any default libraries in the object file
4917 /Zp Set the default maximum struct packing alignment to 1
4918 /Zp<value> Specify the default maximum struct packing alignment
4919 /Zs Run the preprocessor, parser and semantic analysis stages
4922 -### Print (but do not run) the commands to run for this compilation
4923 --analyze Run the static analyzer
4924 -faddrsig Emit an address-significance table
4925 -fansi-escape-codes Use ANSI escape codes for diagnostics
4926 -fblocks Enable the 'blocks' language feature
4927 -fcf-protection=<value> Instrument control-flow architecture protection. Options: return, branch, full, none.
4928 -fcf-protection Enable cf-protection in 'full' mode
4929 -fcolor-diagnostics Use colors in diagnostics
4930 -fcomplete-member-pointers
4931 Require member pointer base types to be complete if they would be significant under the Microsoft ABI
4932 -fcoverage-mapping Generate coverage mapping to enable code coverage analysis
4933 -fcrash-diagnostics-dir=<dir>
4934 Put crash-report files in <dir>
4935 -fdebug-macro Emit macro debug information
4936 -fdelayed-template-parsing
4937 Parse templated function definitions at the end of the translation unit
4938 -fdiagnostics-absolute-paths
4939 Print absolute paths in diagnostics
4940 -fdiagnostics-parseable-fixits
4941 Print fix-its in machine parseable form
4942 -flto=<value> Set LTO mode to either 'full' or 'thin'
4943 -flto Enable LTO in 'full' mode
4944 -fmerge-all-constants Allow merging of constants
4945 -fmodule-file=<module_name>=<module-file>
4946 Use the specified module file that provides the module <module_name>
4947 -fmodule-header=<header>
4948 Build <header> as a C++20 header unit
4949 -fmodule-output=<path>
4950 Save intermediate module file results when compiling a standard C++ module unit.
4951 -fms-compatibility-version=<value>
4952 Dot-separated value representing the Microsoft compiler version
4953 number to report in _MSC_VER (0 = don't define it; default is same value as installed cl.exe, or 1933)
4954 -fms-compatibility Enable full Microsoft Visual C++ compatibility
4955 -fms-extensions Accept some non-standard constructs supported by the Microsoft compiler
4956 -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER
4957 (0 = don't define it; default is same value as installed cl.exe, or 1933)
4958 -fno-addrsig Don't emit an address-significance table
4959 -fno-builtin-<value> Disable implicit builtin knowledge of a specific function
4960 -fno-builtin Disable implicit builtin knowledge of functions
4961 -fno-complete-member-pointers
4962 Do not require member pointer base types to be complete if they would be significant under the Microsoft ABI
4963 -fno-coverage-mapping Disable code coverage analysis
4964 -fno-crash-diagnostics Disable auto-generation of preprocessed source files and a script for reproduction during a clang crash
4965 -fno-debug-macro Do not emit macro debug information
4966 -fno-delayed-template-parsing
4967 Disable delayed template parsing
4968 -fno-sanitize-address-poison-custom-array-cookie
4969 Disable poisoning array cookies when using custom operator new[] in AddressSanitizer
4970 -fno-sanitize-address-use-after-scope
4971 Disable use-after-scope detection in AddressSanitizer
4972 -fno-sanitize-address-use-odr-indicator
4973 Disable ODR indicator globals
4974 -fno-sanitize-ignorelist Don't use ignorelist file for sanitizers
4975 -fno-sanitize-cfi-cross-dso
4976 Disable control flow integrity (CFI) checks for cross-DSO calls.
4977 -fno-sanitize-coverage=<value>
4978 Disable specified features of coverage instrumentation for Sanitizers
4979 -fno-sanitize-memory-track-origins
4980 Disable origins tracking in MemorySanitizer
4981 -fno-sanitize-memory-use-after-dtor
4982 Disable use-after-destroy detection in MemorySanitizer
4983 -fno-sanitize-recover=<value>
4984 Disable recovery for specified sanitizers
4985 -fno-sanitize-stats Disable sanitizer statistics gathering.
4986 -fno-sanitize-thread-atomics
4987 Disable atomic operations instrumentation in ThreadSanitizer
4988 -fno-sanitize-thread-func-entry-exit
4989 Disable function entry/exit instrumentation in ThreadSanitizer
4990 -fno-sanitize-thread-memory-access
4991 Disable memory access instrumentation in ThreadSanitizer
4992 -fno-sanitize-trap=<value>
4993 Disable trapping for specified sanitizers
4994 -fno-standalone-debug Limit debug information produced to reduce size of debug binary
4995 -fno-strict-aliasing Disable optimizations based on strict aliasing rules (default)
4996 -fobjc-runtime=<value> Specify the target Objective-C runtime kind and version
4997 -fprofile-exclude-files=<value>
4998 Instrument only functions from files where names don't match all the regexes separated by a semi-colon
4999 -fprofile-filter-files=<value>
5000 Instrument only functions from files where names match any regex separated by a semi-colon
5001 -fprofile-generate=<dirname>
5002 Generate instrumented code to collect execution counts into a raw profile file in the directory specified by the argument. The filename uses default_%m.profraw pattern
5003 (overridden by LLVM_PROFILE_FILE env var)
5005 Generate instrumented code to collect execution counts into default_%m.profraw file
5006 (overridden by '=' form of option or LLVM_PROFILE_FILE env var)
5007 -fprofile-instr-generate=<file_name_pattern>
5008 Generate instrumented code to collect execution counts into the file whose name pattern is specified as the argument
5009 (overridden by LLVM_PROFILE_FILE env var)
5010 -fprofile-instr-generate
5011 Generate instrumented code to collect execution counts into default.profraw file
5012 (overridden by '=' form of option or LLVM_PROFILE_FILE env var)
5013 -fprofile-instr-use=<value>
5014 Use instrumentation data for coverage testing or profile-guided optimization
5015 -fprofile-use=<value>
5016 Use instrumentation data for profile-guided optimization
5017 -fprofile-remapping-file=<file>
5018 Use the remappings described in <file> to match the profile data against names in the program
5019 -fprofile-list=<file>
5020 Filename defining the list of functions/files to instrument
5021 -fsanitize-address-field-padding=<value>
5022 Level of field padding for AddressSanitizer
5023 -fsanitize-address-globals-dead-stripping
5024 Enable linker dead stripping of globals in AddressSanitizer
5025 -fsanitize-address-poison-custom-array-cookie
5026 Enable poisoning array cookies when using custom operator new[] in AddressSanitizer
5027 -fsanitize-address-use-after-return=<mode>
5028 Select the mode of detecting stack use-after-return in AddressSanitizer: never | runtime (default) | always
5029 -fsanitize-address-use-after-scope
5030 Enable use-after-scope detection in AddressSanitizer
5031 -fsanitize-address-use-odr-indicator
5032 Enable ODR indicator globals to avoid false ODR violation reports in partially sanitized programs at the cost of an increase in binary size
5033 -fsanitize-ignorelist=<value>
5034 Path to ignorelist file for sanitizers
5035 -fsanitize-cfi-cross-dso
5036 Enable control flow integrity (CFI) checks for cross-DSO calls.
5037 -fsanitize-cfi-icall-generalize-pointers
5038 Generalize pointers in CFI indirect call type signature checks
5039 -fsanitize-coverage=<value>
5040 Specify the type of coverage instrumentation for Sanitizers
5041 -fsanitize-hwaddress-abi=<value>
5042 Select the HWAddressSanitizer ABI to target (interceptor or platform, default interceptor)
5043 -fsanitize-memory-track-origins=<value>
5044 Enable origins tracking in MemorySanitizer
5045 -fsanitize-memory-track-origins
5046 Enable origins tracking in MemorySanitizer
5047 -fsanitize-memory-use-after-dtor
5048 Enable use-after-destroy detection in MemorySanitizer
5049 -fsanitize-recover=<value>
5050 Enable recovery for specified sanitizers
5051 -fsanitize-stats Enable sanitizer statistics gathering.
5052 -fsanitize-thread-atomics
5053 Enable atomic operations instrumentation in ThreadSanitizer (default)
5054 -fsanitize-thread-func-entry-exit
5055 Enable function entry/exit instrumentation in ThreadSanitizer (default)
5056 -fsanitize-thread-memory-access
5057 Enable memory access instrumentation in ThreadSanitizer (default)
5058 -fsanitize-trap=<value> Enable trapping for specified sanitizers
5059 -fsanitize-undefined-strip-path-components=<number>
5060 Strip (or keep only, if negative) a given number of path components when emitting check metadata.
5061 -fsanitize=<check> Turn on runtime checks for various forms of undefined or suspicious
5062 behavior. See user manual for available checks
5063 -fsplit-lto-unit Enables splitting of the LTO unit.
5064 -fstandalone-debug Emit full debug info for all types used by the program
5065 -fstrict-aliasing Enable optimizations based on strict aliasing rules
5066 -fsyntax-only Run the preprocessor, parser and semantic analysis stages
5067 -fwhole-program-vtables Enables whole-program vtable optimization. Requires -flto
5068 -gcodeview-ghash Emit type record hashes in a .debug$H section
5069 -gcodeview Generate CodeView debug information
5070 -gline-directives-only Emit debug line info directives only
5071 -gline-tables-only Emit debug line number tables only
5072 -miamcu Use Intel MCU ABI
5073 -mllvm <value> Additional arguments to forward to LLVM's option processing
5074 -nobuiltininc Disable builtin #include directories
5075 -Qunused-arguments Don't emit warning for unused driver arguments
5076 -R<remark> Enable the specified remark
5077 --target=<value> Generate code for the given target
5078 --version Print version information
5079 -v Show commands to run and use verbose output
5080 -W<warning> Enable the specified warning
5081 -Xclang <arg> Pass <arg> to the clang compiler
5086 When clang-cl is run with a set of ``/clang:<arg>`` options, it will gather all
5087 of the ``<arg>`` arguments and process them as if they were passed to the clang
5088 driver. This mechanism allows you to pass flags that are not exposed in the
5089 clang-cl options or flags that have a different meaning when passed to the clang
5090 driver. Regardless of where they appear in the command line, the ``/clang:``
5091 arguments are treated as if they were passed at the end of the clang-cl command
5094 The /Zc:dllexportInlines- Option
5095 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5097 This causes the class-level `dllexport` and `dllimport` attributes to not apply
5098 to inline member functions, as they otherwise would. For example, in the code
5099 below `S::foo()` would normally be defined and exported by the DLL, but when
5100 using the ``/Zc:dllexportInlines-`` flag it is not:
5104 struct __declspec(dllexport) S {
5108 This has the benefit that the compiler doesn't need to emit a definition of
5109 `S::foo()` in every translation unit where the declaration is included, as it
5110 would otherwise do to ensure there's a definition in the DLL even if it's not
5111 used there. If the declaration occurs in a header file that's widely used, this
5112 can save significant compilation time and output size. It also reduces the
5113 number of functions exported by the DLL similarly to what
5114 ``-fvisibility-inlines-hidden`` does for shared objects on ELF and Mach-O.
5115 Since the function declaration comes with an inline definition, users of the
5116 library can use that definition directly instead of importing it from the DLL.
5118 Note that the Microsoft Visual C++ compiler does not support this option, and
5119 if code in a DLL is compiled with ``/Zc:dllexportInlines-``, the code using the
5120 DLL must be compiled in the same way so that it doesn't attempt to dllimport
5121 the inline member functions. The reverse scenario should generally work though:
5122 a DLL compiled without this flag (such as a system library compiled with Visual
5123 C++) can be referenced from code compiled using the flag, meaning that the
5124 referencing code will use the inline definitions instead of importing them from
5127 Also note that like when using ``-fvisibility-inlines-hidden``, the address of
5128 `S::foo()` will be different inside and outside the DLL, breaking the C/C++
5129 standard requirement that functions have a unique address.
5131 The flag does not apply to explicit class template instantiation definitions or
5132 declarations, as those are typically used to explicitly provide a single
5133 definition in a DLL, (dllexported instantiation definition) or to signal that
5134 the definition is available elsewhere (dllimport instantiation declaration). It
5135 also doesn't apply to inline members with static local variables, to ensure
5136 that the same instance of the variable is used inside and outside the DLL.
5138 Using this flag can cause problems when inline functions that would otherwise
5139 be dllexported refer to internal symbols of a DLL. For example:
5145 struct __declspec(dllimport) S {
5146 void foo() { internal(); }
5149 Normally, references to `S::foo()` would use the definition in the DLL from
5150 which it was exported, and which presumably also has the definition of
5151 `internal()`. However, when using ``/Zc:dllexportInlines-``, the inline
5152 definition of `S::foo()` is used directly, resulting in a link error since
5153 `internal()` is not available. Even worse, if there is an inline definition of
5154 `internal()` containing a static local variable, we will now refer to a
5155 different instance of that variable than in the DLL:
5159 inline int internal() { static int x; return x++; }
5161 struct __declspec(dllimport) S {
5162 int foo() { return internal(); }
5165 This could lead to very subtle bugs. Using ``-fvisibility-inlines-hidden`` can
5166 lead to the same issue. To avoid it in this case, make `S::foo()` or
5167 `internal()` non-inline, or mark them `dllimport/dllexport` explicitly.
5169 Finding Clang runtime libraries
5170 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5172 clang-cl supports several features that require runtime library support:
5174 - Address Sanitizer (ASan): ``-fsanitize=address``
5175 - Undefined Behavior Sanitizer (UBSan): ``-fsanitize=undefined``
5176 - Code coverage: ``-fprofile-instr-generate -fcoverage-mapping``
5177 - Profile Guided Optimization (PGO): ``-fprofile-generate``
5178 - Certain math operations (int128 division) require the builtins library
5180 In order to use these features, the user must link the right runtime libraries
5181 into their program. These libraries are distributed alongside Clang in the
5182 library resource directory. Clang searches for the resource directory by
5183 searching relative to the Clang executable. For example, if LLVM is installed
5184 in ``C:\Program Files\LLVM``, then the profile runtime library will be located
5186 ``C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows\clang_rt.profile-x86_64.lib``.
5188 For UBSan, PGO, and coverage, Clang will emit object files that auto-link the
5189 appropriate runtime library, but the user generally needs to help the linker
5190 (whether it is ``lld-link.exe`` or MSVC ``link.exe``) find the library resource
5191 directory. Using the example installation above, this would mean passing
5192 ``/LIBPATH:C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows`` to the linker.
5193 If the user links the program with the ``clang`` or ``clang-cl`` drivers, the
5194 driver will pass this flag for them.
5196 The auto-linking can be disabled with -fno-rtlib-defaultlib. If that flag is
5197 used, pass the complete flag to required libraries as described for ASan below.
5199 If the linker cannot find the appropriate library, it will emit an error like
5202 $ clang-cl -c -fsanitize=undefined t.cpp
5204 $ lld-link t.obj -dll
5205 lld-link: error: could not open 'clang_rt.ubsan_standalone-x86_64.lib': no such file or directory
5206 lld-link: error: could not open 'clang_rt.ubsan_standalone_cxx-x86_64.lib': no such file or directory
5208 $ link t.obj -dll -nologo
5209 LINK : fatal error LNK1104: cannot open file 'clang_rt.ubsan_standalone-x86_64.lib'
5211 To fix the error, add the appropriate ``/libpath:`` flag to the link line.
5213 For ASan, as of this writing, the user is also responsible for linking against
5214 the correct ASan libraries.
5216 If the user is using the dynamic CRT (``/MD``), then they should add
5217 ``clang_rt.asan_dynamic-x86_64.lib`` to the link line as a regular input. For
5218 other architectures, replace x86_64 with the appropriate name here and below.
5220 If the user is using the static CRT (``/MT``), then different runtimes are used
5221 to produce DLLs and EXEs. To link a DLL, pass
5222 ``clang_rt.asan_dll_thunk-x86_64.lib``. To link an EXE, pass
5223 ``-wholearchive:clang_rt.asan-x86_64.lib``.
5225 Windows System Headers and Library Lookup
5226 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5228 clang-cl uses a set of different approaches to locate the right system libraries
5229 to link against when building code. The Windows environment uses libraries from
5230 three distinct sources:
5233 2. UCRT (Universal C Runtime)
5234 3. Visual C++ Tools (VCRuntime)
5236 The Windows SDK provides the import libraries and headers required to build
5237 programs against the Windows system packages. Underlying the Windows SDK is the
5238 UCRT, the universal C runtime.
5240 This difference is best illustrated by the various headers that one would find
5241 in the different categories. The WinSDK would contain headers such as
5242 `WinSock2.h` which is part of the Windows API surface, providing the Windows
5243 socketing interfaces for networking. UCRT provides the C library headers,
5244 including e.g. `stdio.h`. Finally, the Visual C++ tools provides the underlying
5245 Visual C++ Runtime headers such as `stdint.h` or `crtdefs.h`.
5247 There are various controls that allow the user control over where clang-cl will
5248 locate these headers. The default behaviour for the Windows SDK and UCRT is as
5251 1. Consult the command line.
5253 Anything the user specifies is always given precedence. The following
5254 extensions are part of the clang-cl toolset:
5258 The `/winsysroot:` is used as an equivalent to `-sysroot` on Unix
5259 environments. It allows the control of an alternate location to be treated
5260 as a system root. When specified, it will be used as the root where the
5261 `Windows Kits` is located.
5266 If `/winsysroot:` is not specified, the `/winsdkdir:` argument is consulted
5267 as a location to identify where the Windows SDK is located. Contrary to
5268 `/winsysroot:`, `/winsdkdir:` is expected to be the complete path rather
5269 than a root to locate `Windows Kits`.
5271 The `/winsdkversion:` flag allows the user to specify a version identifier
5272 for the SDK to prefer. When this is specified, no additional validation is
5273 performed and this version is preferred. If the version is not specified,
5274 the highest detected version number will be used.
5276 2. Consult the environment.
5278 TODO: This is not yet implemented.
5280 This will consult the environment variables:
5285 3. Fallback to the registry.
5287 If no arguments are used to indicate where the SDK is present, and the
5288 compiler is running on Windows, the registry is consulted to locate the
5291 The Visual C++ Toolset has a slightly more elaborate mechanism for detection.
5293 1. Consult the command line.
5297 The `/winsysroot:` is used as an equivalent to `-sysroot` on Unix
5298 environments. It allows the control of an alternate location to be treated
5299 as a system root. When specified, it will be used as the root where the
5300 `VC` directory is located.
5303 - `/vctoolsversion:`
5305 If `/winsysroot:` is not specified, the `/vctoolsdir:` argument is consulted
5306 as a location to identify where the Visual C++ Tools are located. If
5307 `/vctoolsversion:` is specified, that version is preferred, otherwise, the
5308 highest version detected is used.
5310 2. Consult the environment.
5312 - `/external:[VARIABLE]`
5314 This specifies a user identified environment variable which is treated as
5315 a path delimiter (`;`) separated list of paths to map into `-imsvc`
5316 arguments which are treated as `-isystem`.
5318 - `INCLUDE` and `EXTERNAL_INCLUDE`
5320 The path delimiter (`;`) separated list of paths will be mapped to
5321 `-imsvc` arguments which are treated as `-isystem`.
5323 - `LIB` (indirectly)
5325 The linker `link.exe` or `lld-link.exe` will honour the environment
5326 variable `LIB` which is a path delimiter (`;`) set of paths to consult for
5327 the import libraries to use when linking the final target.
5329 The following environment variables will be consulted and used to form paths
5330 to validate and load content from as appropriate:
5332 - `VCToolsInstallDir`
5336 3. Consult `ISetupConfiguration` [Windows Only]
5338 Assuming that the toolchain is built with `USE_MSVC_SETUP_API` defined and
5339 is running on Windows, the Visual Studio COM interface `ISetupConfiguration`
5340 will be used to locate the installation of the MSVC toolset.
5342 4. Fallback to the registry [DEPRECATED]
5344 The registry information is used to help locate the installation as a final
5345 fallback. This is only possible for pre-VS2017 installations and is
5346 considered deprecated.
5348 Restrictions and Limitations compared to Clang
5349 ----------------------------------------------
5351 Strict aliasing (TBAA) is always off by default in clang-cl whereas in clang,
5352 strict aliasing is turned on by default for all optimization levels. For more
5353 details, see :ref:`Strict aliasing <strict_aliasing>`.