1 /** 2 * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). 3 * 4 * Copyright 2019-2022 Huawei Technologies Co., Ltd 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CSE_H_ 20 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CSE_H_ 21 22 #include <vector> 23 #include <map> 24 #include "utils/hash_map.h" 25 #include "ir/anf.h" 26 #include "ir/manager.h" 27 #include "include/common/visible.h" 28 29 namespace mindspore { 30 /* namespace to support opt */ 31 namespace opt { 32 // Common subexpression elimination. 33 class COMMON_EXPORT CSE { 34 public: 35 CSE() = default; 36 virtual ~CSE() = default; 37 38 virtual bool CheckReplace(const AnfNodePtr &main, const AnfNodePtr &node); 39 40 virtual bool Cse(const FuncGraphPtr root, const FuncGraphManagerPtr manager); 41 42 bool HasHiddenSideEffect(const AnfNodePtr &node); 43 44 protected: 45 void Init(); 46 bool BuildOrderGroupForOneGraph(const FuncGraphPtr &fg); 47 void DoReplace(const FuncGraphManagerPtr &manager); 48 AnfNodePtr GetReplicatedNode(const AnfNodePtr &node) const; 49 50 private: 51 bool BuildOrderGroupAndDoReplace(const FuncGraphManagerPtr manager); 52 bool CalReplaceNodes(const std::vector<std::size_t> &order_group, 53 mindspore::HashMap<std::size_t, std::vector<AnfNodePtr>> *groups); 54 void AddReplicatedNode(const AnfNodePtr &node, const AnfNodePtr &main); 55 bool IsHiddenSideEffectCall(const AnfNodePtr &node); 56 // Record func graphs having hidden_side_effect cnode. 57 HashSet<FuncGraphPtr> hidden_side_effect_func_graphs_; 58 // Record all node need to be replaced 59 OrderedMap<AnfNodePtr, AnfNodePtr> replicated_nodes_; 60 }; 61 62 COMMON_EXPORT BasePtr AbsOf(const AnfNodePtr &node, bool ignore_fg_abs_tracking_id = false); 63 } // namespace opt 64 } // namespace mindspore 65 66 #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_CSE_H_ 67