1 /* 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CPP_ABCKIT_GRAPH_H 17 #define CPP_ABCKIT_GRAPH_H 18 19 #include "base_classes.h" 20 #include "basic_block.h" 21 #include "dynamic_isa.h" 22 #include "instruction.h" 23 #include "./static_isa.h" 24 25 #include <memory> 26 #include <vector> 27 28 namespace abckit { 29 30 /** 31 * @brief Graph 32 */ 33 class Graph final : public Resource<AbckitGraph *> { 34 // To access private constructor. 35 // We restrict constructors in order to prevent C/C++ API mix-up by user. 36 37 /// @brief To access private constructor 38 friend class core::Function; 39 /// @brief To access private constructor 40 friend class DynamicIsa; 41 /// @brief To access private constructor 42 friend class StaticIsa; 43 /// @brief To access private constructor 44 friend class BasicBlock; 45 46 public: 47 /** 48 * @brief Deleted constructor 49 * @param other 50 */ 51 Graph(const Graph &other) = delete; 52 53 /** 54 * @brief Deleted constructor 55 * @param other 56 * @return Graph 57 */ 58 Graph &operator=(const Graph &other) = delete; 59 60 /** 61 * @brief Constructor 62 * @param other 63 */ 64 Graph(Graph &&other) = default; 65 66 /** 67 * @brief Constructor 68 * @param other 69 * @return Graph 70 */ 71 Graph &operator=(Graph &&other) = default; 72 73 /** 74 * @brief Destructor 75 */ 76 ~Graph() override = default; 77 78 /** 79 * @brief Returns ISA type for a graph. 80 * @return ISA of the graph. 81 */ 82 AbckitIsaType GetIsa() const; 83 84 /** 85 * @brief Returns binary file that the current `Graph` is a part of. 86 * @return Pointer to the `File` 87 */ GetFile()88 const File *GetFile() const 89 { 90 return file_; 91 } 92 93 /** 94 * @brief Get start basic block 95 * @return `BasicBlock` 96 */ 97 BasicBlock GetStartBb() const; 98 99 /** 100 * @brief Returns end basic block of current `Graph`. 101 * @return End `BasicBlock`. 102 */ 103 BasicBlock GetEndBb() const; 104 105 /** 106 * @brief Returns number of basic blocks in a `graph`. 107 * @return Number of basic blocks in a `graph`. 108 */ 109 inline uint32_t GetNumBbs() const; 110 111 /** 112 * @brief Get blocks RPO 113 * @return vector of `BasicBlock` 114 */ 115 std::vector<BasicBlock> GetBlocksRPO() const; 116 117 /** 118 * @brief Get dynamic ISA 119 * @return `DynamicIsa` 120 */ 121 DynamicIsa DynIsa() const; 122 123 /** 124 * @brief EnumerateBasicBlocksRpo 125 * @return New state of `graph`. 126 * @param cb 127 */ 128 const Graph &EnumerateBasicBlocksRpo(const std::function<bool(BasicBlock)> &cb) const; 129 130 /** 131 * @brief Returns basic blocks with given `id` of a graph. 132 * @return BasicBlock with given `id`. 133 * @param [ in ] bbId - ID of basic block. 134 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if there is no basic block with given `id` in `graph`. 135 */ 136 BasicBlock GetBasicBlock(uint32_t bbId) const; 137 138 /** 139 * @brief Returns parameter instruction under given `index` of a `graph`. 140 * @return Parameter instruction under given `index` of a `graph`. 141 * @param [ in ] index - Index of the parameter. 142 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if there is no parameter under given `index` in `graph`. 143 */ 144 Instruction GetParameter(uint32_t index) const; 145 146 /** 147 * @brief Returns number of instruction parameters under a `graph`. 148 * @return uint32_t corresponding to number of instruction parameters. 149 */ 150 uint32_t GetNumberOfParameters() const; 151 152 /** 153 * @brief Wraps basic blocks from `tryFirstBB` to `tryLastBB` into try, 154 * inserts basic blocks from `catchBeginBB` to `catchEndBB` into graph. 155 * Basic blocks from `catchBeginBB` to `catchEndBB` are used for exception handling. 156 * @return New state of `graph`. 157 * @param [ in ] tryBegin - Start basic block to wrap into try. 158 * @param [ in ] tryEnd - End basic block to wrap into try. 159 * @param [ in ] catchBegin - Start basic block to handle exception. 160 * @param [ in ] catchEnd - End basic block to handle exception. 161 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `tryBegin` is Empty. 162 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `tryEnd` is Empty. 163 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `catchBegin` is Empty. 164 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `catchEnd` is Empty. 165 */ 166 const Graph &InsertTryCatch(BasicBlock tryBegin, BasicBlock tryEnd, BasicBlock catchBegin, 167 BasicBlock catchEnd) const; 168 169 /** 170 * @brief Dumps a `graph` into given file descriptor. 171 * @return New state of `graph`. 172 * @param [ in ] fd - File descriptor where dump is written. 173 */ 174 const Graph &Dump(int32_t fd) const; 175 176 /** 177 * @brief Creates I32 constant instruction and inserts it in start basic block of current `Graph`. 178 * @return Created `Instruction`. 179 * @param [ in ] val - value of created constant instruction. 180 * @note Allocates 181 */ 182 Instruction FindOrCreateConstantI32(int32_t val) const; 183 184 /** 185 * @brief Creates I64 constant instruction and inserts it in start basic block of current `Graph`. 186 * @return Created `Instruction`. 187 * @param [ in ] val - value of created constant instruction. 188 * @note Allocates 189 */ 190 Instruction FindOrCreateConstantI64(int64_t val) const; 191 192 /** 193 * @brief Creates U64 constant instruction and inserts it in start basic block of current `Graph`. 194 * @return Created `Instruction`. 195 * @param [ in ] val - value of created constant instruction. 196 * @note Allocates 197 */ 198 Instruction FindOrCreateConstantU64(uint64_t val) const; 199 200 /** 201 * @brief Creates F64 constant instruction and inserts it in start basic block of current `Graph`. 202 * @return Created `Instruction`. 203 * @param [ in ] val - value of created constant instruction. 204 * @note Allocates 205 */ 206 Instruction FindOrCreateConstantF64(double val) const; 207 208 /** 209 * @brief Creates empty basic block. 210 * @return Created `BasicBlock`. 211 * @note Allocates 212 */ 213 BasicBlock CreateEmptyBb() const; 214 215 /** 216 * @brief Removes all basic blocks unreachable from start basic block. 217 * @return New state of `graph`. 218 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `bool(*this)` results in `false` 219 */ 220 const Graph &RunPassRemoveUnreachableBlocks() const; 221 222 protected: 223 /** 224 * @brief Get api config 225 * @return `ApiConfig` 226 */ GetApiConfig()227 const ApiConfig *GetApiConfig() const override 228 { 229 return conf_; 230 } 231 232 /** 233 * @brief Struct for using in callbacks 234 */ 235 template <typename D> 236 struct Payload { 237 /** 238 * @brief data 239 */ 240 D data; 241 /** 242 * @brief config 243 */ 244 const ApiConfig *config; 245 /** 246 * @brief resource 247 */ 248 const Graph *resource; 249 }; 250 251 private: 252 class GraphDeleter final : public IResourceDeleter { 253 public: 254 /** 255 * @brief Constructor 256 * @param conf 257 * @param graph 258 */ GraphDeleter(const ApiConfig * conf,const Graph & graph)259 GraphDeleter(const ApiConfig *conf, const Graph &graph) : conf_(conf), deleterGraph_(graph) {}; 260 261 /** 262 * @brief Deleted constructor 263 * @param other 264 */ 265 GraphDeleter(const GraphDeleter &other) = delete; 266 267 /** 268 * @brief Deleted constructor 269 * @param other 270 */ 271 GraphDeleter &operator=(const GraphDeleter &other) = delete; 272 273 /** 274 * @brief Deleted constructor 275 * @param other 276 */ 277 GraphDeleter(GraphDeleter &&other) = delete; 278 279 /** 280 * @brief Deleted constructor 281 * @param other 282 */ 283 GraphDeleter &operator=(GraphDeleter &&other) = delete; 284 285 /** 286 * @brief Destructor 287 */ 288 ~GraphDeleter() override = default; 289 290 /** 291 * @brief Delete resource 292 */ DeleteResource()293 void DeleteResource() override 294 { 295 conf_->cApi_->destroyGraph(deleterGraph_.GetResource()); 296 } 297 298 private: 299 const ApiConfig *conf_; 300 const Graph &deleterGraph_; 301 }; 302 Graph(AbckitGraph * graph,const ApiConfig * conf,const File * file)303 Graph(AbckitGraph *graph, const ApiConfig *conf, const File *file) : Resource(graph), conf_(conf), file_(file) 304 { 305 SetDeleter(std::make_unique<GraphDeleter>(conf_, *this)); 306 }; 307 // for interop with API 308 const ApiConfig *conf_; 309 const File *file_; 310 }; 311 312 } // namespace abckit 313 314 #endif // CPP_ABCKIT_GRAPH_H 315