Bugpoint.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ====================================
  2. LLVM bugpoint tool: design and usage
  3. ====================================
  4. .. contents::
  5. :local:
  6. Description
  7. ===========
  8. ``bugpoint`` narrows down the source of problems in LLVM tools and passes. It
  9. can be used to debug three types of failures: optimizer crashes, miscompilations
  10. by optimizers, or bad native code generation (including problems in the static
  11. and JIT compilers). It aims to reduce large test cases to small, useful ones.
  12. For example, if ``opt`` crashes while optimizing a file, it will identify the
  13. optimization (or combination of optimizations) that causes the crash, and reduce
  14. the file down to a small example which triggers the crash.
  15. For detailed case scenarios, such as debugging ``opt``, or one of the LLVM code
  16. generators, see :doc:`HowToSubmitABug`.
  17. Design Philosophy
  18. =================
  19. ``bugpoint`` is designed to be a useful tool without requiring any hooks into
  20. the LLVM infrastructure at all. It works with any and all LLVM passes and code
  21. generators, and does not need to "know" how they work. Because of this, it may
  22. appear to do stupid things or miss obvious simplifications. ``bugpoint`` is
  23. also designed to trade off programmer time for computer time in the
  24. compiler-debugging process; consequently, it may take a long period of
  25. (unattended) time to reduce a test case, but we feel it is still worth it. Note
  26. that ``bugpoint`` is generally very quick unless debugging a miscompilation
  27. where each test of the program (which requires executing it) takes a long time.
  28. Automatic Debugger Selection
  29. ----------------------------
  30. ``bugpoint`` reads each ``.bc`` or ``.ll`` file specified on the command line
  31. and links them together into a single module, called the test program. If any
  32. LLVM passes are specified on the command line, it runs these passes on the test
  33. program. If any of the passes crash, or if they produce malformed output (which
  34. causes the verifier to abort), ``bugpoint`` starts the `crash debugger`_.
  35. Otherwise, if the ``-output`` option was not specified, ``bugpoint`` runs the
  36. test program with the "safe" backend (which is assumed to generate good code) to
  37. generate a reference output. Once ``bugpoint`` has a reference output for the
  38. test program, it tries executing it with the selected code generator. If the
  39. selected code generator crashes, ``bugpoint`` starts the `crash debugger`_ on
  40. the code generator. Otherwise, if the resulting output differs from the
  41. reference output, it assumes the difference resulted from a code generator
  42. failure, and starts the `code generator debugger`_.
  43. Finally, if the output of the selected code generator matches the reference
  44. output, ``bugpoint`` runs the test program after all of the LLVM passes have
  45. been applied to it. If its output differs from the reference output, it assumes
  46. the difference resulted from a failure in one of the LLVM passes, and enters the
  47. `miscompilation debugger`_. Otherwise, there is no problem ``bugpoint`` can
  48. debug.
  49. .. _crash debugger:
  50. Crash debugger
  51. --------------
  52. If an optimizer or code generator crashes, ``bugpoint`` will try as hard as it
  53. can to reduce the list of passes (for optimizer crashes) and the size of the
  54. test program. First, ``bugpoint`` figures out which combination of optimizer
  55. passes triggers the bug. This is useful when debugging a problem exposed by
  56. ``opt``, for example, because it runs over 38 passes.
  57. Next, ``bugpoint`` tries removing functions from the test program, to reduce its
  58. size. Usually it is able to reduce a test program to a single function, when
  59. debugging intraprocedural optimizations. Once the number of functions has been
  60. reduced, it attempts to delete various edges in the control flow graph, to
  61. reduce the size of the function as much as possible. Finally, ``bugpoint``
  62. deletes any individual LLVM instructions whose absence does not eliminate the
  63. failure. At the end, ``bugpoint`` should tell you what passes crash, give you a
  64. bitcode file, and give you instructions on how to reproduce the failure with
  65. ``opt`` or ``llc``.
  66. .. _code generator debugger:
  67. Code generator debugger
  68. -----------------------
  69. The code generator debugger attempts to narrow down the amount of code that is
  70. being miscompiled by the selected code generator. To do this, it takes the test
  71. program and partitions it into two pieces: one piece which it compiles with the
  72. "safe" backend (into a shared object), and one piece which it runs with either
  73. the JIT or the static LLC compiler. It uses several techniques to reduce the
  74. amount of code pushed through the LLVM code generator, to reduce the potential
  75. scope of the problem. After it is finished, it emits two bitcode files (called
  76. "test" [to be compiled with the code generator] and "safe" [to be compiled with
  77. the "safe" backend], respectively), and instructions for reproducing the
  78. problem. The code generator debugger assumes that the "safe" backend produces
  79. good code.
  80. .. _miscompilation debugger:
  81. Miscompilation debugger
  82. -----------------------
  83. The miscompilation debugger works similarly to the code generator debugger. It
  84. works by splitting the test program into two pieces, running the optimizations
  85. specified on one piece, linking the two pieces back together, and then executing
  86. the result. It attempts to narrow down the list of passes to the one (or few)
  87. which are causing the miscompilation, then reduce the portion of the test
  88. program which is being miscompiled. The miscompilation debugger assumes that
  89. the selected code generator is working properly.
  90. Advice for using bugpoint
  91. =========================
  92. ``bugpoint`` can be a remarkably useful tool, but it sometimes works in
  93. non-obvious ways. Here are some hints and tips:
  94. * In the code generator and miscompilation debuggers, ``bugpoint`` only works
  95. with programs that have deterministic output. Thus, if the program outputs
  96. ``argv[0]``, the date, time, or any other "random" data, ``bugpoint`` may
  97. misinterpret differences in these data, when output, as the result of a
  98. miscompilation. Programs should be temporarily modified to disable outputs
  99. that are likely to vary from run to run.
  100. * In the `crash debugger`_, ``bugpoint`` does not distiguish different crashes
  101. during reduction. Thus, if new crash or miscompilation happens, ``bugpoint``
  102. will continue with the new crash instead. If you would like to stick to
  103. particular crash, you should write check scripts to validate the error
  104. message, see ``-compile-command`` in :doc:`CommandGuide/bugpoint`.
  105. * In the code generator and miscompilation debuggers, debugging will go faster
  106. if you manually modify the program or its inputs to reduce the runtime, but
  107. still exhibit the problem.
  108. * ``bugpoint`` is extremely useful when working on a new optimization: it helps
  109. track down regressions quickly. To avoid having to relink ``bugpoint`` every
  110. time you change your optimization however, have ``bugpoint`` dynamically load
  111. your optimization with the ``-load`` option.
  112. * ``bugpoint`` can generate a lot of output and run for a long period of time.
  113. It is often useful to capture the output of the program to file. For example,
  114. in the C shell, you can run:
  115. .. code-block:: console
  116. $ bugpoint ... |& tee bugpoint.log
  117. to get a copy of ``bugpoint``'s output in the file ``bugpoint.log``, as well
  118. as on your terminal.
  119. * ``bugpoint`` cannot debug problems with the LLVM linker. If ``bugpoint``
  120. crashes before you see its "All input ok" message, you might try ``llvm-link
  121. -v`` on the same set of input files. If that also crashes, you may be
  122. experiencing a linker bug.
  123. * ``bugpoint`` is useful for proactively finding bugs in LLVM. Invoking
  124. ``bugpoint`` with the ``-find-bugs`` option will cause the list of specified
  125. optimizations to be randomized and applied to the program. This process will
  126. repeat until a bug is found or the user kills ``bugpoint``.
  127. * ``bugpoint`` can produce IR which contains long names. Run ``opt
  128. -metarenamer`` over the IR to rename everything using easy-to-read,
  129. metasyntactic names. Alternatively, run ``opt -strip -instnamer`` to rename
  130. everything with very short (often purely numeric) names.
  131. What to do when bugpoint isn't enough
  132. =====================================
  133. Sometimes, ``bugpoint`` is not enough. In particular, InstCombine and
  134. TargetLowering both have visitor structured code with lots of potential
  135. transformations. If the process of using bugpoint has left you with still too
  136. much code to figure out and the problem seems to be in instcombine, the
  137. following steps may help. These same techniques are useful with TargetLowering
  138. as well.
  139. Turn on ``-debug-only=instcombine`` and see which transformations within
  140. instcombine are firing by selecting out lines with "``IC``" in them.
  141. At this point, you have a decision to make. Is the number of transformations
  142. small enough to step through them using a debugger? If so, then try that.
  143. If there are too many transformations, then a source modification approach may
  144. be helpful. In this approach, you can modify the source code of instcombine to
  145. disable just those transformations that are being performed on your test input
  146. and perform a binary search over the set of transformations. One set of places
  147. to modify are the "``visit*``" methods of ``InstCombiner`` (*e.g.*
  148. ``visitICmpInst``) by adding a "``return false``" as the first line of the
  149. method.
  150. If that still doesn't remove enough, then change the caller of
  151. ``InstCombiner::DoOneIteration``, ``InstCombiner::runOnFunction`` to limit the
  152. number of iterations.
  153. You may also find it useful to use "``-stats``" now to see what parts of
  154. instcombine are firing. This can guide where to put additional reporting code.
  155. At this point, if the amount of transformations is still too large, then
  156. inserting code to limit whether or not to execute the body of the code in the
  157. visit function can be helpful. Add a static counter which is incremented on
  158. every invocation of the function. Then add code which simply returns false on
  159. desired ranges. For example:
  160. .. code-block:: c++
  161. static int calledCount = 0;
  162. calledCount++;
  163. LLVM_DEBUG(if (calledCount < 212) return false);
  164. LLVM_DEBUG(if (calledCount > 217) return false);
  165. LLVM_DEBUG(if (calledCount == 213) return false);
  166. LLVM_DEBUG(if (calledCount == 214) return false);
  167. LLVM_DEBUG(if (calledCount == 215) return false);
  168. LLVM_DEBUG(if (calledCount == 216) return false);
  169. LLVM_DEBUG(dbgs() << "visitXOR calledCount: " << calledCount << "\n");
  170. LLVM_DEBUG(dbgs() << "I: "; I->dump());
  171. could be added to ``visitXOR`` to limit ``visitXor`` to being applied only to
  172. calls 212 and 217. This is from an actual test case and raises an important
  173. point---a simple binary search may not be sufficient, as transformations that
  174. interact may require isolating more than one call. In TargetLowering, use
  175. ``return SDNode();`` instead of ``return false;``.
  176. Now that the number of transformations is down to a manageable number, try
  177. examining the output to see if you can figure out which transformations are
  178. being done. If that can be figured out, then do the usual debugging. If which
  179. code corresponds to the transformation being performed isn't obvious, set a
  180. breakpoint after the call count based disabling and step through the code.
  181. Alternatively, you can use "``printf``" style debugging to report waypoints.