• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019-2022 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 
17 #ifndef MINDSPORE_CORE_EFFECT_INFO_H_
18 #define MINDSPORE_CORE_EFFECT_INFO_H_
19 
20 #include <string>
21 
22 namespace mindspore {
23 struct EffectInfo {
24   enum State : unsigned char {
25     kUnknown = 0,
26     kDetecting = 1,
27     kDetected = 2,
28   };
29   State state = kUnknown;  // effect info state.
30   bool memory = false;     // memory side effects, e.g., access global variable.
31   bool io = false;         // IO side effects, e.g., print message.
32   bool load = false;       // load value from global variable, e.g. add(self.para, x).
33   bool back_mem = false;
34 
MergeEffectInfo35   void Merge(const EffectInfo &info) {
36     if (info.state != EffectInfo::kDetected) {
37       state = EffectInfo::kDetecting;
38     }
39     memory = memory || info.memory;
40     io = io || info.io;
41     load = load || info.load;
42     back_mem = back_mem || info.back_mem;
43   }
44 
HasEffectEffectInfo45   bool HasEffect() const { return state == kDetected && (memory || io || load || back_mem); }
46 
ToStringEffectInfo47   std::string ToString() const {
48     std::stringstream ss;
49     if (HasEffect()) {
50       ss << "SideEffect: { " << (memory ? "memory " : "") << (io ? "io " : "") << (load ? "load " : "")
51          << (back_mem ? "back_mem " : "") << "}";
52     } else if (state == kDetected) {
53       ss << "SideEffect: <None>";
54     } else {
55       ss << "SideEffect: <Not Detected>";
56     }
57     return ss.str();
58   }
59 };
60 
61 // EffectInfoHolder as base class for effect info holders, such as CNode, FuncGraph, etc.
62 class EffectInfoHolder {
63  public:
64   // Gets effect info.
GetEffectInfo()65   const EffectInfo &GetEffectInfo() const { return effect_info_; }
66 
67   // Set effect info.
SetEffectInfo(const EffectInfo & info)68   void SetEffectInfo(const EffectInfo &info) { effect_info_ = info; }
69 
70   virtual ~EffectInfoHolder() = default;
71 
72  protected:
73   EffectInfo effect_info_;
74 };
75 }  // namespace mindspore
76 
77 #endif  // MINDSPORE_CORE_EFFECT_INFO_H_
78