[CMake] Allow overriding MSVC_DIA_SDK_DIR via CMake
[llvm-core.git] / docs / Remarks.rst
blob8dba1947d70c57ea34ad165ca998810960ad4a80
1 =======
2 Remarks
3 =======
5 .. contents::
6    :local:
8 Introduction to the LLVM remark diagnostics
9 ===========================================
11 LLVM is able to emit diagnostics from passes describing whether an optimization
12 has been performed or missed for a particular reason, which should give more
13 insight to users about what the compiler did during the compilation pipeline.
15 There are three main remark types:
17 ``Passed``
19     Remarks that describe a successful optimization performed by the compiler.
21     :Example:
23     ::
25         foo inlined into bar with (cost=always): always inline attribute
27 ``Missed``
29     Remarks that describe an attempt to an optimization by the compiler that
30     could not be performed.
32     :Example:
34     ::
36         foo not inlined into bar because it should never be inlined
37         (cost=never): noinline function attribute
39 ``Analysis``
41     Remarks that describe the result of an analysis, that can bring more
42     information to the user regarding the generated code.
44     :Example:
46     ::
48         16 stack bytes in function
50     ::
52         10 instructions in function
54 Enabling optimization remarks
55 =============================
57 There are two modes that are supported for enabling optimization remarks in
58 LLVM: through remark diagnostics, or through serialized remarks.
60 Remark diagnostics
61 ------------------
63 Optimization remarks can be emitted as diagnostics. These diagnostics will be
64 propagated to front-ends if desired, or emitted by tools like :doc:`llc
65 <CommandGuide/llc>` or :doc:`opt <CommandGuide/opt>`.
67 .. option:: -pass-remarks=<regex>
69   Enables optimization remarks from passes whose name match the given (POSIX)
70   regular expression.
72 .. option:: -pass-remarks-missed=<regex>
74   Enables missed optimization remarks from passes whose name match the given
75   (POSIX) regular expression.
77 .. option:: -pass-remarks-analysis=<regex>
79   Enables optimization analysis remarks from passes whose name match the given
80   (POSIX) regular expression.
82 Serialized remarks
83 ------------------
85 While diagnostics are useful during development, it is often more useful to
86 refer to optimization remarks post-compilation, typically during performance
87 analysis.
89 For that, LLVM can serialize the remarks produced for each compilation unit to
90 a file that can be consumed later.
92 By default, the format of the serialized remarks is :ref:`YAML
93 <yamlremarks>`, and it can be accompanied by a :ref:`section <remarkssection>`
94 in the object files to easily retrieve it.
96 :doc:`llc <CommandGuide/llc>` and :doc:`opt <CommandGuide/opt>` support the
97 following options:
100 ``Basic options``
102     .. option:: -pass-remarks-output=<filename>
104       Enables the serialization of remarks to a file specified in <filename>.
106       By default, the output is serialized to :ref:`YAML <yamlremarks>`.
108     .. option:: -pass-remarks-format=<format>
110       Specifies the output format of the serialized remarks.
112       Supported formats:
114       * :ref:`yaml <yamlremarks>` (default)
115       * :ref:`yaml-strtab <yamlstrtabremarks>`
116       * :ref:`bitstream <bitstreamremarks>`
118 ``Content configuration``
120     .. option:: -pass-remarks-filter=<regex>
122       Only passes whose name match the given (POSIX) regular expression will be
123       serialized to the final output.
125     .. option:: -pass-remarks-with-hotness
127       With PGO, include profile count in optimization remarks.
129     .. option:: -pass-remarks-hotness-threshold
131       The minimum profile count required for an optimization remark to be
132       emitted.
134 Other tools that support remarks:
136 :program:`llvm-lto`
138     .. option:: -lto-pass-remarks-output=<filename>
139     .. option:: -lto-pass-remarks-filter=<regex>
140     .. option:: -lto-pass-remarks-format=<format>
141     .. option:: -lto-pass-remarks-with-hotness
142     .. option:: -lto-pass-remarks-hotness-threshold
144 :program:`gold-plugin` and :program:`lld`
146     .. option:: -opt-remarks-filename=<filename>
147     .. option:: -opt-remarks-filter=<regex>
148     .. option:: -opt-remarks-format=<format>
149     .. option:: -opt-remarks-with-hotness
151 Serialization modes
152 ===================
154 There are two modes available for serializing remarks:
156 ``Separate``
158     In this mode, the remarks and the metadata are serialized separately. The
159     client is responsible for parsing the metadata first, then use the metadata
160     to correctly parse the remarks.
162 ``Standalone``
164     In this mode, the remarks and the metadata are serialized to the same
165     stream. The metadata will always come before the remarks.
167     The compiler does not support emitting standalone remarks. This mode is
168     more suited for post-processing tools like linkers, that can merge the
169     remarks for one whole project.
171 .. _yamlremarks:
173 YAML remarks
174 ============
176 A typical remark serialized to YAML looks like this:
178 .. code-block:: yaml
180     --- !<TYPE>
181     Pass: <pass>
182     Name: <name>
183     DebugLoc: { File: <file>, Line: <line>, Column: <column> }
184     Function: <function>
185     Hotness: <hotness>
186     Args:
187       - <key>: <value>
188         DebugLoc: { File: <arg-file>, Line: <arg-line>, Column: <arg-column> }
190 The following entries are mandatory:
192 * ``<TYPE>``: can be ``Passed``, ``Missed``, ``Analysis``,
193   ``AnalysisFPCommute``, ``AnalysisAliasing``, ``Failure``.
194 * ``<pass>``: the name of the pass that emitted this remark.
195 * ``<name>``: the name of the remark coming from ``<pass>``.
196 * ``<function>``: the mangled name of the function.
198 If a ``DebugLoc`` entry is specified, the following fields are required:
200 * ``<file>``
201 * ``<line>``
202 * ``<column>``
204 If an ``arg`` entry is specified, the following fields are required:
206 * ``<key>``
207 * ``<value>``
209 If a ``DebugLoc`` entry is specified within an ``arg`` entry, the following
210 fields are required:
212 * ``<arg-file>``
213 * ``<arg-line>``
214 * ``<arg-column>``
216 .. _yamlstrtabremarks:
218 YAML with a string table
219 ------------------------
221 The YAML serialization supports the usage of a string table by using the
222 ``yaml-strtab`` format.
224 This format replaces strings in the YAML output with integers representing the
225 index in the string table that can be provided separately through metadata.
227 The following entries can take advantage of the string table while respecting
228 YAML rules:
230 * ``<pass>``
231 * ``<name>``
232 * ``<function>``
233 * ``<file>``
234 * ``<value>``
235 * ``<arg-file>``
237 Currently, none of the tools in :ref:`the opt-viewer directory <optviewer>`
238 support this format.
240 .. _optviewer:
242 YAML metadata
243 -------------
245 The metadata used together with the YAML format is:
247 * a magic number: "REMARKS\\0"
248 * the version number: a little-endian uint64_t
249 * the total size of the string table (the size itself excluded):
250   little-endian uint64_t
251 * a list of null-terminated strings
253 Optional:
255 * the absolute file path to the serialized remark diagnostics: a
256   null-terminated string.
258 When the metadata is serialized separately from the remarks, the file path
259 should be present and point to the file where the remarks are serialized to.
261 In case the metadata only acts as a header to the remarks, the file path can be
262 omitted.
264 .. _bitstreamremarks:
266 LLVM bitstream remarks
267 ======================
269 This format is using :doc:`LLVM bitstream <BitCodeFormat>` to serialize remarks
270 and their associated metadata.
272 A bitstream remark stream can be identified by the magic number ``"RMRK"`` that
273 is placed at the very beginning.
275 The format for serializing remarks is composed of two different block types:
277 .. _bitstreamremarksmetablock:
279 META_BLOCK
280 ----------
282 The block providing information about the rest of the content in the stream.
284 Exactly one block is expected. Having multiple metadata blocks is an error.
286 This block can contain the following records:
288 .. _bitstreamremarksrecordmetacontainerinfo:
290 ``RECORD_META_CONTAINER_INFO``
292     The container version and type.
294     Version: u32
296     Type:    u2
298 .. _bitstreamremarksrecordmetaremarkversion:
300 ``RECORD_META_REMARK_VERSION``
302     The version of the remark entries. This can change independently from the
303     container version.
305     Version: u32
307 .. _bitstreamremarksrecordmetastrtab:
309 ``RECORD_META_STRTAB``
311     The string table used by the remark entries. The format of the string table
312     is a sequence of strings separated by ``\0``.
314 .. _bitstreamremarksrecordmetaexternalfile:
316 ``RECORD_META_EXTERNAL_FILE``
318     The external remark file path that contains the remark blocks associated
319     with this metadata. This is an absolute path.
321 .. _bitstreamremarksremarkblock:
323 REMARK_BLOCK
324 ------------
326 The block describing a remark entry.
328 0 or more blocks per file are allowed. Each block will depend on the
329 :ref:`META_BLOCK <bitstreamremarksmetablock>` in order to be parsed correctly.
331 This block can contain the following records:
333 ``RECORD_REMARK_HEADER``
335     The header of the remark. This contains all the mandatory information about
336     a remark.
338     +---------------+---------------------------+
339     | Type          | u3                        |
340     +---------------+---------------------------+
341     | Remark name   | VBR6 (string table index) |
342     +---------------+---------------------------+
343     | Pass name     | VBR6 (string table index) |
344     +---------------+---------------------------+
345     | Function name | VBR6 (string table index) |
346     +---------------+---------------------------+
348 ``RECORD_REMARK_DEBUG_LOC``
350     The source location for the corresponding remark. This record is optional.
352     +--------+---------------------------+
353     | File   | VBR7 (string table index) |
354     +--------+---------------------------+
355     | Line   | u32                       |
356     +--------+---------------------------+
357     | Column | u32                       |
358     +--------+---------------------------+
360 ``RECORD_REMARK_HOTNESS``
362     The hotness of the remark. This record is optional.
364     +---------------+---------------------+
365     | Hotness | VBR8 (string table index) |
366     +---------------+---------------------+
368 ``RECORD_REMARK_ARG_WITH_DEBUGLOC``
370     A remark argument with an associated debug location.
372     +--------+---------------------------+
373     | Key    | VBR7 (string table index) |
374     +--------+---------------------------+
375     | Value  | VBR7 (string table index) |
376     +--------+---------------------------+
377     | File   | VBR7 (string table index) |
378     +--------+---------------------------+
379     | Line   | u32                       |
380     +--------+---------------------------+
381     | Column | u32                       |
382     +--------+---------------------------+
384 ``RECORD_REMARK_ARG_WITHOUT_DEBUGLOC``
386     A remark argument with an associated debug location.
388     +--------+---------------------------+
389     | Key    | VBR7 (string table index) |
390     +--------+---------------------------+
391     | Value  | VBR7 (string table index) |
392     +--------+---------------------------+
394 The remark container
395 --------------------
397 Bitstream remarks are designed to be used in two different modes:
399 ``The separate mode``
401     The separate mode is the mode that is typically used during compilation. It
402     provides a way to serialize the remark entries to a stream while some
403     metadata is kept in memory to be emitted in the product of the compilation
404     (typically, an object file).
406 ``The standalone mode``
408     The standalone mode is typically stored and used after the distribution of
409     a program. It contains all the information that allows the parsing of all
410     the remarks without having any external dependencies.
412 In order to support multiple modes, the format introduces the concept of a
413 bitstream remark container type.
415 .. _bitstreamremarksseparateremarksmeta:
417 ``SeparateRemarksMeta: the metadata emitted separately``
419     This container type expects only a :ref:`META_BLOCK <bitstreamremarksmetablock>` containing only:
421     * :ref:`RECORD_META_CONTAINER_INFO <bitstreamremarksrecordmetacontainerinfo>`
422     * :ref:`RECORD_META_STRTAB <bitstreamremarksrecordmetastrtab>`
423     * :ref:`RECORD_META_EXTERNAL_FILE <bitstreamremarksrecordmetaexternalfile>`
425     Typically, this is emitted in a section in the object files, allowing
426     clients to retrieve remarks and their associated metadata directly from
427     intermediate products.
429 ``SeparateRemarksFile: the remark entries emitted separately``
431     This container type expects only a :ref:`META_BLOCK <bitstreamremarksmetablock>` containing only:
433     * :ref:`RECORD_META_CONTAINER_INFO <bitstreamremarksrecordmetacontainerinfo>`
434     * :ref:`RECORD_META_REMARK_VERSION <bitstreamremarksrecordmetaremarkversion>`
436     This container type expects 0 or more :ref:`REMARK_BLOCK <bitstreamremarksremarkblock>`.
438     Typically, this is emitted in a side-file alongside an object file, and is
439     made to be able to stream to without increasing the memory consumption of
440     the compiler. This is referenced by the :ref:`RECORD_META_EXTERNAL_FILE
441     <bitstreamremarksrecordmetaexternalfile>` entry in the
442     :ref:`SeparateRemarksMeta <bitstreamremarksseparateremarksmeta>` container.
444 When the parser tries to parse a container that contains the metadata for the
445 separate remarks, it should parse the version and type, then keep the string
446 table in memory while opening the external file, validating its metadata and
447 parsing the remark entries.
449 The container versions from the separate container should match in order to
450 have a well-formed file.
452 ``Standalone: the metadata and the remark entries emitted together``
454     This container type expects only a :ref:`META_BLOCK <bitstreamremarksmetablock>` containing only:
456     * :ref:`RECORD_META_CONTAINER_INFO <bitstreamremarksrecordmetacontainerinfo>`
457     * :ref:`RECORD_META_REMARK_VERSION <bitstreamremarksrecordmetaremarkversion>`
458     * :ref:`RECORD_META_STRTAB <bitstreamremarksrecordmetastrtab>`
460     This container type expects 0 or more :ref:`REMARK_BLOCK <bitstreamremarksremarkblock>`.
462 A complete output of :program:`llvm-bcanalyzer` on the different container types:
464 ``SeparateRemarksMeta``
466 .. code-block:: none
468     <BLOCKINFO_BLOCK/>
469     <Meta BlockID=8 NumWords=13 BlockCodeSize=3>
470       <Container info codeid=1 abbrevid=4 op0=5 op1=0/>
471       <String table codeid=3 abbrevid=5/> blob data = 'pass\\x00key\\x00value\\x00'
472       <External File codeid=4 abbrevid=6/> blob data = '/path/to/file/name'
473     </Meta>
475 ``SeparateRemarksFile``
477 .. code-block:: none
479     <BLOCKINFO_BLOCK/>
480     <Meta BlockID=8 NumWords=3 BlockCodeSize=3>
481       <Container info codeid=1 abbrevid=4 op0=0 op1=1/>
482       <Remark version codeid=2 abbrevid=5 op0=0/>
483     </Meta>
484     <Remark BlockID=9 NumWords=8 BlockCodeSize=4>
485       <Remark header codeid=5 abbrevid=4 op0=2 op1=0 op2=1 op3=2/>
486       <Remark debug location codeid=6 abbrevid=5 op0=3 op1=99 op2=55/>
487       <Remark hotness codeid=7 abbrevid=6 op0=999999999/>
488       <Argument with debug location codeid=8 abbrevid=7 op0=4 op1=5 op2=6 op3=11 op4=66/>
489     </Remark>
491 ``Standalone``
493 .. code-block:: none
495     <BLOCKINFO_BLOCK/>
496     <Meta BlockID=8 NumWords=15 BlockCodeSize=3>
497       <Container info codeid=1 abbrevid=4 op0=5 op1=2/>
498       <Remark version codeid=2 abbrevid=5 op0=30/>
499       <String table codeid=3 abbrevid=6/> blob data = 'pass\\x00remark\\x00function\\x00path\\x00key\\x00value\\x00argpath\\x00'
500     </Meta>
501     <Remark BlockID=9 NumWords=8 BlockCodeSize=4>
502       <Remark header codeid=5 abbrevid=4 op0=2 op1=1 op2=0 op3=2/>
503       <Remark debug location codeid=6 abbrevid=5 op0=3 op1=99 op2=55/>
504       <Remark hotness codeid=7 abbrevid=6 op0=999999999/>
505       <Argument with debug location codeid=8 abbrevid=7 op0=4 op1=5 op2=6 op3=11 op4=66/>
506     </Remark>
508 opt-viewer
509 ==========
511 The ``opt-viewer`` directory contains a collection of tools that visualize and
512 summarize serialized remarks.
514 The tools only support the ``yaml`` format.
516 .. _optviewerpy:
518 opt-viewer.py
519 -------------
521 Output a HTML page which gives visual feedback on compiler interactions with
522 your program.
524     :Examples:
526     ::
528         $ opt-viewer.py my_yaml_file.opt.yaml
530     ::
532         $ opt-viewer.py my_build_dir/
535 opt-stats.py
536 ------------
538 Output statistics about the optimization remarks in the input set.
540     :Example:
542     ::
544         $ opt-stats.py my_yaml_file.opt.yaml
546         Total number of remarks           3
549         Top 10 remarks by pass:
550           inline                         33%
551           asm-printer                    33%
552           prologepilog                   33%
554         Top 10 remarks:
555           asm-printer/InstructionCount   33%
556           inline/NoDefinition            33%
557           prologepilog/StackSize         33%
559 opt-diff.py
560 -----------
562 Produce a new YAML file which contains all of the changes in optimizations
563 between two YAML files.
565 Typically, this tool should be used to do diffs between:
567 * new compiler + fixed source vs old compiler + fixed source
568 * fixed compiler + new source vs fixed compiler + old source
570 This diff file can be displayed using :ref:`opt-viewer.py <optviewerpy>`.
572     :Example:
574     ::
576         $ opt-diff.py my_opt_yaml1.opt.yaml my_opt_yaml2.opt.yaml -o my_opt_diff.opt.yaml
577         $ opt-viewer.py my_opt_diff.opt.yaml
579 .. _remarkssection:
581 Emitting remark diagnostics in the object file
582 ==============================================
584 A section containing metadata on remark diagnostics will be emitted when
585 -remarks-section is passed. The section contains the metadata associated to the
586 format used to serialize the remarks.
588 The section is named:
590 * ``__LLVM,__remarks`` (MachO)
591 * ``.remarks`` (ELF)
593 C API
594 =====
596 LLVM provides a library that can be used to parse remarks through a shared
597 library named ``libRemarks``.
599 The typical usage through the C API is like the following:
601 .. code-block:: c
603     LLVMRemarkParserRef Parser = LLVMRemarkParserCreateYAML(Buf, Size);
604     LLVMRemarkEntryRef Remark = NULL;
605     while ((Remark = LLVMRemarkParserGetNext(Parser))) {
606        // use Remark
607        LLVMRemarkEntryDispose(Remark); // Release memory.
608     }
609     bool HasError = LLVMRemarkParserHasError(Parser);
610     LLVMRemarkParserDispose(Parser);
612 .. FIXME: add documentation for llvm-opt-report.
613 .. FIXME: add documentation for Passes supporting optimization remarks
614 .. FIXME: add documentation for IR Passes
615 .. FIXME: add documentation for CodeGen Passes