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
19 #include <fstream>
20 #include <string_view>
21
22 #include "android-base/stringprintf.h"
23
24 #include "arch/instruction_set.h"
25 #include "arch/instruction_set_features.h"
26 #include "art_method-inl.h"
27 #include "base/runtime_debug.h"
28 #include "base/string_view_cpp20.h"
29 #include "base/variant_map.h"
30 #include "class_linker.h"
31 #include "cmdline_parser.h"
32 #include "compiler_options_map-inl.h"
33 #include "dex/dex_file-inl.h"
34 #include "runtime.h"
35 #include "scoped_thread_state_change-inl.h"
36 #include "simple_compiler_options_map.h"
37
38 namespace art HIDDEN {
39
CompilerOptions()40 CompilerOptions::CompilerOptions()
41 : compiler_filter_(CompilerFilter::kDefaultCompilerFilter),
42 huge_method_threshold_(kDefaultHugeMethodThreshold),
43 large_method_threshold_(kDefaultLargeMethodThreshold),
44 num_dex_methods_threshold_(kDefaultNumDexMethodsThreshold),
45 inline_max_code_units_(kUnsetInlineMaxCodeUnits),
46 instruction_set_(kRuntimeISA == InstructionSet::kArm ? InstructionSet::kThumb2 : kRuntimeISA),
47 instruction_set_features_(nullptr),
48 no_inline_from_(),
49 dex_files_for_oat_file_(),
50 image_classes_(),
51 compiler_type_(CompilerType::kAotCompiler),
52 image_type_(ImageType::kNone),
53 multi_image_(false),
54 compile_art_test_(false),
55 baseline_(false),
56 debuggable_(false),
57 generate_debug_info_(kDefaultGenerateDebugInfo),
58 generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
59 generate_build_id_(false),
60 implicit_null_checks_(true),
61 implicit_so_checks_(true),
62 implicit_suspend_checks_(false),
63 compile_pic_(false),
64 dump_timings_(false),
65 dump_pass_timings_(false),
66 dump_stats_(false),
67 top_k_profile_threshold_(kDefaultTopKProfileThreshold),
68 profile_compilation_info_(nullptr),
69 verbose_methods_(),
70 abort_on_hard_verifier_failure_(false),
71 abort_on_soft_verifier_failure_(false),
72 init_failure_output_(nullptr),
73 dump_cfg_file_name_(""),
74 dump_cfg_append_(false),
75 force_determinism_(false),
76 check_linkage_conditions_(false),
77 crash_on_linkage_violation_(false),
78 deduplicate_code_(true),
79 count_hotness_in_compiled_code_(false),
80 resolve_startup_const_strings_(false),
81 initialize_app_image_classes_(false),
82 check_profiled_methods_(ProfileMethodsCheck::kNone),
83 max_image_block_size_(std::numeric_limits<uint32_t>::max()),
84 register_allocation_strategy_(RegisterAllocator::kRegisterAllocatorDefault),
85 passes_to_run_(nullptr) {
86 }
87
~CompilerOptions()88 CompilerOptions::~CompilerOptions() {
89 // Everything done by member destructors.
90 // The definitions of classes forward-declared in the header have now been #included.
91 }
92
93 namespace {
94
95 bool kEmitRuntimeReadBarrierChecks = kIsDebugBuild &&
96 RegisterRuntimeDebugFlag(&kEmitRuntimeReadBarrierChecks);
97
98 } // namespace
99
EmitRunTimeChecksInDebugMode() const100 bool CompilerOptions::EmitRunTimeChecksInDebugMode() const {
101 // Run-time checks (e.g. Marking Register checks) are only emitted in slow-debug mode.
102 return kEmitRuntimeReadBarrierChecks;
103 }
104
ParseDumpInitFailures(const std::string & option,std::string * error_msg)105 bool CompilerOptions::ParseDumpInitFailures(const std::string& option, std::string* error_msg) {
106 init_failure_output_.reset(new std::ofstream(option));
107 if (init_failure_output_.get() == nullptr) {
108 *error_msg = "Failed to construct std::ofstream";
109 return false;
110 } else if (init_failure_output_->fail()) {
111 *error_msg = android::base::StringPrintf(
112 "Failed to open %s for writing the initialization failures.", option.c_str());
113 init_failure_output_.reset();
114 return false;
115 }
116 return true;
117 }
118
ParseRegisterAllocationStrategy(const std::string & option,std::string * error_msg)119 bool CompilerOptions::ParseRegisterAllocationStrategy(const std::string& option,
120 std::string* error_msg) {
121 if (option == "linear-scan") {
122 register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorLinearScan;
123 } else if (option == "graph-color") {
124 register_allocation_strategy_ = RegisterAllocator::Strategy::kRegisterAllocatorGraphColor;
125 } else {
126 *error_msg = "Unrecognized register allocation strategy. Try linear-scan, or graph-color.";
127 return false;
128 }
129 return true;
130 }
131
ParseCompilerOptions(const std::vector<std::string> & options,bool ignore_unrecognized,std::string * error_msg)132 bool CompilerOptions::ParseCompilerOptions(const std::vector<std::string>& options,
133 bool ignore_unrecognized,
134 std::string* error_msg) {
135 auto parser = CreateSimpleParser(ignore_unrecognized);
136 CmdlineResult parse_result = parser.Parse(options);
137 if (!parse_result.IsSuccess()) {
138 *error_msg = parse_result.GetMessage();
139 return false;
140 }
141
142 SimpleParseArgumentMap args = parser.ReleaseArgumentsMap();
143 return ReadCompilerOptions(args, this, error_msg);
144 }
145
IsImageClass(const char * descriptor) const146 bool CompilerOptions::IsImageClass(const char* descriptor) const {
147 // Historical note: We used to hold the set indirectly and there was a distinction between an
148 // empty set and a null, null meaning to include all classes. However, the distinction has been
149 // removed; if we don't have a profile, we treat it as an empty set of classes. b/77340429
150 return image_classes_.find(std::string_view(descriptor)) != image_classes_.end();
151 }
152
IsPreloadedClass(const char * pretty_descriptor) const153 bool CompilerOptions::IsPreloadedClass(const char* pretty_descriptor) const {
154 return preloaded_classes_.find(std::string_view(pretty_descriptor)) != preloaded_classes_.end();
155 }
156
ShouldCompileWithClinitCheck(ArtMethod * method) const157 bool CompilerOptions::ShouldCompileWithClinitCheck(ArtMethod* method) const {
158 if (method != nullptr &&
159 Runtime::Current()->IsAotCompiler() &&
160 method->IsStatic() &&
161 !method->IsConstructor() &&
162 // Compiled code for native methods never do a clinit check, so we may put the resolution
163 // trampoline for native methods. This means that it's possible post zygote fork for the
164 // entry to be dirtied. We could resolve this by either:
165 // - Make these methods use the generic JNI entrypoint, but that's not
166 // desirable for a method that is in the profile.
167 // - Ensure the declaring class of such native methods are always in the
168 // preloaded-classes list.
169 // - Emit the clinit check in the compiled code of native methods.
170 !method->IsNative()) {
171 ScopedObjectAccess soa(Thread::Current());
172 ObjPtr<mirror::Class> cls = method->GetDeclaringClass<kWithoutReadBarrier>();
173 return cls->IsInBootImageAndNotInPreloadedClasses();
174 }
175 return false;
176 }
177
178 } // namespace art
179