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 #include "ecmascript/compiler/aot_compiler_preprocessor.h"
16
17 #include "ecmascript/compiler/bytecode_circuit_builder.h"
18 #include "ecmascript/compiler/pgo_type/pgo_type_parser.h"
19 #include "ecmascript/compiler/pass_manager.h"
20 #include "ecmascript/jspandafile/program_object.h"
21 #include "ecmascript/js_runtime_options.h"
22 #include "ecmascript/module/js_shared_module_manager.h"
23 #include "ecmascript/module/js_module_manager.h"
24 #include "ecmascript/ohos/ohos_pgo_processor.h"
25 #include "ecmascript/ohos/ohos_pkg_args.h"
26
27 namespace panda::ecmascript::kungfu {
28
29 using PGOProfilerManager = pgo::PGOProfilerManager;
30
CompilationOptions(JSRuntimeOptions & runtimeOptions)31 CompilationOptions::CompilationOptions(JSRuntimeOptions &runtimeOptions)
32 {
33 triple_ = runtimeOptions.GetTargetTriple();
34 if (runtimeOptions.GetAOTOutputFile().empty()) {
35 runtimeOptions.SetAOTOutputFile("aot_file");
36 }
37 outputFileName_ = runtimeOptions.GetAOTOutputFile();
38 optLevel_ = runtimeOptions.GetOptLevel();
39 relocMode_ = runtimeOptions.GetRelocMode();
40 logOption_ = runtimeOptions.GetCompilerLogOption();
41 logMethodsList_ = runtimeOptions.GetMethodsListForLog();
42 compilerLogTime_ = runtimeOptions.IsEnableCompilerLogTime();
43 deviceIsScreenOff_ = runtimeOptions.GetDeviceState();
44 maxAotMethodSize_ = runtimeOptions.GetMaxAotMethodSize();
45 maxMethodsInModule_ = runtimeOptions.GetCompilerModuleMethods();
46 hotnessThreshold_ = runtimeOptions.GetPGOHotnessThreshold();
47 profilerIn_ = std::string(runtimeOptions.GetPGOProfilerPath());
48 needMerge_ = false;
49 isEnableArrayBoundsCheckElimination_ = runtimeOptions.IsEnableArrayBoundsCheckElimination();
50 isEnableTypeLowering_ = runtimeOptions.IsEnableTypeLowering();
51 isEnableEarlyElimination_ = runtimeOptions.IsEnableEarlyElimination();
52 isEnableEmptyCatchFunction_ = runtimeOptions.IsEnableEmptyCatchFunction();
53 isEnableLaterElimination_ = runtimeOptions.IsEnableLaterElimination();
54 isEnableValueNumbering_ = runtimeOptions.IsEnableValueNumbering();
55 isEnableOptInlining_ = runtimeOptions.IsEnableOptInlining();
56 isEnableOptString_ = runtimeOptions.IsEnableOptString();
57 isEnableOptPGOType_ = runtimeOptions.IsEnableOptPGOType();
58 isEnableOptTrackField_ = runtimeOptions.IsEnableOptTrackField();
59 isEnableOptLoopPeeling_ = runtimeOptions.IsEnableOptLoopPeeling();
60 isEnableOptLoopInvariantCodeMotion_ = runtimeOptions.IsEnableOptLoopInvariantCodeMotion();
61 isEnableOptConstantFolding_ = runtimeOptions.IsEnableOptConstantFolding();
62 isEnableLexenvSpecialization_ = runtimeOptions.IsEnableLexenvSpecialization();
63 isEnableNativeInline_ = runtimeOptions.IsEnableNativeInline();
64 isEnableLoweringBuiltin_ = runtimeOptions.IsEnableLoweringBuiltin();
65 isEnableOptBranchProfiling_ = runtimeOptions.IsEnableBranchProfiling();
66 optBCRange_ = runtimeOptions.GetOptCodeRange();
67 isEnableEscapeAnalysis_ = runtimeOptions.IsEnableEscapeAnalysis();
68 isEnableVerifierPass_ = !runtimeOptions.IsTargetCompilerMode();
69 isEnableInductionVariableAnalysis_ = runtimeOptions.IsEnableInductionVariableAnalysis();
70 isEnableBaselinePgo_ = runtimeOptions.IsEnableBaselinePgo();
71 std::string optionSelectMethods = runtimeOptions.GetCompilerSelectMethods();
72 std::string optionSkipMethods = runtimeOptions.GetCompilerSkipMethods();
73 if (!optionSelectMethods.empty() && !optionSkipMethods.empty()) {
74 LOG_COMPILER(FATAL) <<
75 "--compiler-select-methods and --compiler-skip-methods should not be set at the same time";
76 }
77 if (!optionSelectMethods.empty()) {
78 ParseOption(optionSelectMethods, optionSelectMethods_);
79 }
80 if (!optionSkipMethods.empty()) {
81 ParseOption(optionSkipMethods, optionSkipMethods_);
82 }
83 }
84
SplitString(const std::string & str,const char ch) const85 std::vector<std::string> CompilationOptions::SplitString(const std::string &str, const char ch) const
86 {
87 std::vector<std::string> vec {};
88 std::istringstream sstr(str.c_str());
89 std::string split;
90 while (getline(sstr, split, ch)) {
91 vec.emplace_back(split);
92 }
93 return vec;
94 }
95
ParseOption(const std::string & option,std::map<std::string,std::vector<std::string>> & optionMap) const96 void CompilationOptions::ParseOption(const std::string &option,
97 std::map<std::string, std::vector<std::string>> &optionMap) const
98 {
99 const char colon = ':';
100 const char comma = ',';
101 std::string str = option;
102 size_t posColon = 0;
103 size_t posComma = 0;
104 do {
105 posColon = str.find_last_of(colon);
106 std::string methodNameList = str.substr(posColon + 1, str.size());
107 std::vector<std::string> methodNameVec = SplitString(methodNameList, comma);
108 str = str.substr(0, posColon);
109 posComma = str.find_last_of(comma);
110 std::string recordName = str.substr(posComma + 1, str.size());
111 str = str.substr(0, posComma);
112 optionMap[recordName] = methodNameVec;
113 } while (posComma != std::string::npos);
114 }
115
HandleTargetCompilerMode(CompilationOptions & cOptions)116 bool AotCompilerPreprocessor::HandleTargetCompilerMode(CompilationOptions &cOptions)
117 {
118 if (runtimeOptions_.IsTargetCompilerMode()) {
119 if (!OhosPkgArgs::ParseArgs(*this, cOptions)) {
120 LOG_COMPILER(ERROR) << GetHelper();
121 LOG_COMPILER(ERROR) << "Parse pkg info failed, exit.";
122 return false;
123 }
124 const auto& mainPkgArgs = GetMainPkgArgs();
125 if (!mainPkgArgs) {
126 LOG_COMPILER(ERROR) << "No main pkg args found, exit";
127 return false;
128 }
129 if (!OhosPgoProcessor::MergeAndRemoveRuntimeAp(cOptions, mainPkgArgs)) {
130 LOG_COMPILER(ERROR) << "Fusion runtime ap failed, exit";
131 return false;
132 }
133 HandleTargetModeInfo(cOptions);
134 }
135 return true;
136 }
137
HandleTargetModeInfo(CompilationOptions & cOptions)138 void AotCompilerPreprocessor::HandleTargetModeInfo(CompilationOptions &cOptions)
139 {
140 JSRuntimeOptions &vmOpt = vm_->GetJSOptions();
141 ASSERT(vmOpt.IsTargetCompilerMode());
142 // target need fast compiler mode
143 vmOpt.SetFastAOTCompileMode(true);
144 vmOpt.SetOptLevel(DEFAULT_OPT_LEVEL);
145 cOptions.optLevel_ = DEFAULT_OPT_LEVEL;
146 cOptions.isEnableOptTrackField_ = false;
147 cOptions.isEnableLoweringBuiltin_ = false;
148 }
149
MethodHasTryCatch(const JSPandaFile * jsPandaFile,const MethodLiteral * methodLiteral) const150 bool AotCompilerPreprocessor::MethodHasTryCatch(const JSPandaFile *jsPandaFile,
151 const MethodLiteral *methodLiteral) const
152 {
153 if (jsPandaFile == nullptr || methodLiteral == nullptr) {
154 return false;
155 }
156 auto pf = jsPandaFile->GetPandaFile();
157 panda_file::MethodDataAccessor mda(*pf, methodLiteral->GetMethodId());
158 panda_file::CodeDataAccessor cda(*pf, mda.GetCodeId().value());
159 return cda.GetTriesSize() != 0;
160 }
161
HandlePandaFileNames(const int argc,const char ** argv)162 bool AotCompilerPreprocessor::HandlePandaFileNames(const int argc, const char **argv)
163 {
164 if (runtimeOptions_.GetCompilerPkgJsonInfo().empty() || pkgsArgs_.empty()) {
165 // if no pkgArgs, last param must be abc file
166 std::string files = argv[argc - 1];
167 if (!base::StringHelper::EndsWith(files, ".abc")) {
168 LOG_COMPILER(ERROR) << "The last argument must be abc file" << std::endl;
169 LOG_COMPILER(ERROR) << GetHelper();
170 return false;
171 }
172 std::string delimiter = GetFileDelimiter();
173 pandaFileNames_ = base::StringHelper::SplitString(files, delimiter);
174 }
175 return true;
176 }
177
AOTInitialize()178 void AotCompilerPreprocessor::AOTInitialize()
179 {
180 BytecodeStubCSigns::Initialize();
181 CommonStubCSigns::Initialize();
182 BuiltinsStubCSigns::Initialize();
183 RuntimeStubCSigns::Initialize();
184 }
185
Process(CompilationOptions & cOptions)186 void AotCompilerPreprocessor::Process(CompilationOptions &cOptions)
187 {
188 GenerateBytecodeInfoCollectors(cOptions);
189 GeneratePGOTypes();
190 SnapshotInitialize();
191 DoPreAnalysis(cOptions);
192 GenerateMethodMap(cOptions);
193 }
194
DoPreAnalysis(CompilationOptions & cOptions)195 void AotCompilerPreprocessor::DoPreAnalysis(CompilationOptions &cOptions)
196 {
197 for (uint32_t i = 0 ; i < fileInfos_.size(); ++i) {
198 JSPandaFile *jsPandaFile = fileInfos_[i].jsPandaFile_.get();
199 auto &collector = *bcInfoCollectors_[i];
200 AnalyzeGraphs(jsPandaFile, collector, cOptions);
201 }
202 }
203
AnalyzeGraphs(JSPandaFile * jsPandaFile,BytecodeInfoCollector & collector,CompilationOptions & cOptions)204 void AotCompilerPreprocessor::AnalyzeGraphs(JSPandaFile *jsPandaFile,
205 BytecodeInfoCollector &collector, CompilationOptions &cOptions)
206 {
207 if (jsPandaFile == nullptr) {
208 return;
209 }
210 auto &bytecodeInfo = collector.GetBytecodeInfo();
211 auto &methodPcInfos = bytecodeInfo.GetMethodPcInfos();
212 for (auto &[methodId, methodInfo] : bytecodeInfo.GetMethodList()) {
213 auto methodLiteral = jsPandaFile->FindMethodLiteral(methodId);
214 auto &methodPcInfo = methodPcInfos[methodInfo.GetMethodPcInfoIndex()];
215 if (MethodHasTryCatch(jsPandaFile, methodLiteral)) {
216 AnalyzeGraph(bytecodeInfo, cOptions, collector, methodLiteral, methodPcInfo);
217 }
218 }
219 }
220
AnalyzeGraph(BCInfo & bytecodeInfo,CompilationOptions & cOptions,BytecodeInfoCollector & collector,MethodLiteral * methodLiteral,MethodPcInfo & methodPCInfo)221 void AotCompilerPreprocessor::AnalyzeGraph(BCInfo &bytecodeInfo, CompilationOptions &cOptions,
222 BytecodeInfoCollector &collector, MethodLiteral *methodLiteral,
223 MethodPcInfo &methodPCInfo)
224 {
225 if (methodLiteral == nullptr) {
226 return;
227 }
228 AotMethodLogList logList(cOptions.logMethodsList_);
229 AOTCompilationEnv aotCompilationEnv(vm_);
230 CompilerLog log(cOptions.logOption_);
231 AOTFileGenerator generator(&log, &logList, &aotCompilationEnv, cOptions.triple_, false);
232 PGOProfilerDecoder profilerDecoder;
233 LOptions lOptions = LOptions(DEFAULT_OPT_LEVEL, FPFlag::RESERVE_FP, DEFAULT_REL_MODE);
234 Module *m = generator.AddModule("", cOptions.triple_, lOptions, false);
235 PassContext ctx(cOptions.triple_, &log, &collector, m->GetModule(), &profilerDecoder);
236 auto jsPandaFile = ctx.GetJSPandaFile();
237 auto cmpCfg = ctx.GetCompilerConfig();
238 auto module = m->GetModule();
239 std::string fullName = module->GetFuncName(methodLiteral, jsPandaFile);
240
241 Circuit circuit(vm_->GetNativeAreaAllocator(), ctx.GetAOTModule()->GetDebugInfo(),
242 fullName.c_str(), cmpCfg->Is64Bit(), FrameType::OPTIMIZED_JS_FUNCTION_FRAME);
243
244 PGOProfilerDecoder defDecoder;
245 PGOProfilerDecoder *decoder = cOptions.isEnableOptPGOType_ ? &profilerDecoder : &defDecoder;
246
247 BytecodeCircuitBuilder builder(jsPandaFile, methodLiteral, methodPCInfo, &circuit,
248 ctx.GetByteCodes(), false,
249 cOptions.isEnableTypeLowering_,
250 fullName, bytecodeInfo.GetRecordNameWithIndex(0), decoder, false);
251 {
252 builder.SetPreAnalysis();
253 builder.BytecodeToCircuit();
254 if (builder.HasEmptyCatchBB()) {
255 emptyCatchBBMethods_.push_back(fullName);
256 }
257 if (builder.HasIrreducibleLoop()) {
258 irreducibleMethods_.push_back(fullName);
259 }
260 }
261 m->DestroyModule();
262 }
263
GenerateAbcFileInfos()264 uint32_t AotCompilerPreprocessor::GenerateAbcFileInfos()
265 {
266 size_t size = pandaFileNames_.size();
267 uint32_t checksum = 0;
268 for (size_t i = 0; i < size; ++i) {
269 const auto &fileName = pandaFileNames_.at(i);
270 auto extendedFilePath = panda::os::file::File::GetExtendedFilePath(fileName);
271 std::shared_ptr<JSPandaFile> jsPandaFile = CreateAndVerifyJSPandaFile(extendedFilePath);
272 AbcFileInfo fileInfo(extendedFilePath, jsPandaFile);
273 if (jsPandaFile == nullptr) {
274 LOG_COMPILER(ERROR) << "Cannot execute panda file '" << extendedFilePath << "'";
275 continue;
276 }
277
278 if (runtimeOptions_.IsTargetCompilerMode()) {
279 if (fileName.compare(mainPkgName_) == 0) {
280 checksum = jsPandaFile->GetChecksum();
281 }
282 } else {
283 checksum = jsPandaFile->GetChecksum();
284 }
285
286 ResolveModule(jsPandaFile.get(), extendedFilePath);
287 fileInfos_.emplace_back(fileInfo);
288 }
289 return checksum;
290 }
291
GenerateBytecodeInfoCollectors(const CompilationOptions & cOptions)292 void AotCompilerPreprocessor::GenerateBytecodeInfoCollectors(const CompilationOptions &cOptions)
293 {
294 bcInfoCollectors_.reserve(fileInfos_.size());
295 for (const AbcFileInfo &fileInfo : fileInfos_) {
296 JSPandaFile *jsPandaFile = fileInfo.jsPandaFile_.get();
297 auto collectorPtr = std::make_unique<BytecodeInfoCollector>(&aotCompilationEnv_, jsPandaFile,
298 profilerDecoder_, cOptions.maxAotMethodSize_);
299 bcInfoCollectors_.emplace_back(std::move(collectorPtr));
300 }
301 }
302
HandleMergedPgoFile(uint32_t checksum)303 bool AotCompilerPreprocessor::HandleMergedPgoFile(uint32_t checksum)
304 {
305 return PGOProfilerManager::MergeApFiles(checksum, profilerDecoder_);
306 }
307
CreateAndVerifyJSPandaFile(const std::string & fileName)308 std::shared_ptr<JSPandaFile> AotCompilerPreprocessor::CreateAndVerifyJSPandaFile(const std::string &fileName)
309 {
310 JSPandaFileManager *jsPandaFileManager = JSPandaFileManager::GetInstance();
311 std::shared_ptr<JSPandaFile> jsPandaFile = nullptr;
312 if (runtimeOptions_.IsTargetCompilerMode()) {
313 auto pkgArgsIter = pkgsArgs_.find(fileName);
314 if (pkgArgsIter == pkgsArgs_.end()) {
315 jsPandaFile = jsPandaFileManager->OpenJSPandaFile(fileName.c_str());
316 } else if (!(pkgArgsIter->second->GetJSPandaFile(runtimeOptions_, jsPandaFile,
317 pkgArgsIter->second->GetPkgFd()))) {
318 return nullptr;
319 }
320 } else {
321 jsPandaFile = jsPandaFileManager->OpenJSPandaFile(fileName.c_str());
322 }
323 if (jsPandaFile == nullptr) {
324 LOG_ECMA(ERROR) << "open file " << fileName << " error";
325 return nullptr;
326 }
327
328 if (!jsPandaFile->IsNewVersion()) {
329 LOG_COMPILER(ERROR) << "AOT only support panda file with new ISA, while the '" <<
330 fileName << "' file is the old version";
331 return nullptr;
332 }
333
334 jsPandaFileManager->AddJSPandaFile(jsPandaFile);
335 return jsPandaFile;
336 }
337
ResolveModule(const JSPandaFile * jsPandaFile,const std::string & fileName)338 void AotCompilerPreprocessor::ResolveModule(const JSPandaFile *jsPandaFile, const std::string &fileName)
339 {
340 const auto &recordInfo = jsPandaFile->GetJSRecordInfo();
341 JSThread *thread = vm_->GetJSThread();
342 SharedModuleManager *sharedModuleManager = SharedModuleManager::GetInstance();
343 [[maybe_unused]] EcmaHandleScope scope(thread);
344 for (auto info: recordInfo) {
345 if (jsPandaFile->IsModule(info.second)) {
346 auto recordName = info.first;
347 sharedModuleManager->ResolveImportedModuleWithMerge(thread, fileName.c_str(), recordName, false);
348 SharedModuleManager::GetInstance()->TransferSModule(thread);
349 }
350 }
351 }
352
GeneratePGOTypes()353 void AotCompilerPreprocessor::GeneratePGOTypes()
354 {
355 PGOTypeManager *ptManager = vm_->GetJSThread()->GetCurrentEcmaContext()->GetPTManager();
356 for (uint32_t i = 0; i < fileInfos_.size(); ++i) {
357 auto& collector = *bcInfoCollectors_[i];
358 PGOTypeParser parser(profilerDecoder_, ptManager);
359 parser.CreatePGOType(collector);
360 }
361 }
362
SnapshotInitialize()363 void AotCompilerPreprocessor::SnapshotInitialize()
364 {
365 PGOTypeManager *ptManager = vm_->GetJSThread()->GetCurrentEcmaContext()->GetPTManager();
366 ptManager->InitAOTSnapshot(fileInfos_.size());
367 }
368
SetIsFastCall(CString fileDesc,uint32_t methodOffset,bool isFastCall)369 void CallMethodFlagMap::SetIsFastCall(CString fileDesc, uint32_t methodOffset, bool isFastCall)
370 {
371 abcIdMethodIdToIsFastCall_[std::pair<CString, uint32_t>(fileDesc, methodOffset)] = isFastCall;
372 }
373
IsFastCall(CString fileDesc,uint32_t methodOffset) const374 bool CallMethodFlagMap::IsFastCall(CString fileDesc, uint32_t methodOffset) const
375 {
376 if (!abcIdMethodIdToIsFastCall_.count(std::pair<CString, uint32_t>(fileDesc, methodOffset))) {
377 return false;
378 }
379 return abcIdMethodIdToIsFastCall_.at(std::pair<CString, uint32_t>(fileDesc, methodOffset));
380 }
381
SetIsAotCompile(CString fileDesc,uint32_t methodOffset,bool isAotCompile)382 void CallMethodFlagMap::SetIsAotCompile(CString fileDesc, uint32_t methodOffset, bool isAotCompile)
383 {
384 abcIdMethodIdToIsAotCompile_[std::pair<CString, uint32_t>(fileDesc, methodOffset)] = isAotCompile;
385 }
386
IsAotCompile(CString fileDesc,uint32_t methodOffset) const387 bool CallMethodFlagMap::IsAotCompile(CString fileDesc, uint32_t methodOffset) const
388 {
389 if (!abcIdMethodIdToIsAotCompile_.count(std::pair<CString, uint32_t>(fileDesc, methodOffset))) {
390 return false;
391 }
392 return abcIdMethodIdToIsAotCompile_.at(std::pair<CString, uint32_t>(fileDesc, methodOffset));
393 }
394
SetIsJitCompile(CString fileDesc,uint32_t methodOffset,bool isAotCompile)395 void CallMethodFlagMap::SetIsJitCompile(CString fileDesc, uint32_t methodOffset, bool isAotCompile)
396 {
397 abcIdMethodIdToIsJitCompile_[std::pair<CString, uint32_t>(fileDesc, methodOffset)] = isAotCompile;
398 }
399
IsJitCompile(CString fileDesc,uint32_t methodOffset) const400 bool CallMethodFlagMap::IsJitCompile(CString fileDesc, uint32_t methodOffset) const
401 {
402 if (!abcIdMethodIdToIsJitCompile_.count(std::pair<CString, uint32_t>(fileDesc, methodOffset))) {
403 return false;
404 }
405 return abcIdMethodIdToIsJitCompile_.at(std::pair<CString, uint32_t>(fileDesc, methodOffset));
406 }
407
408
FilterOption(const std::map<std::string,std::vector<std::string>> & optionMap,const std::string & recordName,const std::string & methodName) const409 bool AotCompilerPreprocessor::FilterOption(const std::map<std::string, std::vector<std::string>> &optionMap,
410 const std::string &recordName, const std::string &methodName) const
411 {
412 if (optionMap.empty()) {
413 return false;
414 }
415
416 auto it = optionMap.find(recordName);
417 if (it == optionMap.end()) {
418 return false;
419 }
420
421 std::vector<std::string> vec = it->second;
422 return find(vec.begin(), vec.end(), methodName) != vec.end();
423 }
424
IsSkipMethod(const JSPandaFile * jsPandaFile,const BCInfo & bytecodeInfo,const CString & recordName,const MethodLiteral * methodLiteral,const MethodPcInfo & methodPCInfo,const std::string & methodName,CompilationOptions & cOptions) const425 bool AotCompilerPreprocessor::IsSkipMethod(const JSPandaFile *jsPandaFile, const BCInfo &bytecodeInfo,
426 const CString &recordName, const MethodLiteral *methodLiteral,
427 const MethodPcInfo &methodPCInfo, const std::string &methodName,
428 CompilationOptions &cOptions) const
429 {
430 if (methodPCInfo.methodsSize > bytecodeInfo.GetMaxMethodSize() ||
431 !profilerDecoder_.Match(jsPandaFile, recordName, methodLiteral->GetMethodId())) {
432 return true;
433 }
434
435 if (OutCompiledMethodsRange()) {
436 return true;
437 }
438
439 if (!cOptions.optionSelectMethods_.empty()) {
440 return !FilterOption(cOptions.optionSelectMethods_, ConvertToStdString(recordName), methodName);
441 } else if (!cOptions.optionSkipMethods_.empty()) {
442 return FilterOption(cOptions.optionSkipMethods_, ConvertToStdString(recordName), methodName);
443 }
444
445 std::string fullName = IRModule::GetFuncName(methodLiteral, jsPandaFile);
446 if (HasSkipMethod(irreducibleMethods_, fullName)) {
447 return true;
448 }
449
450 if (HasSkipMethod(emptyCatchBBMethods_, fullName) && !cOptions.isEnableEmptyCatchFunction_) {
451 return true;
452 }
453
454 return false;
455 }
456
GenerateMethodMap(CompilationOptions & cOptions)457 void AotCompilerPreprocessor::GenerateMethodMap(CompilationOptions &cOptions)
458 {
459 for (uint32_t i = 0; i < fileInfos_.size(); ++i) {
460 JSPandaFile *jsPandaFile = fileInfos_[i].jsPandaFile_.get();
461 auto& collector = *bcInfoCollectors_[i];
462 BCInfo &bytecodeInfo = collector.GetBytecodeInfo();
463 const auto &methodPcInfos = bytecodeInfo.GetMethodPcInfos();
464 auto &methodList = bytecodeInfo.GetMethodList();
465 for (auto it = methodList.begin(); it != methodList.end(); it++) {
466 uint32_t index = it->first;
467 auto &methodInfo = it->second;
468 auto &methodPcInfo = methodPcInfos[methodInfo.GetMethodPcInfoIndex()];
469 auto methodLiteral = jsPandaFile->FindMethodLiteral(index);
470 if (methodLiteral == nullptr) {
471 continue;
472 }
473 auto methodId = methodLiteral->GetMethodId();
474 const std::string methodName(MethodLiteral::GetMethodName(jsPandaFile, methodId));
475 bool isAotcompile = !IsSkipMethod(jsPandaFile, bytecodeInfo, MethodLiteral::GetRecordName(
476 jsPandaFile, EntityId(index)), methodLiteral, methodPcInfo, methodName, cOptions);
477 bool isFastCall = methodLiteral->IsFastCall();
478 CString fileDesc = jsPandaFile->GetNormalizedFileDesc();
479 uint32_t offset = methodId.GetOffset();
480 callMethodFlagMap_.SetIsAotCompile(fileDesc, offset, isAotcompile);
481 callMethodFlagMap_.SetIsFastCall(fileDesc, offset, isFastCall);
482 LOG_COMPILER(INFO) <<"!!!"<< fileDesc <<" "<< offset << " " << isAotcompile << " " << isFastCall;
483 }
484 }
485 }
486
HasSkipMethod(const CVector<std::string> & methodList,const std::string & methodName) const487 bool AotCompilerPreprocessor::HasSkipMethod(const CVector<std::string> &methodList,
488 const std::string &methodName) const
489 {
490 return std::find(methodList.begin(), methodList.end(), methodName) != methodList.end();
491 }
492
GetMainPkgArgsAppSignature() const493 std::string AotCompilerPreprocessor::GetMainPkgArgsAppSignature() const
494 {
495 return GetMainPkgArgs() == nullptr ? "" : GetMainPkgArgs()->GetAppSignature();
496 }
497
ForbidenRebuildAOT(std::string & fileName) const498 bool AotCompilerPreprocessor::ForbidenRebuildAOT(std::string &fileName) const
499 {
500 std::string realPath;
501 if (!RealPath(fileName, realPath, false)) {
502 LOG_COMPILER(ERROR) << "Fail to get realPath: " << fileName;
503 return true;
504 }
505 if (FileExist(realPath.c_str())) {
506 LOG_COMPILER(ERROR) << "AOT file: " << realPath << " exist";
507 return true;
508 }
509 return false;
510 }
511
HasPreloadAotFile() const512 bool AotCompilerPreprocessor::HasPreloadAotFile() const
513 {
514 std::string hapPath;
515 std::string moduleName;
516 if (GetMainPkgArgs()) {
517 hapPath = GetMainPkgArgs()->GetPath();
518 moduleName = GetMainPkgArgs()->GetModuleName();
519 }
520 std::string fileName = OhosPreloadAppInfo::GetPreloadAOTFileName(hapPath, moduleName);
521 std::string aiFileName = fileName + AOTFileManager::FILE_EXTENSION_AI;
522 std::string anFileName = fileName + AOTFileManager::FILE_EXTENSION_AN;
523 bool existsAnFile = ForbidenRebuildAOT(anFileName);
524 bool existsAiFile = ForbidenRebuildAOT(aiFileName);
525 return (existsAnFile || existsAiFile);
526 }
527
HasExistsAOTFiles(CompilationOptions & cOptions) const528 bool AotCompilerPreprocessor::HasExistsAOTFiles(CompilationOptions &cOptions) const
529 {
530 std::string anFileName = cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AN;
531 std::string aiFileName = cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AI;
532 bool existsAnFile = ForbidenRebuildAOT(anFileName);
533 bool existsAiFile = ForbidenRebuildAOT(aiFileName);
534 return (existsAnFile || existsAiFile);
535 }
536 } // namespace panda::ecmascript::kungfu
537