blob: d70211f9a9cfa4dea1948f5ad38b054f83d54143 [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"
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/time_utils.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070023#include "base/timing_logger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080024#include "compiler_callbacks.h"
25#include "dex/pass_manager.h"
26#include "dex/quick_compiler_callbacks.h"
27#include "driver/compiler_driver.h"
28#include "driver/compiler_options.h"
29#include "jit/jit.h"
30#include "jit/jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "oat_file-inl.h"
32#include "object_lock.h"
33#include "thread_list.h"
34#include "verifier/method_verifier-inl.h"
35
36namespace art {
37namespace jit {
38
39JitCompiler* JitCompiler::Create() {
40 return new JitCompiler();
41}
42
43extern "C" void* jit_load(CompilerCallbacks** callbacks) {
44 VLOG(jit) << "loading jit compiler";
45 auto* const jit_compiler = JitCompiler::Create();
46 CHECK(jit_compiler != nullptr);
47 *callbacks = jit_compiler->GetCompilerCallbacks();
48 VLOG(jit) << "Done loading jit compiler";
49 return jit_compiler;
50}
51
52extern "C" void jit_unload(void* handle) {
53 DCHECK(handle != nullptr);
54 delete reinterpret_cast<JitCompiler*>(handle);
55}
56
Mathieu Chartiere401d142015-04-22 13:56:20 -070057extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -070058 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059 auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
60 DCHECK(jit_compiler != nullptr);
61 return jit_compiler->CompileMethod(self, method);
62}
63
64JitCompiler::JitCompiler() : total_time_(0) {
65 auto* pass_manager_options = new PassManagerOptions;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070066 pass_manager_options->SetDisablePassList("GVN,DCE,GVNCleanup");
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080067 compiler_options_.reset(new CompilerOptions(
68 CompilerOptions::kDefaultCompilerFilter,
69 CompilerOptions::kDefaultHugeMethodThreshold,
70 CompilerOptions::kDefaultLargeMethodThreshold,
71 CompilerOptions::kDefaultSmallMethodThreshold,
72 CompilerOptions::kDefaultTinyMethodThreshold,
73 CompilerOptions::kDefaultNumDexMethodsThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010074 /* include_patch_information */ false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080075 CompilerOptions::kDefaultTopKProfileThreshold,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010076 Runtime::Current()->IsDebuggable(),
David Srbecky8363c772015-05-28 16:12:43 +010077 CompilerOptions::kDefaultGenerateDebugInfo,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010078 /* implicit_null_checks */ true,
79 /* implicit_so_checks */ true,
80 /* implicit_suspend_checks */ false,
81 /* pic */ true, // TODO: Support non-PIC in optimizing.
82 /* verbose_methods */ nullptr,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080083 pass_manager_options,
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010084 /* init_failure_output */ nullptr,
85 /* abort_on_hard_verifier_failure */ false));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086 const InstructionSet instruction_set = kRuntimeISA;
87 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
88 cumulative_logger_.reset(new CumulativeLogger("jit times"));
89 verification_results_.reset(new VerificationResults(compiler_options_.get()));
90 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
91 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070092 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -070093 CompilerCallbacks::CallbackMode::kCompileApp));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080094 compiler_driver_.reset(new CompilerDriver(
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +010095 compiler_options_.get(),
96 verification_results_.get(),
97 method_inliner_map_.get(),
98 Compiler::kOptimizing,
99 instruction_set,
100 instruction_set_features_.get(),
101 /* image */ false,
102 /* image_classes */ nullptr,
103 /* compiled_classes */ nullptr,
104 /* compiled_methods */ nullptr,
105 /* thread_count */ 1,
106 /* dump_stats */ false,
107 /* dump_passes */ false,
108 /* dump_cfg_file_name */ "",
109 cumulative_logger_.get(),
110 /* swap_fd */ -1,
111 /* profile_file */ ""));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800112 // Disable dedupe so we can remove compiled methods.
113 compiler_driver_->SetDedupeEnabled(false);
114 compiler_driver_->SetSupportBootImageFixup(false);
115}
116
117JitCompiler::~JitCompiler() {
118}
119
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700121 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700122 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 StackHandleScope<2> hs(self);
124 self->AssertNoPendingException();
125 Runtime* runtime = Runtime::Current();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800126 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
127 VLOG(jit) << "Already compiled " << PrettyMethod(method);
128 return true; // Already compiled
129 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700131 {
132 TimingLogger::ScopedTiming t2("Initializing", &logger);
133 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700135 return false;
136 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800137 }
138 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700139 MethodReference method_ref(dex_file, method->GetDexMethodIndex());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800140 // Only verify if we don't already have verification results.
141 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700142 TimingLogger::ScopedTiming t2("Verifying", &logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800143 std::string error;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 if (verifier::MethodVerifier::VerifyMethod(method, true, &error) ==
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800145 verifier::MethodVerifier::kHardFailure) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 VLOG(jit) << "Not compile method " << PrettyMethod(method)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800147 << " due to verification failure " << error;
148 return false;
149 }
150 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700151 CompiledMethod* compiled_method = nullptr;
152 {
153 TimingLogger::ScopedTiming t2("Compiling", &logger);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 compiled_method = compiler_driver_->CompileMethod(self, method);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700155 }
156 {
157 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
158 // Trim maps to reduce memory usage, TODO: measure how much this increases compile time.
159 runtime->GetArenaPool()->TrimMaps();
160 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800161 if (compiled_method == nullptr) {
162 return false;
163 }
164 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800165 // Don't add the method if we are supposed to be deoptimized.
166 bool result = false;
167 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800169 if (code != nullptr) {
170 // Already have some compiled code, just use this instead of linking.
171 // TODO: Fix recompilation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700172 method->SetEntryPointFromQuickCompiledCode(code);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800173 result = true;
174 } else {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700175 TimingLogger::ScopedTiming t2("MakeExecutable", &logger);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 result = MakeExecutable(compiled_method, method);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800177 }
178 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800179 // Remove the compiled method to save memory.
180 compiler_driver_->RemoveCompiledMethod(method_ref);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700181 runtime->GetJit()->AddTimingLogger(logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 return result;
183}
184
185CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
186 return callbacks_.get();
187}
188
189uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
190 uint8_t* reserve_begin, uint8_t* reserve_end,
191 const uint8_t* mapping_table,
192 const uint8_t* vmap_table,
193 const uint8_t* gc_map) {
194 reserve_begin += sizeof(OatQuickMethodHeader);
195 reserve_begin = reinterpret_cast<uint8_t*>(
196 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
197 const auto* quick_code = compiled_method->GetQuickCode();
198 CHECK_LE(reserve_begin, reserve_end);
199 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
200 auto* code_ptr = reserve_begin;
201 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
202 // Construct the header last.
203 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
204 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
205 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
206 const auto code_size = quick_code->size();
207 CHECK_NE(code_size, 0U);
208 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
209 // After we are done writing we need to update the method header.
210 // Write out the method header last.
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100211 method_header = new(method_header) OatQuickMethodHeader(
212 (mapping_table == nullptr) ? 0 : code_ptr - mapping_table,
213 (vmap_table == nullptr) ? 0 : code_ptr - vmap_table,
214 (gc_map == nullptr) ? 0 : code_ptr - gc_map,
215 frame_size_in_bytes,
216 core_spill_mask,
217 fp_spill_mask,
218 code_size);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800219 // Return the code ptr.
220 return code_ptr;
221}
222
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223bool JitCompiler::AddToCodeCache(ArtMethod* method, const CompiledMethod* compiled_method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800224 OatFile::OatMethod* out_method) {
225 Runtime* runtime = Runtime::Current();
226 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
227 const auto* quick_code = compiled_method->GetQuickCode();
228 if (quick_code == nullptr) {
229 return false;
230 }
231 const auto code_size = quick_code->size();
232 Thread* const self = Thread::Current();
233 const uint8_t* base = code_cache->CodeCachePtr();
234 auto* const mapping_table = compiled_method->GetMappingTable();
235 auto* const vmap_table = compiled_method->GetVmapTable();
236 auto* const gc_map = compiled_method->GetGcMap();
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100237 uint8_t* mapping_table_ptr = nullptr;
238 uint8_t* vmap_table_ptr = nullptr;
239 uint8_t* gc_map_ptr = nullptr;
240
241 if (mapping_table != nullptr) {
242 // Write out pre-header stuff.
243 mapping_table_ptr = code_cache->AddDataArray(
244 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
245 if (mapping_table_ptr == nullptr) {
246 return false; // Out of data cache.
247 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800248 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100249
250 if (vmap_table != nullptr) {
251 vmap_table_ptr = code_cache->AddDataArray(
252 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
253 if (vmap_table_ptr == nullptr) {
254 return false; // Out of data cache.
255 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800256 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100257
258 if (gc_map != nullptr) {
259 gc_map_ptr = code_cache->AddDataArray(
260 self, gc_map->data(), gc_map->data() + gc_map->size());
261 if (gc_map_ptr == nullptr) {
262 return false; // Out of data cache.
263 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800264 }
Nicolas Geoffray7a4d0152015-07-10 17:29:39 +0100265
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800266 // Don't touch this until you protect / unprotect the code.
267 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
268 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
269 if (code_reserve == nullptr) {
270 return false;
271 }
272 auto* code_ptr = WriteMethodHeaderAndCode(
273 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
274 vmap_table_ptr, gc_map_ptr);
275
Mathieu Chartier5783a742015-06-01 19:12:36 -0700276 __builtin___clear_cache(reinterpret_cast<char*>(code_ptr),
277 reinterpret_cast<char*>(code_ptr + quick_code->size()));
278
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800279 const size_t thumb_offset = compiled_method->CodeDelta();
280 const uint32_t code_offset = code_ptr - base + thumb_offset;
281 *out_method = OatFile::OatMethod(base, code_offset);
282 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
283 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
284 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
285 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
286 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
287 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
288 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
289 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
290 << "," << reinterpret_cast<void*>(code_ptr + code_size);
291 return true;
292}
293
Mathieu Chartiere401d142015-04-22 13:56:20 -0700294bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, ArtMethod* method) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800295 CHECK(method != nullptr);
296 CHECK(compiled_method != nullptr);
297 OatFile::OatMethod oat_method(nullptr, 0);
298 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
299 return false;
300 }
301 // TODO: Flush instruction cache.
302 oat_method.LinkMethod(method);
303 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
304 << PrettyMethod(method);
305 return true;
306}
307
308} // namespace jit
309} // namespace art