reader/spirv: Add Reference type

Currently unused, but will be soon.

Also add String() methods on all the types ifndef DEBUG.
Again unused, but these are helpful for debugging type related bugs.

Bug: tint:727
Change-Id: I7598fd7f68f4819ef4b60dbfc08322fd004d9152
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51180
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-05-17 14:56:27 +00:00 committed by Commit Bot service account
parent 85bdf1753e
commit c88fbe0fb2
2 changed files with 252 additions and 5 deletions

View File

@ -14,6 +14,7 @@
#include "src/reader/spirv/parser_type.h"
#include <string>
#include <unordered_map>
#include <utility>
@ -28,6 +29,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::U32);
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::F32);
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::I32);
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Pointer);
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Reference);
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Vector);
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Matrix);
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Array);
@ -53,6 +55,12 @@ struct PointerHasher {
}
};
struct ReferenceHasher {
size_t operator()(const Reference& t) const {
return utils::Hash(t.type, t.storage_class);
}
};
struct VectorHasher {
size_t operator()(const Vector& t) const {
return utils::Hash(t.type, t.size);
@ -100,6 +108,10 @@ static bool operator==(const Pointer& a, const Pointer& b) {
return a.type == b.type && a.storage_class == b.storage_class;
}
static bool operator==(const Reference& a, const Reference& b) {
return a.type == b.type && a.storage_class == b.storage_class;
}
static bool operator==(const Vector& a, const Vector& b) {
return a.type == b.type && a.size == b.size;
}
@ -157,6 +169,14 @@ ast::Type* Pointer::Build(ProgramBuilder& b) const {
return b.ty.pointer(type->Build(b), storage_class);
}
Reference::Reference(const Type* t, ast::StorageClass s)
: type(t), storage_class(s) {}
Reference::Reference(const Reference&) = default;
ast::Type* Reference::Build(ProgramBuilder& b) const {
return type->Build(b);
}
Vector::Vector(const Type* t, uint32_t s) : type(t), size(s) {}
Vector::Vector(const Vector&) = default;
@ -265,6 +285,9 @@ struct TypeManager::State {
/// Map of Pointer to the returned Pointer type instance
std::unordered_map<spirv::Pointer, const spirv::Pointer*, PointerHasher>
pointers_;
/// Map of Reference to the returned Reference type instance
std::unordered_map<spirv::Reference, const spirv::Reference*, ReferenceHasher>
references_;
/// Map of Vector to the returned Vector type instance
std::unordered_map<spirv::Vector, const spirv::Vector*, VectorHasher>
vectors_;
@ -439,6 +462,13 @@ const spirv::Pointer* TypeManager::Pointer(const Type* el,
});
}
const spirv::Reference* TypeManager::Reference(const Type* el,
ast::StorageClass sc) {
return utils::GetOrCreate(state->references_, spirv::Reference(el, sc), [&] {
return state->allocator_.Create<spirv::Reference>(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<spirv::Vector>(el, size);
@ -522,6 +552,105 @@ const spirv::StorageTexture* TypeManager::StorageTexture(
});
}
// Debug String() methods for Type classes. Only enabled in debug builds.
#ifndef NDEBUG
std::string Void::String() const {
return "void";
}
std::string Bool::String() const {
return "bool";
}
std::string U32::String() const {
return "u32";
}
std::string F32::String() const {
return "f32";
}
std::string I32::String() const {
return "i32";
}
std::string Pointer::String() const {
std::stringstream ss;
ss << "ptr<" << std::string(ast::str(storage_class)) << ", "
<< type->String() + ">";
return ss.str();
}
std::string Reference::String() const {
std::stringstream ss;
ss << "ref<" + std::string(ast::str(storage_class)) << ", " << type->String()
<< ">";
return ss.str();
}
std::string Vector::String() const {
std::stringstream ss;
ss << "vec" << size << "<" << type->String() << ">";
return ss.str();
}
std::string Matrix::String() const {
std::stringstream ss;
ss << "mat" << columns << "x" << rows << "<" << type->String() << ">";
return ss.str();
}
std::string Array::String() const {
std::stringstream ss;
ss << "array<" << type->String() << ", " << size << ", " << stride << ">";
return ss.str();
}
std::string AccessControl::String() const {
std::stringstream ss;
ss << "[[access(" << access << ")]] " << type->String();
return ss.str();
}
std::string Sampler::String() const {
switch (kind) {
case ast::SamplerKind::kSampler:
return "sampler";
case ast::SamplerKind::kComparisonSampler:
return "sampler_comparison";
}
return "<unknown sampler>";
}
std::string DepthTexture::String() const {
std::stringstream ss;
ss << "depth_" << dims;
return ss.str();
}
std::string MultisampledTexture::String() const {
std::stringstream ss;
ss << "texture_multisampled_" << dims << "<" << type << ">";
return ss.str();
}
std::string SampledTexture::String() const {
std::stringstream ss;
ss << "texture_" << dims << "<" << type << ">";
return ss.str();
}
std::string StorageTexture::String() const {
std::stringstream ss;
ss << "texture_storage_" << dims << "<" << format << ">";
return ss.str();
}
std::string Named::String() const {
return name.to_str();
}
#endif // NDEBUG
} // namespace spirv
} // namespace reader
} // namespace tint

View File

