2017-05-12 23:27:21 +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_access_chain_convert_pass.h"
|
2017-05-12 23:27:21 +00:00
|
|
|
|
2018-08-15 13:14:21 +00:00
|
|
|
#include "ir_context.h"
|
|
|
|
#include "iterator.h"
|
2021-12-08 17:01:26 +00:00
|
|
|
#include "source/util/string_utils.h"
|
2017-05-12 23:27:21 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
2017-07-18 20:42:51 +00:00
|
|
|
namespace {
|
2022-11-17 18:02:50 +00:00
|
|
|
constexpr uint32_t kStoreValIdInIdx = 1;
|
|
|
|
constexpr uint32_t kAccessChainPtrIdInIdx = 0;
|
|
|
|
} // namespace
|
2017-07-18 20:42:51 +00:00
|
|
|
|
2017-05-12 23:27:21 +00:00
|
|
|
void LocalAccessChainConvertPass::BuildAndAppendInst(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op opcode, uint32_t typeId, uint32_t resultId,
|
2018-07-12 19:14:43 +00:00
|
|
|
const std::vector<Operand>& in_opnds,
|
|
|
|
std::vector<std::unique_ptr<Instruction>>* newInsts) {
|
|
|
|
std::unique_ptr<Instruction> newInst(
|
|
|
|
new Instruction(context(), opcode, typeId, resultId, in_opnds));
|
2017-10-25 17:26:25 +00:00
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(&*newInst);
|
2017-05-12 23:27:21 +00:00
|
|
|
newInsts->emplace_back(std::move(newInst));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LocalAccessChainConvertPass::BuildAndAppendVarLoad(
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* ptrInst, uint32_t* varId, uint32_t* varPteTypeId,
|
|
|
|
std::vector<std::unique_ptr<Instruction>>* newInsts) {
|
2017-05-12 23:27:21 +00:00
|
|
|
const uint32_t ldResultId = TakeNextId();
|
2019-09-24 18:04:54 +00:00
|
|
|
if (ldResultId == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:42:51 +00:00
|
|
|
*varId = ptrInst->GetSingleWordInOperand(kAccessChainPtrIdInIdx);
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varInst = get_def_use_mgr()->GetDef(*varId);
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(varInst->opcode() == spv::Op::OpVariable);
|
2017-05-12 23:27:21 +00:00
|
|
|
*varPteTypeId = GetPointeeTypeId(varInst);
|
2022-11-04 21:27:10 +00:00
|
|
|
BuildAndAppendInst(spv::Op::OpLoad, *varPteTypeId, ldResultId,
|
2017-11-08 17:40:02 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {*varId}}},
|
|
|
|
newInsts);
|
2017-05-12 23:27:21 +00:00
|
|
|
return ldResultId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalAccessChainConvertPass::AppendConstantOperands(
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* ptrInst, std::vector<Operand>* in_opnds) {
|
2017-05-12 23:27:21 +00:00
|
|
|
uint32_t iidIdx = 0;
|
2017-11-08 17:40:02 +00:00
|
|
|
ptrInst->ForEachInId([&iidIdx, &in_opnds, this](const uint32_t* iid) {
|
2017-05-12 23:27:21 +00:00
|
|
|
if (iidIdx > 0) {
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* cInst = get_def_use_mgr()->GetDef(*iid);
|
2022-05-10 17:02:14 +00:00
|
|
|
const auto* constant_value =
|
|
|
|
context()->get_constant_mgr()->GetConstantFromInst(cInst);
|
|
|
|
assert(constant_value != nullptr &&
|
|
|
|
"Expecting the index to be a constant.");
|
|
|
|
|
|
|
|
// We take the sign extended value because OpAccessChain interprets the
|
|
|
|
// index as signed.
|
|
|
|
int64_t long_value = constant_value->GetSignExtendedValue();
|
|
|
|
assert(long_value <= UINT32_MAX && long_value >= 0 &&
|
|
|
|
"The index value is too large for a composite insert or extract "
|
|
|
|
"instruction.");
|
|
|
|
|
|
|
|
uint32_t val = static_cast<uint32_t>(long_value);
|
2017-05-12 23:27:21 +00:00
|
|
|
in_opnds->push_back(
|
2017-11-08 17:40:02 +00:00
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {val}});
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
|
|
|
++iidIdx;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:04:54 +00:00
|
|
|
bool LocalAccessChainConvertPass::ReplaceAccessChainLoad(
|
2018-08-15 13:14:21 +00:00
|
|
|
const Instruction* address_inst, Instruction* original_load) {
|
2017-05-12 23:27:21 +00:00
|
|
|
// Build and append load of variable in ptrInst
|
2020-08-11 02:03:02 +00:00
|
|
|
if (address_inst->NumInOperands() == 1) {
|
|
|
|
// An access chain with no indices is essentially a copy. All that is
|
|
|
|
// needed is to propagate the address.
|
|
|
|
context()->ReplaceAllUsesWith(
|
|
|
|
address_inst->result_id(),
|
|
|
|
address_inst->GetSingleWordInOperand(kAccessChainPtrIdInIdx));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-15 13:14:21 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction>> new_inst;
|
2017-05-12 23:27:21 +00:00
|
|
|
uint32_t varId;
|
|
|
|
uint32_t varPteTypeId;
|
2017-11-08 17:40:02 +00:00
|
|
|
const uint32_t ldResultId =
|
2018-08-15 13:14:21 +00:00
|
|
|
BuildAndAppendVarLoad(address_inst, &varId, &varPteTypeId, &new_inst);
|
2019-09-24 18:04:54 +00:00
|
|
|
if (ldResultId == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:45:24 +00:00
|
|
|
new_inst[0]->UpdateDebugInfoFrom(original_load);
|
2018-08-24 15:55:39 +00:00
|
|
|
context()->get_decoration_mgr()->CloneDecorations(
|
2022-11-04 21:27:10 +00:00
|
|
|
original_load->result_id(), ldResultId,
|
|
|
|
{spv::Decoration::RelaxedPrecision});
|
2018-08-15 13:14:21 +00:00
|
|
|
original_load->InsertBefore(std::move(new_inst));
|
2020-10-02 14:45:24 +00:00
|
|
|
context()->get_debug_info_mgr()->AnalyzeDebugInst(
|
|
|
|
original_load->PreviousNode());
|
2018-08-15 13:14:21 +00:00
|
|
|
|
|
|
|
// Rewrite |original_load| into an extract.
|
|
|
|
Instruction::OperandList new_operands;
|
|
|
|
|
|
|
|
// copy the result id and the type id to the new operand list.
|
|
|
|
new_operands.emplace_back(original_load->GetOperand(0));
|
|
|
|
new_operands.emplace_back(original_load->GetOperand(1));
|
|
|
|
|
|
|
|
new_operands.emplace_back(
|
|
|
|
Operand({spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ldResultId}}));
|
|
|
|
AppendConstantOperands(address_inst, &new_operands);
|
2022-11-04 21:27:10 +00:00
|
|
|
original_load->SetOpcode(spv::Op::OpCompositeExtract);
|
2018-08-15 13:14:21 +00:00
|
|
|
original_load->ReplaceOperands(new_operands);
|
|
|
|
context()->UpdateDefUse(original_load);
|
2019-09-24 18:04:54 +00:00
|
|
|
return true;
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 18:04:54 +00:00
|
|
|
bool LocalAccessChainConvertPass::GenAccessChainStoreReplacement(
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* ptrInst, uint32_t valId,
|
|
|
|
std::vector<std::unique_ptr<Instruction>>* newInsts) {
|
2020-08-11 02:03:02 +00:00
|
|
|
if (ptrInst->NumInOperands() == 1) {
|
|
|
|
// An access chain with no indices is essentially a copy. However, we still
|
|
|
|
// have to create a new store because the old ones will be deleted.
|
|
|
|
BuildAndAppendInst(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op::OpStore, 0, 0,
|
2020-08-11 02:03:02 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID,
|
|
|
|
{ptrInst->GetSingleWordInOperand(kAccessChainPtrIdInIdx)}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {valId}}},
|
|
|
|
newInsts);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-12 23:27:21 +00:00
|
|
|
// Build and append load of variable in ptrInst
|
|
|
|
uint32_t varId;
|
|
|
|
uint32_t varPteTypeId;
|
2017-11-08 17:40:02 +00:00
|
|
|
const uint32_t ldResultId =
|
|
|
|
BuildAndAppendVarLoad(ptrInst, &varId, &varPteTypeId, newInsts);
|
2019-09-24 18:04:54 +00:00
|
|
|
if (ldResultId == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:55:39 +00:00
|
|
|
context()->get_decoration_mgr()->CloneDecorations(
|
2022-11-04 21:27:10 +00:00
|
|
|
varId, ldResultId, {spv::Decoration::RelaxedPrecision});
|
2017-05-12 23:27:21 +00:00
|
|
|
|
|
|
|
// Build and append Insert
|
|
|
|
const uint32_t insResultId = TakeNextId();
|
2019-09-24 18:04:54 +00:00
|
|
|
if (insResultId == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Operand> ins_in_opnds = {
|
2017-11-08 17:40:02 +00:00
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {valId}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {ldResultId}}};
|
2017-05-12 23:27:21 +00:00
|
|
|
AppendConstantOperands(ptrInst, &ins_in_opnds);
|
2022-11-04 21:27:10 +00:00
|
|
|
BuildAndAppendInst(spv::Op::OpCompositeInsert, varPteTypeId, insResultId,
|
2017-11-08 17:40:02 +00:00
|
|
|
ins_in_opnds, newInsts);
|
2017-05-12 23:27:21 +00:00
|
|
|
|
2018-08-24 15:55:39 +00:00
|
|
|
context()->get_decoration_mgr()->CloneDecorations(
|
2022-11-04 21:27:10 +00:00
|
|
|
varId, insResultId, {spv::Decoration::RelaxedPrecision});
|
2018-08-24 15:55:39 +00:00
|
|
|
|
2017-05-12 23:27:21 +00:00
|
|
|
// Build and append Store
|
2022-11-04 21:27:10 +00:00
|
|
|
BuildAndAppendInst(spv::Op::OpStore, 0, 0,
|
2017-11-08 17:40:02 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {varId}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {insResultId}}},
|
|
|
|
newInsts);
|
2019-09-24 18:04:54 +00:00
|
|
|
return true;
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 17:02:14 +00:00
|
|
|
bool LocalAccessChainConvertPass::Is32BitConstantIndexAccessChain(
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* acp) const {
|
2017-05-12 23:27:21 +00:00
|
|
|
uint32_t inIdx = 0;
|
2018-01-12 20:05:53 +00:00
|
|
|
return acp->WhileEachInId([&inIdx, this](const uint32_t* tid) {
|
2017-05-12 23:27:21 +00:00
|
|
|
if (inIdx > 0) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* opInst = get_def_use_mgr()->GetDef(*tid);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (opInst->opcode() != spv::Op::OpConstant) return false;
|
2022-05-10 17:02:14 +00:00
|
|
|
const auto* index =
|
|
|
|
context()->get_constant_mgr()->GetConstantFromInst(opInst);
|
2022-08-09 17:33:04 +00:00
|
|
|
int64_t index_value = index->GetSignExtendedValue();
|
|
|
|
if (index_value > UINT32_MAX) return false;
|
|
|
|
if (index_value < 0) return false;
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
|
|
|
++inIdx;
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
2017-05-12 23:27:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-04 18:00:30 +00:00
|
|
|
bool LocalAccessChainConvertPass::HasOnlySupportedRefs(uint32_t ptrId) {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (supported_ref_ptrs_.find(ptrId) != supported_ref_ptrs_.end()) return true;
|
2018-07-12 19:14:43 +00:00
|
|
|
if (get_def_use_mgr()->WhileEachUser(ptrId, [this](Instruction* user) {
|
2021-07-21 16:04:38 +00:00
|
|
|
if (user->GetCommonDebugOpcode() == CommonDebugInfoDebugValue ||
|
|
|
|
user->GetCommonDebugOpcode() == CommonDebugInfoDebugDeclare) {
|
2020-10-02 14:45:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op op = user->opcode();
|
|
|
|
if (IsNonPtrAccessChain(op) || op == spv::Op::OpCopyObject) {
|
2017-11-27 15:16:41 +00:00
|
|
|
if (!HasOnlySupportedRefs(user->result_id())) {
|
2018-01-12 20:05:53 +00:00
|
|
|
return false;
|
2017-11-27 15:16:41 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (op != spv::Op::OpStore && op != spv::Op::OpLoad &&
|
|
|
|
op != spv::Op::OpName && !IsNonTypeDecorate(op)) {
|
2018-01-12 20:05:53 +00:00
|
|
|
return false;
|
2017-11-27 15:16:41 +00:00
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
|
|
|
})) {
|
2017-11-14 19:11:50 +00:00
|
|
|
supported_ref_ptrs_.insert(ptrId);
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
2017-08-04 18:00:30 +00:00
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return false;
|
2017-08-04 18:00:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void LocalAccessChainConvertPass::FindTargetVars(Function* func) {
|
2017-05-12 23:27:21 +00:00
|
|
|
for (auto bi = func->begin(); bi != func->end(); ++bi) {
|
|
|
|
for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
|
|
|
|
switch (ii->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpStore:
|
|
|
|
case spv::Op::OpLoad: {
|
2017-11-08 17:40:02 +00:00
|
|
|
uint32_t varId;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* ptrInst = GetPtr(&*ii, &varId);
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!IsTargetVar(varId)) break;
|
2022-11-04 21:27:10 +00:00
|
|
|
const spv::Op op = ptrInst->opcode();
|
2017-11-08 17:40:02 +00:00
|
|
|
// Rule out variables with non-supported refs eg function calls
|
|
|
|
if (!HasOnlySupportedRefs(varId)) {
|
|
|
|
seen_non_target_vars_.insert(varId);
|
|
|
|
seen_target_vars_.erase(varId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Rule out variables with nested access chains
|
|
|
|
// TODO(): Convert nested access chains
|
2022-06-14 17:06:38 +00:00
|
|
|
bool is_non_ptr_access_chain = IsNonPtrAccessChain(op);
|
|
|
|
if (is_non_ptr_access_chain && ptrInst->GetSingleWordInOperand(
|
2017-11-27 15:16:41 +00:00
|
|
|
kAccessChainPtrIdInIdx) != varId) {
|
2017-11-08 17:40:02 +00:00
|
|
|
seen_non_target_vars_.insert(varId);
|
|
|
|
seen_target_vars_.erase(varId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Rule out variables accessed with non-constant indices
|
2022-05-10 17:02:14 +00:00
|
|
|
if (!Is32BitConstantIndexAccessChain(ptrInst)) {
|
2017-11-08 17:40:02 +00:00
|
|
|
seen_non_target_vars_.insert(varId);
|
|
|
|
seen_target_vars_.erase(varId);
|
|
|
|
break;
|
|
|
|
}
|
2022-06-14 17:06:38 +00:00
|
|
|
|
|
|
|
if (is_non_ptr_access_chain && AnyIndexIsOutOfBounds(ptrInst)) {
|
|
|
|
seen_non_target_vars_.insert(varId);
|
|
|
|
seen_target_vars_.erase(varId);
|
|
|
|
break;
|
|
|
|
}
|
2017-11-08 17:40:02 +00:00
|
|
|
} break;
|
|
|
|
default:
|
2017-07-18 20:42:51 +00:00
|
|
|
break;
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:04:54 +00:00
|
|
|
Pass::Status LocalAccessChainConvertPass::ConvertLocalAccessChains(
|
|
|
|
Function* func) {
|
2017-05-12 23:27:21 +00:00
|
|
|
FindTargetVars(func);
|
|
|
|
// Replace access chains of all targeted variables with equivalent
|
|
|
|
// extract and insert sequences
|
|
|
|
bool modified = false;
|
|
|
|
for (auto bi = func->begin(); bi != func->end(); ++bi) {
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> dead_instructions;
|
2017-05-12 23:27:21 +00:00
|
|
|
for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
|
|
|
|
switch (ii->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLoad: {
|
2017-11-08 17:40:02 +00:00
|
|
|
uint32_t varId;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* ptrInst = GetPtr(&*ii, &varId);
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!IsNonPtrAccessChain(ptrInst->opcode())) break;
|
|
|
|
if (!IsTargetVar(varId)) break;
|
2019-09-24 18:04:54 +00:00
|
|
|
if (!ReplaceAccessChainLoad(ptrInst, &*ii)) {
|
|
|
|
return Status::Failure;
|
|
|
|
}
|
2017-11-08 17:40:02 +00:00
|
|
|
modified = true;
|
|
|
|
} break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpStore: {
|
2017-11-08 17:40:02 +00:00
|
|
|
uint32_t varId;
|
2020-10-02 14:45:24 +00:00
|
|
|
Instruction* store = &*ii;
|
|
|
|
Instruction* ptrInst = GetPtr(store, &varId);
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!IsNonPtrAccessChain(ptrInst->opcode())) break;
|
|
|
|
if (!IsTargetVar(varId)) break;
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction>> newInsts;
|
2020-10-02 14:45:24 +00:00
|
|
|
uint32_t valId = store->GetSingleWordInOperand(kStoreValIdInIdx);
|
2019-09-24 18:04:54 +00:00
|
|
|
if (!GenAccessChainStoreReplacement(ptrInst, valId, &newInsts)) {
|
|
|
|
return Status::Failure;
|
|
|
|
}
|
2020-08-11 02:03:02 +00:00
|
|
|
size_t num_of_instructions_to_skip = newInsts.size() - 1;
|
2020-10-02 14:45:24 +00:00
|
|
|
dead_instructions.push_back(store);
|
2017-11-08 17:40:02 +00:00
|
|
|
++ii;
|
|
|
|
ii = ii.InsertBefore(std::move(newInsts));
|
2020-08-11 02:03:02 +00:00
|
|
|
for (size_t i = 0; i < num_of_instructions_to_skip; ++i) {
|
2020-10-02 14:45:24 +00:00
|
|
|
ii->UpdateDebugInfoFrom(store);
|
|
|
|
context()->get_debug_info_mgr()->AnalyzeDebugInst(&*ii);
|
2020-08-11 02:03:02 +00:00
|
|
|
++ii;
|
|
|
|
}
|
2020-10-02 14:45:24 +00:00
|
|
|
ii->UpdateDebugInfoFrom(store);
|
|
|
|
context()->get_debug_info_mgr()->AnalyzeDebugInst(&*ii);
|
2017-11-08 17:40:02 +00:00
|
|
|
modified = true;
|
|
|
|
} break;
|
|
|
|
default:
|
2017-05-12 23:27:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 19:47:46 +00:00
|
|
|
|
|
|
|
while (!dead_instructions.empty()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* inst = dead_instructions.back();
|
2017-11-21 19:47:46 +00:00
|
|
|
dead_instructions.pop_back();
|
2018-07-12 19:14:43 +00:00
|
|
|
DCEInst(inst, [&dead_instructions](Instruction* other_inst) {
|
2017-11-21 19:47:46 +00:00
|
|
|
auto i = std::find(dead_instructions.begin(), dead_instructions.end(),
|
|
|
|
other_inst);
|
|
|
|
if (i != dead_instructions.end()) {
|
|
|
|
dead_instructions.erase(i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
2019-09-24 18:04:54 +00:00
|
|
|
return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
void LocalAccessChainConvertPass::Initialize() {
|
2017-05-12 23:27:21 +00:00
|
|
|
// Initialize Target Variable Caches
|
|
|
|
seen_target_vars_.clear();
|
|
|
|
seen_non_target_vars_.clear();
|
2017-08-04 18:00:30 +00:00
|
|
|
|
|
|
|
// Initialize collections
|
|
|
|
supported_ref_ptrs_.clear();
|
2017-05-12 23:27:21 +00:00
|
|
|
|
2020-06-15 17:20:40 +00:00
|
|
|
// Initialize extension allowlist
|
2017-07-19 00:57:26 +00:00
|
|
|
InitExtensions();
|
2018-03-21 09:15:56 +00:00
|
|
|
}
|
2017-05-12 23:27:21 +00:00
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
bool LocalAccessChainConvertPass::AllExtensionsSupported() const {
|
2019-04-03 16:47:51 +00:00
|
|
|
// This capability can now exist without the extension, so we have to check
|
|
|
|
// for the capability. This pass is only looking at function scope symbols,
|
|
|
|
// so we do not care if there are variable pointers on storage buffers.
|
|
|
|
if (context()->get_feature_mgr()->HasCapability(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Capability::VariablePointers))
|
2019-04-03 16:47:51 +00:00
|
|
|
return false;
|
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()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(inst.opcode() == spv::Op::OpExtInstImport &&
|
2021-09-15 18:38:53 +00:00
|
|
|
"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-12 23:27:21 +00:00
|
|
|
Pass::Status LocalAccessChainConvertPass::ProcessImpl() {
|
2017-07-26 22:34:41 +00:00
|
|
|
// Do not process if module contains OpGroupDecorate. Additional
|
|
|
|
// support required in KillNamesAndDecorates().
|
|
|
|
// TODO(greg-lunarg): Add support for OpGroupDecorate
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ai : get_module()->annotations())
|
2022-11-04 21:27:10 +00:00
|
|
|
if (ai.opcode() == spv::Op::OpGroupDecorate)
|
|
|
|
return Status::SuccessWithoutChange;
|
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;
|
2019-09-24 18:04:54 +00:00
|
|
|
|
|
|
|
// Process all functions in the module.
|
|
|
|
Status status = Status::SuccessWithoutChange;
|
|
|
|
for (Function& func : *get_module()) {
|
|
|
|
status = CombineStatus(status, ConvertLocalAccessChains(&func));
|
|
|
|
if (status == Status::Failure) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
2017-05-12 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 23:16:50 +00:00
|
|
|
LocalAccessChainConvertPass::LocalAccessChainConvertPass() {}
|
2017-05-12 23:27:21 +00:00
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
Pass::Status LocalAccessChainConvertPass::Process() {
|
|
|
|
Initialize();
|
2017-05-12 23:27:21 +00:00
|
|
|
return ProcessImpl();
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
void LocalAccessChainConvertPass::InitExtensions() {
|
2020-06-15 17:20:40 +00:00
|
|
|
extensions_allowlist_.clear();
|
2023-03-28 20:57:45 +00:00
|
|
|
extensions_allowlist_.insert(
|
|
|
|
{"SPV_AMD_shader_explicit_vertex_parameter",
|
|
|
|
"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_8bit_storage", "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", "SPV_NV_sample_mask_override_coverage",
|
|
|
|
"SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod",
|
|
|
|
"SPV_KHR_storage_buffer_storage_class",
|
|
|
|
// SPV_KHR_variable_pointers
|
|
|
|
// Currently do not support extended pointer expressions
|
|
|
|
"SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage",
|
|
|
|
"SPV_KHR_shader_atomic_counter_ops", "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",
|
|
|
|
"SPV_GOOGLE_decorate_string", "SPV_GOOGLE_hlsl_functionality1",
|
|
|
|
"SPV_GOOGLE_user_type", "SPV_NV_shader_subgroup_partitioned",
|
|
|
|
"SPV_EXT_demote_to_helper_invocation", "SPV_EXT_descriptor_indexing",
|
|
|
|
"SPV_NV_fragment_shader_barycentric",
|
|
|
|
"SPV_NV_compute_shader_derivatives", "SPV_NV_shader_image_footprint",
|
2024-01-30 17:13:46 +00:00
|
|
|
"SPV_NV_shading_rate", "SPV_NV_mesh_shader", "SPV_EXT_mesh_shader",
|
|
|
|
"SPV_NV_ray_tracing", "SPV_KHR_ray_tracing", "SPV_KHR_ray_query",
|
2023-03-28 20:57:45 +00:00
|
|
|
"SPV_EXT_fragment_invocation_density", "SPV_KHR_terminate_invocation",
|
|
|
|
"SPV_KHR_subgroup_uniform_control_flow", "SPV_KHR_integer_dot_product",
|
|
|
|
"SPV_EXT_shader_image_int64", "SPV_KHR_non_semantic_info",
|
|
|
|
"SPV_KHR_uniform_group_instructions",
|
2023-05-24 15:01:11 +00:00
|
|
|
"SPV_KHR_fragment_shader_barycentric", "SPV_KHR_vulkan_memory_model",
|
2023-09-05 16:10:16 +00:00
|
|
|
"SPV_NV_bindless_texture", "SPV_EXT_shader_atomic_float_add",
|
2023-10-16 19:03:33 +00:00
|
|
|
"SPV_EXT_fragment_shader_interlock",
|
|
|
|
"SPV_NV_compute_shader_derivatives"});
|
2017-07-19 00:57:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 17:06:38 +00:00
|
|
|
bool LocalAccessChainConvertPass::AnyIndexIsOutOfBounds(
|
|
|
|
const Instruction* access_chain_inst) {
|
|
|
|
assert(IsNonPtrAccessChain(access_chain_inst->opcode()));
|
|
|
|
|
|
|
|
analysis::TypeManager* type_mgr = context()->get_type_mgr();
|
|
|
|
analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
|
|
|
|
auto constants = const_mgr->GetOperandConstants(access_chain_inst);
|
|
|
|
uint32_t base_pointer_id = access_chain_inst->GetSingleWordInOperand(0);
|
|
|
|
Instruction* base_pointer = get_def_use_mgr()->GetDef(base_pointer_id);
|
|
|
|
const analysis::Pointer* base_pointer_type =
|
|
|
|
type_mgr->GetType(base_pointer->type_id())->AsPointer();
|
|
|
|
assert(base_pointer_type != nullptr &&
|
|
|
|
"The base of the access chain is not a pointer.");
|
|
|
|
const analysis::Type* current_type = base_pointer_type->pointee_type();
|
|
|
|
for (uint32_t i = 1; i < access_chain_inst->NumInOperands(); ++i) {
|
|
|
|
if (IsIndexOutOfBounds(constants[i], current_type)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t index =
|
|
|
|
(constants[i]
|
|
|
|
? static_cast<uint32_t>(constants[i]->GetZeroExtendedValue())
|
|
|
|
: 0);
|
|
|
|
current_type = type_mgr->GetMemberType(current_type, {index});
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalAccessChainConvertPass::IsIndexOutOfBounds(
|
|
|
|
const analysis::Constant* index, const analysis::Type* type) const {
|
|
|
|
if (index == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return index->GetZeroExtendedValue() >= type->NumberOfComponents();
|
|
|
|
}
|
|
|
|
|
2017-05-12 23:27:21 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|