blob: 42f7657c3eb5a02cda009d9534dacdf62cc3ee85 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080020#include "arch/instruction_set.h"
21#include "arch/instruction_set_features.h"
Mathieu Chartier085fc872015-10-15 18:19:01 -070022#include "base/stringpiece.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/time_utils.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070024#include "base/timing_logger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080025#include "compiler_callbacks.h"
26#include "dex/pass_manager.h"
27#include "dex/quick_compiler_callbacks.h"
28#include "driver/compiler_driver.h"
29#include "driver/compiler_options.h"
30#include "jit/jit.h"
31#include "jit/jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "oat_file-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "object_lock.h"
35#include "thread_list.h"
36#include "verifier/method_verifier-inl.h"
37
38namespace art {
39namespace jit {
40
41JitCompiler* JitCompiler::Create() {
42 return new JitCompiler();
43}
44
45extern "C" void* jit_load(CompilerCallbacks** callbacks) {
46 VLOG(jit) << "loading jit compiler";
47 auto* const jit_compiler = JitCompiler::Create();
48 CHECK(jit_compiler != nullptr);
49 *callbacks = jit_compiler->GetCompilerCallbacks();
50 VLOG(jit) << "Done loading jit compiler";
51 return jit_compiler;
52}
53
54extern "C" void jit_unload(void* handle) {
55 DCHECK(handle != nullptr);
56 delete reinterpret_cast<JitCompiler*>(handle);
57}
58
Mathieu Chartiere401d142015-04-22 13:56:20 -070059extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -070060 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
62 DCHECK(jit_compiler != nullptr);
63 return jit_compiler->CompileMethod(self, method);
64}
65
66JitCompiler::JitCompiler() : total_time_(0) {
67 auto* pass_manager_options = new PassManagerOptions;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070068 pass_manager_options->SetDisablePassList("GVN,DCE,GVNCleanup");
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080069 compiler_options_.reset(new CompilerOptions(
70 CompilerOptions::kDefaultCompilerFilter,
71 CompilerOptions::kDefaultHugeMethodThreshold,
72 CompilerOptions::kDefaultLargeMethodThreshold,
73 CompilerOptions::kDefaultSmallMethodThreshold,
74 CompilerOptions::kDefaultTinyMethodThreshold,
75 CompilerOptions::kDefaultNumDexMethodsThreshold,
Calin Juravleec748352015-07-29 13:52:12 +010076 CompilerOptions::kDefaultInlineDepthLimit,
77 CompilerOptions::kDefaultInlineMaxCodeUnits,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010078 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080079 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010080 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +010081 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010082 /* implicit_null_checks */ true,
83 /* implicit_so_checks */ true,
84 /* implicit_suspend_checks */ false,
85 /* pic */ true, // TODO: Support non-PIC in optimizing.
86 /* verbose_methods */ nullptr,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080087 pass_manager_options,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010088 /* init_failure_output */ nullptr,
89 /* abort_on_hard_verifier_failure */ false));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080090 const InstructionSet instruction_set = kRuntimeISA;
Mathieu Chartier085fc872015-10-15 18:19:01 -070091 for (const StringPiece option : Runtime::Current()->GetCompilerOptions()) {
92 VLOG(compiler) << "JIT compiler option " << option;
93 std::string error_msg;
94 if (option.starts_with("--instruction-set-variant=")) {
95 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
96 VLOG(compiler) << "JIT instruction set variant " << str;
97 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
98 instruction_set, str.as_string(), &error_msg));
99 if (instruction_set_features_ == nullptr) {
100 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
101 }
102 } else if (option.starts_with("--instruction-set-features=")) {
103 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
104 VLOG(compiler) << "JIT instruction set features " << str;
105 if (instruction_set_features_.get() == nullptr) {
106 instruction_set_features_.reset(InstructionSetFeatures::FromVariant(
107 instruction_set, "default", &error_msg));
108 if (instruction_set_features_ == nullptr) {
109 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
110 }
111 }
112 instruction_set_features_.reset(
113 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
114 if (instruction_set_features_ == nullptr) {
115 LOG(WARNING) << "Error parsing " << option << " message=" << error_msg;
116 }
117 }
118 }
119 if (instruction_set_features_ == nullptr) {
120 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
121 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800122 cumulative_logger_.reset(new CumulativeLogger("jit times"));
123 verification_results_.reset(new VerificationResults(compiler_options_.get()));
124 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
125 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700126 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700127 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100129 compiler_options_.get(),
130 verification_results_.get(),
131 method_inliner_map_.get(),
132 Compiler::kOptimizing,
133 instruction_set,
134 instruction_set_features_.get(),
135 /* image */ false,
136 /* image_classes */ nullptr,
137 /* compiled_classes */ nullptr,
138 /* compiled_methods */ nullptr,
139 /* thread_count */ 1,
140 /* dump_stats */ false,
141 /* dump_passes */ false,
142 /* dump_cfg_file_name */ "",
Calin Juravle87000a92015-08-24 15:34:44 +0100143 /* dump_cfg_append */ false,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100144 cumulative_logger_.get(),
145 /* swap_fd */ -1,
146 /* profile_file */ ""));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800147 // Disable dedupe so we can remove compiled methods.
148 compiler_driver_->SetDedupeEnabled(false);
149 compiler_driver_->SetSupportBootImageFixup(false);
150}
151
152JitCompiler::~JitCompiler() {
153}
154
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700156 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700157 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 StackHandleScope<2> hs(self);
159 self->AssertNoPendingException();
160 Runtime* runtime = Runtime::Current();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100161
162 // Check if the method is already compiled.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100163 if (runtime->GetJit()->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800164 VLOG(jit) << "Already compiled " << PrettyMethod(method);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100165 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100167
168 // Don't compile the method if we are supposed to be deoptimized.
169 if (runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
170 return false;
171 }
172
173 // Ensure the class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100175 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
176 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
177 return false;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100179
180 // Do the compilation.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700181 CompiledMethod* compiled_method = nullptr;
182 {
183 TimingLogger::ScopedTiming t2("Compiling", &logger);
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700184 compiled_method = compiler_driver_->CompileArtMethod(self, method);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700185 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100186
187 // Trim maps to reduce memory usage.
188 // TODO: measure how much this increases compile time.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700189 {
190 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700191 runtime->GetArenaPool()->TrimMaps();
192 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100193
194 // Check if we failed compiling.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195 if (compiled_method == nullptr) {
196 return false;
197 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100198
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800199 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800200 bool result = false;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100201 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method);
202
203 if (code != nullptr) {
204 // Already have some compiled code, just use this instead of linking.
205 // TODO: Fix recompilation.
206 method->SetEntryPointFromQuickCompiledCode(code);
207 result = true;
208 } else {
209 TimingLogger::ScopedTiming t2("LinkCode", &logger);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100210 if (AddToCodeCache(method, compiled_method)) {
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800211 result = true;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800212 }
213 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100214
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800215 // Remove the compiled method to save memory.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100216 compiler_driver_->RemoveCompiledMethod(
217 MethodReference(h_class->GetDexCache()->GetDexFile(), method->GetDexMethodIndex()));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700218 runtime->GetJit()->AddTimingLogger(logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800219 return result;
220}
221
222CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
223 return callbacks_.get();
224}
225
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100226bool JitCompiler::AddToCodeCache(ArtMethod* method,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100227 const CompiledMethod* compiled_method) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800228 Runtime* runtime = Runtime::Current();
229 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
230 const auto* quick_code = compiled_method->GetQuickCode();
231 if (quick_code == nullptr) {
232 return false;
233 }
234 const auto code_size = quick_code->size();
235 Thread* const self = Thread::Current();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800236 auto* const mapping_table = compiled_method->GetMappingTable();
237 auto* const vmap_table = compiled_method->GetVmapTable();
238 auto* const gc_map = compiled_method->GetGcMap();
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100239 uint8_t* mapping_table_ptr = nullptr;
240 uint8_t* vmap_table_ptr = nullptr;
241 uint8_t* gc_map_ptr = nullptr;
242
243 if (mapping_table != nullptr) {
244 // Write out pre-header stuff.
245 mapping_table_ptr = code_cache->AddDataArray(
246 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
247 if (mapping_table_ptr == nullptr) {
248 return false; // Out of data cache.
249 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800250 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100251
252 if (vmap_table != nullptr) {
253 vmap_table_ptr = code_cache->AddDataArray(
254 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
255 if (vmap_table_ptr == nullptr) {
256 return false; // Out of data cache.
257 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800258 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100259
260 if (gc_map != nullptr) {
261 gc_map_ptr = code_cache->AddDataArray(
262 self, gc_map->data(), gc_map->data() + gc_map->size());
263 if (gc_map_ptr == nullptr) {
264 return false; // Out of data cache.
265 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800266 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100267
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100268 uint8_t* const code = code_cache->CommitCode(self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100269 method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100270 mapping_table_ptr,
271 vmap_table_ptr,
272 gc_map_ptr,
273 compiled_method->GetFrameSizeInBytes(),
274 compiled_method->GetCoreSpillMask(),
275 compiled_method->GetFpSpillMask(),
276 compiled_method->GetQuickCode()->data(),
277 compiled_method->GetQuickCode()->size());
278
279 if (code == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800280 return false;
281 }
Mathieu Chartier5783a742015-06-01 19:12:36 -0700282
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800283 const size_t thumb_offset = compiled_method->CodeDelta();
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100284 const uint32_t code_offset = sizeof(OatQuickMethodHeader) + thumb_offset;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100285 VLOG(jit)
286 << "JIT added "
287 << PrettyMethod(method) << "@" << method
288 << " ccache_size=" << PrettySize(code_cache->CodeCacheSize()) << ": "
289 << reinterpret_cast<void*>(code + code_offset)
290 << "," << reinterpret_cast<void*>(code + code_offset + code_size);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800291 return true;
292}
293
294} // namespace jit
295} // namespace art