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