2016-09-12 18:11:46 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_
|
|
|
|
#define INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_
|
2016-09-12 18:11:46 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2018-01-04 17:59:50 +00:00
|
|
|
#include <ostream>
|
2016-09-12 18:11:46 +00:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2021-08-18 12:30:48 +00:00
|
|
|
#include <utility>
|
2016-09-12 18:11:46 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "libspirv.hpp"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
|
2018-05-22 21:31:26 +00:00
|
|
|
namespace opt {
|
|
|
|
class Pass;
|
2021-08-18 12:30:48 +00:00
|
|
|
struct DescriptorSetAndBinding;
|
2021-09-15 18:38:34 +00:00
|
|
|
} // namespace opt
|
2018-05-22 21:31:26 +00:00
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
// C++ interface for SPIR-V optimization functionalities. It wraps the context
|
|
|
|
// (including target environment and the corresponding SPIR-V grammar) and
|
|
|
|
// provides methods for registering optimization passes and optimizing.
|
|
|
|
//
|
|
|
|
// Instances of this class provides basic thread-safety guarantee.
|
|
|
|
class Optimizer {
|
|
|
|
public:
|
|
|
|
// The token for an optimization pass. It is returned via one of the
|
|
|
|
// Create*Pass() standalone functions at the end of this header file and
|
|
|
|
// consumed by the RegisterPass() method. Tokens are one-time objects that
|
|
|
|
// only support move; copying is not allowed.
|
|
|
|
struct PassToken {
|
2022-01-26 20:13:08 +00:00
|
|
|
struct Impl; // Opaque struct for holding internal data.
|
2016-09-12 18:11:46 +00:00
|
|
|
|
|
|
|
PassToken(std::unique_ptr<Impl>);
|
|
|
|
|
2018-05-22 21:31:26 +00:00
|
|
|
// Tokens for built-in passes should be created using Create*Pass functions
|
|
|
|
// below; for out-of-tree passes, use this constructor instead.
|
|
|
|
// Note that this API isn't guaranteed to be stable and may change without
|
|
|
|
// preserving source or binary compatibility in the future.
|
|
|
|
PassToken(std::unique_ptr<opt::Pass>&& pass);
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
// Tokens can only be moved. Copying is disabled.
|
|
|
|
PassToken(const PassToken&) = delete;
|
|
|
|
PassToken(PassToken&&);
|
|
|
|
PassToken& operator=(const PassToken&) = delete;
|
|
|
|
PassToken& operator=(PassToken&&);
|
|
|
|
|
|
|
|
~PassToken();
|
|
|
|
|
|
|
|
std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
|
|
|
|
};
|
|
|
|
|
|
|
|
// Constructs an instance with the given target |env|, which is used to decode
|
|
|
|
// the binaries to be optimized later.
|
|
|
|
//
|
2020-01-24 21:26:07 +00:00
|
|
|
// The instance will have an empty message consumer, which ignores all
|
|
|
|
// messages from the library. Use SetMessageConsumer() to supply a consumer
|
|
|
|
// if messages are of concern.
|
2016-09-12 18:11:46 +00:00
|
|
|
explicit Optimizer(spv_target_env env);
|
|
|
|
|
|
|
|
// Disables copy/move constructor/assignment operations.
|
|
|
|
Optimizer(const Optimizer&) = delete;
|
|
|
|
Optimizer(Optimizer&&) = delete;
|
|
|
|
Optimizer& operator=(const Optimizer&) = delete;
|
|
|
|
Optimizer& operator=(Optimizer&&) = delete;
|
|
|
|
|
|
|
|
// Destructs this instance.
|
|
|
|
~Optimizer();
|
|
|
|
|
|
|
|
// Sets the message consumer to the given |consumer|. The |consumer| will be
|
|
|
|
// invoked once for each message communicated from the library.
|
|
|
|
void SetMessageConsumer(MessageConsumer consumer);
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
// Returns a reference to the registered message consumer.
|
|
|
|
const MessageConsumer& consumer() const;
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
// Registers the given |pass| to this optimizer. Passes will be run in the
|
|
|
|
// exact order of registration. The token passed in will be consumed by this
|
|
|
|
// method.
|
|
|
|
Optimizer& RegisterPass(PassToken&& pass);
|
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
// Registers passes that attempt to improve performance of generated code.
|
|
|
|
// This sequence of passes is subject to constant review and will change
|
|
|
|
// from time to time.
|
|
|
|
Optimizer& RegisterPerformancePasses();
|
|
|
|
|
|
|
|
// Registers passes that attempt to improve the size of generated code.
|
|
|
|
// This sequence of passes is subject to constant review and will change
|
|
|
|
// from time to time.
|
|
|
|
Optimizer& RegisterSizePasses();
|
|
|
|
|
2017-11-17 21:50:43 +00:00
|
|
|
// Registers passes that attempt to legalize the generated code.
|
|
|
|
//
|
2018-07-25 19:21:44 +00:00
|
|
|
// Note: this recipe is specially designed for legalizing SPIR-V. It should be
|
|
|
|
// used by compilers after translating HLSL source code literally. It should
|
2017-11-17 21:50:43 +00:00
|
|
|
// *not* be used by general workloads for performance or size improvement.
|
|
|
|
//
|
|
|
|
// This sequence of passes is subject to constant review and will change
|
|
|
|
// from time to time.
|
|
|
|
Optimizer& RegisterLegalizationPasses();
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
// Register passes specified in the list of |flags|. Each flag must be a
|
|
|
|
// string of a form accepted by Optimizer::FlagHasValidForm().
|
|
|
|
//
|
|
|
|
// If the list of flags contains an invalid entry, it returns false and an
|
|
|
|
// error message is emitted to the MessageConsumer object (use
|
|
|
|
// Optimizer::SetMessageConsumer to define a message consumer, if needed).
|
|
|
|
//
|
|
|
|
// If all the passes are registered successfully, it returns true.
|
|
|
|
bool RegisterPassesFromFlags(const std::vector<std::string>& flags);
|
|
|
|
|
|
|
|
// Registers the optimization pass associated with |flag|. This only accepts
|
|
|
|
// |flag| values of the form "--pass_name[=pass_args]". If no such pass
|
|
|
|
// exists, it returns false. Otherwise, the pass is registered and it returns
|
|
|
|
// true.
|
|
|
|
//
|
|
|
|
// The following flags have special meaning:
|
|
|
|
//
|
|
|
|
// -O: Registers all performance optimization passes
|
|
|
|
// (Optimizer::RegisterPerformancePasses)
|
|
|
|
//
|
|
|
|
// -Os: Registers all size optimization passes
|
|
|
|
// (Optimizer::RegisterSizePasses).
|
|
|
|
//
|
|
|
|
// --legalize-hlsl: Registers all passes that legalize SPIR-V generated by an
|
|
|
|
// HLSL front-end.
|
|
|
|
bool RegisterPassFromFlag(const std::string& flag);
|
|
|
|
|
|
|
|
// Validates that |flag| has a valid format. Strings accepted:
|
|
|
|
//
|
|
|
|
// --pass_name[=pass_args]
|
|
|
|
// -O
|
|
|
|
// -Os
|
|
|
|
//
|
|
|
|
// If |flag| takes one of the forms above, it returns true. Otherwise, it
|
|
|
|
// returns false.
|
|
|
|
bool FlagHasValidForm(const std::string& flag) const;
|
|
|
|
|
2018-12-17 21:54:23 +00:00
|
|
|
// Allows changing, after creation time, the target environment to be
|
2020-01-24 21:26:07 +00:00
|
|
|
// optimized for and validated. Should be called before calling Run().
|
2018-12-17 21:54:23 +00:00
|
|
|
void SetTargetEnv(const spv_target_env env);
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
// Optimizes the given SPIR-V module |original_binary| and writes the
|
2020-01-24 21:26:07 +00:00
|
|
|
// optimized binary into |optimized_binary|. The optimized binary uses
|
|
|
|
// the same SPIR-V version as the original binary.
|
|
|
|
//
|
2016-09-12 18:11:46 +00:00
|
|
|
// Returns true on successful optimization, whether or not the module is
|
2018-08-13 17:18:46 +00:00
|
|
|
// modified. Returns false if |original_binary| fails to validate or if errors
|
|
|
|
// occur when processing |original_binary| using any of the registered passes.
|
|
|
|
// In that case, no further passes are executed and the contents in
|
|
|
|
// |optimized_binary| may be invalid.
|
2016-09-12 18:11:46 +00:00
|
|
|
//
|
2020-01-24 21:26:07 +00:00
|
|
|
// By default, the binary is validated before any transforms are performed,
|
|
|
|
// and optionally after each transform. Validation uses SPIR-V spec rules
|
|
|
|
// for the SPIR-V version named in the binary's header (at word offset 1).
|
|
|
|
// Additionally, if the target environment is a client API (such as
|
|
|
|
// Vulkan 1.1), then validate for that client API version, to the extent
|
|
|
|
// that it is verifiable from data in the binary itself.
|
|
|
|
//
|
2016-09-12 18:11:46 +00:00
|
|
|
// It's allowed to alias |original_binary| to the start of |optimized_binary|.
|
|
|
|
bool Run(const uint32_t* original_binary, size_t original_binary_size,
|
|
|
|
std::vector<uint32_t>* optimized_binary) const;
|
2018-08-13 17:18:46 +00:00
|
|
|
|
2018-09-10 15:49:41 +00:00
|
|
|
// DEPRECATED: Same as above, except passes |options| to the validator when
|
|
|
|
// trying to validate the binary. If |skip_validation| is true, then the
|
|
|
|
// caller is guaranteeing that |original_binary| is valid, and the validator
|
|
|
|
// will not be run. The |max_id_bound| is the limit on the max id in the
|
|
|
|
// module.
|
2018-08-13 17:18:46 +00:00
|
|
|
bool Run(const uint32_t* original_binary, const size_t original_binary_size,
|
2018-08-08 15:16:19 +00:00
|
|
|
std::vector<uint32_t>* optimized_binary,
|
2018-09-10 15:49:41 +00:00
|
|
|
const ValidatorOptions& options, bool skip_validation) const;
|
|
|
|
|
|
|
|
// Same as above, except it takes an options object. See the documentation
|
|
|
|
// for |OptimizerOptions| to see which options can be set.
|
2020-01-24 21:26:07 +00:00
|
|
|
//
|
|
|
|
// By default, the binary is validated before any transforms are performed,
|
|
|
|
// and optionally after each transform. Validation uses SPIR-V spec rules
|
|
|
|
// for the SPIR-V version named in the binary's header (at word offset 1).
|
|
|
|
// Additionally, if the target environment is a client API (such as
|
|
|
|
// Vulkan 1.1), then validate for that client API version, to the extent
|
|
|
|
// that it is verifiable from data in the binary itself, or from the
|
|
|
|
// validator options set on the optimizer options.
|
2018-09-10 15:49:41 +00:00
|
|
|
bool Run(const uint32_t* original_binary, const size_t original_binary_size,
|
|
|
|
std::vector<uint32_t>* optimized_binary,
|
|
|
|
const spv_optimizer_options opt_options) const;
|
2016-09-12 18:11:46 +00:00
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
// Returns a vector of strings with all the pass names added to this
|
|
|
|
// optimizer's pass manager. These strings are valid until the associated
|
|
|
|
// pass manager is destroyed.
|
2017-10-20 16:17:41 +00:00
|
|
|
std::vector<const char*> GetPassNames() const;
|
2017-08-30 18:19:22 +00:00
|
|
|
|
2018-01-04 17:59:50 +00:00
|
|
|
// Sets the option to print the disassembly before each pass and after the
|
|
|
|
// last pass. If |out| is null, then no output is generated. Otherwise,
|
|
|
|
// output is sent to the |out| output stream.
|
|
|
|
Optimizer& SetPrintAll(std::ostream* out);
|
|
|
|
|
2018-03-07 14:25:51 +00:00
|
|
|
// Sets the option to print the resource utilization of each pass. If |out|
|
|
|
|
// is null, then no output is generated. Otherwise, output is sent to the
|
|
|
|
// |out| output stream.
|
|
|
|
Optimizer& SetTimeReport(std::ostream* out);
|
|
|
|
|
2019-03-26 18:38:59 +00:00
|
|
|
// Sets the option to validate the module after each pass.
|
|
|
|
Optimizer& SetValidateAfterAll(bool validate);
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
private:
|
|
|
|
struct Impl; // Opaque struct for holding internal data.
|
|
|
|
std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creates a null pass.
|
|
|
|
// A null pass does nothing to the SPIR-V module to be optimized.
|
|
|
|
Optimizer::PassToken CreateNullPass();
|
|
|
|
|
|
|
|
// Creates a strip-debug-info pass.
|
|
|
|
// A strip-debug-info pass removes all debug instructions (as documented in
|
2022-01-26 20:13:47 +00:00
|
|
|
// Section 3.42.2 of the SPIR-V spec) of the SPIR-V module to be optimized.
|
2016-09-12 18:11:46 +00:00
|
|
|
Optimizer::PassToken CreateStripDebugInfoPass();
|
|
|
|
|
2021-12-15 14:55:30 +00:00
|
|
|
// [Deprecated] This will create a strip-nonsemantic-info pass. See below.
|
2018-03-09 21:08:57 +00:00
|
|
|
Optimizer::PassToken CreateStripReflectInfoPass();
|
|
|
|
|
2021-12-15 14:55:30 +00:00
|
|
|
// Creates a strip-nonsemantic-info pass.
|
|
|
|
// A strip-nonsemantic-info pass removes all reflections and explicitly
|
|
|
|
// non-semantic instructions.
|
|
|
|
Optimizer::PassToken CreateStripNonSemanticInfoPass();
|
|
|
|
|
2017-09-19 14:12:13 +00:00
|
|
|
// Creates an eliminate-dead-functions pass.
|
2017-10-20 16:17:41 +00:00
|
|
|
// An eliminate-dead-functions pass will remove all functions that are not in
|
|
|
|
// the call trees rooted at entry points and exported functions. These
|
|
|
|
// functions are not needed because they will never be called.
|
2017-09-19 14:12:13 +00:00
|
|
|
Optimizer::PassToken CreateEliminateDeadFunctionsPass();
|
|
|
|
|
2019-02-14 18:42:35 +00:00
|
|
|
// Creates an eliminate-dead-members pass.
|
|
|
|
// An eliminate-dead-members pass will remove all unused members of structures.
|
|
|
|
// This will not affect the data layout of the remaining members.
|
|
|
|
Optimizer::PassToken CreateEliminateDeadMembersPass();
|
|
|
|
|
2017-04-19 22:10:59 +00:00
|
|
|
// Creates a set-spec-constant-default-value pass from a mapping from spec-ids
|
|
|
|
// to the default values in the form of string.
|
2016-09-12 18:11:46 +00:00
|
|
|
// A set-spec-constant-default-value pass sets the default values for the
|
|
|
|
// spec constants that have SpecId decorations (i.e., those defined by
|
|
|
|
// OpSpecConstant{|True|False} instructions).
|
|
|
|
Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
|
|
|
|
const std::unordered_map<uint32_t, std::string>& id_value_map);
|
|
|
|
|
2017-04-19 22:10:59 +00:00
|
|
|
// Creates a set-spec-constant-default-value pass from a mapping from spec-ids
|
|
|
|
// to the default values in the form of bit pattern.
|
|
|
|
// A set-spec-constant-default-value pass sets the default values for the
|
|
|
|
// spec constants that have SpecId decorations (i.e., those defined by
|
|
|
|
// OpSpecConstant{|True|False} instructions).
|
|
|
|
Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
|
|
|
|
const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map);
|
|
|
|
|
2017-04-01 20:10:16 +00:00
|
|
|
// Creates a flatten-decoration pass.
|
|
|
|
// A flatten-decoration pass replaces grouped decorations with equivalent
|
|
|
|
// ungrouped decorations. That is, it replaces each OpDecorationGroup
|
|
|
|
// instruction and associated OpGroupDecorate and OpGroupMemberDecorate
|
|
|
|
// instructions with equivalent OpDecorate and OpMemberDecorate instructions.
|
|
|
|
// The pass does not attempt to preserve debug information for instructions
|
|
|
|
// it removes.
|
|
|
|
Optimizer::PassToken CreateFlattenDecorationPass();
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
// Creates a freeze-spec-constant-value pass.
|
|
|
|
// A freeze-spec-constant pass specializes the value of spec constants to
|
|
|
|
// their default values. This pass only processes the spec constants that have
|
|
|
|
// SpecId decorations (defined by OpSpecConstant, OpSpecConstantTrue, or
|
|
|
|
// OpSpecConstantFalse instructions) and replaces them with their normal
|
|
|
|
// counterparts (OpConstant, OpConstantTrue, or OpConstantFalse). The
|
|
|
|
// corresponding SpecId annotation instructions will also be removed. This
|
|
|
|
// pass does not fold the newly added normal constants and does not process
|
|
|
|
// other spec constants defined by OpSpecConstantComposite or
|
|
|
|
// OpSpecConstantOp.
|
|
|
|
Optimizer::PassToken CreateFreezeSpecConstantValuePass();
|
|
|
|
|
|
|
|
// Creates a fold-spec-constant-op-and-composite pass.
|
|
|
|
// A fold-spec-constant-op-and-composite pass folds spec constants defined by
|
|
|
|
// OpSpecConstantOp or OpSpecConstantComposite instruction, to normal Constants
|
|
|
|
// defined by OpConstantTrue, OpConstantFalse, OpConstant, OpConstantNull, or
|
|
|
|
// OpConstantComposite instructions. Note that spec constants defined with
|
|
|
|
// OpSpecConstant, OpSpecConstantTrue, or OpSpecConstantFalse instructions are
|
|
|
|
// not handled, as these instructions indicate their value are not determined
|
|
|
|
// and can be changed in future. A spec constant is foldable if all of its
|
|
|
|
// value(s) can be determined from the module. E.g., an integer spec constant
|
|
|
|
// defined with OpSpecConstantOp instruction can be folded if its value won't
|
2022-01-26 20:13:08 +00:00
|
|
|
// change later. This pass will replace the original OpSpecConstantOp
|
|
|
|
// instruction with an OpConstant instruction. When folding composite spec
|
|
|
|
// constants, new instructions may be inserted to define the components of the
|
|
|
|
// composite constant first, then the original spec constants will be replaced
|
|
|
|
// by OpConstantComposite instructions.
|
2016-09-12 18:11:46 +00:00
|
|
|
//
|
|
|
|
// There are some operations not supported yet:
|
|
|
|
// OpSConvert, OpFConvert, OpQuantizeToF16 and
|
|
|
|
// all the operations under Kernel capability.
|
|
|
|
// TODO(qining): Add support for the operations listed above.
|
|
|
|
Optimizer::PassToken CreateFoldSpecConstantOpAndCompositePass();
|
|
|
|
|
|
|
|
// Creates a unify-constant pass.
|
|
|
|
// A unify-constant pass de-duplicates the constants. Constants with the exact
|
|
|
|
// same value and identical form will be unified and only one constant will
|
|
|
|
// be kept for each unique pair of type and value.
|
|
|
|
// There are several cases not handled by this pass:
|
|
|
|
// 1) Constants defined by OpConstantNull instructions (null constants) and
|
|
|
|
// constants defined by OpConstantFalse, OpConstant or OpConstantComposite
|
|
|
|
// with value 0 (zero-valued normal constants) are not considered equivalent.
|
|
|
|
// So null constants won't be used to replace zero-valued normal constants,
|
|
|
|
// vice versa.
|
|
|
|
// 2) Whenever there are decorations to the constant's result id id, the
|
|
|
|
// constant won't be handled, which means, it won't be used to replace any
|
|
|
|
// other constants, neither can other constants replace it.
|
|
|
|
// 3) NaN in float point format with different bit patterns are not unified.
|
|
|
|
Optimizer::PassToken CreateUnifyConstantPass();
|
|
|
|
|
|
|
|
// Creates a eliminate-dead-constant pass.
|
|
|
|
// A eliminate-dead-constant pass removes dead constants, including normal
|
2022-01-26 20:13:08 +00:00
|
|
|
// constants defined by OpConstant, OpConstantComposite, OpConstantTrue, or
|
2016-09-12 18:11:46 +00:00
|
|
|
// OpConstantFalse and spec constants defined by OpSpecConstant,
|
|
|
|
// OpSpecConstantComposite, OpSpecConstantTrue, OpSpecConstantFalse or
|
|
|
|
// OpSpecConstantOp.
|
|
|
|
Optimizer::PassToken CreateEliminateDeadConstantPass();
|
|
|
|
|
2017-09-08 16:08:03 +00:00
|
|
|
// Creates a strength-reduction pass.
|
|
|
|
// A strength-reduction pass will look for opportunities to replace an
|
|
|
|
// instruction with an equivalent and less expensive one. For example,
|
|
|
|
// multiplying by a power of 2 can be replaced by a bit shift.
|
|
|
|
Optimizer::PassToken CreateStrengthReductionPass();
|
|
|
|
|
2017-06-07 21:28:53 +00:00
|
|
|
// Creates a block merge pass.
|
|
|
|
// This pass searches for blocks with a single Branch to a block with no
|
|
|
|
// other predecessors and merges the blocks into a single block. Continue
|
|
|
|
// blocks and Merge blocks are not candidates for the second block.
|
|
|
|
//
|
|
|
|
// The pass is most useful after Dead Branch Elimination, which can leave
|
|
|
|
// such sequences of blocks. Merging them makes subsequent passes more
|
|
|
|
// effective, such as single block local store-load elimination.
|
|
|
|
//
|
|
|
|
// While this pass reduces the number of occurrences of this sequence, at
|
|
|
|
// this time it does not guarantee all such sequences are eliminated.
|
|
|
|
//
|
|
|
|
// Presence of phi instructions can inhibit this optimization. Handling
|
2017-09-19 14:12:13 +00:00
|
|
|
// these is left for future improvements.
|
2017-06-07 21:28:53 +00:00
|
|
|
Optimizer::PassToken CreateBlockMergePass();
|
|
|
|
|
2017-08-15 23:58:28 +00:00
|
|
|
// Creates an exhaustive inline pass.
|
|
|
|
// An exhaustive inline pass attempts to exhaustively inline all function
|
|
|
|
// calls in all functions in an entry point call tree. The intent is to enable,
|
|
|
|
// albeit through brute force, analysis and optimization across function
|
|
|
|
// calls by subsequent optimization passes. As the inlining is exhaustive,
|
|
|
|
// there is no attempt to optimize for size or runtime performance. Functions
|
|
|
|
// that are not in the call tree of an entry point are not changed.
|
2017-08-01 23:20:13 +00:00
|
|
|
Optimizer::PassToken CreateInlineExhaustivePass();
|
2017-09-19 14:12:13 +00:00
|
|
|
|
2017-08-15 23:58:28 +00:00
|
|
|
// Creates an opaque inline pass.
|
|
|
|
// An opaque inline pass inlines all function calls in all functions in all
|
|
|
|
// entry point call trees where the called function contains an opaque type
|
|
|
|
// in either its parameter types or return type. An opaque type is currently
|
|
|
|
// defined as Image, Sampler or SampledImage. The intent is to enable, albeit
|
|
|
|
// through brute force, analysis and optimization across these function calls
|
|
|
|
// by subsequent passes in order to remove the storing of opaque types which is
|
|
|
|
// not legal in Vulkan. Functions that are not in the call tree of an entry
|
|
|
|
// point are not changed.
|
|
|
|
Optimizer::PassToken CreateInlineOpaquePass();
|
2017-09-19 14:12:13 +00:00
|
|
|
|
2017-05-18 20:51:55 +00:00
|
|
|
// Creates a single-block local variable load/store elimination pass.
|
2017-09-19 14:12:13 +00:00
|
|
|
// For every entry point function, do single block memory optimization of
|
2017-05-18 20:51:55 +00:00
|
|
|
// function variables referenced only with non-access-chain loads and stores.
|
|
|
|
// For each targeted variable load, if previous store to that variable in the
|
|
|
|
// block, replace the load's result id with the value id of the store.
|
|
|
|
// If previous load within the block, replace the current load's result id
|
|
|
|
// with the previous load's result id. In either case, delete the current
|
|
|
|
// load. Finally, check if any remaining stores are useless, and delete store
|
|
|
|
// and variable if possible.
|
|
|
|
//
|
|
|
|
// The presence of access chain references and function calls can inhibit
|
|
|
|
// the above optimization.
|
|
|
|
//
|
2017-12-11 18:10:24 +00:00
|
|
|
// Only modules with relaxed logical addressing (see opt/instruction.h) are
|
|
|
|
// currently processed.
|
2017-05-18 20:51:55 +00:00
|
|
|
//
|
2022-01-26 20:13:08 +00:00
|
|
|
// This pass is most effective if preceded by Inlining and
|
2017-05-18 20:51:55 +00:00
|
|
|
// LocalAccessChainConvert. This pass will reduce the work needed to be done
|
2017-06-16 21:37:31 +00:00
|
|
|
// by LocalSingleStoreElim and LocalMultiStoreElim.
|
2017-08-15 23:58:28 +00:00
|
|
|
//
|
|
|
|
// Only functions in the call tree of an entry point are processed.
|
2017-05-18 20:51:55 +00:00
|
|
|
Optimizer::PassToken CreateLocalSingleBlockLoadStoreElimPass();
|
2016-11-10 17:11:50 +00:00
|
|
|
|
2017-06-02 19:23:20 +00:00
|
|
|
// Create dead branch elimination pass.
|
|
|
|
// For each entry point function, this pass will look for SelectionMerge
|
|
|
|
// BranchConditionals with constant condition and convert to a Branch to
|
|
|
|
// the indicated label. It will delete resulting dead blocks.
|
|
|
|
//
|
2017-10-05 20:26:09 +00:00
|
|
|
// For all phi functions in merge block, replace all uses with the id
|
|
|
|
// corresponding to the living predecessor.
|
|
|
|
//
|
2018-01-04 22:04:03 +00:00
|
|
|
// Note that some branches and blocks may be left to avoid creating invalid
|
|
|
|
// control flow. Improving this is left to future work.
|
2017-06-02 19:23:20 +00:00
|
|
|
//
|
2022-01-26 20:13:08 +00:00
|
|
|
// This pass is most effective when preceded by passes which eliminate
|
2017-06-02 19:23:20 +00:00
|
|
|
// local loads and stores, effectively propagating constant values where
|
|
|
|
// possible.
|
|
|
|
Optimizer::PassToken CreateDeadBranchElimPass();
|
|
|
|
|
2017-06-16 21:37:31 +00:00
|
|
|
// Creates an SSA local variable load/store elimination pass.
|
|
|
|
// For every entry point function, eliminate all loads and stores of function
|
|
|
|
// scope variables only referenced with non-access-chain loads and stores.
|
2017-09-19 14:12:13 +00:00
|
|
|
// Eliminate the variables as well.
|
2017-06-16 21:37:31 +00:00
|
|
|
//
|
|
|
|
// The presence of access chain references and function calls can inhibit
|
|
|
|
// the above optimization.
|
|
|
|
//
|
2017-12-11 18:10:24 +00:00
|
|
|
// Only shader modules with relaxed logical addressing (see opt/instruction.h)
|
|
|
|
// are currently processed. Currently modules with any extensions enabled are
|
|
|
|
// not processed. This is left for future work.
|
2017-06-16 21:37:31 +00:00
|
|
|
//
|
2022-01-26 20:13:08 +00:00
|
|
|
// This pass is most effective if preceded by Inlining and
|
2017-06-16 21:37:31 +00:00
|
|
|
// LocalAccessChainConvert. LocalSingleStoreElim and LocalSingleBlockElim
|
|
|
|
// will reduce the work that this pass has to do.
|
|
|
|
Optimizer::PassToken CreateLocalMultiStoreElimPass();
|
|
|
|
|
2017-05-12 23:27:21 +00:00
|
|
|
// Creates a local access chain conversion pass.
|
|
|
|
// A local access chain conversion pass identifies all function scope
|
|
|
|
// variables which are accessed only with loads, stores and access chains
|
|
|
|
// with constant indices. It then converts all loads and stores of such
|
|
|
|
// variables into equivalent sequences of loads, stores, extracts and inserts.
|
|
|
|
//
|
|
|
|
// This pass only processes entry point functions. It currently only converts
|
|
|
|
// non-nested, non-ptr access chains. It does not process modules with
|
|
|
|
// non-32-bit integer types present. Optional memory access options on loads
|
|
|
|
// and stores are ignored as we are only processing function scope variables.
|
|
|
|
//
|
|
|
|
// This pass unifies access to these variables to a single mode and simplifies
|
|
|
|
// subsequent analysis and elimination of these variables along with their
|
|
|
|
// loads and stores allowing values to propagate to their points of use where
|
|
|
|
// possible.
|
|
|
|
Optimizer::PassToken CreateLocalAccessChainConvertPass();
|
|
|
|
|
2017-05-19 23:31:28 +00:00
|
|
|
// Creates a local single store elimination pass.
|
2017-09-19 14:12:13 +00:00
|
|
|
// For each entry point function, this pass eliminates loads and stores for
|
2017-05-19 23:31:28 +00:00
|
|
|
// function scope variable that are stored to only once, where possible. Only
|
|
|
|
// whole variable loads and stores are eliminated; access-chain references are
|
|
|
|
// not optimized. Replace all loads of such variables with the value that is
|
|
|
|
// stored and eliminate any resulting dead code.
|
|
|
|
//
|
|
|
|
// Currently, the presence of access chains and function calls can inhibit this
|
|
|
|
// pass, however the Inlining and LocalAccessChainConvert passes can make it
|
|
|
|
// more effective. In additional, many non-load/store memory operations are
|
|
|
|
// not supported and will prohibit optimization of a function. Support of
|
|
|
|
// these operations are future work.
|
|
|
|
//
|
2017-12-11 18:10:24 +00:00
|
|
|
// Only shader modules with relaxed logical addressing (see opt/instruction.h)
|
|
|
|
// are currently processed.
|
|
|
|
//
|
2017-05-19 23:31:28 +00:00
|
|
|
// This pass will reduce the work needed to be done by LocalSingleBlockElim
|
2017-06-16 21:37:31 +00:00
|
|
|
// and LocalMultiStoreElim and can improve the effectiveness of other passes
|
|
|
|
// such as DeadBranchElimination which depend on values for their analysis.
|
2017-05-19 23:31:28 +00:00
|
|
|
Optimizer::PassToken CreateLocalSingleStoreElimPass();
|
|
|
|
|
2017-05-26 16:33:11 +00:00
|
|
|
// Creates an insert/extract elimination pass.
|
|
|
|
// This pass processes each entry point function in the module, searching for
|
|
|
|
// extracts on a sequence of inserts. It further searches the sequence for an
|
|
|
|
// insert with indices identical to the extract. If such an insert can be
|
|
|
|
// found before hitting a conflicting insert, the extract's result id is
|
|
|
|
// replaced with the id of the values from the insert.
|
|
|
|
//
|
|
|
|
// Besides removing extracts this pass enables subsequent dead code elimination
|
|
|
|
// passes to delete the inserts. This pass performs best after access chains are
|
|
|
|
// converted to inserts and extracts and local loads and stores are eliminated.
|
|
|
|
Optimizer::PassToken CreateInsertExtractElimPass();
|
|
|
|
|
2018-01-27 00:05:33 +00:00
|
|
|
// Creates a dead insert elimination pass.
|
|
|
|
// This pass processes each entry point function in the module, searching for
|
|
|
|
// unreferenced inserts into composite types. These are most often unused
|
|
|
|
// stores to vector components. They are unused because they are never
|
|
|
|
// referenced, or because there is another insert to the same component between
|
|
|
|
// the insert and the reference. After removing the inserts, dead code
|
|
|
|
// elimination is attempted on the inserted values.
|
|
|
|
//
|
|
|
|
// This pass performs best after access chains are converted to inserts and
|
|
|
|
// extracts and local loads and stores are eliminated. While executing this
|
|
|
|
// pass can be advantageous on its own, it is also advantageous to execute
|
|
|
|
// this pass after CreateInsertExtractPass() as it will remove any unused
|
|
|
|
// inserts created by that pass.
|
|
|
|
Optimizer::PassToken CreateDeadInsertElimPass();
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
// Create aggressive dead code elimination pass
|
2017-12-18 17:13:10 +00:00
|
|
|
// This pass eliminates unused code from the module. In addition,
|
2017-06-08 16:37:21 +00:00
|
|
|
// it detects and eliminates code which may have spurious uses but which do
|
|
|
|
// not contribute to the output of the function. The most common cause of
|
|
|
|
// such code sequences is summations in loops whose result is no longer used
|
|
|
|
// due to dead code elimination. This optimization has additional compile
|
|
|
|
// time cost over standard dead code elimination.
|
|
|
|
//
|
|
|
|
// This pass only processes entry point functions. It also only processes
|
2017-12-18 17:13:10 +00:00
|
|
|
// shaders with relaxed logical addressing (see opt/instruction.h). It
|
|
|
|
// currently will not process functions with function calls. Unreachable
|
|
|
|
// functions are deleted.
|
2017-06-08 16:37:21 +00:00
|
|
|
//
|
|
|
|
// This pass will be made more effective by first running passes that remove
|
|
|
|
// dead control flow and inlines function calls.
|
|
|
|
//
|
|
|
|
// This pass can be especially useful after running Local Access Chain
|
|
|
|
// Conversion, which tends to cause cycles of dead code to be left after
|
|
|
|
// Store/Load elimination passes are completed. These cycles cannot be
|
|
|
|
// eliminated with standard dead code elimination.
|
2021-09-15 18:38:34 +00:00
|
|
|
//
|
|
|
|
// If |preserve_interface| is true, all non-io variables in the entry point
|
|
|
|
// interface are considered live and are not eliminated. This mode is needed
|
|
|
|
// by GPU-Assisted validation instrumentation, where a change in the interface
|
|
|
|
// is not allowed.
|
2021-12-09 20:58:53 +00:00
|
|
|
Optimizer::PassToken CreateAggressiveDCEPass();
|
|
|
|
Optimizer::PassToken CreateAggressiveDCEPass(bool preserve_interface);
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2021-06-29 15:33:58 +00:00
|
|
|
// Creates a remove-unused-interface-variables pass.
|
|
|
|
// Removes variables referenced on the |OpEntryPoint| instruction that are not
|
|
|
|
// referenced in the entry point function or any function in its call tree. Note
|
|
|
|
// that this could cause the shader interface to no longer match other shader
|
|
|
|
// stages.
|
|
|
|
Optimizer::PassToken CreateRemoveUnusedInterfaceVariablesPass();
|
|
|
|
|
2020-10-30 22:03:56 +00:00
|
|
|
// Creates an empty pass.
|
|
|
|
// This is deprecated and will be removed.
|
|
|
|
// TODO(jaebaek): remove this pass after handling glslang's broken unit tests.
|
|
|
|
// https://github.com/KhronosGroup/glslang/pull/2440
|
|
|
|
Optimizer::PassToken CreatePropagateLineInfoPass();
|
|
|
|
|
|
|
|
// Creates an empty pass.
|
|
|
|
// This is deprecated and will be removed.
|
|
|
|
// TODO(jaebaek): remove this pass after handling glslang's broken unit tests.
|
|
|
|
// https://github.com/KhronosGroup/glslang/pull/2440
|
|
|
|
Optimizer::PassToken CreateRedundantLineInfoElimPass();
|
|
|
|
|
2017-04-11 19:11:04 +00:00
|
|
|
// Creates a compact ids pass.
|
|
|
|
// The pass remaps result ids to a compact and gapless range starting from %1.
|
|
|
|
Optimizer::PassToken CreateCompactIdsPass();
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
// Creates a remove duplicate pass.
|
|
|
|
// This pass removes various duplicates:
|
|
|
|
// * duplicate capabilities;
|
|
|
|
// * duplicate extended instruction imports;
|
|
|
|
// * duplicate types;
|
|
|
|
// * duplicate decorations.
|
2017-07-13 00:16:51 +00:00
|
|
|
Optimizer::PassToken CreateRemoveDuplicatesPass();
|
|
|
|
|
2017-09-06 12:56:41 +00:00
|
|
|
// Creates a CFG cleanup pass.
|
|
|
|
// This pass removes cruft from the control flow graph of functions that are
|
|
|
|
// reachable from entry points and exported functions. It currently includes the
|
|
|
|
// following functionality:
|
|
|
|
//
|
|
|
|
// - Removal of unreachable basic blocks.
|
|
|
|
Optimizer::PassToken CreateCFGCleanupPass();
|
|
|
|
|
2017-10-20 16:17:41 +00:00
|
|
|
// Create dead variable elimination pass.
|
|
|
|
// This pass will delete module scope variables, along with their decorations,
|
|
|
|
// that are not referenced.
|
|
|
|
Optimizer::PassToken CreateDeadVariableEliminationPass();
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
// create merge return pass.
|
|
|
|
// changes functions that have multiple return statements so they have a single
|
|
|
|
// return statement.
|
2017-11-08 21:22:10 +00:00
|
|
|
//
|
2018-03-06 16:20:28 +00:00
|
|
|
// for structured control flow it is assumed that the only unreachable blocks in
|
|
|
|
// the function are trivial merge and continue blocks.
|
2017-11-08 21:22:10 +00:00
|
|
|
//
|
2018-03-06 16:20:28 +00:00
|
|
|
// a trivial merge block contains the label and an opunreachable instructions,
|
|
|
|
// nothing else. a trivial continue block contain a label and an opbranch to
|
|
|
|
// the header, nothing else.
|
|
|
|
//
|
|
|
|
// these conditions are guaranteed to be met after running dead-branch
|
|
|
|
// elimination.
|
2017-11-08 21:22:10 +00:00
|
|
|
Optimizer::PassToken CreateMergeReturnPass();
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
// Create value numbering pass.
|
|
|
|
// This pass will look for instructions in the same basic block that compute the
|
|
|
|
// same value, and remove the redundant ones.
|
|
|
|
Optimizer::PassToken CreateLocalRedundancyEliminationPass();
|
2018-01-29 10:39:55 +00:00
|
|
|
|
|
|
|
// Create LICM pass.
|
|
|
|
// This pass will look for invariant instructions inside loops and hoist them to
|
|
|
|
// the loops preheader.
|
|
|
|
Optimizer::PassToken CreateLoopInvariantCodeMotionPass();
|
2017-12-04 17:29:51 +00:00
|
|
|
|
2018-04-23 20:01:12 +00:00
|
|
|
// Creates a loop fission pass.
|
|
|
|
// This pass will split all top level loops whose register pressure exceedes the
|
|
|
|
// given |threshold|.
|
|
|
|
Optimizer::PassToken CreateLoopFissionPass(size_t threshold);
|
|
|
|
|
2018-04-20 14:14:45 +00:00
|
|
|
// Creates a loop fusion pass.
|
|
|
|
// This pass will look for adjacent loops that are compatible and legal to be
|
|
|
|
// fused. The fuse all such loops as long as the register usage for the fused
|
|
|
|
// loop stays under the threshold defined by |max_registers_per_loop|.
|
|
|
|
Optimizer::PassToken CreateLoopFusionPass(size_t max_registers_per_loop);
|
|
|
|
|
2018-03-29 11:22:42 +00:00
|
|
|
// Creates a loop peeling pass.
|
|
|
|
// This pass will look for conditions inside a loop that are true or false only
|
|
|
|
// for the N first or last iteration. For loop with such condition, those N
|
|
|
|
// iterations of the loop will be executed outside of the main loop.
|
|
|
|
// To limit code size explosion, the loop peeling can only happen if the code
|
|
|
|
// size growth for each loop is under |code_growth_threshold|.
|
|
|
|
Optimizer::PassToken CreateLoopPeelingPass();
|
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
// Creates a loop unswitch pass.
|
|
|
|
// This pass will look for loop independent branch conditions and move the
|
|
|
|
// condition out of the loop and version the loop based on the taken branch.
|
|
|
|
// Works best after LICM and local multi store elimination pass.
|
|
|
|
Optimizer::PassToken CreateLoopUnswitchPass();
|
|
|
|
|
2017-12-04 17:29:51 +00:00
|
|
|
// Create global value numbering pass.
|
|
|
|
// This pass will look for instructions where the same value is computed on all
|
|
|
|
// paths leading to the instruction. Those instructions are deleted.
|
|
|
|
Optimizer::PassToken CreateRedundancyEliminationPass();
|
2017-11-30 22:03:06 +00:00
|
|
|
|
|
|
|
// Create scalar replacement pass.
|
|
|
|
// This pass replaces composite function scope variables with variables for each
|
2018-04-16 13:58:00 +00:00
|
|
|
// element if those elements are accessed individually. The parameter is a
|
|
|
|
// limit on the number of members in the composite variable that the pass will
|
|
|
|
// consider replacing.
|
|
|
|
Optimizer::PassToken CreateScalarReplacementPass(uint32_t size_limit = 100);
|
2017-12-11 18:10:24 +00:00
|
|
|
|
|
|
|
// Create a private to local pass.
|
2022-01-26 20:13:08 +00:00
|
|
|
// This pass looks for variables declared in the private storage class that are
|
2017-12-11 18:10:24 +00:00
|
|
|
// used in only one function. Those variables are moved to the function storage
|
|
|
|
// class in the function that they are used.
|
|
|
|
Optimizer::PassToken CreatePrivateToLocalPass();
|
2017-12-05 16:39:25 +00:00
|
|
|
|
|
|
|
// Creates a conditional constant propagation (CCP) pass.
|
|
|
|
// This pass implements the SSA-CCP algorithm in
|
|
|
|
//
|
|
|
|
// Constant propagation with conditional branches,
|
|
|
|
// Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
|
|
|
|
//
|
|
|
|
// Constant values in expressions and conditional jumps are folded and
|
|
|
|
// simplified. This may reduce code size by removing never executed jump targets
|
|
|
|
// and computations with constant operands.
|
|
|
|
Optimizer::PassToken CreateCCPPass();
|
|
|
|
|
2018-01-17 19:57:37 +00:00
|
|
|
// Creates a workaround driver bugs pass. This pass attempts to work around
|
|
|
|
// a known driver bug (issue #1209) by identifying the bad code sequences and
|
|
|
|
// rewriting them.
|
|
|
|
//
|
|
|
|
// Current workaround: Avoid OpUnreachable instructions in loops.
|
|
|
|
Optimizer::PassToken CreateWorkaround1209Pass();
|
|
|
|
|
2018-01-16 16:15:06 +00:00
|
|
|
// Creates a pass that converts if-then-else like assignments into OpSelect.
|
|
|
|
Optimizer::PassToken CreateIfConversionPass();
|
|
|
|
|
2018-01-30 16:24:03 +00:00
|
|
|
// Creates a pass that will replace instructions that are not valid for the
|
|
|
|
// current shader stage by constants. Has no effect on non-shader modules.
|
|
|
|
Optimizer::PassToken CreateReplaceInvalidOpcodePass();
|
|
|
|
|
2018-02-02 16:55:05 +00:00
|
|
|
// Creates a pass that simplifies instructions using the instruction folder.
|
|
|
|
Optimizer::PassToken CreateSimplificationPass();
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
// Create loop unroller pass.
|
2018-02-27 11:50:08 +00:00
|
|
|
// Creates a pass to unroll loops which have the "Unroll" loop control
|
2018-02-14 17:03:12 +00:00
|
|
|
// mask set. The loops must meet a specific criteria in order to be unrolled
|
|
|
|
// safely this criteria is checked before doing the unroll by the
|
|
|
|
// LoopUtils::CanPerformUnroll method. Any loop that does not meet the criteria
|
|
|
|
// won't be unrolled. See CanPerformUnroll LoopUtils.h for more information.
|
2018-02-27 11:50:08 +00:00
|
|
|
Optimizer::PassToken CreateLoopUnrollPass(bool fully_unroll, int factor = 0);
|
2018-02-14 17:03:12 +00:00
|
|
|
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
// Create the SSA rewrite pass.
|
|
|
|
// This pass converts load/store operations on function local variables into
|
|
|
|
// operations on SSA IDs. This allows SSA optimizers to act on these variables.
|
|
|
|
// Only variables that are local to the function and of supported types are
|
|
|
|
// processed (see IsSSATargetVar for details).
|
|
|
|
Optimizer::PassToken CreateSSARewritePass();
|
|
|
|
|
2019-09-03 17:22:13 +00:00
|
|
|
// Create pass to convert relaxed precision instructions to half precision.
|
|
|
|
// This pass converts as many relaxed float32 arithmetic operations to half as
|
|
|
|
// possible. It converts any float32 operands to half if needed. It converts
|
|
|
|
// any resulting half precision values back to float32 as needed. No variables
|
|
|
|
// are changed. No image operations are changed.
|
|
|
|
//
|
2019-12-21 02:08:12 +00:00
|
|
|
// Best if run after function scope store/load and composite operation
|
|
|
|
// eliminations are run. Also best if followed by instruction simplification,
|
|
|
|
// redundancy elimination and DCE.
|
2019-09-03 17:22:13 +00:00
|
|
|
Optimizer::PassToken CreateConvertRelaxedToHalfPass();
|
|
|
|
|
|
|
|
// Create relax float ops pass.
|
|
|
|
// This pass decorates all float32 result instructions with RelaxedPrecision
|
|
|
|
// if not already so decorated.
|
|
|
|
Optimizer::PassToken CreateRelaxFloatOpsPass();
|
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
// Create copy propagate arrays pass.
|
|
|
|
// This pass looks to copy propagate memory references for arrays. It looks
|
|
|
|
// for specific code patterns to recognize array copies.
|
|
|
|
Optimizer::PassToken CreateCopyPropagateArraysPass();
|
2018-04-23 15:13:07 +00:00
|
|
|
|
|
|
|
// Create a vector dce pass.
|
|
|
|
// This pass looks for components of vectors that are unused, and removes them
|
|
|
|
// from the vector. Note this would still leave around lots of dead code that
|
|
|
|
// a pass of ADCE will be able to remove.
|
|
|
|
Optimizer::PassToken CreateVectorDCEPass();
|
|
|
|
|
2018-05-07 16:31:03 +00:00
|
|
|
// Create a pass to reduce the size of loads.
|
|
|
|
// This pass looks for loads of structures where only a few of its members are
|
|
|
|
// used. It replaces the loads feeding an OpExtract with an OpAccessChain and
|
2021-09-02 14:45:51 +00:00
|
|
|
// a load of the specific elements. The parameter is a threshold to determine
|
|
|
|
// whether we have to replace the load or not. If the ratio of the used
|
|
|
|
// components of the load is less than the threshold, we replace the load.
|
|
|
|
Optimizer::PassToken CreateReduceLoadSizePass(
|
|
|
|
double load_replacement_threshold = 0.9);
|
2018-05-07 16:31:03 +00:00
|
|
|
|
2018-07-23 15:23:11 +00:00
|
|
|
// Create a pass to combine chained access chains.
|
|
|
|
// This pass looks for access chains fed by other access chains and combines
|
|
|
|
// them into a single instruction where possible.
|
|
|
|
Optimizer::PassToken CreateCombineAccessChainsPass();
|
|
|
|
|
2018-11-08 18:54:54 +00:00
|
|
|
// Create a pass to instrument bindless descriptor checking
|
|
|
|
// This pass instruments all bindless references to check that descriptor
|
2019-03-19 13:53:43 +00:00
|
|
|
// array indices are inbounds, and if the descriptor indexing extension is
|
|
|
|
// enabled, that the descriptor has been initialized. If the reference is
|
|
|
|
// invalid, a record is written to the debug output buffer (if space allows)
|
|
|
|
// and a null value is returned. This pass is designed to support bindless
|
|
|
|
// validation in the Vulkan validation layers.
|
|
|
|
//
|
|
|
|
// TODO(greg-lunarg): Add support for buffer references. Currently only does
|
|
|
|
// checking for image references.
|
2018-11-08 18:54:54 +00:00
|
|
|
//
|
|
|
|
// Dead code elimination should be run after this pass as the original,
|
|
|
|
// potentially invalid code is not removed and could cause undefined behavior,
|
|
|
|
// including crashes. It may also be beneficial to run Simplification
|
|
|
|
// (ie Constant Propagation), DeadBranchElim and BlockMerge after this pass to
|
|
|
|
// optimize instrument code involving the testing of compile-time constants.
|
|
|
|
// It is also generally recommended that this pass (and all
|
|
|
|
// instrumentation passes) be run after any legalization and optimization
|
|
|
|
// passes. This will give better analysis for the instrumentation and avoid
|
|
|
|
// potentially de-optimizing the instrument code, for example, inlining
|
|
|
|
// the debug record output function throughout the module.
|
|
|
|
//
|
|
|
|
// The instrumentation will read and write buffers in debug
|
|
|
|
// descriptor set |desc_set|. It will write |shader_id| in each output record
|
|
|
|
// to identify the shader module which generated the record.
|
2020-12-01 16:28:16 +00:00
|
|
|
// |desc_length_enable| controls instrumentation of runtime descriptor array
|
|
|
|
// references, |desc_init_enable| controls instrumentation of descriptor
|
|
|
|
// initialization checking, and |buff_oob_enable| controls instrumentation
|
|
|
|
// of storage and uniform buffer bounds checking, all of which require input
|
|
|
|
// buffer support. |texbuff_oob_enable| controls instrumentation of texel
|
|
|
|
// buffers, which does not require input buffer support.
|
2019-02-07 19:00:36 +00:00
|
|
|
Optimizer::PassToken CreateInstBindlessCheckPass(
|
2020-12-01 16:28:16 +00:00
|
|
|
uint32_t desc_set, uint32_t shader_id, bool desc_length_enable = false,
|
|
|
|
bool desc_init_enable = false, bool buff_oob_enable = false,
|
|
|
|
bool texbuff_oob_enable = false);
|
2018-11-08 18:54:54 +00:00
|
|
|
|
2019-08-16 13:18:34 +00:00
|
|
|
// Create a pass to instrument physical buffer address checking
|
|
|
|
// This pass instruments all physical buffer address references to check that
|
|
|
|
// all referenced bytes fall in a valid buffer. If the reference is
|
|
|
|
// invalid, a record is written to the debug output buffer (if space allows)
|
|
|
|
// and a null value is returned. This pass is designed to support buffer
|
|
|
|
// address validation in the Vulkan validation layers.
|
|
|
|
//
|
|
|
|
// Dead code elimination should be run after this pass as the original,
|
|
|
|
// potentially invalid code is not removed and could cause undefined behavior,
|
|
|
|
// including crashes. Instruction simplification would likely also be
|
|
|
|
// beneficial. It is also generally recommended that this pass (and all
|
|
|
|
// instrumentation passes) be run after any legalization and optimization
|
|
|
|
// passes. This will give better analysis for the instrumentation and avoid
|
|
|
|
// potentially de-optimizing the instrument code, for example, inlining
|
|
|
|
// the debug record output function throughout the module.
|
|
|
|
//
|
|
|
|
// The instrumentation will read and write buffers in debug
|
|
|
|
// descriptor set |desc_set|. It will write |shader_id| in each output record
|
|
|
|
// to identify the shader module which generated the record.
|
|
|
|
Optimizer::PassToken CreateInstBuffAddrCheckPass(uint32_t desc_set,
|
2020-05-21 17:10:42 +00:00
|
|
|
uint32_t shader_id);
|
2019-08-16 13:18:34 +00:00
|
|
|
|
2020-03-12 13:19:52 +00:00
|
|
|
// Create a pass to instrument OpDebugPrintf instructions.
|
|
|
|
// This pass replaces all OpDebugPrintf instructions with instructions to write
|
|
|
|
// a record containing the string id and the all specified values into a special
|
|
|
|
// printf output buffer (if space allows). This pass is designed to support
|
|
|
|
// the printf validation in the Vulkan validation layers.
|
|
|
|
//
|
|
|
|
// The instrumentation will write buffers in debug descriptor set |desc_set|.
|
|
|
|
// It will write |shader_id| in each output record to identify the shader
|
|
|
|
// module which generated the record.
|
|
|
|
Optimizer::PassToken CreateInstDebugPrintfPass(uint32_t desc_set,
|
|
|
|
uint32_t shader_id);
|
|
|
|
|
2018-11-30 19:15:51 +00:00
|
|
|
// Create a pass to upgrade to the VulkanKHR memory model.
|
|
|
|
// This pass upgrades the Logical GLSL450 memory model to Logical VulkanKHR.
|
|
|
|
// Additionally, it modifies memory, image, atomic and barrier operations to
|
|
|
|
// conform to that model's requirements.
|
|
|
|
Optimizer::PassToken CreateUpgradeMemoryModelPass();
|
|
|
|
|
2019-01-17 20:56:36 +00:00
|
|
|
// Create a pass to do code sinking. Code sinking is a transformation
|
|
|
|
// where an instruction is moved into a more deeply nested construct.
|
|
|
|
Optimizer::PassToken CreateCodeSinkingPass();
|
|
|
|
|
2019-04-05 17:12:08 +00:00
|
|
|
// Create a pass to fix incorrect storage classes. In order to make code
|
|
|
|
// generation simpler, DXC may generate code where the storage classes do not
|
|
|
|
// match up correctly. This pass will fix the errors that it can.
|
|
|
|
Optimizer::PassToken CreateFixStorageClassPass();
|
|
|
|
|
2019-07-30 23:52:46 +00:00
|
|
|
// Creates a graphics robust access pass.
|
|
|
|
//
|
|
|
|
// This pass injects code to clamp indexed accesses to buffers and internal
|
|
|
|
// arrays, providing guarantees satisfying Vulkan's robustBufferAccess rules.
|
|
|
|
//
|
|
|
|
// TODO(dneto): Clamps coordinates and sample index for pointer calculations
|
|
|
|
// into storage images (OpImageTexelPointer). For an cube array image, it
|
|
|
|
// assumes the maximum layer count times 6 is at most 0xffffffff.
|
|
|
|
//
|
|
|
|
// NOTE: This pass will fail with a message if:
|
|
|
|
// - The module is not a Shader module.
|
|
|
|
// - The module declares VariablePointers, VariablePointersStorageBuffer, or
|
|
|
|
// RuntimeDescriptorArrayEXT capabilities.
|
|
|
|
// - The module uses an addressing model other than Logical
|
|
|
|
// - Access chain indices are wider than 64 bits.
|
|
|
|
// - Access chain index for a struct is not an OpConstant integer or is out
|
|
|
|
// of range. (The module is already invalid if that is the case.)
|
|
|
|
// - TODO(dneto): The OpImageTexelPointer coordinate component is not 32-bits
|
|
|
|
// wide.
|
2019-12-03 16:18:56 +00:00
|
|
|
//
|
|
|
|
// NOTE: Access chain indices are always treated as signed integers. So
|
|
|
|
// if an array has a fixed size of more than 2^31 elements, then elements
|
|
|
|
// from 2^31 and above are never accessible with a 32-bit index,
|
|
|
|
// signed or unsigned. For this case, this pass will clamp the index
|
|
|
|
// between 0 and at 2^31-1, inclusive.
|
|
|
|
// Similarly, if an array has more then 2^15 element and is accessed with
|
|
|
|
// a 16-bit index, then elements from 2^15 and above are not accessible.
|
|
|
|
// In this case, the pass will clamp the index between 0 and 2^15-1
|
|
|
|
// inclusive.
|
2019-07-30 23:52:46 +00:00
|
|
|
Optimizer::PassToken CreateGraphicsRobustAccessPass();
|
|
|
|
|
spirv-opt: add pass to Spread Volatile semantics (#4667)
Add a pass to spread Volatile semantics to variables with SMIDNV,
WarpIDNV, SubgroupSize, SubgroupLocalInvocationId, SubgroupEqMask,
SubgroupGeMask, SubgroupGtMask, SubgroupLeMask, or SubgroupLtMask BuiltIn
decorations or OpLoad for them when the shader model is the ray
generation, closest hit, miss, intersection, or callable shaders. This
pass can be used for VUID-StandaloneSpirv-VulkanMemoryModel-04678 and
VUID-StandaloneSpirv-VulkanMemoryModel-04679 (See "Standalone SPIR-V
Validation" section of Vulkan spec "Appendix A: Vulkan Environment for
SPIR-V").
Handle variables used by multiple entry points:
1. Update error check to make it working regardless of the order of
entry points.
2. For a variable, if it is used by two entry points E1 and E2 and
it needs the Volatile semantics for E1 while it does not for E2
- If VulkanMemoryModel capability is enabled, which means we have to
set memory operation of load instructions for the variable, we
update load instructions in E1, but do not update the ones in E2.
- If VulkanMemoryModel capability is disabled, which means we have
to add Volatile decoration for the variable, we report an error
because E1 needs to add Volatile decoration for the variable while
E2 does not.
For the simplicity of the implementation, we assume that all functions
other than entry point functions are inlined.
2022-01-25 18:14:36 +00:00
|
|
|
// Create a pass to spread Volatile semantics to variables with SMIDNV,
|
|
|
|
// WarpIDNV, SubgroupSize, SubgroupLocalInvocationId, SubgroupEqMask,
|
|
|
|
// SubgroupGeMask, SubgroupGtMask, SubgroupLeMask, or SubgroupLtMask BuiltIn
|
|
|
|
// decorations or OpLoad for them when the shader model is the ray generation,
|
|
|
|
// closest hit, miss, intersection, or callable. This pass can be used for
|
|
|
|
// VUID-StandaloneSpirv-VulkanMemoryModel-04678 and
|
|
|
|
// VUID-StandaloneSpirv-VulkanMemoryModel-04679 (See "Standalone SPIR-V
|
|
|
|
// Validation" section of Vulkan spec "Appendix A: Vulkan Environment for
|
|
|
|
// SPIR-V"). When the SPIR-V version is 1.6 or above, the pass also spreads
|
|
|
|
// the Volatile semantics to a variable with HelperInvocation BuiltIn decoration
|
|
|
|
// in the fragement shader.
|
|
|
|
Optimizer::PassToken CreateSpreadVolatileSemanticsPass();
|
|
|
|
|
2021-10-26 21:20:58 +00:00
|
|
|
// Create a pass to replace a descriptor access using variable index.
|
|
|
|
// This pass replaces every access using a variable index to array variable
|
|
|
|
// |desc| that has a DescriptorSet and Binding decorations with a constant
|
|
|
|
// element of the array. In order to replace the access using a variable index
|
|
|
|
// with the constant element, it uses a switch statement.
|
|
|
|
Optimizer::PassToken CreateReplaceDescArrayAccessUsingVarIndexPass();
|
|
|
|
|
2019-08-08 14:53:19 +00:00
|
|
|
// Create descriptor scalar replacement pass.
|
|
|
|
// This pass replaces every array variable |desc| that has a DescriptorSet and
|
|
|
|
// Binding decorations with a new variable for each element of the array.
|
|
|
|
// Suppose |desc| was bound at binding |b|. Then the variable corresponding to
|
|
|
|
// |desc[i]| will have binding |b+i|. The descriptor set will be the same. It
|
|
|
|
// is assumed that no other variable already has a binding that will used by one
|
|
|
|
// of the new variables. If not, the pass will generate invalid Spir-V. All
|
|
|
|
// accesses to |desc| must be OpAccessChain instructions with a literal index
|
|
|
|
// for the first index.
|
|
|
|
Optimizer::PassToken CreateDescriptorScalarReplacementPass();
|
|
|
|
|
2020-07-22 15:45:02 +00:00
|
|
|
// Create a pass to replace each OpKill instruction with a function call to a
|
|
|
|
// function that has a single OpKill. Also replace each OpTerminateInvocation
|
|
|
|
// instruction with a function call to a function that has a single
|
|
|
|
// OpTerminateInvocation. This allows more code to be inlined.
|
2019-08-14 13:27:12 +00:00
|
|
|
Optimizer::PassToken CreateWrapOpKillPass();
|
|
|
|
|
2019-08-29 16:48:17 +00:00
|
|
|
// Replaces the extensions VK_AMD_shader_ballot,VK_AMD_gcn_shader, and
|
|
|
|
// VK_AMD_shader_trinary_minmax with equivalent code using core instructions and
|
|
|
|
// capabilities.
|
|
|
|
Optimizer::PassToken CreateAmdExtToKhrPass();
|
|
|
|
|
2021-03-31 18:26:36 +00:00
|
|
|
// Replaces the internal version of GLSLstd450 InterpolateAt* extended
|
|
|
|
// instructions with the externally valid version. The internal version allows
|
|
|
|
// an OpLoad of the interpolant for the first argument. This pass removes the
|
|
|
|
// OpLoad and replaces it with its pointer. glslang and possibly other
|
|
|
|
// frontends will create the internal version for HLSL. This pass will be part
|
|
|
|
// of HLSL legalization and should be called after interpolants have been
|
|
|
|
// propagated into their final positions.
|
|
|
|
Optimizer::PassToken CreateInterpolateFixupPass();
|
|
|
|
|
2022-03-23 02:50:52 +00:00
|
|
|
// Removes unused components from composite input variables. Current
|
|
|
|
// implementation just removes trailing unused components from input arrays.
|
|
|
|
// The pass performs best after maximizing dead code removal. A subsequent dead
|
|
|
|
// code elimination pass would be beneficial in removing newly unused component
|
|
|
|
// types.
|
|
|
|
Optimizer::PassToken CreateEliminateDeadInputComponentsPass();
|
|
|
|
|
2021-08-18 12:30:48 +00:00
|
|
|
// Creates a convert-to-sampled-image pass to convert images and/or
|
|
|
|
// samplers with given pairs of descriptor set and binding to sampled image.
|
|
|
|
// If a pair of an image and a sampler have the same pair of descriptor set and
|
|
|
|
// binding that is one of the given pairs, they will be converted to a sampled
|
|
|
|
// image. In addition, if only an image has the descriptor set and binding that
|
|
|
|
// is one of the given pairs, it will be converted to a sampled image as well.
|
|
|
|
Optimizer::PassToken CreateConvertToSampledImagePass(
|
|
|
|
const std::vector<opt::DescriptorSetAndBinding>&
|
|
|
|
descriptor_set_binding_pairs);
|
|
|
|
|
2022-03-07 17:45:17 +00:00
|
|
|
// Creates a remove-dont-inline pass to remove the |DontInline| function control
|
|
|
|
// from every function in the module. This is useful if you want the inliner to
|
|
|
|
// inline these functions some reason.
|
|
|
|
Optimizer::PassToken CreateRemoveDontInlinePass();
|
2016-09-12 18:11:46 +00:00
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // INCLUDE_SPIRV_TOOLS_OPTIMIZER_HPP_
|