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.
|
|
|
|
|
|
|
|
#include "local_single_store_elim_pass.h"
|
|
|
|
|
|
|
|
#include "cfa.h"
|
|
|
|
#include "iterator.h"
|
2017-12-13 22:55:00 +00:00
|
|
|
#include "latest_version_glsl_std_450_header.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-04-11 15:58:47 +00:00
|
|
|
bool LocalSingleStoreElimPass::LocalSingleStoreElim(ir::Function* func) {
|
|
|
|
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|.
|
|
|
|
ir::BasicBlock* entry_block = &*func->begin();
|
|
|
|
for (ir::Instruction& inst : *entry_block) {
|
|
|
|
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-10-30 15:13:24 +00:00
|
|
|
void LocalSingleStoreElimPass::Initialize(ir::IRContext* irContext) {
|
|
|
|
InitializeProcessing(irContext);
|
2018-04-11 15:58:47 +00:00
|
|
|
InitExtensionWhiteList();
|
2018-03-21 09:15:56 +00:00
|
|
|
}
|
2017-05-19 23:31:28 +00:00
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
bool LocalSingleStoreElimPass::AllExtensionsSupported() const {
|
|
|
|
// If any extension not in whitelist, return false
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ei : get_module()->extensions()) {
|
2017-11-08 17:40:02 +00:00
|
|
|
const char* extName =
|
|
|
|
reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
|
2017-07-19 00:57:26 +00:00
|
|
|
if (extensions_whitelist_.find(extName) == extensions_whitelist_.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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
|
2017-08-10 22:42:16 +00:00
|
|
|
ProcessFunction pfn = [this](ir::Function* fp) {
|
|
|
|
return LocalSingleStoreElim(fp);
|
|
|
|
};
|
2017-10-25 17:26:25 +00:00
|
|
|
bool modified = ProcessEntryPointCallTree(pfn, get_module());
|
2017-05-19 23:31:28 +00:00
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:26:25 +00:00
|
|
|
LocalSingleStoreElimPass::LocalSingleStoreElimPass() {}
|
2017-05-19 23:31:28 +00:00
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
Pass::Status LocalSingleStoreElimPass::Process(ir::IRContext* irContext) {
|
|
|
|
Initialize(irContext);
|
2017-05-19 23:31:28 +00:00
|
|
|
return ProcessImpl();
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:58:47 +00:00
|
|
|
void LocalSingleStoreElimPass::InitExtensionWhiteList() {
|
2017-07-19 00:57:26 +00:00
|
|
|
extensions_whitelist_.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",
|
|
|
|
"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",
|
|
|
|
// SPV_KHR_variable_pointers
|
|
|
|
// Currently do not support extended pointer expressions
|
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",
|
2017-07-19 00:57:26 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-11 15:58:47 +00:00
|
|
|
bool LocalSingleStoreElimPass::ProcessVariable(ir::Instruction* var_inst) {
|
|
|
|
vector<ir::Instruction*> users;
|
|
|
|
FindUses(var_inst, &users);
|
|
|
|
|
|
|
|
ir::Instruction* store_inst = FindSingleStoreAndCheckUses(var_inst, users);
|
|
|
|
|
|
|
|
if (store_inst == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RewriteLoads(store_inst, users);
|
|
|
|
}
|
|
|
|
|
|
|
|
ir::Instruction* LocalSingleStoreElimPass::FindSingleStoreAndCheckUses(
|
|
|
|
ir::Instruction* var_inst, const vector<ir::Instruction*>& users) const {
|
|
|
|
// Make sure there is exactly 1 store.
|
|
|
|
ir::Instruction* store_inst = nullptr;
|
|
|
|
|
|
|
|
// If |var_inst| has an initializer, then that will count as a store.
|
|
|
|
if (var_inst->NumInOperands() > 1) {
|
|
|
|
store_inst = var_inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ir::Instruction* user : users) {
|
|
|
|
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;
|
|
|
|
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(
|
|
|
|
const ir::Instruction* var_inst,
|
|
|
|
std::vector<ir::Instruction*>* users) const {
|
|
|
|
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
|
|
|
|
def_use_mgr->ForEachUser(var_inst, [users, this](ir::Instruction* user) {
|
|
|
|
users->push_back(user);
|
|
|
|
if (user->opcode() == SpvOpCopyObject) {
|
|
|
|
FindUses(user, users);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalSingleStoreElimPass::FeedsAStore(ir::Instruction* inst) const {
|
|
|
|
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
|
|
|
|
return !def_use_mgr->WhileEachUser(inst, [this](ir::Instruction* user) {
|
|
|
|
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(
|
|
|
|
ir::Instruction* store_inst, const std::vector<ir::Instruction*>& uses) {
|
|
|
|
ir::BasicBlock* store_block = context()->get_instr_block(store_inst);
|
|
|
|
opt::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);
|
|
|
|
|
|
|
|
std::vector<ir::Instruction*> uses_in_store_block;
|
|
|
|
bool modified = false;
|
|
|
|
for (ir::Instruction* use : uses) {
|
|
|
|
if (use->opcode() == SpvOpLoad) {
|
|
|
|
if (dominator_analysis->Dominates(store_inst, use)) {
|
|
|
|
modified = true;
|
|
|
|
context()->KillNamesAndDecorates(use->result_id());
|
|
|
|
context()->ReplaceAllUsesWith(use->result_id(), stored_id);
|
|
|
|
context()->KillInst(use);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
2017-07-19 00:57:26 +00:00
|
|
|
|
2017-05-19 23:31:28 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|