From d5d207ba9f5c44a578b6d790f28eada38687fab6 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 1 Dec 2022 13:41:56 +0000 Subject: [PATCH] tint: Add builtin type aliases (vec3f, etc) Fixed: tint:1772 Change-Id: I4bed36ded91ca5288875ed6ea819ff4bbb432186 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112340 Reviewed-by: Dan Sinclair Kokoro: Kokoro Commit-Queue: Ben Clayton Reviewed-by: James Price --- src/tint/BUILD.gn | 2 + src/tint/CMakeLists.txt | 1 + src/tint/intrinsics.def | 16 +++ src/tint/resolver/dependency_graph.cc | 10 +- src/tint/resolver/resolver.cc | 50 ++++++++- src/tint/resolver/resolver.h | 4 + src/tint/resolver/type_alias.cc | 102 ++++++++++++++++++ src/tint/resolver/type_alias.cc.tmpl | 25 +++++ src/tint/resolver/type_alias.h | 64 +++++++++++ src/tint/resolver/type_alias.h.tmpl | 29 +++++ src/tint/resolver/type_alias_bench.cc | 57 ++++++++++ src/tint/resolver/type_alias_bench.cc.tmpl | 29 +++++ src/tint/resolver/type_alias_test.cc | 97 +++++++++++++++++ src/tint/resolver/type_alias_test.cc.tmpl | 31 ++++++ src/tint/resolver/type_validation_test.cc | 51 +++++++++ src/tint/resolver/uniformity.h | 6 +- test/tint/shadowing/alias/builtin/const.wgsl | 10 ++ .../const.wgsl.expected.dxc.hlsl | 0 .../const.wgsl.expected.fxc.hlsl | 0 .../{ => builtin}/const.wgsl.expected.glsl | 0 .../{ => builtin}/const.wgsl.expected.msl | 0 .../{ => builtin}/const.wgsl.expected.spvasm | 0 .../alias/builtin/const.wgsl.expected.wgsl | 10 ++ test/tint/shadowing/alias/builtin/let.wgsl | 10 ++ .../alias/builtin/let.wgsl.expected.dxc.hlsl | 13 +++ .../alias/builtin/let.wgsl.expected.fxc.hlsl | 13 +++ .../alias/builtin/let.wgsl.expected.glsl | 15 +++ .../alias/builtin/let.wgsl.expected.msl | 12 +++ .../alias/builtin/let.wgsl.expected.spvasm | 26 +++++ .../alias/builtin/let.wgsl.expected.wgsl | 10 ++ test/tint/shadowing/alias/builtin/param.wgsl | 3 + .../builtin/param.wgsl.expected.dxc.hlsl | 8 ++ .../builtin/param.wgsl.expected.fxc.hlsl | 8 ++ .../alias/builtin/param.wgsl.expected.glsl | 10 ++ .../alias/builtin/param.wgsl.expected.msl | 7 ++ .../alias/builtin/param.wgsl.expected.spvasm | 26 +++++ .../alias/builtin/param.wgsl.expected.wgsl | 3 + test/tint/shadowing/alias/builtin/var.wgsl | 10 ++ .../alias/builtin/var.wgsl.expected.dxc.hlsl | 13 +++ .../alias/builtin/var.wgsl.expected.fxc.hlsl | 13 +++ .../alias/builtin/var.wgsl.expected.glsl | 15 +++ .../alias/builtin/var.wgsl.expected.msl | 12 +++ .../alias/builtin/var.wgsl.expected.spvasm | 42 ++++++++ .../alias/builtin/var.wgsl.expected.wgsl | 10 ++ .../shadowing/alias/{ => user}/const.wgsl | 0 .../alias/user/const.wgsl.expected.dxc.hlsl | 9 ++ .../alias/user/const.wgsl.expected.fxc.hlsl | 9 ++ .../alias/user/const.wgsl.expected.glsl | 11 ++ .../alias/user/const.wgsl.expected.msl | 8 ++ .../alias/user/const.wgsl.expected.spvasm | 21 ++++ .../alias/{ => user}/const.wgsl.expected.wgsl | 0 test/tint/shadowing/alias/{ => user}/let.wgsl | 0 .../{ => user}/let.wgsl.expected.dxc.hlsl | 0 .../{ => user}/let.wgsl.expected.fxc.hlsl | 0 .../alias/{ => user}/let.wgsl.expected.glsl | 0 .../alias/{ => user}/let.wgsl.expected.msl | 0 .../alias/{ => user}/let.wgsl.expected.spvasm | 0 .../alias/{ => user}/let.wgsl.expected.wgsl | 0 .../shadowing/alias/{ => user}/param.wgsl | 0 .../{ => user}/param.wgsl.expected.dxc.hlsl | 0 .../{ => user}/param.wgsl.expected.fxc.hlsl | 0 .../alias/{ => user}/param.wgsl.expected.glsl | 0 .../alias/{ => user}/param.wgsl.expected.msl | 0 .../{ => user}/param.wgsl.expected.spvasm | 0 .../alias/{ => user}/param.wgsl.expected.wgsl | 0 test/tint/shadowing/alias/{ => user}/var.wgsl | 0 .../{ => user}/var.wgsl.expected.dxc.hlsl | 0 .../{ => user}/var.wgsl.expected.fxc.hlsl | 0 .../alias/{ => user}/var.wgsl.expected.glsl | 0 .../alias/{ => user}/var.wgsl.expected.msl | 0 .../alias/{ => user}/var.wgsl.expected.spvasm | 0 .../alias/{ => user}/var.wgsl.expected.wgsl | 0 tools/src/tint/intrinsic/lexer/lexer.go | 27 +++-- tools/src/tint/intrinsic/lexer/lexer_test.go | 3 + 74 files changed, 943 insertions(+), 18 deletions(-) create mode 100644 src/tint/resolver/type_alias.cc create mode 100644 src/tint/resolver/type_alias.cc.tmpl create mode 100644 src/tint/resolver/type_alias.h create mode 100644 src/tint/resolver/type_alias.h.tmpl create mode 100644 src/tint/resolver/type_alias_bench.cc create mode 100644 src/tint/resolver/type_alias_bench.cc.tmpl create mode 100644 src/tint/resolver/type_alias_test.cc create mode 100644 src/tint/resolver/type_alias_test.cc.tmpl create mode 100644 test/tint/shadowing/alias/builtin/const.wgsl rename test/tint/shadowing/alias/{ => builtin}/const.wgsl.expected.dxc.hlsl (100%) rename test/tint/shadowing/alias/{ => builtin}/const.wgsl.expected.fxc.hlsl (100%) rename test/tint/shadowing/alias/{ => builtin}/const.wgsl.expected.glsl (100%) rename test/tint/shadowing/alias/{ => builtin}/const.wgsl.expected.msl (100%) rename test/tint/shadowing/alias/{ => builtin}/const.wgsl.expected.spvasm (100%) create mode 100644 test/tint/shadowing/alias/builtin/const.wgsl.expected.wgsl create mode 100644 test/tint/shadowing/alias/builtin/let.wgsl create mode 100644 test/tint/shadowing/alias/builtin/let.wgsl.expected.dxc.hlsl create mode 100644 test/tint/shadowing/alias/builtin/let.wgsl.expected.fxc.hlsl create mode 100644 test/tint/shadowing/alias/builtin/let.wgsl.expected.glsl create mode 100644 test/tint/shadowing/alias/builtin/let.wgsl.expected.msl create mode 100644 test/tint/shadowing/alias/builtin/let.wgsl.expected.spvasm create mode 100644 test/tint/shadowing/alias/builtin/let.wgsl.expected.wgsl create mode 100644 test/tint/shadowing/alias/builtin/param.wgsl create mode 100644 test/tint/shadowing/alias/builtin/param.wgsl.expected.dxc.hlsl create mode 100644 test/tint/shadowing/alias/builtin/param.wgsl.expected.fxc.hlsl create mode 100644 test/tint/shadowing/alias/builtin/param.wgsl.expected.glsl create mode 100644 test/tint/shadowing/alias/builtin/param.wgsl.expected.msl create mode 100644 test/tint/shadowing/alias/builtin/param.wgsl.expected.spvasm create mode 100644 test/tint/shadowing/alias/builtin/param.wgsl.expected.wgsl create mode 100644 test/tint/shadowing/alias/builtin/var.wgsl create mode 100644 test/tint/shadowing/alias/builtin/var.wgsl.expected.dxc.hlsl create mode 100644 test/tint/shadowing/alias/builtin/var.wgsl.expected.fxc.hlsl create mode 100644 test/tint/shadowing/alias/builtin/var.wgsl.expected.glsl create mode 100644 test/tint/shadowing/alias/builtin/var.wgsl.expected.msl create mode 100644 test/tint/shadowing/alias/builtin/var.wgsl.expected.spvasm create mode 100644 test/tint/shadowing/alias/builtin/var.wgsl.expected.wgsl rename test/tint/shadowing/alias/{ => user}/const.wgsl (100%) create mode 100644 test/tint/shadowing/alias/user/const.wgsl.expected.dxc.hlsl create mode 100644 test/tint/shadowing/alias/user/const.wgsl.expected.fxc.hlsl create mode 100644 test/tint/shadowing/alias/user/const.wgsl.expected.glsl create mode 100644 test/tint/shadowing/alias/user/const.wgsl.expected.msl create mode 100644 test/tint/shadowing/alias/user/const.wgsl.expected.spvasm rename test/tint/shadowing/alias/{ => user}/const.wgsl.expected.wgsl (100%) rename test/tint/shadowing/alias/{ => user}/let.wgsl (100%) rename test/tint/shadowing/alias/{ => user}/let.wgsl.expected.dxc.hlsl (100%) rename test/tint/shadowing/alias/{ => user}/let.wgsl.expected.fxc.hlsl (100%) rename test/tint/shadowing/alias/{ => user}/let.wgsl.expected.glsl (100%) rename test/tint/shadowing/alias/{ => user}/let.wgsl.expected.msl (100%) rename test/tint/shadowing/alias/{ => user}/let.wgsl.expected.spvasm (100%) rename test/tint/shadowing/alias/{ => user}/let.wgsl.expected.wgsl (100%) rename test/tint/shadowing/alias/{ => user}/param.wgsl (100%) rename test/tint/shadowing/alias/{ => user}/param.wgsl.expected.dxc.hlsl (100%) rename test/tint/shadowing/alias/{ => user}/param.wgsl.expected.fxc.hlsl (100%) rename test/tint/shadowing/alias/{ => user}/param.wgsl.expected.glsl (100%) rename test/tint/shadowing/alias/{ => user}/param.wgsl.expected.msl (100%) rename test/tint/shadowing/alias/{ => user}/param.wgsl.expected.spvasm (100%) rename test/tint/shadowing/alias/{ => user}/param.wgsl.expected.wgsl (100%) rename test/tint/shadowing/alias/{ => user}/var.wgsl (100%) rename test/tint/shadowing/alias/{ => user}/var.wgsl.expected.dxc.hlsl (100%) rename test/tint/shadowing/alias/{ => user}/var.wgsl.expected.fxc.hlsl (100%) rename test/tint/shadowing/alias/{ => user}/var.wgsl.expected.glsl (100%) rename test/tint/shadowing/alias/{ => user}/var.wgsl.expected.msl (100%) rename test/tint/shadowing/alias/{ => user}/var.wgsl.expected.spvasm (100%) rename test/tint/shadowing/alias/{ => user}/var.wgsl.expected.wgsl (100%) diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index 27ae6560f4..a51de7b82d 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -408,6 +408,8 @@ libtint_source_set("libtint_core_all_src") { "resolver/resolver.h", "resolver/sem_helper.cc", "resolver/sem_helper.h", + "resolver/type_alias.cc", + "resolver/type_alias.h", "resolver/uniformity.cc", "resolver/uniformity.h", "resolver/validator.cc", diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index e846756395..4f193b596e 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -542,6 +542,7 @@ tint_generated(ast/extension BENCH TEST) tint_generated(ast/interpolate_attribute BENCH TEST) tint_generated(ast/texel_format BENCH TEST) tint_generated(resolver/init_conv_intrinsic) +tint_generated(resolver/type_alias BENCH TEST) tint_generated(sem/builtin_type) tint_generated(sem/parameter_usage) diff --git a/src/tint/intrinsics.def b/src/tint/intrinsics.def index 4e89eb85e8..179cf88334 100644 --- a/src/tint/intrinsics.def +++ b/src/tint/intrinsics.def @@ -111,6 +111,22 @@ enum interpolation_sampling { sample } +// https://www.w3.org/TR/WGSL/#vector-types +enum type_alias { + vec2f + vec2h + vec2i + vec2u + vec3f + vec3h + vec3i + vec3u + vec4f + vec4h + vec4i + vec4u +} + //////////////////////////////////////////////////////////////////////////////// // WGSL primitive types // // Types may be decorated with @precedence(N) to prioritize which type // diff --git a/src/tint/resolver/dependency_graph.cc b/src/tint/resolver/dependency_graph.cc index 796412b5d7..29f88a72dc 100644 --- a/src/tint/resolver/dependency_graph.cc +++ b/src/tint/resolver/dependency_graph.cc @@ -66,6 +66,7 @@ #include "src/tint/ast/void.h" #include "src/tint/ast/while_statement.h" #include "src/tint/ast/workgroup_attribute.h" +#include "src/tint/resolver/type_alias.h" #include "src/tint/scope_stack.h" #include "src/tint/sem/builtin.h" #include "src/tint/symbol_table.h" @@ -481,9 +482,14 @@ class DependencyScanner { graph_.resolved_symbols.Add(from, resolved); } - /// @returns true if `name` is the name of a builtin function + /// @returns true if `name` is the name of a builtin function, or builtin type alias bool IsBuiltin(Symbol name) const { - return sem::ParseBuiltinType(symbols_.NameFor(name)) != sem::BuiltinType::kNone; + auto s = symbols_.NameFor(name); + if (sem::ParseBuiltinType(s) != sem::BuiltinType::kNone || + ParseTypeAlias(s) != TypeAlias::kUndefined) { + return true; + } + return false; } /// Appends an error to the diagnostics that the given symbol cannot be diff --git a/src/tint/resolver/resolver.cc b/src/tint/resolver/resolver.cc index cf55a1d30d..addd9895ce 100644 --- a/src/tint/resolver/resolver.cc +++ b/src/tint/resolver/resolver.cc @@ -51,6 +51,7 @@ #include "src/tint/ast/vector.h" #include "src/tint/ast/while_statement.h" #include "src/tint/ast/workgroup_attribute.h" +#include "src/tint/resolver/type_alias.h" #include "src/tint/resolver/uniformity.h" #include "src/tint/sem/abstract_float.h" #include "src/tint/sem/abstract_int.h" @@ -329,13 +330,16 @@ sem::Type* Resolver::Type(const ast::Type* ty) { AddNote("'" + name + "' declared here", func->Declaration()->source); return nullptr; }, - [&](Default) { + [&](Default) -> sem::Type* { if (auto* tn = ty->As()) { if (IsBuiltin(tn->name)) { auto name = builder_->Symbols().NameFor(tn->name); AddError("cannot use builtin '" + name + "' as type", ty->source); return nullptr; } + if (auto* t = BuiltinTypeAlias(tn->name)) { + return t; + } } TINT_UNREACHABLE(Resolver, diagnostics_) << "Unhandled resolved type '" @@ -2228,11 +2232,13 @@ sem::Call* Resolver::Call(const ast::CallExpression* expr) { }, [&](Default) -> sem::Call* { auto name = builder_->Symbols().NameFor(ident->symbol); - auto builtin_type = sem::ParseBuiltinType(name); - if (builtin_type != sem::BuiltinType::kNone) { + if (auto* alias = BuiltinTypeAlias(ident->symbol)) { + return ty_init_or_conv(alias); + } + if (auto builtin_type = sem::ParseBuiltinType(name); + builtin_type != sem::BuiltinType::kNone) { return BuiltinCall(expr, builtin_type, args); } - TINT_ICE(Resolver, diagnostics_) << expr->source << " unhandled CallExpression target:\n" << "resolved: " << (resolved ? resolved->TypeInfo().name : "") << "\n" @@ -2328,6 +2334,40 @@ sem::Call* Resolver::BuiltinCall(const ast::CallExpression* expr, return call; } +sem::Type* Resolver::BuiltinTypeAlias(Symbol sym) const { + auto name = builder_->Symbols().NameFor(sym); + auto& b = *builder_; + switch (ParseTypeAlias(name)) { + case TypeAlias::kVec2F: + return b.create(b.create(), 2u); + case TypeAlias::kVec3F: + return b.create(b.create(), 3u); + case TypeAlias::kVec4F: + return b.create(b.create(), 4u); + case TypeAlias::kVec2H: + return b.create(b.create(), 2u); + case TypeAlias::kVec3H: + return b.create(b.create(), 3u); + case TypeAlias::kVec4H: + return b.create(b.create(), 4u); + case TypeAlias::kVec2I: + return b.create(b.create(), 2u); + case TypeAlias::kVec3I: + return b.create(b.create(), 3u); + case TypeAlias::kVec4I: + return b.create(b.create(), 4u); + case TypeAlias::kVec2U: + return b.create(b.create(), 2u); + case TypeAlias::kVec3U: + return b.create(b.create(), 3u); + case TypeAlias::kVec4U: + return b.create(b.create(), 4u); + case TypeAlias::kUndefined: + break; + } + return nullptr; +} + void Resolver::CollectTextureSamplerPairs(const sem::Builtin* builtin, utils::VectorRef args) const { // Collect a texture/sampler pair for this builtin. @@ -2550,7 +2590,7 @@ sem::Expression* Resolver::Identifier(const ast::IdentifierExpression* expr) { return nullptr; } - if (resolved->Is()) { + if (resolved->Is() || BuiltinTypeAlias(symbol)) { AddError("missing '(' for type initializer or cast", expr->source.End()); return nullptr; } diff --git a/src/tint/resolver/resolver.h b/src/tint/resolver/resolver.h index abc76330cc..56ae91214c 100644 --- a/src/tint/resolver/resolver.h +++ b/src/tint/resolver/resolver.h @@ -384,6 +384,7 @@ class Resolver { /// Set the shadowing information on variable declarations. /// @note this method must only be called after all semantic nodes are built. void SetShadows(); + /// StatementScope() does the following: /// * Creates the AST -> SEM mapping. /// * Assigns `sem` to #current_statement_ @@ -415,6 +416,9 @@ class Resolver { /// @returns true if the symbol is the name of a builtin function. bool IsBuiltin(Symbol) const; + /// @returns the builtin type alias for the given symbol + sem::Type* BuiltinTypeAlias(Symbol) const; + // ArrayInitializerSig represents a unique array initializer signature. // It is a tuple of the array type, number of arguments provided and earliest evaluation stage. using ArrayInitializerSig = diff --git a/src/tint/resolver/type_alias.cc b/src/tint/resolver/type_alias.cc new file mode 100644 index 0000000000..124fc8c7cc --- /dev/null +++ b/src/tint/resolver/type_alias.cc @@ -0,0 +1,102 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/resolver/type_alias.cc.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + +#include "src/tint/resolver/type_alias.h" + +namespace tint::resolver { + +/// ParseTypeAlias parses a TypeAlias from a string. +/// @param str the string to parse +/// @returns the parsed enum, or TypeAlias::kUndefined if the string could not be parsed. +TypeAlias ParseTypeAlias(std::string_view str) { + if (str == "vec2f") { + return TypeAlias::kVec2F; + } + if (str == "vec2h") { + return TypeAlias::kVec2H; + } + if (str == "vec2i") { + return TypeAlias::kVec2I; + } + if (str == "vec2u") { + return TypeAlias::kVec2U; + } + if (str == "vec3f") { + return TypeAlias::kVec3F; + } + if (str == "vec3h") { + return TypeAlias::kVec3H; + } + if (str == "vec3i") { + return TypeAlias::kVec3I; + } + if (str == "vec3u") { + return TypeAlias::kVec3U; + } + if (str == "vec4f") { + return TypeAlias::kVec4F; + } + if (str == "vec4h") { + return TypeAlias::kVec4H; + } + if (str == "vec4i") { + return TypeAlias::kVec4I; + } + if (str == "vec4u") { + return TypeAlias::kVec4U; + } + return TypeAlias::kUndefined; +} + +std::ostream& operator<<(std::ostream& out, TypeAlias value) { + switch (value) { + case TypeAlias::kUndefined: + return out << "undefined"; + case TypeAlias::kVec2F: + return out << "vec2f"; + case TypeAlias::kVec2H: + return out << "vec2h"; + case TypeAlias::kVec2I: + return out << "vec2i"; + case TypeAlias::kVec2U: + return out << "vec2u"; + case TypeAlias::kVec3F: + return out << "vec3f"; + case TypeAlias::kVec3H: + return out << "vec3h"; + case TypeAlias::kVec3I: + return out << "vec3i"; + case TypeAlias::kVec3U: + return out << "vec3u"; + case TypeAlias::kVec4F: + return out << "vec4f"; + case TypeAlias::kVec4H: + return out << "vec4h"; + case TypeAlias::kVec4I: + return out << "vec4i"; + case TypeAlias::kVec4U: + return out << "vec4u"; + } + return out << ""; +} + +} // namespace tint::resolver diff --git a/src/tint/resolver/type_alias.cc.tmpl b/src/tint/resolver/type_alias.cc.tmpl new file mode 100644 index 0000000000..3843864431 --- /dev/null +++ b/src/tint/resolver/type_alias.cc.tmpl @@ -0,0 +1,25 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate type_alias.cc + +To update the generated file, run: + ./tools/run gen + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} +{{- $enum := (Sem.Enum "type_alias") -}} + +#include "src/tint/resolver/type_alias.h" + +namespace tint::resolver { + +{{ Eval "ParseEnum" $enum}} + +{{ Eval "EnumOStream" $enum}} + +} // namespace tint::resolver diff --git a/src/tint/resolver/type_alias.h b/src/tint/resolver/type_alias.h new file mode 100644 index 0000000000..40083a3df9 --- /dev/null +++ b/src/tint/resolver/type_alias.h @@ -0,0 +1,64 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/resolver/type_alias.h.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + +#ifndef SRC_TINT_RESOLVER_TYPE_ALIAS_H_ +#define SRC_TINT_RESOLVER_TYPE_ALIAS_H_ + +#include + +namespace tint::resolver { + +/// An enumerator of builtin type aliases. +enum class TypeAlias { + kUndefined, + kVec2F, + kVec2H, + kVec2I, + kVec2U, + kVec3F, + kVec3H, + kVec3I, + kVec3U, + kVec4F, + kVec4H, + kVec4I, + kVec4U, +}; + +/// @param out the std::ostream to write to +/// @param value the TypeAlias +/// @returns `out` so calls can be chained +std::ostream& operator<<(std::ostream& out, TypeAlias value); + +/// ParseTypeAlias parses a TypeAlias from a string. +/// @param str the string to parse +/// @returns the parsed enum, or TypeAlias::kUndefined if the string could not be parsed. +TypeAlias ParseTypeAlias(std::string_view str); + +constexpr const char* kTypeAliasStrings[] = { + "vec2f", "vec2h", "vec2i", "vec2u", "vec3f", "vec3h", + "vec3i", "vec3u", "vec4f", "vec4h", "vec4i", "vec4u", +}; + +} // namespace tint::resolver + +#endif // SRC_TINT_RESOLVER_TYPE_ALIAS_H_ diff --git a/src/tint/resolver/type_alias.h.tmpl b/src/tint/resolver/type_alias.h.tmpl new file mode 100644 index 0000000000..0a9b2d41d9 --- /dev/null +++ b/src/tint/resolver/type_alias.h.tmpl @@ -0,0 +1,29 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate type_alias.h + +To update the generated file, run: + ./tools/run gen + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} +{{- $enum := (Sem.Enum "type_alias") -}} + +#ifndef SRC_TINT_RESOLVER_TYPE_ALIAS_H_ +#define SRC_TINT_RESOLVER_TYPE_ALIAS_H_ + +#include + +namespace tint::resolver { + +/// An enumerator of builtin type aliases. +{{ Eval "DeclareEnum" $enum}} + +} // namespace tint::resolver + +#endif // SRC_TINT_RESOLVER_TYPE_ALIAS_H_ diff --git a/src/tint/resolver/type_alias_bench.cc b/src/tint/resolver/type_alias_bench.cc new file mode 100644 index 0000000000..25103523af --- /dev/null +++ b/src/tint/resolver/type_alias_bench.cc @@ -0,0 +1,57 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/resolver/type_alias_bench.cc.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + +#include "src/tint/resolver/type_alias.h" + +#include + +#include "benchmark/benchmark.h" + +namespace tint::resolver { +namespace { + +void TypeAliasParser(::benchmark::State& state) { + std::array kStrings{ + "veccf", "32", "vVc2f", "vec2f", "vec21", "qqeJf", "vecll7f", "veqH2pp", + "vh", "Gebh", "vec2h", "vevi2h", "ve8WWh", "Mxxc2", "vgg2i", "V2X", + "vec23", "vec2i", "vec2E", "TTeP2i", "vxxcdd", "v44c2u", "veVVSSu", "22RRu", + "vec2u", "vF2u", "vecu", "ROOHVu", "ecyf", "n77rrlcGf", "vec340", "vec3f", + "oof", "vezz", "1ipp3f", "XXec3h", "ve9IInn5h", "HHreSSaYh", "vec3h", "kk3", + "jgRR", "veb", "vjc3i", "vc3i", "vcq", "vec3i", "Nec3i", "vcvv", + "ve3QQ", "vrcf", "vecju", "NNew23", "vec3u", "ve3u", "vrrc3u", "Gec3u", + "veFF4f", "vE", "verrf", "vec4f", "vef", "veJJD", "v4", "e4k", + "vech", "Jech", "vec4h", "ec4h", "_KKttcH", "vexxh", "__qcF", "vc4qq", + "33e64i", "vec4i", "6QQott4i", "v6c4i", "zzc4O6", "vyyc4u", "vcZZ", "ecWq4u", + "vec4u", "vOO4u", "oYe4", "v4", + }; + for (auto _ : state) { + for (auto& str : kStrings) { + auto result = ParseTypeAlias(str); + benchmark::DoNotOptimize(result); + } + } +} + +BENCHMARK(TypeAliasParser); + +} // namespace +} // namespace tint::resolver diff --git a/src/tint/resolver/type_alias_bench.cc.tmpl b/src/tint/resolver/type_alias_bench.cc.tmpl new file mode 100644 index 0000000000..3a482263ed --- /dev/null +++ b/src/tint/resolver/type_alias_bench.cc.tmpl @@ -0,0 +1,29 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate type_alias_bench.cc + +To update the generated file, run: + ./tools/run gen + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} +{{- $enum := (Sem.Enum "type_alias") -}} + +#include "src/tint/resolver/type_alias.h" + +#include + +#include "benchmark/benchmark.h" + +namespace tint::resolver { +namespace { + +{{ Eval "BenchmarkParseEnum" $enum }} + +} // namespace +} // namespace tint::resolver diff --git a/src/tint/resolver/type_alias_test.cc b/src/tint/resolver/type_alias_test.cc new file mode 100644 index 0000000000..c52d44c109 --- /dev/null +++ b/src/tint/resolver/type_alias_test.cc @@ -0,0 +1,97 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/resolver/type_alias_test.cc.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + +#include "src/tint/resolver/type_alias.h" + +#include + +#include "gtest/gtest.h" + +#include "src/tint/utils/string.h" + +namespace tint::resolver { +namespace { + +namespace parse_print_tests { + +struct Case { + const char* string; + TypeAlias value; +}; + +inline std::ostream& operator<<(std::ostream& out, Case c) { + return out << "'" << std::string(c.string) << "'"; +} + +static constexpr Case kValidCases[] = { + {"vec2f", TypeAlias::kVec2F}, {"vec2h", TypeAlias::kVec2H}, {"vec2i", TypeAlias::kVec2I}, + {"vec2u", TypeAlias::kVec2U}, {"vec3f", TypeAlias::kVec3F}, {"vec3h", TypeAlias::kVec3H}, + {"vec3i", TypeAlias::kVec3I}, {"vec3u", TypeAlias::kVec3U}, {"vec4f", TypeAlias::kVec4F}, + {"vec4h", TypeAlias::kVec4H}, {"vec4i", TypeAlias::kVec4I}, {"vec4u", TypeAlias::kVec4U}, +}; + +static constexpr Case kInvalidCases[] = { + {"veccf", TypeAlias::kUndefined}, {"32", TypeAlias::kUndefined}, + {"vVc2f", TypeAlias::kUndefined}, {"vec21", TypeAlias::kUndefined}, + {"qqeJh", TypeAlias::kUndefined}, {"vecll7h", TypeAlias::kUndefined}, + {"veqH2pp", TypeAlias::kUndefined}, {"vi", TypeAlias::kUndefined}, + {"Gebi", TypeAlias::kUndefined}, {"vevi2u", TypeAlias::kUndefined}, + {"ve8WWu", TypeAlias::kUndefined}, {"Mxxc2", TypeAlias::kUndefined}, + {"vgg3f", TypeAlias::kUndefined}, {"V3X", TypeAlias::kUndefined}, + {"vec33", TypeAlias::kUndefined}, {"vec3E", TypeAlias::kUndefined}, + {"TTeP3h", TypeAlias::kUndefined}, {"vxxcdd", TypeAlias::kUndefined}, + {"v44c3i", TypeAlias::kUndefined}, {"veVVSSi", TypeAlias::kUndefined}, + {"22RRi", TypeAlias::kUndefined}, {"vF3u", TypeAlias::kUndefined}, + {"vecu", TypeAlias::kUndefined}, {"ROOHVu", TypeAlias::kUndefined}, + {"ecyf", TypeAlias::kUndefined}, {"n77rrlcGf", TypeAlias::kUndefined}, + {"vec440", TypeAlias::kUndefined}, {"ooh", TypeAlias::kUndefined}, + {"vezz", TypeAlias::kUndefined}, {"1ipp4h", TypeAlias::kUndefined}, + {"XXec4i", TypeAlias::kUndefined}, {"ve9IInn5i", TypeAlias::kUndefined}, + {"HHreSSaYi", TypeAlias::kUndefined}, {"kk4", TypeAlias::kUndefined}, + {"jgRR", TypeAlias::kUndefined}, {"veb", TypeAlias::kUndefined}, +}; + +using TypeAliasParseTest = testing::TestWithParam; + +TEST_P(TypeAliasParseTest, Parse) { + const char* string = GetParam().string; + TypeAlias expect = GetParam().value; + EXPECT_EQ(expect, ParseTypeAlias(string)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, TypeAliasParseTest, testing::ValuesIn(kValidCases)); +INSTANTIATE_TEST_SUITE_P(InvalidCases, TypeAliasParseTest, testing::ValuesIn(kInvalidCases)); + +using TypeAliasPrintTest = testing::TestWithParam; + +TEST_P(TypeAliasPrintTest, Print) { + TypeAlias value = GetParam().value; + const char* expect = GetParam().string; + EXPECT_EQ(expect, utils::ToString(value)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, TypeAliasPrintTest, testing::ValuesIn(kValidCases)); + +} // namespace parse_print_tests + +} // namespace +} // namespace tint::resolver diff --git a/src/tint/resolver/type_alias_test.cc.tmpl b/src/tint/resolver/type_alias_test.cc.tmpl new file mode 100644 index 0000000000..2f37251004 --- /dev/null +++ b/src/tint/resolver/type_alias_test.cc.tmpl @@ -0,0 +1,31 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate type_alias_test.cc + +To update the generated file, run: + ./tools/run gen + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} +{{- $enum := (Sem.Enum "type_alias") -}} + +#include "src/tint/resolver/type_alias.h" + +#include + +#include "gtest/gtest.h" + +#include "src/tint/utils/string.h" + +namespace tint::resolver { +namespace { + +{{ Eval "TestParsePrintEnum" $enum}} + +} // namespace +} // namespace tint::resolver diff --git a/src/tint/resolver/type_validation_test.cc b/src/tint/resolver/type_validation_test.cc index fe1aa0570f..b389fc1c54 100644 --- a/src/tint/resolver/type_validation_test.cc +++ b/src/tint/resolver/type_validation_test.cc @@ -1395,5 +1395,56 @@ INSTANTIATE_TEST_SUITE_P(ResolverTypeValidationTest, ParamsFor>(2))); } // namespace VectorTests +namespace BuiltinTypeAliasTests { +struct Params { + const char* alias; + builder::ast_type_func_ptr type; +}; + +template +constexpr Params Case(const char* alias) { + return Params{alias, DataType::AST}; +} + +using BuiltinTypeAliasTest = ResolverTestWithParam; +TEST_P(BuiltinTypeAliasTest, CheckEquivalent) { + // var aliased : vecTN; + // var explicit : vecN; + // explicit = aliased; + auto& params = GetParam(); + + Enable(ast::Extension::kF16); + + WrapInFunction(Decl(Var("aliased", ty.type_name(params.alias))), + Decl(Var("explicit", params.type(*this))), // + Assign("explicit", "aliased")); + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} +TEST_P(BuiltinTypeAliasTest, Construct) { + // var v : vecN = vecTN(); + auto& params = GetParam(); + + Enable(ast::Extension::kF16); + + WrapInFunction(Decl(Var("v", params.type(*this), Construct(ty.type_name(params.alias))))); + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} +INSTANTIATE_TEST_SUITE_P(ResolverTypeValidationTest, + BuiltinTypeAliasTest, + testing::Values(Case>("vec2f"), + Case>("vec3f"), + Case>("vec4f"), + Case>("vec2h"), + Case>("vec3h"), + Case>("vec4h"), + Case>("vec2i"), + Case>("vec3i"), + Case>("vec4i"), + Case>("vec2u"), + Case>("vec3u"), + Case>("vec4u"))); + +} // namespace BuiltinTypeAliasTests + } // namespace } // namespace tint::resolver diff --git a/src/tint/resolver/uniformity.h b/src/tint/resolver/uniformity.h index 798080119c..1139980eb0 100644 --- a/src/tint/resolver/uniformity.h +++ b/src/tint/resolver/uniformity.h @@ -16,10 +16,10 @@ #define SRC_TINT_RESOLVER_UNIFORMITY_H_ // Forward declarations. -namespace tint { -namespace resolver { +namespace tint::resolver { struct DependencyGraph; -} // namespace resolver +} // namespace tint::resolver +namespace tint { class ProgramBuilder; } // namespace tint diff --git a/test/tint/shadowing/alias/builtin/const.wgsl b/test/tint/shadowing/alias/builtin/const.wgsl new file mode 100644 index 0000000000..e1fb581c29 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/const.wgsl @@ -0,0 +1,10 @@ +type a = vec3f; + +fn f() { + { + const vec3f = 1; + const b = vec3f; + } + const c = a(); + const d = vec3f(); +} diff --git a/test/tint/shadowing/alias/const.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/builtin/const.wgsl.expected.dxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/const.wgsl.expected.dxc.hlsl rename to test/tint/shadowing/alias/builtin/const.wgsl.expected.dxc.hlsl diff --git a/test/tint/shadowing/alias/const.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/builtin/const.wgsl.expected.fxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/const.wgsl.expected.fxc.hlsl rename to test/tint/shadowing/alias/builtin/const.wgsl.expected.fxc.hlsl diff --git a/test/tint/shadowing/alias/const.wgsl.expected.glsl b/test/tint/shadowing/alias/builtin/const.wgsl.expected.glsl similarity index 100% rename from test/tint/shadowing/alias/const.wgsl.expected.glsl rename to test/tint/shadowing/alias/builtin/const.wgsl.expected.glsl diff --git a/test/tint/shadowing/alias/const.wgsl.expected.msl b/test/tint/shadowing/alias/builtin/const.wgsl.expected.msl similarity index 100% rename from test/tint/shadowing/alias/const.wgsl.expected.msl rename to test/tint/shadowing/alias/builtin/const.wgsl.expected.msl diff --git a/test/tint/shadowing/alias/const.wgsl.expected.spvasm b/test/tint/shadowing/alias/builtin/const.wgsl.expected.spvasm similarity index 100% rename from test/tint/shadowing/alias/const.wgsl.expected.spvasm rename to test/tint/shadowing/alias/builtin/const.wgsl.expected.spvasm diff --git a/test/tint/shadowing/alias/builtin/const.wgsl.expected.wgsl b/test/tint/shadowing/alias/builtin/const.wgsl.expected.wgsl new file mode 100644 index 0000000000..e1fb581c29 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/const.wgsl.expected.wgsl @@ -0,0 +1,10 @@ +type a = vec3f; + +fn f() { + { + const vec3f = 1; + const b = vec3f; + } + const c = a(); + const d = vec3f(); +} diff --git a/test/tint/shadowing/alias/builtin/let.wgsl b/test/tint/shadowing/alias/builtin/let.wgsl new file mode 100644 index 0000000000..9dc6ab93ba --- /dev/null +++ b/test/tint/shadowing/alias/builtin/let.wgsl @@ -0,0 +1,10 @@ +type a = vec3f; + +fn f() { + { + let vec3f = 1; + let b = vec3f; + } + let c = a(); + let d = vec3f(); +} diff --git a/test/tint/shadowing/alias/builtin/let.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/builtin/let.wgsl.expected.dxc.hlsl new file mode 100644 index 0000000000..0e6094b14c --- /dev/null +++ b/test/tint/shadowing/alias/builtin/let.wgsl.expected.dxc.hlsl @@ -0,0 +1,13 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f() { + { + const int vec3f = 1; + const int b = vec3f; + } + const float3 c = (0.0f).xxx; + const float3 d = (0.0f).xxx; +} diff --git a/test/tint/shadowing/alias/builtin/let.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/builtin/let.wgsl.expected.fxc.hlsl new file mode 100644 index 0000000000..0e6094b14c --- /dev/null +++ b/test/tint/shadowing/alias/builtin/let.wgsl.expected.fxc.hlsl @@ -0,0 +1,13 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f() { + { + const int vec3f = 1; + const int b = vec3f; + } + const float3 c = (0.0f).xxx; + const float3 d = (0.0f).xxx; +} diff --git a/test/tint/shadowing/alias/builtin/let.wgsl.expected.glsl b/test/tint/shadowing/alias/builtin/let.wgsl.expected.glsl new file mode 100644 index 0000000000..fc24838e39 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/let.wgsl.expected.glsl @@ -0,0 +1,15 @@ +#version 310 es + +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; +void unused_entry_point() { + return; +} +void f() { + { + int vec3f = 1; + int b = vec3f; + } + vec3 c = vec3(0.0f); + vec3 d = vec3(0.0f); +} + diff --git a/test/tint/shadowing/alias/builtin/let.wgsl.expected.msl b/test/tint/shadowing/alias/builtin/let.wgsl.expected.msl new file mode 100644 index 0000000000..34d9028470 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/let.wgsl.expected.msl @@ -0,0 +1,12 @@ +#include + +using namespace metal; +void f() { + { + int const vec3f = 1; + int const b = vec3f; + } + float3 const c = float3(0.0f); + float3 const d = float3(0.0f); +} + diff --git a/test/tint/shadowing/alias/builtin/let.wgsl.expected.spvasm b/test/tint/shadowing/alias/builtin/let.wgsl.expected.spvasm new file mode 100644 index 0000000000..af04d0b5c1 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/let.wgsl.expected.spvasm @@ -0,0 +1,26 @@ +; SPIR-V +; Version: 1.3 +; Generator: Google Tint Compiler; 0 +; Bound: 12 +; Schema: 0 + OpCapability Shader + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %unused_entry_point "unused_entry_point" + OpExecutionMode %unused_entry_point LocalSize 1 1 1 + OpName %unused_entry_point "unused_entry_point" + OpName %f "f" + %void = OpTypeVoid + %1 = OpTypeFunction %void + %int = OpTypeInt 32 1 + %int_1 = OpConstant %int 1 + %float = OpTypeFloat 32 + %v3float = OpTypeVector %float 3 + %11 = OpConstantNull %v3float +%unused_entry_point = OpFunction %void None %1 + %4 = OpLabel + OpReturn + OpFunctionEnd + %f = OpFunction %void None %1 + %6 = OpLabel + OpReturn + OpFunctionEnd diff --git a/test/tint/shadowing/alias/builtin/let.wgsl.expected.wgsl b/test/tint/shadowing/alias/builtin/let.wgsl.expected.wgsl new file mode 100644 index 0000000000..9dc6ab93ba --- /dev/null +++ b/test/tint/shadowing/alias/builtin/let.wgsl.expected.wgsl @@ -0,0 +1,10 @@ +type a = vec3f; + +fn f() { + { + let vec3f = 1; + let b = vec3f; + } + let c = a(); + let d = vec3f(); +} diff --git a/test/tint/shadowing/alias/builtin/param.wgsl b/test/tint/shadowing/alias/builtin/param.wgsl new file mode 100644 index 0000000000..fd91034443 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/param.wgsl @@ -0,0 +1,3 @@ +fn f(vec3f : vec3f) { + let b = vec3f; +} diff --git a/test/tint/shadowing/alias/builtin/param.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/builtin/param.wgsl.expected.dxc.hlsl new file mode 100644 index 0000000000..9cd33b74d8 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/param.wgsl.expected.dxc.hlsl @@ -0,0 +1,8 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f(float3 vec3f) { + const float3 b = vec3f; +} diff --git a/test/tint/shadowing/alias/builtin/param.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/builtin/param.wgsl.expected.fxc.hlsl new file mode 100644 index 0000000000..9cd33b74d8 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/param.wgsl.expected.fxc.hlsl @@ -0,0 +1,8 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f(float3 vec3f) { + const float3 b = vec3f; +} diff --git a/test/tint/shadowing/alias/builtin/param.wgsl.expected.glsl b/test/tint/shadowing/alias/builtin/param.wgsl.expected.glsl new file mode 100644 index 0000000000..1d19750260 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/param.wgsl.expected.glsl @@ -0,0 +1,10 @@ +#version 310 es + +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; +void unused_entry_point() { + return; +} +void f(vec3 vec3f) { + vec3 b = vec3f; +} + diff --git a/test/tint/shadowing/alias/builtin/param.wgsl.expected.msl b/test/tint/shadowing/alias/builtin/param.wgsl.expected.msl new file mode 100644 index 0000000000..252bf49872 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/param.wgsl.expected.msl @@ -0,0 +1,7 @@ +#include + +using namespace metal; +void f(float3 vec3f) { + float3 const b = vec3f; +} + diff --git a/test/tint/shadowing/alias/builtin/param.wgsl.expected.spvasm b/test/tint/shadowing/alias/builtin/param.wgsl.expected.spvasm new file mode 100644 index 0000000000..bb64ce19d0 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/param.wgsl.expected.spvasm @@ -0,0 +1,26 @@ +; SPIR-V +; Version: 1.3 +; Generator: Google Tint Compiler; 0 +; Bound: 11 +; Schema: 0 + OpCapability Shader + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %unused_entry_point "unused_entry_point" + OpExecutionMode %unused_entry_point LocalSize 1 1 1 + OpName %unused_entry_point "unused_entry_point" + OpName %f "f" + OpName %vec3f "vec3f" + %void = OpTypeVoid + %1 = OpTypeFunction %void + %float = OpTypeFloat 32 + %v3float = OpTypeVector %float 3 + %5 = OpTypeFunction %void %v3float +%unused_entry_point = OpFunction %void None %1 + %4 = OpLabel + OpReturn + OpFunctionEnd + %f = OpFunction %void None %5 + %vec3f = OpFunctionParameter %v3float + %10 = OpLabel + OpReturn + OpFunctionEnd diff --git a/test/tint/shadowing/alias/builtin/param.wgsl.expected.wgsl b/test/tint/shadowing/alias/builtin/param.wgsl.expected.wgsl new file mode 100644 index 0000000000..fd91034443 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/param.wgsl.expected.wgsl @@ -0,0 +1,3 @@ +fn f(vec3f : vec3f) { + let b = vec3f; +} diff --git a/test/tint/shadowing/alias/builtin/var.wgsl b/test/tint/shadowing/alias/builtin/var.wgsl new file mode 100644 index 0000000000..e33e30b9ac --- /dev/null +++ b/test/tint/shadowing/alias/builtin/var.wgsl @@ -0,0 +1,10 @@ +type a = vec3f; + +fn f() { + { + var vec3f = 1; + var b = vec3f; + } + var c = a(); + var d = vec3f(); +} diff --git a/test/tint/shadowing/alias/builtin/var.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/builtin/var.wgsl.expected.dxc.hlsl new file mode 100644 index 0000000000..8a019baef6 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/var.wgsl.expected.dxc.hlsl @@ -0,0 +1,13 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f() { + { + int vec3f = 1; + int b = vec3f; + } + float3 c = (0.0f).xxx; + float3 d = (0.0f).xxx; +} diff --git a/test/tint/shadowing/alias/builtin/var.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/builtin/var.wgsl.expected.fxc.hlsl new file mode 100644 index 0000000000..8a019baef6 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/var.wgsl.expected.fxc.hlsl @@ -0,0 +1,13 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f() { + { + int vec3f = 1; + int b = vec3f; + } + float3 c = (0.0f).xxx; + float3 d = (0.0f).xxx; +} diff --git a/test/tint/shadowing/alias/builtin/var.wgsl.expected.glsl b/test/tint/shadowing/alias/builtin/var.wgsl.expected.glsl new file mode 100644 index 0000000000..fc24838e39 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/var.wgsl.expected.glsl @@ -0,0 +1,15 @@ +#version 310 es + +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; +void unused_entry_point() { + return; +} +void f() { + { + int vec3f = 1; + int b = vec3f; + } + vec3 c = vec3(0.0f); + vec3 d = vec3(0.0f); +} + diff --git a/test/tint/shadowing/alias/builtin/var.wgsl.expected.msl b/test/tint/shadowing/alias/builtin/var.wgsl.expected.msl new file mode 100644 index 0000000000..1446f1823f --- /dev/null +++ b/test/tint/shadowing/alias/builtin/var.wgsl.expected.msl @@ -0,0 +1,12 @@ +#include + +using namespace metal; +void f() { + { + int vec3f = 1; + int b = vec3f; + } + float3 c = float3(0.0f); + float3 d = float3(0.0f); +} + diff --git a/test/tint/shadowing/alias/builtin/var.wgsl.expected.spvasm b/test/tint/shadowing/alias/builtin/var.wgsl.expected.spvasm new file mode 100644 index 0000000000..52051a9b11 --- /dev/null +++ b/test/tint/shadowing/alias/builtin/var.wgsl.expected.spvasm @@ -0,0 +1,42 @@ +; SPIR-V +; Version: 1.3 +; Generator: Google Tint Compiler; 0 +; Bound: 20 +; Schema: 0 + OpCapability Shader + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %unused_entry_point "unused_entry_point" + OpExecutionMode %unused_entry_point LocalSize 1 1 1 + OpName %unused_entry_point "unused_entry_point" + OpName %f "f" + OpName %vec3f "vec3f" + OpName %b "b" + OpName %c "c" + OpName %d "d" + %void = OpTypeVoid + %1 = OpTypeFunction %void + %int = OpTypeInt 32 1 + %int_1 = OpConstant %int 1 +%_ptr_Function_int = OpTypePointer Function %int + %11 = OpConstantNull %int + %float = OpTypeFloat 32 + %v3float = OpTypeVector %float 3 + %16 = OpConstantNull %v3float +%_ptr_Function_v3float = OpTypePointer Function %v3float +%unused_entry_point = OpFunction %void None %1 + %4 = OpLabel + OpReturn + OpFunctionEnd + %f = OpFunction %void None %1 + %6 = OpLabel + %vec3f = OpVariable %_ptr_Function_int Function %11 + %b = OpVariable %_ptr_Function_int Function %11 + %c = OpVariable %_ptr_Function_v3float Function %16 + %d = OpVariable %_ptr_Function_v3float Function %16 + OpStore %vec3f %int_1 + %12 = OpLoad %int %vec3f + OpStore %b %12 + OpStore %c %16 + OpStore %d %16 + OpReturn + OpFunctionEnd diff --git a/test/tint/shadowing/alias/builtin/var.wgsl.expected.wgsl b/test/tint/shadowing/alias/builtin/var.wgsl.expected.wgsl new file mode 100644 index 0000000000..e33e30b9ac --- /dev/null +++ b/test/tint/shadowing/alias/builtin/var.wgsl.expected.wgsl @@ -0,0 +1,10 @@ +type a = vec3f; + +fn f() { + { + var vec3f = 1; + var b = vec3f; + } + var c = a(); + var d = vec3f(); +} diff --git a/test/tint/shadowing/alias/const.wgsl b/test/tint/shadowing/alias/user/const.wgsl similarity index 100% rename from test/tint/shadowing/alias/const.wgsl rename to test/tint/shadowing/alias/user/const.wgsl diff --git a/test/tint/shadowing/alias/user/const.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/user/const.wgsl.expected.dxc.hlsl new file mode 100644 index 0000000000..6316714a17 --- /dev/null +++ b/test/tint/shadowing/alias/user/const.wgsl.expected.dxc.hlsl @@ -0,0 +1,9 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f() { + { + } +} diff --git a/test/tint/shadowing/alias/user/const.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/user/const.wgsl.expected.fxc.hlsl new file mode 100644 index 0000000000..6316714a17 --- /dev/null +++ b/test/tint/shadowing/alias/user/const.wgsl.expected.fxc.hlsl @@ -0,0 +1,9 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void f() { + { + } +} diff --git a/test/tint/shadowing/alias/user/const.wgsl.expected.glsl b/test/tint/shadowing/alias/user/const.wgsl.expected.glsl new file mode 100644 index 0000000000..0da98e38fe --- /dev/null +++ b/test/tint/shadowing/alias/user/const.wgsl.expected.glsl @@ -0,0 +1,11 @@ +#version 310 es + +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; +void unused_entry_point() { + return; +} +void f() { + { + } +} + diff --git a/test/tint/shadowing/alias/user/const.wgsl.expected.msl b/test/tint/shadowing/alias/user/const.wgsl.expected.msl new file mode 100644 index 0000000000..ee183f052d --- /dev/null +++ b/test/tint/shadowing/alias/user/const.wgsl.expected.msl @@ -0,0 +1,8 @@ +#include + +using namespace metal; +void f() { + { + } +} + diff --git a/test/tint/shadowing/alias/user/const.wgsl.expected.spvasm b/test/tint/shadowing/alias/user/const.wgsl.expected.spvasm new file mode 100644 index 0000000000..1c4d684e74 --- /dev/null +++ b/test/tint/shadowing/alias/user/const.wgsl.expected.spvasm @@ -0,0 +1,21 @@ +; SPIR-V +; Version: 1.3 +; Generator: Google Tint Compiler; 0 +; Bound: 7 +; Schema: 0 + OpCapability Shader + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %unused_entry_point "unused_entry_point" + OpExecutionMode %unused_entry_point LocalSize 1 1 1 + OpName %unused_entry_point "unused_entry_point" + OpName %f "f" + %void = OpTypeVoid + %1 = OpTypeFunction %void +%unused_entry_point = OpFunction %void None %1 + %4 = OpLabel + OpReturn + OpFunctionEnd + %f = OpFunction %void None %1 + %6 = OpLabel + OpReturn + OpFunctionEnd diff --git a/test/tint/shadowing/alias/const.wgsl.expected.wgsl b/test/tint/shadowing/alias/user/const.wgsl.expected.wgsl similarity index 100% rename from test/tint/shadowing/alias/const.wgsl.expected.wgsl rename to test/tint/shadowing/alias/user/const.wgsl.expected.wgsl diff --git a/test/tint/shadowing/alias/let.wgsl b/test/tint/shadowing/alias/user/let.wgsl similarity index 100% rename from test/tint/shadowing/alias/let.wgsl rename to test/tint/shadowing/alias/user/let.wgsl diff --git a/test/tint/shadowing/alias/let.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/user/let.wgsl.expected.dxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/let.wgsl.expected.dxc.hlsl rename to test/tint/shadowing/alias/user/let.wgsl.expected.dxc.hlsl diff --git a/test/tint/shadowing/alias/let.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/user/let.wgsl.expected.fxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/let.wgsl.expected.fxc.hlsl rename to test/tint/shadowing/alias/user/let.wgsl.expected.fxc.hlsl diff --git a/test/tint/shadowing/alias/let.wgsl.expected.glsl b/test/tint/shadowing/alias/user/let.wgsl.expected.glsl similarity index 100% rename from test/tint/shadowing/alias/let.wgsl.expected.glsl rename to test/tint/shadowing/alias/user/let.wgsl.expected.glsl diff --git a/test/tint/shadowing/alias/let.wgsl.expected.msl b/test/tint/shadowing/alias/user/let.wgsl.expected.msl similarity index 100% rename from test/tint/shadowing/alias/let.wgsl.expected.msl rename to test/tint/shadowing/alias/user/let.wgsl.expected.msl diff --git a/test/tint/shadowing/alias/let.wgsl.expected.spvasm b/test/tint/shadowing/alias/user/let.wgsl.expected.spvasm similarity index 100% rename from test/tint/shadowing/alias/let.wgsl.expected.spvasm rename to test/tint/shadowing/alias/user/let.wgsl.expected.spvasm diff --git a/test/tint/shadowing/alias/let.wgsl.expected.wgsl b/test/tint/shadowing/alias/user/let.wgsl.expected.wgsl similarity index 100% rename from test/tint/shadowing/alias/let.wgsl.expected.wgsl rename to test/tint/shadowing/alias/user/let.wgsl.expected.wgsl diff --git a/test/tint/shadowing/alias/param.wgsl b/test/tint/shadowing/alias/user/param.wgsl similarity index 100% rename from test/tint/shadowing/alias/param.wgsl rename to test/tint/shadowing/alias/user/param.wgsl diff --git a/test/tint/shadowing/alias/param.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/user/param.wgsl.expected.dxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/param.wgsl.expected.dxc.hlsl rename to test/tint/shadowing/alias/user/param.wgsl.expected.dxc.hlsl diff --git a/test/tint/shadowing/alias/param.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/user/param.wgsl.expected.fxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/param.wgsl.expected.fxc.hlsl rename to test/tint/shadowing/alias/user/param.wgsl.expected.fxc.hlsl diff --git a/test/tint/shadowing/alias/param.wgsl.expected.glsl b/test/tint/shadowing/alias/user/param.wgsl.expected.glsl similarity index 100% rename from test/tint/shadowing/alias/param.wgsl.expected.glsl rename to test/tint/shadowing/alias/user/param.wgsl.expected.glsl diff --git a/test/tint/shadowing/alias/param.wgsl.expected.msl b/test/tint/shadowing/alias/user/param.wgsl.expected.msl similarity index 100% rename from test/tint/shadowing/alias/param.wgsl.expected.msl rename to test/tint/shadowing/alias/user/param.wgsl.expected.msl diff --git a/test/tint/shadowing/alias/param.wgsl.expected.spvasm b/test/tint/shadowing/alias/user/param.wgsl.expected.spvasm similarity index 100% rename from test/tint/shadowing/alias/param.wgsl.expected.spvasm rename to test/tint/shadowing/alias/user/param.wgsl.expected.spvasm diff --git a/test/tint/shadowing/alias/param.wgsl.expected.wgsl b/test/tint/shadowing/alias/user/param.wgsl.expected.wgsl similarity index 100% rename from test/tint/shadowing/alias/param.wgsl.expected.wgsl rename to test/tint/shadowing/alias/user/param.wgsl.expected.wgsl diff --git a/test/tint/shadowing/alias/var.wgsl b/test/tint/shadowing/alias/user/var.wgsl similarity index 100% rename from test/tint/shadowing/alias/var.wgsl rename to test/tint/shadowing/alias/user/var.wgsl diff --git a/test/tint/shadowing/alias/var.wgsl.expected.dxc.hlsl b/test/tint/shadowing/alias/user/var.wgsl.expected.dxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/var.wgsl.expected.dxc.hlsl rename to test/tint/shadowing/alias/user/var.wgsl.expected.dxc.hlsl diff --git a/test/tint/shadowing/alias/var.wgsl.expected.fxc.hlsl b/test/tint/shadowing/alias/user/var.wgsl.expected.fxc.hlsl similarity index 100% rename from test/tint/shadowing/alias/var.wgsl.expected.fxc.hlsl rename to test/tint/shadowing/alias/user/var.wgsl.expected.fxc.hlsl diff --git a/test/tint/shadowing/alias/var.wgsl.expected.glsl b/test/tint/shadowing/alias/user/var.wgsl.expected.glsl similarity index 100% rename from test/tint/shadowing/alias/var.wgsl.expected.glsl rename to test/tint/shadowing/alias/user/var.wgsl.expected.glsl diff --git a/test/tint/shadowing/alias/var.wgsl.expected.msl b/test/tint/shadowing/alias/user/var.wgsl.expected.msl similarity index 100% rename from test/tint/shadowing/alias/var.wgsl.expected.msl rename to test/tint/shadowing/alias/user/var.wgsl.expected.msl diff --git a/test/tint/shadowing/alias/var.wgsl.expected.spvasm b/test/tint/shadowing/alias/user/var.wgsl.expected.spvasm similarity index 100% rename from test/tint/shadowing/alias/var.wgsl.expected.spvasm rename to test/tint/shadowing/alias/user/var.wgsl.expected.spvasm diff --git a/test/tint/shadowing/alias/var.wgsl.expected.wgsl b/test/tint/shadowing/alias/user/var.wgsl.expected.wgsl similarity index 100% rename from test/tint/shadowing/alias/var.wgsl.expected.wgsl rename to test/tint/shadowing/alias/user/var.wgsl.expected.wgsl diff --git a/tools/src/tint/intrinsic/lexer/lexer.go b/tools/src/tint/intrinsic/lexer/lexer.go index dcec9fec19..51045d66d3 100644 --- a/tools/src/tint/intrinsic/lexer/lexer.go +++ b/tools/src/tint/intrinsic/lexer/lexer.go @@ -93,15 +93,26 @@ func (l *lexer) lex() error { case l.match("/", tok.Divide): case l.match(".", tok.Dot): case l.match("->", tok.Arrow): - case l.match("fn", tok.Function): - case l.match("op", tok.Operator): - case l.match("enum", tok.Enum): - case l.match("type", tok.Type): - case l.match("init", tok.Initializer): - case l.match("conv", tok.Converter): - case l.match("match", tok.Match): case unicode.IsLetter(l.peek(0)) || l.peek(0) == '_': - l.tok(l.count(alphaNumericOrUnderscore), tok.Identifier) + n := l.count(alphaNumericOrUnderscore) + switch string(l.runes[:n]) { + case "fn": + l.tok(n, tok.Function) + case "op": + l.tok(n, tok.Operator) + case "enum": + l.tok(n, tok.Enum) + case "type": + l.tok(n, tok.Type) + case "init": + l.tok(n, tok.Initializer) + case "conv": + l.tok(n, tok.Converter) + case "match": + l.tok(n, tok.Match) + default: + l.tok(n, tok.Identifier) + } case unicode.IsNumber(l.peek(0)) || l.peek(0) == '-': isFloat := false isNegative := false diff --git a/tools/src/tint/intrinsic/lexer/lexer_test.go b/tools/src/tint/intrinsic/lexer/lexer_test.go index e74fcd40a1..9eda8b6275 100644 --- a/tools/src/tint/intrinsic/lexer/lexer_test.go +++ b/tools/src/tint/intrinsic/lexer/lexer_test.go @@ -84,6 +84,9 @@ func TestLexTokens(t *testing.T) { {"op", []tok.Token{{Kind: tok.Operator, Runes: []rune("op"), Source: tok.Source{ S: loc(1, 1, 0), E: loc(1, 3, 2), }}}}, + {"operation", []tok.Token{{Kind: tok.Identifier, Runes: []rune("operation"), Source: tok.Source{ + S: loc(1, 1, 0), E: loc(1, 10, 9), + }}}}, {"type", []tok.Token{{Kind: tok.Type, Runes: []rune("type"), Source: tok.Source{ S: loc(1, 1, 0), E: loc(1, 5, 4), }}}},