xref: /minix3/external/bsd/llvm/dist/llvm/docs/Bugpoint.rst (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc====================================
2f4a2713aSLionel SambucLLVM bugpoint tool: design and usage
3f4a2713aSLionel Sambuc====================================
4f4a2713aSLionel Sambuc
5f4a2713aSLionel Sambuc.. contents::
6f4a2713aSLionel Sambuc   :local:
7f4a2713aSLionel Sambuc
8f4a2713aSLionel SambucDescription
9f4a2713aSLionel Sambuc===========
10f4a2713aSLionel Sambuc
11f4a2713aSLionel Sambuc``bugpoint`` narrows down the source of problems in LLVM tools and passes.  It
12f4a2713aSLionel Sambuccan be used to debug three types of failures: optimizer crashes, miscompilations
13f4a2713aSLionel Sambucby optimizers, or bad native code generation (including problems in the static
14f4a2713aSLionel Sambucand JIT compilers).  It aims to reduce large test cases to small, useful ones.
15f4a2713aSLionel SambucFor example, if ``opt`` crashes while optimizing a file, it will identify the
16f4a2713aSLionel Sambucoptimization (or combination of optimizations) that causes the crash, and reduce
17f4a2713aSLionel Sambucthe file down to a small example which triggers the crash.
18f4a2713aSLionel Sambuc
19f4a2713aSLionel SambucFor detailed case scenarios, such as debugging ``opt``, or one of the LLVM code
20*0a6a1f1dSLionel Sambucgenerators, see :doc:`HowToSubmitABug`.
21f4a2713aSLionel Sambuc
22f4a2713aSLionel SambucDesign Philosophy
23f4a2713aSLionel Sambuc=================
24f4a2713aSLionel Sambuc
25f4a2713aSLionel Sambuc``bugpoint`` is designed to be a useful tool without requiring any hooks into
26f4a2713aSLionel Sambucthe LLVM infrastructure at all.  It works with any and all LLVM passes and code
27f4a2713aSLionel Sambucgenerators, and does not need to "know" how they work.  Because of this, it may
28f4a2713aSLionel Sambucappear to do stupid things or miss obvious simplifications.  ``bugpoint`` is
29f4a2713aSLionel Sambucalso designed to trade off programmer time for computer time in the
30f4a2713aSLionel Sambuccompiler-debugging process; consequently, it may take a long period of
31f4a2713aSLionel Sambuc(unattended) time to reduce a test case, but we feel it is still worth it. Note
32f4a2713aSLionel Sambucthat ``bugpoint`` is generally very quick unless debugging a miscompilation
33f4a2713aSLionel Sambucwhere each test of the program (which requires executing it) takes a long time.
34f4a2713aSLionel Sambuc
35f4a2713aSLionel SambucAutomatic Debugger Selection
36f4a2713aSLionel Sambuc----------------------------
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc``bugpoint`` reads each ``.bc`` or ``.ll`` file specified on the command line
39f4a2713aSLionel Sambucand links them together into a single module, called the test program.  If any
40f4a2713aSLionel SambucLLVM passes are specified on the command line, it runs these passes on the test
41f4a2713aSLionel Sambucprogram.  If any of the passes crash, or if they produce malformed output (which
42f4a2713aSLionel Sambuccauses the verifier to abort), ``bugpoint`` starts the `crash debugger`_.
43f4a2713aSLionel Sambuc
44f4a2713aSLionel SambucOtherwise, if the ``-output`` option was not specified, ``bugpoint`` runs the
45f4a2713aSLionel Sambuctest program with the "safe" backend (which is assumed to generate good code) to
46f4a2713aSLionel Sambucgenerate a reference output.  Once ``bugpoint`` has a reference output for the
47f4a2713aSLionel Sambuctest program, it tries executing it with the selected code generator.  If the
48f4a2713aSLionel Sambucselected code generator crashes, ``bugpoint`` starts the `crash debugger`_ on
49f4a2713aSLionel Sambucthe code generator.  Otherwise, if the resulting output differs from the
50f4a2713aSLionel Sambucreference output, it assumes the difference resulted from a code generator
51f4a2713aSLionel Sambucfailure, and starts the `code generator debugger`_.
52f4a2713aSLionel Sambuc
53f4a2713aSLionel SambucFinally, if the output of the selected code generator matches the reference
54f4a2713aSLionel Sambucoutput, ``bugpoint`` runs the test program after all of the LLVM passes have
55f4a2713aSLionel Sambucbeen applied to it.  If its output differs from the reference output, it assumes
56f4a2713aSLionel Sambucthe difference resulted from a failure in one of the LLVM passes, and enters the
57f4a2713aSLionel Sambuc`miscompilation debugger`_.  Otherwise, there is no problem ``bugpoint`` can
58f4a2713aSLionel Sambucdebug.
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc.. _crash debugger:
61f4a2713aSLionel Sambuc
62f4a2713aSLionel SambucCrash debugger
63f4a2713aSLionel Sambuc--------------
64f4a2713aSLionel Sambuc
65f4a2713aSLionel SambucIf an optimizer or code generator crashes, ``bugpoint`` will try as hard as it
66f4a2713aSLionel Sambuccan to reduce the list of passes (for optimizer crashes) and the size of the
67f4a2713aSLionel Sambuctest program.  First, ``bugpoint`` figures out which combination of optimizer
68f4a2713aSLionel Sambucpasses triggers the bug. This is useful when debugging a problem exposed by
69f4a2713aSLionel Sambuc``opt``, for example, because it runs over 38 passes.
70f4a2713aSLionel Sambuc
71f4a2713aSLionel SambucNext, ``bugpoint`` tries removing functions from the test program, to reduce its
72f4a2713aSLionel Sambucsize.  Usually it is able to reduce a test program to a single function, when
73f4a2713aSLionel Sambucdebugging intraprocedural optimizations.  Once the number of functions has been
74f4a2713aSLionel Sambucreduced, it attempts to delete various edges in the control flow graph, to
75f4a2713aSLionel Sambucreduce the size of the function as much as possible.  Finally, ``bugpoint``
76f4a2713aSLionel Sambucdeletes any individual LLVM instructions whose absence does not eliminate the
77f4a2713aSLionel Sambucfailure.  At the end, ``bugpoint`` should tell you what passes crash, give you a
78f4a2713aSLionel Sambucbitcode file, and give you instructions on how to reproduce the failure with
79f4a2713aSLionel Sambuc``opt`` or ``llc``.
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc.. _code generator debugger:
82f4a2713aSLionel Sambuc
83f4a2713aSLionel SambucCode generator debugger
84f4a2713aSLionel Sambuc-----------------------
85f4a2713aSLionel Sambuc
86f4a2713aSLionel SambucThe code generator debugger attempts to narrow down the amount of code that is
87f4a2713aSLionel Sambucbeing miscompiled by the selected code generator.  To do this, it takes the test
88f4a2713aSLionel Sambucprogram and partitions it into two pieces: one piece which it compiles with the
89f4a2713aSLionel Sambuc"safe" backend (into a shared object), and one piece which it runs with either
90f4a2713aSLionel Sambucthe JIT or the static LLC compiler.  It uses several techniques to reduce the
91f4a2713aSLionel Sambucamount of code pushed through the LLVM code generator, to reduce the potential
92f4a2713aSLionel Sambucscope of the problem.  After it is finished, it emits two bitcode files (called
93f4a2713aSLionel Sambuc"test" [to be compiled with the code generator] and "safe" [to be compiled with
94f4a2713aSLionel Sambucthe "safe" backend], respectively), and instructions for reproducing the
95f4a2713aSLionel Sambucproblem.  The code generator debugger assumes that the "safe" backend produces
96f4a2713aSLionel Sambucgood code.
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc.. _miscompilation debugger:
99f4a2713aSLionel Sambuc
100f4a2713aSLionel SambucMiscompilation debugger
101f4a2713aSLionel Sambuc-----------------------
102f4a2713aSLionel Sambuc
103f4a2713aSLionel SambucThe miscompilation debugger works similarly to the code generator debugger.  It
104f4a2713aSLionel Sambucworks by splitting the test program into two pieces, running the optimizations
105f4a2713aSLionel Sambucspecified on one piece, linking the two pieces back together, and then executing
106f4a2713aSLionel Sambucthe result.  It attempts to narrow down the list of passes to the one (or few)
107f4a2713aSLionel Sambucwhich are causing the miscompilation, then reduce the portion of the test
108f4a2713aSLionel Sambucprogram which is being miscompiled.  The miscompilation debugger assumes that
109f4a2713aSLionel Sambucthe selected code generator is working properly.
110f4a2713aSLionel Sambuc
111f4a2713aSLionel SambucAdvice for using bugpoint
112f4a2713aSLionel Sambuc=========================
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc``bugpoint`` can be a remarkably useful tool, but it sometimes works in
115f4a2713aSLionel Sambucnon-obvious ways.  Here are some hints and tips:
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc* In the code generator and miscompilation debuggers, ``bugpoint`` only works
118f4a2713aSLionel Sambuc  with programs that have deterministic output.  Thus, if the program outputs
119f4a2713aSLionel Sambuc  ``argv[0]``, the date, time, or any other "random" data, ``bugpoint`` may
120f4a2713aSLionel Sambuc  misinterpret differences in these data, when output, as the result of a
121f4a2713aSLionel Sambuc  miscompilation.  Programs should be temporarily modified to disable outputs
122f4a2713aSLionel Sambuc  that are likely to vary from run to run.
123f4a2713aSLionel Sambuc
124f4a2713aSLionel Sambuc* In the code generator and miscompilation debuggers, debugging will go faster
125f4a2713aSLionel Sambuc  if you manually modify the program or its inputs to reduce the runtime, but
126f4a2713aSLionel Sambuc  still exhibit the problem.
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc* ``bugpoint`` is extremely useful when working on a new optimization: it helps
129f4a2713aSLionel Sambuc  track down regressions quickly.  To avoid having to relink ``bugpoint`` every
130f4a2713aSLionel Sambuc  time you change your optimization however, have ``bugpoint`` dynamically load
131f4a2713aSLionel Sambuc  your optimization with the ``-load`` option.
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc* ``bugpoint`` can generate a lot of output and run for a long period of time.
134f4a2713aSLionel Sambuc  It is often useful to capture the output of the program to file.  For example,
135f4a2713aSLionel Sambuc  in the C shell, you can run:
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc  .. code-block:: console
138f4a2713aSLionel Sambuc
139f4a2713aSLionel Sambuc    $ bugpoint  ... |& tee bugpoint.log
140f4a2713aSLionel Sambuc
141f4a2713aSLionel Sambuc  to get a copy of ``bugpoint``'s output in the file ``bugpoint.log``, as well
142f4a2713aSLionel Sambuc  as on your terminal.
143f4a2713aSLionel Sambuc
144f4a2713aSLionel Sambuc* ``bugpoint`` cannot debug problems with the LLVM linker. If ``bugpoint``
145f4a2713aSLionel Sambuc  crashes before you see its "All input ok" message, you might try ``llvm-link
146f4a2713aSLionel Sambuc  -v`` on the same set of input files. If that also crashes, you may be
147f4a2713aSLionel Sambuc  experiencing a linker bug.
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc* ``bugpoint`` is useful for proactively finding bugs in LLVM.  Invoking
150f4a2713aSLionel Sambuc  ``bugpoint`` with the ``-find-bugs`` option will cause the list of specified
151f4a2713aSLionel Sambuc  optimizations to be randomized and applied to the program. This process will
152f4a2713aSLionel Sambuc  repeat until a bug is found or the user kills ``bugpoint``.
153f4a2713aSLionel Sambuc
154f4a2713aSLionel SambucWhat to do when bugpoint isn't enough
155f4a2713aSLionel Sambuc=====================================
156f4a2713aSLionel Sambuc
157f4a2713aSLionel SambucSometimes, ``bugpoint`` is not enough. In particular, InstCombine and
158f4a2713aSLionel SambucTargetLowering both have visitor structured code with lots of potential
159f4a2713aSLionel Sambuctransformations.  If the process of using bugpoint has left you with still too
160f4a2713aSLionel Sambucmuch code to figure out and the problem seems to be in instcombine, the
161f4a2713aSLionel Sambucfollowing steps may help.  These same techniques are useful with TargetLowering
162f4a2713aSLionel Sambucas well.
163f4a2713aSLionel Sambuc
164f4a2713aSLionel SambucTurn on ``-debug-only=instcombine`` and see which transformations within
165f4a2713aSLionel Sambucinstcombine are firing by selecting out lines with "``IC``" in them.
166f4a2713aSLionel Sambuc
167f4a2713aSLionel SambucAt this point, you have a decision to make.  Is the number of transformations
168f4a2713aSLionel Sambucsmall enough to step through them using a debugger?  If so, then try that.
169f4a2713aSLionel Sambuc
170f4a2713aSLionel SambucIf there are too many transformations, then a source modification approach may
171f4a2713aSLionel Sambucbe helpful.  In this approach, you can modify the source code of instcombine to
172f4a2713aSLionel Sambucdisable just those transformations that are being performed on your test input
173f4a2713aSLionel Sambucand perform a binary search over the set of transformations.  One set of places
174f4a2713aSLionel Sambucto modify are the "``visit*``" methods of ``InstCombiner`` (*e.g.*
175f4a2713aSLionel Sambuc``visitICmpInst``) by adding a "``return false``" as the first line of the
176f4a2713aSLionel Sambucmethod.
177f4a2713aSLionel Sambuc
178f4a2713aSLionel SambucIf that still doesn't remove enough, then change the caller of
179f4a2713aSLionel Sambuc``InstCombiner::DoOneIteration``, ``InstCombiner::runOnFunction`` to limit the
180f4a2713aSLionel Sambucnumber of iterations.
181f4a2713aSLionel Sambuc
182f4a2713aSLionel SambucYou may also find it useful to use "``-stats``" now to see what parts of
183f4a2713aSLionel Sambucinstcombine are firing.  This can guide where to put additional reporting code.
184f4a2713aSLionel Sambuc
185f4a2713aSLionel SambucAt this point, if the amount of transformations is still too large, then
186f4a2713aSLionel Sambucinserting code to limit whether or not to execute the body of the code in the
187f4a2713aSLionel Sambucvisit function can be helpful.  Add a static counter which is incremented on
188f4a2713aSLionel Sambucevery invocation of the function.  Then add code which simply returns false on
189f4a2713aSLionel Sambucdesired ranges.  For example:
190f4a2713aSLionel Sambuc
191f4a2713aSLionel Sambuc.. code-block:: c++
192f4a2713aSLionel Sambuc
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc  static int calledCount = 0;
195f4a2713aSLionel Sambuc  calledCount++;
196f4a2713aSLionel Sambuc  DEBUG(if (calledCount < 212) return false);
197f4a2713aSLionel Sambuc  DEBUG(if (calledCount > 217) return false);
198f4a2713aSLionel Sambuc  DEBUG(if (calledCount == 213) return false);
199f4a2713aSLionel Sambuc  DEBUG(if (calledCount == 214) return false);
200f4a2713aSLionel Sambuc  DEBUG(if (calledCount == 215) return false);
201f4a2713aSLionel Sambuc  DEBUG(if (calledCount == 216) return false);
202f4a2713aSLionel Sambuc  DEBUG(dbgs() << "visitXOR calledCount: " << calledCount << "\n");
203f4a2713aSLionel Sambuc  DEBUG(dbgs() << "I: "; I->dump());
204f4a2713aSLionel Sambuc
205f4a2713aSLionel Sambuccould be added to ``visitXOR`` to limit ``visitXor`` to being applied only to
206f4a2713aSLionel Sambuccalls 212 and 217. This is from an actual test case and raises an important
207f4a2713aSLionel Sambucpoint---a simple binary search may not be sufficient, as transformations that
208f4a2713aSLionel Sambucinteract may require isolating more than one call.  In TargetLowering, use
209f4a2713aSLionel Sambuc``return SDNode();`` instead of ``return false;``.
210f4a2713aSLionel Sambuc
211f4a2713aSLionel SambucNow that that the number of transformations is down to a manageable number, try
212f4a2713aSLionel Sambucexamining the output to see if you can figure out which transformations are
213f4a2713aSLionel Sambucbeing done.  If that can be figured out, then do the usual debugging.  If which
214f4a2713aSLionel Sambuccode corresponds to the transformation being performed isn't obvious, set a
215f4a2713aSLionel Sambucbreakpoint after the call count based disabling and step through the code.
216f4a2713aSLionel SambucAlternatively, you can use "``printf``" style debugging to report waypoints.
217