1============================ 2Clang Compiler User's Manual 3============================ 4 5.. include:: <isonum.txt> 6 7.. contents:: 8 :local: 9 10Introduction 11============ 12 13The Clang Compiler is an open-source compiler for the C family of 14programming languages, aiming to be the best in class implementation of 15these languages. Clang builds on the LLVM optimizer and code generator, 16allowing it to provide high-quality optimization and code generation 17support for many targets. For more general information, please see the 18`Clang Web Site <https://clang.llvm.org>`_ or the `LLVM Web 19Site <https://llvm.org>`_. 20 21This document describes important notes about using Clang as a compiler 22for an end-user, documenting the supported features, command line 23options, etc. If you are interested in using Clang to build a tool that 24processes code, please see :doc:`InternalsManual`. If you are interested in the 25`Clang Static Analyzer <https://clang-analyzer.llvm.org>`_, please see its web 26page. 27 28Clang is one component in a complete toolchain for C family languages. 29A separate document describes the other pieces necessary to 30:doc:`assemble a complete toolchain <Toolchain>`. 31 32Clang is designed to support the C family of programming languages, 33which 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 35language-specific information, please see the corresponding language 36specific section: 37 38- :ref:`C Language <c>`: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO 39 C99 (+TC1, TC2, TC3). 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. 46 47In addition to these base languages and their dialects, Clang supports a 48broad variety of language extensions, which are documented in the 49corresponding language section. These extensions are provided to be 50compatible with the GCC, Microsoft, and other popular compilers as well 51as to improve functionality through Clang-specific features. The Clang 52driver and language features are intentionally designed to be as 53compatible with the GNU GCC compiler as reasonably possible, easing 54migration from GCC to Clang. In most cases, code "just works". 55Clang also provides an alternative driver, :ref:`clang-cl`, that is designed 56to be compatible with the Visual C++ compiler, cl.exe. 57 58In addition to language specific features, Clang has a variety of 59features that depend on what CPU architecture or operating system is 60being compiled for. Please see the :ref:`Target-Specific Features and 61Limitations <target_features>` section for more details. 62 63The rest of the introduction introduces some basic :ref:`compiler 64terminology <terminology>` that is used throughout this manual and 65contains a basic :ref:`introduction to using Clang <basicusage>` as a 66command line compiler. 67 68.. _terminology: 69 70Terminology 71----------- 72 73Front end, parser, backend, preprocessor, undefined behavior, 74diagnostic, optimizer 75 76.. _basicusage: 77 78Basic Usage 79----------- 80 81Intro to how to use a C compiler for newbies. 82 83compile + link compile then link debug info enabling optimizations 84picking a language to use, defaults to C17 by default. Autosenses based 85on extension. using a makefile 86 87Command Line Options 88==================== 89 90This section is generally an index into other sections. It does not go 91into depth on the ones that are covered by other sections. However, the 92first part introduces the language selection and other high level 93options like :option:`-c`, :option:`-g`, etc. 94 95Options to Control Error and Warning Messages 96--------------------------------------------- 97 98.. option:: -Werror 99 100 Turn warnings into errors. 101 102.. This is in plain monospaced font because it generates the same label as 103.. -Werror, and Sphinx complains. 104 105``-Werror=foo`` 106 107 Turn warning "foo" into an error. 108 109.. option:: -Wno-error=foo 110 111 Turn warning "foo" into a warning even if :option:`-Werror` is specified. 112 113.. option:: -Wfoo 114 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. 118 119.. option:: -Wno-foo 120 121 Disable warning "foo". 122 123.. option:: -w 124 125 Disable all diagnostics. 126 127.. option:: -Weverything 128 129 :ref:`Enable all diagnostics. <diagnostics_enable_everything>` 130 131.. option:: -pedantic 132 133 Warn on language extensions. 134 135.. option:: -pedantic-errors 136 137 Error on language extensions. 138 139.. option:: -Wsystem-headers 140 141 Enable warnings from system headers. 142 143.. option:: -ferror-limit=123 144 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`. 147 148.. option:: -ftemplate-backtrace-limit=123 149 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`. 153 154.. _cl_diag_formatting: 155 156Formatting of Diagnostics 157^^^^^^^^^^^^^^^^^^^^^^^^^ 158 159Clang aims to produce beautiful diagnostics by default, particularly for 160new users that first come to Clang. However, different people have 161different preferences, and sometimes Clang is driven not by a human, 162but by a program that wants consistent and easily parsable output. For 163these cases, Clang provides a wide range of options to control the exact 164output format of the diagnostics that it generates. 165 166.. _opt_fshow-column: 167 168.. option:: -f[no-]show-column 169 170 Print column number in diagnostic. 171 172 This option, which defaults to on, controls whether or not Clang 173 prints the column number of a diagnostic. For example, when this is 174 enabled, Clang will print something like: 175 176 :: 177 178 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 179 #endif bad 180 ^ 181 // 182 183 When this is disabled, Clang will print "test.c:28: warning..." with 184 no column number. 185 186 The printed column numbers count bytes from the beginning of the 187 line; take care if your source contains multibyte characters. 188 189.. _opt_fshow-source-location: 190 191.. option:: -f[no-]show-source-location 192 193 Print source file/line/column information in diagnostic. 194 195 This option, which defaults to on, controls whether or not Clang 196 prints the filename, line number and column number of a diagnostic. 197 For example, when this is enabled, Clang will print something like: 198 199 :: 200 201 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 202 #endif bad 203 ^ 204 // 205 206 When this is disabled, Clang will not print the "test.c:28:8: " 207 part. 208 209.. _opt_fcaret-diagnostics: 210 211.. option:: -f[no-]caret-diagnostics 212 213 Print source line and ranges from source code in diagnostic. 214 This option, which defaults to on, controls whether or not Clang 215 prints the source line, source ranges, and caret when emitting a 216 diagnostic. For example, when this is enabled, Clang will print 217 something like: 218 219 :: 220 221 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 222 #endif bad 223 ^ 224 // 225 226.. option:: -f[no-]color-diagnostics 227 228 This option, which defaults to on when a color-capable terminal is 229 detected, controls whether or not Clang prints diagnostics in color. 230 231 When this option is enabled, Clang will use colors to highlight 232 specific parts of the diagnostic, e.g., 233 234 .. nasty hack to not lose our dignity 235 236 .. raw:: html 237 238 <pre> 239 <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> 240 #endif bad 241 <span style="color:green">^</span> 242 <span style="color:green">//</span> 243 </pre> 244 245 When this is disabled, Clang will just print: 246 247 :: 248 249 test.c:2:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 250 #endif bad 251 ^ 252 // 253 254.. option:: -fansi-escape-codes 255 256 Controls whether ANSI escape codes are used instead of the Windows Console 257 API to output colored diagnostics. This option is only used on Windows and 258 defaults to off. 259 260.. option:: -fdiagnostics-format=clang/msvc/vi 261 262 Changes diagnostic output format to better match IDEs and command line tools. 263 264 This option controls the output format of the filename, line number, 265 and column printed in diagnostic messages. The options, and their 266 affect on formatting a simple conversion diagnostic, follow: 267 268 **clang** (default) 269 :: 270 271 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' 272 273 **msvc** 274 :: 275 276 t.c(3,11) : warning: conversion specifies type 'char *' but the argument has type 'int' 277 278 **vi** 279 :: 280 281 t.c +3:11: warning: conversion specifies type 'char *' but the argument has type 'int' 282 283.. _opt_fdiagnostics-show-option: 284 285.. option:: -f[no-]diagnostics-show-option 286 287 Enable ``[-Woption]`` information in diagnostic line. 288 289 This option, which defaults to on, controls whether or not Clang 290 prints the associated :ref:`warning group <cl_diag_warning_groups>` 291 option name when outputting a warning diagnostic. For example, in 292 this output: 293 294 :: 295 296 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 297 #endif bad 298 ^ 299 // 300 301 Passing **-fno-diagnostics-show-option** will prevent Clang from 302 printing the [:option:`-Wextra-tokens`] information in 303 the diagnostic. This information tells you the flag needed to enable 304 or disable the diagnostic, either from the command line or through 305 :ref:`#pragma GCC diagnostic <pragma_GCC_diagnostic>`. 306 307.. option:: -fdiagnostics-show-category=none/id/name 308 309 Enable printing category information in diagnostic line. 310 311 This option, which defaults to "none", controls whether or not Clang 312 prints the category associated with a diagnostic when emitting it. 313 Each diagnostic may or many not have an associated category, if it 314 has one, it is listed in the diagnostic categorization field of the 315 diagnostic line (in the []'s). 316 317 For example, a format string warning will produce these three 318 renditions based on the setting of this option: 319 320 :: 321 322 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat] 323 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,1] 324 t.c:3:11: warning: conversion specifies type 'char *' but the argument has type 'int' [-Wformat,Format String] 325 326 This category can be used by clients that want to group diagnostics 327 by category, so it should be a high level category. We want dozens 328 of these, not hundreds or thousands of them. 329 330.. _opt_fsave-optimization-record: 331 332.. option:: -f[no-]save-optimization-record[=<format>] 333 334 Enable optimization remarks during compilation and write them to a separate 335 file. 336 337 This option, which defaults to off, controls whether Clang writes 338 optimization reports to a separate file. By recording diagnostics in a file, 339 users can parse or sort the remarks in a convenient way. 340 341 By default, the serialization format is YAML. 342 343 The supported serialization formats are: 344 345 - .. _opt_fsave_optimization_record_yaml: 346 347 ``-fsave-optimization-record=yaml``: A structured YAML format. 348 349 - .. _opt_fsave_optimization_record_bitstream: 350 351 ``-fsave-optimization-record=bitstream``: A binary format based on LLVM 352 Bitstream. 353 354 The output file is controlled by :option:`-foptimization-record-file`. 355 356 In the absence of an explicit output file, the file is chosen using the 357 following scheme: 358 359 ``<base>.opt.<format>`` 360 361 where ``<base>`` is based on the output file of the compilation (whether 362 it's explicitly specified through `-o` or not) when used with `-c` or `-S`. 363 For example: 364 365 * ``clang -fsave-optimization-record -c in.c -o out.o`` will generate 366 ``out.opt.yaml`` 367 368 * ``clang -fsave-optimization-record -c in.c`` will generate 369 ``in.opt.yaml`` 370 371 When targeting (Thin)LTO, the base is derived from the output filename, and 372 the extension is not dropped. 373 374 When targeting ThinLTO, the following scheme is used: 375 376 ``<base>.opt.<format>.thin.<num>.<format>`` 377 378 Darwin-only: when used for generating a linked binary from a source file 379 (through an intermediate object file), the driver will invoke `cc1` to 380 generate a temporary object file. The temporary remark file will be emitted 381 next to the object file, which will then be picked up by `dsymutil` and 382 emitted in the .dSYM bundle. This is available for all formats except YAML. 383 384 For example: 385 386 ``clang -fsave-optimization-record=bitstream in.c -o out`` will generate 387 388 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.o`` 389 390 * ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.opt.bitstream`` 391 392 * ``out`` 393 394 * ``out.dSYM/Contents/Resources/Remarks/out`` 395 396 Darwin-only: compiling for multiple architectures will use the following 397 scheme: 398 399 ``<base>-<arch>.opt.<format>`` 400 401 Note that this is incompatible with passing the 402 :option:`-foptimization-record-file` option. 403 404.. option:: -foptimization-record-file 405 406 Control the file to which optimization reports are written. This implies 407 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`. 408 409 On Darwin platforms, this is incompatible with passing multiple 410 ``-arch <arch>`` options. 411 412.. option:: -foptimization-record-passes 413 414 Only include passes which match a specified regular expression. 415 416 When optimization reports are being output (see 417 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>`), this 418 option controls the passes that will be included in the final report. 419 420 If this option is not used, all the passes are included in the optimization 421 record. 422 423.. _opt_fdiagnostics-show-hotness: 424 425.. option:: -f[no-]diagnostics-show-hotness 426 427 Enable profile hotness information in diagnostic line. 428 429 This option controls whether Clang prints the profile hotness associated 430 with diagnostics in the presence of profile-guided optimization information. 431 This is currently supported with optimization remarks (see 432 :ref:`Options to Emit Optimization Reports <rpass>`). The hotness information 433 allows users to focus on the hot optimization remarks that are likely to be 434 more relevant for run-time performance. 435 436 For example, in this output, the block containing the callsite of `foo` was 437 executed 3000 times according to the profile data: 438 439 :: 440 441 s.c:7:10: remark: foo inlined into bar (hotness: 3000) [-Rpass-analysis=inline] 442 sum += foo(x, x - 2); 443 ^ 444 445 This option is implied when 446 :ref:`-fsave-optimization-record <opt_fsave-optimization-record>` is used. 447 Otherwise, it defaults to off. 448 449.. option:: -fdiagnostics-hotness-threshold 450 451 Prevent optimization remarks from being output if they do not have at least 452 this hotness value. 453 454 This option, which defaults to zero, controls the minimum hotness an 455 optimization remark would need in order to be output by Clang. This is 456 currently supported with optimization remarks (see :ref:`Options to Emit 457 Optimization Reports <rpass>`) when profile hotness information in 458 diagnostics is enabled (see 459 :ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`). 460 461.. _opt_fdiagnostics-fixit-info: 462 463.. option:: -f[no-]diagnostics-fixit-info 464 465 Enable "FixIt" information in the diagnostics output. 466 467 This option, which defaults to on, controls whether or not Clang 468 prints the information on how to fix a specific diagnostic 469 underneath it when it knows. For example, in this output: 470 471 :: 472 473 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 474 #endif bad 475 ^ 476 // 477 478 Passing **-fno-diagnostics-fixit-info** will prevent Clang from 479 printing the "//" line at the end of the message. This information 480 is useful for users who may not understand what is wrong, but can be 481 confusing for machine parsing. 482 483.. _opt_fdiagnostics-print-source-range-info: 484 485.. option:: -fdiagnostics-print-source-range-info 486 487 Print machine parsable information about source ranges. 488 This option makes Clang print information about source ranges in a machine 489 parsable format after the file/line/column number information. The 490 information is a simple sequence of brace enclosed ranges, where each range 491 lists the start and end line/column locations. For example, in this output: 492 493 :: 494 495 exprs.c:47:15:{47:8-47:14}{47:17-47:24}: error: invalid operands to binary expression ('int *' and '_Complex float') 496 P = (P-42) + Gamma*4; 497 ~~~~~~ ^ ~~~~~~~ 498 499 The {}'s are generated by -fdiagnostics-print-source-range-info. 500 501 The printed column numbers count bytes from the beginning of the 502 line; take care if your source contains multibyte characters. 503 504.. option:: -fdiagnostics-parseable-fixits 505 506 Print Fix-Its in a machine parseable form. 507 508 This option makes Clang print available Fix-Its in a machine 509 parseable format at the end of diagnostics. The following example 510 illustrates the format: 511 512 :: 513 514 fix-it:"t.cpp":{7:25-7:29}:"Gamma" 515 516 The range printed is a half-open range, so in this example the 517 characters at column 25 up to but not including column 29 on line 7 518 in t.cpp should be replaced with the string "Gamma". Either the 519 range or the replacement string may be empty (representing strict 520 insertions and strict erasures, respectively). Both the file name 521 and the insertion string escape backslash (as "\\\\"), tabs (as 522 "\\t"), newlines (as "\\n"), double quotes(as "\\"") and 523 non-printable characters (as octal "\\xxx"). 524 525 The printed column numbers count bytes from the beginning of the 526 line; take care if your source contains multibyte characters. 527 528.. option:: -fno-elide-type 529 530 Turns off elision in template type printing. 531 532 The default for template type printing is to elide as many template 533 arguments as possible, removing those which are the same in both 534 template types, leaving only the differences. Adding this flag will 535 print all the template arguments. If supported by the terminal, 536 highlighting will still appear on differing arguments. 537 538 Default: 539 540 :: 541 542 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; 543 544 -fno-elide-type: 545 546 :: 547 548 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; 549 550.. option:: -fdiagnostics-show-template-tree 551 552 Template type diffing prints a text tree. 553 554 For diffing large templated types, this option will cause Clang to 555 display the templates as an indented text tree, one argument per 556 line, with differences marked inline. This is compatible with 557 -fno-elide-type. 558 559 Default: 560 561 :: 562 563 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; 564 565 With :option:`-fdiagnostics-show-template-tree`: 566 567 :: 568 569 t.cc:4:5: note: candidate function not viable: no known conversion for 1st argument; 570 vector< 571 map< 572 [...], 573 map< 574 [float != double], 575 [...]>>> 576 577.. _cl_diag_warning_groups: 578 579Individual Warning Groups 580^^^^^^^^^^^^^^^^^^^^^^^^^ 581 582TODO: Generate this from tblgen. Define one anchor per warning group. 583 584.. option:: -Wextra-tokens 585 586 Warn about excess tokens at the end of a preprocessor directive. 587 588 This option, which defaults to on, enables warnings about extra 589 tokens at the end of preprocessor directives. For example: 590 591 :: 592 593 test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens] 594 #endif bad 595 ^ 596 597 These extra tokens are not strictly conforming, and are usually best 598 handled by commenting them out. 599 600.. option:: -Wambiguous-member-template 601 602 Warn about unqualified uses of a member template whose name resolves to 603 another template at the location of the use. 604 605 This option, which defaults to on, enables a warning in the 606 following code: 607 608 :: 609 610 template<typename T> struct set{}; 611 template<typename T> struct trait { typedef const T& type; }; 612 struct Value { 613 template<typename T> void set(typename trait<T>::type value) {} 614 }; 615 void foo() { 616 Value v; 617 v.set<double>(3.2); 618 } 619 620 C++ [basic.lookup.classref] requires this to be an error, but, 621 because it's hard to work around, Clang downgrades it to a warning 622 as an extension. 623 624.. option:: -Wbind-to-temporary-copy 625 626 Warn about an unusable copy constructor when binding a reference to a 627 temporary. 628 629 This option enables warnings about binding a 630 reference to a temporary when the temporary doesn't have a usable 631 copy constructor. For example: 632 633 :: 634 635 struct NonCopyable { 636 NonCopyable(); 637 private: 638 NonCopyable(const NonCopyable&); 639 }; 640 void foo(const NonCopyable&); 641 void bar() { 642 foo(NonCopyable()); // Disallowed in C++98; allowed in C++11. 643 } 644 645 :: 646 647 struct NonCopyable2 { 648 NonCopyable2(); 649 NonCopyable2(NonCopyable2&); 650 }; 651 void foo(const NonCopyable2&); 652 void bar() { 653 foo(NonCopyable2()); // Disallowed in C++98; allowed in C++11. 654 } 655 656 Note that if ``NonCopyable2::NonCopyable2()`` has a default argument 657 whose instantiation produces a compile error, that error will still 658 be a hard error in C++98 mode even if this warning is turned off. 659 660Options to Control Clang Crash Diagnostics 661------------------------------------------ 662 663As unbelievable as it may sound, Clang does crash from time to time. 664Generally, this only occurs to those living on the `bleeding 665edge <https://llvm.org/releases/download.html#svn>`_. Clang goes to great 666lengths to assist you in filing a bug report. Specifically, Clang 667generates preprocessed source file(s) and associated run script(s) upon 668a crash. These files should be attached to a bug report to ease 669reproducibility of the failure. Below are the command line options to 670control the crash diagnostics. 671 672.. option:: -fcrash-diagnostics=<val> 673 674 Valid values are: 675 676 * ``off`` (Disable auto-generation of preprocessed source files during a clang crash.) 677 * ``compiler`` (Generate diagnostics for compiler crashes (default)) 678 * ``all`` (Generate diagnostics for all tools which support it) 679 680.. option:: -fno-crash-diagnostics 681 682 Disable auto-generation of preprocessed source files during a clang crash. 683 684 The -fno-crash-diagnostics flag can be helpful for speeding the process 685 of generating a delta reduced test case. 686 687.. option:: -fcrash-diagnostics-dir=<dir> 688 689 Specify where to write the crash diagnostics files; defaults to the 690 usual location for temporary files. 691 692.. envvar:: CLANG_CRASH_DIAGNOSTICS_DIR=<dir> 693 694 Like ``-fcrash-diagnostics-dir=<dir>``, specifies where to write the 695 crash diagnostics files, but with lower precedence than the option. 696 697Clang is also capable of generating preprocessed source file(s) and associated 698run script(s) even without a crash. This is specially useful when trying to 699generate a reproducer for warnings or errors while using modules. 700 701.. option:: -gen-reproducer 702 703 Generates preprocessed source files, a reproducer script and if relevant, a 704 cache containing: built module pcm's and all headers needed to rebuild the 705 same modules. 706 707.. _rpass: 708 709Options to Emit Optimization Reports 710------------------------------------ 711 712Optimization reports trace, at a high-level, all the major decisions 713done by compiler transformations. For instance, when the inliner 714decides to inline function ``foo()`` into ``bar()``, or the loop unroller 715decides to unroll a loop N times, or the vectorizer decides to 716vectorize a loop body. 717 718Clang offers a family of flags which the optimizers can use to emit 719a diagnostic in three cases: 720 7211. When the pass makes a transformation (`-Rpass`). 722 7232. When the pass fails to make a transformation (`-Rpass-missed`). 724 7253. When the pass determines whether or not to make a transformation 726 (`-Rpass-analysis`). 727 728NOTE: Although the discussion below focuses on `-Rpass`, the exact 729same options apply to `-Rpass-missed` and `-Rpass-analysis`. 730 731Since there are dozens of passes inside the compiler, each of these flags 732take a regular expression that identifies the name of the pass which should 733emit the associated diagnostic. For example, to get a report from the inliner, 734compile the code with: 735 736.. code-block:: console 737 738 $ clang -O2 -Rpass=inline code.cc -o code 739 code.cc:4:25: remark: foo inlined into bar [-Rpass=inline] 740 int bar(int j) { return foo(j, j - 2); } 741 ^ 742 743Note that remarks from the inliner are identified with `[-Rpass=inline]`. 744To request a report from every optimization pass, you should use 745`-Rpass=.*` (in fact, you can use any valid POSIX regular 746expression). However, do not expect a report from every transformation 747made by the compiler. Optimization remarks do not really make sense 748outside of the major transformations (e.g., inlining, vectorization, 749loop optimizations) and not every optimization pass supports this 750feature. 751 752Note that when using profile-guided optimization information, profile hotness 753information can be included in the remarks (see 754:ref:`-fdiagnostics-show-hotness <opt_fdiagnostics-show-hotness>`). 755 756Current limitations 757^^^^^^^^^^^^^^^^^^^ 758 7591. Optimization remarks that refer to function names will display the 760 mangled name of the function. Since these remarks are emitted by the 761 back end of the compiler, it does not know anything about the input 762 language, nor its mangling rules. 763 7642. Some source locations are not displayed correctly. The front end has 765 a more detailed source location tracking than the locations included 766 in the debug info (e.g., the front end can locate code inside macro 767 expansions). However, the locations used by `-Rpass` are 768 translated from debug annotations. That translation can be lossy, 769 which results in some remarks having no location information. 770 771Options to Emit Resource Consumption Reports 772-------------------------------------------- 773 774These are options that report execution time and consumed memory of different 775compilations steps. 776 777.. option:: -fproc-stat-report= 778 779 This option requests driver to print used memory and execution time of each 780 compilation step. The ``clang`` driver during execution calls different tools, 781 like compiler, assembler, linker etc. With this option the driver reports 782 total execution time, the execution time spent in user mode and peak memory 783 usage of each the called tool. Value of the option specifies where the report 784 is sent to. If it specifies a regular file, the data are saved to this file in 785 CSV format: 786 787 .. code-block:: console 788 789 $ clang -fproc-stat-report=abc foo.c 790 $ cat abc 791 clang-11,"/tmp/foo-123456.o",92000,84000,87536 792 ld,"a.out",900,8000,53568 793 794 The data on each row represent: 795 796 * file name of the tool executable, 797 * output file name in quotes, 798 * total execution time in microseconds, 799 * execution time in user mode in microseconds, 800 * peak memory usage in Kb. 801 802 It is possible to specify this option without any value. In this case statistics 803 are printed on standard output in human readable format: 804 805 .. code-block:: console 806 807 $ clang -fproc-stat-report foo.c 808 clang-11: output=/tmp/foo-855a8e.o, total=68.000 ms, user=60.000 ms, mem=86920 Kb 809 ld: output=a.out, total=8.000 ms, user=4.000 ms, mem=52320 Kb 810 811 The report file specified in the option is locked for write, so this option 812 can be used to collect statistics in parallel builds. The report file is not 813 cleared, new data is appended to it, thus making posible to accumulate build 814 statistics. 815 816 You can also use environment variables to control the process statistics reporting. 817 Setting ``CC_PRINT_PROC_STAT`` to ``1`` enables the feature, the report goes to 818 stdout in human readable format. 819 Setting ``CC_PRINT_PROC_STAT_FILE`` to a fully qualified file path makes it report 820 process statistics to the given file in the CSV format. Specifying a relative 821 path will likely lead to multiple files with the same name created in different 822 directories, since the path is relative to a changing working directory. 823 824 These environment variables are handy when you need to request the statistics 825 report without changing your build scripts or alter the existing set of compiler 826 options. Note that ``-fproc-stat-report`` take precedence over ``CC_PRINT_PROC_STAT`` 827 and ``CC_PRINT_PROC_STAT_FILE``. 828 829 .. code-block:: console 830 831 $ export CC_PRINT_PROC_STAT=1 832 $ export CC_PRINT_PROC_STAT_FILE=~/project-build-proc-stat.csv 833 $ make 834 835Other Options 836------------- 837Clang options that don't fit neatly into other categories. 838 839.. option:: -fgnuc-version= 840 841 This flag controls the value of ``__GNUC__`` and related macros. This flag 842 does not enable or disable any GCC extensions implemented in Clang. Setting 843 the version to zero causes Clang to leave ``__GNUC__`` and other 844 GNU-namespaced macros, such as ``__GXX_WEAK__``, undefined. 845 846.. option:: -MV 847 848 When emitting a dependency file, use formatting conventions appropriate 849 for NMake or Jom. Ignored unless another option causes Clang to emit a 850 dependency file. 851 852 When Clang emits a dependency file (e.g., you supplied the -M option) 853 most filenames can be written to the file without any special formatting. 854 Different Make tools will treat different sets of characters as "special" 855 and use different conventions for telling the Make tool that the character 856 is actually part of the filename. Normally Clang uses backslash to "escape" 857 a special character, which is the convention used by GNU Make. The -MV 858 option tells Clang to put double-quotes around the entire filename, which 859 is the convention used by NMake and Jom. 860 861.. option:: -femit-dwarf-unwind=<value> 862 863 When to emit DWARF unwind (EH frame) info. This is a Mach-O-specific option. 864 865 Valid values are: 866 867 * ``no-compact-unwind`` - Only emit DWARF unwind when compact unwind encodings 868 aren't available. This is the default for arm64. 869 * ``always`` - Always emit DWARF unwind regardless. 870 * ``default`` - Use the platform-specific default (``always`` for all 871 non-arm64-platforms). 872 873 ``no-compact-unwind`` is a performance optimization -- Clang will emit smaller 874 object files that are more quickly processed by the linker. This may cause 875 binary compatibility issues on older x86_64 targets, however, so use it with 876 caution. 877 878.. _configuration-files: 879 880Configuration files 881------------------- 882 883Configuration files group command-line options and allow all of them to be 884specified just by referencing the configuration file. They may be used, for 885example, to collect options required to tune compilation for particular 886target, such as ``-L``, ``-I``, ``-l``, ``--sysroot``, codegen options, etc. 887 888Configuration files can be either specified on the command line or loaded 889from default locations. If both variants are present, the default configuration 890files are loaded first. 891 892The command line option ``--config=`` can be used to specify explicit 893configuration files in a Clang invocation. If the option is used multiple times, 894all specified files are loaded, in order. For example: 895 896:: 897 898 clang --config=/home/user/cfgs/testing.txt 899 clang --config=debug.cfg --config=runtimes.cfg 900 901If the provided argument contains a directory separator, it is considered as 902a file path, and options are read from that file. Otherwise the argument is 903treated as a file name and is searched for sequentially in the directories: 904 905 - user directory, 906 - system directory, 907 - the directory where Clang executable resides. 908 909Both user and system directories for configuration files are specified during 910clang build using CMake parameters, ``CLANG_CONFIG_FILE_USER_DIR`` and 911``CLANG_CONFIG_FILE_SYSTEM_DIR`` respectively. The first file found is used. 912It is an error if the required file cannot be found. 913 914The default configuration files are searched for in the same directories 915following the rules described in the next paragraphs. Loading default 916configuration files can be disabled entirely via passing 917the ``--no-default-config`` flag. 918 919First, the algorithm searches for a configuration file named 920``<triple>-<driver>.cfg`` where `triple` is the triple for the target being 921built for, and `driver` is the name of the currently used driver. The algorithm 922first attempts to use the canonical name for the driver used, then falls back 923to the one found in the executable name. 924 925The following canonical driver names are used: 926 927- ``clang`` for the ``gcc`` driver (used to compile C programs) 928- ``clang++`` for the ``gxx`` driver (used to compile C++ programs) 929- ``clang-cpp`` for the ``cpp`` driver (pure preprocessor) 930- ``clang-cl`` for the ``cl`` driver 931- ``flang`` for the ``flang`` driver 932- ``clang-dxc`` for the ``dxc`` driver 933 934For example, when calling ``x86_64-pc-linux-gnu-clang-g++``, 935the driver will first attempt to use the configuration file named:: 936 937 x86_64-pc-linux-gnu-clang++.cfg 938 939If this file is not found, it will attempt to use the name found 940in the executable instead:: 941 942 x86_64-pc-linux-gnu-clang-g++.cfg 943 944Note that options such as ``--driver-mode=``, ``--target=``, ``-m32`` affect 945the search algorithm. For example, the aforementioned executable called with 946``-m32`` argument will instead search for:: 947 948 i386-pc-linux-gnu-clang++.cfg 949 950If none of the aforementioned files are found, the driver will instead search 951for separate driver and target configuration files and attempt to load both. 952The former is named ``<driver>.cfg`` while the latter is named 953``<triple>.cfg``. Similarly to the previous variants, the canonical driver name 954will be preferred, and the compiler will fall back to the actual name. 955 956For example, ``x86_64-pc-linux-gnu-clang-g++`` will attempt to load two 957configuration files named respectively:: 958 959 clang++.cfg 960 x86_64-pc-linux-gnu.cfg 961 962with fallback to trying:: 963 964 clang-g++.cfg 965 x86_64-pc-linux-gnu.cfg 966 967It is not an error if either of these files is not found. 968 969The configuration file consists of command-line options specified on one or 970more lines. Lines composed of whitespace characters only are ignored as well as 971lines in which the first non-blank character is ``#``. Long options may be split 972between several lines by a trailing backslash. Here is example of a 973configuration file: 974 975:: 976 977 # Several options on line 978 -c --target=x86_64-unknown-linux-gnu 979 980 # Long option split between lines 981 -I/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../\ 982 include/c++/5.4.0 983 984 # other config files may be included 985 @linux.options 986 987Files included by ``@file`` directives in configuration files are resolved 988relative to the including file. For example, if a configuration file 989``~/.llvm/target.cfg`` contains the directive ``@os/linux.opts``, the file 990``linux.opts`` is searched for in the directory ``~/.llvm/os``. Another way to 991include a file content is using the command line option ``--config=``. It works 992similarly but the included file is searched for using the rules for configuration 993files. 994 995To generate paths relative to the configuration file, the ``<CFGDIR>`` token may 996be used. This will expand to the absolute path of the directory containing the 997configuration file. 998 999In cases where a configuration file is deployed alongside SDK contents, the 1000SDK directory can remain fully portable by using ``<CFGDIR>`` prefixed paths. 1001In this way, the user may only need to specify a root configuration file with 1002``--config=`` to establish every aspect of the SDK with the compiler: 1003 1004:: 1005 1006 --target=foo 1007 -isystem <CFGDIR>/include 1008 -L <CFGDIR>/lib 1009 -T <CFGDIR>/ldscripts/link.ld 1010 1011Language and Target-Independent Features 1012======================================== 1013 1014Controlling Errors and Warnings 1015------------------------------- 1016 1017Clang provides a number of ways to control which code constructs cause 1018it to emit errors and warning messages, and how they are displayed to 1019the console. 1020 1021Controlling How Clang Displays Diagnostics 1022^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1023 1024When Clang emits a diagnostic, it includes rich information in the 1025output, and gives you fine-grain control over which information is 1026printed. Clang has the ability to print this information, and these are 1027the options that control it: 1028 1029#. A file/line/column indicator that shows exactly where the diagnostic 1030 occurs in your code [:ref:`-fshow-column <opt_fshow-column>`, 1031 :ref:`-fshow-source-location <opt_fshow-source-location>`]. 1032#. A categorization of the diagnostic as a note, warning, error, or 1033 fatal error. 1034#. A text string that describes what the problem is. 1035#. An option that indicates how to control the diagnostic (for 1036 diagnostics that support it) 1037 [:ref:`-fdiagnostics-show-option <opt_fdiagnostics-show-option>`]. 1038#. A :ref:`high-level category <diagnostics_categories>` for the diagnostic 1039 for clients that want to group diagnostics by class (for diagnostics 1040 that support it) 1041 [:option:`-fdiagnostics-show-category`]. 1042#. The line of source code that the issue occurs on, along with a caret 1043 and ranges that indicate the important locations 1044 [:ref:`-fcaret-diagnostics <opt_fcaret-diagnostics>`]. 1045#. "FixIt" information, which is a concise explanation of how to fix the 1046 problem (when Clang is certain it knows) 1047 [:ref:`-fdiagnostics-fixit-info <opt_fdiagnostics-fixit-info>`]. 1048#. A machine-parsable representation of the ranges involved (off by 1049 default) 1050 [:ref:`-fdiagnostics-print-source-range-info <opt_fdiagnostics-print-source-range-info>`]. 1051 1052For more information please see :ref:`Formatting of 1053Diagnostics <cl_diag_formatting>`. 1054 1055Diagnostic Mappings 1056^^^^^^^^^^^^^^^^^^^ 1057 1058All diagnostics are mapped into one of these 6 classes: 1059 1060- Ignored 1061- Note 1062- Remark 1063- Warning 1064- Error 1065- Fatal 1066 1067.. _diagnostics_categories: 1068 1069Diagnostic Categories 1070^^^^^^^^^^^^^^^^^^^^^ 1071 1072Though not shown by default, diagnostics may each be associated with a 1073high-level category. This category is intended to make it possible to 1074triage builds that produce a large number of errors or warnings in a 1075grouped way. 1076 1077Categories are not shown by default, but they can be turned on with the 1078:option:`-fdiagnostics-show-category` option. 1079When set to "``name``", the category is printed textually in the 1080diagnostic output. When it is set to "``id``", a category number is 1081printed. The mapping of category names to category id's can be obtained 1082by running '``clang --print-diagnostic-categories``'. 1083 1084Controlling Diagnostics via Command Line Flags 1085^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1086 1087TODO: -W flags, -pedantic, etc 1088 1089.. _pragma_gcc_diagnostic: 1090 1091Controlling Diagnostics via Pragmas 1092^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1093 1094Clang can also control what diagnostics are enabled through the use of 1095pragmas in the source code. This is useful for turning off specific 1096warnings in a section of source code. Clang supports GCC's pragma for 1097compatibility with existing source code, as well as several extensions. 1098 1099The pragma may control any warning that can be used from the command 1100line. Warnings may be set to ignored, warning, error, or fatal. The 1101following example code will tell Clang or GCC to ignore the -Wall 1102warnings: 1103 1104.. code-block:: c 1105 1106 #pragma GCC diagnostic ignored "-Wall" 1107 1108In addition to all of the functionality provided by GCC's pragma, Clang 1109also allows you to push and pop the current warning state. This is 1110particularly useful when writing a header file that will be compiled by 1111other people, because you don't know what warning flags they build with. 1112 1113In the below example :option:`-Wextra-tokens` is ignored for only a single line 1114of code, after which the diagnostics return to whatever state had previously 1115existed. 1116 1117.. code-block:: c 1118 1119 #if foo 1120 #endif foo // warning: extra tokens at end of #endif directive 1121 1122 #pragma clang diagnostic push 1123 #pragma clang diagnostic ignored "-Wextra-tokens" 1124 1125 #if foo 1126 #endif foo // no warning 1127 1128 #pragma clang diagnostic pop 1129 1130The push and pop pragmas will save and restore the full diagnostic state 1131of the compiler, regardless of how it was set. That means that it is 1132possible to use push and pop around GCC compatible diagnostics and Clang 1133will push and pop them appropriately, while GCC will ignore the pushes 1134and pops as unknown pragmas. It should be noted that while Clang 1135supports the GCC pragma, Clang and GCC do not support the exact same set 1136of warnings, so even when using GCC compatible #pragmas there is no 1137guarantee that they will have identical behaviour on both compilers. 1138 1139In addition to controlling warnings and errors generated by the compiler, it is 1140possible to generate custom warning and error messages through the following 1141pragmas: 1142 1143.. code-block:: c 1144 1145 // The following will produce warning messages 1146 #pragma message "some diagnostic message" 1147 #pragma GCC warning "TODO: replace deprecated feature" 1148 1149 // The following will produce an error message 1150 #pragma GCC error "Not supported" 1151 1152These pragmas operate similarly to the ``#warning`` and ``#error`` preprocessor 1153directives, except that they may also be embedded into preprocessor macros via 1154the C99 ``_Pragma`` operator, for example: 1155 1156.. code-block:: c 1157 1158 #define STR(X) #X 1159 #define DEFER(M,...) M(__VA_ARGS__) 1160 #define CUSTOM_ERROR(X) _Pragma(STR(GCC error(X " at line " DEFER(STR,__LINE__)))) 1161 1162 CUSTOM_ERROR("Feature not available"); 1163 1164Controlling Diagnostics in System Headers 1165^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1166 1167Warnings are suppressed when they occur in system headers. By default, 1168an included file is treated as a system header if it is found in an 1169include path specified by ``-isystem``, but this can be overridden in 1170several ways. 1171 1172The ``system_header`` pragma can be used to mark the current file as 1173being a system header. No warnings will be produced from the location of 1174the pragma onwards within the same file. 1175 1176.. code-block:: c 1177 1178 #if foo 1179 #endif foo // warning: extra tokens at end of #endif directive 1180 1181 #pragma clang system_header 1182 1183 #if foo 1184 #endif foo // no warning 1185 1186The `--system-header-prefix=` and `--no-system-header-prefix=` 1187command-line arguments can be used to override whether subsets of an include 1188path are treated as system headers. When the name in a ``#include`` directive 1189is found within a header search path and starts with a system prefix, the 1190header is treated as a system header. The last prefix on the 1191command-line which matches the specified header name takes precedence. 1192For instance: 1193 1194.. code-block:: console 1195 1196 $ clang -Ifoo -isystem bar --system-header-prefix=x/ \ 1197 --no-system-header-prefix=x/y/ 1198 1199Here, ``#include "x/a.h"`` is treated as including a system header, even 1200if the header is found in ``foo``, and ``#include "x/y/b.h"`` is treated 1201as not including a system header, even if the header is found in 1202``bar``. 1203 1204A ``#include`` directive which finds a file relative to the current 1205directory is treated as including a system header if the including file 1206is treated as a system header. 1207 1208Controlling Deprecation Diagnostics in Clang-Provided C Runtime Headers 1209^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1210 1211Clang is responsible for providing some of the C runtime headers that cannot be 1212provided by a platform CRT, such as implementation limits or when compiling in 1213freestanding mode. Define the ``_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS`` macro 1214prior to including such a C runtime header to disable the deprecation warnings. 1215Note that the C Standard Library headers are allowed to transitively include 1216other standard library headers (see 7.1.2p5), and so the most appropriate use 1217of this macro is to set it within the build system using ``-D`` or before any 1218include directives in the translation unit. 1219 1220.. code-block:: c 1221 1222 #define _CLANG_DISABLE_CRT_DEPRECATION_WARNINGS 1223 #include <stdint.h> // Clang CRT deprecation warnings are disabled. 1224 #include <stdatomic.h> // Clang CRT deprecation warnings are disabled. 1225 1226.. _diagnostics_enable_everything: 1227 1228Enabling All Diagnostics 1229^^^^^^^^^^^^^^^^^^^^^^^^ 1230 1231In addition to the traditional ``-W`` flags, one can enable **all** diagnostics 1232by passing :option:`-Weverything`. This works as expected with 1233:option:`-Werror`, and also includes the warnings from :option:`-pedantic`. Some 1234diagnostics contradict each other, therefore, users of :option:`-Weverything` 1235often disable many diagnostics such as `-Wno-c++98-compat` and `-Wno-c++-compat` 1236because they contradict recent C++ standards. 1237 1238Since :option:`-Weverything` enables every diagnostic, we generally don't 1239recommend using it. `-Wall` `-Wextra` are a better choice for most projects. 1240Using :option:`-Weverything` means that updating your compiler is more difficult 1241because you're exposed to experimental diagnostics which might be of lower 1242quality than the default ones. If you do use :option:`-Weverything` then we 1243advise that you address all new compiler diagnostics as they get added to Clang, 1244either by fixing everything they find or explicitly disabling that diagnostic 1245with its corresponding `Wno-` option. 1246 1247Note that when combined with :option:`-w` (which disables all warnings), 1248disabling all warnings wins. 1249 1250Controlling Static Analyzer Diagnostics 1251^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1252 1253While not strictly part of the compiler, the diagnostics from Clang's 1254`static analyzer <https://clang-analyzer.llvm.org>`_ can also be 1255influenced by the user via changes to the source code. See the available 1256`annotations <https://clang-analyzer.llvm.org/annotations.html>`_ and the 1257analyzer's `FAQ 1258page <https://clang-analyzer.llvm.org/faq.html#exclude_code>`_ for more 1259information. 1260 1261.. _usersmanual-precompiled-headers: 1262 1263Precompiled Headers 1264------------------- 1265 1266`Precompiled headers <https://en.wikipedia.org/wiki/Precompiled_header>`_ 1267are a general approach employed by many compilers to reduce compilation 1268time. The underlying motivation of the approach is that it is common for 1269the same (and often large) header files to be included by multiple 1270source files. Consequently, compile times can often be greatly improved 1271by caching some of the (redundant) work done by a compiler to process 1272headers. Precompiled header files, which represent one of many ways to 1273implement this optimization, are literally files that represent an 1274on-disk cache that contains the vital information necessary to reduce 1275some of the work needed to process a corresponding header file. While 1276details of precompiled headers vary between compilers, precompiled 1277headers have been shown to be highly effective at speeding up program 1278compilation on systems with very large system headers (e.g., macOS). 1279 1280Generating a PCH File 1281^^^^^^^^^^^^^^^^^^^^^ 1282 1283To generate a PCH file using Clang, one invokes Clang with the 1284`-x <language>-header` option. This mirrors the interface in GCC 1285for generating PCH files: 1286 1287.. code-block:: console 1288 1289 $ gcc -x c-header test.h -o test.h.gch 1290 $ clang -x c-header test.h -o test.h.pch 1291 1292Using a PCH File 1293^^^^^^^^^^^^^^^^ 1294 1295A PCH file can then be used as a prefix header when a ``-include-pch`` 1296option is passed to ``clang``: 1297 1298.. code-block:: console 1299 1300 $ clang -include-pch test.h.pch test.c -o test 1301 1302The ``clang`` driver will check if the PCH file ``test.h.pch`` is 1303available; if so, the contents of ``test.h`` (and the files it includes) 1304will be processed from the PCH file. Otherwise, Clang will report an error. 1305 1306.. note:: 1307 1308 Clang does *not* automatically use PCH files for headers that are directly 1309 included within a source file or indirectly via :option:`-include`. 1310 For example: 1311 1312 .. code-block:: console 1313 1314 $ clang -x c-header test.h -o test.h.pch 1315 $ cat test.c 1316 #include "test.h" 1317 $ clang test.c -o test 1318 1319 In this example, ``clang`` will not automatically use the PCH file for 1320 ``test.h`` since ``test.h`` was included directly in the source file and not 1321 specified on the command line using ``-include-pch``. 1322 1323Relocatable PCH Files 1324^^^^^^^^^^^^^^^^^^^^^ 1325 1326It is sometimes necessary to build a precompiled header from headers 1327that are not yet in their final, installed locations. For example, one 1328might build a precompiled header within the build tree that is then 1329meant to be installed alongside the headers. Clang permits the creation 1330of "relocatable" precompiled headers, which are built with a given path 1331(into the build directory) and can later be used from an installed 1332location. 1333 1334To build a relocatable precompiled header, place your headers into a 1335subdirectory whose structure mimics the installed location. For example, 1336if you want to build a precompiled header for the header ``mylib.h`` 1337that will be installed into ``/usr/include``, create a subdirectory 1338``build/usr/include`` and place the header ``mylib.h`` into that 1339subdirectory. If ``mylib.h`` depends on other headers, then they can be 1340stored within ``build/usr/include`` in a way that mimics the installed 1341location. 1342 1343Building a relocatable precompiled header requires two additional 1344arguments. First, pass the ``--relocatable-pch`` flag to indicate that 1345the resulting PCH file should be relocatable. Second, pass 1346``-isysroot /path/to/build``, which makes all includes for your library 1347relative to the build directory. For example: 1348 1349.. code-block:: console 1350 1351 # clang -x c-header --relocatable-pch -isysroot /path/to/build /path/to/build/mylib.h mylib.h.pch 1352 1353When loading the relocatable PCH file, the various headers used in the 1354PCH file are found from the system header root. For example, ``mylib.h`` 1355can be found in ``/usr/include/mylib.h``. If the headers are installed 1356in some other system root, the ``-isysroot`` option can be used provide 1357a different system root from which the headers will be based. For 1358example, ``-isysroot /Developer/SDKs/MacOSX10.4u.sdk`` will look for 1359``mylib.h`` in ``/Developer/SDKs/MacOSX10.4u.sdk/usr/include/mylib.h``. 1360 1361Relocatable precompiled headers are intended to be used in a limited 1362number of cases where the compilation environment is tightly controlled 1363and the precompiled header cannot be generated after headers have been 1364installed. 1365 1366.. _controlling-fp-behavior: 1367 1368Controlling Floating Point Behavior 1369----------------------------------- 1370 1371Clang provides a number of ways to control floating point behavior, including 1372with command line options and source pragmas. This section 1373describes the various floating point semantic modes and the corresponding options. 1374 1375.. csv-table:: Floating Point Semantic Modes 1376 :header: "Mode", "Values" 1377 :widths: 15, 30, 30 1378 1379 "ffp-exception-behavior", "{ignore, strict, maytrap}", 1380 "fenv_access", "{off, on}", "(none)" 1381 "frounding-math", "{dynamic, tonearest, downward, upward, towardzero}" 1382 "ffp-contract", "{on, off, fast, fast-honor-pragmas}" 1383 "fdenormal-fp-math", "{IEEE, PreserveSign, PositiveZero}" 1384 "fdenormal-fp-math-fp32", "{IEEE, PreserveSign, PositiveZero}" 1385 "fmath-errno", "{on, off}" 1386 "fhonor-nans", "{on, off}" 1387 "fhonor-infinities", "{on, off}" 1388 "fsigned-zeros", "{on, off}" 1389 "freciprocal-math", "{on, off}" 1390 "allow_approximate_fns", "{on, off}" 1391 "fassociative-math", "{on, off}" 1392 1393This table describes the option settings that correspond to the three 1394floating point semantic models: precise (the default), strict, and fast. 1395 1396 1397.. csv-table:: Floating Point Models 1398 :header: "Mode", "Precise", "Strict", "Fast" 1399 :widths: 25, 15, 15, 15 1400 1401 "except_behavior", "ignore", "strict", "ignore" 1402 "fenv_access", "off", "on", "off" 1403 "rounding_mode", "tonearest", "dynamic", "tonearest" 1404 "contract", "on", "off", "fast" 1405 "denormal_fp_math", "IEEE", "IEEE", "IEEE" 1406 "denormal_fp32_math", "IEEE","IEEE", "IEEE" 1407 "support_math_errno", "on", "on", "off" 1408 "no_honor_nans", "off", "off", "on" 1409 "no_honor_infinities", "off", "off", "on" 1410 "no_signed_zeros", "off", "off", "on" 1411 "allow_reciprocal", "off", "off", "on" 1412 "allow_approximate_fns", "off", "off", "on" 1413 "allow_reassociation", "off", "off", "on" 1414 1415.. option:: -ffast-math 1416 1417 Enable fast-math mode. This option lets the 1418 compiler make aggressive, potentially-lossy assumptions about 1419 floating-point math. These include: 1420 1421 * Floating-point math obeys regular algebraic rules for real numbers (e.g. 1422 ``+`` and ``*`` are associative, ``x/y == x * (1/y)``, and 1423 ``(a + b) * c == a * c + b * c``), 1424 * Operands to floating-point operations are not equal to ``NaN`` and 1425 ``Inf``, and 1426 * ``+0`` and ``-0`` are interchangeable. 1427 1428 ``-ffast-math`` also defines the ``__FAST_MATH__`` preprocessor 1429 macro. Some math libraries recognize this macro and change their behavior. 1430 With the exception of ``-ffp-contract=fast``, using any of the options 1431 below to disable any of the individual optimizations in ``-ffast-math`` 1432 will cause ``__FAST_MATH__`` to no longer be set. 1433 1434 This option implies: 1435 1436 * ``-fno-honor-infinities`` 1437 1438 * ``-fno-honor-nans`` 1439 1440 * ``-fapprox-func`` 1441 1442 * ``-fno-math-errno`` 1443 1444 * ``-ffinite-math-only`` 1445 1446 * ``-fassociative-math`` 1447 1448 * ``-freciprocal-math`` 1449 1450 * ``-fno-signed-zeros`` 1451 1452 * ``-fno-trapping-math`` 1453 1454 * ``-fno-rounding-math`` 1455 1456 * ``-ffp-contract=fast`` 1457 1458 Note: ``-ffast-math`` causes ``crtfastmath.o`` to be linked with code. See 1459 :ref:`crtfastmath.o` for more details. 1460 1461.. option:: -fno-fast-math 1462 1463 Disable fast-math mode. This options disables unsafe floating-point 1464 optimizations by preventing the compiler from making any transformations that 1465 could affect the results. 1466 1467 This option implies: 1468 1469 * ``-fhonor-infinities`` 1470 1471 * ``-fhonor-nans`` 1472 1473 * ``-fno-approx-func`` 1474 1475 * ``-fno-finite-math-only`` 1476 1477 * ``-fno-associative-math`` 1478 1479 * ``-fno-reciprocal-math`` 1480 1481 * ``-fsigned-zeros`` 1482 1483 * ``-ffp-contract=on`` 1484 1485 Also, this option resets following options to their target-dependent defaults. 1486 1487 * ``-f[no-]math-errno`` 1488 * ``-fdenormal-fp-math=<value>`` 1489 1490 There is ambiguity about how ``-ffp-contract``, ``-ffast-math``, 1491 and ``-fno-fast-math`` behave when combined. To keep the value of 1492 ``-ffp-contract`` consistent, we define this set of rules: 1493 1494 * ``-ffast-math`` sets ``ffp-contract`` to ``fast``. 1495 1496 * ``-fno-fast-math`` sets ``-ffp-contract`` to ``on`` (``fast`` for CUDA and 1497 HIP). 1498 1499 * If ``-ffast-math`` and ``-ffp-contract`` are both seen, but 1500 ``-ffast-math`` is not followed by ``-fno-fast-math``, ``ffp-contract`` 1501 will be given the value of whichever option was last seen. 1502 1503 * If ``-fno-fast-math`` is seen and ``-ffp-contract`` has been seen at least 1504 once, the ``ffp-contract`` will get the value of the last seen value of 1505 ``-ffp-contract``. 1506 1507 * If ``-fno-fast-math`` is seen and ``-ffp-contract`` has not been seen, the 1508 ``-ffp-contract`` setting is determined by the default value of 1509 ``-ffp-contract``. 1510 1511 Note: ``-fno-fast-math`` implies ``-fdenormal-fp-math=ieee``. 1512 ``-fno-fast-math`` causes ``crtfastmath.o`` to not be linked with code. 1513 1514.. option:: -fdenormal-fp-math=<value> 1515 1516 Select which denormal numbers the code is permitted to require. 1517 1518 Valid values are: 1519 1520 * ``ieee`` - IEEE 754 denormal numbers 1521 * ``preserve-sign`` - the sign of a flushed-to-zero number is preserved in the sign of 0 1522 * ``positive-zero`` - denormals are flushed to positive zero 1523 1524 The default value depends on the target. For most targets, defaults to 1525 ``ieee``. 1526 1527.. option:: -f[no-]strict-float-cast-overflow 1528 1529 When a floating-point value is not representable in a destination integer 1530 type, the code has undefined behavior according to the language standard. 1531 By default, Clang will not guarantee any particular result in that case. 1532 With the 'no-strict' option, Clang will saturate towards the smallest and 1533 largest representable integer values instead. NaNs will be converted to zero. 1534 Defaults to ``-fstrict-float-cast-overflow``. 1535 1536.. option:: -f[no-]math-errno 1537 1538 Require math functions to indicate errors by setting errno. 1539 The default varies by ToolChain. ``-fno-math-errno`` allows optimizations 1540 that might cause standard C math functions to not set ``errno``. 1541 For example, on some systems, the math function ``sqrt`` is specified 1542 as setting ``errno`` to ``EDOM`` when the input is negative. On these 1543 systems, the compiler cannot normally optimize a call to ``sqrt`` to use 1544 inline code (e.g. the x86 ``sqrtsd`` instruction) without additional 1545 checking to ensure that ``errno`` is set appropriately. 1546 ``-fno-math-errno`` permits these transformations. 1547 1548 On some targets, math library functions never set ``errno``, and so 1549 ``-fno-math-errno`` is the default. This includes most BSD-derived 1550 systems, including Darwin. 1551 1552.. option:: -f[no-]trapping-math 1553 1554 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. 1555 1556 - The option ``-ftrapping-math`` behaves identically to ``-ffp-exception-behavior=strict``. 1557 - The option ``-fno-trapping-math`` behaves identically to ``-ffp-exception-behavior=ignore``. This is the default. 1558 1559.. option:: -ffp-contract=<value> 1560 1561 Specify when the compiler is permitted to form fused floating-point 1562 operations, such as fused multiply-add (FMA). Fused operations are 1563 permitted to produce more precise results than performing the same 1564 operations separately. 1565 1566 The C standard permits intermediate floating-point results within an 1567 expression to be computed with more precision than their type would 1568 normally allow. This permits operation fusing, and Clang takes advantage 1569 of this by default. This behavior can be controlled with the ``FP_CONTRACT`` 1570 and ``clang fp contract`` pragmas. Please refer to the pragma documentation 1571 for a description of how the pragmas interact with this option. 1572 1573 Valid values are: 1574 1575 * ``fast`` (fuse across statements disregarding pragmas, default for CUDA) 1576 * ``on`` (fuse in the same statement unless dictated by pragmas, default for languages other than CUDA/HIP) 1577 * ``off`` (never fuse) 1578 * ``fast-honor-pragmas`` (fuse across statements unless dictated by pragmas, default for HIP) 1579 1580.. option:: -f[no-]honor-infinities 1581 1582 Allow floating-point optimizations that assume arguments and results are 1583 not +-Inf. 1584 Defaults to ``-fhonor-infinities``. 1585 1586 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used, 1587 has the same effect as specifying ``-ffinite-math-only``. 1588 1589.. option:: -f[no-]honor-nans 1590 1591 Allow floating-point optimizations that assume arguments and results are 1592 not NaNs. 1593 Defaults to ``-fhonor-nans``. 1594 1595 If both ``-fno-honor-infinities`` and ``-fno-honor-nans`` are used, 1596 has the same effect as specifying ``-ffinite-math-only``. 1597 1598.. option:: -f[no-]approx-func 1599 1600 Allow certain math function calls (such as ``log``, ``sqrt``, ``pow``, etc) 1601 to be replaced with an approximately equivalent set of instructions 1602 or alternative math function calls. For example, a ``pow(x, 0.25)`` 1603 may be replaced with ``sqrt(sqrt(x))``, despite being an inexact result 1604 in cases where ``x`` is ``-0.0`` or ``-inf``. 1605 Defaults to ``-fno-approx-func``. 1606 1607.. option:: -f[no-]signed-zeros 1608 1609 Allow optimizations that ignore the sign of floating point zeros. 1610 Defaults to ``-fsigned-zeros``. 1611 1612.. option:: -f[no-]associative-math 1613 1614 Allow floating point operations to be reassociated. 1615 Defaults to ``-fno-associative-math``. 1616 1617.. option:: -f[no-]reciprocal-math 1618 1619 Allow division operations to be transformed into multiplication by a 1620 reciprocal. This can be significantly faster than an ordinary division 1621 but can also have significantly less precision. Defaults to 1622 ``-fno-reciprocal-math``. 1623 1624.. option:: -f[no-]unsafe-math-optimizations 1625 1626 Allow unsafe floating-point optimizations. 1627 ``-funsafe-math-optimizations`` also implies: 1628 1629 * ``-fapprox-func`` 1630 * ``-fassociative-math`` 1631 * ``-freciprocal-math`` 1632 * ``-fno-signed-zeros`` 1633 * ``-fno-trapping-math`` 1634 * ``-ffp-contract=fast`` 1635 1636 ``-fno-unsafe-math-optimizations`` implies: 1637 1638 * ``-fno-approx-func`` 1639 * ``-fno-associative-math`` 1640 * ``-fno-reciprocal-math`` 1641 * ``-fsigned-zeros`` 1642 * ``-ftrapping-math`` 1643 * ``-ffp-contract=on`` 1644 * ``-fdenormal-fp-math=ieee`` 1645 1646 There is ambiguity about how ``-ffp-contract``, 1647 ``-funsafe-math-optimizations``, and ``-fno-unsafe-math-optimizations`` 1648 behave when combined. Explanation in :option:`-fno-fast-math` also applies 1649 to these options. 1650 1651 Defaults to ``-fno-unsafe-math-optimizations``. 1652 1653.. option:: -f[no-]finite-math-only 1654 1655 Allow floating-point optimizations that assume arguments and results are 1656 not NaNs or +-Inf. ``-ffinite-math-only`` defines the 1657 ``__FINITE_MATH_ONLY__`` preprocessor macro. 1658 ``-ffinite-math-only`` implies: 1659 1660 * ``-fno-honor-infinities`` 1661 * ``-fno-honor-nans`` 1662 1663 ``-ffno-inite-math-only`` implies: 1664 1665 * ``-fhonor-infinities`` 1666 * ``-fhonor-nans`` 1667 1668 Defaults to ``-fno-finite-math-only``. 1669 1670.. option:: -f[no-]rounding-math 1671 1672 Force floating-point operations to honor the dynamically-set rounding mode by default. 1673 1674 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. 1675 1676 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``. 1677 1678 - The option ``-fno-rounding-math`` allows the compiler to assume that the rounding mode is set to ``FE_TONEAREST``. This is the default. 1679 - 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. 1680 1681.. option:: -ffp-model=<value> 1682 1683 Specify floating point behavior. ``-ffp-model`` is an umbrella 1684 option that encompasses functionality provided by other, single 1685 purpose, floating point options. Valid values are: ``precise``, ``strict``, 1686 and ``fast``. 1687 Details: 1688 1689 * ``precise`` Disables optimizations that are not value-safe on floating-point data, although FP contraction (FMA) is enabled (``-ffp-contract=on``). This is the default behavior. 1690 * ``strict`` Enables ``-frounding-math`` and ``-ffp-exception-behavior=strict``, and disables contractions (FMA). All of the ``-ffast-math`` enablements are disabled. Enables ``STDC FENV_ACCESS``: by default ``FENV_ACCESS`` is disabled. This option setting behaves as though ``#pragma STDC FENV_ACESS ON`` appeared at the top of the source file. 1691 * ``fast`` Behaves identically to specifying both ``-ffast-math`` and ``ffp-contract=fast`` 1692 1693 Note: If your command line specifies multiple instances 1694 of the ``-ffp-model`` option, or if your command line option specifies 1695 ``-ffp-model`` and later on the command line selects a floating point 1696 option that has the effect of negating part of the ``ffp-model`` that 1697 has been selected, then the compiler will issue a diagnostic warning 1698 that the override has occurred. 1699 1700.. option:: -ffp-exception-behavior=<value> 1701 1702 Specify the floating-point exception behavior. 1703 1704 Valid values are: ``ignore``, ``maytrap``, and ``strict``. 1705 The default value is ``ignore``. Details: 1706 1707 * ``ignore`` The compiler assumes that the exception status flags will not be read and that floating point exceptions will be masked. 1708 * ``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. 1709 * ``strict`` The compiler ensures that all transformations strictly preserve the floating point exception semantics of the original code. 1710 1711.. option:: -ffp-eval-method=<value> 1712 1713 Specify the floating-point evaluation method for intermediate results within 1714 a single expression of the code. 1715 1716 Valid values are: ``source``, ``double``, and ``extended``. 1717 For 64-bit targets, the default value is ``source``. For 32-bit x86 targets 1718 however, in the case of NETBSD 6.99.26 and under, the default value is 1719 ``double``; in the case of NETBSD greater than 6.99.26, with NoSSE, the 1720 default value is ``extended``, with SSE the default value is ``source``. 1721 Details: 1722 1723 * ``source`` The compiler uses the floating-point type declared in the source program as the evaluation method. 1724 * ``double`` The compiler uses ``double`` as the floating-point evaluation method for all float expressions of type that is narrower than ``double``. 1725 * ``extended`` The compiler uses ``long double`` as the floating-point evaluation method for all float expressions of type that is narrower than ``long double``. 1726 1727.. option:: -f[no-]protect-parens 1728 1729 This option pertains to floating-point types, complex types with 1730 floating-point components, and vectors of these types. Some arithmetic 1731 expression transformations that are mathematically correct and permissible 1732 according to the C and C++ language standards may be incorrect when dealing 1733 with floating-point types, such as reassociation and distribution. Further, 1734 the optimizer may ignore parentheses when computing arithmetic expressions 1735 in circumstances where the parenthesized and unparenthesized expression 1736 express the same mathematical value. For example (a+b)+c is the same 1737 mathematical value as a+(b+c), but the optimizer is free to evaluate the 1738 additions in any order regardless of the parentheses. When enabled, this 1739 option forces the optimizer to honor the order of operations with respect 1740 to parentheses in all circumstances. 1741 Defaults to ``-fno-protect-parens``. 1742 1743 Note that floating-point contraction (option `-ffp-contract=`) is disabled 1744 when `-fprotect-parens` is enabled. Also note that in safe floating-point 1745 modes, such as `-ffp-model=precise` or `-ffp-model=strict`, this option 1746 has no effect because the optimizer is prohibited from making unsafe 1747 transformations. 1748 1749.. option:: -fexcess-precision: 1750 1751 The C and C++ standards allow floating-point expressions to be computed as if 1752 intermediate results had more precision (and/or a wider range) than the type 1753 of the expression strictly allows. This is called excess precision 1754 arithmetic. 1755 Excess precision arithmetic can improve the accuracy of results (although not 1756 always), and it can make computation significantly faster if the target lacks 1757 direct hardware support for arithmetic in a particular type. However, it can 1758 also undermine strict floating-point reproducibility. 1759 1760 Under the standards, assignments and explicit casts force the operand to be 1761 converted to its formal type, discarding any excess precision. Because data 1762 can only flow between statements via an assignment, this means that the use 1763 of excess precision arithmetic is a reliable local property of a single 1764 statement, and results do not change based on optimization. However, when 1765 excess precision arithmetic is in use, Clang does not guarantee strict 1766 reproducibility, and future compiler releases may recognize more 1767 opportunities to use excess precision arithmetic, e.g. with floating-point 1768 builtins. 1769 1770 Clang does not use excess precision arithmetic for most types or on most 1771 targets. For example, even on pre-SSE X86 targets where ``float`` and 1772 ``double`` computations must be performed in the 80-bit X87 format, Clang 1773 rounds all intermediate results correctly for their type. Clang currently 1774 uses excess precision arithmetic by default only for the following types and 1775 targets: 1776 1777 * ``_Float16`` on X86 targets without ``AVX512-FP16``. 1778 1779 The ``-fexcess-precision=<value>`` option can be used to control the use of 1780 excess precision arithmetic. Valid values are: 1781 1782 * ``standard`` - The default. Allow the use of excess precision arithmetic 1783 under the constraints of the C and C++ standards. Has no effect except on 1784 the types and targets listed above. 1785 * ``fast`` - Accepted for GCC compatibility, but currently treated as an 1786 alias for ``standard``. 1787 * ``16`` - Forces ``_Float16`` operations to be emitted without using excess 1788 precision arithmetic. 1789 1790.. _crtfastmath.o: 1791 1792A note about ``crtfastmath.o`` 1793^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1794``-ffast-math`` and ``-funsafe-math-optimizations`` cause ``crtfastmath.o`` to be 1795automatically linked, which adds a static constructor that sets the FTZ/DAZ 1796bits in MXCSR, affecting not only the current compilation unit but all static 1797and shared libraries included in the program. 1798 1799.. _FLT_EVAL_METHOD: 1800 1801A note about ``__FLT_EVAL_METHOD__`` 1802^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1803The ``__FLT_EVAL_METHOD__`` is not defined as a traditional macro, and so it 1804will not appear when dumping preprocessor macros. Instead, the value 1805``__FLT_EVAL_METHOD__`` expands to is determined at the point of expansion 1806either from the value set by the ``-ffp-eval-method`` command line option or 1807from the target. This is because the ``__FLT_EVAL_METHOD__`` macro 1808cannot expand to the correct evaluation method in the presence of a ``#pragma`` 1809which alters the evaluation method. An error is issued if 1810``__FLT_EVAL_METHOD__`` is expanded inside a scope modified by 1811``#pragma clang fp eval_method``. 1812 1813.. _fp-constant-eval: 1814 1815A note about Floating Point Constant Evaluation 1816^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1817 1818In C, the only place floating point operations are guaranteed to be evaluated 1819during translation is in the initializers of variables of static storage 1820duration, which are all notionally initialized before the program begins 1821executing (and thus before a non-default floating point environment can be 1822entered). But C++ has many more contexts where floating point constant 1823evaluation occurs. Specifically: for static/thread-local variables, 1824first try evaluating the initializer in a constant context, including in the 1825constant floating point environment (just like in C), and then, if that fails, 1826fall back to emitting runtime code to perform the initialization (which might 1827in general be in a different floating point environment). 1828 1829Consider this example when compiled with ``-frounding-math`` 1830 1831 .. code-block:: console 1832 1833 constexpr float func_01(float x, float y) { 1834 return x + y; 1835 } 1836 float V1 = func_01(1.0F, 0x0.000001p0F); 1837 1838The C++ rule is that initializers for static storage duration variables are 1839first evaluated during translation (therefore, in the default rounding mode), 1840and only evaluated at runtime (and therefore in the runtime rounding mode) if 1841the compile-time evaluation fails. This is in line with the C rules; 1842C11 F.8.5 says: *All computation for automatic initialization is done (as if) 1843at execution time; thus, it is affected by any operative modes and raises 1844floating-point exceptions as required by IEC 60559 (provided the state for the 1845FENV_ACCESS pragma is ‘‘on’’). All computation for initialization of objects 1846that have static or thread storage duration is done (as if) at translation 1847time.* C++ generalizes this by adding another phase of initialization 1848(at runtime) if the translation-time initialization fails, but the 1849translation-time evaluation of the initializer of succeeds, it will be 1850treated as a constant initializer. 1851 1852 1853.. _controlling-code-generation: 1854 1855Controlling Code Generation 1856--------------------------- 1857 1858Clang provides a number of ways to control code generation. The options 1859are listed below. 1860 1861.. option:: -f[no-]sanitize=check1,check2,... 1862 1863 Turn on runtime checks for various forms of undefined or suspicious 1864 behavior. 1865 1866 This option controls whether Clang adds runtime checks for various 1867 forms of undefined or suspicious behavior, and is disabled by 1868 default. If a check fails, a diagnostic message is produced at 1869 runtime explaining the problem. The main checks are: 1870 1871 - .. _opt_fsanitize_address: 1872 1873 ``-fsanitize=address``: 1874 :doc:`AddressSanitizer`, a memory error 1875 detector. 1876 - .. _opt_fsanitize_thread: 1877 1878 ``-fsanitize=thread``: :doc:`ThreadSanitizer`, a data race detector. 1879 - .. _opt_fsanitize_memory: 1880 1881 ``-fsanitize=memory``: :doc:`MemorySanitizer`, 1882 a detector of uninitialized reads. Requires instrumentation of all 1883 program code. 1884 - .. _opt_fsanitize_undefined: 1885 1886 ``-fsanitize=undefined``: :doc:`UndefinedBehaviorSanitizer`, 1887 a fast and compatible undefined behavior checker. 1888 1889 - ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data 1890 flow analysis. 1891 - ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>` 1892 checks. Requires ``-flto``. 1893 - ``-fsanitize=kcfi``: kernel indirect call forward-edge control flow 1894 integrity. 1895 - ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>` 1896 protection against stack-based memory corruption errors. 1897 1898 There are more fine-grained checks available: see 1899 the :ref:`list <ubsan-checks>` of specific kinds of 1900 undefined behavior that can be detected and the :ref:`list <cfi-schemes>` 1901 of control flow integrity schemes. 1902 1903 The ``-fsanitize=`` argument must also be provided when linking, in 1904 order to link to the appropriate runtime library. 1905 1906 It is not possible to combine more than one of the ``-fsanitize=address``, 1907 ``-fsanitize=thread``, and ``-fsanitize=memory`` checkers in the same 1908 program. 1909 1910.. option:: -f[no-]sanitize-recover=check1,check2,... 1911 1912.. option:: -f[no-]sanitize-recover[=all] 1913 1914 Controls which checks enabled by ``-fsanitize=`` flag are non-fatal. 1915 If the check is fatal, program will halt after the first error 1916 of this kind is detected and error report is printed. 1917 1918 By default, non-fatal checks are those enabled by 1919 :doc:`UndefinedBehaviorSanitizer`, 1920 except for ``-fsanitize=return`` and ``-fsanitize=unreachable``. Some 1921 sanitizers may not support recovery (or not support it by default 1922 e.g. :doc:`AddressSanitizer`), and always crash the program after the issue 1923 is detected. 1924 1925 Note that the ``-fsanitize-trap`` flag has precedence over this flag. 1926 This means that if a check has been configured to trap elsewhere on the 1927 command line, or if the check traps by default, this flag will not have 1928 any effect unless that sanitizer's trapping behavior is disabled with 1929 ``-fno-sanitize-trap``. 1930 1931 For example, if a command line contains the flags ``-fsanitize=undefined 1932 -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment`` 1933 will have no effect on its own; it will need to be accompanied by 1934 ``-fno-sanitize-trap=alignment``. 1935 1936.. option:: -f[no-]sanitize-trap=check1,check2,... 1937 1938.. option:: -f[no-]sanitize-trap[=all] 1939 1940 Controls which checks enabled by the ``-fsanitize=`` flag trap. This 1941 option is intended for use in cases where the sanitizer runtime cannot 1942 be used (for instance, when building libc or a kernel module), or where 1943 the binary size increase caused by the sanitizer runtime is a concern. 1944 1945 This flag is only compatible with :doc:`control flow integrity 1946 <ControlFlowIntegrity>` schemes and :doc:`UndefinedBehaviorSanitizer` 1947 checks other than ``vptr``. 1948 1949 This flag is enabled by default for sanitizers in the ``cfi`` group. 1950 1951.. option:: -fsanitize-ignorelist=/path/to/ignorelist/file 1952 1953 Disable or modify sanitizer checks for objects (source files, functions, 1954 variables, types) listed in the file. See 1955 :doc:`SanitizerSpecialCaseList` for file format description. 1956 1957.. option:: -fno-sanitize-ignorelist 1958 1959 Don't use ignorelist file, if it was specified earlier in the command line. 1960 1961.. option:: -f[no-]sanitize-coverage=[type,features,...] 1962 1963 Enable simple code coverage in addition to certain sanitizers. 1964 See :doc:`SanitizerCoverage` for more details. 1965 1966.. option:: -f[no-]sanitize-address-outline-instrumentation 1967 1968 Controls how address sanitizer code is generated. If enabled will always use 1969 a function call instead of inlining the code. Turning this option on could 1970 reduce the binary size, but might result in a worse run-time performance. 1971 1972 See :doc: `AddressSanitizer` for more details. 1973 1974.. option:: -f[no-]sanitize-stats 1975 1976 Enable simple statistics gathering for the enabled sanitizers. 1977 See :doc:`SanitizerStats` for more details. 1978 1979.. option:: -fsanitize-undefined-trap-on-error 1980 1981 Deprecated alias for ``-fsanitize-trap=undefined``. 1982 1983.. option:: -fsanitize-cfi-cross-dso 1984 1985 Enable cross-DSO control flow integrity checks. This flag modifies 1986 the behavior of sanitizers in the ``cfi`` group to allow checking 1987 of cross-DSO virtual and indirect calls. 1988 1989.. option:: -fsanitize-cfi-icall-generalize-pointers 1990 1991 Generalize pointers in return and argument types in function type signatures 1992 checked by Control Flow Integrity indirect call checking. See 1993 :doc:`ControlFlowIntegrity` for more details. 1994 1995.. option:: -fstrict-vtable-pointers 1996 1997 Enable optimizations based on the strict rules for overwriting polymorphic 1998 C++ objects, i.e. the vptr is invariant during an object's lifetime. 1999 This enables better devirtualization. Turned off by default, because it is 2000 still experimental. 2001 2002.. option:: -fwhole-program-vtables 2003 2004 Enable whole-program vtable optimizations, such as single-implementation 2005 devirtualization and virtual constant propagation, for classes with 2006 :doc:`hidden LTO visibility <LTOVisibility>`. Requires ``-flto``. 2007 2008.. option:: -fforce-emit-vtables 2009 2010 In order to improve devirtualization, forces emitting of vtables even in 2011 modules where it isn't necessary. It causes more inline virtual functions 2012 to be emitted. 2013 2014.. option:: -fno-assume-sane-operator-new 2015 2016 Don't assume that the C++'s new operator is sane. 2017 2018 This option tells the compiler to do not assume that C++'s global 2019 new operator will always return a pointer that does not alias any 2020 other pointer when the function returns. 2021 2022.. option:: -ftrap-function=[name] 2023 2024 Instruct code generator to emit a function call to the specified 2025 function name for ``__builtin_trap()``. 2026 2027 LLVM code generator translates ``__builtin_trap()`` to a trap 2028 instruction if it is supported by the target ISA. Otherwise, the 2029 builtin is translated into a call to ``abort``. If this option is 2030 set, then the code generator will always lower the builtin to a call 2031 to the specified function regardless of whether the target ISA has a 2032 trap instruction. This option is useful for environments (e.g. 2033 deeply embedded) where a trap cannot be properly handled, or when 2034 some custom behavior is desired. 2035 2036.. option:: -ftls-model=[model] 2037 2038 Select which TLS model to use. 2039 2040 Valid values are: ``global-dynamic``, ``local-dynamic``, 2041 ``initial-exec`` and ``local-exec``. The default value is 2042 ``global-dynamic``. The compiler may use a different model if the 2043 selected model is not supported by the target, or if a more 2044 efficient model can be used. The TLS model can be overridden per 2045 variable using the ``tls_model`` attribute. 2046 2047.. option:: -femulated-tls 2048 2049 Select emulated TLS model, which overrides all -ftls-model choices. 2050 2051 In emulated TLS mode, all access to TLS variables are converted to 2052 calls to __emutls_get_address in the runtime library. 2053 2054.. option:: -mhwdiv=[values] 2055 2056 Select the ARM modes (arm or thumb) that support hardware division 2057 instructions. 2058 2059 Valid values are: ``arm``, ``thumb`` and ``arm,thumb``. 2060 This option is used to indicate which mode (arm or thumb) supports 2061 hardware division instructions. This only applies to the ARM 2062 architecture. 2063 2064.. option:: -m[no-]crc 2065 2066 Enable or disable CRC instructions. 2067 2068 This option is used to indicate whether CRC instructions are to 2069 be generated. This only applies to the ARM architecture. 2070 2071 CRC instructions are enabled by default on ARMv8. 2072 2073.. option:: -mgeneral-regs-only 2074 2075 Generate code which only uses the general purpose registers. 2076 2077 This option restricts the generated code to use general registers 2078 only. This only applies to the AArch64 architecture. 2079 2080.. option:: -mcompact-branches=[values] 2081 2082 Control the usage of compact branches for MIPSR6. 2083 2084 Valid values are: ``never``, ``optimal`` and ``always``. 2085 The default value is ``optimal`` which generates compact branches 2086 when a delay slot cannot be filled. ``never`` disables the usage of 2087 compact branches and ``always`` generates compact branches whenever 2088 possible. 2089 2090.. option:: -f[no-]max-type-align=[number] 2091 2092 Instruct the code generator to not enforce a higher alignment than the given 2093 number (of bytes) when accessing memory via an opaque pointer or reference. 2094 This cap is ignored when directly accessing a variable or when the pointee 2095 type has an explicit “aligned” attribute. 2096 2097 The value should usually be determined by the properties of the system allocator. 2098 Some builtin types, especially vector types, have very high natural alignments; 2099 when working with values of those types, Clang usually wants to use instructions 2100 that take advantage of that alignment. However, many system allocators do 2101 not promise to return memory that is more than 8-byte or 16-byte-aligned. Use 2102 this option to limit the alignment that the compiler can assume for an arbitrary 2103 pointer, which may point onto the heap. 2104 2105 This option does not affect the ABI alignment of types; the layout of structs and 2106 unions and the value returned by the alignof operator remain the same. 2107 2108 This option can be overridden on a case-by-case basis by putting an explicit 2109 “aligned” alignment on a struct, union, or typedef. For example: 2110 2111 .. code-block:: console 2112 2113 #include <immintrin.h> 2114 // Make an aligned typedef of the AVX-512 16-int vector type. 2115 typedef __v16si __aligned_v16si __attribute__((aligned(64))); 2116 2117 void initialize_vector(__aligned_v16si *v) { 2118 // The compiler may assume that ‘v’ is 64-byte aligned, regardless of the 2119 // value of -fmax-type-align. 2120 } 2121 2122.. option:: -faddrsig, -fno-addrsig 2123 2124 Controls whether Clang emits an address-significance table into the object 2125 file. Address-significance tables allow linkers to implement `safe ICF 2126 <https://research.google.com/pubs/archive/36912.pdf>`_ without the false 2127 positives that can result from other implementation techniques such as 2128 relocation scanning. Address-significance tables are enabled by default 2129 on ELF targets when using the integrated assembler. This flag currently 2130 only has an effect on ELF targets. 2131 2132.. option:: -f[no]-unique-internal-linkage-names 2133 2134 Controls whether Clang emits a unique (best-effort) symbol name for internal 2135 linkage symbols. When this option is set, compiler hashes the main source 2136 file path from the command line and appends it to all internal symbols. If a 2137 program contains multiple objects compiled with the same command-line source 2138 file path, the symbols are not guaranteed to be unique. This option is 2139 particularly useful in attributing profile information to the correct 2140 function when multiple functions with the same private linkage name exist 2141 in the binary. 2142 2143 It should be noted that this option cannot guarantee uniqueness and the 2144 following is an example where it is not unique when two modules contain 2145 symbols with the same private linkage name: 2146 2147 .. code-block:: console 2148 2149 $ cd $P/foo && clang -c -funique-internal-linkage-names name_conflict.c 2150 $ cd $P/bar && clang -c -funique-internal-linkage-names name_conflict.c 2151 $ cd $P && clang foo/name_conflict.o && bar/name_conflict.o 2152 2153.. option:: -fbasic-block-sections=[labels, all, list=<arg>, none] 2154 2155 Controls how Clang emits text sections for basic blocks. With values ``all`` 2156 and ``list=<arg>``, each basic block or a subset of basic blocks can be placed 2157 in its own unique section. With the "labels" value, normal text sections are 2158 emitted, but a ``.bb_addr_map`` section is emitted which includes address 2159 offsets for each basic block in the program, relative to the parent function 2160 address. 2161 2162 With the ``list=<arg>`` option, a file containing the subset of basic blocks 2163 that need to placed in unique sections can be specified. The format of the 2164 file is as follows. For example, ``list=spec.txt`` where ``spec.txt`` is the 2165 following: 2166 2167 :: 2168 2169 !foo 2170 !!2 2171 !_Z3barv 2172 2173 will place the machine basic block with ``id 2`` in function ``foo`` in a 2174 unique section. It will also place all basic blocks of functions ``bar`` 2175 in unique sections. 2176 2177 Further, section clusters can also be specified using the ``list=<arg>`` 2178 option. For example, ``list=spec.txt`` where ``spec.txt`` contains: 2179 2180 :: 2181 2182 !foo 2183 !!1 !!3 !!5 2184 !!2 !!4 !!6 2185 2186 will create two unique sections for function ``foo`` with the first 2187 containing the odd numbered basic blocks and the second containing the 2188 even numbered basic blocks. 2189 2190 Basic block sections allow the linker to reorder basic blocks and enables 2191 link-time optimizations like whole program inter-procedural basic block 2192 reordering. 2193 2194Profile Guided Optimization 2195--------------------------- 2196 2197Profile information enables better optimization. For example, knowing that a 2198branch is taken very frequently helps the compiler make better decisions when 2199ordering basic blocks. Knowing that a function ``foo`` is called more 2200frequently than another function ``bar`` helps the inliner. Optimization 2201levels ``-O2`` and above are recommended for use of profile guided optimization. 2202 2203Clang supports profile guided optimization with two different kinds of 2204profiling. A sampling profiler can generate a profile with very low runtime 2205overhead, or you can build an instrumented version of the code that collects 2206more detailed profile information. Both kinds of profiles can provide execution 2207counts for instructions in the code and information on branches taken and 2208function invocation. 2209 2210Regardless of which kind of profiling you use, be careful to collect profiles 2211by running your code with inputs that are representative of the typical 2212behavior. Code that is not exercised in the profile will be optimized as if it 2213is unimportant, and the compiler may make poor optimization choices for code 2214that is disproportionately used while profiling. 2215 2216Differences Between Sampling and Instrumentation 2217^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2218 2219Although both techniques are used for similar purposes, there are important 2220differences between the two: 2221 22221. Profile data generated with one cannot be used by the other, and there is no 2223 conversion tool that can convert one to the other. So, a profile generated 2224 via ``-fprofile-instr-generate`` must be used with ``-fprofile-instr-use``. 2225 Similarly, sampling profiles generated by external profilers must be 2226 converted and used with ``-fprofile-sample-use``. 2227 22282. Instrumentation profile data can be used for code coverage analysis and 2229 optimization. 2230 22313. Sampling profiles can only be used for optimization. They cannot be used for 2232 code coverage analysis. Although it would be technically possible to use 2233 sampling profiles for code coverage, sample-based profiles are too 2234 coarse-grained for code coverage purposes; it would yield poor results. 2235 22364. Sampling profiles must be generated by an external tool. The profile 2237 generated by that tool must then be converted into a format that can be read 2238 by LLVM. The section on sampling profilers describes one of the supported 2239 sampling profile formats. 2240 2241 2242Using Sampling Profilers 2243^^^^^^^^^^^^^^^^^^^^^^^^ 2244 2245Sampling profilers are used to collect runtime information, such as 2246hardware counters, while your application executes. They are typically 2247very efficient and do not incur a large runtime overhead. The 2248sample data collected by the profiler can be used during compilation 2249to determine what the most executed areas of the code are. 2250 2251Using the data from a sample profiler requires some changes in the way 2252a program is built. Before the compiler can use profiling information, 2253the code needs to execute under the profiler. The following is the 2254usual build cycle when using sample profilers for optimization: 2255 22561. Build the code with source line table information. You can use all the 2257 usual build flags that you always build your application with. The only 2258 requirement is that you add ``-gline-tables-only`` or ``-g`` to the 2259 command line. This is important for the profiler to be able to map 2260 instructions back to source line locations. 2261 2262 .. code-block:: console 2263 2264 $ clang++ -O2 -gline-tables-only code.cc -o code 2265 22662. Run the executable under a sampling profiler. The specific profiler 2267 you use does not really matter, as long as its output can be converted 2268 into the format that the LLVM optimizer understands. Currently, there 2269 exists a conversion tool for the Linux Perf profiler 2270 (https://perf.wiki.kernel.org/), so these examples assume that you 2271 are using Linux Perf to profile your code. 2272 2273 .. code-block:: console 2274 2275 $ perf record -b ./code 2276 2277 Note the use of the ``-b`` flag. This tells Perf to use the Last Branch 2278 Record (LBR) to record call chains. While this is not strictly required, 2279 it provides better call information, which improves the accuracy of 2280 the profile data. 2281 22823. Convert the collected profile data to LLVM's sample profile format. 2283 This is currently supported via the AutoFDO converter ``create_llvm_prof``. 2284 It is available at https://github.com/google/autofdo. Once built and 2285 installed, you can convert the ``perf.data`` file to LLVM using 2286 the command: 2287 2288 .. code-block:: console 2289 2290 $ create_llvm_prof --binary=./code --out=code.prof 2291 2292 This will read ``perf.data`` and the binary file ``./code`` and emit 2293 the profile data in ``code.prof``. Note that if you ran ``perf`` 2294 without the ``-b`` flag, you need to use ``--use_lbr=false`` when 2295 calling ``create_llvm_prof``. 2296 22974. Build the code again using the collected profile. This step feeds 2298 the profile back to the optimizers. This should result in a binary 2299 that executes faster than the original one. Note that you are not 2300 required to build the code with the exact same arguments that you 2301 used in the first step. The only requirement is that you build the code 2302 with ``-gline-tables-only`` and ``-fprofile-sample-use``. 2303 2304 .. code-block:: console 2305 2306 $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code 2307 2308 [OPTIONAL] Sampling-based profiles can have inaccuracies or missing block/ 2309 edge counters. The profile inference algorithm (profi) can be used to infer 2310 missing blocks and edge counts, and improve the quality of profile data. 2311 Enable it with ``-fsample-profile-use-profi``. 2312 2313 .. code-block:: console 2314 2315 $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof \ 2316 -fsample-profile-use-profi code.cc -o code 2317 2318Sample Profile Formats 2319"""""""""""""""""""""" 2320 2321Since external profilers generate profile data in a variety of custom formats, 2322the data generated by the profiler must be converted into a format that can be 2323read by the backend. LLVM supports three different sample profile formats: 2324 23251. ASCII text. This is the easiest one to generate. The file is divided into 2326 sections, which correspond to each of the functions with profile 2327 information. The format is described below. It can also be generated from 2328 the binary or gcov formats using the ``llvm-profdata`` tool. 2329 23302. Binary encoding. This uses a more efficient encoding that yields smaller 2331 profile files. This is the format generated by the ``create_llvm_prof`` tool 2332 in https://github.com/google/autofdo. 2333 23343. GCC encoding. This is based on the gcov format, which is accepted by GCC. It 2335 is only interesting in environments where GCC and Clang co-exist. This 2336 encoding is only generated by the ``create_gcov`` tool in 2337 https://github.com/google/autofdo. It can be read by LLVM and 2338 ``llvm-profdata``, but it cannot be generated by either. 2339 2340If you are using Linux Perf to generate sampling profiles, you can use the 2341conversion tool ``create_llvm_prof`` described in the previous section. 2342Otherwise, you will need to write a conversion tool that converts your 2343profiler's native format into one of these three. 2344 2345 2346Sample Profile Text Format 2347"""""""""""""""""""""""""" 2348 2349This section describes the ASCII text format for sampling profiles. It is, 2350arguably, the easiest one to generate. If you are interested in generating any 2351of the other two, consult the ``ProfileData`` library in LLVM's source tree 2352(specifically, ``include/llvm/ProfileData/SampleProfReader.h``). 2353 2354.. code-block:: console 2355 2356 function1:total_samples:total_head_samples 2357 offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ] 2358 offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ] 2359 ... 2360 offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ] 2361 offsetA[.discriminator]: fnA:num_of_total_samples 2362 offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ] 2363 offsetA1[.discriminator]: number_of_samples [fn9:num fn10:num ... ] 2364 offsetB[.discriminator]: fnB:num_of_total_samples 2365 offsetB1[.discriminator]: number_of_samples [fn11:num fn12:num ... ] 2366 2367This is a nested tree in which the indentation represents the nesting level 2368of the inline stack. There are no blank lines in the file. And the spacing 2369within a single line is fixed. Additional spaces will result in an error 2370while reading the file. 2371 2372Any line starting with the '#' character is completely ignored. 2373 2374Inlined calls are represented with indentation. The Inline stack is a 2375stack of source locations in which the top of the stack represents the 2376leaf function, and the bottom of the stack represents the actual 2377symbol to which the instruction belongs. 2378 2379Function names must be mangled in order for the profile loader to 2380match them in the current translation unit. The two numbers in the 2381function header specify how many total samples were accumulated in the 2382function (first number), and the total number of samples accumulated 2383in the prologue of the function (second number). This head sample 2384count provides an indicator of how frequently the function is invoked. 2385 2386There are two types of lines in the function body. 2387 2388- Sampled line represents the profile information of a source location. 2389 ``offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]`` 2390 2391- Callsite line represents the profile information of an inlined callsite. 2392 ``offsetA[.discriminator]: fnA:num_of_total_samples`` 2393 2394Each sampled line may contain several items. Some are optional (marked 2395below): 2396 2397a. Source line offset. This number represents the line number 2398 in the function where the sample was collected. The line number is 2399 always relative to the line where symbol of the function is 2400 defined. So, if the function has its header at line 280, the offset 2401 13 is at line 293 in the file. 2402 2403 Note that this offset should never be a negative number. This could 2404 happen in cases like macros. The debug machinery will register the 2405 line number at the point of macro expansion. So, if the macro was 2406 expanded in a line before the start of the function, the profile 2407 converter should emit a 0 as the offset (this means that the optimizers 2408 will not be able to associate a meaningful weight to the instructions 2409 in the macro). 2410 2411b. [OPTIONAL] Discriminator. This is used if the sampled program 2412 was compiled with DWARF discriminator support 2413 (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators). 2414 DWARF discriminators are unsigned integer values that allow the 2415 compiler to distinguish between multiple execution paths on the 2416 same source line location. 2417 2418 For example, consider the line of code ``if (cond) foo(); else bar();``. 2419 If the predicate ``cond`` is true 80% of the time, then the edge 2420 into function ``foo`` should be considered to be taken most of the 2421 time. But both calls to ``foo`` and ``bar`` are at the same source 2422 line, so a sample count at that line is not sufficient. The 2423 compiler needs to know which part of that line is taken more 2424 frequently. 2425 2426 This is what discriminators provide. In this case, the calls to 2427 ``foo`` and ``bar`` will be at the same line, but will have 2428 different discriminator values. This allows the compiler to correctly 2429 set edge weights into ``foo`` and ``bar``. 2430 2431c. Number of samples. This is an integer quantity representing the 2432 number of samples collected by the profiler at this source 2433 location. 2434 2435d. [OPTIONAL] Potential call targets and samples. If present, this 2436 line contains a call instruction. This models both direct and 2437 number of samples. For example, 2438 2439 .. code-block:: console 2440 2441 130: 7 foo:3 bar:2 baz:7 2442 2443 The above means that at relative line offset 130 there is a call 2444 instruction that calls one of ``foo()``, ``bar()`` and ``baz()``, 2445 with ``baz()`` being the relatively more frequently called target. 2446 2447As an example, consider a program with the call chain ``main -> foo -> bar``. 2448When built with optimizations enabled, the compiler may inline the 2449calls to ``bar`` and ``foo`` inside ``main``. The generated profile 2450could then be something like this: 2451 2452.. code-block:: console 2453 2454 main:35504:0 2455 1: _Z3foov:35504 2456 2: _Z32bari:31977 2457 1.1: 31977 2458 2: 0 2459 2460This profile indicates that there were a total of 35,504 samples 2461collected in main. All of those were at line 1 (the call to ``foo``). 2462Of those, 31,977 were spent inside the body of ``bar``. The last line 2463of the profile (``2: 0``) corresponds to line 2 inside ``main``. No 2464samples were collected there. 2465 2466Profiling with Instrumentation 2467^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2468 2469Clang also supports profiling via instrumentation. This requires building a 2470special instrumented version of the code and has some runtime 2471overhead during the profiling, but it provides more detailed results than a 2472sampling profiler. It also provides reproducible results, at least to the 2473extent that the code behaves consistently across runs. 2474 2475Here are the steps for using profile guided optimization with 2476instrumentation: 2477 24781. Build an instrumented version of the code by compiling and linking with the 2479 ``-fprofile-instr-generate`` option. 2480 2481 .. code-block:: console 2482 2483 $ clang++ -O2 -fprofile-instr-generate code.cc -o code 2484 24852. Run the instrumented executable with inputs that reflect the typical usage. 2486 By default, the profile data will be written to a ``default.profraw`` file 2487 in the current directory. You can override that default by using option 2488 ``-fprofile-instr-generate=`` or by setting the ``LLVM_PROFILE_FILE`` 2489 environment variable to specify an alternate file. If non-default file name 2490 is specified by both the environment variable and the command line option, 2491 the environment variable takes precedence. The file name pattern specified 2492 can include different modifiers: ``%p``, ``%h``, and ``%m``. 2493 2494 Any instance of ``%p`` in that file name will be replaced by the process 2495 ID, so that you can easily distinguish the profile output from multiple 2496 runs. 2497 2498 .. code-block:: console 2499 2500 $ LLVM_PROFILE_FILE="code-%p.profraw" ./code 2501 2502 The modifier ``%h`` can be used in scenarios where the same instrumented 2503 binary is run in multiple different host machines dumping profile data 2504 to a shared network based storage. The ``%h`` specifier will be substituted 2505 with the hostname so that profiles collected from different hosts do not 2506 clobber each other. 2507 2508 While the use of ``%p`` specifier can reduce the likelihood for the profiles 2509 dumped from different processes to clobber each other, such clobbering can still 2510 happen because of the ``pid`` re-use by the OS. Another side-effect of using 2511 ``%p`` is that the storage requirement for raw profile data files is greatly 2512 increased. To avoid issues like this, the ``%m`` specifier can used in the profile 2513 name. When this specifier is used, the profiler runtime will substitute ``%m`` 2514 with a unique integer identifier associated with the instrumented binary. Additionally, 2515 multiple raw profiles dumped from different processes that share a file system (can be 2516 on different hosts) will be automatically merged by the profiler runtime during the 2517 dumping. If the program links in multiple instrumented shared libraries, each library 2518 will dump the profile data into its own profile data file (with its unique integer 2519 id embedded in the profile name). Note that the merging enabled by ``%m`` is for raw 2520 profile data generated by profiler runtime. The resulting merged "raw" profile data 2521 file still needs to be converted to a different format expected by the compiler ( 2522 see step 3 below). 2523 2524 .. code-block:: console 2525 2526 $ LLVM_PROFILE_FILE="code-%m.profraw" ./code 2527 2528 25293. Combine profiles from multiple runs and convert the "raw" profile format to 2530 the input expected by clang. Use the ``merge`` command of the 2531 ``llvm-profdata`` tool to do this. 2532 2533 .. code-block:: console 2534 2535 $ llvm-profdata merge -output=code.profdata code-*.profraw 2536 2537 Note that this step is necessary even when there is only one "raw" profile, 2538 since the merge operation also changes the file format. 2539 25404. Build the code again using the ``-fprofile-instr-use`` option to specify the 2541 collected profile data. 2542 2543 .. code-block:: console 2544 2545 $ clang++ -O2 -fprofile-instr-use=code.profdata code.cc -o code 2546 2547 You can repeat step 4 as often as you like without regenerating the 2548 profile. As you make changes to your code, clang may no longer be able to 2549 use the profile data. It will warn you when this happens. 2550 2551Profile generation using an alternative instrumentation method can be 2552controlled by the GCC-compatible flags ``-fprofile-generate`` and 2553``-fprofile-use``. Although these flags are semantically equivalent to 2554their GCC counterparts, they *do not* handle GCC-compatible profiles. 2555They are only meant to implement GCC's semantics with respect to 2556profile creation and use. Flag ``-fcs-profile-generate`` also instruments 2557programs using the same instrumentation method as ``-fprofile-generate``. 2558 2559.. option:: -fprofile-generate[=<dirname>] 2560 2561 The ``-fprofile-generate`` and ``-fprofile-generate=`` flags will use 2562 an alternative instrumentation method for profile generation. When 2563 given a directory name, it generates the profile file 2564 ``default_%m.profraw`` in the directory named ``dirname`` if specified. 2565 If ``dirname`` does not exist, it will be created at runtime. ``%m`` specifier 2566 will be substituted with a unique id documented in step 2 above. In other words, 2567 with ``-fprofile-generate[=<dirname>]`` option, the "raw" profile data automatic 2568 merging is turned on by default, so there will no longer any risk of profile 2569 clobbering from different running processes. For example, 2570 2571 .. code-block:: console 2572 2573 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code 2574 2575 When ``code`` is executed, the profile will be written to the file 2576 ``yyy/zzz/default_xxxx.profraw``. 2577 2578 To generate the profile data file with the compiler readable format, the 2579 ``llvm-profdata`` tool can be used with the profile directory as the input: 2580 2581 .. code-block:: console 2582 2583 $ llvm-profdata merge -output=code.profdata yyy/zzz/ 2584 2585 If the user wants to turn off the auto-merging feature, or simply override the 2586 the profile dumping path specified at command line, the environment variable 2587 ``LLVM_PROFILE_FILE`` can still be used to override 2588 the directory and filename for the profile file at runtime. 2589 2590.. option:: -fcs-profile-generate[=<dirname>] 2591 2592 The ``-fcs-profile-generate`` and ``-fcs-profile-generate=`` flags will use 2593 the same instrumentation method, and generate the same profile as in the 2594 ``-fprofile-generate`` and ``-fprofile-generate=`` flags. The difference is 2595 that the instrumentation is performed after inlining so that the resulted 2596 profile has a better context sensitive information. They cannot be used 2597 together with ``-fprofile-generate`` and ``-fprofile-generate=`` flags. 2598 They are typically used in conjunction with ``-fprofile-use`` flag. 2599 The profile generated by ``-fcs-profile-generate`` and ``-fprofile-generate`` 2600 can be merged by llvm-profdata. A use example: 2601 2602 .. code-block:: console 2603 2604 $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code 2605 $ ./code 2606 $ llvm-profdata merge -output=code.profdata yyy/zzz/ 2607 2608 The first few steps are the same as that in ``-fprofile-generate`` 2609 compilation. Then perform a second round of instrumentation. 2610 2611 .. code-block:: console 2612 2613 $ clang++ -O2 -fprofile-use=code.profdata -fcs-profile-generate=sss/ttt \ 2614 -o cs_code 2615 $ ./cs_code 2616 $ llvm-profdata merge -output=cs_code.profdata sss/ttt code.profdata 2617 2618 The resulted ``cs_code.prodata`` combines ``code.profdata`` and the profile 2619 generated from binary ``cs_code``. Profile ``cs_code.profata`` can be used by 2620 ``-fprofile-use`` compilation. 2621 2622 .. code-block:: console 2623 2624 $ clang++ -O2 -fprofile-use=cs_code.profdata 2625 2626 The above command will read both profiles to the compiler at the identical 2627 point of instrumentations. 2628 2629.. option:: -fprofile-use[=<pathname>] 2630 2631 Without any other arguments, ``-fprofile-use`` behaves identically to 2632 ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a 2633 profile file, it reads from that file. If ``pathname`` is a directory name, 2634 it reads from ``pathname/default.profdata``. 2635 2636.. option:: -fprofile-update[=<method>] 2637 2638 Unless ``-fsanitize=thread`` is specified, the default is ``single``, which 2639 uses non-atomic increments. The counters can be inaccurate under thread 2640 contention. ``atomic`` uses atomic increments which is accurate but has 2641 overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported 2642 by the target, or ``single`` otherwise. 2643 2644 This option currently works with ``-fprofile-arcs`` and ``-fprofile-instr-generate``, 2645 but not with ``-fprofile-generate``. 2646 2647Disabling Instrumentation 2648^^^^^^^^^^^^^^^^^^^^^^^^^ 2649 2650In certain situations, it may be useful to disable profile generation or use 2651for specific files in a build, without affecting the main compilation flags 2652used for the other files in the project. 2653 2654In these cases, you can use the flag ``-fno-profile-instr-generate`` (or 2655``-fno-profile-generate``) to disable profile generation, and 2656``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use. 2657 2658Note that these flags should appear after the corresponding profile 2659flags to have an effect. 2660 2661.. note:: 2662 2663 When none of the translation units inside a binary is instrumented, in the 2664 case of Fuchsia the profile runtime will not be linked into the binary and 2665 no profile will be produced, while on other platforms the profile runtime 2666 will be linked and profile will be produced but there will not be any 2667 counters. 2668 2669Instrumenting only selected files or functions 2670^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2671 2672Sometimes it's useful to only instrument certain files or functions. For 2673example in automated testing infrastructure, it may be desirable to only 2674instrument files or functions that were modified by a patch to reduce the 2675overhead of instrumenting a full system. 2676 2677This can be done using the ``-fprofile-list`` option. 2678 2679.. option:: -fprofile-list=<pathname> 2680 2681 This option can be used to apply profile instrumentation only to selected 2682 files or functions. ``pathname`` should point to a file in the 2683 :doc:`SanitizerSpecialCaseList` format which selects which files and 2684 functions to instrument. 2685 2686 .. code-block:: console 2687 2688 $ clang++ -O2 -fprofile-instr-generate -fprofile-list=fun.list code.cc -o code 2689 2690 The option can be specified multiple times to pass multiple files. 2691 2692 .. code-block:: console 2693 2694 $ clang++ -O2 -fprofile-instr-generate -fcoverage-mapping -fprofile-list=fun.list -fprofile-list=code.list code.cc -o code 2695 2696Supported sections are ``[clang]``, ``[llvm]``, and ``[csllvm]`` representing 2697clang PGO, IRPGO, and CSIRPGO, respectively. Supported prefixes are ``function`` 2698and ``source``. Supported categories are ``allow``, ``skip``, and ``forbid``. 2699``skip`` adds the ``skipprofile`` attribute while ``forbid`` adds the 2700``noprofile`` attribute to the appropriate function. Use 2701``default:<allow|skip|forbid>`` to specify the default category. 2702 2703 .. code-block:: console 2704 2705 $ cat fun.list 2706 # The following cases are for clang instrumentation. 2707 [clang] 2708 2709 # We might not want to profile functions that are inlined in many places. 2710 function:inlinedLots=skip 2711 2712 # We want to forbid profiling where it might be dangerous. 2713 source:lib/unsafe/*.cc=forbid 2714 2715 # Otherwise we allow profiling. 2716 default:allow 2717 2718Older Prefixes 2719"""""""""""""" 2720 An older format is also supported, but it is only able to add the 2721 ``noprofile`` attribute. 2722 To filter individual functions or entire source files use ``fun:<name>`` or 2723 ``src:<file>`` respectively. To exclude a function or a source file, use 2724 ``!fun:<name>`` or ``!src:<file>`` respectively. The format also supports 2725 wildcard expansion. The compiler generated functions are assumed to be located 2726 in the main source file. It is also possible to restrict the filter to a 2727 particular instrumentation type by using a named section. 2728 2729 .. code-block:: none 2730 2731 # all functions whose name starts with foo will be instrumented. 2732 fun:foo* 2733 2734 # except for foo1 which will be excluded from instrumentation. 2735 !fun:foo1 2736 2737 # every function in path/to/foo.cc will be instrumented. 2738 src:path/to/foo.cc 2739 2740 # bar will be instrumented only when using backend instrumentation. 2741 # Recognized section names are clang, llvm and csllvm. 2742 [llvm] 2743 fun:bar 2744 2745 When the file contains only excludes, all files and functions except for the 2746 excluded ones will be instrumented. Otherwise, only the files and functions 2747 specified will be instrumented. 2748 2749Instrument function groups 2750^^^^^^^^^^^^^^^^^^^^^^^^^^ 2751 2752Sometimes it is desirable to minimize the size overhead of instrumented 2753binaries. One way to do this is to partition functions into groups and only 2754instrument functions in a specified group. This can be done using the 2755`-fprofile-function-groups` and `-fprofile-selected-function-group` options. 2756 2757.. option:: -fprofile-function-groups=<N>, -fprofile-selected-function-group=<i> 2758 2759 The following uses 3 groups 2760 2761 .. code-block:: console 2762 2763 $ clang++ -Oz -fprofile-generate=group_0/ -fprofile-function-groups=3 -fprofile-selected-function-group=0 code.cc -o code.0 2764 $ clang++ -Oz -fprofile-generate=group_1/ -fprofile-function-groups=3 -fprofile-selected-function-group=1 code.cc -o code.1 2765 $ clang++ -Oz -fprofile-generate=group_2/ -fprofile-function-groups=3 -fprofile-selected-function-group=2 code.cc -o code.2 2766 2767 After collecting raw profiles from the three binaries, they can be merged into 2768 a single profile like normal. 2769 2770 .. code-block:: console 2771 2772 $ llvm-profdata merge -output=code.profdata group_*/*.profraw 2773 2774 2775Profile remapping 2776^^^^^^^^^^^^^^^^^ 2777 2778When the program is compiled after a change that affects many symbol names, 2779pre-existing profile data may no longer match the program. For example: 2780 2781 * switching from libstdc++ to libc++ will result in the mangled names of all 2782 functions taking standard library types to change 2783 * renaming a widely-used type in C++ will result in the mangled names of all 2784 functions that have parameters involving that type to change 2785 * moving from a 32-bit compilation to a 64-bit compilation may change the 2786 underlying type of ``size_t`` and similar types, resulting in changes to 2787 manglings 2788 2789Clang allows use of a profile remapping file to specify that such differences 2790in mangled names should be ignored when matching the profile data against the 2791program. 2792 2793.. option:: -fprofile-remapping-file=<file> 2794 2795 Specifies a file containing profile remapping information, that will be 2796 used to match mangled names in the profile data to mangled names in the 2797 program. 2798 2799The profile remapping file is a text file containing lines of the form 2800 2801.. code-block:: text 2802 2803 fragmentkind fragment1 fragment2 2804 2805where ``fragmentkind`` is one of ``name``, ``type``, or ``encoding``, 2806indicating whether the following mangled name fragments are 2807<`name <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.name>`_>s, 2808<`type <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.type>`_>s, or 2809<`encoding <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.encoding>`_>s, 2810respectively. 2811Blank lines and lines starting with ``#`` are ignored. 2812 2813For convenience, built-in <substitution>s such as ``St`` and ``Ss`` 2814are accepted as <name>s (even though they technically are not <name>s). 2815 2816For example, to specify that ``absl::string_view`` and ``std::string_view`` 2817should be treated as equivalent when matching profile data, the following 2818remapping file could be used: 2819 2820.. code-block:: text 2821 2822 # absl::string_view is considered equivalent to std::string_view 2823 type N4absl11string_viewE St17basic_string_viewIcSt11char_traitsIcEE 2824 2825 # std:: might be std::__1:: in libc++ or std::__cxx11:: in libstdc++ 2826 name 3std St3__1 2827 name 3std St7__cxx11 2828 2829Matching profile data using a profile remapping file is supported on a 2830best-effort basis. For example, information regarding indirect call targets is 2831currently not remapped. For best results, you are encouraged to generate new 2832profile data matching the updated program, or to remap the profile data 2833using the ``llvm-cxxmap`` and ``llvm-profdata merge`` tools. 2834 2835.. note:: 2836 2837 Profile data remapping is currently only supported for C++ mangled names 2838 following the Itanium C++ ABI mangling scheme. This covers all C++ targets 2839 supported by Clang other than Windows. 2840 2841GCOV-based Profiling 2842-------------------- 2843 2844GCOV is a test coverage program, it helps to know how often a line of code 2845is executed. When instrumenting the code with ``--coverage`` option, some 2846counters are added for each edge linking basic blocks. 2847 2848At compile time, gcno files are generated containing information about 2849blocks and edges between them. At runtime the counters are incremented and at 2850exit the counters are dumped in gcda files. 2851 2852The tool ``llvm-cov gcov`` will parse gcno, gcda and source files to generate 2853a report ``.c.gcov``. 2854 2855.. option:: -fprofile-filter-files=[regexes] 2856 2857 Define a list of regexes separated by a semi-colon. 2858 If a file name matches any of the regexes then the file is instrumented. 2859 2860 .. code-block:: console 2861 2862 $ clang --coverage -fprofile-filter-files=".*\.c$" foo.c 2863 2864 For example, this will only instrument files finishing with ``.c``, skipping ``.h`` files. 2865 2866.. option:: -fprofile-exclude-files=[regexes] 2867 2868 Define a list of regexes separated by a semi-colon. 2869 If a file name doesn't match all the regexes then the file is instrumented. 2870 2871 .. code-block:: console 2872 2873 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" foo.c 2874 2875 For example, this will instrument all the files except the ones in ``/usr/include``. 2876 2877If both options are used then a file is instrumented if its name matches any 2878of the regexes from ``-fprofile-filter-list`` and doesn't match all the regexes 2879from ``-fprofile-exclude-list``. 2880 2881.. code-block:: console 2882 2883 $ clang --coverage -fprofile-exclude-files="^/usr/include/.*$" \ 2884 -fprofile-filter-files="^/usr/.*$" 2885 2886In that case ``/usr/foo/oof.h`` is instrumented since it matches the filter regex and 2887doesn't match the exclude regex, but ``/usr/include/foo.h`` doesn't since it matches 2888the exclude regex. 2889 2890Controlling Debug Information 2891----------------------------- 2892 2893Controlling Size of Debug Information 2894^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2895 2896Debug info kind generated by Clang can be set by one of the flags listed 2897below. If multiple flags are present, the last one is used. 2898 2899.. option:: -g0 2900 2901 Don't generate any debug info (default). 2902 2903.. option:: -gline-tables-only 2904 2905 Generate line number tables only. 2906 2907 This kind of debug info allows to obtain stack traces with function names, 2908 file names and line numbers (by such tools as ``gdb`` or ``addr2line``). It 2909 doesn't contain any other data (e.g. description of local variables or 2910 function parameters). 2911 2912.. option:: -fstandalone-debug 2913 2914 Clang supports a number of optimizations to reduce the size of debug 2915 information in the binary. They work based on the assumption that 2916 the debug type information can be spread out over multiple 2917 compilation units. Specifically, the optimizations are: 2918 2919 - will not emit type definitions for types that are not needed by a 2920 module and could be replaced with a forward declaration. 2921 - will only emit type info for a dynamic C++ class in the module that 2922 contains the vtable for the class. 2923 - will only emit type info for a C++ class (non-trivial, non-aggregate) 2924 in the modules that contain a definition for one of its constructors. 2925 - will only emit type definitions for types that are the subject of explicit 2926 template instantiation declarations in the presence of an explicit 2927 instantiation definition for the type. 2928 2929 The **-fstandalone-debug** option turns off these optimizations. 2930 This is useful when working with 3rd-party libraries that don't come 2931 with debug information. Note that Clang will never emit type 2932 information for types that are not referenced at all by the program. 2933 2934.. option:: -fno-standalone-debug 2935 2936 On Darwin **-fstandalone-debug** is enabled by default. The 2937 **-fno-standalone-debug** option can be used to get to turn on the 2938 vtable-based optimization described above. 2939 2940.. option:: -g 2941 2942 Generate complete debug info. 2943 2944.. option:: -feliminate-unused-debug-types 2945 2946 By default, Clang does not emit type information for types that are defined 2947 but not used in a program. To retain the debug info for these unused types, 2948 the negation **-fno-eliminate-unused-debug-types** can be used. 2949 2950Controlling Macro Debug Info Generation 2951^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2952 2953Debug info for C preprocessor macros increases the size of debug information in 2954the binary. Macro debug info generated by Clang can be controlled by the flags 2955listed below. 2956 2957.. option:: -fdebug-macro 2958 2959 Generate debug info for preprocessor macros. This flag is discarded when 2960 **-g0** is enabled. 2961 2962.. option:: -fno-debug-macro 2963 2964 Do not generate debug info for preprocessor macros (default). 2965 2966Controlling Debugger "Tuning" 2967^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2968 2969While Clang generally emits standard DWARF debug info (http://dwarfstd.org), 2970different debuggers may know how to take advantage of different specific DWARF 2971features. You can "tune" the debug info for one of several different debuggers. 2972 2973.. option:: -ggdb, -glldb, -gsce, -gdbx 2974 2975 Tune the debug info for the ``gdb``, ``lldb``, Sony PlayStation\ |reg| 2976 debugger, or ``dbx``, respectively. Each of these options implies **-g**. 2977 (Therefore, if you want both **-gline-tables-only** and debugger tuning, the 2978 tuning option must come first.) 2979 2980Controlling LLVM IR Output 2981-------------------------- 2982 2983Controlling Value Names in LLVM IR 2984^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2985 2986Emitting value names in LLVM IR increases the size and verbosity of the IR. 2987By default, value names are only emitted in assertion-enabled builds of Clang. 2988However, when reading IR it can be useful to re-enable the emission of value 2989names to improve readability. 2990 2991.. option:: -fdiscard-value-names 2992 2993 Discard value names when generating LLVM IR. 2994 2995.. option:: -fno-discard-value-names 2996 2997 Do not discard value names when generating LLVM IR. This option can be used 2998 to re-enable names for release builds of Clang. 2999 3000 3001Comment Parsing Options 3002----------------------- 3003 3004Clang parses Doxygen and non-Doxygen style documentation comments and attaches 3005them to the appropriate declaration nodes. By default, it only parses 3006Doxygen-style comments and ignores ordinary comments starting with ``//`` and 3007``/*``. 3008 3009.. option:: -Wdocumentation 3010 3011 Emit warnings about use of documentation comments. This warning group is off 3012 by default. 3013 3014 This includes checking that ``\param`` commands name parameters that actually 3015 present in the function signature, checking that ``\returns`` is used only on 3016 functions that actually return a value etc. 3017 3018.. option:: -Wno-documentation-unknown-command 3019 3020 Don't warn when encountering an unknown Doxygen command. 3021 3022.. option:: -fparse-all-comments 3023 3024 Parse all comments as documentation comments (including ordinary comments 3025 starting with ``//`` and ``/*``). 3026 3027.. option:: -fcomment-block-commands=[commands] 3028 3029 Define custom documentation commands as block commands. This allows Clang to 3030 construct the correct AST for these custom commands, and silences warnings 3031 about unknown commands. Several commands must be separated by a comma 3032 *without trailing space*; e.g. ``-fcomment-block-commands=foo,bar`` defines 3033 custom commands ``\foo`` and ``\bar``. 3034 3035 It is also possible to use ``-fcomment-block-commands`` several times; e.g. 3036 ``-fcomment-block-commands=foo -fcomment-block-commands=bar`` does the same 3037 as above. 3038 3039.. _c: 3040 3041C Language Features 3042=================== 3043 3044The support for standard C in clang is feature-complete except for the 3045C99 floating-point pragmas. 3046 3047Extensions supported by clang 3048----------------------------- 3049 3050See :doc:`LanguageExtensions`. 3051 3052Differences between various standard modes 3053------------------------------------------ 3054 3055clang supports the -std option, which changes what language mode clang uses. 3056The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, gnu11, c17, 3057gnu17, c2x, gnu2x, and various aliases for those modes. If no -std option is 3058specified, clang defaults to gnu17 mode. Many C99 and C11 features are 3059supported in earlier modes as a conforming extension, with a warning. Use 3060``-pedantic-errors`` to request an error if a feature from a later standard 3061revision is used in an earlier mode. 3062 3063Differences between all ``c*`` and ``gnu*`` modes: 3064 3065- ``c*`` modes define "``__STRICT_ANSI__``". 3066- Target-specific defines not prefixed by underscores, like ``linux``, 3067 are defined in ``gnu*`` modes. 3068- Trigraphs default to being off in ``gnu*`` modes; they can be enabled 3069 by the ``-trigraphs`` option. 3070- The parser recognizes ``asm`` and ``typeof`` as keywords in ``gnu*`` modes; 3071 the variants ``__asm__`` and ``__typeof__`` are recognized in all modes. 3072- The parser recognizes ``inline`` as a keyword in ``gnu*`` mode, in 3073 addition to recognizing it in the ``*99`` and later modes for which it is 3074 part of the ISO C standard. The variant ``__inline__`` is recognized in all 3075 modes. 3076- The Apple "blocks" extension is recognized by default in ``gnu*`` modes 3077 on some platforms; it can be enabled in any mode with the ``-fblocks`` 3078 option. 3079 3080Differences between ``*89`` and ``*94`` modes: 3081 3082- Digraphs are not recognized in c89 mode. 3083 3084Differences between ``*94`` and ``*99`` modes: 3085 3086- The ``*99`` modes default to implementing ``inline`` / ``__inline__`` 3087 as specified in C99, while the ``*89`` modes implement the GNU version. 3088 This can be overridden for individual functions with the ``__gnu_inline__`` 3089 attribute. 3090- The scope of names defined inside a ``for``, ``if``, ``switch``, ``while``, 3091 or ``do`` statement is different. (example: ``if ((struct x {int x;}*)0) {}``.) 3092- ``__STDC_VERSION__`` is not defined in ``*89`` modes. 3093- ``inline`` is not recognized as a keyword in ``c89`` mode. 3094- ``restrict`` is not recognized as a keyword in ``*89`` modes. 3095- Commas are allowed in integer constant expressions in ``*99`` modes. 3096- Arrays which are not lvalues are not implicitly promoted to pointers 3097 in ``*89`` modes. 3098- Some warnings are different. 3099 3100Differences between ``*99`` and ``*11`` modes: 3101 3102- Warnings for use of C11 features are disabled. 3103- ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``. 3104 3105Differences between ``*11`` and ``*17`` modes: 3106 3107- ``__STDC_VERSION__`` is defined to ``201710L`` rather than ``201112L``. 3108 3109GCC extensions not implemented yet 3110---------------------------------- 3111 3112clang tries to be compatible with gcc as much as possible, but some gcc 3113extensions are not implemented yet: 3114 3115- clang does not support decimal floating point types (``_Decimal32`` and 3116 friends) yet. 3117- clang does not support nested functions; this is a complex feature 3118 which is infrequently used, so it is unlikely to be implemented 3119 anytime soon. In C++11 it can be emulated by assigning lambda 3120 functions to local variables, e.g: 3121 3122 .. code-block:: cpp 3123 3124 auto const local_function = [&](int parameter) { 3125 // Do something 3126 }; 3127 ... 3128 local_function(1); 3129 3130- clang only supports global register variables when the register specified 3131 is non-allocatable (e.g. the stack pointer). Support for general global 3132 register variables is unlikely to be implemented soon because it requires 3133 additional LLVM backend support. 3134- clang does not support static initialization of flexible array 3135 members. This appears to be a rarely used extension, but could be 3136 implemented pending user demand. 3137- clang does not support 3138 ``__builtin_va_arg_pack``/``__builtin_va_arg_pack_len``. This is 3139 used rarely, but in some potentially interesting places, like the 3140 glibc headers, so it may be implemented pending user demand. Note 3141 that because clang pretends to be like GCC 4.2, and this extension 3142 was introduced in 4.3, the glibc headers will not try to use this 3143 extension with clang at the moment. 3144- clang does not support the gcc extension for forward-declaring 3145 function parameters; this has not shown up in any real-world code 3146 yet, though, so it might never be implemented. 3147 3148This is not a complete list; if you find an unsupported extension 3149missing from this list, please send an e-mail to cfe-dev. This list 3150currently excludes C++; see :ref:`C++ Language Features <cxx>`. Also, this 3151list does not include bugs in mostly-implemented features; please see 3152the `bug 3153tracker <https://bugs.llvm.org/buglist.cgi?quicksearch=product%3Aclang+component%3A-New%2BBugs%2CAST%2CBasic%2CDriver%2CHeaders%2CLLVM%2BCodeGen%2Cparser%2Cpreprocessor%2CSemantic%2BAnalyzer>`_ 3154for known existing bugs (FIXME: Is there a section for bug-reporting 3155guidelines somewhere?). 3156 3157Intentionally unsupported GCC extensions 3158---------------------------------------- 3159 3160- clang does not support the gcc extension that allows variable-length 3161 arrays in structures. This is for a few reasons: one, it is tricky to 3162 implement, two, the extension is completely undocumented, and three, 3163 the extension appears to be rarely used. Note that clang *does* 3164 support flexible array members (arrays with a zero or unspecified 3165 size at the end of a structure). 3166- GCC accepts many expression forms that are not valid integer constant 3167 expressions in bit-field widths, enumerator constants, case labels, 3168 and in array bounds at global scope. Clang also accepts additional 3169 expression forms in these contexts, but constructs that GCC accepts due to 3170 simplifications GCC performs while parsing, such as ``x - x`` (where ``x`` is a 3171 variable) will likely never be accepted by Clang. 3172- clang does not support ``__builtin_apply`` and friends; this extension 3173 is extremely obscure and difficult to implement reliably. 3174 3175.. _c_ms: 3176 3177Microsoft extensions 3178-------------------- 3179 3180clang has support for many extensions from Microsoft Visual C++. To enable these 3181extensions, use the ``-fms-extensions`` command-line option. This is the default 3182for Windows targets. Clang does not implement every pragma or declspec provided 3183by MSVC, but the popular ones, such as ``__declspec(dllexport)`` and ``#pragma 3184comment(lib)`` are well supported. 3185 3186clang has a ``-fms-compatibility`` flag that makes clang accept enough 3187invalid C++ to be able to parse most Microsoft headers. For example, it 3188allows `unqualified lookup of dependent base class members 3189<https://clang.llvm.org/compatibility.html#dep_lookup_bases>`_, which is 3190a common compatibility issue with clang. This flag is enabled by default 3191for Windows targets. 3192 3193``-fdelayed-template-parsing`` lets clang delay parsing of function template 3194definitions until the end of a translation unit. This flag is enabled by 3195default for Windows targets. 3196 3197For compatibility with existing code that compiles with MSVC, clang defines the 3198``_MSC_VER`` and ``_MSC_FULL_VER`` macros. These default to the values of 1800 3199and 180000000 respectively, making clang look like an early release of Visual 3200C++ 2013. The ``-fms-compatibility-version=`` flag overrides these values. It 3201accepts a dotted version tuple, such as 19.00.23506. Changing the MSVC 3202compatibility version makes clang behave more like that version of MSVC. For 3203example, ``-fms-compatibility-version=19`` will enable C++14 features and define 3204``char16_t`` and ``char32_t`` as builtin types. 3205 3206.. _cxx: 3207 3208C++ Language Features 3209===================== 3210 3211clang fully implements all of standard C++98 except for exported 3212templates (which were removed in C++11), all of standard C++11, 3213C++14, and C++17, and most of C++20. 3214 3215See the `C++ support in Clang <https://clang.llvm.org/cxx_status.html>`_ page 3216for detailed information on C++ feature support across Clang versions. 3217 3218Controlling implementation limits 3219--------------------------------- 3220 3221.. option:: -fbracket-depth=N 3222 3223 Sets the limit for nested parentheses, brackets, and braces to N. The 3224 default is 256. 3225 3226.. option:: -fconstexpr-depth=N 3227 3228 Sets the limit for recursive constexpr function invocations to N. The 3229 default is 512. 3230 3231.. option:: -fconstexpr-steps=N 3232 3233 Sets the limit for the number of full-expressions evaluated in a single 3234 constant expression evaluation. The default is 1048576. 3235 3236.. option:: -ftemplate-depth=N 3237 3238 Sets the limit for recursively nested template instantiations to N. The 3239 default is 1024. 3240 3241.. option:: -foperator-arrow-depth=N 3242 3243 Sets the limit for iterative calls to 'operator->' functions to N. The 3244 default is 256. 3245 3246.. _objc: 3247 3248Objective-C Language Features 3249============================= 3250 3251.. _objcxx: 3252 3253Objective-C++ Language Features 3254=============================== 3255 3256.. _openmp: 3257 3258OpenMP Features 3259=============== 3260 3261Clang supports all OpenMP 4.5 directives and clauses. See :doc:`OpenMPSupport` 3262for additional details. 3263 3264Use `-fopenmp` to enable OpenMP. Support for OpenMP can be disabled with 3265`-fno-openmp`. 3266 3267Use `-fopenmp-simd` to enable OpenMP simd features only, without linking 3268the runtime library; for combined constructs 3269(e.g. ``#pragma omp parallel for simd``) the non-simd directives and clauses 3270will be ignored. This can be disabled with `-fno-openmp-simd`. 3271 3272Controlling implementation limits 3273--------------------------------- 3274 3275.. option:: -fopenmp-use-tls 3276 3277 Controls code generation for OpenMP threadprivate variables. In presence of 3278 this option all threadprivate variables are generated the same way as thread 3279 local variables, using TLS support. If `-fno-openmp-use-tls` 3280 is provided or target does not support TLS, code generation for threadprivate 3281 variables relies on OpenMP runtime library. 3282 3283.. _opencl: 3284 3285OpenCL Features 3286=============== 3287 3288Clang can be used to compile OpenCL kernels for execution on a device 3289(e.g. GPU). It is possible to compile the kernel into a binary (e.g. for AMDGPU) 3290that can be uploaded to run directly on a device (e.g. using 3291`clCreateProgramWithBinary 3292<https://www.khronos.org/registry/OpenCL/specs/opencl-1.1.pdf#111>`_) or 3293into generic bitcode files loadable into other toolchains. 3294 3295Compiling to a binary using the default target from the installation can be done 3296as follows: 3297 3298 .. code-block:: console 3299 3300 $ echo "kernel void k(){}" > test.cl 3301 $ clang test.cl 3302 3303Compiling for a specific target can be done by specifying the triple corresponding 3304to the target, for example: 3305 3306 .. code-block:: console 3307 3308 $ clang -target nvptx64-unknown-unknown test.cl 3309 $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl 3310 3311Compiling to bitcode can be done as follows: 3312 3313 .. code-block:: console 3314 3315 $ clang -c -emit-llvm test.cl 3316 3317This will produce a file `test.bc` that can be used in vendor toolchains 3318to perform machine code generation. 3319 3320Note that if compiled to bitcode for generic targets such as SPIR/SPIR-V, 3321portable IR is produced that can be used with various vendor 3322tools as well as open source tools such as `SPIRV-LLVM Translator 3323<https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_ 3324to produce SPIR-V binary. More details are provided in `the offline 3325compilation from OpenCL kernel sources into SPIR-V using open source 3326tools 3327<https://github.com/KhronosGroup/OpenCL-Guide/blob/main/chapters/os_tooling.md>`_. 3328From clang 14 onwards SPIR-V can be generated directly as detailed in 3329:ref:`the SPIR-V support section <spir-v>`. 3330 3331Clang currently supports OpenCL C language standards up to v2.0. Clang mainly 3332supports full profile. There is only very limited support of the embedded 3333profile. 3334From clang 9 a C++ mode is available for OpenCL (see 3335:ref:`C++ for OpenCL <cxx_for_opencl>`). 3336 3337OpenCL v3.0 support is complete but it remains in experimental state, see more 3338details about the experimental features and limitations in :doc:`OpenCLSupport` 3339page. 3340 3341OpenCL Specific Options 3342----------------------- 3343 3344Most of the OpenCL build options from `the specification v2.0 section 5.8.4 3345<https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf#200>`_ are available. 3346 3347Examples: 3348 3349 .. code-block:: console 3350 3351 $ clang -cl-std=CL2.0 -cl-single-precision-constant test.cl 3352 3353 3354Many flags used for the compilation for C sources can also be passed while 3355compiling for OpenCL, examples: ``-c``, ``-O<1-4|s>``, ``-o``, ``-emit-llvm``, etc. 3356 3357Some extra options are available to support special OpenCL features. 3358 3359.. option:: -cl-no-stdinc 3360 3361 Allows to disable all extra types and functions that are not native to the compiler. 3362 This might reduce the compilation speed marginally but many declarations from the 3363 OpenCL standard will not be accessible. For example, the following will fail to 3364 compile. 3365 3366 .. code-block:: console 3367 3368 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl 3369 $ clang -cl-std=CL2.0 -cl-no-stdinc test.cl 3370 error: use of undeclared identifier 'get_enqueued_local_size' 3371 error: use of undeclared identifier 'get_local_size' 3372 3373 More information about the standard types and functions is provided in :ref:`the 3374 section on the OpenCL Header <opencl_header>`. 3375 3376.. _opencl_cl_ext: 3377 3378.. option:: -cl-ext 3379 3380 Enables/Disables support of OpenCL extensions and optional features. All OpenCL 3381 targets set a list of extensions that they support. Clang allows to amend this using 3382 the ``-cl-ext`` flag with a comma-separated list of extensions prefixed with 3383 ``'+'`` or ``'-'``. The syntax: ``-cl-ext=<(['-'|'+']<extension>[,])+>``, where 3384 extensions can be either one of `the OpenCL published extensions 3385 <https://www.khronos.org/registry/OpenCL>`_ 3386 or any vendor extension. Alternatively, ``'all'`` can be used to enable 3387 or disable all known extensions. 3388 3389 Example disabling double support for the 64-bit SPIR-V target: 3390 3391 .. code-block:: console 3392 3393 $ clang -c -target spirv64 -cl-ext=-cl_khr_fp64 test.cl 3394 3395 Enabling all extensions except double support in R600 AMD GPU can be done using: 3396 3397 .. code-block:: console 3398 3399 $ clang -target r600 -cl-ext=-all,+cl_khr_fp16 test.cl 3400 3401 Note that some generic targets e.g. SPIR/SPIR-V enable all extensions/features in 3402 clang by default. 3403 3404OpenCL Targets 3405-------------- 3406 3407OpenCL targets are derived from the regular Clang target classes. The OpenCL 3408specific parts of the target representation provide address space mapping as 3409well as a set of supported extensions. 3410 3411Specific Targets 3412^^^^^^^^^^^^^^^^ 3413 3414There is a set of concrete HW architectures that OpenCL can be compiled for. 3415 3416- For AMD target: 3417 3418 .. code-block:: console 3419 3420 $ clang -target amdgcn-amd-amdhsa -mcpu=gfx900 test.cl 3421 3422- For Nvidia architectures: 3423 3424 .. code-block:: console 3425 3426 $ clang -target nvptx64-unknown-unknown test.cl 3427 3428 3429Generic Targets 3430^^^^^^^^^^^^^^^ 3431 3432- A SPIR-V binary can be produced for 32 or 64 bit targets. 3433 3434 .. code-block:: console 3435 3436 $ clang -target spirv32 -c test.cl 3437 $ clang -target spirv64 -c test.cl 3438 3439 More details can be found in :ref:`the SPIR-V support section <spir-v>`. 3440 3441- SPIR is available as a generic target to allow portable bitcode to be produced 3442 that can be used across GPU toolchains. The implementation follows `the SPIR 3443 specification <https://www.khronos.org/spir>`_. There are two flavors 3444 available for 32 and 64 bits. 3445 3446 .. code-block:: console 3447 3448 $ clang -target spir test.cl -emit-llvm -c 3449 $ clang -target spir64 test.cl -emit-llvm -c 3450 3451 Clang will generate SPIR v1.2 compatible IR for OpenCL versions up to 2.0 and 3452 SPIR v2.0 for OpenCL v2.0 or C++ for OpenCL. 3453 3454- x86 is used by some implementations that are x86 compatible and currently 3455 remains for backwards compatibility (with older implementations prior to 3456 SPIR target support). For "non-SPMD" targets which cannot spawn multiple 3457 work-items on the fly using hardware, which covers practically all non-GPU 3458 devices such as CPUs and DSPs, additional processing is needed for the kernels 3459 to support multiple work-item execution. For this, a 3rd party toolchain, 3460 such as for example `POCL <http://portablecl.org/>`_, can be used. 3461 3462 This target does not support multiple memory segments and, therefore, the fake 3463 address space map can be added using the :ref:`-ffake-address-space-map 3464 <opencl_fake_address_space_map>` flag. 3465 3466 All known OpenCL extensions and features are set to supported in the generic targets, 3467 however :option:`-cl-ext` flag can be used to toggle individual extensions and 3468 features. 3469 3470.. _opencl_header: 3471 3472OpenCL Header 3473------------- 3474 3475By default Clang will include standard headers and therefore most of OpenCL 3476builtin functions and types are available during compilation. The 3477default declarations of non-native compiler types and functions can be disabled 3478by using flag :option:`-cl-no-stdinc`. 3479 3480The following example demonstrates that OpenCL kernel sources with various 3481standard builtin functions can be compiled without the need for an explicit 3482includes or compiler flags. 3483 3484 .. code-block:: console 3485 3486 $ echo "bool is_wg_uniform(int i){return get_enqueued_local_size(i)==get_local_size(i);}" > test.cl 3487 $ clang -cl-std=CL2.0 test.cl 3488 3489More information about the default headers is provided in :doc:`OpenCLSupport`. 3490 3491OpenCL Extensions 3492----------------- 3493 3494Most of the ``cl_khr_*`` extensions to OpenCL C from `the official OpenCL 3495registry <https://www.khronos.org/registry/OpenCL/>`_ are available and 3496configured per target depending on the support available in the specific 3497architecture. 3498 3499It is possible to alter the default extensions setting per target using 3500``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details). 3501 3502Vendor extensions can be added flexibly by declaring the list of types and 3503functions associated with each extensions enclosed within the following 3504compiler pragma directives: 3505 3506 .. code-block:: c 3507 3508 #pragma OPENCL EXTENSION the_new_extension_name : begin 3509 // declare types and functions associated with the extension here 3510 #pragma OPENCL EXTENSION the_new_extension_name : end 3511 3512For example, parsing the following code adds ``my_t`` type and ``my_func`` 3513function to the custom ``my_ext`` extension. 3514 3515 .. code-block:: c 3516 3517 #pragma OPENCL EXTENSION my_ext : begin 3518 typedef struct{ 3519 int a; 3520 }my_t; 3521 void my_func(my_t); 3522 #pragma OPENCL EXTENSION my_ext : end 3523 3524There is no conflict resolution for identifier clashes among extensions. 3525It is therefore recommended that the identifiers are prefixed with a 3526double underscore to avoid clashing with user space identifiers. Vendor 3527extension should use reserved identifier prefix e.g. amd, arm, intel. 3528 3529Clang also supports language extensions documented in `The OpenCL C Language 3530Extensions Documentation 3531<https://github.com/KhronosGroup/Khronosdotorg/blob/main/api/opencl/assets/OpenCL_LangExt.pdf>`_. 3532 3533OpenCL-Specific Attributes 3534-------------------------- 3535 3536OpenCL support in Clang contains a set of attribute taken directly from the 3537specification as well as additional attributes. 3538 3539See also :doc:`AttributeReference`. 3540 3541nosvm 3542^^^^^ 3543 3544Clang supports this attribute to comply to OpenCL v2.0 conformance, but it 3545does not have any effect on the IR. For more details reffer to the specification 3546`section 6.7.2 3547<https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_ 3548 3549 3550opencl_unroll_hint 3551^^^^^^^^^^^^^^^^^^ 3552 3553The implementation of this feature mirrors the unroll hint for C. 3554More details on the syntax can be found in the specification 3555`section 6.11.5 3556<https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#61>`_ 3557 3558convergent 3559^^^^^^^^^^ 3560 3561To make sure no invalid optimizations occur for single program multiple data 3562(SPMD) / single instruction multiple thread (SIMT) Clang provides attributes that 3563can be used for special functions that have cross work item semantics. 3564An example is the subgroup operations such as `intel_sub_group_shuffle 3565<https://www.khronos.org/registry/cl/extensions/intel/cl_intel_subgroups.txt>`_ 3566 3567 .. code-block:: c 3568 3569 // Define custom my_sub_group_shuffle(data, c) 3570 // that makes use of intel_sub_group_shuffle 3571 r1 = ... 3572 if (r0) r1 = computeA(); 3573 // Shuffle data from r1 into r3 3574 // of threads id r2. 3575 r3 = my_sub_group_shuffle(r1, r2); 3576 if (r0) r3 = computeB(); 3577 3578with non-SPMD semantics this is optimized to the following equivalent code: 3579 3580 .. code-block:: c 3581 3582 r1 = ... 3583 if (!r0) 3584 // Incorrect functionality! The data in r1 3585 // have not been computed by all threads yet. 3586 r3 = my_sub_group_shuffle(r1, r2); 3587 else { 3588 r1 = computeA(); 3589 r3 = my_sub_group_shuffle(r1, r2); 3590 r3 = computeB(); 3591 } 3592 3593Declaring the function ``my_sub_group_shuffle`` with the convergent attribute 3594would prevent this: 3595 3596 .. code-block:: c 3597 3598 my_sub_group_shuffle() __attribute__((convergent)); 3599 3600Using ``convergent`` guarantees correct execution by keeping CFG equivalence 3601wrt operations marked as ``convergent``. CFG ``G´`` is equivalent to ``G`` wrt 3602node ``Ni`` : ``iff ∀ Nj (i≠j)`` domination and post-domination relations with 3603respect to ``Ni`` remain the same in both ``G`` and ``G´``. 3604 3605noduplicate 3606^^^^^^^^^^^ 3607 3608``noduplicate`` is more restrictive with respect to optimizations than 3609``convergent`` because a convergent function only preserves CFG equivalence. 3610This allows some optimizations to happen as long as the control flow remains 3611unmodified. 3612 3613 .. code-block:: c 3614 3615 for (int i=0; i<4; i++) 3616 my_sub_group_shuffle() 3617 3618can be modified to: 3619 3620 .. code-block:: c 3621 3622 my_sub_group_shuffle(); 3623 my_sub_group_shuffle(); 3624 my_sub_group_shuffle(); 3625 my_sub_group_shuffle(); 3626 3627while using ``noduplicate`` would disallow this. Also ``noduplicate`` doesn't 3628have the same safe semantics of CFG as ``convergent`` and can cause changes in 3629CFG that modify semantics of the original program. 3630 3631``noduplicate`` is kept for backwards compatibility only and it considered to be 3632deprecated for future uses. 3633 3634.. _cxx_for_opencl: 3635 3636C++ for OpenCL 3637-------------- 3638 3639Starting from clang 9 kernel code can contain C++17 features: classes, templates, 3640function overloading, type deduction, etc. Please note that this is not an 3641implementation of `OpenCL C++ 3642<https://www.khronos.org/registry/OpenCL/specs/2.2/pdf/OpenCL_Cxx.pdf>`_ and 3643there is no plan to support it in clang in any new releases in the near future. 3644 3645Clang currently supports C++ for OpenCL 1.0 and 2021. 3646For detailed information about this language refer to the C++ for OpenCL 3647Programming Language Documentation available 3648in `the latest build 3649<https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html>`_ 3650or in `the official release 3651<https://github.com/KhronosGroup/OpenCL-Docs/releases/tag/cxxforopencl-docrev2021.12>`_. 3652 3653To enable the C++ for OpenCL mode, pass one of following command line options when 3654compiling ``.clcpp`` file: 3655 3656- C++ for OpenCL 1.0: ``-cl-std=clc++``, ``-cl-std=CLC++``, ``-cl-std=clc++1.0``, 3657 ``-cl-std=CLC++1.0``, ``-std=clc++``, ``-std=CLC++``, ``-std=clc++1.0`` or 3658 ``-std=CLC++1.0``. 3659 3660- C++ for OpenCL 2021: ``-cl-std=clc++2021``, ``-cl-std=CLC++2021``, 3661 ``-std=clc++2021``, ``-std=CLC++2021``. 3662 3663Example of use: 3664 .. code-block:: c++ 3665 3666 template<class T> T add( T x, T y ) 3667 { 3668 return x + y; 3669 } 3670 3671 __kernel void test( __global float* a, __global float* b) 3672 { 3673 auto index = get_global_id(0); 3674 a[index] = add(b[index], b[index+1]); 3675 } 3676 3677 3678 .. code-block:: console 3679 3680 clang -cl-std=clc++1.0 test.clcpp 3681 clang -cl-std=clc++ -c -target spirv64 test.cl 3682 3683 3684By default, files with ``.clcpp`` extension are compiled with the C++ for 3685OpenCL 1.0 mode. 3686 3687 .. code-block:: console 3688 3689 clang test.clcpp 3690 3691For backward compatibility files with ``.cl`` extensions can also be compiled 3692in C++ for OpenCL mode but the desirable language mode must be activated with 3693a flag. 3694 3695 .. code-block:: console 3696 3697 clang -cl-std=clc++ test.cl 3698 3699Support of C++ for OpenCL 2021 is currently in experimental phase, refer to 3700:doc:`OpenCLSupport` for more details. 3701 3702C++ for OpenCL kernel sources can also be compiled online in drivers supporting 3703`cl_ext_cxx_for_opencl 3704<https://www.khronos.org/registry/OpenCL/extensions/ext/cl_ext_cxx_for_opencl.html>`_ 3705extension. 3706 3707Constructing and destroying global objects 3708^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3709 3710Global objects with non-trivial constructors require the constructors to be run 3711before the first kernel using the global objects is executed. Similarly global 3712objects with non-trivial destructors require destructor invocation just after 3713the last kernel using the program objects is executed. 3714In OpenCL versions earlier than v2.2 there is no support for invoking global 3715constructors. However, an easy workaround is to manually enqueue the 3716constructor initialization kernel that has the following name scheme 3717``_GLOBAL__sub_I_<compiled file name>``. 3718This kernel is only present if there are global objects with non-trivial 3719constructors present in the compiled binary. One way to check this is by 3720passing ``CL_PROGRAM_KERNEL_NAMES`` to ``clGetProgramInfo`` (OpenCL v2.0 3721s5.8.7) and then checking whether any kernel name matches the naming scheme of 3722global constructor initialization kernel above. 3723 3724Note that if multiple files are compiled and linked into libraries, multiple 3725kernels that initialize global objects for multiple modules would have to be 3726invoked. 3727 3728Applications are currently required to run initialization of global objects 3729manually before running any kernels in which the objects are used. 3730 3731 .. code-block:: console 3732 3733 clang -cl-std=clc++ test.cl 3734 3735If there are any global objects to be initialized, the final binary will 3736contain the ``_GLOBAL__sub_I_test.cl`` kernel to be enqueued. 3737 3738Note that the manual workaround only applies to objects declared at the 3739program scope. There is no manual workaround for the construction of static 3740objects with non-trivial constructors inside functions. 3741 3742Global destructors can not be invoked manually in the OpenCL v2.0 drivers. 3743However, all memory used for program scope objects should be released on 3744``clReleaseProgram``. 3745 3746Libraries 3747^^^^^^^^^ 3748Limited experimental support of C++ standard libraries for OpenCL is 3749described in :doc:`OpenCLSupport` page. 3750 3751.. _target_features: 3752 3753Target-Specific Features and Limitations 3754======================================== 3755 3756CPU Architectures Features and Limitations 3757------------------------------------------ 3758 3759X86 3760^^^ 3761 3762The support for X86 (both 32-bit and 64-bit) is considered stable on 3763Darwin (macOS), Linux, FreeBSD, and Dragonfly BSD: it has been tested 3764to correctly compile many large C, C++, Objective-C, and Objective-C++ 3765codebases. 3766 3767On ``x86_64-mingw32``, passing i128(by value) is incompatible with the 3768Microsoft x64 calling convention. You might need to tweak 3769``WinX86_64ABIInfo::classify()`` in lib/CodeGen/TargetInfo.cpp. 3770 3771For the X86 target, clang supports the `-m16` command line 3772argument which enables 16-bit code output. This is broadly similar to 3773using ``asm(".code16gcc")`` with the GNU toolchain. The generated code 3774and the ABI remains 32-bit but the assembler emits instructions 3775appropriate for a CPU running in 16-bit mode, with address-size and 3776operand-size prefixes to enable 32-bit addressing and operations. 3777 3778Several micro-architecture levels as specified by the x86-64 psABI are defined. 3779They are cumulative in the sense that features from previous levels are 3780implicitly included in later levels. 3781 3782- ``-march=x86-64``: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2 3783- ``-march=x86-64-v2``: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3 3784- ``-march=x86-64-v3``: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE 3785- ``-march=x86-64-v4``: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL 3786 3787ARM 3788^^^ 3789 3790The support for ARM (specifically ARMv6 and ARMv7) is considered stable 3791on Darwin (iOS): it has been tested to correctly compile many large C, 3792C++, Objective-C, and Objective-C++ codebases. Clang only supports a 3793limited number of ARM architectures. It does not yet fully support 3794ARMv5, for example. 3795 3796PowerPC 3797^^^^^^^ 3798 3799The support for PowerPC (especially PowerPC64) is considered stable 3800on Linux and FreeBSD: it has been tested to correctly compile many 3801large C and C++ codebases. PowerPC (32bit) is still missing certain 3802features (e.g. PIC code on ELF platforms). 3803 3804Other platforms 3805^^^^^^^^^^^^^^^ 3806 3807clang currently contains some support for other architectures (e.g. Sparc); 3808however, significant pieces of code generation are still missing, and they 3809haven't undergone significant testing. 3810 3811clang contains limited support for the MSP430 embedded processor, but 3812both the clang support and the LLVM backend support are highly 3813experimental. 3814 3815Other platforms are completely unsupported at the moment. Adding the 3816minimal support needed for parsing and semantic analysis on a new 3817platform is quite easy; see ``lib/Basic/Targets.cpp`` in the clang source 3818tree. This level of support is also sufficient for conversion to LLVM IR 3819for simple programs. Proper support for conversion to LLVM IR requires 3820adding code to ``lib/CodeGen/CGCall.cpp`` at the moment; this is likely to 3821change soon, though. Generating assembly requires a suitable LLVM 3822backend. 3823 3824Operating System Features and Limitations 3825----------------------------------------- 3826 3827Windows 3828^^^^^^^ 3829 3830Clang has experimental support for targeting "Cygming" (Cygwin / MinGW) 3831platforms. 3832 3833See also :ref:`Microsoft Extensions <c_ms>`. 3834 3835Cygwin 3836"""""" 3837 3838Clang works on Cygwin-1.7. 3839 3840MinGW32 3841""""""" 3842 3843Clang works on some mingw32 distributions. Clang assumes directories as 3844below; 3845 3846- ``C:/mingw/include`` 3847- ``C:/mingw/lib`` 3848- ``C:/mingw/lib/gcc/mingw32/4.[3-5].0/include/c++`` 3849 3850On MSYS, a few tests might fail. 3851 3852MinGW-w64 3853""""""""" 3854 3855For 32-bit (i686-w64-mingw32), and 64-bit (x86\_64-w64-mingw32), Clang 3856assumes as below; 3857 3858- ``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)`` 3859- ``some_directory/bin/gcc.exe`` 3860- ``some_directory/bin/clang.exe`` 3861- ``some_directory/bin/clang++.exe`` 3862- ``some_directory/bin/../include/c++/GCC_version`` 3863- ``some_directory/bin/../include/c++/GCC_version/x86_64-w64-mingw32`` 3864- ``some_directory/bin/../include/c++/GCC_version/i686-w64-mingw32`` 3865- ``some_directory/bin/../include/c++/GCC_version/backward`` 3866- ``some_directory/bin/../x86_64-w64-mingw32/include`` 3867- ``some_directory/bin/../i686-w64-mingw32/include`` 3868- ``some_directory/bin/../include`` 3869 3870This directory layout is standard for any toolchain you will find on the 3871official `MinGW-w64 website <http://mingw-w64.sourceforge.net>`_. 3872 3873Clang expects the GCC executable "gcc.exe" compiled for 3874``i686-w64-mingw32`` (or ``x86_64-w64-mingw32``) to be present on PATH. 3875 3876`Some tests might fail <https://bugs.llvm.org/show_bug.cgi?id=9072>`_ on 3877``x86_64-w64-mingw32``. 3878 3879AIX 3880^^^ 3881 3882The ``-mdefault-visibility-export-mapping=`` option can be used to control 3883mapping of default visibility to an explicit shared object export 3884(i.e. XCOFF exported visibility). Three values are provided for the option: 3885 3886* ``-mdefault-visibility-export-mapping=none``: no additional export 3887 information is created for entities with default visibility. 3888* ``-mdefault-visibility-export-mapping=explicit``: mark entities for export 3889 if they have explict (e.g. via an attribute) default visibility from the 3890 source, including RTTI. 3891* ``-mdefault-visibility-export-mapping=all``: set XCOFF exported visibility 3892 for all entities with default visibility from any source. This gives a 3893 export behavior similar to ELF platforms where all entities with default 3894 visibility are exported. 3895 3896.. _spir-v: 3897 3898SPIR-V support 3899-------------- 3900 3901Clang supports generation of SPIR-V conformant to `the OpenCL Environment 3902Specification 3903<https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Env.html>`_. 3904 3905To generate SPIR-V binaries, Clang uses the external ``llvm-spirv`` tool from the 3906`SPIRV-LLVM-Translator repo 3907<https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_. 3908 3909Prior to the generation of SPIR-V binary with Clang, ``llvm-spirv`` 3910should be built or installed. Please refer to `the following instructions 3911<https://github.com/KhronosGroup/SPIRV-LLVM-Translator#build-instructions>`_ 3912for more details. Clang will expect the ``llvm-spirv`` executable to 3913be present in the ``PATH`` environment variable. Clang uses ``llvm-spirv`` 3914with `the widely adopted assembly syntax package 3915<https://github.com/KhronosGroup/SPIRV-LLVM-Translator/#build-with-spirv-tools>`_. 3916 3917`The versioning 3918<https://github.com/KhronosGroup/SPIRV-LLVM-Translator/releases>`_ of 3919``llvm-spirv`` is aligned with Clang major releases. The same applies to the 3920main development branch. It is therefore important to ensure the ``llvm-spirv`` 3921version is in alignment with the Clang version. For troubleshooting purposes 3922``llvm-spirv`` can be `tested in isolation 3923<https://github.com/KhronosGroup/SPIRV-LLVM-Translator#test-instructions>`_. 3924 3925Example usage for OpenCL kernel compilation: 3926 3927 .. code-block:: console 3928 3929 $ clang -target spirv32 -c test.cl 3930 $ clang -target spirv64 -c test.cl 3931 3932Both invocations of Clang will result in the generation of a SPIR-V binary file 3933`test.o` for 32 bit and 64 bit respectively. This file can be imported 3934by an OpenCL driver that support SPIR-V consumption or it can be compiled 3935further by offline SPIR-V consumer tools. 3936 3937Converting to SPIR-V produced with the optimization levels other than `-O0` is 3938currently available as an experimental feature and it is not guaranteed to work 3939in all cases. 3940 3941Clang also supports integrated generation of SPIR-V without use of ``llvm-spirv`` 3942tool as an experimental feature when ``-fintegrated-objemitter`` flag is passed in 3943the command line. 3944 3945 .. code-block:: console 3946 3947 $ clang -target spirv32 -fintegrated-objemitter -c test.cl 3948 3949Note that only very basic functionality is supported at this point and therefore 3950it is not suitable for arbitrary use cases. This feature is only enabled when clang 3951build is configured with ``-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=SPIRV`` option. 3952 3953Linking is done using ``spirv-link`` from `the SPIRV-Tools project 3954<https://github.com/KhronosGroup/SPIRV-Tools#linker>`_. Similar to other external 3955linkers, Clang will expect ``spirv-link`` to be installed separately and to be 3956present in the ``PATH`` environment variable. Please refer to `the build and 3957installation instructions 3958<https://github.com/KhronosGroup/SPIRV-Tools#build>`_. 3959 3960 .. code-block:: console 3961 3962 $ clang -target spirv64 test1.cl test2.cl 3963 3964More information about the SPIR-V target settings and supported versions of SPIR-V 3965format can be found in `the SPIR-V target guide 3966<https://llvm.org/docs/SPIRVUsage.html>`__. 3967 3968.. _clang-cl: 3969 3970clang-cl 3971======== 3972 3973clang-cl is an alternative command-line interface to Clang, designed for 3974compatibility with the Visual C++ compiler, cl.exe. 3975 3976To enable clang-cl to find system headers, libraries, and the linker when run 3977from the command-line, it should be executed inside a Visual Studio Native Tools 3978Command Prompt or a regular Command Prompt where the environment has been set 3979up using e.g. `vcvarsall.bat <https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx>`_. 3980 3981clang-cl can also be used from inside Visual Studio by selecting the LLVM 3982Platform Toolset. The toolset is not part of the installer, but may be installed 3983separately from the 3984`Visual Studio Marketplace <https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain>`_. 3985To use the toolset, select a project in Solution Explorer, open its Property 3986Page (Alt+F7), and in the "General" section of "Configuration Properties" 3987change "Platform Toolset" to LLVM. Doing so enables an additional Property 3988Page for selecting the clang-cl executable to use for builds. 3989 3990To use the toolset with MSBuild directly, invoke it with e.g. 3991``/p:PlatformToolset=LLVM``. This allows trying out the clang-cl toolchain 3992without modifying your project files. 3993 3994It's also possible to point MSBuild at clang-cl without changing toolset by 3995passing ``/p:CLToolPath=c:\llvm\bin /p:CLToolExe=clang-cl.exe``. 3996 3997When using CMake and the Visual Studio generators, the toolset can be set with the ``-T`` flag: 3998 3999 :: 4000 4001 cmake -G"Visual Studio 16 2019" -T LLVM .. 4002 4003When using CMake with the Ninja generator, set the ``CMAKE_C_COMPILER`` and 4004``CMAKE_CXX_COMPILER`` variables to clang-cl: 4005 4006 :: 4007 4008 cmake -GNinja -DCMAKE_C_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" 4009 -DCMAKE_CXX_COMPILER="c:/Program Files (x86)/LLVM/bin/clang-cl.exe" .. 4010 4011 4012Command-Line Options 4013-------------------- 4014 4015To be compatible with cl.exe, clang-cl supports most of the same command-line 4016options. Those options can start with either ``/`` or ``-``. It also supports 4017some of Clang's core options, such as the ``-W`` options. 4018 4019Options that are known to clang-cl, but not currently supported, are ignored 4020with a warning. For example: 4021 4022 :: 4023 4024 clang-cl.exe: warning: argument unused during compilation: '/AI' 4025 4026To suppress warnings about unused arguments, use the ``-Qunused-arguments`` option. 4027 4028Options that are not known to clang-cl will be ignored by default. Use the 4029``-Werror=unknown-argument`` option in order to treat them as errors. If these 4030options are spelled with a leading ``/``, they will be mistaken for a filename: 4031 4032 :: 4033 4034 clang-cl.exe: error: no such file or directory: '/foobar' 4035 4036Please `file a bug <https://bugs.llvm.org/enter_bug.cgi?product=clang&component=Driver>`_ 4037for any valid cl.exe flags that clang-cl does not understand. 4038 4039Execute ``clang-cl /?`` to see a list of supported options: 4040 4041 :: 4042 4043 CL.EXE COMPATIBILITY OPTIONS: 4044 /? Display available options 4045 /arch:<value> Set architecture for code generation 4046 /Brepro- Emit an object file which cannot be reproduced over time 4047 /Brepro Emit an object file which can be reproduced over time 4048 /clang:<arg> Pass <arg> to the clang driver 4049 /C Don't discard comments when preprocessing 4050 /c Compile only 4051 /d1PP Retain macro definitions in /E mode 4052 /d1reportAllClassLayout Dump record layout information 4053 /diagnostics:caret Enable caret and column diagnostics (on by default) 4054 /diagnostics:classic Disable column and caret diagnostics 4055 /diagnostics:column Disable caret diagnostics but keep column info 4056 /D <macro[=value]> Define macro 4057 /EH<value> Exception handling model 4058 /EP Disable linemarker output and preprocess to stdout 4059 /execution-charset:<value> 4060 Runtime encoding, supports only UTF-8 4061 /E Preprocess to stdout 4062 /FA Output assembly code file during compilation 4063 /Fa<file or directory> Output assembly code to this file during compilation (with /FA) 4064 /Fe<file or directory> Set output executable file or directory (ends in / or \) 4065 /FI <value> Include file before parsing 4066 /Fi<file> Set preprocess output file name (with /P) 4067 /Fo<file or directory> Set output object file, or directory (ends in / or \) (with /c) 4068 /fp:except- 4069 /fp:except 4070 /fp:fast 4071 /fp:precise 4072 /fp:strict 4073 /Fp<filename> Set pch filename (with /Yc and /Yu) 4074 /GA Assume thread-local variables are defined in the executable 4075 /Gd Set __cdecl as a default calling convention 4076 /GF- Disable string pooling 4077 /GF Enable string pooling (default) 4078 /GR- Disable emission of RTTI data 4079 /Gregcall Set __regcall as a default calling convention 4080 /GR Enable emission of RTTI data 4081 /Gr Set __fastcall as a default calling convention 4082 /GS- Disable buffer security check 4083 /GS Enable buffer security check (default) 4084 /Gs Use stack probes (default) 4085 /Gs<value> Set stack probe size (default 4096) 4086 /guard:<value> Enable Control Flow Guard with /guard:cf, 4087 or only the table with /guard:cf,nochecks. 4088 Enable EH Continuation Guard with /guard:ehcont 4089 /Gv Set __vectorcall as a default calling convention 4090 /Gw- Don't put each data item in its own section 4091 /Gw Put each data item in its own section 4092 /GX- Disable exception handling 4093 /GX Enable exception handling 4094 /Gy- Don't put each function in its own section (default) 4095 /Gy Put each function in its own section 4096 /Gz Set __stdcall as a default calling convention 4097 /help Display available options 4098 /imsvc <dir> Add directory to system include search path, as if part of %INCLUDE% 4099 /I <dir> Add directory to include search path 4100 /J Make char type unsigned 4101 /LDd Create debug DLL 4102 /LD Create DLL 4103 /link <options> Forward options to the linker 4104 /MDd Use DLL debug run-time 4105 /MD Use DLL run-time 4106 /MTd Use static debug run-time 4107 /MT Use static run-time 4108 /O0 Disable optimization 4109 /O1 Optimize for size (same as /Og /Os /Oy /Ob2 /GF /Gy) 4110 /O2 Optimize for speed (same as /Og /Oi /Ot /Oy /Ob2 /GF /Gy) 4111 /Ob0 Disable function inlining 4112 /Ob1 Only inline functions which are (explicitly or implicitly) marked inline 4113 /Ob2 Inline functions as deemed beneficial by the compiler 4114 /Od Disable optimization 4115 /Og No effect 4116 /Oi- Disable use of builtin functions 4117 /Oi Enable use of builtin functions 4118 /Os Optimize for size 4119 /Ot Optimize for speed 4120 /Ox Deprecated (same as /Og /Oi /Ot /Oy /Ob2); use /O2 instead 4121 /Oy- Disable frame pointer omission (x86 only, default) 4122 /Oy Enable frame pointer omission (x86 only) 4123 /O<flags> Set multiple /O flags at once; e.g. '/O2y-' for '/O2 /Oy-' 4124 /o <file or directory> Set output file or directory (ends in / or \) 4125 /P Preprocess to file 4126 /Qvec- Disable the loop vectorization passes 4127 /Qvec Enable the loop vectorization passes 4128 /showFilenames- Don't print the name of each compiled file (default) 4129 /showFilenames Print the name of each compiled file 4130 /showIncludes Print info about included files to stderr 4131 /source-charset:<value> Source encoding, supports only UTF-8 4132 /std:<value> Language standard to compile for 4133 /TC Treat all source files as C 4134 /Tc <filename> Specify a C source file 4135 /TP Treat all source files as C++ 4136 /Tp <filename> Specify a C++ source file 4137 /utf-8 Set source and runtime encoding to UTF-8 (default) 4138 /U <macro> Undefine macro 4139 /vd<value> Control vtordisp placement 4140 /vmb Use a best-case representation method for member pointers 4141 /vmg Use a most-general representation for member pointers 4142 /vmm Set the default most-general representation to multiple inheritance 4143 /vms Set the default most-general representation to single inheritance 4144 /vmv Set the default most-general representation to virtual inheritance 4145 /volatile:iso Volatile loads and stores have standard semantics 4146 /volatile:ms Volatile loads and stores have acquire and release semantics 4147 /W0 Disable all warnings 4148 /W1 Enable -Wall 4149 /W2 Enable -Wall 4150 /W3 Enable -Wall 4151 /W4 Enable -Wall and -Wextra 4152 /Wall Enable -Weverything 4153 /WX- Do not treat warnings as errors 4154 /WX Treat warnings as errors 4155 /w Disable all warnings 4156 /X Don't add %INCLUDE% to the include search path 4157 /Y- Disable precompiled headers, overrides /Yc and /Yu 4158 /Yc<filename> Generate a pch file for all code up to and including <filename> 4159 /Yu<filename> Load a pch file and use it instead of all code up to and including <filename> 4160 /Z7 Enable CodeView debug information in object files 4161 /Zc:char8_t Enable C++2a char8_t type 4162 /Zc:char8_t- Disable C++2a char8_t type 4163 /Zc:dllexportInlines- Don't dllexport/dllimport inline member functions of dllexport/import classes 4164 /Zc:dllexportInlines dllexport/dllimport inline member functions of dllexport/import classes (default) 4165 /Zc:sizedDealloc- Disable C++14 sized global deallocation functions 4166 /Zc:sizedDealloc Enable C++14 sized global deallocation functions 4167 /Zc:strictStrings Treat string literals as const 4168 /Zc:threadSafeInit- Disable thread-safe initialization of static variables 4169 /Zc:threadSafeInit Enable thread-safe initialization of static variables 4170 /Zc:trigraphs- Disable trigraphs (default) 4171 /Zc:trigraphs Enable trigraphs 4172 /Zc:twoPhase- Disable two-phase name lookup in templates 4173 /Zc:twoPhase Enable two-phase name lookup in templates 4174 /Zi Alias for /Z7. Does not produce PDBs. 4175 /Zl Don't mention any default libraries in the object file 4176 /Zp Set the default maximum struct packing alignment to 1 4177 /Zp<value> Specify the default maximum struct packing alignment 4178 /Zs Run the preprocessor, parser and semantic analysis stages 4179 4180 OPTIONS: 4181 -### Print (but do not run) the commands to run for this compilation 4182 --analyze Run the static analyzer 4183 -faddrsig Emit an address-significance table 4184 -fansi-escape-codes Use ANSI escape codes for diagnostics 4185 -fblocks Enable the 'blocks' language feature 4186 -fcf-protection=<value> Instrument control-flow architecture protection. Options: return, branch, full, none. 4187 -fcf-protection Enable cf-protection in 'full' mode 4188 -fcolor-diagnostics Use colors in diagnostics 4189 -fcomplete-member-pointers 4190 Require member pointer base types to be complete if they would be significant under the Microsoft ABI 4191 -fcoverage-mapping Generate coverage mapping to enable code coverage analysis 4192 -fcrash-diagnostics-dir=<dir> 4193 Put crash-report files in <dir> 4194 -fdebug-macro Emit macro debug information 4195 -fdelayed-template-parsing 4196 Parse templated function definitions at the end of the translation unit 4197 -fdiagnostics-absolute-paths 4198 Print absolute paths in diagnostics 4199 -fdiagnostics-parseable-fixits 4200 Print fix-its in machine parseable form 4201 -flto=<value> Set LTO mode to either 'full' or 'thin' 4202 -flto Enable LTO in 'full' mode 4203 -fmerge-all-constants Allow merging of constants 4204 -fms-compatibility-version=<value> 4205 Dot-separated value representing the Microsoft compiler version 4206 number to report in _MSC_VER (0 = don't define it (default)) 4207 -fms-compatibility Enable full Microsoft Visual C++ compatibility 4208 -fms-extensions Accept some non-standard constructs supported by the Microsoft compiler 4209 -fmsc-version=<value> Microsoft compiler version number to report in _MSC_VER 4210 (0 = don't define it (default)) 4211 -fno-addrsig Don't emit an address-significance table 4212 -fno-builtin-<value> Disable implicit builtin knowledge of a specific function 4213 -fno-builtin Disable implicit builtin knowledge of functions 4214 -fno-complete-member-pointers 4215 Do not require member pointer base types to be complete if they would be significant under the Microsoft ABI 4216 -fno-coverage-mapping Disable code coverage analysis 4217 -fno-crash-diagnostics Disable auto-generation of preprocessed source files and a script for reproduction during a clang crash 4218 -fno-debug-macro Do not emit macro debug information 4219 -fno-delayed-template-parsing 4220 Disable delayed template parsing 4221 -fno-sanitize-address-poison-custom-array-cookie 4222 Disable poisoning array cookies when using custom operator new[] in AddressSanitizer 4223 -fno-sanitize-address-use-after-scope 4224 Disable use-after-scope detection in AddressSanitizer 4225 -fno-sanitize-address-use-odr-indicator 4226 Disable ODR indicator globals 4227 -fno-sanitize-ignorelist Don't use ignorelist file for sanitizers 4228 -fno-sanitize-cfi-cross-dso 4229 Disable control flow integrity (CFI) checks for cross-DSO calls. 4230 -fno-sanitize-coverage=<value> 4231 Disable specified features of coverage instrumentation for Sanitizers 4232 -fno-sanitize-memory-track-origins 4233 Disable origins tracking in MemorySanitizer 4234 -fno-sanitize-memory-use-after-dtor 4235 Disable use-after-destroy detection in MemorySanitizer 4236 -fno-sanitize-recover=<value> 4237 Disable recovery for specified sanitizers 4238 -fno-sanitize-stats Disable sanitizer statistics gathering. 4239 -fno-sanitize-thread-atomics 4240 Disable atomic operations instrumentation in ThreadSanitizer 4241 -fno-sanitize-thread-func-entry-exit 4242 Disable function entry/exit instrumentation in ThreadSanitizer 4243 -fno-sanitize-thread-memory-access 4244 Disable memory access instrumentation in ThreadSanitizer 4245 -fno-sanitize-trap=<value> 4246 Disable trapping for specified sanitizers 4247 -fno-standalone-debug Limit debug information produced to reduce size of debug binary 4248 -fobjc-runtime=<value> Specify the target Objective-C runtime kind and version 4249 -fprofile-exclude-files=<value> 4250 Instrument only functions from files where names don't match all the regexes separated by a semi-colon 4251 -fprofile-filter-files=<value> 4252 Instrument only functions from files where names match any regex separated by a semi-colon 4253 -fprofile-instr-generate=<file> 4254 Generate instrumented code to collect execution counts into <file> 4255 (overridden by LLVM_PROFILE_FILE env var) 4256 -fprofile-instr-generate 4257 Generate instrumented code to collect execution counts into default.profraw file 4258 (overridden by '=' form of option or LLVM_PROFILE_FILE env var) 4259 -fprofile-instr-use=<value> 4260 Use instrumentation data for profile-guided optimization 4261 -fprofile-remapping-file=<file> 4262 Use the remappings described in <file> to match the profile data against names in the program 4263 -fprofile-list=<file> 4264 Filename defining the list of functions/files to instrument 4265 -fsanitize-address-field-padding=<value> 4266 Level of field padding for AddressSanitizer 4267 -fsanitize-address-globals-dead-stripping 4268 Enable linker dead stripping of globals in AddressSanitizer 4269 -fsanitize-address-poison-custom-array-cookie 4270 Enable poisoning array cookies when using custom operator new[] in AddressSanitizer 4271 -fsanitize-address-use-after-return=<mode> 4272 Select the mode of detecting stack use-after-return in AddressSanitizer: never | runtime (default) | always 4273 -fsanitize-address-use-after-scope 4274 Enable use-after-scope detection in AddressSanitizer 4275 -fsanitize-address-use-odr-indicator 4276 Enable ODR indicator globals to avoid false ODR violation reports in partially sanitized programs at the cost of an increase in binary size 4277 -fsanitize-ignorelist=<value> 4278 Path to ignorelist file for sanitizers 4279 -fsanitize-cfi-cross-dso 4280 Enable control flow integrity (CFI) checks for cross-DSO calls. 4281 -fsanitize-cfi-icall-generalize-pointers 4282 Generalize pointers in CFI indirect call type signature checks 4283 -fsanitize-coverage=<value> 4284 Specify the type of coverage instrumentation for Sanitizers 4285 -fsanitize-hwaddress-abi=<value> 4286 Select the HWAddressSanitizer ABI to target (interceptor or platform, default interceptor) 4287 -fsanitize-memory-track-origins=<value> 4288 Enable origins tracking in MemorySanitizer 4289 -fsanitize-memory-track-origins 4290 Enable origins tracking in MemorySanitizer 4291 -fsanitize-memory-use-after-dtor 4292 Enable use-after-destroy detection in MemorySanitizer 4293 -fsanitize-recover=<value> 4294 Enable recovery for specified sanitizers 4295 -fsanitize-stats Enable sanitizer statistics gathering. 4296 -fsanitize-thread-atomics 4297 Enable atomic operations instrumentation in ThreadSanitizer (default) 4298 -fsanitize-thread-func-entry-exit 4299 Enable function entry/exit instrumentation in ThreadSanitizer (default) 4300 -fsanitize-thread-memory-access 4301 Enable memory access instrumentation in ThreadSanitizer (default) 4302 -fsanitize-trap=<value> Enable trapping for specified sanitizers 4303 -fsanitize-undefined-strip-path-components=<number> 4304 Strip (or keep only, if negative) a given number of path components when emitting check metadata. 4305 -fsanitize=<check> Turn on runtime checks for various forms of undefined or suspicious 4306 behavior. See user manual for available checks 4307 -fsplit-lto-unit Enables splitting of the LTO unit. 4308 -fstandalone-debug Emit full debug info for all types used by the program 4309 -fsyntax-only Run the preprocessor, parser and semantic analysis stages 4310 -fwhole-program-vtables Enables whole-program vtable optimization. Requires -flto 4311 -gcodeview-ghash Emit type record hashes in a .debug$H section 4312 -gcodeview Generate CodeView debug information 4313 -gline-directives-only Emit debug line info directives only 4314 -gline-tables-only Emit debug line number tables only 4315 -miamcu Use Intel MCU ABI 4316 -mllvm <value> Additional arguments to forward to LLVM's option processing 4317 -nobuiltininc Disable builtin #include directories 4318 -Qunused-arguments Don't emit warning for unused driver arguments 4319 -R<remark> Enable the specified remark 4320 --target=<value> Generate code for the given target 4321 --version Print version information 4322 -v Show commands to run and use verbose output 4323 -W<warning> Enable the specified warning 4324 -Xclang <arg> Pass <arg> to the clang compiler 4325 4326The /clang: Option 4327^^^^^^^^^^^^^^^^^^ 4328 4329When clang-cl is run with a set of ``/clang:<arg>`` options, it will gather all 4330of the ``<arg>`` arguments and process them as if they were passed to the clang 4331driver. This mechanism allows you to pass flags that are not exposed in the 4332clang-cl options or flags that have a different meaning when passed to the clang 4333driver. Regardless of where they appear in the command line, the ``/clang:`` 4334arguments are treated as if they were passed at the end of the clang-cl command 4335line. 4336 4337The /Zc:dllexportInlines- Option 4338^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 4339 4340This causes the class-level `dllexport` and `dllimport` attributes to not apply 4341to inline member functions, as they otherwise would. For example, in the code 4342below `S::foo()` would normally be defined and exported by the DLL, but when 4343using the ``/Zc:dllexportInlines-`` flag it is not: 4344 4345.. code-block:: c 4346 4347 struct __declspec(dllexport) S { 4348 void foo() {} 4349 } 4350 4351This has the benefit that the compiler doesn't need to emit a definition of 4352`S::foo()` in every translation unit where the declaration is included, as it 4353would otherwise do to ensure there's a definition in the DLL even if it's not 4354used there. If the declaration occurs in a header file that's widely used, this 4355can save significant compilation time and output size. It also reduces the 4356number of functions exported by the DLL similarly to what 4357``-fvisibility-inlines-hidden`` does for shared objects on ELF and Mach-O. 4358Since the function declaration comes with an inline definition, users of the 4359library can use that definition directly instead of importing it from the DLL. 4360 4361Note that the Microsoft Visual C++ compiler does not support this option, and 4362if code in a DLL is compiled with ``/Zc:dllexportInlines-``, the code using the 4363DLL must be compiled in the same way so that it doesn't attempt to dllimport 4364the inline member functions. The reverse scenario should generally work though: 4365a DLL compiled without this flag (such as a system library compiled with Visual 4366C++) can be referenced from code compiled using the flag, meaning that the 4367referencing code will use the inline definitions instead of importing them from 4368the DLL. 4369 4370Also note that like when using ``-fvisibility-inlines-hidden``, the address of 4371`S::foo()` will be different inside and outside the DLL, breaking the C/C++ 4372standard requirement that functions have a unique address. 4373 4374The flag does not apply to explicit class template instantiation definitions or 4375declarations, as those are typically used to explicitly provide a single 4376definition in a DLL, (dllexported instantiation definition) or to signal that 4377the definition is available elsewhere (dllimport instantiation declaration). It 4378also doesn't apply to inline members with static local variables, to ensure 4379that the same instance of the variable is used inside and outside the DLL. 4380 4381Using this flag can cause problems when inline functions that would otherwise 4382be dllexported refer to internal symbols of a DLL. For example: 4383 4384.. code-block:: c 4385 4386 void internal(); 4387 4388 struct __declspec(dllimport) S { 4389 void foo() { internal(); } 4390 } 4391 4392Normally, references to `S::foo()` would use the definition in the DLL from 4393which it was exported, and which presumably also has the definition of 4394`internal()`. However, when using ``/Zc:dllexportInlines-``, the inline 4395definition of `S::foo()` is used directly, resulting in a link error since 4396`internal()` is not available. Even worse, if there is an inline definition of 4397`internal()` containing a static local variable, we will now refer to a 4398different instance of that variable than in the DLL: 4399 4400.. code-block:: c 4401 4402 inline int internal() { static int x; return x++; } 4403 4404 struct __declspec(dllimport) S { 4405 int foo() { return internal(); } 4406 } 4407 4408This could lead to very subtle bugs. Using ``-fvisibility-inlines-hidden`` can 4409lead to the same issue. To avoid it in this case, make `S::foo()` or 4410`internal()` non-inline, or mark them `dllimport/dllexport` explicitly. 4411 4412Finding Clang runtime libraries 4413^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 4414 4415clang-cl supports several features that require runtime library support: 4416 4417- Address Sanitizer (ASan): ``-fsanitize=address`` 4418- Undefined Behavior Sanitizer (UBSan): ``-fsanitize=undefined`` 4419- Code coverage: ``-fprofile-instr-generate -fcoverage-mapping`` 4420- Profile Guided Optimization (PGO): ``-fprofile-instr-generate`` 4421- Certain math operations (int128 division) require the builtins library 4422 4423In order to use these features, the user must link the right runtime libraries 4424into their program. These libraries are distributed alongside Clang in the 4425library resource directory. Clang searches for the resource directory by 4426searching relative to the Clang executable. For example, if LLVM is installed 4427in ``C:\Program Files\LLVM``, then the profile runtime library will be located 4428at the path 4429``C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows\clang_rt.profile-x86_64.lib``. 4430 4431For UBSan, PGO, and coverage, Clang will emit object files that auto-link the 4432appropriate runtime library, but the user generally needs to help the linker 4433(whether it is ``lld-link.exe`` or MSVC ``link.exe``) find the library resource 4434directory. Using the example installation above, this would mean passing 4435``/LIBPATH:C:\Program Files\LLVM\lib\clang\11.0.0\lib\windows`` to the linker. 4436If the user links the program with the ``clang`` or ``clang-cl`` drivers, the 4437driver will pass this flag for them. 4438 4439If the linker cannot find the appropriate library, it will emit an error like 4440this:: 4441 4442 $ clang-cl -c -fsanitize=undefined t.cpp 4443 4444 $ lld-link t.obj -dll 4445 lld-link: error: could not open 'clang_rt.ubsan_standalone-x86_64.lib': no such file or directory 4446 lld-link: error: could not open 'clang_rt.ubsan_standalone_cxx-x86_64.lib': no such file or directory 4447 4448 $ link t.obj -dll -nologo 4449 LINK : fatal error LNK1104: cannot open file 'clang_rt.ubsan_standalone-x86_64.lib' 4450 4451To fix the error, add the appropriate ``/libpath:`` flag to the link line. 4452 4453For ASan, as of this writing, the user is also responsible for linking against 4454the correct ASan libraries. 4455 4456If the user is using the dynamic CRT (``/MD``), then they should add 4457``clang_rt.asan_dynamic-x86_64.lib`` to the link line as a regular input. For 4458other architectures, replace x86_64 with the appropriate name here and below. 4459 4460If the user is using the static CRT (``/MT``), then different runtimes are used 4461to produce DLLs and EXEs. To link a DLL, pass 4462``clang_rt.asan_dll_thunk-x86_64.lib``. To link an EXE, pass 4463``-wholearchive:clang_rt.asan-x86_64.lib``. 4464