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/remove_duplicates_pass.h"
|
2017-07-13 00:16:51 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <cstring>
|
2017-07-13 00:16:51 +00:00
|
|
|
#include <limits>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2017-07-13 00:16:51 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/opt/decoration_manager.h"
|
|
|
|
#include "source/opt/ir_context.h"
|
|
|
|
#include "source/opt/reflect.h"
|
2017-07-13 00:16:51 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
Pass::Status RemoveDuplicatesPass::Process() {
|
|
|
|
bool modified = RemoveDuplicateCapabilities();
|
|
|
|
modified |= RemoveDuplicatesExtInstImports();
|
|
|
|
modified |= RemoveDuplicateTypes();
|
|
|
|
modified |= RemoveDuplicateDecorations();
|
2017-07-13 00:16:51 +00:00
|
|
|
|
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicateCapabilities() const {
|
2017-07-13 00:16:51 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
if (context()->capabilities().empty()) {
|
2017-11-21 19:47:46 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
std::unordered_set<uint32_t> capabilities;
|
2018-07-12 13:08:45 +00:00
|
|
|
for (auto* i = &*context()->capability_begin(); i;) {
|
2017-07-13 00:16:51 +00:00
|
|
|
auto res = capabilities.insert(i->GetSingleWordOperand(0u));
|
|
|
|
|
|
|
|
if (res.second) {
|
|
|
|
// Never seen before, keep it.
|
2017-11-21 19:47:46 +00:00
|
|
|
i = i->NextNode();
|
2017-07-13 00:16:51 +00:00
|
|
|
} else {
|
|
|
|
// It's a duplicate, remove it.
|
2018-07-12 13:08:45 +00:00
|
|
|
i = context()->KillInst(i);
|
2017-07-13 00:16:51 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicatesExtInstImports() const {
|
2017-07-13 00:16:51 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
if (context()->ext_inst_imports().empty()) {
|
2017-11-21 19:47:46 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
std::unordered_map<std::string, spv::Id> ext_inst_imports;
|
2018-07-12 13:08:45 +00:00
|
|
|
for (auto* i = &*context()->ext_inst_import_begin(); i;) {
|
2021-12-08 17:01:26 +00:00
|
|
|
auto res = ext_inst_imports.emplace(i->GetInOperand(0u).AsString(),
|
|
|
|
i->result_id());
|
2017-07-13 00:16:51 +00:00
|
|
|
if (res.second) {
|
|
|
|
// Never seen before, keep it.
|
2017-11-21 19:47:46 +00:00
|
|
|
i = i->NextNode();
|
2017-07-13 00:16:51 +00:00
|
|
|
} else {
|
|
|
|
// It's a duplicate, remove it.
|
2018-07-12 13:08:45 +00:00
|
|
|
context()->ReplaceAllUsesWith(i->result_id(), res.first->second);
|
|
|
|
i = context()->KillInst(i);
|
2017-07-13 00:16:51 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicateTypes() const {
|
2017-07-13 00:16:51 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
if (context()->types_values().empty()) {
|
2017-11-21 19:47:46 +00:00
|
|
|
return modified;
|
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2019-05-29 20:12:02 +00:00
|
|
|
analysis::TypeManager type_manager(context()->consumer(), context());
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
std::vector<Instruction*> visited_types;
|
2019-05-29 20:12:02 +00:00
|
|
|
std::vector<analysis::ForwardPointer> visited_forward_pointers;
|
2018-01-03 00:54:55 +00:00
|
|
|
std::vector<Instruction*> to_delete;
|
2018-07-12 13:08:45 +00:00
|
|
|
for (auto* i = &*context()->types_values_begin(); i; i = i->NextNode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
const bool is_i_forward_pointer =
|
|
|
|
i->opcode() == spv::Op::OpTypeForwardPointer;
|
2019-05-29 20:12:02 +00:00
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
// We only care about types.
|
2019-05-29 20:12:02 +00:00
|
|
|
if (!spvOpcodeGeneratesType(i->opcode()) && !is_i_forward_pointer) {
|
2017-07-13 00:16:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:12:02 +00:00
|
|
|
if (!is_i_forward_pointer) {
|
|
|
|
// Is the current type equal to one of the types we have already visited?
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Id id_to_keep = 0u;
|
2019-05-29 20:12:02 +00:00
|
|
|
analysis::Type* i_type = type_manager.GetType(i->result_id());
|
|
|
|
assert(i_type);
|
|
|
|
// TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
|
|
|
|
// ResultIdTrie from unify_const_pass.cpp for this.
|
|
|
|
for (auto j : visited_types) {
|
|
|
|
analysis::Type* j_type = type_manager.GetType(j->result_id());
|
|
|
|
assert(j_type);
|
|
|
|
if (*i_type == *j_type) {
|
|
|
|
id_to_keep = j->result_id();
|
|
|
|
break;
|
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 20:12:02 +00:00
|
|
|
if (id_to_keep == 0u) {
|
|
|
|
// This is a never seen before type, keep it around.
|
|
|
|
visited_types.emplace_back(i);
|
|
|
|
} else {
|
|
|
|
// The same type has already been seen before, remove this one.
|
|
|
|
context()->KillNamesAndDecorates(i->result_id());
|
|
|
|
context()->ReplaceAllUsesWith(i->result_id(), id_to_keep);
|
|
|
|
modified = true;
|
|
|
|
to_delete.emplace_back(i);
|
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
} else {
|
2019-05-29 20:12:02 +00:00
|
|
|
analysis::ForwardPointer i_type(
|
|
|
|
i->GetSingleWordInOperand(0u),
|
2022-11-04 21:27:10 +00:00
|
|
|
(spv::StorageClass)i->GetSingleWordInOperand(1u));
|
2019-05-29 20:12:02 +00:00
|
|
|
i_type.SetTargetPointer(
|
|
|
|
type_manager.GetType(i_type.target_id())->AsPointer());
|
|
|
|
|
|
|
|
// TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
|
|
|
|
// ResultIdTrie from unify_const_pass.cpp for this.
|
|
|
|
const bool found_a_match =
|
|
|
|
std::find(std::begin(visited_forward_pointers),
|
|
|
|
std::end(visited_forward_pointers),
|
|
|
|
i_type) != std::end(visited_forward_pointers);
|
|
|
|
|
|
|
|
if (!found_a_match) {
|
|
|
|
// This is a never seen before type, keep it around.
|
|
|
|
visited_forward_pointers.emplace_back(i_type);
|
|
|
|
} else {
|
|
|
|
// The same type has already been seen before, remove this one.
|
|
|
|
modified = true;
|
|
|
|
to_delete.emplace_back(i);
|
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
for (auto i : to_delete) {
|
2018-07-12 13:08:45 +00:00
|
|
|
context()->KillInst(i);
|
2017-12-18 22:02:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
return modified;
|
2018-06-29 14:21:56 +00:00
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2017-11-29 23:29:34 +00:00
|
|
|
// TODO(pierremoreau): Duplicate decoration groups should be removed. For
|
|
|
|
// example, in
|
|
|
|
// OpDecorate %1 Constant
|
|
|
|
// %1 = OpDecorationGroup
|
|
|
|
// OpDecorate %2 Constant
|
|
|
|
// %2 = OpDecorationGroup
|
|
|
|
// OpGroupDecorate %1 %3
|
|
|
|
// OpGroupDecorate %2 %4
|
|
|
|
// group %2 could be removed.
|
2018-07-12 13:08:45 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicateDecorations() const {
|
2017-07-13 00:16:51 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
std::vector<const Instruction*> visited_decorations;
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::DecorationManager decoration_manager(context()->module());
|
2018-07-12 13:08:45 +00:00
|
|
|
for (auto* i = &*context()->annotation_begin(); i;) {
|
2019-05-29 20:12:02 +00:00
|
|
|
// Is the current decoration equal to one of the decorations we have
|
|
|
|
// already visited?
|
2018-01-03 00:54:55 +00:00
|
|
|
bool already_visited = false;
|
|
|
|
// TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
|
|
|
|
// ResultIdTrie from unify_const_pass.cpp for this.
|
|
|
|
for (const Instruction* j : visited_decorations) {
|
|
|
|
if (decoration_manager.AreDecorationsTheSame(&*i, j, false)) {
|
|
|
|
already_visited = true;
|
2017-07-13 00:16:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
if (!already_visited) {
|
2017-07-13 00:16:51 +00:00
|
|
|
// This is a never seen before decoration, keep it around.
|
2018-01-03 00:54:55 +00:00
|
|
|
visited_decorations.emplace_back(&*i);
|
2017-11-21 19:47:46 +00:00
|
|
|
i = i->NextNode();
|
2017-07-13 00:16:51 +00:00
|
|
|
} else {
|
|
|
|
// The same decoration has already been seen before, remove this one.
|
|
|
|
modified = true;
|
2018-07-12 13:08:45 +00:00
|
|
|
i = context()->KillInst(i);
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|