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>`
117 ``Content configuration``
119 .. option:: -pass-remarks-filter=<regex>
121 Only passes whose name match the given (POSIX) regular expression will be
122 serialized to the final output.
124 .. option:: -pass-remarks-with-hotness
126 With PGO, include profile count in optimization remarks.
128 .. option:: -pass-remarks-hotness-threshold
130 The minimum profile count required for an optimization remark to be
133 Other tools that support remarks:
137 .. option:: -lto-pass-remarks-output=<filename>
138 .. option:: -lto-pass-remarks-filter=<regex>
139 .. option:: -lto-pass-remarks-format=<format>
140 .. option:: -lto-pass-remarks-with-hotness
141 .. option:: -lto-pass-remarks-hotness-threshold
143 :program:`gold-plugin` and :program:`lld`
145 .. option:: -opt-remarks-filename=<filename>
146 .. option:: -opt-remarks-filter=<regex>
147 .. option:: -opt-remarks-format=<format>
148 .. option:: -opt-remarks-with-hotness
155 A typical remark serialized to YAML looks like this:
162 DebugLoc: { File: <file>, Line: <line>, Column: <column> }
167 DebugLoc: { File: <arg-file>, Line: <arg-line>, Column: <arg-column> }
169 The following entries are mandatory:
171 * ``<TYPE>``: can be ``Passed``, ``Missed``, ``Analysis``,
172 ``AnalysisFPCommute``, ``AnalysisAliasing``, ``Failure``.
173 * ``<pass>``: the name of the pass that emitted this remark.
174 * ``<name>``: the name of the remark coming from ``<pass>``.
175 * ``<function>``: the mangled name of the function.
177 If a ``DebugLoc`` entry is specified, the following fields are required:
183 If an ``arg`` entry is specified, the following fields are required:
188 If a ``DebugLoc`` entry is specified within an ``arg`` entry, the following
195 .. _yamlstrtabremarks:
197 YAML with a string table
198 ------------------------
200 The YAML serialization supports the usage of a string table by using the
201 ``yaml-strtab`` format.
203 This format replaces strings in the YAML output with integers representing the
204 index in the string table that can be provided separately through metadata.
206 The following entries can take advantage of the string table while respecting
216 Currently, none of the tools in :ref:`the opt-viewer directory <optviewer>`
224 The ``opt-viewer`` directory contains a collection of tools that visualize and
225 summarize serialized remarks.
227 The tools only support the ``yaml`` format.
234 Output a HTML page which gives visual feedback on compiler interactions with
241 $ opt-viewer.py my_yaml_file.opt.yaml
245 $ opt-viewer.py my_build_dir/
251 Output statistics about the optimization remarks in the input set.
257 $ opt-stats.py my_yaml_file.opt.yaml
259 Total number of remarks 3
262 Top 10 remarks by pass:
268 asm-printer/InstructionCount 33%
269 inline/NoDefinition 33%
270 prologepilog/StackSize 33%
275 Produce a new YAML file which contains all of the changes in optimizations
276 between two YAML files.
278 Typically, this tool should be used to do diffs between:
280 * new compiler + fixed source vs old compiler + fixed source
281 * fixed compiler + new source vs fixed compiler + old source
283 This diff file can be displayed using :ref:`opt-viewer.py <optviewerpy>`.
289 $ opt-diff.py my_opt_yaml1.opt.yaml my_opt_yaml2.opt.yaml -o my_opt_diff.opt.yaml
290 $ opt-viewer.py my_opt_diff.opt.yaml
294 Emitting remark diagnostics in the object file
295 ==============================================
297 A section containing metadata on remark diagnostics will be emitted when
298 -remarks-section is passed. The section contains:
300 * a magic number: "REMARKS\\0"
301 * the version number: a little-endian uint64_t
302 * the total size of the string table (the size itself excluded):
303 little-endian uint64_t
304 * a list of null-terminated strings
305 * the absolute file path to the serialized remark diagnostics: a
306 null-terminated string.
308 The section is named:
310 * ``__LLVM,__remarks`` (MachO)
316 LLVM provides a library that can be used to parse remarks through a shared
317 library named ``libRemarks``.
319 The typical usage through the C API is like the following:
323 LLVMRemarkParserRef Parser = LLVMRemarkParserCreateYAML(Buf, Size);
324 LLVMRemarkEntryRef Remark = NULL;
325 while ((Remark = LLVMRemarkParserGetNext(Parser))) {
327 LLVMRemarkEntryDispose(Remark); // Release memory.
329 bool HasError = LLVMRemarkParserHasError(Parser);
330 LLVMRemarkParserDispose(Parser);
332 .. FIXME: add documentation for llvm-opt-report.
333 .. FIXME: add documentation for Passes supporting optimization remarks
334 .. FIXME: add documentation for IR Passes
335 .. FIXME: add documentation for CodeGen Passes