1 /*
2 * Copyright 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 #include "jit_compiler.h"
18
19 #include "android-base/stringprintf.h"
20
21 #include "arch/instruction_set.h"
22 #include "arch/instruction_set_features.h"
23 #include "art_method-inl.h"
24 #include "base/stringpiece.h"
25 #include "base/time_utils.h"
26 #include "base/timing_logger.h"
27 #include "base/unix_file/fd_file.h"
28 #include "debug/elf_debug_writer.h"
29 #include "driver/compiler_driver.h"
30 #include "driver/compiler_options.h"
31 #include "jit/debugger_interface.h"
32 #include "jit/jit.h"
33 #include "jit/jit_code_cache.h"
34 #include "oat_file-inl.h"
35 #include "oat_quick_method_header.h"
36 #include "object_lock.h"
37 #include "optimizing/register_allocator.h"
38 #include "thread_list.h"
39
40 namespace art {
41 namespace jit {
42
Create()43 JitCompiler* JitCompiler::Create() {
44 return new JitCompiler();
45 }
46
jit_load(bool * generate_debug_info)47 extern "C" void* jit_load(bool* generate_debug_info) {
48 VLOG(jit) << "loading jit compiler";
49 auto* const jit_compiler = JitCompiler::Create();
50 CHECK(jit_compiler != nullptr);
51 *generate_debug_info = jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo();
52 VLOG(jit) << "Done loading jit compiler";
53 return jit_compiler;
54 }
55
jit_unload(void * handle)56 extern "C" void jit_unload(void* handle) {
57 DCHECK(handle != nullptr);
58 delete reinterpret_cast<JitCompiler*>(handle);
59 }
60
jit_compile_method(void * handle,ArtMethod * method,Thread * self,bool osr)61 extern "C" bool jit_compile_method(
62 void* handle, ArtMethod* method, Thread* self, bool osr)
63 REQUIRES_SHARED(Locks::mutator_lock_) {
64 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
65 DCHECK(jit_compiler != nullptr);
66 return jit_compiler->CompileMethod(self, method, osr);
67 }
68
jit_types_loaded(void * handle,mirror::Class ** types,size_t count)69 extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
70 REQUIRES_SHARED(Locks::mutator_lock_) {
71 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
72 DCHECK(jit_compiler != nullptr);
73 if (jit_compiler->GetCompilerOptions()->GetGenerateDebugInfo()) {
74 const ArrayRef<mirror::Class*> types_array(types, count);
75 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
76 kRuntimeISA, jit_compiler->GetCompilerDriver()->GetInstructionSetFeatures(), types_array);
77 CreateJITCodeEntry(std::move(elf_file));
78 }
79 }
80
81 // Callers of this method assume it has NO_RETURN.
Usage(const char * fmt,...)82 NO_RETURN static void Usage(const char* fmt, ...) {
83 va_list ap;
84 va_start(ap, fmt);
85 std::string error;
86 android::base::StringAppendV(&error, fmt, ap);
87 LOG(FATAL) << error;
88 va_end(ap);
89 exit(EXIT_FAILURE);
90 }
91
JitCompiler()92 JitCompiler::JitCompiler() {
93 compiler_options_.reset(new CompilerOptions(
94 CompilerFilter::kDefaultCompilerFilter,
95 CompilerOptions::kDefaultHugeMethodThreshold,
96 CompilerOptions::kDefaultLargeMethodThreshold,
97 CompilerOptions::kDefaultSmallMethodThreshold,
98 CompilerOptions::kDefaultTinyMethodThreshold,
99 CompilerOptions::kDefaultNumDexMethodsThreshold,
100 CompilerOptions::kDefaultInlineMaxCodeUnits,
101 /* no_inline_from */ nullptr,
102 CompilerOptions::kDefaultTopKProfileThreshold,
103 Runtime::Current()->IsJavaDebuggable(),
104 CompilerOptions::kDefaultGenerateDebugInfo,
105 /* implicit_null_checks */ true,
106 /* implicit_so_checks */ true,
107 /* implicit_suspend_checks */ false,
108 /* pic */ true, // TODO: Support non-PIC in optimizing.
109 /* verbose_methods */ nullptr,
110 /* init_failure_output */ nullptr,
111 /* abort_on_hard_verifier_failure */ false,
112 /* dump_cfg_file_name */ "",
113 /* dump_cfg_append */ false,
114 /* force_determinism */ false,
115 RegisterAllocator::kRegisterAllocatorDefault,
116 /* passes_to_run */ nullptr));
117 for (const std::string& argument : Runtime::Current()->GetCompilerOptions()) {
118 compiler_options_->ParseCompilerOption(argument, Usage);
119 }
120 const InstructionSet instruction_set = kRuntimeISA;
121 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
122 VLOG(compiler) << "JIT compiler option " << option;
123 std::string error_msg;
124 if (option.starts_with("--instruction-set-variant=")) {
125 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
126 VLOG(compiler) << "JIT instruction set variant " << str;
127 instruction_set_features_ = InstructionSetFeatures::FromVariant(
128 instruction_set, str.as_string(), &error_msg);
129 if (instruction_set_features_ == nullptr) {
130 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
131 }
132 } else if (option.starts_with("--instruction-set-features=")) {
133 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
134 VLOG(compiler) << "JIT instruction set features " << str;
135 if (instruction_set_features_ == nullptr) {
136 instruction_set_features_ = InstructionSetFeatures::FromVariant(
137 instruction_set, "default", &error_msg);
138 if (instruction_set_features_ == nullptr) {
139 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
140 }
141 }
142 instruction_set_features_ =
143 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg);
144 if (instruction_set_features_ == nullptr) {
145 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
146 }
147 }
148 }
149 if (instruction_set_features_ == nullptr) {
150 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
151 }
152 cumulative_logger_.reset(new CumulativeLogger("jit times"));
153 compiler_driver_.reset(new CompilerDriver(
154 compiler_options_.get(),
155 /* verification_results */ nullptr,
156 Compiler::kOptimizing,
157 instruction_set,
158 instruction_set_features_.get(),
159 /* image_classes */ nullptr,
160 /* compiled_classes */ nullptr,
161 /* compiled_methods */ nullptr,
162 /* thread_count */ 1,
163 /* dump_stats */ false,
164 /* dump_passes */ false,
165 cumulative_logger_.get(),
166 /* swap_fd */ -1,
167 /* profile_compilation_info */ nullptr));
168 // Disable dedupe so we can remove compiled methods.
169 compiler_driver_->SetDedupeEnabled(false);
170 compiler_driver_->SetSupportBootImageFixup(false);
171
172 size_t thread_count = compiler_driver_->GetThreadCount();
173 if (compiler_options_->GetGenerateDebugInfo()) {
174 DCHECK_EQ(thread_count, 1u)
175 << "Generating debug info only works with one compiler thread";
176 jit_logger_.reset(new JitLogger());
177 jit_logger_->OpenLog();
178 }
179 }
180
~JitCompiler()181 JitCompiler::~JitCompiler() {
182 if (compiler_options_->GetGenerateDebugInfo()) {
183 jit_logger_->CloseLog();
184 }
185 }
186
CompileMethod(Thread * self,ArtMethod * method,bool osr)187 bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool osr) {
188 DCHECK(!method->IsProxyMethod());
189 DCHECK(method->GetDeclaringClass()->IsResolved());
190
191 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
192 self->AssertNoPendingException();
193 Runtime* runtime = Runtime::Current();
194
195 // Do the compilation.
196 bool success = false;
197 {
198 TimingLogger::ScopedTiming t2("Compiling", &logger);
199 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
200 success = compiler_driver_->GetCompiler()->JitCompile(self, code_cache, method, osr);
201 if (success && (jit_logger_ != nullptr)) {
202 jit_logger_->WriteLog(code_cache, method, osr);
203 }
204 }
205
206 // Trim maps to reduce memory usage.
207 // TODO: move this to an idle phase.
208 {
209 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
210 runtime->GetJitArenaPool()->TrimMaps();
211 }
212
213 runtime->GetJit()->AddTimingLogger(logger);
214 return success;
215 }
216
217 } // namespace jit
218 } // namespace art
219