1 /*
2 * Copyright (c) 2021 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
17 #include "ecmascript/checkpoint/thread_state_transition.h"
18 #include "ecmascript/compiler/pass_manager.h"
19 #include "ecmascript/log_wrapper.h"
20 #include "ecmascript/ohos/ohos_pkg_verifier.h"
21 #include "ecmascript/platform/aot_crash_info.h"
22
23
24 namespace panda::ecmascript::kungfu {
25 namespace {
26 /**
27 * @param ErrCode return code of ark_aot_compiler
28 * @attention it must sync with RetStatusOfCompiler of
29 * "ets_runtime/compiler_service/include/aot_compiler_constants.h"
30 */
31 enum ErrCode {
32 ERR_OK = (0), // IMPORTANT: Only if aot compiler SUCCESS and save an/ai SUCCESS, return ERR_OK.
33 ERR_FAIL = (-1),
34 ERR_HELP = (1),
35 ERR_NO_AP = (2),
36 ERR_MERGE_AP = (3),
37 ERR_CHECK_VERSION = (4),
38 ERR_AN_EMPTY = (5),
39 ERR_AN_FAIL = (6),
40 ERR_AI_FAIL = (7),
41 };
42
CheckVersion(JSRuntimeOptions & runtimeOptions,AotCompilerStats & compilerStats,bool isPgoMerged)43 bool CheckVersion(JSRuntimeOptions& runtimeOptions, AotCompilerStats& compilerStats, bool isPgoMerged)
44 {
45 if (runtimeOptions.IsCheckPgoVersion()) {
46 if (!isPgoMerged) {
47 LOG_COMPILER(ERROR) << "CheckVersion ap and abc may not match";
48 compilerStats.SetPgoFileLegal(false);
49 }
50 if (runtimeOptions.IsTargetCompilerMode()) {
51 compilerStats.PrintCompilerStatsLog();
52 }
53 return true;
54 }
55 return false;
56 }
57
58 } // namespace
59
Main(const int argc,const char ** argv)60 int Main(const int argc, const char **argv)
61 {
62 if (argc < 2) { // 2: at least have two arguments
63 LOG_COMPILER(ERROR) << AotCompilerPreprocessor::GetHelper();
64 return ERR_FAIL;
65 }
66
67 JSRuntimeOptions runtimeOptions;
68 bool retOpt = runtimeOptions.ParseCommand(argc, argv);
69 if (!retOpt) {
70 LOG_COMPILER(ERROR) << AotCompilerPreprocessor::GetHelper();
71 return ERR_HELP;
72 }
73
74 bool ret = true;
75 // ark_aot_compiler running need disable asm interpreter to disable the loading of AOT files.
76 runtimeOptions.SetEnableAsmInterpreter(false);
77 runtimeOptions.SetOptionsForTargetCompilation();
78 EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
79 if (vm == nullptr) {
80 LOG_COMPILER(ERROR) << "Cannot Create vm";
81 return ERR_FAIL;
82 }
83
84 {
85 AOTCompilationEnv aotCompilationEnv(vm);
86 ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
87 LocalScope scope(vm);
88 arg_list_t pandaFileNames {};
89 std::map<std::string, std::shared_ptr<OhosPkgArgs>> pkgArgsMap;
90 CompilationOptions cOptions(runtimeOptions);
91
92 CompilerLog log(cOptions.logOption_);
93 log.SetEnableCompilerLogTime(cOptions.compilerLogTime_);
94 AotMethodLogList logList(cOptions.logMethodsList_);
95 PGOProfilerDecoder profilerDecoder;
96
97 AotCompilerPreprocessor cPreprocessor(vm, runtimeOptions, pkgArgsMap, profilerDecoder, pandaFileNames);
98 if (!cPreprocessor.HandleTargetCompilerMode(cOptions) || !cPreprocessor.HandlePandaFileNames(argc, argv)) {
99 return ERR_HELP;
100 }
101
102 // Need to verify package information to prevent abnormal input of information
103 if (!ohos::OhosPkgVerifier::VerifyPkgInfo(cPreprocessor, cOptions)) {
104 LOG_COMPILER(ERROR) << "hap verify wrong";
105 return ERR_FAIL;
106 }
107
108 if (runtimeOptions.IsPartialCompilerMode() && cOptions.profilerIn_.empty()) {
109 // no need to compile in partial mode without any ap files.
110 return ERR_NO_AP;
111 }
112
113 AotCompilerStats compilerStats;
114 std::string bundleName = "";
115 if (cPreprocessor.GetMainPkgArgs()) {
116 bundleName = cPreprocessor.GetMainPkgArgs()->GetBundleName();
117 }
118 compilerStats.SetBundleName(bundleName);
119 compilerStats.SetAotFilePath(cOptions.outputFileName_);
120 compilerStats.SetPgoPath(cOptions.profilerIn_);
121 compilerStats.StartCompiler();
122 profilerDecoder.SetHotnessThreshold(cOptions.hotnessThreshold_);
123 profilerDecoder.SetInPath(cOptions.profilerIn_);
124 cPreprocessor.AOTInitialize();
125 std::unordered_map<CString, uint32_t> fileNameToChecksumMap;
126 cPreprocessor.GenerateAbcFileInfos(fileNameToChecksumMap);
127
128 if (runtimeOptions.IsTargetCompilerMode() && (cPreprocessor.HasExistsAOTFiles(cOptions) ||
129 cPreprocessor.HasPreloadAotFile())) {
130 LOG_COMPILER(ERROR) << "The AOT file already exists and will not be compiled anymore";
131 return ERR_OK;
132 }
133 ret = cPreprocessor.GetCompilerResult();
134 // Notice: lx move load pandaFileHead and verify before GeneralAbcFileInfos.
135 // need support multiple abc
136 auto isPgoMerged = cPreprocessor.HandleMergedPgoFile(fileNameToChecksumMap);
137 if (CheckVersion(runtimeOptions, compilerStats, isPgoMerged)) {
138 return ERR_CHECK_VERSION;
139 }
140 std::string appSignature = cPreprocessor.GetMainPkgArgsAppSignature();
141 if (!isPgoMerged) {
142 AOTFileGenerator::SaveEmptyAOTFile(
143 cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AN, appSignature, true);
144 AOTFileGenerator::SaveEmptyAOTFile(
145 cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AI, appSignature, false);
146 return ERR_MERGE_AP;
147 }
148 cPreprocessor.Process(cOptions);
149
150 PassOptions::Builder optionsBuilder;
151 PassOptions passOptions =
152 optionsBuilder.EnableArrayBoundsCheckElimination(cOptions.isEnableArrayBoundsCheckElimination_)
153 .EnableTypeLowering(cOptions.isEnableTypeLowering_)
154 .EnableEarlyElimination(cOptions.isEnableEarlyElimination_)
155 .EnableLaterElimination(cOptions.isEnableLaterElimination_)
156 .EnableValueNumbering(cOptions.isEnableValueNumbering_)
157 .EnableOptInlining(cOptions.isEnableOptInlining_)
158 .EnableOptString(cOptions.isEnableOptString_)
159 .EnableOptPGOType(cOptions.isEnableOptPGOType_)
160 .EnableOptTrackField(cOptions.isEnableOptTrackField_)
161 .EnableOptLoopPeeling(cOptions.isEnableOptLoopPeeling_)
162 .EnableOptLoopInvariantCodeMotion(cOptions.isEnableOptLoopInvariantCodeMotion_)
163 .EnableOptConstantFolding(cOptions.isEnableOptConstantFolding_)
164 .EnableLexenvSpecialization(cOptions.isEnableLexenvSpecialization_)
165 .EnableInlineNative(cOptions.isEnableNativeInline_)
166 .EnableLoweringBuiltin(cOptions.isEnableLoweringBuiltin_)
167 .EnableOptBranchProfiling(cOptions.isEnableOptBranchProfiling_)
168 .EnableEscapeAnalysis(cOptions.isEnableEscapeAnalysis_)
169 .EnableInductionVariableAnalysis(cOptions.isEnableInductionVariableAnalysis_)
170 .EnableVerifierPass(cOptions.isEnableVerifierPass_)
171 .Build();
172
173 PassManager passManager(&aotCompilationEnv,
174 cOptions.triple_,
175 cOptions.optLevel_,
176 cOptions.relocMode_,
177 &log,
178 &logList,
179 cOptions.maxAotMethodSize_,
180 cOptions.maxMethodsInModule_,
181 profilerDecoder,
182 &passOptions,
183 cPreprocessor.GetCallMethodFlagMap(),
184 cPreprocessor.GetAbcFileInfo(),
185 cPreprocessor.GetBcInfoCollectors(),
186 cOptions.optBCRange_);
187
188 bool isEnableLiteCG = runtimeOptions.IsCompilerEnableLiteCG();
189 compilerStats.SetIsLiteCg(isEnableLiteCG);
190
191 AOTFileGenerator generator(&log, &logList, &aotCompilationEnv, cOptions.triple_, isEnableLiteCG,
192 cOptions.anFileMaxByteSize_);
193 if (runtimeOptions.IsTargetCompilerMode() && runtimeOptions.IsEnableAotCodeComment()) {
194 if (!generator.CreateAOTCodeCommentFile(cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AN)) {
195 LOG_COMPILER(ERROR) << "Generate aot code comment file failed.";
196 }
197 }
198 passManager.CompileValidFiles(generator, ret, compilerStats);
199 if (compilerStats.GetCompilerMethodCount() == 0) {
200 return runtimeOptions.IsPartialCompilerMode() ? ERR_AN_EMPTY : ERR_OK;
201 }
202 if (!generator.SaveAOTFile(cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AN, appSignature,
203 fileNameToChecksumMap)) {
204 return ERR_AN_FAIL;
205 }
206 if (!generator.SaveSnapshotFile()) {
207 return ERR_AI_FAIL;
208 }
209 log.Print();
210 if (runtimeOptions.IsTargetCompilerMode()) {
211 compilerStats.PrintCompilerStatsLog();
212 }
213 }
214
215 if (vm->GetJSOptions().IsEnableCompilerLogSnapshot()) {
216 vm->PrintAOTSnapShotStats();
217 }
218 LOG_COMPILER(INFO) << (ret ? "ts aot compile success" : "ts aot compile failed");
219 JSNApi::DestroyJSVM(vm);
220 return ret ? ERR_OK : ERR_FAIL;
221 }
222 } // namespace panda::ecmascript::kungfu
223
main(const int argc,const char ** argv)224 int main(const int argc, const char **argv)
225 {
226 auto result = panda::ecmascript::kungfu::Main(argc, argv);
227 return result;
228 }
229