2017-07-13 00:16:51 +00:00
|
|
|
// Copyright (c) 2017 Pierre Moreau
|
|
|
|
//
|
|
|
|
// 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/decoration_manager.h"
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
#include <algorithm>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
2017-12-24 10:15:14 +00:00
|
|
|
#include <set>
|
2017-11-13 20:31:43 +00:00
|
|
|
#include <stack>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <utility>
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/ir_context.h"
|
2018-02-20 18:19:57 +00:00
|
|
|
|
2022-11-17 18:02:50 +00:00
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
namespace analysis {
|
2019-07-10 15:37:16 +00:00
|
|
|
namespace {
|
|
|
|
using InstructionVector = std::vector<const spvtools::opt::Instruction*>;
|
|
|
|
using DecorationSet = std::set<std::u32string>;
|
|
|
|
|
|
|
|
// Returns true if |a| is a subet of |b|.
|
|
|
|
bool IsSubset(const DecorationSet& a, const DecorationSet& b) {
|
|
|
|
auto it1 = a.begin();
|
|
|
|
auto it2 = b.begin();
|
|
|
|
|
|
|
|
while (it1 != a.end()) {
|
|
|
|
if (it2 == b.end() || *it1 < *it2) {
|
|
|
|
// |*it1| is in |a|, but not in |b|.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (*it1 == *it2) {
|
|
|
|
// Found the element move to the next one.
|
|
|
|
it1++;
|
|
|
|
it2++;
|
|
|
|
} else /* *it1 > *it2 */ {
|
|
|
|
// Did not find |*it1| yet, check the next element in |b|.
|
|
|
|
it2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2021-01-28 20:53:34 +00:00
|
|
|
bool DecorationManager::RemoveDecorationsFrom(
|
2018-07-12 19:14:43 +00:00
|
|
|
uint32_t id, std::function<bool(const Instruction&)> pred) {
|
2021-01-28 20:53:34 +00:00
|
|
|
bool was_modified = false;
|
2018-02-20 18:19:57 +00:00
|
|
|
const auto ids_iter = id_to_decoration_insts_.find(id);
|
2018-09-28 18:16:04 +00:00
|
|
|
if (ids_iter == id_to_decoration_insts_.end()) {
|
2021-01-28 20:53:34 +00:00
|
|
|
return was_modified;
|
2018-09-28 18:16:04 +00:00
|
|
|
}
|
2018-02-20 18:19:57 +00:00
|
|
|
|
|
|
|
TargetData& decorations_info = ids_iter->second;
|
|
|
|
auto context = module_->context();
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> insts_to_kill;
|
2018-02-20 18:19:57 +00:00
|
|
|
const bool is_group = !decorations_info.decorate_insts.empty();
|
|
|
|
|
|
|
|
// Schedule all direct decorations for removal if instructed as such by
|
|
|
|
// |pred|.
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* inst : decorations_info.direct_decorations)
|
2018-02-20 18:19:57 +00:00
|
|
|
if (pred(*inst)) insts_to_kill.push_back(inst);
|
|
|
|
|
|
|
|
// For all groups being directly applied to |id|, remove |id| (and the
|
|
|
|
// literal if |inst| is an OpGroupMemberDecorate) from the instruction
|
|
|
|
// applying the group.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_set<const Instruction*> indirect_decorations_to_remove;
|
|
|
|
for (Instruction* inst : decorations_info.indirect_decorations) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(inst->opcode() == spv::Op::OpGroupDecorate ||
|
|
|
|
inst->opcode() == spv::Op::OpGroupMemberDecorate);
|
2018-02-20 18:19:57 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> group_decorations_to_keep;
|
2018-02-20 18:19:57 +00:00
|
|
|
const uint32_t group_id = inst->GetSingleWordInOperand(0u);
|
|
|
|
const auto group_iter = id_to_decoration_insts_.find(group_id);
|
|
|
|
assert(group_iter != id_to_decoration_insts_.end() &&
|
|
|
|
"Unknown decoration group");
|
|
|
|
const auto& group_decorations = group_iter->second.direct_decorations;
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* decoration : group_decorations) {
|
2018-02-20 18:19:57 +00:00
|
|
|
if (!pred(*decoration)) group_decorations_to_keep.push_back(decoration);
|
|
|
|
}
|
|
|
|
|
2018-09-28 18:16:04 +00:00
|
|
|
// If all decorations should be kept, then we can keep |id| part of the
|
|
|
|
// group. However, if the group itself has no decorations, we should remove
|
|
|
|
// the id from the group. This is needed to make |KillNameAndDecorate| work
|
|
|
|
// correctly when a decoration group has no decorations.
|
|
|
|
if (group_decorations_to_keep.size() == group_decorations.size() &&
|
|
|
|
group_decorations.size() != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-20 18:19:57 +00:00
|
|
|
|
|
|
|
// Otherwise, remove |id| from the targets of |group_id|
|
2022-11-04 21:27:10 +00:00
|
|
|
const uint32_t stride =
|
|
|
|
inst->opcode() == spv::Op::OpGroupDecorate ? 1u : 2u;
|
2018-02-20 18:19:57 +00:00
|
|
|
for (uint32_t i = 1u; i < inst->NumInOperands();) {
|
|
|
|
if (inst->GetSingleWordInOperand(i) != id) {
|
|
|
|
i += stride;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t last_operand_index = inst->NumInOperands() - stride;
|
|
|
|
if (i < last_operand_index)
|
|
|
|
inst->GetInOperand(i) = inst->GetInOperand(last_operand_index);
|
|
|
|
// Remove the associated literal, if it exists.
|
|
|
|
if (stride == 2u) {
|
|
|
|
if (i < last_operand_index)
|
|
|
|
inst->GetInOperand(i + 1u) =
|
|
|
|
inst->GetInOperand(last_operand_index + 1u);
|
|
|
|
inst->RemoveInOperand(last_operand_index + 1u);
|
|
|
|
}
|
|
|
|
inst->RemoveInOperand(last_operand_index);
|
|
|
|
was_modified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the instruction has no targets left, remove the instruction
|
|
|
|
// altogether.
|
|
|
|
if (inst->NumInOperands() == 1u) {
|
|
|
|
indirect_decorations_to_remove.emplace(inst);
|
|
|
|
insts_to_kill.push_back(inst);
|
|
|
|
} else if (was_modified) {
|
|
|
|
context->ForgetUses(inst);
|
|
|
|
indirect_decorations_to_remove.emplace(inst);
|
|
|
|
context->AnalyzeUses(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only some of the decorations should be kept, clone them and apply
|
|
|
|
// them directly to |id|.
|
|
|
|
if (!group_decorations_to_keep.empty()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* decoration : group_decorations_to_keep) {
|
2018-02-20 18:19:57 +00:00
|
|
|
// simply clone decoration and change |group_id| to |id|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> new_inst(
|
2018-02-20 18:19:57 +00:00
|
|
|
decoration->Clone(module_->context()));
|
|
|
|
new_inst->SetInOperand(0, {id});
|
|
|
|
module_->AddAnnotationInst(std::move(new_inst));
|
|
|
|
auto decoration_iter = --module_->annotation_end();
|
|
|
|
context->AnalyzeUses(&*decoration_iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& indirect_decorations = decorations_info.indirect_decorations;
|
|
|
|
indirect_decorations.erase(
|
|
|
|
std::remove_if(
|
|
|
|
indirect_decorations.begin(), indirect_decorations.end(),
|
2018-07-12 19:14:43 +00:00
|
|
|
[&indirect_decorations_to_remove](const Instruction* inst) {
|
2018-02-20 18:19:57 +00:00
|
|
|
return indirect_decorations_to_remove.count(inst);
|
|
|
|
}),
|
|
|
|
indirect_decorations.end());
|
|
|
|
|
2021-01-28 20:53:34 +00:00
|
|
|
was_modified |= !insts_to_kill.empty();
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* inst : insts_to_kill) context->KillInst(inst);
|
2018-02-20 18:19:57 +00:00
|
|
|
insts_to_kill.clear();
|
|
|
|
|
|
|
|
// Schedule all instructions applying the group for removal if this group no
|
|
|
|
// longer applies decorations, either directly or indirectly.
|
|
|
|
if (is_group && decorations_info.direct_decorations.empty() &&
|
|
|
|
decorations_info.indirect_decorations.empty()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* inst : decorations_info.decorate_insts)
|
2018-02-20 18:19:57 +00:00
|
|
|
insts_to_kill.push_back(inst);
|
|
|
|
}
|
2021-01-28 20:53:34 +00:00
|
|
|
was_modified |= !insts_to_kill.empty();
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* inst : insts_to_kill) context->KillInst(inst);
|
2018-02-20 18:19:57 +00:00
|
|
|
|
|
|
|
if (decorations_info.direct_decorations.empty() &&
|
|
|
|
decorations_info.indirect_decorations.empty() &&
|
|
|
|
decorations_info.decorate_insts.empty()) {
|
|
|
|
id_to_decoration_insts_.erase(ids_iter);
|
|
|
|
}
|
2021-01-28 20:53:34 +00:00
|
|
|
return was_modified;
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> DecorationManager::GetDecorationsFor(
|
2017-07-13 00:16:51 +00:00
|
|
|
uint32_t id, bool include_linkage) {
|
2018-07-12 19:14:43 +00:00
|
|
|
return InternalGetDecorationsFor<Instruction*>(id, include_linkage);
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<const Instruction*> DecorationManager::GetDecorationsFor(
|
2017-07-13 00:16:51 +00:00
|
|
|
uint32_t id, bool include_linkage) const {
|
2017-10-20 16:17:41 +00:00
|
|
|
return const_cast<DecorationManager*>(this)
|
2018-07-12 19:14:43 +00:00
|
|
|
->InternalGetDecorationsFor<const Instruction*>(id, include_linkage);
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DecorationManager::HaveTheSameDecorations(uint32_t id1,
|
|
|
|
uint32_t id2) const {
|
2019-07-10 15:37:16 +00:00
|
|
|
const InstructionVector decorations_for1 = GetDecorationsFor(id1, false);
|
|
|
|
const InstructionVector decorations_for2 = GetDecorationsFor(id2, false);
|
2017-12-24 10:15:14 +00:00
|
|
|
|
|
|
|
// This function splits the decoration instructions into different sets,
|
2018-03-13 17:52:53 +00:00
|
|
|
// based on their opcode; only OpDecorate, OpDecorateId,
|
|
|
|
// OpDecorateStringGOOGLE, and OpMemberDecorate are considered, the other
|
|
|
|
// opcodes are ignored.
|
2017-12-24 10:15:14 +00:00
|
|
|
const auto fillDecorationSets =
|
2019-07-10 15:37:16 +00:00
|
|
|
[](const InstructionVector& decoration_list, DecorationSet* decorate_set,
|
2018-03-13 17:52:53 +00:00
|
|
|
DecorationSet* decorate_id_set, DecorationSet* decorate_string_set,
|
|
|
|
DecorationSet* member_decorate_set) {
|
2018-07-12 19:14:43 +00:00
|
|
|
for (const Instruction* inst : decoration_list) {
|
2018-02-20 18:19:57 +00:00
|
|
|
std::u32string decoration_payload;
|
2017-12-24 10:15:14 +00:00
|
|
|
// Ignore the opcode and the target as we do not want them to be
|
|
|
|
// compared.
|
2018-03-13 17:52:53 +00:00
|
|
|
for (uint32_t i = 1u; i < inst->NumInOperands(); ++i) {
|
|
|
|
for (uint32_t word : inst->GetInOperand(i).words) {
|
2018-02-20 18:19:57 +00:00
|
|
|
decoration_payload.push_back(word);
|
2018-03-13 17:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-24 10:15:14 +00:00
|
|
|
|
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
2018-02-20 18:19:57 +00:00
|
|
|
decorate_set->emplace(std::move(decoration_payload));
|
2017-12-24 10:15:14 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpMemberDecorate:
|
2018-02-20 18:19:57 +00:00
|
|
|
member_decorate_set->emplace(std::move(decoration_payload));
|
2017-12-24 10:15:14 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorateId:
|
2018-02-20 18:19:57 +00:00
|
|
|
decorate_id_set->emplace(std::move(decoration_payload));
|
2017-12-24 10:15:14 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorateStringGOOGLE:
|
2018-03-13 17:52:53 +00:00
|
|
|
decorate_string_set->emplace(std::move(decoration_payload));
|
|
|
|
break;
|
2017-12-24 10:15:14 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-20 18:19:57 +00:00
|
|
|
DecorationSet decorate_set_for1;
|
|
|
|
DecorationSet decorate_id_set_for1;
|
2018-03-13 17:52:53 +00:00
|
|
|
DecorationSet decorate_string_set_for1;
|
2018-02-20 18:19:57 +00:00
|
|
|
DecorationSet member_decorate_set_for1;
|
|
|
|
fillDecorationSets(decorations_for1, &decorate_set_for1,
|
2018-03-13 17:52:53 +00:00
|
|
|
&decorate_id_set_for1, &decorate_string_set_for1,
|
|
|
|
&member_decorate_set_for1);
|
2018-02-20 18:19:57 +00:00
|
|
|
|
|
|
|
DecorationSet decorate_set_for2;
|
|
|
|
DecorationSet decorate_id_set_for2;
|
2018-03-13 17:52:53 +00:00
|
|
|
DecorationSet decorate_string_set_for2;
|
2018-02-20 18:19:57 +00:00
|
|
|
DecorationSet member_decorate_set_for2;
|
|
|
|
fillDecorationSets(decorations_for2, &decorate_set_for2,
|
2018-03-13 17:52:53 +00:00
|
|
|
&decorate_id_set_for2, &decorate_string_set_for2,
|
|
|
|
&member_decorate_set_for2);
|
|
|
|
|
|
|
|
const bool result = decorate_set_for1 == decorate_set_for2 &&
|
|
|
|
decorate_id_set_for1 == decorate_id_set_for2 &&
|
|
|
|
member_decorate_set_for1 == member_decorate_set_for2 &&
|
|
|
|
// Compare string sets last in case the strings are long.
|
|
|
|
decorate_string_set_for1 == decorate_string_set_for2;
|
|
|
|
return result;
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 15:37:16 +00:00
|
|
|
bool DecorationManager::HaveSubsetOfDecorations(uint32_t id1,
|
|
|
|
uint32_t id2) const {
|
|
|
|
const InstructionVector decorations_for1 = GetDecorationsFor(id1, false);
|
|
|
|
const InstructionVector decorations_for2 = GetDecorationsFor(id2, false);
|
|
|
|
|
|
|
|
// This function splits the decoration instructions into different sets,
|
|
|
|
// based on their opcode; only OpDecorate, OpDecorateId,
|
|
|
|
// OpDecorateStringGOOGLE, and OpMemberDecorate are considered, the other
|
|
|
|
// opcodes are ignored.
|
|
|
|
const auto fillDecorationSets =
|
|
|
|
[](const InstructionVector& decoration_list, DecorationSet* decorate_set,
|
|
|
|
DecorationSet* decorate_id_set, DecorationSet* decorate_string_set,
|
|
|
|
DecorationSet* member_decorate_set) {
|
|
|
|
for (const Instruction* inst : decoration_list) {
|
|
|
|
std::u32string decoration_payload;
|
|
|
|
// Ignore the opcode and the target as we do not want them to be
|
|
|
|
// compared.
|
|
|
|
for (uint32_t i = 1u; i < inst->NumInOperands(); ++i) {
|
|
|
|
for (uint32_t word : inst->GetInOperand(i).words) {
|
|
|
|
decoration_payload.push_back(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
2019-07-10 15:37:16 +00:00
|
|
|
decorate_set->emplace(std::move(decoration_payload));
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpMemberDecorate:
|
2019-07-10 15:37:16 +00:00
|
|
|
member_decorate_set->emplace(std::move(decoration_payload));
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorateId:
|
2019-07-10 15:37:16 +00:00
|
|
|
decorate_id_set->emplace(std::move(decoration_payload));
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorateStringGOOGLE:
|
2019-07-10 15:37:16 +00:00
|
|
|
decorate_string_set->emplace(std::move(decoration_payload));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DecorationSet decorate_set_for1;
|
|
|
|
DecorationSet decorate_id_set_for1;
|
|
|
|
DecorationSet decorate_string_set_for1;
|
|
|
|
DecorationSet member_decorate_set_for1;
|
|
|
|
fillDecorationSets(decorations_for1, &decorate_set_for1,
|
|
|
|
&decorate_id_set_for1, &decorate_string_set_for1,
|
|
|
|
&member_decorate_set_for1);
|
|
|
|
|
|
|
|
DecorationSet decorate_set_for2;
|
|
|
|
DecorationSet decorate_id_set_for2;
|
|
|
|
DecorationSet decorate_string_set_for2;
|
|
|
|
DecorationSet member_decorate_set_for2;
|
|
|
|
fillDecorationSets(decorations_for2, &decorate_set_for2,
|
|
|
|
&decorate_id_set_for2, &decorate_string_set_for2,
|
|
|
|
&member_decorate_set_for2);
|
|
|
|
|
|
|
|
const bool result =
|
|
|
|
IsSubset(decorate_set_for1, decorate_set_for2) &&
|
|
|
|
IsSubset(decorate_id_set_for1, decorate_id_set_for2) &&
|
|
|
|
IsSubset(member_decorate_set_for1, member_decorate_set_for2) &&
|
|
|
|
// Compare string sets last in case the strings are long.
|
|
|
|
IsSubset(decorate_string_set_for1, decorate_string_set_for2);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-11-29 23:29:34 +00:00
|
|
|
// TODO(pierremoreau): If OpDecorateId is referencing an OpConstant, one could
|
|
|
|
// check that the constants are the same rather than just
|
|
|
|
// looking at the constant ID.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool DecorationManager::AreDecorationsTheSame(const Instruction* inst1,
|
|
|
|
const Instruction* inst2,
|
2017-11-29 23:29:34 +00:00
|
|
|
bool ignore_target) const {
|
|
|
|
switch (inst1->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
|
|
|
case spv::Op::OpMemberDecorate:
|
|
|
|
case spv::Op::OpDecorateId:
|
|
|
|
case spv::Op::OpDecorateStringGOOGLE:
|
2017-11-29 23:29:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2017-11-29 23:29:34 +00:00
|
|
|
if (inst1->opcode() != inst2->opcode() ||
|
|
|
|
inst1->NumInOperands() != inst2->NumInOperands())
|
2017-07-13 00:16:51 +00:00
|
|
|
return false;
|
|
|
|
|
2017-11-29 23:29:34 +00:00
|
|
|
for (uint32_t i = ignore_target ? 1u : 0u; i < inst1->NumInOperands(); ++i)
|
|
|
|
if (inst1->GetInOperand(i) != inst2->GetInOperand(i)) return false;
|
2017-07-13 00:16:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:28:18 +00:00
|
|
|
void DecorationManager::AnalyzeDecorations() {
|
|
|
|
if (!module_) return;
|
2017-07-13 00:16:51 +00:00
|
|
|
|
|
|
|
// For each group and instruction, collect all their decoration instructions.
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction& inst : module_->annotations()) {
|
2017-11-13 20:31:43 +00:00
|
|
|
AddDecoration(&inst);
|
|
|
|
}
|
|
|
|
}
|
2018-11-08 18:54:54 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void DecorationManager::AddDecoration(Instruction* inst) {
|
2017-11-13 20:31:43 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
|
|
|
case spv::Op::OpDecorateId:
|
|
|
|
case spv::Op::OpDecorateStringGOOGLE:
|
|
|
|
case spv::Op::OpMemberDecorate: {
|
2018-02-20 18:19:57 +00:00
|
|
|
const auto target_id = inst->GetSingleWordInOperand(0u);
|
|
|
|
id_to_decoration_insts_[target_id].direct_decorations.push_back(inst);
|
2017-11-13 20:31:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupDecorate:
|
|
|
|
case spv::Op::OpGroupMemberDecorate: {
|
|
|
|
const uint32_t start =
|
|
|
|
inst->opcode() == spv::Op::OpGroupDecorate ? 1u : 2u;
|
2018-02-20 18:19:57 +00:00
|
|
|
const uint32_t stride = start;
|
|
|
|
for (uint32_t i = start; i < inst->NumInOperands(); i += stride) {
|
|
|
|
const auto target_id = inst->GetSingleWordInOperand(i);
|
|
|
|
TargetData& target_data = id_to_decoration_insts_[target_id];
|
|
|
|
target_data.indirect_decorations.push_back(inst);
|
2017-11-13 20:31:43 +00:00
|
|
|
}
|
2018-02-20 18:19:57 +00:00
|
|
|
const auto target_id = inst->GetSingleWordInOperand(0u);
|
|
|
|
id_to_decoration_insts_[target_id].decorate_insts.push_back(inst);
|
2017-11-13 20:31:43 +00:00
|
|
|
break;
|
2018-02-20 18:19:57 +00:00
|
|
|
}
|
2017-11-13 20:31:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
void DecorationManager::AddDecoration(spv::Op opcode,
|
2018-11-08 18:54:54 +00:00
|
|
|
std::vector<Operand> opnds) {
|
|
|
|
IRContext* ctx = module_->context();
|
|
|
|
std::unique_ptr<Instruction> newDecoOp(
|
|
|
|
new Instruction(ctx, opcode, 0, 0, opnds));
|
|
|
|
ctx->AddAnnotationInst(std::move(newDecoOp));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationManager::AddDecoration(uint32_t inst_id, uint32_t decoration) {
|
|
|
|
AddDecoration(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op::OpDecorate,
|
2018-11-08 18:54:54 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inst_id}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {decoration}}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationManager::AddDecorationVal(uint32_t inst_id, uint32_t decoration,
|
|
|
|
uint32_t decoration_value) {
|
|
|
|
AddDecoration(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op::OpDecorate,
|
2018-11-08 18:54:54 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inst_id}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {decoration}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
|
|
|
|
{decoration_value}}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationManager::AddMemberDecoration(uint32_t inst_id, uint32_t member,
|
|
|
|
uint32_t decoration,
|
|
|
|
uint32_t decoration_value) {
|
|
|
|
AddDecoration(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op::OpMemberDecorate,
|
2018-11-08 18:54:54 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {inst_id}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {member}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {decoration}},
|
|
|
|
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
|
|
|
|
{decoration_value}}});
|
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
template <typename T>
|
2017-10-20 16:17:41 +00:00
|
|
|
std::vector<T> DecorationManager::InternalGetDecorationsFor(
|
|
|
|
uint32_t id, bool include_linkage) {
|
2017-07-13 00:16:51 +00:00
|
|
|
std::vector<T> decorations;
|
|
|
|
|
|
|
|
const auto ids_iter = id_to_decoration_insts_.find(id);
|
|
|
|
// |id| has no decorations
|
|
|
|
if (ids_iter == id_to_decoration_insts_.end()) return decorations;
|
|
|
|
|
2018-02-20 18:19:57 +00:00
|
|
|
const TargetData& target_data = ids_iter->second;
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2018-02-20 18:19:57 +00:00
|
|
|
const auto process_direct_decorations =
|
|
|
|
[include_linkage,
|
2018-07-12 19:14:43 +00:00
|
|
|
&decorations](const std::vector<Instruction*>& direct_decorations) {
|
|
|
|
for (Instruction* inst : direct_decorations) {
|
2022-11-04 21:27:10 +00:00
|
|
|
const bool is_linkage =
|
|
|
|
inst->opcode() == spv::Op::OpDecorate &&
|
|
|
|
spv::Decoration(inst->GetSingleWordInOperand(1u)) ==
|
|
|
|
spv::Decoration::LinkageAttributes;
|
2018-02-20 18:19:57 +00:00
|
|
|
if (include_linkage || !is_linkage) decorations.push_back(inst);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Process |id|'s decorations.
|
|
|
|
process_direct_decorations(ids_iter->second.direct_decorations);
|
|
|
|
|
|
|
|
// Process the decorations of all groups applied to |id|.
|
2018-07-12 19:14:43 +00:00
|
|
|
for (const Instruction* inst : target_data.indirect_decorations) {
|
2018-02-20 18:19:57 +00:00
|
|
|
const uint32_t group_id = inst->GetSingleWordInOperand(0u);
|
|
|
|
const auto group_iter = id_to_decoration_insts_.find(group_id);
|
|
|
|
assert(group_iter != id_to_decoration_insts_.end() && "Unknown group ID");
|
|
|
|
process_direct_decorations(group_iter->second.direct_decorations);
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return decorations;
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
bool DecorationManager::WhileEachDecoration(
|
2017-11-13 20:31:43 +00:00
|
|
|
uint32_t id, uint32_t decoration,
|
2023-08-10 14:21:35 +00:00
|
|
|
std::function<bool(const Instruction&)> f) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
for (const Instruction* inst : GetDecorationsFor(id, true)) {
|
2017-10-24 16:28:18 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpMemberDecorate:
|
2017-10-24 16:28:18 +00:00
|
|
|
if (inst->GetSingleWordInOperand(2) == decoration) {
|
2018-01-12 20:05:53 +00:00
|
|
|
if (!f(*inst)) return false;
|
2017-10-24 16:28:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
|
|
|
case spv::Op::OpDecorateId:
|
|
|
|
case spv::Op::OpDecorateStringGOOGLE:
|
2017-10-24 16:28:18 +00:00
|
|
|
if (inst->GetSingleWordInOperand(1) == decoration) {
|
2018-01-12 20:05:53 +00:00
|
|
|
if (!f(*inst)) return false;
|
2017-10-24 16:28:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false && "Unexpected decoration instruction");
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationManager::ForEachDecoration(
|
|
|
|
uint32_t id, uint32_t decoration,
|
2023-08-10 14:21:35 +00:00
|
|
|
std::function<void(const Instruction&)> f) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
WhileEachDecoration(id, decoration, [&f](const Instruction& inst) {
|
2018-01-12 20:05:53 +00:00
|
|
|
f(inst);
|
|
|
|
return true;
|
|
|
|
});
|
2017-10-24 16:28:18 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:21:35 +00:00
|
|
|
bool DecorationManager::HasDecoration(uint32_t id,
|
|
|
|
spv::Decoration decoration) const {
|
|
|
|
return HasDecoration(id, static_cast<uint32_t>(decoration));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecorationManager::HasDecoration(uint32_t id, uint32_t decoration) const {
|
2021-10-26 21:20:58 +00:00
|
|
|
bool has_decoration = false;
|
|
|
|
ForEachDecoration(id, decoration, [&has_decoration](const Instruction&) {
|
|
|
|
has_decoration = true;
|
|
|
|
});
|
|
|
|
return has_decoration;
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:39:15 +00:00
|
|
|
bool DecorationManager::FindDecoration(
|
|
|
|
uint32_t id, uint32_t decoration,
|
|
|
|
std::function<bool(const Instruction&)> f) {
|
|
|
|
return !WhileEachDecoration(
|
|
|
|
id, decoration, [&f](const Instruction& inst) { return !f(inst); });
|
|
|
|
}
|
|
|
|
|
2018-02-20 18:19:57 +00:00
|
|
|
void DecorationManager::CloneDecorations(uint32_t from, uint32_t to) {
|
|
|
|
const auto decoration_list = id_to_decoration_insts_.find(from);
|
2017-10-24 16:28:18 +00:00
|
|
|
if (decoration_list == id_to_decoration_insts_.end()) return;
|
2018-02-20 18:19:57 +00:00
|
|
|
auto context = module_->context();
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* inst : decoration_list->second.direct_decorations) {
|
2018-02-20 18:19:57 +00:00
|
|
|
// simply clone decoration and change |target-id| to |to|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> new_inst(inst->Clone(module_->context()));
|
2018-02-20 18:19:57 +00:00
|
|
|
new_inst->SetInOperand(0, {to});
|
|
|
|
module_->AddAnnotationInst(std::move(new_inst));
|
|
|
|
auto decoration_iter = --module_->annotation_end();
|
|
|
|
context->AnalyzeUses(&*decoration_iter);
|
|
|
|
}
|
|
|
|
// We need to copy the list of instructions as ForgetUses and AnalyzeUses are
|
|
|
|
// going to modify it.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> indirect_decorations =
|
2018-02-20 18:19:57 +00:00
|
|
|
decoration_list->second.indirect_decorations;
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction* inst : indirect_decorations) {
|
2017-10-24 16:28:18 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupDecorate:
|
2018-02-20 18:19:57 +00:00
|
|
|
context->ForgetUses(inst);
|
2017-10-24 16:28:18 +00:00
|
|
|
// add |to| to list of decorated id's
|
2017-11-13 20:31:43 +00:00
|
|
|
inst->AddOperand(
|
2018-07-12 19:14:43 +00:00
|
|
|
Operand(spv_operand_type_t::SPV_OPERAND_TYPE_ID, {to}));
|
2018-02-20 18:19:57 +00:00
|
|
|
context->AnalyzeUses(inst);
|
2017-10-24 16:28:18 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupMemberDecorate: {
|
2018-02-20 18:19:57 +00:00
|
|
|
context->ForgetUses(inst);
|
2017-10-24 16:28:18 +00:00
|
|
|
// for each (id == from), add (to, literal) as operands
|
|
|
|
const uint32_t num_operands = inst->NumOperands();
|
|
|
|
for (uint32_t i = 1; i < num_operands; i += 2) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Operand op = inst->GetOperand(i);
|
2017-11-13 20:31:43 +00:00
|
|
|
if (op.words[0] == from) { // add new pair of operands: (to, literal)
|
|
|
|
inst->AddOperand(
|
2018-07-12 19:14:43 +00:00
|
|
|
Operand(spv_operand_type_t::SPV_OPERAND_TYPE_ID, {to}));
|
2017-10-24 16:28:18 +00:00
|
|
|
op = inst->GetOperand(i + 1);
|
|
|
|
inst->AddOperand(std::move(op));
|
2017-10-20 16:17:41 +00:00
|
|
|
}
|
2017-10-24 16:28:18 +00:00
|
|
|
}
|
2018-02-20 18:19:57 +00:00
|
|
|
context->AnalyzeUses(inst);
|
2017-10-24 16:28:18 +00:00
|
|
|
break;
|
2017-10-20 16:17:41 +00:00
|
|
|
}
|
2017-10-24 16:28:18 +00:00
|
|
|
default:
|
|
|
|
assert(false && "Unexpected decoration instruction");
|
2017-10-20 16:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:55:39 +00:00
|
|
|
void DecorationManager::CloneDecorations(
|
|
|
|
uint32_t from, uint32_t to,
|
2022-11-04 21:27:10 +00:00
|
|
|
const std::vector<spv::Decoration>& decorations_to_copy) {
|
2018-08-24 15:55:39 +00:00
|
|
|
const auto decoration_list = id_to_decoration_insts_.find(from);
|
|
|
|
if (decoration_list == id_to_decoration_insts_.end()) return;
|
|
|
|
auto context = module_->context();
|
|
|
|
for (Instruction* inst : decoration_list->second.direct_decorations) {
|
|
|
|
if (std::find(decorations_to_copy.begin(), decorations_to_copy.end(),
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Decoration(inst->GetSingleWordInOperand(1))) ==
|
2018-08-24 15:55:39 +00:00
|
|
|
decorations_to_copy.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone decoration and change |target-id| to |to|.
|
|
|
|
std::unique_ptr<Instruction> new_inst(inst->Clone(module_->context()));
|
|
|
|
new_inst->SetInOperand(0, {to});
|
|
|
|
module_->AddAnnotationInst(std::move(new_inst));
|
|
|
|
auto decoration_iter = --module_->annotation_end();
|
|
|
|
context->AnalyzeUses(&*decoration_iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to copy the list of instructions as ForgetUses and AnalyzeUses are
|
|
|
|
// going to modify it.
|
|
|
|
std::vector<Instruction*> indirect_decorations =
|
|
|
|
decoration_list->second.indirect_decorations;
|
|
|
|
for (Instruction* inst : indirect_decorations) {
|
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupDecorate:
|
2018-08-24 15:55:39 +00:00
|
|
|
CloneDecorations(inst->GetSingleWordInOperand(0), to,
|
|
|
|
decorations_to_copy);
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupMemberDecorate: {
|
2018-08-24 15:55:39 +00:00
|
|
|
assert(false && "The source id is not suppose to be a type.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false && "Unexpected decoration instruction");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void DecorationManager::RemoveDecoration(Instruction* inst) {
|
|
|
|
const auto remove_from_container = [inst](std::vector<Instruction*>& v) {
|
2018-02-20 18:19:57 +00:00
|
|
|
v.erase(std::remove(v.begin(), v.end(), inst), v.end());
|
|
|
|
};
|
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
|
|
|
case spv::Op::OpDecorateId:
|
|
|
|
case spv::Op::OpDecorateStringGOOGLE:
|
|
|
|
case spv::Op::OpMemberDecorate: {
|
2018-02-20 18:19:57 +00:00
|
|
|
const auto target_id = inst->GetSingleWordInOperand(0u);
|
|
|
|
auto const iter = id_to_decoration_insts_.find(target_id);
|
|
|
|
if (iter == id_to_decoration_insts_.end()) return;
|
|
|
|
remove_from_container(iter->second.direct_decorations);
|
2017-11-13 20:31:43 +00:00
|
|
|
} break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupDecorate:
|
|
|
|
case spv::Op::OpGroupMemberDecorate: {
|
|
|
|
const uint32_t stride =
|
|
|
|
inst->opcode() == spv::Op::OpGroupDecorate ? 1u : 2u;
|
2018-02-20 18:19:57 +00:00
|
|
|
for (uint32_t i = 1u; i < inst->NumInOperands(); i += stride) {
|
|
|
|
const auto target_id = inst->GetSingleWordInOperand(i);
|
|
|
|
auto const iter = id_to_decoration_insts_.find(target_id);
|
|
|
|
if (iter == id_to_decoration_insts_.end()) continue;
|
|
|
|
remove_from_container(iter->second.indirect_decorations);
|
2017-11-13 20:31:43 +00:00
|
|
|
}
|
2018-02-20 18:19:57 +00:00
|
|
|
const auto group_id = inst->GetSingleWordInOperand(0u);
|
|
|
|
auto const iter = id_to_decoration_insts_.find(group_id);
|
|
|
|
if (iter == id_to_decoration_insts_.end()) return;
|
|
|
|
remove_from_container(iter->second.decorate_insts);
|
|
|
|
} break;
|
2017-11-13 20:31:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 18:24:33 +00:00
|
|
|
|
|
|
|
bool operator==(const DecorationManager& lhs, const DecorationManager& rhs) {
|
|
|
|
return lhs.id_to_decoration_insts_ == rhs.id_to_decoration_insts_;
|
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
} // namespace analysis
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|