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:
19 Remarks that describe a successful optimization performed by the compiler.
25 foo inlined into bar with (cost=always): always inline attribute
29 Remarks that describe an attempt to an optimization by the compiler that
30 could not be performed.
36 foo not inlined into bar because it should never be inlined
37 (cost=never): noinline function attribute
41 Remarks that describe the result of an analysis, that can bring more
42 information to the user regarding the generated code.
48 16 stack bytes in function
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.
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)
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.
85 While diagnostics are useful during development, it is often more useful to
86 refer to optimization remarks post-compilation, typically during performance
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
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.
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
134 Other tools that support remarks:
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
154 There are two modes available for serializing remarks:
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.
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.
176 A typical remark serialized to YAML looks like this:
183 DebugLoc: { File: <file>, Line: <line>, Column: <column> }
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:
204 If an ``arg`` entry is specified, the following fields are required:
209 If a ``DebugLoc`` entry is specified within an ``arg`` entry, the following
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
237 Currently, none of the tools in :ref:`the opt-viewer directory <optviewer>`
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
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
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:
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.
298 .. _bitstreamremarksrecordmetaremarkversion:
300 ``RECORD_META_REMARK_VERSION``
302 The version of the remark entries. This can change independently from the
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:
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
338 +---------------+---------------------------+
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 +--------+---------------------------+
356 +--------+---------------------------+
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 +--------+---------------------------+
380 +--------+---------------------------+
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 +--------+---------------------------+
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``
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'
475 ``SeparateRemarksFile``
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/>
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/>
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'
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/>
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.
521 Output a HTML page which gives visual feedback on compiler interactions with
528 $ opt-viewer.py my_yaml_file.opt.yaml
532 $ opt-viewer.py my_build_dir/
538 Output statistics about the optimization remarks in the input set.
544 $ opt-stats.py my_yaml_file.opt.yaml
546 Total number of remarks 3
549 Top 10 remarks by pass:
555 asm-printer/InstructionCount 33%
556 inline/NoDefinition 33%
557 prologepilog/StackSize 33%
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>`.
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
581 Emitting remark diagnostics in the object file
582 ==============================================
584 A section containing metadata on remark diagnostics will be emitted for the
590 This can be overridden by using the flag ``-remarks-section=<bool>``.
592 The section is named:
594 * ``__LLVM,__remarks`` (MachO)
599 LLVM provides a library that can be used to parse remarks through a shared
600 library named ``libRemarks``.
602 The typical usage through the C API is like the following:
606 LLVMRemarkParserRef Parser = LLVMRemarkParserCreateYAML(Buf, Size);
607 LLVMRemarkEntryRef Remark = NULL;
608 while ((Remark = LLVMRemarkParserGetNext(Parser))) {
610 LLVMRemarkEntryDispose(Remark); // Release memory.
612 bool HasError = LLVMRemarkParserHasError(Parser);
613 LLVMRemarkParserDispose(Parser);
618 The ``RemarkStreamer`` interface is used to unify the serialization
619 capabilities of remarks across all the components that can generate remarks.
621 All remark serialization should go through the main remark streamer, the
622 ``llvm::remarks::RemarkStreamer`` set up in the ``LLVMContext``. The interface
623 takes remark objects converted to ``llvm::remarks::Remark``, and takes care of
624 serializing it to the requested format, using the requested type of metadata,
627 Typically, a specialized remark streamer will hold a reference to the one set
628 up in the ``LLVMContext``, and will operate on its own type of diagnostics.
630 For example, LLVM IR passes will emit ``llvm::DiagnosticInfoOptimization*``
631 that get converted to ``llvm::remarks::Remark`` objects. Then, clang could set
632 up its own specialized remark streamer that takes ``clang::Diagnostic``
633 objects. This can allow various components of the frontend to emit remarks
634 using the same techniques as the LLVM remarks.
636 This gives us the following advantages:
638 * Composition: during the compilation pipeline, multiple components can set up
639 their specialized remark streamers that all emit remarks through the same
641 * Re-using the remark infrastructure in ``lib/Remarks``.
642 * Using the same file and format for the remark emitters created throughout the
645 at the cost of an extra layer of abstraction.
647 .. FIXME: add documentation for llvm-opt-report.
648 .. FIXME: add documentation for Passes supporting optimization remarks
649 .. FIXME: add documentation for IR Passes
650 .. FIXME: add documentation for CodeGen Passes