tint: Add operator support to intrinsic-gen

Adapt the builtin parsing and resolving to also support operators.
Will be used to generate intrinsic table entries for operators.

This will simplify maintenance of the operators, and will greatly
simplify the [AbstractInt -> i32|u32] [AbstractFloat -> f32|f16] logic.

Bug: tint:1504
Change-Id: Id75735ea24e501877418812185796f3fba88a521
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/89026
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2022-05-09 18:08:23 +00:00
committed by Dawn LUCI CQ
parent d84daed72c
commit e6e96def66
18 changed files with 783 additions and 347 deletions

View File

@@ -762,9 +762,9 @@ struct OverloadInfo {
bool is_deprecated;
};
/// BuiltinInfo describes a builtin function
struct BuiltinInfo {
/// Number of overloads of the builtin function
/// IntrinsicInfo describes a builtin function
struct IntrinsicInfo {
/// Number of overloads of the intrinsic
const uint8_t num_overloads;
/// Pointer to the start of the overloads for the function
OverloadInfo const* const overloads;
@@ -772,9 +772,8 @@ struct BuiltinInfo {
#include "intrinsic_table.inl"
/// BuiltinPrototype describes a fully matched builtin function, which is
/// used as a lookup for building unique sem::Builtin instances.
struct BuiltinPrototype {
/// IntrinsicPrototype describes a fully matched intrinsic.
struct IntrinsicPrototype {
/// Parameter describes a single parameter
struct Parameter {
/// Parameter type
@@ -783,11 +782,11 @@ struct BuiltinPrototype {
ParameterUsage const usage = ParameterUsage::kNone;
};
/// Hasher provides a hash function for the BuiltinPrototype
/// Hasher provides a hash function for the IntrinsicPrototype
struct Hasher {
/// @param i the BuiltinPrototype to create a hash for
/// @param i the IntrinsicPrototype to create a hash for
/// @return the hash value
inline std::size_t operator()(const BuiltinPrototype& i) const {
inline std::size_t operator()(const IntrinsicPrototype& i) const {
size_t hash = utils::Hash(i.parameters.size());
for (auto& p : i.parameters) {
utils::HashCombine(&hash, p.type, p.usage);
@@ -803,8 +802,8 @@ struct BuiltinPrototype {
bool is_deprecated = false;
};
/// Equality operator for BuiltinPrototype
bool operator==(const BuiltinPrototype& a, const BuiltinPrototype& b) {
/// Equality operator for IntrinsicPrototype
bool operator==(const IntrinsicPrototype& a, const IntrinsicPrototype& b) {
if (a.type != b.type || a.supported_stages != b.supported_stages ||
a.return_type != b.return_type || a.is_deprecated != b.is_deprecated ||
a.parameters.size() != b.parameters.size()) {
@@ -845,7 +844,7 @@ class Impl : public IntrinsicTable {
ProgramBuilder& builder;
Matchers matchers;
std::unordered_map<BuiltinPrototype, sem::Builtin*, BuiltinPrototype::Hasher> builtins;
std::unordered_map<IntrinsicPrototype, sem::Builtin*, IntrinsicPrototype::Hasher> builtins;
};
/// @return a string representing a call to a builtin with the given argument
@@ -949,7 +948,7 @@ const sem::Builtin* Impl::Match(sem::BuiltinType builtin_type,
ClosedState closed(builder);
std::vector<BuiltinPrototype::Parameter> parameters;
std::vector<IntrinsicPrototype::Parameter> parameters;
auto num_params = std::min(num_parameters, num_arguments);
for (uint32_t p = 0; p < num_params; p++) {
@@ -957,7 +956,7 @@ const sem::Builtin* Impl::Match(sem::BuiltinType builtin_type,
auto* indices = parameter.matcher_indices;
auto* type = Match(closed, overload, indices).Type(args[p]->UnwrapRef());
if (type) {
parameters.emplace_back(BuiltinPrototype::Parameter{type, parameter.usage});
parameters.emplace_back(IntrinsicPrototype::Parameter{type, parameter.usage});
match_score += kScorePerMatchedParam;
} else {
overload_matched = false;
@@ -1014,7 +1013,7 @@ const sem::Builtin* Impl::Match(sem::BuiltinType builtin_type,
return_type = builder.create<sem::Void>();
}
BuiltinPrototype builtin;
IntrinsicPrototype builtin;
builtin.type = builtin_type;
builtin.return_type = return_type;
builtin.parameters = std::move(parameters);

View File

@@ -13,11 +13,11 @@
// limitations under the License.
////////////////////////////////////////////////////////////////////////////////
// File generated by tools/builtin-gen
// File generated by tools/intrinsic-gen
// using the template:
// src/tint/builtin_table.inl.tmpl
// and the builtin defintion file:
// src/tint/builtins.def
// src/tint/resolver/intrinsic_table.inl.tmpl
// and the intrinsic defintion file:
// src/tint/intrinsics.def
//
// Do not modify this file directly
////////////////////////////////////////////////////////////////////////////////
@@ -25,7 +25,7 @@
// clang-format off
/// TypeMatcher for 'type bool'
/// @see src/tint/builtins.def:68:6
/// @see src/tint/intrinsics.def:68:6
class Bool : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -52,7 +52,7 @@ std::string Bool::String(MatchState&) const {
}
/// TypeMatcher for 'type f32'
/// @see src/tint/builtins.def:69:6
/// @see src/tint/intrinsics.def:69:6
class F32 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -79,7 +79,7 @@ std::string F32::String(MatchState&) const {
}
/// TypeMatcher for 'type i32'
/// @see src/tint/builtins.def:70:6
/// @see src/tint/intrinsics.def:70:6
class I32 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -106,7 +106,7 @@ std::string I32::String(MatchState&) const {
}
/// TypeMatcher for 'type u32'
/// @see src/tint/builtins.def:71:6
/// @see src/tint/intrinsics.def:71:6
class U32 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -133,7 +133,7 @@ std::string U32::String(MatchState&) const {
}
/// TypeMatcher for 'type vec2'
/// @see src/tint/builtins.def:72:6
/// @see src/tint/intrinsics.def:72:6
class Vec2 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -166,7 +166,7 @@ std::string Vec2::String(MatchState& state) const {
}
/// TypeMatcher for 'type vec3'
/// @see src/tint/builtins.def:73:6
/// @see src/tint/intrinsics.def:73:6
class Vec3 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -199,7 +199,7 @@ std::string Vec3::String(MatchState& state) const {
}
/// TypeMatcher for 'type vec4'
/// @see src/tint/builtins.def:74:6
/// @see src/tint/intrinsics.def:74:6
class Vec4 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -232,7 +232,7 @@ std::string Vec4::String(MatchState& state) const {
}
/// TypeMatcher for 'type vec'
/// @see src/tint/builtins.def:75:37
/// @see src/tint/intrinsics.def:75:37
class Vec : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -273,7 +273,7 @@ std::string Vec::String(MatchState& state) const {
}
/// TypeMatcher for 'type mat'
/// @see src/tint/builtins.def:76:37
/// @see src/tint/intrinsics.def:76:37
class Mat : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -320,7 +320,7 @@ std::string Mat::String(MatchState& state) const {
}
/// TypeMatcher for 'type ptr'
/// @see src/tint/builtins.def:77:6
/// @see src/tint/intrinsics.def:77:6
class Ptr : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -365,7 +365,7 @@ std::string Ptr::String(MatchState& state) const {
}
/// TypeMatcher for 'type atomic'
/// @see src/tint/builtins.def:78:6
/// @see src/tint/intrinsics.def:78:6
class Atomic : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -398,7 +398,7 @@ std::string Atomic::String(MatchState& state) const {
}
/// TypeMatcher for 'type array'
/// @see src/tint/builtins.def:79:6
/// @see src/tint/intrinsics.def:79:6
class Array : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -431,7 +431,7 @@ std::string Array::String(MatchState& state) const {
}
/// TypeMatcher for 'type sampler'
/// @see src/tint/builtins.def:80:6
/// @see src/tint/intrinsics.def:80:6
class Sampler : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -458,7 +458,7 @@ std::string Sampler::String(MatchState&) const {
}
/// TypeMatcher for 'type sampler_comparison'
/// @see src/tint/builtins.def:81:6
/// @see src/tint/intrinsics.def:81:6
class SamplerComparison : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -485,7 +485,7 @@ std::string SamplerComparison::String(MatchState&) const {
}
/// TypeMatcher for 'type texture_1d'
/// @see src/tint/builtins.def:82:6
/// @see src/tint/intrinsics.def:82:6
class Texture1D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -518,7 +518,7 @@ std::string Texture1D::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_2d'
/// @see src/tint/builtins.def:83:6
/// @see src/tint/intrinsics.def:83:6
class Texture2D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -551,7 +551,7 @@ std::string Texture2D::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_2d_array'
/// @see src/tint/builtins.def:84:6
/// @see src/tint/intrinsics.def:84:6
class Texture2DArray : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -584,7 +584,7 @@ std::string Texture2DArray::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_3d'
/// @see src/tint/builtins.def:85:6
/// @see src/tint/intrinsics.def:85:6
class Texture3D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -617,7 +617,7 @@ std::string Texture3D::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_cube'
/// @see src/tint/builtins.def:86:6
/// @see src/tint/intrinsics.def:86:6
class TextureCube : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -650,7 +650,7 @@ std::string TextureCube::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_cube_array'
/// @see src/tint/builtins.def:87:6
/// @see src/tint/intrinsics.def:87:6
class TextureCubeArray : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -683,7 +683,7 @@ std::string TextureCubeArray::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_multisampled_2d'
/// @see src/tint/builtins.def:88:6
/// @see src/tint/intrinsics.def:88:6
class TextureMultisampled2D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -716,7 +716,7 @@ std::string TextureMultisampled2D::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_depth_2d'
/// @see src/tint/builtins.def:89:6
/// @see src/tint/intrinsics.def:89:6
class TextureDepth2D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -743,7 +743,7 @@ std::string TextureDepth2D::String(MatchState&) const {
}
/// TypeMatcher for 'type texture_depth_2d_array'
/// @see src/tint/builtins.def:90:6
/// @see src/tint/intrinsics.def:90:6
class TextureDepth2DArray : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -770,7 +770,7 @@ std::string TextureDepth2DArray::String(MatchState&) const {
}
/// TypeMatcher for 'type texture_depth_cube'
/// @see src/tint/builtins.def:91:6
/// @see src/tint/intrinsics.def:91:6
class TextureDepthCube : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -797,7 +797,7 @@ std::string TextureDepthCube::String(MatchState&) const {
}
/// TypeMatcher for 'type texture_depth_cube_array'
/// @see src/tint/builtins.def:92:6
/// @see src/tint/intrinsics.def:92:6
class TextureDepthCubeArray : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -824,7 +824,7 @@ std::string TextureDepthCubeArray::String(MatchState&) const {
}
/// TypeMatcher for 'type texture_depth_multisampled_2d'
/// @see src/tint/builtins.def:93:6
/// @see src/tint/intrinsics.def:93:6
class TextureDepthMultisampled2D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -851,7 +851,7 @@ std::string TextureDepthMultisampled2D::String(MatchState&) const {
}
/// TypeMatcher for 'type texture_storage_1d'
/// @see src/tint/builtins.def:94:6
/// @see src/tint/intrinsics.def:94:6
class TextureStorage1D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -890,7 +890,7 @@ std::string TextureStorage1D::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_storage_2d'
/// @see src/tint/builtins.def:95:6
/// @see src/tint/intrinsics.def:95:6
class TextureStorage2D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -929,7 +929,7 @@ std::string TextureStorage2D::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_storage_2d_array'
/// @see src/tint/builtins.def:96:6
/// @see src/tint/intrinsics.def:96:6
class TextureStorage2DArray : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -968,7 +968,7 @@ std::string TextureStorage2DArray::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_storage_3d'
/// @see src/tint/builtins.def:97:6
/// @see src/tint/intrinsics.def:97:6
class TextureStorage3D : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -1007,7 +1007,7 @@ std::string TextureStorage3D::String(MatchState& state) const {
}
/// TypeMatcher for 'type texture_external'
/// @see src/tint/builtins.def:98:6
/// @see src/tint/intrinsics.def:98:6
class TextureExternal : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -1034,7 +1034,7 @@ std::string TextureExternal::String(MatchState&) const {
}
/// TypeMatcher for 'type __modf_result'
/// @see src/tint/builtins.def:100:6
/// @see src/tint/intrinsics.def:100:6
class ModfResult : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -1061,7 +1061,7 @@ std::string ModfResult::String(MatchState&) const {
}
/// TypeMatcher for 'type __modf_result_vec'
/// @see src/tint/builtins.def:101:42
/// @see src/tint/intrinsics.def:101:42
class ModfResultVec : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -1096,7 +1096,7 @@ std::string ModfResultVec::String(MatchState& state) const {
}
/// TypeMatcher for 'type __frexp_result'
/// @see src/tint/builtins.def:102:6
/// @see src/tint/intrinsics.def:102:6
class FrexpResult : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -1123,7 +1123,7 @@ std::string FrexpResult::String(MatchState&) const {
}
/// TypeMatcher for 'type __frexp_result_vec'
/// @see src/tint/builtins.def:103:43
/// @see src/tint/intrinsics.def:103:43
class FrexpResultVec : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules.
@@ -1158,7 +1158,7 @@ std::string FrexpResultVec::String(MatchState& state) const {
}
/// TypeMatcher for 'match fiu32'
/// @see src/tint/builtins.def:111:7
/// @see src/tint/intrinsics.def:111:7
class Fiu32 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules, and returns the
@@ -1192,7 +1192,7 @@ std::string Fiu32::String(MatchState&) const {
}
/// TypeMatcher for 'match iu32'
/// @see src/tint/builtins.def:112:7
/// @see src/tint/intrinsics.def:112:7
class Iu32 : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules, and returns the
@@ -1223,7 +1223,7 @@ std::string Iu32::String(MatchState&) const {
}
/// TypeMatcher for 'match scalar'
/// @see src/tint/builtins.def:113:7
/// @see src/tint/intrinsics.def:113:7
class Scalar : public TypeMatcher {
public:
/// Checks whether the given type matches the matcher rules, and returns the
@@ -1260,7 +1260,7 @@ std::string Scalar::String(MatchState&) const {
}
/// EnumMatcher for 'match f32_texel_format'
/// @see src/tint/builtins.def:124:7
/// @see src/tint/intrinsics.def:124:7
class F32TexelFormat : public NumberMatcher {
public:
/// Checks whether the given number matches the enum matcher rules.
@@ -1293,7 +1293,7 @@ std::string F32TexelFormat::String(MatchState&) const {
}
/// EnumMatcher for 'match i32_texel_format'
/// @see src/tint/builtins.def:126:7
/// @see src/tint/intrinsics.def:126:7
class I32TexelFormat : public NumberMatcher {
public:
/// Checks whether the given number matches the enum matcher rules.
@@ -1325,7 +1325,7 @@ std::string I32TexelFormat::String(MatchState&) const {
}
/// EnumMatcher for 'match u32_texel_format'
/// @see src/tint/builtins.def:128:7
/// @see src/tint/intrinsics.def:128:7
class U32TexelFormat : public NumberMatcher {
public:
/// Checks whether the given number matches the enum matcher rules.
@@ -1357,7 +1357,7 @@ std::string U32TexelFormat::String(MatchState&) const {
}
/// EnumMatcher for 'match write_only'
/// @see src/tint/builtins.def:131:7
/// @see src/tint/intrinsics.def:131:7
class WriteOnly : public NumberMatcher {
public:
/// Checks whether the given number matches the enum matcher rules.
@@ -1383,7 +1383,7 @@ std::string WriteOnly::String(MatchState&) const {
}
/// EnumMatcher for 'match function_private_workgroup'
/// @see src/tint/builtins.def:133:7
/// @see src/tint/intrinsics.def:133:7
class FunctionPrivateWorkgroup : public NumberMatcher {
public:
/// Checks whether the given number matches the enum matcher rules.
@@ -1413,7 +1413,7 @@ std::string FunctionPrivateWorkgroup::String(MatchState&) const {
}
/// EnumMatcher for 'match workgroup_or_storage'
/// @see src/tint/builtins.def:134:7
/// @see src/tint/intrinsics.def:134:7
class WorkgroupOrStorage : public NumberMatcher {
public:
/// Checks whether the given number matches the enum matcher rules.
@@ -8814,7 +8814,7 @@ constexpr OverloadInfo kOverloads[] = {
},
};
constexpr BuiltinInfo kBuiltins[] = {
constexpr IntrinsicInfo kBuiltins[] = {
{
/* [0] */
/* fn abs<T : fiu32>(T) -> T */

View File

@@ -23,7 +23,7 @@ See:
{{ end -}}
{{- end -}}
{{- with BuiltinTable -}}
{{- with IntrinsicTable -}}
{{- template "Matchers" . }}
constexpr MatcherIndex kMatcherIndices[] = {
@@ -106,15 +106,15 @@ constexpr OverloadInfo kOverloads[] = {
{{- end }}
};
constexpr BuiltinInfo kBuiltins[] = {
{{- range $i, $f := .Functions }}
constexpr IntrinsicInfo kBuiltins[] = {
{{- range $i, $b := .Builtins }}
{
/* [{{$i}}] */
{{- range $f.OverloadDescriptions }}
{{- range $b.OverloadDescriptions }}
/* {{.}} */
{{- end }}
/* num overloads */ {{$f.NumOverloads}},
/* overloads */ &kOverloads[{{$f.OverloadsOffset}}],
/* num overloads */ {{$b.NumOverloads}},
/* overloads */ &kOverloads[{{$b.OverloadsOffset}}],
},
{{- end }}
};

View File

@@ -15,7 +15,7 @@ See:
namespace tint::sem {
BuiltinType ParseBuiltinType(const std::string& name) {
{{- range .Sem.Functions }}
{{- range .Sem.Builtins }}
if (name == "{{.Name}}") {
return BuiltinType::k{{Title .Name}};
}
@@ -27,7 +27,7 @@ const char* str(BuiltinType i) {
switch (i) {
case BuiltinType::kNone:
return "<none>";
{{- range .Sem.Functions }}
{{- range .Sem.Builtins }}
case BuiltinType::k{{Title .Name}}:
return "{{.Name}}";
{{- end }}

View File

@@ -19,7 +19,7 @@ namespace tint::sem {
/// Enumerator of all builtin functions
enum class BuiltinType {
kNone = -1,
{{- range .Sem.Functions }}
{{- range .Sem.Builtins }}
k{{Title .Name}},
{{- end }}
};