• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_recorder.h"
17 #include "ecmascript/jspandafile/js_pandafile_manager.h"
18 
19 namespace panda::ecmascript::kungfu {
20 using PGOType = pgo::PGOType;
21 using PGOObjectInfo = pgo::PGOObjectInfo;
22 
PGOTypeRecorder(const PGOProfilerDecoder & decoder)23 PGOTypeRecorder::PGOTypeRecorder(const PGOProfilerDecoder &decoder) : decoder_(decoder) {}
24 
PGOTypeRecorder(const PGOProfilerDecoder & decoder,const JSPandaFile * jsPandaFile,uint32_t methodOffset)25 PGOTypeRecorder::PGOTypeRecorder(
26     const PGOProfilerDecoder &decoder, const JSPandaFile *jsPandaFile, uint32_t methodOffset)
27     : decoder_(decoder)
28 {
29     auto callback = [this] (uint32_t offset, const PGOType *type) {
30         if (type->IsScalarOpType()) {
31             bcOffsetPGOOpTypeMap_.emplace(offset, reinterpret_cast<const PGOSampleType *>(type));
32         } else if (type->IsRwOpType()) {
33             bcOffsetPGORwTypeMap_[offset] = reinterpret_cast<const PGORWOpType *>(type);
34         } else if (type->IsDefineOpType()) {
35             bcOffsetPGODefOpTypeMap_.emplace(offset, reinterpret_cast<const PGODefineOpType *>(type));
36         } else {
37             UNREACHABLE();
38         }
39     };
40     const CString recordName = MethodLiteral::GetRecordName(jsPandaFile, EntityId(methodOffset));
41     auto methodLiteral = jsPandaFile->FindMethodLiteral(methodOffset);
42     decoder.GetTypeInfo(jsPandaFile, recordName, methodLiteral, callback);
43 }
44 
GetElementsKindsForUser(int32_t offset) const45 std::vector<ElementsKind> PGOTypeRecorder::GetElementsKindsForUser(int32_t offset) const
46 {
47     std::vector<ElementsKind> elementsKinds;
48     if (bcOffsetPGORwTypeMap_.find(offset) == bcOffsetPGORwTypeMap_.end()) {
49         elementsKinds.emplace_back(ElementsKind::GENERIC);
50         return elementsKinds;
51     }
52 
53     PGORWOpType rwType = *(bcOffsetPGORwTypeMap_.at(offset));
54     if (rwType.GetCount() == 0) {
55         elementsKinds.emplace_back(ElementsKind::GENERIC);
56         return elementsKinds;
57     }
58     for (uint32_t i = 0; i < rwType.GetCount(); i++) {
59         PGOObjectInfo info = rwType.GetObjectInfo(i);
60         auto profileType = info.GetProfileType();
61         if (profileType.IsBuiltinsArray()) {
62             elementsKinds.emplace_back(profileType.GetElementsKindBeforeTransition());
63             continue;
64         }
65     }
66 
67     return elementsKinds;
68 }
69 
GetTransitionElementsKindsForUser(int32_t offset) const70 std::vector<ElementsKind> PGOTypeRecorder::GetTransitionElementsKindsForUser(int32_t offset) const
71 {
72     std::vector<ElementsKind> elementsKinds;
73     if (bcOffsetPGORwTypeMap_.find(offset) == bcOffsetPGORwTypeMap_.end()) {
74         elementsKinds.emplace_back(ElementsKind::GENERIC);
75         return elementsKinds;
76     }
77 
78     PGORWOpType rwType = *(bcOffsetPGORwTypeMap_.at(offset));
79     if (rwType.GetCount() == 0) {
80         elementsKinds.emplace_back(ElementsKind::GENERIC);
81         return elementsKinds;
82     }
83     for (uint32_t i = 0; i < rwType.GetCount(); i++) {
84         PGOObjectInfo info = rwType.GetObjectInfo(i);
85         auto profileType = info.GetProfileType();
86         if (profileType.IsBuiltinsArray()) {
87             elementsKinds.emplace_back(profileType.GetElementsKindAfterTransition());
88             continue;
89         }
90     }
91 
92     return elementsKinds;
93 }
94 
GetElementsKindForCreater(int32_t offset) const95 ElementsKind PGOTypeRecorder::GetElementsKindForCreater(int32_t offset) const
96 {
97     if (bcOffsetPGODefOpTypeMap_.find(offset) != bcOffsetPGODefOpTypeMap_.end()) {
98         const auto iter = bcOffsetPGODefOpTypeMap_.at(offset);
99         return iter->GetElementsKind();
100     }
101     // When ElementsKind switch turned on, it should be GENERIC
102     return ElementsKind::NONE;
103 }
104 
GetElementsLength(int32_t offset) const105 uint32_t PGOTypeRecorder::GetElementsLength(int32_t offset) const
106 {
107     if (bcOffsetPGODefOpTypeMap_.find(offset) != bcOffsetPGODefOpTypeMap_.end()) {
108         const auto iter = bcOffsetPGODefOpTypeMap_.at(offset);
109         return iter->GetElementsLength();
110     }
111     return 0;
112 }
113 
GetPGOType(int32_t offset) const114 PGOTypeRef PGOTypeRecorder::GetPGOType(int32_t offset) const
115 {
116     if (bcOffsetPGOOpTypeMap_.find(offset) != bcOffsetPGOOpTypeMap_.end()) {
117         return PGOTypeRef(bcOffsetPGOOpTypeMap_.at(offset));
118     } else if (bcOffsetPGORwTypeMap_.find(offset) != bcOffsetPGORwTypeMap_.end()) {
119         return PGOTypeRef(bcOffsetPGORwTypeMap_.at(offset));
120     } else if (bcOffsetPGODefOpTypeMap_.find(offset) != bcOffsetPGODefOpTypeMap_.end()) {
121         return PGOTypeRef(bcOffsetPGODefOpTypeMap_.at(offset));
122     }
123     return PGOTypeRef::NoneType();
124 }
125 
IsValidPt(ProfileType type) const126 bool PGOTypeRecorder::IsValidPt(ProfileType type) const
127 {
128     auto abcId = type.GetAbcId();
129     CString abcName;
130     if (decoder_.GetAbcNameById(abcId, abcName)) {
131         CString normalizedFileName = JSPandaFile::GetNormalizedFileDesc(abcName);
132         if (JSPandaFileManager::GetInstance()->FindJSPandaFileByNormalizedName(normalizedFileName) == nullptr) {
133             LOG_ECMA(DEBUG) << "ProfileType is invalid:" << type.GetTypeString() << ", abcName:" << abcName
134                             << ", normalize name:" << normalizedFileName;
135             return false;
136         }
137         return true;
138     }
139     LOG_ECMA(DEBUG) << "ProfileType is invalid:" << type.GetTypeString();
140     return false;
141 }
142 }  // namespace panda::ecmascript
143