diff --git a/src/tint/reader/spirv/parser_type.cc b/src/tint/reader/spirv/parser_type.cc index 42fe6fa0c8..805a10b9f9 100644 --- a/src/tint/reader/spirv/parser_type.cc +++ b/src/tint/reader/spirv/parser_type.cc @@ -21,6 +21,7 @@ #include "src/tint/program_builder.h" #include "src/tint/utils/hash.h" #include "src/tint/utils/map.h" +#include "src/tint/utils/unique_allocator.h" TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Type); TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Void); @@ -79,6 +80,28 @@ struct ArrayHasher { } }; +struct AliasHasher { + size_t operator()(const Alias& t) const { return utils::Hash(t.name); } +}; + +struct StructHasher { + size_t operator()(const Struct& t) const { return utils::Hash(t.name); } +}; + +struct SamplerHasher { + size_t operator()(const Sampler& s) const { return utils::Hash(s.kind); } +}; + +struct DepthTextureHasher { + size_t operator()(const DepthTexture& t) const { return utils::Hash(t.dims); } +}; + +struct DepthMultisampledTextureHasher { + size_t operator()(const DepthMultisampledTexture& t) const { + return utils::Hash(t.dims); + } +}; + struct MultisampledTextureHasher { size_t operator()(const MultisampledTexture& t) const { return utils::Hash(t.dims, t.type); @@ -118,6 +141,23 @@ static bool operator==(const Array& a, const Array& b) { return a.type == b.type && a.size == b.size && a.stride == b.stride; } +static bool operator==(const Named& a, const Named& b) { + return a.name == b.name; +} + +static bool operator==(const Sampler& a, const Sampler& b) { + return a.kind == b.kind; +} + +static bool operator==(const DepthTexture& a, const DepthTexture& b) { + return a.dims == b.dims; +} + +static bool operator==(const DepthMultisampledTexture& a, + const DepthMultisampledTexture& b) { + return a.dims == b.dims; +} + static bool operator==(const MultisampledTexture& a, const MultisampledTexture& b) { return a.dims == b.dims && a.type == b.type; @@ -267,7 +307,7 @@ const ast::Type* Struct::Build(ProgramBuilder& b) const { /// The PIMPL state of the Types object. struct TypeManager::State { - /// The allocator of types + /// The allocator of primitive types utils::BlockAllocator allocator_; /// The lazily-created Void type spirv::Void const* void_ = nullptr; @@ -279,48 +319,37 @@ struct TypeManager::State { spirv::F32 const* f32_ = nullptr; /// The lazily-created I32 type spirv::I32 const* i32_ = nullptr; - /// Map of Pointer to the returned Pointer type instance - std::unordered_map - pointers_; - /// Map of Reference to the returned Reference type instance - std::unordered_map - references_; - /// Map of Vector to the returned Vector type instance - std::unordered_map - vectors_; - /// Map of Matrix to the returned Matrix type instance - std::unordered_map - matrices_; - /// Map of Array to the returned Array type instance - std::unordered_map arrays_; - /// Map of type name to returned Alias instance - std::unordered_map aliases_; - /// Map of type name to returned Struct instance - std::unordered_map structs_; - /// Map of ast::SamplerKind to returned Sampler instance - std::unordered_map samplers_; - /// Map of ast::TextureDimension to returned DepthTexture instance - std::unordered_map + /// Unique Pointer instances + utils::UniqueAllocator pointers_; + /// Unique Reference instances + utils::UniqueAllocator references_; + /// Unique Vector instances + utils::UniqueAllocator vectors_; + /// Unique Matrix instances + utils::UniqueAllocator matrices_; + /// Unique Array instances + utils::UniqueAllocator arrays_; + /// Unique Alias instances + utils::UniqueAllocator aliases_; + /// Unique Struct instances + utils::UniqueAllocator structs_; + /// Unique Sampler instances + utils::UniqueAllocator samplers_; + /// Unique DepthTexture instances + utils::UniqueAllocator depth_textures_; - /// Map of ast::TextureDimension to returned DepthMultisampledTexture instance - std::unordered_map + /// Unique DepthMultisampledTexture instances + utils::UniqueAllocator depth_multisampled_textures_; - /// Map of MultisampledTexture to the returned MultisampledTexture type - /// instance - std::unordered_map + /// Unique MultisampledTexture instances + utils::UniqueAllocator multisampled_textures_; - /// Map of SampledTexture to the returned SampledTexture type instance - std::unordered_map + /// Unique SampledTexture instances + utils::UniqueAllocator sampled_textures_; - /// Map of StorageTexture to the returned StorageTexture type instance - std::unordered_map + /// Unique StorageTexture instances + utils::UniqueAllocator storage_textures_; }; @@ -445,100 +474,69 @@ const spirv::I32* TypeManager::I32() { const spirv::Pointer* TypeManager::Pointer(const Type* el, ast::StorageClass sc) { - return utils::GetOrCreate(state->pointers_, spirv::Pointer(el, sc), [&] { - return state->allocator_.Create(el, sc); - }); + return state->pointers_.Get(el, sc); } const spirv::Reference* TypeManager::Reference(const Type* el, ast::StorageClass sc) { - return utils::GetOrCreate(state->references_, spirv::Reference(el, sc), [&] { - return state->allocator_.Create(el, sc); - }); + return state->references_.Get(el, sc); } const spirv::Vector* TypeManager::Vector(const Type* el, uint32_t size) { - return utils::GetOrCreate(state->vectors_, spirv::Vector(el, size), [&] { - return state->allocator_.Create(el, size); - }); + return state->vectors_.Get(el, size); } const spirv::Matrix* TypeManager::Matrix(const Type* el, uint32_t columns, uint32_t rows) { - return utils::GetOrCreate( - state->matrices_, spirv::Matrix(el, columns, rows), [&] { - return state->allocator_.Create(el, columns, rows); - }); + return state->matrices_.Get(el, columns, rows); } const spirv::Array* TypeManager::Array(const Type* el, uint32_t size, uint32_t stride) { - return utils::GetOrCreate( - state->arrays_, spirv::Array(el, size, stride), - [&] { return state->allocator_.Create(el, size, stride); }); + return state->arrays_.Get(el, size, stride); } const spirv::Alias* TypeManager::Alias(Symbol name, const Type* ty) { - return utils::GetOrCreate(state->aliases_, name, [&] { - return state->allocator_.Create(name, ty); - }); + return state->aliases_.Get(name, ty); } const spirv::Struct* TypeManager::Struct(Symbol name, TypeList members) { - return utils::GetOrCreate(state->structs_, name, [&] { - return state->allocator_.Create(name, std::move(members)); - }); + return state->structs_.Get(name, std::move(members)); } const spirv::Sampler* TypeManager::Sampler(ast::SamplerKind kind) { - return utils::GetOrCreate(state->samplers_, kind, [&] { - return state->allocator_.Create(kind); - }); + return state->samplers_.Get(kind); } const spirv::DepthTexture* TypeManager::DepthTexture( ast::TextureDimension dims) { - return utils::GetOrCreate(state->depth_textures_, dims, [&] { - return state->allocator_.Create(dims); - }); + return state->depth_textures_.Get(dims); } const spirv::DepthMultisampledTexture* TypeManager::DepthMultisampledTexture( ast::TextureDimension dims) { - return utils::GetOrCreate(state->depth_multisampled_textures_, dims, [&] { - return state->allocator_.Create(dims); - }); + return state->depth_multisampled_textures_.Get(dims); } const spirv::MultisampledTexture* TypeManager::MultisampledTexture( ast::TextureDimension dims, const Type* ty) { - return utils::GetOrCreate( - state->multisampled_textures_, spirv::MultisampledTexture(dims, ty), [&] { - return state->allocator_.Create(dims, ty); - }); + return state->multisampled_textures_.Get(dims, ty); } const spirv::SampledTexture* TypeManager::SampledTexture( ast::TextureDimension dims, const Type* ty) { - return utils::GetOrCreate( - state->sampled_textures_, spirv::SampledTexture(dims, ty), [&] { - return state->allocator_.Create(dims, ty); - }); + return state->sampled_textures_.Get(dims, ty); } const spirv::StorageTexture* TypeManager::StorageTexture( ast::TextureDimension dims, ast::TexelFormat fmt, ast::Access access) { - return utils::GetOrCreate( - state->storage_textures_, spirv::StorageTexture(dims, fmt, access), [&] { - return state->allocator_.Create(dims, fmt, - access); - }); + return state->storage_textures_.Get(dims, fmt, access); } // Debug String() methods for Type classes. Only enabled in debug builds.