14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_AOT_COMPILER_PREPROCESSOR_H 164514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_AOT_COMPILER_PREPROCESSOR_H 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/compiler/bytecode_info_collector.h" 194514f5e3Sopenharmony_ci#include "ecmascript/compiler/compiler_log.h" 204514f5e3Sopenharmony_ci#include "ecmascript/pgo_profiler/pgo_profiler_decoder.h" 214514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 224514f5e3Sopenharmony_ci#include "macros.h" 234514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_compilation_env.h" 244514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/aot_file_manager.h" 254514f5e3Sopenharmony_ci#include "ecmascript/ohos/ohos_preload_app_info.h" 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu { 284514f5e3Sopenharmony_ciclass OhosPkgArgs; 294514f5e3Sopenharmony_ciusing PGOProfilerDecoder = pgo::PGOProfilerDecoder; 304514f5e3Sopenharmony_ci 314514f5e3Sopenharmony_cistruct AbcFileInfo { 324514f5e3Sopenharmony_ci explicit AbcFileInfo(std::string extendedFilePath, std::shared_ptr<JSPandaFile> jsPandaFile) 334514f5e3Sopenharmony_ci : extendedFilePath_(extendedFilePath), jsPandaFile_(jsPandaFile) {} 344514f5e3Sopenharmony_ci ~AbcFileInfo() = default; 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_ci std::string extendedFilePath_; 374514f5e3Sopenharmony_ci std::shared_ptr<JSPandaFile> jsPandaFile_; 384514f5e3Sopenharmony_ci}; 394514f5e3Sopenharmony_ci 404514f5e3Sopenharmony_ciclass CallMethodFlagMap { 414514f5e3Sopenharmony_cipublic: 424514f5e3Sopenharmony_ci CallMethodFlagMap() {} 434514f5e3Sopenharmony_ci void SetIsFastCall(CString fileDesc, uint32_t methodOffset, bool isFastCall); 444514f5e3Sopenharmony_ci bool IsFastCall(CString fileDesc, uint32_t methodOffset) const; 454514f5e3Sopenharmony_ci void SetIsAotCompile(CString fileDesc, uint32_t methodOffset, bool isAotCompile); 464514f5e3Sopenharmony_ci bool IsAotCompile(CString fileDesc, uint32_t methodOffset) const; 474514f5e3Sopenharmony_ci void SetIsJitCompile(CString fileDesc, uint32_t methodOffset, bool isAotCompile); 484514f5e3Sopenharmony_ci bool IsJitCompile(CString fileDesc, uint32_t methodOffset) const; 494514f5e3Sopenharmony_ciprivate: 504514f5e3Sopenharmony_ci std::map<std::pair<CString, uint32_t>, bool> abcIdMethodIdToIsFastCall_ {}; 514514f5e3Sopenharmony_ci std::map<std::pair<CString, uint32_t>, bool> abcIdMethodIdToIsAotCompile_ {}; 524514f5e3Sopenharmony_ci std::map<std::pair<CString, uint32_t>, bool> abcIdMethodIdToIsJitCompile_ {}; 534514f5e3Sopenharmony_ci}; 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_cistruct CompilationOptions { 564514f5e3Sopenharmony_ci explicit CompilationOptions(JSRuntimeOptions &runtimeOptions); 574514f5e3Sopenharmony_ci void ParseOption(const std::string &option, std::map<std::string, std::vector<std::string>> &optionMap) const; 584514f5e3Sopenharmony_ci std::vector<std::string> SplitString(const std::string &str, const char ch) const; 594514f5e3Sopenharmony_ci std::string triple_; 604514f5e3Sopenharmony_ci std::string outputFileName_; 614514f5e3Sopenharmony_ci size_t optLevel_; 624514f5e3Sopenharmony_ci size_t relocMode_; 634514f5e3Sopenharmony_ci std::string logOption_; 644514f5e3Sopenharmony_ci std::string logMethodsList_; 654514f5e3Sopenharmony_ci bool compilerLogTime_; 664514f5e3Sopenharmony_ci bool deviceIsScreenOff_; 674514f5e3Sopenharmony_ci size_t maxAotMethodSize_; 684514f5e3Sopenharmony_ci size_t maxMethodsInModule_; 694514f5e3Sopenharmony_ci uint32_t hotnessThreshold_; 704514f5e3Sopenharmony_ci int32_t deviceThermalLevel_ {0}; 714514f5e3Sopenharmony_ci std::string profilerIn_; 724514f5e3Sopenharmony_ci std::string optBCRange_; 734514f5e3Sopenharmony_ci bool needMerge_ {false}; 744514f5e3Sopenharmony_ci bool isEnableArrayBoundsCheckElimination_ {true}; 754514f5e3Sopenharmony_ci bool isEnableTypeLowering_ {true}; 764514f5e3Sopenharmony_ci bool isEnableEarlyElimination_ {true}; 774514f5e3Sopenharmony_ci bool isEnableLaterElimination_ {true}; 784514f5e3Sopenharmony_ci bool isEnableValueNumbering_ {true}; 794514f5e3Sopenharmony_ci bool isEnableOptInlining_ {true}; 804514f5e3Sopenharmony_ci bool isEnableOptString_ {true}; 814514f5e3Sopenharmony_ci bool isEnableOptPGOType_ {true}; 824514f5e3Sopenharmony_ci bool isEnableOptTrackField_ {true}; 834514f5e3Sopenharmony_ci bool isEnableOptLoopPeeling_ {true}; 844514f5e3Sopenharmony_ci bool isEnableOptLoopInvariantCodeMotion_ {false}; 854514f5e3Sopenharmony_ci bool isEnableOptConstantFolding_ {true}; 864514f5e3Sopenharmony_ci bool isEnableLexenvSpecialization_ {false}; 874514f5e3Sopenharmony_ci bool isEnableNativeInline_ {true}; 884514f5e3Sopenharmony_ci bool isEnablePGOHCRLowering_ {false}; 894514f5e3Sopenharmony_ci bool isEnableLoweringBuiltin_ {true}; 904514f5e3Sopenharmony_ci bool isEnableOptBranchProfiling_ {true}; 914514f5e3Sopenharmony_ci bool isEnableEscapeAnalysis_ {false}; 924514f5e3Sopenharmony_ci bool isEnableInductionVariableAnalysis_ {false}; 934514f5e3Sopenharmony_ci bool isEnableVerifierPass_ {true}; 944514f5e3Sopenharmony_ci bool isEnableBaselinePgo_ {false}; 954514f5e3Sopenharmony_ci std::map<std::string, std::vector<std::string>> optionSelectMethods_; 964514f5e3Sopenharmony_ci std::map<std::string, std::vector<std::string>> optionSkipMethods_; 974514f5e3Sopenharmony_ci}; 984514f5e3Sopenharmony_ci 994514f5e3Sopenharmony_ciclass AotCompilerPreprocessor { 1004514f5e3Sopenharmony_cipublic: 1014514f5e3Sopenharmony_ci AotCompilerPreprocessor(EcmaVM *vm, JSRuntimeOptions &runtimeOptions, 1024514f5e3Sopenharmony_ci std::map<std::string, std::shared_ptr<OhosPkgArgs>> &pkgsArgs, 1034514f5e3Sopenharmony_ci PGOProfilerDecoder &profilerDecoder, arg_list_t &pandaFileNames) 1044514f5e3Sopenharmony_ci : vm_(vm), 1054514f5e3Sopenharmony_ci runtimeOptions_(runtimeOptions), 1064514f5e3Sopenharmony_ci pkgsArgs_(pkgsArgs), 1074514f5e3Sopenharmony_ci profilerDecoder_(profilerDecoder), 1084514f5e3Sopenharmony_ci pandaFileNames_(pandaFileNames), 1094514f5e3Sopenharmony_ci aotCompilationEnv_(vm) {}; 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ci ~AotCompilerPreprocessor() = default; 1124514f5e3Sopenharmony_ci 1134514f5e3Sopenharmony_ci bool HandleTargetCompilerMode(CompilationOptions &cOptions); 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci bool HandlePandaFileNames(const int argc, const char **argv); 1164514f5e3Sopenharmony_ci 1174514f5e3Sopenharmony_ci void AOTInitialize(); 1184514f5e3Sopenharmony_ci 1194514f5e3Sopenharmony_ci void DoPreAnalysis(CompilationOptions &cOptions); 1204514f5e3Sopenharmony_ci 1214514f5e3Sopenharmony_ci void AnalyzeGraphs(JSPandaFile *jsPandaFile, BytecodeInfoCollector &collector, CompilationOptions &cOptions); 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_ci void AnalyzeGraph(BCInfo &bytecodeInfo, CompilationOptions &cOptions, BytecodeInfoCollector &collector, 1244514f5e3Sopenharmony_ci MethodLiteral *methodLiteral, MethodPcInfo &methodPCInfo); 1254514f5e3Sopenharmony_ci 1264514f5e3Sopenharmony_ci void Process(CompilationOptions &cOptions); 1274514f5e3Sopenharmony_ci 1284514f5e3Sopenharmony_ci uint32_t GenerateAbcFileInfos(); 1294514f5e3Sopenharmony_ci 1304514f5e3Sopenharmony_ci void GenerateBytecodeInfoCollectors(const CompilationOptions &cOptions); 1314514f5e3Sopenharmony_ci 1324514f5e3Sopenharmony_ci bool HandleMergedPgoFile(uint32_t checksum); 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ci void GeneratePGOTypes(); 1354514f5e3Sopenharmony_ci 1364514f5e3Sopenharmony_ci void SnapshotInitialize(); 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_ci bool FilterOption(const std::map<std::string, std::vector<std::string>> &optionMap, 1394514f5e3Sopenharmony_ci const std::string &recordName, const std::string &methodName) const; 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ci bool IsSkipMethod(const JSPandaFile *jsPandaFile, const BCInfo &bytecodeInfo, 1424514f5e3Sopenharmony_ci const CString &recordName, const MethodLiteral *methodLiteral, 1434514f5e3Sopenharmony_ci const MethodPcInfo &methodPCInfo, const std::string &methodName, 1444514f5e3Sopenharmony_ci CompilationOptions &cOptions) const; 1454514f5e3Sopenharmony_ci 1464514f5e3Sopenharmony_ci void GenerateMethodMap(CompilationOptions &cOptions); 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_ci bool MethodHasTryCatch(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral) const; 1494514f5e3Sopenharmony_ci 1504514f5e3Sopenharmony_ci bool HasSkipMethod(const CVector<std::string> &methodList, const std::string &methodName) const; 1514514f5e3Sopenharmony_ci 1524514f5e3Sopenharmony_ci bool ForbidenRebuildAOT(std::string &fileName) const; 1534514f5e3Sopenharmony_ci 1544514f5e3Sopenharmony_ci bool HasPreloadAotFile() const; 1554514f5e3Sopenharmony_ci 1564514f5e3Sopenharmony_ci bool HasExistsAOTFiles(CompilationOptions &cOptions) const; 1574514f5e3Sopenharmony_ci 1584514f5e3Sopenharmony_ci void SetIsFastCall(CString fileDesc, uint32_t methodOffset, bool isFastCall) 1594514f5e3Sopenharmony_ci { 1604514f5e3Sopenharmony_ci callMethodFlagMap_.SetIsFastCall(fileDesc, methodOffset, isFastCall); 1614514f5e3Sopenharmony_ci } 1624514f5e3Sopenharmony_ci 1634514f5e3Sopenharmony_ci bool IsFastCall(CString fileDesc, uint32_t methodOffset) 1644514f5e3Sopenharmony_ci { 1654514f5e3Sopenharmony_ci return callMethodFlagMap_.IsFastCall(fileDesc, methodOffset); 1664514f5e3Sopenharmony_ci } 1674514f5e3Sopenharmony_ci 1684514f5e3Sopenharmony_ci void SetIsAotCompile(CString fileDesc, uint32_t methodOffset, bool isAotCompile) 1694514f5e3Sopenharmony_ci { 1704514f5e3Sopenharmony_ci callMethodFlagMap_.SetIsAotCompile(fileDesc, methodOffset, isAotCompile); 1714514f5e3Sopenharmony_ci } 1724514f5e3Sopenharmony_ci 1734514f5e3Sopenharmony_ci bool GetIsAotCompile(CString fileDesc, uint32_t methodOffset) 1744514f5e3Sopenharmony_ci { 1754514f5e3Sopenharmony_ci return callMethodFlagMap_.IsAotCompile(fileDesc, methodOffset); 1764514f5e3Sopenharmony_ci } 1774514f5e3Sopenharmony_ci 1784514f5e3Sopenharmony_ci std::string GetMainPkgArgsAppSignature() const; 1794514f5e3Sopenharmony_ci 1804514f5e3Sopenharmony_ci bool GetCompilerResult() 1814514f5e3Sopenharmony_ci { 1824514f5e3Sopenharmony_ci // The size of fileInfos is not equal to pandaFiles size, set compiler result to false 1834514f5e3Sopenharmony_ci return fileInfos_.size() == pandaFileNames_.size(); 1844514f5e3Sopenharmony_ci } 1854514f5e3Sopenharmony_ci 1864514f5e3Sopenharmony_ci const CVector<AbcFileInfo>& GetAbcFileInfo() const 1874514f5e3Sopenharmony_ci { 1884514f5e3Sopenharmony_ci return fileInfos_; 1894514f5e3Sopenharmony_ci } 1904514f5e3Sopenharmony_ci 1914514f5e3Sopenharmony_ci const CVector<std::unique_ptr<BytecodeInfoCollector>>& GetBcInfoCollectors() const 1924514f5e3Sopenharmony_ci { 1934514f5e3Sopenharmony_ci return bcInfoCollectors_; 1944514f5e3Sopenharmony_ci } 1954514f5e3Sopenharmony_ci 1964514f5e3Sopenharmony_ci std::shared_ptr<OhosPkgArgs> GetMainPkgArgs() const 1974514f5e3Sopenharmony_ci { 1984514f5e3Sopenharmony_ci if (pkgsArgs_.empty()) { 1994514f5e3Sopenharmony_ci return nullptr; 2004514f5e3Sopenharmony_ci } 2014514f5e3Sopenharmony_ci return pkgsArgs_.at(mainPkgName_); 2024514f5e3Sopenharmony_ci } 2034514f5e3Sopenharmony_ci 2044514f5e3Sopenharmony_ci const std::map<std::string, std::shared_ptr<OhosPkgArgs>> &GetPkgsArgs() const 2054514f5e3Sopenharmony_ci { 2064514f5e3Sopenharmony_ci return pkgsArgs_; 2074514f5e3Sopenharmony_ci } 2084514f5e3Sopenharmony_ci CallMethodFlagMap *GetCallMethodFlagMap() 2094514f5e3Sopenharmony_ci { 2104514f5e3Sopenharmony_ci return &callMethodFlagMap_; 2114514f5e3Sopenharmony_ci } 2124514f5e3Sopenharmony_ci 2134514f5e3Sopenharmony_ci static std::string GetHelper() 2144514f5e3Sopenharmony_ci { 2154514f5e3Sopenharmony_ci std::string str; 2164514f5e3Sopenharmony_ci str.append(COMPILER_HELP_HEAD_MSG); 2174514f5e3Sopenharmony_ci str.append(HELP_OPTION_MSG); 2184514f5e3Sopenharmony_ci return str; 2194514f5e3Sopenharmony_ci } 2204514f5e3Sopenharmony_ci 2214514f5e3Sopenharmony_ciprivate: 2224514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(AotCompilerPreprocessor); 2234514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(AotCompilerPreprocessor); 2244514f5e3Sopenharmony_ci void HandleTargetModeInfo(CompilationOptions &cOptions); 2254514f5e3Sopenharmony_ci 2264514f5e3Sopenharmony_ci std::shared_ptr<JSPandaFile> CreateAndVerifyJSPandaFile(const std::string &fileName); 2274514f5e3Sopenharmony_ci 2284514f5e3Sopenharmony_ci void ResolveModule(const JSPandaFile *jsPandaFile, const std::string &fileName); 2294514f5e3Sopenharmony_ci 2304514f5e3Sopenharmony_ci bool OutCompiledMethodsRange() const 2314514f5e3Sopenharmony_ci { 2324514f5e3Sopenharmony_ci static uint32_t compiledMethodsCount = 0; 2334514f5e3Sopenharmony_ci ++compiledMethodsCount; 2344514f5e3Sopenharmony_ci return compiledMethodsCount < runtimeOptions_.GetCompilerMethodsRange().first || 2354514f5e3Sopenharmony_ci runtimeOptions_.GetCompilerMethodsRange().second <= compiledMethodsCount; 2364514f5e3Sopenharmony_ci } 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_ci EcmaVM *vm_; 2394514f5e3Sopenharmony_ci JSRuntimeOptions &runtimeOptions_; 2404514f5e3Sopenharmony_ci std::map<std::string, std::shared_ptr<OhosPkgArgs>> &pkgsArgs_; 2414514f5e3Sopenharmony_ci std::string mainPkgName_; 2424514f5e3Sopenharmony_ci PGOProfilerDecoder &profilerDecoder_; 2434514f5e3Sopenharmony_ci arg_list_t &pandaFileNames_; 2444514f5e3Sopenharmony_ci CVector<AbcFileInfo> fileInfos_; 2454514f5e3Sopenharmony_ci CVector<std::unique_ptr<BytecodeInfoCollector>> bcInfoCollectors_; 2464514f5e3Sopenharmony_ci CallMethodFlagMap callMethodFlagMap_; 2474514f5e3Sopenharmony_ci AOTCompilationEnv aotCompilationEnv_; 2484514f5e3Sopenharmony_ci CVector<std::string> irreducibleMethods_; 2494514f5e3Sopenharmony_ci friend class OhosPkgArgs; 2504514f5e3Sopenharmony_ci}; 2514514f5e3Sopenharmony_ci} // namespace panda::ecmascript::kungfu 2524514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_AOT_COMPILER_PREPROCESSOR_H 253