Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "compiler_options.h" |
| 18 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 19 | #include <fstream> |
Vladimir Marko | 2ef0110 | 2019-02-05 15:05:10 +0000 | [diff] [blame] | 20 | #include <string_view> |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 21 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
| 23 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 24 | #include "arch/instruction_set.h" |
| 25 | #include "arch/instruction_set_features.h" |
Andreas Gampe | 2a5d728 | 2018-01-02 11:53:35 -0800 | [diff] [blame] | 26 | #include "base/runtime_debug.h" |
Vladimir Marko | 44ca075 | 2019-07-29 10:18:25 +0100 | [diff] [blame] | 27 | #include "base/string_view_cpp20.h" |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 28 | #include "base/variant_map.h" |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 29 | #include "class_linker.h" |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 30 | #include "cmdline_parser.h" |
| 31 | #include "compiler_options_map-inl.h" |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 32 | #include "dex/dex_file-inl.h" |
| 33 | #include "dex/verification_results.h" |
| 34 | #include "dex/verified_method.h" |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 35 | #include "runtime.h" |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 36 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 37 | #include "simple_compiler_options_map.h" |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 38 | |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 39 | namespace art { |
| 40 | |
| 41 | CompilerOptions::CompilerOptions() |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 42 | : compiler_filter_(CompilerFilter::kDefaultCompilerFilter), |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 43 | huge_method_threshold_(kDefaultHugeMethodThreshold), |
| 44 | large_method_threshold_(kDefaultLargeMethodThreshold), |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 45 | num_dex_methods_threshold_(kDefaultNumDexMethodsThreshold), |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 46 | inline_max_code_units_(kUnsetInlineMaxCodeUnits), |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 47 | instruction_set_(kRuntimeISA == InstructionSet::kArm ? InstructionSet::kThumb2 : kRuntimeISA), |
| 48 | instruction_set_features_(nullptr), |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 49 | no_inline_from_(), |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 50 | dex_files_for_oat_file_(), |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 51 | image_classes_(), |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 52 | verification_results_(nullptr), |
Vladimir Marko | 695348f | 2020-05-19 14:42:02 +0100 | [diff] [blame] | 53 | compiler_type_(CompilerType::kAotCompiler), |
Vladimir Marko | 9c4b970 | 2018-11-14 15:09:02 +0000 | [diff] [blame] | 54 | image_type_(ImageType::kNone), |
David Srbecky | 4fa07a5 | 2020-03-31 20:52:09 +0100 | [diff] [blame] | 55 | compile_art_test_(false), |
Nicolas Geoffray | acc56ac | 2018-10-09 08:45:24 +0100 | [diff] [blame] | 56 | baseline_(false), |
Andreas Gampe | 7b2f09e | 2015-03-02 14:07:33 -0800 | [diff] [blame] | 57 | debuggable_(false), |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 58 | generate_debug_info_(kDefaultGenerateDebugInfo), |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 59 | generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo), |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 60 | generate_build_id_(false), |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 61 | implicit_null_checks_(true), |
| 62 | implicit_so_checks_(true), |
| 63 | implicit_suspend_checks_(false), |
| 64 | compile_pic_(false), |
Nicolas Geoffray | 2d8801f | 2017-11-28 15:50:07 +0000 | [diff] [blame] | 65 | dump_timings_(false), |
Vladimir Marko | 2da52b0 | 2018-05-08 16:31:34 +0100 | [diff] [blame] | 66 | dump_pass_timings_(false), |
Nicolas Geoffray | 2d8801f | 2017-11-28 15:50:07 +0000 | [diff] [blame] | 67 | dump_stats_(false), |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 68 | top_k_profile_threshold_(kDefaultTopKProfileThreshold), |
Vladimir Marko | 1a2a5cd | 2018-11-07 15:39:48 +0000 | [diff] [blame] | 69 | profile_compilation_info_(nullptr), |
Nicolas Geoffray | 57c4704 | 2017-06-29 11:31:39 +0100 | [diff] [blame] | 70 | verbose_methods_(), |
Andreas Gampe | 6cf49e5 | 2015-03-05 13:08:45 -0800 | [diff] [blame] | 71 | abort_on_hard_verifier_failure_(false), |
Andreas Gampe | f39208f | 2017-10-19 15:06:59 -0700 | [diff] [blame] | 72 | abort_on_soft_verifier_failure_(false), |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 73 | init_failure_output_(nullptr), |
| 74 | dump_cfg_file_name_(""), |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 75 | dump_cfg_append_(false), |
Matthew Gharrity | 2cd05b7 | 2016-08-03 16:57:37 -0700 | [diff] [blame] | 76 | force_determinism_(false), |
David Sehr | fe57c2b | 2020-03-27 14:58:54 +0000 | [diff] [blame] | 77 | check_linkage_conditions_(false), |
| 78 | crash_on_linkage_violation_(false), |
Andreas Gampe | cac31ad | 2017-11-06 20:01:17 -0800 | [diff] [blame] | 79 | deduplicate_code_(true), |
Nicolas Geoffray | 8d72832 | 2018-01-18 22:44:32 +0000 | [diff] [blame] | 80 | count_hotness_in_compiled_code_(false), |
Mathieu Chartier | cd0f38f | 2018-10-15 09:44:35 -0700 | [diff] [blame] | 81 | resolve_startup_const_strings_(false), |
Mathieu Chartier | 5132e0d | 2019-07-10 09:38:48 -0700 | [diff] [blame] | 82 | initialize_app_image_classes_(false), |
Andreas Gampe | 5c80311 | 2018-04-13 17:28:34 -0700 | [diff] [blame] | 83 | check_profiled_methods_(ProfileMethodsCheck::kNone), |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 84 | max_image_block_size_(std::numeric_limits<uint32_t>::max()), |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 85 | register_allocation_strategy_(RegisterAllocator::kRegisterAllocatorDefault), |
| 86 | passes_to_run_(nullptr) { |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 89 | CompilerOptions::~CompilerOptions() { |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 90 | // Everything done by member destructors. |
| 91 | // The definitions of classes forward-declared in the header have now been #included. |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Andreas Gampe | 2a5d728 | 2018-01-02 11:53:35 -0800 | [diff] [blame] | 94 | namespace { |
| 95 | |
| 96 | bool kEmitRuntimeReadBarrierChecks = kIsDebugBuild && |
| 97 | RegisterRuntimeDebugFlag(&kEmitRuntimeReadBarrierChecks); |
| 98 | |
| 99 | } // namespace |
| 100 | |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 101 | bool CompilerOptions::EmitRunTimeChecksInDebugMode() const { |
Andreas Gampe | 2a5d728 | 2018-01-02 11:53:35 -0800 | [diff] [blame] | 102 | // Run-time checks (e.g. Marking Register checks) are only emitted in slow-debug mode. |
| 103 | return kEmitRuntimeReadBarrierChecks; |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 106 | bool CompilerOptions::ParseDumpInitFailures(const std::string& option, std::string* error_msg) { |
| 107 | init_failure_output_.reset(new std::ofstream(option)); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 108 | if (init_failure_output_.get() == nullptr) { |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 109 | *error_msg = "Failed to construct std::ofstream"; |
| 110 | return false; |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 111 | } else if (init_failure_output_->fail()) { |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 112 | *error_msg = android::base::StringPrintf( |
| 113 | "Failed to open %s for writing the initialization failures.", option.c_str()); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 114 | init_failure_output_.reset(); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 120 | bool CompilerOptions::ParseRegisterAllocationStrategy(const std::string& option, |
| 121 | std::string* error_msg) { |
| 122 | if (option == "linear-scan") { |
| 123 | register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorLinearScan; |
| 124 | } else if (option == "graph-color") { |
| 125 | register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorGraphColor; |
| 126 | } else { |
| 127 | *error_msg = "Unrecognized register allocation strategy. Try linear-scan, or graph-color."; |
| 128 | return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 133 | bool CompilerOptions::ParseCompilerOptions(const std::vector<std::string>& options, |
| 134 | bool ignore_unrecognized, |
| 135 | std::string* error_msg) { |
| 136 | auto parser = CreateSimpleParser(ignore_unrecognized); |
| 137 | CmdlineResult parse_result = parser.Parse(options); |
| 138 | if (!parse_result.IsSuccess()) { |
| 139 | *error_msg = parse_result.GetMessage(); |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | SimpleParseArgumentMap args = parser.ReleaseArgumentsMap(); |
| 144 | return ReadCompilerOptions(args, this, error_msg); |
| 145 | } |
| 146 | |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 147 | bool CompilerOptions::IsImageClass(const char* descriptor) const { |
| 148 | // Historical note: We used to hold the set indirectly and there was a distinction between an |
| 149 | // empty set and a null, null meaning to include all classes. However, the distiction has been |
| 150 | // removed; if we don't have a profile, we treat it as an empty set of classes. b/77340429 |
Vladimir Marko | 2ef0110 | 2019-02-05 15:05:10 +0000 | [diff] [blame] | 151 | return image_classes_.find(std::string_view(descriptor)) != image_classes_.end(); |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 154 | const VerificationResults* CompilerOptions::GetVerificationResults() const { |
| 155 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 156 | return verification_results_; |
| 157 | } |
| 158 | |
| 159 | const VerifiedMethod* CompilerOptions::GetVerifiedMethod(const DexFile* dex_file, |
| 160 | uint32_t method_idx) const { |
| 161 | MethodReference ref(dex_file, method_idx); |
| 162 | return verification_results_->GetVerifiedMethod(ref); |
| 163 | } |
| 164 | |
| 165 | bool CompilerOptions::IsMethodVerifiedWithoutFailures(uint32_t method_idx, |
| 166 | uint16_t class_def_idx, |
| 167 | const DexFile& dex_file) const { |
| 168 | const VerifiedMethod* verified_method = GetVerifiedMethod(&dex_file, method_idx); |
| 169 | if (verified_method != nullptr) { |
| 170 | return !verified_method->HasVerificationFailures(); |
| 171 | } |
| 172 | |
| 173 | // If we can't find verification metadata, check if this is a system class (we trust that system |
| 174 | // classes have their methods verified). If it's not, be conservative and assume the method |
| 175 | // has not been verified successfully. |
| 176 | |
| 177 | // TODO: When compiling the boot image it should be safe to assume that everything is verified, |
| 178 | // even if methods are not found in the verification cache. |
| 179 | const char* descriptor = dex_file.GetClassDescriptor(dex_file.GetClassDef(class_def_idx)); |
| 180 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 181 | Thread* self = Thread::Current(); |
| 182 | ScopedObjectAccess soa(self); |
| 183 | bool is_system_class = class_linker->FindSystemClass(self, descriptor) != nullptr; |
| 184 | if (!is_system_class) { |
| 185 | self->ClearException(); |
| 186 | } |
| 187 | return is_system_class; |
| 188 | } |
| 189 | |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 190 | } // namespace art |