reader/spirv: Use the new UniqueAllocator

For constructing the spirv unique types.

Change-Id: Ieb89ac8b3d9a35b2035097216c986de112af9b6b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/82742
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-03-07 17:05:28 +00:00
parent 0f324efe4f
commit a0eac2a492
1 changed files with 81 additions and 83 deletions

View File

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