Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 1 | // Copyright (c) 2016 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef SPIRV_TOOLS_OPTIMIZER_HPP_ |
| 16 | #define SPIRV_TOOLS_OPTIMIZER_HPP_ |
| 17 | |
| 18 | #include <memory> |
David Neto | c32e79e | 2018-01-04 12:59:50 -0500 | [diff] [blame] | 19 | #include <ostream> |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "libspirv.hpp" |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 25 | |
| 26 | namespace spvtools { |
| 27 | |
| 28 | // C++ interface for SPIR-V optimization functionalities. It wraps the context |
| 29 | // (including target environment and the corresponding SPIR-V grammar) and |
| 30 | // provides methods for registering optimization passes and optimizing. |
| 31 | // |
| 32 | // Instances of this class provides basic thread-safety guarantee. |
| 33 | class Optimizer { |
| 34 | public: |
| 35 | // The token for an optimization pass. It is returned via one of the |
| 36 | // Create*Pass() standalone functions at the end of this header file and |
| 37 | // consumed by the RegisterPass() method. Tokens are one-time objects that |
| 38 | // only support move; copying is not allowed. |
| 39 | struct PassToken { |
| 40 | struct Impl; // Opaque struct for holding inernal data. |
| 41 | |
| 42 | PassToken(std::unique_ptr<Impl>); |
| 43 | |
| 44 | // Tokens can only be moved. Copying is disabled. |
| 45 | PassToken(const PassToken&) = delete; |
| 46 | PassToken(PassToken&&); |
| 47 | PassToken& operator=(const PassToken&) = delete; |
| 48 | PassToken& operator=(PassToken&&); |
| 49 | |
| 50 | ~PassToken(); |
| 51 | |
| 52 | std::unique_ptr<Impl> impl_; // Unique pointer to internal data. |
| 53 | }; |
| 54 | |
| 55 | // Constructs an instance with the given target |env|, which is used to decode |
| 56 | // the binaries to be optimized later. |
| 57 | // |
| 58 | // The constructed instance will have an empty message consumer, which just |
| 59 | // ignores all messages from the library. Use SetMessageConsumer() to supply |
| 60 | // one if messages are of concern. |
| 61 | explicit Optimizer(spv_target_env env); |
| 62 | |
| 63 | // Disables copy/move constructor/assignment operations. |
| 64 | Optimizer(const Optimizer&) = delete; |
| 65 | Optimizer(Optimizer&&) = delete; |
| 66 | Optimizer& operator=(const Optimizer&) = delete; |
| 67 | Optimizer& operator=(Optimizer&&) = delete; |
| 68 | |
| 69 | // Destructs this instance. |
| 70 | ~Optimizer(); |
| 71 | |
| 72 | // Sets the message consumer to the given |consumer|. The |consumer| will be |
| 73 | // invoked once for each message communicated from the library. |
| 74 | void SetMessageConsumer(MessageConsumer consumer); |
| 75 | |
| 76 | // Registers the given |pass| to this optimizer. Passes will be run in the |
| 77 | // exact order of registration. The token passed in will be consumed by this |
| 78 | // method. |
| 79 | Optimizer& RegisterPass(PassToken&& pass); |
| 80 | |
Diego Novillo | c90d730 | 2017-08-30 14:19:22 -0400 | [diff] [blame] | 81 | // Registers passes that attempt to improve performance of generated code. |
| 82 | // This sequence of passes is subject to constant review and will change |
| 83 | // from time to time. |
| 84 | Optimizer& RegisterPerformancePasses(); |
| 85 | |
| 86 | // Registers passes that attempt to improve the size of generated code. |
| 87 | // This sequence of passes is subject to constant review and will change |
| 88 | // from time to time. |
| 89 | Optimizer& RegisterSizePasses(); |
| 90 | |
Lei Zhang | aec60b8 | 2017-11-17 16:50:43 -0500 | [diff] [blame] | 91 | // Registers passes that attempt to legalize the generated code. |
| 92 | // |
| 93 | // Note: this recipe is specially for legalizing SPIR-V. It should be used |
| 94 | // by compilers after translating HLSL source code literally. It should |
| 95 | // *not* be used by general workloads for performance or size improvement. |
| 96 | // |
| 97 | // This sequence of passes is subject to constant review and will change |
| 98 | // from time to time. |
| 99 | Optimizer& RegisterLegalizationPasses(); |
| 100 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 101 | // Optimizes the given SPIR-V module |original_binary| and writes the |
| 102 | // optimized binary into |optimized_binary|. |
| 103 | // Returns true on successful optimization, whether or not the module is |
| 104 | // modified. Returns false if errors occur when processing |original_binary| |
| 105 | // using any of the registered passes. In that case, no further passes are |
Diego Novillo | c90d730 | 2017-08-30 14:19:22 -0400 | [diff] [blame] | 106 | // executed and the contents in |optimized_binary| may be invalid. |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 107 | // |
| 108 | // It's allowed to alias |original_binary| to the start of |optimized_binary|. |
| 109 | bool Run(const uint32_t* original_binary, size_t original_binary_size, |
| 110 | std::vector<uint32_t>* optimized_binary) const; |
| 111 | |
Diego Novillo | c90d730 | 2017-08-30 14:19:22 -0400 | [diff] [blame] | 112 | // Returns a vector of strings with all the pass names added to this |
| 113 | // optimizer's pass manager. These strings are valid until the associated |
| 114 | // pass manager is destroyed. |
Steven Perron | 5834719 | 2017-10-20 12:17:41 -0400 | [diff] [blame] | 115 | std::vector<const char*> GetPassNames() const; |
Diego Novillo | c90d730 | 2017-08-30 14:19:22 -0400 | [diff] [blame] | 116 | |
David Neto | c32e79e | 2018-01-04 12:59:50 -0500 | [diff] [blame] | 117 | // Sets the option to print the disassembly before each pass and after the |
| 118 | // last pass. If |out| is null, then no output is generated. Otherwise, |
| 119 | // output is sent to the |out| output stream. |
| 120 | Optimizer& SetPrintAll(std::ostream* out); |
| 121 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 122 | private: |
| 123 | struct Impl; // Opaque struct for holding internal data. |
| 124 | std::unique_ptr<Impl> impl_; // Unique pointer to internal data. |
| 125 | }; |
| 126 | |
| 127 | // Creates a null pass. |
| 128 | // A null pass does nothing to the SPIR-V module to be optimized. |
| 129 | Optimizer::PassToken CreateNullPass(); |
| 130 | |
| 131 | // Creates a strip-debug-info pass. |
| 132 | // A strip-debug-info pass removes all debug instructions (as documented in |
| 133 | // Section 3.32.2 of the SPIR-V spec) of the SPIR-V module to be optimized. |
| 134 | Optimizer::PassToken CreateStripDebugInfoPass(); |
| 135 | |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 136 | // Creates an eliminate-dead-functions pass. |
Steven Perron | 5834719 | 2017-10-20 12:17:41 -0400 | [diff] [blame] | 137 | // An eliminate-dead-functions pass will remove all functions that are not in |
| 138 | // the call trees rooted at entry points and exported functions. These |
| 139 | // functions are not needed because they will never be called. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 140 | Optimizer::PassToken CreateEliminateDeadFunctionsPass(); |
| 141 | |
qining | 144f59e | 2017-04-19 18:10:59 -0400 | [diff] [blame] | 142 | // Creates a set-spec-constant-default-value pass from a mapping from spec-ids |
| 143 | // to the default values in the form of string. |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 144 | // A set-spec-constant-default-value pass sets the default values for the |
| 145 | // spec constants that have SpecId decorations (i.e., those defined by |
| 146 | // OpSpecConstant{|True|False} instructions). |
| 147 | Optimizer::PassToken CreateSetSpecConstantDefaultValuePass( |
| 148 | const std::unordered_map<uint32_t, std::string>& id_value_map); |
| 149 | |
qining | 144f59e | 2017-04-19 18:10:59 -0400 | [diff] [blame] | 150 | // Creates a set-spec-constant-default-value pass from a mapping from spec-ids |
| 151 | // to the default values in the form of bit pattern. |
| 152 | // A set-spec-constant-default-value pass sets the default values for the |
| 153 | // spec constants that have SpecId decorations (i.e., those defined by |
| 154 | // OpSpecConstant{|True|False} instructions). |
| 155 | Optimizer::PassToken CreateSetSpecConstantDefaultValuePass( |
| 156 | const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map); |
| 157 | |
David Neto | 11a867f | 2017-04-01 16:10:16 -0400 | [diff] [blame] | 158 | // Creates a flatten-decoration pass. |
| 159 | // A flatten-decoration pass replaces grouped decorations with equivalent |
| 160 | // ungrouped decorations. That is, it replaces each OpDecorationGroup |
| 161 | // instruction and associated OpGroupDecorate and OpGroupMemberDecorate |
| 162 | // instructions with equivalent OpDecorate and OpMemberDecorate instructions. |
| 163 | // The pass does not attempt to preserve debug information for instructions |
| 164 | // it removes. |
| 165 | Optimizer::PassToken CreateFlattenDecorationPass(); |
| 166 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 167 | // Creates a freeze-spec-constant-value pass. |
| 168 | // A freeze-spec-constant pass specializes the value of spec constants to |
| 169 | // their default values. This pass only processes the spec constants that have |
| 170 | // SpecId decorations (defined by OpSpecConstant, OpSpecConstantTrue, or |
| 171 | // OpSpecConstantFalse instructions) and replaces them with their normal |
| 172 | // counterparts (OpConstant, OpConstantTrue, or OpConstantFalse). The |
| 173 | // corresponding SpecId annotation instructions will also be removed. This |
| 174 | // pass does not fold the newly added normal constants and does not process |
| 175 | // other spec constants defined by OpSpecConstantComposite or |
| 176 | // OpSpecConstantOp. |
| 177 | Optimizer::PassToken CreateFreezeSpecConstantValuePass(); |
| 178 | |
| 179 | // Creates a fold-spec-constant-op-and-composite pass. |
| 180 | // A fold-spec-constant-op-and-composite pass folds spec constants defined by |
| 181 | // OpSpecConstantOp or OpSpecConstantComposite instruction, to normal Constants |
| 182 | // defined by OpConstantTrue, OpConstantFalse, OpConstant, OpConstantNull, or |
| 183 | // OpConstantComposite instructions. Note that spec constants defined with |
| 184 | // OpSpecConstant, OpSpecConstantTrue, or OpSpecConstantFalse instructions are |
| 185 | // not handled, as these instructions indicate their value are not determined |
| 186 | // and can be changed in future. A spec constant is foldable if all of its |
| 187 | // value(s) can be determined from the module. E.g., an integer spec constant |
| 188 | // defined with OpSpecConstantOp instruction can be folded if its value won't |
| 189 | // change later. This pass will replace the original OpSpecContantOp instruction |
| 190 | // with an OpConstant instruction. When folding composite spec constants, |
| 191 | // new instructions may be inserted to define the components of the composite |
| 192 | // constant first, then the original spec constants will be replaced by |
| 193 | // OpConstantComposite instructions. |
| 194 | // |
| 195 | // There are some operations not supported yet: |
| 196 | // OpSConvert, OpFConvert, OpQuantizeToF16 and |
| 197 | // all the operations under Kernel capability. |
| 198 | // TODO(qining): Add support for the operations listed above. |
| 199 | Optimizer::PassToken CreateFoldSpecConstantOpAndCompositePass(); |
| 200 | |
| 201 | // Creates a unify-constant pass. |
| 202 | // A unify-constant pass de-duplicates the constants. Constants with the exact |
| 203 | // same value and identical form will be unified and only one constant will |
| 204 | // be kept for each unique pair of type and value. |
| 205 | // There are several cases not handled by this pass: |
| 206 | // 1) Constants defined by OpConstantNull instructions (null constants) and |
| 207 | // constants defined by OpConstantFalse, OpConstant or OpConstantComposite |
| 208 | // with value 0 (zero-valued normal constants) are not considered equivalent. |
| 209 | // So null constants won't be used to replace zero-valued normal constants, |
| 210 | // vice versa. |
| 211 | // 2) Whenever there are decorations to the constant's result id id, the |
| 212 | // constant won't be handled, which means, it won't be used to replace any |
| 213 | // other constants, neither can other constants replace it. |
| 214 | // 3) NaN in float point format with different bit patterns are not unified. |
| 215 | Optimizer::PassToken CreateUnifyConstantPass(); |
| 216 | |
| 217 | // Creates a eliminate-dead-constant pass. |
| 218 | // A eliminate-dead-constant pass removes dead constants, including normal |
| 219 | // contants defined by OpConstant, OpConstantComposite, OpConstantTrue, or |
| 220 | // OpConstantFalse and spec constants defined by OpSpecConstant, |
| 221 | // OpSpecConstantComposite, OpSpecConstantTrue, OpSpecConstantFalse or |
| 222 | // OpSpecConstantOp. |
| 223 | Optimizer::PassToken CreateEliminateDeadConstantPass(); |
| 224 | |
Steven Perron | e4c7d8e | 2017-09-08 12:08:03 -0400 | [diff] [blame] | 225 | // Creates a strength-reduction pass. |
| 226 | // A strength-reduction pass will look for opportunities to replace an |
| 227 | // instruction with an equivalent and less expensive one. For example, |
| 228 | // multiplying by a power of 2 can be replaced by a bit shift. |
| 229 | Optimizer::PassToken CreateStrengthReductionPass(); |
| 230 | |
GregF | ad1d035 | 2017-06-07 15:28:53 -0600 | [diff] [blame] | 231 | // Creates a block merge pass. |
| 232 | // This pass searches for blocks with a single Branch to a block with no |
| 233 | // other predecessors and merges the blocks into a single block. Continue |
| 234 | // blocks and Merge blocks are not candidates for the second block. |
| 235 | // |
| 236 | // The pass is most useful after Dead Branch Elimination, which can leave |
| 237 | // such sequences of blocks. Merging them makes subsequent passes more |
| 238 | // effective, such as single block local store-load elimination. |
| 239 | // |
| 240 | // While this pass reduces the number of occurrences of this sequence, at |
| 241 | // this time it does not guarantee all such sequences are eliminated. |
| 242 | // |
| 243 | // Presence of phi instructions can inhibit this optimization. Handling |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 244 | // these is left for future improvements. |
GregF | ad1d035 | 2017-06-07 15:28:53 -0600 | [diff] [blame] | 245 | Optimizer::PassToken CreateBlockMergePass(); |
| 246 | |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 247 | // Creates an exhaustive inline pass. |
| 248 | // An exhaustive inline pass attempts to exhaustively inline all function |
| 249 | // calls in all functions in an entry point call tree. The intent is to enable, |
| 250 | // albeit through brute force, analysis and optimization across function |
| 251 | // calls by subsequent optimization passes. As the inlining is exhaustive, |
| 252 | // there is no attempt to optimize for size or runtime performance. Functions |
| 253 | // that are not in the call tree of an entry point are not changed. |
GregF | e28bd39 | 2017-08-01 17:20:13 -0600 | [diff] [blame] | 254 | Optimizer::PassToken CreateInlineExhaustivePass(); |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 255 | |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 256 | // Creates an opaque inline pass. |
| 257 | // An opaque inline pass inlines all function calls in all functions in all |
| 258 | // entry point call trees where the called function contains an opaque type |
| 259 | // in either its parameter types or return type. An opaque type is currently |
| 260 | // defined as Image, Sampler or SampledImage. The intent is to enable, albeit |
| 261 | // through brute force, analysis and optimization across these function calls |
| 262 | // by subsequent passes in order to remove the storing of opaque types which is |
| 263 | // not legal in Vulkan. Functions that are not in the call tree of an entry |
| 264 | // point are not changed. |
| 265 | Optimizer::PassToken CreateInlineOpaquePass(); |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 266 | |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 267 | // Creates a single-block local variable load/store elimination pass. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 268 | // For every entry point function, do single block memory optimization of |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 269 | // function variables referenced only with non-access-chain loads and stores. |
| 270 | // For each targeted variable load, if previous store to that variable in the |
| 271 | // block, replace the load's result id with the value id of the store. |
| 272 | // If previous load within the block, replace the current load's result id |
| 273 | // with the previous load's result id. In either case, delete the current |
| 274 | // load. Finally, check if any remaining stores are useless, and delete store |
| 275 | // and variable if possible. |
| 276 | // |
| 277 | // The presence of access chain references and function calls can inhibit |
| 278 | // the above optimization. |
| 279 | // |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 280 | // Only modules with relaxed logical addressing (see opt/instruction.h) are |
| 281 | // currently processed. |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 282 | // |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 283 | // This pass is most effective if preceeded by Inlining and |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 284 | // LocalAccessChainConvert. This pass will reduce the work needed to be done |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 285 | // by LocalSingleStoreElim and LocalMultiStoreElim. |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 286 | // |
| 287 | // Only functions in the call tree of an entry point are processed. |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 288 | Optimizer::PassToken CreateLocalSingleBlockLoadStoreElimPass(); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 289 | |
GregF | 52e247f | 2017-06-02 13:23:20 -0600 | [diff] [blame] | 290 | // Create dead branch elimination pass. |
| 291 | // For each entry point function, this pass will look for SelectionMerge |
| 292 | // BranchConditionals with constant condition and convert to a Branch to |
| 293 | // the indicated label. It will delete resulting dead blocks. |
| 294 | // |
Andrey Tuganov | 4b1577a | 2017-10-05 16:26:09 -0400 | [diff] [blame] | 295 | // For all phi functions in merge block, replace all uses with the id |
| 296 | // corresponding to the living predecessor. |
| 297 | // |
Alan Baker | 1b6cfd3 | 2018-01-04 17:04:03 -0500 | [diff] [blame] | 298 | // Note that some branches and blocks may be left to avoid creating invalid |
| 299 | // control flow. Improving this is left to future work. |
GregF | 52e247f | 2017-06-02 13:23:20 -0600 | [diff] [blame] | 300 | // |
| 301 | // This pass is most effective when preceeded by passes which eliminate |
| 302 | // local loads and stores, effectively propagating constant values where |
| 303 | // possible. |
| 304 | Optimizer::PassToken CreateDeadBranchElimPass(); |
| 305 | |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 306 | // Creates an SSA local variable load/store elimination pass. |
| 307 | // For every entry point function, eliminate all loads and stores of function |
| 308 | // scope variables only referenced with non-access-chain loads and stores. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 309 | // Eliminate the variables as well. |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 310 | // |
| 311 | // The presence of access chain references and function calls can inhibit |
| 312 | // the above optimization. |
| 313 | // |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 314 | // Only shader modules with relaxed logical addressing (see opt/instruction.h) |
| 315 | // are currently processed. Currently modules with any extensions enabled are |
| 316 | // not processed. This is left for future work. |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 317 | // |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 318 | // This pass is most effective if preceeded by Inlining and |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 319 | // LocalAccessChainConvert. LocalSingleStoreElim and LocalSingleBlockElim |
| 320 | // will reduce the work that this pass has to do. |
| 321 | Optimizer::PassToken CreateLocalMultiStoreElimPass(); |
| 322 | |
GregF | aa7e687 | 2017-05-12 17:27:21 -0600 | [diff] [blame] | 323 | // Creates a local access chain conversion pass. |
| 324 | // A local access chain conversion pass identifies all function scope |
| 325 | // variables which are accessed only with loads, stores and access chains |
| 326 | // with constant indices. It then converts all loads and stores of such |
| 327 | // variables into equivalent sequences of loads, stores, extracts and inserts. |
| 328 | // |
| 329 | // This pass only processes entry point functions. It currently only converts |
| 330 | // non-nested, non-ptr access chains. It does not process modules with |
| 331 | // non-32-bit integer types present. Optional memory access options on loads |
| 332 | // and stores are ignored as we are only processing function scope variables. |
| 333 | // |
| 334 | // This pass unifies access to these variables to a single mode and simplifies |
| 335 | // subsequent analysis and elimination of these variables along with their |
| 336 | // loads and stores allowing values to propagate to their points of use where |
| 337 | // possible. |
| 338 | Optimizer::PassToken CreateLocalAccessChainConvertPass(); |
| 339 | |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 340 | // Creates a local single store elimination pass. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 341 | // For each entry point function, this pass eliminates loads and stores for |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 342 | // function scope variable that are stored to only once, where possible. Only |
| 343 | // whole variable loads and stores are eliminated; access-chain references are |
| 344 | // not optimized. Replace all loads of such variables with the value that is |
| 345 | // stored and eliminate any resulting dead code. |
| 346 | // |
| 347 | // Currently, the presence of access chains and function calls can inhibit this |
| 348 | // pass, however the Inlining and LocalAccessChainConvert passes can make it |
| 349 | // more effective. In additional, many non-load/store memory operations are |
| 350 | // not supported and will prohibit optimization of a function. Support of |
| 351 | // these operations are future work. |
| 352 | // |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 353 | // Only shader modules with relaxed logical addressing (see opt/instruction.h) |
| 354 | // are currently processed. |
| 355 | // |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 356 | // This pass will reduce the work needed to be done by LocalSingleBlockElim |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 357 | // and LocalMultiStoreElim and can improve the effectiveness of other passes |
| 358 | // such as DeadBranchElimination which depend on values for their analysis. |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 359 | Optimizer::PassToken CreateLocalSingleStoreElimPass(); |
| 360 | |
GregF | 6136bf9 | 2017-05-26 10:33:11 -0600 | [diff] [blame] | 361 | // Creates an insert/extract elimination pass. |
| 362 | // This pass processes each entry point function in the module, searching for |
| 363 | // extracts on a sequence of inserts. It further searches the sequence for an |
| 364 | // insert with indices identical to the extract. If such an insert can be |
| 365 | // found before hitting a conflicting insert, the extract's result id is |
| 366 | // replaced with the id of the values from the insert. |
| 367 | // |
| 368 | // Besides removing extracts this pass enables subsequent dead code elimination |
| 369 | // passes to delete the inserts. This pass performs best after access chains are |
| 370 | // converted to inserts and extracts and local loads and stores are eliminated. |
| 371 | Optimizer::PassToken CreateInsertExtractElimPass(); |
| 372 | |
GregF | f28b106 | 2018-01-26 17:05:33 -0700 | [diff] [blame] | 373 | // Creates a dead insert elimination pass. |
| 374 | // This pass processes each entry point function in the module, searching for |
| 375 | // unreferenced inserts into composite types. These are most often unused |
| 376 | // stores to vector components. They are unused because they are never |
| 377 | // referenced, or because there is another insert to the same component between |
| 378 | // the insert and the reference. After removing the inserts, dead code |
| 379 | // elimination is attempted on the inserted values. |
| 380 | // |
| 381 | // This pass performs best after access chains are converted to inserts and |
| 382 | // extracts and local loads and stores are eliminated. While executing this |
| 383 | // pass can be advantageous on its own, it is also advantageous to execute |
| 384 | // this pass after CreateInsertExtractPass() as it will remove any unused |
| 385 | // inserts created by that pass. |
| 386 | Optimizer::PassToken CreateDeadInsertElimPass(); |
| 387 | |
GregF | f4b29f3 | 2017-07-03 17:23:04 -0600 | [diff] [blame] | 388 | // Creates a pass to consolidate uniform references. |
| 389 | // For each entry point function in the module, first change all constant index |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 390 | // access chain loads into equivalent composite extracts. Then consolidate |
GregF | f4b29f3 | 2017-07-03 17:23:04 -0600 | [diff] [blame] | 391 | // identical uniform loads into one uniform load. Finally, consolidate |
| 392 | // identical uniform extracts into one uniform extract. This may require |
| 393 | // moving a load or extract to a point which dominates all uses. |
| 394 | // |
| 395 | // This pass requires a module to have structured control flow ie shader |
| 396 | // capability. It also requires logical addressing ie Addresses capability |
| 397 | // is not enabled. It also currently does not support any extensions. |
| 398 | // |
| 399 | // This pass currently only optimizes loads with a single index. |
| 400 | Optimizer::PassToken CreateCommonUniformElimPass(); |
| 401 | |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 402 | // Create aggressive dead code elimination pass |
Alan Baker | 3a054e1 | 2017-12-18 12:13:10 -0500 | [diff] [blame] | 403 | // This pass eliminates unused code from the module. In addition, |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 404 | // it detects and eliminates code which may have spurious uses but which do |
| 405 | // not contribute to the output of the function. The most common cause of |
| 406 | // such code sequences is summations in loops whose result is no longer used |
| 407 | // due to dead code elimination. This optimization has additional compile |
| 408 | // time cost over standard dead code elimination. |
| 409 | // |
| 410 | // This pass only processes entry point functions. It also only processes |
Alan Baker | 3a054e1 | 2017-12-18 12:13:10 -0500 | [diff] [blame] | 411 | // shaders with relaxed logical addressing (see opt/instruction.h). It |
| 412 | // currently will not process functions with function calls. Unreachable |
| 413 | // functions are deleted. |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 414 | // |
| 415 | // This pass will be made more effective by first running passes that remove |
| 416 | // dead control flow and inlines function calls. |
| 417 | // |
| 418 | // This pass can be especially useful after running Local Access Chain |
| 419 | // Conversion, which tends to cause cycles of dead code to be left after |
| 420 | // Store/Load elimination passes are completed. These cycles cannot be |
| 421 | // eliminated with standard dead code elimination. |
| 422 | Optimizer::PassToken CreateAggressiveDCEPass(); |
| 423 | |
Andrey Tuganov | 1e309af | 2017-04-11 15:11:04 -0400 | [diff] [blame] | 424 | // Creates a compact ids pass. |
| 425 | // The pass remaps result ids to a compact and gapless range starting from %1. |
| 426 | Optimizer::PassToken CreateCompactIdsPass(); |
| 427 | |
Pierre Moreau | 7183ad5 | 2018-01-03 01:54:55 +0100 | [diff] [blame] | 428 | // Creates a remove duplicate pass. |
| 429 | // This pass removes various duplicates: |
| 430 | // * duplicate capabilities; |
| 431 | // * duplicate extended instruction imports; |
| 432 | // * duplicate types; |
| 433 | // * duplicate decorations. |
Pierre Moreau | 86627f7 | 2017-07-13 02:16:51 +0200 | [diff] [blame] | 434 | Optimizer::PassToken CreateRemoveDuplicatesPass(); |
| 435 | |
Diego Novillo | c75704e | 2017-09-06 08:56:41 -0400 | [diff] [blame] | 436 | // Creates a CFG cleanup pass. |
| 437 | // This pass removes cruft from the control flow graph of functions that are |
| 438 | // reachable from entry points and exported functions. It currently includes the |
| 439 | // following functionality: |
| 440 | // |
| 441 | // - Removal of unreachable basic blocks. |
| 442 | Optimizer::PassToken CreateCFGCleanupPass(); |
| 443 | |
Steven Perron | 5834719 | 2017-10-20 12:17:41 -0400 | [diff] [blame] | 444 | // Create dead variable elimination pass. |
| 445 | // This pass will delete module scope variables, along with their decorations, |
| 446 | // that are not referenced. |
| 447 | Optimizer::PassToken CreateDeadVariableEliminationPass(); |
| 448 | |
Alan Baker | a92d69b | 2017-11-08 16:22:10 -0500 | [diff] [blame] | 449 | // Create merge return pass. |
| 450 | // This pass replaces all returns with unconditional branches to a new block |
| 451 | // containing a return. If necessary, this new block will contain a PHI node to |
| 452 | // select the correct return value. |
| 453 | // |
| 454 | // This pass does not consider unreachable code, nor does it perform any other |
| 455 | // optimizations. |
| 456 | // |
| 457 | // This pass does not currently support structured control flow. It bails out if |
| 458 | // the shader capability is detected. |
| 459 | Optimizer::PassToken CreateMergeReturnPass(); |
| 460 | |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 461 | // Create value numbering pass. |
| 462 | // This pass will look for instructions in the same basic block that compute the |
| 463 | // same value, and remove the redundant ones. |
| 464 | Optimizer::PassToken CreateLocalRedundancyEliminationPass(); |
Steven Perron | 5d602ab | 2017-12-04 12:29:51 -0500 | [diff] [blame] | 465 | |
Alexander Johnston | 84ccd0b | 2018-01-29 10:39:55 +0000 | [diff] [blame] | 466 | // Create LICM pass. |
| 467 | // This pass will look for invariant instructions inside loops and hoist them to |
| 468 | // the loops preheader. |
| 469 | Optimizer::PassToken CreateLoopInvariantCodeMotionPass(); |
| 470 | |
Steven Perron | 5d602ab | 2017-12-04 12:29:51 -0500 | [diff] [blame] | 471 | // Create global value numbering pass. |
| 472 | // This pass will look for instructions where the same value is computed on all |
| 473 | // paths leading to the instruction. Those instructions are deleted. |
| 474 | Optimizer::PassToken CreateRedundancyEliminationPass(); |
Alan Baker | 867451f | 2017-11-30 17:03:06 -0500 | [diff] [blame] | 475 | |
| 476 | // Create scalar replacement pass. |
| 477 | // This pass replaces composite function scope variables with variables for each |
| 478 | // element if those elements are accessed individually. |
| 479 | Optimizer::PassToken CreateScalarReplacementPass(); |
Steven Perron | b86eb68 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 480 | |
| 481 | // Create a private to local pass. |
| 482 | // This pass looks for variables delcared in the private storage class that are |
| 483 | // used in only one function. Those variables are moved to the function storage |
| 484 | // class in the function that they are used. |
| 485 | Optimizer::PassToken CreatePrivateToLocalPass(); |
Diego Novillo | 4ba9dcc | 2017-12-05 11:39:25 -0500 | [diff] [blame] | 486 | |
| 487 | // Creates a conditional constant propagation (CCP) pass. |
| 488 | // This pass implements the SSA-CCP algorithm in |
| 489 | // |
| 490 | // Constant propagation with conditional branches, |
| 491 | // Wegman and Zadeck, ACM TOPLAS 13(2):181-210. |
| 492 | // |
| 493 | // Constant values in expressions and conditional jumps are folded and |
| 494 | // simplified. This may reduce code size by removing never executed jump targets |
| 495 | // and computations with constant operands. |
| 496 | Optimizer::PassToken CreateCCPPass(); |
| 497 | |
Steven Perron | 34d4294 | 2018-01-17 14:57:37 -0500 | [diff] [blame] | 498 | // Creates a workaround driver bugs pass. This pass attempts to work around |
| 499 | // a known driver bug (issue #1209) by identifying the bad code sequences and |
| 500 | // rewriting them. |
| 501 | // |
| 502 | // Current workaround: Avoid OpUnreachable instructions in loops. |
| 503 | Optimizer::PassToken CreateWorkaround1209Pass(); |
| 504 | |
Alan Baker | 2e93e80 | 2018-01-16 11:15:06 -0500 | [diff] [blame] | 505 | // Creates a pass that converts if-then-else like assignments into OpSelect. |
| 506 | Optimizer::PassToken CreateIfConversionPass(); |
| 507 | |
Steven Perron | 61d8c03 | 2018-01-30 11:24:03 -0500 | [diff] [blame] | 508 | // Creates a pass that will replace instructions that are not valid for the |
| 509 | // current shader stage by constants. Has no effect on non-shader modules. |
| 510 | Optimizer::PassToken CreateReplaceInvalidOpcodePass(); |
| 511 | |
Steven Perron | 06cdb96 | 2018-02-02 11:55:05 -0500 | [diff] [blame] | 512 | // Creates a pass that simplifies instructions using the instruction folder. |
| 513 | Optimizer::PassToken CreateSimplificationPass(); |
| 514 | |
Stephen McGroarty | dd8400e | 2018-02-14 17:03:12 +0000 | [diff] [blame] | 515 | // Create loop unroller pass. |
Stephen McGroarty | e354984 | 2018-02-27 11:50:08 +0000 | [diff] [blame^] | 516 | // Creates a pass to unroll loops which have the "Unroll" loop control |
Stephen McGroarty | dd8400e | 2018-02-14 17:03:12 +0000 | [diff] [blame] | 517 | // mask set. The loops must meet a specific criteria in order to be unrolled |
| 518 | // safely this criteria is checked before doing the unroll by the |
| 519 | // LoopUtils::CanPerformUnroll method. Any loop that does not meet the criteria |
| 520 | // won't be unrolled. See CanPerformUnroll LoopUtils.h for more information. |
Stephen McGroarty | e354984 | 2018-02-27 11:50:08 +0000 | [diff] [blame^] | 521 | Optimizer::PassToken CreateLoopUnrollPass(bool fully_unroll, int factor = 0); |
Stephen McGroarty | dd8400e | 2018-02-14 17:03:12 +0000 | [diff] [blame] | 522 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 523 | } // namespace spvtools |
| 524 | |
| 525 | #endif // SPIRV_TOOLS_OPTIMIZER_HPP_ |