blob: a1d8226f362bd02e7917f9e3e8bec5e9c12c40e4 [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 Chartier3d21bdf2015-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 Marko41b175a2015-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 Chartier3d21bdf2015-04-22 13:56:20 -070057extern "C" bool jit_compile_method(void* handle, ArtMethod* method, Thread* self)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
59 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,
74 false,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080075 CompilerOptions::kDefaultTopKProfileThreshold,
Andreas Gampe7b2f09e2015-03-02 14:07:33 -080076 false, // TODO: Think about debuggability of JIT-compiled code.
David Srbeckya26cb572015-04-22 18:57:06 -070077 CompilerOptions::kDefaultGenerateDebugInfo,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080078 false,
79 false,
David Srbecky8dc73242015-04-12 11:40:39 +010080 false,
Mathieu Chartierdce71f32015-02-27 14:24:37 -080081 false, // pic
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080082 nullptr,
83 pass_manager_options,
Andreas Gampe6cf49e52015-03-05 13:08:45 -080084 nullptr,
85 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(
95 compiler_options_.get(), verification_results_.get(), method_inliner_map_.get(),
96 Compiler::kQuick, instruction_set, instruction_set_features_.get(), false,
Andreas Gampe70bef0d2015-04-15 02:37:28 -070097 nullptr, nullptr, nullptr, 1, false, true,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080098 std::string(), cumulative_logger_.get(), -1, std::string()));
99 // Disable dedupe so we can remove compiled methods.
100 compiler_driver_->SetDedupeEnabled(false);
101 compiler_driver_->SetSupportBootImageFixup(false);
102}
103
104JitCompiler::~JitCompiler() {
105}
106
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700107bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700108 TimingLogger logger("JIT compiler timing logger", true, VLOG_IS_ON(jit));
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700109 const uint64_t start_time = NanoTime();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800110 StackHandleScope<2> hs(self);
111 self->AssertNoPendingException();
112 Runtime* runtime = Runtime::Current();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800113 if (runtime->GetJit()->GetCodeCache()->ContainsMethod(method)) {
114 VLOG(jit) << "Already compiled " << PrettyMethod(method);
115 return true; // Already compiled
116 }
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700117 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700118 {
119 TimingLogger::ScopedTiming t2("Initializing", &logger);
120 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700121 VLOG(jit) << "JIT failed to initialize " << PrettyMethod(method);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700122 return false;
123 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 }
125 const DexFile* dex_file = h_class->GetDexCache()->GetDexFile();
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700126 MethodReference method_ref(dex_file, method->GetDexMethodIndex());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800127 // Only verify if we don't already have verification results.
128 if (verification_results_->GetVerifiedMethod(method_ref) == nullptr) {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700129 TimingLogger::ScopedTiming t2("Verifying", &logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800130 std::string error;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700131 if (verifier::MethodVerifier::VerifyMethod(method, true, &error) ==
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800132 verifier::MethodVerifier::kHardFailure) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700133 VLOG(jit) << "Not compile method " << PrettyMethod(method)
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800134 << " due to verification failure " << error;
135 return false;
136 }
137 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700138 CompiledMethod* compiled_method = nullptr;
139 {
140 TimingLogger::ScopedTiming t2("Compiling", &logger);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700141 compiled_method = compiler_driver_->CompileMethod(self, method);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700142 }
143 {
144 TimingLogger::ScopedTiming t2("TrimMaps", &logger);
145 // Trim maps to reduce memory usage, TODO: measure how much this increases compile time.
146 runtime->GetArenaPool()->TrimMaps();
147 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800148 if (compiled_method == nullptr) {
149 return false;
150 }
151 total_time_ += NanoTime() - start_time;
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800152 // Don't add the method if we are supposed to be deoptimized.
153 bool result = false;
154 if (!runtime->GetInstrumentation()->AreAllMethodsDeoptimized()) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700155 const void* code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(method);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800156 if (code != nullptr) {
157 // Already have some compiled code, just use this instead of linking.
158 // TODO: Fix recompilation.
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700159 method->SetEntryPointFromQuickCompiledCode(code);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800160 result = true;
161 } else {
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700162 TimingLogger::ScopedTiming t2("MakeExecutable", &logger);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700163 result = MakeExecutable(compiled_method, method);
Mathieu Chartierc0d5f892015-02-25 13:22:57 -0800164 }
165 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166 // Remove the compiled method to save memory.
167 compiler_driver_->RemoveCompiledMethod(method_ref);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700168 runtime->GetJit()->AddTimingLogger(logger);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800169 return result;
170}
171
172CompilerCallbacks* JitCompiler::GetCompilerCallbacks() const {
173 return callbacks_.get();
174}
175
176uint8_t* JitCompiler::WriteMethodHeaderAndCode(const CompiledMethod* compiled_method,
177 uint8_t* reserve_begin, uint8_t* reserve_end,
178 const uint8_t* mapping_table,
179 const uint8_t* vmap_table,
180 const uint8_t* gc_map) {
181 reserve_begin += sizeof(OatQuickMethodHeader);
182 reserve_begin = reinterpret_cast<uint8_t*>(
183 compiled_method->AlignCode(reinterpret_cast<uintptr_t>(reserve_begin)));
184 const auto* quick_code = compiled_method->GetQuickCode();
185 CHECK_LE(reserve_begin, reserve_end);
186 CHECK_LE(quick_code->size(), static_cast<size_t>(reserve_end - reserve_begin));
187 auto* code_ptr = reserve_begin;
188 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
189 // Construct the header last.
190 const auto frame_size_in_bytes = compiled_method->GetFrameSizeInBytes();
191 const auto core_spill_mask = compiled_method->GetCoreSpillMask();
192 const auto fp_spill_mask = compiled_method->GetFpSpillMask();
193 const auto code_size = quick_code->size();
194 CHECK_NE(code_size, 0U);
195 std::copy(quick_code->data(), quick_code->data() + code_size, code_ptr);
196 // After we are done writing we need to update the method header.
197 // Write out the method header last.
198 method_header = new(method_header)OatQuickMethodHeader(
199 code_ptr - mapping_table, code_ptr - vmap_table, code_ptr - gc_map, frame_size_in_bytes,
200 core_spill_mask, fp_spill_mask, code_size);
201 // Return the code ptr.
202 return code_ptr;
203}
204
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700205bool JitCompiler::AddToCodeCache(ArtMethod* method, const CompiledMethod* compiled_method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800206 OatFile::OatMethod* out_method) {
207 Runtime* runtime = Runtime::Current();
208 JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
209 const auto* quick_code = compiled_method->GetQuickCode();
210 if (quick_code == nullptr) {
211 return false;
212 }
213 const auto code_size = quick_code->size();
214 Thread* const self = Thread::Current();
215 const uint8_t* base = code_cache->CodeCachePtr();
216 auto* const mapping_table = compiled_method->GetMappingTable();
217 auto* const vmap_table = compiled_method->GetVmapTable();
218 auto* const gc_map = compiled_method->GetGcMap();
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700219 CHECK(gc_map != nullptr) << PrettyMethod(method);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800220 // Write out pre-header stuff.
221 uint8_t* const mapping_table_ptr = code_cache->AddDataArray(
222 self, mapping_table->data(), mapping_table->data() + mapping_table->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700223 if (mapping_table_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800224 return false; // Out of data cache.
225 }
226 uint8_t* const vmap_table_ptr = code_cache->AddDataArray(
227 self, vmap_table->data(), vmap_table->data() + vmap_table->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700228 if (vmap_table_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800229 return false; // Out of data cache.
230 }
231 uint8_t* const gc_map_ptr = code_cache->AddDataArray(
232 self, gc_map->data(), gc_map->data() + gc_map->size());
Mathieu Chartierb4e18082015-03-22 13:52:48 -0700233 if (gc_map_ptr == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800234 return false; // Out of data cache.
235 }
236 // Don't touch this until you protect / unprotect the code.
237 const size_t reserve_size = sizeof(OatQuickMethodHeader) + quick_code->size() + 32;
238 uint8_t* const code_reserve = code_cache->ReserveCode(self, reserve_size);
239 if (code_reserve == nullptr) {
240 return false;
241 }
242 auto* code_ptr = WriteMethodHeaderAndCode(
243 compiled_method, code_reserve, code_reserve + reserve_size, mapping_table_ptr,
244 vmap_table_ptr, gc_map_ptr);
245
Mathieu Chartier2791d5e2015-06-01 19:12:36 -0700246 __builtin___clear_cache(reinterpret_cast<char*>(code_ptr),
247 reinterpret_cast<char*>(code_ptr + quick_code->size()));
248
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800249 const size_t thumb_offset = compiled_method->CodeDelta();
250 const uint32_t code_offset = code_ptr - base + thumb_offset;
251 *out_method = OatFile::OatMethod(base, code_offset);
252 DCHECK_EQ(out_method->GetGcMap(), gc_map_ptr);
253 DCHECK_EQ(out_method->GetMappingTable(), mapping_table_ptr);
254 DCHECK_EQ(out_method->GetVmapTable(), vmap_table_ptr);
255 DCHECK_EQ(out_method->GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
256 DCHECK_EQ(out_method->GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
257 DCHECK_EQ(out_method->GetFpSpillMask(), compiled_method->GetFpSpillMask());
258 VLOG(jit) << "JIT added " << PrettyMethod(method) << "@" << method << " ccache_size="
259 << PrettySize(code_cache->CodeCacheSize()) << ": " << reinterpret_cast<void*>(code_ptr)
260 << "," << reinterpret_cast<void*>(code_ptr + code_size);
261 return true;
262}
263
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700264bool JitCompiler::MakeExecutable(CompiledMethod* compiled_method, ArtMethod* method) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800265 CHECK(method != nullptr);
266 CHECK(compiled_method != nullptr);
267 OatFile::OatMethod oat_method(nullptr, 0);
268 if (!AddToCodeCache(method, compiled_method, &oat_method)) {
269 return false;
270 }
271 // TODO: Flush instruction cache.
272 oat_method.LinkMethod(method);
273 CHECK(Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method))
274 << PrettyMethod(method);
275 return true;
276}
277
278} // namespace jit
279} // namespace art