annotate docs/OptBisect.rst @ 125:56c5119fbcd2

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