spirv-writer: avoid dup OpCapability instructions

Change-Id: Iaf53d2addadd8efbb912e4f97ff426fd4182c9f3
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34002
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: David Neto <dneto@google.com>
This commit is contained in:
David Neto 2020-11-26 13:32:12 +00:00 committed by Commit Bot service account
parent 66794eada3
commit b496611165
3 changed files with 17 additions and 3 deletions

View File

@ -357,8 +357,11 @@ void Builder::iterate(std::function<void(const Instruction&)> cb) const {
}
void Builder::push_capability(uint32_t cap) {
capabilities_.push_back(
Instruction{spv::Op::OpCapability, {Operand::Int(cap)}});
if (capability_set_.count(cap) == 0) {
capability_set_.insert(cap);
capabilities_.push_back(
Instruction{spv::Op::OpCapability, {Operand::Int(cap)}});
}
}
void Builder::GenerateLabel(uint32_t id) {

View File

@ -18,6 +18,7 @@
#include <functional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "spirv/unified1/spirv.h"
@ -93,7 +94,8 @@ class Builder {
/// @param cb the callback to execute
void iterate(std::function<void(const Instruction&)> cb) const;
/// Adds an instruction to the list of capabilities
/// Adds an instruction to the list of capabilities, if the capability
/// hasn't already been added.
/// @param cap the capability to set
void push_capability(uint32_t cap);
/// @returns the capabilities
@ -500,6 +502,7 @@ class Builder {
std::unordered_map<uint32_t, ast::Variable*> spirv_id_to_variable_;
std::vector<uint32_t> merge_stack_;
std::vector<uint32_t> continue_stack_;
std::unordered_set<uint32_t> capability_set_;
};
} // namespace spirv

View File

@ -45,6 +45,14 @@ TEST_F(BuilderTest, TracksIdBounds) {
EXPECT_EQ(6u, b.id_bound());
}
TEST_F(BuilderTest, Capabilities_Dedup) {
b.push_capability(SpvCapabilityShader);
b.push_capability(SpvCapabilityShader);
b.push_capability(SpvCapabilityShader);
EXPECT_EQ(DumpInstructions(b.capabilities()), "OpCapability Shader\n");
}
} // namespace
} // namespace spirv
} // namespace writer