@ -16,6 +16,7 @@
#define SRC_READER_SPIRV_PARSER_TYPE_H_
#include <memory>
#include <string>
#include <vector>
#include "src/ast/access_control.h"
@ -45,8 +46,7 @@ class Type : public Castable<Type> {
/// @returns the constructed ast::Type node for the given type
virtual ast::Type* Build(ProgramBuilder& b) const = 0;
/// @returns the inner most pointee type if this is a pointer, `this`
/// otherwise
/// @returns the inner most store type if this is a pointer, `this` otherwise
const Type* UnwrapPtr() const;
/// @returns the inner most aliased type if this is an alias, `this` otherwise
@ -78,6 +78,11 @@ class Type : public Castable<Type> {
bool IsUnsignedIntegerVector() const;
/// @returns true if this type is an unsigned scalar or vector
bool IsUnsignedScalarOrVector() const;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
virtual std::string String() const = 0;
#endif // NDEBUG
};
using TypeList = std::vector<const Type*>;
@ -87,6 +92,11 @@ struct Void : public Castable<Void, Type> {
/// @param b the ProgramBuilder used to construct the AST types
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
};
/// `bool` type
@ -94,6 +104,11 @@ struct Bool : public Castable<Bool, Type> {
/// @param b the ProgramBuilder used to construct the AST types
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
};
/// `u32` type
@ -101,6 +116,11 @@ struct U32 : public Castable<U32, Type> {
/// @param b the ProgramBuilder used to construct the AST types
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
};
/// `f32` type
@ -108,6 +128,11 @@ struct F32 : public Castable<F32, Type> {
/// @param b the ProgramBuilder used to construct the AST types
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
};
/// `i32` type
@ -115,12 +140,17 @@ struct I32 : public Castable<I32, Type> {
/// @param b the ProgramBuilder used to construct the AST types
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
};
/// `ptr<SC, T>` type
struct Pointer : public Castable<Pointer, Type> {
/// Constructor
/// @param ty the pointee type
/// @param ty the store type
/// @param sc the pointer storage class
Pointer(const Type* ty, ast::StorageClass sc);
@ -132,7 +162,40 @@ struct Pointer : public Castable<Pointer, Type> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
/// the pointee type
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the store type
Type const* const type;
/// the pointer storage class
ast::StorageClass const storage_class;
};
/// `ref<SC, T>` type
/// Note this has no AST representation, but is used for type tracking in the
/// reader.
struct Reference : public Castable<Reference, Type> {
/// Constructor
/// @param ty the referenced type
/// @param sc the reference storage class
Reference(const Type* ty, ast::StorageClass sc);
/// Copy constructor
/// @param other the other type to copy
Reference(const Reference& other);
/// @param b the ProgramBuilder used to construct the AST types
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the store type
Type const* const type;
/// the pointer storage class
ast::StorageClass const storage_class;
@ -153,6 +216,11 @@ struct Vector : public Castable<Vector, Type> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the element type
Type const* const type;
/// the number of elements in the vector
@ -175,6 +243,11 @@ struct Matrix : public Castable<Matrix, Type> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the matrix element type
Type const* const type;
/// the number of columns in the matrix
@ -200,6 +273,11 @@ struct Array : public Castable<Array, Type> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the element type
Type const* const type;
/// the number of elements in the array. 0 represents runtime-sized array.
@ -224,6 +302,11 @@ struct AccessControl : public Castable<AccessControl, Type> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the inner type
Type const* const type;
/// the access control
@ -244,6 +327,11 @@ struct Sampler : public Castable<Sampler, Type> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the sampler kind
ast::SamplerKind const kind;
};
@ -275,6 +363,11 @@ struct DepthTexture : public Castable<DepthTexture, Texture> {
/// @param b the ProgramBuilder used to construct the AST types
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
};
/// `texture_multisampled_D<T>` type
@ -292,6 +385,11 @@ struct MultisampledTexture : public Castable<MultisampledTexture, Texture> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the multisampled texture type
Type const* const type;
};
@ -311,6 +409,11 @@ struct SampledTexture : public Castable<SampledTexture, Texture> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the sampled texture type
Type const* const type;
};
@ -330,6 +433,11 @@ struct StorageTexture : public Castable<StorageTexture, Texture> {
/// @returns the constructed ast::Type node for the given type
ast::Type* Build(ProgramBuilder& b) const override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the storage image format
ast::ImageFormat const format;
};
@ -347,6 +455,11 @@ struct Named : public Castable<Named, Type> {
/// Destructor
~Named() override;
#ifndef NDEBUG
/// @returns a string representation of the type, for debug purposes only
std::string String() const override;
#endif // NDEBUG
/// the type name
Symbol const name;
};
@ -411,11 +524,16 @@ class TypeManager {
const spirv::F32* F32();
/// @return a I32 type. Repeated calls will return the same pointer.
const spirv::I32* I32();
/// @param ty the pointee type
/// @param ty the store type
/// @param sc the pointer storage class
/// @return a Pointer type. Repeated calls with the same arguments will return
/// the same pointer.
const spirv::Pointer* Pointer(const Type* ty, ast::StorageClass sc);
/// @param ty the referenced type
/// @param sc the reference storage class
/// @return a Reference type. Repeated calls with the same arguments will
/// return the same pointer.
const spirv::Reference* Reference(const Type* ty, ast::StorageClass sc);
/// @param ty the element type
/// @param sz the number of elements in the vector
/// @return a Vector type. Repeated calls with the same arguments will return