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.
|
|
|
|
|
|
|
|
#include "remove_duplicates_pass.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "decoration_manager.h"
|
2017-10-30 15:13:24 +00:00
|
|
|
#include "ir_context.h"
|
2017-11-08 17:40:02 +00:00
|
|
|
#include "opcode.h"
|
2017-12-18 22:02:19 +00:00
|
|
|
#include "reflect.h"
|
2017-07-13 00:16:51 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
using ir::Instruction;
|
|
|
|
using ir::Module;
|
|
|
|
using ir::Operand;
|
|
|
|
using opt::analysis::DecorationManager;
|
2017-11-27 15:16:41 +00:00
|
|
|
using opt::analysis::DefUseManager;
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
Pass::Status RemoveDuplicatesPass::Process(ir::IRContext* ir_context) {
|
|
|
|
bool modified = RemoveDuplicateCapabilities(ir_context);
|
|
|
|
modified |= RemoveDuplicatesExtInstImports(ir_context);
|
|
|
|
modified |= RemoveDuplicateTypes(ir_context);
|
|
|
|
modified |= RemoveDuplicateDecorations(ir_context);
|
2017-07-13 00:16:51 +00:00
|
|
|
|
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicateCapabilities(
|
2018-01-03 00:54:55 +00:00
|
|
|
ir::IRContext* ir_context) const {
|
2017-07-13 00:16:51 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
if (ir_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-01-03 00:54:55 +00:00
|
|
|
for (auto* i = &*ir_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-01-03 00:54:55 +00:00
|
|
|
i = ir_context->KillInst(i);
|
2017-07-13 00:16:51 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicatesExtInstImports(
|
2018-01-03 00:54:55 +00:00
|
|
|
ir::IRContext* ir_context) const {
|
2017-07-13 00:16:51 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
if (ir_context->ext_inst_imports().empty()) {
|
2017-11-21 19:47:46 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
std::unordered_map<std::string, SpvId> ext_inst_imports;
|
|
|
|
for (auto* i = &*ir_context->ext_inst_import_begin(); i;) {
|
|
|
|
auto res = ext_inst_imports.emplace(
|
2017-07-13 00:16:51 +00:00
|
|
|
reinterpret_cast<const char*>(i->GetInOperand(0u).words.data()),
|
|
|
|
i->result_id());
|
|
|
|
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-01-03 00:54:55 +00:00
|
|
|
ir_context->ReplaceAllUsesWith(i->result_id(), res.first->second);
|
|
|
|
i = ir_context->KillInst(i);
|
2017-07-13 00:16:51 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicateTypes(
|
2018-01-03 00:54:55 +00:00
|
|
|
ir::IRContext* ir_context) const {
|
2017-07-13 00:16:51 +00:00
|
|
|
bool modified = false;
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
if (ir_context->types_values().empty()) {
|
2017-11-21 19:47:46 +00:00
|
|
|
return modified;
|
|
|
|
}
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
std::vector<Instruction*> visited_types;
|
|
|
|
std::vector<Instruction*> to_delete;
|
|
|
|
for (auto* i = &*ir_context->types_values_begin(); i; i = i->NextNode()) {
|
2017-07-13 00:16:51 +00:00
|
|
|
// We only care about types.
|
|
|
|
if (!spvOpcodeGeneratesType((i->opcode())) &&
|
|
|
|
i->opcode() != SpvOpTypeForwardPointer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the current type equal to one of the types we have aready visited?
|
2018-01-03 00:54:55 +00:00
|
|
|
SpvId id_to_keep = 0u;
|
|
|
|
// TODO(dneto0): Use a trie to avoid quadratic behaviour? Extract the
|
|
|
|
// ResultIdTrie from unify_const_pass.cpp for this.
|
|
|
|
for (auto j : visited_types) {
|
|
|
|
if (AreTypesEqual(*i, *j, ir_context)) {
|
|
|
|
id_to_keep = j->result_id();
|
2017-07-13 00:16:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 00:54:55 +00:00
|
|
|
if (id_to_keep == 0u) {
|
2017-07-13 00:16:51 +00:00
|
|
|
// This is a never seen before type, keep it around.
|
2018-01-03 00:54:55 +00:00
|
|
|
visited_types.emplace_back(i);
|
2017-07-13 00:16:51 +00:00
|
|
|
} else {
|
|
|
|
// The same type has already been seen before, remove this one.
|
2018-03-05 14:57:41 +00:00
|
|
|
ir_context->KillNamesAndDecorates(i->result_id());
|
2018-01-03 00:54:55 +00:00
|
|
|
ir_context->ReplaceAllUsesWith(i->result_id(), id_to_keep);
|
2017-07-13 00:16:51 +00:00
|
|
|
modified = true;
|
2018-01-03 00:54:55 +00:00
|
|
|
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) {
|
|
|
|
ir_context->KillInst(i);
|
2017-12-18 22:02:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
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.
|
2017-07-13 00:16:51 +00:00
|
|
|
bool RemoveDuplicatesPass::RemoveDuplicateDecorations(
|
2018-01-03 00:54:55 +00:00
|
|
|
ir::IRContext* ir_context) 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-01-03 00:54:55 +00:00
|
|
|
opt::analysis::DecorationManager decoration_manager(ir_context->module());
|
|
|
|
for (auto* i = &*ir_context->annotation_begin(); i;) {
|
2017-07-13 00:16:51 +00:00
|
|
|
// Is the current decoration equal to one of the decorations we have aready
|
|
|
|
// 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-01-03 00:54:55 +00:00
|
|
|
i = ir_context->KillInst(i);
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveDuplicatesPass::AreTypesEqual(const Instruction& inst1,
|
|
|
|
const Instruction& inst2,
|
2017-12-18 22:02:19 +00:00
|
|
|
ir::IRContext* context) {
|
2017-07-13 00:16:51 +00:00
|
|
|
if (inst1.opcode() != inst2.opcode()) return false;
|
2017-12-18 22:02:19 +00:00
|
|
|
if (!ir::IsTypeInst(inst1.opcode())) return false;
|
|
|
|
|
|
|
|
const analysis::Type* type1 =
|
|
|
|
context->get_type_mgr()->GetType(inst1.result_id());
|
|
|
|
const analysis::Type* type2 =
|
|
|
|
context->get_type_mgr()->GetType(inst2.result_id());
|
|
|
|
if (type1 && type2 && *type1 == *type2) return true;
|
|
|
|
|
|
|
|
return false;
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|