tools: Expand / clean-up the template WGSL type helpers

Fix `IsAbstract()` so that it doesn't consider most-nested element types.
Add `ElementType()` and `DeepestElementType()` helpers.
Add `OverloadUsesF16` as a helper for https://dawn-review.googlesource.com/c/dawn/+/96722.

Simplifies template code.

Change-Id: Iff5a9a7258caea06e00ee37c29e5298d9c35b799
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97361
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2022-07-27 19:36:49 +00:00
committed by Dawn LUCI CQ
parent 8dbd4d0280
commit a253a5f7e7
4 changed files with 57 additions and 15 deletions

View File

@@ -302,8 +302,11 @@ func (g *generator) bindAndParse(t *template.Template, text string) error {
"IsTemplateNumberParam": is(sem.TemplateNumberParam{}),
"IsTemplateTypeParam": is(sem.TemplateTypeParam{}),
"IsType": is(sem.Type{}),
"ElementType": gen.ElementType,
"DeepestElementType": gen.DeepestElementType,
"IsAbstract": gen.IsAbstract,
"IsDeclarable": gen.IsDeclarable,
"OverloadUsesF16": gen.OverloadUsesF16,
"IsFirstIn": isFirstIn,
"IsLastIn": isLastIn,
"Sem": g.cache.sem,

View File

@@ -444,16 +444,40 @@ func SplitDisplayName(displayName string) []string {
return parts
}
// IsAbstract returns true if the FullyQualifiedName refers to an abstract
// numeric type
// ElementType returns the nested type for type represented by the fully qualified name.
// If the type is not a composite type, then the fully qualified name is returned
func ElementType(fqn sem.FullyQualifiedName) sem.FullyQualifiedName {
switch fqn.Target.GetName() {
case "vec":
return fqn.TemplateArguments[1].(sem.FullyQualifiedName)
case "mat":
return fqn.TemplateArguments[2].(sem.FullyQualifiedName)
case "array":
return fqn.TemplateArguments[0].(sem.FullyQualifiedName)
}
return fqn
}
// DeepestElementType returns the inner most nested type for type represented by the
// fully qualified name.
func DeepestElementType(fqn sem.FullyQualifiedName) sem.FullyQualifiedName {
switch fqn.Target.GetName() {
case "vec":
return fqn.TemplateArguments[1].(sem.FullyQualifiedName)
case "mat":
return DeepestElementType(fqn.TemplateArguments[2].(sem.FullyQualifiedName))
case "array":
return DeepestElementType(fqn.TemplateArguments[0].(sem.FullyQualifiedName))
}
return fqn
}
// IsAbstract returns true if the FullyQualifiedName refers to an abstract numeric type float.
// Use DeepestElementType if you want to include vector, matrices and arrays of abstract types.
func IsAbstract(fqn sem.FullyQualifiedName) bool {
switch fqn.Target.GetName() {
case "ia", "fa":
return true
case "vec":
return IsAbstract(fqn.TemplateArguments[1].(sem.FullyQualifiedName))
case "mat":
return IsAbstract(fqn.TemplateArguments[2].(sem.FullyQualifiedName))
}
return false
}
@@ -461,5 +485,20 @@ func IsAbstract(fqn sem.FullyQualifiedName) bool {
// IsDeclarable returns false if the FullyQualifiedName refers to an abstract
// numeric type, or if it starts with a leading underscore.
func IsDeclarable(fqn sem.FullyQualifiedName) bool {
return !IsAbstract(fqn) && !strings.HasPrefix(fqn.Target.GetName(), "_")
return !IsAbstract(DeepestElementType(fqn)) && !strings.HasPrefix(fqn.Target.GetName(), "_")
}
// OverloadUsesF16 returns true if the overload uses the f16 type anywhere in the signature.
func OverloadUsesF16(overload *sem.Overload, typename string) bool {
for _, param := range overload.Parameters {
if DeepestElementType(param.Type).Target.GetName() == "f16" {
return true
}
}
if ret := overload.ReturnType; ret != nil {
if DeepestElementType(*overload.ReturnType).Target.GetName() == "f16" {
return true
}
}
return false
}

View File

@@ -336,8 +336,8 @@ func validate(fqn sem.FullyQualifiedName, uses *sem.StageUses) bool {
strings.Contains(elTyName, "sampler"),
strings.Contains(elTyName, "texture"):
return false // Not storable
case IsAbstract(elTy):
return false // Abstract types are not typeable nor supported by arrays
case IsAbstract(DeepestElementType(elTy)):
return false // Abstract types are not typeable
}
case "ptr":
// https://gpuweb.github.io/gpuweb/wgsl/#storage-class