From 45ff7a8c4ae718a9ffb980c174626f2683522854 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Thu, 20 Apr 2023 16:50:29 +0000 Subject: [PATCH] Remove extra work from `is_reserved` Currently the `is_reserved` method in the WGSL parser uses the `operator==` on the token to check the reserved words. This ends up re-doing a bunch of work as it checks the token type, then gets the `string_view` or `string` from the variant, then compares. This CL adds a precondition that the token is an identifier (which is true in the only case we call this method) and then extracts the string from the token once and uses that for all the comparisons. Change-Id: If425afea38e727169de7991a84f6fa1e47f660ef Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/128140 Reviewed-by: Ben Clayton Commit-Queue: Dan Sinclair Kokoro: Kokoro --- src/tint/reader/wgsl/parser_impl.cc | 67 +++++++++++++++-------------- src/tint/reader/wgsl/token.cc | 12 ++++++ src/tint/reader/wgsl/token.h | 4 ++ 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc index 753f60b14c..2c4783a1c3 100644 --- a/src/tint/reader/wgsl/parser_impl.cc +++ b/src/tint/reader/wgsl/parser_impl.cc @@ -72,39 +72,42 @@ constexpr uint32_t kMaxParseDepth = 128; constexpr size_t const kMaxResynchronizeLookahead = 32; // https://gpuweb.github.io/gpuweb/wgsl.html#reserved-keywords +// +// Must be called with an identifier token. bool is_reserved(const Token& t) { - return t == "NULL" || t == "Self" || t == "abstract" || t == "active" || t == "alignas" || - t == "alignof" || t == "as" || t == "asm" || t == "asm_fragment" || t == "async" || - t == "attribute" || t == "auto" || t == "await" || t == "become" || - t == "binding_array" || t == "cast" || t == "catch" || t == "class" || t == "co_await" || - t == "co_return" || t == "co_yield" || t == "coherent" || t == "column_major" || - t == "common" || t == "compile" || t == "compile_fragment" || t == "concept" || - t == "const_cast" || t == "consteval" || t == "constexpr" || t == "constinit" || - t == "crate" || t == "debugger" || t == "decltype" || t == "delete" || t == "demote" || - t == "demote_to_helper" || t == "do" || t == "dynamic_cast" || t == "enum" || - t == "explicit" || t == "export" || t == "extends" || t == "extern" || t == "external" || - t == "filter" || t == "final" || t == "finally" || t == "friend" || t == "from" || - t == "fxgroup" || t == "get" || t == "goto" || t == "groupshared" || t == "highp" || - t == "impl" || t == "implements" || t == "import" || t == "inline" || - t == "instanceof" || t == "interface" || t == "layout" || t == "lowp" || t == "macro" || - t == "macro_rules" || t == "match" || t == "mediump" || t == "meta" || t == "mod" || - t == "module" || t == "move" || t == "mut" || t == "mutable" || t == "namespace" || - t == "new" || t == "nil" || t == "noexcept" || t == "noinline" || - t == "nointerpolation" || t == "noperspective" || t == "null" || t == "nullptr" || - t == "of" || t == "operator" || t == "package" || t == "packoffset" || - t == "partition" || t == "pass" || t == "patch" || t == "pixelfragment" || - t == "precise" || t == "precision" || t == "premerge" || t == "priv" || - t == "protected" || t == "pub" || t == "public" || t == "readonly" || t == "ref" || - t == "regardless" || t == "register" || t == "reinterpret_cast" || t == "require" || - t == "resource" || t == "restrict" || t == "self" || t == "set" || t == "shared" || - t == "sizeof" || t == "smooth" || t == "snorm" || t == "static" || - t == "static_assert" || t == "static_cast" || t == "std" || t == "subroutine" || - t == "super" || t == "target" || t == "template" || t == "this" || t == "thread_local" || - t == "throw" || t == "trait" || t == "try" || t == "type" || t == "typedef" || - t == "typeid" || t == "typename" || t == "typeof" || t == "union" || t == "unless" || - t == "unorm" || t == "unsafe" || t == "unsized" || t == "use" || t == "using" || - t == "varying" || t == "virtual" || t == "volatile" || t == "wgsl" || t == "where" || - t == "with" || t == "writeonly" || t == "yield"; + auto s = t.to_str_view(); + return s == "NULL" || s == "Self" || s == "abstract" || s == "active" || s == "alignas" || + s == "alignof" || s == "as" || s == "asm" || s == "asm_fragment" || s == "async" || + s == "attribute" || s == "auto" || s == "await" || s == "become" || + s == "binding_array" || s == "cast" || s == "catch" || s == "class" || s == "co_await" || + s == "co_return" || s == "co_yield" || s == "coherent" || s == "column_major" || + s == "common" || s == "compile" || s == "compile_fragment" || s == "concept" || + s == "const_cast" || s == "consteval" || s == "constexpr" || s == "constinit" || + s == "crate" || s == "debugger" || s == "decltype" || s == "delete" || s == "demote" || + s == "demote_to_helper" || s == "do" || s == "dynamic_cast" || s == "enum" || + s == "explicit" || s == "export" || s == "extends" || s == "extern" || s == "external" || + s == "filter" || s == "final" || s == "finally" || s == "friend" || s == "from" || + s == "fxgroup" || s == "get" || s == "goto" || s == "groupshared" || s == "highp" || + s == "impl" || s == "implements" || s == "import" || s == "inline" || + s == "instanceof" || s == "interface" || s == "layout" || s == "lowp" || s == "macro" || + s == "macro_rules" || s == "match" || s == "mediump" || s == "meta" || s == "mod" || + s == "module" || s == "move" || s == "mut" || s == "mutable" || s == "namespace" || + s == "new" || s == "nil" || s == "noexcept" || s == "noinline" || + s == "nointerpolation" || s == "noperspective" || s == "null" || s == "nullptr" || + s == "of" || s == "operator" || s == "package" || s == "packoffset" || + s == "partition" || s == "pass" || s == "patch" || s == "pixelfragment" || + s == "precise" || s == "precision" || s == "premerge" || s == "priv" || + s == "protected" || s == "pub" || s == "public" || s == "readonly" || s == "ref" || + s == "regardless" || s == "register" || s == "reinterpret_cast" || s == "require" || + s == "resource" || s == "restrict" || s == "self" || s == "set" || s == "shared" || + s == "sizeof" || s == "smooth" || s == "snorm" || s == "static" || + s == "static_assert" || s == "static_cast" || s == "std" || s == "subroutine" || + s == "super" || s == "target" || s == "template" || s == "this" || s == "thread_local" || + s == "throw" || s == "trait" || s == "try" || s == "type" || s == "typedef" || + s == "typeid" || s == "typename" || s == "typeof" || s == "union" || s == "unless" || + s == "unorm" || s == "unsafe" || s == "unsized" || s == "use" || s == "using" || + s == "varying" || s == "virtual" || s == "volatile" || s == "wgsl" || s == "where" || + s == "with" || s == "writeonly" || s == "yield"; } /// Enter-exit counters for block token types. diff --git a/src/tint/reader/wgsl/token.cc b/src/tint/reader/wgsl/token.cc index e133dfccda..04124e125a 100644 --- a/src/tint/reader/wgsl/token.cc +++ b/src/tint/reader/wgsl/token.cc @@ -256,6 +256,18 @@ std::string Token::to_str() const { } } +std::string_view Token::to_str_view() const { + if (type_ != Type::kIdentifier) { + return {}; + } + + if (auto* view = std::get_if(&value_)) { + return *view; + } + auto& s = std::get(value_); + return {s.data(), s.length()}; +} + double Token::to_f64() const { return std::get(value_); } diff --git a/src/tint/reader/wgsl/token.h b/src/tint/reader/wgsl/token.h index 19bdd40a16..f7a9e0f20b 100644 --- a/src/tint/reader/wgsl/token.h +++ b/src/tint/reader/wgsl/token.h @@ -334,6 +334,10 @@ class Token { /// Returns the string value of the token /// @return std::string std::string to_str() const; + /// Returns the string view of the token + /// @return std::string_view + /// @note if the token is not an identifier, an empty string_view will be returned. + std::string_view to_str_view() const; /// Returns the float value of the token. 0 is returned if the token does not /// contain a float value. /// @return double