315 lines · plain
1================================2How to submit an LLVM bug report3================================4 5Introduction - Got bugs?6========================7 8 9If you're working with LLVM and encounter a bug, we definitely want to know10about it. This document describes what you can do to increase the odds of11getting 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 the16bug `crashes the compiler`_ or if the compiler is `miscompiling`_ the program17(i.e., the compiler successfully produces an executable, but it doesn't run18right). Based on what type of bug it is, follow the instructions in the19linked section to narrow down the bug so that the person who fixes it will be20able to find the problem more easily.21 22Once you have a reduced test case, go to `the LLVM Bug Tracking System23<https://github.com/llvm/llvm-project/issues>`_ and fill out the form with the24necessary details (note that you don't need to pick a label, just use if you're25not 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 Git30 repository).31 32Thanks for helping us make LLVM better!33 34.. _crashes the compiler:35 36Crashing Bugs37=============38 39More often than not, bugs in the compiler cause it to crash---often due to40an assertion failure of some sort. The most important piece of the puzzle41is to figure out if it is crashing in the Clang front-end or if it is one of42the LLVM libraries (e.g., the optimizer or code generator) that has43problems.44 45To identify the crashing component (the front-end, middle-end46optimizer, or backend code generator), run the ``clang`` command line as you47were when the crash occurred, but with the following extra command line48options:49 50* ``-emit-llvm -Xclang -disable-llvm-passes``: If ``clang`` still crashes when51 passed these options (which disable the optimizer and code generator), then52 the crash is in the front-end. Jump ahead to :ref:`front-end bugs53 <frontend-crash>`.54 55* ``-emit-llvm``: If ``clang`` crashes with this option (which disables56 the code generator), you've found a middle-end optimizer bug. Jump ahead to57 :ref:`middle-end bugs <middleend-crash>`.58 59* Otherwise, you have a backend code generator crash. Jump ahead to :ref:`code60 generator bugs <backend-crash>`.61 62.. _frontend-crash:63 64Front-end bugs65--------------66 67On a ``clang`` crash, the compiler will dump a preprocessed file and a script68to replay the ``clang`` command. For example, you should see something like69 70.. code-block:: text71 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.c75 clang: note: diagnostic msg: /tmp/foo-xxxxxx.sh76 77The `creduce <https://github.com/csmith-project/creduce>`_ tool helps to78reduce the preprocessed file down to the smallest amount of code that still79replicates the problem. You're encouraged to use creduce to reduce the code80to make the developers' lives easier. The81``clang/utils/creduce-clang-crash.py`` script can be used on the files82that clang dumps to help with automating creating a test to check for the83compiler crash.84 85`cvise <https://github.com/marxin/cvise>`_ is an alternative to ``creduce``.86 87.. _middleend-crash:88 89Middle-end optimization bugs90----------------------------91 92If you find that a bug crashes in the optimizer, compile your test-case to a93``.bc`` file by passing "``-emit-llvm -O1 -Xclang -disable-llvm-passes -c -o94foo.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:: bash99 100 opt -O3 foo.bc -disable-output101 102If this doesn't crash, please follow the instructions for a :ref:`front-end103bug <frontend-crash>`.104 105If this does crash, then you can debug this with the following106:doc:`bugpoint <Bugpoint>` command:107 108.. code-block:: bash109 110 bugpoint foo.bc -O3111 112Run this, then file a bug with the instructions and reduced ``.bc``113files that bugpoint emits.114 115If bugpoint doesn't reproduce the crash,116:doc:`llvm-reduce <CommandGuide/llvm-reduce>` is an alternative way to reduce117LLVM IR. Create a script that reproduces the crash and run:118 119.. code-block:: bash120 121 llvm-reduce --test=path/to/script foo.bc122 123which should produce reduced IR that reproduces the crash.124 125.. TIP::126 ``llvm-reduce`` is still fairly immature and may crash. On the other hand,127 unlike ``bugpoint``, ``llvm-reduce -j $NUM_THREADS`` is multi-threaded and128 can therefore potentially be much faster.129 130If none of the above work, you can get the IR before a crash by running the131``opt`` command with the ``--print-before-all --print-module-scope`` flags to132dump the IR before every pass. Be warned that this is very verbose.133 134.. _backend-crash:135 136Backend code generator bugs137---------------------------138 139If you find a bug that crashes clang in the code generator, compile your140source file to a ``.bc`` file by passing "``-emit-llvm -c -o foo.bc``" to141clang (in addition to the options you already pass). Once you have142``foo.bc``, one of the following commands should fail:143 144#. ``llc foo.bc``145#. ``llc foo.bc -relocation-model=pic``146#. ``llc foo.bc -relocation-model=static``147 148If none of these crash, please follow the instructions for a :ref:`front-end149bug<frontend-crash>`. If one of these crashes, you should be able to reduce150this with one of the following :doc:`bugpoint <Bugpoint>` command lines (use151the one corresponding to the command above that failed):152 153#. ``bugpoint -run-llc foo.bc``154#. ``bugpoint -run-llc foo.bc --tool-args -relocation-model=pic``155#. ``bugpoint -run-llc foo.bc --tool-args -relocation-model=static``156 157Please run this, then file a bug with the instructions and reduced ``.bc`` file158that bugpoint emits. If something goes wrong with bugpoint, please submit159the ``foo.bc`` file and the option that llc crashes with.160 161LTO bugs162---------------------------163 164If you encounter a bug that leads to crashes in the LLVM LTO phase when using165the ``-flto`` option, follow these steps to diagnose and report the issue:166 167Compile your source file to a ``.bc`` (Bitcode) file with the following options,168in addition to your existing compilation options:169 170.. code-block:: bash171 172 export CFLAGS="-flto -fuse-ld=lld" CXXFLAGS="-flto -fuse-ld=lld" LDFLAGS="-Wl,-plugin-opt=save-temps"173 174These options enable LTO and save temporary files generated during compilation175for later analysis.176 177On Windows, use lld-link as the linker. Adjust your compilation 178flags as follows:179* Add ``/lldsavetemps`` to the linker flags.180* When linking from the compiler driver, add ``/link /lldsavetemps`` in order to forward that flag to the linker.181 182Using the specified flags will generate four intermediate bytecode files:183 184#. a.out.0.0.preopt.bc (Before any link-time optimizations (LTO) are applied)185#. a.out.0.2.internalize.bc (After initial optimizations are applied)186#. a.out.0.4.opt.bc (After an extensive set of optimizations)187#. a.out.0.5.precodegen.bc (After LTO but before translating into machine code)188 189Execute one of the following commands to identify the source of the problem:190 191#. ``opt "-passes=lto<O3>" a.out.0.2.internalize.bc``192#. ``llc a.out.0.5.precodegen.bc``193 194If one of these do crash, you should be able to reduce195this with :program:`llvm-reduce`196command line (use the bc file corresponding to the command above that failed):197 198.. code-block:: bash199 200 llvm-reduce --test reduce.sh a.out.0.2.internalize.bc201 202Example of ``reduce.sh`` script203 204.. code-block:: bash205 206 $ cat reduce.sh207 #!/bin/bash -e208 209 path/to/not --crash path/to/opt "-passes=lto<O3>" $1 -o temp.bc 2> err.log210 grep -q "It->second == &Insn" err.log211 212Here we have grepped for the failed assert message.213 214Please run this, then file a bug with the instructions and reduced ``.bc`` file215that llvm-reduce emits.216 217.. _miscompiling:218 219Miscompilations220===============221 222If clang successfully produces an executable, but that executable doesn't run223right, this is either a bug in the code or a bug in the compiler. The first224thing to check is to make sure it is not using undefined behavior (e.g.,225reading a variable before it is defined). In particular, check to see if the226program is clean under various `sanitizers227<https://github.com/google/sanitizers>`_ (e.g., ``clang228-fsanitize=undefined,address``) and `valgrind <http://valgrind.org/>`_. Many229"LLVM bugs" that we have chased down ended up being bugs in the program being230compiled, not LLVM.231 232Once you determine that the program itself is not buggy, you should choose233which code generator you wish to compile the program with (e.g., LLC or the JIT)234and optionally a series of LLVM passes to run. For example:235 236.. code-block:: bash237 238 bugpoint -run-llc [... optzn passes ...] file-to-test.bc --args -- [program arguments]239 240bugpoint will try to narrow down your list of passes to the one pass that241causes an error, and simplify the bitcode file as much as it can to assist242you. It will print a message letting you know how to reproduce the243resulting error.244 245The :doc:`OptBisect <OptBisect>` page shows an alternative method for finding246incorrect optimization passes.247 248Incorrect code generation249=========================250 251Similarly to debugging incorrect compilation by mis-behaving passes, you252can debug incorrect code generation by either LLC or the JIT, using253``bugpoint``. The process ``bugpoint`` follows in this case is to try to254narrow the code down to a function that is miscompiled by one or the other255method, but since for correctness, the entire program must be run,256``bugpoint`` will compile the code it deems to not be affected with the C257Backend, and then link in the shared object it generates.258 259To debug the JIT:260 261.. code-block:: bash262 263 bugpoint -run-jit -output=[correct output file] [bitcode file] \264 --tool-args -- [arguments to pass to lli] \265 --args -- [program arguments]266 267Similarly, to debug the LLC, one would run:268 269.. code-block:: bash270 271 bugpoint -run-llc -output=[correct output file] [bitcode file] \272 --tool-args -- [arguments to pass to llc] \273 --args -- [program arguments]274 275**Special note:** if you are debugging MultiSource or SPEC tests that276already exist in the ``llvm/test`` hierarchy, there is an easier way to277debug the JIT, LLC, and CBE, using the pre-written Makefile targets, which278will pass the program options specified in the Makefiles:279 280.. code-block:: bash281 282 cd llvm/test/../../program283 make bugpoint-jit284 285At the end of a successful ``bugpoint`` run, you will be presented286with two bitcode files: a *safe* file which can be compiled with the C287backend and the *test* file which either LLC or the JIT288mis-codegenerates, and thus causes the error.289 290To reproduce the error that ``bugpoint`` found, it is sufficient to do291the following:292 293#. Regenerate the shared object from the safe bitcode file:294 295 .. code-block:: bash296 297 llc -march=c safe.bc -o safe.c298 gcc -shared safe.c -o safe.so299 300#. If debugging LLC, compile test bitcode native and link with the shared301 object:302 303 .. code-block:: bash304 305 llc test.bc -o test.s306 gcc test.s safe.so -o test.llc307 ./test.llc [program options]308 309#. If debugging the JIT, load the shared object and supply the test310 bitcode:311 312 .. code-block:: bash313 314 lli -load=safe.so test.bc [program options]315