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 | |
dan sinclair | 58a6876 | 2018-08-03 08:05:33 -0400 | [diff] [blame] | 15 | #ifndef INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_ |
| 16 | #define INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_ |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 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 | |
Arseny Kapoulkine | f765d16 | 2018-05-22 14:31:26 -0700 | [diff] [blame] | 28 | namespace opt { |
| 29 | class Pass; |
| 30 | } |
| 31 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 32 | // C++ interface for SPIR-V optimization functionalities. It wraps the context |
| 33 | // (including target environment and the corresponding SPIR-V grammar) and |
| 34 | // provides methods for registering optimization passes and optimizing. |
| 35 | // |
| 36 | // Instances of this class provides basic thread-safety guarantee. |
| 37 | class Optimizer { |
| 38 | public: |
| 39 | // The token for an optimization pass. It is returned via one of the |
| 40 | // Create*Pass() standalone functions at the end of this header file and |
| 41 | // consumed by the RegisterPass() method. Tokens are one-time objects that |
| 42 | // only support move; copying is not allowed. |
| 43 | struct PassToken { |
| 44 | struct Impl; // Opaque struct for holding inernal data. |
| 45 | |
| 46 | PassToken(std::unique_ptr<Impl>); |
| 47 | |
Arseny Kapoulkine | f765d16 | 2018-05-22 14:31:26 -0700 | [diff] [blame] | 48 | // Tokens for built-in passes should be created using Create*Pass functions |
| 49 | // below; for out-of-tree passes, use this constructor instead. |
| 50 | // Note that this API isn't guaranteed to be stable and may change without |
| 51 | // preserving source or binary compatibility in the future. |
| 52 | PassToken(std::unique_ptr<opt::Pass>&& pass); |
| 53 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 54 | // Tokens can only be moved. Copying is disabled. |
| 55 | PassToken(const PassToken&) = delete; |
| 56 | PassToken(PassToken&&); |
| 57 | PassToken& operator=(const PassToken&) = delete; |
| 58 | PassToken& operator=(PassToken&&); |
| 59 | |
| 60 | ~PassToken(); |
| 61 | |
| 62 | std::unique_ptr<Impl> impl_; // Unique pointer to internal data. |
| 63 | }; |
| 64 | |
| 65 | // Constructs an instance with the given target |env|, which is used to decode |
| 66 | // the binaries to be optimized later. |
| 67 | // |
| 68 | // The constructed instance will have an empty message consumer, which just |
| 69 | // ignores all messages from the library. Use SetMessageConsumer() to supply |
| 70 | // one if messages are of concern. |
| 71 | explicit Optimizer(spv_target_env env); |
| 72 | |
| 73 | // Disables copy/move constructor/assignment operations. |
| 74 | Optimizer(const Optimizer&) = delete; |
| 75 | Optimizer(Optimizer&&) = delete; |
| 76 | Optimizer& operator=(const Optimizer&) = delete; |
| 77 | Optimizer& operator=(Optimizer&&) = delete; |
| 78 | |
| 79 | // Destructs this instance. |
| 80 | ~Optimizer(); |
| 81 | |
| 82 | // Sets the message consumer to the given |consumer|. The |consumer| will be |
| 83 | // invoked once for each message communicated from the library. |
| 84 | void SetMessageConsumer(MessageConsumer consumer); |
| 85 | |
Diego Novillo | 99fe61e | 2018-07-25 15:21:44 -0400 | [diff] [blame] | 86 | // Returns a reference to the registered message consumer. |
| 87 | const MessageConsumer& consumer() const; |
| 88 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 89 | // Registers the given |pass| to this optimizer. Passes will be run in the |
| 90 | // exact order of registration. The token passed in will be consumed by this |
| 91 | // method. |
| 92 | Optimizer& RegisterPass(PassToken&& pass); |
| 93 | |
Diego Novillo | c90d730 | 2017-08-30 14:19:22 -0400 | [diff] [blame] | 94 | // Registers passes that attempt to improve performance of generated code. |
| 95 | // This sequence of passes is subject to constant review and will change |
| 96 | // from time to time. |
| 97 | Optimizer& RegisterPerformancePasses(); |
| 98 | |
| 99 | // Registers passes that attempt to improve the size of generated code. |
| 100 | // This sequence of passes is subject to constant review and will change |
| 101 | // from time to time. |
| 102 | Optimizer& RegisterSizePasses(); |
| 103 | |
Ryan Harrison | 47c08a7 | 2018-12-18 15:10:34 -0500 | [diff] [blame] | 104 | // Registers passes that have been prescribed for WebGPU environments. |
| 105 | // This sequence of passes is subject to constant review and will change |
| 106 | // from time to time. |
| 107 | Optimizer& RegisterWebGPUPasses(); |
| 108 | |
Lei Zhang | aec60b8 | 2017-11-17 16:50:43 -0500 | [diff] [blame] | 109 | // Registers passes that attempt to legalize the generated code. |
| 110 | // |
Diego Novillo | 99fe61e | 2018-07-25 15:21:44 -0400 | [diff] [blame] | 111 | // Note: this recipe is specially designed for legalizing SPIR-V. It should be |
| 112 | // used by compilers after translating HLSL source code literally. It should |
Lei Zhang | aec60b8 | 2017-11-17 16:50:43 -0500 | [diff] [blame] | 113 | // *not* be used by general workloads for performance or size improvement. |
| 114 | // |
| 115 | // This sequence of passes is subject to constant review and will change |
| 116 | // from time to time. |
| 117 | Optimizer& RegisterLegalizationPasses(); |
| 118 | |
Diego Novillo | 99fe61e | 2018-07-25 15:21:44 -0400 | [diff] [blame] | 119 | // Register passes specified in the list of |flags|. Each flag must be a |
| 120 | // string of a form accepted by Optimizer::FlagHasValidForm(). |
| 121 | // |
| 122 | // If the list of flags contains an invalid entry, it returns false and an |
| 123 | // error message is emitted to the MessageConsumer object (use |
| 124 | // Optimizer::SetMessageConsumer to define a message consumer, if needed). |
| 125 | // |
| 126 | // If all the passes are registered successfully, it returns true. |
| 127 | bool RegisterPassesFromFlags(const std::vector<std::string>& flags); |
| 128 | |
| 129 | // Registers the optimization pass associated with |flag|. This only accepts |
| 130 | // |flag| values of the form "--pass_name[=pass_args]". If no such pass |
| 131 | // exists, it returns false. Otherwise, the pass is registered and it returns |
| 132 | // true. |
| 133 | // |
| 134 | // The following flags have special meaning: |
| 135 | // |
| 136 | // -O: Registers all performance optimization passes |
| 137 | // (Optimizer::RegisterPerformancePasses) |
| 138 | // |
| 139 | // -Os: Registers all size optimization passes |
| 140 | // (Optimizer::RegisterSizePasses). |
| 141 | // |
| 142 | // --legalize-hlsl: Registers all passes that legalize SPIR-V generated by an |
| 143 | // HLSL front-end. |
| 144 | bool RegisterPassFromFlag(const std::string& flag); |
| 145 | |
| 146 | // Validates that |flag| has a valid format. Strings accepted: |
| 147 | // |
| 148 | // --pass_name[=pass_args] |
| 149 | // -O |
| 150 | // -Os |
| 151 | // |
| 152 | // If |flag| takes one of the forms above, it returns true. Otherwise, it |
| 153 | // returns false. |
| 154 | bool FlagHasValidForm(const std::string& flag) const; |
| 155 | |
Ryan Harrison | e0292c2 | 2018-12-17 16:54:23 -0500 | [diff] [blame] | 156 | // Allows changing, after creation time, the target environment to be |
| 157 | // optimized for. Should be called before calling Run(). |
| 158 | void SetTargetEnv(const spv_target_env env); |
| 159 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 160 | // Optimizes the given SPIR-V module |original_binary| and writes the |
| 161 | // optimized binary into |optimized_binary|. |
| 162 | // Returns true on successful optimization, whether or not the module is |
Steven Perron | bcb0b69 | 2018-08-13 13:18:46 -0400 | [diff] [blame] | 163 | // modified. Returns false if |original_binary| fails to validate or if errors |
| 164 | // occur when processing |original_binary| using any of the registered passes. |
| 165 | // In that case, no further passes are executed and the contents in |
| 166 | // |optimized_binary| may be invalid. |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 167 | // |
| 168 | // It's allowed to alias |original_binary| to the start of |optimized_binary|. |
| 169 | bool Run(const uint32_t* original_binary, size_t original_binary_size, |
| 170 | std::vector<uint32_t>* optimized_binary) const; |
Steven Perron | bcb0b69 | 2018-08-13 13:18:46 -0400 | [diff] [blame] | 171 | |
Steven Perron | 75c1bf2 | 2018-09-10 11:49:41 -0400 | [diff] [blame] | 172 | // DEPRECATED: Same as above, except passes |options| to the validator when |
| 173 | // trying to validate the binary. If |skip_validation| is true, then the |
| 174 | // caller is guaranteeing that |original_binary| is valid, and the validator |
| 175 | // will not be run. The |max_id_bound| is the limit on the max id in the |
| 176 | // module. |
Steven Perron | bcb0b69 | 2018-08-13 13:18:46 -0400 | [diff] [blame] | 177 | bool Run(const uint32_t* original_binary, const size_t original_binary_size, |
Steven Perron | 5c8b4f5 | 2018-08-08 11:16:19 -0400 | [diff] [blame] | 178 | std::vector<uint32_t>* optimized_binary, |
Steven Perron | 75c1bf2 | 2018-09-10 11:49:41 -0400 | [diff] [blame] | 179 | const ValidatorOptions& options, bool skip_validation) const; |
| 180 | |
| 181 | // Same as above, except it takes an options object. See the documentation |
| 182 | // for |OptimizerOptions| to see which options can be set. |
| 183 | bool Run(const uint32_t* original_binary, const size_t original_binary_size, |
| 184 | std::vector<uint32_t>* optimized_binary, |
| 185 | const spv_optimizer_options opt_options) const; |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 186 | |
Diego Novillo | c90d730 | 2017-08-30 14:19:22 -0400 | [diff] [blame] | 187 | // Returns a vector of strings with all the pass names added to this |
| 188 | // optimizer's pass manager. These strings are valid until the associated |
| 189 | // pass manager is destroyed. |
Steven Perron | 5834719 | 2017-10-20 12:17:41 -0400 | [diff] [blame] | 190 | std::vector<const char*> GetPassNames() const; |
Diego Novillo | c90d730 | 2017-08-30 14:19:22 -0400 | [diff] [blame] | 191 | |
David Neto | c32e79e | 2018-01-04 12:59:50 -0500 | [diff] [blame] | 192 | // Sets the option to print the disassembly before each pass and after the |
| 193 | // last pass. If |out| is null, then no output is generated. Otherwise, |
| 194 | // output is sent to the |out| output stream. |
| 195 | Optimizer& SetPrintAll(std::ostream* out); |
| 196 | |
Jaebaek Seo | 3b594e1 | 2018-03-07 09:25:51 -0500 | [diff] [blame] | 197 | // Sets the option to print the resource utilization of each pass. If |out| |
| 198 | // is null, then no output is generated. Otherwise, output is sent to the |
| 199 | // |out| output stream. |
| 200 | Optimizer& SetTimeReport(std::ostream* out); |
| 201 | |
alan-baker | 42e6f1a | 2019-03-26 14:38:59 -0400 | [diff] [blame] | 202 | // Sets the option to validate the module after each pass. |
| 203 | Optimizer& SetValidateAfterAll(bool validate); |
| 204 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 205 | private: |
| 206 | struct Impl; // Opaque struct for holding internal data. |
| 207 | std::unique_ptr<Impl> impl_; // Unique pointer to internal data. |
| 208 | }; |
| 209 | |
| 210 | // Creates a null pass. |
| 211 | // A null pass does nothing to the SPIR-V module to be optimized. |
| 212 | Optimizer::PassToken CreateNullPass(); |
| 213 | |
Ryan Harrison | e545522 | 2019-03-14 13:34:33 -0400 | [diff] [blame] | 214 | // Creates a strip-atomic-counter-memory pass. |
| 215 | // A strip-atomic-counter-memory pass removes all usages of the |
| 216 | // AtomicCounterMemory bit in Memory Semantics bitmasks. This bit is a no-op in |
| 217 | // Vulkan, so isn't needed in that env. And the related capability is not |
| 218 | // allowed in WebGPU, so it is not allowed in that env. |
| 219 | Optimizer::PassToken CreateStripAtomicCounterMemoryPass(); |
| 220 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 221 | // Creates a strip-debug-info pass. |
| 222 | // A strip-debug-info pass removes all debug instructions (as documented in |
| 223 | // Section 3.32.2 of the SPIR-V spec) of the SPIR-V module to be optimized. |
| 224 | Optimizer::PassToken CreateStripDebugInfoPass(); |
| 225 | |
David Neto | 844e186 | 2018-03-09 16:08:57 -0500 | [diff] [blame] | 226 | // Creates a strip-reflect-info pass. |
| 227 | // A strip-reflect-info pass removes all reflections instructions. |
| 228 | // For now, this is limited to removing decorations defined in |
| 229 | // SPV_GOOGLE_hlsl_functionality1. The coverage may expand in |
| 230 | // the future. |
| 231 | Optimizer::PassToken CreateStripReflectInfoPass(); |
| 232 | |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 233 | // Creates an eliminate-dead-functions pass. |
Steven Perron | 5834719 | 2017-10-20 12:17:41 -0400 | [diff] [blame] | 234 | // An eliminate-dead-functions pass will remove all functions that are not in |
| 235 | // the call trees rooted at entry points and exported functions. These |
| 236 | // functions are not needed because they will never be called. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 237 | Optimizer::PassToken CreateEliminateDeadFunctionsPass(); |
| 238 | |
Steven Perron | 1b0047f | 2019-02-14 13:42:35 -0500 | [diff] [blame] | 239 | // Creates an eliminate-dead-members pass. |
| 240 | // An eliminate-dead-members pass will remove all unused members of structures. |
| 241 | // This will not affect the data layout of the remaining members. |
| 242 | Optimizer::PassToken CreateEliminateDeadMembersPass(); |
| 243 | |
qining | 144f59e | 2017-04-19 18:10:59 -0400 | [diff] [blame] | 244 | // Creates a set-spec-constant-default-value pass from a mapping from spec-ids |
| 245 | // to the default values in the form of string. |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 246 | // A set-spec-constant-default-value pass sets the default values for the |
| 247 | // spec constants that have SpecId decorations (i.e., those defined by |
| 248 | // OpSpecConstant{|True|False} instructions). |
| 249 | Optimizer::PassToken CreateSetSpecConstantDefaultValuePass( |
| 250 | const std::unordered_map<uint32_t, std::string>& id_value_map); |
| 251 | |
qining | 144f59e | 2017-04-19 18:10:59 -0400 | [diff] [blame] | 252 | // Creates a set-spec-constant-default-value pass from a mapping from spec-ids |
| 253 | // to the default values in the form of bit pattern. |
| 254 | // A set-spec-constant-default-value pass sets the default values for the |
| 255 | // spec constants that have SpecId decorations (i.e., those defined by |
| 256 | // OpSpecConstant{|True|False} instructions). |
| 257 | Optimizer::PassToken CreateSetSpecConstantDefaultValuePass( |
| 258 | const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map); |
| 259 | |
David Neto | 11a867f | 2017-04-01 16:10:16 -0400 | [diff] [blame] | 260 | // Creates a flatten-decoration pass. |
| 261 | // A flatten-decoration pass replaces grouped decorations with equivalent |
| 262 | // ungrouped decorations. That is, it replaces each OpDecorationGroup |
| 263 | // instruction and associated OpGroupDecorate and OpGroupMemberDecorate |
| 264 | // instructions with equivalent OpDecorate and OpMemberDecorate instructions. |
| 265 | // The pass does not attempt to preserve debug information for instructions |
| 266 | // it removes. |
| 267 | Optimizer::PassToken CreateFlattenDecorationPass(); |
| 268 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 269 | // Creates a freeze-spec-constant-value pass. |
| 270 | // A freeze-spec-constant pass specializes the value of spec constants to |
| 271 | // their default values. This pass only processes the spec constants that have |
| 272 | // SpecId decorations (defined by OpSpecConstant, OpSpecConstantTrue, or |
| 273 | // OpSpecConstantFalse instructions) and replaces them with their normal |
| 274 | // counterparts (OpConstant, OpConstantTrue, or OpConstantFalse). The |
| 275 | // corresponding SpecId annotation instructions will also be removed. This |
| 276 | // pass does not fold the newly added normal constants and does not process |
| 277 | // other spec constants defined by OpSpecConstantComposite or |
| 278 | // OpSpecConstantOp. |
| 279 | Optimizer::PassToken CreateFreezeSpecConstantValuePass(); |
| 280 | |
| 281 | // Creates a fold-spec-constant-op-and-composite pass. |
| 282 | // A fold-spec-constant-op-and-composite pass folds spec constants defined by |
| 283 | // OpSpecConstantOp or OpSpecConstantComposite instruction, to normal Constants |
| 284 | // defined by OpConstantTrue, OpConstantFalse, OpConstant, OpConstantNull, or |
| 285 | // OpConstantComposite instructions. Note that spec constants defined with |
| 286 | // OpSpecConstant, OpSpecConstantTrue, or OpSpecConstantFalse instructions are |
| 287 | // not handled, as these instructions indicate their value are not determined |
| 288 | // and can be changed in future. A spec constant is foldable if all of its |
| 289 | // value(s) can be determined from the module. E.g., an integer spec constant |
| 290 | // defined with OpSpecConstantOp instruction can be folded if its value won't |
| 291 | // change later. This pass will replace the original OpSpecContantOp instruction |
| 292 | // with an OpConstant instruction. When folding composite spec constants, |
| 293 | // new instructions may be inserted to define the components of the composite |
| 294 | // constant first, then the original spec constants will be replaced by |
| 295 | // OpConstantComposite instructions. |
| 296 | // |
| 297 | // There are some operations not supported yet: |
| 298 | // OpSConvert, OpFConvert, OpQuantizeToF16 and |
| 299 | // all the operations under Kernel capability. |
| 300 | // TODO(qining): Add support for the operations listed above. |
| 301 | Optimizer::PassToken CreateFoldSpecConstantOpAndCompositePass(); |
| 302 | |
| 303 | // Creates a unify-constant pass. |
| 304 | // A unify-constant pass de-duplicates the constants. Constants with the exact |
| 305 | // same value and identical form will be unified and only one constant will |
| 306 | // be kept for each unique pair of type and value. |
| 307 | // There are several cases not handled by this pass: |
| 308 | // 1) Constants defined by OpConstantNull instructions (null constants) and |
| 309 | // constants defined by OpConstantFalse, OpConstant or OpConstantComposite |
| 310 | // with value 0 (zero-valued normal constants) are not considered equivalent. |
| 311 | // So null constants won't be used to replace zero-valued normal constants, |
| 312 | // vice versa. |
| 313 | // 2) Whenever there are decorations to the constant's result id id, the |
| 314 | // constant won't be handled, which means, it won't be used to replace any |
| 315 | // other constants, neither can other constants replace it. |
| 316 | // 3) NaN in float point format with different bit patterns are not unified. |
| 317 | Optimizer::PassToken CreateUnifyConstantPass(); |
| 318 | |
| 319 | // Creates a eliminate-dead-constant pass. |
| 320 | // A eliminate-dead-constant pass removes dead constants, including normal |
| 321 | // contants defined by OpConstant, OpConstantComposite, OpConstantTrue, or |
| 322 | // OpConstantFalse and spec constants defined by OpSpecConstant, |
| 323 | // OpSpecConstantComposite, OpSpecConstantTrue, OpSpecConstantFalse or |
| 324 | // OpSpecConstantOp. |
| 325 | Optimizer::PassToken CreateEliminateDeadConstantPass(); |
| 326 | |
Steven Perron | e4c7d8e | 2017-09-08 12:08:03 -0400 | [diff] [blame] | 327 | // Creates a strength-reduction pass. |
| 328 | // A strength-reduction pass will look for opportunities to replace an |
| 329 | // instruction with an equivalent and less expensive one. For example, |
| 330 | // multiplying by a power of 2 can be replaced by a bit shift. |
| 331 | Optimizer::PassToken CreateStrengthReductionPass(); |
| 332 | |
GregF | ad1d035 | 2017-06-07 15:28:53 -0600 | [diff] [blame] | 333 | // Creates a block merge pass. |
| 334 | // This pass searches for blocks with a single Branch to a block with no |
| 335 | // other predecessors and merges the blocks into a single block. Continue |
| 336 | // blocks and Merge blocks are not candidates for the second block. |
| 337 | // |
| 338 | // The pass is most useful after Dead Branch Elimination, which can leave |
| 339 | // such sequences of blocks. Merging them makes subsequent passes more |
| 340 | // effective, such as single block local store-load elimination. |
| 341 | // |
| 342 | // While this pass reduces the number of occurrences of this sequence, at |
| 343 | // this time it does not guarantee all such sequences are eliminated. |
| 344 | // |
| 345 | // Presence of phi instructions can inhibit this optimization. Handling |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 346 | // these is left for future improvements. |
GregF | ad1d035 | 2017-06-07 15:28:53 -0600 | [diff] [blame] | 347 | Optimizer::PassToken CreateBlockMergePass(); |
| 348 | |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 349 | // Creates an exhaustive inline pass. |
| 350 | // An exhaustive inline pass attempts to exhaustively inline all function |
| 351 | // calls in all functions in an entry point call tree. The intent is to enable, |
| 352 | // albeit through brute force, analysis and optimization across function |
| 353 | // calls by subsequent optimization passes. As the inlining is exhaustive, |
| 354 | // there is no attempt to optimize for size or runtime performance. Functions |
| 355 | // 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] | 356 | Optimizer::PassToken CreateInlineExhaustivePass(); |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 357 | |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 358 | // Creates an opaque inline pass. |
| 359 | // An opaque inline pass inlines all function calls in all functions in all |
| 360 | // entry point call trees where the called function contains an opaque type |
| 361 | // in either its parameter types or return type. An opaque type is currently |
| 362 | // defined as Image, Sampler or SampledImage. The intent is to enable, albeit |
| 363 | // through brute force, analysis and optimization across these function calls |
| 364 | // by subsequent passes in order to remove the storing of opaque types which is |
| 365 | // not legal in Vulkan. Functions that are not in the call tree of an entry |
| 366 | // point are not changed. |
| 367 | Optimizer::PassToken CreateInlineOpaquePass(); |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 368 | |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 369 | // Creates a single-block local variable load/store elimination pass. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 370 | // For every entry point function, do single block memory optimization of |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 371 | // function variables referenced only with non-access-chain loads and stores. |
| 372 | // For each targeted variable load, if previous store to that variable in the |
| 373 | // block, replace the load's result id with the value id of the store. |
| 374 | // If previous load within the block, replace the current load's result id |
| 375 | // with the previous load's result id. In either case, delete the current |
| 376 | // load. Finally, check if any remaining stores are useless, and delete store |
| 377 | // and variable if possible. |
| 378 | // |
| 379 | // The presence of access chain references and function calls can inhibit |
| 380 | // the above optimization. |
| 381 | // |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 382 | // Only modules with relaxed logical addressing (see opt/instruction.h) are |
| 383 | // currently processed. |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 384 | // |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 385 | // This pass is most effective if preceeded by Inlining and |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 386 | // LocalAccessChainConvert. This pass will reduce the work needed to be done |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 387 | // by LocalSingleStoreElim and LocalMultiStoreElim. |
GregF | 429ca05 | 2017-08-15 17:58:28 -0600 | [diff] [blame] | 388 | // |
| 389 | // Only functions in the call tree of an entry point are processed. |
GregF | 7c8da66 | 2017-05-18 14:51:55 -0600 | [diff] [blame] | 390 | Optimizer::PassToken CreateLocalSingleBlockLoadStoreElimPass(); |
Greg Fischer | 04fcc66 | 2016-11-10 10:11:50 -0700 | [diff] [blame] | 391 | |
GregF | 52e247f | 2017-06-02 13:23:20 -0600 | [diff] [blame] | 392 | // Create dead branch elimination pass. |
| 393 | // For each entry point function, this pass will look for SelectionMerge |
| 394 | // BranchConditionals with constant condition and convert to a Branch to |
| 395 | // the indicated label. It will delete resulting dead blocks. |
| 396 | // |
Andrey Tuganov | 4b1577a | 2017-10-05 16:26:09 -0400 | [diff] [blame] | 397 | // For all phi functions in merge block, replace all uses with the id |
| 398 | // corresponding to the living predecessor. |
| 399 | // |
Alan Baker | 1b6cfd3 | 2018-01-04 17:04:03 -0500 | [diff] [blame] | 400 | // Note that some branches and blocks may be left to avoid creating invalid |
| 401 | // control flow. Improving this is left to future work. |
GregF | 52e247f | 2017-06-02 13:23:20 -0600 | [diff] [blame] | 402 | // |
| 403 | // This pass is most effective when preceeded by passes which eliminate |
| 404 | // local loads and stores, effectively propagating constant values where |
| 405 | // possible. |
| 406 | Optimizer::PassToken CreateDeadBranchElimPass(); |
| 407 | |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 408 | // Creates an SSA local variable load/store elimination pass. |
| 409 | // For every entry point function, eliminate all loads and stores of function |
| 410 | // scope variables only referenced with non-access-chain loads and stores. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 411 | // Eliminate the variables as well. |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 412 | // |
| 413 | // The presence of access chain references and function calls can inhibit |
| 414 | // the above optimization. |
| 415 | // |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 416 | // Only shader modules with relaxed logical addressing (see opt/instruction.h) |
| 417 | // are currently processed. Currently modules with any extensions enabled are |
| 418 | // not processed. This is left for future work. |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 419 | // |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 420 | // This pass is most effective if preceeded by Inlining and |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 421 | // LocalAccessChainConvert. LocalSingleStoreElim and LocalSingleBlockElim |
| 422 | // will reduce the work that this pass has to do. |
| 423 | Optimizer::PassToken CreateLocalMultiStoreElimPass(); |
| 424 | |
GregF | aa7e687 | 2017-05-12 17:27:21 -0600 | [diff] [blame] | 425 | // Creates a local access chain conversion pass. |
| 426 | // A local access chain conversion pass identifies all function scope |
| 427 | // variables which are accessed only with loads, stores and access chains |
| 428 | // with constant indices. It then converts all loads and stores of such |
| 429 | // variables into equivalent sequences of loads, stores, extracts and inserts. |
| 430 | // |
| 431 | // This pass only processes entry point functions. It currently only converts |
| 432 | // non-nested, non-ptr access chains. It does not process modules with |
| 433 | // non-32-bit integer types present. Optional memory access options on loads |
| 434 | // and stores are ignored as we are only processing function scope variables. |
| 435 | // |
| 436 | // This pass unifies access to these variables to a single mode and simplifies |
| 437 | // subsequent analysis and elimination of these variables along with their |
| 438 | // loads and stores allowing values to propagate to their points of use where |
| 439 | // possible. |
| 440 | Optimizer::PassToken CreateLocalAccessChainConvertPass(); |
| 441 | |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 442 | // Creates a local single store elimination pass. |
Steven Perron | e43c910 | 2017-09-19 10:12:13 -0400 | [diff] [blame] | 443 | // For each entry point function, this pass eliminates loads and stores for |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 444 | // function scope variable that are stored to only once, where possible. Only |
| 445 | // whole variable loads and stores are eliminated; access-chain references are |
| 446 | // not optimized. Replace all loads of such variables with the value that is |
| 447 | // stored and eliminate any resulting dead code. |
| 448 | // |
| 449 | // Currently, the presence of access chains and function calls can inhibit this |
| 450 | // pass, however the Inlining and LocalAccessChainConvert passes can make it |
| 451 | // more effective. In additional, many non-load/store memory operations are |
| 452 | // not supported and will prohibit optimization of a function. Support of |
| 453 | // these operations are future work. |
| 454 | // |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 455 | // Only shader modules with relaxed logical addressing (see opt/instruction.h) |
| 456 | // are currently processed. |
| 457 | // |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 458 | // This pass will reduce the work needed to be done by LocalSingleBlockElim |
GregF | cc8bad3 | 2017-06-16 15:37:31 -0600 | [diff] [blame] | 459 | // and LocalMultiStoreElim and can improve the effectiveness of other passes |
| 460 | // such as DeadBranchElimination which depend on values for their analysis. |
GregF | 0c5722f | 2017-05-19 17:31:28 -0600 | [diff] [blame] | 461 | Optimizer::PassToken CreateLocalSingleStoreElimPass(); |
| 462 | |
GregF | 6136bf9 | 2017-05-26 10:33:11 -0600 | [diff] [blame] | 463 | // Creates an insert/extract elimination pass. |
| 464 | // This pass processes each entry point function in the module, searching for |
| 465 | // extracts on a sequence of inserts. It further searches the sequence for an |
| 466 | // insert with indices identical to the extract. If such an insert can be |
| 467 | // found before hitting a conflicting insert, the extract's result id is |
| 468 | // replaced with the id of the values from the insert. |
| 469 | // |
| 470 | // Besides removing extracts this pass enables subsequent dead code elimination |
| 471 | // passes to delete the inserts. This pass performs best after access chains are |
| 472 | // converted to inserts and extracts and local loads and stores are eliminated. |
| 473 | Optimizer::PassToken CreateInsertExtractElimPass(); |
| 474 | |
GregF | f28b106 | 2018-01-26 17:05:33 -0700 | [diff] [blame] | 475 | // Creates a dead insert elimination pass. |
| 476 | // This pass processes each entry point function in the module, searching for |
| 477 | // unreferenced inserts into composite types. These are most often unused |
| 478 | // stores to vector components. They are unused because they are never |
| 479 | // referenced, or because there is another insert to the same component between |
| 480 | // the insert and the reference. After removing the inserts, dead code |
| 481 | // elimination is attempted on the inserted values. |
| 482 | // |
| 483 | // This pass performs best after access chains are converted to inserts and |
| 484 | // extracts and local loads and stores are eliminated. While executing this |
| 485 | // pass can be advantageous on its own, it is also advantageous to execute |
| 486 | // this pass after CreateInsertExtractPass() as it will remove any unused |
| 487 | // inserts created by that pass. |
| 488 | Optimizer::PassToken CreateDeadInsertElimPass(); |
| 489 | |
GregF | f4b29f3 | 2017-07-03 17:23:04 -0600 | [diff] [blame] | 490 | // Creates a pass to consolidate uniform references. |
| 491 | // 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] | 492 | // access chain loads into equivalent composite extracts. Then consolidate |
GregF | f4b29f3 | 2017-07-03 17:23:04 -0600 | [diff] [blame] | 493 | // identical uniform loads into one uniform load. Finally, consolidate |
| 494 | // identical uniform extracts into one uniform extract. This may require |
| 495 | // moving a load or extract to a point which dominates all uses. |
| 496 | // |
| 497 | // This pass requires a module to have structured control flow ie shader |
| 498 | // capability. It also requires logical addressing ie Addresses capability |
| 499 | // is not enabled. It also currently does not support any extensions. |
| 500 | // |
| 501 | // This pass currently only optimizes loads with a single index. |
| 502 | Optimizer::PassToken CreateCommonUniformElimPass(); |
| 503 | |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 504 | // Create aggressive dead code elimination pass |
Alan Baker | 3a054e1 | 2017-12-18 12:13:10 -0500 | [diff] [blame] | 505 | // This pass eliminates unused code from the module. In addition, |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 506 | // it detects and eliminates code which may have spurious uses but which do |
| 507 | // not contribute to the output of the function. The most common cause of |
| 508 | // such code sequences is summations in loops whose result is no longer used |
| 509 | // due to dead code elimination. This optimization has additional compile |
| 510 | // time cost over standard dead code elimination. |
| 511 | // |
| 512 | // This pass only processes entry point functions. It also only processes |
Alan Baker | 3a054e1 | 2017-12-18 12:13:10 -0500 | [diff] [blame] | 513 | // shaders with relaxed logical addressing (see opt/instruction.h). It |
| 514 | // currently will not process functions with function calls. Unreachable |
| 515 | // functions are deleted. |
GregF | 9de4e69 | 2017-06-08 10:37:21 -0600 | [diff] [blame] | 516 | // |
| 517 | // This pass will be made more effective by first running passes that remove |
| 518 | // dead control flow and inlines function calls. |
| 519 | // |
| 520 | // This pass can be especially useful after running Local Access Chain |
| 521 | // Conversion, which tends to cause cycles of dead code to be left after |
| 522 | // Store/Load elimination passes are completed. These cycles cannot be |
| 523 | // eliminated with standard dead code elimination. |
| 524 | Optimizer::PassToken CreateAggressiveDCEPass(); |
| 525 | |
greg-lunarg | c37388f | 2018-11-15 12:06:17 -0700 | [diff] [blame] | 526 | // Create line propagation pass |
| 527 | // This pass propagates line information based on the rules for OpLine and |
| 528 | // OpNoline and clones an appropriate line instruction into every instruction |
| 529 | // which does not already have debug line instructions. |
| 530 | // |
| 531 | // This pass is intended to maximize preservation of source line information |
| 532 | // through passes which delete, move and clone instructions. Ideally it should |
| 533 | // be run before any such pass. It is a bookend pass with EliminateDeadLines |
| 534 | // which can be used to remove redundant line instructions at the end of a |
| 535 | // run of such passes and reduce final output file size. |
| 536 | Optimizer::PassToken CreatePropagateLineInfoPass(); |
| 537 | |
| 538 | // Create dead line elimination pass |
| 539 | // This pass eliminates redundant line instructions based on the rules for |
| 540 | // OpLine and OpNoline. Its main purpose is to reduce the size of the file |
| 541 | // need to store the SPIR-V without losing line information. |
| 542 | // |
| 543 | // This is a bookend pass with PropagateLines which attaches line instructions |
| 544 | // to every instruction to preserve line information during passes which |
| 545 | // delete, move and clone instructions. DeadLineElim should be run after |
| 546 | // PropagateLines and all such subsequent passes. Normally it would be one |
| 547 | // of the last passes to be run. |
| 548 | Optimizer::PassToken CreateRedundantLineInfoElimPass(); |
| 549 | |
Andrey Tuganov | 1e309af | 2017-04-11 15:11:04 -0400 | [diff] [blame] | 550 | // Creates a compact ids pass. |
| 551 | // The pass remaps result ids to a compact and gapless range starting from %1. |
| 552 | Optimizer::PassToken CreateCompactIdsPass(); |
| 553 | |
Pierre Moreau | 7183ad5 | 2018-01-03 01:54:55 +0100 | [diff] [blame] | 554 | // Creates a remove duplicate pass. |
| 555 | // This pass removes various duplicates: |
| 556 | // * duplicate capabilities; |
| 557 | // * duplicate extended instruction imports; |
| 558 | // * duplicate types; |
| 559 | // * duplicate decorations. |
Pierre Moreau | 86627f7 | 2017-07-13 02:16:51 +0200 | [diff] [blame] | 560 | Optimizer::PassToken CreateRemoveDuplicatesPass(); |
| 561 | |
Diego Novillo | c75704e | 2017-09-06 08:56:41 -0400 | [diff] [blame] | 562 | // Creates a CFG cleanup pass. |
| 563 | // This pass removes cruft from the control flow graph of functions that are |
| 564 | // reachable from entry points and exported functions. It currently includes the |
| 565 | // following functionality: |
| 566 | // |
| 567 | // - Removal of unreachable basic blocks. |
| 568 | Optimizer::PassToken CreateCFGCleanupPass(); |
| 569 | |
Steven Perron | 5834719 | 2017-10-20 12:17:41 -0400 | [diff] [blame] | 570 | // Create dead variable elimination pass. |
| 571 | // This pass will delete module scope variables, along with their decorations, |
| 572 | // that are not referenced. |
| 573 | Optimizer::PassToken CreateDeadVariableEliminationPass(); |
| 574 | |
Steven Perron | b3daa93 | 2018-03-06 11:20:28 -0500 | [diff] [blame] | 575 | // create merge return pass. |
| 576 | // changes functions that have multiple return statements so they have a single |
| 577 | // return statement. |
Alan Baker | a92d69b | 2017-11-08 16:22:10 -0500 | [diff] [blame] | 578 | // |
Steven Perron | b3daa93 | 2018-03-06 11:20:28 -0500 | [diff] [blame] | 579 | // for structured control flow it is assumed that the only unreachable blocks in |
| 580 | // the function are trivial merge and continue blocks. |
Alan Baker | a92d69b | 2017-11-08 16:22:10 -0500 | [diff] [blame] | 581 | // |
Steven Perron | b3daa93 | 2018-03-06 11:20:28 -0500 | [diff] [blame] | 582 | // a trivial merge block contains the label and an opunreachable instructions, |
| 583 | // nothing else. a trivial continue block contain a label and an opbranch to |
| 584 | // the header, nothing else. |
| 585 | // |
| 586 | // these conditions are guaranteed to be met after running dead-branch |
| 587 | // elimination. |
Alan Baker | a92d69b | 2017-11-08 16:22:10 -0500 | [diff] [blame] | 588 | Optimizer::PassToken CreateMergeReturnPass(); |
| 589 | |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 590 | // Create value numbering pass. |
| 591 | // This pass will look for instructions in the same basic block that compute the |
| 592 | // same value, and remove the redundant ones. |
| 593 | Optimizer::PassToken CreateLocalRedundancyEliminationPass(); |
Steven Perron | 5d602ab | 2017-12-04 12:29:51 -0500 | [diff] [blame] | 594 | |
Alexander Johnston | 84ccd0b | 2018-01-29 10:39:55 +0000 | [diff] [blame] | 595 | // Create LICM pass. |
| 596 | // This pass will look for invariant instructions inside loops and hoist them to |
| 597 | // the loops preheader. |
| 598 | Optimizer::PassToken CreateLoopInvariantCodeMotionPass(); |
| 599 | |
Stephen McGroarty | 9a5dd6f | 2018-04-23 21:01:12 +0100 | [diff] [blame] | 600 | // Creates a loop fission pass. |
| 601 | // This pass will split all top level loops whose register pressure exceedes the |
| 602 | // given |threshold|. |
| 603 | Optimizer::PassToken CreateLoopFissionPass(size_t threshold); |
| 604 | |
Toomas Remmelg | 1dc2458 | 2018-04-20 15:14:45 +0100 | [diff] [blame] | 605 | // Creates a loop fusion pass. |
| 606 | // This pass will look for adjacent loops that are compatible and legal to be |
| 607 | // fused. The fuse all such loops as long as the register usage for the fused |
| 608 | // loop stays under the threshold defined by |max_registers_per_loop|. |
| 609 | Optimizer::PassToken CreateLoopFusionPass(size_t max_registers_per_loop); |
| 610 | |
Victor Lomuller | 10e5d7c | 2018-03-29 12:22:42 +0100 | [diff] [blame] | 611 | // Creates a loop peeling pass. |
| 612 | // This pass will look for conditions inside a loop that are true or false only |
| 613 | // for the N first or last iteration. For loop with such condition, those N |
| 614 | // iterations of the loop will be executed outside of the main loop. |
| 615 | // To limit code size explosion, the loop peeling can only happen if the code |
| 616 | // size growth for each loop is under |code_growth_threshold|. |
| 617 | Optimizer::PassToken CreateLoopPeelingPass(); |
| 618 | |
Victor Lomuller | 3497a94 | 2018-02-12 21:42:15 +0000 | [diff] [blame] | 619 | // Creates a loop unswitch pass. |
| 620 | // This pass will look for loop independent branch conditions and move the |
| 621 | // condition out of the loop and version the loop based on the taken branch. |
| 622 | // Works best after LICM and local multi store elimination pass. |
| 623 | Optimizer::PassToken CreateLoopUnswitchPass(); |
| 624 | |
Steven Perron | 5d602ab | 2017-12-04 12:29:51 -0500 | [diff] [blame] | 625 | // Create global value numbering pass. |
| 626 | // This pass will look for instructions where the same value is computed on all |
| 627 | // paths leading to the instruction. Those instructions are deleted. |
| 628 | Optimizer::PassToken CreateRedundancyEliminationPass(); |
Alan Baker | 867451f | 2017-11-30 17:03:06 -0500 | [diff] [blame] | 629 | |
| 630 | // Create scalar replacement pass. |
| 631 | // This pass replaces composite function scope variables with variables for each |
Steven Perron | a579e72 | 2018-04-16 09:58:00 -0400 | [diff] [blame] | 632 | // element if those elements are accessed individually. The parameter is a |
| 633 | // limit on the number of members in the composite variable that the pass will |
| 634 | // consider replacing. |
| 635 | Optimizer::PassToken CreateScalarReplacementPass(uint32_t size_limit = 100); |
Steven Perron | b86eb68 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 636 | |
| 637 | // Create a private to local pass. |
| 638 | // This pass looks for variables delcared in the private storage class that are |
| 639 | // used in only one function. Those variables are moved to the function storage |
| 640 | // class in the function that they are used. |
| 641 | Optimizer::PassToken CreatePrivateToLocalPass(); |
Diego Novillo | 4ba9dcc | 2017-12-05 11:39:25 -0500 | [diff] [blame] | 642 | |
| 643 | // Creates a conditional constant propagation (CCP) pass. |
| 644 | // This pass implements the SSA-CCP algorithm in |
| 645 | // |
| 646 | // Constant propagation with conditional branches, |
| 647 | // Wegman and Zadeck, ACM TOPLAS 13(2):181-210. |
| 648 | // |
| 649 | // Constant values in expressions and conditional jumps are folded and |
| 650 | // simplified. This may reduce code size by removing never executed jump targets |
| 651 | // and computations with constant operands. |
| 652 | Optimizer::PassToken CreateCCPPass(); |
| 653 | |
Steven Perron | 34d4294 | 2018-01-17 14:57:37 -0500 | [diff] [blame] | 654 | // Creates a workaround driver bugs pass. This pass attempts to work around |
| 655 | // a known driver bug (issue #1209) by identifying the bad code sequences and |
| 656 | // rewriting them. |
| 657 | // |
| 658 | // Current workaround: Avoid OpUnreachable instructions in loops. |
| 659 | Optimizer::PassToken CreateWorkaround1209Pass(); |
| 660 | |
Alan Baker | 2e93e80 | 2018-01-16 11:15:06 -0500 | [diff] [blame] | 661 | // Creates a pass that converts if-then-else like assignments into OpSelect. |
| 662 | Optimizer::PassToken CreateIfConversionPass(); |
| 663 | |
Steven Perron | 61d8c03 | 2018-01-30 11:24:03 -0500 | [diff] [blame] | 664 | // Creates a pass that will replace instructions that are not valid for the |
| 665 | // current shader stage by constants. Has no effect on non-shader modules. |
| 666 | Optimizer::PassToken CreateReplaceInvalidOpcodePass(); |
| 667 | |
Steven Perron | 06cdb96 | 2018-02-02 11:55:05 -0500 | [diff] [blame] | 668 | // Creates a pass that simplifies instructions using the instruction folder. |
| 669 | Optimizer::PassToken CreateSimplificationPass(); |
| 670 | |
Stephen McGroarty | dd8400e | 2018-02-14 17:03:12 +0000 | [diff] [blame] | 671 | // Create loop unroller pass. |
Stephen McGroarty | e354984 | 2018-02-27 11:50:08 +0000 | [diff] [blame] | 672 | // Creates a pass to unroll loops which have the "Unroll" loop control |
Stephen McGroarty | dd8400e | 2018-02-14 17:03:12 +0000 | [diff] [blame] | 673 | // mask set. The loops must meet a specific criteria in order to be unrolled |
| 674 | // safely this criteria is checked before doing the unroll by the |
| 675 | // LoopUtils::CanPerformUnroll method. Any loop that does not meet the criteria |
| 676 | // won't be unrolled. See CanPerformUnroll LoopUtils.h for more information. |
Stephen McGroarty | e354984 | 2018-02-27 11:50:08 +0000 | [diff] [blame] | 677 | Optimizer::PassToken CreateLoopUnrollPass(bool fully_unroll, int factor = 0); |
Stephen McGroarty | dd8400e | 2018-02-14 17:03:12 +0000 | [diff] [blame] | 678 | |
Diego Novillo | 735d8a5 | 2018-02-22 16:18:29 -0500 | [diff] [blame] | 679 | // Create the SSA rewrite pass. |
| 680 | // This pass converts load/store operations on function local variables into |
| 681 | // operations on SSA IDs. This allows SSA optimizers to act on these variables. |
| 682 | // Only variables that are local to the function and of supported types are |
| 683 | // processed (see IsSSATargetVar for details). |
| 684 | Optimizer::PassToken CreateSSARewritePass(); |
| 685 | |
Steven Perron | c4dc046 | 2018-03-20 23:33:24 -0400 | [diff] [blame] | 686 | // Create copy propagate arrays pass. |
| 687 | // This pass looks to copy propagate memory references for arrays. It looks |
| 688 | // for specific code patterns to recognize array copies. |
| 689 | Optimizer::PassToken CreateCopyPropagateArraysPass(); |
Steven Perron | 2c0ce87 | 2018-04-23 11:13:07 -0400 | [diff] [blame] | 690 | |
| 691 | // Create a vector dce pass. |
| 692 | // This pass looks for components of vectors that are unused, and removes them |
| 693 | // from the vector. Note this would still leave around lots of dead code that |
| 694 | // a pass of ADCE will be able to remove. |
| 695 | Optimizer::PassToken CreateVectorDCEPass(); |
| 696 | |
Steven Perron | af430ec | 2018-05-07 12:31:03 -0400 | [diff] [blame] | 697 | // Create a pass to reduce the size of loads. |
| 698 | // This pass looks for loads of structures where only a few of its members are |
| 699 | // used. It replaces the loads feeding an OpExtract with an OpAccessChain and |
| 700 | // a load of the specific elements. |
| 701 | Optimizer::PassToken CreateReduceLoadSizePass(); |
| 702 | |
Alan Baker | 755e5c9 | 2018-07-23 11:23:11 -0400 | [diff] [blame] | 703 | // Create a pass to combine chained access chains. |
| 704 | // This pass looks for access chains fed by other access chains and combines |
| 705 | // them into a single instruction where possible. |
| 706 | Optimizer::PassToken CreateCombineAccessChainsPass(); |
| 707 | |
greg-lunarg | 1e9fc1a | 2018-11-08 11:54:54 -0700 | [diff] [blame] | 708 | // Create a pass to instrument bindless descriptor checking |
| 709 | // This pass instruments all bindless references to check that descriptor |
greg-lunarg | e1a7626 | 2019-03-19 06:53:43 -0700 | [diff] [blame] | 710 | // array indices are inbounds, and if the descriptor indexing extension is |
| 711 | // enabled, that the descriptor has been initialized. If the reference is |
| 712 | // invalid, a record is written to the debug output buffer (if space allows) |
| 713 | // and a null value is returned. This pass is designed to support bindless |
| 714 | // validation in the Vulkan validation layers. |
| 715 | // |
| 716 | // TODO(greg-lunarg): Add support for buffer references. Currently only does |
| 717 | // checking for image references. |
greg-lunarg | 1e9fc1a | 2018-11-08 11:54:54 -0700 | [diff] [blame] | 718 | // |
| 719 | // Dead code elimination should be run after this pass as the original, |
| 720 | // potentially invalid code is not removed and could cause undefined behavior, |
| 721 | // including crashes. It may also be beneficial to run Simplification |
| 722 | // (ie Constant Propagation), DeadBranchElim and BlockMerge after this pass to |
| 723 | // optimize instrument code involving the testing of compile-time constants. |
| 724 | // It is also generally recommended that this pass (and all |
| 725 | // instrumentation passes) be run after any legalization and optimization |
| 726 | // passes. This will give better analysis for the instrumentation and avoid |
| 727 | // potentially de-optimizing the instrument code, for example, inlining |
| 728 | // the debug record output function throughout the module. |
| 729 | // |
| 730 | // The instrumentation will read and write buffers in debug |
| 731 | // descriptor set |desc_set|. It will write |shader_id| in each output record |
| 732 | // to identify the shader module which generated the record. |
greg-lunarg | e1a7626 | 2019-03-19 06:53:43 -0700 | [diff] [blame] | 733 | // |input_length_enable| controls instrumentation of runtime descriptor array |
| 734 | // references, and |input_init_enable| controls instrumentation of descriptor |
| 735 | // initialization checking, both of which require input buffer support. |
greg-lunarg | cf21146 | 2019-02-07 12:00:36 -0700 | [diff] [blame] | 736 | Optimizer::PassToken CreateInstBindlessCheckPass( |
greg-lunarg | e1a7626 | 2019-03-19 06:53:43 -0700 | [diff] [blame] | 737 | uint32_t desc_set, uint32_t shader_id, bool input_length_enable = false, |
| 738 | bool input_init_enable = false); |
greg-lunarg | 1e9fc1a | 2018-11-08 11:54:54 -0700 | [diff] [blame] | 739 | |
alan-baker | e510b1b | 2018-11-30 14:15:51 -0500 | [diff] [blame] | 740 | // Create a pass to upgrade to the VulkanKHR memory model. |
| 741 | // This pass upgrades the Logical GLSL450 memory model to Logical VulkanKHR. |
| 742 | // Additionally, it modifies memory, image, atomic and barrier operations to |
| 743 | // conform to that model's requirements. |
| 744 | Optimizer::PassToken CreateUpgradeMemoryModelPass(); |
| 745 | |
Steven Perron | dd4157d | 2019-01-17 15:56:36 -0500 | [diff] [blame] | 746 | // Create a pass to do code sinking. Code sinking is a transformation |
| 747 | // where an instruction is moved into a more deeply nested construct. |
| 748 | Optimizer::PassToken CreateCodeSinkingPass(); |
| 749 | |
Ryan Harrison | 01964e3 | 2019-04-03 11:44:09 -0400 | [diff] [blame] | 750 | // Create a pass to adds initializers for OpVariable calls that require them |
| 751 | // in WebGPU. Currently this pass naively initializes variables that are |
| 752 | // missing an initializer with a null value. In the future it may initialize |
| 753 | // variables to the first value stored in them, if that is a constant. |
| 754 | Optimizer::PassToken CreateGenerateWebGPUInitializersPass(); |
| 755 | |
Lei Zhang | f18e1f2 | 2016-09-12 14:11:46 -0400 | [diff] [blame] | 756 | } // namespace spvtools |
| 757 | |
dan sinclair | 58a6876 | 2018-08-03 08:05:33 -0400 | [diff] [blame] | 758 | #endif // INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_ |