2017-05-19 23:31:28 +00:00
|
|
|
// Copyright (c) 2017 The Khronos Group Inc.
|
|
|
|
// Copyright (c) 2017 Valve Corporation
|
|
|
|
// Copyright (c) 2017 LunarG 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 19:06:09 +00:00
|
|
|
#include "source/opt/local_single_store_elim_pass.h"
|
2017-05-19 23:31:28 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/cfa.h"
|
|
|
|
#include "source/latest_version_glsl_std_450_header.h"
|
|
|
|
#include "source/opt/iterator.h"
|
2021-12-08 17:01:26 +00:00
|
|
|
#include "source/util/string_utils.h"
|
2017-05-19 23:31:28 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
2017-07-18 20:42:51 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const uint32_t kStoreValIdInIdx = 1;
|
2017-12-07 19:11:29 +00:00
|
|
|
const uint32_t kVariableInitIdInIdx = 1;
|
2017-07-18 20:42:51 +00:00
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
} // anonymous namespace
|
2017-07-18 20:42:51 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool LocalSingleStoreElimPass::LocalSingleStoreElim(Function* func) {
|
2018-04-11 15:58:47 +00:00
|
|
|
bool modified = false;
|
2017-05-19 23:31:28 +00:00
|
|
|
|
2018-04-11 15:58:47 +00:00
|
|
|
// Check all function scope variables in |func|.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* entry_block = &*func->begin();
|
|
|
|
for (Instruction& inst : *entry_block) {
|
2018-04-11 15:58:47 +00:00
|
|
|
if (inst.opcode() != SpvOpVariable) {
|
|
|
|
break;
|
2017-05-19 23:31:28 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:58:47 +00:00
|
|
|
modified |= ProcessVariable(&inst);
|
2017-05-19 23:31:28 +00:00
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
bool LocalSingleStoreElimPass::AllExtensionsSupported() const {
|
2020-06-15 17:20:40 +00:00
|
|
|
// If any extension not in allowlist, return false
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ei : get_module()->extensions()) {
|
2021-12-08 17:01:26 +00:00
|
|
|
const std::string extName = ei.GetInOperand(0).AsString();
|
2020-06-15 17:20:40 +00:00
|
|
|
if (extensions_allowlist_.find(extName) == extensions_allowlist_.end())
|
2017-07-19 00:57:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-15 18:38:53 +00:00
|
|
|
// only allow NonSemantic.Shader.DebugInfo.100, we cannot safely optimise
|
|
|
|
// around unknown extended
|
|
|
|
// instruction sets even if they are non-semantic
|
|
|
|
for (auto& inst : context()->module()->ext_inst_imports()) {
|
|
|
|
assert(inst.opcode() == SpvOpExtInstImport &&
|
|
|
|
"Expecting an import of an extension's instruction set.");
|
2021-12-08 17:01:26 +00:00
|
|
|
const std::string extension_name = inst.GetInOperand(0).AsString();
|
|
|
|
if (spvtools::utils::starts_with(extension_name, "NonSemantic.") &&
|
|
|
|
extension_name != "NonSemantic.Shader.DebugInfo.100") {
|
2021-09-15 18:38:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 00:57:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:31:28 +00:00
|
|
|
Pass::Status LocalSingleStoreElimPass::ProcessImpl() {
|
2017-12-11 18:10:24 +00:00
|
|
|
// Assumes relaxed logical addressing only (see instruction.h)
|
2017-12-19 19:18:13 +00:00
|
|
|
if (context()->get_feature_mgr()->HasCapability(SpvCapabilityAddresses))
|
2017-05-19 23:31:28 +00:00
|
|
|
return Status::SuccessWithoutChange;
|
2018-04-11 15:58:47 +00:00
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
// Do not process if any disallowed extensions are enabled
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
|
2017-07-19 00:57:26 +00:00
|
|
|
// Process all entry point functions
|
2018-07-12 19:14:43 +00:00
|
|
|
ProcessFunction pfn = [this](Function* fp) {
|
2017-08-10 22:42:16 +00:00
|
|
|
return LocalSingleStoreElim(fp);
|
|
|
|
};
|
2021-10-18 17:18:16 +00:00
|
|
|
bool modified = context()->ProcessReachableCallTree(pfn);
|
2017-05-19 23:31:28 +00:00
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
LocalSingleStoreElimPass::LocalSingleStoreElimPass() = default;
|
2017-05-19 23:31:28 +00:00
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
Pass::Status LocalSingleStoreElimPass::Process() {
|
2020-06-15 17:20:40 +00:00
|
|
|
InitExtensionAllowList();
|
2017-05-19 23:31:28 +00:00
|
|
|
return ProcessImpl();
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:20:40 +00:00
|
|
|
void LocalSingleStoreElimPass::InitExtensionAllowList() {
|
|
|
|
extensions_allowlist_.insert({
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_AMD_shader_explicit_vertex_parameter",
|
2017-11-27 15:16:41 +00:00
|
|
|
"SPV_AMD_shader_trinary_minmax",
|
|
|
|
"SPV_AMD_gcn_shader",
|
|
|
|
"SPV_KHR_shader_ballot",
|
|
|
|
"SPV_AMD_shader_ballot",
|
|
|
|
"SPV_AMD_gpu_shader_half_float",
|
|
|
|
"SPV_KHR_shader_draw_parameters",
|
|
|
|
"SPV_KHR_subgroup_vote",
|
2020-09-08 18:13:01 +00:00
|
|
|
"SPV_KHR_8bit_storage",
|
2017-11-27 15:16:41 +00:00
|
|
|
"SPV_KHR_16bit_storage",
|
|
|
|
"SPV_KHR_device_group",
|
|
|
|
"SPV_KHR_multiview",
|
|
|
|
"SPV_NVX_multiview_per_view_attributes",
|
|
|
|
"SPV_NV_viewport_array2",
|
|
|
|
"SPV_NV_stereo_view_rendering",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_NV_sample_mask_override_coverage",
|
2017-11-27 15:16:41 +00:00
|
|
|
"SPV_NV_geometry_shader_passthrough",
|
|
|
|
"SPV_AMD_texture_gather_bias_lod",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_KHR_storage_buffer_storage_class",
|
2019-04-03 16:47:51 +00:00
|
|
|
"SPV_KHR_variable_pointers",
|
2017-11-27 15:16:41 +00:00
|
|
|
"SPV_AMD_gpu_shader_int16",
|
|
|
|
"SPV_KHR_post_depth_coverage",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_KHR_shader_atomic_counter_ops",
|
2018-03-08 13:54:00 +00:00
|
|
|
"SPV_EXT_shader_stencil_export",
|
|
|
|
"SPV_EXT_shader_viewport_index_layer",
|
|
|
|
"SPV_AMD_shader_image_load_store_lod",
|
|
|
|
"SPV_AMD_shader_fragment_mask",
|
|
|
|
"SPV_EXT_fragment_fully_covered",
|
|
|
|
"SPV_AMD_gpu_shader_half_float_fetch",
|
2018-03-08 20:33:28 +00:00
|
|
|
"SPV_GOOGLE_decorate_string",
|
|
|
|
"SPV_GOOGLE_hlsl_functionality1",
|
2018-04-06 14:18:34 +00:00
|
|
|
"SPV_NV_shader_subgroup_partitioned",
|
|
|
|
"SPV_EXT_descriptor_indexing",
|
2018-09-19 18:53:33 +00:00
|
|
|
"SPV_NV_fragment_shader_barycentric",
|
|
|
|
"SPV_NV_compute_shader_derivatives",
|
|
|
|
"SPV_NV_shader_image_footprint",
|
|
|
|
"SPV_NV_shading_rate",
|
|
|
|
"SPV_NV_mesh_shader",
|
2018-10-25 18:07:46 +00:00
|
|
|
"SPV_NV_ray_tracing",
|
2020-03-17 19:30:19 +00:00
|
|
|
"SPV_KHR_ray_query",
|
2018-11-23 15:21:19 +00:00
|
|
|
"SPV_EXT_fragment_invocation_density",
|
2019-08-08 13:45:59 +00:00
|
|
|
"SPV_EXT_physical_storage_buffer",
|
2020-07-22 15:45:02 +00:00
|
|
|
"SPV_KHR_terminate_invocation",
|
2021-06-15 14:07:42 +00:00
|
|
|
"SPV_KHR_subgroup_uniform_control_flow",
|
2021-06-23 17:32:24 +00:00
|
|
|
"SPV_KHR_integer_dot_product",
|
2021-07-14 12:43:35 +00:00
|
|
|
"SPV_EXT_shader_image_int64",
|
2021-09-15 18:38:53 +00:00
|
|
|
"SPV_KHR_non_semantic_info",
|
2022-03-25 12:32:50 +00:00
|
|
|
"SPV_KHR_uniform_group_instructions",
|
2022-05-25 13:20:39 +00:00
|
|
|
"SPV_KHR_fragment_shader_barycentric",
|
2017-07-19 00:57:26 +00:00
|
|
|
});
|
|
|
|
}
|
2018-07-12 19:14:43 +00:00
|
|
|
bool LocalSingleStoreElimPass::ProcessVariable(Instruction* var_inst) {
|
2018-08-01 18:58:12 +00:00
|
|
|
std::vector<Instruction*> users;
|
2018-04-11 15:58:47 +00:00
|
|
|
FindUses(var_inst, &users);
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* store_inst = FindSingleStoreAndCheckUses(var_inst, users);
|
2018-04-11 15:58:47 +00:00
|
|
|
|
|
|
|
if (store_inst == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-10 19:17:14 +00:00
|
|
|
bool all_rewritten;
|
|
|
|
bool modified = RewriteLoads(store_inst, users, &all_rewritten);
|
|
|
|
|
|
|
|
// If all uses are rewritten and the variable has a DebugDeclare and the
|
|
|
|
// variable is not an aggregate, add a DebugValue after the store and remove
|
|
|
|
// the DebugDeclare.
|
|
|
|
uint32_t var_id = var_inst->result_id();
|
|
|
|
if (all_rewritten &&
|
2020-07-30 16:18:06 +00:00
|
|
|
context()->get_debug_info_mgr()->IsVariableDebugDeclared(var_id)) {
|
2020-07-10 19:17:14 +00:00
|
|
|
const analysis::Type* var_type =
|
|
|
|
context()->get_type_mgr()->GetType(var_inst->type_id());
|
|
|
|
const analysis::Type* store_type = var_type->AsPointer()->pointee_type();
|
|
|
|
if (!(store_type->AsStruct() || store_type->AsArray())) {
|
Add DebugValue for invisible store in single_store_elim (#4002)
The front-end language compiler would simply emit DebugDeclare for
a variable when it is declared, which is effective through the variable's
scope. Since DebugDeclare only maps an OpVariable to a local variable,
the information can be removed when an optimization pass uses the
loaded value of the variable. DebugValue can be used to specify the
value of a variable. For each value update or phi instruction of a variable,
we can add DebugValue to help debugger inspect the variable at any
point of the program execution.
For example,
float a = 3;
... (complicated cfg) ...
foo(a); // <-- variable inspection: debugger can find DebugValue of `float a` in the nearest dominant
For the code with complicated CFG e.g., for-loop, if-statement, we
need help of ssa-rewrite to analyze the effective value of each variable
in each basic block.
If the value update of the variable happens only once and it dominates
all its uses, local-single-store-elim pass conducts the same value update
with ssa-rewrite and we have to let it add DebugValue for the value assignment.
One main issue is that we have to add DebugValue only when the value
update of a variable is visible to DebugDeclare. For example,
```
{ // scope1
%stack = OpVariable %ptr_int %int_3
{ // scope2
DebugDeclare %foo %stack <-- local variable "foo" in high-level language source code is declared as OpVariable "%stack"
// add DebugValue "foo = 3"
...
Store %stack %int_7 <-- foo = 7, add DebugValue "foo = 7"
...
// debugger can inspect the value of "foo"
}
Store %stack %int_11 <-- out of "scope2" i.e., scope of "foo". DO NOT add DebugValue "foo = 11"
}
```
However, the initalization of a variable is an exception.
For example, an argument passing of an inlined function must be done out of
the function's scope, but we must add a DebugValue for it.
```
// in HLSL
bar(float arg) { ... }
...
float foo = 3;
bar(foo);
// in SPIR-V
%arg = OpVariable
OpStore %arg %foo <-- Argument passing. Out of "float arg" scope, but we must add DebugValue for "float arg"
... body of function bar(float arg) ...
```
This PR handles the except case in local-single-store-elim pass. It adds
DebugValue for a store that is considered as an initialization.
The same exception handling code for ssa-rewrite is done by this commit: df4198e50eaa705298167d3c0b3cb01ffd28a5ff.
2020-11-04 18:43:59 +00:00
|
|
|
modified |= RewriteDebugDeclares(store_inst, var_id);
|
2020-07-10 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
2018-04-11 15:58:47 +00:00
|
|
|
}
|
|
|
|
|
Add DebugValue for invisible store in single_store_elim (#4002)
The front-end language compiler would simply emit DebugDeclare for
a variable when it is declared, which is effective through the variable's
scope. Since DebugDeclare only maps an OpVariable to a local variable,
the information can be removed when an optimization pass uses the
loaded value of the variable. DebugValue can be used to specify the
value of a variable. For each value update or phi instruction of a variable,
we can add DebugValue to help debugger inspect the variable at any
point of the program execution.
For example,
float a = 3;
... (complicated cfg) ...
foo(a); // <-- variable inspection: debugger can find DebugValue of `float a` in the nearest dominant
For the code with complicated CFG e.g., for-loop, if-statement, we
need help of ssa-rewrite to analyze the effective value of each variable
in each basic block.
If the value update of the variable happens only once and it dominates
all its uses, local-single-store-elim pass conducts the same value update
with ssa-rewrite and we have to let it add DebugValue for the value assignment.
One main issue is that we have to add DebugValue only when the value
update of a variable is visible to DebugDeclare. For example,
```
{ // scope1
%stack = OpVariable %ptr_int %int_3
{ // scope2
DebugDeclare %foo %stack <-- local variable "foo" in high-level language source code is declared as OpVariable "%stack"
// add DebugValue "foo = 3"
...
Store %stack %int_7 <-- foo = 7, add DebugValue "foo = 7"
...
// debugger can inspect the value of "foo"
}
Store %stack %int_11 <-- out of "scope2" i.e., scope of "foo". DO NOT add DebugValue "foo = 11"
}
```
However, the initalization of a variable is an exception.
For example, an argument passing of an inlined function must be done out of
the function's scope, but we must add a DebugValue for it.
```
// in HLSL
bar(float arg) { ... }
...
float foo = 3;
bar(foo);
// in SPIR-V
%arg = OpVariable
OpStore %arg %foo <-- Argument passing. Out of "float arg" scope, but we must add DebugValue for "float arg"
... body of function bar(float arg) ...
```
This PR handles the except case in local-single-store-elim pass. It adds
DebugValue for a store that is considered as an initialization.
The same exception handling code for ssa-rewrite is done by this commit: df4198e50eaa705298167d3c0b3cb01ffd28a5ff.
2020-11-04 18:43:59 +00:00
|
|
|
bool LocalSingleStoreElimPass::RewriteDebugDeclares(Instruction* store_inst,
|
|
|
|
uint32_t var_id) {
|
|
|
|
std::unordered_set<Instruction*> invisible_decls;
|
|
|
|
uint32_t value_id = store_inst->GetSingleWordInOperand(1);
|
|
|
|
bool modified =
|
|
|
|
context()->get_debug_info_mgr()->AddDebugValueIfVarDeclIsVisible(
|
|
|
|
store_inst, var_id, value_id, store_inst, &invisible_decls);
|
|
|
|
|
|
|
|
// For cases like the argument passing for an inlined function, the value
|
|
|
|
// assignment is out of DebugDeclare's scope, but we have to preserve the
|
|
|
|
// value assignment information using DebugValue. Generally, we need
|
|
|
|
// ssa-rewrite analysis to decide a proper value assignment but at this point
|
|
|
|
// we confirm that |var_id| has a single store. We can safely add DebugValue.
|
|
|
|
if (!invisible_decls.empty()) {
|
|
|
|
BasicBlock* store_block = context()->get_instr_block(store_inst);
|
|
|
|
DominatorAnalysis* dominator_analysis =
|
|
|
|
context()->GetDominatorAnalysis(store_block->GetParent());
|
|
|
|
for (auto* decl : invisible_decls) {
|
|
|
|
if (dominator_analysis->Dominates(store_inst, decl)) {
|
2020-11-13 17:06:38 +00:00
|
|
|
context()->get_debug_info_mgr()->AddDebugValueForDecl(decl, value_id,
|
2021-01-28 17:57:35 +00:00
|
|
|
decl, store_inst);
|
Add DebugValue for invisible store in single_store_elim (#4002)
The front-end language compiler would simply emit DebugDeclare for
a variable when it is declared, which is effective through the variable's
scope. Since DebugDeclare only maps an OpVariable to a local variable,
the information can be removed when an optimization pass uses the
loaded value of the variable. DebugValue can be used to specify the
value of a variable. For each value update or phi instruction of a variable,
we can add DebugValue to help debugger inspect the variable at any
point of the program execution.
For example,
float a = 3;
... (complicated cfg) ...
foo(a); // <-- variable inspection: debugger can find DebugValue of `float a` in the nearest dominant
For the code with complicated CFG e.g., for-loop, if-statement, we
need help of ssa-rewrite to analyze the effective value of each variable
in each basic block.
If the value update of the variable happens only once and it dominates
all its uses, local-single-store-elim pass conducts the same value update
with ssa-rewrite and we have to let it add DebugValue for the value assignment.
One main issue is that we have to add DebugValue only when the value
update of a variable is visible to DebugDeclare. For example,
```
{ // scope1
%stack = OpVariable %ptr_int %int_3
{ // scope2
DebugDeclare %foo %stack <-- local variable "foo" in high-level language source code is declared as OpVariable "%stack"
// add DebugValue "foo = 3"
...
Store %stack %int_7 <-- foo = 7, add DebugValue "foo = 7"
...
// debugger can inspect the value of "foo"
}
Store %stack %int_11 <-- out of "scope2" i.e., scope of "foo". DO NOT add DebugValue "foo = 11"
}
```
However, the initalization of a variable is an exception.
For example, an argument passing of an inlined function must be done out of
the function's scope, but we must add a DebugValue for it.
```
// in HLSL
bar(float arg) { ... }
...
float foo = 3;
bar(foo);
// in SPIR-V
%arg = OpVariable
OpStore %arg %foo <-- Argument passing. Out of "float arg" scope, but we must add DebugValue for "float arg"
... body of function bar(float arg) ...
```
This PR handles the except case in local-single-store-elim pass. It adds
DebugValue for a store that is considered as an initialization.
The same exception handling code for ssa-rewrite is done by this commit: df4198e50eaa705298167d3c0b3cb01ffd28a5ff.
2020-11-04 18:43:59 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
modified |= context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* LocalSingleStoreElimPass::FindSingleStoreAndCheckUses(
|
2018-08-01 18:58:12 +00:00
|
|
|
Instruction* var_inst, const std::vector<Instruction*>& users) const {
|
2018-04-11 15:58:47 +00:00
|
|
|
// Make sure there is exactly 1 store.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* store_inst = nullptr;
|
2018-04-11 15:58:47 +00:00
|
|
|
|
|
|
|
// If |var_inst| has an initializer, then that will count as a store.
|
|
|
|
if (var_inst->NumInOperands() > 1) {
|
|
|
|
store_inst = var_inst;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* user : users) {
|
2018-04-11 15:58:47 +00:00
|
|
|
switch (user->opcode()) {
|
|
|
|
case SpvOpStore:
|
|
|
|
// Since we are in the relaxed addressing mode, the use has to be the
|
|
|
|
// base address of the store, and not the value being store. Otherwise,
|
|
|
|
// we would have a pointer to a pointer to function scope memory, which
|
|
|
|
// is not allowed.
|
|
|
|
if (store_inst == nullptr) {
|
|
|
|
store_inst = user;
|
|
|
|
} else {
|
|
|
|
// More than 1 store.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOpAccessChain:
|
|
|
|
case SpvOpInBoundsAccessChain:
|
|
|
|
if (FeedsAStore(user)) {
|
|
|
|
// Has a partial store. Cannot propagate that.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpvOpLoad:
|
|
|
|
case SpvOpImageTexelPointer:
|
|
|
|
case SpvOpName:
|
|
|
|
case SpvOpCopyObject:
|
|
|
|
break;
|
2020-07-10 19:17:14 +00:00
|
|
|
case SpvOpExtInst: {
|
2021-07-21 16:04:38 +00:00
|
|
|
auto dbg_op = user->GetCommonDebugOpcode();
|
|
|
|
if (dbg_op == CommonDebugInfoDebugDeclare ||
|
|
|
|
dbg_op == CommonDebugInfoDebugValue) {
|
2020-07-10 19:17:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-04-11 15:58:47 +00:00
|
|
|
default:
|
|
|
|
if (!user->IsDecoration()) {
|
|
|
|
// Don't know if this instruction modifies the variable.
|
|
|
|
// Conservatively assume it is a store.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return store_inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalSingleStoreElimPass::FindUses(
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* var_inst, std::vector<Instruction*>* users) const {
|
2018-04-11 15:58:47 +00:00
|
|
|
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
|
2018-07-12 19:14:43 +00:00
|
|
|
def_use_mgr->ForEachUser(var_inst, [users, this](Instruction* user) {
|
2018-04-11 15:58:47 +00:00
|
|
|
users->push_back(user);
|
|
|
|
if (user->opcode() == SpvOpCopyObject) {
|
|
|
|
FindUses(user, users);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool LocalSingleStoreElimPass::FeedsAStore(Instruction* inst) const {
|
2018-04-11 15:58:47 +00:00
|
|
|
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
|
2018-07-12 19:14:43 +00:00
|
|
|
return !def_use_mgr->WhileEachUser(inst, [this](Instruction* user) {
|
2018-04-11 15:58:47 +00:00
|
|
|
switch (user->opcode()) {
|
|
|
|
case SpvOpStore:
|
|
|
|
return false;
|
|
|
|
case SpvOpAccessChain:
|
|
|
|
case SpvOpInBoundsAccessChain:
|
|
|
|
case SpvOpCopyObject:
|
|
|
|
return !FeedsAStore(user);
|
|
|
|
case SpvOpLoad:
|
|
|
|
case SpvOpImageTexelPointer:
|
|
|
|
case SpvOpName:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
// Don't know if this instruction modifies the variable.
|
|
|
|
// Conservatively assume it is a store.
|
|
|
|
return user->IsDecoration();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalSingleStoreElimPass::RewriteLoads(
|
2020-07-10 19:17:14 +00:00
|
|
|
Instruction* store_inst, const std::vector<Instruction*>& uses,
|
|
|
|
bool* all_rewritten) {
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* store_block = context()->get_instr_block(store_inst);
|
|
|
|
DominatorAnalysis* dominator_analysis =
|
2018-04-20 16:28:40 +00:00
|
|
|
context()->GetDominatorAnalysis(store_block->GetParent());
|
2018-04-11 15:58:47 +00:00
|
|
|
|
|
|
|
uint32_t stored_id;
|
|
|
|
if (store_inst->opcode() == SpvOpStore)
|
|
|
|
stored_id = store_inst->GetSingleWordInOperand(kStoreValIdInIdx);
|
|
|
|
else
|
|
|
|
stored_id = store_inst->GetSingleWordInOperand(kVariableInitIdInIdx);
|
|
|
|
|
2020-07-10 19:17:14 +00:00
|
|
|
*all_rewritten = true;
|
2018-04-11 15:58:47 +00:00
|
|
|
bool modified = false;
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* use : uses) {
|
2020-07-10 19:17:14 +00:00
|
|
|
if (use->opcode() == SpvOpStore) continue;
|
2021-07-21 16:04:38 +00:00
|
|
|
auto dbg_op = use->GetCommonDebugOpcode();
|
|
|
|
if (dbg_op == CommonDebugInfoDebugDeclare ||
|
|
|
|
dbg_op == CommonDebugInfoDebugValue)
|
2020-07-10 19:17:14 +00:00
|
|
|
continue;
|
|
|
|
if (use->opcode() == SpvOpLoad &&
|
|
|
|
dominator_analysis->Dominates(store_inst, use)) {
|
|
|
|
modified = true;
|
|
|
|
context()->KillNamesAndDecorates(use->result_id());
|
|
|
|
context()->ReplaceAllUsesWith(use->result_id(), stored_id);
|
|
|
|
context()->KillInst(use);
|
|
|
|
} else {
|
|
|
|
*all_rewritten = false;
|
2018-04-11 15:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
2017-07-19 00:57:26 +00:00
|
|
|
|
2017-05-19 23:31:28 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|