1*c217d954SCole Faust /* 2*c217d954SCole Faust * Copyright (c) 2021 Arm Limited. 3*c217d954SCole Faust * 4*c217d954SCole Faust * SPDX-License-Identifier: MIT 5*c217d954SCole Faust * 6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy 7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to 8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the 9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is 11*c217d954SCole Faust * furnished to do so, subject to the following conditions: 12*c217d954SCole Faust * 13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all 14*c217d954SCole Faust * copies or substantial portions of the Software. 15*c217d954SCole Faust * 16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*c217d954SCole Faust * SOFTWARE. 23*c217d954SCole Faust */ 24*c217d954SCole Faust #ifndef SRC_RUNTIME_CL_MLGO_MLGO_PARSER_H 25*c217d954SCole Faust #define SRC_RUNTIME_CL_MLGO_MLGO_PARSER_H 26*c217d954SCole Faust 27*c217d954SCole Faust #include "src/runtime/CL/mlgo/MLGOHeuristics.h" 28*c217d954SCole Faust 29*c217d954SCole Faust #include <deque> 30*c217d954SCole Faust #include <istream> 31*c217d954SCole Faust #include <string> 32*c217d954SCole Faust #include <utility> 33*c217d954SCole Faust 34*c217d954SCole Faust /** A DotMLGO file parser (LL(k) parser) 35*c217d954SCole Faust * 36*c217d954SCole Faust * The grammar of DotMLGO is defined as the following ENBF: 37*c217d954SCole Faust * 38*c217d954SCole Faust * delim = "," | "\n"; // Note that delimiters are omitted from the definition below 39*c217d954SCole Faust * 40*c217d954SCole Faust * mlgo = header, heuristics-table, {heuristic-tree}; 41*c217d954SCole Faust * 42*c217d954SCole Faust * header = "<header>", gemm-version, ip-type, "</header>"; 43*c217d954SCole Faust * gemm-version = "gemm-version", "[", int, int, int, "]"; 44*c217d954SCole Faust * ip-type = "ip-type", ("gpu" | "cpu"); 45*c217d954SCole Faust * 46*c217d954SCole Faust * heiristics-table = "<heuristics-table>", {heuristics-table-entry}, "</heuristics-table>"; 47*c217d954SCole Faust * heuristics-table-entry = entry-id, ip-name, num-cores, data-type, gpu-priority, gpu-behavior, heuristic-type, free-vars; 48*c217d954SCole Faust * entry-id = int; 49*c217d954SCole Faust * ip-name = char-sequence; 50*c217d954SCole Faust * num-cores = int; 51*c217d954SCole Faust * data-type = "f32" | "f16" | "qasymm8"; 52*c217d954SCole Faust * gpu-priority = "best-performance" | "best-memory-usage"; 53*c217d954SCole Faust * gpu-behavior = "static" | "dynamic"; 54*c217d954SCole Faust * heuristic-type = "gemm-type" | "gemm-config-native" | "gemm-config-reshaped-only-rhs" | "gemm-config-reshaped"; 55*c217d954SCole Faust * free-vars = "[", {char-sequence}, "]"; 56*c217d954SCole Faust * 57*c217d954SCole Faust * heuristic-tree = "<heuristic", entry-id, ">", {tree-node}, "</heuristic>"; 58*c217d954SCole Faust * tree-node = branch-node | leaf-node; 59*c217d954SCole Faust * branch-node = "b", entry-id, lhs-type, lhs-value, conditional-op, rhs-type, rhs-value, true-node, false-node; 60*c217d954SCole Faust * lhs-type = comparator-type; 61*c217d954SCole Faust * lhs-value = comparator-value; 62*c217d954SCole Faust * rhs-type = comparator-type; 63*c217d954SCole Faust * rhs-value = comparator-value; 64*c217d954SCole Faust * comparator-type = "var" | "num" | "enum"; 65*c217d954SCole Faust * comparator-value = char-sequence | float; 66*c217d954SCole Faust * conditional-op = "<" | "<=" | "==" | ">=" | ">"; 67*c217d954SCole Faust * true-node = entry-id; 68*c217d954SCole Faust * false-node = entry-id; 69*c217d954SCole Faust * leaf-node = "l", entry-id, heuristic-type, leaf-value; 70*c217d954SCole Faust * leaf-value = gemm-type | gemm-config-native | gemm-config-reshaped-only-rhs | gemm-config-reshaped 71*c217d954SCole Faust * gemm-type = "native" | "reshaped-only-rhs" | "reshaped"; 72*c217d954SCole Faust * gemm-config-native = "[", int, int, int, "]"; 73*c217d954SCole Faust * gemm-config-reshaped-only-rhs = "[", int, int, int, int, bool, bool, bool, "]"; 74*c217d954SCole Faust * gemm-config-reshaped = "[", int, int, int, int, int, bool, bool, bool, bool, "]"; 75*c217d954SCole Faust */ 76*c217d954SCole Faust 77*c217d954SCole Faust namespace arm_compute 78*c217d954SCole Faust { 79*c217d954SCole Faust namespace mlgo 80*c217d954SCole Faust { 81*c217d954SCole Faust namespace parser 82*c217d954SCole Faust { 83*c217d954SCole Faust /** Type of Token */ 84*c217d954SCole Faust enum class TokenType 85*c217d954SCole Faust { 86*c217d954SCole Faust L_List = '[', /**< List open */ 87*c217d954SCole Faust R_List = ']', /**< List close */ 88*c217d954SCole Faust Int, /**< Integral */ 89*c217d954SCole Faust Float, /**< Floating */ 90*c217d954SCole Faust Text, /**< Text/String */ 91*c217d954SCole Faust End, /**< End of stream */ 92*c217d954SCole Faust }; 93*c217d954SCole Faust 94*c217d954SCole Faust struct CharPosition 95*c217d954SCole Faust { 96*c217d954SCole Faust bool operator==(const CharPosition &other) const 97*c217d954SCole Faust { 98*c217d954SCole Faust return ln == other.ln && col == other.col; 99*c217d954SCole Faust } 100*c217d954SCole Faust 101*c217d954SCole Faust size_t ln{ 0 }; 102*c217d954SCole Faust size_t col{ 0 }; 103*c217d954SCole Faust }; 104*c217d954SCole Faust 105*c217d954SCole Faust /** Token */ 106*c217d954SCole Faust struct Token 107*c217d954SCole Faust { TokenToken108*c217d954SCole Faust Token(TokenType t, std::string v, CharPosition pos) 109*c217d954SCole Faust : type{ t }, value{ v }, pos{ pos } 110*c217d954SCole Faust { 111*c217d954SCole Faust } 112*c217d954SCole Faust 113*c217d954SCole Faust bool operator==(const Token &other) const 114*c217d954SCole Faust { 115*c217d954SCole Faust return type == other.type && value == other.value && pos == other.pos; 116*c217d954SCole Faust } 117*c217d954SCole Faust 118*c217d954SCole Faust TokenType type; /**< Token type */ 119*c217d954SCole Faust std::string value; /**< Token value */ 120*c217d954SCole Faust CharPosition pos; 121*c217d954SCole Faust }; 122*c217d954SCole Faust 123*c217d954SCole Faust /** A stream of token */ 124*c217d954SCole Faust class TokenStream 125*c217d954SCole Faust { 126*c217d954SCole Faust // NOTE: _tokens is never empty. The end of token stream is signalled by the End Token 127*c217d954SCole Faust public: 128*c217d954SCole Faust static constexpr size_t max_look_ahead = 10; 129*c217d954SCole Faust 130*c217d954SCole Faust public: 131*c217d954SCole Faust /** Constructor 132*c217d954SCole Faust * 133*c217d954SCole Faust * @param[in] s Input stream 134*c217d954SCole Faust * @param[in] delims Delimiter characters packed in a string. Each char from the string can be used as a delim on its own 135*c217d954SCole Faust */ 136*c217d954SCole Faust TokenStream(std::istream &s, const std::string &delims = ",\n"); 137*c217d954SCole Faust 138*c217d954SCole Faust /** Check if there're more (non-End) Tokens 139*c217d954SCole Faust * @return true If there are more tokens 140*c217d954SCole Faust * @return false If reached end of stream (only End token) 141*c217d954SCole Faust */ 142*c217d954SCole Faust explicit operator bool() const; 143*c217d954SCole Faust 144*c217d954SCole Faust /** Get and pop off the current token 145*c217d954SCole Faust * 146*c217d954SCole Faust * @return Token 147*c217d954SCole Faust */ 148*c217d954SCole Faust Token take(); 149*c217d954SCole Faust 150*c217d954SCole Faust /** Peek the next ith token 151*c217d954SCole Faust * 152*c217d954SCole Faust * @param[in] i The next ith token. i < @ref max_look_ahead. 153*c217d954SCole Faust * 154*c217d954SCole Faust * @return Token 155*c217d954SCole Faust */ 156*c217d954SCole Faust Token peek(size_t i = 0); 157*c217d954SCole Faust 158*c217d954SCole Faust /** Get the position of the current token 159*c217d954SCole Faust * 160*c217d954SCole Faust * @return CharPosition 161*c217d954SCole Faust */ current_pos()162*c217d954SCole Faust CharPosition current_pos() const 163*c217d954SCole Faust { 164*c217d954SCole Faust return _tokens.front().pos; 165*c217d954SCole Faust } 166*c217d954SCole Faust 167*c217d954SCole Faust private: 168*c217d954SCole Faust void read(); 169*c217d954SCole Faust 170*c217d954SCole Faust Token recognize_tok(char ch); 171*c217d954SCole Faust 172*c217d954SCole Faust Token num_st(std::string value = ""); 173*c217d954SCole Faust 174*c217d954SCole Faust Token float_after_dp_st(std::string value = ""); 175*c217d954SCole Faust 176*c217d954SCole Faust Token text_st(std::string value = ""); 177*c217d954SCole Faust 178*c217d954SCole Faust bool reached_end() const; 179*c217d954SCole Faust 180*c217d954SCole Faust bool is_delim(char ch) const; 181*c217d954SCole Faust 182*c217d954SCole Faust std::string _delims; 183*c217d954SCole Faust std::istream &_istream; 184*c217d954SCole Faust std::deque<Token> _tokens; 185*c217d954SCole Faust CharPosition _lookahead_pos; 186*c217d954SCole Faust }; 187*c217d954SCole Faust 188*c217d954SCole Faust /** Parse and construct a @ref MLGOHeuristics from input stream 189*c217d954SCole Faust * 190*c217d954SCole Faust * @param[in] in Input stream 191*c217d954SCole Faust * 192*c217d954SCole Faust * @return MLGOHeuristics 193*c217d954SCole Faust */ 194*c217d954SCole Faust std::pair<bool, MLGOHeuristics> parse_mlgo(std::istream &in); 195*c217d954SCole Faust 196*c217d954SCole Faust } // namespace parser 197*c217d954SCole Faust } // namespace mlgo 198*c217d954SCole Faust } // namespace arm_compute 199*c217d954SCole Faust #endif //SRC_RUNTIME_CL_MLGO_MLGO_PARSER_H