• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string>
17 #include "pipeline/jit/pi/graph_capture/node.h"
18 #include "pipeline/jit/pi/graph_capture/cfg.h"
19 #include "pipeline/jit/pi/graph_capture/graph.h"
20 
21 namespace mindspore {
22 namespace pijit {
23 
24 static AbstractObjectBase kNullObject(AObject::kTypeAnyValue);
25 ValueNode ValueNode::kUnboundLocal(ValueNode::kUnbound, &kNullObject, 0, 0);
26 
27 // these value node not in locals
IsNonLocalValue(ValueNode * i)28 bool IsNonLocalValue(ValueNode *i) {
29   int op = i->GetOpcode();
30   return op == LOAD_CONST || op == LOAD_GLOBAL || op == LOAD_DEREF || i->GetType() == ValueNode::CellVar ||
31          i->GetType() == ValueNode::FreeVar;
32 }
33 
SetVobj(AObject * object_info)34 void ValueNode::SetVobj(AObject *object_info) {
35   auto replaced = this->vobj_;
36   this->vobj_ = object_info;
37   if (this->GetGraph() == nullptr) {
38     return;
39   }
40   const auto &data = this->GetGraph()->GetSideEffect()->data();
41   if (replaced != nullptr) {
42     data->UnTrack(replaced->GetPyObject().ptr(), this);
43   }
44   if (object_info != nullptr) {
45     data->Track(object_info->GetPyObject().ptr(), this);
46   }
47 }
48 
get_attr(const std::string & nam)49 AObject *ValueNode::get_attr(const std::string &nam) {
50   if (vobj_) {
51     return vobj_->GetAttr(nam);
52   }
53   return AObject::MakeAObject(AObject::kTypeAnyValue);
54 }
55 
binary_subscr(ValueNode * sub)56 AObject *ValueNode::binary_subscr(ValueNode *sub) {
57   if (vobj_) {
58     return vobj_->GetItem(sub->GetVobj());
59   }
60   return AObject::MakeAObject(AObject::kTypeAnyValue);
61 }
62 
IsConstantValue() const63 bool ValueNode::IsConstantValue() const {
64   return constant_info_ != nullptr && constant_info_->value().ptr() != nullptr;
65 }
66 
SetConstantValue(bool constant)67 void ValueNode::SetConstantValue(bool constant) {
68   if (constant && this->GetVobj() != nullptr) {
69     MakeConstantInfo()->set_value(this->GetVobj()->GetPyObject());
70     return;
71   }
72   if (constant_info_ != nullptr) {
73     constant_info_->set_value(py::object());
74   }
75 }
76 
MakeConstantInfo()77 const std::unique_ptr<ConstantInfo> &ValueNode::MakeConstantInfo() {
78   if (constant_info_ == nullptr) {
79     constant_info_ = std::make_unique<ConstantInfo>();
80   }
81   return constant_info_;
82 }
83 
ToString() const84 std::string ParamNode::ToString() const {
85   std::stringstream s;
86   s << GetOparg() << ":" << GetVobj()->ToString() << '<' << this << '>';
87   return s.str();
88 }
89 
ToString() const90 std::string CellVarNode::ToString() const {
91   if (val_) {
92     return std::string("Cell:").append(val_->ToString());
93   }
94   char buf[64];
95   snprintf(buf, sizeof(buf), "Cell:%p->(nil)", this);
96   return buf;
97 }
98 
ToString() const99 std::string CallNode::ToString() const {
100   std::stringstream s;
101   s << this->ValueNode::ToString() << "sub-graph " << sub_graph_;
102   return s.str();
103 }
104 
ToString() const105 std::string ValueNode::ToString() const {
106   if (this == &ValueNode::kUnboundLocal) {
107     return "(kUnboundLocal)";
108   }
109   std::stringstream s;
110   s << this->InstrNode::ToString();
111   s << " vobj:{" << vobj_ << ":" << (vobj_ ? vobj_->ToString() : "(nil)") << "}";
112   for (auto i : inputs_) {
113     s << i << ',';
114   }
115   if (constant_info_ != nullptr) {
116     s << " constant: " << constant_info_->ToString();
117   }
118   return s.str();
119 }
120 
ToString() const121 std::string InstrNode::ToString() const {
122   std::stringstream s;
123   s << this->AbstractNode::ToString() << " bci " << bci() << " lno " << GetLineNo() << ' ' << Opcode(GetOpcode()).name()
124     << ' ' << GetOparg() << ' ' << GetName();
125   return s.str();
126 }
127 
ToString() const128 std::string AbstractNode::ToString() const {
129   std::stringstream s;
130   s << this << " type " << type_ << " graph " << graph_;
131   return s.str();
132 }
133 
SetParent(ValueNode * parent)134 void ValueNode::SetParent(ValueNode *parent) { parent_ = std::make_optional<ValueNode *>(parent); }
135 
SetSubGraph(Graph * n)136 void CallNode::SetSubGraph(Graph *n) {
137   sub_graph_ = n;
138   if (n) {
139     n->SetParent(GetGraph());
140   }
141 }
142 }  // namespace pijit
143 }  // namespace mindspore
144