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 #include <chrono>
17 #include <iostream>
18 #include <memory>
19 #include <signal.h> // NOLINTNEXTLINE(modernize-deprecated-headers)
20 #include <vector>
21
22 #include "ecmascript/base/string_helper.h"
23 #include "ecmascript/compiler/aot_compiler_preprocessor.h"
24 #include "ecmascript/compiler/aot_file/aot_file_manager.h"
25 #include "ecmascript/compiler/pass_manager.h"
26 #include "ecmascript/ecma_string.h"
27 #include "ecmascript/js_runtime_options.h"
28 #include "ecmascript/jspandafile/js_pandafile_manager.h"
29 #include "ecmascript/jspandafile/program_object.h"
30 #include "ecmascript/log.h"
31 #include "ecmascript/log_wrapper.h"
32 #include "ecmascript/module/js_module_manager.h"
33 #include "ecmascript/napi/include/jsnapi.h"
34 #include "ecmascript/ohos/ohos_pkg_args.h"
35 #include "ecmascript/platform/file.h"
36
37 namespace panda::ecmascript::kungfu {
38 namespace {
CompileValidFiles(PassManager & passManager,AOTFileGenerator & generator,bool & ret,const CVector<AbcFileInfo> & fileInfos)39 void CompileValidFiles(PassManager &passManager, AOTFileGenerator &generator, bool &ret,
40 const CVector<AbcFileInfo> &fileInfos)
41 {
42 for (const AbcFileInfo &fileInfo : fileInfos) {
43 JSPandaFile *jsPandaFile = fileInfo.jsPandaFile_.get();
44 const std::string &extendedFilePath = fileInfo.extendedFilePath_;
45 LOG_COMPILER(INFO) << "AOT compile: " << extendedFilePath;
46 generator.SetCurrentCompileFileName(jsPandaFile->GetNormalizedFileDesc());
47 if (passManager.Compile(jsPandaFile, extendedFilePath, generator) == false) {
48 ret = false;
49 continue;
50 }
51 }
52 }
53 } // namespace
54
Main(const int argc,const char ** argv)55 int Main(const int argc, const char **argv)
56 {
57 auto startTime =
58 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
59 .count();
60 LOG_ECMA(DEBUG) << "Print ark_aot_compiler received args:";
61 for (int i = 0; i < argc; i++) {
62 LOG_ECMA(DEBUG) << argv[i];
63 }
64
65 if (argc < 2) { // 2: at least have two arguments
66 LOG_COMPILER(ERROR) << AotCompilerPreprocessor::GetHelper();
67 return -1;
68 }
69
70 JSRuntimeOptions runtimeOptions;
71 bool retOpt = runtimeOptions.ParseCommand(argc, argv);
72 if (!retOpt) {
73 LOG_COMPILER(ERROR) << AotCompilerPreprocessor::GetHelper();
74 return 1;
75 }
76
77 if (runtimeOptions.IsStartupTime()) {
78 LOG_COMPILER(DEBUG) << "Startup start time: " << startTime;
79 }
80
81 bool ret = true;
82 // ark_aot_compiler running need disable asm interpreter to disable the loading of AOT files.
83 runtimeOptions.SetEnableAsmInterpreter(false);
84 runtimeOptions.DisableReportModuleResolvingFailure();
85 runtimeOptions.SetOptionsForTargetCompilation();
86 EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
87 if (vm == nullptr) {
88 LOG_COMPILER(ERROR) << "Cannot Create vm";
89 return -1;
90 }
91
92 {
93 LocalScope scope(vm);
94 arg_list_t pandaFileNames {};
95 std::map<std::string, std::shared_ptr<OhosPkgArgs>> pkgArgsMap;
96 CompilationOptions cOptions(vm, runtimeOptions);
97
98 CompilerLog log(cOptions.logOption_);
99 log.SetEnableCompilerLogTime(cOptions.compilerLogTime_);
100 AotMethodLogList logList(cOptions.logMethodsList_);
101 PGOProfilerDecoder profilerDecoder;
102
103 AotCompilerPreprocessor cPreprocessor(vm, runtimeOptions, pkgArgsMap, profilerDecoder, pandaFileNames);
104 if (!cPreprocessor.HandleTargetCompilerMode(cOptions) || !cPreprocessor.HandlePandaFileNames(argc, argv)) {
105 return 1;
106 }
107 if (runtimeOptions.IsPartialCompilerMode() && cOptions.profilerIn_.empty()) {
108 // no need to compile in partial mode without any ap files.
109 return 0;
110 }
111 profilerDecoder.SetHotnessThreshold(cOptions.hotnessThreshold_);
112 profilerDecoder.SetInPath(cOptions.profilerIn_);
113 cPreprocessor.AOTInitialize();
114 cPreprocessor.SetShouldCollectLiteralInfo(cOptions, &log);
115 if (!cPreprocessor.GenerateAbcFileInfos()) {
116 return 1;
117 }
118 cPreprocessor.GenerateGlobalTypes(cOptions);
119 cPreprocessor.GeneratePGOTypes(cOptions);
120 cPreprocessor.SnapshotInitialize();
121 ret = cPreprocessor.GetCompilerResult();
122
123 PassOptions::Builder optionsBuilder;
124 PassOptions passOptions =
125 optionsBuilder.EnableArrayBoundsCheckElimination(cOptions.isEnableArrayBoundsCheckElimination_)
126 .EnableTypeLowering(cOptions.isEnableTypeLowering_)
127 .EnableEarlyElimination(cOptions.isEnableEarlyElimination_)
128 .EnableLaterElimination(cOptions.isEnableLaterElimination_)
129 .EnableValueNumbering(cOptions.isEnableValueNumbering_)
130 .EnableTypeInfer(cOptions.isEnableTypeInfer_)
131 .EnableOptInlining(cOptions.isEnableOptInlining_)
132 .EnableOptString(cOptions.isEnableOptString_)
133 .EnableOptPGOType(cOptions.isEnableOptPGOType_)
134 .EnableOptTrackField(cOptions.isEnableOptTrackField_)
135 .EnableOptLoopPeeling(cOptions.isEnableOptLoopPeeling_)
136 .EnableOptLoopInvariantCodeMotion(cOptions.isEnableOptLoopInvariantCodeMotion_)
137 .EnableCollectLiteralInfo(cOptions.isEnableCollectLiteralInfo_)
138 .EnableOptConstantFolding(cOptions.isEnableOptConstantFolding_)
139 .EnableLexenvSpecialization(cOptions.isEnableLexenvSpecialization_)
140 .EnableInlineNative(cOptions.isEnableNativeInline_)
141 .EnableLoweringBuiltin(cOptions.isEnableLoweringBuiltin_)
142 .EnableOptBranchProfiling(cOptions.isEnableOptBranchProfiling_)
143 .Build();
144
145 PassManager passManager(vm,
146 cOptions.triple_,
147 cOptions.optLevel_,
148 cOptions.relocMode_,
149 &log,
150 &logList,
151 cOptions.maxAotMethodSize_,
152 cOptions.maxMethodsInModule_,
153 profilerDecoder,
154 &passOptions);
155
156 bool isEnableLiteCG = runtimeOptions.IsCompilerEnableLiteCG();
157 AOTFileGenerator generator(&log, &logList, vm, cOptions.triple_, isEnableLiteCG);
158 const auto &fileInfos = cPreprocessor.GetAbcFileInfo();
159 CompileValidFiles(passManager, generator, ret, fileInfos);
160 std::string appSignature = cPreprocessor.GetMainPkgArgsAppSignature();
161 generator.SaveAOTFile(cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AN, appSignature);
162 generator.SaveSnapshotFile();
163 log.Print();
164 }
165
166 LOG_COMPILER(INFO) << (ret ? "ts aot compile success" : "ts aot compile failed");
167 JSNApi::DestroyJSVM(vm);
168 return ret ? 0 : -1;
169 }
170 } // namespace panda::ecmascript::kungfu
171
main(const int argc,const char ** argv)172 int main(const int argc, const char **argv)
173 {
174 auto result = panda::ecmascript::kungfu::Main(argc, argv);
175 return result;
176 }
177