Remove more uses of AST to_str() and type_name()

These methods are going to be removed as they provide little benefit over the WGSL form, are a maintainance burden and they massively bloat our codebase.

This change introduces sem::CallTargetSignature, which can be used as a std::unordered_map key.
This is used in writer/spirv to replace a map that was keyed off ast::Function::type_name().

Bug: tint:1225
Change-Id: Ic220b3155011f21b14d49eecc8042001148e4ca5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/66443
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2021-10-14 21:17:29 +00:00
committed by Tint LUCI CQ
parent 11ed0db30d
commit 1364f202da
13 changed files with 156 additions and 79 deletions

View File

@@ -15,6 +15,7 @@
#include "src/sem/call_target.h"
#include "src/symbol_table.h"
#include "src/utils/hash.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::CallTarget);
@@ -22,15 +23,20 @@ namespace tint {
namespace sem {
CallTarget::CallTarget(sem::Type* return_type, const ParameterList& parameters)
: return_type_(return_type), parameters_(parameters) {
: signature_{return_type, parameters} {
TINT_ASSERT(Semantic, return_type);
}
CallTarget::CallTarget(const CallTarget&) = default;
CallTarget::~CallTarget() = default;
int IndexOf(const ParameterList& parameters, ParameterUsage usage) {
CallTargetSignature::CallTargetSignature(sem::Type* ret_ty,
const ParameterList& params)
: return_type(ret_ty), parameters(params) {}
CallTargetSignature::CallTargetSignature(const CallTargetSignature&) = default;
CallTargetSignature::~CallTargetSignature() = default;
int CallTargetSignature::IndexOf(ParameterUsage usage) const {
for (size_t i = 0; i < parameters.size(); i++) {
if (parameters[i]->Usage() == usage) {
return static_cast<int>(i);
@@ -39,5 +45,33 @@ int IndexOf(const ParameterList& parameters, ParameterUsage usage) {
return -1;
}
bool CallTargetSignature::operator==(const CallTargetSignature& other) const {
if (return_type != other.return_type ||
parameters.size() != other.parameters.size()) {
return false;
}
for (size_t i = 0; i < parameters.size(); i++) {
auto* a = parameters[i];
auto* b = other.parameters[i];
if (a->Type() != b->Type() || a->Usage() != b->Usage()) {
return false;
}
}
return true;
}
} // namespace sem
} // namespace tint
namespace std {
std::size_t hash<tint::sem::CallTargetSignature>::operator()(
const tint::sem::CallTargetSignature& sig) const {
size_t hash = tint::utils::Hash(sig.parameters.size());
for (auto* p : sig.parameters) {
tint::utils::HashCombine(&hash, p->Type(), p->Usage());
}
return tint::utils::Hash(hash, sig.return_type);
}
} // namespace std

View File

@@ -27,11 +27,34 @@ namespace sem {
// Forward declarations
class Type;
/// @param parameters the list of parameters
/// @param usage the parameter usage to find
/// @returns the index of the parameter with the given usage, or -1 if no
/// parameter with the given usage exists.
int IndexOf(const ParameterList& parameters, ParameterUsage usage);
/// CallTargetSignature holds the return type and parameters for a call target
struct CallTargetSignature {
/// Constructor
/// @param ret_ty the call target return type
/// @param params the call target parameters
CallTargetSignature(sem::Type* ret_ty, const ParameterList& params);
/// Copy constructor
CallTargetSignature(const CallTargetSignature&);
/// Destructor
~CallTargetSignature();
/// The type of the call target return value
sem::Type* const return_type = nullptr;
/// The parameters of the call target
ParameterList const parameters;
/// Equality operator
/// @param other the signature to compare this to
/// @returns true if this signature is equal to other
bool operator==(const CallTargetSignature& other) const;
/// @param usage the parameter usage to find
/// @returns the index of the parameter with the given usage, or -1 if no
/// parameter with the given usage exists.
int IndexOf(ParameterUsage usage) const;
};
/// CallTarget is the base for callable functions
class CallTarget : public Castable<CallTarget, Node> {
@@ -44,21 +67,38 @@ class CallTarget : public Castable<CallTarget, Node> {
/// Copy constructor
CallTarget(const CallTarget&);
/// @return the return type of the call target
sem::Type* ReturnType() const { return return_type_; }
/// Destructor
~CallTarget() override;
/// @return the return type of the call target
sem::Type* ReturnType() const { return signature_.return_type; }
/// @return the parameters of the call target
const ParameterList& Parameters() const { return parameters_; }
const ParameterList& Parameters() const { return signature_.parameters; }
/// @return the signature of the call target
const CallTargetSignature& Signature() const { return signature_; }
private:
sem::Type* const return_type_;
ParameterList const parameters_;
CallTargetSignature signature_;
};
} // namespace sem
} // namespace tint
namespace std {
/// Custom std::hash specialization for tint::sem::CallTargetSignature so
/// CallTargetSignature can be used as keys for std::unordered_map and
/// std::unordered_set.
template <>
class hash<tint::sem::CallTargetSignature> {
public:
/// @param sig the CallTargetSignature to hash
/// @return the hash value
std::size_t operator()(const tint::sem::CallTargetSignature& sig) const;
};
} // namespace std
#endif // SRC_SEM_CALL_TARGET_H_