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.
|
|
|
|
|
2016-09-16 20:12:04 +00:00
|
|
|
#include "spirv-tools/optimizer.hpp"
|
2016-09-12 18:11:46 +00:00
|
|
|
|
2019-08-29 14:04:55 +00:00
|
|
|
#include <cassert>
|
2023-08-22 00:16:35 +00:00
|
|
|
#include <charconv>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "source/opt/build_module.h"
|
2019-07-30 23:52:46 +00:00
|
|
|
#include "source/opt/graphics_robust_access_pass.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/log.h"
|
|
|
|
#include "source/opt/pass_manager.h"
|
|
|
|
#include "source/opt/passes.h"
|
2019-09-19 14:26:24 +00:00
|
|
|
#include "source/spirv_optimizer_options.h"
|
2018-08-14 16:44:54 +00:00
|
|
|
#include "source/util/make_unique.h"
|
2018-09-10 15:49:41 +00:00
|
|
|
#include "source/util/string_utils.h"
|
2016-09-12 18:11:46 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
|
|
|
|
struct Optimizer::PassToken::Impl {
|
|
|
|
Impl(std::unique_ptr<opt::Pass> p) : pass(std::move(p)) {}
|
|
|
|
|
|
|
|
std::unique_ptr<opt::Pass> pass; // Internal implementation pass.
|
|
|
|
};
|
|
|
|
|
|
|
|
Optimizer::PassToken::PassToken(
|
|
|
|
std::unique_ptr<Optimizer::PassToken::Impl> impl)
|
|
|
|
: impl_(std::move(impl)) {}
|
2018-05-22 21:31:26 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken::PassToken(std::unique_ptr<opt::Pass>&& pass)
|
|
|
|
: impl_(MakeUnique<Optimizer::PassToken::Impl>(std::move(pass))) {}
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
Optimizer::PassToken::PassToken(PassToken&& that)
|
|
|
|
: impl_(std::move(that.impl_)) {}
|
|
|
|
|
|
|
|
Optimizer::PassToken& Optimizer::PassToken::operator=(PassToken&& that) {
|
|
|
|
impl_ = std::move(that.impl_);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken::~PassToken() {}
|
|
|
|
|
|
|
|
struct Optimizer::Impl {
|
|
|
|
explicit Impl(spv_target_env env) : target_env(env), pass_manager() {}
|
|
|
|
|
2019-02-07 19:00:36 +00:00
|
|
|
spv_target_env target_env; // Target environment.
|
|
|
|
opt::PassManager pass_manager; // Internal implementation pass manager.
|
2022-11-02 17:23:25 +00:00
|
|
|
std::unordered_set<uint32_t> live_locs; // Arg to debug dead output passes
|
2016-09-12 18:11:46 +00:00
|
|
|
};
|
|
|
|
|
2021-01-14 21:45:18 +00:00
|
|
|
Optimizer::Optimizer(spv_target_env env) : impl_(new Impl(env)) {
|
|
|
|
assert(env != SPV_ENV_WEBGPU_0);
|
|
|
|
}
|
2016-09-12 18:11:46 +00:00
|
|
|
|
|
|
|
Optimizer::~Optimizer() {}
|
|
|
|
|
|
|
|
void Optimizer::SetMessageConsumer(MessageConsumer c) {
|
|
|
|
// All passes' message consumer needs to be updated.
|
|
|
|
for (uint32_t i = 0; i < impl_->pass_manager.NumPasses(); ++i) {
|
|
|
|
impl_->pass_manager.GetPass(i)->SetMessageConsumer(c);
|
|
|
|
}
|
|
|
|
impl_->pass_manager.SetMessageConsumer(std::move(c));
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
const MessageConsumer& Optimizer::consumer() const {
|
|
|
|
return impl_->pass_manager.consumer();
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
Optimizer& Optimizer::RegisterPass(PassToken&& p) {
|
|
|
|
// Change to use the pass manager's consumer.
|
2018-07-25 19:21:44 +00:00
|
|
|
p.impl_->pass->SetMessageConsumer(consumer());
|
2016-09-12 18:11:46 +00:00
|
|
|
impl_->pass_manager.AddPass(std::move(p.impl_->pass));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:08:54 +00:00
|
|
|
// The legalization passes take a spir-v shader generated by an HLSL front-end
|
|
|
|
// and turn it into a valid vulkan spir-v shader. There are two ways in which
|
|
|
|
// the code will be invalid at the start:
|
|
|
|
//
|
|
|
|
// 1) There will be opaque objects, like images, which will be passed around
|
|
|
|
// in intermediate objects. Valid spir-v will have to replace the use of
|
|
|
|
// the opaque object with an intermediate object that is the result of the
|
|
|
|
// load of the global opaque object.
|
|
|
|
//
|
|
|
|
// 2) There will be variables that contain pointers to structured or uniform
|
|
|
|
// buffers. It be legal, the variables must be eliminated, and the
|
|
|
|
// references to the structured buffers must use the result of OpVariable
|
|
|
|
// in the Uniform storage class.
|
|
|
|
//
|
|
|
|
// Optimization in this list must accept shaders with these relaxation of the
|
|
|
|
// rules. There is not guarantee that this list of optimizations is able to
|
|
|
|
// legalize all inputs, but it is on a best effort basis.
|
|
|
|
//
|
|
|
|
// The legalization problem is essentially a very general copy propagation
|
|
|
|
// problem. The optimization we use are all used to either do copy propagation
|
|
|
|
// or enable more copy propagation.
|
2023-06-14 14:00:26 +00:00
|
|
|
Optimizer& Optimizer::RegisterLegalizationPasses(bool preserve_interface) {
|
2017-12-19 20:08:54 +00:00
|
|
|
return
|
2019-08-14 13:27:12 +00:00
|
|
|
// Wrap OpKill instructions so all other code can be inlined.
|
|
|
|
RegisterPass(CreateWrapOpKillPass())
|
|
|
|
// Remove unreachable block so that merge return works.
|
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2018-03-06 16:20:28 +00:00
|
|
|
// Merge the returns so we can inline.
|
|
|
|
.RegisterPass(CreateMergeReturnPass())
|
|
|
|
// Make sure uses and definitions are in the same function.
|
|
|
|
.RegisterPass(CreateInlineExhaustivePass())
|
2017-12-19 20:08:54 +00:00
|
|
|
// Make private variable function scope
|
|
|
|
.RegisterPass(CreateEliminateDeadFunctionsPass())
|
|
|
|
.RegisterPass(CreatePrivateToLocalPass())
|
2019-04-05 17:12:08 +00:00
|
|
|
// Fix up the storage classes that DXC may have purposely generated
|
|
|
|
// incorrectly. All functions are inlined, and a lot of dead code has
|
|
|
|
// been removed.
|
|
|
|
.RegisterPass(CreateFixStorageClassPass())
|
2018-02-22 17:23:21 +00:00
|
|
|
// Propagate the value stored to the loads in very simple cases.
|
|
|
|
.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass())
|
|
|
|
.RegisterPass(CreateLocalSingleStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2018-08-03 22:08:46 +00:00
|
|
|
// Split up aggregates so they are easier to deal with.
|
2018-04-16 13:58:00 +00:00
|
|
|
.RegisterPass(CreateScalarReplacementPass(0))
|
2017-12-19 20:08:54 +00:00
|
|
|
// Remove loads and stores so everything is in intermediate values.
|
|
|
|
// Takes care of copy propagation of non-members.
|
2018-01-03 19:03:07 +00:00
|
|
|
.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass())
|
|
|
|
.RegisterPass(CreateLocalSingleStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2017-12-19 20:08:54 +00:00
|
|
|
.RegisterPass(CreateLocalMultiStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2018-02-15 17:14:39 +00:00
|
|
|
// Propagate constants to get as many constant conditions on branches
|
|
|
|
// as possible.
|
|
|
|
.RegisterPass(CreateCCPPass())
|
2018-09-19 20:40:09 +00:00
|
|
|
.RegisterPass(CreateLoopUnrollPass(true))
|
2018-02-15 17:14:39 +00:00
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2017-12-19 20:08:54 +00:00
|
|
|
// Copy propagate members. Cleans up code sequences generated by
|
2018-02-15 17:14:39 +00:00
|
|
|
// scalar replacement. Also important for removing OpPhi nodes.
|
2018-02-02 16:55:05 +00:00
|
|
|
.RegisterPass(CreateSimplificationPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2018-03-21 03:33:24 +00:00
|
|
|
.RegisterPass(CreateCopyPropagateArraysPass())
|
2017-12-19 20:08:54 +00:00
|
|
|
// May need loop unrolling here see
|
|
|
|
// https://github.com/Microsoft/DirectXShaderCompiler/pull/930
|
2018-01-27 00:05:33 +00:00
|
|
|
// Get rid of unused code that contain traces of illegal code
|
|
|
|
// or unused references to unbound external objects
|
2018-04-23 15:13:07 +00:00
|
|
|
.RegisterPass(CreateVectorDCEPass())
|
2018-04-23 15:17:07 +00:00
|
|
|
.RegisterPass(CreateDeadInsertElimPass())
|
2018-05-07 16:31:03 +00:00
|
|
|
.RegisterPass(CreateReduceLoadSizePass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
Add a new legalization pass to dedupe invocation interlock instructions (#5409)
Add a new legalization pass to dedupe invocation interlock instructions
DXC will be adding support for HLSL's rasterizer ordered views by using
the SPV_EXT_fragment_shader_interlock_extension. That extension
stipulates that if an entry point has an interlock ordering execution
mode, it must dynamically execute OpBeginInvocationInterlockEXT and
OpEndInvocationInterlockEXT, in that order, exactly once. This would be
difficult to determine in DXC's SPIR-V backend, so instead we will emit
these instructions potentially multiple times, and use this legalization
pass to ensure that the final SPIR-V follows the specification.
This PR uses data-flow analysis to determine where to place begin and
end instructions; in essence, determining whether a block contains or is
preceded by a begin instruction is similar to a specialized case of a
reaching definitions analysis, where we have only a single definition,
such as `bool has_begun = false`. For this simpler case, we can compute
the set of blocks using BFS to determine the reachability of the begin
instruction.
We need to do this for both begin and end instructions, so I have
generalized portions of the code to run both forward and backward over
the CFG for each respective case.
2023-09-27 23:54:10 +00:00
|
|
|
.RegisterPass(CreateInterpolateFixupPass())
|
|
|
|
.RegisterPass(CreateInvocationInterlockPlacementPass());
|
2017-11-17 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 14:00:26 +00:00
|
|
|
Optimizer& Optimizer::RegisterLegalizationPasses() {
|
|
|
|
return RegisterLegalizationPasses(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer& Optimizer::RegisterPerformancePasses(bool preserve_interface) {
|
2019-08-14 13:27:12 +00:00
|
|
|
return RegisterPass(CreateWrapOpKillPass())
|
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2018-10-10 19:18:15 +00:00
|
|
|
.RegisterPass(CreateMergeReturnPass())
|
2017-11-08 21:22:10 +00:00
|
|
|
.RegisterPass(CreateInlineExhaustivePass())
|
2020-11-05 18:03:45 +00:00
|
|
|
.RegisterPass(CreateEliminateDeadFunctionsPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2018-07-04 19:26:32 +00:00
|
|
|
.RegisterPass(CreatePrivateToLocalPass())
|
2018-02-22 17:23:21 +00:00
|
|
|
.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass())
|
|
|
|
.RegisterPass(CreateLocalSingleStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2017-11-30 22:03:06 +00:00
|
|
|
.RegisterPass(CreateScalarReplacementPass())
|
2017-08-30 18:19:22 +00:00
|
|
|
.RegisterPass(CreateLocalAccessChainConvertPass())
|
|
|
|
.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass())
|
|
|
|
.RegisterPass(CreateLocalSingleStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2018-01-03 17:38:21 +00:00
|
|
|
.RegisterPass(CreateLocalMultiStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2018-01-08 16:56:57 +00:00
|
|
|
.RegisterPass(CreateCCPPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2020-05-20 19:43:13 +00:00
|
|
|
.RegisterPass(CreateLoopUnrollPass(true))
|
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2018-01-31 15:40:33 +00:00
|
|
|
.RegisterPass(CreateRedundancyEliminationPass())
|
2018-07-23 15:23:11 +00:00
|
|
|
.RegisterPass(CreateCombineAccessChainsPass())
|
2018-05-18 15:15:38 +00:00
|
|
|
.RegisterPass(CreateSimplificationPass())
|
2020-05-20 19:43:13 +00:00
|
|
|
.RegisterPass(CreateScalarReplacementPass())
|
|
|
|
.RegisterPass(CreateLocalAccessChainConvertPass())
|
|
|
|
.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass())
|
|
|
|
.RegisterPass(CreateLocalSingleStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2020-05-20 19:43:13 +00:00
|
|
|
.RegisterPass(CreateSSARewritePass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2018-04-23 15:13:07 +00:00
|
|
|
.RegisterPass(CreateVectorDCEPass())
|
2018-04-23 15:17:07 +00:00
|
|
|
.RegisterPass(CreateDeadInsertElimPass())
|
2017-08-30 18:19:22 +00:00
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2018-02-15 17:14:39 +00:00
|
|
|
.RegisterPass(CreateSimplificationPass())
|
2018-01-16 16:15:06 +00:00
|
|
|
.RegisterPass(CreateIfConversionPass())
|
2018-03-28 16:27:19 +00:00
|
|
|
.RegisterPass(CreateCopyPropagateArraysPass())
|
2018-05-07 16:31:03 +00:00
|
|
|
.RegisterPass(CreateReduceLoadSizePass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2017-08-30 18:19:22 +00:00
|
|
|
.RegisterPass(CreateBlockMergePass())
|
2017-12-04 17:29:51 +00:00
|
|
|
.RegisterPass(CreateRedundancyEliminationPass())
|
2018-01-31 15:40:33 +00:00
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
|
|
|
.RegisterPass(CreateBlockMergePass())
|
2019-01-28 16:50:50 +00:00
|
|
|
.RegisterPass(CreateSimplificationPass());
|
2017-08-30 18:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 14:00:26 +00:00
|
|
|
Optimizer& Optimizer::RegisterPerformancePasses() {
|
|
|
|
return RegisterPerformancePasses(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer& Optimizer::RegisterSizePasses(bool preserve_interface) {
|
2019-08-14 13:27:12 +00:00
|
|
|
return RegisterPass(CreateWrapOpKillPass())
|
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2018-10-10 19:18:15 +00:00
|
|
|
.RegisterPass(CreateMergeReturnPass())
|
2017-11-08 21:22:10 +00:00
|
|
|
.RegisterPass(CreateInlineExhaustivePass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateEliminateDeadFunctionsPass())
|
2018-07-04 19:26:32 +00:00
|
|
|
.RegisterPass(CreatePrivateToLocalPass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateScalarReplacementPass(0))
|
2018-01-03 17:38:21 +00:00
|
|
|
.RegisterPass(CreateLocalMultiStoreElimPass())
|
2018-01-08 16:56:57 +00:00
|
|
|
.RegisterPass(CreateCCPPass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateLoopUnrollPass(true))
|
2017-08-30 18:19:22 +00:00
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateSimplificationPass())
|
|
|
|
.RegisterPass(CreateScalarReplacementPass(0))
|
|
|
|
.RegisterPass(CreateLocalSingleStoreElimPass())
|
2018-01-16 16:15:06 +00:00
|
|
|
.RegisterPass(CreateIfConversionPass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateSimplificationPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateDeadBranchElimPass())
|
2017-08-30 18:19:22 +00:00
|
|
|
.RegisterPass(CreateBlockMergePass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateLocalAccessChainConvertPass())
|
|
|
|
.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateCopyPropagateArraysPass())
|
|
|
|
.RegisterPass(CreateVectorDCEPass())
|
2018-01-27 00:05:33 +00:00
|
|
|
.RegisterPass(CreateDeadInsertElimPass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateEliminateDeadMembersPass())
|
|
|
|
.RegisterPass(CreateLocalSingleStoreElimPass())
|
|
|
|
.RegisterPass(CreateBlockMergePass())
|
|
|
|
.RegisterPass(CreateLocalMultiStoreElimPass())
|
2017-12-04 17:29:51 +00:00
|
|
|
.RegisterPass(CreateRedundancyEliminationPass())
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateSimplificationPass())
|
2023-06-14 14:00:26 +00:00
|
|
|
.RegisterPass(CreateAggressiveDCEPass(preserve_interface))
|
2019-11-27 14:41:50 +00:00
|
|
|
.RegisterPass(CreateCFGCleanupPass());
|
2017-08-30 18:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 14:00:26 +00:00
|
|
|
Optimizer& Optimizer::RegisterSizePasses() { return RegisterSizePasses(false); }
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
bool Optimizer::RegisterPassesFromFlags(const std::vector<std::string>& flags) {
|
|
|
|
for (const auto& flag : flags) {
|
|
|
|
if (!RegisterPassFromFlag(flag)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Optimizer::FlagHasValidForm(const std::string& flag) const {
|
|
|
|
if (flag == "-O" || flag == "-Os") {
|
|
|
|
return true;
|
|
|
|
} else if (flag.size() > 2 && flag.substr(0, 2) == "--") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"%s is not a valid flag. Flag passes should have the form "
|
|
|
|
"'--pass_name[=pass_args]'. Special flag names also accepted: -O "
|
|
|
|
"and -Os.",
|
|
|
|
flag.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Optimizer::RegisterPassFromFlag(const std::string& flag) {
|
|
|
|
if (!FlagHasValidForm(flag)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split flags of the form --pass_name=pass_args.
|
2018-09-10 15:49:41 +00:00
|
|
|
auto p = utils::SplitFlagArgs(flag);
|
2018-07-25 19:21:44 +00:00
|
|
|
std::string pass_name = p.first;
|
|
|
|
std::string pass_args = p.second;
|
|
|
|
|
|
|
|
// FIXME(dnovillo): This should be re-factored so that pass names can be
|
|
|
|
// automatically checked against Pass::name() and PassToken instances created
|
|
|
|
// via a template function. Additionally, class Pass should have a desc()
|
|
|
|
// method that describes the pass (so it can be used in --help).
|
|
|
|
//
|
|
|
|
// Both Pass::name() and Pass::desc() should be static class members so they
|
|
|
|
// can be invoked without creating a pass instance.
|
2021-01-14 21:45:18 +00:00
|
|
|
if (pass_name == "strip-debug") {
|
2018-07-25 19:21:44 +00:00
|
|
|
RegisterPass(CreateStripDebugInfoPass());
|
|
|
|
} else if (pass_name == "strip-reflect") {
|
|
|
|
RegisterPass(CreateStripReflectInfoPass());
|
2021-12-15 14:55:30 +00:00
|
|
|
} else if (pass_name == "strip-nonsemantic") {
|
|
|
|
RegisterPass(CreateStripNonSemanticInfoPass());
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "set-spec-const-default-value") {
|
|
|
|
if (pass_args.size() > 0) {
|
|
|
|
auto spec_ids_vals =
|
|
|
|
opt::SetSpecConstantDefaultValuePass::ParseDefaultValuesString(
|
|
|
|
pass_args.c_str());
|
|
|
|
if (!spec_ids_vals) {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Invalid argument for --set-spec-const-default-value: %s",
|
|
|
|
pass_args.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RegisterPass(
|
|
|
|
CreateSetSpecConstantDefaultValuePass(std::move(*spec_ids_vals)));
|
|
|
|
} else {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Invalid spec constant value string '%s'. Expected a string of "
|
|
|
|
"<spec id>:<default value> pairs.",
|
|
|
|
pass_args.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pass_name == "if-conversion") {
|
|
|
|
RegisterPass(CreateIfConversionPass());
|
|
|
|
} else if (pass_name == "freeze-spec-const") {
|
|
|
|
RegisterPass(CreateFreezeSpecConstantValuePass());
|
|
|
|
} else if (pass_name == "inline-entry-points-exhaustive") {
|
|
|
|
RegisterPass(CreateInlineExhaustivePass());
|
|
|
|
} else if (pass_name == "inline-entry-points-opaque") {
|
|
|
|
RegisterPass(CreateInlineOpaquePass());
|
2018-07-23 15:23:11 +00:00
|
|
|
} else if (pass_name == "combine-access-chains") {
|
|
|
|
RegisterPass(CreateCombineAccessChainsPass());
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "convert-local-access-chains") {
|
|
|
|
RegisterPass(CreateLocalAccessChainConvertPass());
|
2021-10-26 21:20:58 +00:00
|
|
|
} else if (pass_name == "replace-desc-array-access-using-var-index") {
|
|
|
|
RegisterPass(CreateReplaceDescArrayAccessUsingVarIndexPass());
|
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
|
|
|
} else if (pass_name == "spread-volatile-semantics") {
|
|
|
|
RegisterPass(CreateSpreadVolatileSemanticsPass());
|
2019-08-08 14:53:19 +00:00
|
|
|
} else if (pass_name == "descriptor-scalar-replacement") {
|
|
|
|
RegisterPass(CreateDescriptorScalarReplacementPass());
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "eliminate-dead-code-aggressive") {
|
|
|
|
RegisterPass(CreateAggressiveDCEPass());
|
|
|
|
} else if (pass_name == "eliminate-insert-extract") {
|
|
|
|
RegisterPass(CreateInsertExtractElimPass());
|
|
|
|
} else if (pass_name == "eliminate-local-single-block") {
|
|
|
|
RegisterPass(CreateLocalSingleBlockLoadStoreElimPass());
|
|
|
|
} else if (pass_name == "eliminate-local-single-store") {
|
|
|
|
RegisterPass(CreateLocalSingleStoreElimPass());
|
|
|
|
} else if (pass_name == "merge-blocks") {
|
|
|
|
RegisterPass(CreateBlockMergePass());
|
|
|
|
} else if (pass_name == "merge-return") {
|
|
|
|
RegisterPass(CreateMergeReturnPass());
|
|
|
|
} else if (pass_name == "eliminate-dead-branches") {
|
|
|
|
RegisterPass(CreateDeadBranchElimPass());
|
|
|
|
} else if (pass_name == "eliminate-dead-functions") {
|
|
|
|
RegisterPass(CreateEliminateDeadFunctionsPass());
|
|
|
|
} else if (pass_name == "eliminate-local-multi-store") {
|
|
|
|
RegisterPass(CreateLocalMultiStoreElimPass());
|
|
|
|
} else if (pass_name == "eliminate-dead-const") {
|
|
|
|
RegisterPass(CreateEliminateDeadConstantPass());
|
|
|
|
} else if (pass_name == "eliminate-dead-inserts") {
|
|
|
|
RegisterPass(CreateDeadInsertElimPass());
|
|
|
|
} else if (pass_name == "eliminate-dead-variables") {
|
|
|
|
RegisterPass(CreateDeadVariableEliminationPass());
|
2019-02-14 18:42:35 +00:00
|
|
|
} else if (pass_name == "eliminate-dead-members") {
|
|
|
|
RegisterPass(CreateEliminateDeadMembersPass());
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "fold-spec-const-op-composite") {
|
|
|
|
RegisterPass(CreateFoldSpecConstantOpAndCompositePass());
|
|
|
|
} else if (pass_name == "loop-unswitch") {
|
|
|
|
RegisterPass(CreateLoopUnswitchPass());
|
|
|
|
} else if (pass_name == "scalar-replacement") {
|
|
|
|
if (pass_args.size() == 0) {
|
|
|
|
RegisterPass(CreateScalarReplacementPass());
|
|
|
|
} else {
|
2018-09-26 13:58:28 +00:00
|
|
|
int limit = -1;
|
|
|
|
if (pass_args.find_first_not_of("0123456789") == std::string::npos) {
|
|
|
|
limit = atoi(pass_args.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit >= 0) {
|
2018-07-25 19:21:44 +00:00
|
|
|
RegisterPass(CreateScalarReplacementPass(limit));
|
|
|
|
} else {
|
|
|
|
Error(consumer(), nullptr, {},
|
2018-09-26 13:58:28 +00:00
|
|
|
"--scalar-replacement must have no arguments or a non-negative "
|
2018-07-25 19:21:44 +00:00
|
|
|
"integer argument");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (pass_name == "strength-reduction") {
|
|
|
|
RegisterPass(CreateStrengthReductionPass());
|
|
|
|
} else if (pass_name == "unify-const") {
|
|
|
|
RegisterPass(CreateUnifyConstantPass());
|
|
|
|
} else if (pass_name == "flatten-decorations") {
|
|
|
|
RegisterPass(CreateFlattenDecorationPass());
|
|
|
|
} else if (pass_name == "compact-ids") {
|
|
|
|
RegisterPass(CreateCompactIdsPass());
|
|
|
|
} else if (pass_name == "cfg-cleanup") {
|
|
|
|
RegisterPass(CreateCFGCleanupPass());
|
|
|
|
} else if (pass_name == "local-redundancy-elimination") {
|
|
|
|
RegisterPass(CreateLocalRedundancyEliminationPass());
|
|
|
|
} else if (pass_name == "loop-invariant-code-motion") {
|
|
|
|
RegisterPass(CreateLoopInvariantCodeMotionPass());
|
|
|
|
} else if (pass_name == "reduce-load-size") {
|
2021-09-02 14:45:51 +00:00
|
|
|
if (pass_args.size() == 0) {
|
|
|
|
RegisterPass(CreateReduceLoadSizePass());
|
|
|
|
} else {
|
|
|
|
double load_replacement_threshold = 0.9;
|
|
|
|
if (pass_args.find_first_not_of(".0123456789") == std::string::npos) {
|
|
|
|
load_replacement_threshold = atof(pass_args.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (load_replacement_threshold >= 0) {
|
|
|
|
RegisterPass(CreateReduceLoadSizePass(load_replacement_threshold));
|
|
|
|
} else {
|
|
|
|
Error(consumer(), nullptr, {},
|
|
|
|
"--reduce-load-size must have no arguments or a non-negative "
|
|
|
|
"double argument");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "redundancy-elimination") {
|
|
|
|
RegisterPass(CreateRedundancyEliminationPass());
|
|
|
|
} else if (pass_name == "private-to-local") {
|
|
|
|
RegisterPass(CreatePrivateToLocalPass());
|
|
|
|
} else if (pass_name == "remove-duplicates") {
|
|
|
|
RegisterPass(CreateRemoveDuplicatesPass());
|
|
|
|
} else if (pass_name == "workaround-1209") {
|
|
|
|
RegisterPass(CreateWorkaround1209Pass());
|
|
|
|
} else if (pass_name == "replace-invalid-opcode") {
|
|
|
|
RegisterPass(CreateReplaceInvalidOpcodePass());
|
2023-06-22 15:39:49 +00:00
|
|
|
} else if (pass_name == "inst-bindless-check" ||
|
|
|
|
pass_name == "inst-desc-idx-check" ||
|
|
|
|
pass_name == "inst-buff-oob-check") {
|
|
|
|
// preserve legacy names
|
2023-09-20 16:50:30 +00:00
|
|
|
RegisterPass(CreateInstBindlessCheckPass(23));
|
2020-09-16 13:23:46 +00:00
|
|
|
RegisterPass(CreateSimplificationPass());
|
|
|
|
RegisterPass(CreateDeadBranchElimPass());
|
|
|
|
RegisterPass(CreateBlockMergePass());
|
2019-08-16 13:18:34 +00:00
|
|
|
} else if (pass_name == "inst-buff-addr-check") {
|
2023-09-20 16:50:30 +00:00
|
|
|
RegisterPass(CreateInstBuffAddrCheckPass(23));
|
2019-09-03 17:22:13 +00:00
|
|
|
} else if (pass_name == "convert-relaxed-to-half") {
|
|
|
|
RegisterPass(CreateConvertRelaxedToHalfPass());
|
|
|
|
} else if (pass_name == "relax-float-ops") {
|
|
|
|
RegisterPass(CreateRelaxFloatOpsPass());
|
2020-03-12 13:19:52 +00:00
|
|
|
} else if (pass_name == "inst-debug-printf") {
|
2023-11-14 18:00:54 +00:00
|
|
|
// This private option is not for user consumption.
|
|
|
|
// It is here to assist in debugging and fixing the debug printf
|
|
|
|
// instrumentation pass.
|
|
|
|
// For users who wish to utilize debug printf, see the white paper at
|
|
|
|
// https://www.lunarg.com/wp-content/uploads/2021/08/Using-Debug-Printf-02August2021.pdf
|
2020-03-12 13:19:52 +00:00
|
|
|
RegisterPass(CreateInstDebugPrintfPass(7, 23));
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "simplify-instructions") {
|
|
|
|
RegisterPass(CreateSimplificationPass());
|
|
|
|
} else if (pass_name == "ssa-rewrite") {
|
|
|
|
RegisterPass(CreateSSARewritePass());
|
|
|
|
} else if (pass_name == "copy-propagate-arrays") {
|
|
|
|
RegisterPass(CreateCopyPropagateArraysPass());
|
|
|
|
} else if (pass_name == "loop-fission") {
|
|
|
|
int register_threshold_to_split =
|
|
|
|
(pass_args.size() > 0) ? atoi(pass_args.c_str()) : -1;
|
|
|
|
if (register_threshold_to_split > 0) {
|
|
|
|
RegisterPass(CreateLoopFissionPass(
|
|
|
|
static_cast<size_t>(register_threshold_to_split)));
|
|
|
|
} else {
|
|
|
|
Error(consumer(), nullptr, {},
|
|
|
|
"--loop-fission must have a positive integer argument");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pass_name == "loop-fusion") {
|
|
|
|
int max_registers_per_loop =
|
|
|
|
(pass_args.size() > 0) ? atoi(pass_args.c_str()) : -1;
|
|
|
|
if (max_registers_per_loop > 0) {
|
|
|
|
RegisterPass(
|
|
|
|
CreateLoopFusionPass(static_cast<size_t>(max_registers_per_loop)));
|
|
|
|
} else {
|
|
|
|
Error(consumer(), nullptr, {},
|
2018-08-03 22:08:46 +00:00
|
|
|
"--loop-fusion must have a positive integer argument");
|
2018-07-25 19:21:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pass_name == "loop-unroll") {
|
|
|
|
RegisterPass(CreateLoopUnrollPass(true));
|
2018-11-30 19:15:51 +00:00
|
|
|
} else if (pass_name == "upgrade-memory-model") {
|
|
|
|
RegisterPass(CreateUpgradeMemoryModelPass());
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "vector-dce") {
|
|
|
|
RegisterPass(CreateVectorDCEPass());
|
|
|
|
} else if (pass_name == "loop-unroll-partial") {
|
|
|
|
int factor = (pass_args.size() > 0) ? atoi(pass_args.c_str()) : 0;
|
2018-08-03 22:08:46 +00:00
|
|
|
if (factor > 0) {
|
2018-07-25 19:21:44 +00:00
|
|
|
RegisterPass(CreateLoopUnrollPass(false, factor));
|
|
|
|
} else {
|
|
|
|
Error(consumer(), nullptr, {},
|
2018-08-03 22:08:46 +00:00
|
|
|
"--loop-unroll-partial must have a positive integer argument");
|
2018-07-25 19:21:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pass_name == "loop-peeling") {
|
|
|
|
RegisterPass(CreateLoopPeelingPass());
|
2018-08-03 22:08:46 +00:00
|
|
|
} else if (pass_name == "loop-peeling-threshold") {
|
|
|
|
int factor = (pass_args.size() > 0) ? atoi(pass_args.c_str()) : 0;
|
|
|
|
if (factor > 0) {
|
|
|
|
opt::LoopPeelingPass::SetLoopPeelingThreshold(factor);
|
|
|
|
} else {
|
|
|
|
Error(consumer(), nullptr, {},
|
|
|
|
"--loop-peeling-threshold must have a positive integer argument");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "ccp") {
|
|
|
|
RegisterPass(CreateCCPPass());
|
2019-01-17 20:56:36 +00:00
|
|
|
} else if (pass_name == "code-sink") {
|
|
|
|
RegisterPass(CreateCodeSinkingPass());
|
2019-04-05 17:12:08 +00:00
|
|
|
} else if (pass_name == "fix-storage-class") {
|
|
|
|
RegisterPass(CreateFixStorageClassPass());
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (pass_name == "O") {
|
|
|
|
RegisterPerformancePasses();
|
|
|
|
} else if (pass_name == "Os") {
|
|
|
|
RegisterSizePasses();
|
|
|
|
} else if (pass_name == "legalize-hlsl") {
|
|
|
|
RegisterLegalizationPasses();
|
2021-06-29 15:33:58 +00:00
|
|
|
} else if (pass_name == "remove-unused-interface-variables") {
|
|
|
|
RegisterPass(CreateRemoveUnusedInterfaceVariablesPass());
|
2019-07-30 23:52:46 +00:00
|
|
|
} else if (pass_name == "graphics-robust-access") {
|
|
|
|
RegisterPass(CreateGraphicsRobustAccessPass());
|
2019-08-14 13:27:12 +00:00
|
|
|
} else if (pass_name == "wrap-opkill") {
|
|
|
|
RegisterPass(CreateWrapOpKillPass());
|
2019-08-29 16:48:17 +00:00
|
|
|
} else if (pass_name == "amd-ext-to-khr") {
|
|
|
|
RegisterPass(CreateAmdExtToKhrPass());
|
2021-03-31 18:26:36 +00:00
|
|
|
} else if (pass_name == "interpolate-fixup") {
|
|
|
|
RegisterPass(CreateInterpolateFixupPass());
|
2022-03-07 17:45:17 +00:00
|
|
|
} else if (pass_name == "remove-dont-inline") {
|
|
|
|
RegisterPass(CreateRemoveDontInlinePass());
|
2022-03-23 02:50:52 +00:00
|
|
|
} else if (pass_name == "eliminate-dead-input-components") {
|
2022-11-14 18:44:26 +00:00
|
|
|
RegisterPass(CreateEliminateDeadInputComponentsSafePass());
|
2022-05-06 14:39:26 +00:00
|
|
|
} else if (pass_name == "fix-func-call-param") {
|
|
|
|
RegisterPass(CreateFixFuncCallArgumentsPass());
|
2021-08-18 12:30:48 +00:00
|
|
|
} else if (pass_name == "convert-to-sampled-image") {
|
|
|
|
if (pass_args.size() > 0) {
|
|
|
|
auto descriptor_set_binding_pairs =
|
|
|
|
opt::ConvertToSampledImagePass::ParseDescriptorSetBindingPairsString(
|
|
|
|
pass_args.c_str());
|
|
|
|
if (!descriptor_set_binding_pairs) {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Invalid argument for --convert-to-sampled-image: %s",
|
|
|
|
pass_args.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RegisterPass(CreateConvertToSampledImagePass(
|
|
|
|
std::move(*descriptor_set_binding_pairs)));
|
|
|
|
} else {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Invalid pairs of descriptor set and binding '%s'. Expected a "
|
|
|
|
"string of <descriptor set>:<binding> pairs.",
|
|
|
|
pass_args.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-22 00:16:35 +00:00
|
|
|
} else if (pass_name == "switch-descriptorset") {
|
|
|
|
if (pass_args.size() == 0) {
|
|
|
|
Error(consumer(), nullptr, {},
|
|
|
|
"--switch-descriptorset requires a from:to argument.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t from_set, to_set;
|
|
|
|
const char* start = pass_args.data();
|
|
|
|
const char* end = pass_args.data() + pass_args.size();
|
|
|
|
|
|
|
|
auto result = std::from_chars(start, end, from_set);
|
|
|
|
if (result.ec != std::errc()) {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Invalid argument for --switch-descriptorset: %s",
|
|
|
|
pass_args.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
start = result.ptr;
|
|
|
|
if (start[0] != ':') {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Invalid argument for --switch-descriptorset: %s",
|
|
|
|
pass_args.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
start++;
|
|
|
|
result = std::from_chars(start, end, to_set);
|
|
|
|
if (result.ec != std::errc() || result.ptr != end) {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Invalid argument for --switch-descriptorset: %s",
|
|
|
|
pass_args.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RegisterPass(CreateSwitchDescriptorSetPass(from_set, to_set));
|
2018-07-25 19:21:44 +00:00
|
|
|
} else {
|
|
|
|
Errorf(consumer(), nullptr, {},
|
|
|
|
"Unknown flag '--%s'. Use --help for a list of valid flags",
|
|
|
|
pass_name.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-17 21:54:23 +00:00
|
|
|
void Optimizer::SetTargetEnv(const spv_target_env env) {
|
|
|
|
impl_->target_env = env;
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
bool Optimizer::Run(const uint32_t* original_binary,
|
|
|
|
const size_t original_binary_size,
|
|
|
|
std::vector<uint32_t>* optimized_binary) const {
|
2018-08-08 15:16:19 +00:00
|
|
|
return Run(original_binary, original_binary_size, optimized_binary,
|
2018-09-10 15:49:41 +00:00
|
|
|
OptimizerOptions());
|
2018-08-08 15:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Optimizer::Run(const uint32_t* original_binary,
|
|
|
|
const size_t original_binary_size,
|
|
|
|
std::vector<uint32_t>* optimized_binary,
|
2018-09-10 15:49:41 +00:00
|
|
|
const ValidatorOptions& validator_options,
|
2018-08-13 17:18:46 +00:00
|
|
|
bool skip_validation) const {
|
2018-09-10 15:49:41 +00:00
|
|
|
OptimizerOptions opt_options;
|
|
|
|
opt_options.set_run_validator(!skip_validation);
|
|
|
|
opt_options.set_validator_options(validator_options);
|
|
|
|
return Run(original_binary, original_binary_size, optimized_binary,
|
|
|
|
opt_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Optimizer::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 {
|
2018-08-08 15:16:19 +00:00
|
|
|
spvtools::SpirvTools tools(impl_->target_env);
|
|
|
|
tools.SetMessageConsumer(impl_->pass_manager.consumer());
|
2018-09-10 15:49:41 +00:00
|
|
|
if (opt_options->run_validator_ &&
|
|
|
|
!tools.Validate(original_binary, original_binary_size,
|
|
|
|
&opt_options->val_options_)) {
|
2018-08-08 15:16:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
std::unique_ptr<opt::IRContext> context = BuildModule(
|
|
|
|
impl_->target_env, consumer(), original_binary, original_binary_size);
|
2017-11-14 19:11:50 +00:00
|
|
|
if (context == nullptr) return false;
|
2016-09-12 18:11:46 +00:00
|
|
|
|
2018-09-10 15:49:41 +00:00
|
|
|
context->set_max_id_bound(opt_options->max_id_bound_);
|
2019-07-10 18:12:19 +00:00
|
|
|
context->set_preserve_bindings(opt_options->preserve_bindings_);
|
|
|
|
context->set_preserve_spec_constants(opt_options->preserve_spec_constants_);
|
2018-09-10 15:49:41 +00:00
|
|
|
|
2019-03-26 18:38:59 +00:00
|
|
|
impl_->pass_manager.SetValidatorOptions(&opt_options->val_options_);
|
|
|
|
impl_->pass_manager.SetTargetEnv(impl_->target_env);
|
2017-11-14 19:11:50 +00:00
|
|
|
auto status = impl_->pass_manager.Run(context.get());
|
2019-04-05 17:36:42 +00:00
|
|
|
|
2019-08-29 14:04:55 +00:00
|
|
|
if (status == opt::Pass::Status::Failure) {
|
|
|
|
return false;
|
2019-04-05 17:36:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:04:55 +00:00
|
|
|
#ifndef NDEBUG
|
2020-03-23 15:01:18 +00:00
|
|
|
// We do not keep the result id of DebugScope in struct DebugScope.
|
2020-09-10 13:52:21 +00:00
|
|
|
// Instead, we assign random ids for them, which results in integrity
|
2021-01-13 14:08:28 +00:00
|
|
|
// check failures. In addition, propagating the OpLine/OpNoLine to preserve
|
|
|
|
// the debug information through transformations results in integrity
|
2020-09-10 13:52:21 +00:00
|
|
|
// check failures. We want to skip the integrity check when the module
|
2021-01-13 14:08:28 +00:00
|
|
|
// contains DebugScope or OpLine/OpNoLine instructions.
|
2020-03-23 15:01:18 +00:00
|
|
|
if (status == opt::Pass::Status::SuccessWithoutChange &&
|
2021-01-13 14:08:28 +00:00
|
|
|
!context->module()->ContainsDebugInfo()) {
|
2019-10-18 13:53:29 +00:00
|
|
|
std::vector<uint32_t> optimized_binary_with_nop;
|
|
|
|
context->module()->ToBinary(&optimized_binary_with_nop,
|
|
|
|
/* skip_nop = */ false);
|
2019-10-30 17:01:28 +00:00
|
|
|
assert(optimized_binary_with_nop.size() == original_binary_size &&
|
|
|
|
"Binary size unexpectedly changed despite the optimizer saying "
|
|
|
|
"there was no change");
|
2022-06-21 19:58:21 +00:00
|
|
|
|
|
|
|
// Compare the magic number to make sure the binaries were encoded in the
|
|
|
|
// endianness. If not, the contents of the binaries will be different, so
|
|
|
|
// do not check the contents.
|
|
|
|
if (optimized_binary_with_nop[0] == original_binary[0]) {
|
|
|
|
assert(memcmp(optimized_binary_with_nop.data(), original_binary,
|
|
|
|
original_binary_size) == 0 &&
|
|
|
|
"Binary content unexpectedly changed despite the optimizer saying "
|
|
|
|
"there was no change");
|
|
|
|
}
|
2016-09-12 18:11:46 +00:00
|
|
|
}
|
2019-08-29 14:04:55 +00:00
|
|
|
#endif // !NDEBUG
|
2016-09-12 18:11:46 +00:00
|
|
|
|
2019-10-30 17:01:28 +00:00
|
|
|
// Note that |original_binary| and |optimized_binary| may share the same
|
|
|
|
// buffer and the below will invalidate |original_binary|.
|
|
|
|
optimized_binary->clear();
|
|
|
|
context->module()->ToBinary(optimized_binary, /* skip_nop = */ true);
|
|
|
|
|
2019-08-29 14:04:55 +00:00
|
|
|
return true;
|
2016-09-12 18:11:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 17:59:50 +00:00
|
|
|
Optimizer& Optimizer::SetPrintAll(std::ostream* out) {
|
|
|
|
impl_->pass_manager.SetPrintAll(out);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:25:51 +00:00
|
|
|
Optimizer& Optimizer::SetTimeReport(std::ostream* out) {
|
|
|
|
impl_->pass_manager.SetTimeReport(out);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:38:59 +00:00
|
|
|
Optimizer& Optimizer::SetValidateAfterAll(bool validate) {
|
|
|
|
impl_->pass_manager.SetValidateAfterAll(validate);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
Optimizer::PassToken CreateNullPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(MakeUnique<opt::NullPass>());
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateStripDebugInfoPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::StripDebugInfoPass>());
|
|
|
|
}
|
|
|
|
|
2018-03-09 21:08:57 +00:00
|
|
|
Optimizer::PassToken CreateStripReflectInfoPass() {
|
2021-12-15 14:55:30 +00:00
|
|
|
return CreateStripNonSemanticInfoPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateStripNonSemanticInfoPass() {
|
2018-03-09 21:08:57 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2021-12-15 14:55:30 +00:00
|
|
|
MakeUnique<opt::StripNonSemanticInfoPass>());
|
2018-03-09 21:08:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 14:12:13 +00:00
|
|
|
Optimizer::PassToken CreateEliminateDeadFunctionsPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::EliminateDeadFunctionsPass>());
|
|
|
|
}
|
|
|
|
|
2019-02-14 18:42:35 +00:00
|
|
|
Optimizer::PassToken CreateEliminateDeadMembersPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::EliminateDeadMembersPass>());
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
|
|
|
|
const std::unordered_map<uint32_t, std::string>& id_value_map) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::SetSpecConstantDefaultValuePass>(id_value_map));
|
|
|
|
}
|
|
|
|
|
2017-04-19 22:10:59 +00:00
|
|
|
Optimizer::PassToken CreateSetSpecConstantDefaultValuePass(
|
|
|
|
const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::SetSpecConstantDefaultValuePass>(id_value_map));
|
|
|
|
}
|
|
|
|
|
2017-04-01 20:10:16 +00:00
|
|
|
Optimizer::PassToken CreateFlattenDecorationPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::FlattenDecorationPass>());
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:11:46 +00:00
|
|
|
Optimizer::PassToken CreateFreezeSpecConstantValuePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::FreezeSpecConstantValuePass>());
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateFoldSpecConstantOpAndCompositePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::FoldSpecConstantOpAndCompositePass>());
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateUnifyConstantPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::UnifyConstantPass>());
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateEliminateDeadConstantPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::EliminateDeadConstantPass>());
|
|
|
|
}
|
|
|
|
|
2017-10-20 16:17:41 +00:00
|
|
|
Optimizer::PassToken CreateDeadVariableEliminationPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::DeadVariableElimination>());
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:08:03 +00:00
|
|
|
Optimizer::PassToken CreateStrengthReductionPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::StrengthReductionPass>());
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:28:53 +00:00
|
|
|
Optimizer::PassToken CreateBlockMergePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::BlockMergePass>());
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:20:13 +00:00
|
|
|
Optimizer::PassToken CreateInlineExhaustivePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::InlineExhaustivePass>());
|
2016-11-10 17:11:50 +00:00
|
|
|
}
|
2017-08-30 18:19:22 +00:00
|
|
|
|
2017-08-15 23:58:28 +00:00
|
|
|
Optimizer::PassToken CreateInlineOpaquePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::InlineOpaquePass>());
|
|
|
|
}
|
2017-08-30 18:19:22 +00:00
|
|
|
|
2017-05-12 23:27:21 +00:00
|
|
|
Optimizer::PassToken CreateLocalAccessChainConvertPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LocalAccessChainConvertPass>());
|
|
|
|
}
|
2017-08-30 18:19:22 +00:00
|
|
|
|
2017-05-18 20:51:55 +00:00
|
|
|
Optimizer::PassToken CreateLocalSingleBlockLoadStoreElimPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LocalSingleBlockLoadStoreElimPass>());
|
|
|
|
}
|
2017-05-12 23:27:21 +00:00
|
|
|
|
2017-05-19 23:31:28 +00:00
|
|
|
Optimizer::PassToken CreateLocalSingleStoreElimPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LocalSingleStoreElimPass>());
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:33:11 +00:00
|
|
|
Optimizer::PassToken CreateInsertExtractElimPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2018-05-31 03:50:07 +00:00
|
|
|
MakeUnique<opt::SimplificationPass>());
|
2017-05-26 16:33:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 00:05:33 +00:00
|
|
|
Optimizer::PassToken CreateDeadInsertElimPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::DeadInsertElimPass>());
|
|
|
|
}
|
|
|
|
|
2017-06-02 19:23:20 +00:00
|
|
|
Optimizer::PassToken CreateDeadBranchElimPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::DeadBranchElimPass>());
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:37:31 +00:00
|
|
|
Optimizer::PassToken CreateLocalMultiStoreElimPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2019-09-10 13:38:23 +00:00
|
|
|
MakeUnique<opt::SSARewritePass>());
|
2017-06-16 21:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 20:11:30 +00:00
|
|
|
Optimizer::PassToken CreateAggressiveDCEPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::AggressiveDCEPass>(false, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateAggressiveDCEPass(bool preserve_interface) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::AggressiveDCEPass>(preserve_interface, false));
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:48:58 +00:00
|
|
|
Optimizer::PassToken CreateAggressiveDCEPass(bool preserve_interface,
|
|
|
|
bool remove_outputs) {
|
2021-12-09 20:58:53 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2022-11-23 17:48:58 +00:00
|
|
|
MakeUnique<opt::AggressiveDCEPass>(preserve_interface, remove_outputs));
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:33:58 +00:00
|
|
|
Optimizer::PassToken CreateRemoveUnusedInterfaceVariablesPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::RemoveUnusedInterfaceVariablesPass>());
|
|
|
|
}
|
|
|
|
|
2020-10-30 22:03:56 +00:00
|
|
|
Optimizer::PassToken CreatePropagateLineInfoPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(MakeUnique<opt::EmptyPass>());
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateRedundantLineInfoElimPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(MakeUnique<opt::EmptyPass>());
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:11:04 +00:00
|
|
|
Optimizer::PassToken CreateCompactIdsPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::CompactIdsPass>());
|
|
|
|
}
|
|
|
|
|
2017-11-08 21:22:10 +00:00
|
|
|
Optimizer::PassToken CreateMergeReturnPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::MergeReturnPass>());
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
std::vector<const char*> Optimizer::GetPassNames() const {
|
|
|
|
std::vector<const char*> v;
|
|
|
|
for (uint32_t i = 0; i < impl_->pass_manager.NumPasses(); i++) {
|
|
|
|
v.push_back(impl_->pass_manager.GetPass(i)->name());
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2017-10-19 19:22:02 +00:00
|
|
|
Optimizer::PassToken CreateCFGCleanupPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::CFGCleanupPass>());
|
|
|
|
}
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
Optimizer::PassToken CreateLocalRedundancyEliminationPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LocalRedundancyEliminationPass>());
|
|
|
|
}
|
2017-12-04 17:29:51 +00:00
|
|
|
|
2018-04-23 20:01:12 +00:00
|
|
|
Optimizer::PassToken CreateLoopFissionPass(size_t threshold) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LoopFissionPass>(threshold));
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:14:45 +00:00
|
|
|
Optimizer::PassToken CreateLoopFusionPass(size_t max_registers_per_loop) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LoopFusionPass>(max_registers_per_loop));
|
|
|
|
}
|
|
|
|
|
2018-01-29 10:39:55 +00:00
|
|
|
Optimizer::PassToken CreateLoopInvariantCodeMotionPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(MakeUnique<opt::LICMPass>());
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:22:42 +00:00
|
|
|
Optimizer::PassToken CreateLoopPeelingPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LoopPeelingPass>());
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
Optimizer::PassToken CreateLoopUnswitchPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::LoopUnswitchPass>());
|
|
|
|
}
|
|
|
|
|
2017-12-04 17:29:51 +00:00
|
|
|
Optimizer::PassToken CreateRedundancyEliminationPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::RedundancyEliminationPass>());
|
|
|
|
}
|
2017-11-30 22:03:06 +00:00
|
|
|
|
2017-12-08 20:33:19 +00:00
|
|
|
Optimizer::PassToken CreateRemoveDuplicatesPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::RemoveDuplicatesPass>());
|
|
|
|
}
|
|
|
|
|
2018-04-16 13:58:00 +00:00
|
|
|
Optimizer::PassToken CreateScalarReplacementPass(uint32_t size_limit) {
|
2017-11-30 22:03:06 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2018-04-16 13:58:00 +00:00
|
|
|
MakeUnique<opt::ScalarReplacementPass>(size_limit));
|
2017-11-30 22:03:06 +00:00
|
|
|
}
|
2017-12-11 18:10:24 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreatePrivateToLocalPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::PrivateToLocalPass>());
|
|
|
|
}
|
2017-12-05 16:39:25 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreateCCPPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(MakeUnique<opt::CCPPass>());
|
|
|
|
}
|
|
|
|
|
2018-01-17 19:57:37 +00:00
|
|
|
Optimizer::PassToken CreateWorkaround1209Pass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::Workaround1209>());
|
|
|
|
}
|
|
|
|
|
2018-01-16 16:15:06 +00:00
|
|
|
Optimizer::PassToken CreateIfConversionPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::IfConversion>());
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:24:03 +00:00
|
|
|
Optimizer::PassToken CreateReplaceInvalidOpcodePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::ReplaceInvalidOpcodePass>());
|
|
|
|
}
|
2018-02-02 16:55:05 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreateSimplificationPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::SimplificationPass>());
|
|
|
|
}
|
2018-02-14 17:03:12 +00:00
|
|
|
|
2018-02-27 11:50:08 +00:00
|
|
|
Optimizer::PassToken CreateLoopUnrollPass(bool fully_unroll, int factor) {
|
2018-02-14 17:03:12 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2018-02-27 11:50:08 +00:00
|
|
|
MakeUnique<opt::LoopUnroller>(fully_unroll, factor));
|
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
|
|
|
|
|
|
|
Optimizer::PassToken CreateSSARewritePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::SSARewritePass>());
|
|
|
|
}
|
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
Optimizer::PassToken CreateCopyPropagateArraysPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::CopyPropagateArrays>());
|
|
|
|
}
|
|
|
|
|
2018-04-23 15:13:07 +00:00
|
|
|
Optimizer::PassToken CreateVectorDCEPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(MakeUnique<opt::VectorDCE>());
|
|
|
|
}
|
|
|
|
|
2021-09-02 14:45:51 +00:00
|
|
|
Optimizer::PassToken CreateReduceLoadSizePass(
|
|
|
|
double load_replacement_threshold) {
|
2018-05-07 16:31:03 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2021-09-02 14:45:51 +00:00
|
|
|
MakeUnique<opt::ReduceLoadSize>(load_replacement_threshold));
|
2018-05-07 16:31:03 +00:00
|
|
|
}
|
2018-07-23 15:23:11 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreateCombineAccessChainsPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::CombineAccessChains>());
|
|
|
|
}
|
2018-11-08 18:54:54 +00:00
|
|
|
|
2018-11-30 19:15:51 +00:00
|
|
|
Optimizer::PassToken CreateUpgradeMemoryModelPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::UpgradeMemoryModel>());
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:50:30 +00:00
|
|
|
Optimizer::PassToken CreateInstBindlessCheckPass(uint32_t shader_id) {
|
2018-11-08 18:54:54 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2023-09-20 16:50:30 +00:00
|
|
|
MakeUnique<opt::InstBindlessCheckPass>(shader_id));
|
2018-11-08 18:54:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:19:52 +00:00
|
|
|
Optimizer::PassToken CreateInstDebugPrintfPass(uint32_t desc_set,
|
|
|
|
uint32_t shader_id) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::InstDebugPrintfPass>(desc_set, shader_id));
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:50:30 +00:00
|
|
|
Optimizer::PassToken CreateInstBuffAddrCheckPass(uint32_t shader_id) {
|
2019-08-16 13:18:34 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2023-09-20 16:50:30 +00:00
|
|
|
MakeUnique<opt::InstBuffAddrCheckPass>(shader_id));
|
2019-08-16 13:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 17:22:13 +00:00
|
|
|
Optimizer::PassToken CreateConvertRelaxedToHalfPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::ConvertToHalfPass>());
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateRelaxFloatOpsPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::RelaxFloatOpsPass>());
|
|
|
|
}
|
|
|
|
|
2019-01-17 20:56:36 +00:00
|
|
|
Optimizer::PassToken CreateCodeSinkingPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::CodeSinkingPass>());
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:12:08 +00:00
|
|
|
Optimizer::PassToken CreateFixStorageClassPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::FixStorageClass>());
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:52:46 +00:00
|
|
|
Optimizer::PassToken CreateGraphicsRobustAccessPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::GraphicsRobustAccessPass>());
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:20:58 +00:00
|
|
|
Optimizer::PassToken CreateReplaceDescArrayAccessUsingVarIndexPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::ReplaceDescArrayAccessUsingVarIndex>());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
Optimizer::PassToken CreateSpreadVolatileSemanticsPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::SpreadVolatileSemantics>());
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:53:19 +00:00
|
|
|
Optimizer::PassToken CreateDescriptorScalarReplacementPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::DescriptorScalarReplacement>());
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:27:12 +00:00
|
|
|
Optimizer::PassToken CreateWrapOpKillPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(MakeUnique<opt::WrapOpKill>());
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:48:17 +00:00
|
|
|
Optimizer::PassToken CreateAmdExtToKhrPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::AmdExtensionToKhrPass>());
|
|
|
|
}
|
|
|
|
|
2021-03-31 18:26:36 +00:00
|
|
|
Optimizer::PassToken CreateInterpolateFixupPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::InterpFixupPass>());
|
|
|
|
}
|
|
|
|
|
2022-03-23 02:50:52 +00:00
|
|
|
Optimizer::PassToken CreateEliminateDeadInputComponentsPass() {
|
2022-11-14 18:44:26 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2022-11-25 23:48:13 +00:00
|
|
|
MakeUnique<opt::EliminateDeadIOComponentsPass>(spv::StorageClass::Input,
|
|
|
|
/* safe_mode */ false));
|
2022-11-14 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateEliminateDeadOutputComponentsPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2022-11-25 23:48:13 +00:00
|
|
|
MakeUnique<opt::EliminateDeadIOComponentsPass>(spv::StorageClass::Output,
|
|
|
|
/* safe_mode */ false));
|
2022-11-14 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateEliminateDeadInputComponentsSafePass() {
|
2022-03-23 02:50:52 +00:00
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
2022-11-25 23:48:13 +00:00
|
|
|
MakeUnique<opt::EliminateDeadIOComponentsPass>(spv::StorageClass::Input,
|
|
|
|
/* safe_mode */ true));
|
2022-03-23 02:50:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 17:23:25 +00:00
|
|
|
Optimizer::PassToken CreateAnalyzeLiveInputPass(
|
|
|
|
std::unordered_set<uint32_t>* live_locs,
|
|
|
|
std::unordered_set<uint32_t>* live_builtins) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::AnalyzeLiveInputPass>(live_locs, live_builtins));
|
|
|
|
}
|
|
|
|
|
|
|
|
Optimizer::PassToken CreateEliminateDeadOutputStoresPass(
|
|
|
|
std::unordered_set<uint32_t>* live_locs,
|
|
|
|
std::unordered_set<uint32_t>* live_builtins) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::EliminateDeadOutputStoresPass>(live_locs, live_builtins));
|
|
|
|
}
|
|
|
|
|
2021-08-18 12:30:48 +00:00
|
|
|
Optimizer::PassToken CreateConvertToSampledImagePass(
|
|
|
|
const std::vector<opt::DescriptorSetAndBinding>&
|
|
|
|
descriptor_set_binding_pairs) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::ConvertToSampledImagePass>(descriptor_set_binding_pairs));
|
|
|
|
}
|
|
|
|
|
2022-05-09 18:04:52 +00:00
|
|
|
Optimizer::PassToken CreateInterfaceVariableScalarReplacementPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::InterfaceVariableScalarReplacement>());
|
|
|
|
}
|
|
|
|
|
2022-03-07 17:45:17 +00:00
|
|
|
Optimizer::PassToken CreateRemoveDontInlinePass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::RemoveDontInline>());
|
|
|
|
}
|
2022-05-06 14:39:26 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreateFixFuncCallArgumentsPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::FixFuncCallArgumentsPass>());
|
|
|
|
}
|
2023-08-02 09:52:53 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreateTrimCapabilitiesPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::TrimCapabilitiesPass>());
|
|
|
|
}
|
2023-08-22 00:16:35 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreateSwitchDescriptorSetPass(uint32_t from, uint32_t to) {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::SwitchDescriptorSetPass>(from, to));
|
|
|
|
}
|
Add a new legalization pass to dedupe invocation interlock instructions (#5409)
Add a new legalization pass to dedupe invocation interlock instructions
DXC will be adding support for HLSL's rasterizer ordered views by using
the SPV_EXT_fragment_shader_interlock_extension. That extension
stipulates that if an entry point has an interlock ordering execution
mode, it must dynamically execute OpBeginInvocationInterlockEXT and
OpEndInvocationInterlockEXT, in that order, exactly once. This would be
difficult to determine in DXC's SPIR-V backend, so instead we will emit
these instructions potentially multiple times, and use this legalization
pass to ensure that the final SPIR-V follows the specification.
This PR uses data-flow analysis to determine where to place begin and
end instructions; in essence, determining whether a block contains or is
preceded by a begin instruction is similar to a specialized case of a
reaching definitions analysis, where we have only a single definition,
such as `bool has_begun = false`. For this simpler case, we can compute
the set of blocks using BFS to determine the reachability of the begin
instruction.
We need to do this for both begin and end instructions, so I have
generalized portions of the code to run both forward and backward over
the CFG for each respective case.
2023-09-27 23:54:10 +00:00
|
|
|
|
|
|
|
Optimizer::PassToken CreateInvocationInterlockPlacementPass() {
|
|
|
|
return MakeUnique<Optimizer::PassToken::Impl>(
|
|
|
|
MakeUnique<opt::InvocationInterlockPlacementPass>());
|
|
|
|
}
|
2016-09-12 18:11:46 +00:00
|
|
|
} // namespace spvtools
|
2023-02-01 13:58:52 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT spv_optimizer_t* spvOptimizerCreate(spv_target_env env) {
|
|
|
|
return reinterpret_cast<spv_optimizer_t*>(new spvtools::Optimizer(env));
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT void spvOptimizerDestroy(spv_optimizer_t* optimizer) {
|
|
|
|
delete reinterpret_cast<spvtools::Optimizer*>(optimizer);
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT void spvOptimizerSetMessageConsumer(
|
|
|
|
spv_optimizer_t* optimizer, spv_message_consumer consumer) {
|
|
|
|
reinterpret_cast<spvtools::Optimizer*>(optimizer)->
|
|
|
|
SetMessageConsumer(
|
|
|
|
[consumer](spv_message_level_t level, const char* source,
|
|
|
|
const spv_position_t& position, const char* message) {
|
|
|
|
return consumer(level, source, &position, message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT void spvOptimizerRegisterLegalizationPasses(
|
|
|
|
spv_optimizer_t* optimizer) {
|
|
|
|
reinterpret_cast<spvtools::Optimizer*>(optimizer)->
|
|
|
|
RegisterLegalizationPasses();
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT void spvOptimizerRegisterPerformancePasses(
|
|
|
|
spv_optimizer_t* optimizer) {
|
|
|
|
reinterpret_cast<spvtools::Optimizer*>(optimizer)->
|
|
|
|
RegisterPerformancePasses();
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT void spvOptimizerRegisterSizePasses(
|
|
|
|
spv_optimizer_t* optimizer) {
|
|
|
|
reinterpret_cast<spvtools::Optimizer*>(optimizer)->RegisterSizePasses();
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT bool spvOptimizerRegisterPassFromFlag(
|
|
|
|
spv_optimizer_t* optimizer, const char* flag)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<spvtools::Optimizer*>(optimizer)->
|
|
|
|
RegisterPassFromFlag(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT bool spvOptimizerRegisterPassesFromFlags(
|
|
|
|
spv_optimizer_t* optimizer, const char** flags, const size_t flag_count) {
|
|
|
|
std::vector<std::string> opt_flags;
|
|
|
|
for (uint32_t i = 0; i < flag_count; i++) {
|
|
|
|
opt_flags.emplace_back(flags[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<spvtools::Optimizer*>(optimizer)->
|
|
|
|
RegisterPassesFromFlags(opt_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
SPIRV_TOOLS_EXPORT
|
|
|
|
spv_result_t spvOptimizerRun(spv_optimizer_t* optimizer,
|
|
|
|
const uint32_t* binary,
|
|
|
|
const size_t word_count,
|
|
|
|
spv_binary* optimized_binary,
|
|
|
|
const spv_optimizer_options options) {
|
|
|
|
std::vector<uint32_t> optimized;
|
|
|
|
|
|
|
|
if (!reinterpret_cast<spvtools::Optimizer*>(optimizer)->
|
|
|
|
Run(binary, word_count, &optimized, options)) {
|
|
|
|
return SPV_ERROR_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result_binary = new spv_binary_t();
|
|
|
|
if (!result_binary) {
|
|
|
|
*optimized_binary = nullptr;
|
|
|
|
return SPV_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
result_binary->code = new uint32_t[optimized.size()];
|
|
|
|
if (!result_binary->code) {
|
|
|
|
delete result_binary;
|
|
|
|
*optimized_binary = nullptr;
|
|
|
|
return SPV_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
result_binary->wordCount = optimized.size();
|
|
|
|
|
|
|
|
memcpy(result_binary->code, optimized.data(),
|
|
|
|
optimized.size() * sizeof(uint32_t));
|
|
|
|
|
|
|
|
*optimized_binary = result_binary;
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|