xref: /llvm-project/llvm/docs/HowToSubmitABug.rst (revision 8e2b3309a975748649a504620a9600da9fe3c837)
1================================
2How to submit an LLVM bug report
3================================
4
5Introduction - Got bugs?
6========================
7
8
9If you're working with LLVM and run into a bug, we definitely want to know
10about it.  This document describes what you can do to increase the odds of
11getting it fixed quickly.
12
13�� If you believe that the bug is security related, please follow :ref:`report-security-issue`. ��
14
15Basically you have to do two things at a minimum. First, decide whether the
16bug `crashes the compiler`_ or if the compiler is `miscompiling`_ the program
17(i.e., the compiler successfully produces an executable, but it doesn't run
18right). Based on what type of bug it is, follow the instructions in the
19linked section to narrow down the bug so that the person who fixes it will be
20able to find the problem more easily.
21
22Once you have a reduced test-case, go to `the LLVM Bug Tracking System
23<https://github.com/llvm/llvm-project/issues>`_ and fill out the form with the
24necessary details (note that you don't need to pick a label, just use if you're
25not sure).  The bug description should contain the following information:
26
27* All information necessary to reproduce the problem.
28* The reduced test-case that triggers the bug.
29* The location where you obtained LLVM (if not from our Git
30  repository).
31
32Thanks for helping us make LLVM better!
33
34.. _crashes the compiler:
35
36Crashing Bugs
37=============
38
39More often than not, bugs in the compiler cause it to crash---often due to
40an assertion failure of some sort. The most important piece of the puzzle
41is to figure out if it is crashing in the Clang front-end or if it is one of
42the LLVM libraries (e.g. the optimizer or code generator) that has
43problems.
44
45To figure out which component is crashing (the front-end, middle-end
46optimizer, or backend code generator), run the ``clang`` command line as you
47were when the crash occurred, but with the following extra command line
48options:
49
50* ``-emit-llvm -Xclang -disable-llvm-passes``: If ``clang`` still crashes when
51  passed these options (which disable the optimizer and code generator), then
52  the crash is in the front-end. Jump ahead to :ref:`front-end bugs
53  <frontend-crash>`.
54
55* ``-emit-llvm``: If ``clang`` crashes with this option (which disables
56  the code generator), you found a middle-end optimizer bug. Jump ahead to
57  :ref:`middle-end bugs <middleend-crash>`.
58
59* Otherwise, you have a backend code generator crash. Jump ahead to :ref:`code
60  generator bugs <backend-crash>`.
61
62.. _frontend-crash:
63
64Front-end bugs
65--------------
66
67On a ``clang`` crash, the compiler will dump a preprocessed file and a script
68to replay the ``clang`` command. For example, you should see something like
69
70.. code-block:: text
71
72   PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
73   Preprocessed source(s) and associated run script(s) are located at:
74   clang: note: diagnostic msg: /tmp/foo-xxxxxx.c
75   clang: note: diagnostic msg: /tmp/foo-xxxxxx.sh
76
77The `creduce <https://github.com/csmith-project/creduce>`_ tool helps to
78reduce the preprocessed file down to the smallest amount of code that still
79replicates the problem. You're encouraged to use creduce to reduce the code
80to make the developers' lives easier. The
81``clang/utils/creduce-clang-crash.py`` script can be used on the files
82that clang dumps to help with automating creating a test to check for the
83compiler crash.
84
85`cvise <https://github.com/marxin/cvise>`_ is an alternative to ``creduce``.
86
87.. _middleend-crash:
88
89Middle-end optimization bugs
90----------------------------
91
92If you find that a bug crashes in the optimizer, compile your test-case to a
93``.bc`` file by passing "``-emit-llvm -O1 -Xclang -disable-llvm-passes -c -o
94foo.bc``". The ``-O1`` is important because ``-O0`` adds the ``optnone``
95function attribute to all functions and many passes don't run on ``optnone``
96functions. Then run:
97
98.. code-block:: bash
99
100   opt -O3 foo.bc -disable-output
101
102If this doesn't crash, please follow the instructions for a :ref:`front-end
103bug <frontend-crash>`.
104
105If this does crash, then you should be able to debug this with the following
106:doc:`bugpoint <Bugpoint>` command:
107
108.. code-block:: bash
109
110   bugpoint foo.bc -O3
111
112Run this, then file a bug with the instructions and reduced .bc
113files that bugpoint emits.
114
115If bugpoint doesn't reproduce the crash, ``llvm-reduce`` is an alternative
116way to reduce LLVM IR. Create a script that repros the crash and run:
117
118.. code-block:: bash
119
120   llvm-reduce --test=path/to/script foo.bc
121
122which should produce reduced IR that reproduces the crash. Be warned the
123``llvm-reduce`` is still fairly immature and may crash.
124
125If none of the above work, you can get the IR before a crash by running the
126``opt`` command with the ``--print-before-all --print-module-scope`` flags to
127dump the IR before every pass. Be warned that this is very verbose.
128
129.. _backend-crash:
130
131Backend code generator bugs
132---------------------------
133
134If you find a bug that crashes clang in the code generator, compile your
135source file to a .bc file by passing "``-emit-llvm -c -o foo.bc``" to
136clang (in addition to the options you already pass).  Once your have
137foo.bc, one of the following commands should fail:
138
139#. ``llc foo.bc``
140#. ``llc foo.bc -relocation-model=pic``
141#. ``llc foo.bc -relocation-model=static``
142
143If none of these crash, please follow the instructions for a :ref:`front-end
144bug<frontend-crash>`. If one of these do crash, you should be able to reduce
145this with one of the following :doc:`bugpoint <Bugpoint>` command lines (use
146the one corresponding to the command above that failed):
147
148#. ``bugpoint -run-llc foo.bc``
149#. ``bugpoint -run-llc foo.bc --tool-args -relocation-model=pic``
150#. ``bugpoint -run-llc foo.bc --tool-args -relocation-model=static``
151
152Please run this, then file a bug with the instructions and reduced .bc file
153that bugpoint emits.  If something goes wrong with bugpoint, please submit
154the "foo.bc" file and the option that llc crashes with.
155
156LTO bugs
157---------------------------
158
159If you encounter a bug that leads to crashes in the LLVM LTO phase when using
160the ``-flto`` option, follow these steps to diagnose and report the issue:
161
162Compile your source file to a ``.bc`` (Bitcode) file with the following options,
163in addition to your existing compilation options:
164
165.. code-block:: bash
166
167   export CFLAGS="-flto -fuse-ld=lld" CXXFLAGS="-flto -fuse-ld=lld" LDFLAGS="-Wl,-plugin-opt=save-temps"
168
169These options enable LTO and save temporary files generated during compilation
170for later analysis.
171
172On Windows, you should be using lld-link as the linker. Adjust your compilation
173flags as follows:
174* Add ``/lldsavetemps`` to the linker flags.
175* When linking from the compiler driver, add ``/link /lldsavetemps`` in order to forward that flag to the linker.
176
177Using the specified flags will generate four intermediate bytecode files:
178
179#. a.out.0.0.preopt.bc (Before any link-time optimizations (LTO) are applied)
180#. a.out.0.2.internalize.bc (After initial optimizations are applied)
181#. a.out.0.4.opt.bc (After an extensive set of optimizations)
182#. a.out.0.5.precodegen.bc (After LTO but before translating into machine code)
183
184Execute one of the following commands to identify the source of the problem:
185
186#. ``opt "-passes=lto<O3>" a.out.0.2.internalize.bc``
187#. ``llc a.out.0.5.precodegen.bc``
188
189If one of these do crash, you should be able to reduce
190this with :program:`llvm-reduce`
191command line (use the bc file corresponding to the command above that failed):
192
193.. code-block:: bash
194
195   llvm-reduce --test reduce.sh a.out.0.2.internalize.bc
196
197Example of reduce.sh script
198
199.. code-block:: bash
200
201   $ cat reduce.sh
202   #!/bin/bash -e
203
204   path/to/not --crash path/to/opt "-passes=lto<O3>" $1 -o temp.bc  2> err.log
205   grep -q "It->second == &Insn" err.log
206
207Here we have grepped the failed assert message.
208
209Please run this, then file a bug with the instructions and reduced .bc file
210that llvm-reduce emits.
211
212.. _miscompiling:
213
214Miscompilations
215===============
216
217If clang successfully produces an executable, but that executable doesn't run
218right, this is either a bug in the code or a bug in the compiler. The first
219thing to check is to make sure it is not using undefined behavior (e.g.
220reading a variable before it is defined). In particular, check to see if the
221program is clean under various `sanitizers
222<https://github.com/google/sanitizers>`_ (e.g. ``clang
223-fsanitize=undefined,address``) and `valgrind <http://valgrind.org/>`_. Many
224"LLVM bugs" that we have chased down ended up being bugs in the program being
225compiled, not LLVM.
226
227Once you determine that the program itself is not buggy, you should choose
228which code generator you wish to compile the program with (e.g. LLC or the JIT)
229and optionally a series of LLVM passes to run.  For example:
230
231.. code-block:: bash
232
233   bugpoint -run-llc [... optzn passes ...] file-to-test.bc --args -- [program arguments]
234
235bugpoint will try to narrow down your list of passes to the one pass that
236causes an error, and simplify the bitcode file as much as it can to assist
237you. It will print a message letting you know how to reproduce the
238resulting error.
239
240The :doc:`OptBisect <OptBisect>` page shows an alternative method for finding
241incorrect optimization passes.
242
243Incorrect code generation
244=========================
245
246Similarly to debugging incorrect compilation by mis-behaving passes, you
247can debug incorrect code generation by either LLC or the JIT, using
248``bugpoint``. The process ``bugpoint`` follows in this case is to try to
249narrow the code down to a function that is miscompiled by one or the other
250method, but since for correctness, the entire program must be run,
251``bugpoint`` will compile the code it deems to not be affected with the C
252Backend, and then link in the shared object it generates.
253
254To debug the JIT:
255
256.. code-block:: bash
257
258   bugpoint -run-jit -output=[correct output file] [bitcode file]  \
259            --tool-args -- [arguments to pass to lli]              \
260            --args -- [program arguments]
261
262Similarly, to debug the LLC, one would run:
263
264.. code-block:: bash
265
266   bugpoint -run-llc -output=[correct output file] [bitcode file]  \
267            --tool-args -- [arguments to pass to llc]              \
268            --args -- [program arguments]
269
270**Special note:** if you are debugging MultiSource or SPEC tests that
271already exist in the ``llvm/test`` hierarchy, there is an easier way to
272debug the JIT, LLC, and CBE, using the pre-written Makefile targets, which
273will pass the program options specified in the Makefiles:
274
275.. code-block:: bash
276
277   cd llvm/test/../../program
278   make bugpoint-jit
279
280At the end of a successful ``bugpoint`` run, you will be presented
281with two bitcode files: a *safe* file which can be compiled with the C
282backend and the *test* file which either LLC or the JIT
283mis-codegenerates, and thus causes the error.
284
285To reproduce the error that ``bugpoint`` found, it is sufficient to do
286the following:
287
288#. Regenerate the shared object from the safe bitcode file:
289
290   .. code-block:: bash
291
292      llc -march=c safe.bc -o safe.c
293      gcc -shared safe.c -o safe.so
294
295#. If debugging LLC, compile test bitcode native and link with the shared
296   object:
297
298   .. code-block:: bash
299
300      llc test.bc -o test.s
301      gcc test.s safe.so -o test.llc
302      ./test.llc [program options]
303
304#. If debugging the JIT, load the shared object and supply the test
305   bitcode:
306
307   .. code-block:: bash
308
309      lli -load=safe.so test.bc [program options]
310