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 #include "option.h"
17 #include <iostream>
18 #include <cstring>
19 #include <cctype>
20 #include "driver_options.h"
21 #include "file_utils.h"
22 #include "mpl_logging.h"
23 #include "mpl2mpl_options.h"
24 #include "triple.h"
25
26 namespace maple {
27
28 bool Options::dumpBefore = false;
29 bool Options::dumpAfter = false;
30 std::string Options::dumpPhase = "";
31 std::string Options::dumpFunc = "*";
32 std::string Options::skipPhase;
33 std::string Options::skipFrom;
34 std::string Options::skipAfter;
35 bool Options::quiet = false;
36 bool Options::regNativeFunc = false;
37 bool Options::nativeWrapper = true; // Enabled by default
38 bool Options::inlineWithProfile = false;
39 bool Options::useInline = true; // Enabled by default
40 bool Options::enableIPAClone = true;
41 bool Options::useCrossModuleInline = true; // Enabled by default
42 std::string Options::noInlineFuncList = "";
43 std::string Options::importFileList = "";
44 uint32 Options::inlineSmallFunctionThreshold = 80; // Only for srcLangC, value will be reset later for other srcLang
45 uint32 Options::inlineHotFunctionThreshold = 100; // Only for srcLangC, value will be reset later for other srcLang
46 uint32 Options::inlineRecursiveFunctionThreshold = 15;
47 uint32 Options::inlineDepth = 8;
48 uint32 Options::inlineModuleGrowth = 10;
49 uint32 Options::inlineColdFunctionThreshold = 3;
50 uint32 Options::profileHotCount = 1000;
51 uint32 Options::profileColdCount = 10;
52 uint32 Options::numOfCloneVersions = 2;
53 uint32 Options::numOfImpExprLowBound = 2;
54 uint32 Options::numOfImpExprHighBound = 5;
55 uint32 Options::numOfCallSiteLowBound = 2;
56 uint32 Options::numOfCallSiteUpBound = 10;
57 uint32 Options::numOfConstpropValue = 2;
58 bool Options::profileHotCountSeted = false;
59 bool Options::profileColdCountSeted = false;
60 uint32 Options::profileHotRate = 500000;
61 uint32 Options::profileColdRate = 900000;
62 bool Options::regNativeDynamicOnly = false;
63 std::string Options::staticBindingList;
64 bool Options::usePreg = false;
65 bool Options::mapleLinker = false;
66 bool Options::dumpMuidFile = false;
67 bool Options::emitVtableImpl = false;
68 bool Options::profileGen = false;
69 bool Options::profileUse = false;
70 bool Options::genLMBC = false;
71
72 // Ready to be deleted.
73 bool Options::noRC = false;
74 bool Options::analyzeCtor = true;
75 bool Options::strictNaiveRC = false;
76 bool Options::gcOnly = false;
77 bool Options::bigEndian = false;
78 bool Options::rcOpt1 = true;
79 bool Options::nativeOpt = true;
80 bool Options::optForSize = false;
81 bool Options::O2 = false;
82 bool Options::noDot = false;
83 bool Options::genIRProfile = false;
84 bool Options::profileTest = false;
85 std::string Options::criticalNativeFile = "maple/mrt/codetricks/profile.pv/criticalNative.list";
86 std::string Options::fastNativeFile = "maple/mrt/codetricks/profile.pv/fastNative.list";
87 bool Options::barrier = false;
88 std::string Options::nativeFuncPropertyFile = "maple/mrt/codetricks/native_binding/native_func_property.list";
89 bool Options::mapleLinkerTransformLocal = true;
90 bool Options::partialAot = false;
91 uint32 Options::decoupleInit = 0;
92 uint32 Options::buildApp = kNoDecouple;
93 std::string Options::sourceMuid = "";
94 bool Options::decoupleSuper = false;
95 bool Options::deferredVisit = false;
96 bool Options::deferredVisit2 = false;
97 bool Options::genVtabAndItabForDecouple = false;
98 bool Options::profileFunc = false;
99 uint32 Options::parserOpt = 0;
100 std::string Options::dumpDevirtualList = "";
101 std::string Options::readDevirtualList = "";
102 bool Options::usePreloadedClass = false;
103 std::string Options::profile = "";
104 std::string Options::appPackageName = "";
105 bool Options::profileStaticFields = false;
106 std::string Options::proFileData = "";
107 std::string Options::proFileFuncData = "";
108 std::string Options::proFileClassData = "";
109 std::string Options::classLoaderInvocationList = ""; // maple/mrt/codetricks/profile.pv/classloaderInvocation.list
110 bool Options::dumpClassLoaderInvocation = false;
111 unsigned int Options::warningLevel = 0;
112 bool Options::lazyBinding = false;
113 bool Options::hotFix = false;
114 bool Options::compactMeta = false;
115 bool Options::genPGOReport = false;
116 bool Options::verify = false;
117 uint32 Options::inlineCache = 0;
118 bool Options::checkArrayStore = false;
119 bool Options::noComment = false;
120 bool Options::rmNoUseFunc = true; // default remove no-used static function
121 bool Options::sideEffect = true;
122 bool Options::dumpIPA = false;
123 bool Options::wpaa = false; // whole program alias analysis
124
GetInstance()125 Options &Options::GetInstance()
126 {
127 static Options instance;
128 return instance;
129 }
130
DecideMpl2MplRealLevel() const131 void Options::DecideMpl2MplRealLevel() const
132 {
133 if (opts::mpl2mpl::o2 || opts::mpl2mpl::os) {
134 if (opts::mpl2mpl::os) {
135 optForSize = true;
136 }
137 O2 = true;
138 usePreg = true;
139 }
140 }
141
SolveOptions(bool isDebug) const142 bool Options::SolveOptions(bool isDebug) const
143 {
144 DecideMpl2MplRealLevel();
145
146 for (const auto &opt : mpl2mplCategory.GetEnabledOptions()) {
147 std::string printOpt;
148 if (isDebug) {
149 for (const auto &val : opt->GetRawValues()) {
150 printOpt += opt->GetName() + " " + val + " ";
151 }
152 LogInfo::MapleLogger() << "mpl2mpl options: " << printOpt << '\n';
153 }
154 }
155
156 maplecl::CopyIfEnabled(dumpBefore, opts::mpl2mpl::dumpBefore);
157 maplecl::CopyIfEnabled(dumpAfter, opts::mpl2mpl::dumpAfter);
158 maplecl::CopyIfEnabled(dumpFunc, opts::mpl2mpl::dumpFunc);
159
160 // quiet can be set by verbose option in driver
161 maplecl::CopyIfEnabled(quiet, !opts::verbose, opts::verbose);
162 maplecl::CopyIfEnabled(quiet, opts::mpl2mpl::quiet);
163
164 maplecl::CopyIfEnabled(dumpPhase, opts::mpl2mpl::dumpPhase);
165 maplecl::CopyIfEnabled(skipPhase, opts::mpl2mpl::skipPhase);
166 maplecl::CopyIfEnabled(skipFrom, opts::mpl2mpl::skipFrom);
167 maplecl::CopyIfEnabled(skipAfter, opts::mpl2mpl::skipAfter);
168 maplecl::CopyIfEnabled(regNativeDynamicOnly, opts::mpl2mpl::regnativeDynamicOnly);
169 maplecl::CopyIfEnabled(staticBindingList, opts::mpl2mpl::staticBindingList);
170 maplecl::CopyIfEnabled(regNativeFunc, opts::mpl2mpl::regnativefunc);
171 maplecl::CopyIfEnabled(nativeWrapper, opts::mpl2mpl::nativewrapper);
172 maplecl::CopyIfEnabled(inlineWithProfile, opts::mpl2mpl::inlineWithProfile);
173 maplecl::CopyIfEnabled(useInline, opts::mpl2mpl::inlineOpt);
174 maplecl::CopyIfEnabled(enableIPAClone, opts::mpl2mpl::ipaClone);
175 maplecl::CopyIfEnabled(noInlineFuncList, opts::mpl2mpl::noInlineFunc);
176 maplecl::CopyIfEnabled(importFileList, opts::mpl2mpl::importFileList);
177 maplecl::CopyIfEnabled(numOfCloneVersions, opts::mpl2mpl::numOfCloneVersions);
178 maplecl::CopyIfEnabled(numOfImpExprLowBound, opts::mpl2mpl::numOfImpExprLowBound);
179 maplecl::CopyIfEnabled(numOfImpExprHighBound, opts::mpl2mpl::numOfImpExprHighBound);
180 maplecl::CopyIfEnabled(numOfCallSiteLowBound, opts::mpl2mpl::numOfCallSiteLowBound);
181 maplecl::CopyIfEnabled(numOfCallSiteUpBound, opts::mpl2mpl::numOfCallSiteUpBound);
182 maplecl::CopyIfEnabled(numOfConstpropValue, opts::mpl2mpl::numOfConstpropValue);
183 maplecl::CopyIfEnabled(useCrossModuleInline, opts::mpl2mpl::crossModuleInline);
184 maplecl::CopyIfEnabled(inlineSmallFunctionThreshold, opts::mpl2mpl::inlineSmallFunctionThreshold);
185 maplecl::CopyIfEnabled(inlineHotFunctionThreshold, opts::mpl2mpl::inlineHotFunctionThreshold);
186 maplecl::CopyIfEnabled(inlineRecursiveFunctionThreshold, opts::mpl2mpl::inlineRecursiveFunctionThreshold);
187 maplecl::CopyIfEnabled(inlineDepth, opts::mpl2mpl::inlineDepth);
188 maplecl::CopyIfEnabled(inlineModuleGrowth, opts::mpl2mpl::inlineModuleGrow);
189 maplecl::CopyIfEnabled(inlineColdFunctionThreshold, opts::mpl2mpl::inlineColdFuncThresh);
190 maplecl::CopyIfEnabled(profileHotCount, opts::mpl2mpl::profileHotCount);
191 maplecl::CopyIfEnabled(profileColdCount, opts::mpl2mpl::profileColdCount);
192 maplecl::CopyIfEnabled(profileHotRate, opts::mpl2mpl::profileHotRate);
193 maplecl::CopyIfEnabled(profileColdRate, opts::mpl2mpl::profileColdRate);
194
195 if (opts::mpl2mpl::maplelinker) {
196 mapleLinker = opts::mpl2mpl::maplelinker;
197 dumpMuidFile = mapleLinker; // quiet is overwrited by maplelinker option
198 if (isDebug) {
199 LogInfo::MapleLogger() << "--sub options: dumpMuidFile " << dumpMuidFile << '\n';
200 }
201 }
202
203 maplecl::CopyIfEnabled(dumpMuidFile, opts::mpl2mpl::dumpMuid);
204 maplecl::CopyIfEnabled(emitVtableImpl, opts::mpl2mpl::emitVtableImpl);
205
206 maplecl::CopyIfEnabled(noRC, !opts::mpl2mpl::userc, opts::mpl2mpl::userc);
207 maplecl::CopyIfEnabled(strictNaiveRC, opts::mpl2mpl::strictNaiveRc);
208
209 /* big endian can be set with several options: --target, -Be.
210 * Triple takes to account all these options and allows to detect big endian with IsBigEndian() interface */
211 if (Triple::GetTriple().IsBigEndian()) {
212 bigEndian = true;
213 }
214
215 maplecl::CopyIfEnabled(rcOpt1, opts::mpl2mpl::rcOpt1);
216 maplecl::CopyIfEnabled(nativeOpt, opts::mpl2mpl::nativeopt);
217 maplecl::CopyIfEnabled(criticalNativeFile, opts::mpl2mpl::criticalNative);
218 maplecl::CopyIfEnabled(fastNativeFile, opts::mpl2mpl::fastNative);
219 maplecl::CopyIfEnabled(noDot, opts::mpl2mpl::nodot);
220 maplecl::CopyIfEnabled(genIRProfile, opts::mpl2mpl::genIrProfile);
221 maplecl::CopyIfEnabled(profileTest, opts::mpl2mpl::profileTest);
222 maplecl::CopyIfEnabled(barrier, opts::mpl2mpl::barrier);
223 maplecl::CopyIfEnabled(nativeFuncPropertyFile, opts::mpl2mpl::nativeFuncPropertyFile);
224
225 maplecl::CopyIfEnabled(mapleLinkerTransformLocal, !opts::mpl2mpl::maplelinkerNolocal,
226 opts::mpl2mpl::maplelinkerNolocal);
227 maplecl::CopyIfEnabled(deferredVisit, opts::mpl2mpl::deferredVisit);
228
229 if (opts::mpl2mpl::deferredVisit.IsEnabledByUser()) {
230 deferredVisit = true;
231 deferredVisit2 = true;
232 if (isDebug) {
233 LogInfo::MapleLogger() << "--sub options: deferredVisit " << deferredVisit << '\n';
234 LogInfo::MapleLogger() << "--sub options: deferredVisit2 " << deferredVisit2 << '\n';
235 }
236 }
237
238 maplecl::CopyIfEnabled(decoupleSuper, opts::mpl2mpl::decoupleSuper);
239
240 if (opts::mpl2mpl::buildApp.IsEnabledByUser()) {
241 if (opts::mpl2mpl::buildApp != kMpl2MplLevelZero && opts::mpl2mpl::buildApp != kMpl2MplLevelOne &&
242 opts::mpl2mpl::buildApp != kMpl2MplLevelTwo) {
243 LogInfo::MapleLogger(kLlErr) << "expecting 0,1,2 or empty for --build-app\n";
244 return false;
245 }
246 /* Default buildApp value is 1, so if an User does not select buildApp (with --buildApp=2), it will be 1 */
247 buildApp = opts::mpl2mpl::buildApp;
248 }
249
250 maplecl::CopyIfEnabled(sourceMuid, opts::mpl2mpl::sourceMuid);
251
252 maplecl::CopyIfEnabled(partialAot, opts::mpl2mpl::partialAot);
253
254 if (opts::mpl2mpl::decoupleInit.IsEnabledByUser()) {
255 if (opts::mpl2mpl::decoupleInit != kNoDecouple && opts::mpl2mpl::decoupleInit != kConservativeDecouple &&
256 opts::mpl2mpl::decoupleInit != kAggressiveDecouple) {
257 std::cerr << "expecting 0,1,2 or empty for --decouple-init\n";
258 return false;
259 }
260
261 /* Default opts::mpl2mpl::decoupleInit value is Options::kConservativeDecouple==1
262 * so if an User does not select buildApp (with --decouple-init=2), it will be 1.
263 */
264 decoupleInit = opts::mpl2mpl::decoupleInit;
265 buildApp = Options::kConservativeDecouple;
266 }
267
268 maplecl::CopyIfEnabled(genVtabAndItabForDecouple, opts::mpl2mpl::genDecoupleVtab);
269 maplecl::CopyIfEnabled(profileFunc, opts::mpl2mpl::profileFunc);
270 maplecl::CopyIfEnabled(dumpDevirtualList, opts::mpl2mpl::dumpDevirtual);
271 maplecl::CopyIfEnabled(readDevirtualList, opts::mpl2mpl::readDevirtual);
272 maplecl::CopyIfEnabled(usePreloadedClass, opts::mpl2mpl::usewhiteclass);
273
274 if (opts::profileGen.IsEnabledByUser()) {
275 if (O2) {
276 LogInfo::MapleLogger(kLlErr) << "profileGen requires no optimization\n";
277 return false;
278 } else {
279 profileGen = true;
280 }
281 }
282
283 maplecl::CopyIfEnabled(profileUse, opts::profileUse);
284 maplecl::CopyIfEnabled(genLMBC, opts::genLMBC);
285 maplecl::CopyIfEnabled(appPackageName, opts::mpl2mpl::appPackageName);
286 maplecl::CopyIfEnabled(classLoaderInvocationList, opts::mpl2mpl::checkClInvocation);
287 maplecl::CopyIfEnabled(dumpClassLoaderInvocation, opts::mpl2mpl::dumpClInvocation);
288 maplecl::CopyIfEnabled(warningLevel, opts::mpl2mpl::warning);
289 maplecl::CopyIfEnabled(lazyBinding, opts::mpl2mpl::lazyBinding);
290 maplecl::CopyIfEnabled(hotFix, opts::mpl2mpl::hotFix);
291 maplecl::CopyIfEnabled(compactMeta, opts::mpl2mpl::compactMeta);
292 maplecl::CopyIfEnabled(genPGOReport, opts::mpl2mpl::genPGOReport);
293
294 if (opts::mpl2mpl::inlineCache.IsEnabledByUser()) {
295 if (opts::mpl2mpl::inlineCache != 0 && opts::mpl2mpl::inlineCache != 1 &&
296 opts::mpl2mpl::inlineCache != 2 && opts::mpl2mpl::inlineCache != 3) { // expecting 0, 1, 2, 3
297 LogInfo::MapleLogger(kLlErr) << "expecting 0,1,2,3 or empty for --inlineCache\n";
298 return false;
299 }
300
301 inlineCache = opts::mpl2mpl::inlineCache;
302 }
303
304 maplecl::CopyIfEnabled(noComment, opts::mpl2mpl::noComment);
305 maplecl::CopyIfEnabled(rmNoUseFunc, opts::mpl2mpl::rmnousefunc);
306 maplecl::CopyIfEnabled(sideEffect, opts::mpl2mpl::sideeffect);
307 maplecl::CopyIfEnabled(dumpIPA, opts::mpl2mpl::dumpIPA);
308 maplecl::CopyIfEnabled(wpaa, opts::mpl2mpl::wpaa);
309
310 return true;
311 }
312
ParseOptions(int argc,char ** argv,std::string & fileName) const313 bool Options::ParseOptions(int argc, char **argv, std::string &fileName) const
314 {
315 (void)maplecl::CommandLine::GetCommandLine().Parse(argc, static_cast<char **>(argv), mpl2mplCategory);
316 bool result = SolveOptions(false);
317 if (!result) {
318 return result;
319 }
320
321 auto &badArgs = maplecl::CommandLine::GetCommandLine().badCLArgs;
322 int inputFileCount = 0;
323 for (auto &arg : badArgs) {
324 if (FileUtils::IsFileExists(arg.first)) {
325 inputFileCount++;
326 fileName = arg.first;
327 } else {
328 LogInfo::MapleLogger(kLlErr) << "Unknown Option: " << arg.first;
329 return false;
330 }
331 }
332
333 if (inputFileCount != 1) {
334 LogInfo::MapleLogger(kLlErr) << "expecting one .mpl file as last argument, found: ";
335 for (const auto &optionArg : badArgs) {
336 LogInfo::MapleLogger(kLlErr) << optionArg.first << " ";
337 }
338 LogInfo::MapleLogger(kLlErr) << "\n";
339 return false;
340 }
341
342 #ifdef DEBUG_OPTION
343 LogInfo::MapleLogger() << "mpl file : " << fileName << "\t";
344 DumpOptions();
345 #endif
346
347 return true;
348 }
349
DumpOptions() const350 void Options::DumpOptions() const
351 {
352 LogInfo::MapleLogger() << "phase sequence : \t";
353 if (phaseSeq.empty()) {
354 LogInfo::MapleLogger() << "default phase sequence\n";
355 } else {
356 for (size_t i = 0; i < phaseSeq.size(); ++i) {
357 LogInfo::MapleLogger() << " " << phaseSeq[i];
358 }
359 }
360 LogInfo::MapleLogger() << "\n";
361 }
362 }; // namespace maple
363