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 #ifndef ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_RECORD_H 17 #define ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_RECORD_H 18 19 #include "ecmascript/jit/jit_profiler.h" 20 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h" 21 #include "ecmascript/pgo_profiler/types/pgo_profiler_type.h" 22 23 namespace panda::ecmascript::kungfu { 24 using PGOProfilerDecoder = pgo::PGOProfilerDecoder; 25 using PGOHClassTreeDesc = pgo::PGOHClassTreeDesc; 26 using PGODefineOpType = pgo::PGODefineOpType; 27 using PGOProtoTransitionType = pgo::PGOProtoTransitionType; 28 using RootHClassLayoutDesc = pgo::RootHClassLayoutDesc; 29 using PGORWOpType = pgo::PGORWOpType; 30 31 class PGOTypeRecorder { 32 public: 33 explicit PGOTypeRecorder(const PGOProfilerDecoder &decoder); 34 PUBLIC_API PGOTypeRecorder(const PGOProfilerDecoder &decoder, 35 const JSPandaFile *jsPandaFile, uint32_t methodOffset); 36 ~PGOTypeRecorder() = default; 37 38 std::vector<ElementsKind> PUBLIC_API GetTransitionElementsKindsForUser(int32_t offset) const; 39 std::vector<ElementsKind> PUBLIC_API GetElementsKindsForUser(int32_t offset) const; 40 ElementsKind PUBLIC_API GetElementsKindForCreater(int32_t offset) const; 41 uint32_t PUBLIC_API GetElementsLength(int32_t offset) const; 42 RegionSpaceFlag PUBLIC_API GetRegionSpaceFlag(int32_t offset) const; 43 44 PGOTypeRef PUBLIC_API GetPGOType(int32_t offset) const; 45 46 bool PUBLIC_API IsInsufficientProfile(int32_t offset) const; 47 GetPGODefOpType(int32_t bcOffset)48 inline PGODefineOpType GetPGODefOpType(int32_t bcOffset) const 49 { 50 auto typeIter = bcOffsetPGODefOpTypeMap_.find(bcOffset); 51 if (typeIter == bcOffsetPGODefOpTypeMap_.end()) { 52 return PGODefineOpType(ProfileType::PROFILE_TYPE_NONE); 53 } 54 return *(typeIter->second); 55 } 56 GetHClassTreeDesc(PGOSampleType type,PGOHClassTreeDesc ** desc)57 inline bool GetHClassTreeDesc(PGOSampleType type, PGOHClassTreeDesc **desc) const 58 { 59 return decoder_.GetHClassTreeDesc(type, desc); 60 } 61 62 template <typename Callback> IterateHClassTreeDesc(Callback callback)63 void IterateHClassTreeDesc(Callback callback) const 64 { 65 decoder_.IterateHClassTreeDesc(callback); 66 } 67 68 template <typename Callback> IterateProtoTransitionPool(Callback callback)69 void IterateProtoTransitionPool(Callback callback) const 70 { 71 decoder_.IterateProtoTransitionPool(callback); 72 } 73 InitMap(JITProfiler * jitProfile)74 void InitMap(JITProfiler* jitProfile) 75 { 76 bcOffsetPGOOpTypeMap_ = jitProfile->GetOpTypeMap(); 77 bcOffsetPGODefOpTypeMap_ = jitProfile->GetDefOpTypeMap(); 78 bcOffsetPGORwTypeMap_ = jitProfile->GetRwTypeMap(); 79 bcOffsetBoolMap_ = jitProfile->GetBoolMap(); 80 } 81 82 bool IsValidPt(ProfileType type) const; 83 84 private: 85 const PGOProfilerDecoder &decoder_; 86 std::unordered_map<int32_t, const PGOSampleType*> bcOffsetPGOOpTypeMap_ {}; 87 std::unordered_map<int32_t, const PGORWOpType*> bcOffsetPGORwTypeMap_ {}; 88 std::unordered_map<int32_t, const PGODefineOpType*> bcOffsetPGODefOpTypeMap_; 89 std::unordered_map<int32_t, bool> bcOffsetBoolMap_ {}; 90 }; 91 } // panda::ecmascript::kungfu 92 #endif // ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_RECORD_H 93