1 /** 2 * Copyright 2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_PI_JIT_GRAPH_CAPTURE_LOOP_H 17 #define MINDSPORE_PI_JIT_GRAPH_CAPTURE_LOOP_H 18 19 #include <memory> 20 #include <queue> 21 #include <set> 22 #include <string> 23 #include "pipeline/jit/pi/pydef.h" 24 #include "pipeline/jit/pi/utils/allocator.h" 25 26 namespace mindspore { 27 namespace pijit { 28 class Block; 29 class LoopInfo { 30 public: 31 LoopInfo() = default; 32 ~LoopInfo() = default; 33 prev()34 LoopInfo *prev() { return prev_; } set_prev(LoopInfo * loop)35 void set_prev(LoopInfo *loop) { prev_ = loop; } next()36 LoopInfo *next() { return next_; } set_next(LoopInfo * loop)37 void set_next(LoopInfo *loop) { next_ = loop; } header()38 Block *header() const { return header_; } set_header(Block * bb)39 void set_header(Block *bb) { header_ = bb; } loop_members()40 const std::set<Block *> &loop_members() const { return loop_members_; } backedges()41 const std::set<Block *> &backedges() const { return backedges_; } exits()42 const std::set<Block *> &exits() const { return exits_; } InsertLoopMembers(Block * bb)43 void InsertLoopMembers(Block *bb) { loop_members_.insert(bb); } InsertBackedge(Block * bb)44 void InsertBackedge(Block *bb) { backedges_.insert(bb); } InsertExit(Block * bb)45 void InsertExit(Block *bb) { exits_.insert(bb); } 46 std::string Dump() const; 47 48 protected: 49 LoopInfo *prev_ = nullptr; 50 LoopInfo *next_ = nullptr; 51 52 private: 53 Block *header_ = nullptr; 54 std::set<Block *> otherLoopEntries_; 55 std::set<Block *> loop_members_; 56 std::set<Block *> backedges_; 57 std::set<Block *> exits_; 58 std::set<Block *> innerLoops_; 59 LoopInfo *outerLoop_ = nullptr; 60 }; 61 62 class Graph; 63 class LoopFinder { 64 public: 65 explicit LoopFinder(Graph *graph); 66 ~LoopFinder() = default; 67 68 void FormSimpleLoopInfo(); 69 void UpdateLoop2Graph(); 70 71 private: 72 Graph &graph_; 73 Allocator &alloc_; 74 LoopInfo *loops_ = nullptr; 75 }; 76 } // namespace pijit 77 } // namespace mindspore 78 79 #endif // MINDSPORE_PI_JIT_GRAPH_CAPTURE_LOOP_H 80