OptBisect.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ====================================================
  2. Using -opt-bisect-limit to debug optimization errors
  3. ====================================================
  4. .. contents::
  5. :local:
  6. :depth: 1
  7. Introduction
  8. ============
  9. The -opt-bisect-limit option provides a way to disable all optimization passes
  10. above a specified limit without modifying the way in which the Pass Managers
  11. are populated. The intention of this option is to assist in tracking down
  12. problems where incorrect transformations during optimization result in incorrect
  13. run-time behavior.
  14. This feature is implemented on an opt-in basis. Passes which can be safely
  15. skipped while still allowing correct code generation call a function to
  16. check the opt-bisect limit before performing optimizations. Passes which
  17. either must be run or do not modify the IR do not perform this check and are
  18. therefore never skipped. Generally, this means analysis passes, passes
  19. that are run at CodeGenOpt::None and passes which are required for register
  20. allocation.
  21. The -opt-bisect-limit option can be used with any tool, including front ends
  22. such as clang, that uses the core LLVM library for optimization and code
  23. generation. The exact syntax for invoking the option is discussed below.
  24. This feature is not intended to replace other debugging tools such as bugpoint.
  25. Rather it provides an alternate course of action when reproducing the problem
  26. requires a complex build infrastructure that would make using bugpoint
  27. impractical or when reproducing the failure requires a sequence of
  28. transformations that is difficult to replicate with tools like opt and llc.
  29. Getting Started
  30. ===============
  31. The -opt-bisect-limit command line option can be passed directly to tools such
  32. as opt, llc and lli. The syntax is as follows:
  33. ::
  34. <tool name> [other options] -opt-bisect-limit=<limit>
  35. If a value of -1 is used the tool will perform all optimizations but a message
  36. will be printed to stderr for each optimization that could be skipped
  37. indicating the index value that is associated with that optimization. To skip
  38. optimizations, pass the value of the last optimization to be performed as the
  39. opt-bisect-limit. All optimizations with a higher index value will be skipped.
  40. In order to use the -opt-bisect-limit option with a driver that provides a
  41. wrapper around the LLVM core library, an additional prefix option may be
  42. required, as defined by the driver. For example, to use this option with
  43. clang, the "-mllvm" prefix must be used. A typical clang invocation would look
  44. like this:
  45. ::
  46. clang -O2 -mllvm -opt-bisect-limit=256 my_file.c
  47. The -opt-bisect-limit option may also be applied to link-time optimizations by
  48. using a prefix to indicate that this is a plug-in option for the linker. The
  49. following syntax will set a bisect limit for LTO transformations:
  50. ::
  51. # When using lld, or ld64 (macOS)
  52. clang -flto -Wl,-mllvm,-opt-bisect-limit=256 my_file.o my_other_file.o
  53. # When using Gold
  54. clang -flto -Wl,-plugin-opt,-opt-bisect-limit=256 my_file.o my_other_file.o
  55. LTO passes are run by a library instance invoked by the linker. Therefore any
  56. passes run in the primary driver compilation phase are not affected by options
  57. passed via '-Wl,-plugin-opt' and LTO passes are not affected by options
  58. passed to the driver-invoked LLVM invocation via '-mllvm'.
  59. Bisection Index Values
  60. ======================
  61. The granularity of the optimizations associated with a single index value is
  62. variable. Depending on how the optimization pass has been instrumented the
  63. value may be associated with as much as all transformations that would have
  64. been performed by an optimization pass on an IR unit for which it is invoked
  65. (for instance, during a single call of runOnFunction for a FunctionPass) or as
  66. little as a single transformation. The index values may also be nested so that
  67. if an invocation of the pass is not skipped individual transformations within
  68. that invocation may still be skipped.
  69. The order of the values assigned is guaranteed to remain stable and consistent
  70. from one run to the next up to and including the value specified as the limit.
  71. Above the limit value skipping of optimizations can cause a change in the
  72. numbering, but because all optimizations above the limit are skipped this
  73. is not a problem.
  74. When an opt-bisect index value refers to an entire invocation of the run
  75. function for a pass, the pass will query whether or not it should be skipped
  76. each time it is invoked and each invocation will be assigned a unique value.
  77. For example, if a FunctionPass is used with a module containing three functions
  78. a different index value will be assigned to the pass for each of the functions
  79. as the pass is run. The pass may be run on two functions but skipped for the
  80. third.
  81. If the pass internally performs operations on a smaller IR unit the pass must be
  82. specifically instrumented to enable bisection at this finer level of granularity
  83. (see below for details).
  84. Example Usage
  85. =============
  86. .. code-block:: console
  87. $ opt -O2 -o test-opt.bc -opt-bisect-limit=16 test.ll
  88. BISECT: running pass (1) Simplify the CFG on function (g)
  89. BISECT: running pass (2) SROA on function (g)
  90. BISECT: running pass (3) Early CSE on function (g)
  91. BISECT: running pass (4) Infer set function attributes on module (test.ll)
  92. BISECT: running pass (5) Interprocedural Sparse Conditional Constant Propagation on module (test.ll)
  93. BISECT: running pass (6) Global Variable Optimizer on module (test.ll)
  94. BISECT: running pass (7) Promote Memory to Register on function (g)
  95. BISECT: running pass (8) Dead Argument Elimination on module (test.ll)
  96. BISECT: running pass (9) Combine redundant instructions on function (g)
  97. BISECT: running pass (10) Simplify the CFG on function (g)
  98. BISECT: running pass (11) Remove unused exception handling info on SCC (<<null function>>)
  99. BISECT: running pass (12) Function Integration/Inlining on SCC (<<null function>>)
  100. BISECT: running pass (13) Deduce function attributes on SCC (<<null function>>)
  101. BISECT: running pass (14) Remove unused exception handling info on SCC (f)
  102. BISECT: running pass (15) Function Integration/Inlining on SCC (f)
  103. BISECT: running pass (16) Deduce function attributes on SCC (f)
  104. BISECT: NOT running pass (17) Remove unused exception handling info on SCC (g)
  105. BISECT: NOT running pass (18) Function Integration/Inlining on SCC (g)
  106. BISECT: NOT running pass (19) Deduce function attributes on SCC (g)
  107. BISECT: NOT running pass (20) SROA on function (g)
  108. BISECT: NOT running pass (21) Early CSE on function (g)
  109. BISECT: NOT running pass (22) Speculatively execute instructions if target has divergent branches on function (g)
  110. ... etc. ...
  111. Pass Skipping Implementation
  112. ============================
  113. The -opt-bisect-limit implementation depends on individual passes opting in to
  114. the opt-bisect process. The OptBisect object that manages the process is
  115. entirely passive and has no knowledge of how any pass is implemented. When a
  116. pass is run if the pass may be skipped, it should call the OptBisect object to
  117. see if it should be skipped.
  118. The OptBisect object is intended to be accessed through LLVMContext and each
  119. Pass base class contains a helper function that abstracts the details in order
  120. to make this check uniform across all passes. These helper functions are:
  121. .. code-block:: c++
  122. bool ModulePass::skipModule(Module &M);
  123. bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC);
  124. bool FunctionPass::skipFunction(const Function &F);
  125. bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB);
  126. bool LoopPass::skipLoop(const Loop *L);
  127. A MachineFunctionPass should use FunctionPass::skipFunction() as such:
  128. .. code-block:: c++
  129. bool MyMachineFunctionPass::runOnMachineFunction(Function &MF) {
  130. if (skipFunction(*MF.getFunction())
  131. return false;
  132. // Otherwise, run the pass normally.
  133. }
  134. In addition to checking with the OptBisect class to see if the pass should be
  135. skipped, the skipFunction(), skipLoop() and skipBasicBlock() helper functions
  136. also look for the presence of the "optnone" function attribute. The calling
  137. pass will be unable to determine whether it is being skipped because the
  138. "optnone" attribute is present or because the opt-bisect-limit has been
  139. reached. This is desirable because the behavior should be the same in either
  140. case.
  141. The majority of LLVM passes which can be skipped have already been instrumented
  142. in the manner described above. If you are adding a new pass or believe you
  143. have found a pass which is not being included in the opt-bisect process but
  144. should be, you can add it as described above.
  145. Adding Finer Granularity
  146. ========================
  147. Once the pass in which an incorrect transformation is performed has been
  148. determined, it may be useful to perform further analysis in order to determine
  149. which specific transformation is causing the problem. Debug counters
  150. can be used for this purpose.