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