• 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_IR_SCOPE_H_
18 #define MINDSPORE_CORE_IR_SCOPE_H_
19 #include <string>
20 #include <memory>
21 #include <stack>
22 #include "mindapi/base/macros.h"
23 
24 namespace mindspore {
25 class Scope;
26 using ScopePtr = std::shared_ptr<Scope>;
27 MS_CORE_API extern const ScopePtr kDefaultScope;
28 
29 class MS_CORE_API Scope {
30  public:
31   /// \brief Constructor of Scope.
32   ///
33   /// \param[in] name The name of scope, usually using the default scope.
Scope(const std::string & name)34   explicit Scope(const std::string &name) : name_(name) {}
35 
36   /// \brief Destructor of Scope.
37   ~Scope() = default;
38 
39   /// \brief Get the name of scope.
40   ///
41   /// \return The name of scope.
name()42   std::string name() const { return name_; }
43 
44  private:
45   std::string name_;
46 };
47 
48 class MS_CORE_API ScopeManager {
49  public:
50   /// \brief Get instance of ScopeManager.
51   ///
52   /// \return Instance of ScopeManager.
53   static ScopeManager &GetInstance() noexcept;
54 
55   /// \brief Disable the default constructor.
56   ScopeManager(const ScopeManager &) = delete;
57   /// \brief Disable the default copy constructor.
58   ScopeManager &operator=(const ScopeManager &) = delete;
59   /// \brief Destructor.
60   ~ScopeManager() = default;
61 
62   /// \brief Enter the scope.
63   ///
64   /// \param[in] scope The scope.
65   void EnterScope(const ScopePtr &scope);
66 
67   /// \brief Leave the scope.
68   ///
69   /// \param[in] scope The scope.
70   void LeaveScope(const ScopePtr &scope) noexcept;
71 
72   /// \brief Get the current scope.
73   ///
74   /// \return The current scope.
75   ScopePtr GetCurrentScope();
76 
77   /// \brief Clear the scope.
78   void ClearScope();
79 
80  private:
81   ScopeManager() = default;
82   std::stack<ScopePtr> scope_stack_;
83 };
84 
85 // ScopeGuard is a class that help generate the anf node of specified scope
86 // in the current c++ action scope.
87 class ScopeGuard {
88  public:
ScopeGuard(const ScopePtr & scope)89   explicit ScopeGuard(const ScopePtr &scope) {
90     scope_ = scope;
91     ScopeManager::GetInstance().EnterScope(scope);
92   }
~ScopeGuard()93   ~ScopeGuard() { ScopeManager::GetInstance().LeaveScope(scope_); }
94 
95  private:
96   ScopePtr scope_;
97 };
98 }  // namespace mindspore
99 #endif  // MINDSPORE_CORE_IR_SCOPE_H_
100