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