operand: Implement operand hashing and use hashed set for declarations

Instead of manually searching each element in the declarations vector,
use an unordered_set to emplace new declarations avoiding repetition.
This commit is contained in:
ReinUsesLisp
2019-06-08 05:23:15 -03:00
parent f7c4b07a7e
commit 1e665afa36
10 changed files with 66 additions and 13 deletions

View File

@@ -59,7 +59,7 @@ std::vector<u8> Module::Assemble() const {
WriteSet(stream, execution_modes);
WriteSet(stream, debug);
WriteSet(stream, annotations);
WriteSet(stream, declarations);
WriteSet(stream, sorted_declarations);
WriteSet(stream, global_variables);
WriteSet(stream, code);
@@ -74,13 +74,14 @@ void Module::AddCapability(spv::Capability capability) {
capabilities.insert(capability);
}
void Module::SetMemoryModel(spv::AddressingModel addressing_model_, spv::MemoryModel memory_model_) {
void Module::SetMemoryModel(spv::AddressingModel addressing_model_,
spv::MemoryModel memory_model_) {
this->addressing_model = addressing_model_;
this->memory_model = memory_model_;
}
void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point,
std::string name, const std::vector<Id>& interfaces) {
void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, std::string name,
const std::vector<Id>& interfaces) {
auto op{std::make_unique<Op>(spv::Op::OpEntryPoint)};
op->Add(static_cast<u32>(execution_model));
op->Add(entry_point);
@@ -121,14 +122,12 @@ Id Module::AddCode(spv::Op opcode, std::optional<u32> id) {
}
Id Module::AddDeclaration(std::unique_ptr<Op> op) {
const auto& found{std::find_if(declarations.begin(), declarations.end(),
[&op](const auto& other) { return *other == *op; })};
if (found != declarations.end()) {
return found->get();
const auto [it, is_inserted] = declarations.emplace(std::move(op));
const Id id = it->get();
if (is_inserted) {
sorted_declarations.push_back(id);
++bound;
}
const auto id = op.get();
declarations.push_back(std::move(op));
bound++;
return id;
}