blob: ab6ca5106658480dd8f0ffd96a3918067294b795 [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
15#ifndef SPIRV_TOOLS_OPTIMIZER_HPP_
16#define SPIRV_TOOLS_OPTIMIZER_HPP_
17
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
28// C++ interface for SPIR-V optimization functionalities. It wraps the context
29// (including target environment and the corresponding SPIR-V grammar) and
30// provides methods for registering optimization passes and optimizing.
31//
32// Instances of this class provides basic thread-safety guarantee.
33class Optimizer {
34 public:
35 // The token for an optimization pass. It is returned via one of the
36 // Create*Pass() standalone functions at the end of this header file and
37 // consumed by the RegisterPass() method. Tokens are one-time objects that
38 // only support move; copying is not allowed.
39 struct PassToken {
40 struct Impl; // Opaque struct for holding inernal data.
41
42 PassToken(std::unique_ptr<Impl>);
43
44 // Tokens can only be moved. Copying is disabled.
45 PassToken(const PassToken&) = delete;
46 PassToken(PassToken&&);
47 PassToken& operator=(const PassToken&) = delete;
48 PassToken& operator=(PassToken&&);
49
50 ~PassToken();
51
52 std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
53 };
54
55 // Constructs an instance with the given target |env|, which is used to decode
56 // the binaries to be optimized later.
57 //
58 // The constructed instance will have an empty message consumer, which just
59 // ignores all messages from the library. Use SetMessageConsumer() to supply
60 // one if messages are of concern.
61 explicit Optimizer(spv_target_env env);
62
63 // Disables copy/move constructor/assignment operations.
64 Optimizer(const Optimizer&) = delete;
65 Optimizer(Optimizer&&) = delete;
66 Optimizer& operator=(const Optimizer&) = delete;
67 Optimizer& operator=(Optimizer&&) = delete;
68
69 // Destructs this instance.
70 ~Optimizer();
71
72 // Sets the message consumer to the given |consumer|. The |consumer| will be
73 // invoked once for each message communicated from the library.
74 void SetMessageConsumer(MessageConsumer consumer);
75
76 // Registers the given |pass| to this optimizer. Passes will be run in the
77 // exact order of registration. The token passed in will be consumed by this
78 // method.
79 Optimizer& RegisterPass(PassToken&& pass);
80
Diego Novilloc90d7302017-08-30 14:19:22 -040081 // Registers passes that attempt to improve performance of generated code.
82 // This sequence of passes is subject to constant review and will change
83 // from time to time.
84 Optimizer& RegisterPerformancePasses();
85
86 // Registers passes that attempt to improve the size of generated code.
87 // This sequence of passes is subject to constant review and will change
88 // from time to time.
89 Optimizer& RegisterSizePasses();
90
Lei Zhangaec60b82017-11-17 16:50:43 -050091 // Registers passes that attempt to legalize the generated code.
92 //
93 // Note: this recipe is specially for legalizing SPIR-V. It should be used
94 // by compilers after translating HLSL source code literally. It should
95 // *not* be used by general workloads for performance or size improvement.
96 //
97 // This sequence of passes is subject to constant review and will change
98 // from time to time.
99 Optimizer& RegisterLegalizationPasses();
100
Lei Zhangf18e1f22016-09-12 14:11:46 -0400101 // Optimizes the given SPIR-V module |original_binary| and writes the
102 // optimized binary into |optimized_binary|.
103 // Returns true on successful optimization, whether or not the module is
104 // modified. Returns false if errors occur when processing |original_binary|
105 // using any of the registered passes. In that case, no further passes are
Diego Novilloc90d7302017-08-30 14:19:22 -0400106 // executed and the contents in |optimized_binary| may be invalid.
Lei Zhangf18e1f22016-09-12 14:11:46 -0400107 //
108 // It's allowed to alias |original_binary| to the start of |optimized_binary|.
109 bool Run(const uint32_t* original_binary, size_t original_binary_size,
110 std::vector<uint32_t>* optimized_binary) const;
111
Diego Novilloc90d7302017-08-30 14:19:22 -0400112 // Returns a vector of strings with all the pass names added to this
113 // optimizer's pass manager. These strings are valid until the associated
114 // pass manager is destroyed.
Steven Perron58347192017-10-20 12:17:41 -0400115 std::vector<const char*> GetPassNames() const;
Diego Novilloc90d7302017-08-30 14:19:22 -0400116
David Netoc32e79e2018-01-04 12:59:50 -0500117 // Sets the option to print the disassembly before each pass and after the
118 // last pass. If |out| is null, then no output is generated. Otherwise,
119 // output is sent to the |out| output stream.
120 Optimizer& SetPrintAll(std::ostream* out);
121
Jaebaek Seo3b594e12018-03-07 09:25:51 -0500122 // Sets the option to print the resource utilization of each pass. If |out|
123 // is null, then no output is generated. Otherwise, output is sent to the
124 // |out| output stream.
125 Optimizer& SetTimeReport(std::ostream* out);
126
Lei Zhangf18e1f22016-09-12 14:11:46 -0400127 private:
128 struct Impl; // Opaque struct for holding internal data.
129 std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
130};
131
132// Creates a null pass.
133// A null pass does nothing to the SPIR-V module to be optimized.
134Optimizer::PassToken CreateNullPass();
135
136// Creates a strip-debug-info pass.
137// A strip-debug-info pass removes all debug instructions (as documented in
138// Section 3.32.2 of the SPIR-V spec) of the SPIR-V module to be optimized.
139Optimizer::PassToken CreateStripDebugInfoPass();
140
David Neto844e1862018-03-09 16:08:57 -0500141// Creates a strip-reflect-info pass.
142// A strip-reflect-info pass removes all reflections instructions.
143// For now, this is limited to removing decorations defined in
144// SPV_GOOGLE_hlsl_functionality1. The coverage may expand in
145// the future.
146Optimizer::PassToken CreateStripReflectInfoPass();
147
Steven Perrone43c9102017-09-19 10:12:13 -0400148// Creates an eliminate-dead-functions pass.
Steven Perron58347192017-10-20 12:17:41 -0400149// An eliminate-dead-functions pass will remove all functions that are not in
150// the call trees rooted at entry points and exported functions. These
151// functions are not needed because they will never be called.
Steven Perrone43c9102017-09-19 10:12:13 -0400152Optimizer::PassToken CreateEliminateDeadFunctionsPass();
153
qining144f59e2017-04-19 18:10:59 -0400154// Creates a set-spec-constant-default-value pass from a mapping from spec-ids
155// to the default values in the form of string.
Lei Zhangf18e1f22016-09-12 14:11:46 -0400156// A set-spec-constant-default-value pass sets the default values for the
157// spec constants that have SpecId decorations (i.e., those defined by
158// OpSpecConstant{|True|False} instructions).
159Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
160 const std::unordered_map<uint32_t, std::string>& id_value_map);
161
qining144f59e2017-04-19 18:10:59 -0400162// Creates a set-spec-constant-default-value pass from a mapping from spec-ids
163// to the default values in the form of bit pattern.
164// A set-spec-constant-default-value pass sets the default values for the
165// spec constants that have SpecId decorations (i.e., those defined by
166// OpSpecConstant{|True|False} instructions).
167Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
168 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map);
169
David Neto11a867f2017-04-01 16:10:16 -0400170// Creates a flatten-decoration pass.
171// A flatten-decoration pass replaces grouped decorations with equivalent
172// ungrouped decorations. That is, it replaces each OpDecorationGroup
173// instruction and associated OpGroupDecorate and OpGroupMemberDecorate
174// instructions with equivalent OpDecorate and OpMemberDecorate instructions.
175// The pass does not attempt to preserve debug information for instructions
176// it removes.
177Optimizer::PassToken CreateFlattenDecorationPass();
178
Lei Zhangf18e1f22016-09-12 14:11:46 -0400179// Creates a freeze-spec-constant-value pass.
180// A freeze-spec-constant pass specializes the value of spec constants to
181// their default values. This pass only processes the spec constants that have
182// SpecId decorations (defined by OpSpecConstant, OpSpecConstantTrue, or
183// OpSpecConstantFalse instructions) and replaces them with their normal
184// counterparts (OpConstant, OpConstantTrue, or OpConstantFalse). The
185// corresponding SpecId annotation instructions will also be removed. This
186// pass does not fold the newly added normal constants and does not process
187// other spec constants defined by OpSpecConstantComposite or
188// OpSpecConstantOp.
189Optimizer::PassToken CreateFreezeSpecConstantValuePass();
190
191// Creates a fold-spec-constant-op-and-composite pass.
192// A fold-spec-constant-op-and-composite pass folds spec constants defined by
193// OpSpecConstantOp or OpSpecConstantComposite instruction, to normal Constants
194// defined by OpConstantTrue, OpConstantFalse, OpConstant, OpConstantNull, or
195// OpConstantComposite instructions. Note that spec constants defined with
196// OpSpecConstant, OpSpecConstantTrue, or OpSpecConstantFalse instructions are
197// not handled, as these instructions indicate their value are not determined
198// and can be changed in future. A spec constant is foldable if all of its
199// value(s) can be determined from the module. E.g., an integer spec constant
200// defined with OpSpecConstantOp instruction can be folded if its value won't
201// change later. This pass will replace the original OpSpecContantOp instruction
202// with an OpConstant instruction. When folding composite spec constants,
203// new instructions may be inserted to define the components of the composite
204// constant first, then the original spec constants will be replaced by
205// OpConstantComposite instructions.
206//
207// There are some operations not supported yet:
208// OpSConvert, OpFConvert, OpQuantizeToF16 and
209// all the operations under Kernel capability.
210// TODO(qining): Add support for the operations listed above.
211Optimizer::PassToken CreateFoldSpecConstantOpAndCompositePass();
212
213// Creates a unify-constant pass.
214// A unify-constant pass de-duplicates the constants. Constants with the exact
215// same value and identical form will be unified and only one constant will
216// be kept for each unique pair of type and value.
217// There are several cases not handled by this pass:
218// 1) Constants defined by OpConstantNull instructions (null constants) and
219// constants defined by OpConstantFalse, OpConstant or OpConstantComposite
220// with value 0 (zero-valued normal constants) are not considered equivalent.
221// So null constants won't be used to replace zero-valued normal constants,
222// vice versa.
223// 2) Whenever there are decorations to the constant's result id id, the
224// constant won't be handled, which means, it won't be used to replace any
225// other constants, neither can other constants replace it.
226// 3) NaN in float point format with different bit patterns are not unified.
227Optimizer::PassToken CreateUnifyConstantPass();
228
229// Creates a eliminate-dead-constant pass.
230// A eliminate-dead-constant pass removes dead constants, including normal
231// contants defined by OpConstant, OpConstantComposite, OpConstantTrue, or
232// OpConstantFalse and spec constants defined by OpSpecConstant,
233// OpSpecConstantComposite, OpSpecConstantTrue, OpSpecConstantFalse or
234// OpSpecConstantOp.
235Optimizer::PassToken CreateEliminateDeadConstantPass();
236
Steven Perrone4c7d8e2017-09-08 12:08:03 -0400237// Creates a strength-reduction pass.
238// A strength-reduction pass will look for opportunities to replace an
239// instruction with an equivalent and less expensive one. For example,
240// multiplying by a power of 2 can be replaced by a bit shift.
241Optimizer::PassToken CreateStrengthReductionPass();
242
GregFad1d0352017-06-07 15:28:53 -0600243// Creates a block merge pass.
244// This pass searches for blocks with a single Branch to a block with no
245// other predecessors and merges the blocks into a single block. Continue
246// blocks and Merge blocks are not candidates for the second block.
247//
248// The pass is most useful after Dead Branch Elimination, which can leave
249// such sequences of blocks. Merging them makes subsequent passes more
250// effective, such as single block local store-load elimination.
251//
252// While this pass reduces the number of occurrences of this sequence, at
253// this time it does not guarantee all such sequences are eliminated.
254//
255// Presence of phi instructions can inhibit this optimization. Handling
Steven Perrone43c9102017-09-19 10:12:13 -0400256// these is left for future improvements.
GregFad1d0352017-06-07 15:28:53 -0600257Optimizer::PassToken CreateBlockMergePass();
258
GregF429ca052017-08-15 17:58:28 -0600259// Creates an exhaustive inline pass.
260// An exhaustive inline pass attempts to exhaustively inline all function
261// calls in all functions in an entry point call tree. The intent is to enable,
262// albeit through brute force, analysis and optimization across function
263// calls by subsequent optimization passes. As the inlining is exhaustive,
264// there is no attempt to optimize for size or runtime performance. Functions
265// that are not in the call tree of an entry point are not changed.
GregFe28bd392017-08-01 17:20:13 -0600266Optimizer::PassToken CreateInlineExhaustivePass();
Steven Perrone43c9102017-09-19 10:12:13 -0400267
GregF429ca052017-08-15 17:58:28 -0600268// Creates an opaque inline pass.
269// An opaque inline pass inlines all function calls in all functions in all
270// entry point call trees where the called function contains an opaque type
271// in either its parameter types or return type. An opaque type is currently
272// defined as Image, Sampler or SampledImage. The intent is to enable, albeit
273// through brute force, analysis and optimization across these function calls
274// by subsequent passes in order to remove the storing of opaque types which is
275// not legal in Vulkan. Functions that are not in the call tree of an entry
276// point are not changed.
277Optimizer::PassToken CreateInlineOpaquePass();
Steven Perrone43c9102017-09-19 10:12:13 -0400278
GregF7c8da662017-05-18 14:51:55 -0600279// Creates a single-block local variable load/store elimination pass.
Steven Perrone43c9102017-09-19 10:12:13 -0400280// For every entry point function, do single block memory optimization of
GregF7c8da662017-05-18 14:51:55 -0600281// function variables referenced only with non-access-chain loads and stores.
282// For each targeted variable load, if previous store to that variable in the
283// block, replace the load's result id with the value id of the store.
284// If previous load within the block, replace the current load's result id
285// with the previous load's result id. In either case, delete the current
286// load. Finally, check if any remaining stores are useless, and delete store
287// and variable if possible.
288//
289// The presence of access chain references and function calls can inhibit
290// the above optimization.
291//
Steven Perron79a00642017-12-11 13:10:24 -0500292// Only modules with relaxed logical addressing (see opt/instruction.h) are
293// currently processed.
GregF7c8da662017-05-18 14:51:55 -0600294//
Steven Perrone43c9102017-09-19 10:12:13 -0400295// This pass is most effective if preceeded by Inlining and
GregF7c8da662017-05-18 14:51:55 -0600296// LocalAccessChainConvert. This pass will reduce the work needed to be done
GregFcc8bad32017-06-16 15:37:31 -0600297// by LocalSingleStoreElim and LocalMultiStoreElim.
GregF429ca052017-08-15 17:58:28 -0600298//
299// Only functions in the call tree of an entry point are processed.
GregF7c8da662017-05-18 14:51:55 -0600300Optimizer::PassToken CreateLocalSingleBlockLoadStoreElimPass();
Greg Fischer04fcc662016-11-10 10:11:50 -0700301
GregF52e247f2017-06-02 13:23:20 -0600302// Create dead branch elimination pass.
303// For each entry point function, this pass will look for SelectionMerge
304// BranchConditionals with constant condition and convert to a Branch to
305// the indicated label. It will delete resulting dead blocks.
306//
Andrey Tuganov4b1577a2017-10-05 16:26:09 -0400307// For all phi functions in merge block, replace all uses with the id
308// corresponding to the living predecessor.
309//
Alan Baker1b6cfd32018-01-04 17:04:03 -0500310// Note that some branches and blocks may be left to avoid creating invalid
311// control flow. Improving this is left to future work.
GregF52e247f2017-06-02 13:23:20 -0600312//
313// This pass is most effective when preceeded by passes which eliminate
314// local loads and stores, effectively propagating constant values where
315// possible.
316Optimizer::PassToken CreateDeadBranchElimPass();
317
GregFcc8bad32017-06-16 15:37:31 -0600318// Creates an SSA local variable load/store elimination pass.
319// For every entry point function, eliminate all loads and stores of function
320// scope variables only referenced with non-access-chain loads and stores.
Steven Perrone43c9102017-09-19 10:12:13 -0400321// Eliminate the variables as well.
GregFcc8bad32017-06-16 15:37:31 -0600322//
323// The presence of access chain references and function calls can inhibit
324// the above optimization.
325//
Steven Perron79a00642017-12-11 13:10:24 -0500326// Only shader modules with relaxed logical addressing (see opt/instruction.h)
327// are currently processed. Currently modules with any extensions enabled are
328// not processed. This is left for future work.
GregFcc8bad32017-06-16 15:37:31 -0600329//
Steven Perrone43c9102017-09-19 10:12:13 -0400330// This pass is most effective if preceeded by Inlining and
GregFcc8bad32017-06-16 15:37:31 -0600331// LocalAccessChainConvert. LocalSingleStoreElim and LocalSingleBlockElim
332// will reduce the work that this pass has to do.
333Optimizer::PassToken CreateLocalMultiStoreElimPass();
334
GregFaa7e6872017-05-12 17:27:21 -0600335// Creates a local access chain conversion pass.
336// A local access chain conversion pass identifies all function scope
337// variables which are accessed only with loads, stores and access chains
338// with constant indices. It then converts all loads and stores of such
339// variables into equivalent sequences of loads, stores, extracts and inserts.
340//
341// This pass only processes entry point functions. It currently only converts
342// non-nested, non-ptr access chains. It does not process modules with
343// non-32-bit integer types present. Optional memory access options on loads
344// and stores are ignored as we are only processing function scope variables.
345//
346// This pass unifies access to these variables to a single mode and simplifies
347// subsequent analysis and elimination of these variables along with their
348// loads and stores allowing values to propagate to their points of use where
349// possible.
350Optimizer::PassToken CreateLocalAccessChainConvertPass();
351
GregF0c5722f2017-05-19 17:31:28 -0600352// Creates a local single store elimination pass.
Steven Perrone43c9102017-09-19 10:12:13 -0400353// For each entry point function, this pass eliminates loads and stores for
GregF0c5722f2017-05-19 17:31:28 -0600354// function scope variable that are stored to only once, where possible. Only
355// whole variable loads and stores are eliminated; access-chain references are
356// not optimized. Replace all loads of such variables with the value that is
357// stored and eliminate any resulting dead code.
358//
359// Currently, the presence of access chains and function calls can inhibit this
360// pass, however the Inlining and LocalAccessChainConvert passes can make it
361// more effective. In additional, many non-load/store memory operations are
362// not supported and will prohibit optimization of a function. Support of
363// these operations are future work.
364//
Steven Perron79a00642017-12-11 13:10:24 -0500365// Only shader modules with relaxed logical addressing (see opt/instruction.h)
366// are currently processed.
367//
GregF0c5722f2017-05-19 17:31:28 -0600368// This pass will reduce the work needed to be done by LocalSingleBlockElim
GregFcc8bad32017-06-16 15:37:31 -0600369// and LocalMultiStoreElim and can improve the effectiveness of other passes
370// such as DeadBranchElimination which depend on values for their analysis.
GregF0c5722f2017-05-19 17:31:28 -0600371Optimizer::PassToken CreateLocalSingleStoreElimPass();
372
GregF6136bf92017-05-26 10:33:11 -0600373// Creates an insert/extract elimination pass.
374// This pass processes each entry point function in the module, searching for
375// extracts on a sequence of inserts. It further searches the sequence for an
376// insert with indices identical to the extract. If such an insert can be
377// found before hitting a conflicting insert, the extract's result id is
378// replaced with the id of the values from the insert.
379//
380// Besides removing extracts this pass enables subsequent dead code elimination
381// passes to delete the inserts. This pass performs best after access chains are
382// converted to inserts and extracts and local loads and stores are eliminated.
383Optimizer::PassToken CreateInsertExtractElimPass();
384
GregFf28b1062018-01-26 17:05:33 -0700385// Creates a dead insert elimination pass.
386// This pass processes each entry point function in the module, searching for
387// unreferenced inserts into composite types. These are most often unused
388// stores to vector components. They are unused because they are never
389// referenced, or because there is another insert to the same component between
390// the insert and the reference. After removing the inserts, dead code
391// elimination is attempted on the inserted values.
392//
393// This pass performs best after access chains are converted to inserts and
394// extracts and local loads and stores are eliminated. While executing this
395// pass can be advantageous on its own, it is also advantageous to execute
396// this pass after CreateInsertExtractPass() as it will remove any unused
397// inserts created by that pass.
398Optimizer::PassToken CreateDeadInsertElimPass();
399
GregFf4b29f32017-07-03 17:23:04 -0600400// Creates a pass to consolidate uniform references.
401// For each entry point function in the module, first change all constant index
Steven Perrone43c9102017-09-19 10:12:13 -0400402// access chain loads into equivalent composite extracts. Then consolidate
GregFf4b29f32017-07-03 17:23:04 -0600403// identical uniform loads into one uniform load. Finally, consolidate
404// identical uniform extracts into one uniform extract. This may require
405// moving a load or extract to a point which dominates all uses.
406//
407// This pass requires a module to have structured control flow ie shader
408// capability. It also requires logical addressing ie Addresses capability
409// is not enabled. It also currently does not support any extensions.
410//
411// This pass currently only optimizes loads with a single index.
412Optimizer::PassToken CreateCommonUniformElimPass();
413
GregF9de4e692017-06-08 10:37:21 -0600414// Create aggressive dead code elimination pass
Alan Baker3a054e12017-12-18 12:13:10 -0500415// This pass eliminates unused code from the module. In addition,
GregF9de4e692017-06-08 10:37:21 -0600416// it detects and eliminates code which may have spurious uses but which do
417// not contribute to the output of the function. The most common cause of
418// such code sequences is summations in loops whose result is no longer used
419// due to dead code elimination. This optimization has additional compile
420// time cost over standard dead code elimination.
421//
422// This pass only processes entry point functions. It also only processes
Alan Baker3a054e12017-12-18 12:13:10 -0500423// shaders with relaxed logical addressing (see opt/instruction.h). It
424// currently will not process functions with function calls. Unreachable
425// functions are deleted.
GregF9de4e692017-06-08 10:37:21 -0600426//
427// This pass will be made more effective by first running passes that remove
428// dead control flow and inlines function calls.
429//
430// This pass can be especially useful after running Local Access Chain
431// Conversion, which tends to cause cycles of dead code to be left after
432// Store/Load elimination passes are completed. These cycles cannot be
433// eliminated with standard dead code elimination.
434Optimizer::PassToken CreateAggressiveDCEPass();
435
Andrey Tuganov1e309af2017-04-11 15:11:04 -0400436// Creates a compact ids pass.
437// The pass remaps result ids to a compact and gapless range starting from %1.
438Optimizer::PassToken CreateCompactIdsPass();
439
Pierre Moreau7183ad52018-01-03 01:54:55 +0100440// Creates a remove duplicate pass.
441// This pass removes various duplicates:
442// * duplicate capabilities;
443// * duplicate extended instruction imports;
444// * duplicate types;
445// * duplicate decorations.
Pierre Moreau86627f72017-07-13 02:16:51 +0200446Optimizer::PassToken CreateRemoveDuplicatesPass();
447
Diego Novilloc75704e2017-09-06 08:56:41 -0400448// Creates a CFG cleanup pass.
449// This pass removes cruft from the control flow graph of functions that are
450// reachable from entry points and exported functions. It currently includes the
451// following functionality:
452//
453// - Removal of unreachable basic blocks.
454Optimizer::PassToken CreateCFGCleanupPass();
455
Steven Perron58347192017-10-20 12:17:41 -0400456// Create dead variable elimination pass.
457// This pass will delete module scope variables, along with their decorations,
458// that are not referenced.
459Optimizer::PassToken CreateDeadVariableEliminationPass();
460
Steven Perronb3daa932018-03-06 11:20:28 -0500461// create merge return pass.
462// changes functions that have multiple return statements so they have a single
463// return statement.
Alan Bakera92d69b2017-11-08 16:22:10 -0500464//
Steven Perronb3daa932018-03-06 11:20:28 -0500465// for structured control flow it is assumed that the only unreachable blocks in
466// the function are trivial merge and continue blocks.
Alan Bakera92d69b2017-11-08 16:22:10 -0500467//
Steven Perronb3daa932018-03-06 11:20:28 -0500468// a trivial merge block contains the label and an opunreachable instructions,
469// nothing else. a trivial continue block contain a label and an opbranch to
470// the header, nothing else.
471//
472// these conditions are guaranteed to be met after running dead-branch
473// elimination.
Alan Bakera92d69b2017-11-08 16:22:10 -0500474Optimizer::PassToken CreateMergeReturnPass();
475
Steven Perron28c41552017-11-10 20:26:55 -0500476// Create value numbering pass.
477// This pass will look for instructions in the same basic block that compute the
478// same value, and remove the redundant ones.
479Optimizer::PassToken CreateLocalRedundancyEliminationPass();
Steven Perron5d602ab2017-12-04 12:29:51 -0500480
Alexander Johnston84ccd0b2018-01-29 10:39:55 +0000481// Create LICM pass.
482// This pass will look for invariant instructions inside loops and hoist them to
483// the loops preheader.
484Optimizer::PassToken CreateLoopInvariantCodeMotionPass();
485
Victor Lomuller10e5d7c2018-03-29 12:22:42 +0100486// Creates a loop peeling pass.
487// This pass will look for conditions inside a loop that are true or false only
488// for the N first or last iteration. For loop with such condition, those N
489// iterations of the loop will be executed outside of the main loop.
490// To limit code size explosion, the loop peeling can only happen if the code
491// size growth for each loop is under |code_growth_threshold|.
492Optimizer::PassToken CreateLoopPeelingPass();
493
Victor Lomuller3497a942018-02-12 21:42:15 +0000494// Creates a loop unswitch pass.
495// This pass will look for loop independent branch conditions and move the
496// condition out of the loop and version the loop based on the taken branch.
497// Works best after LICM and local multi store elimination pass.
498Optimizer::PassToken CreateLoopUnswitchPass();
499
Steven Perron5d602ab2017-12-04 12:29:51 -0500500// Create global value numbering pass.
501// This pass will look for instructions where the same value is computed on all
502// paths leading to the instruction. Those instructions are deleted.
503Optimizer::PassToken CreateRedundancyEliminationPass();
Alan Baker867451f2017-11-30 17:03:06 -0500504
505// Create scalar replacement pass.
506// This pass replaces composite function scope variables with variables for each
507// element if those elements are accessed individually.
508Optimizer::PassToken CreateScalarReplacementPass();
Steven Perronb86eb682017-12-11 13:10:24 -0500509
510// Create a private to local pass.
511// This pass looks for variables delcared in the private storage class that are
512// used in only one function. Those variables are moved to the function storage
513// class in the function that they are used.
514Optimizer::PassToken CreatePrivateToLocalPass();
Diego Novillo4ba9dcc2017-12-05 11:39:25 -0500515
516// Creates a conditional constant propagation (CCP) pass.
517// This pass implements the SSA-CCP algorithm in
518//
519// Constant propagation with conditional branches,
520// Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
521//
522// Constant values in expressions and conditional jumps are folded and
523// simplified. This may reduce code size by removing never executed jump targets
524// and computations with constant operands.
525Optimizer::PassToken CreateCCPPass();
526
Steven Perron34d42942018-01-17 14:57:37 -0500527// Creates a workaround driver bugs pass. This pass attempts to work around
528// a known driver bug (issue #1209) by identifying the bad code sequences and
529// rewriting them.
530//
531// Current workaround: Avoid OpUnreachable instructions in loops.
532Optimizer::PassToken CreateWorkaround1209Pass();
533
Alan Baker2e93e802018-01-16 11:15:06 -0500534// Creates a pass that converts if-then-else like assignments into OpSelect.
535Optimizer::PassToken CreateIfConversionPass();
536
Steven Perron61d8c032018-01-30 11:24:03 -0500537// Creates a pass that will replace instructions that are not valid for the
538// current shader stage by constants. Has no effect on non-shader modules.
539Optimizer::PassToken CreateReplaceInvalidOpcodePass();
540
Steven Perron06cdb962018-02-02 11:55:05 -0500541// Creates a pass that simplifies instructions using the instruction folder.
542Optimizer::PassToken CreateSimplificationPass();
543
Stephen McGroartydd8400e2018-02-14 17:03:12 +0000544// Create loop unroller pass.
Stephen McGroartye3549842018-02-27 11:50:08 +0000545// Creates a pass to unroll loops which have the "Unroll" loop control
Stephen McGroartydd8400e2018-02-14 17:03:12 +0000546// mask set. The loops must meet a specific criteria in order to be unrolled
547// safely this criteria is checked before doing the unroll by the
548// LoopUtils::CanPerformUnroll method. Any loop that does not meet the criteria
549// won't be unrolled. See CanPerformUnroll LoopUtils.h for more information.
Stephen McGroartye3549842018-02-27 11:50:08 +0000550Optimizer::PassToken CreateLoopUnrollPass(bool fully_unroll, int factor = 0);
Stephen McGroartydd8400e2018-02-14 17:03:12 +0000551
Diego Novillo735d8a52018-02-22 16:18:29 -0500552// Create the SSA rewrite pass.
553// This pass converts load/store operations on function local variables into
554// operations on SSA IDs. This allows SSA optimizers to act on these variables.
555// Only variables that are local to the function and of supported types are
556// processed (see IsSSATargetVar for details).
557Optimizer::PassToken CreateSSARewritePass();
558
Steven Perronc4dc0462018-03-20 23:33:24 -0400559// Create copy propagate arrays pass.
560// This pass looks to copy propagate memory references for arrays. It looks
561// for specific code patterns to recognize array copies.
562Optimizer::PassToken CreateCopyPropagateArraysPass();
Lei Zhangf18e1f22016-09-12 14:11:46 -0400563} // namespace spvtools
564
565#endif // SPIRV_TOOLS_OPTIMIZER_HPP_