blob: 700f59f70fdd7077694e45750db67e88bdce77be [file] [log] [blame]
Lei Zhangf18e1f22016-09-12 14:11:46 -04001// 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 sinclair58a68762018-08-03 08:05:33 -040015#ifndef INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_
16#define INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_
Lei Zhangf18e1f22016-09-12 14:11:46 -040017
18#include <memory>
David Netoc32e79e2018-01-04 12:59:50 -050019#include <ostream>
Lei Zhangf18e1f22016-09-12 14:11:46 -040020#include <string>
21#include <unordered_map>
22#include <vector>
23
24#include "libspirv.hpp"
Lei Zhangf18e1f22016-09-12 14:11:46 -040025
26namespace spvtools {
27
Arseny Kapoulkinef765d162018-05-22 14:31:26 -070028namespace opt {
29class Pass;
30}
31
Lei Zhangf18e1f22016-09-12 14:11:46 -040032// 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.
37class 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 Kapoulkinef765d162018-05-22 14:31:26 -070048 // 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 Zhangf18e1f22016-09-12 14:11:46 -040054 // 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 //
David Neto5d786f62020-01-24 16:26:07 -050068 // The instance will have an empty message consumer, which ignores all
69 // messages from the library. Use SetMessageConsumer() to supply a consumer
70 // if messages are of concern.
Lei Zhangf18e1f22016-09-12 14:11:46 -040071 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 Novillo99fe61e2018-07-25 15:21:44 -040086 // Returns a reference to the registered message consumer.
87 const MessageConsumer& consumer() const;
88
Lei Zhangf18e1f22016-09-12 14:11:46 -040089 // 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 Novilloc90d7302017-08-30 14:19:22 -040094 // 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
Lei Zhangaec60b82017-11-17 16:50:43 -0500104 // Registers passes that attempt to legalize the generated code.
105 //
Diego Novillo99fe61e2018-07-25 15:21:44 -0400106 // Note: this recipe is specially designed for legalizing SPIR-V. It should be
107 // used by compilers after translating HLSL source code literally. It should
Lei Zhangaec60b82017-11-17 16:50:43 -0500108 // *not* be used by general workloads for performance or size improvement.
109 //
110 // This sequence of passes is subject to constant review and will change
111 // from time to time.
112 Optimizer& RegisterLegalizationPasses();
113
Diego Novillo99fe61e2018-07-25 15:21:44 -0400114 // Register passes specified in the list of |flags|. Each flag must be a
115 // string of a form accepted by Optimizer::FlagHasValidForm().
116 //
117 // If the list of flags contains an invalid entry, it returns false and an
118 // error message is emitted to the MessageConsumer object (use
119 // Optimizer::SetMessageConsumer to define a message consumer, if needed).
120 //
121 // If all the passes are registered successfully, it returns true.
122 bool RegisterPassesFromFlags(const std::vector<std::string>& flags);
123
124 // Registers the optimization pass associated with |flag|. This only accepts
125 // |flag| values of the form "--pass_name[=pass_args]". If no such pass
126 // exists, it returns false. Otherwise, the pass is registered and it returns
127 // true.
128 //
129 // The following flags have special meaning:
130 //
131 // -O: Registers all performance optimization passes
132 // (Optimizer::RegisterPerformancePasses)
133 //
134 // -Os: Registers all size optimization passes
135 // (Optimizer::RegisterSizePasses).
136 //
137 // --legalize-hlsl: Registers all passes that legalize SPIR-V generated by an
138 // HLSL front-end.
139 bool RegisterPassFromFlag(const std::string& flag);
140
141 // Validates that |flag| has a valid format. Strings accepted:
142 //
143 // --pass_name[=pass_args]
144 // -O
145 // -Os
146 //
147 // If |flag| takes one of the forms above, it returns true. Otherwise, it
148 // returns false.
149 bool FlagHasValidForm(const std::string& flag) const;
150
Ryan Harrisone0292c22018-12-17 16:54:23 -0500151 // Allows changing, after creation time, the target environment to be
David Neto5d786f62020-01-24 16:26:07 -0500152 // optimized for and validated. Should be called before calling Run().
Ryan Harrisone0292c22018-12-17 16:54:23 -0500153 void SetTargetEnv(const spv_target_env env);
154
Lei Zhangf18e1f22016-09-12 14:11:46 -0400155 // Optimizes the given SPIR-V module |original_binary| and writes the
David Neto5d786f62020-01-24 16:26:07 -0500156 // optimized binary into |optimized_binary|. The optimized binary uses
157 // the same SPIR-V version as the original binary.
158 //
Lei Zhangf18e1f22016-09-12 14:11:46 -0400159 // Returns true on successful optimization, whether or not the module is
Steven Perronbcb0b692018-08-13 13:18:46 -0400160 // modified. Returns false if |original_binary| fails to validate or if errors
161 // occur when processing |original_binary| using any of the registered passes.
162 // In that case, no further passes are executed and the contents in
163 // |optimized_binary| may be invalid.
Lei Zhangf18e1f22016-09-12 14:11:46 -0400164 //
David Neto5d786f62020-01-24 16:26:07 -0500165 // By default, the binary is validated before any transforms are performed,
166 // and optionally after each transform. Validation uses SPIR-V spec rules
167 // for the SPIR-V version named in the binary's header (at word offset 1).
168 // Additionally, if the target environment is a client API (such as
169 // Vulkan 1.1), then validate for that client API version, to the extent
170 // that it is verifiable from data in the binary itself.
171 //
Lei Zhangf18e1f22016-09-12 14:11:46 -0400172 // It's allowed to alias |original_binary| to the start of |optimized_binary|.
173 bool Run(const uint32_t* original_binary, size_t original_binary_size,
174 std::vector<uint32_t>* optimized_binary) const;
Steven Perronbcb0b692018-08-13 13:18:46 -0400175
Steven Perron75c1bf22018-09-10 11:49:41 -0400176 // DEPRECATED: Same as above, except passes |options| to the validator when
177 // trying to validate the binary. If |skip_validation| is true, then the
178 // caller is guaranteeing that |original_binary| is valid, and the validator
179 // will not be run. The |max_id_bound| is the limit on the max id in the
180 // module.
Steven Perronbcb0b692018-08-13 13:18:46 -0400181 bool Run(const uint32_t* original_binary, const size_t original_binary_size,
Steven Perron5c8b4f52018-08-08 11:16:19 -0400182 std::vector<uint32_t>* optimized_binary,
Steven Perron75c1bf22018-09-10 11:49:41 -0400183 const ValidatorOptions& options, bool skip_validation) const;
184
185 // Same as above, except it takes an options object. See the documentation
186 // for |OptimizerOptions| to see which options can be set.
David Neto5d786f62020-01-24 16:26:07 -0500187 //
188 // By default, the binary is validated before any transforms are performed,
189 // and optionally after each transform. Validation uses SPIR-V spec rules
190 // for the SPIR-V version named in the binary's header (at word offset 1).
191 // Additionally, if the target environment is a client API (such as
192 // Vulkan 1.1), then validate for that client API version, to the extent
193 // that it is verifiable from data in the binary itself, or from the
194 // validator options set on the optimizer options.
Steven Perron75c1bf22018-09-10 11:49:41 -0400195 bool Run(const uint32_t* original_binary, const size_t original_binary_size,
196 std::vector<uint32_t>* optimized_binary,
197 const spv_optimizer_options opt_options) const;
Lei Zhangf18e1f22016-09-12 14:11:46 -0400198
Diego Novilloc90d7302017-08-30 14:19:22 -0400199 // Returns a vector of strings with all the pass names added to this
200 // optimizer's pass manager. These strings are valid until the associated
201 // pass manager is destroyed.
Steven Perron58347192017-10-20 12:17:41 -0400202 std::vector<const char*> GetPassNames() const;
Diego Novilloc90d7302017-08-30 14:19:22 -0400203
David Netoc32e79e2018-01-04 12:59:50 -0500204 // Sets the option to print the disassembly before each pass and after the
205 // last pass. If |out| is null, then no output is generated. Otherwise,
206 // output is sent to the |out| output stream.
207 Optimizer& SetPrintAll(std::ostream* out);
208
Jaebaek Seo3b594e12018-03-07 09:25:51 -0500209 // Sets the option to print the resource utilization of each pass. If |out|
210 // is null, then no output is generated. Otherwise, output is sent to the
211 // |out| output stream.
212 Optimizer& SetTimeReport(std::ostream* out);
213
alan-baker42e6f1a2019-03-26 14:38:59 -0400214 // Sets the option to validate the module after each pass.
215 Optimizer& SetValidateAfterAll(bool validate);
216
Lei Zhangf18e1f22016-09-12 14:11:46 -0400217 private:
218 struct Impl; // Opaque struct for holding internal data.
219 std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
220};
221
222// Creates a null pass.
223// A null pass does nothing to the SPIR-V module to be optimized.
224Optimizer::PassToken CreateNullPass();
225
226// Creates a strip-debug-info pass.
227// A strip-debug-info pass removes all debug instructions (as documented in
228// Section 3.32.2 of the SPIR-V spec) of the SPIR-V module to be optimized.
229Optimizer::PassToken CreateStripDebugInfoPass();
230
David Neto844e1862018-03-09 16:08:57 -0500231// Creates a strip-reflect-info pass.
232// A strip-reflect-info pass removes all reflections instructions.
233// For now, this is limited to removing decorations defined in
234// SPV_GOOGLE_hlsl_functionality1. The coverage may expand in
235// the future.
236Optimizer::PassToken CreateStripReflectInfoPass();
237
Steven Perrone43c9102017-09-19 10:12:13 -0400238// Creates an eliminate-dead-functions pass.
Steven Perron58347192017-10-20 12:17:41 -0400239// An eliminate-dead-functions pass will remove all functions that are not in
240// the call trees rooted at entry points and exported functions. These
241// functions are not needed because they will never be called.
Steven Perrone43c9102017-09-19 10:12:13 -0400242Optimizer::PassToken CreateEliminateDeadFunctionsPass();
243
Steven Perron1b0047f2019-02-14 13:42:35 -0500244// Creates an eliminate-dead-members pass.
245// An eliminate-dead-members pass will remove all unused members of structures.
246// This will not affect the data layout of the remaining members.
247Optimizer::PassToken CreateEliminateDeadMembersPass();
248
qining144f59e2017-04-19 18:10:59 -0400249// Creates a set-spec-constant-default-value pass from a mapping from spec-ids
250// to the default values in the form of string.
Lei Zhangf18e1f22016-09-12 14:11:46 -0400251// A set-spec-constant-default-value pass sets the default values for the
252// spec constants that have SpecId decorations (i.e., those defined by
253// OpSpecConstant{|True|False} instructions).
254Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
255 const std::unordered_map<uint32_t, std::string>& id_value_map);
256
qining144f59e2017-04-19 18:10:59 -0400257// Creates a set-spec-constant-default-value pass from a mapping from spec-ids
258// to the default values in the form of bit pattern.
259// A set-spec-constant-default-value pass sets the default values for the
260// spec constants that have SpecId decorations (i.e., those defined by
261// OpSpecConstant{|True|False} instructions).
262Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
263 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map);
264
David Neto11a867f2017-04-01 16:10:16 -0400265// Creates a flatten-decoration pass.
266// A flatten-decoration pass replaces grouped decorations with equivalent
267// ungrouped decorations. That is, it replaces each OpDecorationGroup
268// instruction and associated OpGroupDecorate and OpGroupMemberDecorate
269// instructions with equivalent OpDecorate and OpMemberDecorate instructions.
270// The pass does not attempt to preserve debug information for instructions
271// it removes.
272Optimizer::PassToken CreateFlattenDecorationPass();
273
Lei Zhangf18e1f22016-09-12 14:11:46 -0400274// Creates a freeze-spec-constant-value pass.
275// A freeze-spec-constant pass specializes the value of spec constants to
276// their default values. This pass only processes the spec constants that have
277// SpecId decorations (defined by OpSpecConstant, OpSpecConstantTrue, or
278// OpSpecConstantFalse instructions) and replaces them with their normal
279// counterparts (OpConstant, OpConstantTrue, or OpConstantFalse). The
280// corresponding SpecId annotation instructions will also be removed. This
281// pass does not fold the newly added normal constants and does not process
282// other spec constants defined by OpSpecConstantComposite or
283// OpSpecConstantOp.
284Optimizer::PassToken CreateFreezeSpecConstantValuePass();
285
286// Creates a fold-spec-constant-op-and-composite pass.
287// A fold-spec-constant-op-and-composite pass folds spec constants defined by
288// OpSpecConstantOp or OpSpecConstantComposite instruction, to normal Constants
289// defined by OpConstantTrue, OpConstantFalse, OpConstant, OpConstantNull, or
290// OpConstantComposite instructions. Note that spec constants defined with
291// OpSpecConstant, OpSpecConstantTrue, or OpSpecConstantFalse instructions are
292// not handled, as these instructions indicate their value are not determined
293// and can be changed in future. A spec constant is foldable if all of its
294// value(s) can be determined from the module. E.g., an integer spec constant
295// defined with OpSpecConstantOp instruction can be folded if its value won't
296// change later. This pass will replace the original OpSpecContantOp instruction
297// with an OpConstant instruction. When folding composite spec constants,
298// new instructions may be inserted to define the components of the composite
299// constant first, then the original spec constants will be replaced by
300// OpConstantComposite instructions.
301//
302// There are some operations not supported yet:
303// OpSConvert, OpFConvert, OpQuantizeToF16 and
304// all the operations under Kernel capability.
305// TODO(qining): Add support for the operations listed above.
306Optimizer::PassToken CreateFoldSpecConstantOpAndCompositePass();
307
308// Creates a unify-constant pass.
309// A unify-constant pass de-duplicates the constants. Constants with the exact
310// same value and identical form will be unified and only one constant will
311// be kept for each unique pair of type and value.
312// There are several cases not handled by this pass:
313// 1) Constants defined by OpConstantNull instructions (null constants) and
314// constants defined by OpConstantFalse, OpConstant or OpConstantComposite
315// with value 0 (zero-valued normal constants) are not considered equivalent.
316// So null constants won't be used to replace zero-valued normal constants,
317// vice versa.
318// 2) Whenever there are decorations to the constant's result id id, the
319// constant won't be handled, which means, it won't be used to replace any
320// other constants, neither can other constants replace it.
321// 3) NaN in float point format with different bit patterns are not unified.
322Optimizer::PassToken CreateUnifyConstantPass();
323
324// Creates a eliminate-dead-constant pass.
325// A eliminate-dead-constant pass removes dead constants, including normal
326// contants defined by OpConstant, OpConstantComposite, OpConstantTrue, or
327// OpConstantFalse and spec constants defined by OpSpecConstant,
328// OpSpecConstantComposite, OpSpecConstantTrue, OpSpecConstantFalse or
329// OpSpecConstantOp.
330Optimizer::PassToken CreateEliminateDeadConstantPass();
331
Steven Perrone4c7d8e2017-09-08 12:08:03 -0400332// Creates a strength-reduction pass.
333// A strength-reduction pass will look for opportunities to replace an
334// instruction with an equivalent and less expensive one. For example,
335// multiplying by a power of 2 can be replaced by a bit shift.
336Optimizer::PassToken CreateStrengthReductionPass();
337
GregFad1d0352017-06-07 15:28:53 -0600338// Creates a block merge pass.
339// This pass searches for blocks with a single Branch to a block with no
340// other predecessors and merges the blocks into a single block. Continue
341// blocks and Merge blocks are not candidates for the second block.
342//
343// The pass is most useful after Dead Branch Elimination, which can leave
344// such sequences of blocks. Merging them makes subsequent passes more
345// effective, such as single block local store-load elimination.
346//
347// While this pass reduces the number of occurrences of this sequence, at
348// this time it does not guarantee all such sequences are eliminated.
349//
350// Presence of phi instructions can inhibit this optimization. Handling
Steven Perrone43c9102017-09-19 10:12:13 -0400351// these is left for future improvements.
GregFad1d0352017-06-07 15:28:53 -0600352Optimizer::PassToken CreateBlockMergePass();
353
GregF429ca052017-08-15 17:58:28 -0600354// Creates an exhaustive inline pass.
355// An exhaustive inline pass attempts to exhaustively inline all function
356// calls in all functions in an entry point call tree. The intent is to enable,
357// albeit through brute force, analysis and optimization across function
358// calls by subsequent optimization passes. As the inlining is exhaustive,
359// there is no attempt to optimize for size or runtime performance. Functions
360// that are not in the call tree of an entry point are not changed.
GregFe28bd392017-08-01 17:20:13 -0600361Optimizer::PassToken CreateInlineExhaustivePass();
Steven Perrone43c9102017-09-19 10:12:13 -0400362
GregF429ca052017-08-15 17:58:28 -0600363// Creates an opaque inline pass.
364// An opaque inline pass inlines all function calls in all functions in all
365// entry point call trees where the called function contains an opaque type
366// in either its parameter types or return type. An opaque type is currently
367// defined as Image, Sampler or SampledImage. The intent is to enable, albeit
368// through brute force, analysis and optimization across these function calls
369// by subsequent passes in order to remove the storing of opaque types which is
370// not legal in Vulkan. Functions that are not in the call tree of an entry
371// point are not changed.
372Optimizer::PassToken CreateInlineOpaquePass();
Steven Perrone43c9102017-09-19 10:12:13 -0400373
GregF7c8da662017-05-18 14:51:55 -0600374// Creates a single-block local variable load/store elimination pass.
Steven Perrone43c9102017-09-19 10:12:13 -0400375// For every entry point function, do single block memory optimization of
GregF7c8da662017-05-18 14:51:55 -0600376// function variables referenced only with non-access-chain loads and stores.
377// For each targeted variable load, if previous store to that variable in the
378// block, replace the load's result id with the value id of the store.
379// If previous load within the block, replace the current load's result id
380// with the previous load's result id. In either case, delete the current
381// load. Finally, check if any remaining stores are useless, and delete store
382// and variable if possible.
383//
384// The presence of access chain references and function calls can inhibit
385// the above optimization.
386//
Steven Perron79a00642017-12-11 13:10:24 -0500387// Only modules with relaxed logical addressing (see opt/instruction.h) are
388// currently processed.
GregF7c8da662017-05-18 14:51:55 -0600389//
Steven Perrone43c9102017-09-19 10:12:13 -0400390// This pass is most effective if preceeded by Inlining and
GregF7c8da662017-05-18 14:51:55 -0600391// LocalAccessChainConvert. This pass will reduce the work needed to be done
GregFcc8bad32017-06-16 15:37:31 -0600392// by LocalSingleStoreElim and LocalMultiStoreElim.
GregF429ca052017-08-15 17:58:28 -0600393//
394// Only functions in the call tree of an entry point are processed.
GregF7c8da662017-05-18 14:51:55 -0600395Optimizer::PassToken CreateLocalSingleBlockLoadStoreElimPass();
Greg Fischer04fcc662016-11-10 10:11:50 -0700396
GregF52e247f2017-06-02 13:23:20 -0600397// Create dead branch elimination pass.
398// For each entry point function, this pass will look for SelectionMerge
399// BranchConditionals with constant condition and convert to a Branch to
400// the indicated label. It will delete resulting dead blocks.
401//
Andrey Tuganov4b1577a2017-10-05 16:26:09 -0400402// For all phi functions in merge block, replace all uses with the id
403// corresponding to the living predecessor.
404//
Alan Baker1b6cfd32018-01-04 17:04:03 -0500405// Note that some branches and blocks may be left to avoid creating invalid
406// control flow. Improving this is left to future work.
GregF52e247f2017-06-02 13:23:20 -0600407//
408// This pass is most effective when preceeded by passes which eliminate
409// local loads and stores, effectively propagating constant values where
410// possible.
411Optimizer::PassToken CreateDeadBranchElimPass();
412
GregFcc8bad32017-06-16 15:37:31 -0600413// Creates an SSA local variable load/store elimination pass.
414// For every entry point function, eliminate all loads and stores of function
415// scope variables only referenced with non-access-chain loads and stores.
Steven Perrone43c9102017-09-19 10:12:13 -0400416// Eliminate the variables as well.
GregFcc8bad32017-06-16 15:37:31 -0600417//
418// The presence of access chain references and function calls can inhibit
419// the above optimization.
420//
Steven Perron79a00642017-12-11 13:10:24 -0500421// Only shader modules with relaxed logical addressing (see opt/instruction.h)
422// are currently processed. Currently modules with any extensions enabled are
423// not processed. This is left for future work.
GregFcc8bad32017-06-16 15:37:31 -0600424//
Steven Perrone43c9102017-09-19 10:12:13 -0400425// This pass is most effective if preceeded by Inlining and
GregFcc8bad32017-06-16 15:37:31 -0600426// LocalAccessChainConvert. LocalSingleStoreElim and LocalSingleBlockElim
427// will reduce the work that this pass has to do.
428Optimizer::PassToken CreateLocalMultiStoreElimPass();
429
GregFaa7e6872017-05-12 17:27:21 -0600430// Creates a local access chain conversion pass.
431// A local access chain conversion pass identifies all function scope
432// variables which are accessed only with loads, stores and access chains
433// with constant indices. It then converts all loads and stores of such
434// variables into equivalent sequences of loads, stores, extracts and inserts.
435//
436// This pass only processes entry point functions. It currently only converts
437// non-nested, non-ptr access chains. It does not process modules with
438// non-32-bit integer types present. Optional memory access options on loads
439// and stores are ignored as we are only processing function scope variables.
440//
441// This pass unifies access to these variables to a single mode and simplifies
442// subsequent analysis and elimination of these variables along with their
443// loads and stores allowing values to propagate to their points of use where
444// possible.
445Optimizer::PassToken CreateLocalAccessChainConvertPass();
446
GregF0c5722f2017-05-19 17:31:28 -0600447// Creates a local single store elimination pass.
Steven Perrone43c9102017-09-19 10:12:13 -0400448// For each entry point function, this pass eliminates loads and stores for
GregF0c5722f2017-05-19 17:31:28 -0600449// function scope variable that are stored to only once, where possible. Only
450// whole variable loads and stores are eliminated; access-chain references are
451// not optimized. Replace all loads of such variables with the value that is
452// stored and eliminate any resulting dead code.
453//
454// Currently, the presence of access chains and function calls can inhibit this
455// pass, however the Inlining and LocalAccessChainConvert passes can make it
456// more effective. In additional, many non-load/store memory operations are
457// not supported and will prohibit optimization of a function. Support of
458// these operations are future work.
459//
Steven Perron79a00642017-12-11 13:10:24 -0500460// Only shader modules with relaxed logical addressing (see opt/instruction.h)
461// are currently processed.
462//
GregF0c5722f2017-05-19 17:31:28 -0600463// This pass will reduce the work needed to be done by LocalSingleBlockElim
GregFcc8bad32017-06-16 15:37:31 -0600464// and LocalMultiStoreElim and can improve the effectiveness of other passes
465// such as DeadBranchElimination which depend on values for their analysis.
GregF0c5722f2017-05-19 17:31:28 -0600466Optimizer::PassToken CreateLocalSingleStoreElimPass();
467
GregF6136bf92017-05-26 10:33:11 -0600468// Creates an insert/extract elimination pass.
469// This pass processes each entry point function in the module, searching for
470// extracts on a sequence of inserts. It further searches the sequence for an
471// insert with indices identical to the extract. If such an insert can be
472// found before hitting a conflicting insert, the extract's result id is
473// replaced with the id of the values from the insert.
474//
475// Besides removing extracts this pass enables subsequent dead code elimination
476// passes to delete the inserts. This pass performs best after access chains are
477// converted to inserts and extracts and local loads and stores are eliminated.
478Optimizer::PassToken CreateInsertExtractElimPass();
479
GregFf28b1062018-01-26 17:05:33 -0700480// Creates a dead insert elimination pass.
481// This pass processes each entry point function in the module, searching for
482// unreferenced inserts into composite types. These are most often unused
483// stores to vector components. They are unused because they are never
484// referenced, or because there is another insert to the same component between
485// the insert and the reference. After removing the inserts, dead code
486// elimination is attempted on the inserted values.
487//
488// This pass performs best after access chains are converted to inserts and
489// extracts and local loads and stores are eliminated. While executing this
490// pass can be advantageous on its own, it is also advantageous to execute
491// this pass after CreateInsertExtractPass() as it will remove any unused
492// inserts created by that pass.
493Optimizer::PassToken CreateDeadInsertElimPass();
494
GregF9de4e692017-06-08 10:37:21 -0600495// Create aggressive dead code elimination pass
Alan Baker3a054e12017-12-18 12:13:10 -0500496// This pass eliminates unused code from the module. In addition,
GregF9de4e692017-06-08 10:37:21 -0600497// it detects and eliminates code which may have spurious uses but which do
498// not contribute to the output of the function. The most common cause of
499// such code sequences is summations in loops whose result is no longer used
500// due to dead code elimination. This optimization has additional compile
501// time cost over standard dead code elimination.
502//
503// This pass only processes entry point functions. It also only processes
Alan Baker3a054e12017-12-18 12:13:10 -0500504// shaders with relaxed logical addressing (see opt/instruction.h). It
505// currently will not process functions with function calls. Unreachable
506// functions are deleted.
GregF9de4e692017-06-08 10:37:21 -0600507//
508// This pass will be made more effective by first running passes that remove
509// dead control flow and inlines function calls.
510//
511// This pass can be especially useful after running Local Access Chain
512// Conversion, which tends to cause cycles of dead code to be left after
513// Store/Load elimination passes are completed. These cycles cannot be
514// eliminated with standard dead code elimination.
515Optimizer::PassToken CreateAggressiveDCEPass();
516
ZHOU Hef9893c42021-06-29 23:33:58 +0800517// Creates a remove-unused-interface-variables pass.
518// Removes variables referenced on the |OpEntryPoint| instruction that are not
519// referenced in the entry point function or any function in its call tree. Note
520// that this could cause the shader interface to no longer match other shader
521// stages.
522Optimizer::PassToken CreateRemoveUnusedInterfaceVariablesPass();
523
Jaebaek Seof7da5272020-10-30 18:03:56 -0400524// Creates an empty pass.
525// This is deprecated and will be removed.
526// TODO(jaebaek): remove this pass after handling glslang's broken unit tests.
527// https://github.com/KhronosGroup/glslang/pull/2440
528Optimizer::PassToken CreatePropagateLineInfoPass();
529
530// Creates an empty pass.
531// This is deprecated and will be removed.
532// TODO(jaebaek): remove this pass after handling glslang's broken unit tests.
533// https://github.com/KhronosGroup/glslang/pull/2440
534Optimizer::PassToken CreateRedundantLineInfoElimPass();
535
Andrey Tuganov1e309af2017-04-11 15:11:04 -0400536// Creates a compact ids pass.
537// The pass remaps result ids to a compact and gapless range starting from %1.
538Optimizer::PassToken CreateCompactIdsPass();
539
Pierre Moreau7183ad52018-01-03 01:54:55 +0100540// Creates a remove duplicate pass.
541// This pass removes various duplicates:
542// * duplicate capabilities;
543// * duplicate extended instruction imports;
544// * duplicate types;
545// * duplicate decorations.
Pierre Moreau86627f72017-07-13 02:16:51 +0200546Optimizer::PassToken CreateRemoveDuplicatesPass();
547
Diego Novilloc75704e2017-09-06 08:56:41 -0400548// Creates a CFG cleanup pass.
549// This pass removes cruft from the control flow graph of functions that are
550// reachable from entry points and exported functions. It currently includes the
551// following functionality:
552//
553// - Removal of unreachable basic blocks.
554Optimizer::PassToken CreateCFGCleanupPass();
555
Steven Perron58347192017-10-20 12:17:41 -0400556// Create dead variable elimination pass.
557// This pass will delete module scope variables, along with their decorations,
558// that are not referenced.
559Optimizer::PassToken CreateDeadVariableEliminationPass();
560
Steven Perronb3daa932018-03-06 11:20:28 -0500561// create merge return pass.
562// changes functions that have multiple return statements so they have a single
563// return statement.
Alan Bakera92d69b2017-11-08 16:22:10 -0500564//
Steven Perronb3daa932018-03-06 11:20:28 -0500565// for structured control flow it is assumed that the only unreachable blocks in
566// the function are trivial merge and continue blocks.
Alan Bakera92d69b2017-11-08 16:22:10 -0500567//
Steven Perronb3daa932018-03-06 11:20:28 -0500568// a trivial merge block contains the label and an opunreachable instructions,
569// nothing else. a trivial continue block contain a label and an opbranch to
570// the header, nothing else.
571//
572// these conditions are guaranteed to be met after running dead-branch
573// elimination.
Alan Bakera92d69b2017-11-08 16:22:10 -0500574Optimizer::PassToken CreateMergeReturnPass();
575
Steven Perron28c41552017-11-10 20:26:55 -0500576// Create value numbering pass.
577// This pass will look for instructions in the same basic block that compute the
578// same value, and remove the redundant ones.
579Optimizer::PassToken CreateLocalRedundancyEliminationPass();
Steven Perron5d602ab2017-12-04 12:29:51 -0500580
Alexander Johnston84ccd0b2018-01-29 10:39:55 +0000581// Create LICM pass.
582// This pass will look for invariant instructions inside loops and hoist them to
583// the loops preheader.
584Optimizer::PassToken CreateLoopInvariantCodeMotionPass();
585
Stephen McGroarty9a5dd6f2018-04-23 21:01:12 +0100586// Creates a loop fission pass.
587// This pass will split all top level loops whose register pressure exceedes the
588// given |threshold|.
589Optimizer::PassToken CreateLoopFissionPass(size_t threshold);
590
Toomas Remmelg1dc24582018-04-20 15:14:45 +0100591// Creates a loop fusion pass.
592// This pass will look for adjacent loops that are compatible and legal to be
593// fused. The fuse all such loops as long as the register usage for the fused
594// loop stays under the threshold defined by |max_registers_per_loop|.
595Optimizer::PassToken CreateLoopFusionPass(size_t max_registers_per_loop);
596
Victor Lomuller10e5d7c2018-03-29 12:22:42 +0100597// Creates a loop peeling pass.
598// This pass will look for conditions inside a loop that are true or false only
599// for the N first or last iteration. For loop with such condition, those N
600// iterations of the loop will be executed outside of the main loop.
601// To limit code size explosion, the loop peeling can only happen if the code
602// size growth for each loop is under |code_growth_threshold|.
603Optimizer::PassToken CreateLoopPeelingPass();
604
Victor Lomuller3497a942018-02-12 21:42:15 +0000605// Creates a loop unswitch pass.
606// This pass will look for loop independent branch conditions and move the
607// condition out of the loop and version the loop based on the taken branch.
608// Works best after LICM and local multi store elimination pass.
609Optimizer::PassToken CreateLoopUnswitchPass();
610
Steven Perron5d602ab2017-12-04 12:29:51 -0500611// Create global value numbering pass.
612// This pass will look for instructions where the same value is computed on all
613// paths leading to the instruction. Those instructions are deleted.
614Optimizer::PassToken CreateRedundancyEliminationPass();
Alan Baker867451f2017-11-30 17:03:06 -0500615
616// Create scalar replacement pass.
617// This pass replaces composite function scope variables with variables for each
Steven Perrona579e722018-04-16 09:58:00 -0400618// element if those elements are accessed individually. The parameter is a
619// limit on the number of members in the composite variable that the pass will
620// consider replacing.
621Optimizer::PassToken CreateScalarReplacementPass(uint32_t size_limit = 100);
Steven Perronb86eb682017-12-11 13:10:24 -0500622
623// Create a private to local pass.
624// This pass looks for variables delcared in the private storage class that are
625// used in only one function. Those variables are moved to the function storage
626// class in the function that they are used.
627Optimizer::PassToken CreatePrivateToLocalPass();
Diego Novillo4ba9dcc2017-12-05 11:39:25 -0500628
629// Creates a conditional constant propagation (CCP) pass.
630// This pass implements the SSA-CCP algorithm in
631//
632// Constant propagation with conditional branches,
633// Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
634//
635// Constant values in expressions and conditional jumps are folded and
636// simplified. This may reduce code size by removing never executed jump targets
637// and computations with constant operands.
638Optimizer::PassToken CreateCCPPass();
639
Steven Perron34d42942018-01-17 14:57:37 -0500640// Creates a workaround driver bugs pass. This pass attempts to work around
641// a known driver bug (issue #1209) by identifying the bad code sequences and
642// rewriting them.
643//
644// Current workaround: Avoid OpUnreachable instructions in loops.
645Optimizer::PassToken CreateWorkaround1209Pass();
646
Alan Baker2e93e802018-01-16 11:15:06 -0500647// Creates a pass that converts if-then-else like assignments into OpSelect.
648Optimizer::PassToken CreateIfConversionPass();
649
Steven Perron61d8c032018-01-30 11:24:03 -0500650// Creates a pass that will replace instructions that are not valid for the
651// current shader stage by constants. Has no effect on non-shader modules.
652Optimizer::PassToken CreateReplaceInvalidOpcodePass();
653
Steven Perron06cdb962018-02-02 11:55:05 -0500654// Creates a pass that simplifies instructions using the instruction folder.
655Optimizer::PassToken CreateSimplificationPass();
656
Stephen McGroartydd8400e2018-02-14 17:03:12 +0000657// Create loop unroller pass.
Stephen McGroartye3549842018-02-27 11:50:08 +0000658// Creates a pass to unroll loops which have the "Unroll" loop control
Stephen McGroartydd8400e2018-02-14 17:03:12 +0000659// mask set. The loops must meet a specific criteria in order to be unrolled
660// safely this criteria is checked before doing the unroll by the
661// LoopUtils::CanPerformUnroll method. Any loop that does not meet the criteria
662// won't be unrolled. See CanPerformUnroll LoopUtils.h for more information.
Stephen McGroartye3549842018-02-27 11:50:08 +0000663Optimizer::PassToken CreateLoopUnrollPass(bool fully_unroll, int factor = 0);
Stephen McGroartydd8400e2018-02-14 17:03:12 +0000664
Diego Novillo735d8a52018-02-22 16:18:29 -0500665// Create the SSA rewrite pass.
666// This pass converts load/store operations on function local variables into
667// operations on SSA IDs. This allows SSA optimizers to act on these variables.
668// Only variables that are local to the function and of supported types are
669// processed (see IsSSATargetVar for details).
670Optimizer::PassToken CreateSSARewritePass();
671
greg-lunargd11725b2019-09-03 11:22:13 -0600672// Create pass to convert relaxed precision instructions to half precision.
673// This pass converts as many relaxed float32 arithmetic operations to half as
674// possible. It converts any float32 operands to half if needed. It converts
675// any resulting half precision values back to float32 as needed. No variables
676// are changed. No image operations are changed.
677//
greg-lunarg9215c1b2019-12-20 19:08:12 -0700678// Best if run after function scope store/load and composite operation
679// eliminations are run. Also best if followed by instruction simplification,
680// redundancy elimination and DCE.
greg-lunargd11725b2019-09-03 11:22:13 -0600681Optimizer::PassToken CreateConvertRelaxedToHalfPass();
682
683// Create relax float ops pass.
684// This pass decorates all float32 result instructions with RelaxedPrecision
685// if not already so decorated.
686Optimizer::PassToken CreateRelaxFloatOpsPass();
687
Steven Perronc4dc0462018-03-20 23:33:24 -0400688// Create copy propagate arrays pass.
689// This pass looks to copy propagate memory references for arrays. It looks
690// for specific code patterns to recognize array copies.
691Optimizer::PassToken CreateCopyPropagateArraysPass();
Steven Perron2c0ce872018-04-23 11:13:07 -0400692
693// Create a vector dce pass.
694// This pass looks for components of vectors that are unused, and removes them
695// from the vector. Note this would still leave around lots of dead code that
696// a pass of ADCE will be able to remove.
697Optimizer::PassToken CreateVectorDCEPass();
698
Steven Perronaf430ec2018-05-07 12:31:03 -0400699// Create a pass to reduce the size of loads.
700// This pass looks for loads of structures where only a few of its members are
701// used. It replaces the loads feeding an OpExtract with an OpAccessChain and
702// a load of the specific elements.
703Optimizer::PassToken CreateReduceLoadSizePass();
704
Alan Baker755e5c92018-07-23 11:23:11 -0400705// Create a pass to combine chained access chains.
706// This pass looks for access chains fed by other access chains and combines
707// them into a single instruction where possible.
708Optimizer::PassToken CreateCombineAccessChainsPass();
709
greg-lunarg1e9fc1a2018-11-08 11:54:54 -0700710// Create a pass to instrument bindless descriptor checking
711// This pass instruments all bindless references to check that descriptor
greg-lunarge1a76262019-03-19 06:53:43 -0700712// array indices are inbounds, and if the descriptor indexing extension is
713// enabled, that the descriptor has been initialized. If the reference is
714// invalid, a record is written to the debug output buffer (if space allows)
715// and a null value is returned. This pass is designed to support bindless
716// validation in the Vulkan validation layers.
717//
718// TODO(greg-lunarg): Add support for buffer references. Currently only does
719// checking for image references.
greg-lunarg1e9fc1a2018-11-08 11:54:54 -0700720//
721// Dead code elimination should be run after this pass as the original,
722// potentially invalid code is not removed and could cause undefined behavior,
723// including crashes. It may also be beneficial to run Simplification
724// (ie Constant Propagation), DeadBranchElim and BlockMerge after this pass to
725// optimize instrument code involving the testing of compile-time constants.
726// It is also generally recommended that this pass (and all
727// instrumentation passes) be run after any legalization and optimization
728// passes. This will give better analysis for the instrumentation and avoid
729// potentially de-optimizing the instrument code, for example, inlining
730// the debug record output function throughout the module.
731//
732// The instrumentation will read and write buffers in debug
733// descriptor set |desc_set|. It will write |shader_id| in each output record
734// to identify the shader module which generated the record.
greg-lunarg7046c052020-12-01 09:28:16 -0700735// |desc_length_enable| controls instrumentation of runtime descriptor array
736// references, |desc_init_enable| controls instrumentation of descriptor
737// initialization checking, and |buff_oob_enable| controls instrumentation
738// of storage and uniform buffer bounds checking, all of which require input
739// buffer support. |texbuff_oob_enable| controls instrumentation of texel
740// buffers, which does not require input buffer support.
greg-lunargcf211462019-02-07 12:00:36 -0700741Optimizer::PassToken CreateInstBindlessCheckPass(
greg-lunarg7046c052020-12-01 09:28:16 -0700742 uint32_t desc_set, uint32_t shader_id, bool desc_length_enable = false,
743 bool desc_init_enable = false, bool buff_oob_enable = false,
744 bool texbuff_oob_enable = false);
greg-lunarg1e9fc1a2018-11-08 11:54:54 -0700745
greg-lunarg06407252019-08-16 07:18:34 -0600746// Create a pass to instrument physical buffer address checking
747// This pass instruments all physical buffer address references to check that
748// all referenced bytes fall in a valid buffer. If the reference is
749// invalid, a record is written to the debug output buffer (if space allows)
750// and a null value is returned. This pass is designed to support buffer
751// address validation in the Vulkan validation layers.
752//
753// Dead code elimination should be run after this pass as the original,
754// potentially invalid code is not removed and could cause undefined behavior,
755// including crashes. Instruction simplification would likely also be
756// beneficial. It is also generally recommended that this pass (and all
757// instrumentation passes) be run after any legalization and optimization
758// passes. This will give better analysis for the instrumentation and avoid
759// potentially de-optimizing the instrument code, for example, inlining
760// the debug record output function throughout the module.
761//
762// The instrumentation will read and write buffers in debug
763// descriptor set |desc_set|. It will write |shader_id| in each output record
764// to identify the shader module which generated the record.
greg-lunarg06407252019-08-16 07:18:34 -0600765Optimizer::PassToken CreateInstBuffAddrCheckPass(uint32_t desc_set,
greg-lunarg44102722020-05-21 11:10:42 -0600766 uint32_t shader_id);
greg-lunarg06407252019-08-16 07:18:34 -0600767
greg-lunarg1fe9bcc2020-03-12 07:19:52 -0600768// Create a pass to instrument OpDebugPrintf instructions.
769// This pass replaces all OpDebugPrintf instructions with instructions to write
770// a record containing the string id and the all specified values into a special
771// printf output buffer (if space allows). This pass is designed to support
772// the printf validation in the Vulkan validation layers.
773//
774// The instrumentation will write buffers in debug descriptor set |desc_set|.
775// It will write |shader_id| in each output record to identify the shader
776// module which generated the record.
777Optimizer::PassToken CreateInstDebugPrintfPass(uint32_t desc_set,
778 uint32_t shader_id);
779
alan-bakere510b1b2018-11-30 14:15:51 -0500780// Create a pass to upgrade to the VulkanKHR memory model.
781// This pass upgrades the Logical GLSL450 memory model to Logical VulkanKHR.
782// Additionally, it modifies memory, image, atomic and barrier operations to
783// conform to that model's requirements.
784Optimizer::PassToken CreateUpgradeMemoryModelPass();
785
Steven Perrondd4157d2019-01-17 15:56:36 -0500786// Create a pass to do code sinking. Code sinking is a transformation
787// where an instruction is moved into a more deeply nested construct.
788Optimizer::PassToken CreateCodeSinkingPass();
789
Steven Perron3a0bc9e2019-04-05 13:12:08 -0400790// Create a pass to fix incorrect storage classes. In order to make code
791// generation simpler, DXC may generate code where the storage classes do not
792// match up correctly. This pass will fix the errors that it can.
793Optimizer::PassToken CreateFixStorageClassPass();
794
David Neto31590102019-07-30 19:52:46 -0400795// Creates a graphics robust access pass.
796//
797// This pass injects code to clamp indexed accesses to buffers and internal
798// arrays, providing guarantees satisfying Vulkan's robustBufferAccess rules.
799//
800// TODO(dneto): Clamps coordinates and sample index for pointer calculations
801// into storage images (OpImageTexelPointer). For an cube array image, it
802// assumes the maximum layer count times 6 is at most 0xffffffff.
803//
804// NOTE: This pass will fail with a message if:
805// - The module is not a Shader module.
806// - The module declares VariablePointers, VariablePointersStorageBuffer, or
807// RuntimeDescriptorArrayEXT capabilities.
808// - The module uses an addressing model other than Logical
809// - Access chain indices are wider than 64 bits.
810// - Access chain index for a struct is not an OpConstant integer or is out
811// of range. (The module is already invalid if that is the case.)
812// - TODO(dneto): The OpImageTexelPointer coordinate component is not 32-bits
813// wide.
David Netoaf741052019-12-03 11:18:56 -0500814//
815// NOTE: Access chain indices are always treated as signed integers. So
816// if an array has a fixed size of more than 2^31 elements, then elements
817// from 2^31 and above are never accessible with a 32-bit index,
818// signed or unsigned. For this case, this pass will clamp the index
819// between 0 and at 2^31-1, inclusive.
820// Similarly, if an array has more then 2^15 element and is accessed with
821// a 16-bit index, then elements from 2^15 and above are not accessible.
822// In this case, the pass will clamp the index between 0 and 2^15-1
823// inclusive.
David Neto31590102019-07-30 19:52:46 -0400824Optimizer::PassToken CreateGraphicsRobustAccessPass();
825
Steven Perron4b64beb2019-08-08 10:53:19 -0400826// Create descriptor scalar replacement pass.
827// This pass replaces every array variable |desc| that has a DescriptorSet and
828// Binding decorations with a new variable for each element of the array.
829// Suppose |desc| was bound at binding |b|. Then the variable corresponding to
830// |desc[i]| will have binding |b+i|. The descriptor set will be the same. It
831// is assumed that no other variable already has a binding that will used by one
832// of the new variables. If not, the pass will generate invalid Spir-V. All
833// accesses to |desc| must be OpAccessChain instructions with a literal index
834// for the first index.
835Optimizer::PassToken CreateDescriptorScalarReplacementPass();
836
alan-bakerf3cec932020-07-22 11:45:02 -0400837// Create a pass to replace each OpKill instruction with a function call to a
838// function that has a single OpKill. Also replace each OpTerminateInvocation
839// instruction with a function call to a function that has a single
840// OpTerminateInvocation. This allows more code to be inlined.
Steven Perron60043ed2019-08-14 09:27:12 -0400841Optimizer::PassToken CreateWrapOpKillPass();
842
Steven Perron35d98be2019-08-29 12:48:17 -0400843// Replaces the extensions VK_AMD_shader_ballot,VK_AMD_gcn_shader, and
844// VK_AMD_shader_trinary_minmax with equivalent code using core instructions and
845// capabilities.
846Optimizer::PassToken CreateAmdExtToKhrPass();
847
Greg Fischer48007a52021-03-31 12:26:36 -0600848// Replaces the internal version of GLSLstd450 InterpolateAt* extended
849// instructions with the externally valid version. The internal version allows
850// an OpLoad of the interpolant for the first argument. This pass removes the
851// OpLoad and replaces it with its pointer. glslang and possibly other
852// frontends will create the internal version for HLSL. This pass will be part
853// of HLSL legalization and should be called after interpolants have been
854// propagated into their final positions.
855Optimizer::PassToken CreateInterpolateFixupPass();
856
Lei Zhangf18e1f22016-09-12 14:11:46 -0400857} // namespace spvtools
858
dan sinclair58a68762018-08-03 08:05:33 -0400859#endif // INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_