1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_DRIVER_COMPILER_OPTIONS_H_
18 #define ART_COMPILER_DRIVER_COMPILER_OPTIONS_H_
19 
20 #include <memory>
21 #include <ostream>
22 #include <string>
23 #include <vector>
24 
25 #include "base/compiler_filter.h"
26 #include "base/globals.h"
27 #include "base/hash_set.h"
28 #include "base/macros.h"
29 #include "base/utils.h"
30 #include "optimizing/register_allocator.h"
31 
32 namespace art {
33 
34 namespace jit {
35 class JitCompiler;
36 }  // namespace jit
37 
38 namespace verifier {
39 class VerifierDepsTest;
40 }  // namespace verifier
41 
42 namespace linker {
43 class Arm64RelativePatcherTest;
44 }  // namespace linker
45 
46 class DexFile;
47 enum class InstructionSet;
48 class InstructionSetFeatures;
49 class ProfileCompilationInfo;
50 class VerificationResults;
51 class VerifiedMethod;
52 
53 // Enum for CheckProfileMethodsCompiled. Outside CompilerOptions so it can be forward-declared.
54 enum class ProfileMethodsCheck : uint8_t {
55   kNone,
56   kLog,
57   kAbort,
58 };
59 
60 class CompilerOptions final {
61  public:
62   // Guide heuristics to determine whether to compile method if profile data not available.
63   static const size_t kDefaultHugeMethodThreshold = 10000;
64   static const size_t kDefaultLargeMethodThreshold = 600;
65   static const size_t kDefaultNumDexMethodsThreshold = 900;
66   static constexpr double kDefaultTopKProfileThreshold = 90.0;
67   static const bool kDefaultGenerateDebugInfo = false;
68   static const bool kDefaultGenerateMiniDebugInfo = true;
69   static const size_t kDefaultInlineMaxCodeUnits = 32;
70   static constexpr size_t kUnsetInlineMaxCodeUnits = -1;
71 
72   enum class CompilerType : uint8_t {
73     kAotCompiler,             // AOT compiler.
74     kJitCompiler,             // Normal JIT compiler.
75     kSharedCodeJitCompiler,   // Zygote JIT producing code in the shared region area, putting
76                               // restrictions on, for example, how literals are being generated.
77   };
78 
79   enum class ImageType : uint8_t {
80     kNone,                    // JIT or AOT app compilation producing only an oat file but no image.
81     kBootImage,               // Creating boot image.
82     kBootImageExtension,      // Creating boot image extension.
83     kAppImage,                // Creating app image.
84   };
85 
86   CompilerOptions();
87   ~CompilerOptions();
88 
GetCompilerFilter()89   CompilerFilter::Filter GetCompilerFilter() const {
90     return compiler_filter_;
91   }
92 
SetCompilerFilter(CompilerFilter::Filter compiler_filter)93   void SetCompilerFilter(CompilerFilter::Filter compiler_filter) {
94     compiler_filter_ = compiler_filter;
95   }
96 
IsAotCompilationEnabled()97   bool IsAotCompilationEnabled() const {
98     return CompilerFilter::IsAotCompilationEnabled(compiler_filter_);
99   }
100 
IsJniCompilationEnabled()101   bool IsJniCompilationEnabled() const {
102     return CompilerFilter::IsJniCompilationEnabled(compiler_filter_);
103   }
104 
IsVerificationEnabled()105   bool IsVerificationEnabled() const {
106     return CompilerFilter::IsVerificationEnabled(compiler_filter_);
107   }
108 
AssumeDexFilesAreVerified()109   bool AssumeDexFilesAreVerified() const {
110     return compiler_filter_ == CompilerFilter::kAssumeVerified;
111   }
112 
AssumeClassesAreVerified()113   bool AssumeClassesAreVerified() const {
114     return compiler_filter_ == CompilerFilter::kAssumeVerified;
115   }
116 
VerifyAtRuntime()117   bool VerifyAtRuntime() const {
118     return compiler_filter_ == CompilerFilter::kExtract;
119   }
120 
IsAnyCompilationEnabled()121   bool IsAnyCompilationEnabled() const {
122     return CompilerFilter::IsAnyCompilationEnabled(compiler_filter_);
123   }
124 
GetHugeMethodThreshold()125   size_t GetHugeMethodThreshold() const {
126     return huge_method_threshold_;
127   }
128 
GetLargeMethodThreshold()129   size_t GetLargeMethodThreshold() const {
130     return large_method_threshold_;
131   }
132 
IsHugeMethod(size_t num_dalvik_instructions)133   bool IsHugeMethod(size_t num_dalvik_instructions) const {
134     return num_dalvik_instructions > huge_method_threshold_;
135   }
136 
IsLargeMethod(size_t num_dalvik_instructions)137   bool IsLargeMethod(size_t num_dalvik_instructions) const {
138     return num_dalvik_instructions > large_method_threshold_;
139   }
140 
GetNumDexMethodsThreshold()141   size_t GetNumDexMethodsThreshold() const {
142     return num_dex_methods_threshold_;
143   }
144 
GetInlineMaxCodeUnits()145   size_t GetInlineMaxCodeUnits() const {
146     return inline_max_code_units_;
147   }
SetInlineMaxCodeUnits(size_t units)148   void SetInlineMaxCodeUnits(size_t units) {
149     inline_max_code_units_ = units;
150   }
151 
GetTopKProfileThreshold()152   double GetTopKProfileThreshold() const {
153     return top_k_profile_threshold_;
154   }
155 
GetDebuggable()156   bool GetDebuggable() const {
157     return debuggable_;
158   }
159 
SetDebuggable(bool value)160   void SetDebuggable(bool value) {
161     debuggable_ = value;
162   }
163 
GetNativeDebuggable()164   bool GetNativeDebuggable() const {
165     return GetDebuggable() && GetGenerateDebugInfo();
166   }
167 
168   // This flag controls whether the compiler collects debugging information.
169   // The other flags control how the information is written to disk.
GenerateAnyDebugInfo()170   bool GenerateAnyDebugInfo() const {
171     return GetGenerateDebugInfo() || GetGenerateMiniDebugInfo();
172   }
173 
GetGenerateDebugInfo()174   bool GetGenerateDebugInfo() const {
175     return generate_debug_info_;
176   }
177 
GetGenerateMiniDebugInfo()178   bool GetGenerateMiniDebugInfo() const {
179     return generate_mini_debug_info_;
180   }
181 
182   // Should run-time checks be emitted in debug mode?
183   bool EmitRunTimeChecksInDebugMode() const;
184 
GetGenerateBuildId()185   bool GetGenerateBuildId() const {
186     return generate_build_id_;
187   }
188 
GetImplicitNullChecks()189   bool GetImplicitNullChecks() const {
190     return implicit_null_checks_;
191   }
192 
GetImplicitStackOverflowChecks()193   bool GetImplicitStackOverflowChecks() const {
194     return implicit_so_checks_;
195   }
196 
IsAotCompiler()197   bool IsAotCompiler() const {
198     return compiler_type_ == CompilerType::kAotCompiler;
199   }
200 
IsJitCompiler()201   bool IsJitCompiler() const {
202     return compiler_type_ == CompilerType::kJitCompiler ||
203            compiler_type_ == CompilerType::kSharedCodeJitCompiler;
204   }
205 
IsJitCompilerForSharedCode()206   bool IsJitCompilerForSharedCode() const {
207     return compiler_type_ == CompilerType::kSharedCodeJitCompiler;
208   }
209 
GetImplicitSuspendChecks()210   bool GetImplicitSuspendChecks() const {
211     return implicit_suspend_checks_;
212   }
213 
IsGeneratingImage()214   bool IsGeneratingImage() const {
215     return IsBootImage() || IsBootImageExtension() || IsAppImage();
216   }
217 
218   // Are we compiling a boot image?
IsBootImage()219   bool IsBootImage() const {
220     return image_type_ == ImageType::kBootImage;
221   }
222 
223   // Are we compiling a boot image extension?
IsBootImageExtension()224   bool IsBootImageExtension() const {
225     return image_type_ == ImageType::kBootImageExtension;
226   }
227 
IsBaseline()228   bool IsBaseline() const {
229     return baseline_;
230   }
231 
232   // Are we compiling an app image?
IsAppImage()233   bool IsAppImage() const {
234     return image_type_ == ImageType::kAppImage;
235   }
236 
237   // Returns whether we are running ART tests.
238   // The compiler will use that information for checking invariants.
CompileArtTest()239   bool CompileArtTest() const {
240     return compile_art_test_;
241   }
242 
243   // Should the code be compiled as position independent?
GetCompilePic()244   bool GetCompilePic() const {
245     return compile_pic_;
246   }
247 
GetProfileCompilationInfo()248   const ProfileCompilationInfo* GetProfileCompilationInfo() const {
249     return profile_compilation_info_;
250   }
251 
HasVerboseMethods()252   bool HasVerboseMethods() const {
253     return !verbose_methods_.empty();
254   }
255 
IsVerboseMethod(const std::string & pretty_method)256   bool IsVerboseMethod(const std::string& pretty_method) const {
257     for (const std::string& cur_method : verbose_methods_) {
258       if (pretty_method.find(cur_method) != std::string::npos) {
259         return true;
260       }
261     }
262     return false;
263   }
264 
GetInitFailureOutput()265   std::ostream* GetInitFailureOutput() const {
266     return init_failure_output_.get();
267   }
268 
AbortOnHardVerifierFailure()269   bool AbortOnHardVerifierFailure() const {
270     return abort_on_hard_verifier_failure_;
271   }
AbortOnSoftVerifierFailure()272   bool AbortOnSoftVerifierFailure() const {
273     return abort_on_soft_verifier_failure_;
274   }
275 
GetInstructionSet()276   InstructionSet GetInstructionSet() const {
277     return instruction_set_;
278   }
279 
GetInstructionSetFeatures()280   const InstructionSetFeatures* GetInstructionSetFeatures() const {
281     return instruction_set_features_.get();
282   }
283 
284 
GetNoInlineFromDexFile()285   const std::vector<const DexFile*>& GetNoInlineFromDexFile() const {
286     return no_inline_from_;
287   }
288 
GetDexFilesForOatFile()289   const std::vector<const DexFile*>& GetDexFilesForOatFile() const {
290     return dex_files_for_oat_file_;
291   }
292 
GetImageClasses()293   const HashSet<std::string>& GetImageClasses() const {
294     return image_classes_;
295   }
296 
297   bool IsImageClass(const char* descriptor) const;
298 
299   const VerificationResults* GetVerificationResults() const;
300 
301   const VerifiedMethod* GetVerifiedMethod(const DexFile* dex_file, uint32_t method_idx) const;
302 
303   // Checks if the specified method has been verified without failures. Returns
304   // false if the method is not in the verification results (GetVerificationResults).
305   bool IsMethodVerifiedWithoutFailures(uint32_t method_idx,
306                                        uint16_t class_def_idx,
307                                        const DexFile& dex_file) const;
308 
309   bool ParseCompilerOptions(const std::vector<std::string>& options,
310                             bool ignore_unrecognized,
311                             std::string* error_msg);
312 
SetNonPic()313   void SetNonPic() {
314     compile_pic_ = false;
315   }
316 
GetDumpCfgFileName()317   const std::string& GetDumpCfgFileName() const {
318     return dump_cfg_file_name_;
319   }
320 
GetDumpCfgAppend()321   bool GetDumpCfgAppend() const {
322     return dump_cfg_append_;
323   }
324 
IsForceDeterminism()325   bool IsForceDeterminism() const {
326     return force_determinism_;
327   }
328 
IsCheckLinkageConditions()329   bool IsCheckLinkageConditions() const {
330     return check_linkage_conditions_;
331   }
332 
IsCrashOnLinkageViolation()333   bool IsCrashOnLinkageViolation() const {
334     return crash_on_linkage_violation_;
335   }
336 
DeduplicateCode()337   bool DeduplicateCode() const {
338     return deduplicate_code_;
339   }
340 
GetRegisterAllocationStrategy()341   RegisterAllocator::Strategy GetRegisterAllocationStrategy() const {
342     return register_allocation_strategy_;
343   }
344 
GetPassesToRun()345   const std::vector<std::string>* GetPassesToRun() const {
346     return passes_to_run_;
347   }
348 
GetDumpTimings()349   bool GetDumpTimings() const {
350     return dump_timings_;
351   }
352 
GetDumpPassTimings()353   bool GetDumpPassTimings() const {
354     return dump_pass_timings_;
355   }
356 
GetDumpStats()357   bool GetDumpStats() const {
358     return dump_stats_;
359   }
360 
CountHotnessInCompiledCode()361   bool CountHotnessInCompiledCode() const {
362     return count_hotness_in_compiled_code_;
363   }
364 
ResolveStartupConstStrings()365   bool ResolveStartupConstStrings() const {
366     return resolve_startup_const_strings_;
367   }
368 
CheckProfiledMethodsCompiled()369   ProfileMethodsCheck CheckProfiledMethodsCompiled() const {
370     return check_profiled_methods_;
371   }
372 
MaxImageBlockSize()373   uint32_t MaxImageBlockSize() const {
374     return max_image_block_size_;
375   }
376 
SetMaxImageBlockSize(uint32_t size)377   void SetMaxImageBlockSize(uint32_t size) {
378     max_image_block_size_ = size;
379   }
380 
InitializeAppImageClasses()381   bool InitializeAppImageClasses() const {
382     return initialize_app_image_classes_;
383   }
384 
385  private:
386   bool ParseDumpInitFailures(const std::string& option, std::string* error_msg);
387   bool ParseRegisterAllocationStrategy(const std::string& option, std::string* error_msg);
388 
389   CompilerFilter::Filter compiler_filter_;
390   size_t huge_method_threshold_;
391   size_t large_method_threshold_;
392   size_t num_dex_methods_threshold_;
393   size_t inline_max_code_units_;
394 
395   InstructionSet instruction_set_;
396   std::unique_ptr<const InstructionSetFeatures> instruction_set_features_;
397 
398   // Dex files from which we should not inline code. Does not own the dex files.
399   // This is usually a very short list (i.e. a single dex file), so we
400   // prefer vector<> over a lookup-oriented container, such as set<>.
401   std::vector<const DexFile*> no_inline_from_;
402 
403   // List of dex files associated with the oat file, empty for JIT.
404   std::vector<const DexFile*> dex_files_for_oat_file_;
405 
406   // Image classes, specifies the classes that will be included in the image if creating an image.
407   // Must not be empty for real boot image, only for tests pretending to compile boot image.
408   HashSet<std::string> image_classes_;
409 
410   // Results of AOT verification.
411   const VerificationResults* verification_results_;
412 
413   CompilerType compiler_type_;
414   ImageType image_type_;
415   bool compile_art_test_;
416   bool baseline_;
417   bool debuggable_;
418   bool generate_debug_info_;
419   bool generate_mini_debug_info_;
420   bool generate_build_id_;
421   bool implicit_null_checks_;
422   bool implicit_so_checks_;
423   bool implicit_suspend_checks_;
424   bool compile_pic_;
425   bool dump_timings_;
426   bool dump_pass_timings_;
427   bool dump_stats_;
428 
429   // When using a profile file only the top K% of the profiled samples will be compiled.
430   double top_k_profile_threshold_;
431 
432   // Info for profile guided compilation.
433   const ProfileCompilationInfo* profile_compilation_info_;
434 
435   // Vector of methods to have verbose output enabled for.
436   std::vector<std::string> verbose_methods_;
437 
438   // Abort compilation with an error if we find a class that fails verification with a hard
439   // failure.
440   bool abort_on_hard_verifier_failure_;
441   // Same for soft failures.
442   bool abort_on_soft_verifier_failure_;
443 
444   // Log initialization of initialization failures to this stream if not null.
445   std::unique_ptr<std::ostream> init_failure_output_;
446 
447   std::string dump_cfg_file_name_;
448   bool dump_cfg_append_;
449 
450   // Whether the compiler should trade performance for determinism to guarantee exactly reproducible
451   // outcomes.
452   bool force_determinism_;
453 
454   // Whether the compiler should check for violation of the conditions required to perform AOT
455   // "linkage".
456   bool check_linkage_conditions_;
457   // Whether the compiler should crash when encountering a violation of one of
458   // the conditions required to perform AOT "linkage".
459   bool crash_on_linkage_violation_;
460 
461   // Whether code should be deduplicated.
462   bool deduplicate_code_;
463 
464   // Whether compiled code should increment the hotness count of ArtMethod. Note that the increments
465   // won't be atomic for performance reasons, so we accept races, just like in interpreter.
466   bool count_hotness_in_compiled_code_;
467 
468   // Whether we eagerly resolve all of the const strings that are loaded from startup methods in the
469   // profile.
470   bool resolve_startup_const_strings_;
471 
472   // Whether we attempt to run class initializers for app image classes.
473   bool initialize_app_image_classes_;
474 
475   // When running profile-guided compilation, check that methods intended to be compiled end
476   // up compiled and are not punted.
477   ProfileMethodsCheck check_profiled_methods_;
478 
479   // Maximum solid block size in the generated image.
480   uint32_t max_image_block_size_;
481 
482   RegisterAllocator::Strategy register_allocation_strategy_;
483 
484   // If not null, specifies optimization passes which will be run instead of defaults.
485   // Note that passes_to_run_ is not checked for correctness and providing an incorrect
486   // list of passes can lead to unexpected compiler behaviour. This is caused by dependencies
487   // between passes. Failing to satisfy them can for example lead to compiler crashes.
488   // Passing pass names which are not recognized by the compiler will result in
489   // compiler-dependant behavior.
490   const std::vector<std::string>* passes_to_run_;
491 
492   friend class Dex2Oat;
493   friend class DexToDexDecompilerTest;
494   friend class CommonCompilerDriverTest;
495   friend class CommonCompilerTestImpl;
496   friend class jit::JitCompiler;
497   friend class verifier::VerifierDepsTest;
498   friend class linker::Arm64RelativePatcherTest;
499 
500   template <class Base>
501   friend bool ReadCompilerOptions(Base& map, CompilerOptions* options, std::string* error_msg);
502 
503   DISALLOW_COPY_AND_ASSIGN(CompilerOptions);
504 };
505 
506 }  // namespace art
507 
508 #endif  // ART_COMPILER_DRIVER_COMPILER_OPTIONS_H_
509