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)
116 ``Content configuration``
118 .. option:: -pass-remarks-filter=<regex>
120 Only passes whose name match the given (POSIX) regular expression will be
121 serialized to the final output.
123 .. option:: -pass-remarks-with-hotness
125 With PGO, include profile count in optimization remarks.
127 .. option:: -pass-remarks-hotness-threshold
129 The minimum profile count required for an optimization remark to be
132 Other tools that support remarks:
136 .. option:: -lto-pass-remarks-output=<filename>
137 .. option:: -lto-pass-remarks-filter=<regex>
138 .. option:: -lto-pass-remarks-format=<format>
139 .. option:: -lto-pass-remarks-with-hotness
140 .. option:: -lto-pass-remarks-hotness-threshold
142 :program:`gold-plugin` and :program:`lld`
144 .. option:: -opt-remarks-filename=<filename>
145 .. option:: -opt-remarks-filter=<regex>
146 .. option:: -opt-remarks-format=<format>
147 .. option:: -opt-remarks-with-hotness
154 A typical remark serialized to YAML looks like this:
161 DebugLoc: { File: <file>, Line: <line>, Column: <column> }
166 DebugLoc: { File: <arg-file>, Line: <arg-line>, Column: <arg-column> }
168 The following entries are mandatory:
170 * ``<TYPE>``: can be ``Passed``, ``Missed``, ``Analysis``,
171 ``AnalysisFPCommute``, ``AnalysisAliasing``, ``Failure``.
172 * ``<pass>``: the name of the pass that emitted this remark.
173 * ``<name>``: the name of the remark coming from ``<pass>``.
174 * ``<function>``: the mangled name of the function.
176 If a ``DebugLoc`` entry is specified, the following fields are required:
182 If an ``arg`` entry is specified, the following fields are required:
187 If a ``DebugLoc`` entry is specified within an ``arg`` entry, the following
197 The ``opt-viewer`` directory contains a collection of tools that visualize and
198 summarize serialized remarks.
205 Output a HTML page which gives visual feedback on compiler interactions with
212 $ opt-viewer.py my_yaml_file.opt.yaml
216 $ opt-viewer.py my_build_dir/
222 Output statistics about the optimization remarks in the input set.
228 $ opt-stats.py my_yaml_file.opt.yaml
230 Total number of remarks 3
233 Top 10 remarks by pass:
239 asm-printer/InstructionCount 33%
240 inline/NoDefinition 33%
241 prologepilog/StackSize 33%
246 Produce a new YAML file which contains all of the changes in optimizations
247 between two YAML files.
249 Typically, this tool should be used to do diffs between:
251 * new compiler + fixed source vs old compiler + fixed source
252 * fixed compiler + new source vs fixed compiler + old source
254 This diff file can be displayed using :ref:`opt-viewer.py <optviewerpy>`.
260 $ opt-diff.py my_opt_yaml1.opt.yaml my_opt_yaml2.opt.yaml -o my_opt_diff.opt.yaml
261 $ opt-viewer.py my_opt_diff.opt.yaml
265 Emitting remark diagnostics in the object file
266 ==============================================
268 A section containing metadata on remark diagnostics will be emitted when
269 -remarks-section is passed. The section contains:
271 * a magic number: "REMARKS\\0"
272 * the version number: a little-endian uint64_t
273 * the total size of the string table (the size itself excluded):
274 little-endian uint64_t
275 * a list of null-terminated strings
276 * the absolute file path to the serialized remark diagnostics: a
277 null-terminated string.
279 The section is named:
281 * ``__LLVM,__remarks`` (MachO)
287 LLVM provides a library that can be used to parse remarks through a shared
288 library named ``libRemarks``.
290 The typical usage through the C API is like the following:
294 LLVMRemarkParserRef Parser = LLVMRemarkParserCreateYAML(Buf, Size);
295 LLVMRemarkEntryRef Remark = NULL;
296 while ((Remark = LLVMRemarkParserGetNext(Parser))) {
298 LLVMRemarkEntryDispose(Remark); // Release memory.
300 bool HasError = LLVMRemarkParserHasError(Parser);
301 LLVMRemarkParserDispose(Parser);
303 .. FIXME: add documentation for llvm-opt-report.
304 .. FIXME: add documentation for Passes supporting optimization remarks
305 .. FIXME: add documentation for IR Passes
306 .. FIXME: add documentation for CodeGen Passes