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> |
| 19 | #include <string> |
| 20 | #include <unordered_map> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "libspirv.hpp" |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 24 | |
| 25 | namespace spvtools { |
| 26 | |
| 27 | // C++ interface for SPIR-V optimization functionalities. It wraps the context |
| 28 | // (including target environment and the corresponding SPIR-V grammar) and |
| 29 | // provides methods for registering optimization passes and optimizing. |
| 30 | // |
| 31 | // Instances of this class provides basic thread-safety guarantee. |
| 32 | class Optimizer { |
| 33 | public: |
| 34 | // The token for an optimization pass. It is returned via one of the |
| 35 | // Create*Pass() standalone functions at the end of this header file and |
| 36 | // consumed by the RegisterPass() method. Tokens are one-time objects that |
| 37 | // only support move; copying is not allowed. |
| 38 | struct PassToken { |
| 39 | struct Impl; // Opaque struct for holding inernal data. |
| 40 | |
| 41 | PassToken(std::unique_ptr<Impl>); |
| 42 | |
| 43 | // Tokens can only be moved. Copying is disabled. |
| 44 | PassToken(const PassToken&) = delete; |
| 45 | PassToken(PassToken&&); |
| 46 | PassToken& operator=(const PassToken&) = delete; |
| 47 | PassToken& operator=(PassToken&&); |
| 48 | |
| 49 | ~PassToken(); |
| 50 | |
| 51 | std::unique_ptr<Impl> impl_; // Unique pointer to internal data. |
| 52 | }; |
| 53 | |
| 54 | // Constructs an instance with the given target |env|, which is used to decode |
| 55 | // the binaries to be optimized later. |
| 56 | // |
| 57 | // The constructed instance will have an empty message consumer, which just |
| 58 | // ignores all messages from the library. Use SetMessageConsumer() to supply |
| 59 | // one if messages are of concern. |
| 60 | explicit Optimizer(spv_target_env env); |
| 61 | |
| 62 | // Disables copy/move constructor/assignment operations. |
| 63 | Optimizer(const Optimizer&) = delete; |
| 64 | Optimizer(Optimizer&&) = delete; |
| 65 | Optimizer& operator=(const Optimizer&) = delete; |
| 66 | Optimizer& operator=(Optimizer&&) = delete; |
| 67 | |
| 68 | // Destructs this instance. |
| 69 | ~Optimizer(); |
| 70 | |
| 71 | // Sets the message consumer to the given |consumer|. The |consumer| will be |
| 72 | // invoked once for each message communicated from the library. |
| 73 | void SetMessageConsumer(MessageConsumer consumer); |
| 74 | |
| 75 | // Registers the given |pass| to this optimizer. Passes will be run in the |
| 76 | // exact order of registration. The token passed in will be consumed by this |
| 77 | // method. |
| 78 | Optimizer& RegisterPass(PassToken&& pass); |
| 79 | |
| 80 | // Optimizes the given SPIR-V module |original_binary| and writes the |
| 81 | // optimized binary into |optimized_binary|. |
| 82 | // Returns true on successful optimization, whether or not the module is |
| 83 | // modified. Returns false if errors occur when processing |original_binary| |
| 84 | // using any of the registered passes. In that case, no further passes are |
| 85 | // excuted and the contents in |optimized_binary| may be invalid. |
| 86 | // |
| 87 | // It's allowed to alias |original_binary| to the start of |optimized_binary|. |
| 88 | bool Run(const uint32_t* original_binary, size_t original_binary_size, |
| 89 | std::vector<uint32_t>* optimized_binary) const; |
| 90 | |
| 91 | private: |
| 92 | struct Impl; // Opaque struct for holding internal data. |
| 93 | std::unique_ptr<Impl> impl_; // Unique pointer to internal data. |
| 94 | }; |
| 95 | |
| 96 | // Creates a null pass. |
| 97 | // A null pass does nothing to the SPIR-V module to be optimized. |
| 98 | Optimizer::PassToken CreateNullPass(); |
| 99 | |
| 100 | // Creates a strip-debug-info pass. |
| 101 | // A strip-debug-info pass removes all debug instructions (as documented in |
| 102 | // Section 3.32.2 of the SPIR-V spec) of the SPIR-V module to be optimized. |
| 103 | Optimizer::PassToken CreateStripDebugInfoPass(); |
| 104 | |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 105 | // Creates an eliminate-dead-functions pass. |
| 106 | // An eliminate-dead-functions pass will remove all functions that are not in the |
| 107 | // call trees rooted at entry points and exported functions. These functions |
| 108 | // are not needed because they will never be called. |
| 109 | Optimizer::PassToken CreateEliminateDeadFunctionsPass(); |
| 110 | |
qining | 144f59e | 2017-04-19 18:10:59 -0400 | [diff] [blame] | 111 | // Creates a set-spec-constant-default-value pass from a mapping from spec-ids |
| 112 | // to the default values in the form of string. |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 113 | // A set-spec-constant-default-value pass sets the default values for the |
| 114 | // spec constants that have SpecId decorations (i.e., those defined by |
| 115 | // OpSpecConstant{|True|False} instructions). |
| 116 | Optimizer::PassToken CreateSetSpecConstantDefaultValuePass( |
| 117 | const std::unordered_map<uint32_t, std::string>& id_value_map); |
| 118 | |
qining | 144f59e | 2017-04-19 18:10:59 -0400 | [diff] [blame] | 119 | // Creates a set-spec-constant-default-value pass from a mapping from spec-ids |
| 120 | // to the default values in the form of bit pattern. |
| 121 | // A set-spec-constant-default-value pass sets the default values for the |
| 122 | // spec constants that have SpecId decorations (i.e., those defined by |
| 123 | // OpSpecConstant{|True|False} instructions). |
| 124 | Optimizer::PassToken CreateSetSpecConstantDefaultValuePass( |
| 125 | const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map); |
| 126 | |
David Neto | 11a867f | 2017-04-01 16:10:16 -0400 | [diff] [blame] | 127 | // Creates a flatten-decoration pass. |
| 128 | // A flatten-decoration pass replaces grouped decorations with equivalent |
| 129 | // ungrouped decorations. That is, it replaces each OpDecorationGroup |
| 130 | // instruction and associated OpGroupDecorate and OpGroupMemberDecorate |
| 131 | // instructions with equivalent OpDecorate and OpMemberDecorate instructions. |
| 132 | // The pass does not attempt to preserve debug information for instructions |
| 133 | // it removes. |
| 134 | Optimizer::PassToken CreateFlattenDecorationPass(); |
| 135 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 136 | // Creates a freeze-spec-constant-value pass. |
| 137 | // A freeze-spec-constant pass specializes the value of spec constants to |
| 138 | // their default values. This pass only processes the spec constants that have |
| 139 | // SpecId decorations (defined by OpSpecConstant, OpSpecConstantTrue, or |
| 140 | // OpSpecConstantFalse instructions) and replaces them with their normal |
| 141 | // counterparts (OpConstant, OpConstantTrue, or OpConstantFalse). The |
| 142 | // corresponding SpecId annotation instructions will also be removed. This |
| 143 | // pass does not fold the newly added normal constants and does not process |
| 144 | // other spec constants defined by OpSpecConstantComposite or |
| 145 | // OpSpecConstantOp. |
| 146 | Optimizer::PassToken CreateFreezeSpecConstantValuePass(); |
| 147 | |
| 148 | // Creates a fold-spec-constant-op-and-composite pass. |
| 149 | // A fold-spec-constant-op-and-composite pass folds spec constants defined by |
| 150 | // OpSpecConstantOp or OpSpecConstantComposite instruction, to normal Constants |
| 151 | // defined by OpConstantTrue, OpConstantFalse, OpConstant, OpConstantNull, or |
| 152 | // OpConstantComposite instructions. Note that spec constants defined with |
| 153 | // OpSpecConstant, OpSpecConstantTrue, or OpSpecConstantFalse instructions are |
| 154 | // not handled, as these instructions indicate their value are not determined |
| 155 | // and can be changed in future. A spec constant is foldable if all of its |
| 156 | // value(s) can be determined from the module. E.g., an integer spec constant |
| 157 | // defined with OpSpecConstantOp instruction can be folded if its value won't |
| 158 | // change later. This pass will replace the original OpSpecContantOp instruction |
| 159 | // with an OpConstant instruction. When folding composite spec constants, |
| 160 | // new instructions may be inserted to define the components of the composite |
| 161 | // constant first, then the original spec constants will be replaced by |
| 162 | // OpConstantComposite instructions. |
| 163 | // |
| 164 | // There are some operations not supported yet: |
| 165 | // OpSConvert, OpFConvert, OpQuantizeToF16 and |
| 166 | // all the operations under Kernel capability. |
| 167 | // TODO(qining): Add support for the operations listed above. |
| 168 | Optimizer::PassToken CreateFoldSpecConstantOpAndCompositePass(); |
| 169 | |
| 170 | // Creates a unify-constant pass. |
| 171 | // A unify-constant pass de-duplicates the constants. Constants with the exact |
| 172 | // same value and identical form will be unified and only one constant will |
| 173 | // be kept for each unique pair of type and value. |
| 174 | // There are several cases not handled by this pass: |
| 175 | // 1) Constants defined by OpConstantNull instructions (null constants) and |
| 176 | // constants defined by OpConstantFalse, OpConstant or OpConstantComposite |
| 177 | // with value 0 (zero-valued normal constants) are not considered equivalent. |
| 178 | // So null constants won't be used to replace zero-valued normal constants, |
| 179 | // vice versa. |
| 180 | // 2) Whenever there are decorations to the constant's result id id, the |
| 181 | // constant won't be handled, which means, it won't be used to replace any |
| 182 | // other constants, neither can other constants replace it. |
| 183 | // 3) NaN in float point format with different bit patterns are not unified. |
| 184 | Optimizer::PassToken CreateUnifyConstantPass(); |
| 185 | |
| 186 | // Creates a eliminate-dead-constant pass. |
| 187 | // A eliminate-dead-constant pass removes dead constants, including normal |
| 188 | // contants defined by OpConstant, OpConstantComposite, OpConstantTrue, or |
| 189 | // OpConstantFalse and spec constants defined by OpSpecConstant, |
| 190 | // OpSpecConstantComposite, OpSpecConstantTrue, OpSpecConstantFalse or |
| 191 | // OpSpecConstantOp. |
| 192 | Optimizer::PassToken CreateEliminateDeadConstantPass(); |
| 193 | |
Steven Perron | e4c7d8e | 2017-09-08 12:08:03 -0400 | [diff] [blame] | 194 | // Creates a strength-reduction pass. |
| 195 | // A strength-reduction pass will look for opportunities to replace an |
| 196 | // instruction with an equivalent and less expensive one. For example, |
| 197 | // multiplying by a power of 2 can be replaced by a bit shift. |
| 198 | Optimizer::PassToken CreateStrengthReductionPass(); |
| 199 | |
GregF | ad1d035 | 2017-06-07 15:28:53 -0600 | [diff] [blame] | 200 | // Creates a block merge pass. |
| 201 | // This pass searches for blocks with a single Branch to a block with no |
| 202 | // other predecessors and merges the blocks into a single block. Continue |
| 203 | // blocks and Merge blocks are not candidates for the second block. |
| 204 | // |
| 205 | // The pass is most useful after Dead Branch Elimination, which can leave |
| 206 | // such sequences of blocks. Merging them makes subsequent passes more |
| 207 | // effective, such as single block local store-load elimination. |
| 208 | // |
| 209 | // While this pass reduces the number of occurrences of this sequence, at |
| 210 | // this time it does not guarantee all such sequences are eliminated. |
| 211 | // |
| 212 | // Presence of phi instructions can inhibit this optimization. Handling |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 213 | // these is left for future improvements. |
GregF | ad1d035 | 2017-06-07 15:28:53 -0600 | [diff] [blame] | 214 | Optimizer::PassToken CreateBlockMergePass(); |
| 215 | |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 216 | // Creates an exhaustive inline pass. |
| 217 | // An exhaustive inline pass attempts to exhaustively inline all function |
| 218 | // calls in all functions in an entry point call tree. The intent is to enable, |
| 219 | // albeit through brute force, analysis and optimization across function |
| 220 | // calls by subsequent optimization passes. As the inlining is exhaustive, |
| 221 | // there is no attempt to optimize for size or runtime performance. Functions |
| 222 | // 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] | 223 | Optimizer::PassToken CreateInlineExhaustivePass(); |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 224 | |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 225 | // Creates an opaque inline pass. |
| 226 | // An opaque inline pass inlines all function calls in all functions in all |
| 227 | // entry point call trees where the called function contains an opaque type |
| 228 | // in either its parameter types or return type. An opaque type is currently |
| 229 | // defined as Image, Sampler or SampledImage. The intent is to enable, albeit |
| 230 | // through brute force, analysis and optimization across these function calls |
| 231 | // by subsequent passes in order to remove the storing of opaque types which is |
| 232 | // not legal in Vulkan. Functions that are not in the call tree of an entry |
| 233 | // point are not changed. |
| 234 | Optimizer::PassToken CreateInlineOpaquePass(); |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 235 | |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 236 | // Creates a single-block local variable load/store elimination pass. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 237 | // For every entry point function, do single block memory optimization of |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 238 | // function variables referenced only with non-access-chain loads and stores. |
| 239 | // For each targeted variable load, if previous store to that variable in the |
| 240 | // block, replace the load's result id with the value id of the store. |
| 241 | // If previous load within the block, replace the current load's result id |
| 242 | // with the previous load's result id. In either case, delete the current |
| 243 | // load. Finally, check if any remaining stores are useless, and delete store |
| 244 | // and variable if possible. |
| 245 | // |
| 246 | // The presence of access chain references and function calls can inhibit |
| 247 | // the above optimization. |
| 248 | // |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 249 | // Only modules with logical addressing are currently processed. |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 250 | // |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 251 | // This pass is most effective if preceeded by Inlining and |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 252 | // LocalAccessChainConvert. This pass will reduce the work needed to be done |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 253 | // by LocalSingleStoreElim and LocalMultiStoreElim. |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 254 | // |
| 255 | // Only functions in the call tree of an entry point are processed. |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 256 | Optimizer::PassToken CreateLocalSingleBlockLoadStoreElimPass(); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 257 | |
GregF | 52e247f | 2017-06-02 13:23:20 -0600 | [diff] [blame] | 258 | // Create dead branch elimination pass. |
| 259 | // For each entry point function, this pass will look for SelectionMerge |
| 260 | // BranchConditionals with constant condition and convert to a Branch to |
| 261 | // the indicated label. It will delete resulting dead blocks. |
| 262 | // |
| 263 | // This pass only works on shaders (guaranteed to have structured control |
| 264 | // flow). Note that some such branches and blocks may be left to avoid |
| 265 | // creating invalid control flow. Improving this is left to future work. |
| 266 | // |
| 267 | // This pass is most effective when preceeded by passes which eliminate |
| 268 | // local loads and stores, effectively propagating constant values where |
| 269 | // possible. |
| 270 | Optimizer::PassToken CreateDeadBranchElimPass(); |
| 271 | |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 272 | // Creates an SSA local variable load/store elimination pass. |
| 273 | // For every entry point function, eliminate all loads and stores of function |
| 274 | // scope variables only referenced with non-access-chain loads and stores. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 275 | // Eliminate the variables as well. |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 276 | // |
| 277 | // The presence of access chain references and function calls can inhibit |
| 278 | // the above optimization. |
| 279 | // |
| 280 | // Only shader modules with logical addressing are currently processed. |
| 281 | // Currently modules with any extensions enabled are not processed. This |
| 282 | // is left for future work. |
| 283 | // |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 284 | // This pass is most effective if preceeded by Inlining and |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 285 | // LocalAccessChainConvert. LocalSingleStoreElim and LocalSingleBlockElim |
| 286 | // will reduce the work that this pass has to do. |
| 287 | Optimizer::PassToken CreateLocalMultiStoreElimPass(); |
| 288 | |
GregF | aa7e687 | 2017-05-12 17:27:21 -0600 | [diff] [blame] | 289 | // Creates a local access chain conversion pass. |
| 290 | // A local access chain conversion pass identifies all function scope |
| 291 | // variables which are accessed only with loads, stores and access chains |
| 292 | // with constant indices. It then converts all loads and stores of such |
| 293 | // variables into equivalent sequences of loads, stores, extracts and inserts. |
| 294 | // |
| 295 | // This pass only processes entry point functions. It currently only converts |
| 296 | // non-nested, non-ptr access chains. It does not process modules with |
| 297 | // non-32-bit integer types present. Optional memory access options on loads |
| 298 | // and stores are ignored as we are only processing function scope variables. |
| 299 | // |
| 300 | // This pass unifies access to these variables to a single mode and simplifies |
| 301 | // subsequent analysis and elimination of these variables along with their |
| 302 | // loads and stores allowing values to propagate to their points of use where |
| 303 | // possible. |
| 304 | Optimizer::PassToken CreateLocalAccessChainConvertPass(); |
| 305 | |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 306 | // Create aggressive dead code elimination pass |
| 307 | // This pass eliminates unused code from functions. In addition, |
| 308 | // it detects and eliminates code which may have spurious uses but which do |
| 309 | // not contribute to the output of the function. The most common cause of |
| 310 | // such code sequences is summations in loops whose result is no longer used |
| 311 | // due to dead code elimination. This optimization has additional compile |
| 312 | // time cost over standard dead code elimination. |
| 313 | // |
| 314 | // This pass only processes entry point functions. It also only processes |
| 315 | // shaders with logical addressing. It currently will not process functions |
| 316 | // with function calls. It currently only supports the GLSL.std.450 extended |
| 317 | // instruction set. It currently does not support any extensions. |
| 318 | // |
| 319 | // This pass will be made more effective by first running passes that remove |
| 320 | // dead control flow and inlines function calls. |
| 321 | // |
| 322 | // This pass can be especially useful after running Local Access Chain |
| 323 | // Conversion, which tends to cause cycles of dead code to be left after |
| 324 | // Store/Load elimination passes are completed. These cycles cannot be |
| 325 | // eliminated with standard dead code elimination. |
| 326 | Optimizer::PassToken CreateAggressiveDCEPass(); |
| 327 | |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 328 | // Creates a local single store elimination pass. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame^] | 329 | // For each entry point function, this pass eliminates loads and stores for |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 330 | // function scope variable that are stored to only once, where possible. Only |
| 331 | // whole variable loads and stores are eliminated; access-chain references are |
| 332 | // not optimized. Replace all loads of such variables with the value that is |
| 333 | // stored and eliminate any resulting dead code. |
| 334 | // |
| 335 | // Currently, the presence of access chains and function calls can inhibit this |
| 336 | // pass, however the Inlining and LocalAccessChainConvert passes can make it |
| 337 | // more effective. In additional, many non-load/store memory operations are |
| 338 | // not supported and will prohibit optimization of a function. Support of |
| 339 | // these operations are future work. |
| 340 | // |
| 341 | // This pass will reduce the work needed to be done by LocalSingleBlockElim |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 342 | // and LocalMultiStoreElim and can improve the effectiveness of other passes |
| 343 | // such as DeadBranchElimination which depend on values for their analysis. |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 344 | Optimizer::PassToken CreateLocalSingleStoreElimPass(); |
| 345 | |
GregF | 6136bf9 | 2017-05-26 10:33:11 -0600 | [diff] [blame] | 346 | // Creates an insert/extract elimination pass. |
| 347 | // This pass processes each entry point function in the module, searching for |
| 348 | // extracts on a sequence of inserts. It further searches the sequence for an |
| 349 | // insert with indices identical to the extract. If such an insert can be |
| 350 | // found before hitting a conflicting insert, the extract's result id is |
| 351 | // replaced with the id of the values from the insert. |
| 352 | // |
| 353 | // Besides removing extracts this pass enables subsequent dead code elimination |
| 354 | // passes to delete the inserts. This pass performs best after access chains are |
| 355 | // converted to inserts and extracts and local loads and stores are eliminated. |
| 356 | Optimizer::PassToken CreateInsertExtractElimPass(); |
| 357 | |
GregF | 52e247f | 2017-06-02 13:23:20 -0600 | [diff] [blame] | 358 | // Create dead branch elimination pass. |
| 359 | // For each entry point function, this pass will look for BranchConditionals |
| 360 | // with constant condition and convert to a branch. The BranchConditional must |
| 361 | // be preceeded by OpSelectionMerge. For all phi functions in merge block, |
| 362 | // replace all uses with the id corresponding to the living predecessor. |
| 363 | // |
| 364 | // This pass is most effective when preceeded by passes which eliminate |
| 365 | // local loads and stores, effectively propagating constant values where |
| 366 | // possible. |
| 367 | Optimizer::PassToken CreateDeadBranchElimPass(); |
| 368 | |
GregF | f4b29f3 | 2017-07-03 17:23:04 -0600 | [diff] [blame] | 369 | // Creates a pass to consolidate uniform references. |
| 370 | // 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^] | 371 | // access chain loads into equivalent composite extracts. Then consolidate |
GregF | f4b29f3 | 2017-07-03 17:23:04 -0600 | [diff] [blame] | 372 | // identical uniform loads into one uniform load. Finally, consolidate |
| 373 | // identical uniform extracts into one uniform extract. This may require |
| 374 | // moving a load or extract to a point which dominates all uses. |
| 375 | // |
| 376 | // This pass requires a module to have structured control flow ie shader |
| 377 | // capability. It also requires logical addressing ie Addresses capability |
| 378 | // is not enabled. It also currently does not support any extensions. |
| 379 | // |
| 380 | // This pass currently only optimizes loads with a single index. |
| 381 | Optimizer::PassToken CreateCommonUniformElimPass(); |
| 382 | |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 383 | // Create aggressive dead code elimination pass |
| 384 | // This pass eliminates unused code from functions. In addition, |
| 385 | // it detects and eliminates code which may have spurious uses but which do |
| 386 | // not contribute to the output of the function. The most common cause of |
| 387 | // such code sequences is summations in loops whose result is no longer used |
| 388 | // due to dead code elimination. This optimization has additional compile |
| 389 | // time cost over standard dead code elimination. |
| 390 | // |
| 391 | // This pass only processes entry point functions. It also only processes |
| 392 | // shaders with logical addressing. It currently will not process functions |
| 393 | // with function calls. |
| 394 | // |
| 395 | // This pass will be made more effective by first running passes that remove |
| 396 | // dead control flow and inlines function calls. |
| 397 | // |
| 398 | // This pass can be especially useful after running Local Access Chain |
| 399 | // Conversion, which tends to cause cycles of dead code to be left after |
| 400 | // Store/Load elimination passes are completed. These cycles cannot be |
| 401 | // eliminated with standard dead code elimination. |
| 402 | Optimizer::PassToken CreateAggressiveDCEPass(); |
| 403 | |
Andrey Tuganov | 1e309af | 2017-04-11 15:11:04 -0400 | [diff] [blame] | 404 | // Creates a compact ids pass. |
| 405 | // The pass remaps result ids to a compact and gapless range starting from %1. |
| 406 | Optimizer::PassToken CreateCompactIdsPass(); |
| 407 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 408 | } // namespace spvtools |
| 409 | |
| 410 | #endif // SPIRV_TOOLS_OPTIMIZER_HPP_ |