1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/compiler/pgo_type/pgo_type_manager.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/object_factory.h"
20 #include "ecmascript/tagged_array-inl.h"
21 #include "index_accessor.h"
22
23 namespace panda::ecmascript::kungfu {
Iterate(const RootVisitor & v)24 void PGOTypeManager::Iterate(const RootVisitor &v)
25 {
26 for (auto &iter : hcData_) {
27 for (auto &hclassIter : iter.second) {
28 v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&(hclassIter.second))));
29 }
30 }
31 aotSnapshot_.Iterate(v);
32 }
33
GetConstantPoolIDByMethodOffset(const JSPandaFile * jsPandaFile,uint32_t methodOffset)34 int32_t PGOTypeManager::GetConstantPoolIDByMethodOffset(const JSPandaFile *jsPandaFile, uint32_t methodOffset)
35 {
36 panda_file::IndexAccessor indexAccessor(*jsPandaFile->GetPandaFile(),
37 panda_file::File::EntityId(methodOffset));
38 return static_cast<int32_t>(indexAccessor.GetHeaderIndex());
39 }
40
InitAOTSnapshot(uint32_t compileFilesCount)41 void PGOTypeManager::InitAOTSnapshot(uint32_t compileFilesCount)
42 {
43 aotSnapshot_.InitSnapshot(compileFilesCount);
44 GenHClassInfo();
45 GenArrayInfo();
46 GenConstantIndexInfo();
47 }
48
GenHClassInfo()49 void PGOTypeManager::GenHClassInfo()
50 {
51 uint32_t count = 0;
52 for (auto &data : hcData_) {
53 count += data.second.size();
54 }
55 ObjectFactory *factory = thread_->GetEcmaVM()->GetFactory();
56 JSHandle<TaggedArray> hclassInfo = factory->NewTaggedArray(count);
57 uint32_t pos = 0;
58 for (auto &x : hcData_) {
59 ProfileType rootType = x.first;
60 for (auto &y : x.second) {
61 ProfileType childType = y.first;
62 JSTaggedType hclass = y.second;
63 ProfileTyper key = std::make_pair(rootType, childType);
64 profileTyperToHClassIndex_.emplace(key, pos);
65 hclassInfo->Set(thread_, pos++, JSTaggedValue(hclass));
66 }
67 }
68 aotSnapshot_.StoreHClassInfo(hclassInfo);
69 }
70
GenArrayInfo()71 void PGOTypeManager::GenArrayInfo()
72 {
73 uint32_t count = arrayData_.size();
74 ObjectFactory *factory = thread_->GetEcmaVM()->GetFactory();
75 JSHandle<TaggedArray> arrayInfo = factory->NewTaggedArray(count);
76 for (uint32_t pos = 0; pos < count; pos++) {
77 arrayInfo->Set(thread_, pos, JSTaggedValue(arrayData_[pos]));
78 }
79 aotSnapshot_.StoreArrayInfo(arrayInfo);
80 }
81
GenConstantIndexInfo()82 void PGOTypeManager::GenConstantIndexInfo()
83 {
84 uint32_t count = constantIndexData_.size();
85 ObjectFactory *factory = thread_->GetEcmaVM()->GetFactory();
86 JSHandle<TaggedArray> constantIndexInfo = factory->NewTaggedArray(count);
87 for (uint32_t pos = 0; pos < count; pos++) {
88 constantIndexInfo->Set(thread_, pos, JSTaggedValue(constantIndexData_[pos]));
89 }
90 aotSnapshot_.StoreConstantIndexInfo(constantIndexInfo);
91 }
92
RecordHClass(ProfileType rootType,ProfileType childType,JSTaggedType hclass)93 void PGOTypeManager::RecordHClass(ProfileType rootType, ProfileType childType, JSTaggedType hclass)
94 {
95 auto iter = hcData_.find(rootType);
96 if (iter == hcData_.end()) {
97 auto map = TransIdToHClass();
98 map.emplace(childType, hclass);
99 hcData_.emplace(rootType, map);
100 return;
101 }
102
103 auto &hclassMap = iter->second;
104 auto hclassIter = hclassMap.find(childType);
105 if (hclassIter != hclassMap.end()) {
106 ASSERT(hclass == hclassIter->second);
107 return;
108 }
109 hclassMap.emplace(childType, hclass);
110 }
111
RecordElements(panda_file::File::EntityId id,JSTaggedValue elements)112 void PGOTypeManager::RecordElements(panda_file::File::EntityId id, JSTaggedValue elements)
113 {
114 JSHandle<TaggedArray> elementsHandle(thread_, elements);
115 arrayData_.emplace_back(elementsHandle.GetTaggedType());
116 idElmsIdxMap_.emplace(id, arrayData_.size() - 1);
117 }
118
RecordConstantIndex(uint32_t bcAbsoluteOffset,uint32_t index)119 void PGOTypeManager::RecordConstantIndex(uint32_t bcAbsoluteOffset, uint32_t index)
120 {
121 constantIndexData_.emplace_back(bcAbsoluteOffset);
122 constantIndexData_.emplace_back(index);
123 }
124
GetElementsIndexByEntityId(panda_file::File::EntityId id)125 int PGOTypeManager::GetElementsIndexByEntityId(panda_file::File::EntityId id)
126 {
127 auto iter = idElmsIdxMap_.find(id);
128 auto endIter = idElmsIdxMap_.end();
129 if (iter == endIter) {
130 return -1;
131 }
132 return iter->second;
133 }
134
GetHClassIndexByProfileType(ProfileTyper type) const135 uint32_t PGOTypeManager::GetHClassIndexByProfileType(ProfileTyper type) const
136 {
137 uint32_t index = -1;
138 auto iter = profileTyperToHClassIndex_.find(type);
139 if (iter != profileTyperToHClassIndex_.end()) {
140 index = iter->second;
141 }
142 return index;
143 }
144
QueryHClass(ProfileType rootType,ProfileType childType) const145 JSTaggedValue PGOTypeManager::QueryHClass(ProfileType rootType, ProfileType childType) const
146 {
147 JSTaggedValue result = JSTaggedValue::Undefined();
148 auto iter = hcData_.find(rootType);
149 if (iter != hcData_.end()) {
150 auto hclassMap = iter->second;
151 auto hclassIter = hclassMap.find(childType);
152 if (hclassIter != hclassMap.end()) {
153 result = JSTaggedValue(hclassIter->second);
154 }
155 }
156 return result;
157 }
158
SetCurConstantPool(const JSPandaFile * jsPandaFile,uint32_t methodOffset)159 void PGOTypeManager::SetCurConstantPool(const JSPandaFile *jsPandaFile, uint32_t methodOffset)
160 {
161 curCPID_ = GetConstantPoolIDByMethodOffset(jsPandaFile, methodOffset);
162 curCP_ = thread_->GetCurrentEcmaContext()->FindConstpool(jsPandaFile, curCPID_);
163 }
164 } // namespace panda::ecmascript
165